drm/vkms: add rotate-90 property
[platform/kernel/linux-rpi.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 #include <drm/drm_rect.h>
6 #include <drm/drm_fixed.h>
7
8 #include "vkms_formats.h"
9
10 static size_t pixel_offset(const struct vkms_frame_info *frame_info, int x, int y)
11 {
12         return frame_info->offset + (y * frame_info->pitch)
13                                   + (x * frame_info->cpp);
14 }
15
16 /*
17  * packed_pixels_addr - Get the pointer to pixel of a given pair of coordinates
18  *
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
22  *
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).
28  */
29 static void *packed_pixels_addr(const struct vkms_frame_info *frame_info,
30                                 int x, int y)
31 {
32         size_t offset = pixel_offset(frame_info, x, y);
33
34         return (u8 *)frame_info->map[0].vaddr + offset;
35 }
36
37 static void *get_packed_src_addr(const struct vkms_frame_info *frame_info, int y)
38 {
39         int x_src = frame_info->src.x1 >> 16;
40         int y_src = y - frame_info->rotated.y1 + (frame_info->src.y1 >> 16);
41
42         return packed_pixels_addr(frame_info, x_src, y_src);
43 }
44
45 static int get_x_position(const struct vkms_frame_info *frame_info, int limit, int x)
46 {
47         if (frame_info->rotation & DRM_MODE_REFLECT_X)
48                 return limit - x - 1;
49         return x;
50 }
51
52 static void ARGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
53 {
54         /*
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.
59          */
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;
64 }
65
66 static void XRGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
67 {
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;
72 }
73
74 static void ARGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
75 {
76         u16 *pixels = (u16 *)src_pixels;
77
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]);
82 }
83
84 static void XRGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
85 {
86         u16 *pixels = (u16 *)src_pixels;
87
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]);
92 }
93
94 static void RGB565_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
95 {
96         u16 *pixels = (u16 *)src_pixels;
97
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));
100
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);
105
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));
110 }
111
112 void vkms_compose_row(struct line_buffer *stage_buffer, struct vkms_plane_state *plane, int y)
113 {
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);
118
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);
121
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;
125
126                 plane->pixel_read(src_pixels, &out_pixels[x_pos]);
127         }
128 }
129
130 /*
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
133  * destination.
134  *
135  * They are used in the `compose_active_planes` to convert and store a line
136  * from the src_buffer to the writeback buffer.
137  */
138 static void argb_u16_to_ARGB8888(struct vkms_frame_info *frame_info,
139                                  const struct line_buffer *src_buffer, int y)
140 {
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);
146
147         for (size_t x = 0; x < x_limit; x++, dst_pixels += 4) {
148                 /*
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:
152                  *
153                  * | Addr     | = blue channel
154                  * | Addr + 1 | = green channel
155                  * | Addr + 2 | = Red channel
156                  * | Addr + 3 | = Alpha channel
157                  */
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);
162         }
163 }
164
165 static void argb_u16_to_XRGB8888(struct vkms_frame_info *frame_info,
166                                  const struct line_buffer *src_buffer, int y)
167 {
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);
173
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);
179         }
180 }
181
182 static void argb_u16_to_ARGB16161616(struct vkms_frame_info *frame_info,
183                                      const struct line_buffer *src_buffer, int y)
184 {
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);
190
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);
196         }
197 }
198
199 static void argb_u16_to_XRGB16161616(struct vkms_frame_info *frame_info,
200                                      const struct line_buffer *src_buffer, int y)
201 {
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);
207
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);
213         }
214 }
215
216 static void argb_u16_to_RGB565(struct vkms_frame_info *frame_info,
217                                const struct line_buffer *src_buffer, int y)
218 {
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);
224
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));
227
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);
232
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));
236
237                 *dst_pixels = cpu_to_le16(r << 11 | g << 5 | b);
238         }
239 }
240
241 void *get_pixel_conversion_function(u32 format)
242 {
243         switch (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;
254         default:
255                 return NULL;
256         }
257 }
258
259 void *get_line_to_frame_function(u32 format)
260 {
261         switch (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;
272         default:
273                 return NULL;
274         }
275 }