From: "Leo (Sunpeng) Li" <sunpeng...@amd.com>

We compose the two LUTs when pushing non-legacy gamma changes, and the
same needs to be done when setting legacy gamma.

If a non-legacy LUT has not been set (i.e. using the default value),
the legacy LUT will act independantly. However, instead of using DRM's
legacy gamma code-path, we adapt to the non-legacy path by upscaling the
LUT to non-legacy size.

It's also possible that the kernel driver doesn't support non-legacy
color management. In which case, we fall back to legacy gamma.

Signed-off-by: Leo (Sunpeng) Li <sunpeng...@amd.com>
---
 src/drmmode_display.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 109 insertions(+), 2 deletions(-)

diff --git a/src/drmmode_display.c b/src/drmmode_display.c
index 9c8c344..2b38a71 100644
--- a/src/drmmode_display.c
+++ b/src/drmmode_display.c
@@ -958,6 +958,63 @@ static void drmmode_lut_compose(uint16_t *a_red,
 }
 
 /**
+ * Resize a LUT, using linear interpolation.
+ *
+ * @in_(red|green|blue): Legacy LUT components
+ * @out_lut: The resized LUT is returned here, in DRM color LUT format.
+ * @len_in: Length of legacy LUT.
+ * @len_out: Length of out_lut, i.e. the target size.
+ */
+static void drmmode_lut_interpolate(uint16_t *in_red,
+                                   uint16_t *in_green,
+                                   uint16_t *in_blue,
+                                   struct drm_color_lut *out_lut,
+                                   uint32_t len_in, uint32_t len_out)
+{
+       uint32_t i_l, i_r, i;
+       uint32_t i_amax, i_bmax;
+       uint32_t coeff_ibmax;
+       uint64_t out_ibmax;
+       int color;
+       size_t struct_size = sizeof(struct drm_color_lut);
+
+       i_amax = len_in - 1;
+       i_bmax = len_out - 1;
+
+       /* See @drmmode_lut_compose for details */
+       for (color = 0; color < 3; color++) {
+               uint16_t *in, *out;
+
+               if (color == 0) {
+                       in = in_red;
+                       out = &out_lut[0].red;
+               } else if (color == 1) {
+                       in = in_green;
+                       out = &out_lut[0].green;
+               } else {
+                       in = in_blue;
+                       out = &out_lut[0].blue;
+               }
+
+               for (i = 0; i < len_out; i++) {
+
+                       i_l = i * i_amax / i_bmax;
+                       i_r = i_l + !!(i_amax - i_l);
+
+                       coeff_ibmax = (i * i_amax) - (i_l * i_bmax);
+                       out_ibmax = i_bmax * in[i_l] +
+                                     coeff_ibmax * (in[i_r] - in[i_l]);
+
+                       *(uint16_t*)((void*)out + (i*struct_size)) =
+                               out_ibmax / i_bmax;
+               }
+       }
+
+       for (i = 0; i < len_out; i++)
+               out_lut[i].reserved = 0;
+}
+
+/**
  * Configure and change a color property on a CRTC, through RandR. Only the
  * specified output will be affected, even if the CRTC is attached to multiple
  * outputs. If the request is pending, then the change will make it's way into
@@ -1293,9 +1350,59 @@ drmmode_crtc_gamma_do_set(xf86CrtcPtr crtc, uint16_t 
*red, uint16_t *green,
 {
        drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
        AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(crtc->scrn);
+       struct drm_color_lut *composed;
+       uint32_t drm_prop_id, created_blob_id;
+       size_t bytes;
+       int ret;
+
+       drm_prop_id = get_drm_cm_prop_id(crtc, CM_GAMMA_LUT);
+       /* Use legacy if kernel does not support non-legacy gamma */
+       if (!drm_prop_id) {
+               drmModeCrtcSetGamma(pAMDGPUEnt->fd,
+                                   drmmode_crtc->mode_crtc->crtc_id,
+                                   size, red, green, blue);
+               return;
+       }
+
+       bytes = sizeof(*composed) * drmmode_crtc->gamma_lut_size;
+
+       composed = malloc(bytes);
+       if (!composed) {
+               xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
+                          "Memory error allocating legacy LUT.");
+               return;
+       }
+
+       if (drmmode_crtc->gamma_lut)
+               drmmode_lut_compose(red, green, blue, drmmode_crtc->gamma_lut,
+                                   composed, size,
+                                   drmmode_crtc->gamma_lut_size);
+       else
+               drmmode_lut_interpolate(red, green, blue, composed,
+                                       size, drmmode_crtc->gamma_lut_size);
+
+       ret = drmModeCreatePropertyBlob(pAMDGPUEnt->fd, composed, bytes,
+                                       &created_blob_id);
+       if (ret) {
+               xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
+                          "Creating Gamma LUT failed with errno %d\n",
+                          ret);
+               free(composed);
+               return;
+       }
+
+       ret = drmModeObjectSetProperty(pAMDGPUEnt->fd,
+                                      drmmode_crtc->mode_crtc->crtc_id,
+                                      DRM_MODE_OBJECT_CRTC,
+                                      drm_prop_id,
+                                      (uint64_t)created_blob_id);
+
+       if (ret)
+               xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
+                          "Setting Gamma LUT failed with errno %d\n",
+                          ret);
 
-       drmModeCrtcSetGamma(pAMDGPUEnt->fd, drmmode_crtc->mode_crtc->crtc_id,
-                           size, red, green, blue);
+       free(composed);
 }
 
 Bool
-- 
2.7.4

_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reply via email to