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