1 /*
2 * Copyright © 2013 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
25 #include "igt.h"
26 #include "igt_sysfs.h"
27 #include <errno.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <fcntl.h>
32
33
34 typedef struct {
35 int drm_fd;
36 int debugfs;
37 igt_display_t display;
38 struct igt_fb fb;
39 } data_t;
40
41 static struct {
42 double r, g, b;
43 igt_crc_t crc;
44 } colors[2] = {
45 { .r = 0.0, .g = 1.0, .b = 0.0 },
46 { .r = 0.0, .g = 1.0, .b = 1.0 },
47 };
48
test_bad_source(data_t * data)49 static void test_bad_source(data_t *data)
50 {
51 errno = 0;
52 if (igt_sysfs_set(data->debugfs, "crtc-0/crc/control", "foo")) {
53 igt_assert(openat(data->debugfs, "crtc-0/crc/data", O_WRONLY) == -1);
54 igt_skip_on(errno == EIO);
55 }
56
57 igt_assert_eq(errno, EINVAL);
58 }
59
60 #define N_CRCS 3
61
62 #define TEST_SEQUENCE (1<<0)
63 #define TEST_NONBLOCK (1<<1)
64
test_read_crc(data_t * data,enum pipe pipe,unsigned flags)65 static void test_read_crc(data_t *data, enum pipe pipe, unsigned flags)
66 {
67 igt_display_t *display = &data->display;
68 igt_output_t *output = igt_get_single_output_for_pipe(display, pipe);
69 igt_plane_t *primary;
70 drmModeModeInfo *mode;
71 igt_crc_t *crcs = NULL;
72 int c, j;
73
74 igt_skip_on(pipe >= data->display.n_pipes);
75 igt_require_f(output, "No connector found for pipe %s\n",
76 kmstest_pipe_name(pipe));
77
78 igt_display_reset(display);
79 igt_output_set_pipe(output, pipe);
80
81 for (c = 0; c < ARRAY_SIZE(colors); c++) {
82 char *crc_str;
83 int n_crcs;
84
85 igt_debug("Clearing the fb with color (%.02lf,%.02lf,%.02lf)\n",
86 colors[c].r, colors[c].g, colors[c].b);
87
88 mode = igt_output_get_mode(output);
89 igt_create_color_fb(data->drm_fd,
90 mode->hdisplay, mode->vdisplay,
91 DRM_FORMAT_XRGB8888,
92 LOCAL_DRM_FORMAT_MOD_NONE,
93 colors[c].r,
94 colors[c].g,
95 colors[c].b,
96 &data->fb);
97
98 primary = igt_output_get_plane(output, 0);
99 igt_plane_set_fb(primary, &data->fb);
100
101 igt_display_commit(display);
102
103 /* wait for N_CRCS vblanks and the corresponding N_CRCS CRCs */
104 if (flags & TEST_NONBLOCK) {
105 igt_pipe_crc_t *pipe_crc;
106
107 pipe_crc = igt_pipe_crc_new_nonblock(data->drm_fd, pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
108 igt_wait_for_vblank(data->drm_fd, pipe);
109 igt_pipe_crc_start(pipe_crc);
110
111 igt_wait_for_vblank_count(data->drm_fd, pipe, N_CRCS);
112 n_crcs = igt_pipe_crc_get_crcs(pipe_crc, N_CRCS+1, &crcs);
113 igt_pipe_crc_stop(pipe_crc);
114 igt_pipe_crc_free(pipe_crc);
115
116 /* allow a one frame difference */
117 igt_assert_lte(N_CRCS, n_crcs);
118 } else {
119 igt_pipe_crc_t *pipe_crc;
120
121 pipe_crc = igt_pipe_crc_new(data->drm_fd, pipe, INTEL_PIPE_CRC_SOURCE_AUTO);
122 igt_pipe_crc_start(pipe_crc);
123
124 n_crcs = igt_pipe_crc_get_crcs(pipe_crc, N_CRCS, &crcs);
125
126 igt_pipe_crc_stop(pipe_crc);
127 igt_pipe_crc_free(pipe_crc);
128
129 igt_assert_eq(n_crcs, N_CRCS);
130 }
131
132
133 /*
134 * save the CRC in colors so it can be compared to the CRC of
135 * other fbs
136 */
137 colors[c].crc = crcs[0];
138
139 crc_str = igt_crc_to_string(&crcs[0]);
140 igt_debug("CRC for this fb: %s\n", crc_str);
141 free(crc_str);
142
143 /* and ensure that they'are all equal, we haven't changed the fb */
144 for (j = 0; j < (n_crcs - 1); j++)
145 igt_assert_crc_equal(&crcs[j], &crcs[j + 1]);
146
147 if (flags & TEST_SEQUENCE)
148 for (j = 0; j < (n_crcs - 1); j++)
149 igt_assert_eq(crcs[j].frame + 1, crcs[j + 1].frame);
150
151 free(crcs);
152 igt_remove_fb(data->drm_fd, &data->fb);
153 }
154 }
155
156 data_t data = {0, };
157
158 igt_main
159 {
160 enum pipe pipe;
161
162 igt_fixture {
163 data.drm_fd = drm_open_driver_master(DRIVER_ANY);
164
165 kmstest_set_vt_graphics_mode();
166
167 igt_require_pipe_crc(data.drm_fd);
168
169 igt_display_require(&data.display, data.drm_fd);
170 data.debugfs = igt_debugfs_dir(data.drm_fd);
171 }
172
173 igt_subtest("bad-source")
174 test_bad_source(&data);
175
176 igt_skip_on_simulation();
177
for_each_pipe_static(pipe)178 for_each_pipe_static(pipe) {
179 igt_subtest_f("read-crc-pipe-%s", kmstest_pipe_name(pipe))
180 test_read_crc(&data, pipe, 0);
181
182 igt_subtest_f("read-crc-pipe-%s-frame-sequence", kmstest_pipe_name(pipe))
183 test_read_crc(&data, pipe, TEST_SEQUENCE);
184
185 igt_subtest_f("nonblocking-crc-pipe-%s", kmstest_pipe_name(pipe))
186 test_read_crc(&data, pipe, TEST_NONBLOCK);
187
188 igt_subtest_f("nonblocking-crc-pipe-%s-frame-sequence", kmstest_pipe_name(pipe))
189 test_read_crc(&data, pipe, TEST_SEQUENCE | TEST_NONBLOCK);
190
191 igt_subtest_f("suspend-read-crc-pipe-%s", kmstest_pipe_name(pipe)) {
192 igt_skip_on(pipe >= data.display.n_pipes);
193
194 test_read_crc(&data, pipe, 0);
195
196 igt_system_suspend_autoresume(SUSPEND_STATE_MEM,
197 SUSPEND_TEST_NONE);
198
199 test_read_crc(&data, pipe, 0);
200 }
201
202 igt_subtest_f("hang-read-crc-pipe-%s", kmstest_pipe_name(pipe)) {
203 igt_hang_t hang = igt_allow_hang(data.drm_fd, 0, 0);
204
205 test_read_crc(&data, pipe, 0);
206
207 igt_force_gpu_reset(data.drm_fd);
208
209 test_read_crc(&data, pipe, 0);
210
211 igt_disallow_hang(data.drm_fd, hang);
212 }
213 }
214
215 igt_fixture {
216 igt_display_fini(&data.display);
217 }
218 }
219