summaryrefslogtreecommitdiff
path: root/tests/amdgpu/amd_color.c
blob: 6d313bae74b526ca0a3b0754789b44bc86089aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright 2019 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "igt.h"

/* (De)gamma LUT. */
typedef struct lut {
	struct drm_color_lut *data;
	uint32_t size;
} lut_t;

/* RGB color. */
typedef struct color {
	double r;
	double g;
	double b;
} color_t;

/* Common test data. */
typedef struct data {
	igt_display_t display;
	igt_plane_t *primary;
	igt_output_t *output;
	igt_pipe_t *pipe;
	igt_pipe_crc_t *pipe_crc;
	drmModeModeInfo *mode;
	enum pipe pipe_id;
	int fd;
	int w;
	int h;
	uint32_t regamma_lut_size;
	uint32_t degamma_lut_size;
} data_t;

static void lut_init(lut_t *lut, uint32_t size)
{
	igt_assert(size > 0);

	lut->size = size;
	lut->data = malloc(size * sizeof(struct drm_color_lut));
	igt_assert(lut);
}

/* Generates the linear gamma LUT. */
static void lut_gen_linear(lut_t *lut, uint16_t mask)
{
	uint32_t n = lut->size - 1;
	uint32_t i;

	for (i = 0; i < lut->size; ++i) {
		uint32_t v = (i * 0xffff / n) & mask;

		lut->data[i].red = v;
		lut->data[i].blue = v;
		lut->data[i].green = v;
	}
}

/* Generates the sRGB degamma LUT. */
static void lut_gen_degamma_srgb(lut_t *lut, uint16_t mask)
{
	double range = lut->size - 1;
	uint32_t i;

	for (i = 0; i < lut->size; ++i) {
		double u, coeff;
		uint32_t v;

		u = i / range;
		coeff = u <= 0.040449936 ? (u / 12.92)
					 : (pow((u + 0.055) / 1.055, 2.4));
		v = (uint32_t)(coeff * 0xffff) & mask;

		lut->data[i].red = v;
		lut->data[i].blue = v;
		lut->data[i].green = v;
	}
}

/* Generates the sRGB gamma LUT. */
static void lut_gen_regamma_srgb(lut_t *lut, uint16_t mask)
{
	double range = lut->size - 1;
	uint32_t i;

	for (i = 0; i < lut->size; ++i) {
		double u, coeff;
		uint32_t v;

		u = i / range;
		coeff = u <= 0.00313080 ? (12.92 * u)
					: (1.055 * pow(u, 1.0 / 2.4) - 0.055);
		v = (uint32_t)(coeff * 0xffff) & mask;

		lut->data[i].red = v;
		lut->data[i].blue = v;
		lut->data[i].green = v;
	}
}

static void lut_free(lut_t *lut)
{
	if (lut->data) {
		free(lut->data);
		lut->data = NULL;
	}

	lut->size = 0;
}

/* Fills a FB with the solid color given. */
static void draw_color(igt_fb_t *fb, double r, double g, double b)
{
	cairo_t *cr = igt_get_cairo_ctx(fb->fd, fb);

	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	igt_paint_color(cr, 0, 0, fb->width, fb->height, r, g, b);
	igt_put_cairo_ctx(cr);
}

/* Generates the gamma test pattern. */
static void draw_gamma_test(igt_fb_t *fb)
{
	cairo_t *cr = igt_get_cairo_ctx(fb->fd, fb);
	int gh = fb->height / 4;

	igt_paint_color_gradient(cr, 0, 0, fb->width, gh, 1, 1, 1);
	igt_paint_color_gradient(cr, 0, gh, fb->width, gh, 1, 0, 0);
	igt_paint_color_gradient(cr, 0, gh * 2, fb->width, gh, 0, 1, 0);
	igt_paint_color_gradient(cr, 0, gh * 3, fb->width, gh, 0, 0, 1);

	igt_put_cairo_ctx(cr);
}

/* Sets the degamma LUT. */
static void set_degamma_lut(data_t *data, lut_t const *lut)
{
	size_t size = lut ? sizeof(lut->data[0]) * lut->size : 0;
	const void *ptr = lut ? lut->data : NULL;

	igt_pipe_obj_replace_prop_blob(data->pipe, IGT_CRTC_DEGAMMA_LUT, ptr,
				       size);
}

