Skip to contentSkip to conversation

Apply gamma correction to HW cursor

PATCH In review Michel Dänzer 2 participants 2 messages May 10, 2017 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Apply gamma correction to HW cursor

From: Michel Dänzer <[email protected]>

The display hardware CLUT we're currently using for gamma correction doesn't affect the HW cursor, so we have to apply it manually when uploading the HW cursor data.

This currently only works in depth 24/32.

(Ported from amdgpu commit 82fa615f38137add75f9cd4bb49c48dd88de916f)

---
 src/drmmode_display.c | 66 ++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 58 insertions(+), 8 deletions(-)
src/drmmode_display.c +58 -8
index 84e7ef967..0b823754b 100644
@@ -812,6 +812,17 @@ drmmode_crtc_scanout_update(xf86CrtcPtr crtc, DisplayModePtr mode,
 	}
 }
 
+static void
+drmmode_crtc_gamma_do_set(xf86CrtcPtr crtc, uint16_t *red, uint16_t *green,
+			  uint16_t *blue, int size)
+{
+	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
+
+	drmModeCrtcSetGamma(drmmode_crtc->drmmode->fd,
+			    drmmode_crtc->mode_crtc->crtc_id, size, red, green,
+			    blue);
+}
+
 static Bool
 drmmode_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
 		     Rotation rotation, int x, int y)
@@ -873,8 +884,8 @@ drmmode_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
 		if (drmmode_crtc->tear_free)
 			scanout_id = drmmode_crtc->scanout_id;
 
-		crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
-				       crtc->gamma_blue, crtc->gamma_size);
+		drmmode_crtc_gamma_do_set(crtc, crtc->gamma_red, crtc->gamma_green,
+					  crtc->gamma_blue, crtc->gamma_size);
 
 		drmmode_ConvertToKMode(crtc->scrn, &kmode, mode);
 
@@ -1043,6 +1054,31 @@ drmmode_cursor_src_offset(Rotation rotation, int width, int height,
 
 #endif
 
+static uint32_t
+drmmode_cursor_gamma(xf86CrtcPtr crtc, uint32_t argb)
+{
+	uint32_t alpha = argb >> 24;
+	uint32_t rgb[3];
+	int i;
+
+	if (!alpha)
+		return 0;
+
+	if (crtc->scrn->depth != 24 && crtc->scrn->depth != 32)
+		return argb;
+
+	/* Un-premultiply alpha */
+	for (i = 0; i < 3; i++)
+		rgb[i] = ((argb >> (i * 8)) & 0xff) * 0xff / alpha;
+
+	/* Apply gamma correction and pre-multiply alpha */
+	rgb[0] = (crtc->gamma_blue[rgb[0]] >> 8) * alpha / 0xff;
+	rgb[1] = (crtc->gamma_green[rgb[1]] >> 8) * alpha / 0xff;
+	rgb[2] = (crtc->gamma_red[rgb[2]] >> 8) * alpha / 0xff;
+
+	return alpha << 24 | rgb[2] << 16 | rgb[1] << 8 | rgb[0];
+}
+
 static void
 drmmode_load_cursor_argb (xf86CrtcPtr crtc, CARD32 *image)
 {
@@ -1068,7 +1104,8 @@ drmmode_load_cursor_argb (xf86CrtcPtr crtc, CARD32 *image)
 								      dstx, dsty);
 
 				ptr[dsty * info->cursor_w + dstx] =
-					cpu_to_le32(image[srcoffset]);
+					cpu_to_le32(drmmode_cursor_gamma(crtc,
+									 image[srcoffset]));
 			}
 		}
 	} else
@@ -1078,7 +1115,7 @@ drmmode_load_cursor_argb (xf86CrtcPtr crtc, CARD32 *image)
 		int i;
 
 		for (i = 0; i < cursor_size; i++)
-			ptr[i] = cpu_to_le32(image[i]);
+			ptr[i] = cpu_to_le32(drmmode_cursor_gamma(crtc, image[i]));
 	}
 }
 
@@ -1209,11 +1246,24 @@ static void
 drmmode_crtc_gamma_set(xf86CrtcPtr crtc, uint16_t *red, uint16_t *green,
                       uint16_t *blue, int size)
 {
-	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
-	drmmode_ptr drmmode = drmmode_crtc->drmmode;
+	ScrnInfoPtr scrn = crtc->scrn;
+	xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
+	RADEONInfoPtr info = RADEONPTR(scrn);
+	int i;
 
-	drmModeCrtcSetGamma(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
-			    size, red, green, blue);
+	drmmode_crtc_gamma_do_set(crtc, red, green, blue, size);
+
+	/* Compute index of this CRTC into xf86_config->crtc */
+	for (i = 0; xf86_config->crtc[i] != crtc; i++) {}
+
+	if (info->hwcursor_disabled & (1 << i))
+		return;
+
+#ifdef HAVE_XF86_CURSOR_RESET_CURSOR
+	xf86CursorResetCursor(scrn->pScreen);
+#else
+	xf86_reload_cursors(scrn->pScreen);
+#endif
 }
 
 #ifdef RADEON_PIXMAP_SHARING
Signature · 6 lines
2.11.0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 20170510082436.324-1-michel-otUistvHUpPR7s880joybQ@public.gmane.org

Reviewed-by Alex Deucher
#2 lore raw
Quoted 13 lines

-----Original Message-----
From: amd-gfx [mailto:[email protected]] On Behalf Of Michel Dänzer
Sent: Wednesday, May 10, 2017 4:25 AM
To: [email protected]
Subject: [PATCH xf86-video-ati] Apply gamma correction to HW cursor

