Skip to contentSkip to conversation

drm/vkms: complete the plane color pipeline

PATCH New Oarora Etimis 1 participant 8 messages Nov 28

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover drm/vkms: complete the plane color pipeline

The vkms plane pipeline has a fixed matrix, two 1D curves and two 3x4 matrices. Userspace cannot supply its own LUT, do a 3D lookup, or apply a gain to the channels.

Patches 1 to 3 fix three bugs in the pipeline plumbing. Patches 4 to 6 add a 1D LUT, a 3D LUT and a multiplier. Patch 7 adds KUnit coverage for the 3D LUT and the multiplier; the 1D LUT gets its coverage in patch 4.

The color math has KUnit coverage; there is no IGT test for the color pipeline yet, so the uAPI paths are not exercised end-to-end.

The three new colorops are appended after the existing ones; their order is unchanged.

---
Changes in v2:
- Use 64-bit division helpers in the LUT code for 32-bit builds.
- Compute the 3D LUT cell once per pixel.
- Record the colorop states at atomic_check(), before swap_state().
- Only clean up colorops that were actually linked.
- Link to v1: https://patch.msgid.link/[email protected]

---
Oarora Etimis (7):
drm/vkms: add the colorops of a plane that is being enabled drm/vkms: free the colorop whose initialization failed drm/vkms: use the colorop states of the state being composed drm/vkms: add a programmable 1D LUT colorop
drm/vkms: add a 3D LUT colorop
drm/vkms: add a multiplier colorop
drm/vkms: add KUnit coverage for the 3D LUT and multiplier colorops

 drivers/gpu/drm/vkms/tests/vkms_color_test.c | 194 ++++++++++++++++
 drivers/gpu/drm/vkms/vkms_colorop.c          |  73 +++++-
 drivers/gpu/drm/vkms/vkms_composer.c         | 327 +++++++++++++++++++++++----
 drivers/gpu/drm/vkms/vkms_composer.h         |   5 +
 drivers/gpu/drm/vkms/vkms_crtc.c             |  60 ++++-
 drivers/gpu/drm/vkms/vkms_drv.h              |  61 +++++
 drivers/gpu/drm/vkms/vkms_plane.c            |   2 +
 7 files changed, 669 insertions(+), 53 deletions(-)

---
base-commit: 5ce32a0c94a827ff44ee60898c28060714dcff75
change-id: 20261113-colorop-693f016a156d

Best regards,

Signature · 1 line
Oarora Etimis <[email protected]>
#1 lore raw

PATCH v2 1/7 drm/vkms: add the colorops of a plane that is being enabled

drm_atomic_add_affected_planes() walks the plane mask of the old CRTC state, so a plane that is being enabled is not in that mask and its colorops are left out of the commit. Its first frame then sees the pipeline as bypassed, and a YUV plane is composed without the YUV-to-RGB conversion.

Add them explicitly in vkms_crtc_atomic_check().

Fixes 589179b18224 ("drm/vkms: Add atomic check and matrix handling for fixed matrix colorop")
---
 drivers/gpu/drm/vkms/vkms_crtc.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/gpu/drm/vkms/vkms_crtc.c +16 -0
