Linux 4.19.133
[linux/fpc-iii.git] / drivers / gpu / drm / drm_rect.c
blob0f5a0c64c4c4aa2776629405c6076c080ef60900
1 /*
2 * Copyright (C) 2011-2013 Intel Corporation
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:
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.
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.
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>
30 /**
31 * drm_rect_intersect - intersect two rectangles
32 * @r1: first rectangle
33 * @r2: second rectangle
35 * Calculate the intersection of rectangles @r1 and @r2.
36 * @r1 will be overwritten with the intersection.
38 * RETURNS:
39 * %true if rectangle @r1 is still visible after the operation,
40 * %false otherwise.
42 bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
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);
49 return drm_rect_visible(r1);
51 EXPORT_SYMBOL(drm_rect_intersect);
53 static u32 clip_scaled(u32 src, u32 dst, u32 clip)
55 u64 tmp;
57 if (dst == 0)
58 return 0;
60 tmp = mul_u32_u32(src, dst - clip);
63 * Round toward 1.0 when clipping so that we don't accidentally
64 * change upscaling to downscaling or vice versa.
66 if (src < (dst << 16))
67 return DIV_ROUND_UP_ULL(tmp, dst);
68 else
69 return DIV_ROUND_DOWN_ULL(tmp, dst);
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
78 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
79 * same amounts multiplied by @hscale and @vscale.
81 * RETURNS:
82 * %true if rectangle @dst is still visible after being clipped,
83 * %false otherwise
85 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
86 const struct drm_rect *clip)
88 int diff;
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);
95 src->x1 = clamp_t(int64_t, src->x2 - new_src_w, INT_MIN, INT_MAX);
96 dst->x1 = clip->x1;
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);
103 src->y1 = clamp_t(int64_t, src->y2 - new_src_h, INT_MIN, INT_MAX);
104 dst->y1 = clip->y1;
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);
111 src->x2 = clamp_t(int64_t, src->x1 + new_src_w, INT_MIN, INT_MAX);
112 dst->x2 = clip->x2;
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);
119 src->y2 = clamp_t(int64_t, src->y1 + new_src_h, INT_MIN, INT_MAX);
120 dst->y2 = clip->y2;
123 return drm_rect_visible(dst);
125 EXPORT_SYMBOL(drm_rect_clip_scaled);
127 static int drm_calc_scale(int src, int dst)
129 int scale = 0;
131 if (WARN_ON(src < 0 || dst < 0))
132 return -EINVAL;
134 if (dst == 0)
135 return 0;
137 if (src > (dst << 16))
138 return DIV_ROUND_UP(src, dst);
139 else
140 scale = src / dst;
142 return scale;
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
152 * Calculate the horizontal scaling factor as
153 * (@src width) / (@dst width).
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.
159 * RETURNS:
160 * The horizontal scaling factor, or errno of out of limits.
162 int drm_rect_calc_hscale(const struct drm_rect *src,
163 const struct drm_rect *dst,
164 int min_hscale, int max_hscale)
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);
170 if (hscale < 0 || dst_w == 0)
171 return hscale;
173 if (hscale < min_hscale || hscale > max_hscale)
174 return -ERANGE;
176 return hscale;
178 EXPORT_SYMBOL(drm_rect_calc_hscale);
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
187 * Calculate the vertical scaling factor as
188 * (@src height) / (@dst height).
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.
194 * RETURNS:
195 * The vertical scaling factor, or errno of out of limits.
197 int drm_rect_calc_vscale(const struct drm_rect *src,
198 const struct drm_rect *dst,
199 int min_vscale, int max_vscale)
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);
205 if (vscale < 0 || dst_h == 0)
206 return vscale;
208 if (vscale < min_vscale || vscale > max_vscale)
209 return -ERANGE;
211 return vscale;
213 EXPORT_SYMBOL(drm_rect_calc_vscale);
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
222 * Calculate the horizontal scaling factor as
223 * (@src width) / (@dst width).
225 * If the calculated scaling factor is below @min_vscale,
226 * decrease the height of rectangle @dst to compensate.
228 * If the calculated scaling factor is above @max_vscale,
229 * decrease the height of rectangle @src to compensate.
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.
235 * RETURNS:
236 * The horizontal scaling factor.
238 int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
239 struct drm_rect *dst,
240 int min_hscale, int max_hscale)
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);
246 if (hscale < 0 || dst_w == 0)
247 return hscale;
249 if (hscale < min_hscale) {
250 int max_dst_w = src_w / min_hscale;
252 drm_rect_adjust_size(dst, max_dst_w - dst_w, 0);
254 return min_hscale;
257 if (hscale > max_hscale) {
258 int max_src_w = dst_w * max_hscale;
260 drm_rect_adjust_size(src, max_src_w - src_w, 0);
262 return max_hscale;
265 return hscale;
267 EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed);
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
276 * Calculate the vertical scaling factor as
277 * (@src height) / (@dst height).
279 * If the calculated scaling factor is below @min_vscale,
280 * decrease the height of rectangle @dst to compensate.
282 * If the calculated scaling factor is above @max_vscale,
283 * decrease the height of rectangle @src to compensate.
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.
289 * RETURNS:
290 * The vertical scaling factor.
292 int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
293 struct drm_rect *dst,
294 int min_vscale, int max_vscale)
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);
300 if (vscale < 0 || dst_h == 0)
301 return vscale;
303 if (vscale < min_vscale) {
304 int max_dst_h = src_h / min_vscale;
306 drm_rect_adjust_size(dst, 0, max_dst_h - dst_h);
308 return min_vscale;
311 if (vscale > max_vscale) {
312 int max_src_h = dst_h * max_vscale;
314 drm_rect_adjust_size(src, 0, max_src_h - src_h);
316 return max_vscale;
319 return vscale;
321 EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed);
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
329 void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
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));
336 EXPORT_SYMBOL(drm_rect_debug_print);
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
345 * Apply @rotation to the coordinates of rectangle @r.
347 * @width and @height combined with @rotation define
348 * the location of the new origin.
350 * @width correcsponds to the horizontal and @height
351 * to the vertical axis of the untransformed coordinate
352 * space.
354 void drm_rect_rotate(struct drm_rect *r,
355 int width, int height,
356 unsigned int rotation)
358 struct drm_rect tmp;
360 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
361 tmp = *r;
363 if (rotation & DRM_MODE_REFLECT_X) {
364 r->x1 = width - tmp.x2;
365 r->x2 = width - tmp.x1;
368 if (rotation & DRM_MODE_REFLECT_Y) {
369 r->y1 = height - tmp.y2;
370 r->y2 = height - tmp.y1;
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;
402 EXPORT_SYMBOL(drm_rect_rotate);
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
411 * Apply the inverse of @rotation to the coordinates
412 * of rectangle @r.
414 * @width and @height combined with @rotation define
415 * the location of the new origin.
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 ::
423 * drm_rect_rotate(&r, width, height, rotation);
424 * drm_rect_rotate_inv(&r, width, height, rotation);
426 * you will always get back the original rectangle.
428 void drm_rect_rotate_inv(struct drm_rect *r,
429 int width, int height,
430 unsigned int rotation)
432 struct drm_rect tmp;
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;
462 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
463 tmp = *r;
465 if (rotation & DRM_MODE_REFLECT_X) {
466 r->x1 = width - tmp.x2;
467 r->x2 = width - tmp.x1;
470 if (rotation & DRM_MODE_REFLECT_Y) {
471 r->y1 = height - tmp.y2;
472 r->y2 = height - tmp.y1;
476 EXPORT_SYMBOL(drm_rect_rotate_inv);