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_xrgb1555_line(void *dbuf, const void *sbuf, unsigned int pixels)
401 __le16 *dbuf16 = dbuf;
402 const __le32 *sbuf32 = sbuf;
407 for (x = 0; x < pixels; x++) {
408 pix = le32_to_cpu(sbuf32[x]);
409 val16 = ((pix & 0x00f80000) >> 9) |
410 ((pix & 0x0000f800) >> 6) |
411 ((pix & 0x000000f8) >> 3);
412 dbuf16[x] = cpu_to_le16(val16);
417 * drm_fb_xrgb8888_to_xrgb1555 - Convert XRGB8888 to XRGB1555 clip buffer
418 * @dst: Array of XRGB1555 destination buffers
419 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
420 * within @dst; can be NULL if scanlines are stored next to each other.
421 * @src: Array of XRGB8888 source buffer
422 * @fb: DRM framebuffer
423 * @clip: Clip rectangle area to copy
425 * This function copies parts of a framebuffer to display memory and converts
426 * the color format during the process. The parameters @dst, @dst_pitch and
427 * @src refer to arrays. Each array must have at least as many entries as
428 * there are planes in @fb's format. Each entry stores the value for the
429 * format's respective color plane at the same index.
431 * This function does not apply clipping on @dst (i.e. the destination is at the
434 * Drivers can use this function for XRGB1555 devices that don't support
437 void drm_fb_xrgb8888_to_xrgb1555(struct iosys_map *dst, const unsigned int *dst_pitch,
438 const struct iosys_map *src, const struct drm_framebuffer *fb,
439 const struct drm_rect *clip)
441 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
445 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
446 drm_fb_xrgb8888_to_xrgb1555_line);
448 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb1555);
450 static void drm_fb_xrgb8888_to_argb1555_line(void *dbuf, const void *sbuf, unsigned int pixels)
452 __le16 *dbuf16 = dbuf;
453 const __le32 *sbuf32 = sbuf;
458 for (x = 0; x < pixels; x++) {
459 pix = le32_to_cpu(sbuf32[x]);
460 val16 = BIT(15) | /* set alpha bit */
461 ((pix & 0x00f80000) >> 9) |
462 ((pix & 0x0000f800) >> 6) |
463 ((pix & 0x000000f8) >> 3);
464 dbuf16[x] = cpu_to_le16(val16);
469 * drm_fb_xrgb8888_to_argb1555 - Convert XRGB8888 to ARGB1555 clip buffer
470 * @dst: Array of ARGB1555 destination buffers
471 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
472 * within @dst; can be NULL if scanlines are stored next to each other.
473 * @src: Array of XRGB8888 source buffer
474 * @fb: DRM framebuffer
475 * @clip: Clip rectangle area to copy
477 * This function copies parts of a framebuffer to display memory and converts
478 * the color format during the process. The parameters @dst, @dst_pitch and
479 * @src refer to arrays. Each array must have at least as many entries as
480 * there are planes in @fb's format. Each entry stores the value for the
481 * format's respective color plane at the same index.
483 * This function does not apply clipping on @dst (i.e. the destination is at the
486 * Drivers can use this function for ARGB1555 devices that don't support
487 * XRGB8888 natively. It sets an opaque alpha channel as part of the conversion.
489 void drm_fb_xrgb8888_to_argb1555(struct iosys_map *dst, const unsigned int *dst_pitch,
490 const struct iosys_map *src, const struct drm_framebuffer *fb,
491 const struct drm_rect *clip)
493 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
497 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
498 drm_fb_xrgb8888_to_argb1555_line);
500 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb1555);
502 static void drm_fb_xrgb8888_to_rgba5551_line(void *dbuf, const void *sbuf, unsigned int pixels)
504 __le16 *dbuf16 = dbuf;
505 const __le32 *sbuf32 = sbuf;
510 for (x = 0; x < pixels; x++) {
511 pix = le32_to_cpu(sbuf32[x]);
512 val16 = ((pix & 0x00f80000) >> 8) |
513 ((pix & 0x0000f800) >> 5) |
514 ((pix & 0x000000f8) >> 2) |
515 BIT(0); /* set alpha bit */
516 dbuf16[x] = cpu_to_le16(val16);
521 * drm_fb_xrgb8888_to_rgba5551 - Convert XRGB8888 to RGBA5551 clip buffer
522 * @dst: Array of RGBA5551 destination buffers
523 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
524 * within @dst; can be NULL if scanlines are stored next to each other.
525 * @src: Array of XRGB8888 source buffer
526 * @fb: DRM framebuffer
527 * @clip: Clip rectangle area to copy
529 * This function copies parts of a framebuffer to display memory and converts
530 * the color format during the process. The parameters @dst, @dst_pitch and
531 * @src refer to arrays. Each array must have at least as many entries as
532 * there are planes in @fb's format. Each entry stores the value for the
533 * format's respective color plane at the same index.
535 * This function does not apply clipping on @dst (i.e. the destination is at the
538 * Drivers can use this function for RGBA5551 devices that don't support
539 * XRGB8888 natively. It sets an opaque alpha channel as part of the conversion.
541 void drm_fb_xrgb8888_to_rgba5551(struct iosys_map *dst, const unsigned int *dst_pitch,
542 const struct iosys_map *src, const struct drm_framebuffer *fb,
543 const struct drm_rect *clip)
545 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
549 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
550 drm_fb_xrgb8888_to_rgba5551_line);
552 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgba5551);
554 static void drm_fb_xrgb8888_to_rgb888_line(void *dbuf, const void *sbuf, unsigned int pixels)
557 const __le32 *sbuf32 = sbuf;
561 for (x = 0; x < pixels; x++) {
562 pix = le32_to_cpu(sbuf32[x]);
563 /* write blue-green-red to output in little endianness */
564 *dbuf8++ = (pix & 0x000000FF) >> 0;
565 *dbuf8++ = (pix & 0x0000FF00) >> 8;
566 *dbuf8++ = (pix & 0x00FF0000) >> 16;
571 * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
572 * @dst: Array of RGB888 destination buffers
573 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
574 * within @dst; can be NULL if scanlines are stored next to each other.
575 * @src: Array of XRGB8888 source buffers
576 * @fb: DRM framebuffer
577 * @clip: Clip rectangle area to copy
579 * This function copies parts of a framebuffer to display memory and converts the
580 * color format during the process. Destination and framebuffer formats must match. The
581 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
582 * least as many entries as there are planes in @fb's format. Each entry stores the
583 * value for the format's respective color plane at the same index.
585 * This function does not apply clipping on @dst (i.e. the destination is at the
588 * Drivers can use this function for RGB888 devices that don't natively
591 void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pitch,
592 const struct iosys_map *src, const struct drm_framebuffer *fb,
593 const struct drm_rect *clip)
595 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
599 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
600 drm_fb_xrgb8888_to_rgb888_line);
602 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
604 static void drm_fb_xrgb8888_to_argb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
606 __le32 *dbuf32 = dbuf;
607 const __le32 *sbuf32 = sbuf;
611 for (x = 0; x < pixels; x++) {
612 pix = le32_to_cpu(sbuf32[x]);
613 pix |= GENMASK(31, 24); /* fill alpha bits */
614 dbuf32[x] = cpu_to_le32(pix);
619 * drm_fb_xrgb8888_to_argb8888 - Convert XRGB8888 to ARGB8888 clip buffer
620 * @dst: Array of ARGB8888 destination buffers
621 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
622 * within @dst; can be NULL if scanlines are stored next to each other.
623 * @src: Array of XRGB8888 source buffer
624 * @fb: DRM framebuffer
625 * @clip: Clip rectangle area to copy
627 * This function copies parts of a framebuffer to display memory and converts the
628 * color format during the process. The parameters @dst, @dst_pitch and @src refer
629 * to arrays. Each array must have at least as many entries as there are planes in
630 * @fb's format. Each entry stores the value for the format's respective color plane
633 * This function does not apply clipping on @dst (i.e. the destination is at the
636 * Drivers can use this function for ARGB8888 devices that don't support XRGB8888
637 * natively. It sets an opaque alpha channel as part of the conversion.
639 void drm_fb_xrgb8888_to_argb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
640 const struct iosys_map *src, const struct drm_framebuffer *fb,
641 const struct drm_rect *clip)
643 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
647 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
648 drm_fb_xrgb8888_to_argb8888_line);
650 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb8888);
652 static void drm_fb_xrgb8888_to_xrgb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
654 __le32 *dbuf32 = dbuf;
655 const __le32 *sbuf32 = sbuf;
660 for (x = 0; x < pixels; x++) {
661 pix = le32_to_cpu(sbuf32[x]);
662 val32 = ((pix & 0x000000FF) << 2) |
663 ((pix & 0x0000FF00) << 4) |
664 ((pix & 0x00FF0000) << 6);
665 pix = val32 | ((val32 >> 8) & 0x00300C03);
666 *dbuf32++ = cpu_to_le32(pix);
671 * drm_fb_xrgb8888_to_xrgb2101010 - Convert XRGB8888 to XRGB2101010 clip buffer
672 * @dst: Array of XRGB2101010 destination buffers
673 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
674 * within @dst; can be NULL if scanlines are stored next to each other.
675 * @src: Array of XRGB8888 source buffers
676 * @fb: DRM framebuffer
677 * @clip: Clip rectangle area to copy
679 * This function copies parts of a framebuffer to display memory and converts the
680 * color format during the process. Destination and framebuffer formats must match. The
681 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
682 * least as many entries as there are planes in @fb's format. Each entry stores the
683 * value for the format's respective color plane at the same index.
685 * This function does not apply clipping on @dst (i.e. the destination is at the
688 * Drivers can use this function for XRGB2101010 devices that don't support XRGB8888
691 void drm_fb_xrgb8888_to_xrgb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
692 const struct iosys_map *src, const struct drm_framebuffer *fb,
693 const struct drm_rect *clip)
695 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
699 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
700 drm_fb_xrgb8888_to_xrgb2101010_line);
702 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010);
704 static void drm_fb_xrgb8888_to_argb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
706 __le32 *dbuf32 = dbuf;
707 const __le32 *sbuf32 = sbuf;
712 for (x = 0; x < pixels; x++) {
713 pix = le32_to_cpu(sbuf32[x]);
714 val32 = ((pix & 0x000000ff) << 2) |
715 ((pix & 0x0000ff00) << 4) |
716 ((pix & 0x00ff0000) << 6);
717 pix = GENMASK(31, 30) | /* set alpha bits */
718 val32 | ((val32 >> 8) & 0x00300c03);
719 *dbuf32++ = cpu_to_le32(pix);
724 * drm_fb_xrgb8888_to_argb2101010 - Convert XRGB8888 to ARGB2101010 clip buffer
725 * @dst: Array of ARGB2101010 destination buffers
726 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
727 * within @dst; can be NULL if scanlines are stored next to each other.
728 * @src: Array of XRGB8888 source buffers
729 * @fb: DRM framebuffer
730 * @clip: Clip rectangle area to copy
732 * This function copies parts of a framebuffer to display memory and converts
733 * the color format during the process. The parameters @dst, @dst_pitch and
734 * @src refer to arrays. Each array must have at least as many entries as
735 * there are planes in @fb's format. Each entry stores the value for the
736 * format's respective color plane at the same index.
738 * This function does not apply clipping on @dst (i.e. the destination is at the
741 * Drivers can use this function for ARGB2101010 devices that don't support XRGB8888
744 void drm_fb_xrgb8888_to_argb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
745 const struct iosys_map *src, const struct drm_framebuffer *fb,
746 const struct drm_rect *clip)
748 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
752 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
753 drm_fb_xrgb8888_to_argb2101010_line);
755 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb2101010);
757 static void drm_fb_xrgb8888_to_gray8_line(void *dbuf, const void *sbuf, unsigned int pixels)
760 const __le32 *sbuf32 = sbuf;
763 for (x = 0; x < pixels; x++) {
764 u32 pix = le32_to_cpu(sbuf32[x]);
765 u8 r = (pix & 0x00ff0000) >> 16;
766 u8 g = (pix & 0x0000ff00) >> 8;
767 u8 b = pix & 0x000000ff;
769 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
770 *dbuf8++ = (3 * r + 6 * g + b) / 10;
775 * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
776 * @dst: Array of 8-bit grayscale destination buffers
777 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
778 * within @dst; can be NULL if scanlines are stored next to each other.
779 * @src: Array of XRGB8888 source buffers
780 * @fb: DRM framebuffer
781 * @clip: Clip rectangle area to copy
783 * This function copies parts of a framebuffer to display memory and converts the
784 * color format during the process. Destination and framebuffer formats must match. The
785 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
786 * least as many entries as there are planes in @fb's format. Each entry stores the
787 * value for the format's respective color plane at the same index.
789 * This function does not apply clipping on @dst (i.e. the destination is at the
792 * DRM doesn't have native monochrome or grayscale support. Drivers can use this
793 * function for grayscale devices that don't support XRGB8888 natively.Such
794 * drivers can announce the commonly supported XR24 format to userspace and use
795 * this function to convert to the native format. Monochrome drivers will use the
796 * most significant bit, where 1 means foreground color and 0 background color.
797 * ITU BT.601 is being used for the RGB -> luma (brightness) conversion.
799 void drm_fb_xrgb8888_to_gray8(struct iosys_map *dst, const unsigned int *dst_pitch,
800 const struct iosys_map *src, const struct drm_framebuffer *fb,
801 const struct drm_rect *clip)
803 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
807 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
808 drm_fb_xrgb8888_to_gray8_line);
810 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);
813 * drm_fb_blit - Copy parts of a framebuffer to display memory
814 * @dst: Array of display-memory addresses to copy to
815 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
816 * within @dst; can be NULL if scanlines are stored next to each other.
817 * @dst_format: FOURCC code of the display's color format
818 * @src: The framebuffer memory to copy from
819 * @fb: The framebuffer to copy from
820 * @clip: Clip rectangle area to copy
822 * This function copies parts of a framebuffer to display memory. If the
823 * formats of the display and the framebuffer mismatch, the blit function
824 * will attempt to convert between them during the process. The parameters @dst,
825 * @dst_pitch and @src refer to arrays. Each array must have at least as many
826 * entries as there are planes in @dst_format's format. Each entry stores the
827 * value for the format's respective color plane at the same index.
829 * This function does not apply clipping on @dst (i.e. the destination is at the
834 * -EINVAL if the color-format conversion failed, or
835 * a negative error code otherwise.
837 int drm_fb_blit(struct iosys_map *dst, const unsigned int *dst_pitch, uint32_t dst_format,
838 const struct iosys_map *src, const struct drm_framebuffer *fb,
839 const struct drm_rect *clip)
841 uint32_t fb_format = fb->format->format;
843 if (fb_format == dst_format) {
844 drm_fb_memcpy(dst, dst_pitch, src, fb, clip);
846 } else if (fb_format == (dst_format | DRM_FORMAT_BIG_ENDIAN)) {
847 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
849 } else if (fb_format == (dst_format & ~DRM_FORMAT_BIG_ENDIAN)) {
850 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
852 } else if (fb_format == DRM_FORMAT_XRGB8888) {
853 if (dst_format == DRM_FORMAT_RGB565) {
854 drm_fb_xrgb8888_to_rgb565(dst, dst_pitch, src, fb, clip, false);
856 } else if (dst_format == DRM_FORMAT_XRGB1555) {
857 drm_fb_xrgb8888_to_xrgb1555(dst, dst_pitch, src, fb, clip);
859 } else if (dst_format == DRM_FORMAT_ARGB1555) {
860 drm_fb_xrgb8888_to_argb1555(dst, dst_pitch, src, fb, clip);
862 } else if (dst_format == DRM_FORMAT_RGBA5551) {
863 drm_fb_xrgb8888_to_rgba5551(dst, dst_pitch, src, fb, clip);
865 } else if (dst_format == DRM_FORMAT_RGB888) {
866 drm_fb_xrgb8888_to_rgb888(dst, dst_pitch, src, fb, clip);
868 } else if (dst_format == DRM_FORMAT_ARGB8888) {
869 drm_fb_xrgb8888_to_argb8888(dst, dst_pitch, src, fb, clip);
871 } else if (dst_format == DRM_FORMAT_XRGB2101010) {
872 drm_fb_xrgb8888_to_xrgb2101010(dst, dst_pitch, src, fb, clip);
874 } else if (dst_format == DRM_FORMAT_ARGB2101010) {
875 drm_fb_xrgb8888_to_argb2101010(dst, dst_pitch, src, fb, clip);
877 } else if (dst_format == DRM_FORMAT_BGRX8888) {
878 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
883 drm_warn_once(fb->dev, "No conversion helper from %p4cc to %p4cc found.\n",
884 &fb_format, &dst_format);
888 EXPORT_SYMBOL(drm_fb_blit);
890 static void drm_fb_gray8_to_mono_line(void *dbuf, const void *sbuf, unsigned int pixels)
893 const u8 *sbuf8 = sbuf;
896 unsigned int i, bits = min(pixels, 8U);
899 for (i = 0; i < bits; i++, pixels--) {
908 * drm_fb_xrgb8888_to_mono - Convert XRGB8888 to monochrome
909 * @dst: Array of monochrome destination buffers (0=black, 1=white)
910 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
911 * within @dst; can be NULL if scanlines are stored next to each other.
912 * @src: Array of XRGB8888 source buffers
913 * @fb: DRM framebuffer
914 * @clip: Clip rectangle area to copy
916 * This function copies parts of a framebuffer to display memory and converts the
917 * color format during the process. Destination and framebuffer formats must match. The
918 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
919 * least as many entries as there are planes in @fb's format. Each entry stores the
920 * value for the format's respective color plane at the same index.
922 * This function does not apply clipping on @dst (i.e. the destination is at the
923 * top-left corner). The first pixel (upper left corner of the clip rectangle) will
924 * be converted and copied to the first bit (LSB) in the first byte of the monochrome
925 * destination buffer. If the caller requires that the first pixel in a byte must
926 * be located at an x-coordinate that is a multiple of 8, then the caller must take
927 * care itself of supplying a suitable clip rectangle.
929 * DRM doesn't have native monochrome support. Drivers can use this function for
930 * monochrome devices that don't support XRGB8888 natively. Such drivers can
931 * announce the commonly supported XR24 format to userspace and use this function
932 * to convert to the native format.
934 * This function uses drm_fb_xrgb8888_to_gray8() to convert to grayscale and
935 * then the result is converted from grayscale to monochrome.
937 void drm_fb_xrgb8888_to_mono(struct iosys_map *dst, const unsigned int *dst_pitch,
938 const struct iosys_map *src, const struct drm_framebuffer *fb,
939 const struct drm_rect *clip)
941 static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
944 unsigned int linepixels = drm_rect_width(clip);
945 unsigned int lines = drm_rect_height(clip);
946 unsigned int cpp = fb->format->cpp[0];
947 unsigned int len_src32 = linepixels * cpp;
948 struct drm_device *dev = fb->dev;
949 void *vaddr = src[0].vaddr;
950 unsigned int dst_pitch_0;
952 u8 *mono = dst[0].vaddr, *gray8;
955 if (drm_WARN_ON(dev, fb->format->format != DRM_FORMAT_XRGB8888))
959 dst_pitch = default_dst_pitch;
960 dst_pitch_0 = dst_pitch[0];
963 * The mono destination buffer contains 1 bit per pixel
966 dst_pitch_0 = DIV_ROUND_UP(linepixels, 8);
969 * The dma memory is write-combined so reads are uncached.
970 * Speed up by fetching one line at a time.
972 * Also, format conversion from XR24 to monochrome are done
973 * line-by-line but are converted to 8-bit grayscale as an
976 * Allocate a buffer to be used for both copying from the cma
977 * memory and to store the intermediate grayscale line pixels.
979 src32 = kmalloc(len_src32 + linepixels, GFP_KERNEL);
983 gray8 = (u8 *)src32 + len_src32;
985 vaddr += clip_offset(clip, fb->pitches[0], cpp);
986 for (y = 0; y < lines; y++) {
987 src32 = memcpy(src32, vaddr, len_src32);
988 drm_fb_xrgb8888_to_gray8_line(gray8, src32, linepixels);
989 drm_fb_gray8_to_mono_line(mono, gray8, linepixels);
990 vaddr += fb->pitches[0];
996 EXPORT_SYMBOL(drm_fb_xrgb8888_to_mono);
998 static uint32_t drm_fb_nonalpha_fourcc(uint32_t fourcc)
1000 /* only handle formats with depth != 0 and alpha channel */
1002 case DRM_FORMAT_ARGB1555:
1003 return DRM_FORMAT_XRGB1555;
1004 case DRM_FORMAT_ABGR1555:
1005 return DRM_FORMAT_XBGR1555;
1006 case DRM_FORMAT_RGBA5551:
1007 return DRM_FORMAT_RGBX5551;
1008 case DRM_FORMAT_BGRA5551:
1009 return DRM_FORMAT_BGRX5551;
1010 case DRM_FORMAT_ARGB8888:
1011 return DRM_FORMAT_XRGB8888;
1012 case DRM_FORMAT_ABGR8888:
1013 return DRM_FORMAT_XBGR8888;
1014 case DRM_FORMAT_RGBA8888:
1015 return DRM_FORMAT_RGBX8888;
1016 case DRM_FORMAT_BGRA8888:
1017 return DRM_FORMAT_BGRX8888;
1018 case DRM_FORMAT_ARGB2101010:
1019 return DRM_FORMAT_XRGB2101010;
1020 case DRM_FORMAT_ABGR2101010:
1021 return DRM_FORMAT_XBGR2101010;
1022 case DRM_FORMAT_RGBA1010102:
1023 return DRM_FORMAT_RGBX1010102;
1024 case DRM_FORMAT_BGRA1010102:
1025 return DRM_FORMAT_BGRX1010102;
1031 static bool is_listed_fourcc(const uint32_t *fourccs, size_t nfourccs, uint32_t fourcc)
1033 const uint32_t *fourccs_end = fourccs + nfourccs;
1035 while (fourccs < fourccs_end) {
1036 if (*fourccs == fourcc)
1044 * drm_fb_build_fourcc_list - Filters a list of supported color formats against
1045 * the device's native formats
1047 * @native_fourccs: 4CC codes of natively supported color formats
1048 * @native_nfourccs: The number of entries in @native_fourccs
1049 * @fourccs_out: Returns 4CC codes of supported color formats
1050 * @nfourccs_out: The number of available entries in @fourccs_out
1052 * This function create a list of supported color format from natively
1053 * supported formats and additional emulated formats.
1054 * At a minimum, most userspace programs expect at least support for
1055 * XRGB8888 on the primary plane. Devices that have to emulate the
1056 * format, and possibly others, can use drm_fb_build_fourcc_list() to
1057 * create a list of supported color formats. The returned list can
1058 * be handed over to drm_universal_plane_init() et al. Native formats
1059 * will go before emulated formats. Native formats with alpha channel
1060 * will be replaced by such without, as primary planes usually don't
1061 * support alpha. Other heuristics might be applied
1062 * to optimize the order. Formats near the beginning of the list are
1063 * usually preferred over formats near the end of the list.
1066 * The number of color-formats 4CC codes returned in @fourccs_out.
1068 size_t drm_fb_build_fourcc_list(struct drm_device *dev,
1069 const u32 *native_fourccs, size_t native_nfourccs,
1070 u32 *fourccs_out, size_t nfourccs_out)
1073 * XRGB8888 is the default fallback format for most of userspace
1074 * and it's currently the only format that should be emulated for
1075 * the primary plane. Only if there's ever another default fallback,
1076 * it should be added here.
1078 static const uint32_t extra_fourccs[] = {
1079 DRM_FORMAT_XRGB8888,
1081 static const size_t extra_nfourccs = ARRAY_SIZE(extra_fourccs);
1083 u32 *fourccs = fourccs_out;
1084 const u32 *fourccs_end = fourccs_out + nfourccs_out;
1088 * The device's native formats go first.
1091 for (i = 0; i < native_nfourccs; ++i) {
1093 * Several DTs, boot loaders and firmware report native
1094 * alpha formats that are non-alpha formats instead. So
1095 * replace alpha formats by non-alpha formats.
1097 u32 fourcc = drm_fb_nonalpha_fourcc(native_fourccs[i]);
1099 if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
1100 continue; /* skip duplicate entries */
1101 } else if (fourccs == fourccs_end) {
1102 drm_warn(dev, "Ignoring native format %p4cc\n", &fourcc);
1103 continue; /* end of available output buffer */
1106 drm_dbg_kms(dev, "adding native format %p4cc\n", &fourcc);
1113 * The extra formats, emulated by the driver, go second.
1116 for (i = 0; (i < extra_nfourccs) && (fourccs < fourccs_end); ++i) {
1117 u32 fourcc = extra_fourccs[i];
1119 if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
1120 continue; /* skip duplicate and native entries */
1121 } else if (fourccs == fourccs_end) {
1122 drm_warn(dev, "Ignoring emulated format %p4cc\n", &fourcc);
1123 continue; /* end of available output buffer */
1126 drm_dbg_kms(dev, "adding emulated format %p4cc\n", &fourcc);
1132 return fourccs - fourccs_out;
1134 EXPORT_SYMBOL(drm_fb_build_fourcc_list);