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 20 static 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 */ 44 static 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 59 static 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 67 VISIBLE_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 } 76 EXPORT_SYMBOL_IF_KUNIT(lerp_u16); 77 78 VISIBLE_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 } 84 EXPORT_SYMBOL_IF_KUNIT(get_lut_index); 85 86 VISIBLE_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 } 112 EXPORT_SYMBOL_IF_KUNIT(apply_lut_to_channel_value); 113 114 115 static 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 132 VISIBLE_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 } 161 EXPORT_SYMBOL_IF_KUNIT(apply_3x4_matrix); 162 163 static 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 193 static 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 */ 248 static 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 */ 300 static 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 */ 363 static 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 */ 471 static 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 479 const struct pixel_argb_u16 background_color = { .a = 0xffff }; 480 481 int crtc_y_limit = crtc_state->base.mode.vdisplay; 482 int crtc_x_limit = crtc_state->base.mode.hdisplay; 483 484 /* 485 * The planes are composed line-by-line to avoid heavy memory usage. It is a necessary 486 * complexity to avoid poor blending performance. 487 * 488 * The function pixel_read_line callback is used to read a line, using an efficient 489 * algorithm for a specific format, into the staging buffer. 490 */ 491 for (int y = 0; y < crtc_y_limit; y++) { 492 fill_background(&background_color, output_buffer); 493 494 /* The active planes are composed associatively in z-order. */ 495 for (size_t i = 0; i < n_active_planes; i++) { 496 blend_line(plane[i], y, crtc_x_limit, stage_buffer, output_buffer); 497 } 498 499 apply_lut(crtc_state, output_buffer); 500 501 *crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size); 502 503 if (wb) 504 vkms_writeback_row(wb, output_buffer, y); 505 } 506 } 507 508 static int check_format_funcs(struct vkms_crtc_state *crtc_state, 509 struct vkms_writeback_job *active_wb) 510 { 511 struct vkms_plane_state **planes = crtc_state->active_planes; 512 u32 n_active_planes = crtc_state->num_active_planes; 513 514 for (size_t i = 0; i < n_active_planes; i++) 515 if (!planes[i]->pixel_read_line) 516 return -1; 517 518 if (active_wb && !active_wb->pixel_write) 519 return -1; 520 521 return 0; 522 } 523 524 static int check_iosys_map(struct vkms_crtc_state *crtc_state) 525 { 526 struct vkms_plane_state **plane_state = crtc_state->active_planes; 527 u32 n_active_planes = crtc_state->num_active_planes; 528 529 for (size_t i = 0; i < n_active_planes; i++) 530 if (iosys_map_is_null(&plane_state[i]->frame_info->map[0])) 531 return -1; 532 533 return 0; 534 } 535 536 static int compose_active_planes(struct vkms_writeback_job *active_wb, 537 struct vkms_crtc_state *crtc_state, 538 u32 *crc32) 539 { 540 size_t line_width, pixel_size = sizeof(struct pixel_argb_u16); 541 struct line_buffer output_buffer, stage_buffer; 542 int ret = 0; 543 544 /* 545 * This check exists so we can call `crc32_le` for the entire line 546 * instead doing it for each channel of each pixel in case 547 * `struct `pixel_argb_u16` had any gap added by the compiler 548 * between the struct fields. 549 */ 550 static_assert(sizeof(struct pixel_argb_u16) == 8); 551 552 if (WARN_ON(check_iosys_map(crtc_state))) 553 return -EINVAL; 554 555 if (WARN_ON(check_format_funcs(crtc_state, active_wb))) 556 return -EINVAL; 557 558 line_width = crtc_state->base.mode.hdisplay; 559 stage_buffer.n_pixels = line_width; 560 output_buffer.n_pixels = line_width; 561 562 stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL); 563 if (!stage_buffer.pixels) { 564 DRM_ERROR("Cannot allocate memory for the output line buffer"); 565 return -ENOMEM; 566 } 567 568 output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL); 569 if (!output_buffer.pixels) { 570 DRM_ERROR("Cannot allocate memory for intermediate line buffer"); 571 ret = -ENOMEM; 572 goto free_stage_buffer; 573 } 574 575 blend(active_wb, crtc_state, crc32, &stage_buffer, 576 &output_buffer, line_width * pixel_size); 577 578 kvfree(output_buffer.pixels); 579 free_stage_buffer: 580 kvfree(stage_buffer.pixels); 581 582 return ret; 583 } 584 585 /** 586 * vkms_composer_worker - ordered work_struct to compute CRC 587 * 588 * @work: work_struct 589 * 590 * Work handler for composing and computing CRCs. work_struct scheduled in 591 * an ordered workqueue that's periodically scheduled to run by 592 * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail(). 593 */ 594 void vkms_composer_worker(struct work_struct *work) 595 { 596 struct vkms_crtc_state *crtc_state = container_of(work, 597 struct vkms_crtc_state, 598 composer_work); 599 struct drm_crtc *crtc = crtc_state->base.crtc; 600 struct vkms_writeback_job *active_wb = crtc_state->active_writeback; 601 struct vkms_output *out = drm_crtc_to_vkms_output(crtc); 602 bool crc_pending, wb_pending; 603 u64 frame_start, frame_end; 604 u32 crc32 = 0; 605 int ret; 606 607 spin_lock_irq(&out->composer_lock); 608 frame_start = crtc_state->frame_start; 609 frame_end = crtc_state->frame_end; 610 crc_pending = crtc_state->crc_pending; 611 wb_pending = crtc_state->wb_pending; 612 crtc_state->frame_start = 0; 613 crtc_state->frame_end = 0; 614 crtc_state->crc_pending = false; 615 616 if (crtc->state->gamma_lut) { 617 s64 max_lut_index_fp; 618 s64 u16_max_fp = drm_int2fixp(0xffff); 619 620 crtc_state->gamma_lut.base = (struct drm_color_lut *)crtc->state->gamma_lut->data; 621 crtc_state->gamma_lut.lut_length = 622 crtc->state->gamma_lut->length / sizeof(struct drm_color_lut); 623 max_lut_index_fp = drm_int2fixp(crtc_state->gamma_lut.lut_length - 1); 624 crtc_state->gamma_lut.channel_value2index_ratio = drm_fixp_div(max_lut_index_fp, 625 u16_max_fp); 626 627 } else { 628 crtc_state->gamma_lut.base = NULL; 629 } 630 631 spin_unlock_irq(&out->composer_lock); 632 633 /* 634 * We raced with the vblank hrtimer and previous work already computed 635 * the crc, nothing to do. 636 */ 637 if (!crc_pending) 638 return; 639 640 if (wb_pending) 641 ret = compose_active_planes(active_wb, crtc_state, &crc32); 642 else 643 ret = compose_active_planes(NULL, crtc_state, &crc32); 644 645 if (ret) 646 return; 647 648 if (wb_pending) { 649 drm_writeback_signal_completion(&out->wb_connector, 0); 650 spin_lock_irq(&out->composer_lock); 651 crtc_state->wb_pending = false; 652 spin_unlock_irq(&out->composer_lock); 653 } 654 655 /* 656 * The worker can fall behind the vblank hrtimer, make sure we catch up. 657 */ 658 while (frame_start <= frame_end) 659 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32); 660 } 661 662 static const char *const pipe_crc_sources[] = { "auto" }; 663 664 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc, 665 size_t *count) 666 { 667 *count = ARRAY_SIZE(pipe_crc_sources); 668 return pipe_crc_sources; 669 } 670 671 static int vkms_crc_parse_source(const char *src_name, bool *enabled) 672 { 673 int ret = 0; 674 675 if (!src_name) { 676 *enabled = false; 677 } else if (strcmp(src_name, "auto") == 0) { 678 *enabled = true; 679 } else { 680 *enabled = false; 681 ret = -EINVAL; 682 } 683 684 return ret; 685 } 686 687 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name, 688 size_t *values_cnt) 689 { 690 bool enabled; 691 692 if (vkms_crc_parse_source(src_name, &enabled) < 0) { 693 DRM_DEBUG_DRIVER("unknown source %s\n", src_name); 694 return -EINVAL; 695 } 696 697 *values_cnt = 1; 698 699 return 0; 700 } 701 702 void vkms_set_composer(struct vkms_output *out, bool enabled) 703 { 704 bool old_enabled; 705 706 if (enabled) 707 drm_crtc_vblank_get(&out->crtc); 708 709 spin_lock_irq(&out->lock); 710 old_enabled = out->composer_enabled; 711 out->composer_enabled = enabled; 712 spin_unlock_irq(&out->lock); 713 714 if (old_enabled) 715 drm_crtc_vblank_put(&out->crtc); 716 } 717 718 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name) 719 { 720 struct vkms_output *out = drm_crtc_to_vkms_output(crtc); 721 bool enabled = false; 722 int ret = 0; 723 724 ret = vkms_crc_parse_source(src_name, &enabled); 725 726 vkms_set_composer(out, enabled); 727 728 return ret; 729 } 730