1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
// SPDX-License-Identifier: GPL-2.0
/*
* Test cases for the drm_rect functions
*
* Copyright (c) 2022 Maíra Canal <mairacanal@riseup.net>
*/
#include <kunit/test.h>
#include <drm/drm_rect.h>
#include <linux/string_helpers.h>
#include <linux/errno.h>
static void drm_rect_compare(struct kunit *test, const struct drm_rect *r,
const struct drm_rect *expected)
{
KUNIT_EXPECT_EQ(test, r->x1, expected->x1);
KUNIT_EXPECT_EQ(test, r->y1, expected->y1);
KUNIT_EXPECT_EQ(test, drm_rect_width(r), drm_rect_width(expected));
KUNIT_EXPECT_EQ(test, drm_rect_height(r), drm_rect_height(expected));
}
static void drm_test_rect_clip_scaled_div_by_zero(struct kunit *test)
{
struct drm_rect src, dst, clip;
bool visible;
/*
* Make sure we don't divide by zero when dst
* width/height is zero and dst and clip do not intersect.
*/
drm_rect_init(&src, 0, 0, 0, 0);
drm_rect_init(&dst, 0, 0, 0, 0);
drm_rect_init(&clip, 1, 1, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, visible, "Destination not be visible\n");
KUNIT_EXPECT_FALSE_MSG(test, drm_rect_visible(&src), "Source should not be visible\n");
drm_rect_init(&src, 0, 0, 0, 0);
drm_rect_init(&dst, 3, 3, 0, 0);
drm_rect_init(&clip, 1, 1, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, visible, "Destination not be visible\n");
KUNIT_EXPECT_FALSE_MSG(test, drm_rect_visible(&src), "Source should not be visible\n");
}
static void drm_test_rect_clip_scaled_not_clipped(struct kunit *test)
{
struct drm_rect src, dst, clip;
bool visible;
/* 1:1 scaling */
drm_rect_init(&src, 0, 0, 1 << 16, 1 << 16);
drm_rect_init(&dst, 0, 0, 1, 1);
drm_rect_init(&clip, 0, 0, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 0 || src.x2 != 1 << 16 ||
src.y1 != 0 || src.y2 != 1 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 0 || dst.x2 != 1 ||
dst.y1 != 0 || dst.y2 != 1, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
/* 2:1 scaling */
drm_rect_init(&src, 0, 0, 2 << 16, 2 << 16);
drm_rect_init(&dst, 0, 0, 1, 1);
drm_rect_init(&clip, 0, 0, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 0 || src.x2 != 2 << 16 ||
src.y1 != 0 || src.y2 != 2 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 0 || dst.x2 != 1 ||
dst.y1 != 0 || dst.y2 != 1, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
/* 1:2 scaling */
drm_rect_init(&src, 0, 0, 1 << 16, 1 << 16);
drm_rect_init(&dst, 0, 0, 2, 2);
drm_rect_init(&clip, 0, 0, 2, 2);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 0 || src.x2 != 1 << 16 ||
src.y1 != 0 || src.y2 != 1 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 0 || dst.x2 != 2 ||
dst.y1 != 0 || dst.y2 != 2, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
}
static void drm_test_rect_clip_scaled_clipped(struct kunit *test)
{
struct drm_rect src, dst, clip;
bool visible;
/* 1:1 scaling top/left clip */
drm_rect_init(&src, 0, 0, 2 << 16, 2 << 16);
drm_rect_init(&dst, 0, 0, 2, 2);
drm_rect_init(&clip, 0, 0, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 0 || src.x2 != 1 << 16 ||
src.y1 != 0 || src.y2 != 1 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 0 || dst.x2 != 1 ||
dst.y1 != 0 || dst.y2 != 1, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
/* 1:1 scaling bottom/right clip */
drm_rect_init(&src, 0, 0, 2 << 16, 2 << 16);
drm_rect_init(&dst, 0, 0, 2, 2);
drm_rect_init(&clip, 1, 1, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 1 << 16 || src.x2 != 2 << 16 ||
src.y1 != 1 << 16 || src.y2 != 2 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 1 || dst.x2 != 2 || dst.y1 != 1 ||
dst.y2 != 2, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
/* 2:1 scaling top/left clip */
drm_rect_init(&src, 0, 0, 4 << 16, 4 << 16);
drm_rect_init(&dst, 0, 0, 2, 2);
drm_rect_init(&clip, 0, 0, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 0 || src.x2 != 2 << 16 ||
src.y1 != 0 || src.y2 != 2 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 0 || dst.x2 != 1 || dst.y1 != 0 ||
dst.y2 != 1, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
/* 2:1 scaling bottom/right clip */
drm_rect_init(&src, 0, 0, 4 << 16, 4 << 16);
drm_rect_init(&dst, 0, 0, 2, 2);
drm_rect_init(&clip, 1, 1, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 2 << 16 || src.x2 != 4 << 16 ||
src.y1 != 2 << 16 || src.y2 != 4 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 1 || dst.x2 != 2 || dst.y1 != 1 ||
dst.y2 != 2, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
/* 1:2 scaling top/left clip */
drm_rect_init(&src, 0, 0, 2 << 16, 2 << 16);
drm_rect_init(&dst, 0, 0, 4, 4);
drm_rect_init(&clip, 0, 0, 2, 2);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 0 || src.x2 != 1 << 16 ||
src.y1 != 0 || src.y2 != 1 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 0 || dst.x2 != 2 || dst.y1 != 0 ||
dst.y2 != 2, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
/* 1:2 scaling bottom/right clip */
drm_rect_init(&src, 0, 0, 2 << 16, 2 << 16);
drm_rect_init(&dst, 0, 0, 4, 4);
drm_rect_init(&clip, 2, 2, 2, 2);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, src.x1 != 1 << 16 || src.x2 != 2 << 16 ||
src.y1 != 1 << 16 || src.y2 != 2 << 16, "Source badly clipped\n");
KUNIT_EXPECT_FALSE_MSG(test, dst.x1 != 2 || dst.x2 != 4 || dst.y1 != 2 ||
dst.y2 != 4, "Destination badly clipped\n");
KUNIT_EXPECT_TRUE_MSG(test, visible, "Destination should be visible\n");
KUNIT_EXPECT_TRUE_MSG(test, drm_rect_visible(&src), "Source should be visible\n");
}
static void drm_test_rect_clip_scaled_signed_vs_unsigned(struct kunit *test)
{
struct drm_rect src, dst, clip;
bool visible;
/*
* 'clip.x2 - dst.x1 >= dst width' could result a negative
* src rectangle width which is no longer expected by the
* code as it's using unsigned types. This could lead to
* the clipped source rectangle appering visible when it
* should have been fully clipped. Make sure both rectangles
* end up invisible.
*/
drm_rect_init(&src, 0, 0, INT_MAX, INT_MAX);
drm_rect_init(&dst, 0, 0, 2, 2);
drm_rect_init(&clip, 3, 3, 1, 1);
visible = drm_rect_clip_scaled(&src, &dst, &clip);
KUNIT_EXPECT_FALSE_MSG(test, visible, "Destination should not be visible\n");
KUNIT_EXPECT_FALSE_MSG(test, drm_rect_visible(&src), "Source should not be visible\n");
}
struct drm_rect_intersect_case {
const char *description;
struct drm_rect r1, r2;
bool should_be_visible;
struct drm_rect expected_intersection;
};
static const struct drm_rect_intersect_case drm_rect_intersect_cases[] = {
{
.description = "top-left x bottom-right",
.r1 = DRM_RECT_INIT(1, 1, 2, 2),
.r2 = DRM_RECT_INIT(0, 0, 2, 2),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(1, 1, 1, 1),
},
{
.description = "top-right x bottom-left",
.r1 = DRM_RECT_INIT(0, 0, 2, 2),
.r2 = DRM_RECT_INIT(1, -1, 2, 2),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(1, 0, 1, 1),
},
{
.description = "bottom-left x top-right",
.r1 = DRM_RECT_INIT(1, -1, 2, 2),
.r2 = DRM_RECT_INIT(0, 0, 2, 2),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(1, 0, 1, 1),
},
{
.description = "bottom-right x top-left",
.r1 = DRM_RECT_INIT(0, 0, 2, 2),
.r2 = DRM_RECT_INIT(1, 1, 2, 2),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(1, 1, 1, 1),
},
{
.description = "right x left",
.r1 = DRM_RECT_INIT(0, 0, 2, 1),
.r2 = DRM_RECT_INIT(1, 0, 3, 1),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(1, 0, 1, 1),
},
{
.description = "left x right",
.r1 = DRM_RECT_INIT(1, 0, 3, 1),
.r2 = DRM_RECT_INIT(0, 0, 2, 1),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(1, 0, 1, 1),
},
{
.description = "up x bottom",
.r1 = DRM_RECT_INIT(0, 0, 1, 2),
.r2 = DRM_RECT_INIT(0, -1, 1, 3),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(0, 0, 1, 2),
},
{
.description = "bottom x up",
.r1 = DRM_RECT_INIT(0, -1, 1, 3),
.r2 = DRM_RECT_INIT(0, 0, 1, 2),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(0, 0, 1, 2),
},
{
.description = "touching corner",
.r1 = DRM_RECT_INIT(0, 0, 1, 1),
.r2 = DRM_RECT_INIT(1, 1, 2, 2),
.should_be_visible = false,
.expected_intersection = DRM_RECT_INIT(1, 1, 0, 0),
},
{
.description = "touching side",
.r1 = DRM_RECT_INIT(0, 0, 1, 1),
.r2 = DRM_RECT_INIT(1, 0, 1, 1),
.should_be_visible = false,
.expected_intersection = DRM_RECT_INIT(1, 0, 0, 1),
},
{
.description = "equal rects",
.r1 = DRM_RECT_INIT(0, 0, 2, 2),
.r2 = DRM_RECT_INIT(0, 0, 2, 2),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(0, 0, 2, 2),
},
{
.description = "inside another",
.r1 = DRM_RECT_INIT(0, 0, 2, 2),
.r2 = DRM_RECT_INIT(1, 1, 1, 1),
.should_be_visible = true,
.expected_intersection = DRM_RECT_INIT(1, 1, 1, 1),
},
{
.description = "far away",
.r1 = DRM_RECT_INIT(0, 0, 1, 1),
.r2 = DRM_RECT_INIT(3, 6, 1, 1),
.should_be_visible = false,
.expected_intersection = DRM_RECT_INIT(3, 6, -2, -5),
},
{
.description = "points intersecting",
.r1 = DRM_RECT_INIT(5, 10, 0, 0),
.r2 = DRM_RECT_INIT(5, 10, 0, 0),
.should_be_visible = false,
.expected_intersection = DRM_RECT_INIT(5, 10, 0, 0),
},
{
.description = "points not intersecting",
.r1 = DRM_RECT_INIT(0, 0, 0, 0),
.r2 = DRM_RECT_INIT(5, 10, 0, 0),
.should_be_visible = false,
.expected_intersection = DRM_RECT_INIT(5, 10, -5, -10),
},
};
static void drm_rect_intersect_case_desc(const struct drm_rect_intersect_case *t, char *desc)
{
snprintf(desc, KUNIT_PARAM_DESC_SIZE,
"%s: " DRM_RECT_FMT " x " DRM_RECT_FMT,
t->description, DRM_RECT_ARG(&t->r1), DRM_RECT_ARG(&t->r2));
}
KUNIT_ARRAY_PARAM(drm_rect_intersect, drm_rect_intersect_cases, drm_rect_intersect_case_desc);
static void drm_test_rect_intersect(struct kunit *test)
{
const struct drm_rect_intersect_case *params = test->param_value;
struct drm_rect r1_aux = params->r1;
bool visible;
visible = drm_rect_intersect(&r1_aux, ¶ms->r2);
KUNIT_EXPECT_EQ(test, visible, params->should_be_visible);
drm_rect_compare(test, &r1_aux, ¶ms->expected_intersection);
}
struct drm_rect_scale_case {
const char *name;
struct drm_rect src, dst;
int min_range, max_range;
int expected_scaling_factor;
};
static const struct drm_rect_scale_case drm_rect_scale_cases[] = {
{
.name = "normal use",
.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
.min_range = 0, .max_range = INT_MAX,
.expected_scaling_factor = 2,
},
{
.name = "out of max range",
.src = DRM_RECT_INIT(0, 0, 10 << 16, 10 << 16),
.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
.min_range = 3, .max_range = 5,
.expected_scaling_factor = -ERANGE,
},
{
.name = "out of min range",
.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
.min_range = 3, .max_range = 5,
.expected_scaling_factor = -ERANGE,
},
{
.name = "zero dst",
.src = DRM_RECT_INIT(0, 0, 2 << 16, 2 << 16),
.dst = DRM_RECT_INIT(0, 0, 0 << 16, 0 << 16),
.min_range = 0, .max_range = INT_MAX,
.expected_scaling_factor = 0,
},
{
.name = "negative src",
.src = DRM_RECT_INIT(0, 0, -(1 << 16), -(1 << 16)),
.dst = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
.min_range = 0, .max_range = INT_MAX,
.expected_scaling_factor = -EINVAL,
},
{
.name = "negative dst",
.src = DRM_RECT_INIT(0, 0, 1 << 16, 1 << 16),
.dst = DRM_RECT_INIT(0, 0, -(1 << 16), -(1 << 16)),
.min_range = 0, .max_range = INT_MAX,
.expected_scaling_factor = -EINVAL,
},
};
static void drm_rect_scale_case_desc(const struct drm_rect_scale_case *t, char *desc)
{
strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
}
KUNIT_ARRAY_PARAM(drm_rect_scale, drm_rect_scale_cases, drm_rect_scale_case_desc);
static void drm_test_rect_calc_hscale(struct kunit *test)
{
const struct drm_rect_scale_case *params = test->param_value;
int scaling_factor;
scaling_factor = drm_rect_calc_hscale(¶ms->src, ¶ms->dst,
params->min_range, params->max_range);
KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
}
static void drm_test_rect_calc_vscale(struct kunit *test)
{
const struct drm_rect_scale_case *params = test->param_value;
int scaling_factor;
scaling_factor = drm_rect_calc_vscale(¶ms->src, ¶ms->dst,
params->min_range, params->max_range);
KUNIT_EXPECT_EQ(test, scaling_factor, params->expected_scaling_factor);
}
static struct kunit_case drm_rect_tests[] = {
KUNIT_CASE(drm_test_rect_clip_scaled_div_by_zero),
KUNIT_CASE(drm_test_rect_clip_scaled_not_clipped),
KUNIT_CASE(drm_test_rect_clip_scaled_clipped),
KUNIT_CASE(drm_test_rect_clip_scaled_signed_vs_unsigned),
KUNIT_CASE_PARAM(drm_test_rect_intersect, drm_rect_intersect_gen_params),
KUNIT_CASE_PARAM(drm_test_rect_calc_hscale, drm_rect_scale_gen_params),
KUNIT_CASE_PARAM(drm_test_rect_calc_vscale, drm_rect_scale_gen_params),
{ }
};
static struct kunit_suite drm_rect_test_suite = {
.name = "drm_rect",
.test_cases = drm_rect_tests,
};
kunit_test_suite(drm_rect_test_suite);
MODULE_LICENSE("GPL");
|