lib/igt.cocci: Add s/assert/igt_assert/
[platform/upstream/intel-gpu-tools.git] / tests / kms_cursor_crc.c
1 /*
2  * Copyright © 2013 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 <errno.h>
26 #include <limits.h>
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "drmtest.h"
32 #include "igt_debugfs.h"
33 #include "igt_kms.h"
34
35 #ifndef DRM_CAP_CURSOR_WIDTH
36 #define DRM_CAP_CURSOR_WIDTH 0x8
37 #endif
38 #ifndef DRM_CAP_CURSOR_HEIGHT
39 #define DRM_CAP_CURSOR_HEIGHT 0x9
40 #endif
41
42 typedef struct {
43         int drm_fd;
44         igt_display_t display;
45         struct igt_fb primary_fb;
46         struct igt_fb fb;
47         igt_output_t *output;
48         enum pipe pipe;
49         igt_crc_t ref_crc;
50         int left, right, top, bottom;
51         int screenw, screenh;
52         int curw, curh; /* cursor size */
53         int cursor_max_size;
54         igt_pipe_crc_t *pipe_crc;
55 } data_t;
56
57 static void draw_cursor(cairo_t *cr, int x, int y, int w)
58 {
59         w /= 2;
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))
62                 return;
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);
69 }
70
71 static void cursor_enable(data_t *data)
72 {
73         igt_display_t *display = &data->display;
74         igt_output_t *output = data->output;
75         igt_plane_t *cursor;
76
77         cursor = igt_output_get_plane(output, IGT_PLANE_CURSOR);
78         igt_plane_set_fb(cursor, &data->fb);
79         igt_display_commit(display);
80 }
81
82 static void cursor_disable(data_t *data)
83 {
84         igt_display_t *display = &data->display;
85         igt_output_t *output = data->output;
86         igt_plane_t *cursor;
87
88         cursor = igt_output_get_plane(output, IGT_PLANE_CURSOR);
89         igt_plane_set_fb(cursor, NULL);
90         igt_display_commit(display);
91 }
92
93
94 static void do_single_test(data_t *data, int x, int y)
95 {
96         igt_display_t *display = &data->display;
97         igt_pipe_crc_t *pipe_crc = data->pipe_crc;
98         igt_crc_t crc, ref_crc;
99         igt_plane_t *cursor;
100         cairo_t *cr = igt_get_cairo_ctx(data->drm_fd, &data->primary_fb);
101
102         igt_info("."); fflush(stdout);
103
104         /* Hardware test */
105         igt_paint_test_pattern(cr, data->screenw, data->screenh);
106         cursor_enable(data);
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);
113
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,
121                         0.0, 0.0, 0.0);
122
123         igt_assert(igt_crc_equal(&crc, &ref_crc));
124 }
125
126 static void do_test(data_t *data,
127                     int left, int right, int top, int bottom)
128 {
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);
133 }
134
135 static void test_crc_onscreen(data_t *data)
136 {
137         int left = data->left;
138         int right = data->right;
139         int top = data->top;
140         int bottom = data->bottom;
141         int cursor_w = data->curw;
142         int cursor_h = data->curh;
143
144         /* fully inside  */
145         do_test(data, left, right, top, bottom);
146
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));
151
152         /* 1 pixel inside */
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));
156 }
157
158 static void test_crc_offscreen(data_t *data)
159 {
160         int left = data->left;
161         int right = data->right;
162         int top = data->top;
163         int bottom = data->bottom;
164         int cursor_w = data->curw;
165         int cursor_h = data->curh;
166
167         /* fully outside */
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));
171
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));
176
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));
181
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));
186
187         /* go nuts */
188         do_test(data, INT_MIN, INT_MAX, INT_MIN, INT_MAX);
189 }
190
191 static void test_crc_sliding(data_t *data)
192 {
193         int i;
194
195         /* Make sure cursor moves smoothly and pixel-by-pixel, and that there are
196          * no alignment issues. Horizontal, vertical and diagonal test.
197          */
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);
202         }
203 }
204
205 static void test_crc_random(data_t *data)
206 {
207         int i;
208
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);
214         }
215 }
216
217 static bool prepare_crtc(data_t *data, igt_output_t *output,
218                          int cursor_w, int cursor_h)
219 {
220         drmModeModeInfo *mode;
221         igt_display_t *display = &data->display;
222         igt_plane_t *primary;
223
224         /* select the pipe we want to use */
225         igt_output_set_pipe(output, data->pipe);
226         igt_display_commit(display);
227
228         if (!output->valid) {
229                 igt_output_set_pipe(output, PIPE_ANY);
230                 igt_display_commit(display);
231                 return false;
232         }
233
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,
237                             DRM_FORMAT_XRGB8888,
238                             false, /* tiled */
239                             0.0, 0.0, 0.0,
240                             &data->primary_fb);
241
242         primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
243         igt_plane_set_fb(primary, &data->primary_fb);
244
245         igt_display_commit(display);
246
247         /* create the pipe_crc object for this pipe */
248         if (data->pipe_crc)
249                 igt_pipe_crc_free(data->pipe_crc);
250
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",
255                          data->pipe);
256                 return false;
257         }
258
259         /* x/y position where the cursor is still fully visible */
260         data->left = 0;
261         data->right = mode->hdisplay - cursor_w;
262         data->top = 0;
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;
269
270         /* make sure cursor is disabled */
271         cursor_disable(data);
272         igt_wait_for_vblank(data->drm_fd, data->pipe);
273
274         /* get reference crc w/o cursor */
275         igt_pipe_crc_collect_crc(data->pipe_crc, &data->ref_crc);
276
277         return true;
278 }
279
280 static void cleanup_crtc(data_t *data, igt_output_t *output)
281 {
282         igt_display_t *display = &data->display;
283         igt_plane_t *primary;
284
285         igt_pipe_crc_free(data->pipe_crc);
286         data->pipe_crc = NULL;
287
288         igt_remove_fb(data->drm_fd, &data->primary_fb);
289
290         primary = igt_output_get_plane(output, IGT_PLANE_PRIMARY);
291         igt_plane_set_fb(primary, NULL);
292
293         igt_output_set_pipe(output, PIPE_ANY);
294         igt_display_commit(display);
295 }
296
297 static void run_test(data_t *data, void (*testfunc)(data_t *), int cursor_w, int cursor_h)
298 {
299         igt_display_t *display = &data->display;
300         igt_output_t *output;
301         enum pipe p;
302         int valid_tests = 0;
303
304         for_each_connected_output(display, output) {
305                 data->output = output;
306                 for_each_pipe(display, p) {
307                         data->pipe = p;
308
309                         if (!prepare_crtc(data, output, cursor_w, cursor_h))
310                                 continue;
311
312                         valid_tests++;
313
314                         igt_info("Beginning %s on pipe %c, connector %s\n",
315                                  igt_subtest_name(), pipe_name(data->pipe),
316                                  igt_output_name(output));
317
318                         testfunc(data);
319
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));
323
324                         /* cleanup what prepare_crtc() has done */
325                         cleanup_crtc(data, output);
326                 }
327         }
328
329         igt_require_f(valid_tests, "no valid crtc/connector combinations found\n");
330 }
331
332 static void create_cursor_fb(data_t *data, int cur_w, int cur_h)
333 {
334         cairo_t *cr;
335         uint32_t fb_id;
336
337         fb_id = igt_create_fb(data->drm_fd, cur_w, cur_h,
338                               DRM_FORMAT_ARGB8888, false,
339                               &data->fb);
340         igt_assert(fb_id);
341
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);
345 }
346
347 static void test_cursor_size(data_t *data)
348 {
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;
352         cairo_t *cr;
353         uint32_t fb_id;
354         int i, size, cursor_max_size = data->cursor_max_size;
355         int ret;
356
357         /* Create a maximum size cursor, then change the size in flight to
358          * smaller ones to see that the size is applied correctly
359          */
360         fb_id = igt_create_fb(data->drm_fd, cursor_max_size, cursor_max_size,
361                               DRM_FORMAT_ARGB8888, false, &data->fb);
362         igt_assert(fb_id);
363
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);
367
368         /* Hardware test loop */
369         cursor_enable(data);
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]);
379         }
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,
391                                 0.0, 0.0, 0.0);
392                 igt_assert(igt_crc_equal(&crc[i], &ref_crc));
393         }
394 }
395
396 static void run_test_generic(data_t *data, int cursor_max_size)
397 {
398         int cursor_size;
399         for (cursor_size = 64; cursor_size <= 256; cursor_size *= 2)
400         {
401                 igt_fixture
402                         igt_require(cursor_max_size >= cursor_size);
403
404                 igt_fixture
405                         create_cursor_fb(data, cursor_size, cursor_size);
406
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);
416
417                 igt_fixture
418                         igt_remove_fb(data->drm_fd, &data->fb);
419         }
420 }
421
422 igt_main
423 {
424         uint64_t cursor_width = 64, cursor_height = 64;
425         data_t data = {};
426         int ret;
427
428         igt_skip_on_simulation();
429
430         igt_fixture {
431                 data.drm_fd = drm_open_any();
432
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);
438
439                 /* We assume width and height are same so max is assigned width */
440                 igt_assert_cmpint(cursor_width, ==, cursor_height);
441
442                 igt_set_vt_graphics_mode();
443
444                 igt_require_pipe_crc();
445
446                 igt_display_init(&data.display, data.drm_fd);
447         }
448
449         igt_subtest_f("cursor-size-change")
450                 run_test(&data, test_cursor_size, cursor_width, cursor_height);
451
452         run_test_generic(&data, cursor_width);
453
454         igt_fixture {
455                 igt_display_fini(&data.display);
456         }
457 }