2 * Copyright © 2013 Intel Corporation
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:
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
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
32 #include "igt_debugfs.h"
35 #ifndef DRM_CAP_CURSOR_WIDTH
36 #define DRM_CAP_CURSOR_WIDTH 0x8
38 #ifndef DRM_CAP_CURSOR_HEIGHT
39 #define DRM_CAP_CURSOR_HEIGHT 0x9
44 igt_display_t display;
45 struct igt_fb primary_fb;
50 int left, right, top, bottom;
52 int curw, curh; /* cursor size */
54 igt_pipe_crc_t *pipe_crc;
57 static void draw_cursor(cairo_t *cr, int x, int y, int w)
60 /* Cairo doesn't like to be fed numbers that are too wild */
61 if ((x < SHRT_MIN) || (x > SHRT_MAX) || (y < SHRT_MIN) || (y > SHRT_MAX))
63 cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
64 /* 4 color rectangles in the corners, RGBY */
65 igt_paint_color_alpha(cr, x, y, w, w, 1.0, 0.0, 0.0, 1.0);
66 igt_paint_color_alpha(cr, x + w, y, w, w, 0.0, 1.0, 0.0, 1.0);
67 igt_paint_color_alpha(cr, x, y + w, w, w, 0.0, 0.0, 1.0, 1.0);
68 igt_paint_color_alpha(cr, x + w, y + w, w, w, 0.5, 0.5, 0.5, 1.0);
71 static void cursor_enable(data_t *data)
73 igt_display_t *display = &data->display;
74 igt_output_t *output = data->output;
77 cursor = igt_output_get_plane(output, IGT_PLANE_CURSOR);
78 igt_plane_set_fb(cursor, &data->fb);
79 igt_display_commit(display);
82 static void cursor_disable(data_t *data)
84 igt_display_t *display = &data->display;
85 igt_output_t *output = data->output;
88 cursor = igt_output_get_plane(output, IGT_PLANE_CURSOR);
89 igt_plane_set_fb(cursor, NULL);
90 igt_display_commit(display);
94 static void do_single_test(data_t *data, int x, int y)
96 igt_display_t *display = &data->display;
97 igt_pipe_crc_t *pipe_crc = data->pipe_crc;
98 igt_crc_t crc, ref_crc;
100 cairo_t *cr = igt_get_cairo_ctx(data->drm_fd, &data->primary_fb);
102 igt_info("."); fflush(stdout);
105 igt_paint_test_pattern(cr, data->screenw, data->screenh);
107 cursor = igt_output_get_plane(data->output, IGT_PLANE_CURSOR);
108 igt_plane_set_position(cursor, x, y);
109 igt_display_commit(display);
110 igt_wait_for_vblank(data->drm_fd, data->pipe);
111 igt_pipe_crc_collect_crc(pipe_crc, &crc);
112 cursor_disable(data);
114 /* Now render the same in software and collect crc */
115 draw_cursor(cr, x, y, data->curw);
116 igt_display_commit(display);
117 igt_wait_for_vblank(data->drm_fd, data->pipe);
118 igt_pipe_crc_collect_crc(pipe_crc, &ref_crc);
119 /* Clear screen afterwards */
120 igt_paint_color(cr, 0, 0, data->screenw, data->screenh,
123 igt_assert(igt_crc_equal(&crc, &ref_crc));
126 static void do_test(data_t *data,
127 int left, int right, int top, int bottom)
129 do_single_test(data, left, top);
130 do_single_test(data, right, top);
131 do_single_test(data, right, bottom);
132 do_single_test(data, left, bottom);
135 static void test_crc_onscreen(data_t *data)
137 int left = data->left;
138 int right = data->right;
140 int bottom = data->bottom;
141 int cursor_w = data->curw;
142 int cursor_h = data->curh;
145 do_test(data, left, right, top, bottom);
147 /* 2 pixels inside */
148 do_test(data, left - (cursor_w-2), right + (cursor_w-2), top , bottom );
149 do_test(data, left , right , top - (cursor_h-2), bottom + (cursor_h-2));
150 do_test(data, left - (cursor_w-2), right + (cursor_w-2), top - (cursor_h-2), bottom + (cursor_h-2));
153 do_test(data, left - (cursor_w-1), right + (cursor_w-1), top , bottom );
154 do_test(data, left , right , top - (cursor_h-1), bottom + (cursor_h-1));
155 do_test(data, left - (cursor_w-1), right + (cursor_w-1), top - (cursor_h-1), bottom + (cursor_h-1));
158 static void test_crc_offscreen(data_t *data)
160 int left = data->left;
161 int right = data->right;
163 int bottom = data->bottom;
164 int cursor_w = data->curw;
165 int cursor_h = data->curh;
168 do_test(data, left - (cursor_w), right + (cursor_w), top , bottom );
169 do_test(data, left , right , top - (cursor_h), bottom + (cursor_h));
170 do_test(data, left - (cursor_w), right + (cursor_w), top - (cursor_h), bottom + (cursor_h));
172 /* fully outside by 1 extra pixels */
173 do_test(data, left - (cursor_w+1), right + (cursor_w+1), top , bottom );
174 do_test(data, left , right , top - (cursor_h+1), bottom + (cursor_h+1));
175 do_test(data, left - (cursor_w+1), right + (cursor_w+1), top - (cursor_h+1), bottom + (cursor_h+1));
177 /* fully outside by 2 extra pixels */
178 do_test(data, left - (cursor_w+2), right + (cursor_w+2), top , bottom );
179 do_test(data, left , right , top - (cursor_h+2), bottom + (cursor_h+2));
180 do_test(data, left - (cursor_w+2), right + (cursor_w+2), top - (cursor_h+2), bottom + (cursor_h+2));
182 /* fully outside by a lot of extra pixels */
183 do_test(data, left - (cursor_w+512), right + (cursor_w+512), top , bottom );
184 do_test(data, left , right , top - (cursor_h+512), bottom + (cursor_h+512));
185 do_test(data, left - (cursor_w+512), right + (cursor_w+512), top - (cursor_h+512), bottom + (cursor_h+512));
188 do_test(data, INT_MIN, INT_MAX, INT_MIN, INT_MAX);
191 static void test_crc_sliding(data_t *data)
195 /* Make sure cursor moves smoothly and pixel-by-pixel, and that there are
196 * no alignment issues. Horizontal, vertical and diagonal test.
198 for (i = 0; i < 16; i++) {
199 do_single_test(data, i, 0);
200 do_single_test(data, 0, i);
201 do_single_test(data, i, i);
205 static void test_crc_random(data_t *data)
209 /* Random cursor placement */
210 for (i = 0; i < 50; i++) {
211 int x = rand() % (data->screenw + data->curw * 2) - data->curw;
212 int y = rand() % (data->screenh + data->curh * 2) - data->curh;
213 do_single_test(data, x, y);
217 static bool prepare_crtc(data_t *data, igt_output_t *output,
218 int cursor_w, int cursor_h)
220 drmModeModeInfo *mode;
221 igt_display_t *display = &data->display;
222 igt_plane_t *primary;
224 /* select the pipe we want to use */
225 igt_output_set_pipe(output, data->pipe);
226 igt_display_commit(display);
228 if (!output->valid) {
229 igt_output_set_pipe(output, PIPE_ANY);
230 igt_display_commit(display);
234 /* create and set the primary plane fb */
235 mode = igt_output_get_mode(output);
236 igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
242 primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
243 igt_plane_set_fb(primary, &data->primary_fb);
245 igt_display_commit(display);
247 /* create the pipe_crc object for this pipe */
249 igt_pipe_crc_free(data->pipe_crc);
251 data->pipe_crc = igt_pipe_crc_new(data->pipe,
252 INTEL_PIPE_CRC_SOURCE_AUTO);
253 if (!data->pipe_crc) {
254 igt_info("auto crc not supported on this connector with pipe %i\n",
259 /* x/y position where the cursor is still fully visible */
261 data->right = mode->hdisplay - cursor_w;
263 data->bottom = mode->vdisplay - cursor_h;
264 data->screenw = mode->hdisplay;
265 data->screenh = mode->vdisplay;
266 data->curw = cursor_w;
267 data->curh = cursor_h;
268 data->cursor_max_size = cursor_w;
270 /* make sure cursor is disabled */
271 cursor_disable(data);
272 igt_wait_for_vblank(data->drm_fd, data->pipe);
274 /* get reference crc w/o cursor */
275 igt_pipe_crc_collect_crc(data->pipe_crc, &data->ref_crc);
280 static void cleanup_crtc(data_t *data, igt_output_t *output)
282 igt_display_t *display = &data->display;
283 igt_plane_t *primary;
285 igt_pipe_crc_free(data->pipe_crc);
286 data->pipe_crc = NULL;
288 igt_remove_fb(data->drm_fd, &data->primary_fb);
290 primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
291 igt_plane_set_fb(primary, NULL);
293 igt_output_set_pipe(output, PIPE_ANY);
294 igt_display_commit(display);
297 static void run_test(data_t *data, void (*testfunc)(data_t *), int cursor_w, int cursor_h)
299 igt_display_t *display = &data->display;
300 igt_output_t *output;
304 for_each_connected_output(display, output) {
305 data->output = output;
306 for_each_pipe(display, p) {
309 if (!prepare_crtc(data, output, cursor_w, cursor_h))
314 igt_info("Beginning %s on pipe %c, connector %s\n",
315 igt_subtest_name(), pipe_name(data->pipe),
316 igt_output_name(output));
320 igt_info("\n%s on pipe %c, connector %s: PASSED\n\n",
321 igt_subtest_name(), pipe_name(data->pipe),
322 igt_output_name(output));
324 /* cleanup what prepare_crtc() has done */
325 cleanup_crtc(data, output);
329 igt_require_f(valid_tests, "no valid crtc/connector combinations found\n");
332 static void create_cursor_fb(data_t *data, int cur_w, int cur_h)
337 fb_id = igt_create_fb(data->drm_fd, cur_w, cur_h,
338 DRM_FORMAT_ARGB8888, false,
342 cr = igt_get_cairo_ctx(data->drm_fd, &data->fb);
343 draw_cursor(cr, 0, 0, cur_w);
344 igt_assert(cairo_status(cr) == 0);
347 static void test_cursor_size(data_t *data)
349 igt_display_t *display = &data->display;
350 igt_pipe_crc_t *pipe_crc = data->pipe_crc;
351 igt_crc_t crc[10], ref_crc;
354 int i, size, cursor_max_size = data->cursor_max_size;
357 /* Create a maximum size cursor, then change the size in flight to
358 * smaller ones to see that the size is applied correctly
360 fb_id = igt_create_fb(data->drm_fd, cursor_max_size, cursor_max_size,
361 DRM_FORMAT_ARGB8888, false, &data->fb);
364 /* Use a solid white rectangle as the cursor */
365 cr = igt_get_cairo_ctx(data->drm_fd, &data->fb);
366 igt_paint_color_alpha(cr, 0, 0, cursor_max_size, cursor_max_size, 1.0, 1.0, 1.0, 1.0);
368 /* Hardware test loop */
370 ret = drmModeMoveCursor(data->drm_fd, data->output->config.crtc->crtc_id, 0, 0);
371 igt_assert(ret == 0);
372 for (i = 0, size = cursor_max_size; size >= 64; size /= 2, i++) {
373 /* Change size in flight: */
374 ret = drmModeSetCursor(data->drm_fd, data->output->config.crtc->crtc_id,
375 data->fb.gem_handle, size, size);
376 igt_assert(ret == 0);
377 igt_wait_for_vblank(data->drm_fd, data->pipe);
378 igt_pipe_crc_collect_crc(pipe_crc, &crc[i]);
380 cursor_disable(data);
381 /* Software test loop */
382 cr = igt_get_cairo_ctx(data->drm_fd, &data->primary_fb);
383 for (i = 0, size = cursor_max_size; size >= 64; size /= 2, i++) {
384 /* Now render the same in software and collect crc */
385 igt_paint_color_alpha(cr, 0, 0, size, size, 1.0, 1.0, 1.0, 1.0);
386 igt_display_commit(display);
387 igt_wait_for_vblank(data->drm_fd, data->pipe);
388 igt_pipe_crc_collect_crc(pipe_crc, &ref_crc);
389 /* Clear screen afterwards */
390 igt_paint_color(cr, 0, 0, data->screenw, data->screenh,
392 igt_assert(igt_crc_equal(&crc[i], &ref_crc));
396 static void run_test_generic(data_t *data, int cursor_max_size)
399 for (cursor_size = 64; cursor_size <= 256; cursor_size *= 2)
402 igt_require(cursor_max_size >= cursor_size);
405 create_cursor_fb(data, cursor_size, cursor_size);
407 /* Using created cursor FBs to test cursor support */
408 igt_subtest_f("cursor-%d-onscreen", cursor_size)
409 run_test(data, test_crc_onscreen, cursor_size, cursor_size);
410 igt_subtest_f("cursor-%d-offscreen", cursor_size)
411 run_test(data, test_crc_offscreen, cursor_size, cursor_size);
412 igt_subtest_f("cursor-%d-sliding", cursor_size)
413 run_test(data, test_crc_sliding, cursor_size, cursor_size);
414 igt_subtest_f("cursor-%d-random", cursor_size)
415 run_test(data, test_crc_random, cursor_size, cursor_size);
418 igt_remove_fb(data->drm_fd, &data->fb);
424 uint64_t cursor_width = 64, cursor_height = 64;
428 igt_skip_on_simulation();
431 data.drm_fd = drm_open_any();
433 ret = drmGetCap(data.drm_fd, DRM_CAP_CURSOR_WIDTH, &cursor_width);
434 igt_assert(ret == 0 || errno == EINVAL);
435 /* Not making use of cursor_height since it is same as width, still reading */
436 ret = drmGetCap(data.drm_fd, DRM_CAP_CURSOR_HEIGHT, &cursor_height);
437 igt_assert(ret == 0 || errno == EINVAL);
439 /* We assume width and height are same so max is assigned width */
440 igt_assert_cmpint(cursor_width, ==, cursor_height);
442 igt_set_vt_graphics_mode();
444 igt_require_pipe_crc();
446 igt_display_init(&data.display, data.drm_fd);
449 igt_subtest_f("cursor-size-change")
450 run_test(&data, test_cursor_size, cursor_width, cursor_height);
452 run_test_generic(&data, cursor_width);
455 igt_display_fini(&data.display);