1 /* $NetBSD: drm_rect.c,v 1.4 2021/12/19 01:15:00 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.4 2021/12/19 01:15:00 riastradh Exp $");
28
29 #include <linux/errno.h>
30 #include <linux/export.h>
31 #include <linux/kernel.h>
32 #include <linux/math64.h>
33
34 #include <drm/drm_mode.h>
35 #include <drm/drm_print.h>
36 #include <drm/drm_rect.h>
37
38 /**
39 * drm_rect_intersect - intersect two rectangles
40 * @r1: first rectangle
41 * @r2: second rectangle
42 *
43 * Calculate the intersection of rectangles @r1 and @r2.
44 * @r1 will be overwritten with the intersection.
45 *
46 * RETURNS:
47 * %true if rectangle @r1 is still visible after the operation,
48 * %false otherwise.
49 */
drm_rect_intersect(struct drm_rect * r1,const struct drm_rect * r2)50 bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
51 {
52 r1->x1 = max(r1->x1, r2->x1);
53 r1->y1 = max(r1->y1, r2->y1);
54 r1->x2 = min(r1->x2, r2->x2);
55 r1->y2 = min(r1->y2, r2->y2);
56
57 return drm_rect_visible(r1);
58 }
59 EXPORT_SYMBOL(drm_rect_intersect);
60
clip_scaled(int src,int dst,int * clip)61 static u32 clip_scaled(int src, int dst, int *clip)
62 {
63 u64 tmp;
64
65 if (dst == 0)
66 return 0;
67
68 /* Only clip what we have. Keeps the result bounded. */
69 *clip = min(*clip, dst);
70
71 tmp = mul_u32_u32(src, dst - *clip);
72
73 /*
74 * Round toward 1.0 when clipping so that we don't accidentally
75 * change upscaling to downscaling or vice versa.
76 */
77 if (src < (dst << 16))
78 return DIV_ROUND_UP_ULL(tmp, dst);
79 else
80 return DIV_ROUND_DOWN_ULL(tmp, dst);
81 }
82
83 /**
84 * drm_rect_clip_scaled - perform a scaled clip operation
85 * @src: source window rectangle
86 * @dst: destination window rectangle
87 * @clip: clip rectangle
88 *
89 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
90 * the corresponding amounts, retaining the vertical and horizontal scaling
91 * factors from @src to @dst.
92 *
93 * RETURNS:
94 *
95 * %true if rectangle @dst is still visible after being clipped,
96 * %false otherwise.
97 */
drm_rect_clip_scaled(struct drm_rect * src,struct drm_rect * dst,const struct drm_rect * clip)98 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
99 const struct drm_rect *clip)
100 {
101 int diff;
102
103 diff = clip->x1 - dst->x1;
104 if (diff > 0) {
105 u32 new_src_w = clip_scaled(drm_rect_width(src),
106 drm_rect_width(dst), &diff);
107
108 src->x1 = src->x2 - new_src_w;
109 dst->x1 += diff;
110 }
111 diff = clip->y1 - dst->y1;
112 if (diff > 0) {
113 u32 new_src_h = clip_scaled(drm_rect_height(src),
114 drm_rect_height(dst), &diff);
115
116 src->y1 = src->y2 - new_src_h;
117 dst->y1 += diff;
118 }
119 diff = dst->x2 - clip->x2;
120 if (diff > 0) {
121 u32 new_src_w = clip_scaled(drm_rect_width(src),
122 drm_rect_width(dst), &diff);
123
124 src->x2 = src->x1 + new_src_w;
125 dst->x2 -= diff;
126 }
127 diff = dst->y2 - clip->y2;
128 if (diff > 0) {
129 u32 new_src_h = clip_scaled(drm_rect_height(src),
130 drm_rect_height(dst), &diff);
131
132 src->y2 = src->y1 + new_src_h;
133 dst->y2 -= diff;
134 }
135
136 return drm_rect_visible(dst);
137 }
138 EXPORT_SYMBOL(drm_rect_clip_scaled);
139
drm_calc_scale(int src,int dst)140 static int drm_calc_scale(int src, int dst)
141 {
142 int scale = 0;
143
144 if (WARN_ON(src < 0 || dst < 0))
145 return -EINVAL;
146
147 if (dst == 0)
148 return 0;
149
150 if (src > (dst << 16))
151 return DIV_ROUND_UP(src, dst);
152 else
153 scale = src / dst;
154
155 return scale;
156 }
157
158 /**
159 * drm_rect_calc_hscale - calculate the horizontal scaling factor
160 * @src: source window rectangle
161 * @dst: destination window rectangle
162 * @min_hscale: minimum allowed horizontal scaling factor
163 * @max_hscale: maximum allowed horizontal scaling factor
164 *
165 * Calculate the horizontal scaling factor as
166 * (@src width) / (@dst width).
167 *
168 * If the scale is below 1 << 16, round down. If the scale is above
169 * 1 << 16, round up. This will calculate the scale with the most
170 * pessimistic limit calculation.
171 *
172 * RETURNS:
173 * The horizontal scaling factor, or errno of out of limits.
174 */
drm_rect_calc_hscale(const struct drm_rect * src,const struct drm_rect * dst,int min_hscale,int max_hscale)175 int drm_rect_calc_hscale(const struct drm_rect *src,
176 const struct drm_rect *dst,
177 int min_hscale, int max_hscale)
178 {
179 int src_w = drm_rect_width(src);
180 int dst_w = drm_rect_width(dst);
181 int hscale = drm_calc_scale(src_w, dst_w);
182
183 if (hscale < 0 || dst_w == 0)
184 return hscale;
185
186 if (hscale < min_hscale || hscale > max_hscale)
187 return -ERANGE;
188
189 return hscale;
190 }
191 EXPORT_SYMBOL(drm_rect_calc_hscale);
192
193 /**
194 * drm_rect_calc_vscale - calculate the vertical scaling factor
195 * @src: source window rectangle
196 * @dst: destination window rectangle
197 * @min_vscale: minimum allowed vertical scaling factor
198 * @max_vscale: maximum allowed vertical scaling factor
199 *
200 * Calculate the vertical scaling factor as
201 * (@src height) / (@dst height).
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 vertical scaling factor, or errno of out of limits.
209 */
drm_rect_calc_vscale(const struct drm_rect * src,const struct drm_rect * dst,int min_vscale,int max_vscale)210 int drm_rect_calc_vscale(const struct drm_rect *src,
211 const struct drm_rect *dst,
212 int min_vscale, int max_vscale)
213 {
214 int src_h = drm_rect_height(src);
215 int dst_h = drm_rect_height(dst);
216 int vscale = drm_calc_scale(src_h, dst_h);
217
218 if (vscale < 0 || dst_h == 0)
219 return vscale;
220
221 if (vscale < min_vscale || vscale > max_vscale)
222 return -ERANGE;
223
224 return vscale;
225 }
226 EXPORT_SYMBOL(drm_rect_calc_vscale);
227
228 /**
229 * drm_rect_debug_print - print the rectangle information
230 * @prefix: prefix string
231 * @r: rectangle to print
232 * @fixed_point: rectangle is in 16.16 fixed point format
233 */
drm_rect_debug_print(const char * prefix,const struct drm_rect * r,bool fixed_point)234 void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
235 {
236 if (fixed_point)
237 DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
238 else
239 DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
240 }
241 EXPORT_SYMBOL(drm_rect_debug_print);
242
243 /**
244 * drm_rect_rotate - Rotate the rectangle
245 * @r: rectangle to be rotated
246 * @width: Width of the coordinate space
247 * @height: Height of the coordinate space
248 * @rotation: Transformation to be applied
249 *
250 * Apply @rotation to the coordinates of rectangle @r.
251 *
252 * @width and @height combined with @rotation define
253 * the location of the new origin.
254 *
255 * @width correcsponds to the horizontal and @height
256 * to the vertical axis of the untransformed coordinate
257 * space.
258 */
drm_rect_rotate(struct drm_rect * r,int width,int height,unsigned int rotation)259 void drm_rect_rotate(struct drm_rect *r,
260 int width, int height,
261 unsigned int rotation)
262 {
263 struct drm_rect tmp;
264
265 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
266 tmp = *r;
267
268 if (rotation & DRM_MODE_REFLECT_X) {
269 r->x1 = width - tmp.x2;
270 r->x2 = width - tmp.x1;
271 }
272
273 if (rotation & DRM_MODE_REFLECT_Y) {
274 r->y1 = height - tmp.y2;
275 r->y2 = height - tmp.y1;
276 }
277 }
278
279 switch (rotation & DRM_MODE_ROTATE_MASK) {
280 case DRM_MODE_ROTATE_0:
281 break;
282 case DRM_MODE_ROTATE_90:
283 tmp = *r;
284 r->x1 = tmp.y1;
285 r->x2 = tmp.y2;
286 r->y1 = width - tmp.x2;
287 r->y2 = width - tmp.x1;
288 break;
289 case DRM_MODE_ROTATE_180:
290 tmp = *r;
291 r->x1 = width - tmp.x2;
292 r->x2 = width - tmp.x1;
293 r->y1 = height - tmp.y2;
294 r->y2 = height - tmp.y1;
295 break;
296 case DRM_MODE_ROTATE_270:
297 tmp = *r;
298 r->x1 = height - tmp.y2;
299 r->x2 = height - tmp.y1;
300 r->y1 = tmp.x1;
301 r->y2 = tmp.x2;
302 break;
303 default:
304 break;
305 }
306 }
307 EXPORT_SYMBOL(drm_rect_rotate);
308
309 /**
310 * drm_rect_rotate_inv - Inverse rotate the rectangle
311 * @r: rectangle to be rotated
312 * @width: Width of the coordinate space
313 * @height: Height of the coordinate space
314 * @rotation: Transformation whose inverse is to be applied
315 *
316 * Apply the inverse of @rotation to the coordinates
317 * 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 original untransformed
324 * coordinate space, so that you never have to flip
325 * them when doing a rotatation and its inverse.
326 * That is, if you do ::
327 *
328 * drm_rect_rotate(&r, width, height, rotation);
329 * drm_rect_rotate_inv(&r, width, height, rotation);
330 *
331 * you will always get back the original rectangle.
332 */
drm_rect_rotate_inv(struct drm_rect * r,int width,int height,unsigned int rotation)333 void drm_rect_rotate_inv(struct drm_rect *r,
334 int width, int height,
335 unsigned int rotation)
336 {
337 struct drm_rect tmp;
338
339 switch (rotation & DRM_MODE_ROTATE_MASK) {
340 case DRM_MODE_ROTATE_0:
341 break;
342 case DRM_MODE_ROTATE_90:
343 tmp = *r;
344 r->x1 = width - tmp.y2;
345 r->x2 = width - tmp.y1;
346 r->y1 = tmp.x1;
347 r->y2 = tmp.x2;
348 break;
349 case DRM_MODE_ROTATE_180:
350 tmp = *r;
351 r->x1 = width - tmp.x2;
352 r->x2 = width - tmp.x1;
353 r->y1 = height - tmp.y2;
354 r->y2 = height - tmp.y1;
355 break;
356 case DRM_MODE_ROTATE_270:
357 tmp = *r;
358 r->x1 = tmp.y1;
359 r->x2 = tmp.y2;
360 r->y1 = height - tmp.x2;
361 r->y2 = height - tmp.x1;
362 break;
363 default:
364 break;
365 }
366
367 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
368 tmp = *r;
369
370 if (rotation & DRM_MODE_REFLECT_X) {
371 r->x1 = width - tmp.x2;
372 r->x2 = width - tmp.x1;
373 }
374
375 if (rotation & DRM_MODE_REFLECT_Y) {
376 r->y1 = height - tmp.y2;
377 r->y2 = height - tmp.y1;
378 }
379 }
380 }
381 EXPORT_SYMBOL(drm_rect_rotate_inv);
382