1 // SPDX-License-Identifier: GPL-2.0 or MIT
3 * Copyright (C) 2016 Noralf Trønnes
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
12 #include <linux/iosys-map.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <drm/drm_device.h>
17 #include <drm/drm_format_helper.h>
18 #include <drm/drm_framebuffer.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_print.h>
21 #include <drm/drm_rect.h>
23 static unsigned int clip_offset(const struct drm_rect *clip, unsigned int pitch, unsigned int cpp)
25 return clip->y1 * pitch + clip->x1 * cpp;
29 * drm_fb_clip_offset - Returns the clipping rectangles byte-offset in a framebuffer
30 * @pitch: Framebuffer line pitch in byte
31 * @format: Framebuffer format
32 * @clip: Clip rectangle
35 * The byte offset of the clip rectangle's top-left corner within the framebuffer.
37 unsigned int drm_fb_clip_offset(unsigned int pitch, const struct drm_format_info *format,
38 const struct drm_rect *clip)
40 return clip_offset(clip, pitch, format->cpp[0]);
42 EXPORT_SYMBOL(drm_fb_clip_offset);
44 /* TODO: Make this function work with multi-plane formats. */
45 static int __drm_fb_xfrm(void *dst, unsigned long dst_pitch, unsigned long dst_pixsize,
46 const void *vaddr, const struct drm_framebuffer *fb,
47 const struct drm_rect *clip, bool vaddr_cached_hint,
48 void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
50 unsigned long linepixels = drm_rect_width(clip);
51 unsigned long lines = drm_rect_height(clip);
52 size_t sbuf_len = linepixels * fb->format->cpp[0];
58 * Some source buffers, such as DMA memory, use write-combine
59 * caching, so reads are uncached. Speed up access by fetching
62 if (!vaddr_cached_hint) {
63 stmp = kmalloc(sbuf_len, GFP_KERNEL);
69 dst_pitch = drm_rect_width(clip) * dst_pixsize;
70 vaddr += clip_offset(clip, fb->pitches[0], fb->format->cpp[0]);
72 for (i = 0; i < lines; ++i) {
74 sbuf = memcpy(stmp, vaddr, sbuf_len);
77 xfrm_line(dst, sbuf, linepixels);
78 vaddr += fb->pitches[0];
87 /* TODO: Make this function work with multi-plane formats. */
88 static int __drm_fb_xfrm_toio(void __iomem *dst, unsigned long dst_pitch, unsigned long dst_pixsize,
89 const void *vaddr, const struct drm_framebuffer *fb,
90 const struct drm_rect *clip, bool vaddr_cached_hint,
91 void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
93 unsigned long linepixels = drm_rect_width(clip);
94 unsigned long lines = drm_rect_height(clip);
95 size_t dbuf_len = linepixels * dst_pixsize;
96 size_t stmp_off = round_up(dbuf_len, ARCH_KMALLOC_MINALIGN); /* for sbuf alignment */
97 size_t sbuf_len = linepixels * fb->format->cpp[0];
103 if (vaddr_cached_hint) {
104 dbuf = kmalloc(dbuf_len, GFP_KERNEL);
106 dbuf = kmalloc(stmp_off + sbuf_len, GFP_KERNEL);
107 stmp = dbuf + stmp_off;
113 dst_pitch = linepixels * dst_pixsize;
114 vaddr += clip_offset(clip, fb->pitches[0], fb->format->cpp[0]);
116 for (i = 0; i < lines; ++i) {
118 sbuf = memcpy(stmp, vaddr, sbuf_len);
121 xfrm_line(dbuf, sbuf, linepixels);
122 memcpy_toio(dst, dbuf, dbuf_len);
123 vaddr += fb->pitches[0];
132 /* TODO: Make this function work with multi-plane formats. */
133 static int drm_fb_xfrm(struct iosys_map *dst,
134 const unsigned int *dst_pitch, const u8 *dst_pixsize,
135 const struct iosys_map *src, const struct drm_framebuffer *fb,
136 const struct drm_rect *clip, bool vaddr_cached_hint,
137 void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels))
139 static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
144 dst_pitch = default_dst_pitch;
146 /* TODO: handle src in I/O memory here */
148 return __drm_fb_xfrm_toio(dst[0].vaddr_iomem, dst_pitch[0], dst_pixsize[0],
149 src[0].vaddr, fb, clip, vaddr_cached_hint, xfrm_line);
151 return __drm_fb_xfrm(dst[0].vaddr, dst_pitch[0], dst_pixsize[0],
152 src[0].vaddr, fb, clip, vaddr_cached_hint, xfrm_line);
156 * drm_fb_memcpy - Copy clip buffer
157 * @dst: Array of destination buffers
158 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
159 * within @dst; can be NULL if scanlines are stored next to each other.
160 * @src: Array of source buffers
161 * @fb: DRM framebuffer
162 * @clip: Clip rectangle area to copy
164 * This function copies parts of a framebuffer to display memory. Destination and
165 * framebuffer formats must match. No conversion takes place. The parameters @dst,
166 * @dst_pitch and @src refer to arrays. Each array must have at least as many entries
167 * as there are planes in @fb's format. Each entry stores the value for the format's
168 * respective color plane at the same index.
170 * This function does not apply clipping on @dst (i.e. the destination is at the
173 void drm_fb_memcpy(struct iosys_map *dst, const unsigned int *dst_pitch,
174 const struct iosys_map *src, const struct drm_framebuffer *fb,
175 const struct drm_rect *clip)
177 static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
181 const struct drm_format_info *format = fb->format;
182 unsigned int i, y, lines = drm_rect_height(clip);
185 dst_pitch = default_dst_pitch;
187 for (i = 0; i < format->num_planes; ++i) {
188 unsigned int bpp_i = drm_format_info_bpp(format, i);
189 unsigned int cpp_i = DIV_ROUND_UP(bpp_i, 8);
190 size_t len_i = DIV_ROUND_UP(drm_rect_width(clip) * bpp_i, 8);
191 unsigned int dst_pitch_i = dst_pitch[i];
192 struct iosys_map dst_i = dst[i];
193 struct iosys_map src_i = src[i];
198 iosys_map_incr(&src_i, clip_offset(clip, fb->pitches[i], cpp_i));
199 for (y = 0; y < lines; y++) {
200 /* TODO: handle src_i in I/O memory here */
201 iosys_map_memcpy_to(&dst_i, 0, src_i.vaddr, len_i);
202 iosys_map_incr(&src_i, fb->pitches[i]);
203 iosys_map_incr(&dst_i, dst_pitch_i);
207 EXPORT_SYMBOL(drm_fb_memcpy);
209 static void drm_fb_swab16_line(void *dbuf, const void *sbuf, unsigned int pixels)
212 const u16 *sbuf16 = sbuf;
213 const u16 *send16 = sbuf16 + pixels;
215 while (sbuf16 < send16)
216 *dbuf16++ = swab16(*sbuf16++);
219 static void drm_fb_swab32_line(void *dbuf, const void *sbuf, unsigned int pixels)
222 const u32 *sbuf32 = sbuf;
223 const u32 *send32 = sbuf32 + pixels;
225 while (sbuf32 < send32)
226 *dbuf32++ = swab32(*sbuf32++);
230 * drm_fb_swab - Swap bytes into clip buffer
231 * @dst: Array of destination buffers
232 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
233 * within @dst; can be NULL if scanlines are stored next to each other.
234 * @src: Array of source buffers
235 * @fb: DRM framebuffer
236 * @clip: Clip rectangle area to copy
237 * @cached: Source buffer is mapped cached (eg. not write-combined)
239 * This function copies parts of a framebuffer to display memory and swaps per-pixel
240 * bytes during the process. Destination and framebuffer formats must match. The
241 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
242 * least as many entries as there are planes in @fb's format. Each entry stores the
243 * value for the format's respective color plane at the same index. If @cached is
244 * false a temporary buffer is used to cache one pixel line at a time to speed up
245 * slow uncached reads.
247 * This function does not apply clipping on @dst (i.e. the destination is at the
250 void drm_fb_swab(struct iosys_map *dst, const unsigned int *dst_pitch,
251 const struct iosys_map *src, const struct drm_framebuffer *fb,
252 const struct drm_rect *clip, bool cached)
254 const struct drm_format_info *format = fb->format;
255 u8 cpp = DIV_ROUND_UP(drm_format_info_bpp(format, 0), 8);
256 void (*swab_line)(void *dbuf, const void *sbuf, unsigned int npixels);
260 swab_line = drm_fb_swab32_line;
263 swab_line = drm_fb_swab16_line;
266 drm_warn_once(fb->dev, "Format %p4cc has unsupported pixel size.\n",
271 drm_fb_xfrm(dst, dst_pitch, &cpp, src, fb, clip, cached, swab_line);
273 EXPORT_SYMBOL(drm_fb_swab);
275 static void drm_fb_xrgb8888_to_rgb332_line(void *dbuf, const void *sbuf, unsigned int pixels)
278 const __le32 *sbuf32 = sbuf;
282 for (x = 0; x < pixels; x++) {
283 pix = le32_to_cpu(sbuf32[x]);
284 dbuf8[x] = ((pix & 0x00e00000) >> 16) |
285 ((pix & 0x0000e000) >> 11) |
286 ((pix & 0x000000c0) >> 6);
291 * drm_fb_xrgb8888_to_rgb332 - Convert XRGB8888 to RGB332 clip buffer
292 * @dst: Array of RGB332 destination buffers
293 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
294 * within @dst; can be NULL if scanlines are stored next to each other.
295 * @src: Array of XRGB8888 source buffers
296 * @fb: DRM framebuffer
297 * @clip: Clip rectangle area to copy
299 * This function copies parts of a framebuffer to display memory and converts the
300 * color format during the process. Destination and framebuffer formats must match. The
301 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
302 * least as many entries as there are planes in @fb's format. Each entry stores the
303 * value for the format's respective color plane at the same index.
305 * This function does not apply clipping on @dst (i.e. the destination is at the
308 * Drivers can use this function for RGB332 devices that don't support XRGB8888 natively.
310 void drm_fb_xrgb8888_to_rgb332(struct iosys_map *dst, const unsigned int *dst_pitch,
311 const struct iosys_map *src, const struct drm_framebuffer *fb,
312 const struct drm_rect *clip)
314 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
318 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
319 drm_fb_xrgb8888_to_rgb332_line);
321 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb332);
323 static void drm_fb_xrgb8888_to_rgb565_line(void *dbuf, const void *sbuf, unsigned int pixels)
326 const __le32 *sbuf32 = sbuf;
331 for (x = 0; x < pixels; x++) {
332 pix = le32_to_cpu(sbuf32[x]);
333 val16 = ((pix & 0x00F80000) >> 8) |
334 ((pix & 0x0000FC00) >> 5) |
335 ((pix & 0x000000F8) >> 3);
340 static void drm_fb_xrgb8888_to_rgb565_swab_line(void *dbuf, const void *sbuf,
344 const __le32 *sbuf32 = sbuf;
349 for (x = 0; x < pixels; x++) {
350 pix = le32_to_cpu(sbuf32[x]);
351 val16 = ((pix & 0x00F80000) >> 8) |
352 ((pix & 0x0000FC00) >> 5) |
353 ((pix & 0x000000F8) >> 3);
354 dbuf16[x] = swab16(val16);
359 * drm_fb_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
360 * @dst: Array of RGB565 destination buffers
361 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
362 * within @dst; can be NULL if scanlines are stored next to each other.
363 * @src: Array of XRGB8888 source buffer
364 * @fb: DRM framebuffer
365 * @clip: Clip rectangle area to copy
368 * This function copies parts of a framebuffer to display memory and converts the
369 * color format during the process. Destination and framebuffer formats must match. The
370 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
371 * least as many entries as there are planes in @fb's format. Each entry stores the
372 * value for the format's respective color plane at the same index.
374 * This function does not apply clipping on @dst (i.e. the destination is at the
377 * Drivers can use this function for RGB565 devices that don't support XRGB8888 natively.
379 void drm_fb_xrgb8888_to_rgb565(struct iosys_map *dst, const unsigned int *dst_pitch,
380 const struct iosys_map *src, const struct drm_framebuffer *fb,
381 const struct drm_rect *clip, bool swab)
383 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
387 void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels);
390 xfrm_line = drm_fb_xrgb8888_to_rgb565_swab_line;
392 xfrm_line = drm_fb_xrgb8888_to_rgb565_line;
394 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, xfrm_line);
396 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);
398 static void drm_fb_xrgb8888_to_rgb888_line(void *dbuf, const void *sbuf, unsigned int pixels)
401 const __le32 *sbuf32 = sbuf;
405 for (x = 0; x < pixels; x++) {
406 pix = le32_to_cpu(sbuf32[x]);
407 /* write blue-green-red to output in little endianness */
408 *dbuf8++ = (pix & 0x000000FF) >> 0;
409 *dbuf8++ = (pix & 0x0000FF00) >> 8;
410 *dbuf8++ = (pix & 0x00FF0000) >> 16;
415 * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
416 * @dst: Array of RGB888 destination buffers
417 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
418 * within @dst; can be NULL if scanlines are stored next to each other.
419 * @src: Array of XRGB8888 source buffers
420 * @fb: DRM framebuffer
421 * @clip: Clip rectangle area to copy
423 * This function copies parts of a framebuffer to display memory and converts the
424 * color format during the process. Destination and framebuffer formats must match. The
425 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
426 * least as many entries as there are planes in @fb's format. Each entry stores the
427 * value for the format's respective color plane at the same index.
429 * This function does not apply clipping on @dst (i.e. the destination is at the
432 * Drivers can use this function for RGB888 devices that don't natively
435 void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pitch,
436 const struct iosys_map *src, const struct drm_framebuffer *fb,
437 const struct drm_rect *clip)
439 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
443 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
444 drm_fb_xrgb8888_to_rgb888_line);
446 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
448 static void drm_fb_rgb565_to_xrgb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
450 __le32 *dbuf32 = dbuf;
451 const __le16 *sbuf16 = sbuf;
454 for (x = 0; x < pixels; x++) {
455 u16 val16 = le16_to_cpu(sbuf16[x]);
456 u32 val32 = ((val16 & 0xf800) << 8) |
457 ((val16 & 0x07e0) << 5) |
458 ((val16 & 0x001f) << 3);
459 val32 = 0xff000000 | val32 |
460 ((val32 >> 3) & 0x00070007) |
461 ((val32 >> 2) & 0x00000300);
462 dbuf32[x] = cpu_to_le32(val32);
466 static void drm_fb_rgb565_to_xrgb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
467 const struct iosys_map *src,
468 const struct drm_framebuffer *fb,
469 const struct drm_rect *clip)
471 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
475 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
476 drm_fb_rgb565_to_xrgb8888_line);
479 static void drm_fb_rgb888_to_xrgb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
481 __le32 *dbuf32 = dbuf;
482 const u8 *sbuf8 = sbuf;
485 for (x = 0; x < pixels; x++) {
489 u32 pix = 0xff000000 | (r << 16) | (g << 8) | b;
490 dbuf32[x] = cpu_to_le32(pix);
494 static void drm_fb_rgb888_to_xrgb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
495 const struct iosys_map *src,
496 const struct drm_framebuffer *fb,
497 const struct drm_rect *clip)
499 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
503 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
504 drm_fb_rgb888_to_xrgb8888_line);
507 static void drm_fb_xrgb8888_to_xrgb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
509 __le32 *dbuf32 = dbuf;
510 const __le32 *sbuf32 = sbuf;
515 for (x = 0; x < pixels; x++) {
516 pix = le32_to_cpu(sbuf32[x]);
517 val32 = ((pix & 0x000000FF) << 2) |
518 ((pix & 0x0000FF00) << 4) |
519 ((pix & 0x00FF0000) << 6);
520 pix = val32 | ((val32 >> 8) & 0x00300C03);
521 *dbuf32++ = cpu_to_le32(pix);
526 * drm_fb_xrgb8888_to_xrgb2101010 - Convert XRGB8888 to XRGB2101010 clip buffer
527 * @dst: Array of XRGB2101010 destination buffers
528 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
529 * within @dst; can be NULL if scanlines are stored next to each other.
530 * @src: Array of XRGB8888 source buffers
531 * @fb: DRM framebuffer
532 * @clip: Clip rectangle area to copy
534 * This function copies parts of a framebuffer to display memory and converts the
535 * color format during the process. Destination and framebuffer formats must match. The
536 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
537 * least as many entries as there are planes in @fb's format. Each entry stores the
538 * value for the format's respective color plane at the same index.
540 * This function does not apply clipping on @dst (i.e. the destination is at the
543 * Drivers can use this function for XRGB2101010 devices that don't support XRGB8888
546 void drm_fb_xrgb8888_to_xrgb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
547 const struct iosys_map *src, const struct drm_framebuffer *fb,
548 const struct drm_rect *clip)
550 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
554 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
555 drm_fb_xrgb8888_to_xrgb2101010_line);
557 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010);
559 static void drm_fb_xrgb8888_to_gray8_line(void *dbuf, const void *sbuf, unsigned int pixels)
562 const __le32 *sbuf32 = sbuf;
565 for (x = 0; x < pixels; x++) {
566 u32 pix = le32_to_cpu(sbuf32[x]);
567 u8 r = (pix & 0x00ff0000) >> 16;
568 u8 g = (pix & 0x0000ff00) >> 8;
569 u8 b = pix & 0x000000ff;
571 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
572 *dbuf8++ = (3 * r + 6 * g + b) / 10;
577 * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
578 * @dst: Array of 8-bit grayscale destination buffers
579 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
580 * within @dst; can be NULL if scanlines are stored next to each other.
581 * @src: Array of XRGB8888 source buffers
582 * @fb: DRM framebuffer
583 * @clip: Clip rectangle area to copy
585 * This function copies parts of a framebuffer to display memory and converts the
586 * color format during the process. Destination and framebuffer formats must match. The
587 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
588 * least as many entries as there are planes in @fb's format. Each entry stores the
589 * value for the format's respective color plane at the same index.
591 * This function does not apply clipping on @dst (i.e. the destination is at the
594 * DRM doesn't have native monochrome or grayscale support. Drivers can use this
595 * function for grayscale devices that don't support XRGB8888 natively.Such
596 * drivers can announce the commonly supported XR24 format to userspace and use
597 * this function to convert to the native format. Monochrome drivers will use the
598 * most significant bit, where 1 means foreground color and 0 background color.
599 * ITU BT.601 is being used for the RGB -> luma (brightness) conversion.
601 void drm_fb_xrgb8888_to_gray8(struct iosys_map *dst, const unsigned int *dst_pitch,
602 const struct iosys_map *src, const struct drm_framebuffer *fb,
603 const struct drm_rect *clip)
605 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
609 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
610 drm_fb_xrgb8888_to_gray8_line);
612 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);
615 * drm_fb_blit - Copy parts of a framebuffer to display memory
616 * @dst: Array of display-memory addresses to copy to
617 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
618 * within @dst; can be NULL if scanlines are stored next to each other.
619 * @dst_format: FOURCC code of the display's color format
620 * @src: The framebuffer memory to copy from
621 * @fb: The framebuffer to copy from
622 * @clip: Clip rectangle area to copy
624 * This function copies parts of a framebuffer to display memory. If the
625 * formats of the display and the framebuffer mismatch, the blit function
626 * will attempt to convert between them during the process. The parameters @dst,
627 * @dst_pitch and @src refer to arrays. Each array must have at least as many
628 * entries as there are planes in @dst_format's format. Each entry stores the
629 * value for the format's respective color plane at the same index.
631 * This function does not apply clipping on @dst (i.e. the destination is at the
636 * -EINVAL if the color-format conversion failed, or
637 * a negative error code otherwise.
639 int drm_fb_blit(struct iosys_map *dst, const unsigned int *dst_pitch, uint32_t dst_format,
640 const struct iosys_map *src, const struct drm_framebuffer *fb,
641 const struct drm_rect *clip)
643 uint32_t fb_format = fb->format->format;
645 /* treat alpha channel like filler bits */
646 if (fb_format == DRM_FORMAT_ARGB8888)
647 fb_format = DRM_FORMAT_XRGB8888;
648 if (dst_format == DRM_FORMAT_ARGB8888)
649 dst_format = DRM_FORMAT_XRGB8888;
650 if (fb_format == DRM_FORMAT_ARGB2101010)
651 fb_format = DRM_FORMAT_XRGB2101010;
652 if (dst_format == DRM_FORMAT_ARGB2101010)
653 dst_format = DRM_FORMAT_XRGB2101010;
655 if (dst_format == fb_format) {
656 drm_fb_memcpy(dst, dst_pitch, src, fb, clip);
659 } else if (dst_format == DRM_FORMAT_RGB565) {
660 if (fb_format == DRM_FORMAT_XRGB8888) {
661 drm_fb_xrgb8888_to_rgb565(dst, dst_pitch, src, fb, clip, false);
664 } else if (dst_format == (DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN)) {
665 if (fb_format == DRM_FORMAT_RGB565) {
666 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
669 } else if (dst_format == DRM_FORMAT_RGB888) {
670 if (fb_format == DRM_FORMAT_XRGB8888) {
671 drm_fb_xrgb8888_to_rgb888(dst, dst_pitch, src, fb, clip);
674 } else if (dst_format == DRM_FORMAT_XRGB8888) {
675 if (fb_format == DRM_FORMAT_RGB888) {
676 drm_fb_rgb888_to_xrgb8888(dst, dst_pitch, src, fb, clip);
678 } else if (fb_format == DRM_FORMAT_RGB565) {
679 drm_fb_rgb565_to_xrgb8888(dst, dst_pitch, src, fb, clip);
682 } else if (dst_format == DRM_FORMAT_XRGB2101010) {
683 if (fb_format == DRM_FORMAT_XRGB8888) {
684 drm_fb_xrgb8888_to_xrgb2101010(dst, dst_pitch, src, fb, clip);
687 } else if (dst_format == DRM_FORMAT_BGRX8888) {
688 if (fb_format == DRM_FORMAT_XRGB8888) {
689 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
694 drm_warn_once(fb->dev, "No conversion helper from %p4cc to %p4cc found.\n",
695 &fb_format, &dst_format);
699 EXPORT_SYMBOL(drm_fb_blit);
701 static void drm_fb_gray8_to_mono_line(void *dbuf, const void *sbuf, unsigned int pixels)
704 const u8 *sbuf8 = sbuf;
707 unsigned int i, bits = min(pixels, 8U);
710 for (i = 0; i < bits; i++, pixels--) {
719 * drm_fb_xrgb8888_to_mono - Convert XRGB8888 to monochrome
720 * @dst: Array of monochrome destination buffers (0=black, 1=white)
721 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
722 * within @dst; can be NULL if scanlines are stored next to each other.
723 * @src: Array of XRGB8888 source buffers
724 * @fb: DRM framebuffer
725 * @clip: Clip rectangle area to copy
727 * This function copies parts of a framebuffer to display memory and converts the
728 * color format during the process. Destination and framebuffer formats must match. The
729 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
730 * least as many entries as there are planes in @fb's format. Each entry stores the
731 * value for the format's respective color plane at the same index.
733 * This function does not apply clipping on @dst (i.e. the destination is at the
734 * top-left corner). The first pixel (upper left corner of the clip rectangle) will
735 * be converted and copied to the first bit (LSB) in the first byte of the monochrome
736 * destination buffer. If the caller requires that the first pixel in a byte must
737 * be located at an x-coordinate that is a multiple of 8, then the caller must take
738 * care itself of supplying a suitable clip rectangle.
740 * DRM doesn't have native monochrome support. Drivers can use this function for
741 * monochrome devices that don't support XRGB8888 natively. Such drivers can
742 * announce the commonly supported XR24 format to userspace and use this function
743 * to convert to the native format.
745 * This function uses drm_fb_xrgb8888_to_gray8() to convert to grayscale and
746 * then the result is converted from grayscale to monochrome.
748 void drm_fb_xrgb8888_to_mono(struct iosys_map *dst, const unsigned int *dst_pitch,
749 const struct iosys_map *src, const struct drm_framebuffer *fb,
750 const struct drm_rect *clip)
752 static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
755 unsigned int linepixels = drm_rect_width(clip);
756 unsigned int lines = drm_rect_height(clip);
757 unsigned int cpp = fb->format->cpp[0];
758 unsigned int len_src32 = linepixels * cpp;
759 struct drm_device *dev = fb->dev;
760 void *vaddr = src[0].vaddr;
761 unsigned int dst_pitch_0;
763 u8 *mono = dst[0].vaddr, *gray8;
766 if (drm_WARN_ON(dev, fb->format->format != DRM_FORMAT_XRGB8888))
770 dst_pitch = default_dst_pitch;
771 dst_pitch_0 = dst_pitch[0];
774 * The mono destination buffer contains 1 bit per pixel
777 dst_pitch_0 = DIV_ROUND_UP(linepixels, 8);
780 * The dma memory is write-combined so reads are uncached.
781 * Speed up by fetching one line at a time.
783 * Also, format conversion from XR24 to monochrome are done
784 * line-by-line but are converted to 8-bit grayscale as an
787 * Allocate a buffer to be used for both copying from the cma
788 * memory and to store the intermediate grayscale line pixels.
790 src32 = kmalloc(len_src32 + linepixels, GFP_KERNEL);
794 gray8 = (u8 *)src32 + len_src32;
796 vaddr += clip_offset(clip, fb->pitches[0], cpp);
797 for (y = 0; y < lines; y++) {
798 src32 = memcpy(src32, vaddr, len_src32);
799 drm_fb_xrgb8888_to_gray8_line(gray8, src32, linepixels);
800 drm_fb_gray8_to_mono_line(mono, gray8, linepixels);
801 vaddr += fb->pitches[0];
807 EXPORT_SYMBOL(drm_fb_xrgb8888_to_mono);
809 static bool is_listed_fourcc(const uint32_t *fourccs, size_t nfourccs, uint32_t fourcc)
811 const uint32_t *fourccs_end = fourccs + nfourccs;
813 while (fourccs < fourccs_end) {
814 if (*fourccs == fourcc)
821 static const uint32_t conv_from_xrgb8888[] = {
824 DRM_FORMAT_XRGB2101010,
825 DRM_FORMAT_ARGB2101010,
830 static const uint32_t conv_from_rgb565_888[] = {
835 static bool is_conversion_supported(uint32_t from, uint32_t to)
838 case DRM_FORMAT_XRGB8888:
839 case DRM_FORMAT_ARGB8888:
840 return is_listed_fourcc(conv_from_xrgb8888, ARRAY_SIZE(conv_from_xrgb8888), to);
841 case DRM_FORMAT_RGB565:
842 case DRM_FORMAT_RGB888:
843 return is_listed_fourcc(conv_from_rgb565_888, ARRAY_SIZE(conv_from_rgb565_888), to);
844 case DRM_FORMAT_XRGB2101010:
845 return to == DRM_FORMAT_ARGB2101010;
846 case DRM_FORMAT_ARGB2101010:
847 return to == DRM_FORMAT_XRGB2101010;
854 * drm_fb_build_fourcc_list - Filters a list of supported color formats against
855 * the device's native formats
857 * @native_fourccs: 4CC codes of natively supported color formats
858 * @native_nfourccs: The number of entries in @native_fourccs
859 * @driver_fourccs: 4CC codes of all driver-supported color formats
860 * @driver_nfourccs: The number of entries in @driver_fourccs
861 * @fourccs_out: Returns 4CC codes of supported color formats
862 * @nfourccs_out: The number of available entries in @fourccs_out
864 * This function create a list of supported color format from natively
865 * supported formats and the emulated formats.
866 * At a minimum, most userspace programs expect at least support for
867 * XRGB8888 on the primary plane. Devices that have to emulate the
868 * format, and possibly others, can use drm_fb_build_fourcc_list() to
869 * create a list of supported color formats. The returned list can
870 * be handed over to drm_universal_plane_init() et al. Native formats
871 * will go before emulated formats. Other heuristics might be applied
872 * to optimize the order. Formats near the beginning of the list are
873 * usually preferred over formats near the end of the list. Formats
874 * without conversion helpers will be skipped. New drivers should only
875 * pass in XRGB8888 and avoid exposing additional emulated formats.
878 * The number of color-formats 4CC codes returned in @fourccs_out.
880 size_t drm_fb_build_fourcc_list(struct drm_device *dev,
881 const u32 *native_fourccs, size_t native_nfourccs,
882 const u32 *driver_fourccs, size_t driver_nfourccs,
883 u32 *fourccs_out, size_t nfourccs_out)
885 u32 *fourccs = fourccs_out;
886 const u32 *fourccs_end = fourccs_out + nfourccs_out;
887 uint32_t native_format = 0;
891 * The device's native formats go first.
894 for (i = 0; i < native_nfourccs; ++i) {
895 u32 fourcc = native_fourccs[i];
897 if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
898 continue; /* skip duplicate entries */
899 } else if (fourccs == fourccs_end) {
900 drm_warn(dev, "Ignoring native format %p4cc\n", &fourcc);
901 continue; /* end of available output buffer */
904 drm_dbg_kms(dev, "adding native format %p4cc\n", &fourcc);
907 * There should only be one native format with the current API.
908 * This API needs to be refactored to correctly support arbitrary
909 * sets of native formats, since it needs to report which native
910 * format to use for each emulated format.
913 native_format = fourcc;
919 * The extra formats, emulated by the driver, go second.
922 for (i = 0; (i < driver_nfourccs) && (fourccs < fourccs_end); ++i) {
923 u32 fourcc = driver_fourccs[i];
925 if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
926 continue; /* skip duplicate and native entries */
927 } else if (fourccs == fourccs_end) {
928 drm_warn(dev, "Ignoring emulated format %p4cc\n", &fourcc);
929 continue; /* end of available output buffer */
930 } else if (!is_conversion_supported(fourcc, native_format)) {
931 drm_dbg_kms(dev, "Unsupported emulated format %p4cc\n", &fourcc);
932 continue; /* format is not supported for conversion */
935 drm_dbg_kms(dev, "adding emulated format %p4cc\n", &fourcc);
941 return fourccs - fourccs_out;
943 EXPORT_SYMBOL(drm_fb_build_fourcc_list);