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.
11 #include <linux/module.h>
12 #include <linux/slab.h>
15 #include <drm/drm_device.h>
16 #include <drm/drm_format_helper.h>
17 #include <drm/drm_framebuffer.h>
18 #include <drm/drm_fourcc.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_rect.h>
22 static unsigned int clip_offset(const struct drm_rect *clip, unsigned int pitch, unsigned int cpp)
24 return clip->y1 * pitch + clip->x1 * cpp;
28 * drm_fb_clip_offset - Returns the clipping rectangles byte-offset in a framebuffer
29 * @pitch: Framebuffer line pitch in byte
30 * @format: Framebuffer format
31 * @clip: Clip rectangle
34 * The byte offset of the clip rectangle's top-left corner within the framebuffer.
36 unsigned int drm_fb_clip_offset(unsigned int pitch, const struct drm_format_info *format,
37 const struct drm_rect *clip)
39 return clip_offset(clip, pitch, format->cpp[0]);
41 EXPORT_SYMBOL(drm_fb_clip_offset);
44 * drm_fb_memcpy - Copy clip buffer
45 * @dst: Destination buffer
46 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
47 * @vaddr: Source buffer
48 * @fb: DRM framebuffer
49 * @clip: Clip rectangle area to copy
51 * This function does not apply clipping on dst, i.e. the destination
52 * is at the top-left corner.
54 void drm_fb_memcpy(void *dst, unsigned int dst_pitch, const void *vaddr,
55 const struct drm_framebuffer *fb, const struct drm_rect *clip)
57 unsigned int cpp = fb->format->cpp[0];
58 size_t len = (clip->x2 - clip->x1) * cpp;
59 unsigned int y, lines = clip->y2 - clip->y1;
64 vaddr += clip_offset(clip, fb->pitches[0], cpp);
65 for (y = 0; y < lines; y++) {
66 memcpy(dst, vaddr, len);
67 vaddr += fb->pitches[0];
71 EXPORT_SYMBOL(drm_fb_memcpy);
74 * drm_fb_memcpy_toio - Copy clip buffer
75 * @dst: Destination buffer (iomem)
76 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
77 * @vaddr: Source buffer
78 * @fb: DRM framebuffer
79 * @clip: Clip rectangle area to copy
81 * This function does not apply clipping on dst, i.e. the destination
82 * is at the top-left corner.
84 void drm_fb_memcpy_toio(void __iomem *dst, unsigned int dst_pitch, const void *vaddr,
85 const struct drm_framebuffer *fb, const struct drm_rect *clip)
87 unsigned int cpp = fb->format->cpp[0];
88 size_t len = (clip->x2 - clip->x1) * cpp;
89 unsigned int y, lines = clip->y2 - clip->y1;
94 vaddr += clip_offset(clip, fb->pitches[0], cpp);
95 for (y = 0; y < lines; y++) {
96 memcpy_toio(dst, vaddr, len);
97 vaddr += fb->pitches[0];
101 EXPORT_SYMBOL(drm_fb_memcpy_toio);
104 * drm_fb_swab - Swap bytes into clip buffer
105 * @dst: Destination buffer
106 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
107 * @src: Source buffer
108 * @fb: DRM framebuffer
109 * @clip: Clip rectangle area to copy
110 * @cached: Source buffer is mapped cached (eg. not write-combined)
112 * If @cached is false a temporary buffer is used to cache one pixel line at a
113 * time to speed up slow uncached reads.
115 * This function does not apply clipping on dst, i.e. the destination
116 * is at the top-left corner.
118 void drm_fb_swab(void *dst, unsigned int dst_pitch, const void *src,
119 const struct drm_framebuffer *fb, const struct drm_rect *clip,
122 u8 cpp = fb->format->cpp[0];
123 size_t len = drm_rect_width(clip) * cpp;
131 if (WARN_ON_ONCE(cpp != 2 && cpp != 4))
138 buf = kmalloc(len, GFP_KERNEL);
140 src += clip_offset(clip, fb->pitches[0], cpp);
142 for (y = clip->y1; y < clip->y2; y++) {
144 memcpy(buf, src, len);
155 for (x = clip->x1; x < clip->x2; x++) {
157 *dst32++ = swab32(*src32++);
159 *dst16++ = swab16(*src16++);
162 src += fb->pitches[0];
168 EXPORT_SYMBOL(drm_fb_swab);
170 static void drm_fb_xrgb8888_to_rgb332_line(u8 *dbuf, const __le32 *sbuf, unsigned int pixels)
175 for (x = 0; x < pixels; x++) {
176 pix = le32_to_cpu(sbuf[x]);
177 dbuf[x] = ((pix & 0x00e00000) >> 16) |
178 ((pix & 0x0000e000) >> 11) |
179 ((pix & 0x000000c0) >> 6);
184 * drm_fb_xrgb8888_to_rgb332 - Convert XRGB8888 to RGB332 clip buffer
185 * @dst: RGB332 destination buffer
186 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
187 * @src: XRGB8888 source buffer
188 * @fb: DRM framebuffer
189 * @clip: Clip rectangle area to copy
191 * Drivers can use this function for RGB332 devices that don't natively support XRGB8888.
193 void drm_fb_xrgb8888_to_rgb332(void *dst, unsigned int dst_pitch, const void *src,
194 const struct drm_framebuffer *fb, const struct drm_rect *clip)
196 size_t width = drm_rect_width(clip);
197 size_t src_len = width * sizeof(u32);
204 /* Use a buffer to speed up access on buffers with uncached read mapping (i.e. WC) */
205 sbuf = kmalloc(src_len, GFP_KERNEL);
209 src += clip_offset(clip, fb->pitches[0], sizeof(u32));
210 for (y = 0; y < drm_rect_height(clip); y++) {
211 memcpy(sbuf, src, src_len);
212 drm_fb_xrgb8888_to_rgb332_line(dst, sbuf, width);
213 src += fb->pitches[0];
219 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb332);
221 static void drm_fb_xrgb8888_to_rgb565_line(u16 *dbuf, const u32 *sbuf,
228 for (x = 0; x < pixels; x++) {
229 val16 = ((sbuf[x] & 0x00F80000) >> 8) |
230 ((sbuf[x] & 0x0000FC00) >> 5) |
231 ((sbuf[x] & 0x000000F8) >> 3);
233 dbuf[x] = swab16(val16);
240 * drm_fb_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
241 * @dst: RGB565 destination buffer
242 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
243 * @vaddr: XRGB8888 source buffer
244 * @fb: DRM framebuffer
245 * @clip: Clip rectangle area to copy
248 * Drivers can use this function for RGB565 devices that don't natively
251 void drm_fb_xrgb8888_to_rgb565(void *dst, unsigned int dst_pitch, const void *vaddr,
252 const struct drm_framebuffer *fb, const struct drm_rect *clip,
255 size_t linepixels = clip->x2 - clip->x1;
256 size_t src_len = linepixels * sizeof(u32);
257 size_t dst_len = linepixels * sizeof(u16);
258 unsigned y, lines = clip->y2 - clip->y1;
265 * The cma memory is write-combined so reads are uncached.
266 * Speed up by fetching one line at a time.
268 sbuf = kmalloc(src_len, GFP_KERNEL);
272 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
273 for (y = 0; y < lines; y++) {
274 memcpy(sbuf, vaddr, src_len);
275 drm_fb_xrgb8888_to_rgb565_line(dst, sbuf, linepixels, swab);
276 vaddr += fb->pitches[0];
282 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);
285 * drm_fb_xrgb8888_to_rgb565_toio - Convert XRGB8888 to RGB565 clip buffer
286 * @dst: RGB565 destination buffer (iomem)
287 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
288 * @vaddr: XRGB8888 source buffer
289 * @fb: DRM framebuffer
290 * @clip: Clip rectangle area to copy
293 * Drivers can use this function for RGB565 devices that don't natively
296 void drm_fb_xrgb8888_to_rgb565_toio(void __iomem *dst, unsigned int dst_pitch,
297 const void *vaddr, const struct drm_framebuffer *fb,
298 const struct drm_rect *clip, bool swab)
300 size_t linepixels = clip->x2 - clip->x1;
301 size_t dst_len = linepixels * sizeof(u16);
302 unsigned y, lines = clip->y2 - clip->y1;
308 dbuf = kmalloc(dst_len, GFP_KERNEL);
312 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
313 for (y = 0; y < lines; y++) {
314 drm_fb_xrgb8888_to_rgb565_line(dbuf, vaddr, linepixels, swab);
315 memcpy_toio(dst, dbuf, dst_len);
316 vaddr += fb->pitches[0];
322 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565_toio);
324 static void drm_fb_xrgb8888_to_rgb888_line(u8 *dbuf, const u32 *sbuf,
329 for (x = 0; x < pixels; x++) {
330 *dbuf++ = (sbuf[x] & 0x000000FF) >> 0;
331 *dbuf++ = (sbuf[x] & 0x0000FF00) >> 8;
332 *dbuf++ = (sbuf[x] & 0x00FF0000) >> 16;
337 * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
338 * @dst: RGB888 destination buffer
339 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
340 * @src: XRGB8888 source buffer
341 * @fb: DRM framebuffer
342 * @clip: Clip rectangle area to copy
344 * Drivers can use this function for RGB888 devices that don't natively
347 void drm_fb_xrgb8888_to_rgb888(void *dst, unsigned int dst_pitch, const void *src,
348 const struct drm_framebuffer *fb, const struct drm_rect *clip)
350 size_t width = drm_rect_width(clip);
351 size_t src_len = width * sizeof(u32);
356 dst_pitch = width * 3;
358 /* Use a buffer to speed up access on buffers with uncached read mapping (i.e. WC) */
359 sbuf = kmalloc(src_len, GFP_KERNEL);
363 src += clip_offset(clip, fb->pitches[0], sizeof(u32));
364 for (y = 0; y < drm_rect_height(clip); y++) {
365 memcpy(sbuf, src, src_len);
366 drm_fb_xrgb8888_to_rgb888_line(dst, sbuf, width);
367 src += fb->pitches[0];
373 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
376 * drm_fb_xrgb8888_to_rgb888_toio - Convert XRGB8888 to RGB888 clip buffer
377 * @dst: RGB565 destination buffer (iomem)
378 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
379 * @vaddr: XRGB8888 source buffer
380 * @fb: DRM framebuffer
381 * @clip: Clip rectangle area to copy
383 * Drivers can use this function for RGB888 devices that don't natively
386 void drm_fb_xrgb8888_to_rgb888_toio(void __iomem *dst, unsigned int dst_pitch,
387 const void *vaddr, const struct drm_framebuffer *fb,
388 const struct drm_rect *clip)
390 size_t linepixels = clip->x2 - clip->x1;
391 size_t dst_len = linepixels * 3;
392 unsigned y, lines = clip->y2 - clip->y1;
398 dbuf = kmalloc(dst_len, GFP_KERNEL);
402 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
403 for (y = 0; y < lines; y++) {
404 drm_fb_xrgb8888_to_rgb888_line(dbuf, vaddr, linepixels);
405 memcpy_toio(dst, dbuf, dst_len);
406 vaddr += fb->pitches[0];
412 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888_toio);
414 static void drm_fb_xrgb8888_to_xrgb2101010_line(u32 *dbuf, const u32 *sbuf,
420 for (x = 0; x < pixels; x++) {
421 val32 = ((sbuf[x] & 0x000000FF) << 2) |
422 ((sbuf[x] & 0x0000FF00) << 4) |
423 ((sbuf[x] & 0x00FF0000) << 6);
424 *dbuf++ = val32 | ((val32 >> 8) & 0x00300C03);
429 * drm_fb_xrgb8888_to_xrgb2101010_toio - Convert XRGB8888 to XRGB2101010 clip
431 * @dst: XRGB2101010 destination buffer (iomem)
432 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
433 * @vaddr: XRGB8888 source buffer
434 * @fb: DRM framebuffer
435 * @clip: Clip rectangle area to copy
437 * Drivers can use this function for XRGB2101010 devices that don't natively
440 void drm_fb_xrgb8888_to_xrgb2101010_toio(void __iomem *dst,
441 unsigned int dst_pitch, const void *vaddr,
442 const struct drm_framebuffer *fb,
443 const struct drm_rect *clip)
445 size_t linepixels = clip->x2 - clip->x1;
446 size_t dst_len = linepixels * sizeof(u32);
447 unsigned int y, lines = clip->y2 - clip->y1;
453 dbuf = kmalloc(dst_len, GFP_KERNEL);
457 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
458 for (y = 0; y < lines; y++) {
459 drm_fb_xrgb8888_to_xrgb2101010_line(dbuf, vaddr, linepixels);
460 memcpy_toio(dst, dbuf, dst_len);
461 vaddr += fb->pitches[0];
467 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010_toio);
469 static void drm_fb_xrgb8888_to_gray8_line(u8 *dst, const u32 *src, unsigned int pixels)
473 for (x = 0; x < pixels; x++) {
474 u8 r = (*src & 0x00ff0000) >> 16;
475 u8 g = (*src & 0x0000ff00) >> 8;
476 u8 b = *src & 0x000000ff;
478 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
479 *dst++ = (3 * r + 6 * g + b) / 10;
485 * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
486 * @dst: 8-bit grayscale destination buffer
487 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
488 * @vaddr: XRGB8888 source buffer
489 * @fb: DRM framebuffer
490 * @clip: Clip rectangle area to copy
492 * Drm doesn't have native monochrome or grayscale support.
493 * Such drivers can announce the commonly supported XR24 format to userspace
494 * and use this function to convert to the native format.
496 * Monochrome drivers will use the most significant bit,
497 * where 1 means foreground color and 0 background color.
499 * ITU BT.601 is used for the RGB -> luma (brightness) conversion.
501 void drm_fb_xrgb8888_to_gray8(void *dst, unsigned int dst_pitch, const void *vaddr,
502 const struct drm_framebuffer *fb, const struct drm_rect *clip)
504 unsigned int linepixels = clip->x2 - clip->x1;
505 unsigned int len = linepixels * sizeof(u32);
511 if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
515 dst_pitch = drm_rect_width(clip);
518 * The cma memory is write-combined so reads are uncached.
519 * Speed up by fetching one line at a time.
521 buf = kmalloc(len, GFP_KERNEL);
525 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
526 for (y = clip->y1; y < clip->y2; y++) {
528 src32 = memcpy(buf, vaddr, len);
529 drm_fb_xrgb8888_to_gray8_line(dst8, src32, linepixels);
530 vaddr += fb->pitches[0];
536 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);
539 * drm_fb_blit_toio - Copy parts of a framebuffer to display memory
540 * @dst: The display memory to copy to
541 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
542 * @dst_format: FOURCC code of the display's color format
543 * @vmap: The framebuffer memory to copy from
544 * @fb: The framebuffer to copy from
545 * @clip: Clip rectangle area to copy
547 * This function copies parts of a framebuffer to display memory. If the
548 * formats of the display and the framebuffer mismatch, the blit function
549 * will attempt to convert between them.
553 * -EINVAL if the color-format conversion failed, or
554 * a negative error code otherwise.
556 int drm_fb_blit_toio(void __iomem *dst, unsigned int dst_pitch, uint32_t dst_format,
557 const void *vmap, const struct drm_framebuffer *fb,
558 const struct drm_rect *clip)
560 uint32_t fb_format = fb->format->format;
562 /* treat alpha channel like filler bits */
563 if (fb_format == DRM_FORMAT_ARGB8888)
564 fb_format = DRM_FORMAT_XRGB8888;
565 if (dst_format == DRM_FORMAT_ARGB8888)
566 dst_format = DRM_FORMAT_XRGB8888;
567 if (fb_format == DRM_FORMAT_ARGB2101010)
568 fb_format = DRM_FORMAT_XRGB2101010;
569 if (dst_format == DRM_FORMAT_ARGB2101010)
570 dst_format = DRM_FORMAT_XRGB2101010;
572 if (dst_format == fb_format) {
573 drm_fb_memcpy_toio(dst, dst_pitch, vmap, fb, clip);
576 } else if (dst_format == DRM_FORMAT_RGB565) {
577 if (fb_format == DRM_FORMAT_XRGB8888) {
578 drm_fb_xrgb8888_to_rgb565_toio(dst, dst_pitch, vmap, fb, clip, false);
581 } else if (dst_format == DRM_FORMAT_RGB888) {
582 if (fb_format == DRM_FORMAT_XRGB8888) {
583 drm_fb_xrgb8888_to_rgb888_toio(dst, dst_pitch, vmap, fb, clip);
586 } else if (dst_format == DRM_FORMAT_XRGB2101010) {
587 if (fb_format == DRM_FORMAT_XRGB8888) {
588 drm_fb_xrgb8888_to_xrgb2101010_toio(dst, dst_pitch, vmap, fb, clip);
595 EXPORT_SYMBOL(drm_fb_blit_toio);
597 static void drm_fb_gray8_to_mono_reversed_line(u8 *dst, const u8 *src, unsigned int pixels,
598 unsigned int start_offset, unsigned int end_len)
602 for (xb = 0; xb < pixels; xb++) {
603 unsigned int start = 0, end = 8;
606 if (xb == 0 && start_offset)
607 start = start_offset;
609 if (xb == pixels - 1 && end_len)
612 for (i = start; i < end; i++) {
613 unsigned int x = xb * 8 + i;
624 * drm_fb_xrgb8888_to_mono_reversed - Convert XRGB8888 to reversed monochrome
625 * @dst: reversed monochrome destination buffer
626 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
627 * @src: XRGB8888 source buffer
628 * @fb: DRM framebuffer
629 * @clip: Clip rectangle area to copy
631 * DRM doesn't have native monochrome support.
632 * Such drivers can announce the commonly supported XR24 format to userspace
633 * and use this function to convert to the native format.
635 * This function uses drm_fb_xrgb8888_to_gray8() to convert to grayscale and
636 * then the result is converted from grayscale to reversed monohrome.
638 void drm_fb_xrgb8888_to_mono_reversed(void *dst, unsigned int dst_pitch, const void *vaddr,
639 const struct drm_framebuffer *fb, const struct drm_rect *clip)
641 unsigned int linepixels = drm_rect_width(clip);
642 unsigned int lines = clip->y2 - clip->y1;
643 unsigned int cpp = fb->format->cpp[0];
644 unsigned int len_src32 = linepixels * cpp;
645 struct drm_device *dev = fb->dev;
646 unsigned int start_offset, end_len;
648 u8 *mono = dst, *gray8;
651 if (drm_WARN_ON(dev, fb->format->format != DRM_FORMAT_XRGB8888))
655 * The reversed mono destination buffer contains 1 bit per pixel
656 * and destination scanlines have to be in multiple of 8 pixels.
659 dst_pitch = DIV_ROUND_UP(linepixels, 8);
661 drm_WARN_ONCE(dev, dst_pitch % 8 != 0, "dst_pitch is not a multiple of 8\n");
664 * The cma memory is write-combined so reads are uncached.
665 * Speed up by fetching one line at a time.
667 * Also, format conversion from XR24 to reversed monochrome
668 * are done line-by-line but are converted to 8-bit grayscale
669 * as an intermediate step.
671 * Allocate a buffer to be used for both copying from the cma
672 * memory and to store the intermediate grayscale line pixels.
674 src32 = kmalloc(len_src32 + linepixels, GFP_KERNEL);
678 gray8 = (u8 *)src32 + len_src32;
681 * For damage handling, it is possible that only parts of the source
682 * buffer is copied and this could lead to start and end pixels that
683 * are not aligned to multiple of 8.
685 * Calculate if the start and end pixels are not aligned and set the
686 * offsets for the reversed mono line conversion function to adjust.
688 start_offset = clip->x1 % 8;
689 end_len = clip->x2 % 8;
691 vaddr += clip_offset(clip, fb->pitches[0], cpp);
692 for (y = 0; y < lines; y++) {
693 src32 = memcpy(src32, vaddr, len_src32);
694 drm_fb_xrgb8888_to_gray8_line(gray8, src32, linepixels);
695 drm_fb_gray8_to_mono_reversed_line(mono, gray8, dst_pitch,
696 start_offset, end_len);
697 vaddr += fb->pitches[0];
703 EXPORT_SYMBOL(drm_fb_xrgb8888_to_mono_reversed);