summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/drm_colorop.c
blob: 44eb823585d2e88b1e606c3b435ba30a88d4cfcb (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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// SPDX-License-Identifier: MIT
/*
 * Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved.
 *
 * 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.
 *
 * Authors: AMD
 *
 */

#include <drm/drm_colorop.h>
#include <drm/drm_print.h>
#include <drm/drm_drv.h>
#include <drm/drm_plane.h>

#include "drm_crtc_internal.h"

/**
 * DOC: overview
 *
 * When userspace signals the &DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE it
 * should use the COLOR_PIPELINE plane property and associated colorops
 * for any color operation on the &drm_plane. Setting of all old color
 * properties, such as COLOR_ENCODING and COLOR_RANGE, will be rejected
 * and the values of the properties will be ignored.
 *
 * Colorops are only advertised and valid for atomic drivers and atomic
 * userspace that signals the &DRM_CLIENT_CAP_PLANE_COLOR_PIPELINE
 * client cap.
 *
 * A colorop represents a single color operation. Colorops are chained
 * via the NEXT property and make up color pipelines. Color pipelines
 * are advertised and selected via the COLOR_PIPELINE &drm_plane
 * property.
 *
 * A colorop will be of a certain type, advertised by the read-only TYPE
 * property. Each type of colorop will advertise a different set of
 * properties and is programmed in a different manner. Types can be
 * enumerated 1D curves, 1D LUTs, 3D LUTs, matrices, etc. See the
 * &drm_colorop_type documentation for information on each type.
 *
 * If a colorop advertises the BYPASS property it can be bypassed.
 *
 * Information about colorop and color pipeline design decisions can be
 * found at rfc/color_pipeline.rst, but note that this document will
 * grow stale over time.
 */

static const struct drm_prop_enum_list drm_colorop_type_enum_list[] = {
	{ DRM_COLOROP_1D_CURVE, "1D Curve" },
	{ DRM_COLOROP_1D_LUT, "1D LUT" },
	{ DRM_COLOROP_CTM_3X4, "3x4 Matrix"},
	{ DRM_COLOROP_MULTIPLIER, "Multiplier"},
	{ DRM_COLOROP_3D_LUT, "3D LUT"},
};

static const char * const colorop_curve_1d_type_names[] = {
	[DRM_COLOROP_1D_CURVE_SRGB_EOTF] = "sRGB EOTF",
	[DRM_COLOROP_1D_CURVE_SRGB_INV_EOTF] = "sRGB Inverse EOTF",
	[DRM_COLOROP_1D_CURVE_PQ_125_EOTF] = "PQ 125 EOTF",
	[DRM_COLOROP_1D_CURVE_PQ_125_INV_EOTF] = "PQ 125 Inverse EOTF",
	[DRM_COLOROP_1D_CURVE_BT2020_INV_OETF] = "BT.2020 Inverse OETF",
	[DRM_COLOROP_1D_CURVE_BT2020_OETF] = "BT.2020 OETF",
	[DRM_COLOROP_1D_CURVE_GAMMA22] = "Gamma 2.2",
	[DRM_COLOROP_1D_CURVE_GAMMA22_INV] = "Gamma 2.2 Inverse",
};

static const struct drm_prop_enum_list drm_colorop_lut1d_interpolation_list[] = {
	{ DRM_COLOROP_LUT1D_INTERPOLATION_LINEAR, "Linear" },
};


static const struct drm_prop_enum_list drm_colorop_lut3d_interpolation_list[] = {
	{ DRM_COLOROP_LUT3D_INTERPOLATION_TETRAHEDRAL, "Tetrahedral" },
};

/* Init Helpers */

static int drm_plane_colorop_init(struct drm_device *dev, struct drm_colorop *colorop,
				  struct drm_plane *plane, enum drm_colorop_type type,
				  uint32_t flags)
{
	struct drm_mode_config *config = &dev->mode_config;
	struct drm_property *prop;
	int ret = 0;

	ret = drm_mode_object_add(dev, &colorop->base, DRM_MODE_OBJECT_COLOROP);
	if (ret)
		return ret;

	colorop->base.properties = &colorop->properties;
	colorop->dev = dev;
	colorop->type = type;
	colorop->plane = plane;
	colorop->next = NULL;

	list_add_tail(&colorop->head, &config->colorop_list);
	colorop->index = config->num_colorop++;

	/* add properties */

	/* type */
	prop = drm_property_create_enum(dev,
					DRM_MODE_PROP_IMMUTABLE,
					"TYPE", drm_colorop_type_enum_list,
					ARRAY_SIZE(drm_colorop_type_enum_list));

	if (!prop)
		return -ENOMEM;

	colorop->type_property = prop;

	drm_object_attach_property(&colorop->base,
				   colorop->type_property,
				   colorop->type);

	if (flags & DRM_COLOROP_FLAG_ALLOW_BYPASS) {
		/* bypass */
		prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
						"BYPASS");
		if (!prop)
			return -ENOMEM;

		colorop->bypass_property = prop;
		drm_object_attach_property(&colorop->base,
					colorop->bypass_property,
					1);
	}

	/* next */
	prop = drm_property_create_object(dev, DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_ATOMIC,
					  "NEXT", DRM_MODE_OBJECT_COLOROP);
	if (!prop)
		return -ENOMEM;
	colorop->next_property = prop;
	drm_object_attach_property(&colorop->base,
				   colorop->next_property,
				   0);

	return ret;
}

