[libdrm] xf86drmMode: Add RGBA property helpers

Submitted by Matt Roper on Oct. 23, 2015, 12:26 a.m.

Details

Message ID 1445559996-17342-1-git-send-email-matthew.d.roper@intel.com
State New
Headers show

Not browsing as part of any series.

Patch hide | download patch | download mbox

diff --git a/xf86drmMode.c b/xf86drmMode.c
index ab6b519..4bfd419 100644
--- a/xf86drmMode.c
+++ b/xf86drmMode.c
@@ -1445,3 +1445,39 @@  drmModeDestroyPropertyBlob(int fd, uint32_t id)
 	destroy.blob_id = id;
 	return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy);
 }
+
+/*
+ * Builds an RGBA 16bpc color value with bits laid out in the format expected
+ * by DRM RGBA properties.  @bpc is the number of bits per component value
+ * being provided as parameters.
+ */
+uint64_t
+drmModeRGBA(unsigned bpc,
+	    uint16_t red,
+	    uint16_t green,
+	    uint16_t blue,
+	    uint16_t alpha)
+{
+	int shift;
+	uint64_t val;
+
+	if (bpc > 16)
+		return -ERANGE;
+
+	/*
+	 * If we were provided with fewer than 16 bpc, shift the value we
+	 * received into the most significant bits.
+	 */
+	shift = 16 - bpc;
+
+	val = red << shift;
+	val <<= 16;
+	val |= green << shift;
+	val <<= 16;
+	val |= blue << shift;
+	val <<= 16;
+	val |= alpha << shift;
+
+	return val;
+}
+
diff --git a/xf86drmMode.h b/xf86drmMode.h
index 4de7bbb..4aa4917 100644
--- a/xf86drmMode.h
+++ b/xf86drmMode.h
@@ -507,6 +507,13 @@  extern int drmModeCreatePropertyBlob(int fd, const void *data, size_t size,
 				     uint32_t *id);
 extern int drmModeDestroyPropertyBlob(int fd, uint32_t id);
 
+extern uint64_t drmModeRGBA(unsigned bpc,
+			    uint16_t red,
+			    uint16_t green,
+			    uint16_t blue,
+			    uint16_t alpha);
+#define DRM_RGBA8888(r, g, b, a)     drmModeRGBA(8, r, g, b, a)
+#define DRM_RGBA16161616(r, g, b, a) drmModeRGBA(16, r, g, b, a)
 
 #if defined(__cplusplus)
 }