1 /*
2 * Copyright 2019 Advanced Micro Devices, Inc.
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 shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "igt.h"
24
25 /* (De)gamma LUT. */
26 typedef struct lut {
27 struct drm_color_lut *data;
28 uint32_t size;
29 } lut_t;
30
31 /* RGB color. */
32 typedef struct color {
33 double r;
34 double g;
35 double b;
36 } color_t;
37
38 /* Common test data. */
39 typedef struct data {
40 igt_display_t display;
41 igt_plane_t *primary;
42 igt_output_t *output;
43 igt_pipe_t *pipe;
44 igt_pipe_crc_t *pipe_crc;
45 drmModeModeInfo *mode;
46 enum pipe pipe_id;
47 int fd;
48 int w;
49 int h;
50 uint32_t regamma_lut_size;
51 uint32_t degamma_lut_size;
52 } data_t;
53
lut_init(lut_t * lut,uint32_t size)54 static void lut_init(lut_t *lut, uint32_t size)
55 {
56 igt_assert(size > 0);
57
58 lut->size = size;
59 lut->data = malloc(size * sizeof(struct drm_color_lut));
60 igt_assert(lut);
61 }
62
63 /* Generates the linear gamma LUT. */
lut_gen_linear(lut_t * lut,uint16_t mask)64 static void lut_gen_linear(lut_t *lut, uint16_t mask)
65 {
66 uint32_t n = lut->size - 1;
67 uint32_t i;
68
69 for (i = 0; i < lut->size; ++i) {
70 uint32_t v = (i * 0xffff / n) & mask;
71
72 lut->data[i].red = v;
73 lut->data[i].blue = v;
74 lut->data[i].green = v;
75 }
76 }
77
78 /* Generates the sRGB degamma LUT. */
lut_gen_degamma_srgb(lut_t * lut,uint16_t mask)79 static void lut_gen_degamma_srgb(lut_t *lut, uint16_t mask)
80 {
81 double range = lut->size - 1;
82 uint32_t i;
83
84 for (i = 0; i < lut->size; ++i) {
85 double u, coeff;
86 uint32_t v;
87
88 u = i / range;
89 coeff = u <= 0.040449936 ? (u / 12.92)
90 : (pow((u + 0.055) / 1.055, 2.4));
91 v = (uint32_t)(coeff * 0xffff) & mask;
92
93 lut->data[i].red = v;
94 lut->data[i].blue = v;
95 lut->data[i].green = v;
96 }
97 }
98
99 /* Generates the sRGB gamma LUT. */
lut_gen_regamma_srgb(lut_t * lut,uint16_t mask)100 static void lut_gen_regamma_srgb(lut_t *lut, uint16_t mask)
101 {
102 double range = lut->size - 1;
103 uint32_t i;
104
105 for (i = 0; i < lut->size; ++i) {
106 double u, coeff;
107 uint32_t v;
108
109 u = i / range;
110 coeff = u <= 0.00313080 ? (12.92 * u)
111 : (1.055 * pow(u, 1.0 / 2.4) - 0.055);
112 v = (uint32_t)(coeff * 0xffff) & mask;
113
114 lut->data[i].red = v;
115 lut->data[i].blue = v;
116 lut->data[i].green = v;
117 }
118 }
119
lut_free(lut_t * lut)120 static void lut_free(lut_t *lut)
121 {
122 if (lut->data) {
123 free(lut->data);
124 lut->data = NULL;
125 }
126
127 lut->size = 0;
128 }
129
130 /* Fills a FB with the solid color given. */
draw_color(igt_fb_t * fb,double r,double g,double b)131 static void draw_color(igt_fb_t *fb, double r, double g, double b)
132 {
133 cairo_t *cr = igt_get_cairo_ctx(fb->fd, fb);
134
135 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
136 igt_paint_color(cr, 0, 0, fb->width, fb->height, r, g, b);
137 igt_put_cairo_ctx(fb->fd, fb, cr);
138 }
139
140 /* Generates the gamma test pattern. */
draw_gamma_test(igt_fb_t * fb)141 static void draw_gamma_test(igt_fb_t *fb)
142 {
143 cairo_t *cr = igt_get_cairo_ctx(fb->fd, fb);
144 int gh = fb->height / 4;
145
146 igt_paint_color_gradient(cr, 0, 0, fb->width, gh, 1, 1, 1);
147 igt_paint_color_gradient(cr, 0, gh, fb->width, gh, 1, 0, 0);
148 igt_paint_color_gradient(cr, 0, gh * 2, fb->width, gh, 0, 1, 0);
149 igt_paint_color_gradient(cr, 0, gh * 3, fb->width, gh, 0, 0, 1);
150
151 igt_put_cairo_ctx(fb->fd, fb, cr);
152 }
153
154 /* Sets the degamma LUT. */
set_degamma_lut(data_t * data,lut_t const * lut)155 static void set_degamma_lut(data_t *data, lut_t const *lut)
156 {
157 size_t size = lut ? sizeof(lut->data[0]) * lut->size : 0;
158 const void *ptr = lut ? lut->data : NULL;
159
160 igt_pipe_obj_replace_prop_blob(data->pipe, IGT_CRTC_DEGAMMA_LUT, ptr,
161 size);
162 }
163
164 /* Sets the regamma LUT. */
set_regamma_lut(data_t * data,lut_t const * lut)165 static void set_regamma_lut(data_t *data, lut_t const *lut)
166 {
167 size_t size = lut ? sizeof(lut->data[0]) * lut->size : 0;
168 const void *ptr = lut ? lut->data : NULL;
169
170 igt_pipe_obj_replace_prop_blob(data->pipe, IGT_CRTC_GAMMA_LUT, ptr,
171 size);
172 }
173
174 /* Common test setup. */
test_init(data_t * data)175 static void test_init(data_t *data)
176 {
177 igt_display_t *display = &data->display;
178
179 /* It doesn't matter which pipe we choose on amdpgu. */
180 data->pipe_id = PIPE_A;
181 data->pipe = &data->display.pipes[data->pipe_id];
182
183 igt_display_reset(display);
184
185 data->output = igt_get_single_output_for_pipe(display, data->pipe_id);
186 igt_require(data->output);
187
188 data->mode = igt_output_get_mode(data->output);
189 igt_assert(data->mode);
190
191 data->primary =
192 igt_pipe_get_plane_type(data->pipe, DRM_PLANE_TYPE_PRIMARY);
193
194 data->pipe_crc = igt_pipe_crc_new(data->fd, data->pipe_id,
195 INTEL_PIPE_CRC_SOURCE_AUTO);
196
197 igt_output_set_pipe(data->output, data->pipe_id);
198
199 data->degamma_lut_size =
200 igt_pipe_obj_get_prop(data->pipe, IGT_CRTC_DEGAMMA_LUT_SIZE);
201 igt_assert_lt(0, data->degamma_lut_size);
202
203 data->regamma_lut_size =
204 igt_pipe_obj_get_prop(data->pipe, IGT_CRTC_GAMMA_LUT_SIZE);
205 igt_assert_lt(0, data->regamma_lut_size);
206
207 data->w = data->mode->hdisplay;
208 data->h = data->mode->vdisplay;
209 }
210
211 /* Common test cleanup. */
test_fini(data_t * data)212 static void test_fini(data_t *data)
213 {
214 igt_pipe_crc_free(data->pipe_crc);
215 igt_display_reset(&data->display);
216 }
217
218 /*
219 * Older versions of amdgpu would put the pipe into bypass mode for degamma
220 * when passed a linear sRGB matrix but would still use an sRGB regamma
221 * matrix if not passed any. The whole pipe should be in linear bypass mode
222 * when all the matrices are NULL - CRCs for a linear degamma matrix and
223 * a NULL one should match.
224 */
test_crtc_linear_degamma(data_t * data)225 static void test_crtc_linear_degamma(data_t *data)
226 {
227 igt_display_t *display = &data->display;
228 igt_crc_t ref_crc, new_crc;
229 igt_fb_t afb;
230 lut_t lut_linear;
231
232 test_init(data);
233
234 lut_init(&lut_linear, data->degamma_lut_size);
235 lut_gen_linear(&lut_linear, 0xffff);
236
237 igt_create_fb(data->fd, data->w, data->h, DRM_FORMAT_XRGB8888, 0, &afb);
238 draw_gamma_test(&afb);
239
240 /* Draw the reference image. */
241 igt_plane_set_fb(data->primary, &afb);
242 set_regamma_lut(data, NULL);
243 set_degamma_lut(data, NULL);
244 igt_display_commit_atomic(display, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
245
246 igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);
247
248 /* Apply a linear degamma. The result should remain the same. */
249 set_degamma_lut(data, &lut_linear);
250 igt_display_commit_atomic(display, 0, NULL);
251
252 igt_pipe_crc_collect_crc(data->pipe_crc, &new_crc);
253 igt_assert_crc_equal(&ref_crc, &new_crc);
254
255 test_fini(data);
256 igt_remove_fb(data->fd, &afb);
257 lut_free(&lut_linear);
258 }
259
260 /*
261 * Older versions of amdgpu would apply the CRTC regamma on top of a custom
262 * sRGB regamma matrix with incorrect calculations or rounding errors.
263 * If we put the pipe into bypass or use the hardware defined sRGB regamma
264 * on the plane then we can and should get the correct CRTC when passing a
265 * liner regamma matrix to DRM.
266 */
test_crtc_linear_regamma(data_t * data)267 static void test_crtc_linear_regamma(data_t *data)
268 {
269 igt_display_t *display = &data->display;
270 igt_crc_t ref_crc, new_crc;
271 igt_fb_t afb;
272 lut_t lut_linear;
273
274 test_init(data);
275
276 lut_init(&lut_linear, data->regamma_lut_size);
277 lut_gen_linear(&lut_linear, 0xffff);
278
279 igt_create_fb(data->fd, data->w, data->h, DRM_FORMAT_XRGB8888, 0, &afb);
280 draw_gamma_test(&afb);
281
282 /* Draw the reference image. */
283 igt_plane_set_fb(data->primary, &afb);
284 set_regamma_lut(data, NULL);
285 set_degamma_lut(data, NULL);
286 igt_display_commit_atomic(display, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
287
288 igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);
289
290 /* Apply a linear degamma. The result should remain the same. */
291 set_regamma_lut(data, &lut_linear);
292 igt_display_commit_atomic(display, 0, NULL);
293
294 igt_pipe_crc_collect_crc(data->pipe_crc, &new_crc);
295 igt_assert_crc_equal(&ref_crc, &new_crc);
296
297 test_fini(data);
298 igt_remove_fb(data->fd, &afb);
299 lut_free(&lut_linear);
300 }
301
302 /*
303 * Tests LUT accuracy. CRTC regamma and CRTC degamma should produce a visually
304 * correct image when used. Hardware limitations on degamma prevent this from
305 * being CRC level accurate across a full test gradient but most values should
306 * still match.
307 *
308 * This test can't pass on DCE because it doesn't support non-linear degamma.
309 */
test_crtc_lut_accuracy(data_t * data)310 static void test_crtc_lut_accuracy(data_t *data)
311 {
312 /*
313 * Channels are independent, so we can verify multiple colors at the
314 * same time for improved performance.
315 */
316 static const color_t colors[] = {
317 { 1.00, 1.00, 1.00 },
318 { 0.90, 0.85, 0.75 }, /* 0.95 fails */
319 { 0.70, 0.65, 0.60 },
320 { 0.55, 0.50, 0.45 },
321 { 0.40, 0.35, 0.30 },
322 { 0.25, 0.20, 0.15 },
323 { 0.10, 0.04, 0.02 }, /* 0.05 fails */
324 { 0.00, 0.00, 0.00 },
325 };
326 igt_display_t *display = &data->display;
327 igt_crc_t ref_crc, new_crc;
328 igt_fb_t afb;
329 lut_t lut_degamma, lut_regamma;
330 int i, w, h;
331
332 test_init(data);
333
334 lut_init(&lut_degamma, data->degamma_lut_size);
335 lut_gen_degamma_srgb(&lut_degamma, 0xffff);
336
337 lut_init(&lut_regamma, data->regamma_lut_size);
338 lut_gen_regamma_srgb(&lut_regamma, 0xffff);
339
340 /* Don't draw across the whole screen to improve perf. */
341 w = 64;
342 h = 64;
343
344 igt_create_fb(data->fd, w, h, DRM_FORMAT_XRGB8888, 0, &afb);
345 igt_plane_set_fb(data->primary, &afb);
346 igt_display_commit_atomic(display, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);
347
348 /* Test colors. */
349 for (i = 0; i < ARRAY_SIZE(colors); ++i) {
350 color_t col = colors[i];
351
352 igt_info("Testing color (%.2f, %.2f, %.2f) ...\n", col.r, col.g,
353 col.b);
354
355 draw_color(&afb, col.r, col.g, col.b);
356
357 set_regamma_lut(data, NULL);
358 set_degamma_lut(data, NULL);
359 igt_display_commit_atomic(display, 0, NULL);
360
361 igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);
362
363 set_degamma_lut(data, &lut_degamma);
364 set_regamma_lut(data, &lut_regamma);
365 igt_display_commit_atomic(display, 0, NULL);
366
367 igt_pipe_crc_collect_crc(data->pipe_crc, &new_crc);
368
369 igt_assert_crc_equal(&ref_crc, &new_crc);
370 }
371
372 test_fini(data);
373 igt_remove_fb(data->fd, &afb);
374 lut_free(&lut_regamma);
375 lut_free(&lut_degamma);
376 }
377
378 igt_main
379 {
380 data_t data;
381
382 igt_skip_on_simulation();
383
384 memset(&data, 0, sizeof(data));
385
386 igt_fixture
387 {
388 data.fd = drm_open_driver_master(DRIVER_AMDGPU);
389
390 kmstest_set_vt_graphics_mode();
391
392 igt_display_require(&data.display, data.fd);
393 igt_require(data.display.is_atomic);
394 igt_display_require_output(&data.display);
395 }
396
397 igt_subtest("crtc-linear-degamma") test_crtc_linear_degamma(&data);
398 igt_subtest("crtc-linear-regamma") test_crtc_linear_regamma(&data);
399 igt_subtest("crtc-lut-accuracy") test_crtc_lut_accuracy(&data);
400
401 igt_fixture
402 {
403 igt_display_fini(&data.display);
404 }
405 }
406