/**
 * drm_colorop_cleanup - Cleanup a drm_colorop object in color_pipeline
 *
 * @colorop: The drm_colorop object to be cleaned
 */
void drm_colorop_cleanup(struct drm_colorop *colorop)
{
	struct drm_device *dev = colorop->dev;
	struct drm_mode_config *config = &dev->mode_config;

	list_del(&colorop->head);
	config->num_colorop--;

	if (colorop->state && colorop->state->data) {
		drm_property_blob_put(colorop->state->data);
		colorop->state->data = NULL;
	}

	kfree(colorop->state);
}
EXPORT_SYMBOL(drm_colorop_cleanup);

/**
 * drm_colorop_pipeline_destroy - Helper for color pipeline destruction
 *
 * @dev: - The drm_device containing the drm_planes with the color_pipelines
 *
 * Provides a default color pipeline destroy handler for drm_device.
 */
void drm_colorop_pipeline_destroy(struct drm_device *dev)
{
	struct drm_mode_config *config = &dev->mode_config;
	struct drm_colorop *colorop, *next;

	list_for_each_entry_safe(colorop, next, &config->colorop_list, head) {
		drm_colorop_cleanup(colorop);
		kfree(colorop);
	}
}
EXPORT_SYMBOL(drm_colorop_pipeline_destroy);

/**
 * drm_plane_colorop_curve_1d_init - Initialize a DRM_COLOROP_1D_CURVE
 *
 * @dev: DRM device
 * @colorop: The drm_colorop object to initialize
 * @plane: The associated drm_plane
 * @supported_tfs: A bitfield of supported drm_plane_colorop_curve_1d_init enum values,
 *                 created using BIT(curve_type) and combined with the OR '|'
 *                 operator.
 * @flags: bitmask of misc, see DRM_COLOROP_FLAG_* defines.
 * @return zero on success, -E value on failure
 */
int drm_plane_colorop_curve_1d_init(struct drm_device *dev, struct drm_colorop *colorop,
				    struct drm_plane *plane, u64 supported_tfs, uint32_t flags)
{
	struct drm_prop_enum_list enum_list[DRM_COLOROP_1D_CURVE_COUNT];
	int i, len;

	struct drm_property *prop;
	int ret;

	if (!supported_tfs) {
		drm_err(dev,
			"No supported TFs for new 1D curve colorop on [PLANE:%d:%s]\n",
			plane->base.id, plane->name);
		return -EINVAL;
	}

	if ((supported_tfs & -BIT(DRM_COLOROP_1D_CURVE_COUNT)) != 0) {
		drm_err(dev, "Unknown TF provided on [PLANE:%d:%s]\n",
			plane->base.id, plane->name);
		return -EINVAL;
	}

