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(u32 src, u32 dst, u32 clip)
62 tmp = mul_u32_u32(src, dst - clip);
65 * Round toward 1.0 when clipping so that we don't accidentally
66 * change upscaling to downscaling or vice versa.
68 if (src < (dst << 16))
69 return DIV_ROUND_UP_ULL(tmp, dst);
71 return DIV_ROUND_DOWN_ULL(tmp, dst);
75 * drm_rect_clip_scaled - perform a scaled clip operation
76 * @src: source window rectangle
77 * @dst: destination window rectangle
78 * @clip: clip rectangle
80 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
81 * same amounts multiplied by @hscale and @vscale.
84 * %true if rectangle @dst is still visible after being clipped,
87 bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
88 const struct drm_rect *clip)
92 diff = clip->x1 - dst->x1;
94 u32 new_src_w = clip_scaled(drm_rect_width(src),
95 drm_rect_width(dst), diff);
97 src->x1 = clamp_t(int64_t, src->x2 - new_src_w, INT_MIN, INT_MAX);
100 diff = clip->y1 - dst->y1;
102 u32 new_src_h = clip_scaled(drm_rect_height(src),
103 drm_rect_height(dst), diff);
105 src->y1 = clamp_t(int64_t, src->y2 - new_src_h, INT_MIN, INT_MAX);
108 diff = dst->x2 - clip->x2;
110 u32 new_src_w = clip_scaled(drm_rect_width(src),
111 drm_rect_width(dst), diff);
113 src->x2 = clamp_t(int64_t, src->x1 + new_src_w, INT_MIN, INT_MAX);
116 diff = dst->y2 - clip->y2;
118 u32 new_src_h = clip_scaled(drm_rect_height(src),
119 drm_rect_height(dst), diff);
121 src->y2 = clamp_t(int64_t, src->y1 + new_src_h, INT_MIN, INT_MAX);
125 return drm_rect_visible(dst);
127 EXPORT_SYMBOL(drm_rect_clip_scaled);
129 static int drm_calc_scale(int src, int dst)
133 if (WARN_ON(src < 0 || dst < 0))
139 if (src > (dst << 16))
140 return DIV_ROUND_UP(src, dst);
148 * drm_rect_calc_hscale - calculate the horizontal scaling factor
149 * @src: source window rectangle
150 * @dst: destination window rectangle
151 * @min_hscale: minimum allowed horizontal scaling factor
152 * @max_hscale: maximum allowed horizontal scaling factor
154 * Calculate the horizontal scaling factor as
155 * (@src width) / (@dst width).
157 * If the scale is below 1 << 16, round down. If the scale is above
158 * 1 << 16, round up. This will calculate the scale with the most
159 * pessimistic limit calculation.
162 * The horizontal scaling factor, or errno of out of limits.
164 int drm_rect_calc_hscale(const struct drm_rect *src,
165 const struct drm_rect *dst,
166 int min_hscale, int max_hscale)
168 int src_w = drm_rect_width(src);
169 int dst_w = drm_rect_width(dst);
170 int hscale = drm_calc_scale(src_w, dst_w);
172 if (hscale < 0 || dst_w == 0)
175 if (hscale < min_hscale || hscale > max_hscale)
180 EXPORT_SYMBOL(drm_rect_calc_hscale);
183 * drm_rect_calc_vscale - calculate the vertical scaling factor
184 * @src: source window rectangle
185 * @dst: destination window rectangle
186 * @min_vscale: minimum allowed vertical scaling factor
187 * @max_vscale: maximum allowed vertical scaling factor
189 * Calculate the vertical scaling factor as
190 * (@src height) / (@dst height).
192 * If the scale is below 1 << 16, round down. If the scale is above
193 * 1 << 16, round up. This will calculate the scale with the most
194 * pessimistic limit calculation.
197 * The vertical scaling factor, or errno of out of limits.
199 int drm_rect_calc_vscale(const struct drm_rect *src,
200 const struct drm_rect *dst,
201 int min_vscale, int max_vscale)
203 int src_h = drm_rect_height(src);
204 int dst_h = drm_rect_height(dst);
205 int vscale = drm_calc_scale(src_h, dst_h);
207 if (vscale < 0 || dst_h == 0)
210 if (vscale < min_vscale || vscale > max_vscale)
215 EXPORT_SYMBOL(drm_rect_calc_vscale);
218 * drm_rect_debug_print - print the rectangle information
219 * @prefix: prefix string
220 * @r: rectangle to print
221 * @fixed_point: rectangle is in 16.16 fixed point format
223 void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
226 DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
228 DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
230 EXPORT_SYMBOL(drm_rect_debug_print);
233 * drm_rect_rotate - Rotate the rectangle
234 * @r: rectangle to be rotated
235 * @width: Width of the coordinate space
236 * @height: Height of the coordinate space
237 * @rotation: Transformation to be applied
239 * Apply @rotation to the coordinates of rectangle @r.
241 * @width and @height combined with @rotation define
242 * the location of the new origin.
244 * @width correcsponds to the horizontal and @height
245 * to the vertical axis of the untransformed coordinate
248 void drm_rect_rotate(struct drm_rect *r,
249 int width, int height,
250 unsigned int rotation)
254 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
257 if (rotation & DRM_MODE_REFLECT_X) {
258 r->x1 = width - tmp.x2;
259 r->x2 = width - tmp.x1;
262 if (rotation & DRM_MODE_REFLECT_Y) {
263 r->y1 = height - tmp.y2;
264 r->y2 = height - tmp.y1;
268 switch (rotation & DRM_MODE_ROTATE_MASK) {
269 case DRM_MODE_ROTATE_0:
271 case DRM_MODE_ROTATE_90:
275 r->y1 = width - tmp.x2;
276 r->y2 = width - tmp.x1;
278 case DRM_MODE_ROTATE_180:
280 r->x1 = width - tmp.x2;
281 r->x2 = width - tmp.x1;
282 r->y1 = height - tmp.y2;
283 r->y2 = height - tmp.y1;
285 case DRM_MODE_ROTATE_270:
287 r->x1 = height - tmp.y2;
288 r->x2 = height - tmp.y1;
296 EXPORT_SYMBOL(drm_rect_rotate);
299 * drm_rect_rotate_inv - Inverse rotate the rectangle
300 * @r: rectangle to be rotated
301 * @width: Width of the coordinate space
302 * @height: Height of the coordinate space
303 * @rotation: Transformation whose inverse is to be applied
305 * Apply the inverse of @rotation to the coordinates
308 * @width and @height combined with @rotation define
309 * the location of the new origin.
311 * @width correcsponds to the horizontal and @height
312 * to the vertical axis of the original untransformed
313 * coordinate space, so that you never have to flip
314 * them when doing a rotatation and its inverse.
315 * That is, if you do ::
317 * drm_rect_rotate(&r, width, height, rotation);
318 * drm_rect_rotate_inv(&r, width, height, rotation);
320 * you will always get back the original rectangle.
322 void drm_rect_rotate_inv(struct drm_rect *r,
323 int width, int height,
324 unsigned int rotation)
328 switch (rotation & DRM_MODE_ROTATE_MASK) {
329 case DRM_MODE_ROTATE_0:
331 case DRM_MODE_ROTATE_90:
333 r->x1 = width - tmp.y2;
334 r->x2 = width - tmp.y1;
338 case DRM_MODE_ROTATE_180:
340 r->x1 = width - tmp.x2;
341 r->x2 = width - tmp.x1;
342 r->y1 = height - tmp.y2;
343 r->y2 = height - tmp.y1;
345 case DRM_MODE_ROTATE_270:
349 r->y1 = height - tmp.x2;
350 r->y2 = height - tmp.x1;
356 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
359 if (rotation & DRM_MODE_REFLECT_X) {
360 r->x1 = width - tmp.x2;
361 r->x2 = width - tmp.x1;
364 if (rotation & DRM_MODE_REFLECT_Y) {
365 r->y1 = height - tmp.y2;
366 r->y2 = height - tmp.y1;
370 EXPORT_SYMBOL(drm_rect_rotate_inv);