drm_format_helper.c 9.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * 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>

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

19 20 21 22 23 24 25 26 27 28 29 30 31
static void drm_fb_memcpy_lines(void *dst, unsigned int dst_pitch,
				void *src, unsigned int src_pitch,
				unsigned int linelength, unsigned int lines)
{
	int line;

	for (line = 0; line < lines; line++) {
		memcpy(dst, src, linelength);
		src += src_pitch;
		dst += dst_pitch;
	}
}

32 33 34 35 36 37
/**
 * drm_fb_memcpy - Copy clip buffer
 * @dst: Destination buffer
 * @vaddr: Source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
38 39 40
 *
 * This function does not apply clipping on dst, i.e. the destination
 * is a small buffer containing the clip rect only.
41 42 43 44 45
 */
void drm_fb_memcpy(void *dst, void *vaddr, struct drm_framebuffer *fb,
		   struct drm_rect *clip)
{
	unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0);
46
	unsigned int offset = (clip->y1 * fb->pitches[0]) + (clip->x1 * cpp);
47 48
	size_t len = (clip->x2 - clip->x1) * cpp;

49 50 51
	drm_fb_memcpy_lines(dst, len,
			    vaddr + offset, fb->pitches[0],
			    len, clip->y2 - clip->y1);
52 53 54
}
EXPORT_SYMBOL(drm_fb_memcpy);

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/**
 * drm_fb_memcpy_dstclip - Copy clip buffer
 * @dst: Destination buffer
 * @vaddr: Source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 *
 * This function applies clipping on dst, i.e. the destination is a
 * full framebuffer but only the clip rect content is copied over.
 */
void drm_fb_memcpy_dstclip(void *dst, void *vaddr, struct drm_framebuffer *fb,
			   struct drm_rect *clip)
{
	unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0);
	unsigned int offset = (clip->y1 * fb->pitches[0]) + (clip->x1 * cpp);
	size_t len = (clip->x2 - clip->x1) * cpp;

	drm_fb_memcpy_lines(dst + offset, fb->pitches[0],
			    vaddr + offset, fb->pitches[0],
			    len, clip->y2 - clip->y1);
}
EXPORT_SYMBOL(drm_fb_memcpy_dstclip);

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/**
 * drm_fb_swab16 - Swap bytes into clip buffer
 * @dst: RGB565 destination buffer
 * @vaddr: RGB565 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 */
void drm_fb_swab16(u16 *dst, void *vaddr, struct drm_framebuffer *fb,
		   struct drm_rect *clip)
{
	size_t len = (clip->x2 - clip->x1) * sizeof(u16);
	unsigned int x, y;
	u16 *src, *buf;

	/*
	 * 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;

	for (y = clip->y1; y < clip->y2; y++) {
		src = vaddr + (y * fb->pitches[0]);
		src += clip->x1;
		memcpy(buf, src, len);
		src = buf;
		for (x = clip->x1; x < clip->x2; x++)
			*dst++ = swab16(*src++);
	}

	kfree(buf);
}
EXPORT_SYMBOL(drm_fb_swab16);

113 114 115 116 117
static void drm_fb_xrgb8888_to_rgb565_lines(void *dst, unsigned int dst_pitch,
					    void *src, unsigned int src_pitch,
					    unsigned int src_linelength,
					    unsigned int lines,
					    bool swap)
118
{
119
	unsigned int linepixels = src_linelength / sizeof(u32);
120
	unsigned int x, y;
121 122
	u32 *sbuf;
	u16 *dbuf, val16;
123 124 125 126 127

	/*
	 * The cma memory is write-combined so reads are uncached.
	 * Speed up by fetching one line at a time.
	 */
128 129
	sbuf = kmalloc(src_linelength, GFP_KERNEL);
	if (!sbuf)
130 131
		return;

132 133 134 135 136 137 138
	for (y = 0; y < lines; y++) {
		memcpy(sbuf, src, src_linelength);
		dbuf = dst;
		for (x = 0; x < linepixels; x++) {
			val16 = ((sbuf[x] & 0x00F80000) >> 8) |
				((sbuf[x] & 0x0000FC00) >> 5) |
				((sbuf[x] & 0x000000F8) >> 3);
139
			if (swap)
140
				*dbuf++ = swab16(val16);
141
			else
142
				*dbuf++ = val16;
143
		}
144 145
		src += src_pitch;
		dst += dst_pitch;
146 147
	}

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	kfree(sbuf);
}

/**
 * drm_fb_xrgb8888_to_rgb565 - Convert XRGB8888 to RGB565 clip buffer
 * @dst: RGB565 destination buffer
 * @vaddr: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 * @swap: Swap bytes
 *
 * Drivers can use this function for RGB565 devices that don't natively
 * support XRGB8888.
 *
 * This function does not apply clipping on dst, i.e. the destination
 * is a small buffer containing the clip rect only.
 */