	ret = drm_plane_colorop_init(dev, colorop, plane, DRM_COLOROP_1D_CURVE, flags);
	if (ret)
		return ret;

	len = 0;
	for (i = 0; i < DRM_COLOROP_1D_CURVE_COUNT; i++) {
		if ((supported_tfs & BIT(i)) == 0)
			continue;

		enum_list[len].type = i;
		enum_list[len].name = colorop_curve_1d_type_names[i];
		len++;
	}

	if (WARN_ON(len <= 0))
		return -EINVAL;

	/* initialize 1D curve only attribute */
	prop = drm_property_create_enum(dev, DRM_MODE_PROP_ATOMIC, "CURVE_1D_TYPE",
					enum_list, len);

	if (!prop)
		return -ENOMEM;

	colorop->curve_1d_type_property = prop;
	drm_object_attach_property(&colorop->base, colorop->curve_1d_type_property,
				   enum_list[0].type);
	drm_colorop_reset(colorop);

	return 0;
}
EXPORT_SYMBOL(drm_plane_colorop_curve_1d_init);

static int drm_colorop_create_data_prop(struct drm_device *dev, struct drm_colorop *colorop)
{
	struct drm_property *prop;

	/* data */
	prop = drm_property_create(dev, DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
				   "DATA", 0);
	if (!prop)
		return -ENOMEM;

	colorop->data_property = prop;
	drm_object_attach_property(&colorop->base,
				   colorop->data_property,
				   0);

	return 0;
}

/**
 * drm_plane_colorop_curve_1d_lut_init - Initialize a DRM_COLOROP_1D_LUT
 *
 * @dev: DRM device
 * @colorop: The drm_colorop object to initialize
 * @plane: The associated drm_plane
 * @lut_size: LUT size supported by driver
 * @interpolation: 1D LUT interpolation type
 * @flags: bitmask of misc, see DRM_COLOROP_FLAG_* defines.
 * @return zero on success, -E value on failure
 */
int drm_plane_colorop_curve_1d_lut_init(struct drm_device *dev, struct drm_colorop *colorop,
					struct drm_plane *plane, uint32_t lut_size,
					enum drm_colorop_lut1d_interpolation_type interpolation,
					uint32_t flags)
{
	struct drm_property *prop;
	int ret;

	ret = drm_plane_colorop_init(dev, colorop, plane, DRM_COLOROP_1D_LUT, flags);
	if (ret)
		return ret;

	/* initialize 1D LUT only attribute */
	/* LUT size */
	prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE | DRM_MODE_PROP_ATOMIC,
					 "SIZE", 0, UINT_MAX);
	if (!prop)
		return -ENOMEM;

	colorop->size_property = prop;
	drm_object_attach_property(&colorop->base, colorop->size_property, lut_size);
	colorop->size = lut_size;

	/* interpolation */
	prop = drm_property_create_enum(dev, 0, "LUT1D_INTERPOLATION",
					drm_colorop_lut1d_interpolation_list,
					ARRAY_SIZE(drm_colorop_lut1d_interpolation_list));
	if (!prop)
		return -ENOMEM;

	colorop->lut1d_interpolation_property = prop;
	drm_object_attach_property(&colorop->base, prop, interpolation);
	colorop->lut1d_interpolation = interpolation;

	/* data */
	ret = drm_colorop_create_data_prop(dev, colorop);
	if (ret)
		return ret;

	drm_colorop_reset(colorop);

	return 0;
}
EXPORT_SYMBOL(drm_plane_colorop_curve_1d_lut_init);

int drm_plane_colorop_ctm_3x4_init(struct drm_device *dev, struct drm_colorop *colorop,
				   struct drm_plane *plane, uint32_t flags)
{
	int ret;

	ret = drm_plane_colorop_init(dev, colorop, plane, DRM_COLOROP_CTM_3X4, flags);
	if (ret)
		return ret;

	ret = drm_colorop_create_data_prop(dev, colorop);
	if (ret)
		return ret;

	drm_colorop_reset(colorop);

