xref: /openbmc/linux/drivers/gpu/drm/vkms/vkms_composer.c (revision 8ba1648567e289c90fa4f65b4204d0f160e22ac3)
1a4e7e98eSRodrigo Siqueira // SPDX-License-Identifier: GPL-2.0+
2a4e7e98eSRodrigo Siqueira 
3a4e7e98eSRodrigo Siqueira #include <linux/crc32.h>
4ce672a1bSSam Ravnborg 
5a4e7e98eSRodrigo Siqueira #include <drm/drm_atomic.h>
6a4e7e98eSRodrigo Siqueira #include <drm/drm_atomic_helper.h>
732a1648aSMelissa Wen #include <drm/drm_fourcc.h>
8a4e7e98eSRodrigo Siqueira #include <drm/drm_gem_framebuffer_helper.h>
9ce672a1bSSam Ravnborg #include <drm/drm_vblank.h>
10*8ba16485SIgor Torrente #include <linux/minmax.h>
11ce672a1bSSam Ravnborg 
12ce672a1bSSam Ravnborg #include "vkms_drv.h"
13a4e7e98eSRodrigo Siqueira 
14*8ba16485SIgor Torrente static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
1560cc2021SRodrigo Siqueira {
16*8ba16485SIgor Torrente 	u32 new_color;
1760cc2021SRodrigo Siqueira 
18*8ba16485SIgor Torrente 	new_color = (src * 0xffff + dst * (0xffff - alpha));
1960cc2021SRodrigo Siqueira 
20*8ba16485SIgor Torrente 	return DIV_ROUND_CLOSEST(new_color, 0xffff);
2160cc2021SRodrigo Siqueira }
2260cc2021SRodrigo Siqueira 
23a4e7e98eSRodrigo Siqueira /**
24*8ba16485SIgor Torrente  * pre_mul_alpha_blend - alpha blending equation
251645e7b9SIgor Torrente  * @src_frame_info: source framebuffer's metadata
26*8ba16485SIgor Torrente  * @stage_buffer: The line with the pixels from src_plane
27*8ba16485SIgor Torrente  * @output_buffer: A line buffer that receives all the blends output
28a4e7e98eSRodrigo Siqueira  *
29*8ba16485SIgor Torrente  * Using the information from the `frame_info`, this blends only the
30*8ba16485SIgor Torrente  * necessary pixels from the `stage_buffer` to the `output_buffer`
31*8ba16485SIgor Torrente  * using premultiplied blend formula.
3232a1648aSMelissa Wen  *
33*8ba16485SIgor Torrente  * The current DRM assumption is that pixel color values have been already
34*8ba16485SIgor Torrente  * pre-multiplied with the alpha channel values. See more
35*8ba16485SIgor Torrente  * drm_plane_create_blend_mode_property(). Also, this formula assumes a
36*8ba16485SIgor Torrente  * completely opaque background.
37a4e7e98eSRodrigo Siqueira  */
38*8ba16485SIgor Torrente static void pre_mul_alpha_blend(struct vkms_frame_info *frame_info,
39*8ba16485SIgor Torrente 				struct line_buffer *stage_buffer,
40*8ba16485SIgor Torrente 				struct line_buffer *output_buffer)
41a4e7e98eSRodrigo Siqueira {
42*8ba16485SIgor Torrente 	int x_dst = frame_info->dst.x1;
43*8ba16485SIgor Torrente 	struct pixel_argb_u16 *out = output_buffer->pixels + x_dst;
44*8ba16485SIgor Torrente 	struct pixel_argb_u16 *in = stage_buffer->pixels;
45*8ba16485SIgor Torrente 	int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
46*8ba16485SIgor Torrente 			    stage_buffer->n_pixels);
47a4e7e98eSRodrigo Siqueira 
48*8ba16485SIgor Torrente 	for (int x = 0; x < x_limit; x++) {
49*8ba16485SIgor Torrente 		out[x].a = (u16)0xffff;
50*8ba16485SIgor Torrente 		out[x].r = pre_mul_blend_channel(in[x].r, out[x].r, in[x].a);
51*8ba16485SIgor Torrente 		out[x].g = pre_mul_blend_channel(in[x].g, out[x].g, in[x].a);
52*8ba16485SIgor Torrente 		out[x].b = pre_mul_blend_channel(in[x].b, out[x].b, in[x].a);
53a4e7e98eSRodrigo Siqueira 	}
54a4e7e98eSRodrigo Siqueira }
55a4e7e98eSRodrigo Siqueira 
56*8ba16485SIgor Torrente static bool check_y_limit(struct vkms_frame_info *frame_info, int y)
57a4e7e98eSRodrigo Siqueira {
58*8ba16485SIgor Torrente 	if (y >= frame_info->dst.y1 && y < frame_info->dst.y2)
59*8ba16485SIgor Torrente 		return true;
60a4e7e98eSRodrigo Siqueira 
61*8ba16485SIgor Torrente 	return false;
62a4e7e98eSRodrigo Siqueira }
63a4e7e98eSRodrigo Siqueira 
64*8ba16485SIgor Torrente /**
65*8ba16485SIgor Torrente  * @wb_frame_info: The writeback frame buffer metadata
66*8ba16485SIgor Torrente  * @crtc_state: The crtc state
67*8ba16485SIgor Torrente  * @crc32: The crc output of the final frame
68*8ba16485SIgor Torrente  * @output_buffer: A buffer of a row that will receive the result of the blend(s)
69*8ba16485SIgor Torrente  * @stage_buffer: The line with the pixels from plane being blend to the output
70*8ba16485SIgor Torrente  *
71*8ba16485SIgor Torrente  * This function blends the pixels (Using the `pre_mul_alpha_blend`)
72*8ba16485SIgor Torrente  * from all planes, calculates the crc32 of the output from the former step,
73*8ba16485SIgor Torrente  * and, if necessary, convert and store the output to the writeback buffer.
74*8ba16485SIgor Torrente  */
75*8ba16485SIgor Torrente static void blend(struct vkms_writeback_job *wb,
76*8ba16485SIgor Torrente 		  struct vkms_crtc_state *crtc_state,
77*8ba16485SIgor Torrente 		  u32 *crc32, struct line_buffer *stage_buffer,
78*8ba16485SIgor Torrente 		  struct line_buffer *output_buffer, size_t row_size)
79a4e7e98eSRodrigo Siqueira {
80*8ba16485SIgor Torrente 	struct vkms_plane_state **plane = crtc_state->active_planes;
81*8ba16485SIgor Torrente 	struct vkms_frame_info *primary_plane_info = plane[0]->frame_info;
82*8ba16485SIgor Torrente 	u32 n_active_planes = crtc_state->num_active_planes;
83a4e7e98eSRodrigo Siqueira 
84*8ba16485SIgor Torrente 	int y_dst = primary_plane_info->dst.y1;
85*8ba16485SIgor Torrente 	int h_dst = drm_rect_height(&primary_plane_info->dst);
86*8ba16485SIgor Torrente 	int y_limit = y_dst + h_dst;
87a4e7e98eSRodrigo Siqueira 
88*8ba16485SIgor Torrente 	for (size_t y = y_dst; y < y_limit; y++) {
89*8ba16485SIgor Torrente 		plane[0]->plane_read(output_buffer, primary_plane_info, y);
90a4e7e98eSRodrigo Siqueira 
91310e506cSMelissa Wen 		/* If there are other planes besides primary, we consider the active
92310e506cSMelissa Wen 		 * planes should be in z-order and compose them associatively:
93310e506cSMelissa Wen 		 * ((primary <- overlay) <- cursor)
94310e506cSMelissa Wen 		 */
95*8ba16485SIgor Torrente 		for (size_t i = 1; i < n_active_planes; i++) {
96*8ba16485SIgor Torrente 			if (!check_y_limit(plane[i]->frame_info, y))
97*8ba16485SIgor Torrente 				continue;
98*8ba16485SIgor Torrente 
99*8ba16485SIgor Torrente 			plane[i]->plane_read(stage_buffer, plane[i]->frame_info, y);
100*8ba16485SIgor Torrente 			pre_mul_alpha_blend(plane[i]->frame_info, stage_buffer,
101*8ba16485SIgor Torrente 					    output_buffer);
102*8ba16485SIgor Torrente 		}
103*8ba16485SIgor Torrente 
104*8ba16485SIgor Torrente 		*crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
105*8ba16485SIgor Torrente 
106*8ba16485SIgor Torrente 		if (wb)
107*8ba16485SIgor Torrente 			wb->wb_write(&wb->wb_frame_info, output_buffer, y);
108*8ba16485SIgor Torrente 	}
109*8ba16485SIgor Torrente }
110*8ba16485SIgor Torrente 
111*8ba16485SIgor Torrente static int check_format_funcs(struct vkms_crtc_state *crtc_state,
112*8ba16485SIgor Torrente 			      struct vkms_writeback_job *active_wb)
113*8ba16485SIgor Torrente {
114*8ba16485SIgor Torrente 	struct vkms_plane_state **planes = crtc_state->active_planes;
115*8ba16485SIgor Torrente 	u32 n_active_planes = crtc_state->num_active_planes;
116*8ba16485SIgor Torrente 
117*8ba16485SIgor Torrente 	for (size_t i = 0; i < n_active_planes; i++)
118*8ba16485SIgor Torrente 		if (!planes[i]->plane_read)
119*8ba16485SIgor Torrente 			return -1;
120*8ba16485SIgor Torrente 
121*8ba16485SIgor Torrente 	if (active_wb && !active_wb->wb_write)
122*8ba16485SIgor Torrente 		return -1;
123a4e7e98eSRodrigo Siqueira 
12495302576SRodrigo Siqueira 	return 0;
125a4e7e98eSRodrigo Siqueira }
126a4e7e98eSRodrigo Siqueira 
127*8ba16485SIgor Torrente static int compose_active_planes(struct vkms_writeback_job *active_wb,
128*8ba16485SIgor Torrente 				 struct vkms_crtc_state *crtc_state,
129*8ba16485SIgor Torrente 				 u32 *crc32)
130*8ba16485SIgor Torrente {
131*8ba16485SIgor Torrente 	size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
132*8ba16485SIgor Torrente 	struct vkms_frame_info *primary_plane_info = NULL;
133*8ba16485SIgor Torrente 	struct line_buffer output_buffer, stage_buffer;
134*8ba16485SIgor Torrente 	struct vkms_plane_state *act_plane = NULL;
135*8ba16485SIgor Torrente 	int ret = 0;
136*8ba16485SIgor Torrente 
137*8ba16485SIgor Torrente 	/*
138*8ba16485SIgor Torrente 	 * This check exists so we can call `crc32_le` for the entire line
139*8ba16485SIgor Torrente 	 * instead doing it for each channel of each pixel in case
140*8ba16485SIgor Torrente 	 * `struct `pixel_argb_u16` had any gap added by the compiler
141*8ba16485SIgor Torrente 	 * between the struct fields.
142*8ba16485SIgor Torrente 	 */
143*8ba16485SIgor Torrente 	static_assert(sizeof(struct pixel_argb_u16) == 8);
144*8ba16485SIgor Torrente 
145*8ba16485SIgor Torrente 	if (crtc_state->num_active_planes >= 1) {
146*8ba16485SIgor Torrente 		act_plane = crtc_state->active_planes[0];
147*8ba16485SIgor Torrente 		if (act_plane->base.base.plane->type == DRM_PLANE_TYPE_PRIMARY)
148*8ba16485SIgor Torrente 			primary_plane_info = act_plane->frame_info;
149*8ba16485SIgor Torrente 	}
150*8ba16485SIgor Torrente 
151*8ba16485SIgor Torrente 	if (!primary_plane_info)
152*8ba16485SIgor Torrente 		return -EINVAL;
153*8ba16485SIgor Torrente 
154*8ba16485SIgor Torrente 	if (WARN_ON(iosys_map_is_null(&primary_plane_info->map[0])))
155*8ba16485SIgor Torrente 		return -EINVAL;
156*8ba16485SIgor Torrente 
157*8ba16485SIgor Torrente 	if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
158*8ba16485SIgor Torrente 		return -EINVAL;
159*8ba16485SIgor Torrente 
160*8ba16485SIgor Torrente 	line_width = drm_rect_width(&primary_plane_info->dst);
161*8ba16485SIgor Torrente 	stage_buffer.n_pixels = line_width;
162*8ba16485SIgor Torrente 	output_buffer.n_pixels = line_width;
163*8ba16485SIgor Torrente 
164*8ba16485SIgor Torrente 	stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
165*8ba16485SIgor Torrente 	if (!stage_buffer.pixels) {
166*8ba16485SIgor Torrente 		DRM_ERROR("Cannot allocate memory for the output line buffer");
167*8ba16485SIgor Torrente 		return -ENOMEM;
168*8ba16485SIgor Torrente 	}
169*8ba16485SIgor Torrente 
170*8ba16485SIgor Torrente 	output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
171*8ba16485SIgor Torrente 	if (!output_buffer.pixels) {
172*8ba16485SIgor Torrente 		DRM_ERROR("Cannot allocate memory for intermediate line buffer");
173*8ba16485SIgor Torrente 		ret = -ENOMEM;
174*8ba16485SIgor Torrente 		goto free_stage_buffer;
175*8ba16485SIgor Torrente 	}
176*8ba16485SIgor Torrente 
177*8ba16485SIgor Torrente 	if (active_wb) {
178*8ba16485SIgor Torrente 		struct vkms_frame_info *wb_frame_info = &active_wb->wb_frame_info;
179*8ba16485SIgor Torrente 
180*8ba16485SIgor Torrente 		wb_frame_info->src = primary_plane_info->src;
181*8ba16485SIgor Torrente 		wb_frame_info->dst = primary_plane_info->dst;
182*8ba16485SIgor Torrente 	}
183*8ba16485SIgor Torrente 
184*8ba16485SIgor Torrente 	blend(active_wb, crtc_state, crc32, &stage_buffer,
185*8ba16485SIgor Torrente 	      &output_buffer, line_width * pixel_size);
186*8ba16485SIgor Torrente 
187*8ba16485SIgor Torrente 	kvfree(output_buffer.pixels);
188*8ba16485SIgor Torrente free_stage_buffer:
189*8ba16485SIgor Torrente 	kvfree(stage_buffer.pixels);
190*8ba16485SIgor Torrente 
191*8ba16485SIgor Torrente 	return ret;
192*8ba16485SIgor Torrente }
193*8ba16485SIgor Torrente 
194a4e7e98eSRodrigo Siqueira /**
195a4e7e98eSRodrigo Siqueira  * vkms_composer_worker - ordered work_struct to compute CRC
196a4e7e98eSRodrigo Siqueira  *
197a4e7e98eSRodrigo Siqueira  * @work: work_struct
198a4e7e98eSRodrigo Siqueira  *
199a4e7e98eSRodrigo Siqueira  * Work handler for composing and computing CRCs. work_struct scheduled in
200a4e7e98eSRodrigo Siqueira  * an ordered workqueue that's periodically scheduled to run by
201e3137249SAndré Almeida  * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
202a4e7e98eSRodrigo Siqueira  */
203a4e7e98eSRodrigo Siqueira void vkms_composer_worker(struct work_struct *work)
204a4e7e98eSRodrigo Siqueira {
205a4e7e98eSRodrigo Siqueira 	struct vkms_crtc_state *crtc_state = container_of(work,
206a4e7e98eSRodrigo Siqueira 						struct vkms_crtc_state,
207a4e7e98eSRodrigo Siqueira 						composer_work);
208a4e7e98eSRodrigo Siqueira 	struct drm_crtc *crtc = crtc_state->base.crtc;
209*8ba16485SIgor Torrente 	struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
210a4e7e98eSRodrigo Siqueira 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
211dbd9d80cSRodrigo Siqueira 	bool crc_pending, wb_pending;
212a4e7e98eSRodrigo Siqueira 	u64 frame_start, frame_end;
213*8ba16485SIgor Torrente 	u32 crc32 = 0;
21495302576SRodrigo Siqueira 	int ret;
215a4e7e98eSRodrigo Siqueira 
216a4e7e98eSRodrigo Siqueira 	spin_lock_irq(&out->composer_lock);
217a4e7e98eSRodrigo Siqueira 	frame_start = crtc_state->frame_start;
218a4e7e98eSRodrigo Siqueira 	frame_end = crtc_state->frame_end;
219a4e7e98eSRodrigo Siqueira 	crc_pending = crtc_state->crc_pending;
220dbd9d80cSRodrigo Siqueira 	wb_pending = crtc_state->wb_pending;
221a4e7e98eSRodrigo Siqueira 	crtc_state->frame_start = 0;
222a4e7e98eSRodrigo Siqueira 	crtc_state->frame_end = 0;
223a4e7e98eSRodrigo Siqueira 	crtc_state->crc_pending = false;
224a4e7e98eSRodrigo Siqueira 	spin_unlock_irq(&out->composer_lock);
225a4e7e98eSRodrigo Siqueira 
226a4e7e98eSRodrigo Siqueira 	/*
227a4e7e98eSRodrigo Siqueira 	 * We raced with the vblank hrtimer and previous work already computed
228a4e7e98eSRodrigo Siqueira 	 * the crc, nothing to do.
229a4e7e98eSRodrigo Siqueira 	 */
230a4e7e98eSRodrigo Siqueira 	if (!crc_pending)
231a4e7e98eSRodrigo Siqueira 		return;
232a4e7e98eSRodrigo Siqueira 
233dbd9d80cSRodrigo Siqueira 	if (wb_pending)
234*8ba16485SIgor Torrente 		ret = compose_active_planes(active_wb, crtc_state, &crc32);
235*8ba16485SIgor Torrente 	else
236*8ba16485SIgor Torrente 		ret = compose_active_planes(NULL, crtc_state, &crc32);
237dbd9d80cSRodrigo Siqueira 
238*8ba16485SIgor Torrente 	if (ret)
23995302576SRodrigo Siqueira 		return;
240a4e7e98eSRodrigo Siqueira 
241dbd9d80cSRodrigo Siqueira 	if (wb_pending) {
242dbd9d80cSRodrigo Siqueira 		drm_writeback_signal_completion(&out->wb_connector, 0);
243dbd9d80cSRodrigo Siqueira 		spin_lock_irq(&out->composer_lock);
244dbd9d80cSRodrigo Siqueira 		crtc_state->wb_pending = false;
245dbd9d80cSRodrigo Siqueira 		spin_unlock_irq(&out->composer_lock);
246dbd9d80cSRodrigo Siqueira 	}
247dbd9d80cSRodrigo Siqueira 
248a4e7e98eSRodrigo Siqueira 	/*
249a4e7e98eSRodrigo Siqueira 	 * The worker can fall behind the vblank hrtimer, make sure we catch up.
250a4e7e98eSRodrigo Siqueira 	 */
251a4e7e98eSRodrigo Siqueira 	while (frame_start <= frame_end)
252a4e7e98eSRodrigo Siqueira 		drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
253a4e7e98eSRodrigo Siqueira }
254a4e7e98eSRodrigo Siqueira 
255a4e7e98eSRodrigo Siqueira static const char * const pipe_crc_sources[] = {"auto"};
256a4e7e98eSRodrigo Siqueira 
257a4e7e98eSRodrigo Siqueira const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
258a4e7e98eSRodrigo Siqueira 					size_t *count)
259a4e7e98eSRodrigo Siqueira {
260a4e7e98eSRodrigo Siqueira 	*count = ARRAY_SIZE(pipe_crc_sources);
261a4e7e98eSRodrigo Siqueira 	return pipe_crc_sources;
262a4e7e98eSRodrigo Siqueira }
263a4e7e98eSRodrigo Siqueira 
264a4e7e98eSRodrigo Siqueira static int vkms_crc_parse_source(const char *src_name, bool *enabled)
265a4e7e98eSRodrigo Siqueira {
266a4e7e98eSRodrigo Siqueira 	int ret = 0;
267a4e7e98eSRodrigo Siqueira 
268a4e7e98eSRodrigo Siqueira 	if (!src_name) {
269a4e7e98eSRodrigo Siqueira 		*enabled = false;
270a4e7e98eSRodrigo Siqueira 	} else if (strcmp(src_name, "auto") == 0) {
271a4e7e98eSRodrigo Siqueira 		*enabled = true;
272a4e7e98eSRodrigo Siqueira 	} else {
273a4e7e98eSRodrigo Siqueira 		*enabled = false;
274a4e7e98eSRodrigo Siqueira 		ret = -EINVAL;
275a4e7e98eSRodrigo Siqueira 	}
276a4e7e98eSRodrigo Siqueira 
277a4e7e98eSRodrigo Siqueira 	return ret;
278a4e7e98eSRodrigo Siqueira }
279a4e7e98eSRodrigo Siqueira 
280a4e7e98eSRodrigo Siqueira int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
281a4e7e98eSRodrigo Siqueira 			   size_t *values_cnt)
282a4e7e98eSRodrigo Siqueira {
283a4e7e98eSRodrigo Siqueira 	bool enabled;
284a4e7e98eSRodrigo Siqueira 
285a4e7e98eSRodrigo Siqueira 	if (vkms_crc_parse_source(src_name, &enabled) < 0) {
286a4e7e98eSRodrigo Siqueira 		DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
287a4e7e98eSRodrigo Siqueira 		return -EINVAL;
288a4e7e98eSRodrigo Siqueira 	}
289a4e7e98eSRodrigo Siqueira 
290a4e7e98eSRodrigo Siqueira 	*values_cnt = 1;
291a4e7e98eSRodrigo Siqueira 
292a4e7e98eSRodrigo Siqueira 	return 0;
293a4e7e98eSRodrigo Siqueira }
294a4e7e98eSRodrigo Siqueira 
295dbd9d80cSRodrigo Siqueira void vkms_set_composer(struct vkms_output *out, bool enabled)
2965bd858d7SMelissa Wen {
2975bd858d7SMelissa Wen 	bool old_enabled;
2985bd858d7SMelissa Wen 
2995bd858d7SMelissa Wen 	if (enabled)
3005bd858d7SMelissa Wen 		drm_crtc_vblank_get(&out->crtc);
3015bd858d7SMelissa Wen 
3025bd858d7SMelissa Wen 	spin_lock_irq(&out->lock);
3035bd858d7SMelissa Wen 	old_enabled = out->composer_enabled;
3045bd858d7SMelissa Wen 	out->composer_enabled = enabled;
3055bd858d7SMelissa Wen 	spin_unlock_irq(&out->lock);
3065bd858d7SMelissa Wen 
3075bd858d7SMelissa Wen 	if (old_enabled)
3085bd858d7SMelissa Wen 		drm_crtc_vblank_put(&out->crtc);
3095bd858d7SMelissa Wen }
3105bd858d7SMelissa Wen 
311a4e7e98eSRodrigo Siqueira int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
312a4e7e98eSRodrigo Siqueira {
313a4e7e98eSRodrigo Siqueira 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
314a4e7e98eSRodrigo Siqueira 	bool enabled = false;
315a4e7e98eSRodrigo Siqueira 	int ret = 0;
316a4e7e98eSRodrigo Siqueira 
317a4e7e98eSRodrigo Siqueira 	ret = vkms_crc_parse_source(src_name, &enabled);
318a4e7e98eSRodrigo Siqueira 
3195bd858d7SMelissa Wen 	vkms_set_composer(out, enabled);
320a4e7e98eSRodrigo Siqueira 
321a4e7e98eSRodrigo Siqueira 	return ret;
322a4e7e98eSRodrigo Siqueira }
323