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)
325 __le16 *dbuf16 = dbuf;
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);
336 dbuf16[x] = cpu_to_le16(val16);
340 /* TODO: implement this helper as conversion to RGB565|BIG_ENDIAN */
341 static void drm_fb_xrgb8888_to_rgb565_swab_line(void *dbuf, const void *sbuf,
344 __le16 *dbuf16 = dbuf;
345 const __le32 *sbuf32 = sbuf;
350 for (x = 0; x < pixels; x++) {
351 pix = le32_to_cpu(sbuf32[x]);
352 val16 = ((pix & 0x00F80000) >> 8) |
353 ((pix & 0x0000FC00) >> 5) |
354 ((pix & 0x000000F8) >> 3);
355 dbuf16[x] = cpu_to_le16(swab16(val16));
360 * drm_fb_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
361 * @dst: Array of RGB565 destination buffers
362 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
363 * within @dst; can be NULL if scanlines are stored next to each other.
364 * @src: Array of XRGB8888 source buffer
365 * @fb: DRM framebuffer
366 * @clip: Clip rectangle area to copy
369 * This function copies parts of a framebuffer to display memory and converts the
370 * color format during the process. Destination and framebuffer formats must match. The
371 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
372 * least as many entries as there are planes in @fb's format. Each entry stores the
373 * value for the format's respective color plane at the same index.
375 * This function does not apply clipping on @dst (i.e. the destination is at the
378 * Drivers can use this function for RGB565 devices that don't support XRGB8888 natively.
380 void drm_fb_xrgb8888_to_rgb565(struct iosys_map *dst, const unsigned int *dst_pitch,
381 const struct iosys_map *src, const struct drm_framebuffer *fb,
382 const struct drm_rect *clip, bool swab)
384 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
388 void (*xfrm_line)(void *dbuf, const void *sbuf, unsigned int npixels);
391 xfrm_line = drm_fb_xrgb8888_to_rgb565_swab_line;
393 xfrm_line = drm_fb_xrgb8888_to_rgb565_line;
395 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, xfrm_line);
397 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);
399 static void drm_fb_xrgb8888_to_rgb888_line(void *dbuf, const void *sbuf, unsigned int pixels)
402 const __le32 *sbuf32 = sbuf;
406 for (x = 0; x < pixels; x++) {
407 pix = le32_to_cpu(sbuf32[x]);
408 /* write blue-green-red to output in little endianness */
409 *dbuf8++ = (pix & 0x000000FF) >> 0;
410 *dbuf8++ = (pix & 0x0000FF00) >> 8;
411 *dbuf8++ = (pix & 0x00FF0000) >> 16;
416 * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
417 * @dst: Array of RGB888 destination buffers
418 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
419 * within @dst; can be NULL if scanlines are stored next to each other.
420 * @src: Array of XRGB8888 source buffers
421 * @fb: DRM framebuffer
422 * @clip: Clip rectangle area to copy
424 * This function copies parts of a framebuffer to display memory and converts the
425 * color format during the process. Destination and framebuffer formats must match. The
426 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
427 * least as many entries as there are planes in @fb's format. Each entry stores the
428 * value for the format's respective color plane at the same index.
430 * This function does not apply clipping on @dst (i.e. the destination is at the
433 * Drivers can use this function for RGB888 devices that don't natively
436 void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pitch,
437 const struct iosys_map *src, const struct drm_framebuffer *fb,
438 const struct drm_rect *clip)
440 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
444 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
445 drm_fb_xrgb8888_to_rgb888_line);
447 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
449 static void drm_fb_rgb565_to_xrgb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
451 __le32 *dbuf32 = dbuf;
452 const __le16 *sbuf16 = sbuf;
455 for (x = 0; x < pixels; x++) {
456 u16 val16 = le16_to_cpu(sbuf16[x]);
457 u32 val32 = ((val16 & 0xf800) << 8) |
458 ((val16 & 0x07e0) << 5) |
459 ((val16 & 0x001f) << 3);
460 val32 = 0xff000000 | val32 |
461 ((val32 >> 3) & 0x00070007) |
462 ((val32 >> 2) & 0x00000300);
463 dbuf32[x] = cpu_to_le32(val32);
467 static void drm_fb_rgb565_to_xrgb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
468 const struct iosys_map *src,
469 const struct drm_framebuffer *fb,
470 const struct drm_rect *clip)
472 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
476 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
477 drm_fb_rgb565_to_xrgb8888_line);
480 static void drm_fb_rgb888_to_xrgb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
482 __le32 *dbuf32 = dbuf;
483 const u8 *sbuf8 = sbuf;
486 for (x = 0; x < pixels; x++) {
490 u32 pix = 0xff000000 | (r << 16) | (g << 8) | b;
491 dbuf32[x] = cpu_to_le32(pix);
495 static void drm_fb_rgb888_to_xrgb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
496 const struct iosys_map *src,
497 const struct drm_framebuffer *fb,
498 const struct drm_rect *clip)
500 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
504 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
505 drm_fb_rgb888_to_xrgb8888_line);
508 static void drm_fb_xrgb8888_to_xrgb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
510 __le32 *dbuf32 = dbuf;
511 const __le32 *sbuf32 = sbuf;
516 for (x = 0; x < pixels; x++) {
517 pix = le32_to_cpu(sbuf32[x]);
518 val32 = ((pix & 0x000000FF) << 2) |
519 ((pix & 0x0000FF00) << 4) |
520 ((pix & 0x00FF0000) << 6);
521 pix = val32 | ((val32 >> 8) & 0x00300C03);
522 *dbuf32++ = cpu_to_le32(pix);
527 * drm_fb_xrgb8888_to_xrgb2101010 - Convert XRGB8888 to XRGB2101010 clip buffer
528 * @dst: Array of XRGB2101010 destination buffers
529 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
530 * within @dst; can be NULL if scanlines are stored next to each other.
531 * @src: Array of XRGB8888 source buffers
532 * @fb: DRM framebuffer
533 * @clip: Clip rectangle area to copy
535 * This function copies parts of a framebuffer to display memory and converts the
536 * color format during the process. Destination and framebuffer formats must match. The
537 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
538 * least as many entries as there are planes in @fb's format. Each entry stores the
539 * value for the format's respective color plane at the same index.
541 * This function does not apply clipping on @dst (i.e. the destination is at the
544 * Drivers can use this function for XRGB2101010 devices that don't support XRGB8888
547 void drm_fb_xrgb8888_to_xrgb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
548 const struct iosys_map *src, const struct drm_framebuffer *fb,
549 const struct drm_rect *clip)
551 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
555 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
556 drm_fb_xrgb8888_to_xrgb2101010_line);
558 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010);
560 static void drm_fb_xrgb8888_to_gray8_line(void *dbuf, const void *sbuf, unsigned int pixels)
563 const __le32 *sbuf32 = sbuf;
566 for (x = 0; x < pixels; x++) {
567 u32 pix = le32_to_cpu(sbuf32[x]);
568 u8 r = (pix & 0x00ff0000) >> 16;
569 u8 g = (pix & 0x0000ff00) >> 8;
570 u8 b = pix & 0x000000ff;
572 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
573 *dbuf8++ = (3 * r + 6 * g + b) / 10;
578 * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
579 * @dst: Array of 8-bit grayscale destination buffers
580 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
581 * within @dst; can be NULL if scanlines are stored next to each other.
582 * @src: Array of XRGB8888 source buffers
583 * @fb: DRM framebuffer
584 * @clip: Clip rectangle area to copy
586 * This function copies parts of a framebuffer to display memory and converts the
587 * color format during the process. Destination and framebuffer formats must match. The
588 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
589 * least as many entries as there are planes in @fb's format. Each entry stores the
590 * value for the format's respective color plane at the same index.
592 * This function does not apply clipping on @dst (i.e. the destination is at the
595 * DRM doesn't have native monochrome or grayscale support. Drivers can use this
596 * function for grayscale devices that don't support XRGB8888 natively.Such
597 * drivers can announce the commonly supported XR24 format to userspace and use
598 * this function to convert to the native format. Monochrome drivers will use the
599 * most significant bit, where 1 means foreground color and 0 background color.
600 * ITU BT.601 is being used for the RGB -> luma (brightness) conversion.
602 void drm_fb_xrgb8888_to_gray8(struct iosys_map *dst, const unsigned int *dst_pitch,
603 const struct iosys_map *src, const struct drm_framebuffer *fb,
604 const struct drm_rect *clip)
606 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
610 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
611 drm_fb_xrgb8888_to_gray8_line);
613 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);
616 * drm_fb_blit - Copy parts of a framebuffer to display memory
617 * @dst: Array of display-memory addresses to copy to
618 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
619 * within @dst; can be NULL if scanlines are stored next to each other.
620 * @dst_format: FOURCC code of the display's color format
621 * @src: The framebuffer memory to copy from
622 * @fb: The framebuffer to copy from
623 * @clip: Clip rectangle area to copy
625 * This function copies parts of a framebuffer to display memory. If the
626 * formats of the display and the framebuffer mismatch, the blit function
627 * will attempt to convert between them during the process. The parameters @dst,
628 * @dst_pitch and @src refer to arrays. Each array must have at least as many
629 * entries as there are planes in @dst_format's format. Each entry stores the
630 * value for the format's respective color plane at the same index.
632 * This function does not apply clipping on @dst (i.e. the destination is at the
637 * -EINVAL if the color-format conversion failed, or
638 * a negative error code otherwise.
640 int drm_fb_blit(struct iosys_map *dst, const unsigned int *dst_pitch, uint32_t dst_format,
641 const struct iosys_map *src, const struct drm_framebuffer *fb,
642 const struct drm_rect *clip)
644 uint32_t fb_format = fb->format->format;
646 /* treat alpha channel like filler bits */
647 if (fb_format == DRM_FORMAT_ARGB8888)
648 fb_format = DRM_FORMAT_XRGB8888;
649 if (dst_format == DRM_FORMAT_ARGB8888)
650 dst_format = DRM_FORMAT_XRGB8888;
651 if (fb_format == DRM_FORMAT_ARGB2101010)
652 fb_format = DRM_FORMAT_XRGB2101010;
653 if (dst_format == DRM_FORMAT_ARGB2101010)
654 dst_format = DRM_FORMAT_XRGB2101010;
656 if (dst_format == fb_format) {
657 drm_fb_memcpy(dst, dst_pitch, src, fb, clip);
660 } else if (dst_format == DRM_FORMAT_RGB565) {
661 if (fb_format == DRM_FORMAT_XRGB8888) {
662 drm_fb_xrgb8888_to_rgb565(dst, dst_pitch, src, fb, clip, false);
665 } else if (dst_format == (DRM_FORMAT_RGB565 | DRM_FORMAT_BIG_ENDIAN)) {
666 if (fb_format == DRM_FORMAT_RGB565) {
667 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
670 } else if (dst_format == DRM_FORMAT_RGB888) {
671 if (fb_format == DRM_FORMAT_XRGB8888) {
672 drm_fb_xrgb8888_to_rgb888(dst, dst_pitch, src, fb, clip);
675 } else if (dst_format == DRM_FORMAT_XRGB8888) {
676 if (fb_format == DRM_FORMAT_RGB888) {
677 drm_fb_rgb888_to_xrgb8888(dst, dst_pitch, src, fb, clip);
679 } else if (fb_format == DRM_FORMAT_RGB565) {
680 drm_fb_rgb565_to_xrgb8888(dst, dst_pitch, src, fb, clip);
683 } else if (dst_format == DRM_FORMAT_XRGB2101010) {
684 if (fb_format == DRM_FORMAT_XRGB8888) {
685 drm_fb_xrgb8888_to_xrgb2101010(dst, dst_pitch, src, fb, clip);
688 } else if (dst_format == DRM_FORMAT_BGRX8888) {
689 if (fb_format == DRM_FORMAT_XRGB8888) {
690 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
695 drm_warn_once(fb->dev, "No conversion helper from %p4cc to %p4cc found.\n",
696 &fb_format, &dst_format);
700 EXPORT_SYMBOL(drm_fb_blit);
702 static void drm_fb_gray8_to_mono_line(void *dbuf, const void *sbuf, unsigned int pixels)
705 const u8 *sbuf8 = sbuf;
708 unsigned int i, bits = min(pixels, 8U);
711 for (i = 0; i < bits; i++, pixels--) {
720 * drm_fb_xrgb8888_to_mono - Convert XRGB8888 to monochrome
721 * @dst: Array of monochrome destination buffers (0=black, 1=white)
722 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
723 * within @dst; can be NULL if scanlines are stored next to each other.
724 * @src: Array of XRGB8888 source buffers
725 * @fb: DRM framebuffer
726 * @clip: Clip rectangle area to copy
728 * This function copies parts of a framebuffer to display memory and converts the
729 * color format during the process. Destination and framebuffer formats must match. The
730 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
731 * least as many entries as there are planes in @fb's format. Each entry stores the
732 * value for the format's respective color plane at the same index.
734 * This function does not apply clipping on @dst (i.e. the destination is at the
735 * top-left corner). The first pixel (upper left corner of the clip rectangle) will
736 * be converted and copied to the first bit (LSB) in the first byte of the monochrome
737 * destination buffer. If the caller requires that the first pixel in a byte must
738 * be located at an x-coordinate that is a multiple of 8, then the caller must take
739 * care itself of supplying a suitable clip rectangle.
741 * DRM doesn't have native monochrome support. Drivers can use this function for
742 * monochrome devices that don't support XRGB8888 natively. Such drivers can
743 * announce the commonly supported XR24 format to userspace and use this function
744 * to convert to the native format.
746 * This function uses drm_fb_xrgb8888_to_gray8() to convert to grayscale and
747 * then the result is converted from grayscale to monochrome.
749 void drm_fb_xrgb8888_to_mono(struct iosys_map *dst, const unsigned int *dst_pitch,
750 const struct iosys_map *src, const struct drm_framebuffer *fb,
751 const struct drm_rect *clip)
753 static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
756 unsigned int linepixels = drm_rect_width(clip);
757 unsigned int lines = drm_rect_height(clip);
758 unsigned int cpp = fb->format->cpp[0];
759 unsigned int len_src32 = linepixels * cpp;
760 struct drm_device *dev = fb->dev;
761 void *vaddr = src[0].vaddr;
762 unsigned int dst_pitch_0;
764 u8 *mono = dst[0].vaddr, *gray8;
767 if (drm_WARN_ON(dev, fb->format->format != DRM_FORMAT_XRGB8888))
771 dst_pitch = default_dst_pitch;
772 dst_pitch_0 = dst_pitch[0];
775 * The mono destination buffer contains 1 bit per pixel
778 dst_pitch_0 = DIV_ROUND_UP(linepixels, 8);
781 * The dma memory is write-combined so reads are uncached.
782 * Speed up by fetching one line at a time.
784 * Also, format conversion from XR24 to monochrome are done
785 * line-by-line but are converted to 8-bit grayscale as an
788 * Allocate a buffer to be used for both copying from the cma
789 * memory and to store the intermediate grayscale line pixels.
791 src32 = kmalloc(len_src32 + linepixels, GFP_KERNEL);
795 gray8 = (u8 *)src32 + len_src32;
797 vaddr += clip_offset(clip, fb->pitches[0], cpp);
798 for (y = 0; y < lines; y++) {
799 src32 = memcpy(src32, vaddr, len_src32);
800 drm_fb_xrgb8888_to_gray8_line(gray8, src32, linepixels);
801 drm_fb_gray8_to_mono_line(mono, gray8, linepixels);
802 vaddr += fb->pitches[0];
808 EXPORT_SYMBOL(drm_fb_xrgb8888_to_mono);
810 static bool is_listed_fourcc(const uint32_t *fourccs, size_t nfourccs, uint32_t fourcc)
812 const uint32_t *fourccs_end = fourccs + nfourccs;
814 while (fourccs < fourccs_end) {
815 if (*fourccs == fourcc)
822 static const uint32_t conv_from_xrgb8888[] = {
825 DRM_FORMAT_XRGB2101010,
826 DRM_FORMAT_ARGB2101010,
831 static const uint32_t conv_from_rgb565_888[] = {
836 static bool is_conversion_supported(uint32_t from, uint32_t to)
839 case DRM_FORMAT_XRGB8888:
840 case DRM_FORMAT_ARGB8888:
841 return is_listed_fourcc(conv_from_xrgb8888, ARRAY_SIZE(conv_from_xrgb8888), to);
842 case DRM_FORMAT_RGB565:
843 case DRM_FORMAT_RGB888:
844 return is_listed_fourcc(conv_from_rgb565_888, ARRAY_SIZE(conv_from_rgb565_888), to);
845 case DRM_FORMAT_XRGB2101010:
846 return to == DRM_FORMAT_ARGB2101010;
847 case DRM_FORMAT_ARGB2101010:
848 return to == DRM_FORMAT_XRGB2101010;
855 * drm_fb_build_fourcc_list - Filters a list of supported color formats against
856 * the device's native formats
858 * @native_fourccs: 4CC codes of natively supported color formats
859 * @native_nfourccs: The number of entries in @native_fourccs
860 * @driver_fourccs: 4CC codes of all driver-supported color formats
861 * @driver_nfourccs: The number of entries in @driver_fourccs
862 * @fourccs_out: Returns 4CC codes of supported color formats
863 * @nfourccs_out: The number of available entries in @fourccs_out
865 * This function create a list of supported color format from natively
866 * supported formats and the emulated formats.
867 * At a minimum, most userspace programs expect at least support for
868 * XRGB8888 on the primary plane. Devices that have to emulate the
869 * format, and possibly others, can use drm_fb_build_fourcc_list() to
870 * create a list of supported color formats. The returned list can
871 * be handed over to drm_universal_plane_init() et al. Native formats
872 * will go before emulated formats. Other heuristics might be applied
873 * to optimize the order. Formats near the beginning of the list are
874 * usually preferred over formats near the end of the list. Formats
875 * without conversion helpers will be skipped. New drivers should only
876 * pass in XRGB8888 and avoid exposing additional emulated formats.
879 * The number of color-formats 4CC codes returned in @fourccs_out.
881 size_t drm_fb_build_fourcc_list(struct drm_device *dev,
882 const u32 *native_fourccs, size_t native_nfourccs,
883 const u32 *driver_fourccs, size_t driver_nfourccs,
884 u32 *fourccs_out, size_t nfourccs_out)
886 u32 *fourccs = fourccs_out;
887 const u32 *fourccs_end = fourccs_out + nfourccs_out;
888 uint32_t native_format = 0;
892 * The device's native formats go first.
895 for (i = 0; i < native_nfourccs; ++i) {
896 u32 fourcc = native_fourccs[i];
898 if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
899 continue; /* skip duplicate entries */
900 } else if (fourccs == fourccs_end) {
901 drm_warn(dev, "Ignoring native format %p4cc\n", &fourcc);
902 continue; /* end of available output buffer */
905 drm_dbg_kms(dev, "adding native format %p4cc\n", &fourcc);
908 * There should only be one native format with the current API.
909 * This API needs to be refactored to correctly support arbitrary
910 * sets of native formats, since it needs to report which native
911 * format to use for each emulated format.
914 native_format = fourcc;
920 * The extra formats, emulated by the driver, go second.
923 for (i = 0; (i < driver_nfourccs) && (fourccs < fourccs_end); ++i) {
924 u32 fourcc = driver_fourccs[i];
926 if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
927 continue; /* skip duplicate and native entries */
928 } else if (fourccs == fourccs_end) {
929 drm_warn(dev, "Ignoring emulated format %p4cc\n", &fourcc);
930 continue; /* end of available output buffer */
931 } else if (!is_conversion_supported(fourcc, native_format)) {
932 drm_dbg_kms(dev, "Unsupported emulated format %p4cc\n", &fourcc);
933 continue; /* format is not supported for conversion */
936 drm_dbg_kms(dev, "adding emulated format %p4cc\n", &fourcc);
942 return fourccs - fourccs_out;
944 EXPORT_SYMBOL(drm_fb_build_fourcc_list);