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
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
24 #include <linux/errno.h>
25 #include <linux/export.h>
26 #include <linux/kernel.h>
28 #include <drm/drm_mode.h>
29 #include <drm/drm_print.h>
30 #include <drm/drm_rect.h>
33 * drm_rect_intersect - intersect two rectangles
34 * @r1: first rectangle
35 * @r2: second rectangle
37 * Calculate the intersection of rectangles @r1 and @r2.
38 * @r1 will be overwritten with the intersection.
41 * %true if rectangle @r1 is still visible after the operation,
44 bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
46 r1->x1 = max(r1->x1, r2->x1);
47 r1->y1 = max(r1->y1, r2->y1);
48 r1->x2 = min(r1->x2, r2->x2);
49 r1->y2 = min(r1->y2, r2->y2);
51 return drm_rect_visible(r1);
53 EXPORT_SYMBOL(drm_rect_intersect);
55 static u32 clip_scaled(int src, int dst, int *clip)
62 /* Only clip what we have. Keeps the result bounded. */
63 *clip = min(*clip, dst);
65 tmp = mul_u32_u32(src, dst - *clip);
68 * Round toward 1.0 when clipping so that we don't accidentally
69 * change upscaling to downscaling or vice versa.
71 if (src < (dst << 16))
72 return DIV_ROUND_UP_ULL(tmp, dst);
74 return DIV_ROUND_DOWN_ULL(tmp, dst);
78 * drm_rect_clip_scaled - perform a scaled clip operation
79 * @src: source window rectangle
80 * @dst: destination window rectangle
81 * @clip: clip rectangle
83 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by
84 * the corresponding amounts, retaining the vertical and horizontal scaling
85 * factors from @src to @dst.
88 * %true if rectangle @dst is still visible after being clipped,
91 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
92 const struct drm_rect *clip)
96 diff = clip->x1 - dst->x1;
98 u32 new_src_w = clip_scaled(drm_rect_width(src),
99 drm_rect_width(dst), &diff);
101 src->x1 = src->x2 - new_src_w;
104 diff = clip->y1 - dst->y1;
106 u32 new_src_h = clip_scaled(drm_rect_height(src),
107 drm_rect_height(dst), &diff);
109 src->y1 = src->y2 - new_src_h;
112 diff = dst->x2 - clip->x2;
114 u32 new_src_w = clip_scaled(drm_rect_width(src),
115 drm_rect_width(dst), &diff);
117 src->x2 = src->x1 + new_src_w;
120 diff = dst->y2 - clip->y2;
122 u32 new_src_h = clip_scaled(drm_rect_height(src),
123 drm_rect_height(dst), &diff);
125 src->y2 = src->y1 + new_src_h;
129 return drm_rect_visible(dst);
131 EXPORT_SYMBOL(drm_rect_clip_scaled);
133 static int drm_calc_scale(int src, int dst)
137 if (WARN_ON(src < 0 || dst < 0))
143 if (src > (dst << 16))
144 return DIV_ROUND_UP(src, dst);
152 * drm_rect_calc_hscale - calculate the horizontal scaling factor
153 * @src: source window rectangle
154 * @dst: destination window rectangle
155 * @min_hscale: minimum allowed horizontal scaling factor
156 * @max_hscale: maximum allowed horizontal scaling factor
158 * Calculate the horizontal scaling factor as
159 * (@src width) / (@dst width).
161 * If the scale is below 1 << 16, round down. If the scale is above
162 * 1 << 16, round up. This will calculate the scale with the most
163 * pessimistic limit calculation.
166 * The horizontal scaling factor, or errno of out of limits.
168 int drm_rect_calc_hscale(const struct drm_rect *src,
169 const struct drm_rect *dst,
170 int min_hscale, int max_hscale)
172 int src_w = drm_rect_width(src);
173 int dst_w = drm_rect_width(dst);
174 int hscale = drm_calc_scale(src_w, dst_w);
176 if (hscale < 0 || dst_w == 0)
179 if (hscale < min_hscale || hscale > max_hscale)
184 EXPORT_SYMBOL(drm_rect_calc_hscale);
187 * drm_rect_calc_vscale - calculate the vertical scaling factor
188 * @src: source window rectangle
189 * @dst: destination window rectangle
190 * @min_vscale: minimum allowed vertical scaling factor
191 * @max_vscale: maximum allowed vertical scaling factor
193 * Calculate the vertical scaling factor as
194 * (@src height) / (@dst height).
196 * If the scale is below 1 << 16, round down. If the scale is above
197 * 1 << 16, round up. This will calculate the scale with the most
198 * pessimistic limit calculation.
201 * The vertical scaling factor, or errno of out of limits.
203 int drm_rect_calc_vscale(const struct drm_rect *src,
204 const struct drm_rect *dst,
205 int min_vscale, int max_vscale)
207 int src_h = drm_rect_height(src);
208 int dst_h = drm_rect_height(dst);
209 int vscale = drm_calc_scale(src_h, dst_h);
211 if (vscale < 0 || dst_h == 0)
214 if (vscale < min_vscale || vscale > max_vscale)
219 EXPORT_SYMBOL(drm_rect_calc_vscale);
222 * drm_rect_debug_print - print the rectangle information
223 * @prefix: prefix string
224 * @r: rectangle to print
225 * @fixed_point: rectangle is in 16.16 fixed point format
227 void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
230 DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
232 DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
234 EXPORT_SYMBOL(drm_rect_debug_print);
237 * drm_rect_rotate - Rotate the rectangle
238 * @r: rectangle to be rotated
239 * @width: Width of the coordinate space
240 * @height: Height of the coordinate space
241 * @rotation: Transformation to be applied
243 * Apply @rotation to the coordinates of rectangle @r.
245 * @width and @height combined with @rotation define
246 * the location of the new origin.
248 * @width correcsponds to the horizontal and @height
249 * to the vertical axis of the untransformed coordinate
252 void drm_rect_rotate(struct drm_rect *r,
253 int width, int height,
254 unsigned int rotation)
258 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
261 if (rotation & DRM_MODE_REFLECT_X) {
262 r->x1 = width - tmp.x2;
263 r->x2 = width - tmp.x1;
266 if (rotation & DRM_MODE_REFLECT_Y) {
267 r->y1 = height - tmp.y2;
268 r->y2 = height - tmp.y1;
272 switch (rotation & DRM_MODE_ROTATE_MASK) {
273 case DRM_MODE_ROTATE_0:
275 case DRM_MODE_ROTATE_90:
279 r->y1 = width - tmp.x2;
280 r->y2 = width - tmp.x1;
282 case DRM_MODE_ROTATE_180:
284 r->x1 = width - tmp.x2;
285 r->x2 = width - tmp.x1;
286 r->y1 = height - tmp.y2;
287 r->y2 = height - tmp.y1;
289 case DRM_MODE_ROTATE_270:
291 r->x1 = height - tmp.y2;
292 r->x2 = height - tmp.y1;
300 EXPORT_SYMBOL(drm_rect_rotate);
303 * drm_rect_rotate_inv - Inverse rotate the rectangle
304 * @r: rectangle to be rotated
305 * @width: Width of the coordinate space
306 * @height: Height of the coordinate space
307 * @rotation: Transformation whose inverse is to be applied
309 * Apply the inverse of @rotation to the coordinates
312 * @width and @height combined with @rotation define
313 * the location of the new origin.
315 * @width correcsponds to the horizontal and @height
316 * to the vertical axis of the original untransformed
317 * coordinate space, so that you never have to flip
318 * them when doing a rotatation and its inverse.
319 * That is, if you do ::
321 * drm_rect_rotate(&r, width, height, rotation);
322 * drm_rect_rotate_inv(&r, width, height, rotation);
324 * you will always get back the original rectangle.
326 void drm_rect_rotate_inv(struct drm_rect *r,
327 int width, int height,
328 unsigned int rotation)
332 switch (rotation & DRM_MODE_ROTATE_MASK) {
333 case DRM_MODE_ROTATE_0:
335 case DRM_MODE_ROTATE_90:
337 r->x1 = width - tmp.y2;
338 r->x2 = width - tmp.y1;
342 case DRM_MODE_ROTATE_180:
344 r->x1 = width - tmp.x2;
345 r->x2 = width - tmp.x1;
346 r->y1 = height - tmp.y2;
347 r->y2 = height - tmp.y1;
349 case DRM_MODE_ROTATE_270:
353 r->y1 = height - tmp.x2;
354 r->y2 = height - tmp.x1;
360 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
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 EXPORT_SYMBOL(drm_rect_rotate_inv);