drm_fourcc.h 10.0 KB

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