	return 0;
}
EXPORT_SYMBOL(drm_plane_colorop_ctm_3x4_init);

/**
 * drm_plane_colorop_mult_init - Initialize a DRM_COLOROP_MULTIPLIER
 *
 * @dev: DRM device
 * @colorop: The drm_colorop object to initialize
 * @plane: The associated drm_plane
 * @flags: bitmask of misc, see DRM_COLOROP_FLAG_* defines.
 * @return zero on success, -E value on failure
 */
int drm_plane_colorop_mult_init(struct drm_device *dev, struct drm_colorop *colorop,
				struct drm_plane *plane, uint32_t flags)
{
	struct drm_property *prop;
	int ret;

	ret = drm_plane_colorop_init(dev, colorop, plane, DRM_COLOROP_MULTIPLIER, flags);
	if (ret)
		return ret;

	prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC, "MULTIPLIER", 0, U64_MAX);
	if (!prop)
		return -ENOMEM;

	colorop->multiplier_property = prop;
	drm_object_attach_property(&colorop->base, colorop->multiplier_property, 0);

	drm_colorop_reset(colorop);

	return 0;
}
EXPORT_SYMBOL(drm_plane_colorop_mult_init);

int drm_plane_colorop_3dlut_init(struct drm_device *dev, struct drm_colorop *colorop,
				 struct drm_plane *plane,
				 uint32_t lut_size,
				 enum drm_colorop_lut3d_interpolation_type interpolation,
				 uint32_t flags)
{
	struct drm_property *prop;
	int ret;

	ret = drm_plane_colorop_init(dev, colorop, plane, DRM_COLOROP_3D_LUT, flags);
	if (ret)
		return ret;

	/* LUT size */
	prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE  | DRM_MODE_PROP_ATOMIC,
					 "SIZE", 0, UINT_MAX);
	if (!prop)
		return -ENOMEM;

	colorop->size_property = prop;
	drm_object_attach_property(&colorop->base, colorop->size_property, lut_size);
	colorop->size = lut_size;

	/* interpolation */
	prop = drm_property_create_enum(dev, 0, "LUT3D_INTERPOLATION",
					drm_colorop_lut3d_interpolation_list,
					ARRAY_SIZE(drm_colorop_lut3d_interpolation_list));
	if (!prop)
		return -ENOMEM;

	colorop->lut3d_interpolation_property = prop;
	drm_object_attach_property(&colorop->base, prop, interpolation);
	colorop->lut3d_interpolation = interpolation;

	/* data */
	ret = drm_colorop_create_data_prop(dev, colorop);
	if (ret)
		return ret;

	drm_colorop_reset(colorop);

	return 0;
}
EXPORT_SYMBOL(drm_plane_colorop_3dlut_init);

static void __drm_atomic_helper_colorop_duplicate_state(struct drm_colorop *colorop,
							struct drm_colorop_state *state)
{
	memcpy(state, colorop->state, sizeof(*state));

	if (state->data)
		drm_property_blob_get(state->data);

	state->bypass = true;
}

struct drm_colorop_state *
drm_atomic_helper_colorop_duplicate_state(struct drm_colorop *colorop)
{
	struct drm_colorop_state *state;

	if (WARN_ON(!colorop->state))
		return NULL;

	state = kmalloc(sizeof(*state), GFP_KERNEL);
	if (state)
		__drm_atomic_helper_colorop_duplicate_state(colorop, state);

	return state;
}

void drm_colorop_atomic_destroy_state(struct drm_colorop *colorop,
				      struct drm_colorop_state *state)
{
	kfree(state);
}

/**
 * __drm_colorop_state_reset - resets colorop state to default values
 * @colorop_state: atomic colorop state, must not be NULL
 * @colorop: colorop object, must not be NULL
 *
 * Initializes the newly allocated @colorop_state with default
 * values. This is useful for drivers that subclass the CRTC state.
 */
static void __drm_colorop_state_reset(struct drm_colorop_state *colorop_state,
				      struct drm_colorop *colorop)
{
	u64 val;

	colorop_state->colorop = colorop;
	colorop_state->bypass = true;

