vkms_composer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/crc32.h>
  3. #include <drm/drm_atomic.h>
  4. #include <drm/drm_atomic_helper.h>
  5. #include <drm/drm_blend.h>
  6. #include <drm/drm_fourcc.h>
  7. #include <drm/drm_fixed.h>
  8. #include <drm/drm_gem_framebuffer_helper.h>
  9. #include <drm/drm_vblank.h>
  10. #include <linux/minmax.h>
  11. #include "vkms_drv.h"
  12. static u16 pre_mul_blend_channel(u16 src, u16 dst, u16 alpha)
  13. {
  14. u32 new_color;
  15. new_color = (src * 0xffff + dst * (0xffff - alpha));
  16. return DIV_ROUND_CLOSEST(new_color, 0xffff);
  17. }
  18. /**
  19. * pre_mul_alpha_blend - alpha blending equation
  20. * @frame_info: Source framebuffer's metadata
  21. * @stage_buffer: The line with the pixels from src_plane
  22. * @output_buffer: A line buffer that receives all the blends output
  23. *
  24. * Using the information from the `frame_info`, this blends only the
  25. * necessary pixels from the `stage_buffer` to the `output_buffer`
  26. * using premultiplied blend formula.
  27. *
  28. * The current DRM assumption is that pixel color values have been already
  29. * pre-multiplied with the alpha channel values. See more
  30. * drm_plane_create_blend_mode_property(). Also, this formula assumes a
  31. * completely opaque background.
  32. */
  33. static void pre_mul_alpha_blend(struct vkms_frame_info *frame_info,
  34. struct line_buffer *stage_buffer,
  35. struct line_buffer *output_buffer)
  36. {
  37. int x_dst = frame_info->dst.x1;
  38. struct pixel_argb_u16 *out = output_buffer->pixels + x_dst;
  39. struct pixel_argb_u16 *in = stage_buffer->pixels;
  40. int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst),
  41. stage_buffer->n_pixels);
  42. for (int x = 0; x < x_limit; x++) {
  43. out[x].a = (u16)0xffff;
  44. out[x].r = pre_mul_blend_channel(in[x].r, out[x].r, in[x].a);
  45. out[x].g = pre_mul_blend_channel(in[x].g, out[x].g, in[x].a);
  46. out[x].b = pre_mul_blend_channel(in[x].b, out[x].b, in[x].a);
  47. }
  48. }
  49. static int get_y_pos(struct vkms_frame_info *frame_info, int y)
  50. {
  51. if (frame_info->rotation & DRM_MODE_REFLECT_Y)
  52. return drm_rect_height(&frame_info->rotated) - y - 1;
  53. switch (frame_info->rotation & DRM_MODE_ROTATE_MASK) {
  54. case DRM_MODE_ROTATE_90:
  55. return frame_info->rotated.x2 - y - 1;
  56. case DRM_MODE_ROTATE_270:
  57. return y + frame_info->rotated.x1;
  58. default:
  59. return y;
  60. }
  61. }
  62. static bool check_limit(struct vkms_frame_info *frame_info, int pos)
  63. {
  64. if (drm_rotation_90_or_270(frame_info->rotation)) {
  65. if (pos >= 0 && pos < drm_rect_width(&frame_info->rotated))
  66. return true;
  67. } else {
  68. if (pos >= frame_info->rotated.y1 && pos < frame_info->rotated.y2)
  69. return true;
  70. }
  71. return false;
  72. }
  73. static void fill_background(const struct pixel_argb_u16 *background_color,
  74. struct line_buffer *output_buffer)
  75. {
  76. for (size_t i = 0; i < output_buffer->n_pixels; i++)
  77. output_buffer->pixels[i] = *background_color;
  78. }
  79. // lerp(a, b, t) = a + (b - a) * t
  80. static u16 lerp_u16(u16 a, u16 b, s64 t)
  81. {
  82. s64 a_fp = drm_int2fixp(a);
  83. s64 b_fp = drm_int2fixp(b);
  84. s64 delta = drm_fixp_mul(b_fp - a_fp, t);
  85. return drm_fixp2int_round(a_fp + delta);
  86. }
  87. static s64 get_lut_index(const struct vkms_color_lut *lut, u16 channel_value)
  88. {
  89. s64 color_channel_fp = drm_int2fixp(channel_value);
  90. return drm_fixp_mul(color_channel_fp, lut->channel_value2index_ratio);
  91. }
  92. /*
  93. * This enum is related to the positions of the variables inside
  94. * `struct drm_color_lut`, so the order of both needs to be the same.
  95. */
  96. enum lut_channel {
  97. LUT_RED = 0,
  98. LUT_GREEN,
  99. LUT_BLUE,
  100. LUT_RESERVED
  101. };
  102. static u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_value,
  103. enum lut_channel channel)
  104. {
  105. s64 lut_index = get_lut_index(lut, channel_value);
  106. u16 *floor_lut_value, *ceil_lut_value;
  107. u16 floor_channel_value, ceil_channel_value;
  108. /*
  109. * This checks if `struct drm_color_lut` has any gap added by the compiler
  110. * between the struct fields.
  111. */
  112. static_assert(sizeof(struct drm_color_lut) == sizeof(__u16) * 4);
  113. floor_lut_value = (__u16 *)&lut->base[drm_fixp2int(lut_index)];
  114. if (drm_fixp2int(lut_index) == (lut->lut_length - 1))
  115. /* We're at the end of the LUT array, use same value for ceil and floor */
  116. ceil_lut_value = floor_lut_value;
  117. else
  118. ceil_lut_value = (__u16 *)&lut->base[drm_fixp2int_ceil(lut_index)];
  119. floor_channel_value = floor_lut_value[channel];
  120. ceil_channel_value = ceil_lut_value[channel];
  121. return lerp_u16(floor_channel_value, ceil_channel_value,
  122. lut_index & DRM_FIXED_DECIMAL_MASK);
  123. }
  124. static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
  125. {
  126. if (!crtc_state->gamma_lut.base)
  127. return;
  128. if (!crtc_state->gamma_lut.lut_length)
  129. return;
  130. for (size_t x = 0; x < output_buffer->n_pixels; x++) {
  131. struct pixel_argb_u16 *pixel = &output_buffer->pixels[x];
  132. pixel->r = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->r, LUT_RED);
  133. pixel->g = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->g, LUT_GREEN);
  134. pixel->b = apply_lut_to_channel_value(&crtc_state->gamma_lut, pixel->b, LUT_BLUE);
  135. }
  136. }
  137. /**
  138. * blend - blend the pixels from all planes and compute crc
  139. * @wb: The writeback frame buffer metadata
  140. * @crtc_state: The crtc state
  141. * @crc32: The crc output of the final frame
  142. * @output_buffer: A buffer of a row that will receive the result of the blend(s)
  143. * @stage_buffer: The line with the pixels from plane being blend to the output
  144. * @row_size: The size, in bytes, of a single row
  145. *
  146. * This function blends the pixels (Using the `pre_mul_alpha_blend`)
  147. * from all planes, calculates the crc32 of the output from the former step,
  148. * and, if necessary, convert and store the output to the writeback buffer.
  149. */
  150. static void blend(struct vkms_writeback_job *wb,
  151. struct vkms_crtc_state *crtc_state,
  152. u32 *crc32, struct line_buffer *stage_buffer,
  153. struct line_buffer *output_buffer, size_t row_size)
  154. {
  155. struct vkms_plane_state **plane = crtc_state->active_planes;
  156. u32 n_active_planes = crtc_state->num_active_planes;
  157. int y_pos;
  158. const struct pixel_argb_u16 background_color = { .a = 0xffff };
  159. size_t crtc_y_limit = crtc_state->base.crtc->mode.vdisplay;
  160. for (size_t y = 0; y < crtc_y_limit; y++) {
  161. fill_background(&background_color, output_buffer);
  162. /* The active planes are composed associatively in z-order. */
  163. for (size_t i = 0; i < n_active_planes; i++) {
  164. y_pos = get_y_pos(plane[i]->frame_info, y);
  165. if (!check_limit(plane[i]->frame_info, y_pos))
  166. continue;
  167. vkms_compose_row(stage_buffer, plane[i], y_pos);
  168. pre_mul_alpha_blend(plane[i]->frame_info, stage_buffer,
  169. output_buffer);
  170. }
  171. apply_lut(crtc_state, output_buffer);
  172. *crc32 = crc32_le(*crc32, (void *)output_buffer->pixels, row_size);
  173. if (wb)
  174. vkms_writeback_row(wb, output_buffer, y_pos);
  175. }
  176. }
  177. static int check_format_funcs(struct vkms_crtc_state *crtc_state,
  178. struct vkms_writeback_job *active_wb)
  179. {
  180. struct vkms_plane_state **planes = crtc_state->active_planes;
  181. u32 n_active_planes = crtc_state->num_active_planes;
  182. for (size_t i = 0; i < n_active_planes; i++)
  183. if (!planes[i]->pixel_read)
  184. return -1;
  185. if (active_wb && !active_wb->pixel_write)
  186. return -1;
  187. return 0;
  188. }
  189. static int check_iosys_map(struct vkms_crtc_state *crtc_state)
  190. {
  191. struct vkms_plane_state **plane_state = crtc_state->active_planes;
  192. u32 n_active_planes = crtc_state->num_active_planes;
  193. for (size_t i = 0; i < n_active_planes; i++)
  194. if (iosys_map_is_null(&plane_state[i]->frame_info->map[0]))
  195. return -1;
  196. return 0;
  197. }
  198. static int compose_active_planes(struct vkms_writeback_job *active_wb,
  199. struct vkms_crtc_state *crtc_state,
  200. u32 *crc32)
  201. {
  202. size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
  203. struct line_buffer output_buffer, stage_buffer;
  204. int ret = 0;
  205. /*
  206. * This check exists so we can call `crc32_le` for the entire line
  207. * instead doing it for each channel of each pixel in case
  208. * `struct `pixel_argb_u16` had any gap added by the compiler
  209. * between the struct fields.
  210. */
  211. static_assert(sizeof(struct pixel_argb_u16) == 8);
  212. if (WARN_ON(check_iosys_map(crtc_state)))
  213. return -EINVAL;
  214. if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
  215. return -EINVAL;
  216. line_width = crtc_state->base.crtc->mode.hdisplay;
  217. stage_buffer.n_pixels = line_width;
  218. output_buffer.n_pixels = line_width;
  219. stage_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
  220. if (!stage_buffer.pixels) {
  221. DRM_ERROR("Cannot allocate memory for the output line buffer");
  222. return -ENOMEM;
  223. }
  224. output_buffer.pixels = kvmalloc(line_width * pixel_size, GFP_KERNEL);
  225. if (!output_buffer.pixels) {
  226. DRM_ERROR("Cannot allocate memory for intermediate line buffer");
  227. ret = -ENOMEM;
  228. goto free_stage_buffer;
  229. }
  230. blend(active_wb, crtc_state, crc32, &stage_buffer,
  231. &output_buffer, line_width * pixel_size);
  232. kvfree(output_buffer.pixels);
  233. free_stage_buffer:
  234. kvfree(stage_buffer.pixels);
  235. return ret;
  236. }
  237. /**
  238. * vkms_composer_worker - ordered work_struct to compute CRC
  239. *
  240. * @work: work_struct
  241. *
  242. * Work handler for composing and computing CRCs. work_struct scheduled in
  243. * an ordered workqueue that's periodically scheduled to run by
  244. * vkms_vblank_simulate() and flushed at vkms_atomic_commit_tail().
  245. */
  246. void vkms_composer_worker(struct work_struct *work)
  247. {
  248. struct vkms_crtc_state *crtc_state = container_of(work,
  249. struct vkms_crtc_state,
  250. composer_work);
  251. struct drm_crtc *crtc = crtc_state->base.crtc;
  252. struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
  253. struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
  254. bool crc_pending, wb_pending;
  255. u64 frame_start, frame_end;
  256. u32 crc32 = 0;
  257. int ret;
  258. spin_lock_irq(&out->composer_lock);
  259. frame_start = crtc_state->frame_start;
  260. frame_end = crtc_state->frame_end;
  261. crc_pending = crtc_state->crc_pending;
  262. wb_pending = crtc_state->wb_pending;
  263. crtc_state->frame_start = 0;
  264. crtc_state->frame_end = 0;
  265. crtc_state->crc_pending = false;
  266. if (crtc->state->gamma_lut) {
  267. s64 max_lut_index_fp;
  268. s64 u16_max_fp = drm_int2fixp(0xffff);
  269. crtc_state->gamma_lut.base = (struct drm_color_lut *)crtc->state->gamma_lut->data;
  270. crtc_state->gamma_lut.lut_length =
  271. crtc->state->gamma_lut->length / sizeof(struct drm_color_lut);
  272. max_lut_index_fp = drm_int2fixp(crtc_state->gamma_lut.lut_length - 1);
  273. crtc_state->gamma_lut.channel_value2index_ratio = drm_fixp_div(max_lut_index_fp,
  274. u16_max_fp);
  275. } else {
  276. crtc_state->gamma_lut.base = NULL;
  277. }
  278. spin_unlock_irq(&out->composer_lock);
  279. /*
  280. * We raced with the vblank hrtimer and previous work already computed
  281. * the crc, nothing to do.
  282. */
  283. if (!crc_pending)
  284. return;
  285. if (wb_pending)
  286. ret = compose_active_planes(active_wb, crtc_state, &crc32);
  287. else
  288. ret = compose_active_planes(NULL, crtc_state, &crc32);
  289. if (ret)
  290. return;
  291. if (wb_pending) {
  292. drm_writeback_signal_completion(&out->wb_connector, 0);
  293. spin_lock_irq(&out->composer_lock);
  294. crtc_state->wb_pending = false;
  295. spin_unlock_irq(&out->composer_lock);
  296. }
  297. /*
  298. * The worker can fall behind the vblank hrtimer, make sure we catch up.
  299. */
  300. while (frame_start <= frame_end)
  301. drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
  302. }
  303. static const char * const pipe_crc_sources[] = {"auto"};
  304. const char *const *vkms_get_crc_sources(struct drm_crtc *crtc,
  305. size_t *count)
  306. {
  307. *count = ARRAY_SIZE(pipe_crc_sources);
  308. return pipe_crc_sources;
  309. }
  310. static int vkms_crc_parse_source(const char *src_name, bool *enabled)
  311. {
  312. int ret = 0;
  313. if (!src_name) {
  314. *enabled = false;
  315. } else if (strcmp(src_name, "auto") == 0) {
  316. *enabled = true;
  317. } else {
  318. *enabled = false;
  319. ret = -EINVAL;
  320. }
  321. return ret;
  322. }
  323. int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name,
  324. size_t *values_cnt)
  325. {
  326. bool enabled;
  327. if (vkms_crc_parse_source(src_name, &enabled) < 0) {
  328. DRM_DEBUG_DRIVER("unknown source %s\n", src_name);
  329. return -EINVAL;
  330. }
  331. *values_cnt = 1;
  332. return 0;
  333. }
  334. void vkms_set_composer(struct vkms_output *out, bool enabled)
  335. {
  336. bool old_enabled;
  337. if (enabled)
  338. drm_crtc_vblank_get(&out->crtc);
  339. spin_lock_irq(&out->lock);
  340. old_enabled = out->composer_enabled;
  341. out->composer_enabled = enabled;
  342. spin_unlock_irq(&out->lock);
  343. if (old_enabled)
  344. drm_crtc_vblank_put(&out->crtc);
  345. }
  346. int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
  347. {
  348. struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
  349. bool enabled = false;
  350. int ret = 0;
  351. ret = vkms_crc_parse_source(src_name, &enabled);
  352. vkms_set_composer(out, enabled);
  353. return ret;
  354. }