drm/vkms: add rotate-90 property
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / vkms / vkms_composer.c
1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include <linux/crc32.h>
4
5 #include <drm/drm_atomic.h>
6 #include <drm/drm_atomic_helper.h>
7 #include <drm/drm_fourcc.h>
8 #include <drm/drm_gem_framebuffer_helper.h>
9 #include <drm/drm_vblank.h>
10 #include <linux/minmax.h>
11
12 #include "vkms_drv.h"
13
14 static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
15 {
16         u32 new_color;
17
18         new_color = (src * 0xffff + dst * (0xffff - alpha));
19
20         return DIV_ROUND_CLOSEST(new_color, 0xffff);
21 }
22
23 /**
24  * pre_mul_alpha_blend - alpha blending equation
25  * @src_frame_info: source framebuffer's metadata
26  * @stage_buffer: The line with the pixels from src_plane
27  * @output_buffer: A line buffer that receives all the blends output
28  *
29  * Using the information from the `frame_info`, this blends only the
30  * necessary pixels from the `stage_buffer` to the `output_buffer`
31  * using premultiplied blend formula.
32  *
33  * The current DRM assumption is that pixel color values have been already
34  * pre-multiplied with the alpha channel values. See more
35  * drm_plane_create_blend_mode_property(). Also, this formula assumes a
36  * completely opaque background.
37  */
38 static void pre_mul_alpha_blend(struct vkms_frame_info *frame_info,
39                                 struct line_buffer *stage_buffer,
40                                 struct line_buffer *output_buffer)
41 {
42         int x_dst = frame_info->dst.x1;
43         struct pixel_argb_u16 *out = output_buffer->pixels + x_dst;
44         struct pixel_argb_u16 *in = stage_buffer->pixels;
45         int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
46                             stage_buffer->n_pixels);
47
48         for (int x = 0; x < x_limit; x++) {
49                 out[x].a = (u16)0xffff;
50                 out[x].r = pre_mul_blend_channel(in[x].r, out[x].r, in[x].a);
51                 out[x].g = pre_mul_blend_channel(in[x].g, out[x].g, in[x].a);
52                 out[x].b = pre_mul_blend_channel(in[x].b, out[x].b, in[x].a);
53         }
54 }
55
56 static int get_y_pos(struct vkms_frame_info *frame_info, int y)
57 {
58         if (frame_info->rotation & DRM_MODE_REFLECT_Y)
59                 return drm_rect_height(&frame_info->rotated) - y - 1;
60
61         switch (frame_info->rotation & DRM_MODE_ROTATE_MASK) {
62         case DRM_MODE_ROTATE_90:
63                 return frame_info->rotated.x2 - y - 1;
64         default:
65                 return y;
66         }
67 }
68
69 static bool check_limit(struct vkms_frame_info *frame_info, int pos)
70 {
71         if (frame_info->rotation & DRM_MODE_ROTATE_90) {
72                 if (pos >= 0 && pos < drm_rect_width(&frame_info->rotated))
73                         return true;
74         } else {
75                 if (pos >= frame_info->rotated.y1 && pos < frame_info->rotated.y2)
76                         return true;
77         }
78
79         return false;
80 }
81
82 static void fill_background(const struct pixel_argb_u16 *background_color,
83                             struct line_buffer *output_buffer)
84 {
85         for (size_t i = 0; i < output_buffer->n_pixels; i++)
86                 output_buffer->pixels[i] = *background_color;
87 }
88
89 /**
90  * @wb_frame_info: The writeback frame buffer metadata
91  * @crtc_state: The crtc state
92  * @crc32: The crc output of the final frame
93  * @output_buffer: A buffer of a row that will receive the result of the blend(s)
94  * @stage_buffer: The line with the pixels from plane being blend to the output
95  *
96  * This function blends the pixels (Using the `pre_mul_alpha_blend`)
97  * from all planes, calculates the crc32 of the output from the former step,
98  * and, if necessary, convert and store the output to the writeback buffer.
99  */
100 static void blend(struct vkms_writeback_job *wb,
101                   struct vkms_crtc_state *crtc_state,
102                   u32 *crc32, struct line_buffer *stage_buffer,
103                   struct line_buffer *output_buffer, size_t row_size)
104 {
105         struct vkms_plane_state **plane = crtc_state->active_planes;
106         u32 n_active_planes = crtc_state->num_active_planes;
107         int y_pos;
108
109         const struct pixel_argb_u16 background_color = { .a = 0xffff };
110
111         size_t crtc_y_limit = crtc_state->base.crtc->mode.vdisplay;
112
113         for (size_t y = 0; y < crtc_y_limit; y++) {
114                 fill_background(&background_color, output_buffer);
115
116                 /* The active planes are composed associatively in z-order. */
117                 for (size_t i = 0; i < n_active_planes; i++) {
118                         y_pos = get_y_pos(plane[i]->frame_info, y);
119
120                         if (!check_limit(plane[i]->frame_info, y_pos))
121                                 continue;
122
123                         vkms_compose_row(stage_buffer, plane[i], y_pos);
124                         pre_mul_alpha_blend(plane[i]->frame_info, stage_buffer,
125                                             output_buffer);
126                 }
127
128                 *crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
129
130                 if (wb)
131                         wb->wb_write(&wb->wb_frame_info, output_buffer, y_pos);
132         }
133 }
134
135 static int check_format_funcs(struct vkms_crtc_state *crtc_state,
136                               struct vkms_writeback_job *active_wb)
137 {
138         struct vkms_plane_state **planes = crtc_state->active_planes;
139         u32 n_active_planes = crtc_state->num_active_planes;
140
141         for (size_t i = 0; i < n_active_planes; i++)
142                 if (!planes[i]->pixel_read)
143                         return -1;
144
145         if (active_wb && !active_wb->wb_write)
146                 return -1;
147
148         return 0;
149 }
150
151 static int check_iosys_map(struct vkms_crtc_state *crtc_state)
152 {
153         struct vkms_plane_state **plane_state = crtc_state->active_planes;
154         u32 n_active_planes = crtc_state->num_active_planes;
155
156         for (size_t i = 0; i < n_active_planes; i++)
157                 if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
158                         return -1;
159
160         return 0;
161 }
162
163 static int compose_active_planes(struct vkms_writeback_job *active_wb,
164                                  struct vkms_crtc_state *crtc_state,
165                                  u32 *crc32)
166 {
167         size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
168         struct line_buffer output_buffer, stage_buffer;
169         int ret = 0;
170
171         /*
172          * This check exists so we can call `crc32_le` for the entire line
173          * instead doing it for each channel of each pixel in case
174          * `struct `pixel_argb_u16` had any gap added by the compiler
175          * between the struct fields.
176          */
177         static_assert(sizeof(struct pixel_argb_u16) == 8);
178
179         if (WARN_ON(check_iosys_map(crtc_state)))
180                 return -EINVAL;
181
182         if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
183                 return -EINVAL;
184
185         line_width = crtc_state->base.crtc->mode.hdisplay;
186         stage_buffer.n_pixels = line_width;
187         output_buffer.n_pixels = line_width;
188
189         stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
190         if (!stage_buffer.pixels) {
191                 DRM_ERROR("Cannot allocate memory for the output line buffer");
192                 return -ENOMEM;
193         }
194
195         output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
196         if (!output_buffer.pixels) {
197                 DRM_ERROR("Cannot allocate memory for intermediate line buffer");
198                 ret = -ENOMEM;
199                 goto free_stage_buffer;
200         }
201
202         blend(active_wb, crtc_state, crc32, &stage_buffer,
203               &output_buffer, line_width * pixel_size);
204
205         kvfree(output_buffer.pixels);
206 free_stage_buffer:
207         kvfree(stage_buffer.pixels);
208
209         return ret;
210 }
211
212 /**
213  * vkms_composer_worker - ordered work_struct to compute CRC
214  *
215  * @work: work_struct
216  *
217  * Work handler for composing and computing CRCs. work_struct scheduled in
218  * an ordered workqueue that's periodically scheduled to run by
219  * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
220  */
221 void vkms_composer_worker(struct work_struct *work)
222 {
223         struct vkms_crtc_state *crtc_state = container_of(work,
224                                                 struct vkms_crtc_state,
225                                                 composer_work);
226         struct drm_crtc *crtc = crtc_state->base.crtc;
227         struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
228         struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
229         bool crc_pending, wb_pending;
230         u64 frame_start, frame_end;
231         u32 crc32 = 0;
232         int ret;
233
234         spin_lock_irq(&out->composer_lock);
235         frame_start = crtc_state->frame_start;
236         frame_end = crtc_state->frame_end;
237         crc_pending = crtc_state->crc_pending;
238         wb_pending = crtc_state->wb_pending;
239         crtc_state->frame_start = 0;
240         crtc_state->frame_end = 0;
241         crtc_state->crc_pending = false;
242         spin_unlock_irq(&out->composer_lock);
243
244         /*
245          * We raced with the vblank hrtimer and previous work already computed
246          * the crc, nothing to do.
247          */
248         if (!crc_pending)
249                 return;
250
251         if (wb_pending)
252                 ret = compose_active_planes(active_wb, crtc_state, &crc32);
253         else
254                 ret = compose_active_planes(NULL, crtc_state, &crc32);
255
256         if (ret)
257                 return;
258
259         if (wb_pending) {
260                 drm_writeback_signal_completion(&out->wb_connector, 0);
261                 spin_lock_irq(&out->composer_lock);
262                 crtc_state->wb_pending = false;
263                 spin_unlock_irq(&out->composer_lock);
264         }
265
266         /*
267          * The worker can fall behind the vblank hrtimer, make sure we catch up.
268          */
269         while (frame_start <= frame_end)
270                 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
271 }
272
273 static const char * const pipe_crc_sources[] = {"auto"};
274
275 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
276                                         size_t *count)
277 {
278         *count = ARRAY_SIZE(pipe_crc_sources);
279         return pipe_crc_sources;
280 }
281
282 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
283 {
284         int ret = 0;
285
286         if (!src_name) {
287                 *enabled = false;
288         } else if (strcmp(src_name, "auto") == 0) {
289                 *enabled = true;
290         } else {
291                 *enabled = false;
292                 ret = -EINVAL;
293         }
294
295         return ret;
296 }
297
298 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
299                            size_t *values_cnt)
300 {
301         bool enabled;
302
303         if (vkms_crc_parse_source(src_name, &enabled) < 0) {
304                 DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
305                 return -EINVAL;
306         }
307
308         *values_cnt = 1;
309
310         return 0;
311 }
312
313 void vkms_set_composer(struct vkms_output *out, bool enabled)
314 {
315         bool old_enabled;
316
317         if (enabled)
318                 drm_crtc_vblank_get(&out->crtc);
319
320         spin_lock_irq(&out->lock);
321         old_enabled = out->composer_enabled;
322         out->composer_enabled = enabled;
323         spin_unlock_irq(&out->lock);
324
325         if (old_enabled)
326                 drm_crtc_vblank_put(&out->crtc);
327 }
328
329 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
330 {
331         struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
332         bool enabled = false;
333         int ret = 0;
334
335         ret = vkms_crc_parse_source(src_name, &enabled);
336
337         vkms_set_composer(out, enabled);
338
339         return ret;
340 }