1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/kernel.h>
4 #include <linux/minmax.h>
5 #include <drm/drm_rect.h>
6 #include <drm/drm_fixed.h>
8 #include "vkms_formats.h"
10 static size_t pixel_offset(const struct vkms_frame_info *frame_info, int x, int y)
12 return frame_info->offset + (y * frame_info->pitch)
13 + (x * frame_info->cpp);
17 * packed_pixels_addr - Get the pointer to pixel of a given pair of coordinates
19 * @frame_info: Buffer metadata
20 * @x: The x(width) coordinate of the 2D buffer
21 * @y: The y(Heigth) coordinate of the 2D buffer
23 * Takes the information stored in the frame_info, a pair of coordinates, and
24 * returns the address of the first color channel.
25 * This function assumes the channels are packed together, i.e. a color channel
26 * comes immediately after another in the memory. And therefore, this function
27 * doesn't work for YUV with chroma subsampling (e.g. YUV420 and NV21).
29 static void *packed_pixels_addr(const struct vkms_frame_info *frame_info,
32 size_t offset = pixel_offset(frame_info, x, y);
34 return (u8 *)frame_info->map[0].vaddr + offset;
37 static void *get_packed_src_addr(const struct vkms_frame_info *frame_info, int y)
39 int x_src = frame_info->src.x1 >> 16;
40 int y_src = y - frame_info->rotated.y1 + (frame_info->src.y1 >> 16);
42 return packed_pixels_addr(frame_info, x_src, y_src);
45 static int get_x_position(const struct vkms_frame_info *frame_info, int limit, int x)
47 if (frame_info->rotation & DRM_MODE_REFLECT_X)
52 static void ARGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
55 * The 257 is the "conversion ratio". This number is obtained by the
56 * (2^16 - 1) / (2^8 - 1) division. Which, in this case, tries to get
57 * the best color value in a pixel format with more possibilities.
58 * A similar idea applies to others RGB color conversions.
60 out_pixel->a = (u16)src_pixels[3] * 257;
61 out_pixel->r = (u16)src_pixels[2] * 257;
62 out_pixel->g = (u16)src_pixels[1] * 257;
63 out_pixel->b = (u16)src_pixels[0] * 257;
66 static void XRGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
68 out_pixel->a = (u16)0xffff;
69 out_pixel->r = (u16)src_pixels[2] * 257;
70 out_pixel->g = (u16)src_pixels[1] * 257;
71 out_pixel->b = (u16)src_pixels[0] * 257;
74 static void ARGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
76 u16 *pixels = (u16 *)src_pixels;
78 out_pixel->a = le16_to_cpu(pixels[3]);
79 out_pixel->r = le16_to_cpu(pixels[2]);
80 out_pixel->g = le16_to_cpu(pixels[1]);
81 out_pixel->b = le16_to_cpu(pixels[0]);
84 static void XRGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
86 u16 *pixels = (u16 *)src_pixels;
88 out_pixel->a = (u16)0xffff;
89 out_pixel->r = le16_to_cpu(pixels[2]);
90 out_pixel->g = le16_to_cpu(pixels[1]);
91 out_pixel->b = le16_to_cpu(pixels[0]);
94 static void RGB565_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
96 u16 *pixels = (u16 *)src_pixels;
98 s64 fp_rb_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(31));
99 s64 fp_g_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(63));
101 u16 rgb_565 = le16_to_cpu(*pixels);
102 s64 fp_r = drm_int2fixp((rgb_565 >> 11) & 0x1f);
103 s64 fp_g = drm_int2fixp((rgb_565 >> 5) & 0x3f);
104 s64 fp_b = drm_int2fixp(rgb_565 & 0x1f);
106 out_pixel->a = (u16)0xffff;
107 out_pixel->r = drm_fixp2int(drm_fixp_mul(fp_r, fp_rb_ratio));
108 out_pixel->g = drm_fixp2int(drm_fixp_mul(fp_g, fp_g_ratio));
109 out_pixel->b = drm_fixp2int(drm_fixp_mul(fp_b, fp_rb_ratio));
112 void vkms_compose_row(struct line_buffer *stage_buffer, struct vkms_plane_state *plane, int y)
114 struct pixel_argb_u16 *out_pixels = stage_buffer->pixels;
115 struct vkms_frame_info *frame_info = plane->frame_info;
116 u8 *src_pixels = get_packed_src_addr(frame_info, y);
117 int limit = min_t(size_t, drm_rect_width(&frame_info->dst), stage_buffer->n_pixels);
119 for (size_t x = 0; x < limit; x++, src_pixels += frame_info->cpp) {
120 int x_pos = get_x_position(frame_info, limit, x);
122 if (frame_info->rotation & DRM_MODE_ROTATE_90)
123 src_pixels = get_packed_src_addr(frame_info, x + frame_info->rotated.y1)
124 + frame_info->cpp * y;
126 plane->pixel_read(src_pixels, &out_pixels[x_pos]);
131 * The following functions take an line of argb_u16 pixels from the
132 * src_buffer, convert them to a specific format, and store them in the
135 * They are used in the `compose_active_planes` to convert and store a line
136 * from the src_buffer to the writeback buffer.
138 static void argb_u16_to_ARGB8888(struct vkms_frame_info *frame_info,
139 const struct line_buffer *src_buffer, int y)
141 int x_dst = frame_info->dst.x1;
142 u8 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
143 struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
144 int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
145 src_buffer->n_pixels);
147 for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
149 * This sequence below is important because the format's byte order is
150 * in little-endian. In the case of the ARGB8888 the memory is
151 * organized this way:
153 * | Addr | = blue channel
154 * | Addr + 1 | = green channel
155 * | Addr + 2 | = Red channel
156 * | Addr + 3 | = Alpha channel
158 dst_pixels[3] = DIV_ROUND_CLOSEST(in_pixels[x].a, 257);
159 dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixels[x].r, 257);
160 dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixels[x].g, 257);
161 dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixels[x].b, 257);
165 static void argb_u16_to_XRGB8888(struct vkms_frame_info *frame_info,
166 const struct line_buffer *src_buffer, int y)
168 int x_dst = frame_info->dst.x1;
169 u8 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
170 struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
171 int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
172 src_buffer->n_pixels);
174 for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
175 dst_pixels[3] = 0xff;
176 dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixels[x].r, 257);
177 dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixels[x].g, 257);
178 dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixels[x].b, 257);
182 static void argb_u16_to_ARGB16161616(struct vkms_frame_info *frame_info,
183 const struct line_buffer *src_buffer, int y)
185 int x_dst = frame_info->dst.x1;
186 u16 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
187 struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
188 int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
189 src_buffer->n_pixels);
191 for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
192 dst_pixels[3] = cpu_to_le16(in_pixels[x].a);
193 dst_pixels[2] = cpu_to_le16(in_pixels[x].r);
194 dst_pixels[1] = cpu_to_le16(in_pixels[x].g);
195 dst_pixels[0] = cpu_to_le16(in_pixels[x].b);
199 static void argb_u16_to_XRGB16161616(struct vkms_frame_info *frame_info,
200 const struct line_buffer *src_buffer, int y)
202 int x_dst = frame_info->dst.x1;
203 u16 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
204 struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
205 int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
206 src_buffer->n_pixels);
208 for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
209 dst_pixels[3] = 0xffff;
210 dst_pixels[2] = cpu_to_le16(in_pixels[x].r);
211 dst_pixels[1] = cpu_to_le16(in_pixels[x].g);
212 dst_pixels[0] = cpu_to_le16(in_pixels[x].b);
216 static void argb_u16_to_RGB565(struct vkms_frame_info *frame_info,
217 const struct line_buffer *src_buffer, int y)
219 int x_dst = frame_info->dst.x1;
220 u16 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
221 struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
222 int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
223 src_buffer->n_pixels);
225 s64 fp_rb_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(31));
226 s64 fp_g_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(63));
228 for (size_t x = 0; x < x_limit; x++, dst_pixels++) {
229 s64 fp_r = drm_int2fixp(in_pixels[x].r);
230 s64 fp_g = drm_int2fixp(in_pixels[x].g);
231 s64 fp_b = drm_int2fixp(in_pixels[x].b);
233 u16 r = drm_fixp2int(drm_fixp_div(fp_r, fp_rb_ratio));
234 u16 g = drm_fixp2int(drm_fixp_div(fp_g, fp_g_ratio));
235 u16 b = drm_fixp2int(drm_fixp_div(fp_b, fp_rb_ratio));
237 *dst_pixels = cpu_to_le16(r << 11 | g << 5 | b);
241 void *get_pixel_conversion_function(u32 format)
244 case DRM_FORMAT_ARGB8888:
245 return &ARGB8888_to_argb_u16;
246 case DRM_FORMAT_XRGB8888:
247 return &XRGB8888_to_argb_u16;
248 case DRM_FORMAT_ARGB16161616:
249 return &ARGB16161616_to_argb_u16;
250 case DRM_FORMAT_XRGB16161616:
251 return &XRGB16161616_to_argb_u16;
252 case DRM_FORMAT_RGB565:
253 return &RGB565_to_argb_u16;
259 void *get_line_to_frame_function(u32 format)
262 case DRM_FORMAT_ARGB8888:
263 return &argb_u16_to_ARGB8888;
264 case DRM_FORMAT_XRGB8888:
265 return &argb_u16_to_XRGB8888;
266 case DRM_FORMAT_ARGB16161616:
267 return &argb_u16_to_ARGB16161616;
268 case DRM_FORMAT_XRGB16161616:
269 return &argb_u16_to_XRGB16161616;
270 case DRM_FORMAT_RGB565:
271 return &argb_u16_to_RGB565;