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