blob: 66c41b12719cbdbc3bbf7a43d6f45fab861b98d8 [file] [log] [blame]
Ville Syrjälä3512f972013-04-24 18:52:34 +03001/*
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>
Ville Syrjäläe7272df2013-04-24 18:52:36 +030027#include <drm/drmP.h>
Ville Syrjälä3512f972013-04-24 18:52:34 +030028#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 */
42bool 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}
51EXPORT_SYMBOL(drm_rect_intersect);
52
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +020053static u32 clip_scaled(u32 src, u32 dst, u32 clip)
54{
55 u64 tmp = mul_u32_u32(src, dst - clip);
56
57 /*
58 * Round toward 1.0 when clipping so that we don't accidentally
59 * change upscaling to downscaling or vice versa.
60 */
61 if (src < (dst << 16))
62 return DIV_ROUND_UP_ULL(tmp, dst);
63 else
64 return DIV_ROUND_DOWN_ULL(tmp, dst);
65}
66
Ville Syrjälä3512f972013-04-24 18:52:34 +030067/**
68 * drm_rect_clip_scaled - perform a scaled clip operation
69 * @src: source window rectangle
70 * @dst: destination window rectangle
71 * @clip: clip rectangle
Ville Syrjälä3512f972013-04-24 18:52:34 +030072 *
73 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
74 * same amounts multiplied by @hscale and @vscale.
75 *
76 * RETURNS:
77 * %true if rectangle @dst is still visible after being clipped,
78 * %false otherwise
79 */
80bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +020081 const struct drm_rect *clip)
Ville Syrjälä3512f972013-04-24 18:52:34 +030082{
83 int diff;
84
85 diff = clip->x1 - dst->x1;
86 if (diff > 0) {
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +020087 u32 new_src_w = clip_scaled(drm_rect_width(src),
88 drm_rect_width(dst), diff);
89
90 src->x1 = clamp_t(int64_t, src->x2 - new_src_w, INT_MIN, INT_MAX);
91 dst->x1 = clip->x1;
Ville Syrjälä3512f972013-04-24 18:52:34 +030092 }
93 diff = clip->y1 - dst->y1;
94 if (diff > 0) {
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +020095 u32 new_src_h = clip_scaled(drm_rect_height(src),
96 drm_rect_height(dst), diff);
97
98 src->y1 = clamp_t(int64_t, src->y2 - new_src_h, INT_MIN, INT_MAX);
99 dst->y1 = clip->y1;
Ville Syrjälä3512f972013-04-24 18:52:34 +0300100 }
101 diff = dst->x2 - clip->x2;
102 if (diff > 0) {
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +0200103 u32 new_src_w = clip_scaled(drm_rect_width(src),
104 drm_rect_width(dst), diff);
105
106 src->x2 = clamp_t(int64_t, src->x1 + new_src_w, INT_MIN, INT_MAX);
107 dst->x2 = clip->x2;
Ville Syrjälä3512f972013-04-24 18:52:34 +0300108 }
109 diff = dst->y2 - clip->y2;
110 if (diff > 0) {
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +0200111 u32 new_src_h = clip_scaled(drm_rect_height(src),
112 drm_rect_height(dst), diff);
113
114 src->y2 = clamp_t(int64_t, src->y1 + new_src_h, INT_MIN, INT_MAX);
115 dst->y2 = clip->y2;
Ville Syrjälä3512f972013-04-24 18:52:34 +0300116 }
117
Maarten Lankhorstf96bdf52018-05-03 13:22:14 +0200118 return drm_rect_visible(dst);
Ville Syrjälä3512f972013-04-24 18:52:34 +0300119}
120EXPORT_SYMBOL(drm_rect_clip_scaled);
Ville Syrjälä4954c422013-04-24 18:52:35 +0300121
122static int drm_calc_scale(int src, int dst)
123{
124 int scale = 0;
125
Ville Syrjälä1e1a5f82016-07-26 19:06:56 +0300126 if (WARN_ON(src < 0 || dst < 0))
Ville Syrjälä4954c422013-04-24 18:52:35 +0300127 return -EINVAL;
128
129 if (dst == 0)
130 return 0;
131
Maarten Lankhorst6f96f202018-05-03 13:22:13 +0200132 if (src > (dst << 16))
133 return DIV_ROUND_UP(src, dst);
134 else
135 scale = src / dst;
Ville Syrjälä4954c422013-04-24 18:52:35 +0300136
137 return scale;
138}
139
140/**
141 * drm_rect_calc_hscale - calculate the horizontal scaling factor
142 * @src: source window rectangle
143 * @dst: destination window rectangle
144 * @min_hscale: minimum allowed horizontal scaling factor
145 * @max_hscale: maximum allowed horizontal scaling factor
146 *
147 * Calculate the horizontal scaling factor as
148 * (@src width) / (@dst width).
149 *
Maarten Lankhorst6f96f202018-05-03 13:22:13 +0200150 * If the scale is below 1 << 16, round down. If the scale is above
151 * 1 << 16, round up. This will calculate the scale with the most
152 * pessimistic limit calculation.
153 *
Ville Syrjälä4954c422013-04-24 18:52:35 +0300154 * RETURNS:
155 * The horizontal scaling factor, or errno of out of limits.
156 */
157int drm_rect_calc_hscale(const struct drm_rect *src,
158 const struct drm_rect *dst,
159 int min_hscale, int max_hscale)
160{
161 int src_w = drm_rect_width(src);
162 int dst_w = drm_rect_width(dst);
163 int hscale = drm_calc_scale(src_w, dst_w);
164
165 if (hscale < 0 || dst_w == 0)
166 return hscale;
167
168 if (hscale < min_hscale || hscale > max_hscale)
169 return -ERANGE;
170
171 return hscale;
172}
173EXPORT_SYMBOL(drm_rect_calc_hscale);
174
175/**
176 * drm_rect_calc_vscale - calculate the vertical scaling factor
177 * @src: source window rectangle
178 * @dst: destination window rectangle
179 * @min_vscale: minimum allowed vertical scaling factor
180 * @max_vscale: maximum allowed vertical scaling factor
181 *
182 * Calculate the vertical scaling factor as
183 * (@src height) / (@dst height).
184 *
Maarten Lankhorst6f96f202018-05-03 13:22:13 +0200185 * If the scale is below 1 << 16, round down. If the scale is above
186 * 1 << 16, round up. This will calculate the scale with the most
187 * pessimistic limit calculation.
188 *
Ville Syrjälä4954c422013-04-24 18:52:35 +0300189 * RETURNS:
190 * The vertical scaling factor, or errno of out of limits.
191 */
192int drm_rect_calc_vscale(const struct drm_rect *src,
193 const struct drm_rect *dst,
194 int min_vscale, int max_vscale)
195{
196 int src_h = drm_rect_height(src);
197 int dst_h = drm_rect_height(dst);
198 int vscale = drm_calc_scale(src_h, dst_h);
199
200 if (vscale < 0 || dst_h == 0)
201 return vscale;
202
203 if (vscale < min_vscale || vscale > max_vscale)
204 return -ERANGE;
205
206 return vscale;
207}
208EXPORT_SYMBOL(drm_rect_calc_vscale);
209
210/**
Ville Syrjäläe7272df2013-04-24 18:52:36 +0300211 * drm_rect_debug_print - print the rectangle information
Ville Syrjäläc70f5772015-11-16 17:02:36 +0200212 * @prefix: prefix string
Ville Syrjäläe7272df2013-04-24 18:52:36 +0300213 * @r: rectangle to print
214 * @fixed_point: rectangle is in 16.16 fixed point format
215 */
Ville Syrjäläc70f5772015-11-16 17:02:36 +0200216void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
Ville Syrjäläe7272df2013-04-24 18:52:36 +0300217{
Ville Syrjäläe7272df2013-04-24 18:52:36 +0300218 if (fixed_point)
Rob Clark65c7dc12016-11-05 11:08:06 -0400219 DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
Ville Syrjäläe7272df2013-04-24 18:52:36 +0300220 else
Rob Clark65c7dc12016-11-05 11:08:06 -0400221 DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
Ville Syrjäläe7272df2013-04-24 18:52:36 +0300222}
223EXPORT_SYMBOL(drm_rect_debug_print);
Ville Syrjälä070740062014-07-08 10:31:55 +0530224
225/**
226 * drm_rect_rotate - Rotate the rectangle
227 * @r: rectangle to be rotated
228 * @width: Width of the coordinate space
229 * @height: Height of the coordinate space
230 * @rotation: Transformation to be applied
231 *
232 * Apply @rotation to the coordinates of rectangle @r.
233 *
234 * @width and @height combined with @rotation define
235 * the location of the new origin.
236 *
237 * @width correcsponds to the horizontal and @height
238 * to the vertical axis of the untransformed coordinate
239 * space.
240 */
241void drm_rect_rotate(struct drm_rect *r,
242 int width, int height,
243 unsigned int rotation)
244{
245 struct drm_rect tmp;
246
Robert Fossc2c446a2017-05-19 16:50:17 -0400247 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
Ville Syrjälä070740062014-07-08 10:31:55 +0530248 tmp = *r;
249
Robert Fossc2c446a2017-05-19 16:50:17 -0400250 if (rotation & DRM_MODE_REFLECT_X) {
Ville Syrjälä070740062014-07-08 10:31:55 +0530251 r->x1 = width - tmp.x2;
252 r->x2 = width - tmp.x1;
253 }
254
Robert Fossc2c446a2017-05-19 16:50:17 -0400255 if (rotation & DRM_MODE_REFLECT_Y) {
Ville Syrjälä070740062014-07-08 10:31:55 +0530256 r->y1 = height - tmp.y2;
257 r->y2 = height - tmp.y1;
258 }
259 }
260
Robert Fossc2c446a2017-05-19 16:50:17 -0400261 switch (rotation & DRM_MODE_ROTATE_MASK) {
262 case DRM_MODE_ROTATE_0:
Ville Syrjälä070740062014-07-08 10:31:55 +0530263 break;
Robert Fossc2c446a2017-05-19 16:50:17 -0400264 case DRM_MODE_ROTATE_90:
Ville Syrjälä070740062014-07-08 10:31:55 +0530265 tmp = *r;
266 r->x1 = tmp.y1;
267 r->x2 = tmp.y2;
268 r->y1 = width - tmp.x2;
269 r->y2 = width - tmp.x1;
270 break;
Robert Fossc2c446a2017-05-19 16:50:17 -0400271 case DRM_MODE_ROTATE_180:
Ville Syrjälä070740062014-07-08 10:31:55 +0530272 tmp = *r;
273 r->x1 = width - tmp.x2;
274 r->x2 = width - tmp.x1;
275 r->y1 = height - tmp.y2;
276 r->y2 = height - tmp.y1;
277 break;
Robert Fossc2c446a2017-05-19 16:50:17 -0400278 case DRM_MODE_ROTATE_270:
Ville Syrjälä070740062014-07-08 10:31:55 +0530279 tmp = *r;
280 r->x1 = height - tmp.y2;
281 r->x2 = height - tmp.y1;
282 r->y1 = tmp.x1;
283 r->y2 = tmp.x2;
284 break;
285 default:
286 break;
287 }
288}
289EXPORT_SYMBOL(drm_rect_rotate);
290
291/**
292 * drm_rect_rotate_inv - Inverse rotate the rectangle
293 * @r: rectangle to be rotated
294 * @width: Width of the coordinate space
295 * @height: Height of the coordinate space
296 * @rotation: Transformation whose inverse is to be applied
297 *
298 * Apply the inverse of @rotation to the coordinates
299 * of rectangle @r.
300 *
301 * @width and @height combined with @rotation define
302 * the location of the new origin.
303 *
304 * @width correcsponds to the horizontal and @height
305 * to the vertical axis of the original untransformed
306 * coordinate space, so that you never have to flip
307 * them when doing a rotatation and its inverse.
Daniel Vetter5edbfc42016-12-29 21:48:29 +0100308 * That is, if you do ::
Ville Syrjälä070740062014-07-08 10:31:55 +0530309 *
Ville Syrjäläec667232018-04-26 17:16:31 +0300310 * drm_rect_rotate(&r, width, height, rotation);
311 * drm_rect_rotate_inv(&r, width, height, rotation);
Ville Syrjälä070740062014-07-08 10:31:55 +0530312 *
313 * you will always get back the original rectangle.
314 */
315void drm_rect_rotate_inv(struct drm_rect *r,
316 int width, int height,
317 unsigned int rotation)
318{
319 struct drm_rect tmp;
320
Robert Fossc2c446a2017-05-19 16:50:17 -0400321 switch (rotation & DRM_MODE_ROTATE_MASK) {
322 case DRM_MODE_ROTATE_0:
Ville Syrjälä070740062014-07-08 10:31:55 +0530323 break;
Robert Fossc2c446a2017-05-19 16:50:17 -0400324 case DRM_MODE_ROTATE_90:
Ville Syrjälä070740062014-07-08 10:31:55 +0530325 tmp = *r;
326 r->x1 = width - tmp.y2;
327 r->x2 = width - tmp.y1;
328 r->y1 = tmp.x1;
329 r->y2 = tmp.x2;
330 break;
Robert Fossc2c446a2017-05-19 16:50:17 -0400331 case DRM_MODE_ROTATE_180:
Ville Syrjälä070740062014-07-08 10:31:55 +0530332 tmp = *r;
333 r->x1 = width - tmp.x2;
334 r->x2 = width - tmp.x1;
335 r->y1 = height - tmp.y2;
336 r->y2 = height - tmp.y1;
337 break;
Robert Fossc2c446a2017-05-19 16:50:17 -0400338 case DRM_MODE_ROTATE_270:
Ville Syrjälä070740062014-07-08 10:31:55 +0530339 tmp = *r;
340 r->x1 = tmp.y1;
341 r->x2 = tmp.y2;
342 r->y1 = height - tmp.x2;
343 r->y2 = height - tmp.x1;
344 break;
345 default:
346 break;
347 }
348
Robert Fossc2c446a2017-05-19 16:50:17 -0400349 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
Ville Syrjälä070740062014-07-08 10:31:55 +0530350 tmp = *r;
351
Robert Fossc2c446a2017-05-19 16:50:17 -0400352 if (rotation & DRM_MODE_REFLECT_X) {
Ville Syrjälä070740062014-07-08 10:31:55 +0530353 r->x1 = width - tmp.x2;
354 r->x2 = width - tmp.x1;
355 }
356
Robert Fossc2c446a2017-05-19 16:50:17 -0400357 if (rotation & DRM_MODE_REFLECT_Y) {
Ville Syrjälä070740062014-07-08 10:31:55 +0530358 r->y1 = height - tmp.y2;
359 r->y2 = height - tmp.y1;
360 }
361 }
362}
363EXPORT_SYMBOL(drm_rect_rotate_inv);