1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/crc32.h>
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_gem_framebuffer_helper.h>
10 #include <drm/drm_vblank.h>
11 #include <linux/minmax.h>
15 static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
19 new_color = (src * 0xffff + dst * (0xffff - alpha));
21 return DIV_ROUND_CLOSEST(new_color, 0xffff);
25 * pre_mul_alpha_blend - alpha blending equation
26 * @src_frame_info: source framebuffer's metadata
27 * @stage_buffer: The line with the pixels from src_plane
28 * @output_buffer: A line buffer that receives all the blends output
30 * Using the information from the `frame_info`, this blends only the
31 * necessary pixels from the `stage_buffer` to the `output_buffer`
32 * using premultiplied blend formula.
34 * The current DRM assumption is that pixel color values have been already
35 * pre-multiplied with the alpha channel values. See more
36 * drm_plane_create_blend_mode_property(). Also, this formula assumes a
37 * completely opaque background.
39 static void pre_mul_alpha_blend(struct vkms_frame_info *frame_info,
40 struct line_buffer *stage_buffer,
41 struct line_buffer *output_buffer)
43 int x_dst = frame_info->dst.x1;
44 struct pixel_argb_u16 *out = output_buffer->pixels + x_dst;
45 struct pixel_argb_u16 *in = stage_buffer->pixels;
46 int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
47 stage_buffer->n_pixels);
49 for (int x = 0; x < x_limit; x++) {
50 out[x].a = (u16)0xffff;
51 out[x].r = pre_mul_blend_channel(in[x].r, out[x].r, in[x].a);
52 out[x].g = pre_mul_blend_channel(in[x].g, out[x].g, in[x].a);
53 out[x].b = pre_mul_blend_channel(in[x].b, out[x].b, in[x].a);
57 static int get_y_pos(struct vkms_frame_info *frame_info, int y)
59 if (frame_info->rotation & DRM_MODE_REFLECT_Y)
60 return drm_rect_height(&frame_info->rotated) - y - 1;
62 switch (frame_info->rotation & DRM_MODE_ROTATE_MASK) {
63 case DRM_MODE_ROTATE_90:
64 return frame_info->rotated.x2 - y - 1;
65 case DRM_MODE_ROTATE_270:
66 return y + frame_info->rotated.x1;
72 static bool check_limit(struct vkms_frame_info *frame_info, int pos)
74 if (drm_rotation_90_or_270(frame_info->rotation)) {
75 if (pos >= 0 && pos < drm_rect_width(&frame_info->rotated))
78 if (pos >= frame_info->rotated.y1 && pos < frame_info->rotated.y2)
85 static void fill_background(const struct pixel_argb_u16 *background_color,
86 struct line_buffer *output_buffer)
88 for (size_t i = 0; i < output_buffer->n_pixels; i++)
89 output_buffer->pixels[i] = *background_color;
93 * @wb_frame_info: The writeback frame buffer metadata
94 * @crtc_state: The crtc state
95 * @crc32: The crc output of the final frame
96 * @output_buffer: A buffer of a row that will receive the result of the blend(s)
97 * @stage_buffer: The line with the pixels from plane being blend to the output
99 * This function blends the pixels (Using the `pre_mul_alpha_blend`)
100 * from all planes, calculates the crc32 of the output from the former step,
101 * and, if necessary, convert and store the output to the writeback buffer.
103 static void blend(struct vkms_writeback_job *wb,
104 struct vkms_crtc_state *crtc_state,
105 u32 *crc32, struct line_buffer *stage_buffer,
106 struct line_buffer *output_buffer, size_t row_size)
108 struct vkms_plane_state **plane = crtc_state->active_planes;
109 u32 n_active_planes = crtc_state->num_active_planes;
112 const struct pixel_argb_u16 background_color = { .a = 0xffff };
114 size_t crtc_y_limit = crtc_state->base.crtc->mode.vdisplay;
116 for (size_t y = 0; y < crtc_y_limit; y++) {
117 fill_background(&background_color, output_buffer);
119 /* The active planes are composed associatively in z-order. */
120 for (size_t i = 0; i < n_active_planes; i++) {
121 y_pos = get_y_pos(plane[i]->frame_info, y);
123 if (!check_limit(plane[i]->frame_info, y_pos))
126 vkms_compose_row(stage_buffer, plane[i], y_pos);
127 pre_mul_alpha_blend(plane[i]->frame_info, stage_buffer,
131 *crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
134 wb->wb_write(&wb->wb_frame_info, output_buffer, y_pos);
138 static int check_format_funcs(struct vkms_crtc_state *crtc_state,
139 struct vkms_writeback_job *active_wb)
141 struct vkms_plane_state **planes = crtc_state->active_planes;
142 u32 n_active_planes = crtc_state->num_active_planes;
144 for (size_t i = 0; i < n_active_planes; i++)
145 if (!planes[i]->pixel_read)
148 if (active_wb && !active_wb->wb_write)
154 static int check_iosys_map(struct vkms_crtc_state *crtc_state)
156 struct vkms_plane_state **plane_state = crtc_state->active_planes;
157 u32 n_active_planes = crtc_state->num_active_planes;
159 for (size_t i = 0; i < n_active_planes; i++)
160 if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
166 static int compose_active_planes(struct vkms_writeback_job *active_wb,
167 struct vkms_crtc_state *crtc_state,
170 size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
171 struct line_buffer output_buffer, stage_buffer;
175 * This check exists so we can call `crc32_le` for the entire line
176 * instead doing it for each channel of each pixel in case
177 * `struct `pixel_argb_u16` had any gap added by the compiler
178 * between the struct fields.
180 static_assert(sizeof(struct pixel_argb_u16) == 8);
182 if (WARN_ON(check_iosys_map(crtc_state)))
185 if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
188 line_width = crtc_state->base.crtc->mode.hdisplay;
189 stage_buffer.n_pixels = line_width;
190 output_buffer.n_pixels = line_width;
192 stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
193 if (!stage_buffer.pixels) {
194 DRM_ERROR("Cannot allocate memory for the output line buffer");
198 output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
199 if (!output_buffer.pixels) {
200 DRM_ERROR("Cannot allocate memory for intermediate line buffer");
202 goto free_stage_buffer;
205 blend(active_wb, crtc_state, crc32, &stage_buffer,
206 &output_buffer, line_width * pixel_size);
208 kvfree(output_buffer.pixels);
210 kvfree(stage_buffer.pixels);
216 * vkms_composer_worker - ordered work_struct to compute CRC
220 * Work handler for composing and computing CRCs. work_struct scheduled in
221 * an ordered workqueue that's periodically scheduled to run by
222 * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
224 void vkms_composer_worker(struct work_struct *work)
226 struct vkms_crtc_state *crtc_state = container_of(work,
227 struct vkms_crtc_state,
229 struct drm_crtc *crtc = crtc_state->base.crtc;
230 struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
231 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
232 bool crc_pending, wb_pending;
233 u64 frame_start, frame_end;
237 spin_lock_irq(&out->composer_lock);
238 frame_start = crtc_state->frame_start;
239 frame_end = crtc_state->frame_end;
240 crc_pending = crtc_state->crc_pending;
241 wb_pending = crtc_state->wb_pending;
242 crtc_state->frame_start = 0;
243 crtc_state->frame_end = 0;
244 crtc_state->crc_pending = false;
245 spin_unlock_irq(&out->composer_lock);
248 * We raced with the vblank hrtimer and previous work already computed
249 * the crc, nothing to do.
255 ret = compose_active_planes(active_wb, crtc_state, &crc32);
257 ret = compose_active_planes(NULL, crtc_state, &crc32);
263 drm_writeback_signal_completion(&out->wb_connector, 0);
264 spin_lock_irq(&out->composer_lock);
265 crtc_state->wb_pending = false;
266 spin_unlock_irq(&out->composer_lock);
270 * The worker can fall behind the vblank hrtimer, make sure we catch up.
272 while (frame_start <= frame_end)
273 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
276 static const char * const pipe_crc_sources[] = {"auto"};
278 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
281 *count = ARRAY_SIZE(pipe_crc_sources);
282 return pipe_crc_sources;
285 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
291 } else if (strcmp(src_name, "auto") == 0) {
301 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
306 if (vkms_crc_parse_source(src_name, &enabled) < 0) {
307 DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
316 void vkms_set_composer(struct vkms_output *out, bool enabled)
321 drm_crtc_vblank_get(&out->crtc);
323 spin_lock_irq(&out->lock);
324 old_enabled = out->composer_enabled;
325 out->composer_enabled = enabled;
326 spin_unlock_irq(&out->lock);
329 drm_crtc_vblank_put(&out->crtc);
332 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
334 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
335 bool enabled = false;
338 ret = vkms_crc_parse_source(src_name, &enabled);
340 vkms_set_composer(out, enabled);