index 019d7bc90cdc..7d18178d5320 100644
@@ -129,6 +129,22 @@ static int vkms_crtc_atomic_check(struct drm_crtc *crtc,
 	if (ret < 0)
 		return ret;
 
+	/*
+	 * drm_atomic_add_affected_planes() only walks the plane mask of the
+	 * old CRTC state. A plane that is being enabled is not in that mask,
+	 * so its colorops would be missing from this commit.
+	 */
+	drm_for_each_plane_mask(plane, crtc->dev, crtc_state->plane_mask) {
+		plane_state = drm_atomic_get_new_plane_state(crtc_state->state, plane);
+
+		if (!plane_state || !plane_state->color_pipeline)
+			continue;
+
+		ret = drm_atomic_add_affected_colorops(crtc_state->state, plane);
+		if (ret < 0)
+			return ret;
+	}
+
 	drm_for_each_plane_mask(plane, crtc->dev, crtc_state->plane_mask) {
 		plane_state = drm_atomic_get_new_plane_state(crtc_state->state, plane);
 		WARN_ON(!plane_state);
Signature · 1 line
2.56.1
#2 lore raw

PATCH v2 2/7 drm/vkms: free the colorop whose initialization failed

In reply to Oarora Etimis

The error path of vkms_initialize_color_pipeline() walks ops[0..i-1], but ops[i] is already allocated when its drm_plane_colorop_*_init() call fails, so it leaks.

Walk ops[0..i] instead, and call drm_colorop_cleanup() only on the entries that were linked, since init can fail before that.

Fixes c1e578bd08da ("drm/vkms: Add enumerated 1D curve colorop")
---
 drivers/gpu/drm/vkms/vkms_colorop.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/gpu/drm/vkms/vkms_colorop.c +10 -4
index 9424b1994239..4e6a82193f2e 100644
@@ -126,11 +126,17 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 	return 0;
 
 cleanup:
-	for (j = 0; j < i; j++) {
-		if (ops[j]) {
+	for (j = 0; j <= i && j < ARRAY_SIZE(ops); j++) {
+		if (!ops[j])
+			continue;
+
+		/*
+		 * drm_plane_colorop_init() can fail before the colorop is
+		 * linked and has a device; there is nothing to undo then.
+		 */
+		if (ops[j]->dev)
 			drm_colorop_cleanup(ops[j]);
-			kfree(ops[j]);
-		}
+		kfree(ops[j]);
 	}
 
 	return ret;
Signature · 1 line
2.56.1
#3 lore raw

PATCH v2 3/7 drm/vkms: use the colorop states of the state being composed

In reply to Oarora Etimis

The composer runs asynchronously and reads drm_colorop::state, which a later commit can replace and free. If that commit writes a value the colorop already has, core leaves the plane and its CRTC out, so nothing flushes the composer first and the worker is left holding a pointer to the freed state.

Record the values the composer needs in vkms_plane_state at atomic_check(), before the new colorop states are published, together with a reference of the LUT blob, and read only that copy.

Fixes c1e578bd08da ("drm/vkms: Add enumerated 1D curve colorop")
---
 drivers/gpu/drm/vkms/vkms_colorop.c  |  4 +-
 drivers/gpu/drm/vkms/vkms_composer.c | 92 +++++++++++++++++++-----------------
 drivers/gpu/drm/vkms/vkms_crtc.c     | 44 ++++++++++++++++-
 drivers/gpu/drm/vkms/vkms_drv.h      | 56 ++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_plane.c    |  2 +
 5 files changed, 150 insertions(+), 48 deletions(-)
drivers/gpu/drm/vkms/vkms_colorop.c +1 -3
index 4e6a82193f2e..2c4188f80e9b 100644
@@ -24,11 +24,9 @@ static const struct drm_colorop_funcs vkms_colorop_funcs = {
 	.destroy = drm_colorop_destroy,
 };
 
-#define MAX_COLOR_PIPELINE_OPS 5
-
 static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_prop_enum_list *list)
 {
-	struct drm_colorop *ops[MAX_COLOR_PIPELINE_OPS];
+	struct drm_colorop *ops[VKMS_MAX_COLOROPS];
 	struct drm_device *dev = plane->dev;
 	int ret;
 	int i = 0, j = 0;
drivers/gpu/drm/vkms/vkms_composer.c +48 -44

113 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_crtc.c +43 -1

74 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_drv.h +56 -0

88 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_plane.c +2 -0
index fd3d34dcf0fe..b05475b0f5d4 100644
@@ -79,6 +79,8 @@ static void vkms_plane_destroy_state(struct drm_plane *plane,
 			drm_framebuffer_put(vkms_state->frame_info.fb);
 	}
 
+	vkms_plane_colorops_release(vkms_state);
+
 	__drm_gem_destroy_shadow_plane_state(&vkms_state->base);
 	kfree(vkms_state);
 }
Signature · 1 line
2.56.1
#4 lore raw

PATCH v2 4/7 drm/vkms: add a programmable 1D LUT colorop

In reply to Oarora Etimis

The two curve stages apply a curve built into the driver, so userspace can pick one but cannot supply a table.

Add a 1D LUT colorop with 256 entries and linear interpolation, and apply it in the composer from the blob it carries. The entries are U0.32; the composer keeps their upper 16 bits.

Add KUnit coverage for the interpolation, including a table that decreases.

The existing colorops and their order are unchanged.

---
 drivers/gpu/drm/vkms/tests/vkms_color_test.c | 89 ++++++++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_colorop.c          | 19 ++++++
 drivers/gpu/drm/vkms/vkms_composer.c         | 79 ++++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_composer.h         |  2 +
 drivers/gpu/drm/vkms/vkms_drv.h              |  3 +
 5 files changed, 192 insertions(+)
drivers/gpu/drm/vkms/tests/vkms_color_test.c +89 -0
index c3c61037cb71..0d68de6462d9 100644
@@ -120,6 +120,91 @@ static void vkms_color_test_linear(struct kunit *test)
 	}
 }
 
