]> Git Repo - linux.git/blob - drivers/gpu/drm/vkms/vkms_formats.c
Merge tag 'cxl-for-6.10' of git://git.kernel.org/pub/scm/linux/kernel/git/cxl/cxl
[linux.git] / drivers / gpu / drm / vkms / vkms_formats.c
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 /**
115  * vkms_compose_row - compose a single row of a plane
116  * @stage_buffer: output line with the composed pixels
117  * @plane: state of the plane that is being composed
118  * @y: y coordinate of the row
119  *
120  * This function composes a single row of a plane. It gets the source pixels
121  * through the y coordinate (see get_packed_src_addr()) and goes linearly
122  * through the source pixel, reading the pixels and converting it to
123  * ARGB16161616 (see the pixel_read() callback). For rotate-90 and rotate-270,
124  * the source pixels are not traversed linearly. The source pixels are queried
125  * on each iteration in order to traverse the pixels vertically.
126  */
127 void vkms_compose_row(struct line_buffer *stage_buffer, struct vkms_plane_state *plane, int y)
128 {
129         struct pixel_argb_u16 *out_pixels = stage_buffer->pixels;
130         struct vkms_frame_info *frame_info = plane->frame_info;
131         u8 *src_pixels = get_packed_src_addr(frame_info, y);
132         int limit = min_t(size_t, drm_rect_width(&frame_info->dst), stage_buffer->n_pixels);
133
134         for (size_t x = 0; x < limit; x++, src_pixels += frame_info->cpp) {
135                 int x_pos = get_x_position(frame_info, limit, x);
136
137                 if (drm_rotation_90_or_270(frame_info->rotation))
138                         src_pixels = get_packed_src_addr(frame_info, x + frame_info->rotated.y1)
139                                 + frame_info->cpp * y;
140
141                 plane->pixel_read(src_pixels, &out_pixels[x_pos]);
142         }
143 }
144
145 /*
146  * The following  functions take an line of argb_u16 pixels from the
147  * src_buffer, convert them to a specific format, and store them in the
148  * destination.
149  *
150  * They are used in the `compose_active_planes` to convert and store a line
151  * from the src_buffer to the writeback buffer.
152  */
153 static void argb_u16_to_ARGB8888(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
154 {
155         /*
156          * This sequence below is important because the format's byte order is
157          * in little-endian. In the case of the ARGB8888 the memory is
158          * organized this way:
159          *
160          * | Addr     | = blue channel
161          * | Addr + 1 | = green channel
162          * | Addr + 2 | = Red channel
163          * | Addr + 3 | = Alpha channel
164          */
165         dst_pixels[3] = DIV_ROUND_CLOSEST(in_pixel->a, 257);
166         dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixel->r, 257);
167         dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixel->g, 257);
168         dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixel->b, 257);
169 }
170
171 static void argb_u16_to_XRGB8888(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
172 {
173         dst_pixels[3] = 0xff;
174         dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixel->r, 257);
175         dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixel->g, 257);
176         dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixel->b, 257);
177 }
178
179 static void argb_u16_to_ARGB16161616(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
180 {
181         u16 *pixels = (u16 *)dst_pixels;
182
183         pixels[3] = cpu_to_le16(in_pixel->a);
184         pixels[2] = cpu_to_le16(in_pixel->r);
185         pixels[1] = cpu_to_le16(in_pixel->g);
186         pixels[0] = cpu_to_le16(in_pixel->b);
187 }
188
189 static void argb_u16_to_XRGB16161616(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
190 {
191         u16 *pixels = (u16 *)dst_pixels;
192
193         pixels[3] = 0xffff;
194         pixels[2] = cpu_to_le16(in_pixel->r);
195         pixels[1] = cpu_to_le16(in_pixel->g);
196         pixels[0] = cpu_to_le16(in_pixel->b);
197 }
198
199 static void argb_u16_to_RGB565(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
200 {
201         u16 *pixels = (u16 *)dst_pixels;
202
203         s64 fp_rb_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(31));
204         s64 fp_g_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(63));
205
206         s64 fp_r = drm_int2fixp(in_pixel->r);
207         s64 fp_g = drm_int2fixp(in_pixel->g);
208         s64 fp_b = drm_int2fixp(in_pixel->b);
209
210         u16 r = drm_fixp2int(drm_fixp_div(fp_r, fp_rb_ratio));
211         u16 g = drm_fixp2int(drm_fixp_div(fp_g, fp_g_ratio));
212         u16 b = drm_fixp2int(drm_fixp_div(fp_b, fp_rb_ratio));
213
214         *pixels = cpu_to_le16(r << 11 | g << 5 | b);
215 }
216
217 void vkms_writeback_row(struct vkms_writeback_job *wb,
218                         const struct line_buffer *src_buffer, int y)
219 {
220         struct vkms_frame_info *frame_info = &wb->wb_frame_info;
221         int x_dst = frame_info->dst.x1;
222         u8 *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), src_buffer->n_pixels);
225
226         for (size_t x = 0; x < x_limit; x++, dst_pixels += frame_info->cpp)
227                 wb->pixel_write(dst_pixels, &in_pixels[x]);
228 }
229
230 void *get_pixel_conversion_function(u32 format)
231 {
232         switch (format) {
233         case DRM_FORMAT_ARGB8888:
234                 return &ARGB8888_to_argb_u16;
235         case DRM_FORMAT_XRGB8888:
236                 return &XRGB8888_to_argb_u16;
237         case DRM_FORMAT_ARGB16161616:
238                 return &ARGB16161616_to_argb_u16;
239         case DRM_FORMAT_XRGB16161616:
240                 return &XRGB16161616_to_argb_u16;
241         case DRM_FORMAT_RGB565:
242                 return &RGB565_to_argb_u16;
243         default:
244                 return NULL;
245         }
246 }
247
248 void *get_pixel_write_function(u32 format)
249 {
250         switch (format) {
251         case DRM_FORMAT_ARGB8888:
252                 return &argb_u16_to_ARGB8888;
253         case DRM_FORMAT_XRGB8888:
254                 return &argb_u16_to_XRGB8888;
255         case DRM_FORMAT_ARGB16161616:
256                 return &argb_u16_to_ARGB16161616;
257         case DRM_FORMAT_XRGB16161616:
258                 return &argb_u16_to_XRGB16161616;
259         case DRM_FORMAT_RGB565:
260                 return &argb_u16_to_RGB565;
261         default:
262                 return NULL;
263         }
264 }
This page took 0.05697 seconds and 4 git commands to generate.