Skip to content

Commit 379ea9a

Browse files
committed
drm/tinydrm: Add tinydrm_xrgb8888_to_gray8() helper
Drm has no monochrome or greyscale support so add a conversion from the common format XR24. Also reorder includes into the common order. Acked-by: Daniel Vetter <[email protected]> Signed-off-by: Noralf Trønnes <[email protected]> Link: http://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 7f0dc77 commit 379ea9a

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

drivers/gpu/drm/tinydrm/core/tinydrm-helpers.c

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
* (at your option) any later version.
88
*/
99

10-
#include <drm/tinydrm/tinydrm.h>
11-
#include <drm/tinydrm/tinydrm-helpers.h>
1210
#include <linux/backlight.h>
11+
#include <linux/dma-buf.h>
1312
#include <linux/pm.h>
1413
#include <linux/spi/spi.h>
1514
#include <linux/swab.h>
1615

16+
#include <drm/tinydrm/tinydrm.h>
17+
#include <drm/tinydrm/tinydrm-helpers.h>
18+
1719
static unsigned int spi_max;
1820
module_param(spi_max, uint, 0400);
1921
MODULE_PARM_DESC(spi_max, "Set a lower SPI max transfer size");
@@ -180,6 +182,74 @@ void tinydrm_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
180182
}
181183
EXPORT_SYMBOL(tinydrm_xrgb8888_to_rgb565);
182184

185+
/**
186+
* tinydrm_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
187+
* @dst: 8-bit grayscale destination buffer
188+
* @fb: DRM framebuffer
189+
*
190+
* Drm doesn't have native monochrome or grayscale support.
191+
* Such drivers can announce the commonly supported XR24 format to userspace
192+
* and use this function to convert to the native format.
193+
*
194+
* Monochrome drivers will use the most significant bit,
195+
* where 1 means foreground color and 0 background color.
196+
*
197+
* ITU BT.601 is used for the RGB -> luma (brightness) conversion.
198+
*
199+
* Returns:
200+
* Zero on success, negative error code on failure.
201+
*/
202+
int tinydrm_xrgb8888_to_gray8(u8 *dst, struct drm_framebuffer *fb)
203+
{
204+
struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
205+
struct dma_buf_attachment *import_attach = cma_obj->base.import_attach;
206+
unsigned int x, y, pitch = fb->pitches[0];
207+
int ret = 0;
208+
void *buf;
209+
u32 *src;
210+
211+
if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
212+
return -EINVAL;
213+
/*
214+
* The cma memory is write-combined so reads are uncached.
215+
* Speed up by fetching one line at a time.
216+
*/
217+
buf = kmalloc(pitch, GFP_KERNEL);
218+
if (!buf)
219+
return -ENOMEM;
220+
221+
if (import_attach) {
222+
ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
223+
DMA_FROM_DEVICE);
224+
if (ret)
225+
goto err_free;
226+
}
227+
228+
for (y = 0; y < fb->height; y++) {
229+
src = cma_obj->vaddr + (y * pitch);
230+
memcpy(buf, src, pitch);
231+
src = buf;
232+
for (x = 0; x < fb->width; x++) {
233+
u8 r = (*src & 0x00ff0000) >> 16;
234+
u8 g = (*src & 0x0000ff00) >> 8;
235+
u8 b = *src & 0x000000ff;
236+
237+
/* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
238+
*dst++ = (3 * r + 6 * g + b) / 10;
239+
src++;
240+
}
241+
}
242+
243+
if (import_attach)
244+
ret = dma_buf_end_cpu_access(import_attach->dmabuf,
245+
DMA_FROM_DEVICE);
246+
err_free:
247+
kfree(buf);
248+
249+
return ret;
250+
}
251+
EXPORT_SYMBOL(tinydrm_xrgb8888_to_gray8);
252+
183253
/**
184254
* tinydrm_of_find_backlight - Find backlight device in device-tree
185255
* @dev: Device

include/drm/tinydrm/tinydrm-helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void tinydrm_swab16(u16 *dst, void *vaddr, struct drm_framebuffer *fb,
4343
void tinydrm_xrgb8888_to_rgb565(u16 *dst, void *vaddr,
4444
struct drm_framebuffer *fb,
4545
struct drm_clip_rect *clip, bool swap);
46+
int tinydrm_xrgb8888_to_gray8(u8 *dst, struct drm_framebuffer *fb);
4647

4748
struct backlight_device *tinydrm_of_find_backlight(struct device *dev);
4849
int tinydrm_enable_backlight(struct backlight_device *backlight);

0 commit comments

Comments
 (0)