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