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_fourcc.h>
8 #include <drm/drm_gem_framebuffer_helper.h>
9 #include <drm/drm_vblank.h>
10 #include <linux/minmax.h>
14 static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
18 new_color = (src * 0xffff + dst * (0xffff - alpha));
20 return DIV_ROUND_CLOSEST(new_color, 0xffff);
24 * pre_mul_alpha_blend - alpha blending equation
25 * @src_frame_info: source framebuffer's metadata
26 * @stage_buffer: The line with the pixels from src_plane
27 * @output_buffer: A line buffer that receives all the blends output
29 * Using the information from the `frame_info`, this blends only the
30 * necessary pixels from the `stage_buffer` to the `output_buffer`
31 * using premultiplied blend formula.
33 * The current DRM assumption is that pixel color values have been already
34 * pre-multiplied with the alpha channel values. See more
35 * drm_plane_create_blend_mode_property(). Also, this formula assumes a
36 * completely opaque background.
38 static void pre_mul_alpha_blend(struct vkms_frame_info *frame_info,
39 struct line_buffer *stage_buffer,
40 struct line_buffer *output_buffer)
42 int x_dst = frame_info->dst.x1;
43 struct pixel_argb_u16 *out = output_buffer->pixels + x_dst;
44 struct pixel_argb_u16 *in = stage_buffer->pixels;
45 int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
46 stage_buffer->n_pixels);
48 for (int x = 0; x < x_limit; x++) {
49 out[x].a = (u16)0xffff;
50 out[x].r = pre_mul_blend_channel(in[x].r, out[x].r, in[x].a);
51 out[x].g = pre_mul_blend_channel(in[x].g, out[x].g, in[x].a);
52 out[x].b = pre_mul_blend_channel(in[x].b, out[x].b, in[x].a);
56 static int get_y_pos(struct vkms_frame_info *frame_info, int y)
58 if (frame_info->rotation & DRM_MODE_REFLECT_Y)
59 return drm_rect_height(&frame_info->rotated) - y - 1;
61 switch (frame_info->rotation & DRM_MODE_ROTATE_MASK) {
62 case DRM_MODE_ROTATE_90:
63 return frame_info->rotated.x2 - y - 1;
69 static bool check_limit(struct vkms_frame_info *frame_info, int pos)
71 if (frame_info->rotation & DRM_MODE_ROTATE_90) {
72 if (pos >= 0 && pos < drm_rect_width(&frame_info->rotated))
75 if (pos >= frame_info->rotated.y1 && pos < frame_info->rotated.y2)
82 static void fill_background(const struct pixel_argb_u16 *background_color,
83 struct line_buffer *output_buffer)
85 for (size_t i = 0; i < output_buffer->n_pixels; i++)
86 output_buffer->pixels[i] = *background_color;
90 * @wb_frame_info: The writeback frame buffer metadata
91 * @crtc_state: The crtc state
92 * @crc32: The crc output of the final frame
93 * @output_buffer: A buffer of a row that will receive the result of the blend(s)
94 * @stage_buffer: The line with the pixels from plane being blend to the output
96 * This function blends the pixels (Using the `pre_mul_alpha_blend`)
97 * from all planes, calculates the crc32 of the output from the former step,
98 * and, if necessary, convert and store the output to the writeback buffer.
100 static void blend(struct vkms_writeback_job *wb,
101 struct vkms_crtc_state *crtc_state,
102 u32 *crc32, struct line_buffer *stage_buffer,
103 struct line_buffer *output_buffer, size_t row_size)
105 struct vkms_plane_state **plane = crtc_state->active_planes;
106 u32 n_active_planes = crtc_state->num_active_planes;
109 const struct pixel_argb_u16 background_color = { .a = 0xffff };
111 size_t crtc_y_limit = crtc_state->base.crtc->mode.vdisplay;
113 for (size_t y = 0; y < crtc_y_limit; y++) {
114 fill_background(&background_color, output_buffer);
116 /* The active planes are composed associatively in z-order. */
117 for (size_t i = 0; i < n_active_planes; i++) {
118 y_pos = get_y_pos(plane[i]->frame_info, y);
120 if (!check_limit(plane[i]->frame_info, y_pos))
123 vkms_compose_row(stage_buffer, plane[i], y_pos);
124 pre_mul_alpha_blend(plane[i]->frame_info, stage_buffer,
128 *crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
131 wb->wb_write(&wb->wb_frame_info, output_buffer, y_pos);
135 static int check_format_funcs(struct vkms_crtc_state *crtc_state,
136 struct vkms_writeback_job *active_wb)
138 struct vkms_plane_state **planes = crtc_state->active_planes;
139 u32 n_active_planes = crtc_state->num_active_planes;
141 for (size_t i = 0; i < n_active_planes; i++)
142 if (!planes[i]->pixel_read)
145 if (active_wb && !active_wb->wb_write)
151 static int check_iosys_map(struct vkms_crtc_state *crtc_state)
153 struct vkms_plane_state **plane_state = crtc_state->active_planes;
154 u32 n_active_planes = crtc_state->num_active_planes;
156 for (size_t i = 0; i < n_active_planes; i++)
157 if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
163 static int compose_active_planes(struct vkms_writeback_job *active_wb,
164 struct vkms_crtc_state *crtc_state,
167 size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
168 struct line_buffer output_buffer, stage_buffer;
172 * This check exists so we can call `crc32_le` for the entire line
173 * instead doing it for each channel of each pixel in case
174 * `struct `pixel_argb_u16` had any gap added by the compiler
175 * between the struct fields.
177 static_assert(sizeof(struct pixel_argb_u16) == 8);
179 if (WARN_ON(check_iosys_map(crtc_state)))
182 if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
185 line_width = crtc_state->base.crtc->mode.hdisplay;
186 stage_buffer.n_pixels = line_width;
187 output_buffer.n_pixels = line_width;
189 stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
190 if (!stage_buffer.pixels) {
191 DRM_ERROR("Cannot allocate memory for the output line buffer");
195 output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
196 if (!output_buffer.pixels) {
197 DRM_ERROR("Cannot allocate memory for intermediate line buffer");
199 goto free_stage_buffer;
202 blend(active_wb, crtc_state, crc32, &stage_buffer,
203 &output_buffer, line_width * pixel_size);
205 kvfree(output_buffer.pixels);
207 kvfree(stage_buffer.pixels);
213 * vkms_composer_worker - ordered work_struct to compute CRC
217 * Work handler for composing and computing CRCs. work_struct scheduled in
218 * an ordered workqueue that's periodically scheduled to run by
219 * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
221 void vkms_composer_worker(struct work_struct *work)
223 struct vkms_crtc_state *crtc_state = container_of(work,
224 struct vkms_crtc_state,
226 struct drm_crtc *crtc = crtc_state->base.crtc;
227 struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
228 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
229 bool crc_pending, wb_pending;
230 u64 frame_start, frame_end;
234 spin_lock_irq(&out->composer_lock);
235 frame_start = crtc_state->frame_start;
236 frame_end = crtc_state->frame_end;
237 crc_pending = crtc_state->crc_pending;
238 wb_pending = crtc_state->wb_pending;
239 crtc_state->frame_start = 0;
240 crtc_state->frame_end = 0;
241 crtc_state->crc_pending = false;
242 spin_unlock_irq(&out->composer_lock);
245 * We raced with the vblank hrtimer and previous work already computed
246 * the crc, nothing to do.
252 ret = compose_active_planes(active_wb, crtc_state, &crc32);
254 ret = compose_active_planes(NULL, crtc_state, &crc32);
260 drm_writeback_signal_completion(&out->wb_connector, 0);
261 spin_lock_irq(&out->composer_lock);
262 crtc_state->wb_pending = false;
263 spin_unlock_irq(&out->composer_lock);
267 * The worker can fall behind the vblank hrtimer, make sure we catch up.
269 while (frame_start <= frame_end)
270 drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
273 static const char * const pipe_crc_sources[] = {"auto"};
275 const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
278 *count = ARRAY_SIZE(pipe_crc_sources);
279 return pipe_crc_sources;
282 static int vkms_crc_parse_source(const char *src_name, bool *enabled)
288 } else if (strcmp(src_name, "auto") == 0) {
298 int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
303 if (vkms_crc_parse_source(src_name, &enabled) < 0) {
304 DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
313 void vkms_set_composer(struct vkms_output *out, bool enabled)
318 drm_crtc_vblank_get(&out->crtc);
320 spin_lock_irq(&out->lock);
321 old_enabled = out->composer_enabled;
322 out->composer_enabled = enabled;
323 spin_unlock_irq(&out->lock);
326 drm_crtc_vblank_put(&out->crtc);
329 int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
331 struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
332 bool enabled = false;
335 ret = vkms_crc_parse_source(src_name, &enabled);
337 vkms_set_composer(out, enabled);