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>
27 #include <drm/drm_rect.h>
30 * drm_rect_intersect - intersect two rectangles
31 * @r1: first rectangle
32 * @r2: second rectangle
34 * Calculate the intersection of rectangles @r1 and @r2.
35 * @r1 will be overwritten with the intersection.
38 * %true if rectangle @r1 is still visible after the operation,
41 bool drm_rect_intersect(struct drm_rect
*r1
, const struct drm_rect
*r2
)
43 r1
->x1
= max(r1
->x1
, r2
->x1
);
44 r1
->y1
= max(r1
->y1
, r2
->y1
);
45 r1
->x2
= min(r1
->x2
, r2
->x2
);
46 r1
->y2
= min(r1
->y2
, r2
->y2
);
48 return drm_rect_visible(r1
);
50 EXPORT_SYMBOL(drm_rect_intersect
);
53 * drm_rect_clip_scaled - perform a scaled clip operation
54 * @src: source window rectangle
55 * @dst: destination window rectangle
56 * @clip: clip rectangle
57 * @hscale: horizontal scaling factor
58 * @vscale: vertical scaling factor
60 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
61 * same amounts multiplied by @hscale and @vscale.
64 * %true if rectangle @dst is still visible after being clipped,
67 bool drm_rect_clip_scaled(struct drm_rect
*src
, struct drm_rect
*dst
,
68 const struct drm_rect
*clip
,
69 int hscale
, int vscale
)
73 diff
= clip
->x1
- dst
->x1
;
75 int64_t tmp
= src
->x1
+ (int64_t) diff
* hscale
;
76 src
->x1
= clamp_t(int64_t, tmp
, INT_MIN
, INT_MAX
);
78 diff
= clip
->y1
- dst
->y1
;
80 int64_t tmp
= src
->y1
+ (int64_t) diff
* vscale
;
81 src
->y1
= clamp_t(int64_t, tmp
, INT_MIN
, INT_MAX
);
83 diff
= dst
->x2
- clip
->x2
;
85 int64_t tmp
= src
->x2
- (int64_t) diff
* hscale
;
86 src
->x2
= clamp_t(int64_t, tmp
, INT_MIN
, INT_MAX
);
88 diff
= dst
->y2
- clip
->y2
;
90 int64_t tmp
= src
->y2
- (int64_t) diff
* vscale
;
91 src
->y2
= clamp_t(int64_t, tmp
, INT_MIN
, INT_MAX
);
94 return drm_rect_intersect(dst
, clip
);
96 EXPORT_SYMBOL(drm_rect_clip_scaled
);
98 static int drm_calc_scale(int src
, int dst
)
102 if (src
< 0 || dst
< 0)
114 * drm_rect_calc_hscale - calculate the horizontal scaling factor
115 * @src: source window rectangle
116 * @dst: destination window rectangle
117 * @min_hscale: minimum allowed horizontal scaling factor
118 * @max_hscale: maximum allowed horizontal scaling factor
120 * Calculate the horizontal scaling factor as
121 * (@src width) / (@dst width).
124 * The horizontal scaling factor, or errno of out of limits.
126 int drm_rect_calc_hscale(const struct drm_rect
*src
,
127 const struct drm_rect
*dst
,
128 int min_hscale
, int max_hscale
)
130 int src_w
= drm_rect_width(src
);
131 int dst_w
= drm_rect_width(dst
);
132 int hscale
= drm_calc_scale(src_w
, dst_w
);
134 if (hscale
< 0 || dst_w
== 0)
137 if (hscale
< min_hscale
|| hscale
> max_hscale
)
142 EXPORT_SYMBOL(drm_rect_calc_hscale
);
145 * drm_rect_calc_vscale - calculate the vertical scaling factor
146 * @src: source window rectangle
147 * @dst: destination window rectangle
148 * @min_vscale: minimum allowed vertical scaling factor
149 * @max_vscale: maximum allowed vertical scaling factor
151 * Calculate the vertical scaling factor as
152 * (@src height) / (@dst height).
155 * The vertical scaling factor, or errno of out of limits.
157 int drm_rect_calc_vscale(const struct drm_rect
*src
,
158 const struct drm_rect
*dst
,
159 int min_vscale
, int max_vscale
)
161 int src_h
= drm_rect_height(src
);
162 int dst_h
= drm_rect_height(dst
);
163 int vscale
= drm_calc_scale(src_h
, dst_h
);
165 if (vscale
< 0 || dst_h
== 0)
168 if (vscale
< min_vscale
|| vscale
> max_vscale
)
173 EXPORT_SYMBOL(drm_rect_calc_vscale
);
176 * drm_calc_hscale_relaxed - calculate the horizontal scaling factor
177 * @src: source window rectangle
178 * @dst: destination window rectangle
179 * @min_hscale: minimum allowed horizontal scaling factor
180 * @max_hscale: maximum allowed horizontal scaling factor
182 * Calculate the horizontal scaling factor as
183 * (@src width) / (@dst width).
185 * If the calculated scaling factor is below @min_vscale,
186 * decrease the height of rectangle @dst to compensate.
188 * If the calculated scaling factor is above @max_vscale,
189 * decrease the height of rectangle @src to compensate.
192 * The horizontal scaling factor.
194 int drm_rect_calc_hscale_relaxed(struct drm_rect
*src
,
195 struct drm_rect
*dst
,
196 int min_hscale
, int max_hscale
)
198 int src_w
= drm_rect_width(src
);
199 int dst_w
= drm_rect_width(dst
);
200 int hscale
= drm_calc_scale(src_w
, dst_w
);
202 if (hscale
< 0 || dst_w
== 0)
205 if (hscale
< min_hscale
) {
206 int max_dst_w
= src_w
/ min_hscale
;
208 drm_rect_adjust_size(dst
, max_dst_w
- dst_w
, 0);
213 if (hscale
> max_hscale
) {
214 int max_src_w
= dst_w
* max_hscale
;
216 drm_rect_adjust_size(src
, max_src_w
- src_w
, 0);
223 EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed
);
226 * drm_rect_calc_vscale_relaxed - calculate the vertical scaling factor
227 * @src: source window rectangle
228 * @dst: destination window rectangle
229 * @min_vscale: minimum allowed vertical scaling factor
230 * @max_vscale: maximum allowed vertical scaling factor
232 * Calculate the vertical scaling factor as
233 * (@src height) / (@dst height).
235 * If the calculated scaling factor is below @min_vscale,
236 * decrease the height of rectangle @dst to compensate.
238 * If the calculated scaling factor is above @max_vscale,
239 * decrease the height of rectangle @src to compensate.
242 * The vertical scaling factor.
244 int drm_rect_calc_vscale_relaxed(struct drm_rect
*src
,
245 struct drm_rect
*dst
,
246 int min_vscale
, int max_vscale
)
248 int src_h
= drm_rect_height(src
);
249 int dst_h
= drm_rect_height(dst
);
250 int vscale
= drm_calc_scale(src_h
, dst_h
);
252 if (vscale
< 0 || dst_h
== 0)
255 if (vscale
< min_vscale
) {
256 int max_dst_h
= src_h
/ min_vscale
;
258 drm_rect_adjust_size(dst
, 0, max_dst_h
- dst_h
);
263 if (vscale
> max_vscale
) {
264 int max_src_h
= dst_h
* max_vscale
;
266 drm_rect_adjust_size(src
, 0, max_src_h
- src_h
);
273 EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed
);