+/*
+ * A two-entry LUT that maps 0 to 0 and 1.0 to 1.0 is the identity, so every
+ * input has to come back unchanged, including the ones between the entries.
+ */
+static const struct drm_color_lut32 test_lut32_identity[] = {
+	{ 0x00000000, 0x00000000, 0x00000000, 0 },
+	{ 0xffffffff, 0xffffffff, 0xffffffff, 0 },
+};
+
+static void vkms_color_test_lut32_identity(struct kunit *test)
+{
+	static const u16 inputs[] = { 0x0000, 0x4000, 0x8000, 0xc000, 0xffff };
+
+	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++)
+		KUNIT_EXPECT_EQ(test, inputs[i],
+				apply_lut32_to_channel_value(test_lut32_identity,
+							     ARRAY_SIZE(test_lut32_identity),
+							     inputs[i], LUT_RED));
+}
+
+/*
+ * Three entries at 0, 0.5 and 1.0. The values tested here fall exactly on
+ * the entries or halfway between two of them, so the expected output is
+ * exact.
+ */
+static const struct drm_color_lut32 test_lut32_three_points[] = {
+	{ 0x00000000, 0x00000000, 0x00000000, 0 },
+	{ 0x80000000, 0x80000000, 0x80000000, 0 },
+	{ 0xffffffff, 0xffffffff, 0xffffffff, 0 },
+};
+
+static void vkms_color_test_lut32_three_points(struct kunit *test)
+{
+	static const struct {
+		u16 input;
+		u16 expected;
+	} cases[] = {
+		{ 0x0000, 0x0000 },
+		{ 0x4000, 0x4000 },
+		{ 0x8000, 0x8000 },
+		{ 0xc000, 0xc000 },
+		{ 0xffff, 0xffff },
+	};
+
+	for (size_t i = 0; i < ARRAY_SIZE(cases); i++)
+		KUNIT_EXPECT_EQ(test, cases[i].expected,
+				apply_lut32_to_channel_value(test_lut32_three_points,
+							     ARRAY_SIZE(test_lut32_three_points),
+							     cases[i].input, LUT_RED));
+}
+
+/* A single-entry LUT returns that entry for every input. */
+static const struct drm_color_lut32 test_lut32_single[] = {
+	{ 0x12345678, 0x12345678, 0x12345678, 0 },
+};
+
+/* A decreasing table must round the same way an increasing one does. */
+static const struct drm_color_lut32 test_lut32_descending[] = {
+	{ 0xffffffff, 0xffffffff, 0xffffffff, 0 },
+	{ 0x00000000, 0x00000000, 0x00000000, 0 },
+};
+
+static void vkms_color_test_lut32_descending(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 0xffff,
+			apply_lut32_to_channel_value(test_lut32_descending, 2,
+						     0x0000, LUT_RED));
+	KUNIT_EXPECT_EQ(test, 0x7fff,
+			apply_lut32_to_channel_value(test_lut32_descending, 2,
+						     0x8000, LUT_RED));
+	KUNIT_EXPECT_EQ(test, 0x0000,
+			apply_lut32_to_channel_value(test_lut32_descending, 2,
+						     0xffff, LUT_RED));
+}
+
+static void vkms_color_test_lut32_single_entry(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 0x1234,
+			apply_lut32_to_channel_value(test_lut32_single, 1,
+						     0x0000, LUT_RED));
+	KUNIT_EXPECT_EQ(test, 0x1234,
+			apply_lut32_to_channel_value(test_lut32_single, 1,
+						     0xffff, LUT_RED));
+}
+
 static void vkms_color_srgb_inv_srgb(struct kunit *test)
 {
 	u16 srgb, final;
@@ -385,6 +470,10 @@ static struct kunit_case vkms_color_test_cases[] = {
 	KUNIT_CASE(vkms_color_test_get_uniform_lut_index),
 	KUNIT_CASE(vkms_color_test_lerp),
 	KUNIT_CASE(vkms_color_test_linear),
+	KUNIT_CASE(vkms_color_test_lut32_identity),
+	KUNIT_CASE(vkms_color_test_lut32_three_points),
+	KUNIT_CASE(vkms_color_test_lut32_single_entry),
+	KUNIT_CASE(vkms_color_test_lut32_descending),
 	KUNIT_CASE(vkms_color_srgb_inv_srgb),
 	KUNIT_CASE(vkms_color_ctm_3x4_50_desat),
 	KUNIT_CASE(vkms_color_ctm_3x4_bt709),
drivers/gpu/drm/vkms/vkms_colorop.c +19 -0
index 2c4188f80e9b..fdbdad103412 100644
@@ -119,6 +119,25 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 
 	drm_colorop_set_next_property(ops[i - 1], ops[i]);
 
+	i++;
+
+	/* 6th op: 1D LUT */
+	ops[i] = kzalloc_obj(*ops[i]);
+	if (!ops[i]) {
+		drm_err(dev, "KMS: Failed to allocate colorop\n");
+		ret = -ENOMEM;
+		goto cleanup;
+	}
+
+	ret = drm_plane_colorop_curve_1d_lut_init(dev, ops[i], plane, &vkms_colorop_funcs,
+						  VKMS_LUT1D_SIZE,
+						  DRM_COLOROP_LUT1D_INTERPOLATION_LINEAR,
+						  DRM_COLOROP_FLAG_ALLOW_BYPASS);
+	if (ret)
+		goto cleanup;
+
+	drm_colorop_set_next_property(ops[i - 1], ops[i]);
+
 	list->name = kasprintf(GFP_KERNEL, "Color Pipeline %d", ops[0]->base.id);
 
 	return 0;
drivers/gpu/drm/vkms/vkms_composer.c +79 -0

107 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_composer.h +2 -0
index fa003af12f85..6da85efc5524 100644
@@ -22,6 +22,8 @@ u16 lerp_u16(u16 a, u16 b, s64 t);
 s64 get_uniform_lut_index(const struct vkms_color_lut *lut, u16 channel_value);
 u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_value,
 			       enum lut_channel channel);
+u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut, u32 lut_size,
+				 u16 channel_value, enum lut_channel channel);
 void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix);
 #endif
 