/* Sets the regamma LUT. */
static void set_regamma_lut(data_t *data, lut_t const *lut)
{
	size_t size = lut ? sizeof(lut->data[0]) * lut->size : 0;
	const void *ptr = lut ? lut->data : NULL;

	igt_pipe_obj_replace_prop_blob(data->pipe, IGT_CRTC_GAMMA_LUT, ptr,
				       size);
}

/* Common test setup. */
static void test_init(data_t *data)
{
	igt_display_t *display = &data->display;

	/* It doesn't matter which pipe we choose on amdpgu. */
	data->pipe_id = PIPE_A;
	data->pipe = &data->display.pipes[data->pipe_id];

	igt_display_reset(display);

	data->output = igt_get_single_output_for_pipe(display, data->pipe_id);
	igt_require(data->output);

	data->mode = igt_output_get_mode(data->output);
	igt_assert(data->mode);

	data->primary =
		igt_pipe_get_plane_type(data->pipe, DRM_PLANE_TYPE_PRIMARY);

	data->pipe_crc = igt_pipe_crc_new(data->fd, data->pipe_id,
					  INTEL_PIPE_CRC_SOURCE_AUTO);

	igt_output_set_pipe(data->output, data->pipe_id);

	data->degamma_lut_size =
		igt_pipe_obj_get_prop(data->pipe, IGT_CRTC_DEGAMMA_LUT_SIZE);
	igt_assert_lt(0, data->degamma_lut_size);

	data->regamma_lut_size =
		igt_pipe_obj_get_prop(data->pipe, IGT_CRTC_GAMMA_LUT_SIZE);
	igt_assert_lt(0, data->regamma_lut_size);

	data->w = data->mode->hdisplay;
	data->h = data->mode->vdisplay;
}

/* Common test cleanup. */
static void test_fini(data_t *data)
{
	igt_pipe_crc_free(data->pipe_crc);
	igt_display_reset(&data->display);
}

/*
 * Older versions of amdgpu would put the pipe into bypass mode for degamma
 * when passed a linear sRGB matrix but would still use an sRGB regamma
 * matrix if not passed any. The whole pipe should be in linear bypass mode
 * when all the matrices are NULL - CRCs for a linear degamma matrix and
 * a NULL one should match.
 */
static void test_crtc_linear_degamma(data_t *data)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, new_crc;
	igt_fb_t afb;
	lut_t lut_linear;

	test_init(data);

	lut_init(&lut_linear, data->degamma_lut_size);
	lut_gen_linear(&lut_linear, 0xffff);

	igt_create_fb(data->fd, data->w, data->h, DRM_FORMAT_XRGB8888, 0, &afb);
	draw_gamma_test(&afb);

	/* Draw the reference image. */
	igt_plane_set_fb(data->primary, &afb);
	set_regamma_lut(data, NULL);
	set_degamma_lut(data, NULL);
	igt_display_commit_atomic(display, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);

	igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);

	/* Apply a linear degamma. The result should remain the same. */
	set_degamma_lut(data, &lut_linear);
	igt_display_commit_atomic(display, 0, NULL);

	igt_pipe_crc_collect_crc(data->pipe_crc, &new_crc);
	igt_assert_crc_equal(&ref_crc, &new_crc);

	test_fini(data);
	igt_remove_fb(data->fd, &afb);
	lut_free(&lut_linear);
}

/*
 * Older versions of amdgpu would apply the CRTC regamma on top of a custom
 * sRGB regamma matrix with incorrect calculations or rounding errors.
 * If we put the pipe into bypass or use the hardware defined sRGB regamma
 * on the plane then we can and should get the correct CRTC when passing a
 * liner regamma matrix to DRM.
 */
