kms_plane: Start a basic display plane 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 <glib.h>
33
34 #include "drmtest.h"
35 #include "igt_debugfs.h"
36 #include "igt_kms.h"
37
38 typedef struct {
39         int drm_fd;
40         igt_debugfs_t debugfs;
41         igt_display_t display;
42 } data_t;
43
44 /*
45  * Plane position test.
46  *   - We start by grabbing a reference CRC of a full green fb being scanned
47  *     out on the primary plane
48  *   - Then we scannout 2 planes:
49  *      - the primary plane uses a green fb with a black rectangle
50  *      - a plane, on top of the primary plane, with a green fb that is set-up
51  *        to cover the black rectangle of the primary plane fb
52  *     The resulting CRC should be identical to the reference CRC
53  */
54
55 typedef struct {
56         data_t *data;
57         igt_pipe_crc_t *pipe_crc;
58         igt_crc_t reference_crc;
59 } test_position_t;
60
61 /*
62  * create a green fb with a black rectangle at (rect_x,rect_y) and of size
63  * (rect_w,rect_h)
64  */
65 static void
66 create_fb_for_mode__position(data_t *data, drmModeModeInfo *mode,
67                              double rect_x, double rect_y,
68                              double rect_w, double rect_h,
69                              struct kmstest_fb *fb /* out */)
70 {
71         unsigned int fb_id;
72         cairo_t *cr;
73
74         fb_id = kmstest_create_fb(data->drm_fd,
75                                   mode->hdisplay, mode->vdisplay,
76                                   32 /* bpp */, 24 /* depth */,
77                                   false /* tiling */,
78                                   fb);
79         igt_assert(fb_id);
80
81         cr = kmstest_get_cairo_ctx(data->drm_fd, fb);
82         kmstest_paint_color(cr, 0, 0, mode->hdisplay, mode->vdisplay,
83                             0.0, 1.0, 0.0);
84         kmstest_paint_color(cr, rect_x, rect_y, rect_w, rect_h, 0.0, 0.0, 0.0);
85         igt_assert(cairo_status(cr) == 0);
86         cairo_destroy(cr);
87 }
88
89 static void
90 test_position_init(test_position_t *test, igt_output_t *output, enum pipe pipe)
91 {
92         data_t *data = test->data;
93         struct kmstest_fb green_fb;
94         drmModeModeInfo *mode;
95         igt_plane_t *primary;
96
97         test->pipe_crc = igt_pipe_crc_new(&data->debugfs, data->drm_fd,
98                                           pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
99
100         igt_output_set_pipe(output, pipe);
101         primary = igt_ouput_get_plane(output, 0);
102
103         mode = igt_output_get_mode(output);
104         kmstest_create_color_fb(data->drm_fd, mode->hdisplay, mode->vdisplay,
105                                 DRM_FORMAT_XRGB8888,
106                                 false, /* tiled */
107                                 0.0, 1.0, 0.0,
108                                 &green_fb);
109         igt_plane_set_fb(primary, &green_fb);
110
111         igt_display_commit(&data->display);
112
113         igt_pipe_crc_collect_crc(test->pipe_crc, &test->reference_crc);
114
115         igt_plane_set_fb(primary, NULL);
116         igt_display_commit(&data->display);
117
118         kmstest_remove_fb(data->drm_fd, &green_fb);
119 }
120
121 static void
122 test_position_fini(test_position_t *test, igt_output_t *output)
123 {
124         igt_pipe_crc_free(test->pipe_crc);
125
126         igt_output_set_pipe(output, PIPE_ANY);
127         igt_display_commit(&test->data->display);
128 }
129
130 enum {
131         TEST_POSITION_FULLY_COVERED = 1 << 0,
132 };
133
134 static void
135 test_plane_position_with_output(data_t *data,
136                                 enum pipe pipe,
137                                 enum igt_plane plane,
138                                 igt_output_t *output,
139                                 unsigned int flags)
140 {
141         test_position_t test = { .data = data };
142         igt_plane_t *primary, *sprite;
143         struct kmstest_fb primary_fb, sprite_fb;
144         drmModeModeInfo *mode;
145         igt_crc_t crc;
146
147         fprintf(stdout, "Testing connector %s using pipe %c plane %d\n",
148                 igt_output_name(output), pipe_name(pipe), plane);
149
150         test_position_init(&test, output, pipe);
151
152         mode = igt_output_get_mode(output);
153         primary = igt_ouput_get_plane(output, 0);
154         sprite = igt_ouput_get_plane(output, plane);
155
156         create_fb_for_mode__position(data, mode, 100, 100, 64, 64,
157                                      &primary_fb);
158         igt_plane_set_fb(primary, &primary_fb);
159
160         kmstest_create_color_fb(data->drm_fd,
161                                 64, 64, /* width, height */
162                                 DRM_FORMAT_XRGB8888,
163                                 false, /* tiled */
164                                 0.0, 1.0, 0.0,
165                                 &sprite_fb);
166         igt_plane_set_fb(sprite, &sprite_fb);
167
168         if (flags & TEST_POSITION_FULLY_COVERED)
169                 igt_plane_set_position(sprite, 100, 100);
170         else
171                 igt_plane_set_position(sprite, 132, 132);
172
173         igt_display_commit(&data->display);
174
175         igt_pipe_crc_collect_crc(test.pipe_crc, &crc);
176
177         if (flags & TEST_POSITION_FULLY_COVERED)
178                 igt_assert(igt_crc_equal(&test.reference_crc, &crc));
179         else
180                 igt_assert(!igt_crc_equal(&test.reference_crc, &crc));
181
182         igt_plane_set_fb(primary, NULL);
183         igt_plane_set_fb(sprite, NULL);
184
185         test_position_fini(&test, output);
186 }
187
188 static void
189 test_plane_position(data_t *data, enum pipe pipe, enum igt_plane plane,
190                     unsigned int flags)
191 {
192         igt_output_t *output;
193
194         igt_skip_on(pipe >= data->display.n_pipes);
195         igt_skip_on(plane >= data->display.pipes[pipe].n_planes);
196
197         for_each_connected_output(&data->display, output)
198                 test_plane_position_with_output(data, pipe, plane, output,
199                                                 flags);
200 }
201
202 static void
203 run_tests_for_pipe_plane(data_t *data, enum pipe pipe, enum igt_plane plane)
204 {
205         igt_subtest_f("plane-position-covered-pipe-%c-plane-%d",
206                       pipe_name(pipe), plane)
207                 test_plane_position(data, pipe, plane,
208                                     TEST_POSITION_FULLY_COVERED);
209
210         igt_subtest_f("plane-position-hole-pipe-%c-plane-%d",
211                       pipe_name(pipe), plane)
212                 test_plane_position(data, pipe, plane, 0);
213 }
214
215 static void
216 run_tests_for_pipe(data_t *data, enum pipe pipe)
217 {
218         int plane;
219
220         for (plane = 1; plane < IGT_MAX_PLANES; plane++)
221                 run_tests_for_pipe_plane(data, pipe, plane);
222 }
223
224 static data_t data;
225
226 igt_main
227 {
228
229         igt_skip_on_simulation();
230
231         igt_fixture {
232                 data.drm_fd = drm_open_any();
233
234                 igt_set_vt_graphics_mode();
235
236                 igt_debugfs_init(&data.debugfs);
237                 igt_pipe_crc_check(&data.debugfs);
238                 igt_display_init(&data.display, data.drm_fd);
239         }
240
241         for (int pipe = 0; pipe < 3; pipe++)
242                 run_tests_for_pipe(&data, pipe);
243
244         igt_fixture {
245                 igt_display_fini(&data.display);
246         }
247 }