drivers/gpu/drm/vkms/vkms_drv.h +3 -0
index 1c5205aa6e0c..05fba864cf36 100644
@@ -35,6 +35,9 @@
 
 #define VKMS_GAMMA_LUT_SIZE 256
 
+/* Entries in the 1D LUT colorop of a plane's color pipeline. */
+#define VKMS_LUT1D_SIZE 256
+
 /**
  * struct vkms_frame_info - Structure to store the state of a frame
  *
Signature · 1 line
2.56.1
#5 lore raw

PATCH v2 5/7 drm/vkms: add a 3D LUT colorop

In reply to Oarora Etimis

Add a 3D LUT colorop of 17x17x17 entries, interpolated tetrahedrally. The composer locates the cell of the pixel and weights the four corners of the tetrahedron its fractions select. Red is the fastest moving index, as the uapi documents.

The existing colorops and their order are unchanged.

---
 drivers/gpu/drm/vkms/vkms_colorop.c  |  19 +++++
 drivers/gpu/drm/vkms/vkms_composer.c | 140 +++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_composer.h |   2 +
 drivers/gpu/drm/vkms/vkms_drv.h      |   2 +
 4 files changed, 163 insertions(+)
drivers/gpu/drm/vkms/vkms_colorop.c +19 -0
index fdbdad103412..9ffc6e354206 100644
@@ -138,6 +138,25 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 
 	drm_colorop_set_next_property(ops[i - 1], ops[i]);
 
+	i++;
+
+	/* 7th op: 3D LUT */
+	ops[i] = kzalloc_obj(*ops[i]);
+	if (!ops[i]) {
+		drm_err(dev, "KMS: Failed to allocate colorop\n");
+		ret = -ENOMEM;
+		goto cleanup;
+	}
+
+	ret = drm_plane_colorop_3dlut_init(dev, ops[i], plane, &vkms_colorop_funcs,
+					   VKMS_LUT3D_SIZE,
+					   DRM_COLOROP_LUT3D_INTERPOLATION_TETRAHEDRAL,
+					   DRM_COLOROP_FLAG_ALLOW_BYPASS);
+	if (ret)
+		goto cleanup;
+
+	drm_colorop_set_next_property(ops[i - 1], ops[i]);
+
 	list->name = kasprintf(GFP_KERNEL, "Color Pipeline %d", ops[0]->base.id);
 
 	return 0;
