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>
7cd075550SMaíra Canal #include <drm/drm_blend.h>
832a1648aSMelissa Wen #include <drm/drm_fourcc.h>
9db1f254fSArthur Grillo #include <drm/drm_fixed.h>
10a4e7e98eSRodrigo Siqueira #include <drm/drm_gem_framebuffer_helper.h>
11ce672a1bSSam Ravnborg #include <drm/drm_vblank.h>
128ba16485SIgor Torrente #include <linux/minmax.h>
13ce672a1bSSam Ravnborg
14ce672a1bSSam Ravnborg #include "vkms_drv.h"
15a4e7e98eSRodrigo Siqueira
pre_mul_blend_channel(u16 src,u16 dst,u16 alpha)168ba16485SIgor Torrente static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
1760cc2021SRodrigo Siqueira {
188ba16485SIgor Torrente u32 new_color;
1960cc2021SRodrigo Siqueira
208ba16485SIgor Torrente new_color = (src * 0xffff + dst * (0xffff - alpha));
2160cc2021SRodrigo Siqueira
228ba16485SIgor Torrente return DIV_ROUND_CLOSEST(new_color, 0xffff);
2360cc2021SRodrigo Siqueira }
2460cc2021SRodrigo Siqueira
25a4e7e98eSRodrigo Siqueira /**
268ba16485SIgor Torrente * pre_mul_alpha_blend - alpha blending equation
27fc429807SMaíra Canal * @frame_info: Source framebuffer's metadata
288ba16485SIgor Torrente * @stage_buffer: The line with the pixels from src_plane
298ba16485SIgor Torrente * @output_buffer: A line buffer that receives all the blends output
30a4e7e98eSRodrigo Siqueira *
318ba16485SIgor Torrente * Using the information from the `frame_info`, this blends only the
328ba16485SIgor Torrente * necessary pixels from the `stage_buffer` to the `output_buffer`
338ba16485SIgor Torrente * using premultiplied blend formula.
3432a1648aSMelissa Wen *
358ba16485SIgor Torrente * The current DRM assumption is that pixel color values have been already
368ba16485SIgor Torrente * pre-multiplied with the alpha channel values. See more
378ba16485SIgor Torrente * drm_plane_create_blend_mode_property(). Also, this formula assumes a
388ba16485SIgor Torrente * completely opaque background.
39a4e7e98eSRodrigo Siqueira */
pre_mul_alpha_blend(struct vkms_frame_info * frame_info,struct line_buffer * stage_buffer,struct line_buffer * output_buffer)408ba16485SIgor Torrente static void pre_mul_alpha_blend(struct vkms_frame_info *frame_info,
418ba16485SIgor Torrente struct line_buffer *stage_buffer,
428ba16485SIgor Torrente struct line_buffer *output_buffer)
43a4e7e98eSRodrigo Siqueira {
448ba16485SIgor Torrente int x_dst = frame_info->dst.x1;
458ba16485SIgor Torrente struct pixel_argb_u16 *out = output_buffer->pixels + x_dst;
468ba16485SIgor Torrente struct pixel_argb_u16 *in = stage_buffer->pixels;
478ba16485SIgor Torrente int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
488ba16485SIgor Torrente stage_buffer->n_pixels);
49a4e7e98eSRodrigo Siqueira
508ba16485SIgor Torrente for (int x = 0; x < x_limit; x++) {
518ba16485SIgor Torrente out[x].a = (u16)0xffff;
528ba16485SIgor Torrente out[x].r = pre_mul_blend_channel(in[x].r, out[x].r, in[x].a);
538ba16485SIgor Torrente out[x].g = pre_mul_blend_channel(in[x].g, out[x].g, in[x].a);
548ba16485SIgor Torrente out[x].b = pre_mul_blend_channel(in[x].b, out[x].b, in[x].a);
55a4e7e98eSRodrigo Siqueira }
56a4e7e98eSRodrigo Siqueira }
57a4e7e98eSRodrigo Siqueira
get_y_pos(struct vkms_frame_info * frame_info,int y)581ce76faeSMaíra Canal static int get_y_pos(struct vkms_frame_info *frame_info, int y)
591ce76faeSMaíra Canal {
601ce76faeSMaíra Canal if (frame_info->rotation & DRM_MODE_REFLECT_Y)
611ce76faeSMaíra Canal return drm_rect_height(&frame_info->rotated) - y - 1;
62cf7f8c67SMaíra Canal
63cf7f8c67SMaíra Canal switch (frame_info->rotation & DRM_MODE_ROTATE_MASK) {
64cf7f8c67SMaíra Canal case DRM_MODE_ROTATE_90:
65cf7f8c67SMaíra Canal return frame_info->rotated.x2 - y - 1;
66cd075550SMaíra Canal case DRM_MODE_ROTATE_270:
67cd075550SMaíra Canal return y + frame_info->rotated.x1;
68cf7f8c67SMaíra Canal default:
691ce76faeSMaíra Canal return y;
701ce76faeSMaíra Canal }
71cf7f8c67SMaíra Canal }
721ce76faeSMaíra Canal
check_limit(struct vkms_frame_info * frame_info,int pos)73cf7f8c67SMaíra Canal static bool check_limit(struct vkms_frame_info *frame_info, int pos)
74a4e7e98eSRodrigo Siqueira {
75cd075550SMaíra Canal if (drm_rotation_90_or_270(frame_info->rotation)) {
76cf7f8c67SMaíra Canal if (pos >= 0 && pos < drm_rect_width(&frame_info->rotated))
778ba16485SIgor Torrente return true;
78cf7f8c67SMaíra Canal } else {
79cf7f8c67SMaíra Canal if (pos >= frame_info->rotated.y1 && pos < frame_info->rotated.y2)
80cf7f8c67SMaíra Canal return true;
81cf7f8c67SMaíra Canal }
82a4e7e98eSRodrigo Siqueira
838ba16485SIgor Torrente return false;
84a4e7e98eSRodrigo Siqueira }
85a4e7e98eSRodrigo Siqueira
fill_background(const struct pixel_argb_u16 * background_color,struct line_buffer * output_buffer)86bc0d7fdeSIgor Torrente static void fill_background(const struct pixel_argb_u16 *background_color,
87bc0d7fdeSIgor Torrente struct line_buffer *output_buffer)
88bc0d7fdeSIgor Torrente {
89bc0d7fdeSIgor Torrente for (size_t i = 0; i < output_buffer->n_pixels; i++)
90bc0d7fdeSIgor Torrente output_buffer->pixels[i] = *background_color;
91bc0d7fdeSIgor Torrente }
92bc0d7fdeSIgor Torrente
93db1f254fSArthur Grillo // lerp(a, b, t) = a + (b - a) * t
lerp_u16(u16 a,u16 b,s64 t)94db1f254fSArthur Grillo static u16 lerp_u16(u16 a, u16 b, s64 t)
95db1f254fSArthur Grillo {
96db1f254fSArthur Grillo s64 a_fp = drm_int2fixp(a);
97db1f254fSArthur Grillo s64 b_fp = drm_int2fixp(b);
98db1f254fSArthur Grillo
99db1f254fSArthur Grillo s64 delta = drm_fixp_mul(b_fp - a_fp, t);
100db1f254fSArthur Grillo
101db1f254fSArthur Grillo return drm_fixp2int(a_fp + delta);
102db1f254fSArthur Grillo }
103db1f254fSArthur Grillo
get_lut_index(const struct vkms_color_lut * lut,u16 channel_value)104db1f254fSArthur Grillo static s64 get_lut_index(const struct vkms_color_lut *lut, u16 channel_value)
105db1f254fSArthur Grillo {
106db1f254fSArthur Grillo s64 color_channel_fp = drm_int2fixp(channel_value);
107db1f254fSArthur Grillo
108db1f254fSArthur Grillo return drm_fixp_mul(color_channel_fp, lut->channel_value2index_ratio);
109db1f254fSArthur Grillo }
110db1f254fSArthur Grillo
111db1f254fSArthur Grillo /*
112db1f254fSArthur Grillo * This enum is related to the positions of the variables inside
113db1f254fSArthur Grillo * `struct drm_color_lut`, so the order of both needs to be the same.
114db1f254fSArthur Grillo */
115db1f254fSArthur Grillo enum lut_channel {
116db1f254fSArthur Grillo LUT_RED = 0,
117db1f254fSArthur Grillo LUT_GREEN,
118db1f254fSArthur Grillo LUT_BLUE,
119db1f254fSArthur Grillo LUT_RESERVED
120db1f254fSArthur Grillo };
121db1f254fSArthur Grillo
apply_lut_to_channel_value(const struct vkms_color_lut * lut,u16 channel_value,enum lut_channel channel)122db1f254fSArthur Grillo static u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_value,
123db1f254fSArthur Grillo enum lut_channel channel)
124db1f254fSArthur Grillo {
125db1f254fSArthur Grillo s64 lut_index = get_lut_index(lut, channel_value);
126*2fee8403SHarry Wentland u16 *floor_lut_value, *ceil_lut_value;
127*2fee8403SHarry Wentland u16 floor_channel_value, ceil_channel_value;
128db1f254fSArthur Grillo
129db1f254fSArthur Grillo /*
130db1f254fSArthur Grillo * This checks if `struct drm_color_lut` has any gap added by the compiler
131db1f254fSArthur Grillo * between the struct fields.
132db1f254fSArthur Grillo */
133db1f254fSArthur Grillo static_assert(sizeof(struct drm_color_lut) == sizeof(__u16) * 4);
134db1f254fSArthur Grillo
135*2fee8403SHarry Wentland floor_lut_value = (__u16 *)&lut->base[drm_fixp2int(lut_index)];
136*2fee8403SHarry Wentland if (drm_fixp2int(lut_index) == (lut->lut_length - 1))
137*2fee8403SHarry Wentland /* We're at the end of the LUT array, use same value for ceil and floor */
138*2fee8403SHarry Wentland ceil_lut_value = floor_lut_value;
139*2fee8403SHarry Wentland else
140*2fee8403SHarry Wentland ceil_lut_value = (__u16 *)&lut->base[drm_fixp2int_ceil(lut_index)];
141db1f254fSArthur Grillo
142*2fee8403SHarry Wentland floor_channel_value = floor_lut_value[channel];
143*2fee8403SHarry Wentland ceil_channel_value = ceil_lut_value[channel];
144db1f254fSArthur Grillo
145db1f254fSArthur Grillo return lerp_u16(floor_channel_value, ceil_channel_value,
146db1f254fSArthur Grillo lut_index & DRM_FIXED_DECIMAL_MASK);
147db1f254fSArthur Grillo }
148db1f254fSArthur Grillo
apply_lut(const struct vkms_crtc_state * crtc_state,struct line_buffer * output_buffer)149db1f254fSArthur Grillo static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
150db1f254fSArthur Grillo {
151db1f254fSArthur Grillo if (!crtc_state->gamma_lut.base)
152db1f254fSArthur Grillo return;
153db1f254fSArthur Grillo
154db1f254fSArthur Grillo if (!crtc_state->gamma_lut.lut_length)
155db1f254fSArthur Grillo return;
156db1f254fSArthur Grillo
157db1f254fSArthur Grillo for (size_t x = 0; x < output_buffer->n_pixels; x++) {
158db1f254fSArthur Grillo struct pixel_argb_u16 *pixel = &output_buffer->pixels[x];
159db1f254fSArthur Grillo
160db1f254fSArthur Grillo pixel->r = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->r, LUT_RED);
161db1f254fSArthur Grillo pixel->g = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->g, LUT_GREEN);
162db1f254fSArthur Grillo pixel->b = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->b, LUT_BLUE);
163db1f254fSArthur Grillo }
164db1f254fSArthur Grillo }
165db1f254fSArthur Grillo
1668ba16485SIgor Torrente /**
167fc429807SMaíra Canal * blend - blend the pixels from all planes and compute crc
168fc429807SMaíra Canal * @wb: The writeback frame buffer metadata
1698ba16485SIgor Torrente * @crtc_state: The crtc state
1708ba16485SIgor Torrente * @crc32: The crc output of the final frame
1718ba16485SIgor Torrente * @output_buffer: A buffer of a row that will receive the result of the blend(s)
1728ba16485SIgor Torrente * @stage_buffer: The line with the pixels from plane being blend to the output
173fc429807SMaíra Canal * @row_size: The size, in bytes, of a single row
1748ba16485SIgor Torrente *
1758ba16485SIgor Torrente * This function blends the pixels (Using the `pre_mul_alpha_blend`)
1768ba16485SIgor Torrente * from all planes, calculates the crc32 of the output from the former step,
1778ba16485SIgor Torrente * and, if necessary, convert and store the output to the writeback buffer.
1788ba16485SIgor Torrente */
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)1798ba16485SIgor Torrente static void blend(struct vkms_writeback_job *wb,
1808ba16485SIgor Torrente struct vkms_crtc_state *crtc_state,
1818ba16485SIgor Torrente u32 *crc32, struct line_buffer *stage_buffer,
1828ba16485SIgor Torrente struct line_buffer *output_buffer, size_t row_size)
183a4e7e98eSRodrigo Siqueira {
1848ba16485SIgor Torrente struct vkms_plane_state **plane = crtc_state->active_planes;
1858ba16485SIgor Torrente u32 n_active_planes = crtc_state->num_active_planes;
1861ce76faeSMaíra Canal int y_pos;
187a4e7e98eSRodrigo Siqueira
188bc0d7fdeSIgor Torrente const struct pixel_argb_u16 background_color = { .a = 0xffff };
189a4e7e98eSRodrigo Siqueira
190bc0d7fdeSIgor Torrente size_t crtc_y_limit = crtc_state->base.crtc->mode.vdisplay;
191a4e7e98eSRodrigo Siqueira
192bc0d7fdeSIgor Torrente for (size_t y = 0; y < crtc_y_limit; y++) {
193bc0d7fdeSIgor Torrente fill_background(&background_color, output_buffer);
194bc0d7fdeSIgor Torrente
195bc0d7fdeSIgor Torrente /* The active planes are composed associatively in z-order. */
196bc0d7fdeSIgor Torrente for (size_t i = 0; i < n_active_planes; i++) {
1971ce76faeSMaíra Canal y_pos = get_y_pos(plane[i]->frame_info, y);
1981ce76faeSMaíra Canal
199cf7f8c67SMaíra Canal if (!check_limit(plane[i]->frame_info, y_pos))
2008ba16485SIgor Torrente continue;
2018ba16485SIgor Torrente
2021ce76faeSMaíra Canal vkms_compose_row(stage_buffer, plane[i], y_pos);
2038ba16485SIgor Torrente pre_mul_alpha_blend(plane[i]->frame_info, stage_buffer,
2048ba16485SIgor Torrente output_buffer);
2058ba16485SIgor Torrente }
2068ba16485SIgor Torrente
207db1f254fSArthur Grillo apply_lut(crtc_state, output_buffer);
208db1f254fSArthur Grillo
2098ba16485SIgor Torrente *crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
2108ba16485SIgor Torrente
2118ba16485SIgor Torrente if (wb)
212cc4fd293SMaíra Canal vkms_writeback_row(wb, output_buffer, y_pos);
2138ba16485SIgor Torrente }
2148ba16485SIgor Torrente }
2158ba16485SIgor Torrente
check_format_funcs(struct vkms_crtc_state * crtc_state,struct vkms_writeback_job * active_wb)2168ba16485SIgor Torrente static int check_format_funcs(struct vkms_crtc_state *crtc_state,
2178ba16485SIgor Torrente struct vkms_writeback_job *active_wb)
2188ba16485SIgor Torrente {
2198ba16485SIgor Torrente struct vkms_plane_state **planes = crtc_state->active_planes;
2208ba16485SIgor Torrente u32 n_active_planes = crtc_state->num_active_planes;
2218ba16485SIgor Torrente
2228ba16485SIgor Torrente for (size_t i = 0; i < n_active_planes; i++)
223322d716aSMaíra Canal if (!planes[i]->pixel_read)
2248ba16485SIgor Torrente return -1;
2258ba16485SIgor Torrente
226cc4fd293SMaíra Canal if (active_wb && !active_wb->pixel_write)
2278ba16485SIgor Torrente return -1;
228a4e7e98eSRodrigo Siqueira
22995302576SRodrigo Siqueira return 0;
230a4e7e98eSRodrigo Siqueira }
231a4e7e98eSRodrigo Siqueira
check_iosys_map(struct vkms_crtc_state * crtc_state)232bc0d7fdeSIgor Torrente static int check_iosys_map(struct vkms_crtc_state *crtc_state)
233bc0d7fdeSIgor Torrente {
234bc0d7fdeSIgor Torrente struct vkms_plane_state **plane_state = crtc_state->active_planes;
235bc0d7fdeSIgor Torrente u32 n_active_planes = crtc_state->num_active_planes;
236bc0d7fdeSIgor Torrente
237bc0d7fdeSIgor Torrente for (size_t i = 0; i < n_active_planes; i++)
238bc0d7fdeSIgor Torrente if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
239bc0d7fdeSIgor Torrente return -1;
240bc0d7fdeSIgor Torrente
241bc0d7fdeSIgor Torrente return 0;
242bc0d7fdeSIgor Torrente }
243bc0d7fdeSIgor Torrente
compose_active_planes(struct vkms_writeback_job * active_wb,struct vkms_crtc_state * crtc_state,u32 * crc32)2448ba16485SIgor Torrente static int compose_active_planes(struct vkms_writeback_job *active_wb,
2458ba16485SIgor Torrente struct vkms_crtc_state *crtc_state,
2468ba16485SIgor Torrente u32 *crc32)
2478ba16485SIgor Torrente {
2488ba16485SIgor Torrente size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
2498ba16485SIgor Torrente struct line_buffer output_buffer, stage_buffer;
2508ba16485SIgor Torrente int ret = 0;
2518ba16485SIgor Torrente
2528ba16485SIgor Torrente /*
2538ba16485SIgor Torrente * This check exists so we can call `crc32_le` for the entire line
2548ba16485SIgor Torrente * instead doing it for each channel of each pixel in case
2558ba16485SIgor Torrente * `struct `pixel_argb_u16` had any gap added by the compiler
2568ba16485SIgor Torrente * between the struct fields.
2578ba16485SIgor Torrente */
2588ba16485SIgor Torrente static_assert(sizeof(struct pixel_argb_u16) == 8);
2598ba16485SIgor Torrente
260bc0d7fdeSIgor Torrente if (WARN_ON(check_iosys_map(crtc_state)))
2618ba16485SIgor Torrente return -EINVAL;
2628ba16485SIgor Torrente
2638ba16485SIgor Torrente if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
2648ba16485SIgor Torrente return -EINVAL;
2658ba16485SIgor Torrente
266bc0d7fdeSIgor Torrente line_width = crtc_state->base.crtc->mode.hdisplay;
2678ba16485SIgor Torrente stage_buffer.n_pixels = line_width;
2688ba16485SIgor Torrente output_buffer.n_pixels = line_width;
2698ba16485SIgor Torrente
2708ba16485SIgor Torrente stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
2718ba16485SIgor Torrente if (!stage_buffer.pixels) {
2728ba16485SIgor Torrente DRM_ERROR("Cannot allocate memory for the output line buffer");
2738ba16485SIgor Torrente return -ENOMEM;
2748ba16485SIgor Torrente }
2758ba16485SIgor Torrente
2768ba16485SIgor Torrente output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
2778ba16485SIgor Torrente if (!output_buffer.pixels) {
2788ba16485SIgor Torrente DRM_ERROR("Cannot allocate memory for intermediate line buffer");
2798ba16485SIgor Torrente ret = -ENOMEM;
2808ba16485SIgor Torrente goto free_stage_buffer;
2818ba16485SIgor Torrente }
2828ba16485SIgor Torrente
2838ba16485SIgor Torrente blend(active_wb, crtc_state, crc32, &stage_buffer,
2848ba16485SIgor Torrente &output_buffer, line_width * pixel_size);
2858ba16485SIgor Torrente
2868ba16485SIgor Torrente kvfree(output_buffer.pixels);
2878ba16485SIgor Torrente free_stage_buffer:
2888ba16485SIgor Torrente kvfree(stage_buffer.pixels);
2898ba16485SIgor Torrente
2908ba16485SIgor Torrente return ret;
2918ba16485SIgor Torrente }
2928ba16485SIgor Torrente
293a4e7e98eSRodrigo Siqueira /**
294a4e7e98eSRodrigo Siqueira * vkms_composer_worker - ordered work_struct to compute CRC
295a4e7e98eSRodrigo Siqueira *
296a4e7e98eSRodrigo Siqueira * @work: work_struct
297a4e7e98eSRodrigo Siqueira *
298a4e7e98eSRodrigo Siqueira * Work handler for composing and computing CRCs. work_struct scheduled in
299a4e7e98eSRodrigo Siqueira * an ordered workqueue that's periodically scheduled to run by
300e3137249SAndré Almeida * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
301a4e7e98eSRodrigo Siqueira */
vkms_composer_worker(struct work_struct * work)302a4e7e98eSRodrigo Siqueira void vkms_composer_worker(struct work_struct *work)
303a4e7e98eSRodrigo Siqueira {
304a4e7e98eSRodrigo Siqueira struct vkms_crtc_state *crtc_state = container_of(work,
305a4e7e98eSRodrigo Siqueira struct vkms_crtc_state,
306a4e7e98eSRodrigo Siqueira composer_work);
307a4e7e98eSRodrigo Siqueira struct drm_crtc *crtc = crtc_state->base.crtc;
3088ba16485SIgor Torrente struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
309a4e7e98eSRodrigo Siqueira struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
310dbd9d80cSRodrigo Siqueira bool crc_pending, wb_pending;
311a4e7e98eSRodrigo Siqueira u64 frame_start, frame_end;
3128ba16485SIgor Torrente u32 crc32 = 0;
31395302576SRodrigo Siqueira int ret;
314a4e7e98eSRodrigo Siqueira
315a4e7e98eSRodrigo Siqueira spin_lock_irq(&out->composer_lock);
316a4e7e98eSRodrigo Siqueira frame_start = crtc_state->frame_start;
317a4e7e98eSRodrigo Siqueira frame_end = crtc_state->frame_end;
318a4e7e98eSRodrigo Siqueira crc_pending = crtc_state->crc_pending;
319dbd9d80cSRodrigo Siqueira wb_pending = crtc_state->wb_pending;
320a4e7e98eSRodrigo Siqueira crtc_state->frame_start = 0;
321a4e7e98eSRodrigo Siqueira crtc_state->frame_end = 0;
322a4e7e98eSRodrigo Siqueira crtc_state->crc_pending = false;
323db1f254fSArthur Grillo
324db1f254fSArthur Grillo if (crtc->state->gamma_lut) {
325db1f254fSArthur Grillo s64 max_lut_index_fp;
326db1f254fSArthur Grillo s64 u16_max_fp = drm_int2fixp(0xffff);
327db1f254fSArthur Grillo
328db1f254fSArthur Grillo crtc_state->gamma_lut.base = (struct drm_color_lut *)crtc->state->gamma_lut->data;
329db1f254fSArthur Grillo crtc_state->gamma_lut.lut_length =
330db1f254fSArthur Grillo crtc->state->gamma_lut->length / sizeof(struct drm_color_lut);
331db1f254fSArthur Grillo max_lut_index_fp = drm_int2fixp(crtc_state->gamma_lut.lut_length - 1);
332db1f254fSArthur Grillo crtc_state->gamma_lut.channel_value2index_ratio = drm_fixp_div(max_lut_index_fp,
333db1f254fSArthur Grillo u16_max_fp);
334db1f254fSArthur Grillo
335db1f254fSArthur Grillo } else {
336db1f254fSArthur Grillo crtc_state->gamma_lut.base = NULL;
337db1f254fSArthur Grillo }
338db1f254fSArthur Grillo
339a4e7e98eSRodrigo Siqueira spin_unlock_irq(&out->composer_lock);
340a4e7e98eSRodrigo Siqueira
341a4e7e98eSRodrigo Siqueira /*
342a4e7e98eSRodrigo Siqueira * We raced with the vblank hrtimer and previous work already computed
343a4e7e98eSRodrigo Siqueira * the crc, nothing to do.
344a4e7e98eSRodrigo Siqueira */
345a4e7e98eSRodrigo Siqueira if (!crc_pending)
346a4e7e98eSRodrigo Siqueira return;
347a4e7e98eSRodrigo Siqueira
348dbd9d80cSRodrigo Siqueira if (wb_pending)
3498ba16485SIgor Torrente ret = compose_active_planes(active_wb, crtc_state, &crc32);
3508ba16485SIgor Torrente else
3518ba16485SIgor Torrente ret = compose_active_planes(NULL, crtc_state, &crc32);
352dbd9d80cSRodrigo Siqueira
3538ba16485SIgor Torrente if (ret)
35495302576SRodrigo Siqueira return;
355a4e7e98eSRodrigo Siqueira
356dbd9d80cSRodrigo Siqueira if (wb_pending) {
357dbd9d80cSRodrigo Siqueira drm_writeback_signal_completion(&out->wb_connector, 0);
358dbd9d80cSRodrigo Siqueira spin_lock_irq(&out->composer_lock);
359dbd9d80cSRodrigo Siqueira crtc_state->wb_pending = false;
360dbd9d80cSRodrigo Siqueira spin_unlock_irq(&out->composer_lock);
361dbd9d80cSRodrigo Siqueira }
362dbd9d80cSRodrigo Siqueira
363a4e7e98eSRodrigo Siqueira /*
364a4e7e98eSRodrigo Siqueira * The worker can fall behind the vblank hrtimer, make sure we catch up.
365a4e7e98eSRodrigo Siqueira */
366a4e7e98eSRodrigo Siqueira while (frame_start <= frame_end)
367a4e7e98eSRodrigo Siqueira drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
368a4e7e98eSRodrigo Siqueira }
369a4e7e98eSRodrigo Siqueira
370a4e7e98eSRodrigo Siqueira static const char * const pipe_crc_sources[] = {"auto"};
371a4e7e98eSRodrigo Siqueira
vkms_get_crc_sources(struct drm_crtc * crtc,size_t * count)372a4e7e98eSRodrigo Siqueira const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
373a4e7e98eSRodrigo Siqueira size_t *count)
374a4e7e98eSRodrigo Siqueira {
375a4e7e98eSRodrigo Siqueira *count = ARRAY_SIZE(pipe_crc_sources);
376a4e7e98eSRodrigo Siqueira return pipe_crc_sources;
377a4e7e98eSRodrigo Siqueira }
378a4e7e98eSRodrigo Siqueira
vkms_crc_parse_source(const char * src_name,bool * enabled)379a4e7e98eSRodrigo Siqueira static int vkms_crc_parse_source(const char *src_name, bool *enabled)
380a4e7e98eSRodrigo Siqueira {
381a4e7e98eSRodrigo Siqueira int ret = 0;
382a4e7e98eSRodrigo Siqueira
383a4e7e98eSRodrigo Siqueira if (!src_name) {
384a4e7e98eSRodrigo Siqueira *enabled = false;
385a4e7e98eSRodrigo Siqueira } else if (strcmp(src_name, "auto") == 0) {
386a4e7e98eSRodrigo Siqueira *enabled = true;
387a4e7e98eSRodrigo Siqueira } else {
388a4e7e98eSRodrigo Siqueira *enabled = false;
389a4e7e98eSRodrigo Siqueira ret = -EINVAL;
390a4e7e98eSRodrigo Siqueira }
391a4e7e98eSRodrigo Siqueira
392a4e7e98eSRodrigo Siqueira return ret;
393a4e7e98eSRodrigo Siqueira }
394a4e7e98eSRodrigo Siqueira
vkms_verify_crc_source(struct drm_crtc * crtc,const char * src_name,size_t * values_cnt)395a4e7e98eSRodrigo Siqueira int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
396a4e7e98eSRodrigo Siqueira size_t *values_cnt)
397a4e7e98eSRodrigo Siqueira {
398a4e7e98eSRodrigo Siqueira bool enabled;
399a4e7e98eSRodrigo Siqueira
400a4e7e98eSRodrigo Siqueira if (vkms_crc_parse_source(src_name, &enabled) < 0) {
401a4e7e98eSRodrigo Siqueira DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
402a4e7e98eSRodrigo Siqueira return -EINVAL;
403a4e7e98eSRodrigo Siqueira }
404a4e7e98eSRodrigo Siqueira
405a4e7e98eSRodrigo Siqueira *values_cnt = 1;
406a4e7e98eSRodrigo Siqueira
407a4e7e98eSRodrigo Siqueira return 0;
408a4e7e98eSRodrigo Siqueira }
409a4e7e98eSRodrigo Siqueira
vkms_set_composer(struct vkms_output * out,bool enabled)410dbd9d80cSRodrigo Siqueira void vkms_set_composer(struct vkms_output *out, bool enabled)
4115bd858d7SMelissa Wen {
4125bd858d7SMelissa Wen bool old_enabled;
4135bd858d7SMelissa Wen
4145bd858d7SMelissa Wen if (enabled)
4155bd858d7SMelissa Wen drm_crtc_vblank_get(&out->crtc);
4165bd858d7SMelissa Wen
4177908632fSMaíra Canal spin_lock_irq(&out->lock);
4185bd858d7SMelissa Wen old_enabled = out->composer_enabled;
4195bd858d7SMelissa Wen out->composer_enabled = enabled;
4207908632fSMaíra Canal spin_unlock_irq(&out->lock);
4215bd858d7SMelissa Wen
4225bd858d7SMelissa Wen if (old_enabled)
4235bd858d7SMelissa Wen drm_crtc_vblank_put(&out->crtc);
4245bd858d7SMelissa Wen }
4255bd858d7SMelissa Wen
vkms_set_crc_source(struct drm_crtc * crtc,const char * src_name)426a4e7e98eSRodrigo Siqueira int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
427a4e7e98eSRodrigo Siqueira {
428a4e7e98eSRodrigo Siqueira struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
429a4e7e98eSRodrigo Siqueira bool enabled = false;
430a4e7e98eSRodrigo Siqueira int ret = 0;
431a4e7e98eSRodrigo Siqueira
432a4e7e98eSRodrigo Siqueira ret = vkms_crc_parse_source(src_name, &enabled);
433a4e7e98eSRodrigo Siqueira
4345bd858d7SMelissa Wen vkms_set_composer(out, enabled);
435a4e7e98eSRodrigo Siqueira
436a4e7e98eSRodrigo Siqueira return ret;
437a4e7e98eSRodrigo Siqueira }
438