AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: <sunpeng.li-5C7GfCeVMHo@public.gmane.org>
To: amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org
Cc: "Leo (Sunpeng) Li" <sunpeng.li-5C7GfCeVMHo@public.gmane.org>,
	michel-otUistvHUpPR7s880joybQ@public.gmane.org,
	harry.wentland-5C7GfCeVMHo@public.gmane.org
Subject: [PATCH xf86-video-amdgpu 2/7] Initialize color properties on CRTC during CRTC init
Date: Fri, 1 Jun 2018 12:03:32 -0400	[thread overview]
Message-ID: <1527869017-9209-3-git-send-email-sunpeng.li@amd.com> (raw)
In-Reply-To: <1527869017-9209-1-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>

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

And destroy them on the CRTC destroy hook.

When initializing color management properties on the private
drmmode_crtc, we want to:

1. Obtain its degamma and regamma LUT sizes
2. Default its color transform matrix (CTM) to identity
3. Program hardware with default color management values (SRGB for
   de/regamma, identity for CTM)

It's possible that cm initialization fails due to memory error or DRM
error. In which case, DDX support for color management will be disabled
on this CRTC.

Signed-off-by: Leo (Sunpeng) Li <sunpeng.li@amd.com>
---
 src/drmmode_display.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/drmmode_display.h |   8 +++
 2 files changed, 196 insertions(+), 1 deletion(-)

diff --git a/src/drmmode_display.c b/src/drmmode_display.c
index 36b22ad..de09361 100644
--- a/src/drmmode_display.c
+++ b/src/drmmode_display.c
@@ -768,6 +768,88 @@ static enum drmmode_cm_prop get_cm_enum_from_str(const char *prop_name)
 	return CM_INVALID_PROP;
 }
 
+/**
+ * Return TRUE if the given CRTC supports non-legacy color management. False
+ * otherwise.
+ */
+static Bool drmmode_crtc_cm_enabled(drmmode_crtc_private_ptr drmmode_crtc)
+{
+	return drmmode_crtc->gamma_lut_size > 0 &&
+	       drmmode_crtc->degamma_lut_size > 0;
+}
+
+/**
+ * Push staged color management properties on the CRTC to DRM.
+ *
+ * @crtc: The CRTC containing staged properties
+ * @cm_prop_index: The color property to push
+ *
+ * Return 0 on success, X-defined error codes on failure.
+ */
+static int drmmode_crtc_push_cm_prop(xf86CrtcPtr crtc,
+				     enum drmmode_cm_prop cm_prop_index)
+{
+	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
+	AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(crtc->scrn);
+	uint32_t created_blob_id = 0;
+	uint32_t drm_prop_id;
+	size_t expected_bytes = 0;
+	void *blob_data = NULL;
+	int ret;
+
+	if (!drmmode_crtc_cm_enabled(drmmode_crtc))
+		return BadName;
+
+	if (cm_prop_index == CM_GAMMA_LUT) {
+		/* Calculate the expected size of value in bytes */
+		expected_bytes = sizeof(struct drm_color_lut) *
+					drmmode_crtc->gamma_lut_size;
+		blob_data = drmmode_crtc->gamma_lut;
+	} else if (cm_prop_index == CM_DEGAMMA_LUT) {
+		expected_bytes = sizeof(struct drm_color_lut) *
+					drmmode_crtc->degamma_lut_size;
+		blob_data = drmmode_crtc->degamma_lut;
+	} else if (cm_prop_index == CM_CTM) {
+		expected_bytes = sizeof(struct drm_color_ctm);
+		blob_data = drmmode_crtc->ctm;
+	} else
+		return BadName;
+
+	if (blob_data) {
+		ret = drmModeCreatePropertyBlob(pAMDGPUEnt->fd,
+						blob_data, expected_bytes,
+						&created_blob_id);
+		if (ret) {
+			xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
+				   "Creating DRM blob failed with errno %d\n",
+				   ret);
+			return BadRequest;
+		}
+	}
+
+	drm_prop_id = drmmode_crtc->drmmode->cm_prop_ids[cm_prop_index];
+	ret = drmModeObjectSetProperty(pAMDGPUEnt->fd,
+				       drmmode_crtc->mode_crtc->crtc_id,
+				       DRM_MODE_OBJECT_CRTC,
+				       drm_prop_id,
+				       (uint64_t)created_blob_id);
+
+	/* If successful, kernel will have a reference already. Safe to destroy
+	 * the blob either way.
+	 */
+	if (blob_data)
+		drmModeDestroyPropertyBlob(pAMDGPUEnt->fd, created_blob_id);
+
+	if (ret) {
+		xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
+			   "Setting DRM property blob failed with errno %d\n",
+			   ret);
+		return BadRequest;
+	}
+
+	return Success;
+}
+
 static void
 drmmode_crtc_gamma_do_set(xf86CrtcPtr crtc, uint16_t *red, uint16_t *green,
 			  uint16_t *blue, int size)