drivers/gpu/drm/vkms/vkms_composer.c +140 -0

158 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_composer.h +2 -0
index 6da85efc5524..28323df662a4 100644
@@ -24,6 +24,8 @@ u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_val
 			       enum lut_channel channel);
 u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut, u32 lut_size,
 				 u16 channel_value, enum lut_channel channel);
+void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
+			  const struct drm_color_lut32 *lut, u32 size);
 void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix);
 #endif
 
drivers/gpu/drm/vkms/vkms_drv.h +2 -0
index 05fba864cf36..d975cb684fd9 100644
@@ -38,6 +38,8 @@
 /* Entries in the 1D LUT colorop of a plane's color pipeline. */
 #define VKMS_LUT1D_SIZE 256
 
+/* Entries per dimension in the 3D LUT colorop of a plane's color pipeline. */
+#define VKMS_LUT3D_SIZE 17
 /**
  * struct vkms_frame_info - Structure to store the state of a frame
  *
Signature · 1 line
2.56.1
#6 lore raw

PATCH v2 6/7 drm/vkms: add a multiplier colorop

In reply to Oarora Etimis

Add a multiplier colorop that scales the channels by an S31.32 gain. The value is sign-magnitude like the 3x4 matrix, so it goes through drm_sm2fixp() first.

---
 drivers/gpu/drm/vkms/vkms_colorop.c  | 17 +++++++++++++++++
 drivers/gpu/drm/vkms/vkms_composer.c | 14 ++++++++++++++
 drivers/gpu/drm/vkms/vkms_composer.h |  1 +
 3 files changed, 32 insertions(+)
drivers/gpu/drm/vkms/vkms_colorop.c +17 -0
index 9ffc6e354206..94f419658073 100644
@@ -157,6 +157,23 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 
 	drm_colorop_set_next_property(ops[i - 1], ops[i]);
 
+	i++;
+
+	/* 8th op: multiplier */
+	ops[i] = kzalloc_obj(*ops[i]);
+	if (!ops[i]) {
+		drm_err(dev, "KMS: Failed to allocate colorop\n");
+		ret = -ENOMEM;
+		goto cleanup;
+	}
+
+	ret = drm_plane_colorop_mult_init(dev, ops[i], plane, &vkms_colorop_funcs,
+					  DRM_COLOROP_FLAG_ALLOW_BYPASS);
+	if (ret)
+		goto cleanup;
+
+	drm_colorop_set_next_property(ops[i - 1], ops[i]);
+
 	list->name = kasprintf(GFP_KERNEL, "Color Pipeline %d", ops[0]->base.id);
 
 	return 0;
drivers/gpu/drm/vkms/vkms_composer.c +14 -0
index 671c16854a49..6aef2a6faf09 100644
@@ -378,6 +378,17 @@ VISIBLE_IF_KUNIT void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
 }
 EXPORT_SYMBOL_IF_KUNIT(apply_3dlut_to_pixel);
 
