1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/crc32.h>
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>
16 static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
20 new_color = (src * 0xffff + dst * (0xffff - alpha));
22 return DIV_ROUND_CLOSEST(new_color, 0xffff);
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
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.
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.
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)
43 struct pixel_argb_u16 *out = &output_buffer->pixels[x_start];
44 const struct pixel_argb_u16 *in = &stage_buffer->pixels[x_start];
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);
55 static void fill_background(const struct pixel_argb_u16 *background_color,
56 struct line_buffer *output_buffer)
58 for (size_t i = 0; i < output_buffer->n_pixels; i++)
59 output_buffer->pixels[i] = *background_color;
62 // lerp(a, b, t) = a + (b - a) * t
63 static u16 lerp_u16(u16 a, u16 b, s64 t)
65 s64 a_fp = drm_int2fixp(a);
66 s64 b_fp = drm_int2fixp(b);
68 s64 delta = drm_fixp_mul(b_fp - a_fp, t);
70 return drm_fixp2int(a_fp + delta);
73 static s64 get_lut_index(const struct vkms_color_lut *lut, u16 channel_value)
75 s64 color_channel_fp = drm_int2fixp(channel_value);
77 return drm_fixp_mul(color_channel_fp, lut->channel_value2index_ratio);
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.
91 static u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_value,
92 enum lut_channel channel)
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;
99 * This checks if `struct drm_color_lut` has any gap added by the compiler
100 * between the struct fields.
102 static_assert(sizeof(struct drm_color_lut) == sizeof(__u16) * 4);
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;
109 ceil_lut_value = (__u16 *)&lut->base[drm_fixp2int_ceil(lut_index)];
111 floor_channel_value = floor_lut_value[channel];
112 ceil_channel_value = ceil_lut_value[channel];
114 return lerp_u16(floor_channel_value, ceil_channel_value,
115 lut_index & DRM_FIXED_DECIMAL_MASK);
118 static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
120 if (!crtc_state->gamma_lut.base)
123 if (!crtc_state->gamma_lut.lut_length)
126 for (size_t x = 0; x < output_buffer->n_pixels; x++) {
127 struct pixel_argb_u16 *pixel = &output_buffer->pixels[x];
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);
136 * direction_for_rotation() - Get the correct reading direction for a given rotation
138 * @rotation: Rotation to analyze. It correspond the field @frame_info.rotation.
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.
145 static enum pixel_read_direction direction_for_rotation(unsigned int rotation)
147 struct drm_rect tmp_a, tmp_b;
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
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);
163 x = tmp_b.x1 - tmp_a.x1;
164 y = tmp_b.y1 - tmp_a.y1;
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;
175 WARN_ONCE(true, "The inverse of the rotation gives an incorrect direction.");
176 return READ_LEFT_TO_RIGHT;
180 * clamp_line_coordinates() - Compute and clamp the coordinate to read and write during the blend
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
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
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)
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;
207 /* Get the correct number of pixel to blend, it depends of the direction */
209 case READ_LEFT_TO_RIGHT:
210 case READ_RIGHT_TO_LEFT:
211 *pixel_count = drm_rect_width(src_line);
213 case READ_BOTTOM_TO_TOP:
214 case READ_TOP_TO_BOTTOM:
215 *pixel_count = drm_rect_height(src_line);
220 * Clamp the coordinates to avoid reading outside the buffer
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.
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;
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 -
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;
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 -
252 * blend_line() - Blend a line from a plane to the output buffer
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.
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)
264 int src_x_start, src_y_start, dst_x_start, pixel_count;
265 struct drm_rect dst_line, tmp_src, src_line;
267 /* Avoid rendering useless lines */
268 if (y < current_plane->frame_info->dst.y1 ||
269 y >= current_plane->frame_info->dst.y2)
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.
277 dst_line = DRM_RECT_INIT(current_plane->frame_info->dst.x1, y,
278 drm_rect_width(¤t_plane->frame_info->dst),
281 drm_rect_fp_to_int(&tmp_src, ¤t_plane->frame_info->src);
284 * [1]: Clamping src_line to the crtc_x_limit to avoid writing outside of
285 * the destination buffer
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)
296 * Transform the coordinate x/y from the crtc to coordinates into
297 * coordinates for the src buffer.
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.
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);
312 /* Get the correct reading direction in the source buffer. */
314 enum pixel_read_direction direction =
315 direction_for_rotation(current_plane->frame_info->rotation);
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);
321 if (pixel_count <= 0) {
322 /* Nothing to read, so avoid multiple function calls */
327 * Modify the starting point to take in account the rotation
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
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;
342 * Perform the conversion and the blending
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
348 current_plane->pixel_read_line(current_plane, src_x_start, src_y_start, direction,
349 pixel_count, &stage_buffer->pixels[dst_x_start]);
351 pre_mul_alpha_blend(stage_buffer, output_buffer,
352 dst_x_start, pixel_count);
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
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.
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)
373 struct vkms_plane_state **plane = crtc_state->active_planes;
374 u32 n_active_planes = crtc_state->num_active_planes;
376 const struct pixel_argb_u16 background_color = { .a = 0xffff };
378 int crtc_y_limit = crtc_state->base.mode.vdisplay;
379 int crtc_x_limit = crtc_state->base.mode.hdisplay;
382 * The planes are composed line-by-line to avoid heavy memory usage. It is a necessary
383 * complexity to avoid poor blending performance.
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.
388 for (int y = 0; y < crtc_y_limit; y++) {
389 fill_background(&background_color, output_buffer);
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);
396 apply_lut(crtc_state, output_buffer);
398 *crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
401 vkms_writeback_row(wb, output_buffer, y);
405 static int check_format_funcs(struct vkms_crtc_state *crtc_state,
406 struct vkms_writeback_job *active_wb)
408 struct vkms_plane_state **planes = crtc_state->active_planes;
409 u32 n_active_planes = crtc_state->num_active_planes;
411 for (size_t i = 0; i < n_active_planes; i++)
412 if (!planes[i]->pixel_read_line)
415 if (active_wb && !active_wb->pixel_write)
421 static int check_iosys_map(struct vkms_crtc_state *crtc_state)
423 struct vkms_plane_state **plane_state = crtc_state->active_planes;
424 u32 n_active_planes = crtc_state->num_active_planes;
426 for (size_t i = 0; i < n_active_planes; i++)
427 if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
433 static int compose_active_planes(struct vkms_writeback_job *active_wb,
434 struct vkms_crtc_state *crtc_state,
437 size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
438 struct line_buffer output_buffer, stage_buffer;
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.
447 static_assert(sizeof(struct pixel_argb_u16) == 8);
449 if (WARN_ON(check_iosys_map(crtc_state)))
452 if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
455 line_width = crtc_state->base.mode.hdisplay;
456 stage_buffer.n_pixels = line_width;
457 output_buffer.n_pixels = line_width;
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");
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");
469 goto free_stage_buffer;
472 blend(active_wb, crtc_state, crc32, &stage_buffer,
473 &output_buffer, line_width * pixel_size);
475 kvfree(output_buffer.pixels);
477 kvfree(stage_buffer.pixels);
483 * vkms_composer_worker - ordered work_struct to compute CRC
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().
491 void vkms_composer_worker(struct work_struct *work)
493 struct vkms_crtc_state *crtc_state = container_of(work,
494 struct vkms_crtc_state,
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;
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;
513 if (crtc->state->gamma_lut) {
514 s64 max_lut_index_fp;
515 s64 u16_max_fp = drm_int2fixp(0xffff);
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,
525 crtc_state->gamma_lut.base = NULL;
528 spin_unlock_irq(&out->composer_lock);
531 * We raced with the vblank hrtimer and previous work already computed
532 * the crc, nothing to do.
538 ret = compose_active_planes(active_wb, crtc_state, &crc32);
540 ret = compose_active_planes(NULL, crtc_state, &crc32);
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);
553 * The worker can fall behind the vblank hrtimer, make sure we catch up.
555 while (frame_start <= frame_end)
556 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
559 static const char *const pipe_crc_sources[] = { "auto" };
561 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
564 *count = ARRAY_SIZE(pipe_crc_sources);
565 return pipe_crc_sources;
568 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
574 } else if (strcmp(src_name, "auto") == 0) {
584 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
589 if (vkms_crc_parse_source(src_name, &enabled) < 0) {
590 DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
599 void vkms_set_composer(struct vkms_output *out, bool enabled)
604 drm_crtc_vblank_get(&out->crtc);
606 spin_lock_irq(&out->lock);
607 old_enabled = out->composer_enabled;
608 out->composer_enabled = enabled;
609 spin_unlock_irq(&out->lock);
612 drm_crtc_vblank_put(&out->crtc);
615 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
617 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
618 bool enabled = false;
621 ret = vkms_crc_parse_source(src_name, &enabled);
623 vkms_set_composer(out, enabled);