1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/kernel.h>
4 #include <linux/minmax.h>
6 #include <drm/drm_blend.h>
7 #include <drm/drm_rect.h>
8 #include <drm/drm_fixed.h>
10 #include "vkms_formats.h"
12 static size_t pixel_offset(const struct vkms_frame_info *frame_info, int x, int y)
14 return frame_info->offset + (y * frame_info->pitch)
15 + (x * frame_info->cpp);
19 * packed_pixels_addr - Get the pointer to pixel of a given pair of coordinates
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
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).
31 static void *packed_pixels_addr(const struct vkms_frame_info *frame_info,
34 size_t offset = pixel_offset(frame_info, x, y);
36 return (u8 *)frame_info->map[0].vaddr + offset;
39 static void *get_packed_src_addr(const struct vkms_frame_info *frame_info, int y)
41 int x_src = frame_info->src.x1 >> 16;
42 int y_src = y - frame_info->rotated.y1 + (frame_info->src.y1 >> 16);
44 return packed_pixels_addr(frame_info, x_src, y_src);
47 static int get_x_position(const struct vkms_frame_info *frame_info, int limit, int x)
49 if (frame_info->rotation & (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_270))
54 static void ARGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
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.
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;
68 static void XRGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
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;
76 static void ARGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
78 u16 *pixels = (u16 *)src_pixels;
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]);
86 static void XRGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
88 u16 *pixels = (u16 *)src_pixels;
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]);
96 static void RGB565_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
98 u16 *pixels = (u16 *)src_pixels;
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));
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);
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));
114 void vkms_compose_row(struct line_buffer *stage_buffer, struct vkms_plane_state *plane, int y)
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);
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);
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;
128 plane->pixel_read(src_pixels, &out_pixels[x_pos]);
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
137 * They are used in the `compose_active_planes` to convert and store a line
138 * from the src_buffer to the writeback buffer.
140 static void argb_u16_to_ARGB8888(struct vkms_frame_info *frame_info,
141 const struct line_buffer *src_buffer, int y)
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);
149 for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
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:
155 * | Addr | = blue channel
156 * | Addr + 1 | = green channel
157 * | Addr + 2 | = Red channel
158 * | Addr + 3 | = Alpha channel
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);
167 static void argb_u16_to_XRGB8888(struct vkms_frame_info *frame_info,
168 const struct line_buffer *src_buffer, int y)
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);
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);
184 static void argb_u16_to_ARGB16161616(struct vkms_frame_info *frame_info,
185 const struct line_buffer *src_buffer, int y)
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);
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);
201 static void argb_u16_to_XRGB16161616(struct vkms_frame_info *frame_info,
202 const struct line_buffer *src_buffer, int y)
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);
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);
218 static void argb_u16_to_RGB565(struct vkms_frame_info *frame_info,
219 const struct line_buffer *src_buffer, int y)
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);
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));
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);
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));
239 *dst_pixels = cpu_to_le16(r << 11 | g << 5 | b);
243 void *get_pixel_conversion_function(u32 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;
261 void *get_line_to_frame_function(u32 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;