+VISIBLE_IF_KUNIT void apply_multiplier_to_pixel(struct pixel_argb_s32 *pixel,
+						u64 multiplier)
+{
+	s64 gain = drm_sm2fixp(multiplier);
+
+	pixel->r = drm_fixp2int_round(drm_fixp_mul(drm_int2fixp(pixel->r), gain));
+	pixel->g = drm_fixp2int_round(drm_fixp_mul(drm_int2fixp(pixel->g), gain));
+	pixel->b = drm_fixp2int_round(drm_fixp_mul(drm_int2fixp(pixel->b), gain));
+}
+EXPORT_SYMBOL_IF_KUNIT(apply_multiplier_to_pixel);
+
 
 static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
 {
@@ -502,6 +513,9 @@ static void apply_colorop(struct pixel_argb_s32 *pixel,
 	case DRM_COLOROP_3D_LUT:
 		apply_3d_lut(pixel, state);
 		break;
+	case DRM_COLOROP_MULTIPLIER:
+		apply_multiplier_to_pixel(pixel, state->multiplier);
+		break;
 	case DRM_COLOROP_FIXED_MATRIX:
 		break;
 	default:
drivers/gpu/drm/vkms/vkms_composer.h +1 -0
index 28323df662a4..5546ee864504 100644
@@ -26,6 +26,7 @@ u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut, u32 lut_size
 				 u16 channel_value, enum lut_channel channel);
 void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
 			  const struct drm_color_lut32 *lut, u32 size);
+void apply_multiplier_to_pixel(struct pixel_argb_s32 *pixel, u64 multiplier);
 void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix);
 #endif
 
Signature · 1 line
2.56.1
#7 lore raw

PATCH v2 7/7 drm/vkms: add KUnit coverage for the 3D LUT and multiplier colorops

In reply to Oarora Etimis

The 3D LUT test uses a 2x2x2 identity LUT with inputs that reach all six tetrahedra, and checks that values outside [0, 1] saturate. The multiplier test covers gains of 1.0, 0.5, 2.0 and -0.5.

---
 drivers/gpu/drm/vkms/tests/vkms_color_test.c | 105 +++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)
drivers/gpu/drm/vkms/tests/vkms_color_test.c +105 -0
index 0d68de6462d9..15f71a977ed9 100644
@@ -205,6 +205,108 @@ static void vkms_color_test_lut32_single_entry(struct kunit *test)
 						     0xffff, LUT_RED));
 }
 
