drm_fourcc.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2016 Laurent Pinchart <[email protected]>
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #ifndef __DRM_FOURCC_H__
  23. #define __DRM_FOURCC_H__
  24. #include <linux/types.h>
  25. #include <uapi/drm/drm_fourcc.h>
  26. /**
  27. * DRM_FORMAT_MAX_PLANES - maximum number of planes a DRM format can have
  28. */
  29. #define DRM_FORMAT_MAX_PLANES 4u
  30. /*
  31. * DRM formats are little endian. Define host endian variants for the
  32. * most common formats here, to reduce the #ifdefs needed in drivers.
  33. *
  34. * Note that the DRM_FORMAT_BIG_ENDIAN flag should only be used in
  35. * case the format can't be specified otherwise, so we don't end up
  36. * with two values describing the same format.
  37. */
  38. #ifdef __BIG_ENDIAN
  39. # define DRM_FORMAT_HOST_XRGB1555 (DRM_FORMAT_XRGB1555 | \
  40. DRM_FORMAT_BIG_ENDIAN)
  41. # define DRM_FORMAT_HOST_RGB565 (DRM_FORMAT_RGB565 | \
  42. DRM_FORMAT_BIG_ENDIAN)
  43. # define DRM_FORMAT_HOST_XRGB8888 DRM_FORMAT_BGRX8888
  44. # define DRM_FORMAT_HOST_ARGB8888 DRM_FORMAT_BGRA8888
  45. #else
  46. # define DRM_FORMAT_HOST_XRGB1555 DRM_FORMAT_XRGB1555
  47. # define DRM_FORMAT_HOST_RGB565 DRM_FORMAT_RGB565
  48. # define DRM_FORMAT_HOST_XRGB8888 DRM_FORMAT_XRGB8888
  49. # define DRM_FORMAT_HOST_ARGB8888 DRM_FORMAT_ARGB8888
  50. #endif
  51. struct drm_device;
  52. struct drm_mode_fb_cmd2;
  53. /**
  54. * struct drm_format_info - information about a DRM format
  55. */
  56. struct drm_format_info {
  57. /** @format: 4CC format identifier (DRM_FORMAT_*) */
  58. u32 format;
  59. /**
  60. * @depth:
  61. *
  62. * Color depth (number of bits per pixel excluding padding bits),
  63. * valid for a subset of RGB formats only. This is a legacy field, do
  64. * not use in new code and set to 0 for new formats.
  65. */
  66. u8 depth;
  67. /** @num_planes: Number of color planes (1 to 3) */
  68. u8 num_planes;
  69. union {
  70. /**
  71. * @cpp:
  72. *
  73. * Number of bytes per pixel (per plane), this is aliased with
  74. * @char_per_block. It is deprecated in favour of using the
  75. * triplet @char_per_block, @block_w, @block_h for better
  76. * describing the pixel format.
  77. */
  78. u8 cpp[DRM_FORMAT_MAX_PLANES];
  79. /**
  80. * @char_per_block:
  81. *
  82. * Number of bytes per block (per plane), where blocks are
  83. * defined as a rectangle of pixels which are stored next to
  84. * each other in a byte aligned memory region. Together with
  85. * @block_w and @block_h this is used to properly describe tiles
  86. * in tiled formats or to describe groups of pixels in packed
  87. * formats for which the memory needed for a single pixel is not
  88. * byte aligned.
  89. *
  90. * @cpp has been kept for historical reasons because there are
  91. * a lot of places in drivers where it's used. In drm core for
  92. * generic code paths the preferred way is to use
  93. * @char_per_block, drm_format_info_block_width() and
  94. * drm_format_info_block_height() which allows handling both
  95. * block and non-block formats in the same way.
  96. *
  97. * For formats that are intended to be used only with non-linear
  98. * modifiers both @cpp and @char_per_block must be 0 in the
  99. * generic format table. Drivers could supply accurate
  100. * information from their drm_mode_config.get_format_info hook
  101. * if they want the core to be validating the pitch.
  102. */
  103. u8 char_per_block[DRM_FORMAT_MAX_PLANES];
  104. };
  105. /**
  106. * @block_w:
  107. *
  108. * Block width in pixels, this is intended to be accessed through
  109. * drm_format_info_block_width()
  110. */
  111. u8 block_w[DRM_FORMAT_MAX_PLANES];
  112. /**
  113. * @block_h:
  114. *
  115. * Block height in pixels, this is intended to be accessed through
  116. * drm_format_info_block_height()
  117. */
  118. u8 block_h[DRM_FORMAT_MAX_PLANES];
  119. /** @hsub: Horizontal chroma subsampling factor */
  120. u8 hsub;
  121. /** @vsub: Vertical chroma subsampling factor */
  122. u8 vsub;
  123. /** @has_alpha: Does the format embeds an alpha component? */
  124. bool has_alpha;
  125. /** @is_yuv: Is it a YUV format? */
  126. bool is_yuv;
  127. /** @is_color_indexed: Is it a color-indexed format? */
  128. bool is_color_indexed;
  129. };
  130. /**
  131. * drm_format_info_is_yuv_packed - check that the format info matches a YUV
  132. * format with data laid in a single plane
  133. * @info: format info
  134. *
  135. * Returns:
  136. * A boolean indicating whether the format info matches a packed YUV format.
  137. */
  138. static inline bool
  139. drm_format_info_is_yuv_packed(const struct drm_format_info *info)
  140. {
  141. return info->is_yuv && info->num_planes == 1;
  142. }
  143. /**
  144. * drm_format_info_is_yuv_semiplanar - check that the format info matches a YUV
  145. * format with data laid in two planes (luminance and chrominance)
  146. * @info: format info
  147. *
  148. * Returns:
  149. * A boolean indicating whether the format info matches a semiplanar YUV format.
  150. */
  151. static inline bool
  152. drm_format_info_is_yuv_semiplanar(const struct drm_format_info *info)
  153. {
  154. return info->is_yuv && info->num_planes == 2;
  155. }
  156. /**
  157. * drm_format_info_is_yuv_planar - check that the format info matches a YUV
  158. * format with data laid in three planes (one for each YUV component)
  159. * @info: format info
  160. *
  161. * Returns:
  162. * A boolean indicating whether the format info matches a planar YUV format.
  163. */
  164. static inline bool
  165. drm_format_info_is_yuv_planar(const struct drm_format_info *info)
  166. {
  167. return info->is_yuv && info->num_planes == 3;
  168. }
  169. /**
  170. * drm_format_info_is_yuv_sampling_410 - check that the format info matches a
  171. * YUV format with 4:1:0 sub-sampling
  172. * @info: format info
  173. *
  174. * Returns:
  175. * A boolean indicating whether the format info matches a YUV format with 4:1:0
  176. * sub-sampling.
  177. */
  178. static inline bool
  179. drm_format_info_is_yuv_sampling_410(const struct drm_format_info *info)
  180. {
  181. return info->is_yuv && info->hsub == 4 && info->vsub == 4;
  182. }
  183. /**
  184. * drm_format_info_is_yuv_sampling_411 - check that the format info matches a
  185. * YUV format with 4:1:1 sub-sampling
  186. * @info: format info
  187. *
  188. * Returns:
  189. * A boolean indicating whether the format info matches a YUV format with 4:1:1
  190. * sub-sampling.
  191. */
  192. static inline bool
  193. drm_format_info_is_yuv_sampling_411(const struct drm_format_info *info)
  194. {
  195. return info->is_yuv && info->hsub == 4 && info->vsub == 1;
  196. }
  197. /**
  198. * drm_format_info_is_yuv_sampling_420 - check that the format info matches a
  199. * YUV format with 4:2:0 sub-sampling
  200. * @info: format info
  201. *
  202. * Returns:
  203. * A boolean indicating whether the format info matches a YUV format with 4:2:0
  204. * sub-sampling.
  205. */
  206. static inline bool
  207. drm_format_info_is_yuv_sampling_420(const struct drm_format_info *info)
  208. {
  209. return info->is_yuv && info->hsub == 2 && info->vsub == 2;
  210. }
  211. /**
  212. * drm_format_info_is_yuv_sampling_422 - check that the format info matches a
  213. * YUV format with 4:2:2 sub-sampling
  214. * @info: format info
  215. *
  216. * Returns:
  217. * A boolean indicating whether the format info matches a YUV format with 4:2:2
  218. * sub-sampling.
  219. */
  220. static inline bool
  221. drm_format_info_is_yuv_sampling_422(const struct drm_format_info *info)
  222. {
  223. return info->is_yuv && info->hsub == 2 && info->vsub == 1;
  224. }
  225. /**
  226. * drm_format_info_is_yuv_sampling_444 - check that the format info matches a
  227. * YUV format with 4:4:4 sub-sampling
  228. * @info: format info
  229. *
  230. * Returns:
  231. * A boolean indicating whether the format info matches a YUV format with 4:4:4
  232. * sub-sampling.
  233. */
  234. static inline bool
  235. drm_format_info_is_yuv_sampling_444(const struct drm_format_info *info)
  236. {
  237. return info->is_yuv && info->hsub == 1 && info->vsub == 1;
  238. }
  239. /**
  240. * drm_format_info_plane_width - width of the plane given the first plane
  241. * @info: pixel format info
  242. * @width: width of the first plane
  243. * @plane: plane index
  244. *
  245. * Returns:
  246. * The width of @plane, given that the width of the first plane is @width.
  247. */
  248. static inline
  249. int drm_format_info_plane_width(const struct drm_format_info *info, int width,
  250. int plane)
  251. {
  252. if (!info || plane >= info->num_planes)
  253. return 0;
  254. if (plane == 0)
  255. return width;
  256. return width / info->hsub;
  257. }
  258. /**
  259. * drm_format_info_plane_height - height of the plane given the first plane
  260. * @info: pixel format info
  261. * @height: height of the first plane
  262. * @plane: plane index
  263. *
  264. * Returns:
  265. * The height of @plane, given that the height of the first plane is @height.
  266. */
  267. static inline
  268. int drm_format_info_plane_height(const struct drm_format_info *info, int height,
  269. int plane)
  270. {
  271. if (!info || plane >= info->num_planes)
  272. return 0;
  273. if (plane == 0)
  274. return height;
  275. return height / info->vsub;
  276. }
  277. const struct drm_format_info *__drm_format_info(u32 format);
  278. const struct drm_format_info *drm_format_info(u32 format);
  279. const struct drm_format_info *
  280. drm_get_format_info(struct drm_device *dev,
  281. const struct drm_mode_fb_cmd2 *mode_cmd);
  282. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth);
  283. uint32_t drm_driver_legacy_fb_format(struct drm_device *dev,
  284. uint32_t bpp, uint32_t depth);
  285. unsigned int drm_format_info_block_width(const struct drm_format_info *info,
  286. int plane);
  287. unsigned int drm_format_info_block_height(const struct drm_format_info *info,
  288. int plane);
  289. unsigned int drm_format_info_bpp(const struct drm_format_info *info, int plane);
  290. uint64_t drm_format_info_min_pitch(const struct drm_format_info *info,
  291. int plane, unsigned int buffer_width);
  292. #endif /* __DRM_FOURCC_H__ */