@@ -1314,6 +1396,22 @@ static Bool drmmode_set_scanout_pixmap(xf86CrtcPtr crtc, PixmapPtr ppix)
 	return TRUE;
 }
 
+static void drmmode_crtc_destroy(xf86CrtcPtr crtc)
+{
+	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
+
+	drmModeFreeCrtc(drmmode_crtc->mode_crtc);
+
+	/* Free LUTs and CTM */
+	free(drmmode_crtc->gamma_lut);
+	free(drmmode_crtc->degamma_lut);
+	free(drmmode_crtc->ctm);
+
+	free(drmmode_crtc);
+	crtc->driver_private = NULL;
+}
+
+
 static xf86CrtcFuncsRec drmmode_crtc_funcs = {
 	.dpms = drmmode_crtc_dpms,
 	.set_mode_major = drmmode_set_mode_major,
@@ -1330,7 +1428,7 @@ static xf86CrtcFuncsRec drmmode_crtc_funcs = {
 	.shadow_create = drmmode_crtc_shadow_create,
 	.shadow_allocate = drmmode_crtc_shadow_allocate,
 	.shadow_destroy = drmmode_crtc_shadow_destroy,
-	.destroy = NULL,	/* XXX */
+	.destroy = drmmode_crtc_destroy,
 	.set_scanout_pixmap = drmmode_set_scanout_pixmap,
 };
 
@@ -1354,6 +1452,93 @@ void drmmode_crtc_hw_id(xf86CrtcPtr crtc)
 		drmmode_crtc->hw_id = -1;
 }
 