static void test_crtc_linear_regamma(data_t *data)
{
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, new_crc;
	igt_fb_t afb;
	lut_t lut_linear;

	test_init(data);

	lut_init(&lut_linear, data->regamma_lut_size);
	lut_gen_linear(&lut_linear, 0xffff);

	igt_create_fb(data->fd, data->w, data->h, DRM_FORMAT_XRGB8888, 0, &afb);
	draw_gamma_test(&afb);

	/* Draw the reference image. */
	igt_plane_set_fb(data->primary, &afb);
	set_regamma_lut(data, NULL);
	set_degamma_lut(data, NULL);
	igt_display_commit_atomic(display, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);

	igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);

	/* Apply a linear degamma. The result should remain the same. */
	set_regamma_lut(data, &lut_linear);
	igt_display_commit_atomic(display, 0, NULL);

	igt_pipe_crc_collect_crc(data->pipe_crc, &new_crc);
	igt_assert_crc_equal(&ref_crc, &new_crc);

	test_fini(data);
	igt_remove_fb(data->fd, &afb);
	lut_free(&lut_linear);
}

/*
 * Tests LUT accuracy. CRTC regamma and CRTC degamma should produce a visually
 * correct image when used. Hardware limitations on degamma prevent this from
 * being CRC level accurate across a full test gradient but most values should
 * still match.
 *
 * This test can't pass on DCE because it doesn't support non-linear degamma.
 */
static void test_crtc_lut_accuracy(data_t *data)
{
	/*
	 * Channels are independent, so we can verify multiple colors at the
	 * same time for improved performance.
	 */
	static const color_t colors[] = {
		{ 1.00, 1.00, 1.00 },
		{ 0.90, 0.85, 0.75 }, /* 0.95 fails */
		{ 0.70, 0.65, 0.60 },
		{ 0.55, 0.50, 0.45 },
		{ 0.40, 0.35, 0.30 },
		{ 0.25, 0.20, 0.15 },
		{ 0.10, 0.04, 0.02 }, /* 0.05 fails */
		{ 0.00, 0.00, 0.00 },
	};
	igt_display_t *display = &data->display;
	igt_crc_t ref_crc, new_crc;
	igt_fb_t afb;
	lut_t lut_degamma, lut_regamma;
	int i, w, h;

	test_init(data);

	lut_init(&lut_degamma, data->degamma_lut_size);
	lut_gen_degamma_srgb(&lut_degamma, 0xffff);

	lut_init(&lut_regamma, data->regamma_lut_size);
	lut_gen_regamma_srgb(&lut_regamma, 0xffff);

	/* Don't draw across the whole screen to improve perf. */
	w = 64;
	h = 64;

	igt_create_fb(data->fd, w, h, DRM_FORMAT_XRGB8888, 0, &afb);
	igt_plane_set_fb(data->primary, &afb);
	igt_display_commit_atomic(display, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL);

	/* Test colors. */
	for (i = 0; i < ARRAY_SIZE(colors); ++i) {
		color_t col = colors[i];

		igt_info("Testing color (%.2f, %.2f, %.2f) ...\n", col.r, col.g,
			 col.b);

		draw_color(&afb, col.r, col.g, col.b);

		set_regamma_lut(data, NULL);
		set_degamma_lut(data, NULL);
		igt_display_commit_atomic(display, 0, NULL);

		igt_pipe_crc_collect_crc(data->pipe_crc, &ref_crc);

		set_degamma_lut(data, &lut_degamma);
		set_regamma_lut(data, &lut_regamma);
		igt_display_commit_atomic(display, 0, NULL);

		igt_pipe_crc_collect_crc(data->pipe_crc, &new_crc);

		igt_assert_crc_equal(&ref_crc, &new_crc);
	}

	test_fini(data);
	igt_remove_fb(data->fd, &afb);
	lut_free(&lut_regamma);
	lut_free(&lut_degamma);
}

igt_main
{
	data_t data;

	igt_skip_on_simulation();

	memset(&data, 0, sizeof(data));

	igt_fixture
	{
		data.fd = drm_open_driver_master(DRIVER_AMDGPU);

		kmstest_set_vt_graphics_mode();

		igt_display_require(&data.display, data.fd);
		igt_require(data.display.is_atomic);
		igt_display_require_output(&data.display);
	}

	igt_subtest("crtc-linear-degamma") test_crtc_linear_degamma(&data);
	igt_subtest("crtc-linear-regamma") test_crtc_linear_regamma(&data);
	igt_subtest("crtc-lut-accuracy") test_crtc_lut_accuracy(&data);

	igt_fixture
	{
		igt_display_fini(&data.display);
	}
}