drm_format_helper.c 16.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0 or MIT
2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2016 Noralf Trønnes
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/slab.h>
13
#include <linux/io.h>
14 15 16 17 18 19

#include <drm/drm_format_helper.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_rect.h>

20
static unsigned int clip_offset(const struct drm_rect *clip, unsigned int pitch, unsigned int cpp)
21
{
22
	return clip->y1 * pitch + clip->x1 * cpp;
23 24
}

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**
 * drm_fb_clip_offset - Returns the clipping rectangles byte-offset in a framebuffer
 * @pitch: Framebuffer line pitch in byte
 * @format: Framebuffer format
 * @clip: Clip rectangle
 *
 * Returns:
 * The byte offset of the clip rectangle's top-left corner within the framebuffer.
 */
unsigned int drm_fb_clip_offset(unsigned int pitch, const struct drm_format_info *format,
				const struct drm_rect *clip)
{
	return clip_offset(clip, pitch, format->cpp[0]);
}
EXPORT_SYMBOL(drm_fb_clip_offset);

41 42 43
/**
 * drm_fb_memcpy - Copy clip buffer
 * @dst: Destination buffer
44
 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
45 46 47
 * @vaddr: Source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
48 49
 *
 * This function does not apply clipping on dst, i.e. the destination
50
 * is at the top-left corner.
51
 */
52 53
void drm_fb_memcpy(void *dst, unsigned int dst_pitch, const void *vaddr,
		   const struct drm_framebuffer *fb, const struct drm_rect *clip)
54
{
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;
58

59 60 61
	if (!dst_pitch)
		dst_pitch = len;

62 63 64 65
	vaddr += clip_offset(clip, fb->pitches[0], cpp);
	for (y = 0; y < lines; y++) {
		memcpy(dst, vaddr, len);
		vaddr += fb->pitches[0];
66
		dst += dst_pitch;
67
	}
68 69 70
}
EXPORT_SYMBOL(drm_fb_memcpy);

71
/**
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 76 77 78
 * @vaddr: Source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 *
79 80
 * This function does not apply clipping on dst, i.e. the destination
 * is at the top-left corner.
81
 */
82 83
void drm_fb_memcpy_toio(void __iomem *dst, unsigned int dst_pitch, const void *vaddr,
			const struct drm_framebuffer *fb, const struct drm_rect *clip)
84
{
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;
88

89 90 91 92
	if (!dst_pitch)
		dst_pitch = len;

	vaddr += clip_offset(clip, fb->pitches[0], cpp);
93 94 95
	for (y = 0; y < lines; y++) {
		memcpy_toio(dst, vaddr, len);
		vaddr += fb->pitches[0];
96
		dst += dst_pitch;
97
	}
98
}
99
EXPORT_SYMBOL(drm_fb_memcpy_toio);
100

101
/**
102 103
 * drm_fb_swab - Swap bytes into clip buffer
 * @dst: Destination buffer
104
 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
105
 * @src: Source buffer
106 107
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
108 109 110 111 112 113
 * @cached: Source buffer is mapped cached (eg. not write-combined)
 *
 * If @cached is false a temporary buffer is used to cache one pixel line at a
 * time to speed up slow uncached reads.
 *
 * This function does not apply clipping on dst, i.e. the destination
114
 * is at the top-left corner.
115
 */
116 117 118
void drm_fb_swab(void *dst, unsigned int dst_pitch, const void *src,
		 const struct drm_framebuffer *fb, const struct drm_rect *clip,
		 bool cached)
