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