drm: Add drm_rect_calc_{hscale, vscale}() utility functions
[deliverable/linux.git] / drivers / gpu / drm / drm_rect.c
1 /*
2 * Copyright (C) 2011-2013 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 */
23
24 #include <linux/errno.h>
25 #include <linux/export.h>
26 #include <linux/kernel.h>
27 #include <drm/drm_rect.h>
28
29 /**
30 * drm_rect_intersect - intersect two rectangles
31 * @r1: first rectangle
32 * @r2: second rectangle
33 *
34 * Calculate the intersection of rectangles @r1 and @r2.
35 * @r1 will be overwritten with the intersection.
36 *
37 * RETURNS:
38 * %true if rectangle @r1 is still visible after the operation,
39 * %false otherwise.
40 */
41 bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
42 {
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);
47
48 return drm_rect_visible(r1);
49 }
50 EXPORT_SYMBOL(drm_rect_intersect);
51
52 /**
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
59 *
60 * Clip rectangle @dst by rectangle @clip. Clip rectangle @src by the
61 * same amounts multiplied by @hscale and @vscale.
62 *
63 * RETURNS:
64 * %true if rectangle @dst is still visible after being clipped,
65 * %false otherwise
66 */
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)
70 {
71 int diff;
72
73 diff = clip->x1 - dst->x1;
74 if (diff > 0) {
75 int64_t tmp = src->x1 + (int64_t) diff * hscale;
76 src->x1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
77 }
78 diff = clip->y1 - dst->y1;
79 if (diff > 0) {
80 int64_t tmp = src->y1 + (int64_t) diff * vscale;
81 src->y1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
82 }
83 diff = dst->x2 - clip->x2;
84 if (diff > 0) {
85 int64_t tmp = src->x2 - (int64_t) diff * hscale;
86 src->x2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
87 }
88 diff = dst->y2 - clip->y2;
89 if (diff > 0) {
90 int64_t tmp = src->y2 - (int64_t) diff * vscale;
91 src->y2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
92 }
93
94 return drm_rect_intersect(dst, clip);
95 }
96 EXPORT_SYMBOL(drm_rect_clip_scaled);
97
98 static int drm_calc_scale(int src, int dst)
99 {
100 int scale = 0;
101
102 if (src < 0 || dst < 0)
103 return -EINVAL;
104
105 if (dst == 0)
106 return 0;
107
108 scale = src / dst;
109
110 return scale;
111 }
112
113 /**
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
119 *
120 * Calculate the horizontal scaling factor as
121 * (@src width) / (@dst width).
122 *
123 * RETURNS:
124 * The horizontal scaling factor, or errno of out of limits.
125 */
126 int drm_rect_calc_hscale(const struct drm_rect *src,
127 const struct drm_rect *dst,
128 int min_hscale, int max_hscale)
129 {
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);
133
134 if (hscale < 0 || dst_w == 0)
135 return hscale;
136
137 if (hscale < min_hscale || hscale > max_hscale)
138 return -ERANGE;
139
140 return hscale;
141 }
142 EXPORT_SYMBOL(drm_rect_calc_hscale);
143
144 /**
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
150 *
151 * Calculate the vertical scaling factor as
152 * (@src height) / (@dst height).
153 *
154 * RETURNS:
155 * The vertical scaling factor, or errno of out of limits.
156 */
157 int drm_rect_calc_vscale(const struct drm_rect *src,
158 const struct drm_rect *dst,
159 int min_vscale, int max_vscale)
160 {
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);
164
165 if (vscale < 0 || dst_h == 0)
166 return vscale;
167
168 if (vscale < min_vscale || vscale > max_vscale)
169 return -ERANGE;
170
171 return vscale;
172 }
173 EXPORT_SYMBOL(drm_rect_calc_vscale);
174
175 /**
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
181 *
182 * Calculate the horizontal scaling factor as
183 * (@src width) / (@dst width).
184 *
185 * If the calculated scaling factor is below @min_vscale,
186 * decrease the height of rectangle @dst to compensate.
187 *
188 * If the calculated scaling factor is above @max_vscale,
189 * decrease the height of rectangle @src to compensate.
190 *
191 * RETURNS:
192 * The horizontal scaling factor.
193 */
194 int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
195 struct drm_rect *dst,
196 int min_hscale, int max_hscale)
197 {
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);
201
202 if (hscale < 0 || dst_w == 0)
203 return hscale;
204
205 if (hscale < min_hscale) {
206 int max_dst_w = src_w / min_hscale;
207
208 drm_rect_adjust_size(dst, max_dst_w - dst_w, 0);
209
210 return min_hscale;
211 }
212
213 if (hscale > max_hscale) {
214 int max_src_w = dst_w * max_hscale;
215
216 drm_rect_adjust_size(src, max_src_w - src_w, 0);
217
218 return max_hscale;
219 }
220
221 return hscale;
222 }
223 EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed);
224
225 /**
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
231 *
232 * Calculate the vertical scaling factor as
233 * (@src height) / (@dst height).
234 *
235 * If the calculated scaling factor is below @min_vscale,
236 * decrease the height of rectangle @dst to compensate.
237 *
238 * If the calculated scaling factor is above @max_vscale,
239 * decrease the height of rectangle @src to compensate.
240 *
241 * RETURNS:
242 * The vertical scaling factor.
243 */
244 int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
245 struct drm_rect *dst,
246 int min_vscale, int max_vscale)
247 {
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);
251
252 if (vscale < 0 || dst_h == 0)
253 return vscale;
254
255 if (vscale < min_vscale) {
256 int max_dst_h = src_h / min_vscale;
257
258 drm_rect_adjust_size(dst, 0, max_dst_h - dst_h);
259
260 return min_vscale;
261 }
262
263 if (vscale > max_vscale) {
264 int max_src_h = dst_h * max_vscale;
265
266 drm_rect_adjust_size(src, 0, max_src_h - src_h);
267
268 return max_vscale;
269 }
270
271 return vscale;
272 }
273 EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed);
This page took 0.039066 seconds and 5 git commands to generate.