mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ho Jie Feng <hjf3108@gmail.com>
To: dri-devel@lists.freedesktop.org
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v3 1/4] drm/format-helper: Add XRGB8888 to UYVY conversion
Date: Sun,  6 Dec 2026 16:02:23 +0800	[thread overview]
Message-ID: <20261206080229.102629-2-hjf3108@gmail.com> (raw)
In-Reply-To: <20261206080229.102629-1-hjf3108@gmail.com>

Add a helper for devices that use packed UYVY but accept XRGB8888
framebuffers. Read conversion input through iosys_map so imported
framebuffers mapped in I/O memory can use the same helper.

Signed-off-by: Ho Jie Feng <hjf3108@gmail.com>
---
 drivers/gpu/drm/drm_format_helper.c   | 102 +++++++++++++++++++-------
 drivers/gpu/drm/drm_format_internal.h |  19 +++++
 include/drm/drm_format_helper.h       |   4 +
 3 files changed, 99 insertions(+), 26 deletions(-)

diff --git a/drivers/gpu/drm/drm_format_helper.c b/drivers/gpu/drm/drm_format_helper.c
index 6cddf05c493b..cd10e78fb0f3 100644
--- a/drivers/gpu/drm/drm_format_helper.c
+++ b/drivers/gpu/drm/drm_format_helper.c
@@ -137,7 +137,7 @@ EXPORT_SYMBOL(drm_fb_clip_offset);
 
 /* TODO: Make this function work with multi-plane formats. */
 static int __drm_fb_xfrm(void *dst, unsigned long dst_pitch, unsigned long dst_pixsize,
-			 const void *vaddr, const struct drm_framebuffer *fb,
+			 const struct iosys_map *src, const struct drm_framebuffer *fb,
 			 const struct drm_rect *clip, bool vaddr_cached_hint,
 			 struct drm_format_conv_state *state,
 			 void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
@@ -145,6 +145,7 @@ static int __drm_fb_xfrm(void *dst, unsigned long dst_pitch, unsigned long dst_p
 	unsigned long linepixels = drm_rect_width(clip);
 	unsigned long lines = drm_rect_height(clip);
 	size_t sbuf_len = linepixels * fb->format->cpp[0];
+	size_t offset = drm_fb_clip_offset(fb->pitches[0], fb->format, clip);
 	void *stmp = NULL;
 	unsigned long i;
 	const void *sbuf;
@@ -154,7 +155,7 @@ static int __drm_fb_xfrm(void *dst, unsigned long dst_pitch, unsigned long dst_p
 	 * caching, so reads are uncached. Speed up access by fetching
 	 * one line at a time.
 	 */
-	if (!vaddr_cached_hint) {
+	if (!vaddr_cached_hint || src->is_iomem) {
 		stmp = drm_format_conv_state_reserve(state, sbuf_len, GFP_KERNEL);
 		if (!stmp)
 			return -ENOMEM;
@@ -162,15 +163,16 @@ static int __drm_fb_xfrm(void *dst, unsigned long dst_pitch, unsigned long dst_p
 
 	if (!dst_pitch)
 		dst_pitch = drm_rect_width(clip) * dst_pixsize;
-	vaddr += clip_offset(clip, fb->pitches[0], fb->format->cpp[0]);
 
 	for (i = 0; i < lines; ++i) {
-		if (stmp)
-			sbuf = memcpy(stmp, vaddr, sbuf_len);
-		else
-			sbuf = vaddr;
+		if (stmp) {
+			iosys_map_memcpy_from(stmp, src, offset, sbuf_len);
+			sbuf = stmp;
+		} else {
+			sbuf = src->vaddr + offset;
+		}
 		xfrm_line(dst, sbuf, linepixels);
-		vaddr += fb->pitches[0];
+		offset += fb->pitches[0];
 		dst += dst_pitch;
 	}
 
@@ -179,7 +181,7 @@ static int __drm_fb_xfrm(void *dst, unsigned long dst_pitch, unsigned long dst_p
 
 /* TODO: Make this function work with multi-plane formats. */
 static int __drm_fb_xfrm_toio(void __iomem *dst, unsigned long dst_pitch, unsigned long dst_pixsize,
-			      const void *vaddr, const struct drm_framebuffer *fb,
+			      const struct iosys_map *src, const struct drm_framebuffer *fb,
 			      const struct drm_rect *clip, bool vaddr_cached_hint,
 			      struct drm_format_conv_state *state,
 			      void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
@@ -189,32 +191,33 @@ static int __drm_fb_xfrm_toio(void __iomem *dst, unsigned long dst_pitch, unsign
 	size_t dbuf_len = linepixels * dst_pixsize;
 	size_t stmp_off = round_up(dbuf_len, ARCH_KMALLOC_MINALIGN); /* for sbuf alignment */
 	size_t sbuf_len = linepixels * fb->format->cpp[0];
+	size_t offset = drm_fb_clip_offset(fb->pitches[0], fb->format, clip);
+	bool need_stmp = !vaddr_cached_hint || src->is_iomem;
 	void *stmp = NULL;
 	unsigned long i;
 	const void *sbuf;
 	void *dbuf;
 
-	if (vaddr_cached_hint) {
-		dbuf = drm_format_conv_state_reserve(state, dbuf_len, GFP_KERNEL);
-	} else {
-		dbuf = drm_format_conv_state_reserve(state, stmp_off + sbuf_len, GFP_KERNEL);
-		stmp = dbuf + stmp_off;
-	}
+	dbuf = drm_format_conv_state_reserve(state, need_stmp ? stmp_off + sbuf_len : dbuf_len,
+					     GFP_KERNEL);
 	if (!dbuf)
 		return -ENOMEM;
+	if (need_stmp)
+		stmp = dbuf + stmp_off;
 
 	if (!dst_pitch)
 		dst_pitch = linepixels * dst_pixsize;
-	vaddr += clip_offset(clip, fb->pitches[0], fb->format->cpp[0]);
 
 	for (i = 0; i < lines; ++i) {
-		if (stmp)
-			sbuf = memcpy(stmp, vaddr, sbuf_len);
-		else
-			sbuf = vaddr;
+		if (stmp) {
+			iosys_map_memcpy_from(stmp, src, offset, sbuf_len);
+			sbuf = stmp;
+		} else {
+			sbuf = src->vaddr + offset;
+		}
 		xfrm_line(dbuf, sbuf, linepixels);
 		memcpy_toio(dst, dbuf, dbuf_len);
-		vaddr += fb->pitches[0];
+		offset += fb->pitches[0];
 		dst += dst_pitch;
 	}
 
@@ -236,15 +239,12 @@ static int drm_fb_xfrm(struct iosys_map *dst,
 	if (!dst_pitch)
 		dst_pitch = default_dst_pitch;
 
-	/* TODO: handle src in I/O memory here */
 	if (dst[0].is_iomem)
 		return __drm_fb_xfrm_toio(dst[0].vaddr_iomem, dst_pitch[0], dst_pixsize[0],
-					  src[0].vaddr, fb, clip, vaddr_cached_hint, state,
-					  xfrm_line);
+					  src, fb, clip, vaddr_cached_hint, state, xfrm_line);
 	else
 		return __drm_fb_xfrm(dst[0].vaddr, dst_pitch[0], dst_pixsize[0],
-				     src[0].vaddr, fb, clip, vaddr_cached_hint, state,
-				     xfrm_line);
+				     src, fb, clip, vaddr_cached_hint, state, xfrm_line);
 }
 
 #define ALIGN_DOWN_PIXELS(end, n, a) \
@@ -1379,3 +1379,53 @@ void drm_fb_xrgb8888_to_gray2(struct iosys_map *dst, const unsigned int *dst_pit
 }
 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray2);
 
+static void drm_fb_xrgb8888_to_uyvy_line(void *dbuf, const void *sbuf, unsigned int pixels)
+{
+	__le32 *dbuf32 = dbuf;
+	const __le32 *sbuf32 = sbuf;
+	const __le32 *send32 = sbuf32 + pixels;
+
+	while (sbuf32 < send32) {
+		u32 pix1 = le32_to_cpup(sbuf32);
+		u32 pix2 = le32_to_cpup(sbuf32 + 1);
+		u32 val32 = drm_pixel_xrgb8888_to_uyvy_bt601(pix1, pix2);
+
+		*dbuf32++ = cpu_to_le32(val32);
+		sbuf32 += 2;
+	}
+}
+
+/**
+ * drm_fb_xrgb8888_to_uyvy - Convert XRGB8888 to UYVY clip buffer
+ * @dst: Array of UYVY destination buffers
+ * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
+ *             within @dst; can be NULL if scanlines are stored next to each other.
+ * @src: Array of XRGB8888 source buffers
+ * @fb: DRM framebuffer
+ * @clip: Clip rectangle area to copy; x1 and x2 must be even
+ * @state: Transform and conversion state
+ *
+ * This function copies parts of a framebuffer to display memory and converts the
+ * color format during the process. The parameters @dst, @dst_pitch and @src refer
+ * to arrays. Each array must have at least as many entries as there are planes in
+ * @fb's format. Each entry stores the value for the format's respective color plane
+ * at the same index.
+ *
+ * This function does not apply clipping on @dst (i.e. the destination is at the
+ * top-left corner).
+ *
+ * Drivers can use this function for UYVY devices that don't support XRGB8888
+ * natively. ITU BT.601 is being used for the RGB -> YUV conversion.
+ */
+void drm_fb_xrgb8888_to_uyvy(struct iosys_map *dst, const unsigned int *dst_pitch,
+			     const struct iosys_map *src, const struct drm_framebuffer *fb,
+			     const struct drm_rect *clip, struct drm_format_conv_state *state)
+{
+	static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
+		2,
+	};
+
+	drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
+		    drm_fb_xrgb8888_to_uyvy_line);
+}
+EXPORT_SYMBOL(drm_fb_xrgb8888_to_uyvy);
diff --git a/drivers/gpu/drm/drm_format_internal.h b/drivers/gpu/drm/drm_format_internal.h
index ce29dd05bcc5..41875656e59e 100644
--- a/drivers/gpu/drm/drm_format_internal.h
+++ b/drivers/gpu/drm/drm_format_internal.h
@@ -159,6 +159,25 @@ static inline u32 drm_pixel_xrgb8888_to_abgr2101010(u32 pix)
 	       drm_pixel_xrgb8888_to_xbgr2101010(pix);
 }
 
+static inline u32 drm_pixel_xrgb8888_to_uyvy_bt601(u32 pix1, u32 pix2)
+{
+	u32 r1 = (pix1 & 0x00ff0000) >> 16;
+	u32 g1 = (pix1 & 0x0000ff00) >> 8;
+	u32 b1 =  pix1 & 0x000000ff;
+	u32 r2 = (pix2 & 0x00ff0000) >> 16;
+	u32 g2 = (pix2 & 0x0000ff00) >> 8;
+	u32 b2 =  pix2 & 0x000000ff;
+	u32 r = r1 + r2;
+	u32 g = g1 + g2;
+	u32 b = b1 + b2;
+	u32 y1 = ((16U << 16) + 16829 * r1 + 33039 * g1 + 6416 * b1 + 32768) >> 16;
+	u32 y2 = ((16U << 16) + 16829 * r2 + 33039 * g2 + 6416 * b2 + 32768) >> 16;
+	u32 u = ((128U << 17) - 9714 * r - 19071 * g + 28784 * b + 65536) >> 17;
+	u32 v = ((128U << 17) + 28784 * r - 24103 * g - 4681 * b + 65536) >> 17;
+
+	return u | (y1 << 8) | (v << 16) | (y2 << 24);
+}
+
 /*
  * Conversion from ARGB8888
  */
diff --git a/include/drm/drm_format_helper.h b/include/drm/drm_format_helper.h
index 2b5c1aef80b0..e50f96e07484 100644
--- a/include/drm/drm_format_helper.h
+++ b/include/drm/drm_format_helper.h
@@ -136,4 +136,8 @@ void drm_fb_xrgb8888_to_gray2(struct iosys_map *dst, const unsigned int *dst_pit
 			      const struct iosys_map *src, const struct drm_framebuffer *fb,
 			      const struct drm_rect *clip, struct drm_format_conv_state *state);
 
+void drm_fb_xrgb8888_to_uyvy(struct iosys_map *dst, const unsigned int *dst_pitch,
+			     const struct iosys_map *src, const struct drm_framebuffer *fb,
+			     const struct drm_rect *clip, struct drm_format_conv_state *state);
+
 #endif /* __LINUX_DRM_FORMAT_HELPER_H */
-- 
2.55.0


       reply	other threads:[~2026-12-06  8:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261206080229.102629-1-hjf3108@gmail.com>
2026-12-06  8:02 ` Ho Jie Feng [this message]
2026-12-06  8:02 ` [PATCH v3 2/4] drm/ms91xx: Add register access and connector support Ho Jie Feng
2026-12-06  8:02 ` [PATCH v3 3/4] drm/ms91xx: Add atomic modesetting and USB framebuffer transfers Ho Jie Feng
2026-12-06  8:02 ` [PATCH v3 4/4] drm/ms91xx: Add Kconfig and Makefile integration Ho Jie Feng

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=20261206080229.102629-2-hjf3108@gmail.com \
    --to=hjf3108@gmail.com \
    --cc=airlied@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tzimmermann@suse.de \
    /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

all inboxes | Powered by JetHome®