1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/kernel.h>
4 #include <linux/minmax.h>
5 
6 #include <drm/drm_blend.h>
7 #include <drm/drm_rect.h>
8 #include <drm/drm_fixed.h>
9 
10 #include "vkms_formats.h"
11 
12 static size_t pixel_offset(const struct vkms_frame_info *frame_info, int x, int y)
13 {
14 	return frame_info->offset + (y * frame_info->pitch)
15 				  + (x * frame_info->cpp);
16 }
17 
18 /*
19  * packed_pixels_addr - Get the pointer to pixel of a given pair of coordinates
20  *
21  * @frame_info: Buffer metadata
22  * @x: The x(width) coordinate of the 2D buffer
23  * @y: The y(Heigth) coordinate of the 2D buffer
24  *
25  * Takes the information stored in the frame_info, a pair of coordinates, and
26  * returns the address of the first color channel.
27  * This function assumes the channels are packed together, i.e. a color channel
28  * comes immediately after another in the memory. And therefore, this function
29  * doesn't work for YUV with chroma subsampling (e.g. YUV420 and NV21).
30  */
31 static void *packed_pixels_addr(const struct vkms_frame_info *frame_info,
32 				int x, int y)
33 {
34 	size_t offset = pixel_offset(frame_info, x, y);
35 
36 	return (u8 *)frame_info->map[0].vaddr + offset;
37 }
38 
39 static void *get_packed_src_addr(const struct vkms_frame_info *frame_info, int y)
40 {
41 	int x_src = frame_info->src.x1 >> 16;
42 	int y_src = y - frame_info->rotated.y1 + (frame_info->src.y1 >> 16);
43 
44 	return packed_pixels_addr(frame_info, x_src, y_src);
45 }
46 
47 static int get_x_position(const struct vkms_frame_info *frame_info, int limit, int x)
48 {
49 	if (frame_info->rotation & (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_270))
50 		return limit - x - 1;
51 	return x;
52 }
53 
54 static void ARGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
55 {
56 	/*
57 	 * The 257 is the "conversion ratio". This number is obtained by the
58 	 * (2^16 - 1) / (2^8 - 1) division. Which, in this case, tries to get
59 	 * the best color value in a pixel format with more possibilities.
60 	 * A similar idea applies to others RGB color conversions.
61 	 */
62 	out_pixel->a = (u16)src_pixels[3] * 257;
63 	out_pixel->r = (u16)src_pixels[2] * 257;
64 	out_pixel->g = (u16)src_pixels[1] * 257;
65 	out_pixel->b = (u16)src_pixels[0] * 257;
66 }
67 
68 static void XRGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
69 {
70 	out_pixel->a = (u16)0xffff;
71 	out_pixel->r = (u16)src_pixels[2] * 257;
72 	out_pixel->g = (u16)src_pixels[1] * 257;
73 	out_pixel->b = (u16)src_pixels[0] * 257;
74 }
75 
76 static void ARGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
77 {
78 	u16 *pixels = (u16 *)src_pixels;
79 
80 	out_pixel->a = le16_to_cpu(pixels[3]);
81 	out_pixel->r = le16_to_cpu(pixels[2]);
82 	out_pixel->g = le16_to_cpu(pixels[1]);
83 	out_pixel->b = le16_to_cpu(pixels[0]);
84 }
85 
86 static void XRGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
87 {
88 	u16 *pixels = (u16 *)src_pixels;
89 
90 	out_pixel->a = (u16)0xffff;
91 	out_pixel->r = le16_to_cpu(pixels[2]);
92 	out_pixel->g = le16_to_cpu(pixels[1]);
93 	out_pixel->b = le16_to_cpu(pixels[0]);
94 }
95 
96 static void RGB565_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
97 {
98 	u16 *pixels = (u16 *)src_pixels;
99 
100 	s64 fp_rb_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(31));
101 	s64 fp_g_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(63));
102 
103 	u16 rgb_565 = le16_to_cpu(*pixels);
104 	s64 fp_r = drm_int2fixp((rgb_565 >> 11) & 0x1f);
105 	s64 fp_g = drm_int2fixp((rgb_565 >> 5) & 0x3f);
106 	s64 fp_b = drm_int2fixp(rgb_565 & 0x1f);
107 
108 	out_pixel->a = (u16)0xffff;
109 	out_pixel->r = drm_fixp2int_round(drm_fixp_mul(fp_r, fp_rb_ratio));
110 	out_pixel->g = drm_fixp2int_round(drm_fixp_mul(fp_g, fp_g_ratio));
111 	out_pixel->b = drm_fixp2int_round(drm_fixp_mul(fp_b, fp_rb_ratio));
112 }
113 
114 void vkms_compose_row(struct line_buffer *stage_buffer, struct vkms_plane_state *plane, int y)
115 {
116 	struct pixel_argb_u16 *out_pixels = stage_buffer->pixels;
117 	struct vkms_frame_info *frame_info = plane->frame_info;
118 	u8 *src_pixels = get_packed_src_addr(frame_info, y);
119 	int limit = min_t(size_t, drm_rect_width(&frame_info->dst), stage_buffer->n_pixels);
120 
121 	for (size_t x = 0; x < limit; x++, src_pixels += frame_info->cpp) {
122 		int x_pos = get_x_position(frame_info, limit, x);
123 
124 		if (drm_rotation_90_or_270(frame_info->rotation))
125 			src_pixels = get_packed_src_addr(frame_info, x + frame_info->rotated.y1)
126 				+ frame_info->cpp * y;
127 
128 		plane->pixel_read(src_pixels, &out_pixels[x_pos]);
129 	}
130 }
131 
132 /*
133  * The following  functions take an line of argb_u16 pixels from the
134  * src_buffer, convert them to a specific format, and store them in the
135  * destination.
136  *
137  * They are used in the `compose_active_planes` to convert and store a line
138  * from the src_buffer to the writeback buffer.
139  */
140 static void argb_u16_to_ARGB8888(struct vkms_frame_info *frame_info,
141 				 const struct line_buffer *src_buffer, int y)
142 {
143 	int x_dst = frame_info->dst.x1;
144 	u8 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
145 	struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
146 	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
147 			    src_buffer->n_pixels);
148 
149 	for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
150 		/*
151 		 * This sequence below is important because the format's byte order is
152 		 * in little-endian. In the case of the ARGB8888 the memory is
153 		 * organized this way:
154 		 *
155 		 * | Addr     | = blue channel
156 		 * | Addr + 1 | = green channel
157 		 * | Addr + 2 | = Red channel
158 		 * | Addr + 3 | = Alpha channel
159 		 */
160 		dst_pixels[3] = DIV_ROUND_CLOSEST(in_pixels[x].a, 257);
161 		dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixels[x].r, 257);
162 		dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixels[x].g, 257);
163 		dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixels[x].b, 257);
164 	}
165 }
166 
167 static void argb_u16_to_XRGB8888(struct vkms_frame_info *frame_info,
168 				 const struct line_buffer *src_buffer, int y)
169 {
170 	int x_dst = frame_info->dst.x1;
171 	u8 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
172 	struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
173 	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
174 			    src_buffer->n_pixels);
175 
176 	for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
177 		dst_pixels[3] = 0xff;
178 		dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixels[x].r, 257);
179 		dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixels[x].g, 257);
180 		dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixels[x].b, 257);
181 	}
182 }
183 
184 static void argb_u16_to_ARGB16161616(struct vkms_frame_info *frame_info,
185 				     const struct line_buffer *src_buffer, int y)
186 {
187 	int x_dst = frame_info->dst.x1;
188 	u16 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
189 	struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
190 	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
191 			    src_buffer->n_pixels);
192 
193 	for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
194 		dst_pixels[3] = cpu_to_le16(in_pixels[x].a);
195 		dst_pixels[2] = cpu_to_le16(in_pixels[x].r);
196 		dst_pixels[1] = cpu_to_le16(in_pixels[x].g);
197 		dst_pixels[0] = cpu_to_le16(in_pixels[x].b);
198 	}
199 }
200 
201 static void argb_u16_to_XRGB16161616(struct vkms_frame_info *frame_info,
202 				     const struct line_buffer *src_buffer, int y)
203 {
204 	int x_dst = frame_info->dst.x1;
205 	u16 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
206 	struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
207 	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
208 			    src_buffer->n_pixels);
209 
210 	for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
211 		dst_pixels[3] = 0xffff;
212 		dst_pixels[2] = cpu_to_le16(in_pixels[x].r);
213 		dst_pixels[1] = cpu_to_le16(in_pixels[x].g);
214 		dst_pixels[0] = cpu_to_le16(in_pixels[x].b);
215 	}
216 }
217 
218 static void argb_u16_to_RGB565(struct vkms_frame_info *frame_info,
219 			       const struct line_buffer *src_buffer, int y)
220 {
221 	int x_dst = frame_info->dst.x1;
222 	u16 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
223 	struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
224 	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
225 			    src_buffer->n_pixels);
226 
227 	s64 fp_rb_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(31));
228 	s64 fp_g_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(63));
229 
230 	for (size_t x = 0; x < x_limit; x++, dst_pixels++) {
231 		s64 fp_r = drm_int2fixp(in_pixels[x].r);
232 		s64 fp_g = drm_int2fixp(in_pixels[x].g);
233 		s64 fp_b = drm_int2fixp(in_pixels[x].b);
234 
235 		u16 r = drm_fixp2int_round(drm_fixp_div(fp_r, fp_rb_ratio));
236 		u16 g = drm_fixp2int_round(drm_fixp_div(fp_g, fp_g_ratio));
237 		u16 b = drm_fixp2int_round(drm_fixp_div(fp_b, fp_rb_ratio));
238 
239 		*dst_pixels = cpu_to_le16(r << 11 | g << 5 | b);
240 	}
241 }
242 
243 void *get_pixel_conversion_function(u32 format)
244 {
245 	switch (format) {
246 	case DRM_FORMAT_ARGB8888:
247 		return &ARGB8888_to_argb_u16;
248 	case DRM_FORMAT_XRGB8888:
249 		return &XRGB8888_to_argb_u16;
250 	case DRM_FORMAT_ARGB16161616:
251 		return &ARGB16161616_to_argb_u16;
252 	case DRM_FORMAT_XRGB16161616:
253 		return &XRGB16161616_to_argb_u16;
254 	case DRM_FORMAT_RGB565:
255 		return &RGB565_to_argb_u16;
256 	default:
257 		return NULL;
258 	}
259 }
260 
261 void *get_line_to_frame_function(u32 format)
262 {
263 	switch (format) {
264 	case DRM_FORMAT_ARGB8888:
265 		return &argb_u16_to_ARGB8888;
266 	case DRM_FORMAT_XRGB8888:
267 		return &argb_u16_to_XRGB8888;
268 	case DRM_FORMAT_ARGB16161616:
269 		return &argb_u16_to_ARGB16161616;
270 	case DRM_FORMAT_XRGB16161616:
271 		return &argb_u16_to_XRGB16161616;
272 	case DRM_FORMAT_RGB565:
273 		return &argb_u16_to_RGB565;
274 	default:
275 		return NULL;
276 	}
277 }
278