1 /*
2 * Copyright © 2019 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 #include "igt.h"
25 #include "igt_vgem.h"
26
27 #include <sys/ioctl.h>
28 #include <sys/poll.h>
29 #include <time.h>
30
31 struct crc_info {
32 igt_crc_t crc;
33 char *str;
34 const char *name;
35 };
36
37 static struct {
38 double r, g, b;
39 uint32_t color;
40 struct crc_info prime_crc, direct_crc;
41 } colors[3] = {
42 { .r = 0.0, .g = 0.0, .b = 0.0, .color = 0xff000000 },
43 { .r = 1.0, .g = 1.0, .b = 1.0, .color = 0xffffffff },
44 { .r = 1.0, .g = 0.0, .b = 0.0, .color = 0xffff0000 },
45 };
46
47 IGT_TEST_DESCRIPTION("Prime tests, focusing on KMS side");
48
has_prime_import(int fd)49 static bool has_prime_import(int fd)
50 {
51 uint64_t value;
52
53 if (drmGetCap(fd, DRM_CAP_PRIME, &value))
54 return false;
55
56 return value & DRM_PRIME_CAP_IMPORT;
57 }
58
has_prime_export(int fd)59 static bool has_prime_export(int fd)
60 {
61 uint64_t value;
62
63 if (drmGetCap(fd, DRM_CAP_PRIME, &value))
64 return false;
65
66 return value & DRM_PRIME_CAP_EXPORT;
67 }
68
setup_display(int importer_fd,igt_display_t * display,enum pipe pipe)69 static igt_output_t *setup_display(int importer_fd, igt_display_t *display,
70 enum pipe pipe)
71 {
72 igt_display_require(display, importer_fd);
73 igt_skip_on(pipe >= display->n_pipes);
74 igt_output_t *output = igt_get_single_output_for_pipe(display, pipe);
75
76 igt_require_f(output, "No connector found for pipe %s\n",
77 kmstest_pipe_name(pipe));
78
79 igt_display_reset(display);
80 igt_output_set_pipe(output, pipe);
81 return output;
82 }
83
prepare_scratch(int exporter_fd,struct vgem_bo * scratch,drmModeModeInfo * mode,uint32_t color)84 static void prepare_scratch(int exporter_fd, struct vgem_bo *scratch,
85 drmModeModeInfo *mode, uint32_t color)
86 {
87 uint32_t *ptr;
88
89 scratch->width = mode->hdisplay;
90 scratch->height = mode->vdisplay;
91 scratch->bpp = 32;
92 vgem_create(exporter_fd, scratch);
93
94 ptr = vgem_mmap(exporter_fd, scratch, PROT_WRITE);
95 for (size_t idx = 0; idx < scratch->size / sizeof(*ptr); ++idx)
96 ptr[idx] = color;
97
98 munmap(ptr, scratch->size);
99 }
100
prepare_fb(int importer_fd,struct vgem_bo * scratch,struct igt_fb * fb)101 static void prepare_fb(int importer_fd, struct vgem_bo *scratch, struct igt_fb *fb)
102 {
103 enum igt_color_encoding color_encoding = IGT_COLOR_YCBCR_BT709;
104 enum igt_color_range color_range = IGT_COLOR_YCBCR_LIMITED_RANGE;
105
106 igt_init_fb(fb, importer_fd, scratch->width, scratch->height,
107 DRM_FORMAT_XRGB8888, LOCAL_DRM_FORMAT_MOD_NONE,
108 color_encoding, color_range);
109 }
110
import_fb(int importer_fd,struct igt_fb * fb,int dmabuf_fd,uint32_t pitch)111 static void import_fb(int importer_fd, struct igt_fb *fb,
112 int dmabuf_fd, uint32_t pitch)
113 {
114 uint32_t offsets[4] = {}, pitches[4] = {}, handles[4] = {};
115 int ret;
116
117 fb->gem_handle = prime_fd_to_handle(importer_fd, dmabuf_fd);
118
119 handles[0] = fb->gem_handle;
120 pitches[0] = pitch;
121 offsets[0] = 0;
122
123 ret = drmModeAddFB2(importer_fd, fb->width, fb->height,
124 DRM_FORMAT_XRGB8888,
125 handles, pitches, offsets,
126 &fb->fb_id, 0);
127 igt_assert(ret == 0);
128 }
129
set_fb(struct igt_fb * fb,igt_display_t * display,igt_output_t * output)130 static void set_fb(struct igt_fb *fb,
131 igt_display_t *display,
132 igt_output_t *output)
133 {
134 igt_plane_t *primary;
135 int ret;
136
137 primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
138 igt_assert(primary);
139
140 igt_plane_set_fb(primary, fb);
141 ret = igt_display_commit(display);
142
143 igt_assert(ret == 0);
144 }
145
collect_crc_for_fb(int importer_fd,struct igt_fb * fb,igt_display_t * display,igt_output_t * output,igt_pipe_crc_t * pipe_crc,uint32_t color,struct crc_info * info)146 static void collect_crc_for_fb(int importer_fd, struct igt_fb *fb, igt_display_t *display,
147 igt_output_t *output, igt_pipe_crc_t *pipe_crc,
148 uint32_t color, struct crc_info *info)
149 {
150 set_fb(fb, display, output);
151 igt_pipe_crc_collect_crc(pipe_crc, &info->crc);
152 info->str = igt_crc_to_string(&info->crc);
153 igt_debug("CRC through '%s' method for %#08x is %s\n",
154 info->name, color, info->str);
155 igt_remove_fb(importer_fd, fb);
156 }
157
test_crc(int exporter_fd,int importer_fd)158 static void test_crc(int exporter_fd, int importer_fd)
159 {
160 igt_display_t display;
161 igt_output_t *output;
162 igt_pipe_crc_t *pipe_crc;
163 enum pipe pipe = PIPE_A;
164 struct igt_fb fb;
165 int dmabuf_fd;
166 struct vgem_bo scratch = {}; /* despite the name, it suits for any
167 * gem-compatible device
168 * TODO: rename
169 */
170 int i, j;
171 drmModeModeInfo *mode;
172
173 bool crc_equal = false;
174
175 output = setup_display(importer_fd, &display, pipe);
176
177 mode = igt_output_get_mode(output);
178 pipe_crc = igt_pipe_crc_new(importer_fd, pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
179
180 for (i = 0; i < ARRAY_SIZE(colors); i++) {
181 prepare_scratch(exporter_fd, &scratch, mode, colors[i].color);
182 dmabuf_fd = prime_handle_to_fd(exporter_fd, scratch.handle);
183 gem_close(exporter_fd, scratch.handle);
184
185 prepare_fb(importer_fd, &scratch, &fb);
186 import_fb(importer_fd, &fb, dmabuf_fd, scratch.pitch);
187 close(dmabuf_fd);
188
189 colors[i].prime_crc.name = "prime";
190 collect_crc_for_fb(importer_fd, &fb, &display, output,
191 pipe_crc, colors[i].color, &colors[i].prime_crc);
192
193 igt_create_color_fb(importer_fd,
194 mode->hdisplay, mode->vdisplay,
195 DRM_FORMAT_XRGB8888, LOCAL_DRM_FORMAT_MOD_NONE,
196 colors[i].r, colors[i].g, colors[i].b,
197 &fb);
198
199 colors[i].direct_crc.name = "direct";
200 collect_crc_for_fb(importer_fd, &fb, &display, output,
201 pipe_crc, colors[i].color, &colors[i].direct_crc);
202 }
203 igt_pipe_crc_free(pipe_crc);
204
205 igt_debug("CRC table:\n");
206 igt_debug("Color\t\tPrime\t\tDirect\n");
207 for (i = 0; i < ARRAY_SIZE(colors); i++) {
208 igt_debug("%#08x\t%.8s\t%.8s\n", colors[i].color,
209 colors[i].prime_crc.str, colors[i].direct_crc.str);
210 free(colors[i].prime_crc.str);
211 free(colors[i].direct_crc.str);
212 }
213
214 for (i = 0; i < ARRAY_SIZE(colors); i++) {
215 for (j = 0; j < ARRAY_SIZE(colors); j++) {
216 if (i == j) {
217 igt_assert_crc_equal(&colors[i].prime_crc.crc,
218 &colors[j].direct_crc.crc);
219 continue;
220 }
221 crc_equal = igt_check_crc_equal(&colors[i].prime_crc.crc,
222 &colors[j].direct_crc.crc);
223 igt_assert_f(!crc_equal, "CRC should be different");
224 }
225 }
226 igt_display_fini(&display);
227 }
228
run_test_crc(int export_chipset,int import_chipset)229 static void run_test_crc(int export_chipset, int import_chipset)
230 {
231 int importer_fd = -1;
232 int exporter_fd = -1;
233
234 exporter_fd = drm_open_driver(export_chipset);
235 importer_fd = drm_open_driver_master(import_chipset);
236
237 igt_require(has_prime_export(exporter_fd));
238 igt_require(has_prime_import(importer_fd));
239 igt_require_pipe_crc(importer_fd);
240
241 test_crc(exporter_fd, importer_fd);
242 close(importer_fd);
243 close(exporter_fd);
244 }
245
246 igt_main
247 {
248 igt_fixture {
249 kmstest_set_vt_graphics_mode();
250 }
251 igt_describe("Make a dumb buffer inside vgem, fill it, export to another device and compare the CRC");
252 igt_subtest("basic-crc")
253 run_test_crc(DRIVER_VGEM, DRIVER_ANY);
254 }
255