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