From: Michel Dänzer <[email protected]>

The display hardware CLUT we're currently using for gamma correction doesn't affect the HW cursor, so we have to apply it manually when uploading the HW cursor data.

Quoted 5 lines

This currently only works in depth 24/32.

(Ported from amdgpu commit 82fa615f38137add75f9cd4bb49c48dd88de916f)

Quoted 132 lines
---
 src/drmmode_display.c | 66
++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 58 insertions(+), 8 deletions(-)
src/drmmode_display.c +11 -0
index 84e7ef967..0b823754b 100644
@@ -812,6 +812,17 @@ drmmode_crtc_scanout_update(xf86CrtcPtr crtc,
 DisplayModePtr mode,
 	}
 }
 
+static void
+drmmode_crtc_gamma_do_set(xf86CrtcPtr crtc, uint16_t *red, uint16_t
 *green,
+			  uint16_t *blue, int size)
+{
+	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
+
+	drmModeCrtcSetGamma(drmmode_crtc->drmmode->fd,
+			    drmmode_crtc->mode_crtc->crtc_id, size, red,
 green,
+			    blue);
+}
+

static Bool
drmmode_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,

src/drmmode_display.c +2 -2
 		     Rotation rotation, int x, int y)
@@ -873,8 +884,8 @@ drmmode_set_mode_major(xf86CrtcPtr crtc,
 DisplayModePtr mode,
 		if (drmmode_crtc->tear_free)
 			scanout_id = drmmode_crtc->scanout_id;
 
-		crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc-
 >gamma_green,
-				       crtc->gamma_blue, crtc->gamma_size);
+		drmmode_crtc_gamma_do_set(crtc, crtc->gamma_red, crtc-
 >gamma_green,
+					  crtc->gamma_blue, crtc-
Quoted 1 line

gamma_size);

src/drmmode_display.c +25 -0
 		drmmode_ConvertToKMode(crtc->scrn, &kmode, mode);
@@ -1043,6 +1054,31 @@ drmmode_cursor_src_offset(Rotation rotation,
 int width, int height,
 
 #endif
 
+static uint32_t
+drmmode_cursor_gamma(xf86CrtcPtr crtc, uint32_t argb)
+{
+	uint32_t alpha = argb >> 24;
+	uint32_t rgb[3];
+	int i;
+
+	if (!alpha)
+		return 0;
+
+	if (crtc->scrn->depth != 24 && crtc->scrn->depth != 32)
+		return argb;
+
+	/* Un-premultiply alpha */
+	for (i = 0; i < 3; i++)
+		rgb[i] = ((argb >> (i * 8)) & 0xff) * 0xff / alpha;
+
+	/* Apply gamma correction and pre-multiply alpha */
+	rgb[0] = (crtc->gamma_blue[rgb[0]] >> 8) * alpha / 0xff;
+	rgb[1] = (crtc->gamma_green[rgb[1]] >> 8) * alpha / 0xff;
+	rgb[2] = (crtc->gamma_red[rgb[2]] >> 8) * alpha / 0xff;
+
+	return alpha << 24 | rgb[2] << 16 | rgb[1] << 8 | rgb[0];
+}
+
 static void
 drmmode_load_cursor_argb (xf86CrtcPtr crtc, CARD32 *image)

{

src/drmmode_display.c +2 -1
@@ -1068,7 +1104,8 @@ drmmode_load_cursor_argb (xf86CrtcPtr crtc,
 CARD32 *image)
 								      dstx, dsty);
 
 				ptr[dsty * info->cursor_w + dstx] =
-					cpu_to_le32(image[srcoffset]);
+
 	cpu_to_le32(drmmode_cursor_gamma(crtc,
+
 image[srcoffset]));
src/drmmode_display.c +1 -1
 			}
 		}
 	} else
@@ -1078,7 +1115,7 @@ drmmode_load_cursor_argb (xf86CrtcPtr crtc,
 CARD32 *image)
 		int i;
 
 		for (i = 0; i < cursor_size; i++)
-			ptr[i] = cpu_to_le32(image[i]);
+			ptr[i] = cpu_to_le32(drmmode_cursor_gamma(crtc,
 image[i]));
 	}

}

src/drmmode_display.c +17 -4
@@ -1209,11 +1246,24 @@ static void
 drmmode_crtc_gamma_set(xf86CrtcPtr crtc, uint16_t *red, uint16_t
 *green,
                       uint16_t *blue, int size)
 {
-	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
-	drmmode_ptr drmmode = drmmode_crtc->drmmode;
+	ScrnInfoPtr scrn = crtc->scrn;
+	xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
+	RADEONInfoPtr info = RADEONPTR(scrn);
+	int i;
 
-	drmModeCrtcSetGamma(drmmode->fd, drmmode_crtc-
 >mode_crtc->crtc_id,
-			    size, red, green, blue);
+	drmmode_crtc_gamma_do_set(crtc, red, green, blue, size);
+
+	/* Compute index of this CRTC into xf86_config->crtc */
+	for (i = 0; xf86_config->crtc[i] != crtc; i++) {}
+
+	if (info->hwcursor_disabled & (1 << i))
+		return;
+
+#ifdef HAVE_XF86_CURSOR_RESET_CURSOR
+	xf86CursorResetCursor(scrn->pScreen);
+#else
+	xf86_reload_cursors(scrn->pScreen);
+#endif
 }
Quoted 8 lines

#ifdef RADEON_PIXMAP_SHARING
--
2.11.0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx