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