void drm_fb_xrgb8888_to_rgb565(void *dst, void *vaddr,
			       struct drm_framebuffer *fb,
			       struct drm_rect *clip, bool swap)
{
	unsigned int src_offset = (clip->y1 * fb->pitches[0])
		+ (clip->x1 * sizeof(u32));
	size_t src_len = (clip->x2 - clip->x1) * sizeof(u32);
	size_t dst_len = (clip->x2 - clip->x1) * sizeof(u16);

	drm_fb_xrgb8888_to_rgb565_lines(dst, dst_len,
					vaddr + src_offset, fb->pitches[0],
					src_len, clip->y2 - clip->y1,
					swap);
178 179 180
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565);

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/**
 * drm_fb_xrgb8888_to_rgb565_dstclip - Convert XRGB8888 to RGB565 clip buffer
 * @dst: RGB565 destination buffer
 * @dst_pitch: destination buffer pitch
 * @vaddr: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 * @swap: Swap bytes
 *
 * Drivers can use this function for RGB565 devices that don't natively
 * support XRGB8888.
 *
 * This function applies clipping on dst, i.e. the destination is a
 * full framebuffer but only the clip rect content is copied over.
 */
void drm_fb_xrgb8888_to_rgb565_dstclip(void *dst, unsigned int dst_pitch,
				       void *vaddr, struct drm_framebuffer *fb,
				       struct drm_rect *clip, bool swap)
{
	unsigned int src_offset = (clip->y1 * fb->pitches[0])
		+ (clip->x1 * sizeof(u32));
	unsigned int dst_offset = (clip->y1 * dst_pitch)
		+ (clip->x1 * sizeof(u16));
	size_t src_len = (clip->x2 - clip->x1) * sizeof(u32);

	drm_fb_xrgb8888_to_rgb565_lines(dst + dst_offset, dst_pitch,
					vaddr + src_offset, fb->pitches[0],
					src_len, clip->y2 - clip->y1,
					swap);
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb565_dstclip);

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
static void drm_fb_xrgb8888_to_rgb888_lines(void *dst, unsigned int dst_pitch,
					    void *src, unsigned int src_pitch,
					    unsigned int src_linelength,
					    unsigned int lines)
{
	unsigned int linepixels = src_linelength / 3;
	unsigned int x, y;
	u32 *sbuf;
	u8 *dbuf;

	sbuf = kmalloc(src_linelength, GFP_KERNEL);
	if (!sbuf)
		return;

	for (y = 0; y < lines; y++) {
		memcpy(sbuf, src, src_linelength);
		dbuf = dst;
		for (x = 0; x < linepixels; x++) {
			*dbuf++ = (sbuf[x] & 0x000000FF) >>  0;
			*dbuf++ = (sbuf[x] & 0x0000FF00) >>  8;
			*dbuf++ = (sbuf[x] & 0x00FF0000) >> 16;
		}
		src += src_pitch;
		dst += dst_pitch;
	}

	kfree(sbuf);
}

/**
 * drm_fb_xrgb8888_to_rgb888_dstclip - Convert XRGB8888 to RGB888 clip buffer
 * @dst: RGB565 destination buffer
 * @dst_pitch: destination buffer pitch
 * @vaddr: XRGB8888 source buffer
 * @fb: DRM framebuffer
 * @clip: Clip rectangle area to copy
 * @dstclip: Clip destination too.
 *
 * Drivers can use this function for RGB888 devices that don't natively
 * support XRGB8888.
 *
 * This function applies clipping on dst, i.e. the destination is a
 * full framebuffer but only the clip rect content is copied over.
 */
void drm_fb_xrgb8888_to_rgb888_dstclip(void *dst, unsigned int dst_pitch,
				       void *vaddr, struct drm_framebuffer *fb,
				       struct drm_rect *clip)
{
	unsigned int src_offset = (clip->y1 * fb->pitches[0])
		+ (clip->x1 * sizeof(u32));
	unsigned int dst_offset = (clip->y1 * dst_pitch)
		+ (clip->x1 * 3);
	size_t src_len = (clip->x2 - clip->x1) * sizeof(u32);

	drm_fb_xrgb8888_to_rgb888_lines(dst + dst_offset, dst_pitch,
					vaddr + src_offset, fb->pitches[0],
					src_len, clip->y2 - clip->y1);
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888_dstclip);

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
/**
 * drm_fb_xrgb8888_to_gray8 - Convert XRGB8888 to grayscale
 * @dst: 8-bit grayscale destination buffer
 * @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.
 */
void drm_fb_xrgb8888_to_gray8(u8 *dst, void *vaddr, struct drm_framebuffer *fb,
			       struct drm_rect *clip)
{
	unsigned int len = (clip->x2 - clip->x1) * sizeof(u32);
	unsigned int x, y;
	void *buf;
	u32 *src;

	if (WARN_ON(fb->format->format != DRM_FORMAT_XRGB8888))
		return;
	/*
	 * 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;

	for (y = clip->y1; y < clip->y2; y++) {
		src = vaddr + (y * fb->pitches[0]);
		src += clip->x1;
		memcpy(buf, src, len);
		src = buf;
		for (x = clip->x1; x < clip->x2; 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++;
		}
	}

	kfree(buf);
}
EXPORT_SYMBOL(drm_fb_xrgb8888_to_gray8);