119
{
120 121
	u8 cpp = fb->format->cpp[0];
	size_t len = drm_rect_width(clip) * cpp;
122 123 124 125
	const u16 *src16;
	const u32 *src32;
	u16 *dst16;
	u32 *dst32;
126
	unsigned int x, y;
127 128 129
	void *buf = NULL;

	if (WARN_ON_ONCE(cpp != 2 && cpp != 4))
130 131
		return;

132 133 134
	if (!dst_pitch)
		dst_pitch = len;

135 136 137 138 139
	if (!cached)
		buf = kmalloc(len, GFP_KERNEL);

	src += clip_offset(clip, fb->pitches[0], cpp);

140
	for (y = clip->y1; y < clip->y2; y++) {
141 142 143 144 145 146 147 148 149
		if (buf) {
			memcpy(buf, src, len);
			src16 = buf;
			src32 = buf;
		} else {
			src16 = src;
			src32 = src;
		}

150 151 152
		dst16 = dst;
		dst32 = dst;

153 154 155 156 157 158 159 160
		for (x = clip->x1; x < clip->x2; x++) {
			if (cpp == 4)
				*dst32++ = swab32(*src32++);
			else
				*dst16++ = swab16(*src16++);
		}

		src += fb->pitches[0];
161
		dst += dst_pitch;
162 163 164 165
	}

	kfree(buf);
}
166
EXPORT_SYMBOL(drm_fb_swab);
167

168
static void drm_fb_xrgb8888_to_rgb332_line(u8 *dbuf, const __le32 *sbuf, unsigned int pixels)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
	unsigned int x;
	u32 pix;

	for (x = 0; x < pixels; x++) {
		pix = le32_to_cpu(sbuf[x]);
		dbuf[x] = ((pix & 0x00e00000) >> 16) |
			  ((pix & 0x0000e000) >> 11) |
			  ((pix & 0x000000c0) >> 6);
	}
}

/**
 * drm_fb_xrgb8888_to_rgb332 - Convert XRGB8888 to RGB332 clip buffer
 * @dst: RGB332 destination buffer
184
 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
185 186 187 188 189 190
 * @src: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 *
 * Drivers can use this function for RGB332 devices that don't natively support XRGB8888.
 */
191 192
void drm_fb_xrgb8888_to_rgb332(void *dst, unsigned int dst_pitch, const void *src,
			       const struct drm_framebuffer *fb, const struct drm_rect *clip)
193 194 195 196 197 198
{
	size_t width = drm_rect_width(clip);
	size_t src_len = width * sizeof(u32);
	unsigned int y;
	void *sbuf;

199 200 201
	if (!dst_pitch)
		dst_pitch = width;

202 203 204 205 206 207 208 209 210 211
	/* Use a buffer to speed up access on buffers with uncached read mapping (i.e. WC) */
	sbuf = kmalloc(src_len, GFP_KERNEL);
	if (!sbuf)
		return;

	src += clip_offset(clip, fb->pitches[0], sizeof(u32));
	for (y = 0; y < drm_rect_height(clip); y++) {
		memcpy(sbuf, src, src_len);
		drm_fb_xrgb8888_to_rgb332_line(dst, sbuf, width);
		src += fb->pitches[0];
212
		dst += dst_pitch;
213 214 215 216 217 218
	}

	kfree(sbuf);
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb332);

219
static void drm_fb_xrgb8888_to_rgb565_line(u16 *dbuf, const u32 *sbuf,
220 221
					   unsigned int pixels,
					   bool swab)
222
{
223 224 225 226 227 228 229 230 231 232 233
	unsigned int x;
	u16 val16;

	for (x = 0; x < pixels; x++) {
		val16 = ((sbuf[x] & 0x00F80000) >> 8) |
			((sbuf[x] & 0x0000FC00) >> 5) |
			((sbuf[x] & 0x000000F8) >> 3);
		if (swab)
			dbuf[x] = swab16(val16);
		else
			dbuf[x] = val16;
234
	}
235 236 237 238 239
}

/**
 * drm_fb_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
 * @dst: RGB565 destination buffer
240
 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
241 242 243
 * @vaddr: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
244
 * @swab: Swap bytes
245 246 247 248
 *
 * Drivers can use this function for RGB565 devices that don't natively
 * support XRGB8888.
 */
249 250 251
void drm_fb_xrgb8888_to_rgb565(void *dst, unsigned int dst_pitch, const void *vaddr,
			       const struct drm_framebuffer *fb, const struct drm_rect *clip,
			       bool swab)
