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

From: Oarora Etimis

Date: Sat Nov 28 2026 - 13:20:29 EST


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.

Signed-off-by: Oarora Etimis <oaroraetimis@xxxxxxxxx>
---
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(+)

diff --git a/drivers/gpu/drm/vkms/tests/vkms_color_test.c b/drivers/gpu/drm/vkms/tests/vkms_color_test.c
index c3c61037cb71..0d68de6462d9 100644
--- a/drivers/gpu/drm/vkms/tests/vkms_color_test.c
+++ b/drivers/gpu/drm/vkms/tests/vkms_color_test.c
@@ -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),
diff --git a/drivers/gpu/drm/vkms/vkms_colorop.c b/drivers/gpu/drm/vkms/vkms_colorop.c
index 2c4188f80e9b..fdbdad103412 100644
--- a/drivers/gpu/drm/vkms/vkms_colorop.c
+++ b/drivers/gpu/drm/vkms/vkms_colorop.c
@@ -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;
diff --git a/drivers/gpu/drm/vkms/vkms_composer.c b/drivers/gpu/drm/vkms/vkms_composer.c
index 2cd909bc8adc..56749ecdd17f 100644
--- a/drivers/gpu/drm/vkms/vkms_composer.c
+++ b/drivers/gpu/drm/vkms/vkms_composer.c
@@ -1,10 +1,14 @@
// SPDX-License-Identifier: GPL-2.0+

+#include <linux/bits.h>
#include <linux/crc32.h>
+#include <linux/limits.h>
+#include <linux/math64.h>

#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_blend.h>
+#include <drm/drm_color_mgmt.h>
#include <drm/drm_colorop.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_fixed.h>
@@ -198,6 +202,59 @@ VISIBLE_IF_KUNIT u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut
}
EXPORT_SYMBOL_IF_KUNIT(apply_lut_to_channel_value);

+static u16 lut32_channel_value(const struct drm_color_lut32 *entry,
+ enum lut_channel channel)
+{
+ switch (channel) {
+ case LUT_RED:
+ return drm_color_lut32_extract(entry->red, BITS_PER_TYPE(u16));
+ case LUT_GREEN:
+ return drm_color_lut32_extract(entry->green, BITS_PER_TYPE(u16));
+ case LUT_BLUE:
+ return drm_color_lut32_extract(entry->blue, BITS_PER_TYPE(u16));
+ case LUT_RESERVED:
+ break;
+ }
+
+ return 0;
+}
+
+/*
+ * Apply a uniformly spaced 1D LUT to one channel, interpolating linearly
+ * between the two entries around the input value.
+ */
+VISIBLE_IF_KUNIT u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut,
+ u32 lut_size, u16 channel_value,
+ enum lut_channel channel)
+{
+ u64 position;
+ u32 index, fraction;
+ s64 delta, result;
+ u16 floor_value, ceil_value;
+
+ if (!lut_size)
+ return channel_value;
+
+ if (lut_size == 1)
+ return lut32_channel_value(&lut[0], channel);
+
+ position = (u64)channel_value * (lut_size - 1);
+ index = div_u64_rem(position, U16_MAX, &fraction);
+
+ if (index >= lut_size - 1)
+ return lut32_channel_value(&lut[lut_size - 1], channel);
+
+ floor_value = lut32_channel_value(&lut[index], channel);
+ ceil_value = lut32_channel_value(&lut[index + 1], channel);
+
+ delta = (s64)ceil_value - (s64)floor_value;
+ result = (s64)floor_value +
+ DIV_S64_ROUND_CLOSEST(delta * fraction, U16_MAX);
+
+ return (u16)clamp(result, 0LL, (s64)U16_MAX);
+}
+EXPORT_SYMBOL_IF_KUNIT(apply_lut32_to_channel_value);
+

static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
{
@@ -270,6 +327,25 @@ static void apply_1d_curve(struct pixel_argb_s32 *pixel,
}
}

+static void apply_1d_lut(struct pixel_argb_s32 *pixel,
+ const struct vkms_colorop_state *state)
+{
+ const struct drm_colorop *colorop = state->colorop;
+ const struct drm_color_lut32 *lut;
+
+ if (!state->data)
+ return;
+
+ lut = state->data->data;
+
+ pixel->r = apply_lut32_to_channel_value(lut, colorop->size,
+ clamp_val(pixel->r, 0, 0xffff), LUT_RED);
+ pixel->g = apply_lut32_to_channel_value(lut, colorop->size,
+ clamp_val(pixel->g, 0, 0xffff), LUT_GREEN);
+ pixel->b = apply_lut32_to_channel_value(lut, colorop->size,
+ clamp_val(pixel->b, 0, 0xffff), LUT_BLUE);
+}
+
static void apply_colorop(struct pixel_argb_s32 *pixel,
const struct vkms_colorop_state *state)
{
@@ -283,6 +359,9 @@ static void apply_colorop(struct pixel_argb_s32 *pixel,
if (state->data)
apply_3x4_matrix(pixel, (struct drm_color_ctm_3x4 *)state->data->data);
break;
+ case DRM_COLOROP_1D_LUT:
+ apply_1d_lut(pixel, state);
+ break;
case DRM_COLOROP_FIXED_MATRIX:
break;
default:
diff --git a/drivers/gpu/drm/vkms/vkms_composer.h b/drivers/gpu/drm/vkms/vkms_composer.h
index fa003af12f85..6da85efc5524 100644
--- a/drivers/gpu/drm/vkms/vkms_composer.h
+++ b/drivers/gpu/drm/vkms/vkms_composer.h
@@ -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

diff --git a/drivers/gpu/drm/vkms/vkms_drv.h b/drivers/gpu/drm/vkms/vkms_drv.h
index 1c5205aa6e0c..05fba864cf36 100644
--- a/drivers/gpu/drm/vkms/vkms_drv.h
+++ b/drivers/gpu/drm/vkms/vkms_drv.h
@@ -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
*

--
2.56.1