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_format_helper.h>
16 #include <drm/drm_framebuffer.h>
17 #include <drm/drm_fourcc.h>
18 #include <drm/drm_rect.h>
20 static unsigned int clip_offset(const struct drm_rect *clip, unsigned int pitch, unsigned int cpp)
22 return clip->y1 * pitch + clip->x1 * cpp;
26 * drm_fb_clip_offset - Returns the clipping rectangles byte-offset in a framebuffer
27 * @pitch: Framebuffer line pitch in byte
28 * @format: Framebuffer format
29 * @clip: Clip rectangle
32 * The byte offset of the clip rectangle's top-left corner within the framebuffer.
34 unsigned int drm_fb_clip_offset(unsigned int pitch, const struct drm_format_info *format,
35 const struct drm_rect *clip)
37 return clip_offset(clip, pitch, format->cpp[0]);
39 EXPORT_SYMBOL(drm_fb_clip_offset);
42 * drm_fb_memcpy - Copy clip buffer
43 * @dst: Destination buffer
44 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
45 * @vaddr: Source buffer
46 * @fb: DRM framebuffer
47 * @clip: Clip rectangle area to copy
49 * This function does not apply clipping on dst, i.e. the destination
50 * is at the top-left corner.
52 void drm_fb_memcpy(void *dst, unsigned int dst_pitch, const void *vaddr,
53 const struct drm_framebuffer *fb, const struct drm_rect *clip)
55 unsigned int cpp = fb->format->cpp[0];
56 size_t len = (clip->x2 - clip->x1) * cpp;
57 unsigned int y, lines = clip->y2 - clip->y1;
62 vaddr += clip_offset(clip, fb->pitches[0], cpp);
63 for (y = 0; y < lines; y++) {
64 memcpy(dst, vaddr, len);
65 vaddr += fb->pitches[0];
69 EXPORT_SYMBOL(drm_fb_memcpy);
72 * drm_fb_memcpy_toio - Copy clip buffer
73 * @dst: Destination buffer (iomem)
74 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
75 * @vaddr: Source buffer
76 * @fb: DRM framebuffer
77 * @clip: Clip rectangle area to copy
79 * This function does not apply clipping on dst, i.e. the destination
80 * is at the top-left corner.
82 void drm_fb_memcpy_toio(void __iomem *dst, unsigned int dst_pitch, const void *vaddr,
83 const struct drm_framebuffer *fb, const struct drm_rect *clip)
85 unsigned int cpp = fb->format->cpp[0];
86 size_t len = (clip->x2 - clip->x1) * cpp;
87 unsigned int y, lines = clip->y2 - clip->y1;
92 vaddr += clip_offset(clip, fb->pitches[0], cpp);
93 for (y = 0; y < lines; y++) {
94 memcpy_toio(dst, vaddr, len);
95 vaddr += fb->pitches[0];
99 EXPORT_SYMBOL(drm_fb_memcpy_toio);
102 * drm_fb_swab - Swap bytes into clip buffer
103 * @dst: Destination buffer
104 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
105 * @src: Source buffer
106 * @fb: DRM framebuffer
107 * @clip: Clip rectangle area to copy
108 * @cached: Source buffer is mapped cached (eg. not write-combined)
110 * If @cached is false a temporary buffer is used to cache one pixel line at a
111 * time to speed up slow uncached reads.
113 * This function does not apply clipping on dst, i.e. the destination
114 * is at the top-left corner.
116 void drm_fb_swab(void *dst, unsigned int dst_pitch, const void *src,
117 const struct drm_framebuffer *fb, const struct drm_rect *clip,
120 u8 cpp = fb->format->cpp[0];
121 size_t len = drm_rect_width(clip) * cpp;
129 if (WARN_ON_ONCE(cpp != 2 && cpp != 4))
136 buf = kmalloc(len, GFP_KERNEL);
138 src += clip_offset(clip, fb->pitches[0], cpp);
140 for (y = clip->y1; y < clip->y2; y++) {
142 memcpy(buf, src, len);
153 for (x = clip->x1; x < clip->x2; x++) {
155 *dst32++ = swab32(*src32++);
157 *dst16++ = swab16(*src16++);
160 src += fb->pitches[0];
166 EXPORT_SYMBOL(drm_fb_swab);
168 static void drm_fb_xrgb8888_to_rgb332_line(u8 *dbuf, const __le32 *sbuf, unsigned int pixels)
173 for (x = 0; x < pixels; x++) {
174 pix = le32_to_cpu(sbuf[x]);
175 dbuf[x] = ((pix & 0x00e00000) >> 16) |
176 ((pix & 0x0000e000) >> 11) |
177 ((pix & 0x000000c0) >> 6);
182 * drm_fb_xrgb8888_to_rgb332 - Convert XRGB8888 to RGB332 clip buffer
183 * @dst: RGB332 destination buffer
184 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
185 * @src: XRGB8888 source buffer
186 * @fb: DRM framebuffer
187 * @clip: Clip rectangle area to copy
189 * Drivers can use this function for RGB332 devices that don't natively support XRGB8888.
191 void drm_fb_xrgb8888_to_rgb332(void *dst, unsigned int dst_pitch, const void *src,
192 const struct drm_framebuffer *fb, const struct drm_rect *clip)
194 size_t width = drm_rect_width(clip);
195 size_t src_len = width * sizeof(u32);
202 /* Use a buffer to speed up access on buffers with uncached read mapping (i.e. WC) */
203 sbuf = kmalloc(src_len, GFP_KERNEL);
207 src += clip_offset(clip, fb->pitches[0], sizeof(u32));
208 for (y = 0; y < drm_rect_height(clip); y++) {
209 memcpy(sbuf, src, src_len);
210 drm_fb_xrgb8888_to_rgb332_line(dst, sbuf, width);
211 src += fb->pitches[0];
217 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb332);
219 static void drm_fb_xrgb8888_to_rgb565_line(u16 *dbuf, const u32 *sbuf,
226 for (x = 0; x < pixels; x++) {
227 val16 = ((sbuf[x] & 0x00F80000) >> 8) |
228 ((sbuf[x] & 0x0000FC00) >> 5) |
229 ((sbuf[x] & 0x000000F8) >> 3);
231 dbuf[x] = swab16(val16);
238 * drm_fb_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
239 * @dst: RGB565 destination buffer
240 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
241 * @vaddr: XRGB8888 source buffer
242 * @fb: DRM framebuffer
243 * @clip: Clip rectangle area to copy
246 * Drivers can use this function for RGB565 devices that don't natively
249 void drm_fb_xrgb8888_to_rgb565(void *dst, unsigned int dst_pitch, const void *vaddr,
250 const struct drm_framebuffer *fb, const struct drm_rect *clip,
253 size_t linepixels = clip->x2 - clip->x1;
254 size_t src_len = linepixels * sizeof(u32);
255 size_t dst_len = linepixels * sizeof(u16);
256 unsigned y, lines = clip->y2 - clip->y1;
263 * The cma memory is write-combined so reads are uncached.
264 * Speed up by fetching one line at a time.
266 sbuf = kmalloc(src_len, GFP_KERNEL);
270 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
271 for (y = 0; y < lines; y++) {
272 memcpy(sbuf, vaddr, src_len);
273 drm_fb_xrgb8888_to_rgb565_line(dst, sbuf, linepixels, swab);
274 vaddr += fb->pitches[0];
280 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);
283 * drm_fb_xrgb8888_to_rgb565_toio - Convert XRGB8888 to RGB565 clip buffer
284 * @dst: RGB565 destination buffer (iomem)
285 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
286 * @vaddr: XRGB8888 source buffer
287 * @fb: DRM framebuffer
288 * @clip: Clip rectangle area to copy
291 * Drivers can use this function for RGB565 devices that don't natively
294 void drm_fb_xrgb8888_to_rgb565_toio(void __iomem *dst, unsigned int dst_pitch,
295 const void *vaddr, const struct drm_framebuffer *fb,
296 const struct drm_rect *clip, bool swab)
298 size_t linepixels = clip->x2 - clip->x1;
299 size_t dst_len = linepixels * sizeof(u16);
300 unsigned y, lines = clip->y2 - clip->y1;
306 dbuf = kmalloc(dst_len, GFP_KERNEL);
310 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
311 for (y = 0; y < lines; y++) {
312 drm_fb_xrgb8888_to_rgb565_line(dbuf, vaddr, linepixels, swab);
313 memcpy_toio(dst, dbuf, dst_len);
314 vaddr += fb->pitches[0];
320 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565_toio);
322 static void drm_fb_xrgb8888_to_rgb888_line(u8 *dbuf, const u32 *sbuf,
327 for (x = 0; x < pixels; x++) {
328 *dbuf++ = (sbuf[x] & 0x000000FF) >> 0;
329 *dbuf++ = (sbuf[x] & 0x0000FF00) >> 8;
330 *dbuf++ = (sbuf[x] & 0x00FF0000) >> 16;
335 * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
336 * @dst: RGB888 destination buffer
337 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
338 * @src: XRGB8888 source buffer
339 * @fb: DRM framebuffer
340 * @clip: Clip rectangle area to copy
342 * Drivers can use this function for RGB888 devices that don't natively
345 void drm_fb_xrgb8888_to_rgb888(void *dst, unsigned int dst_pitch, const void *src,
346 const struct drm_framebuffer *fb, const struct drm_rect *clip)
348 size_t width = drm_rect_width(clip);
349 size_t src_len = width * sizeof(u32);
354 dst_pitch = width * 3;
356 /* Use a buffer to speed up access on buffers with uncached read mapping (i.e. WC) */
357 sbuf = kmalloc(src_len, GFP_KERNEL);
361 src += clip_offset(clip, fb->pitches[0], sizeof(u32));
362 for (y = 0; y < drm_rect_height(clip); y++) {
363 memcpy(sbuf, src, src_len);
364 drm_fb_xrgb8888_to_rgb888_line(dst, sbuf, width);
365 src += fb->pitches[0];
371 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
374 * drm_fb_xrgb8888_to_rgb888_toio - Convert XRGB8888 to RGB888 clip buffer
375 * @dst: RGB565 destination buffer (iomem)
376 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
377 * @vaddr: XRGB8888 source buffer
378 * @fb: DRM framebuffer
379 * @clip: Clip rectangle area to copy
381 * Drivers can use this function for RGB888 devices that don't natively
384 void drm_fb_xrgb8888_to_rgb888_toio(void __iomem *dst, unsigned int dst_pitch,
385 const void *vaddr, const struct drm_framebuffer *fb,
386 const struct drm_rect *clip)
388 size_t linepixels = clip->x2 - clip->x1;
389 size_t dst_len = linepixels * 3;
390 unsigned y, lines = clip->y2 - clip->y1;
396 dbuf = kmalloc(dst_len, GFP_KERNEL);
400 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
401 for (y = 0; y < lines; y++) {
402 drm_fb_xrgb8888_to_rgb888_line(dbuf, vaddr, linepixels);
403 memcpy_toio(dst, dbuf, dst_len);
404 vaddr += fb->pitches[0];
410 EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888_toio);
412 static void drm_fb_xrgb8888_to_xrgb2101010_line(u32 *dbuf, const u32 *sbuf,
418 for (x = 0; x < pixels; x++) {
419 val32 = ((sbuf[x] & 0x000000FF) << 2) |
420 ((sbuf[x] & 0x0000FF00) << 4) |
421 ((sbuf[x] & 0x00FF0000) << 6);
422 *dbuf++ = val32 | ((val32 >> 8) & 0x00300C03);
427 * drm_fb_xrgb8888_to_xrgb2101010_toio - Convert XRGB8888 to XRGB2101010 clip
429 * @dst: XRGB2101010 destination buffer (iomem)
430 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
431 * @vaddr: XRGB8888 source buffer
432 * @fb: DRM framebuffer
433 * @clip: Clip rectangle area to copy
435 * Drivers can use this function for XRGB2101010 devices that don't natively
438 void drm_fb_xrgb8888_to_xrgb2101010_toio(void __iomem *dst,
439 unsigned int dst_pitch, const void *vaddr,
440 const struct drm_framebuffer *fb,
441 const struct drm_rect *clip)
443 size_t linepixels = clip->x2 - clip->x1;
444 size_t dst_len = linepixels * sizeof(u32);
445 unsigned int y, lines = clip->y2 - clip->y1;
451 dbuf = kmalloc(dst_len, GFP_KERNEL);
455 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
456 for (y = 0; y < lines; y++) {
457 drm_fb_xrgb8888_to_xrgb2101010_line(dbuf, vaddr, linepixels);
458 memcpy_toio(dst, dbuf, dst_len);
459 vaddr += fb->pitches[0];
465 EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010_toio);
468 * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
469 * @dst: 8-bit grayscale destination buffer
470 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
471 * @vaddr: XRGB8888 source buffer
472 * @fb: DRM framebuffer
473 * @clip: Clip rectangle area to copy
475 * Drm doesn't have native monochrome or grayscale support.
476 * Such drivers can announce the commonly supported XR24 format to userspace
477 * and use this function to convert to the native format.
479 * Monochrome drivers will use the most significant bit,
480 * where 1 means foreground color and 0 background color.
482 * ITU BT.601 is used for the RGB -> luma (brightness) conversion.
484 void drm_fb_xrgb8888_to_gray8(void *dst, unsigned int dst_pitch, const void *vaddr,
485 const struct drm_framebuffer *fb, const struct drm_rect *clip)
487 unsigned int len = (clip->x2 - clip->x1) * sizeof(u32);
493 if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
497 dst_pitch = drm_rect_width(clip);
500 * The cma memory is write-combined so reads are uncached.
501 * Speed up by fetching one line at a time.
503 buf = kmalloc(len, GFP_KERNEL);
507 vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
508 for (y = clip->y1; y < clip->y2; y++) {
510 src32 = memcpy(buf, vaddr, len);
511 for (x = clip->x1; x < clip->x2; x++) {
512 u8 r = (*src32 & 0x00ff0000) >> 16;
513 u8 g = (*src32 & 0x0000ff00) >> 8;
514 u8 b = *src32 & 0x000000ff;
516 /* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
517 *dst8++ = (3 * r + 6 * g + b) / 10;
521 vaddr += fb->pitches[0];
527 EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);
530 * drm_fb_blit_toio - Copy parts of a framebuffer to display memory
531 * @dst: The display memory to copy to
532 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
533 * @dst_format: FOURCC code of the display's color format
534 * @vmap: The framebuffer memory to copy from
535 * @fb: The framebuffer to copy from
536 * @clip: Clip rectangle area to copy
538 * This function copies parts of a framebuffer to display memory. If the
539 * formats of the display and the framebuffer mismatch, the blit function
540 * will attempt to convert between them.
544 * -EINVAL if the color-format conversion failed, or
545 * a negative error code otherwise.
547 int drm_fb_blit_toio(void __iomem *dst, unsigned int dst_pitch, uint32_t dst_format,
548 const void *vmap, const struct drm_framebuffer *fb,
549 const struct drm_rect *clip)
551 uint32_t fb_format = fb->format->format;
553 /* treat alpha channel like filler bits */
554 if (fb_format == DRM_FORMAT_ARGB8888)
555 fb_format = DRM_FORMAT_XRGB8888;
556 if (dst_format == DRM_FORMAT_ARGB8888)
557 dst_format = DRM_FORMAT_XRGB8888;
558 if (fb_format == DRM_FORMAT_ARGB2101010)
559 fb_format = DRM_FORMAT_XRGB2101010;
560 if (dst_format == DRM_FORMAT_ARGB2101010)
561 dst_format = DRM_FORMAT_XRGB2101010;
563 if (dst_format == fb_format) {
564 drm_fb_memcpy_toio(dst, dst_pitch, vmap, fb, clip);
567 } else if (dst_format == DRM_FORMAT_RGB565) {
568 if (fb_format == DRM_FORMAT_XRGB8888) {
569 drm_fb_xrgb8888_to_rgb565_toio(dst, dst_pitch, vmap, fb, clip, false);
572 } else if (dst_format == DRM_FORMAT_RGB888) {
573 if (fb_format == DRM_FORMAT_XRGB8888) {
574 drm_fb_xrgb8888_to_rgb888_toio(dst, dst_pitch, vmap, fb, clip);
577 } else if (dst_format == DRM_FORMAT_XRGB2101010) {
578 if (fb_format == DRM_FORMAT_XRGB8888) {
579 drm_fb_xrgb8888_to_xrgb2101010_toio(dst, dst_pitch, vmap, fb, clip);
586 EXPORT_SYMBOL(drm_fb_blit_toio);