252
{
253 254 255 256 257 258
	size_t linepixels = clip->x2 - clip->x1;
	size_t src_len = linepixels * sizeof(u32);
	size_t dst_len = linepixels * sizeof(u16);
	unsigned y, lines = clip->y2 - clip->y1;
	void *sbuf;

259 260 261
	if (!dst_pitch)
		dst_pitch = dst_len;

262 263 264 265 266 267 268 269 270 271 272 273 274
	/*
	 * The cma memory is write-combined so reads are uncached.
	 * Speed up by fetching one line at a time.
	 */
	sbuf = kmalloc(src_len, GFP_KERNEL);
	if (!sbuf)
		return;

	vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
	for (y = 0; y < lines; y++) {
		memcpy(sbuf, vaddr, src_len);
		drm_fb_xrgb8888_to_rgb565_line(dst, sbuf, linepixels, swab);
		vaddr += fb->pitches[0];
275
		dst += dst_pitch;
276 277 278
	}

	kfree(sbuf);
279 280 281
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);

282
/**
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 287 288
 * @vaddr: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
289
 * @swab: Swap bytes
290 291 292 293
 *
 * Drivers can use this function for RGB565 devices that don't natively
 * support XRGB8888.
 */
294 295 296
void drm_fb_xrgb8888_to_rgb565_toio(void __iomem *dst, unsigned int dst_pitch,
				    const void *vaddr, const struct drm_framebuffer *fb,
				    const struct drm_rect *clip, bool swab)
297
{
298 299 300 301 302
	size_t linepixels = clip->x2 - clip->x1;
	size_t dst_len = linepixels * sizeof(u16);
	unsigned y, lines = clip->y2 - clip->y1;
	void *dbuf;

303 304 305
	if (!dst_pitch)
		dst_pitch = dst_len;

306 307 308 309 310 311 312 313 314
	dbuf = kmalloc(dst_len, GFP_KERNEL);
	if (!dbuf)
		return;

	vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
	for (y = 0; y < lines; y++) {
		drm_fb_xrgb8888_to_rgb565_line(dbuf, vaddr, linepixels, swab);
		memcpy_toio(dst, dbuf, dst_len);
		vaddr += fb->pitches[0];
315
		dst += dst_pitch;
316 317 318
	}

	kfree(dbuf);
319
}
320
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565_toio);
321

322
static void drm_fb_xrgb8888_to_rgb888_line(u8 *dbuf, const u32 *sbuf,
323
					   unsigned int pixels)
324
{
325
	unsigned int x;
326

327 328 329 330
	for (x = 0; x < pixels; x++) {
		*dbuf++ = (sbuf[x] & 0x000000FF) >>  0;
		*dbuf++ = (sbuf[x] & 0x0000FF00) >>  8;
		*dbuf++ = (sbuf[x] & 0x00FF0000) >> 16;
331 332 333
	}
}

334 335 336
/**
 * drm_fb_xrgb8888_to_rgb888 - Convert XRGB8888 to RGB888 clip buffer
 * @dst: RGB888 destination buffer
337
 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
338 339 340 341 342 343 344
 * @src: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 *
 * Drivers can use this function for RGB888 devices that don't natively
 * support XRGB8888.
 */
345 346
void drm_fb_xrgb8888_to_rgb888(void *dst, unsigned int dst_pitch, const void *src,
			       const struct drm_framebuffer *fb, const struct drm_rect *clip)
347 348 349 350 351 352
{
	size_t width = drm_rect_width(clip);
	size_t src_len = width * sizeof(u32);
	unsigned int y;
	void *sbuf;

353 354 355
	if (!dst_pitch)
		dst_pitch = width * 3;

356 357 358 359 360 361 362 363 364 365
	/* Use a buffer to speed up access on buffers with uncached read mapping (i.e. WC) */
	sbuf = kmalloc(src_len, GFP_KERNEL);
	if (!sbuf)
		return;

	src += clip_offset(clip, fb->pitches[0], sizeof(u32));
	for (y = 0; y < drm_rect_height(clip); y++) {
		memcpy(sbuf, src, src_len);
		drm_fb_xrgb8888_to_rgb888_line(dst, sbuf, width);
		src += fb->pitches[0];
366
		dst += dst_pitch;
367 368 369 370 371 372
	}

	kfree(sbuf);
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);

