]> Git Repo - linux.git/blob - drivers/gpu/drm/vkms/vkms_composer.c
Merge tag 'linux-watchdog-6.14-rc1' of git://www.linux-watchdog.org/linux-watchdog
[linux.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_blend.h>
8 #include <drm/drm_fourcc.h>
9 #include <drm/drm_fixed.h>
10 #include <drm/drm_gem_framebuffer_helper.h>
11 #include <drm/drm_vblank.h>
12 #include <linux/minmax.h>
13
14 #include "vkms_drv.h"
15
16 static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
17 {
18         u32 new_color;
19
20         new_color = (src * 0xffff + dst * (0xffff - alpha));
21
22         return DIV_ROUND_CLOSEST(new_color, 0xffff);
23 }
24
25 /**
26  * pre_mul_alpha_blend - alpha blending equation
27  * @stage_buffer: The line with the pixels from src_plane
28  * @output_buffer: A line buffer that receives all the blends output
29  * @x_start: The start offset
30  * @pixel_count: The number of pixels to blend
31  *
32  * The pixels [@x_start;@x_start+@pixel_count) in stage_buffer are blended at
33  * [@x_start;@x_start+@pixel_count) in output_buffer.
34  *
35  * The current DRM assumption is that pixel color values have been already
36  * pre-multiplied with the alpha channel values. See more
37  * drm_plane_create_blend_mode_property(). Also, this formula assumes a
38  * completely opaque background.
39  */
40 static void pre_mul_alpha_blend(const struct line_buffer *stage_buffer,
41                                 struct line_buffer *output_buffer, int x_start, int pixel_count)
42 {
43         struct pixel_argb_u16 *out = &output_buffer->pixels[x_start];
44         const struct pixel_argb_u16 *in = &stage_buffer->pixels[x_start];
45
46         for (int i = 0; i < pixel_count; i++) {
47                 out[i].a = (u16)0xffff;
48                 out[i].r = pre_mul_blend_channel(in[i].r, out[i].r, in[i].a);
49                 out[i].g = pre_mul_blend_channel(in[i].g, out[i].g, in[i].a);
50                 out[i].b = pre_mul_blend_channel(in[i].b, out[i].b, in[i].a);
51         }
52 }
53
54
55 static void fill_background(const struct pixel_argb_u16 *background_color,
56                             struct line_buffer *output_buffer)
57 {
58         for (size_t i = 0; i < output_buffer->n_pixels; i++)
59                 output_buffer->pixels[i] = *background_color;
60 }
61
62 // lerp(a, b, t) = a + (b - a) * t
63 static u16 lerp_u16(u16 a, u16 b, s64 t)
64 {
65         s64 a_fp = drm_int2fixp(a);
66         s64 b_fp = drm_int2fixp(b);
67
68         s64 delta = drm_fixp_mul(b_fp - a_fp, t);
69
70         return drm_fixp2int(a_fp + delta);
71 }
72
73 static s64 get_lut_index(const struct vkms_color_lut *lut, u16 channel_value)
74 {
75         s64 color_channel_fp = drm_int2fixp(channel_value);
76
77         return drm_fixp_mul(color_channel_fp, lut->channel_value2index_ratio);
78 }
79
80 /*
81  * This enum is related to the positions of the variables inside
82  * `struct drm_color_lut`, so the order of both needs to be the same.
83  */
84 enum lut_channel {
85         LUT_RED = 0,
86         LUT_GREEN,
87         LUT_BLUE,
88         LUT_RESERVED
89 };
90
91 static u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_value,
92                                       enum lut_channel channel)
93 {
94         s64 lut_index = get_lut_index(lut, channel_value);
95         u16 *floor_lut_value, *ceil_lut_value;
96         u16 floor_channel_value, ceil_channel_value;
97
98         /*
99          * This checks if `struct drm_color_lut` has any gap added by the compiler
100          * between the struct fields.
101          */
102         static_assert(sizeof(struct drm_color_lut) == sizeof(__u16) * 4);
103
104         floor_lut_value = (__u16 *)&lut->base[drm_fixp2int(lut_index)];
105         if (drm_fixp2int(lut_index) == (lut->lut_length - 1))
106                 /* We're at the end of the LUT array, use same value for ceil and floor */
107                 ceil_lut_value = floor_lut_value;
108         else
109                 ceil_lut_value = (__u16 *)&lut->base[drm_fixp2int_ceil(lut_index)];
110
111         floor_channel_value = floor_lut_value[channel];
112         ceil_channel_value = ceil_lut_value[channel];
113
114         return lerp_u16(floor_channel_value, ceil_channel_value,
115                         lut_index & DRM_FIXED_DECIMAL_MASK);
116 }
117
118 static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
119 {
120         if (!crtc_state->gamma_lut.base)
121                 return;
122
123         if (!crtc_state->gamma_lut.lut_length)
124                 return;
125
126         for (size_t x = 0; x < output_buffer->n_pixels; x++) {
127                 struct pixel_argb_u16 *pixel = &output_buffer->pixels[x];
128
129                 pixel->r = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->r, LUT_RED);
130                 pixel->g = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->g, LUT_GREEN);
131                 pixel->b = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->b, LUT_BLUE);
132         }
133 }
134
135 /**
136  * direction_for_rotation() - Get the correct reading direction for a given rotation
137  *
138  * @rotation: Rotation to analyze. It correspond the field @frame_info.rotation.
139  *
140  * This function will use the @rotation setting of a source plane to compute the reading
141  * direction in this plane which correspond to a "left to right writing" in the CRTC.
142  * For example, if the buffer is reflected on X axis, the pixel must be read from right to left
143  * to be written from left to right on the CRTC.
144  */
145 static enum pixel_read_direction direction_for_rotation(unsigned int rotation)
146 {
147         struct drm_rect tmp_a, tmp_b;
148         int x, y;
149
150         /*
151          * Points A and B are depicted as zero-size rectangles on the CRTC.
152          * The CRTC writing direction is from A to B. The plane reading direction
153          * is discovered by inverse-transforming A and B.
154          * The reading direction is computed by rotating the vector AB (top-left to top-right) in a
155          * 1x1 square.
156          */
157
158         tmp_a = DRM_RECT_INIT(0, 0, 0, 0);
159         tmp_b = DRM_RECT_INIT(1, 0, 0, 0);
160         drm_rect_rotate_inv(&tmp_a, 1, 1, rotation);
161         drm_rect_rotate_inv(&tmp_b, 1, 1, rotation);
162
163         x = tmp_b.x1 - tmp_a.x1;
164         y = tmp_b.y1 - tmp_a.y1;
165
166         if (x == 1 && y == 0)
167                 return READ_LEFT_TO_RIGHT;
168         else if (x == -1 && y == 0)
169                 return READ_RIGHT_TO_LEFT;
170         else if (y == 1 && x == 0)
171                 return READ_TOP_TO_BOTTOM;
172         else if (y == -1 && x == 0)
173                 return READ_BOTTOM_TO_TOP;
174
175         WARN_ONCE(true, "The inverse of the rotation gives an incorrect direction.");
176         return READ_LEFT_TO_RIGHT;
177 }
178
179 /**
180  * clamp_line_coordinates() - Compute and clamp the coordinate to read and write during the blend
181  * process.
182  *
183  * @direction: direction of the reading
184  * @current_plane: current plane blended
185  * @src_line: source line of the reading. Only the top-left coordinate is used. This rectangle
186  * must be rotated and have a shape of 1*pixel_count if @direction is vertical and a shape of
187  * pixel_count*1 if @direction is horizontal.
188  * @src_x_start: x start coordinate for the line reading
189  * @src_y_start: y start coordinate for the line reading
190  * @dst_x_start: x coordinate to blend the read line
191  * @pixel_count: number of pixels to blend
192  *
193  * This function is mainly a safety net to avoid reading outside the source buffer. As the
194  * userspace should never ask to read outside the source plane, all the cases covered here should
195  * be dead code.
196  */
197 static void clamp_line_coordinates(enum pixel_read_direction direction,
198                                    const struct vkms_plane_state *current_plane,
199                                    const struct drm_rect *src_line, int *src_x_start,
200                                    int *src_y_start, int *dst_x_start, int *pixel_count)
201 {
202         /* By default the start points are correct */
203         *src_x_start = src_line->x1;
204         *src_y_start = src_line->y1;
205         *dst_x_start = current_plane->frame_info->dst.x1;
206
207         /* Get the correct number of pixel to blend, it depends of the direction */
208         switch (direction) {
209         case READ_LEFT_TO_RIGHT:
210         case READ_RIGHT_TO_LEFT:
211                 *pixel_count = drm_rect_width(src_line);
212                 break;
213         case READ_BOTTOM_TO_TOP:
214         case READ_TOP_TO_BOTTOM:
215                 *pixel_count = drm_rect_height(src_line);
216                 break;
217         }
218
219         /*
220          * Clamp the coordinates to avoid reading outside the buffer
221          *
222          * This is mainly a security check to avoid reading outside the buffer, the userspace
223          * should never request to read outside the source buffer.
224          */
225         switch (direction) {
226         case READ_LEFT_TO_RIGHT:
227         case READ_RIGHT_TO_LEFT:
228                 if (*src_x_start < 0) {
229                         *pixel_count += *src_x_start;
230                         *dst_x_start -= *src_x_start;
231                         *src_x_start = 0;
232                 }
233                 if (*src_x_start + *pixel_count > current_plane->frame_info->fb->width)
234                         *pixel_count = max(0, (int)current_plane->frame_info->fb->width -
235                                 *src_x_start);
236                 break;
237         case READ_BOTTOM_TO_TOP:
238         case READ_TOP_TO_BOTTOM:
239                 if (*src_y_start < 0) {
240                         *pixel_count += *src_y_start;
241                         *dst_x_start -= *src_y_start;
242                         *src_y_start = 0;
243                 }
244                 if (*src_y_start + *pixel_count > current_plane->frame_info->fb->height)
245                         *pixel_count = max(0, (int)current_plane->frame_info->fb->height -
246                                 *src_y_start);
247                 break;
248         }
249 }
250
251 /**
252  * blend_line() - Blend a line from a plane to the output buffer
253  *
254  * @current_plane: current plane to work on
255  * @y: line to write in the output buffer
256  * @crtc_x_limit: width of the output buffer
257  * @stage_buffer: temporary buffer to convert the pixel line from the source buffer
258  * @output_buffer: buffer to blend the read line into.
259  */
260 static void blend_line(struct vkms_plane_state *current_plane, int y,
261                        int crtc_x_limit, struct line_buffer *stage_buffer,
262                        struct line_buffer *output_buffer)
263 {
264         int src_x_start, src_y_start, dst_x_start, pixel_count;
265         struct drm_rect dst_line, tmp_src, src_line;
266
267         /* Avoid rendering useless lines */
268         if (y < current_plane->frame_info->dst.y1 ||
269             y >= current_plane->frame_info->dst.y2)
270                 return;
271
272         /*
273          * dst_line is the line to copy. The initial coordinates are inside the
274          * destination framebuffer, and then drm_rect_* helpers are used to
275          * compute the correct position into the source framebuffer.
276          */
277         dst_line = DRM_RECT_INIT(current_plane->frame_info->dst.x1, y,
278                                  drm_rect_width(&current_plane->frame_info->dst),
279                                  1);
280
281         drm_rect_fp_to_int(&tmp_src, &current_plane->frame_info->src);
282
283         /*
284          * [1]: Clamping src_line to the crtc_x_limit to avoid writing outside of
285          * the destination buffer
286          */
287         dst_line.x1 = max_t(int, dst_line.x1, 0);
288         dst_line.x2 = min_t(int, dst_line.x2, crtc_x_limit);
289         /* The destination is completely outside of the crtc. */
290         if (dst_line.x2 <= dst_line.x1)
291                 return;
292
293         src_line = dst_line;
294
295         /*
296          * Transform the coordinate x/y from the crtc to coordinates into
297          * coordinates for the src buffer.
298          *
299          * - Cancel the offset of the dst buffer.
300          * - Invert the rotation. This assumes that
301          *   dst = drm_rect_rotate(src, rotation) (dst and src have the
302          *   same size, but can be rotated).
303          * - Apply the offset of the source rectangle to the coordinate.
304          */
305         drm_rect_translate(&src_line, -current_plane->frame_info->dst.x1,
306                            -current_plane->frame_info->dst.y1);
307         drm_rect_rotate_inv(&src_line, drm_rect_width(&tmp_src),
308                             drm_rect_height(&tmp_src),
309                             current_plane->frame_info->rotation);
310         drm_rect_translate(&src_line, tmp_src.x1, tmp_src.y1);
311
312         /* Get the correct reading direction in the source buffer. */
313
314         enum pixel_read_direction direction =
315                 direction_for_rotation(current_plane->frame_info->rotation);
316
317         /* [2]: Compute and clamp the number of pixel to read */
318         clamp_line_coordinates(direction, current_plane, &src_line, &src_x_start, &src_y_start,
319                                &dst_x_start, &pixel_count);
320
321         if (pixel_count <= 0) {
322                 /* Nothing to read, so avoid multiple function calls */
323                 return;
324         }
325
326         /*
327          * Modify the starting point to take in account the rotation
328          *
329          * src_line is the top-left corner, so when reading READ_RIGHT_TO_LEFT or
330          * READ_BOTTOM_TO_TOP, it must be changed to the top-right/bottom-left
331          * corner.
332          */
333         if (direction == READ_RIGHT_TO_LEFT) {
334                 // src_x_start is now the right point
335                 src_x_start += pixel_count - 1;
336         } else if (direction == READ_BOTTOM_TO_TOP) {
337                 // src_y_start is now the bottom point
338                 src_y_start += pixel_count - 1;
339         }
340
341         /*
342          * Perform the conversion and the blending
343          *
344          * Here we know that the read line (x_start, y_start, pixel_count) is
345          * inside the source buffer [2] and we don't write outside the stage
346          * buffer [1].
347          */
348         current_plane->pixel_read_line(current_plane, src_x_start, src_y_start, direction,
349                                        pixel_count, &stage_buffer->pixels[dst_x_start]);
350
351         pre_mul_alpha_blend(stage_buffer, output_buffer,
352                             dst_x_start, pixel_count);
353 }
354
355 /**
356  * blend - blend the pixels from all planes and compute crc
357  * @wb: The writeback frame buffer metadata
358  * @crtc_state: The crtc state
359  * @crc32: The crc output of the final frame
360  * @output_buffer: A buffer of a row that will receive the result of the blend(s)
361  * @stage_buffer: The line with the pixels from plane being blend to the output
362  * @row_size: The size, in bytes, of a single row
363  *
364  * This function blends the pixels (Using the `pre_mul_alpha_blend`)
365  * from all planes, calculates the crc32 of the output from the former step,
366  * and, if necessary, convert and store the output to the writeback buffer.
367  */
368 static void blend(struct vkms_writeback_job *wb,
369                   struct vkms_crtc_state *crtc_state,
370                   u32 *crc32, struct line_buffer *stage_buffer,
371                   struct line_buffer *output_buffer, size_t row_size)
372 {
373         struct vkms_plane_state **plane = crtc_state->active_planes;
374         u32 n_active_planes = crtc_state->num_active_planes;
375
376         const struct pixel_argb_u16 background_color = { .a = 0xffff };
377
378         int crtc_y_limit = crtc_state->base.mode.vdisplay;
379         int crtc_x_limit = crtc_state->base.mode.hdisplay;
380
381         /*
382          * The planes are composed line-by-line to avoid heavy memory usage. It is a necessary
383          * complexity to avoid poor blending performance.
384          *
385          * The function pixel_read_line callback is used to read a line, using an efficient
386          * algorithm for a specific format, into the staging buffer.
387          */
388         for (int y = 0; y < crtc_y_limit; y++) {
389                 fill_background(&background_color, output_buffer);
390
391                 /* The active planes are composed associatively in z-order. */
392                 for (size_t i = 0; i < n_active_planes; i++) {
393                         blend_line(plane[i], y, crtc_x_limit, stage_buffer, output_buffer);
394                 }
395
396                 apply_lut(crtc_state, output_buffer);
397
398                 *crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
399
400                 if (wb)
401                         vkms_writeback_row(wb, output_buffer, y);
402         }
403 }
404
405 static int check_format_funcs(struct vkms_crtc_state *crtc_state,
406                               struct vkms_writeback_job *active_wb)
407 {
408         struct vkms_plane_state **planes = crtc_state->active_planes;
409         u32 n_active_planes = crtc_state->num_active_planes;
410
411         for (size_t i = 0; i < n_active_planes; i++)
412                 if (!planes[i]->pixel_read_line)
413                         return -1;
414
415         if (active_wb && !active_wb->pixel_write)
416                 return -1;
417
418         return 0;
419 }
420
421 static int check_iosys_map(struct vkms_crtc_state *crtc_state)
422 {
423         struct vkms_plane_state **plane_state = crtc_state->active_planes;
424         u32 n_active_planes = crtc_state->num_active_planes;
425
426         for (size_t i = 0; i < n_active_planes; i++)
427                 if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
428                         return -1;
429
430         return 0;
431 }
432
433 static int compose_active_planes(struct vkms_writeback_job *active_wb,
434                                  struct vkms_crtc_state *crtc_state,
435                                  u32 *crc32)
436 {
437         size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
438         struct line_buffer output_buffer, stage_buffer;
439         int ret = 0;
440
441         /*
442          * This check exists so we can call `crc32_le` for the entire line
443          * instead doing it for each channel of each pixel in case
444          * `struct `pixel_argb_u16` had any gap added by the compiler
445          * between the struct fields.
446          */
447         static_assert(sizeof(struct pixel_argb_u16) == 8);
448
449         if (WARN_ON(check_iosys_map(crtc_state)))
450                 return -EINVAL;
451
452         if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
453                 return -EINVAL;
454
455         line_width = crtc_state->base.mode.hdisplay;
456         stage_buffer.n_pixels = line_width;
457         output_buffer.n_pixels = line_width;
458
459         stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
460         if (!stage_buffer.pixels) {
461                 DRM_ERROR("Cannot allocate memory for the output line buffer");
462                 return -ENOMEM;
463         }
464
465         output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
466         if (!output_buffer.pixels) {
467                 DRM_ERROR("Cannot allocate memory for intermediate line buffer");
468                 ret = -ENOMEM;
469                 goto free_stage_buffer;
470         }
471
472         blend(active_wb, crtc_state, crc32, &stage_buffer,
473               &output_buffer, line_width * pixel_size);
474
475         kvfree(output_buffer.pixels);
476 free_stage_buffer:
477         kvfree(stage_buffer.pixels);
478
479         return ret;
480 }
481
482 /**
483  * vkms_composer_worker - ordered work_struct to compute CRC
484  *
485  * @work: work_struct
486  *
487  * Work handler for composing and computing CRCs. work_struct scheduled in
488  * an ordered workqueue that's periodically scheduled to run by
489  * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
490  */
491 void vkms_composer_worker(struct work_struct *work)
492 {
493         struct vkms_crtc_state *crtc_state = container_of(work,
494                                                           struct vkms_crtc_state,
495                                                           composer_work);
496         struct drm_crtc *crtc = crtc_state->base.crtc;
497         struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
498         struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
499         bool crc_pending, wb_pending;
500         u64 frame_start, frame_end;
501         u32 crc32 = 0;
502         int ret;
503
504         spin_lock_irq(&out->composer_lock);
505         frame_start = crtc_state->frame_start;
506         frame_end = crtc_state->frame_end;
507         crc_pending = crtc_state->crc_pending;
508         wb_pending = crtc_state->wb_pending;
509         crtc_state->frame_start = 0;
510         crtc_state->frame_end = 0;
511         crtc_state->crc_pending = false;
512
513         if (crtc->state->gamma_lut) {
514                 s64 max_lut_index_fp;
515                 s64 u16_max_fp = drm_int2fixp(0xffff);
516
517                 crtc_state->gamma_lut.base = (struct drm_color_lut *)crtc->state->gamma_lut->data;
518                 crtc_state->gamma_lut.lut_length =
519                         crtc->state->gamma_lut->length / sizeof(struct drm_color_lut);
520                 max_lut_index_fp = drm_int2fixp(crtc_state->gamma_lut.lut_length - 1);
521                 crtc_state->gamma_lut.channel_value2index_ratio = drm_fixp_div(max_lut_index_fp,
522                                                                                u16_max_fp);
523
524         } else {
525                 crtc_state->gamma_lut.base = NULL;
526         }
527
528         spin_unlock_irq(&out->composer_lock);
529
530         /*
531          * We raced with the vblank hrtimer and previous work already computed
532          * the crc, nothing to do.
533          */
534         if (!crc_pending)
535                 return;
536
537         if (wb_pending)
538                 ret = compose_active_planes(active_wb, crtc_state, &crc32);
539         else
540                 ret = compose_active_planes(NULL, crtc_state, &crc32);
541
542         if (ret)
543                 return;
544
545         if (wb_pending) {
546                 drm_writeback_signal_completion(&out->wb_connector, 0);
547                 spin_lock_irq(&out->composer_lock);
548                 crtc_state->wb_pending = false;
549                 spin_unlock_irq(&out->composer_lock);
550         }
551
552         /*
553          * The worker can fall behind the vblank hrtimer, make sure we catch up.
554          */
555         while (frame_start <= frame_end)
556                 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
557 }
558
559 static const char *const pipe_crc_sources[] = { "auto" };
560
561 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
562                                         size_t *count)
563 {
564         *count = ARRAY_SIZE(pipe_crc_sources);
565         return pipe_crc_sources;
566 }
567
568 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
569 {
570         int ret = 0;
571
572         if (!src_name) {
573                 *enabled = false;
574         } else if (strcmp(src_name, "auto") == 0) {
575                 *enabled = true;
576         } else {
577                 *enabled = false;
578                 ret = -EINVAL;
579         }
580
581         return ret;
582 }
583
584 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
585                            size_t *values_cnt)
586 {
587         bool enabled;
588
589         if (vkms_crc_parse_source(src_name, &enabled) < 0) {
590                 DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
591                 return -EINVAL;
592         }
593
594         *values_cnt = 1;
595
596         return 0;
597 }
598
599 void vkms_set_composer(struct vkms_output *out, bool enabled)
600 {
601         bool old_enabled;
602
603         if (enabled)
604                 drm_crtc_vblank_get(&out->crtc);
605
606         spin_lock_irq(&out->lock);
607         old_enabled = out->composer_enabled;
608         out->composer_enabled = enabled;
609         spin_unlock_irq(&out->lock);
610
611         if (old_enabled)
612                 drm_crtc_vblank_put(&out->crtc);
613 }
614
615 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
616 {
617         struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
618         bool enabled = false;
619         int ret = 0;
620
621         ret = vkms_crc_parse_source(src_name, &enabled);
622
623         vkms_set_composer(out, enabled);
624
625         return ret;
626 }
This page took 0.068309 seconds and 4 git commands to generate.