xref: /aosp_15_r20/external/igt-gpu-tools/tests/kms_plane_multiple.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1 /*
2  * Copyright © 2016 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include "igt.h"
26 #include "drmtest.h"
27 #include <errno.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <time.h>
32 
33 IGT_TEST_DESCRIPTION("Test atomic mode setting with multiple planes.");
34 
35 #define SIZE_PLANE      256
36 #define SIZE_CURSOR     128
37 #define LOOP_FOREVER     -1
38 
39 typedef struct {
40 	float red;
41 	float green;
42 	float blue;
43 } color_t;
44 
45 typedef struct {
46 	int drm_fd;
47 	igt_display_t display;
48 	igt_crc_t ref_crc;
49 	igt_pipe_crc_t *pipe_crc;
50 	igt_plane_t **plane;
51 	struct igt_fb *fb;
52 } data_t;
53 
54 /* Command line parameters. */
55 struct {
56 	int iterations;
57 	bool user_seed;
58 	int seed;
59 } opt = {
60 	.iterations = 1,
61 	.user_seed = false,
62 	.seed = 1,
63 };
64 
65 /*
66  * Common code across all tests, acting on data_t
67  */
test_init(data_t * data,enum pipe pipe,int n_planes)68 static void test_init(data_t *data, enum pipe pipe, int n_planes)
69 {
70 	data->pipe_crc = igt_pipe_crc_new(data->drm_fd, pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
71 
72 	data->plane = calloc(n_planes, sizeof(*data->plane));
73 	igt_assert_f(data->plane != NULL, "Failed to allocate memory for planes\n");
74 
75 	data->fb = calloc(n_planes, sizeof(struct igt_fb));
76 	igt_assert_f(data->fb != NULL, "Failed to allocate memory for FBs\n");
77 }
78 
test_fini(data_t * data,igt_output_t * output,int n_planes)79 static void test_fini(data_t *data, igt_output_t *output, int n_planes)
80 {
81 	igt_pipe_crc_stop(data->pipe_crc);
82 
83 	/* reset the constraint on the pipe */
84 	igt_output_set_pipe(output, PIPE_ANY);
85 
86 	igt_pipe_crc_free(data->pipe_crc);
87 	data->pipe_crc = NULL;
88 
89 	free(data->plane);
90 	data->plane = NULL;
91 
92 	free(data->fb);
93 	data->fb = NULL;
94 
95 	igt_display_reset(&data->display);
96 }
97 
98 static void
get_reference_crc(data_t * data,igt_output_t * output,enum pipe pipe,color_t * color,uint64_t tiling)99 get_reference_crc(data_t *data, igt_output_t *output, enum pipe pipe,
100 	      color_t *color, uint64_t tiling)
101 {
102 	drmModeModeInfo *mode;
103 	igt_plane_t *primary;
104 	int ret;
105 
106 	igt_output_set_pipe(output, pipe);
107 
108 	primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
109 	data->plane[primary->index] = primary;
110 
111 	mode = igt_output_get_mode(output);
112 
113 	igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
114 			    DRM_FORMAT_XRGB8888,
115 			    LOCAL_DRM_FORMAT_MOD_NONE,
116 			    color->red, color->green, color->blue,
117 			    &data->fb[primary->index]);
118 
119 	igt_plane_set_fb(data->plane[primary->index], &data->fb[primary->index]);
120 
121 	ret = igt_display_try_commit2(&data->display, COMMIT_ATOMIC);
122 	igt_skip_on(ret != 0);
123 
124 	igt_pipe_crc_start(data->pipe_crc);
125 	igt_pipe_crc_get_single(data->pipe_crc, &data->ref_crc);
126 }
127 
128 static void
create_fb_for_mode_position(data_t * data,igt_output_t * output,drmModeModeInfo * mode,color_t * color,int * rect_x,int * rect_y,int * rect_w,int * rect_h,uint64_t tiling,int max_planes)129 create_fb_for_mode_position(data_t *data, igt_output_t *output, drmModeModeInfo *mode,
130 			    color_t *color, int *rect_x, int *rect_y,
131 			    int *rect_w, int *rect_h, uint64_t tiling,
132 			    int max_planes)
133 {
134 	unsigned int fb_id;
135 	cairo_t *cr;
136 	igt_plane_t *primary;
137 
138 	primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
139 
140 	igt_skip_on(!igt_display_has_format_mod(&data->display,
141 						DRM_FORMAT_XRGB8888,
142 						tiling));
143 
144 	fb_id = igt_create_fb(data->drm_fd,
145 			      mode->hdisplay, mode->vdisplay,
146 			      DRM_FORMAT_XRGB8888,
147 			      tiling,
148 			      &data->fb[primary->index]);
149 	igt_assert(fb_id);
150 
151 	cr = igt_get_cairo_ctx(data->drm_fd, &data->fb[primary->index]);
152 	igt_paint_color(cr, rect_x[0], rect_y[0],
153 			mode->hdisplay, mode->vdisplay,
154 			color->red, color->green, color->blue);
155 
156 	for (int i = 0; i < max_planes; i++) {
157 		if (data->plane[i]->type == DRM_PLANE_TYPE_PRIMARY)
158 			continue;
159 		igt_paint_color(cr, rect_x[i], rect_y[i],
160 				rect_w[i], rect_h[i], 0.0, 0.0, 0.0);
161 		}
162 
163 	igt_put_cairo_ctx(data->drm_fd, &data->fb[primary->index], cr);
164 }
165 
166 
167 static void
prepare_planes(data_t * data,enum pipe pipe_id,color_t * color,uint64_t tiling,int max_planes,igt_output_t * output)168 prepare_planes(data_t *data, enum pipe pipe_id, color_t *color,
169 	       uint64_t tiling, int max_planes, igt_output_t *output)
170 {
171 	drmModeModeInfo *mode;
172 	igt_pipe_t *pipe;
173 	igt_plane_t *primary;
174 	int *x;
175 	int *y;
176 	int *size;
177 	int i;
178 	int* suffle;
179 
180 	igt_output_set_pipe(output, pipe_id);
181 	primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
182 	pipe = primary->pipe;
183 
184 	x = malloc(pipe->n_planes * sizeof(*x));
185 	igt_assert_f(x, "Failed to allocate %ld bytes for variable x\n", (long int) (pipe->n_planes * sizeof(*x)));
186 	y = malloc(pipe->n_planes * sizeof(*y));
187 	igt_assert_f(y, "Failed to allocate %ld bytes for variable y\n", (long int) (pipe->n_planes * sizeof(*y)));
188 	size = malloc(pipe->n_planes * sizeof(*size));
189 	igt_assert_f(size, "Failed to allocate %ld bytes for variable size\n", (long int) (pipe->n_planes * sizeof(*size)));
190 	suffle = malloc(pipe->n_planes * sizeof(*suffle));
191 	igt_assert_f(suffle, "Failed to allocate %ld bytes for variable size\n", (long int) (pipe->n_planes * sizeof(*suffle)));
192 
193 	for (i = 0; i < pipe->n_planes; i++)
194 		suffle[i] = i;
195 
196 	/*
197 	 * suffle table for planes. using rand() should keep it
198 	 * 'randomized in expected way'
199 	 */
200 	for (i = 0; i < 256; i++) {
201 		int n, m;
202 		int a, b;
203 
204 		n = rand() % (pipe->n_planes-1);
205 		m = rand() % (pipe->n_planes-1);
206 
207 		/*
208 		 * keep primary plane at its place for test's sake.
209 		 */
210 		if(n == primary->index || m == primary->index)
211 			continue;
212 
213 		a = suffle[n];
214 		b = suffle[m];
215 		suffle[n] = b;
216 		suffle[m] = a;
217 	}
218 
219 	mode = igt_output_get_mode(output);
220 
221 	/* planes with random positions */
222 	x[primary->index] = 0;
223 	y[primary->index] = 0;
224 	for (i = 0; i < max_planes; i++) {
225 		/*
226 		 * Here is made assumption primary plane will have
227 		 * index zero.
228 		 */
229 		igt_plane_t *plane = igt_output_get_plane(output, suffle[i]);
230 		uint32_t plane_format;
231 		uint64_t plane_tiling;
232 
233 		if (plane->type == DRM_PLANE_TYPE_PRIMARY)
234 			continue;
235 		else if (plane->type == DRM_PLANE_TYPE_CURSOR)
236 			size[i] = SIZE_CURSOR;
237 		else
238 			size[i] = SIZE_PLANE;
239 
240 		x[i] = rand() % (mode->hdisplay - size[i]);
241 		y[i] = rand() % (mode->vdisplay - size[i]);
242 
243 		data->plane[i] = plane;
244 
245 		plane_format = data->plane[i]->type == DRM_PLANE_TYPE_CURSOR ? DRM_FORMAT_ARGB8888 : DRM_FORMAT_XRGB8888;
246 		plane_tiling = data->plane[i]->type == DRM_PLANE_TYPE_CURSOR ? LOCAL_DRM_FORMAT_MOD_NONE : tiling;
247 
248 		igt_skip_on(!igt_plane_has_format_mod(plane, plane_format,
249 						      plane_tiling));
250 
251 		igt_create_color_fb(data->drm_fd,
252 				    size[i], size[i],
253 				    plane_format,
254 				    plane_tiling,
255 				    color->red, color->green, color->blue,
256 				    &data->fb[i]);
257 
258 		igt_plane_set_position(data->plane[i], x[i], y[i]);
259 		igt_plane_set_fb(data->plane[i], &data->fb[i]);
260 	}
261 
262 	/* primary plane */
263 	data->plane[primary->index] = primary;
264 	create_fb_for_mode_position(data, output, mode, color, x, y,
265 				    size, size, tiling, max_planes);
266 	igt_plane_set_fb(data->plane[primary->index], &data->fb[primary->index]);
267 	free((void*)x);
268 	free((void*)y);
269 	free((void*)size);
270 	free((void*)suffle);
271 }
272 
273 /*
274  * Multiple plane position test.
275  *   - We start by grabbing a reference CRC of a full blue fb being scanned
276  *     out on the primary plane
277  *   - Then we scannout number of planes:
278  *      * the primary plane uses a blue fb with a black rectangle holes
279  *      * planes, on top of the primary plane, with a blue fb that is set-up
280  *        to cover the black rectangles of the primary plane
281  *     The resulting CRC should be identical to the reference CRC
282  */
283 
284 static void
test_plane_position_with_output(data_t * data,enum pipe pipe,igt_output_t * output,int n_planes,uint64_t tiling)285 test_plane_position_with_output(data_t *data, enum pipe pipe,
286 				igt_output_t *output, int n_planes,
287 				uint64_t tiling)
288 {
289 	color_t blue  = { 0.0f, 0.0f, 1.0f };
290 	igt_crc_t crc;
291 	igt_plane_t *plane;
292 	int i;
293 	int err, c = 0;
294 	int iterations = opt.iterations < 1 ? 1 : opt.iterations;
295 	bool loop_forever;
296 	char info[256];
297 
298 	if (opt.iterations == LOOP_FOREVER) {
299 		loop_forever = true;
300 		sprintf(info, "forever");
301 	} else {
302 		loop_forever = false;
303 		sprintf(info, "for %d %s",
304 			iterations, iterations > 1 ? "iterations" : "iteration");
305 	}
306 
307 	test_init(data, pipe, n_planes);
308 
309 	get_reference_crc(data, output, pipe, &blue, tiling);
310 
311 	/* Find out how many planes are allowed simultaneously */
312 	do {
313 		c++;
314 		prepare_planes(data, pipe, &blue, tiling, c, output);
315 		err = igt_display_try_commit2(&data->display, COMMIT_ATOMIC);
316 
317 		for_each_plane_on_pipe(&data->display, pipe, plane)
318 			igt_plane_set_fb(plane, NULL);
319 
320 		igt_display_commit2(&data->display, COMMIT_ATOMIC);
321 
322 		for (int x = 0; x < c; x++)
323 			igt_remove_fb(data->drm_fd, &data->fb[x]);
324 	} while (!err && c < n_planes);
325 
326 	if (err)
327 		c--;
328 
329 	igt_info("Testing connector %s using pipe %s with %d planes %s with seed %d\n",
330 		 igt_output_name(output), kmstest_pipe_name(pipe), c,
331 		 info, opt.seed);
332 
333 	i = 0;
334 	while (i < iterations || loop_forever) {
335 		/* randomize planes and set up the holes */
336 		prepare_planes(data, pipe, &blue, tiling, c, output);
337 
338 		igt_display_commit2(&data->display, COMMIT_ATOMIC);
339 
340 		igt_pipe_crc_get_current(data->display.drm_fd, data->pipe_crc, &crc);
341 
342 		for_each_plane_on_pipe(&data->display, pipe, plane)
343 			igt_plane_set_fb(plane, NULL);
344 
345 		igt_display_commit2(&data->display, COMMIT_ATOMIC);
346 
347 		for (int x = 0; x < c; x++)
348 			igt_remove_fb(data->drm_fd, &data->fb[x]);
349 
350 		igt_assert_crc_equal(&data->ref_crc, &crc);
351 
352 		i++;
353 	}
354 
355 	test_fini(data, output, n_planes);
356 }
357 
358 static void
test_plane_position(data_t * data,enum pipe pipe,uint64_t tiling)359 test_plane_position(data_t *data, enum pipe pipe, uint64_t tiling)
360 {
361 	igt_output_t *output;
362 	int n_planes = data->display.pipes[pipe].n_planes;
363 
364 	output = igt_get_single_output_for_pipe(&data->display, pipe);
365 	igt_require(output);
366 
367 	if (!opt.user_seed)
368 		opt.seed = time(NULL);
369 
370 	srand(opt.seed);
371 
372 	test_plane_position_with_output(data, pipe, output,
373 					n_planes, tiling);
374 }
375 
376 static void
run_tests_for_pipe(data_t * data,enum pipe pipe)377 run_tests_for_pipe(data_t *data, enum pipe pipe)
378 {
379 	igt_fixture {
380 		igt_skip_on(pipe >= data->display.n_pipes);
381 		igt_require(data->display.pipes[pipe].n_planes > 0);
382 	}
383 
384 	igt_subtest_f("atomic-pipe-%s-tiling-x", kmstest_pipe_name(pipe))
385 		test_plane_position(data, pipe, LOCAL_I915_FORMAT_MOD_X_TILED);
386 
387 	igt_subtest_f("atomic-pipe-%s-tiling-y", kmstest_pipe_name(pipe))
388 		test_plane_position(data, pipe, LOCAL_I915_FORMAT_MOD_Y_TILED);
389 
390 	igt_subtest_f("atomic-pipe-%s-tiling-yf", kmstest_pipe_name(pipe))
391 		test_plane_position(data, pipe, LOCAL_I915_FORMAT_MOD_Yf_TILED);
392 
393 	igt_subtest_f("atomic-pipe-%s-tiling-none", kmstest_pipe_name(pipe))
394 		test_plane_position(data, pipe, LOCAL_DRM_FORMAT_MOD_NONE);
395 }
396 
397 static data_t data;
398 
opt_handler(int option,int option_index,void * input)399 static int opt_handler(int option, int option_index, void *input)
400 {
401 	switch (option) {
402 	case 'i':
403 		opt.iterations = strtol(optarg, NULL, 0);
404 
405 		if (opt.iterations < LOOP_FOREVER || opt.iterations == 0) {
406 			igt_info("incorrect number of iterations: %d\n", opt.iterations);
407 			return IGT_OPT_HANDLER_ERROR;
408 		}
409 
410 		break;
411 	case 's':
412 		opt.user_seed = true;
413 		opt.seed = strtol(optarg, NULL, 0);
414 		break;
415 	default:
416 		return IGT_OPT_HANDLER_ERROR;
417 	}
418 
419 	return IGT_OPT_HANDLER_SUCCESS;
420 }
421 
422 const char *help_str =
423 	"  --iterations Number of iterations for test coverage. -1 loop forever, default 64 iterations\n"
424 	"  --seed       Seed for random number generator\n";
425 
426 struct option long_options[] = {
427 	{ "iterations", required_argument, NULL, 'i'},
428 	{ "seed",    required_argument, NULL, 's'},
429 	{ 0, 0, 0, 0 }
430 };
431 
432 igt_main_args("", long_options, help_str, opt_handler, NULL)
433 {
434 	enum pipe pipe;
435 
436 	igt_skip_on_simulation();
437 
438 	igt_fixture {
439 		data.drm_fd = drm_open_driver_master(DRIVER_INTEL | DRIVER_AMDGPU);
440 		kmstest_set_vt_graphics_mode();
441 		igt_require_pipe_crc(data.drm_fd);
442 		igt_display_require(&data.display, data.drm_fd);
443 		igt_require(data.display.is_atomic);
444 	}
445 
for_each_pipe_static(pipe)446 	for_each_pipe_static(pipe) {
447 		igt_describe("Check that the kernel handles atomic updates of "
448 			     "multiple planes correctly by changing their "
449 			     "geometry and making sure the changes are "
450 			     "reflected immediately after each commit.");
451 		igt_subtest_group
452 			run_tests_for_pipe(&data, pipe);
453 	}
454 
455 	igt_fixture {
456 		igt_display_fini(&data.display);
457 	}
458 }
459