+/*
+ * A 2x2x2 identity LUT: every corner maps the axis index to the same
+ * value, so any input has to come back unchanged. The entries are ordered
+ * with red as the fastest moving index, as the uapi documents.
+ */
+static const struct drm_color_lut32 test_lut3d_identity[] = {
+	{ 0x00000000, 0x00000000, 0x00000000, 0 },
+	{ 0xffffffff, 0x00000000, 0x00000000, 0 },
+	{ 0x00000000, 0xffffffff, 0x00000000, 0 },
+	{ 0xffffffff, 0xffffffff, 0x00000000, 0 },
+	{ 0x00000000, 0x00000000, 0xffffffff, 0 },
+	{ 0xffffffff, 0x00000000, 0xffffffff, 0 },
+	{ 0x00000000, 0xffffffff, 0xffffffff, 0 },
+	{ 0xffffffff, 0xffffffff, 0xffffffff, 0 },
+};
+
+static void vkms_color_test_lut3d_identity(struct kunit *test)
+{
+	static const struct {
+		u16 r, g, b;
+	} cases[] = {
+		{ 0x0000, 0x0000, 0x0000 },
+		{ 0xffff, 0xffff, 0xffff },
+		{ 0x8000, 0x8000, 0x8000 },
+		{ 0xffff, 0x0000, 0x0000 },
+		{ 0x0000, 0xffff, 0x0000 },
+		{ 0x0000, 0x0000, 0xffff },
+		{ 0x8000, 0x4000, 0x1000 },
+		/* One input per tetrahedron the lists above do not reach. */
+		{ 0x9000, 0x1000, 0x5000 },
+		{ 0x5000, 0x1000, 0x9000 },
+		{ 0x1000, 0x9000, 0x5000 },
+		{ 0x1000, 0x5000, 0x9000 },
+	};
+
+	for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
+		struct pixel_argb_s32 pixel = {
+			.r = cases[i].r,
+			.g = cases[i].g,
+			.b = cases[i].b,
+			.a = 0xffff,
+		};
+
+		apply_3dlut_to_pixel(&pixel, test_lut3d_identity, 2);
+
+		KUNIT_EXPECT_EQ(test, cases[i].r, pixel.r);
+		KUNIT_EXPECT_EQ(test, cases[i].g, pixel.g);
+		KUNIT_EXPECT_EQ(test, cases[i].b, pixel.b);
+		KUNIT_EXPECT_EQ(test, 0xffff, pixel.a);
+	}
+}
+
+/* A value outside [0, 1] saturates at the LUT input instead of wrapping. */
+static void vkms_color_test_lut3d_clamp(struct kunit *test)
+{
+	struct pixel_argb_s32 pixel = {
+		.r = -1,
+		.g = 0x10000,
+		.b = 0xffff,
+		.a = 0xffff,
+	};
+
+	apply_3dlut_to_pixel(&pixel, test_lut3d_identity, 2);
+
+	KUNIT_EXPECT_EQ(test, 0x0000, pixel.r);
+	KUNIT_EXPECT_EQ(test, 0xffff, pixel.g);
+	KUNIT_EXPECT_EQ(test, 0xffff, pixel.b);
+}
+
+static void vkms_color_test_multiplier(struct kunit *test)
+{
+	struct pixel_argb_s32 pixel;
+
+	/* 1.0 leaves the pixel unchanged. */
+	pixel = (struct pixel_argb_s32){ .r = 0x8000, .g = 0x4000, .b = 0x2000 };
+	apply_multiplier_to_pixel(&pixel, 1ULL << 32);
+	KUNIT_EXPECT_EQ(test, 0x8000, pixel.r);
+	KUNIT_EXPECT_EQ(test, 0x4000, pixel.g);
+	KUNIT_EXPECT_EQ(test, 0x2000, pixel.b);
+
+	/* 0.5 halves every channel. */
+	pixel = (struct pixel_argb_s32){ .r = 0x8000, .g = 0x4000, .b = 0x2000 };
+	apply_multiplier_to_pixel(&pixel, 1ULL << 31);
+	KUNIT_EXPECT_EQ(test, 0x4000, pixel.r);
+	KUNIT_EXPECT_EQ(test, 0x2000, pixel.g);
+	KUNIT_EXPECT_EQ(test, 0x1000, pixel.b);
+
+	/* 2.0 doubles every channel. */
+	pixel = (struct pixel_argb_s32){ .r = 0x4000, .g = 0x2000, .b = 0x1000 };
+	apply_multiplier_to_pixel(&pixel, 1ULL << 33);
+	KUNIT_EXPECT_EQ(test, 0x8000, pixel.r);
+	KUNIT_EXPECT_EQ(test, 0x4000, pixel.g);
+	KUNIT_EXPECT_EQ(test, 0x2000, pixel.b);
+
+	/* A negative gain flips the sign; the composer clamps it at the end. */
+	pixel = (struct pixel_argb_s32){ .r = 0x8000, .g = 0x4000, .b = 0x2000 };
+	apply_multiplier_to_pixel(&pixel, (1ULL << 63) | (1ULL << 31));
+	KUNIT_EXPECT_EQ(test, -0x4000, pixel.r);
+	KUNIT_EXPECT_EQ(test, -0x2000, pixel.g);
+	KUNIT_EXPECT_EQ(test, -0x1000, pixel.b);
+}
+
 static void vkms_color_srgb_inv_srgb(struct kunit *test)
 {
 	u16 srgb, final;
@@ -474,6 +576,9 @@ static struct kunit_case vkms_color_test_cases[] = {
 	KUNIT_CASE(vkms_color_test_lut32_three_points),
 	KUNIT_CASE(vkms_color_test_lut32_single_entry),
 	KUNIT_CASE(vkms_color_test_lut32_descending),
+	KUNIT_CASE(vkms_color_test_lut3d_identity),
+	KUNIT_CASE(vkms_color_test_lut3d_clamp),
+	KUNIT_CASE(vkms_color_test_multiplier),
 	KUNIT_CASE(vkms_color_srgb_inv_srgb),
 	KUNIT_CASE(vkms_color_ctm_3x4_50_desat),
 	KUNIT_CASE(vkms_color_ctm_3x4_bt709),
Signature · 1 line
2.56.1