drm/rect: Round above 1 << 16 upwards to correct scale calculation functions.
[sfrench/cifs-2.6.git] / drivers / gpu / drm / drm_rect.c
1 /*
2  * Copyright (C) 2011-2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 #include <linux/errno.h>
25 #include <linux/export.h>
26 #include <linux/kernel.h>
27 #include <drm/drmP.h>
28 #include <drm/drm_rect.h>
29
30 /**
31  * drm_rect_intersect - intersect two rectangles
32  * @r1: first rectangle
33  * @r2: second rectangle
34  *
35  * Calculate the intersection of rectangles @r1 and @r2.
36  * @r1 will be overwritten with the intersection.
37  *
38  * RETURNS:
39  * %true if rectangle @r1 is still visible after the operation,
40  * %false otherwise.
41  */
42 bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
43 {
44         r1->x1 = max(r1->x1, r2->x1);
45         r1->y1 = max(r1->y1, r2->y1);
46         r1->x2 = min(r1->x2, r2->x2);
47         r1->y2 = min(r1->y2, r2->y2);
48
49         return drm_rect_visible(r1);
50 }
51 EXPORT_SYMBOL(drm_rect_intersect);
52
53 /**
54  * drm_rect_clip_scaled - perform a scaled clip operation
55  * @src: source window rectangle
56  * @dst: destination window rectangle
57  * @clip: clip rectangle
58  * @hscale: horizontal scaling factor
59  * @vscale: vertical scaling factor
60  *
61  * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
62  * same amounts multiplied by @hscale and @vscale.
63  *
64  * RETURNS:
65  * %true if rectangle @dst is still visible after being clipped,
66  * %false otherwise
67  */
68 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
69                           const struct drm_rect *clip,
70                           int hscale, int vscale)
71 {
72         int diff;
73
74         diff = clip->x1 - dst->x1;
75         if (diff > 0) {
76                 int64_t tmp = src->x1 + (int64_t) diff * hscale;
77                 src->x1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
78         }
79         diff = clip->y1 - dst->y1;
80         if (diff > 0) {
81                 int64_t tmp = src->y1 + (int64_t) diff * vscale;
82                 src->y1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
83         }
84         diff = dst->x2 - clip->x2;
85         if (diff > 0) {
86                 int64_t tmp = src->x2 - (int64_t) diff * hscale;
87                 src->x2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
88         }
89         diff = dst->y2 - clip->y2;
90         if (diff > 0) {
91                 int64_t tmp = src->y2 - (int64_t) diff * vscale;
92                 src->y2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
93         }
94
95         return drm_rect_intersect(dst, clip);
96 }
97 EXPORT_SYMBOL(drm_rect_clip_scaled);
98
99 static int drm_calc_scale(int src, int dst)
100 {
101         int scale = 0;
102
103         if (WARN_ON(src < 0 || dst < 0))
104                 return -EINVAL;
105
106         if (dst == 0)
107                 return 0;
108
109         if (src > (dst << 16))
110                 return DIV_ROUND_UP(src, dst);
111         else
112                 scale = src / dst;
113
114         return scale;
115 }
116
117 /**
118  * drm_rect_calc_hscale - calculate the horizontal scaling factor
119  * @src: source window rectangle
120  * @dst: destination window rectangle
121  * @min_hscale: minimum allowed horizontal scaling factor
122  * @max_hscale: maximum allowed horizontal scaling factor
123  *
124  * Calculate the horizontal scaling factor as
125  * (@src width) / (@dst width).
126  *
127  * If the scale is below 1 << 16, round down. If the scale is above
128  * 1 << 16, round up. This will calculate the scale with the most
129  * pessimistic limit calculation.
130  *
131  * RETURNS:
132  * The horizontal scaling factor, or errno of out of limits.
133  */
134 int drm_rect_calc_hscale(const struct drm_rect *src,
135                          const struct drm_rect *dst,
136                          int min_hscale, int max_hscale)
137 {
138         int src_w = drm_rect_width(src);
139         int dst_w = drm_rect_width(dst);
140         int hscale = drm_calc_scale(src_w, dst_w);
141
142         if (hscale < 0 || dst_w == 0)
143                 return hscale;
144
145         if (hscale < min_hscale || hscale > max_hscale)
146                 return -ERANGE;
147
148         return hscale;
149 }
150 EXPORT_SYMBOL(drm_rect_calc_hscale);
151
152 /**
153  * drm_rect_calc_vscale - calculate the vertical scaling factor
154  * @src: source window rectangle
155  * @dst: destination window rectangle
156  * @min_vscale: minimum allowed vertical scaling factor
157  * @max_vscale: maximum allowed vertical scaling factor
158  *
159  * Calculate the vertical scaling factor as
160  * (@src height) / (@dst height).
161  *
162  * If the scale is below 1 << 16, round down. If the scale is above
163  * 1 << 16, round up. This will calculate the scale with the most
164  * pessimistic limit calculation.
165  *
166  * RETURNS:
167  * The vertical scaling factor, or errno of out of limits.
168  */
169 int drm_rect_calc_vscale(const struct drm_rect *src,
170                          const struct drm_rect *dst,
171                          int min_vscale, int max_vscale)
172 {
173         int src_h = drm_rect_height(src);
174         int dst_h = drm_rect_height(dst);
175         int vscale = drm_calc_scale(src_h, dst_h);
176
177         if (vscale < 0 || dst_h == 0)
178                 return vscale;
179
180         if (vscale < min_vscale || vscale > max_vscale)
181                 return -ERANGE;
182
183         return vscale;
184 }
185 EXPORT_SYMBOL(drm_rect_calc_vscale);
186
187 /**
188  * drm_calc_hscale_relaxed - calculate the horizontal scaling factor
189  * @src: source window rectangle
190  * @dst: destination window rectangle
191  * @min_hscale: minimum allowed horizontal scaling factor
192  * @max_hscale: maximum allowed horizontal scaling factor
193  *
194  * Calculate the horizontal scaling factor as
195  * (@src width) / (@dst width).
196  *
197  * If the calculated scaling factor is below @min_vscale,
198  * decrease the height of rectangle @dst to compensate.
199  *
200  * If the calculated scaling factor is above @max_vscale,
201  * decrease the height of rectangle @src to compensate.
202  *
203  * If the scale is below 1 << 16, round down. If the scale is above
204  * 1 << 16, round up. This will calculate the scale with the most
205  * pessimistic limit calculation.
206  *
207  * RETURNS:
208  * The horizontal scaling factor.
209  */
210 int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
211                                  struct drm_rect *dst,
212                                  int min_hscale, int max_hscale)
213 {
214         int src_w = drm_rect_width(src);
215         int dst_w = drm_rect_width(dst);
216         int hscale = drm_calc_scale(src_w, dst_w);
217
218         if (hscale < 0 || dst_w == 0)
219                 return hscale;
220
221         if (hscale < min_hscale) {
222                 int max_dst_w = src_w / min_hscale;
223
224                 drm_rect_adjust_size(dst, max_dst_w - dst_w, 0);
225
226                 return min_hscale;
227         }
228
229         if (hscale > max_hscale) {
230                 int max_src_w = dst_w * max_hscale;
231
232                 drm_rect_adjust_size(src, max_src_w - src_w, 0);
233
234                 return max_hscale;
235         }
236
237         return hscale;
238 }
239 EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed);
240
241 /**
242  * drm_rect_calc_vscale_relaxed - calculate the vertical scaling factor
243  * @src: source window rectangle
244  * @dst: destination window rectangle
245  * @min_vscale: minimum allowed vertical scaling factor
246  * @max_vscale: maximum allowed vertical scaling factor
247  *
248  * Calculate the vertical scaling factor as
249  * (@src height) / (@dst height).
250  *
251  * If the calculated scaling factor is below @min_vscale,
252  * decrease the height of rectangle @dst to compensate.
253  *
254  * If the calculated scaling factor is above @max_vscale,
255  * decrease the height of rectangle @src to compensate.
256  *
257  * If the scale is below 1 << 16, round down. If the scale is above
258  * 1 << 16, round up. This will calculate the scale with the most
259  * pessimistic limit calculation.
260  *
261  * RETURNS:
262  * The vertical scaling factor.
263  */
264 int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
265                                  struct drm_rect *dst,
266                                  int min_vscale, int max_vscale)
267 {
268         int src_h = drm_rect_height(src);
269         int dst_h = drm_rect_height(dst);
270         int vscale = drm_calc_scale(src_h, dst_h);
271
272         if (vscale < 0 || dst_h == 0)
273                 return vscale;
274
275         if (vscale < min_vscale) {
276                 int max_dst_h = src_h / min_vscale;
277
278                 drm_rect_adjust_size(dst, 0, max_dst_h - dst_h);
279
280                 return min_vscale;
281         }
282
283         if (vscale > max_vscale) {
284                 int max_src_h = dst_h * max_vscale;
285
286                 drm_rect_adjust_size(src, 0, max_src_h - src_h);
287
288                 return max_vscale;
289         }
290
291         return vscale;
292 }
293 EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed);
294
295 /**
296  * drm_rect_debug_print - print the rectangle information
297  * @prefix: prefix string
298  * @r: rectangle to print
299  * @fixed_point: rectangle is in 16.16 fixed point format
300  */
301 void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
302 {
303         if (fixed_point)
304                 DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
305         else
306                 DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
307 }
308 EXPORT_SYMBOL(drm_rect_debug_print);
309
310 /**
311  * drm_rect_rotate - Rotate the rectangle
312  * @r: rectangle to be rotated
313  * @width: Width of the coordinate space
314  * @height: Height of the coordinate space
315  * @rotation: Transformation to be applied
316  *
317  * Apply @rotation to the coordinates of rectangle @r.
318  *
319  * @width and @height combined with @rotation define
320  * the location of the new origin.
321  *
322  * @width correcsponds to the horizontal and @height
323  * to the vertical axis of the untransformed coordinate
324  * space.
325  */
326 void drm_rect_rotate(struct drm_rect *r,
327                      int width, int height,
328                      unsigned int rotation)
329 {
330         struct drm_rect tmp;
331
332         if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
333                 tmp = *r;
334
335                 if (rotation & DRM_MODE_REFLECT_X) {
336                         r->x1 = width - tmp.x2;
337                         r->x2 = width - tmp.x1;
338                 }
339
340                 if (rotation & DRM_MODE_REFLECT_Y) {
341                         r->y1 = height - tmp.y2;
342                         r->y2 = height - tmp.y1;
343                 }
344         }
345
346         switch (rotation & DRM_MODE_ROTATE_MASK) {
347         case DRM_MODE_ROTATE_0:
348                 break;
349         case DRM_MODE_ROTATE_90:
350                 tmp = *r;
351                 r->x1 = tmp.y1;
352                 r->x2 = tmp.y2;
353                 r->y1 = width - tmp.x2;
354                 r->y2 = width - tmp.x1;
355                 break;
356         case DRM_MODE_ROTATE_180:
357                 tmp = *r;
358                 r->x1 = width - tmp.x2;
359                 r->x2 = width - tmp.x1;
360                 r->y1 = height - tmp.y2;
361                 r->y2 = height - tmp.y1;
362                 break;
363         case DRM_MODE_ROTATE_270:
364                 tmp = *r;
365                 r->x1 = height - tmp.y2;
366                 r->x2 = height - tmp.y1;
367                 r->y1 = tmp.x1;
368                 r->y2 = tmp.x2;
369                 break;
370         default:
371                 break;
372         }
373 }
374 EXPORT_SYMBOL(drm_rect_rotate);
375
376 /**
377  * drm_rect_rotate_inv - Inverse rotate the rectangle
378  * @r: rectangle to be rotated
379  * @width: Width of the coordinate space
380  * @height: Height of the coordinate space
381  * @rotation: Transformation whose inverse is to be applied
382  *
383  * Apply the inverse of @rotation to the coordinates
384  * of rectangle @r.
385  *
386  * @width and @height combined with @rotation define
387  * the location of the new origin.
388  *
389  * @width correcsponds to the horizontal and @height
390  * to the vertical axis of the original untransformed
391  * coordinate space, so that you never have to flip
392  * them when doing a rotatation and its inverse.
393  * That is, if you do ::
394  *
395  *     drm_rect_rotate(&r, width, height, rotation);
396  *     drm_rect_rotate_inv(&r, width, height, rotation);
397  *
398  * you will always get back the original rectangle.
399  */
400 void drm_rect_rotate_inv(struct drm_rect *r,
401                          int width, int height,
402                          unsigned int rotation)
403 {
404         struct drm_rect tmp;
405
406         switch (rotation & DRM_MODE_ROTATE_MASK) {
407         case DRM_MODE_ROTATE_0:
408                 break;
409         case DRM_MODE_ROTATE_90:
410                 tmp = *r;
411                 r->x1 = width - tmp.y2;
412                 r->x2 = width - tmp.y1;
413                 r->y1 = tmp.x1;
414                 r->y2 = tmp.x2;
415                 break;
416         case DRM_MODE_ROTATE_180:
417                 tmp = *r;
418                 r->x1 = width - tmp.x2;
419                 r->x2 = width - tmp.x1;
420                 r->y1 = height - tmp.y2;
421                 r->y2 = height - tmp.y1;
422                 break;
423         case DRM_MODE_ROTATE_270:
424                 tmp = *r;
425                 r->x1 = tmp.y1;
426                 r->x2 = tmp.y2;
427                 r->y1 = height - tmp.x2;
428                 r->y2 = height - tmp.x1;
429                 break;
430         default:
431                 break;
432         }
433
434         if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
435                 tmp = *r;
436
437                 if (rotation & DRM_MODE_REFLECT_X) {
438                         r->x1 = width - tmp.x2;
439                         r->x2 = width - tmp.x1;
440                 }
441
442                 if (rotation & DRM_MODE_REFLECT_Y) {
443                         r->y1 = height - tmp.y2;
444                         r->y2 = height - tmp.y1;
445                 }
446         }
447 }
448 EXPORT_SYMBOL(drm_rect_rotate_inv);