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_abgr8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
654 __le32 *dbuf32 = dbuf;
655 const __le32 *sbuf32 = sbuf;
659 for (x = 0; x < pixels; x++) {
660 pix = le32_to_cpu(sbuf32[x]);
661 pix = ((pix & 0x00ff0000) >> 16) << 0 |
662 ((pix & 0x0000ff00) >> 8) << 8 |
663 ((pix & 0x000000ff) >> 0) << 16 |
664 GENMASK(31, 24); /* fill alpha bits */
665 *dbuf32++ = cpu_to_le32(pix);
669 static void drm_fb_xrgb8888_to_abgr8888(struct iosys_map *dst, const unsigned int *dst_pitch,
670 const struct iosys_map *src,
671 const struct drm_framebuffer *fb,
672 const struct drm_rect *clip)
674 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
678 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
679 drm_fb_xrgb8888_to_abgr8888_line);
682 static void drm_fb_xrgb8888_to_xbgr8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
684 __le32 *dbuf32 = dbuf;
685 const __le32 *sbuf32 = sbuf;
689 for (x = 0; x < pixels; x++) {
690 pix = le32_to_cpu(sbuf32[x]);
691 pix = ((pix & 0x00ff0000) >> 16) << 0 |
692 ((pix & 0x0000ff00) >> 8) << 8 |
693 ((pix & 0x000000ff) >> 0) << 16 |
694 ((pix & 0xff000000) >> 24) << 24;
695 *dbuf32++ = cpu_to_le32(pix);
699 static void drm_fb_xrgb8888_to_xbgr8888(struct iosys_map *dst, const unsigned int *dst_pitch,
700 const struct iosys_map *src,
701 const struct drm_framebuffer *fb,
702 const struct drm_rect *clip)
704 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
708 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
709 drm_fb_xrgb8888_to_xbgr8888_line);
712 static void drm_fb_xrgb8888_to_xrgb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
714 __le32 *dbuf32 = dbuf;
715 const __le32 *sbuf32 = sbuf;
720 for (x = 0; x < pixels; x++) {
721 pix = le32_to_cpu(sbuf32[x]);
722 val32 = ((pix & 0x000000FF) << 2) |
723 ((pix & 0x0000FF00) << 4) |
724 ((pix & 0x00FF0000) << 6);
725 pix = val32 | ((val32 >> 8) & 0x00300C03);
726 *dbuf32++ = cpu_to_le32(pix);
731 * drm_fb_xrgb8888_to_xrgb2101010 - Convert XRGB8888 to XRGB2101010 clip buffer
732 * @dst: Array of XRGB2101010 destination buffers
733 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
734 * within @dst; can be NULL if scanlines are stored next to each other.
735 * @src: Array of XRGB8888 source buffers
736 * @fb: DRM framebuffer
737 * @clip: Clip rectangle area to copy
739 * This function copies parts of a framebuffer to display memory and converts the
740 * color format during the process. Destination and framebuffer formats must match. The
741 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
742 * least as many entries as there are planes in @fb's format. Each entry stores the
743 * value for the format's respective color plane at the same index.
745 * This function does not apply clipping on @dst (i.e. the destination is at the
748 * Drivers can use this function for XRGB2101010 devices that don't support XRGB8888
751 void drm_fb_xrgb8888_to_xrgb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
752 const struct iosys_map *src, const struct drm_framebuffer *fb,
753 const struct drm_rect *clip)
755 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
759 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
760 drm_fb_xrgb8888_to_xrgb2101010_line);
762 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010);
764 static void drm_fb_xrgb8888_to_argb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
766 __le32 *dbuf32 = dbuf;
767 const __le32 *sbuf32 = sbuf;
772 for (x = 0; x < pixels; x++) {
773 pix = le32_to_cpu(sbuf32[x]);
774 val32 = ((pix & 0x000000ff) << 2) |
775 ((pix & 0x0000ff00) << 4) |
776 ((pix & 0x00ff0000) << 6);
777 pix = GENMASK(31, 30) | /* set alpha bits */
778 val32 | ((val32 >> 8) & 0x00300c03);
779 *dbuf32++ = cpu_to_le32(pix);
784 * drm_fb_xrgb8888_to_argb2101010 - Convert XRGB8888 to ARGB2101010 clip buffer
785 * @dst: Array of ARGB2101010 destination buffers
786 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
787 * within @dst; can be NULL if scanlines are stored next to each other.
788 * @src: Array of XRGB8888 source buffers
789 * @fb: DRM framebuffer
790 * @clip: Clip rectangle area to copy
792 * This function copies parts of a framebuffer to display memory and converts
793 * the color format during the process. The parameters @dst, @dst_pitch and
794 * @src refer to arrays. Each array must have at least as many entries as
795 * there are planes in @fb's format. Each entry stores the value for the
796 * format's respective color plane at the same index.
798 * This function does not apply clipping on @dst (i.e. the destination is at the
801 * Drivers can use this function for ARGB2101010 devices that don't support XRGB8888
804 void drm_fb_xrgb8888_to_argb2101010(struct iosys_map *dst, const unsigned int *dst_pitch,
805 const struct iosys_map *src, const struct drm_framebuffer *fb,
806 const struct drm_rect *clip)
808 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
812 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
813 drm_fb_xrgb8888_to_argb2101010_line);
815 EXPORT_SYMBOL(drm_fb_xrgb8888_to_argb2101010);
817 static void drm_fb_xrgb8888_to_gray8_line(void *dbuf, const void *sbuf, unsigned int pixels)
820 const __le32 *sbuf32 = sbuf;
823 for (x = 0; x < pixels; x++) {
824 u32 pix = le32_to_cpu(sbuf32[x]);
825 u8 r = (pix & 0x00ff0000) >> 16;
826 u8 g = (pix & 0x0000ff00) >> 8;
827 u8 b = pix & 0x000000ff;
829 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
830 *dbuf8++ = (3 * r + 6 * g + b) / 10;
835 * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
836 * @dst: Array of 8-bit grayscale destination buffers
837 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
838 * within @dst; can be NULL if scanlines are stored next to each other.
839 * @src: Array of XRGB8888 source buffers
840 * @fb: DRM framebuffer
841 * @clip: Clip rectangle area to copy
843 * This function copies parts of a framebuffer to display memory and converts the
844 * color format during the process. Destination and framebuffer formats must match. The
845 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
846 * least as many entries as there are planes in @fb's format. Each entry stores the
847 * value for the format's respective color plane at the same index.
849 * This function does not apply clipping on @dst (i.e. the destination is at the
852 * DRM doesn't have native monochrome or grayscale support. Drivers can use this
853 * function for grayscale devices that don't support XRGB8888 natively.Such
854 * drivers can announce the commonly supported XR24 format to userspace and use
855 * this function to convert to the native format. Monochrome drivers will use the
856 * most significant bit, where 1 means foreground color and 0 background color.
857 * ITU BT.601 is being used for the RGB -> luma (brightness) conversion.
859 void drm_fb_xrgb8888_to_gray8(struct iosys_map *dst, const unsigned int *dst_pitch,
860 const struct iosys_map *src, const struct drm_framebuffer *fb,
861 const struct drm_rect *clip)
863 static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
867 drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false,
868 drm_fb_xrgb8888_to_gray8_line);
870 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);
873 * drm_fb_blit - Copy parts of a framebuffer to display memory
874 * @dst: Array of display-memory addresses to copy to
875 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
876 * within @dst; can be NULL if scanlines are stored next to each other.
877 * @dst_format: FOURCC code of the display's color format
878 * @src: The framebuffer memory to copy from
879 * @fb: The framebuffer to copy from
880 * @clip: Clip rectangle area to copy
882 * This function copies parts of a framebuffer to display memory. If the
883 * formats of the display and the framebuffer mismatch, the blit function
884 * will attempt to convert between them during the process. The parameters @dst,
885 * @dst_pitch and @src refer to arrays. Each array must have at least as many
886 * entries as there are planes in @dst_format's format. Each entry stores the
887 * value for the format's respective color plane at the same index.
889 * This function does not apply clipping on @dst (i.e. the destination is at the
894 * -EINVAL if the color-format conversion failed, or
895 * a negative error code otherwise.
897 int drm_fb_blit(struct iosys_map *dst, const unsigned int *dst_pitch, uint32_t dst_format,
898 const struct iosys_map *src, const struct drm_framebuffer *fb,
899 const struct drm_rect *clip)
901 uint32_t fb_format = fb->format->format;
903 if (fb_format == dst_format) {
904 drm_fb_memcpy(dst, dst_pitch, src, fb, clip);
906 } else if (fb_format == (dst_format | DRM_FORMAT_BIG_ENDIAN)) {
907 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
909 } else if (fb_format == (dst_format & ~DRM_FORMAT_BIG_ENDIAN)) {
910 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
912 } else if (fb_format == DRM_FORMAT_XRGB8888) {
913 if (dst_format == DRM_FORMAT_RGB565) {
914 drm_fb_xrgb8888_to_rgb565(dst, dst_pitch, src, fb, clip, false);
916 } else if (dst_format == DRM_FORMAT_XRGB1555) {
917 drm_fb_xrgb8888_to_xrgb1555(dst, dst_pitch, src, fb, clip);
919 } else if (dst_format == DRM_FORMAT_ARGB1555) {
920 drm_fb_xrgb8888_to_argb1555(dst, dst_pitch, src, fb, clip);
922 } else if (dst_format == DRM_FORMAT_RGBA5551) {
923 drm_fb_xrgb8888_to_rgba5551(dst, dst_pitch, src, fb, clip);
925 } else if (dst_format == DRM_FORMAT_RGB888) {
926 drm_fb_xrgb8888_to_rgb888(dst, dst_pitch, src, fb, clip);
928 } else if (dst_format == DRM_FORMAT_ARGB8888) {
929 drm_fb_xrgb8888_to_argb8888(dst, dst_pitch, src, fb, clip);
931 } else if (dst_format == DRM_FORMAT_XBGR8888) {
932 drm_fb_xrgb8888_to_xbgr8888(dst, dst_pitch, src, fb, clip);
934 } else if (dst_format == DRM_FORMAT_ABGR8888) {
935 drm_fb_xrgb8888_to_abgr8888(dst, dst_pitch, src, fb, clip);
937 } else if (dst_format == DRM_FORMAT_XRGB2101010) {
938 drm_fb_xrgb8888_to_xrgb2101010(dst, dst_pitch, src, fb, clip);
940 } else if (dst_format == DRM_FORMAT_ARGB2101010) {
941 drm_fb_xrgb8888_to_argb2101010(dst, dst_pitch, src, fb, clip);
943 } else if (dst_format == DRM_FORMAT_BGRX8888) {
944 drm_fb_swab(dst, dst_pitch, src, fb, clip, false);
949 drm_warn_once(fb->dev, "No conversion helper from %p4cc to %p4cc found.\n",
950 &fb_format, &dst_format);
954 EXPORT_SYMBOL(drm_fb_blit);
956 static void drm_fb_gray8_to_mono_line(void *dbuf, const void *sbuf, unsigned int pixels)
959 const u8 *sbuf8 = sbuf;
962 unsigned int i, bits = min(pixels, 8U);
965 for (i = 0; i < bits; i++, pixels--) {
974 * drm_fb_xrgb8888_to_mono - Convert XRGB8888 to monochrome
975 * @dst: Array of monochrome destination buffers (0=black, 1=white)
976 * @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
977 * within @dst; can be NULL if scanlines are stored next to each other.
978 * @src: Array of XRGB8888 source buffers
979 * @fb: DRM framebuffer
980 * @clip: Clip rectangle area to copy
982 * This function copies parts of a framebuffer to display memory and converts the
983 * color format during the process. Destination and framebuffer formats must match. The
984 * parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
985 * least as many entries as there are planes in @fb's format. Each entry stores the
986 * value for the format's respective color plane at the same index.
988 * This function does not apply clipping on @dst (i.e. the destination is at the
989 * top-left corner). The first pixel (upper left corner of the clip rectangle) will
990 * be converted and copied to the first bit (LSB) in the first byte of the monochrome
991 * destination buffer. If the caller requires that the first pixel in a byte must
992 * be located at an x-coordinate that is a multiple of 8, then the caller must take
993 * care itself of supplying a suitable clip rectangle.
995 * DRM doesn't have native monochrome support. Drivers can use this function for
996 * monochrome devices that don't support XRGB8888 natively. Such drivers can
997 * announce the commonly supported XR24 format to userspace and use this function
998 * to convert to the native format.
1000 * This function uses drm_fb_xrgb8888_to_gray8() to convert to grayscale and
1001 * then the result is converted from grayscale to monochrome.
1003 void drm_fb_xrgb8888_to_mono(struct iosys_map *dst, const unsigned int *dst_pitch,
1004 const struct iosys_map *src, const struct drm_framebuffer *fb,
1005 const struct drm_rect *clip)
1007 static const unsigned int default_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
1010 unsigned int linepixels = drm_rect_width(clip);
1011 unsigned int lines = drm_rect_height(clip);
1012 unsigned int cpp = fb->format->cpp[0];
1013 unsigned int len_src32 = linepixels * cpp;
1014 struct drm_device *dev = fb->dev;
1015 void *vaddr = src[0].vaddr;
1016 unsigned int dst_pitch_0;
1018 u8 *mono = dst[0].vaddr, *gray8;
1021 if (drm_WARN_ON(dev, fb->format->format != DRM_FORMAT_XRGB8888))
1025 dst_pitch = default_dst_pitch;
1026 dst_pitch_0 = dst_pitch[0];
1029 * The mono destination buffer contains 1 bit per pixel
1032 dst_pitch_0 = DIV_ROUND_UP(linepixels, 8);
1035 * The dma memory is write-combined so reads are uncached.
1036 * Speed up by fetching one line at a time.
1038 * Also, format conversion from XR24 to monochrome are done
1039 * line-by-line but are converted to 8-bit grayscale as an
1040 * intermediate step.
1042 * Allocate a buffer to be used for both copying from the cma
1043 * memory and to store the intermediate grayscale line pixels.
1045 src32 = kmalloc(len_src32 + linepixels, GFP_KERNEL);
1049 gray8 = (u8 *)src32 + len_src32;
1051 vaddr += clip_offset(clip, fb->pitches[0], cpp);
1052 for (y = 0; y < lines; y++) {
1053 src32 = memcpy(src32, vaddr, len_src32);
1054 drm_fb_xrgb8888_to_gray8_line(gray8, src32, linepixels);
1055 drm_fb_gray8_to_mono_line(mono, gray8, linepixels);
1056 vaddr += fb->pitches[0];
1057 mono += dst_pitch_0;
1062 EXPORT_SYMBOL(drm_fb_xrgb8888_to_mono);
1064 static uint32_t drm_fb_nonalpha_fourcc(uint32_t fourcc)
1066 /* only handle formats with depth != 0 and alpha channel */
1068 case DRM_FORMAT_ARGB1555:
1069 return DRM_FORMAT_XRGB1555;
1070 case DRM_FORMAT_ABGR1555:
1071 return DRM_FORMAT_XBGR1555;
1072 case DRM_FORMAT_RGBA5551:
1073 return DRM_FORMAT_RGBX5551;
1074 case DRM_FORMAT_BGRA5551:
1075 return DRM_FORMAT_BGRX5551;
1076 case DRM_FORMAT_ARGB8888:
1077 return DRM_FORMAT_XRGB8888;
1078 case DRM_FORMAT_ABGR8888:
1079 return DRM_FORMAT_XBGR8888;
1080 case DRM_FORMAT_RGBA8888:
1081 return DRM_FORMAT_RGBX8888;
1082 case DRM_FORMAT_BGRA8888:
1083 return DRM_FORMAT_BGRX8888;
1084 case DRM_FORMAT_ARGB2101010:
1085 return DRM_FORMAT_XRGB2101010;
1086 case DRM_FORMAT_ABGR2101010:
1087 return DRM_FORMAT_XBGR2101010;
1088 case DRM_FORMAT_RGBA1010102:
1089 return DRM_FORMAT_RGBX1010102;
1090 case DRM_FORMAT_BGRA1010102:
1091 return DRM_FORMAT_BGRX1010102;
1097 static bool is_listed_fourcc(const uint32_t *fourccs, size_t nfourccs, uint32_t fourcc)
1099 const uint32_t *fourccs_end = fourccs + nfourccs;
1101 while (fourccs < fourccs_end) {
1102 if (*fourccs == fourcc)
1110 * drm_fb_build_fourcc_list - Filters a list of supported color formats against
1111 * the device's native formats
1113 * @native_fourccs: 4CC codes of natively supported color formats
1114 * @native_nfourccs: The number of entries in @native_fourccs
1115 * @fourccs_out: Returns 4CC codes of supported color formats
1116 * @nfourccs_out: The number of available entries in @fourccs_out
1118 * This function create a list of supported color format from natively
1119 * supported formats and additional emulated formats.
1120 * At a minimum, most userspace programs expect at least support for
1121 * XRGB8888 on the primary plane. Devices that have to emulate the
1122 * format, and possibly others, can use drm_fb_build_fourcc_list() to
1123 * create a list of supported color formats. The returned list can
1124 * be handed over to drm_universal_plane_init() et al. Native formats
1125 * will go before emulated formats. Native formats with alpha channel
1126 * will be replaced by such without, as primary planes usually don't
1127 * support alpha. Other heuristics might be applied
1128 * to optimize the order. Formats near the beginning of the list are
1129 * usually preferred over formats near the end of the list.
1132 * The number of color-formats 4CC codes returned in @fourccs_out.
1134 size_t drm_fb_build_fourcc_list(struct drm_device *dev,
1135 const u32 *native_fourccs, size_t native_nfourccs,
1136 u32 *fourccs_out, size_t nfourccs_out)
1139 * XRGB8888 is the default fallback format for most of userspace
1140 * and it's currently the only format that should be emulated for
1141 * the primary plane. Only if there's ever another default fallback,
1142 * it should be added here.
1144 static const uint32_t extra_fourccs[] = {
1145 DRM_FORMAT_XRGB8888,
1147 static const size_t extra_nfourccs = ARRAY_SIZE(extra_fourccs);
1149 u32 *fourccs = fourccs_out;
1150 const u32 *fourccs_end = fourccs_out + nfourccs_out;
1154 * The device's native formats go first.
1157 for (i = 0; i < native_nfourccs; ++i) {
1159 * Several DTs, boot loaders and firmware report native
1160 * alpha formats that are non-alpha formats instead. So
1161 * replace alpha formats by non-alpha formats.
1163 u32 fourcc = drm_fb_nonalpha_fourcc(native_fourccs[i]);
1165 if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
1166 continue; /* skip duplicate entries */
1167 } else if (fourccs == fourccs_end) {
1168 drm_warn(dev, "Ignoring native format %p4cc\n", &fourcc);
1169 continue; /* end of available output buffer */
1172 drm_dbg_kms(dev, "adding native format %p4cc\n", &fourcc);
1179 * The extra formats, emulated by the driver, go second.
1182 for (i = 0; (i < extra_nfourccs) && (fourccs < fourccs_end); ++i) {
1183 u32 fourcc = extra_fourccs[i];
1185 if (is_listed_fourcc(fourccs_out, fourccs - fourccs_out, fourcc)) {
1186 continue; /* skip duplicate and native entries */
1187 } else if (fourccs == fourccs_end) {
1188 drm_warn(dev, "Ignoring emulated format %p4cc\n", &fourcc);
1189 continue; /* end of available output buffer */
1192 drm_dbg_kms(dev, "adding emulated format %p4cc\n", &fourcc);
1198 return fourccs - fourccs_out;
1200 EXPORT_SYMBOL(drm_fb_build_fourcc_list);