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_rect.h>
31 * drm_rect_intersect - intersect two rectangles
32 * @r1: first rectangle
33 * @r2: second rectangle
35 * Calculate the intersection of rectangles @r1 and @r2.
36 * @r1 will be overwritten with the intersection.
39 * %true if rectangle @r1 is still visible after the operation,
42 bool drm_rect_intersect(struct drm_rect
*r1
, const struct drm_rect
*r2
)
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
);
49 return drm_rect_visible(r1
);
51 EXPORT_SYMBOL(drm_rect_intersect
);
54 * drm_rect_clip_scaled - perform a scaled clip operation
55 * @src: source window rectangle
56 * @dst: destination window rectangle
57 * @clip: clip rectangle
58 * @hscale: horizontal scaling factor
59 * @vscale: vertical scaling factor
61 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
62 * same amounts multiplied by @hscale and @vscale.
65 * %true if rectangle @dst is still visible after being clipped,
68 bool drm_rect_clip_scaled(struct drm_rect
*src
, struct drm_rect
*dst
,
69 const struct drm_rect
*clip
,
70 int hscale
, int vscale
)
74 diff
= clip
->x1
- dst
->x1
;
76 int64_t tmp
= src
->x1
+ (int64_t) diff
* hscale
;
77 src
->x1
= clamp_t(int64_t, tmp
, INT_MIN
, INT_MAX
);
79 diff
= clip
->y1
- dst
->y1
;
81 int64_t tmp
= src
->y1
+ (int64_t) diff
* vscale
;
82 src
->y1
= clamp_t(int64_t, tmp
, INT_MIN
, INT_MAX
);
84 diff
= dst
->x2
- clip
->x2
;
86 int64_t tmp
= src
->x2
- (int64_t) diff
* hscale
;
87 src
->x2
= clamp_t(int64_t, tmp
, INT_MIN
, INT_MAX
);
89 diff
= dst
->y2
- clip
->y2
;
91 int64_t tmp
= src
->y2
- (int64_t) diff
* vscale
;
92 src
->y2
= clamp_t(int64_t, tmp
, INT_MIN
, INT_MAX
);
95 return drm_rect_intersect(dst
, clip
);
97 EXPORT_SYMBOL(drm_rect_clip_scaled
);
99 static int drm_calc_scale(int src
, int dst
)
103 if (WARN_ON(src
< 0 || dst
< 0))
115 * drm_rect_calc_hscale - calculate the horizontal scaling factor
116 * @src: source window rectangle
117 * @dst: destination window rectangle
118 * @min_hscale: minimum allowed horizontal scaling factor
119 * @max_hscale: maximum allowed horizontal scaling factor
121 * Calculate the horizontal scaling factor as
122 * (@src width) / (@dst width).
125 * The horizontal scaling factor, or errno of out of limits.
127 int drm_rect_calc_hscale(const struct drm_rect
*src
,
128 const struct drm_rect
*dst
,
129 int min_hscale
, int max_hscale
)
131 int src_w
= drm_rect_width(src
);
132 int dst_w
= drm_rect_width(dst
);
133 int hscale
= drm_calc_scale(src_w
, dst_w
);
135 if (hscale
< 0 || dst_w
== 0)
138 if (hscale
< min_hscale
|| hscale
> max_hscale
)
143 EXPORT_SYMBOL(drm_rect_calc_hscale
);
146 * drm_rect_calc_vscale - calculate the vertical scaling factor
147 * @src: source window rectangle
148 * @dst: destination window rectangle
149 * @min_vscale: minimum allowed vertical scaling factor
150 * @max_vscale: maximum allowed vertical scaling factor
152 * Calculate the vertical scaling factor as
153 * (@src height) / (@dst height).
156 * The vertical scaling factor, or errno of out of limits.
158 int drm_rect_calc_vscale(const struct drm_rect
*src
,
159 const struct drm_rect
*dst
,
160 int min_vscale
, int max_vscale
)
162 int src_h
= drm_rect_height(src
);
163 int dst_h
= drm_rect_height(dst
);
164 int vscale
= drm_calc_scale(src_h
, dst_h
);
166 if (vscale
< 0 || dst_h
== 0)
169 if (vscale
< min_vscale
|| vscale
> max_vscale
)
174 EXPORT_SYMBOL(drm_rect_calc_vscale
);
177 * drm_calc_hscale_relaxed - calculate the horizontal scaling factor
178 * @src: source window rectangle
179 * @dst: destination window rectangle
180 * @min_hscale: minimum allowed horizontal scaling factor
181 * @max_hscale: maximum allowed horizontal scaling factor
183 * Calculate the horizontal scaling factor as
184 * (@src width) / (@dst width).
186 * If the calculated scaling factor is below @min_vscale,
187 * decrease the height of rectangle @dst to compensate.
189 * If the calculated scaling factor is above @max_vscale,
190 * decrease the height of rectangle @src to compensate.
193 * The horizontal scaling factor.
195 int drm_rect_calc_hscale_relaxed(struct drm_rect
*src
,
196 struct drm_rect
*dst
,
197 int min_hscale
, int max_hscale
)
199 int src_w
= drm_rect_width(src
);
200 int dst_w
= drm_rect_width(dst
);
201 int hscale
= drm_calc_scale(src_w
, dst_w
);
203 if (hscale
< 0 || dst_w
== 0)
206 if (hscale
< min_hscale
) {
207 int max_dst_w
= src_w
/ min_hscale
;
209 drm_rect_adjust_size(dst
, max_dst_w
- dst_w
, 0);
214 if (hscale
> max_hscale
) {
215 int max_src_w
= dst_w
* max_hscale
;
217 drm_rect_adjust_size(src
, max_src_w
- src_w
, 0);
224 EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed
);
227 * drm_rect_calc_vscale_relaxed - calculate the vertical scaling factor
228 * @src: source window rectangle
229 * @dst: destination window rectangle
230 * @min_vscale: minimum allowed vertical scaling factor
231 * @max_vscale: maximum allowed vertical scaling factor
233 * Calculate the vertical scaling factor as
234 * (@src height) / (@dst height).
236 * If the calculated scaling factor is below @min_vscale,
237 * decrease the height of rectangle @dst to compensate.
239 * If the calculated scaling factor is above @max_vscale,
240 * decrease the height of rectangle @src to compensate.
243 * The vertical scaling factor.
245 int drm_rect_calc_vscale_relaxed(struct drm_rect
*src
,
246 struct drm_rect
*dst
,
247 int min_vscale
, int max_vscale
)
249 int src_h
= drm_rect_height(src
);
250 int dst_h
= drm_rect_height(dst
);
251 int vscale
= drm_calc_scale(src_h
, dst_h
);
253 if (vscale
< 0 || dst_h
== 0)
256 if (vscale
< min_vscale
) {
257 int max_dst_h
= src_h
/ min_vscale
;
259 drm_rect_adjust_size(dst
, 0, max_dst_h
- dst_h
);
264 if (vscale
> max_vscale
) {
265 int max_src_h
= dst_h
* max_vscale
;
267 drm_rect_adjust_size(src
, 0, max_src_h
- src_h
);
274 EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed
);
277 * drm_rect_debug_print - print the rectangle information
278 * @prefix: prefix string
279 * @r: rectangle to print
280 * @fixed_point: rectangle is in 16.16 fixed point format
282 void drm_rect_debug_print(const char *prefix
, const struct drm_rect
*r
, bool fixed_point
)
284 int w
= drm_rect_width(r
);
285 int h
= drm_rect_height(r
);
288 DRM_DEBUG_KMS("%s%d.%06ux%d.%06u%+d.%06u%+d.%06u\n", prefix
,
289 w
>> 16, ((w
& 0xffff) * 15625) >> 10,
290 h
>> 16, ((h
& 0xffff) * 15625) >> 10,
291 r
->x1
>> 16, ((r
->x1
& 0xffff) * 15625) >> 10,
292 r
->y1
>> 16, ((r
->y1
& 0xffff) * 15625) >> 10);
294 DRM_DEBUG_KMS("%s%dx%d%+d%+d\n", prefix
, w
, h
, r
->x1
, r
->y1
);
296 EXPORT_SYMBOL(drm_rect_debug_print
);
299 * drm_rect_rotate - 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 to be applied
305 * Apply @rotation to the coordinates of rectangle @r.
307 * @width and @height combined with @rotation define
308 * the location of the new origin.
310 * @width correcsponds to the horizontal and @height
311 * to the vertical axis of the untransformed coordinate
314 void drm_rect_rotate(struct drm_rect
*r
,
315 int width
, int height
,
316 unsigned int rotation
)
320 if (rotation
& (DRM_REFLECT_X
| DRM_REFLECT_Y
)) {
323 if (rotation
& DRM_REFLECT_X
) {
324 r
->x1
= width
- tmp
.x2
;
325 r
->x2
= width
- tmp
.x1
;
328 if (rotation
& DRM_REFLECT_Y
) {
329 r
->y1
= height
- tmp
.y2
;
330 r
->y2
= height
- tmp
.y1
;
334 switch (rotation
& DRM_ROTATE_MASK
) {
341 r
->y1
= width
- tmp
.x2
;
342 r
->y2
= width
- tmp
.x1
;
346 r
->x1
= width
- tmp
.x2
;
347 r
->x2
= width
- tmp
.x1
;
348 r
->y1
= height
- tmp
.y2
;
349 r
->y2
= height
- tmp
.y1
;
353 r
->x1
= height
- tmp
.y2
;
354 r
->x2
= height
- tmp
.y1
;
362 EXPORT_SYMBOL(drm_rect_rotate
);
365 * drm_rect_rotate_inv - Inverse rotate the rectangle
366 * @r: rectangle to be rotated
367 * @width: Width of the coordinate space
368 * @height: Height of the coordinate space
369 * @rotation: Transformation whose inverse is to be applied
371 * Apply the inverse of @rotation to the coordinates
374 * @width and @height combined with @rotation define
375 * the location of the new origin.
377 * @width correcsponds to the horizontal and @height
378 * to the vertical axis of the original untransformed
379 * coordinate space, so that you never have to flip
380 * them when doing a rotatation and its inverse.
381 * That is, if you do:
383 * drm_rotate(&r, width, height, rotation);
384 * drm_rotate_inv(&r, width, height, rotation);
386 * you will always get back the original rectangle.
388 void drm_rect_rotate_inv(struct drm_rect
*r
,
389 int width
, int height
,
390 unsigned int rotation
)
394 switch (rotation
& DRM_ROTATE_MASK
) {
399 r
->x1
= width
- tmp
.y2
;
400 r
->x2
= width
- tmp
.y1
;
406 r
->x1
= width
- tmp
.x2
;
407 r
->x2
= width
- tmp
.x1
;
408 r
->y1
= height
- tmp
.y2
;
409 r
->y2
= height
- tmp
.y1
;
415 r
->y1
= height
- tmp
.x2
;
416 r
->y2
= height
- tmp
.x1
;
422 if (rotation
& (DRM_REFLECT_X
| DRM_REFLECT_Y
)) {
425 if (rotation
& DRM_REFLECT_X
) {
426 r
->x1
= width
- tmp
.x2
;
427 r
->x2
= width
- tmp
.x1
;
430 if (rotation
& DRM_REFLECT_Y
) {
431 r
->y1
= height
- tmp
.y2
;
432 r
->y2
= height
- tmp
.y1
;
436 EXPORT_SYMBOL(drm_rect_rotate_inv
);