	if (colorop->curve_1d_type_property) {
		drm_object_property_get_default_value(&colorop->base,
						      colorop->curve_1d_type_property,
						      &val);
		colorop_state->curve_1d_type = val;
	}
}

/**
 * __drm_colorop_reset - reset state on colorop
 * @colorop: drm colorop
 * @colorop_state: colorop state to assign
 *
 * Initializes the newly allocated @colorop_state and assigns it to
 * the &drm_crtc->state pointer of @colorop, usually required when
 * initializing the drivers or when called from the &drm_colorop_funcs.reset
 * hook.
 *
 * This is useful for drivers that subclass the colorop state.
 */
static void __drm_colorop_reset(struct drm_colorop *colorop,
				struct drm_colorop_state *colorop_state)
{
	if (colorop_state)
		__drm_colorop_state_reset(colorop_state, colorop);

	colorop->state = colorop_state;
}

void drm_colorop_reset(struct drm_colorop *colorop)
{
	kfree(colorop->state);
	colorop->state = kzalloc(sizeof(*colorop->state), GFP_KERNEL);

	if (colorop->state)
		__drm_colorop_reset(colorop, colorop->state);
}

static const char * const colorop_type_name[] = {
	[DRM_COLOROP_1D_CURVE] = "1D Curve",
	[DRM_COLOROP_1D_LUT] = "1D LUT",
	[DRM_COLOROP_CTM_3X4] = "3x4 Matrix",
	[DRM_COLOROP_MULTIPLIER] = "Multiplier",
	[DRM_COLOROP_3D_LUT] = "3D LUT",
};

static const char * const colorop_lu3d_interpolation_name[] = {
	[DRM_COLOROP_LUT3D_INTERPOLATION_TETRAHEDRAL] = "Tetrahedral",
};

static const char * const colorop_lut1d_interpolation_name[] = {
	[DRM_COLOROP_LUT1D_INTERPOLATION_LINEAR] = "Linear",
};

const char *drm_get_colorop_type_name(enum drm_colorop_type type)
{
	if (WARN_ON(type >= ARRAY_SIZE(colorop_type_name)))
		return "unknown";

	return colorop_type_name[type];
}

const char *drm_get_colorop_curve_1d_type_name(enum drm_colorop_curve_1d_type type)
{
	if (WARN_ON(type >= ARRAY_SIZE(colorop_curve_1d_type_names)))
		return "unknown";

	return colorop_curve_1d_type_names[type];
}

/**
 * drm_get_colorop_lut1d_interpolation_name: return a string for interpolation type
 * @type: interpolation type to compute name of
 *
 * In contrast to the other drm_get_*_name functions this one here returns a
 * const pointer and hence is threadsafe.
 */
const char *drm_get_colorop_lut1d_interpolation_name(enum drm_colorop_lut1d_interpolation_type type)
{
	if (WARN_ON(type >= ARRAY_SIZE(colorop_lut1d_interpolation_name)))
		return "unknown";

	return colorop_lut1d_interpolation_name[type];
}

/**
 * drm_get_colorop_lut3d_interpolation_name - return a string for interpolation type
 * @type: interpolation type to compute name of
 *
 * In contrast to the other drm_get_*_name functions this one here returns a
 * const pointer and hence is threadsafe.
 */
const char *drm_get_colorop_lut3d_interpolation_name(enum drm_colorop_lut3d_interpolation_type type)
{
	if (WARN_ON(type >= ARRAY_SIZE(colorop_lu3d_interpolation_name)))
		return "unknown";

	return colorop_lu3d_interpolation_name[type];
}

/**
 * drm_colorop_set_next_property - sets the next pointer
 * @colorop: drm colorop
 * @next: next colorop
 *
 * Should be used when constructing the color pipeline
 */
void drm_colorop_set_next_property(struct drm_colorop *colorop, struct drm_colorop *next)
{
	drm_object_property_set_value(&colorop->base,
				      colorop->next_property,
				      next ? next->base.id : 0);
	colorop->next = next;
}
EXPORT_SYMBOL(drm_colorop_set_next_property);