373
/**
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 378 379 380 381 382 383
 * @vaddr: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 *
 * Drivers can use this function for RGB888 devices that don't natively
 * support XRGB8888.
 */
384 385 386
void drm_fb_xrgb8888_to_rgb888_toio(void __iomem *dst, unsigned int dst_pitch,
				    const void *vaddr, const struct drm_framebuffer *fb,
				    const struct drm_rect *clip)
387
{
388 389 390 391
	size_t linepixels = clip->x2 - clip->x1;
	size_t dst_len = linepixels * 3;
	unsigned y, lines = clip->y2 - clip->y1;
	void *dbuf;
392

393 394 395
	if (!dst_pitch)
		dst_pitch = dst_len;

396 397 398 399 400 401 402 403 404
	dbuf = kmalloc(dst_len, GFP_KERNEL);
	if (!dbuf)
		return;

	vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
	for (y = 0; y < lines; y++) {
		drm_fb_xrgb8888_to_rgb888_line(dbuf, vaddr, linepixels);
		memcpy_toio(dst, dbuf, dst_len);
		vaddr += fb->pitches[0];
405
		dst += dst_pitch;
406 407 408
	}

	kfree(dbuf);
409
}
410
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888_toio);
411

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
static void drm_fb_xrgb8888_to_xrgb2101010_line(u32 *dbuf, const u32 *sbuf,
						unsigned int pixels)
{
	unsigned int x;
	u32 val32;

	for (x = 0; x < pixels; x++) {
		val32 = ((sbuf[x] & 0x000000FF) << 2) |
			((sbuf[x] & 0x0000FF00) << 4) |
			((sbuf[x] & 0x00FF0000) << 6);
		*dbuf++ = val32 | ((val32 >> 8) & 0x00300C03);
	}
}

/**
 * drm_fb_xrgb8888_to_xrgb2101010_toio - Convert XRGB8888 to XRGB2101010 clip
 * buffer
 * @dst: XRGB2101010 destination buffer (iomem)
 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
 * @vaddr: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 *
 * Drivers can use this function for XRGB2101010 devices that don't natively
 * support XRGB8888.
 */
void drm_fb_xrgb8888_to_xrgb2101010_toio(void __iomem *dst,
					 unsigned int dst_pitch, const void *vaddr,
					 const struct drm_framebuffer *fb,
					 const struct drm_rect *clip)
{
	size_t linepixels = clip->x2 - clip->x1;
	size_t dst_len = linepixels * sizeof(u32);
	unsigned int y, lines = clip->y2 - clip->y1;
	void *dbuf;

	if (!dst_pitch)
		dst_pitch = dst_len;

	dbuf = kmalloc(dst_len, GFP_KERNEL);
	if (!dbuf)
		return;

	vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
	for (y = 0; y < lines; y++) {
		drm_fb_xrgb8888_to_xrgb2101010_line(dbuf, vaddr, linepixels);
		memcpy_toio(dst, dbuf, dst_len);
		vaddr += fb->pitches[0];
		dst += dst_pitch;
	}

	kfree(dbuf);
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_xrgb2101010_toio);

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
static void drm_fb_xrgb8888_to_gray8_line(u8 *dst, const u32 *src, unsigned int pixels)
{
	unsigned int x;

	for (x = 0; x < pixels; x++) {
		u8 r = (*src & 0x00ff0000) >> 16;
		u8 g = (*src & 0x0000ff00) >> 8;
		u8 b =  *src & 0x000000ff;

		/* ITU BT.601: Y = 0.299 R + 0.587 G + 0.114 B */
		*dst++ = (3 * r + 6 * g + b) / 10;
		src++;
	}
}

482 483 484
/**
 * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
 * @dst: 8-bit grayscale destination buffer
485
 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
486 487 488 489 490 491 492 493 494 495 496 497 498
 * @vaddr: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 *
 * Drm doesn't have native monochrome or grayscale support.
 * Such drivers can announce the commonly supported XR24 format to userspace
 * and use this function to convert to the native format.
 *
 * Monochrome drivers will use the most significant bit,
 * where 1 means foreground color and 0 background color.
 *
 * ITU BT.601 is used for the RGB -> luma (brightness) conversion.
 */
