1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/errno.h>
25#include <linux/export.h>
26#include <linux/kernel.h>
27
28#include <drm/drm_mode.h>
29#include <drm/drm_print.h>
30#include <drm/drm_rect.h>
31
32
33
34
35
36
37
38
39
40
41
42
43
44bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
45{
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);
50
51 return drm_rect_visible(r1);
52}
53EXPORT_SYMBOL(drm_rect_intersect);
54
55static u32 clip_scaled(int src, int dst, int *clip)
56{
57 u64 tmp;
58
59 if (dst == 0)
60 return 0;
61
62
63 *clip = min(*clip, dst);
64
65 tmp = mul_u32_u32(src, dst - *clip);
66
67
68
69
70
71 if (src < (dst << 16))
72 return DIV_ROUND_UP_ULL(tmp, dst);
73 else
74 return DIV_ROUND_DOWN_ULL(tmp, dst);
75}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
93 const struct drm_rect *clip)
94{
95 int diff;
96
97 diff = clip->x1 - dst->x1;
98 if (diff > 0) {
99 u32 new_src_w = clip_scaled(drm_rect_width(src),
100 drm_rect_width(dst), &diff);
101
102 src->x1 = src->x2 - new_src_w;
103 dst->x1 += diff;
104 }
105 diff = clip->y1 - dst->y1;
106 if (diff > 0) {
107 u32 new_src_h = clip_scaled(drm_rect_height(src),
108 drm_rect_height(dst), &diff);
109
110 src->y1 = src->y2 - new_src_h;
111 dst->y1 += diff;
112 }
113 diff = dst->x2 - clip->x2;
114 if (diff > 0) {
115 u32 new_src_w = clip_scaled(drm_rect_width(src),
116 drm_rect_width(dst), &diff);
117
118 src->x2 = src->x1 + new_src_w;
119 dst->x2 -= diff;
120 }
121 diff = dst->y2 - clip->y2;
122 if (diff > 0) {
123 u32 new_src_h = clip_scaled(drm_rect_height(src),
124 drm_rect_height(dst), &diff);
125
126 src->y2 = src->y1 + new_src_h;
127 dst->y2 -= diff;
128 }
129
130 return drm_rect_visible(dst);
131}
132EXPORT_SYMBOL(drm_rect_clip_scaled);
133
134static int drm_calc_scale(int src, int dst)
135{
136 int scale = 0;
137
138 if (WARN_ON(src < 0 || dst < 0))
139 return -EINVAL;
140
141 if (dst == 0)
142 return 0;
143
144 if (src > (dst << 16))
145 return DIV_ROUND_UP(src, dst);
146 else
147 scale = src / dst;
148
149 return scale;
150}
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169int drm_rect_calc_hscale(const struct drm_rect *src,
170 const struct drm_rect *dst,
171 int min_hscale, int max_hscale)
172{
173 int src_w = drm_rect_width(src);
174 int dst_w = drm_rect_width(dst);
175 int hscale = drm_calc_scale(src_w, dst_w);
176
177 if (hscale < 0 || dst_w == 0)
178 return hscale;
179
180 if (hscale < min_hscale || hscale > max_hscale)
181 return -ERANGE;
182
183 return hscale;
184}
185EXPORT_SYMBOL(drm_rect_calc_hscale);
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204int drm_rect_calc_vscale(const struct drm_rect *src,
205 const struct drm_rect *dst,
206 int min_vscale, int max_vscale)
207{
208 int src_h = drm_rect_height(src);
209 int dst_h = drm_rect_height(dst);
210 int vscale = drm_calc_scale(src_h, dst_h);
211
212 if (vscale < 0 || dst_h == 0)
213 return vscale;
214
215 if (vscale < min_vscale || vscale > max_vscale)
216 return -ERANGE;
217
218 return vscale;
219}
220EXPORT_SYMBOL(drm_rect_calc_vscale);
221
222
223
224
225
226
227
228void drm_rect_debug_print(const char *prefix, const struct drm_rect *r, bool fixed_point)
229{
230 if (fixed_point)
231 DRM_DEBUG_KMS("%s" DRM_RECT_FP_FMT "\n", prefix, DRM_RECT_FP_ARG(r));
232 else
233 DRM_DEBUG_KMS("%s" DRM_RECT_FMT "\n", prefix, DRM_RECT_ARG(r));
234}
235EXPORT_SYMBOL(drm_rect_debug_print);
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253void drm_rect_rotate(struct drm_rect *r,
254 int width, int height,
255 unsigned int rotation)
256{
257 struct drm_rect tmp;
258
259 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
260 tmp = *r;
261
262 if (rotation & DRM_MODE_REFLECT_X) {
263 r->x1 = width - tmp.x2;
264 r->x2 = width - tmp.x1;
265 }
266
267 if (rotation & DRM_MODE_REFLECT_Y) {
268 r->y1 = height - tmp.y2;
269 r->y2 = height - tmp.y1;
270 }
271 }
272
273 switch (rotation & DRM_MODE_ROTATE_MASK) {
274 case DRM_MODE_ROTATE_0:
275 break;
276 case DRM_MODE_ROTATE_90:
277 tmp = *r;
278 r->x1 = tmp.y1;
279 r->x2 = tmp.y2;
280 r->y1 = width - tmp.x2;
281 r->y2 = width - tmp.x1;
282 break;
283 case DRM_MODE_ROTATE_180:
284 tmp = *r;
285 r->x1 = width - tmp.x2;
286 r->x2 = width - tmp.x1;
287 r->y1 = height - tmp.y2;
288 r->y2 = height - tmp.y1;
289 break;
290 case DRM_MODE_ROTATE_270:
291 tmp = *r;
292 r->x1 = height - tmp.y2;
293 r->x2 = height - tmp.y1;
294 r->y1 = tmp.x1;
295 r->y2 = tmp.x2;
296 break;
297 default:
298 break;
299 }
300}
301EXPORT_SYMBOL(drm_rect_rotate);
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327void drm_rect_rotate_inv(struct drm_rect *r,
328 int width, int height,
329 unsigned int rotation)
330{
331 struct drm_rect tmp;
332
333 switch (rotation & DRM_MODE_ROTATE_MASK) {
334 case DRM_MODE_ROTATE_0:
335 break;
336 case DRM_MODE_ROTATE_90:
337 tmp = *r;
338 r->x1 = width - tmp.y2;
339 r->x2 = width - tmp.y1;
340 r->y1 = tmp.x1;
341 r->y2 = tmp.x2;
342 break;
343 case DRM_MODE_ROTATE_180:
344 tmp = *r;
345 r->x1 = width - tmp.x2;
346 r->x2 = width - tmp.x1;
347 r->y1 = height - tmp.y2;
348 r->y2 = height - tmp.y1;
349 break;
350 case DRM_MODE_ROTATE_270:
351 tmp = *r;
352 r->x1 = tmp.y1;
353 r->x2 = tmp.y2;
354 r->y1 = height - tmp.x2;
355 r->y2 = height - tmp.x1;
356 break;
357 default:
358 break;
359 }
360
361 if (rotation & (DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y)) {
362 tmp = *r;
363
364 if (rotation & DRM_MODE_REFLECT_X) {
365 r->x1 = width - tmp.x2;
366 r->x2 = width - tmp.x1;
367 }
368
369 if (rotation & DRM_MODE_REFLECT_Y) {
370 r->y1 = height - tmp.y2;
371 r->y2 = height - tmp.y1;
372 }
373 }
374}
375EXPORT_SYMBOL(drm_rect_rotate_inv);
376