Add a helper for devices that use packed UYVY but accept XRGB8888
framebuffers.

Signed-off-by: Ho Jie Feng <[email protected]>
---
 drivers/gpu/drm/drm_format_helper.c   | 50 +++++++++++++++++++++++++++
 drivers/gpu/drm/drm_format_internal.h | 19 ++++++++++
 include/drm/drm_format_helper.h       |  4 +++
 3 files changed, 73 insertions(+)

diff --git a/drivers/gpu/drm/drm_format_helper.c 
b/drivers/gpu/drm/drm_format_helper.c
index 6cddf05c4..7e5cbb605 100644
--- a/drivers/gpu/drm/drm_format_helper.c
+++ b/drivers/gpu/drm/drm_format_helper.c
@@ -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 ce29dd05b..41875656e 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 2b5c1aef8..e50f96e07 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 via email to