+/**
+ * Initialize color management properties for the given CRTC by fetching its
+ * gamma/degamma LUT sizes, then programming default gamma/degamma LUTs and
+ * CTM.
+ *
+ * If the CRTC does not support color management, or if errors occur during
+ * initialization, all color properties on the driver-private CRTC will left
+ * as NULL.
+ *
+ * @drm_fd: DRM file descriptor
+ * @crtc: CRTC to initialize color management on.
+ */
+static void drmmode_crtc_cm_init(int drm_fd, xf86CrtcPtr crtc)
+{
+	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
+	drmModeObjectPropertiesPtr drm_props;
+	drmModePropertyPtr drm_prop;
+	enum drmmode_cm_prop prop_id;
+	int i;
+
+	drm_props = drmModeObjectGetProperties(drm_fd,
+					       drmmode_crtc->mode_crtc->crtc_id,
+					       DRM_MODE_OBJECT_CRTC);
+	if (!drm_props)
+		goto err_allocs;
+
+	/* Fetch degamma/gamma LUT sizes */
+	for (i = 0; i < drm_props->count_props; i++) {
+		drm_prop = drmModeGetProperty(drm_fd, drm_props->props[i]);
+		if (!drm_prop){
+			drmModeFreeObjectProperties(drm_props);
+			goto err_allocs;
+		}
+
+		prop_id = get_cm_enum_from_str(drm_prop->name);
+
+		if (prop_id == CM_GAMMA_LUT_SIZE)
+			drmmode_crtc->gamma_lut_size = drm_props->prop_values[i];
+		else if (prop_id == CM_DEGAMMA_LUT_SIZE)
+			drmmode_crtc->degamma_lut_size = drm_props->prop_values[i];
+
+		drmModeFreeProperty(drm_prop);
+	}
+	drmModeFreeObjectProperties(drm_props);
+
+	if (!drmmode_crtc_cm_enabled(drmmode_crtc)) {
+		xf86DrvMsg(crtc->scrn->scrnIndex, X_INFO,
+			   "CRTC%d does not support non-legacy color management.\n",
+			   drmmode_get_crtc_id(crtc));
+		drmmode_crtc->degamma_lut_size = 0;
+		drmmode_crtc->gamma_lut_size = 0;
+		return;
+	}
+	xf86DrvMsg(crtc->scrn->scrnIndex, X_INFO,
+		   "CRTC%d supports non-legacy color management.\n",
+		   drmmode_get_crtc_id(crtc));
+
+	/* Init CTM to identity. Values are in S31.32 fixed-point format */
+	drmmode_crtc->ctm = calloc(1, sizeof(*drmmode_crtc->ctm));
+	if (drmmode_crtc->ctm == NULL)
+		goto err_allocs;
+
+	drmmode_crtc->ctm->matrix[0] = drmmode_crtc->ctm->matrix[4] =
+		drmmode_crtc->ctm->matrix[8] = (uint64_t)1 << 32;
+
+	/* Push properties to reset properties currently in hardware */
+	for (i = 0; i < CM_NUM_PROPS; i++) {
+		if (i == CM_DEGAMMA_LUT_SIZE || i == CM_GAMMA_LUT_SIZE)
+			continue;
+
+		if (drmmode_crtc_push_cm_prop(crtc, i))
+			xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
+				   "Failed to initialize color management "
+				   "property %s on CRTC%d. Property value may "
+				   "not reflect actual hardware state.\n",
+				   cm_prop_names[i],
+				   drmmode_get_crtc_id(crtc));
+	}
+
+	return;
+
+err_allocs:
+	xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
+		   "Memory error initializing cm properties for CRTC%d",
+		   drmmode_get_crtc_id(crtc));
+}
+
 static unsigned int
 drmmode_crtc_init(ScrnInfoPtr pScrn, drmmode_ptr drmmode, drmModeResPtr mode_res, int num)
 {
@@ -1374,6 +1559,8 @@ drmmode_crtc_init(ScrnInfoPtr pScrn, drmmode_ptr drmmode, drmModeResPtr mode_res
 	crtc->driver_private = drmmode_crtc;
 	drmmode_crtc_hw_id(crtc);
 
+	drmmode_crtc_cm_init(pAMDGPUEnt->fd, crtc);
+
 	/* Mark num'th crtc as in use on this device. */
 	pAMDGPUEnt->assigned_crtcs |= (1 << num);
 	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, AMDGPU_LOGLEVEL_DEBUG,
diff --git a/src/drmmode_display.h b/src/drmmode_display.h
index bbb5423..1293249 100644
--- a/src/drmmode_display.h
+++ b/src/drmmode_display.h
@@ -128,6 +128,14 @@ typedef struct {
 	unsigned present_vblank_msc;
 	Bool present_flip_expected;
 #endif
+
+	uint32_t gamma_lut_size;
+	struct drm_color_lut *gamma_lut;
+
+	uint32_t degamma_lut_size;
+	struct drm_color_lut *degamma_lut;
+
+	struct drm_color_ctm *ctm;
 } drmmode_crtc_private_rec, *drmmode_crtc_private_ptr;
 
 typedef struct {
-- 
2.7.4

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

  parent reply	other threads:[~2018-06-01 16:03 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-01 16:03 [PATCH xf86-video-amdgpu 0/7] Enabling Color Management - Round 3 sunpeng.li-5C7GfCeVMHo
     [not found] ` <1527869017-9209-1-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-01 16:03   ` [PATCH xf86-video-amdgpu 1/7] Cache color property IDs during pre-init sunpeng.li-5C7GfCeVMHo
     [not found]     ` <1527869017-9209-2-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-15 21:02       ` [PATCH xf86-video-amdgpu v2 1/7] Cache color property IDs and LUT sizes " sunpeng.li-5C7GfCeVMHo
2018-06-01 16:03   ` sunpeng.li-5C7GfCeVMHo [this message]
     [not found]     ` <1527869017-9209-3-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-14 16:58       ` [PATCH xf86-video-amdgpu 2/7] Initialize color properties on CRTC during CRTC init Michel Dänzer
2018-06-15 21:04       ` [PATCH xf86-video-amdgpu v2 " sunpeng.li-5C7GfCeVMHo
2018-06-01 16:03   ` [PATCH xf86-video-amdgpu 3/7] Configure color properties when creating output resources sunpeng.li-5C7GfCeVMHo
     [not found]     ` <1527869017-9209-4-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-14 16:58       ` Michel Dänzer
2018-06-15 21:04       ` [PATCH xf86-video-amdgpu v2 " sunpeng.li-5C7GfCeVMHo
2018-06-01 16:03   ` [PATCH xf86-video-amdgpu 4/7] Update color properties on output_get_property sunpeng.li-5C7GfCeVMHo
     [not found]     ` <1527869017-9209-5-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-15 21:05       ` [PATCH xf86-video-amdgpu v2 " sunpeng.li-5C7GfCeVMHo
2018-06-01 16:03   ` [PATCH xf86-video-amdgpu 5/7] Enable setting of color properties via RandR sunpeng.li-5C7GfCeVMHo
     [not found]     ` <1527869017-9209-6-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-15 21:05       ` [PATCH xf86-video-amdgpu v2 " sunpeng.li-5C7GfCeVMHo
2018-06-01 16:03   ` [PATCH xf86-video-amdgpu 6/7] Compose non-legacy with legacy regamma LUT sunpeng.li-5C7GfCeVMHo
     [not found]     ` <1527869017-9209-7-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-08 17:21       ` [PATCH xf86-video-amdgpu 6/7 v2] " sunpeng.li-5C7GfCeVMHo
     [not found]         ` <1528478484-31177-1-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-15 21:12           ` [PATCH xf86-video-amdgpu v3 6/7] " sunpeng.li-5C7GfCeVMHo
2018-06-01 16:03   ` [PATCH xf86-video-amdgpu 7/7] Also compose LUT when setting legacy gamma sunpeng.li-5C7GfCeVMHo
     [not found]     ` <1527869017-9209-8-git-send-email-sunpeng.li-5C7GfCeVMHo@public.gmane.org>
2018-06-15 21:05       ` [PATCH xf86-video-amdgpu v2 " sunpeng.li-5C7GfCeVMHo
2018-06-06 16:01   ` [PATCH xf86-video-amdgpu 0/7] Enabling Color Management - Round 3 Michel Dänzer
     [not found]     ` <e0a71df1-265b-cb62-1c1f-567b26808b71-otUistvHUpPR7s880joybQ@public.gmane.org>
2018-06-06 17:03       ` Michel Dänzer
     [not found]         ` <e47b6ff4-9696-bf20-d299-687b96ab0437-otUistvHUpPR7s880joybQ@public.gmane.org>
2018-06-06 17:22           ` Leo Li
2018-06-07  7:36           ` Michel Dänzer
2018-06-07 22:21           ` Leo Li
     [not found]             ` <9392443d-13d4-c1c2-c07b-67a2b6c72556-5C7GfCeVMHo@public.gmane.org>
2018-06-08 14:33               ` Michel Dänzer
     [not found]                 ` <8807a407-352d-3caa-108a-95aadcd6b414-otUistvHUpPR7s880joybQ@public.gmane.org>
2018-06-08 14:37                   ` Michel Dänzer
2018-06-07  7:22       ` Mike Lothian
     [not found]         ` <CAHbf0-GW9MDmTBDepJjG-WXdqNPhsHWwQMvHo1Sma+JCDy-1dQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2018-06-07  7:31           ` Michel Dänzer
     [not found]             ` <64b1e3fc-d267-b1fc-032a-86aebfe2ff1d-otUistvHUpPR7s880joybQ@public.gmane.org>
2018-06-07 13:26               ` Mike Lothian
     [not found]                 ` <CAHbf0-E=KyYDVj-L5fknEPGL6kUeP--bDOuPXUz5oMjciABXYw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2018-06-07 13:55                   ` Michel Dänzer
2018-06-14 16:57   ` Michel Dänzer
     [not found]     ` <afdb23a3-8722-7928-a612-291b42e7f260-otUistvHUpPR7s880joybQ@public.gmane.org>
2018-06-14 19:49       ` Leo Li
     [not found]         ` <a5a043dd-cd40-33bc-1e1d-e3f8980cb69d-5C7GfCeVMHo@public.gmane.org>
2018-06-15  6:57           ` Michel Dänzer
     [not found]             ` <b64b3eea-5780-ef62-2b59-b2e4733ead16-otUistvHUpPR7s880joybQ@public.gmane.org>
2018-06-22 13:41               ` Leo Li
     [not found]                 ` <09c87c7a-76ed-062e-d911-5b188e3ed5cb-5C7GfCeVMHo@public.gmane.org>
2018-06-22 13:49                   ` Michel Dänzer
2018-06-26 15:46                   ` Michel Dänzer
     [not found]                     ` <d977b737-6d9d-de47-6a37-763af76efa54-otUistvHUpPR7s880joybQ@public.gmane.org>
2018-06-26 18:16                       ` Harry Wentland

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1527869017-9209-3-git-send-email-sunpeng.li@amd.com \
    --to=sunpeng.li-5c7gfcevmho@public.gmane.org \
    --cc=amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
    --cc=harry.wentland-5C7GfCeVMHo@public.gmane.org \
    --cc=michel-otUistvHUpPR7s880joybQ@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox