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