/** * pre_mul_alpha_blend - alpha blending equation * @stage_buffer: The line with the pixels from src_plane * @output_buffer: A line buffer that receives all the blends output * @x_start: The start offset * @pixel_count: The number of pixels to blend * * The pixels [@x_start;@x_start+@pixel_count) in stage_buffer are blended at * [@x_start;@x_start+@pixel_count) in output_buffer. * * The current DRM assumption is that pixel color values have been already * pre-multiplied with the alpha channel values. See more * drm_plane_create_blend_mode_property(). Also, this formula assumes a * completely opaque background.
*/ staticvoid pre_mul_alpha_blend(conststruct line_buffer *stage_buffer, struct line_buffer *output_buffer, int x_start, int pixel_count)
{ struct pixel_argb_u16 *out = &output_buffer->pixels[x_start]; conststruct pixel_argb_u16 *in = &stage_buffer->pixels[x_start];
for (int i = 0; i < pixel_count; i++) {
out[i].a = (u16)0xffff;
out[i].r = pre_mul_blend_channel(in[i].r, out[i].r, in[i].a);
out[i].g = pre_mul_blend_channel(in[i].g, out[i].g, in[i].a);
out[i].b = pre_mul_blend_channel(in[i].b, out[i].b, in[i].a);
}
}
staticvoid fill_background(conststruct pixel_argb_u16 *background_color, struct line_buffer *output_buffer)
{ for (size_t i = 0; i < output_buffer->n_pixels; i++)
output_buffer->pixels[i] = *background_color;
}
// lerp(a, b, t) = a + (b - a) * t static u16 lerp_u16(u16 a, u16 b, s64 t)
{
s64 a_fp = drm_int2fixp(a);
s64 b_fp = drm_int2fixp(b);
/* * This enum is related to the positions of the variables inside * `struct drm_color_lut`, so the order of both needs to be the same.
*/ enum lut_channel {
LUT_RED = 0,
LUT_GREEN,
LUT_BLUE,
LUT_RESERVED
};
/* * This checks if `struct drm_color_lut` has any gap added by the compiler * between the struct fields.
*/
static_assert(sizeof(struct drm_color_lut) == sizeof(__u16) * 4);
floor_lut_value = (__u16 *)&lut->base[drm_fixp2int(lut_index)]; if (drm_fixp2int(lut_index) == (lut->lut_length - 1)) /* We're at the end of the LUT array, use same value for ceil and floor */
ceil_lut_value = floor_lut_value; else
ceil_lut_value = (__u16 *)&lut->base[drm_fixp2int_ceil(lut_index)];
/** * direction_for_rotation() - Get the correct reading direction for a given rotation * * @rotation: Rotation to analyze. It correspond the field @frame_info.rotation. * * This function will use the @rotation setting of a source plane to compute the reading * direction in this plane which correspond to a "left to right writing" in the CRTC. * For example, if the buffer is reflected on X axis, the pixel must be read from right to left * to be written from left to right on the CRTC.
*/ staticenum pixel_read_direction direction_for_rotation(unsignedint rotation)
{ struct drm_rect tmp_a, tmp_b; int x, y;
/* * Points A and B are depicted as zero-size rectangles on the CRTC. * The CRTC writing direction is from A to B. The plane reading direction * is discovered by inverse-transforming A and B. * The reading direction is computed by rotating the vector AB (top-left to top-right) in a * 1x1 square.
*/
if (x == 1 && y == 0) return READ_LEFT_TO_RIGHT; elseif (x == -1 && y == 0) return READ_RIGHT_TO_LEFT; elseif (y == 1 && x == 0) return READ_TOP_TO_BOTTOM; elseif (y == -1 && x == 0) return READ_BOTTOM_TO_TOP;
WARN_ONCE(true, "The inverse of the rotation gives an incorrect direction."); return READ_LEFT_TO_RIGHT;
}
/** * clamp_line_coordinates() - Compute and clamp the coordinate to read and write during the blend * process. * * @direction: direction of the reading * @current_plane: current plane blended * @src_line: source line of the reading. Only the top-left coordinate is used. This rectangle * must be rotated and have a shape of 1*pixel_count if @direction is vertical and a shape of * pixel_count*1 if @direction is horizontal. * @src_x_start: x start coordinate for the line reading * @src_y_start: y start coordinate for the line reading * @dst_x_start: x coordinate to blend the read line * @pixel_count: number of pixels to blend * * This function is mainly a safety net to avoid reading outside the source buffer. As the * userspace should never ask to read outside the source plane, all the cases covered here should * be dead code.
*/ staticvoid clamp_line_coordinates(enum pixel_read_direction direction, conststruct vkms_plane_state *current_plane, conststruct drm_rect *src_line, int *src_x_start, int *src_y_start, int *dst_x_start, int *pixel_count)
{ /* By default the start points are correct */
*src_x_start = src_line->x1;
*src_y_start = src_line->y1;
*dst_x_start = current_plane->frame_info->dst.x1;
/* Get the correct number of pixel to blend, it depends of the direction */ switch (direction) { case READ_LEFT_TO_RIGHT: case READ_RIGHT_TO_LEFT:
*pixel_count = drm_rect_width(src_line); break; case READ_BOTTOM_TO_TOP: case READ_TOP_TO_BOTTOM:
*pixel_count = drm_rect_height(src_line); break;
}
/* * Clamp the coordinates to avoid reading outside the buffer * * This is mainly a security check to avoid reading outside the buffer, the userspace * should never request to read outside the source buffer.
*/ switch (direction) { case READ_LEFT_TO_RIGHT: case READ_RIGHT_TO_LEFT: if (*src_x_start < 0) {
*pixel_count += *src_x_start;
*dst_x_start -= *src_x_start;
*src_x_start = 0;
} if (*src_x_start + *pixel_count > current_plane->frame_info->fb->width)
*pixel_count = max(0, (int)current_plane->frame_info->fb->width -
*src_x_start); break; case READ_BOTTOM_TO_TOP: case READ_TOP_TO_BOTTOM: if (*src_y_start < 0) {
*pixel_count += *src_y_start;
*dst_x_start -= *src_y_start;
*src_y_start = 0;
} if (*src_y_start + *pixel_count > current_plane->frame_info->fb->height)
*pixel_count = max(0, (int)current_plane->frame_info->fb->height -
*src_y_start); break;
}
}
/** * blend_line() - Blend a line from a plane to the output buffer * * @current_plane: current plane to work on * @y: line to write in the output buffer * @crtc_x_limit: width of the output buffer * @stage_buffer: temporary buffer to convert the pixel line from the source buffer * @output_buffer: buffer to blend the read line into.
*/ staticvoid blend_line(struct vkms_plane_state *current_plane, int y, int crtc_x_limit, struct line_buffer *stage_buffer, struct line_buffer *output_buffer)
{ int src_x_start, src_y_start, dst_x_start, pixel_count; struct drm_rect dst_line, tmp_src, src_line;
/* Avoid rendering useless lines */ if (y < current_plane->frame_info->dst.y1 ||
y >= current_plane->frame_info->dst.y2) return;
/* * dst_line is the line to copy. The initial coordinates are inside the * destination framebuffer, and then drm_rect_* helpers are used to * compute the correct position into the source framebuffer.
*/
dst_line = DRM_RECT_INIT(current_plane->frame_info->dst.x1, y,
drm_rect_width(¤t_plane->frame_info->dst),
1);
/* * [1]: Clamping src_line to the crtc_x_limit to avoid writing outside of * the destination buffer
*/
dst_line.x1 = max_t(int, dst_line.x1, 0);
dst_line.x2 = min_t(int, dst_line.x2, crtc_x_limit); /* The destination is completely outside of the crtc. */ if (dst_line.x2 <= dst_line.x1) return;
src_line = dst_line;
/* * Transform the coordinate x/y from the crtc to coordinates into * coordinates for the src buffer. * * - Cancel the offset of the dst buffer. * - Invert the rotation. This assumes that * dst = drm_rect_rotate(src, rotation) (dst and src have the * same size, but can be rotated). * - Apply the offset of the source rectangle to the coordinate.
*/
drm_rect_translate(&src_line, -current_plane->frame_info->dst.x1,
-current_plane->frame_info->dst.y1);
drm_rect_rotate_inv(&src_line, drm_rect_width(&tmp_src),
drm_rect_height(&tmp_src),
current_plane->frame_info->rotation);
drm_rect_translate(&src_line, tmp_src.x1, tmp_src.y1);
/* Get the correct reading direction in the source buffer. */
enum pixel_read_direction direction =
direction_for_rotation(current_plane->frame_info->rotation);
/* [2]: Compute and clamp the number of pixel to read */
clamp_line_coordinates(direction, current_plane, &src_line, &src_x_start, &src_y_start,
&dst_x_start, &pixel_count);
if (pixel_count <= 0) { /* Nothing to read, so avoid multiple function calls */ return;
}
/* * Modify the starting point to take in account the rotation * * src_line is the top-left corner, so when reading READ_RIGHT_TO_LEFT or * READ_BOTTOM_TO_TOP, it must be changed to the top-right/bottom-left * corner.
*/ if (direction == READ_RIGHT_TO_LEFT) { // src_x_start is now the right point
src_x_start += pixel_count - 1;
} elseif (direction == READ_BOTTOM_TO_TOP) { // src_y_start is now the bottom point
src_y_start += pixel_count - 1;
}
/* * Perform the conversion and the blending * * Here we know that the read line (x_start, y_start, pixel_count) is * inside the source buffer [2] and we don't write outside the stage * buffer [1].
*/
current_plane->pixel_read_line(current_plane, src_x_start, src_y_start, direction,
pixel_count, &stage_buffer->pixels[dst_x_start]);
/** * blend - blend the pixels from all planes and compute crc * @wb: The writeback frame buffer metadata * @crtc_state: The crtc state * @crc32: The crc output of the final frame * @output_buffer: A buffer of a row that will receive the result of the blend(s) * @stage_buffer: The line with the pixels from plane being blend to the output * @row_size: The size, in bytes, of a single row * * This function blends the pixels (Using the `pre_mul_alpha_blend`) * from all planes, calculates the crc32 of the output from the former step, * and, if necessary, convert and store the output to the writeback buffer.
*/ staticvoid 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)
{ struct vkms_plane_state **plane = crtc_state->active_planes;
u32 n_active_planes = crtc_state->num_active_planes;
int crtc_y_limit = crtc_state->base.mode.vdisplay; int crtc_x_limit = crtc_state->base.mode.hdisplay;
/* * The planes are composed line-by-line to avoid heavy memory usage. It is a necessary * complexity to avoid poor blending performance. * * The function pixel_read_line callback is used to read a line, using an efficient * algorithm for a specific format, into the staging buffer.
*/ for (int y = 0; y < crtc_y_limit; y++) {
fill_background(&background_color, output_buffer);
/* The active planes are composed associatively in z-order. */ for (size_t i = 0; i < n_active_planes; i++) {
blend_line(plane[i], y, crtc_x_limit, stage_buffer, output_buffer);
}
/* * This check exists so we can call `crc32_le` for the entire line * instead doing it for each channel of each pixel in case * `struct `pixel_argb_u16` had any gap added by the compiler * between the struct fields.
*/
static_assert(sizeof(struct pixel_argb_u16) == 8);
if (WARN_ON(check_iosys_map(crtc_state))) return -EINVAL;
if (WARN_ON(check_format_funcs(crtc_state, active_wb))) return -EINVAL;
stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL); if (!stage_buffer.pixels) {
DRM_ERROR("Cannot allocate memory for the output line buffer"); return -ENOMEM;
}
output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL); if (!output_buffer.pixels) {
DRM_ERROR("Cannot allocate memory for intermediate line buffer");
ret = -ENOMEM; goto free_stage_buffer;
}
/* * The worker can fall behind the vblank hrtimer, make sure we catch up.
*/ while (frame_start <= frame_end)
drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.