[PATCH v2 5/6] drm: add drm_fb_xrgb8888_to_rgb888() function to drm_fb_helper.c

Gerd Hoffmann
2019-04-04T15:24:39Z

Like drm_fb_xrgb8888_to_rgb565() but converts to 24bpp (DRM_FORMAT_RGB888).

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
include/drm/drm_fb_helper.h | 4 +++
drivers/gpu/drm/drm_fb_helper.c | 49 +++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+)

diff --git a/include/drm/drm_fb_helper.h b/include/drm/drm_fb_helper.h
index 1406057e1a93..a6e56fcd948e 100644
--- a/include/drm/drm_fb_helper.h
+++ b/include/drm/drm_fb_helper.h
@@ -648,5 +648,9 @@ void drm_fb_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
struct drm_framebuffer *fb,
struct drm_rect *clip,
bool swap, bool dstclip);
+void drm_fb_xrgb8888_to_rgb888(u8 *dst, void *vaddr,
+ struct drm_framebuffer *fb,
+ struct drm_rect *clip,
+ bool dstclip);

#endif
diff --git a/drivers/gpu/drm/drm_fb_helper.c b/drivers/gpu/drm/drm_fb_helper.c
index 2c9286702c3f..6ef0ad4f40b1 100644
--- a/drivers/gpu/drm/drm_fb_helper.c
+++ b/drivers/gpu/drm/drm_fb_helper.c
@@ -3431,3 +3431,52 @@ void drm_fb_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
kfree(buf);
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);
+
+/**
+ * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
+ * @dst: RGB565 destination buffer
+ * @vaddr: XRGB8888 source buffer
+ * @fb: DRM framebuffer
+ * @clip: Clip rectangle area to copy
+ * @dstclip: Clip destination too.
+ *
+ * Drivers can use this function for RGB888 devices that don't natively
+ * support XRGB8888.
+ */
+void drm_fb_xrgb8888_to_rgb888(u8 *dst, void *vaddr,
+ struct drm_framebuffer *fb,
+ struct drm_rect *clip,
+ bool dstclip)
+{
+ size_t len = (clip->x2 - clip->x1) * sizeof(u32);
+ unsigned int x, y;
+ u32 *src, *buf;
+ u8 red, green, blue;
+
+ buf = kmalloc(len, GFP_KERNEL);
+ if (!buf)
+ return;
+
+ if (dstclip)
+ dst += (clip->y1 * fb->width + clip->x1) * 3;
+ for (y = clip->y1; y < clip->y2; y++) {
+ src = vaddr + (y * fb->pitches[0]);
+ src += clip->x1;
+ memcpy(buf, src, len);
+ src = buf;
+ for (x = clip->x1; x < clip->x2; x++) {
+ red = (*src & 0x00FF0000) >> 16;
+ green = (*src & 0x0000FF00) >> 8;
+ blue = (*src & 0x000000FF) >> 0;
+ src++;
+ *dst++ = blue;
+ *dst++ = green;
+ *dst++ = red;
+ }
+ if (dstclip)
+ dst += (fb->width - (clip->x2 - clip->x1)) * 3;
+ }
+
+ kfree(buf);
+}
+EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
--
2.18.1