summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/tests/drm_plane_helper_test.c
blob: 7e975a3f4a7196bea67a64b083ecde3696cd7dcf (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * Test cases for the drm_plane_helper functions
 *
 * Copyright (c) 2022 Maíra Canal <mairacanal@riseup.net>
 */

#include <kunit/test.h>

#include <drm/drm_atomic_helper.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_modes.h>
#include <drm/drm_rect.h>

static const struct drm_crtc_state crtc_state = {
	.crtc = ZERO_SIZE_PTR,
	.enable = true,
	.active = true,
	.mode = {
		DRM_MODE("1024x768", 0, 65000, 1024, 1048,
			 1184, 1344, 0, 768, 771, 777, 806, 0,
			 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC)
	},
};

struct drm_check_plane_state_test {
	const char *name;
	const char *msg;
	struct {
		unsigned int x;
		unsigned int y;
		unsigned int w;
		unsigned int h;
	} src, src_expected;
	struct {
		int x;
		int y;
		unsigned int w;
		unsigned int h;
	} crtc, crtc_expected;
	unsigned int rotation;
	int min_scale;
	int max_scale;
	bool can_position;
};

static int drm_plane_helper_init(struct kunit *test)
{
	const struct drm_check_plane_state_test *params = test->param_value;
	struct drm_plane *plane;
	struct drm_framebuffer *fb;
	struct drm_plane_state *mock;

	plane = kunit_kzalloc(test, sizeof(*plane), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, plane);

	fb = kunit_kzalloc(test, sizeof(*fb), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, fb);
	fb->width = 2048;
	fb->height = 2048;

	mock = kunit_kzalloc(test, sizeof(*mock), GFP_KERNEL);
	KUNIT_ASSERT_NOT_NULL(test, mock);
	mock->plane = plane;
	mock->crtc = ZERO_SIZE_PTR;
	mock->fb = fb;
	mock->rotation = params->rotation;
	mock->src_x = params->src.x;
	mock->src_y = params->src.y;
	mock->src_w = params->src.w;
	mock->src_h = params->src.h;
	mock->crtc_x = params->crtc.x;
	mock->crtc_y = params->crtc.y;
	mock->crtc_w = params->crtc.w;
	mock->crtc_h = params->crtc.h;

	test->priv = mock;

	return 0;
}

static void check_src_eq(struct kunit *test, struct drm_plane_state *plane_state,
			 unsigned int src_x, unsigned int src_y,
			 unsigned int src_w, unsigned int src_h)
{
	struct drm_rect expected = DRM_RECT_INIT(src_x, src_y, src_w, src_h);

	KUNIT_ASSERT_GE_MSG(test, plane_state->src.x1, 0,
			    "src x coordinate %x should never be below 0, src: " DRM_RECT_FP_FMT,
			    plane_state->src.x1, DRM_RECT_FP_ARG(&plane_state->src));

	KUNIT_ASSERT_GE_MSG(test, plane_state->src.y1, 0,
			    "src y coordinate %x should never be below 0, src: " DRM_RECT_FP_FMT,
			    plane_state->src.y1, DRM_RECT_FP_ARG(&plane_state->src));

	KUNIT_EXPECT_TRUE_MSG(test, drm_rect_equals(&plane_state->src, &expected),
			      "dst: " DRM_RECT_FP_FMT ", expected: " DRM_RECT_FP_FMT,
			      DRM_RECT_FP_ARG(&plane_state->src), DRM_RECT_FP_ARG(&expected));
}

static void check_crtc_eq(struct kunit *test, struct drm_plane_state *plane_state,
			  int crtc_x, int crtc_y,
			  unsigned int crtc_w, unsigned int crtc_h)
{
	struct drm_rect expected = DRM_RECT_INIT(crtc_x, crtc_y, crtc_w, crtc_h);

	KUNIT_EXPECT_TRUE_MSG(test, drm_rect_equals(&plane_state->dst, &expected),
			      "dst: " DRM_RECT_FMT ", expected: " DRM_RECT_FMT,
			      DRM_RECT_ARG(&plane_state->dst), DRM_RECT_ARG(&expected));
}

static void drm_test_check_plane_state(struct kunit *test)
{
	const struct drm_check_plane_state_test *params = test->param_value;
	struct drm_plane_state *plane_state = test->priv;

	KUNIT_ASSERT_EQ_MSG(test,
			    drm_atomic_helper_check_plane_state(plane_state, &crtc_state,
								params->min_scale,
								params->max_scale,
								params->can_position, false),
			    0, params->msg);
	KUNIT_EXPECT_TRUE(test, plane_state->visible);
	check_src_eq(test, plane_state, params->src_expected.x, params->src_expected.y,
		     params->src_expected.w, params->src_expected.h);
	check_crtc_eq(test, plane_state, params->crtc_expected.x, params->crtc_expected.y,
		      params->crtc_expected.w, params->crtc_expected.h);
}

static void drm_check_plane_state_desc(const struct drm_check_plane_state_test *t,
				       char *desc)
{
	sprintf(desc, "%s", t->name);
}

static const struct drm_check_plane_state_test drm_check_plane_state_tests[] = {
	{
		.name = "clipping_simple",
		.msg = "Simple clipping check should pass",
		.src = { 0, 0,
			 2048 << 16,
			 2048 << 16 },
		.crtc = { 0, 0, 2048, 2048 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = DRM_PLANE_NO_SCALING,
		.max_scale = DRM_PLANE_NO_SCALING,
		.can_position = false,
		.src_expected = { 0, 0, 1024 << 16, 768 << 16 },
		.crtc_expected = { 0, 0, 1024, 768 },
	},
	{
		.name = "clipping_rotate_reflect",
		.msg = "Rotated clipping check should pass",
		.src = { 0, 0,
			 2048 << 16,
			 2048 << 16 },
		.crtc = { 0, 0, 2048, 2048 },
		.rotation = DRM_MODE_ROTATE_90 | DRM_MODE_REFLECT_X,
		.min_scale = DRM_PLANE_NO_SCALING,
		.max_scale = DRM_PLANE_NO_SCALING,
		.can_position = false,
		.src_expected = { 0, 0, 768 << 16, 1024 << 16 },
		.crtc_expected = { 0, 0, 1024, 768 },
	},
	{
		.name = "positioning_simple",
		.msg = "Simple positioning should work",
		.src = { 0, 0, 1023 << 16, 767 << 16 },
		.crtc = { 0, 0, 1023, 767 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = DRM_PLANE_NO_SCALING,
		.max_scale = DRM_PLANE_NO_SCALING,
		.can_position = true,
		.src_expected = { 0, 0, 1023 << 16, 767 << 16 },
		.crtc_expected = { 0, 0, 1023, 767 },
	},
	{
		.name = "upscaling",
		.msg = "Upscaling exactly 2x should work",
		.src = { 0, 0, 512 << 16, 384 << 16 },
		.crtc = { 0, 0, 1024, 768 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = 0x8000,
		.max_scale = DRM_PLANE_NO_SCALING,
		.can_position = false,
		.src_expected = { 0, 0, 512 << 16, 384 << 16 },
		.crtc_expected = { 0, 0, 1024, 768 },
	},
	{
		.name = "downscaling",
		.msg = "Should succeed with exact scaling limit",
		.src = { 0, 0, 2048 << 16, 1536 << 16 },
		.crtc = { 0, 0, 1024, 768 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = DRM_PLANE_NO_SCALING,
		.max_scale = 0x20000,
		.can_position = false,
		.src_expected = { 0, 0, 2048 << 16, 1536 << 16 },
		.crtc_expected = { 0, 0, 1024, 768 },
	},
	{
		.name = "rounding1",
		.msg = "Should succeed by clipping to exact multiple",
		.src = { 0, 0, 0x40001, 0x40001 },
		.crtc = { 1022, 766, 4, 4 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = DRM_PLANE_NO_SCALING,
		.max_scale = 0x10001,
		.can_position = true,
		.src_expected = { 0, 0, 2 << 16, 2 << 16 },
		.crtc_expected = { 1022, 766, 2, 2 },
	},
	{
		.name = "rounding2",
		.msg = "Should succeed by clipping to exact multiple",
		.src = { 0x20001, 0x20001, 0x4040001, 0x3040001 },
		.crtc = { -2, -2, 1028, 772 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = DRM_PLANE_NO_SCALING,
		.max_scale = 0x10001,
		.can_position = false,
		.src_expected = { 0x40002, 0x40002, 1024 << 16, 768 << 16 },
		.crtc_expected = { 0, 0, 1024, 768 },
	},
	{
		.name = "rounding3",
		.msg = "Should succeed by clipping to exact multiple",
		.src = { 0, 0, 0x3ffff, 0x3ffff },
		.crtc = { 1022, 766, 4, 4 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = 0xffff,
		.max_scale = DRM_PLANE_NO_SCALING,
		.can_position = true,
		/* Should not be rounded to 0x20001, which would be upscaling. */
		.src_expected = { 0, 0, 2 << 16, 2 << 16 },
		.crtc_expected = { 1022, 766, 2, 2 },
	},
	{
		.name = "rounding4",
		.msg = "Should succeed by clipping to exact multiple",
		.src = { 0x1ffff, 0x1ffff, 0x403ffff, 0x303ffff },
		.crtc = { -2, -2, 1028, 772 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = 0xffff,
		.max_scale = DRM_PLANE_NO_SCALING,
		.can_position = false,
		.src_expected = { 0x3fffe, 0x3fffe, 1024 << 16, 768 << 16 },
		.crtc_expected = { 0, 0, 1024, 768 },
	},
};

KUNIT_ARRAY_PARAM(drm_check_plane_state, drm_check_plane_state_tests, drm_check_plane_state_desc);

static void drm_test_check_invalid_plane_state(struct kunit *test)
{
	const struct drm_check_plane_state_test *params = test->param_value;
	struct drm_plane_state *plane_state = test->priv;

	KUNIT_ASSERT_LT_MSG(test,
			    drm_atomic_helper_check_plane_state(plane_state, &crtc_state,
								params->min_scale,
								params->max_scale,
								params->can_position, false),
			    0, params->msg);
}

static const struct drm_check_plane_state_test drm_check_invalid_plane_state_tests[] = {
	{
		.name = "positioning_invalid",
		.msg = "Should not be able to position on the crtc with can_position=false",
		.src = { 0, 0, 1023 << 16, 767 << 16 },
		.crtc = { 0, 0, 1023, 767 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = DRM_PLANE_NO_SCALING,
		.max_scale = DRM_PLANE_NO_SCALING,
		.can_position = false,
	},
	{
		.name = "upscaling_invalid",
		.msg = "Upscaling out of range should fail",
		.src = { 0, 0, 512 << 16, 384 << 16 },
		.crtc = { 0, 0, 1024, 768 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = 0x8001,
		.max_scale = DRM_PLANE_NO_SCALING,
		.can_position = false,
	},
	{
		.name = "downscaling_invalid",
		.msg = "Downscaling out of range should fail",
		.src = { 0, 0, 2048 << 16, 1536 << 16 },
		.crtc = { 0, 0, 1024, 768 },
		.rotation = DRM_MODE_ROTATE_0,
		.min_scale = DRM_PLANE_NO_SCALING,
		.max_scale = 0x1ffff,
		.can_position = false,
	},
};

KUNIT_ARRAY_PARAM(drm_check_invalid_plane_state, drm_check_invalid_plane_state_tests,
		  drm_check_plane_state_desc);

static struct kunit_case drm_plane_helper_test[] = {
	KUNIT_CASE_PARAM(drm_test_check_plane_state, drm_check_plane_state_gen_params),
	KUNIT_CASE_PARAM(drm_test_check_invalid_plane_state,
			 drm_check_invalid_plane_state_gen_params),
	{}
};

static struct kunit_suite drm_plane_helper_test_suite = {
	.name = "drm_plane_helper",
	.init = drm_plane_helper_init,
	.test_cases = drm_plane_helper_test,
};

kunit_test_suite(drm_plane_helper_test_suite);

MODULE_DESCRIPTION("Test cases for the drm_plane_helper functions");
MODULE_LICENSE("GPL");