kms_plane: Add a debug message when grabbing the CRC of a colored fb
[platform/upstream/intel-gpu-tools.git] / tests / kms_plane.c
1 /*
2  * Copyright © 2014 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  * Authors:
24  *   Damien Lespiau <damien.lespiau@intel.com>
25  */
26
27 #include <errno.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "drmtest.h"
33 #include "igt_debugfs.h"
34 #include "igt_kms.h"
35
36 typedef struct {
37         float red;
38         float green;
39         float blue;
40 } color_t;
41
42 typedef struct {
43         int drm_fd;
44         igt_display_t display;
45         igt_pipe_crc_t *pipe_crc;
46 } data_t;
47
48 static color_t red   = { 1.0f, 0.0f, 0.0f };
49 static color_t green = { 0.0f, 1.0f, 0.0f };
50 static color_t blue  = { 0.0f, 0.0f, 1.0f };
51
52 /*
53  * Common code across all tests, acting on data_t
54  */
55 static void test_init(data_t *data, enum pipe pipe)
56 {
57         data->pipe_crc = igt_pipe_crc_new(pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
58 }
59
60 static void test_fini(data_t *data)
61 {
62         igt_pipe_crc_free(data->pipe_crc);
63 }
64
65 static void
66 test_grab_crc(data_t *data, igt_output_t *output, color_t *fb_color,
67               igt_crc_t *crc /* out */)
68 {
69         struct igt_fb fb;
70         drmModeModeInfo *mode;
71         igt_plane_t *primary;
72         char *crc_str;
73
74         primary = igt_output_get_plane(output, 0);
75
76         mode = igt_output_get_mode(output);
77         igt_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
78                             DRM_FORMAT_XRGB8888,
79                             false, /* tiled */
80                             fb_color->red, fb_color->green, fb_color->blue,
81                             &fb);
82         igt_plane_set_fb(primary, &fb);
83
84         igt_display_commit(&data->display);
85
86         igt_pipe_crc_collect_crc(data->pipe_crc, crc);
87
88         igt_plane_set_fb(primary, NULL);
89         igt_display_commit(&data->display);
90
91         igt_remove_fb(data->drm_fd, &fb);
92
93         crc_str = igt_crc_to_string(crc);
94         igt_debug("CRC for a (%.02f,%.02f,%.02f) fb: %s\n", fb_color->red,
95                   fb_color->green, fb_color->blue, crc_str);
96         free(crc_str);
97 }
98
99 /*
100  * Plane position test.
101  *   - We start by grabbing a reference CRC of a full green fb being scanned
102  *     out on the primary plane
103  *   - Then we scannout 2 planes:
104  *      - the primary plane uses a green fb with a black rectangle
105  *      - a plane, on top of the primary plane, with a green fb that is set-up
106  *        to cover the black rectangle of the primary plane fb
107  *     The resulting CRC should be identical to the reference CRC
108  */
109
110 typedef struct {
111         data_t *data;
112         igt_crc_t reference_crc;
113 } test_position_t;
114
115 /*
116  * create a green fb with a black rectangle at (rect_x,rect_y) and of size
117  * (rect_w,rect_h)
118  */
119 static void
120 create_fb_for_mode__position(data_t *data, drmModeModeInfo *mode,
121                              double rect_x, double rect_y,
122                              double rect_w, double rect_h,
123                              struct igt_fb *fb /* out */)
124 {
125         unsigned int fb_id;
126         cairo_t *cr;
127
128         fb_id = igt_create_fb(data->drm_fd,
129                                   mode->hdisplay, mode->vdisplay,
130                                   DRM_FORMAT_XRGB8888,
131                                   false /* tiling */,
132                                   fb);
133         igt_assert(fb_id);
134
135         cr = igt_get_cairo_ctx(data->drm_fd, fb);
136         igt_paint_color(cr, 0, 0, mode->hdisplay, mode->vdisplay,
137                             0.0, 1.0, 0.0);
138         igt_paint_color(cr, rect_x, rect_y, rect_w, rect_h, 0.0, 0.0, 0.0);
139         igt_assert(cairo_status(cr) == 0);
140         cairo_destroy(cr);
141 }
142
143 enum {
144         TEST_POSITION_FULLY_COVERED = 1 << 0,
145 };
146
147 static void
148 test_plane_position_with_output(data_t *data,
149                                 enum pipe pipe,
150                                 enum igt_plane plane,
151                                 igt_output_t *output,
152                                 unsigned int flags)
153 {
154         test_position_t test = { .data = data };
155         igt_plane_t *primary, *sprite;
156         struct igt_fb primary_fb, sprite_fb;
157         drmModeModeInfo *mode;
158         igt_crc_t crc;
159
160         igt_info("Testing connector %s using pipe %c plane %d\n",
161                  igt_output_name(output), pipe_name(pipe), plane);
162
163         test_init(data, pipe);
164
165         test_grab_crc(data, output, &green, &test.reference_crc);
166
167         igt_output_set_pipe(output, pipe);
168
169         mode = igt_output_get_mode(output);
170         primary = igt_output_get_plane(output, 0);
171         sprite = igt_output_get_plane(output, plane);
172
173         create_fb_for_mode__position(data, mode, 100, 100, 64, 64,
174                                      &primary_fb);
175         igt_plane_set_fb(primary, &primary_fb);
176
177         igt_create_color_fb(data->drm_fd,
178                                 64, 64, /* width, height */
179                                 DRM_FORMAT_XRGB8888,
180                                 false, /* tiled */
181                                 0.0, 1.0, 0.0,
182                                 &sprite_fb);
183         igt_plane_set_fb(sprite, &sprite_fb);
184
185         if (flags & TEST_POSITION_FULLY_COVERED)
186                 igt_plane_set_position(sprite, 100, 100);
187         else
188                 igt_plane_set_position(sprite, 132, 132);
189
190         igt_display_commit(&data->display);
191
192         igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
193
194         if (flags & TEST_POSITION_FULLY_COVERED)
195                 igt_assert(igt_crc_equal(&test.reference_crc, &crc));
196         else
197                 igt_assert(!igt_crc_equal(&test.reference_crc, &crc));
198
199         igt_plane_set_fb(primary, NULL);
200         igt_plane_set_fb(sprite, NULL);
201
202         /* reset the constraint on the pipe */
203         igt_output_set_pipe(output, PIPE_ANY);
204
205         test_fini(data);
206 }
207
208 static void
209 test_plane_position(data_t *data, enum pipe pipe, enum igt_plane plane,
210                     unsigned int flags)
211 {
212         igt_output_t *output;
213
214         igt_skip_on(pipe >= data->display.n_pipes);
215         igt_skip_on(plane >= data->display.pipes[pipe].n_planes);
216
217         for_each_connected_output(&data->display, output)
218                 test_plane_position_with_output(data, pipe, plane, output,
219                                                 flags);
220 }
221
222 /*
223  * Plane panning test.
224  *   - We start by grabbing reference CRCs of a full red and a full blue fb
225  *     being scanned out on the primary plane
226  *   - Then we create a big fb, sized (2 * hdisplay, 2 * vdisplay) and:
227  *      - fill the top left quarter with red
228  *      - fill the bottom right quarter with blue
229  *   - The TEST_PANNING_TOP_LEFT test makes sure that with panning at (0, 0)
230  *     we do get the same CRC than the full red fb.
231  *   - The TEST_PANNING_BOTTOM_RIGHT test makes sure that with panning at
232  *     (vdisplay, hdisplay) we do get the same CRC than the full blue fb.
233  */
234 typedef struct {
235         data_t *data;
236         igt_crc_t red_crc, blue_crc;
237 } test_panning_t;
238
239 static void
240 create_fb_for_mode__panning(data_t *data, drmModeModeInfo *mode,
241                             struct igt_fb *fb /* out */)
242 {
243         unsigned int fb_id;
244         cairo_t *cr;
245
246         fb_id = igt_create_fb(data->drm_fd,
247                               mode->hdisplay * 2, mode->vdisplay * 2,
248                               DRM_FORMAT_XRGB8888,
249                               false /* tiling */,
250                               fb);
251         igt_assert(fb_id);
252
253         cr = igt_get_cairo_ctx(data->drm_fd, fb);
254
255         igt_paint_color(cr, 0, 0, mode->hdisplay, mode->vdisplay,
256                         1.0, 0.0, 0.0);
257
258         igt_paint_color(cr,
259                         mode->hdisplay, mode->vdisplay,
260                         mode->hdisplay, mode->vdisplay,
261                         0.0, 0.0, 1.0);
262
263         igt_assert(cairo_status(cr) == 0);
264         cairo_destroy(cr);
265 }
266
267 enum {
268         TEST_PANNING_TOP_LEFT     = 1 << 0,
269         TEST_PANNING_BOTTOM_RIGHT = 1 << 1,
270 };
271
272 static void
273 test_plane_panning_with_output(data_t *data,
274                                enum pipe pipe,
275                                enum igt_plane plane,
276                                igt_output_t *output,
277                                unsigned int flags)
278 {
279         test_panning_t test = { .data = data };
280         igt_plane_t *primary;
281         struct igt_fb primary_fb;
282         drmModeModeInfo *mode;
283         igt_crc_t crc;
284
285         fprintf(stdout, "Testing connector %s using pipe %c plane %d\n",
286                 igt_output_name(output), pipe_name(pipe), plane);
287
288         test_init(data, pipe);
289
290         test_grab_crc(data, output, &red, &test.red_crc);
291         test_grab_crc(data, output, &blue, &test.blue_crc);
292
293         igt_output_set_pipe(output, pipe);
294
295         mode = igt_output_get_mode(output);
296         primary = igt_output_get_plane(output, 0);
297
298         create_fb_for_mode__panning(data, mode, &primary_fb);
299         igt_plane_set_fb(primary, &primary_fb);
300
301         if (flags & TEST_PANNING_TOP_LEFT)
302                 igt_plane_set_panning(primary, 0, 0);
303         else
304                 igt_plane_set_panning(primary, mode->hdisplay, mode->vdisplay);
305
306         igt_plane_set_position(primary, 0, 0);
307
308         igt_display_commit(&data->display);
309
310         igt_pipe_crc_collect_crc(data->pipe_crc, &crc);
311
312         if (flags & TEST_PANNING_TOP_LEFT)
313                 igt_assert(igt_crc_equal(&test.red_crc, &crc));
314         else
315                 igt_assert(igt_crc_equal(&test.blue_crc, &crc));
316
317         igt_plane_set_fb(primary, NULL);
318
319         /* reset states to neutral values, assumed by other tests */
320         igt_output_set_pipe(output, PIPE_ANY);
321         igt_plane_set_panning(primary, 0, 0);
322
323         test_fini(data);
324 }
325
326 static void
327 test_plane_panning(data_t *data, enum pipe pipe, enum igt_plane plane,
328             unsigned int flags)
329 {
330         igt_output_t *output;
331
332         igt_skip_on(pipe >= data->display.n_pipes);
333         igt_skip_on(plane >= data->display.pipes[pipe].n_planes);
334
335         for_each_connected_output(&data->display, output)
336                 test_plane_panning_with_output(data, pipe, plane, output,
337                                                 flags);
338 }
339
340 static void
341 run_tests_for_pipe_plane(data_t *data, enum pipe pipe, enum igt_plane plane)
342 {
343         igt_subtest_f("plane-position-covered-pipe-%c-plane-%d",
344                       pipe_name(pipe), plane)
345                 test_plane_position(data, pipe, plane,
346                                     TEST_POSITION_FULLY_COVERED);
347
348         igt_subtest_f("plane-position-hole-pipe-%c-plane-%d",
349                       pipe_name(pipe), plane)
350                 test_plane_position(data, pipe, plane, 0);
351
352         igt_subtest_f("plane-panning-top-left-pipe-%c-plane-%d",
353                       pipe_name(pipe), plane)
354                 test_plane_panning(data, pipe, plane, TEST_PANNING_TOP_LEFT);
355
356         igt_subtest_f("plane-panning-bottom-right-pipe-%c-plane-%d",
357                       pipe_name(pipe), plane)
358                 test_plane_panning(data, pipe, plane,
359                                    TEST_PANNING_BOTTOM_RIGHT);
360
361 }
362
363 static void
364 run_tests_for_pipe(data_t *data, enum pipe pipe)
365 {
366         int plane;
367
368         for (plane = 1; plane < IGT_MAX_PLANES; plane++)
369                 run_tests_for_pipe_plane(data, pipe, plane);
370 }
371
372 static data_t data;
373
374 igt_main
375 {
376
377         igt_skip_on_simulation();
378
379         igt_fixture {
380                 data.drm_fd = drm_open_any();
381
382                 igt_set_vt_graphics_mode();
383
384                 igt_require_pipe_crc();
385                 igt_display_init(&data.display, data.drm_fd);
386         }
387
388         for (int pipe = 0; pipe < 3; pipe++)
389                 run_tests_for_pipe(&data, pipe);
390
391         igt_fixture {
392                 igt_display_fini(&data.display);
393         }
394 }