499 500
void drm_fb_xrgb8888_to_gray8(void *dst, unsigned int dst_pitch, const void *vaddr,
			      const struct drm_framebuffer *fb, const struct drm_rect *clip)
501
{
502 503 504
	unsigned int linepixels = clip->x2 - clip->x1;
	unsigned int len = linepixels * sizeof(u32);
	unsigned int y;
505
	void *buf;
506 507
	u8 *dst8;
	u32 *src32;
508 509 510

	if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
		return;
511 512 513 514

	if (!dst_pitch)
		dst_pitch = drm_rect_width(clip);

515 516 517 518 519 520 521 522
	/*
	 * The cma memory is write-combined so reads are uncached.
	 * Speed up by fetching one line at a time.
	 */
	buf = kmalloc(len, GFP_KERNEL);
	if (!buf)
		return;

523
	vaddr += clip_offset(clip, fb->pitches[0], sizeof(u32));
524
	for (y = clip->y1; y < clip->y2; y++) {
525 526
		dst8 = dst;
		src32 = memcpy(buf, vaddr, len);
527
		drm_fb_xrgb8888_to_gray8_line(dst8, src32, linepixels);
528 529
		vaddr += fb->pitches[0];
		dst += dst_pitch;
530 531 532 533 534 535
	}

	kfree(buf);
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);

536
/**
537
 * drm_fb_blit_toio - Copy parts of a framebuffer to display memory
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
 * @dst:	The display memory to copy to
 * @dst_pitch:	Number of bytes between two consecutive scanlines within dst
 * @dst_format:	FOURCC code of the display's color format
 * @vmap:	The framebuffer memory to copy from
 * @fb:		The framebuffer to copy from
 * @clip:	Clip rectangle area to copy
 *
 * This function copies parts of a framebuffer to display memory. If the
 * formats of the display and the framebuffer mismatch, the blit function
 * will attempt to convert between them.
 *
 * Returns:
 * 0 on success, or
 * -EINVAL if the color-format conversion failed, or
 * a negative error code otherwise.
 */
554 555 556
int drm_fb_blit_toio(void __iomem *dst, unsigned int dst_pitch, uint32_t dst_format,
		     const void *vmap, const struct drm_framebuffer *fb,
		     const struct drm_rect *clip)
557 558 559 560 561 562 563 564
{
	uint32_t fb_format = fb->format->format;

	/* treat alpha channel like filler bits */
	if (fb_format == DRM_FORMAT_ARGB8888)
		fb_format = DRM_FORMAT_XRGB8888;
	if (dst_format == DRM_FORMAT_ARGB8888)
		dst_format = DRM_FORMAT_XRGB8888;
565 566 567 568
	if (fb_format == DRM_FORMAT_ARGB2101010)
		fb_format = DRM_FORMAT_XRGB2101010;
	if (dst_format == DRM_FORMAT_ARGB2101010)
		dst_format = DRM_FORMAT_XRGB2101010;
569 570

	if (dst_format == fb_format) {
571
		drm_fb_memcpy_toio(dst, dst_pitch, vmap, fb, clip);
572 573 574 575
		return 0;

	} else if (dst_format == DRM_FORMAT_RGB565) {
		if (fb_format == DRM_FORMAT_XRGB8888) {
576
			drm_fb_xrgb8888_to_rgb565_toio(dst, dst_pitch, vmap, fb, clip, false);
577 578 579 580
			return 0;
		}
	} else if (dst_format == DRM_FORMAT_RGB888) {
		if (fb_format == DRM_FORMAT_XRGB8888) {
581
			drm_fb_xrgb8888_to_rgb888_toio(dst, dst_pitch, vmap, fb, clip);
582 583
			return 0;
		}
584 585 586 587 588
	} else if (dst_format == DRM_FORMAT_XRGB2101010) {
		if (fb_format == DRM_FORMAT_XRGB8888) {
			drm_fb_xrgb8888_to_xrgb2101010_toio(dst, dst_pitch, vmap, fb, clip);
			return 0;
		}
589 590 591 592
	}

	return -EINVAL;
}
593
EXPORT_SYMBOL(drm_fb_blit_toio);