vkms_formats.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/kernel.h>
  3. #include <linux/minmax.h>
  4. #include <drm/drm_blend.h>
  5. #include <drm/drm_rect.h>
  6. #include <drm/drm_fixed.h>
  7. #include "vkms_formats.h"
  8. static size_t pixel_offset(const struct vkms_frame_info *frame_info, int x, int y)
  9. {
  10. return frame_info->offset + (y * frame_info->pitch)
  11. + (x * frame_info->cpp);
  12. }
  13. /*
  14. * packed_pixels_addr - Get the pointer to pixel of a given pair of coordinates
  15. *
  16. * @frame_info: Buffer metadata
  17. * @x: The x(width) coordinate of the 2D buffer
  18. * @y: The y(Heigth) coordinate of the 2D buffer
  19. *
  20. * Takes the information stored in the frame_info, a pair of coordinates, and
  21. * returns the address of the first color channel.
  22. * This function assumes the channels are packed together, i.e. a color channel
  23. * comes immediately after another in the memory. And therefore, this function
  24. * doesn't work for YUV with chroma subsampling (e.g. YUV420 and NV21).
  25. */
  26. static void *packed_pixels_addr(const struct vkms_frame_info *frame_info,
  27. int x, int y)
  28. {
  29. size_t offset = pixel_offset(frame_info, x, y);
  30. return (u8 *)frame_info->map[0].vaddr + offset;
  31. }
  32. static void *get_packed_src_addr(const struct vkms_frame_info *frame_info, int y)
  33. {
  34. int x_src = frame_info->src.x1 >> 16;
  35. int y_src = y - frame_info->rotated.y1 + (frame_info->src.y1 >> 16);
  36. return packed_pixels_addr(frame_info, x_src, y_src);
  37. }
  38. static int get_x_position(const struct vkms_frame_info *frame_info, int limit, int x)
  39. {
  40. if (frame_info->rotation & (DRM_MODE_REFLECT_X | DRM_MODE_ROTATE_270))
  41. return limit - x - 1;
  42. return x;
  43. }
  44. static void ARGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
  45. {
  46. /*
  47. * The 257 is the "conversion ratio". This number is obtained by the
  48. * (2^16 - 1) / (2^8 - 1) division. Which, in this case, tries to get
  49. * the best color value in a pixel format with more possibilities.
  50. * A similar idea applies to others RGB color conversions.
  51. */
  52. out_pixel->a = (u16)src_pixels[3] * 257;
  53. out_pixel->r = (u16)src_pixels[2] * 257;
  54. out_pixel->g = (u16)src_pixels[1] * 257;
  55. out_pixel->b = (u16)src_pixels[0] * 257;
  56. }
  57. static void XRGB8888_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
  58. {
  59. out_pixel->a = (u16)0xffff;
  60. out_pixel->r = (u16)src_pixels[2] * 257;
  61. out_pixel->g = (u16)src_pixels[1] * 257;
  62. out_pixel->b = (u16)src_pixels[0] * 257;
  63. }
  64. static void ARGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
  65. {
  66. __le16 *pixels = (__force __le16 *)src_pixels;
  67. out_pixel->a = le16_to_cpu(pixels[3]);
  68. out_pixel->r = le16_to_cpu(pixels[2]);
  69. out_pixel->g = le16_to_cpu(pixels[1]);
  70. out_pixel->b = le16_to_cpu(pixels[0]);
  71. }
  72. static void XRGB16161616_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
  73. {
  74. __le16 *pixels = (__force __le16 *)src_pixels;
  75. out_pixel->a = (u16)0xffff;
  76. out_pixel->r = le16_to_cpu(pixels[2]);
  77. out_pixel->g = le16_to_cpu(pixels[1]);
  78. out_pixel->b = le16_to_cpu(pixels[0]);
  79. }
  80. static void RGB565_to_argb_u16(u8 *src_pixels, struct pixel_argb_u16 *out_pixel)
  81. {
  82. __le16 *pixels = (__force __le16 *)src_pixels;
  83. s64 fp_rb_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(31));
  84. s64 fp_g_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(63));
  85. u16 rgb_565 = le16_to_cpu(*pixels);
  86. s64 fp_r = drm_int2fixp((rgb_565 >> 11) & 0x1f);
  87. s64 fp_g = drm_int2fixp((rgb_565 >> 5) & 0x3f);
  88. s64 fp_b = drm_int2fixp(rgb_565 & 0x1f);
  89. out_pixel->a = (u16)0xffff;
  90. out_pixel->r = drm_fixp2int_round(drm_fixp_mul(fp_r, fp_rb_ratio));
  91. out_pixel->g = drm_fixp2int_round(drm_fixp_mul(fp_g, fp_g_ratio));
  92. out_pixel->b = drm_fixp2int_round(drm_fixp_mul(fp_b, fp_rb_ratio));
  93. }
  94. /**
  95. * vkms_compose_row - compose a single row of a plane
  96. * @stage_buffer: output line with the composed pixels
  97. * @plane: state of the plane that is being composed
  98. * @y: y coordinate of the row
  99. *
  100. * This function composes a single row of a plane. It gets the source pixels
  101. * through the y coordinate (see get_packed_src_addr()) and goes linearly
  102. * through the source pixel, reading the pixels and converting it to
  103. * ARGB16161616 (see the pixel_read() callback). For rotate-90 and rotate-270,
  104. * the source pixels are not traversed linearly. The source pixels are queried
  105. * on each iteration in order to traverse the pixels vertically.
  106. */
  107. void vkms_compose_row(struct line_buffer *stage_buffer, struct vkms_plane_state *plane, int y)
  108. {
  109. struct pixel_argb_u16 *out_pixels = stage_buffer->pixels;
  110. struct vkms_frame_info *frame_info = plane->frame_info;
  111. u8 *src_pixels = get_packed_src_addr(frame_info, y);
  112. int limit = min_t(size_t, drm_rect_width(&frame_info->dst), stage_buffer->n_pixels);
  113. for (size_t x = 0; x < limit; x++, src_pixels += frame_info->cpp) {
  114. int x_pos = get_x_position(frame_info, limit, x);
  115. if (drm_rotation_90_or_270(frame_info->rotation))
  116. src_pixels = get_packed_src_addr(frame_info, x + frame_info->rotated.y1)
  117. + frame_info->cpp * y;
  118. plane->pixel_read(src_pixels, &out_pixels[x_pos]);
  119. }
  120. }
  121. /*
  122. * The following functions take an line of argb_u16 pixels from the
  123. * src_buffer, convert them to a specific format, and store them in the
  124. * destination.
  125. *
  126. * They are used in the `compose_active_planes` to convert and store a line
  127. * from the src_buffer to the writeback buffer.
  128. */
  129. static void argb_u16_to_ARGB8888(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
  130. {
  131. /*
  132. * This sequence below is important because the format's byte order is
  133. * in little-endian. In the case of the ARGB8888 the memory is
  134. * organized this way:
  135. *
  136. * | Addr | = blue channel
  137. * | Addr + 1 | = green channel
  138. * | Addr + 2 | = Red channel
  139. * | Addr + 3 | = Alpha channel
  140. */
  141. dst_pixels[3] = DIV_ROUND_CLOSEST(in_pixel->a, 257);
  142. dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixel->r, 257);
  143. dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixel->g, 257);
  144. dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixel->b, 257);
  145. }
  146. static void argb_u16_to_XRGB8888(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
  147. {
  148. dst_pixels[3] = 0xff;
  149. dst_pixels[2] = DIV_ROUND_CLOSEST(in_pixel->r, 257);
  150. dst_pixels[1] = DIV_ROUND_CLOSEST(in_pixel->g, 257);
  151. dst_pixels[0] = DIV_ROUND_CLOSEST(in_pixel->b, 257);
  152. }
  153. static void argb_u16_to_ARGB16161616(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
  154. {
  155. __le16 *pixels = (__force __le16 *)dst_pixels;
  156. pixels[3] = cpu_to_le16(in_pixel->a);
  157. pixels[2] = cpu_to_le16(in_pixel->r);
  158. pixels[1] = cpu_to_le16(in_pixel->g);
  159. pixels[0] = cpu_to_le16(in_pixel->b);
  160. }
  161. static void argb_u16_to_XRGB16161616(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
  162. {
  163. __le16 *pixels = (__force __le16 *)dst_pixels;
  164. pixels[3] = cpu_to_le16(0xffff);
  165. pixels[2] = cpu_to_le16(in_pixel->r);
  166. pixels[1] = cpu_to_le16(in_pixel->g);
  167. pixels[0] = cpu_to_le16(in_pixel->b);
  168. }
  169. static void argb_u16_to_RGB565(u8 *dst_pixels, struct pixel_argb_u16 *in_pixel)
  170. {
  171. __le16 *pixels = (__force __le16 *)dst_pixels;
  172. s64 fp_rb_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(31));
  173. s64 fp_g_ratio = drm_fixp_div(drm_int2fixp(65535), drm_int2fixp(63));
  174. s64 fp_r = drm_int2fixp(in_pixel->r);
  175. s64 fp_g = drm_int2fixp(in_pixel->g);
  176. s64 fp_b = drm_int2fixp(in_pixel->b);
  177. u16 r = drm_fixp2int(drm_fixp_div(fp_r, fp_rb_ratio));
  178. u16 g = drm_fixp2int(drm_fixp_div(fp_g, fp_g_ratio));
  179. u16 b = drm_fixp2int(drm_fixp_div(fp_b, fp_rb_ratio));
  180. *pixels = cpu_to_le16(r << 11 | g << 5 | b);
  181. }
  182. void vkms_writeback_row(struct vkms_writeback_job *wb,
  183. const struct line_buffer *src_buffer, int y)
  184. {
  185. struct vkms_frame_info *frame_info = &wb->wb_frame_info;
  186. int x_dst = frame_info->dst.x1;
  187. u8 *dst_pixels = packed_pixels_addr(frame_info, x_dst, y);
  188. struct pixel_argb_u16 *in_pixels = src_buffer->pixels;
  189. int x_limit = min_t(size_t, drm_rect_width(&frame_info->dst), src_buffer->n_pixels);
  190. for (size_t x = 0; x < x_limit; x++, dst_pixels += frame_info->cpp)
  191. wb->pixel_write(dst_pixels, &in_pixels[x]);
  192. }
  193. void *get_pixel_conversion_function(u32 format)
  194. {
  195. switch (format) {
  196. case DRM_FORMAT_ARGB8888:
  197. return &ARGB8888_to_argb_u16;
  198. case DRM_FORMAT_XRGB8888:
  199. return &XRGB8888_to_argb_u16;
  200. case DRM_FORMAT_ARGB16161616:
  201. return &ARGB16161616_to_argb_u16;
  202. case DRM_FORMAT_XRGB16161616:
  203. return &XRGB16161616_to_argb_u16;
  204. case DRM_FORMAT_RGB565:
  205. return &RGB565_to_argb_u16;
  206. default:
  207. return NULL;
  208. }
  209. }
  210. void *get_pixel_write_function(u32 format)
  211. {
  212. switch (format) {
  213. case DRM_FORMAT_ARGB8888:
  214. return &argb_u16_to_ARGB8888;
  215. case DRM_FORMAT_XRGB8888:
  216. return &argb_u16_to_XRGB8888;
  217. case DRM_FORMAT_ARGB16161616:
  218. return &argb_u16_to_ARGB16161616;
  219. case DRM_FORMAT_XRGB16161616:
  220. return &argb_u16_to_XRGB16161616;
  221. case DRM_FORMAT_RGB565:
  222. return &argb_u16_to_RGB565;
  223. default:
  224. return NULL;
  225. }
  226. }