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#include <drm/drmP.h>
28#include <drm/drm_rect.h>
29
30
31
32
33
34
35
36
37
38
39
40
41
42bool drm_rect_intersect(struct drm_rect *r1, const struct drm_rect *r2)
43{
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);
48
49 return drm_rect_visible(r1);
50}
51EXPORT_SYMBOL(drm_rect_intersect);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68bool drm_rect_clip_scaled(struct drm_rect *src, struct drm_rect *dst,
69 const struct drm_rect *clip,
70 int hscale, int vscale)
71{
72 int diff;
73
74 diff = clip->x1 - dst->x1;
75 if (diff > 0) {
76 int64_t tmp = src->x1 + (int64_t) diff * hscale;
77 src->x1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
78 }
79 diff = clip->y1 - dst->y1;
80 if (diff > 0) {
81 int64_t tmp = src->y1 + (int64_t) diff * vscale;
82 src->y1 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
83 }
84 diff = dst->x2 - clip->x2;
85 if (diff > 0) {
86 int64_t tmp = src->x2 - (int64_t) diff * hscale;
87 src->x2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
88 }
89 diff = dst->y2 - clip->y2;
90 if (diff > 0) {
91 int64_t tmp = src->y2 - (int64_t) diff * vscale;
92 src->y2 = clamp_t(int64_t, tmp, INT_MIN, INT_MAX);
93 }
94
95 return drm_rect_intersect(dst, clip);
96}
97EXPORT_SYMBOL(drm_rect_clip_scaled);
98
99static int drm_calc_scale(int src, int dst)
100{
101 int scale = 0;
102
103 if (src < 0 || dst < 0)
104 return -EINVAL;
105
106 if (dst == 0)
107 return 0;
108
109 scale = src / dst;
110
111 return scale;
112}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127int drm_rect_calc_hscale(const struct drm_rect *src,
128 const struct drm_rect *dst,
129 int min_hscale, int max_hscale)
130{
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);
134
135 if (hscale < 0 || dst_w == 0)
136 return hscale;
137
138 if (hscale < min_hscale || hscale > max_hscale)
139 return -ERANGE;
140
141 return hscale;
142}
143EXPORT_SYMBOL(drm_rect_calc_hscale);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158int drm_rect_calc_vscale(const struct drm_rect *src,
159 const struct drm_rect *dst,
160 int min_vscale, int max_vscale)
161{
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);
165
166 if (vscale < 0 || dst_h == 0)
167 return vscale;
168
169 if (vscale < min_vscale || vscale > max_vscale)
170 return -ERANGE;
171
172 return vscale;
173}
174EXPORT_SYMBOL(drm_rect_calc_vscale);
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195int drm_rect_calc_hscale_relaxed(struct drm_rect *src,
196 struct drm_rect *dst,
197 int min_hscale, int max_hscale)
198{
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);
202
203 if (hscale < 0 || dst_w == 0)
204 return hscale;
205
206 if (hscale < min_hscale) {
207 int max_dst_w = src_w / min_hscale;
208
209 drm_rect_adjust_size(dst, max_dst_w - dst_w, 0);
210
211 return min_hscale;
212 }
213
214 if (hscale > max_hscale) {
215 int max_src_w = dst_w * max_hscale;
216
217 drm_rect_adjust_size(src, max_src_w - src_w, 0);
218
219 return max_hscale;
220 }
221
222 return hscale;
223}
224EXPORT_SYMBOL(drm_rect_calc_hscale_relaxed);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245int drm_rect_calc_vscale_relaxed(struct drm_rect *src,
246 struct drm_rect *dst,
247 int min_vscale, int max_vscale)
248{
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);
252
253 if (vscale < 0 || dst_h == 0)
254 return vscale;
255
256 if (vscale < min_vscale) {
257 int max_dst_h = src_h / min_vscale;
258
259 drm_rect_adjust_size(dst, 0, max_dst_h - dst_h);
260
261 return min_vscale;
262 }
263
264 if (vscale > max_vscale) {
265 int max_src_h = dst_h * max_vscale;
266
267 drm_rect_adjust_size(src, 0, max_src_h - src_h);
268
269 return max_vscale;
270 }
271
272 return vscale;
273}
274EXPORT_SYMBOL(drm_rect_calc_vscale_relaxed);
275
276
277
278
279
280
281void drm_rect_debug_print(const struct drm_rect *r, bool fixed_point)
282{
283 int w = drm_rect_width(r);
284 int h = drm_rect_height(r);
285
286 if (fixed_point)
287 DRM_DEBUG_KMS("%d.%06ux%d.%06u%+d.%06u%+d.%06u\n",
288 w >> 16, ((w & 0xffff) * 15625) >> 10,
289 h >> 16, ((h & 0xffff) * 15625) >> 10,
290 r->x1 >> 16, ((r->x1 & 0xffff) * 15625) >> 10,
291 r->y1 >> 16, ((r->y1 & 0xffff) * 15625) >> 10);
292 else
293 DRM_DEBUG_KMS("%dx%d%+d%+d\n", w, h, r->x1, r->y1);
294}
295EXPORT_SYMBOL(drm_rect_debug_print);
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313void drm_rect_rotate(struct drm_rect *r,
314 int width, int height,
315 unsigned int rotation)
316{
317 struct drm_rect tmp;
318
319 if (rotation & (BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y))) {
320 tmp = *r;
321
322 if (rotation & BIT(DRM_REFLECT_X)) {
323 r->x1 = width - tmp.x2;
324 r->x2 = width - tmp.x1;
325 }
326
327 if (rotation & BIT(DRM_REFLECT_Y)) {
328 r->y1 = height - tmp.y2;
329 r->y2 = height - tmp.y1;
330 }
331 }
332
333 switch (rotation & 0xf) {
334 case BIT(DRM_ROTATE_0):
335 break;
336 case BIT(DRM_ROTATE_90):
337 tmp = *r;
338 r->x1 = tmp.y1;
339 r->x2 = tmp.y2;
340 r->y1 = width - tmp.x2;
341 r->y2 = width - tmp.x1;
342 break;
343 case BIT(DRM_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 BIT(DRM_ROTATE_270):
351 tmp = *r;
352 r->x1 = height - tmp.y2;
353 r->x2 = height - tmp.y1;
354 r->y1 = tmp.x1;
355 r->y2 = tmp.x2;
356 break;
357 default:
358 break;
359 }
360}
361EXPORT_SYMBOL(drm_rect_rotate);
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387void drm_rect_rotate_inv(struct drm_rect *r,
388 int width, int height,
389 unsigned int rotation)
390{
391 struct drm_rect tmp;
392
393 switch (rotation & 0xf) {
394 case BIT(DRM_ROTATE_0):
395 break;
396 case BIT(DRM_ROTATE_90):
397 tmp = *r;
398 r->x1 = width - tmp.y2;
399 r->x2 = width - tmp.y1;
400 r->y1 = tmp.x1;
401 r->y2 = tmp.x2;
402 break;
403 case BIT(DRM_ROTATE_180):
404 tmp = *r;
405 r->x1 = width - tmp.x2;
406 r->x2 = width - tmp.x1;
407 r->y1 = height - tmp.y2;
408 r->y2 = height - tmp.y1;
409 break;
410 case BIT(DRM_ROTATE_270):
411 tmp = *r;
412 r->x1 = tmp.y1;
413 r->x2 = tmp.y2;
414 r->y1 = height - tmp.x2;
415 r->y2 = height - tmp.x1;
416 break;
417 default:
418 break;
419 }
420
421 if (rotation & (BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y))) {
422 tmp = *r;
423
424 if (rotation & BIT(DRM_REFLECT_X)) {
425 r->x1 = width - tmp.x2;
426 r->x2 = width - tmp.x1;
427 }
428
429 if (rotation & BIT(DRM_REFLECT_Y)) {
430 r->y1 = height - tmp.y2;
431 r->y2 = height - tmp.y1;
432 }
433 }
434}
435EXPORT_SYMBOL(drm_rect_rotate_inv);
436