summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/vkms/vkms_formats.c
blob: 33803d3e30b7b416cec0d4c26bc6bced6e614dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SPDX-License-Identifier: GPL-2.0+

#include <drm/drm_rect.h>
#include <linux/minmax.h>

#include "vkms_formats.h"

static size_t pixel_offset(const struct vkms_frame_info *frame_info, int x, int y)
{
	return frame_info->offset + (y * frame_info->pitch)
				  + (x * frame_info->cpp);
}

/*
 * packed_pixels_addr - Get the pointer to pixel of a given pair of coordinates
 *
 * @frame_info: Buffer metadata
 * @x: The x(width) coordinate of the 2D buffer
 * @y: The y(Heigth) coordinate of the 2D buffer
 *
 * Takes the information stored in the frame_info, a pair of coordinates, and
 * returns the address of the first color channel.
 * This function assumes the channels are packed together, i.e. a color channel
 * comes immediately after another in the memory. And therefore, this function
 * doesn't work for YUV with chroma subsampling (e.g. YUV420 and NV21).
 */
static void *packed_pixels_addr(const struct vkms_frame_info *frame_info,
				int x, int y)
{
	size_t offset = pixel_offset(frame_info, x, y);

	return (u8 *)frame_info->map[0].vaddr + offset;
}

static void *get_packed_src_addr(const struct vkms_frame_info *frame_info, int y)
{
	int x_src = frame_info->src.x1 >> 16;
	int y_src = y - frame_info->dst.y1 + (frame_info->src.y1 >> 16);

	return packed_pixels_addr(frame_info, x_src, y_src);
}

static void ARGB8888_to_argb_u16(struct line_buffer *stage_buffer,
				 const struct vkms_frame_info *frame_info, int y)
{
	struct pixel_argb_u16 *out_pixels = stage_buffer->pixels;
	u8 *src_pixels = get_packed_src_addr(frame_info, y);
	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
			    stage_buffer->n_pixels);

	for (size_t x = 0; x < x_limit; x++, src_pixels += 4) {
		/*
		 * The 257 is the "conversion ratio". This number is obtained by the
		 * (2^16 - 1) / (2^8 - 1) division. Which, in this case, tries to get
		 * the best color value in a pixel format with more possibilities.
		 * A similar idea applies to others RGB color conversions.
		 */
		out_pixels[x].a = (u16)src_pixels[3] * 257;
		out_pixels[x].r = (u16)src_pixels[2] * 257;
		out_pixels[x].g = (u16)src_pixels[1] * 257;
		out_pixels[x].b = (u16)src_pixels[0] * 257;
	}
}

static void XRGB8888_to_argb_u16(struct line_buffer *stage_buffer,
				 const struct vkms_frame_info *frame_info, int y)
{
	struct pixel_argb_u16 *out_pixels = stage_buffer->pixels;
	u8 *src_pixels = get_packed_src_addr(frame_info, y);
	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
			    stage_buffer->n_pixels);

	for (size_t x = 0; x < x_limit; x++, src_pixels += 4) {
		out_pixels[x].a = (u16)0xffff;
		out_pixels[x].r = (u16)src_pixels[2] * 257;
		out_pixels[x].g = (u16)src_pixels[1] * 257;
		out_pixels[x].b = (u16)src_pixels[0] * 257;
	}
}

/*
 * The following  functions take an line of argb_u16 pixels from the
 * src_buffer, convert them to a specific format, and store them in the
 * destination.
 *
 * They are used in the `compose_active_planes` to convert and store a line
 * from the src_buffer to the writeback buffer.
 */
static void argb_u16_to_ARGB8888(struct vkms_frame_info *frame_info,
				 const struct line_buffer *src_buffer, int y)
{
	int x_dst = frame_info->dst.x1;
	u8 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
	struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
			    src_buffer->n_pixels);

	for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
		/*
		 * This sequence below is important because the format's byte order is
		 * in little-endian. In the case of the ARGB8888 the memory is
		 * organized this way:
		 *
		 * | Addr     | = blue channel
		 * | Addr + 1 | = green channel
		 * | Addr + 2 | = Red channel
		 * | Addr + 3 | = Alpha channel
		 */
		dst_pixels[3] = DIV_ROUND_CLOSEST(in_pixels[x].a, 257);
		dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixels[x].r, 257);
		dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixels[x].g, 257);
		dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixels[x].b, 257);
	}
}

static void argb_u16_to_XRGB8888(struct vkms_frame_info *frame_info,
				 const struct line_buffer *src_buffer, int y)
{
	int x_dst = frame_info->dst.x1;
	u8 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
	struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
			    src_buffer->n_pixels);

	for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
		dst_pixels[3] = 0xff;
		dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixels[x].r, 257);
		dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixels[x].g, 257);
		dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixels[x].b, 257);
	}
}

void *get_frame_to_line_function(u32 format)
{
	switch (format) {
	case DRM_FORMAT_ARGB8888:
		return &ARGB8888_to_argb_u16;
	case DRM_FORMAT_XRGB8888:
		return &XRGB8888_to_argb_u16;
	default:
		return NULL;
	}
}

void *get_line_to_frame_function(u32 format)
{
	switch (format) {
	case DRM_FORMAT_ARGB8888:
		return &argb_u16_to_ARGB8888;
	case DRM_FORMAT_XRGB8888:
		return &argb_u16_to_XRGB8888;
	default:
		return NULL;
	}
}