GNU Linux-libre 4.19.263-gnu1
[releases.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 static u32 clip_scaled(u32 src, u32 dst, u32 clip)
54 {
55         u64 tmp;
56
57         if (dst == 0)
58                 return 0;
59
60         tmp = mul_u32_u32(src, dst - clip);
61
62         /*
63          * Round toward 1.0 when clipping so that we don't accidentally
64          * change upscaling to downscaling or vice versa.
65          */
66         if (src < (dst << 16))
67                 return DIV_ROUND_UP_ULL(tmp, dst);
68         else
69                 return DIV_ROUND_DOWN_ULL(tmp, dst);
70 }
71
72 /**
73  * drm_rect_clip_scaled - perform a scaled clip operation
74  * @src: source window rectangle
75  * @dst: destination window rectangle
76  * @clip: clip rectangle
77  *
78  * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
79  * same amounts multiplied by @hscale and @vscale.
80  *
81  * RETURNS:
82  * %true if rectangle @dst is still visible after being clipped,
83  * %false otherwise
84  */
85 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
86                           const struct drm_rect *clip)
87 {
88         int diff;
89
90         diff = clip->x1 - dst->x1;
91         if (diff > 0) {
92                 u32 new_src_w = clip_scaled(drm_rect_width(src),
93                                             drm_rect_width(dst), diff);
94
95                 src->x1 = clamp_t(int64_t, src->x2 - new_src_w, INT_MIN, INT_MAX);
96                 dst->x1 = clip->x1;
97         }
98         diff = clip->y1 - dst->y1;
99         if (diff > 0) {
100                 u32 new_src_h = clip_scaled(drm_rect_height(src),
101                                             drm_rect_height(dst), diff);
102
103                 src->y1 = clamp_t(int64_t, src->y2 - new_src_h, INT_MIN, INT_MAX);
104                 dst->y1 = clip->y1;
105         }
106         diff = dst->x2 - clip->x2;
107         if (diff > 0) {
108                 u32 new_src_w = clip_scaled(drm_rect_width(src),
109                                             drm_rect_width(dst), diff);
110
111                 src->x2 = clamp_t(int64_t, src->x1 + new_src_w, INT_MIN, INT_MAX);
112                 dst->x2 = clip->x2;
113         }
114         diff = dst->y2 - clip->y2;
115         if (diff > 0) {
116                 u32 new_src_h = clip_scaled(drm_rect_height(src),
117                                             drm_rect_height(dst), diff);
118
119                 src->y2 = clamp_t(int64_t, src->y1 + new_src_h, INT_MIN, INT_MAX);
120                 dst->y2 = clip->y2;
121         }
122
123         return drm_rect_visible(dst);
124 }
125 EXPORT_SYMBOL(drm_rect_clip_scaled);
126
127 static int drm_calc_scale(int src, int dst)
128 {
129         int scale = 0;
130
131         if (WARN_ON(src < 0 || dst < 0))
132                 return -EINVAL;
133
134         if (dst == 0)
135                 return 0;
136
137         if (src > (dst << 16))
138                 return DIV_ROUND_UP(src, dst);
139         else
140                 scale = src / dst;
141
142         return scale;
143 }
144
145 /**
146  * drm_rect_calc_hscale - calculate the horizontal scaling factor
147  * @src: source window rectangle
148  * @dst: destination window rectangle
149  * @min_hscale: minimum allowed horizontal scaling factor
150  * @max_hscale: maximum allowed horizontal scaling factor
151  *
152  * Calculate the horizontal scaling factor as
153  * (@src width) / (@dst width).
154  *
155  * If the scale is below 1 << 16, round down. If the scale is above
156  * 1 << 16, round up. This will calculate the scale with the most
157  * pessimistic limit calculation.
158  *
159  * RETURNS:
160  * The horizontal scaling factor, or errno of out of limits.
161  */
162 int drm_rect_calc_hscale(const struct drm_rect *src,
163                          const struct drm_rect *dst,
164                          int min_hscale, int max_hscale)
165 {
166         int src_w = drm_rect_width(src);
167         int dst_w = drm_rect_width(dst);
168         int hscale = drm_calc_scale(src_w, dst_w);
169
170         if (hscale < 0 || dst_w == 0)
171                 return hscale;
172
173         if (hscale < min_hscale || hscale > max_hscale)
174                 return -ERANGE;
175
176         return hscale;
177 }
178 EXPORT_SYMBOL(drm_rect_calc_hscale);
179
180 /**
181  * drm_rect_calc_vscale - calculate the vertical scaling factor
182  * @src: source window rectangle
183  * @dst: destination window rectangle
184  * @min_vscale: minimum allowed vertical scaling factor
185  * @max_vscale: maximum allowed vertical scaling factor
186  *
187  * Calculate the vertical scaling factor as
188  * (@src height) / (@dst height).
189  *
190  * If the scale is below 1 << 16, round down. If the scale is above
191  * 1 << 16, round up. This will calculate the scale with the most
192  * pessimistic limit calculation.
193  *
194  * RETURNS:
195  * The vertical scaling factor, or errno of out of limits.
196  */
197 int drm_rect_calc_vscale(const struct drm_rect *src,
198                          const struct drm_rect *dst,
199                          int min_vscale, int max_vscale)
200 {
201         int src_h = drm_rect_height(src);
202         int dst_h = drm_rect_height(dst);
203         int vscale = drm_calc_scale(src_h, dst_h);
204
205         if (vscale < 0 || dst_h == 0)
206                 return vscale;
207
208         if (vscale < min_vscale || vscale > max_vscale)
209                 return -ERANGE;
210
211         return vscale;
212 }
213 EXPORT_SYMBOL(drm_rect_calc_vscale);
214
215 /**
216  * drm_calc_hscale_relaxed - calculate the horizontal scaling factor
217  * @src: source window rectangle
218  * @dst: destination window rectangle
219  * @min_hscale: minimum allowed horizontal scaling factor
220  * @max_hscale: maximum allowed horizontal scaling factor
221  *
222  * Calculate the horizontal scaling factor as
223  * (@src width) / (@dst width).
224  *
225  * If the calculated scaling factor is below @min_vscale,
226  * decrease the height of rectangle @dst to compensate.
227  *
228  * If the calculated scaling factor is above @max_vscale,
229  * decrease the height of rectangle @src to compensate.
230  *
231  * If the scale is below 1 << 16, round down. If the scale is above
232  * 1 << 16, round up. This will calculate the scale with the most
233  * pessimistic limit calculation.
234  *
235  * RETURNS:
236  * The horizontal scaling factor.
237  */
238 int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
239                                  struct drm_rect *dst,
240                                  int min_hscale, int max_hscale)
241 {
242         int src_w = drm_rect_width(src);
243         int dst_w = drm_rect_width(dst);
244         int hscale = drm_calc_scale(src_w, dst_w);
245
246         if (hscale < 0 || dst_w == 0)
247                 return hscale;
248
249         if (hscale < min_hscale) {
250                 int max_dst_w = src_w / min_hscale;
251
252                 drm_rect_adjust_size(dst, max_dst_w - dst_w, 0);
253
254                 return min_hscale;
255         }
256
257         if (hscale > max_hscale) {
258                 int max_src_w = dst_w * max_hscale;
259
260                 drm_rect_adjust_size(src, max_src_w - src_w, 0);
261
262                 return max_hscale;
263         }
264
265         return hscale;
266 }
267 EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed);
268
269 /**
270  * drm_rect_calc_vscale_relaxed - calculate the vertical scaling factor
271  * @src: source window rectangle
272  * @dst: destination window rectangle
273  * @min_vscale: minimum allowed vertical scaling factor
274  * @max_vscale: maximum allowed vertical scaling factor
275  *
276  * Calculate the vertical scaling factor as
277  * (@src height) / (@dst height).
278  *
279  * If the calculated scaling factor is below @min_vscale,
280  * decrease the height of rectangle @dst to compensate.
281  *
282  * If the calculated scaling factor is above @max_vscale,
283  * decrease the height of rectangle @src to compensate.
284  *
285  * If the scale is below 1 << 16, round down. If the scale is above
286  * 1 << 16, round up. This will calculate the scale with the most
287  * pessimistic limit calculation.
288  *
289  * RETURNS:
290  * The vertical scaling factor.
291  */
292 int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
293                                  struct drm_rect *dst,
294                                  int min_vscale, int max_vscale)
295 {
296         int src_h = drm_rect_height(src);
297         int dst_h = drm_rect_height(dst);
298         int vscale = drm_calc_scale(src_h, dst_h);
299
300         if (vscale < 0 || dst_h == 0)
301                 return vscale;
302
303         if (vscale < min_vscale) {
304                 int max_dst_h = src_h / min_vscale;
305
306                 drm_rect_adjust_size(dst, 0, max_dst_h - dst_h);
307
308                 return min_vscale;
309         }
310
311         if (vscale > max_vscale) {
312                 int max_src_h = dst_h * max_vscale;
313
314                 drm_rect_adjust_size(src, 0, max_src_h - src_h);
315
316                 return max_vscale;
317         }
318
319         return vscale;
320 }
321 EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed);
322
323 /**
324  * drm_rect_debug_print - print the rectangle information
325  * @prefix: prefix string
326  * @r: rectangle to print
327  * @fixed_point: rectangle is in 16.16 fixed point format
328  */
329 void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
330 {
331         if (fixed_point)
332                 DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
333         else
334                 DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
335 }
336 EXPORT_SYMBOL(drm_rect_debug_print);
337
338 /**
339  * drm_rect_rotate - Rotate the rectangle
340  * @r: rectangle to be rotated
341  * @width: Width of the coordinate space
342  * @height: Height of the coordinate space
343  * @rotation: Transformation to be applied
344  *
345  * Apply @rotation to the coordinates of rectangle @r.
346  *
347  * @width and @height combined with @rotation define
348  * the location of the new origin.
349  *
350  * @width correcsponds to the horizontal and @height
351  * to the vertical axis of the untransformed coordinate
352  * space.
353  */
354 void drm_rect_rotate(struct drm_rect *r,
355                      int width, int height,
356                      unsigned int rotation)
357 {
358         struct drm_rect tmp;
359
360         if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
361                 tmp = *r;
362
363                 if (rotation & DRM_MODE_REFLECT_X) {
364                         r->x1 = width - tmp.x2;
365                         r->x2 = width - tmp.x1;
366                 }
367
368                 if (rotation & DRM_MODE_REFLECT_Y) {
369                         r->y1 = height - tmp.y2;
370                         r->y2 = height - tmp.y1;
371                 }
372         }
373
374         switch (rotation & DRM_MODE_ROTATE_MASK) {
375         case DRM_MODE_ROTATE_0:
376                 break;
377         case DRM_MODE_ROTATE_90:
378                 tmp = *r;
379                 r->x1 = tmp.y1;
380                 r->x2 = tmp.y2;
381                 r->y1 = width - tmp.x2;
382                 r->y2 = width - tmp.x1;
383                 break;
384         case DRM_MODE_ROTATE_180:
385                 tmp = *r;
386                 r->x1 = width - tmp.x2;
387                 r->x2 = width - tmp.x1;
388                 r->y1 = height - tmp.y2;
389                 r->y2 = height - tmp.y1;
390                 break;
391         case DRM_MODE_ROTATE_270:
392                 tmp = *r;
393                 r->x1 = height - tmp.y2;
394                 r->x2 = height - tmp.y1;
395                 r->y1 = tmp.x1;
396                 r->y2 = tmp.x2;
397                 break;
398         default:
399                 break;
400         }
401 }
402 EXPORT_SYMBOL(drm_rect_rotate);
403
404 /**
405  * drm_rect_rotate_inv - Inverse rotate the rectangle
406  * @r: rectangle to be rotated
407  * @width: Width of the coordinate space
408  * @height: Height of the coordinate space
409  * @rotation: Transformation whose inverse is to be applied
410  *
411  * Apply the inverse of @rotation to the coordinates
412  * of rectangle @r.
413  *
414  * @width and @height combined with @rotation define
415  * the location of the new origin.
416  *
417  * @width correcsponds to the horizontal and @height
418  * to the vertical axis of the original untransformed
419  * coordinate space, so that you never have to flip
420  * them when doing a rotatation and its inverse.
421  * That is, if you do ::
422  *
423  *     drm_rect_rotate(&r, width, height, rotation);
424  *     drm_rect_rotate_inv(&r, width, height, rotation);
425  *
426  * you will always get back the original rectangle.
427  */
428 void drm_rect_rotate_inv(struct drm_rect *r,
429                          int width, int height,
430                          unsigned int rotation)
431 {
432         struct drm_rect tmp;
433
434         switch (rotation & DRM_MODE_ROTATE_MASK) {
435         case DRM_MODE_ROTATE_0:
436                 break;
437         case DRM_MODE_ROTATE_90:
438                 tmp = *r;
439                 r->x1 = width - tmp.y2;
440                 r->x2 = width - tmp.y1;
441                 r->y1 = tmp.x1;
442                 r->y2 = tmp.x2;
443                 break;
444         case DRM_MODE_ROTATE_180:
445                 tmp = *r;
446                 r->x1 = width - tmp.x2;
447                 r->x2 = width - tmp.x1;
448                 r->y1 = height - tmp.y2;
449                 r->y2 = height - tmp.y1;
450                 break;
451         case DRM_MODE_ROTATE_270:
452                 tmp = *r;
453                 r->x1 = tmp.y1;
454                 r->x2 = tmp.y2;
455                 r->y1 = height - tmp.x2;
456                 r->y2 = height - tmp.x1;
457                 break;
458         default:
459                 break;
460         }
461
462         if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
463                 tmp = *r;
464
465                 if (rotation & DRM_MODE_REFLECT_X) {
466                         r->x1 = width - tmp.x2;
467                         r->x2 = width - tmp.x1;
468                 }
469
470                 if (rotation & DRM_MODE_REFLECT_Y) {
471                         r->y1 = height - tmp.y2;
472                         r->y2 = height - tmp.y1;
473                 }
474         }
475 }
476 EXPORT_SYMBOL(drm_rect_rotate_inv);