/*1* Copyright (c) 2016 Laurent Pinchart <[email protected]>2*3* Permission to use, copy, modify, distribute, and sell this software and its4* documentation for any purpose is hereby granted without fee, provided that5* the above copyright notice appear in all copies and that both that copyright6* notice and this permission notice appear in supporting documentation, and7* that the name of the copyright holders not be used in advertising or8* publicity pertaining to distribution of the software without specific,9* written prior permission. The copyright holders make no representations10* about the suitability of this software for any purpose. It is provided "as11* is" without express or implied warranty.12*13* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,14* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO15* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR16* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,17* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER18* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE19* OF THIS SOFTWARE.20*/21#ifndef __DRM_FOURCC_H__22#define __DRM_FOURCC_H__2324#include <linux/math.h>25#include <linux/types.h>26#include <uapi/drm/drm_fourcc.h>2728/**29* DRM_FORMAT_MAX_PLANES - maximum number of planes a DRM format can have30*/31#define DRM_FORMAT_MAX_PLANES 4u3233/*34* DRM formats are little endian. Define host endian variants for the35* most common formats here, to reduce the #ifdefs needed in drivers.36*37* Note that the DRM_FORMAT_BIG_ENDIAN flag should only be used in38* case the format can't be specified otherwise, so we don't end up39* with two values describing the same format.40*/41#ifdef __BIG_ENDIAN42# define DRM_FORMAT_HOST_XRGB1555 (DRM_FORMAT_XRGB1555 | \43DRM_FORMAT_BIG_ENDIAN)44# define DRM_FORMAT_HOST_RGB565 (DRM_FORMAT_RGB565 | \45DRM_FORMAT_BIG_ENDIAN)46# define DRM_FORMAT_HOST_XRGB8888 DRM_FORMAT_BGRX888847# define DRM_FORMAT_HOST_ARGB8888 DRM_FORMAT_BGRA888848#else49# define DRM_FORMAT_HOST_XRGB1555 DRM_FORMAT_XRGB155550# define DRM_FORMAT_HOST_RGB565 DRM_FORMAT_RGB56551# define DRM_FORMAT_HOST_XRGB8888 DRM_FORMAT_XRGB888852# define DRM_FORMAT_HOST_ARGB8888 DRM_FORMAT_ARGB888853#endif5455struct drm_device;5657/**58* struct drm_format_info - information about a DRM format59*/60struct drm_format_info {61/** @format: 4CC format identifier (DRM_FORMAT_*) */62u32 format;6364/**65* @depth:66*67* Color depth (number of bits per pixel excluding padding bits),68* valid for a subset of RGB formats only. This is a legacy field, do69* not use in new code and set to 0 for new formats.70*/71u8 depth;7273/** @num_planes: Number of color planes (1 to 3) */74u8 num_planes;7576union {77/**78* @cpp:79*80* Number of bytes per pixel (per plane), this is aliased with81* @char_per_block. It is deprecated in favour of using the82* triplet @char_per_block, @block_w, @block_h for better83* describing the pixel format.84*/85u8 cpp[DRM_FORMAT_MAX_PLANES];8687/**88* @char_per_block:89*90* Number of bytes per block (per plane), where blocks are91* defined as a rectangle of pixels which are stored next to92* each other in a byte aligned memory region. Together with93* @block_w and @block_h this is used to properly describe tiles94* in tiled formats or to describe groups of pixels in packed95* formats for which the memory needed for a single pixel is not96* byte aligned.97*98* @cpp has been kept for historical reasons because there are99* a lot of places in drivers where it's used. In drm core for100* generic code paths the preferred way is to use101* @char_per_block, drm_format_info_block_width() and102* drm_format_info_block_height() which allows handling both103* block and non-block formats in the same way.104*105* For formats that are intended to be used only with non-linear106* modifiers both @cpp and @char_per_block must be 0 in the107* generic format table. Drivers could supply accurate108* information from their drm_mode_config.get_format_info hook109* if they want the core to be validating the pitch.110*/111u8 char_per_block[DRM_FORMAT_MAX_PLANES];112};113114/**115* @block_w:116*117* Block width in pixels, this is intended to be accessed through118* drm_format_info_block_width()119*/120u8 block_w[DRM_FORMAT_MAX_PLANES];121122/**123* @block_h:124*125* Block height in pixels, this is intended to be accessed through126* drm_format_info_block_height()127*/128u8 block_h[DRM_FORMAT_MAX_PLANES];129130/** @hsub: Horizontal chroma subsampling factor */131u8 hsub;132/** @vsub: Vertical chroma subsampling factor */133u8 vsub;134135/** @has_alpha: Does the format embeds an alpha component? */136bool has_alpha;137138/** @is_yuv: Is it a YUV format? */139bool is_yuv;140141/** @is_color_indexed: Is it a color-indexed format? */142bool is_color_indexed;143};144145/**146* drm_format_info_is_yuv_packed - check that the format info matches a YUV147* format with data laid in a single plane148* @info: format info149*150* Returns:151* A boolean indicating whether the format info matches a packed YUV format.152*/153static inline bool154drm_format_info_is_yuv_packed(const struct drm_format_info *info)155{156return info->is_yuv && info->num_planes == 1;157}158159/**160* drm_format_info_is_yuv_semiplanar - check that the format info matches a YUV161* format with data laid in two planes (luminance and chrominance)162* @info: format info163*164* Returns:165* A boolean indicating whether the format info matches a semiplanar YUV format.166*/167static inline bool168drm_format_info_is_yuv_semiplanar(const struct drm_format_info *info)169{170return info->is_yuv && info->num_planes == 2;171}172173/**174* drm_format_info_is_yuv_planar - check that the format info matches a YUV175* format with data laid in three planes (one for each YUV component)176* @info: format info177*178* Returns:179* A boolean indicating whether the format info matches a planar YUV format.180*/181static inline bool182drm_format_info_is_yuv_planar(const struct drm_format_info *info)183{184return info->is_yuv && info->num_planes == 3;185}186187/**188* drm_format_info_is_yuv_sampling_410 - check that the format info matches a189* YUV format with 4:1:0 sub-sampling190* @info: format info191*192* Returns:193* A boolean indicating whether the format info matches a YUV format with 4:1:0194* sub-sampling.195*/196static inline bool197drm_format_info_is_yuv_sampling_410(const struct drm_format_info *info)198{199return info->is_yuv && info->hsub == 4 && info->vsub == 4;200}201202/**203* drm_format_info_is_yuv_sampling_411 - check that the format info matches a204* YUV format with 4:1:1 sub-sampling205* @info: format info206*207* Returns:208* A boolean indicating whether the format info matches a YUV format with 4:1:1209* sub-sampling.210*/211static inline bool212drm_format_info_is_yuv_sampling_411(const struct drm_format_info *info)213{214return info->is_yuv && info->hsub == 4 && info->vsub == 1;215}216217/**218* drm_format_info_is_yuv_sampling_420 - check that the format info matches a219* YUV format with 4:2:0 sub-sampling220* @info: format info221*222* Returns:223* A boolean indicating whether the format info matches a YUV format with 4:2:0224* sub-sampling.225*/226static inline bool227drm_format_info_is_yuv_sampling_420(const struct drm_format_info *info)228{229return info->is_yuv && info->hsub == 2 && info->vsub == 2;230}231232/**233* drm_format_info_is_yuv_sampling_422 - check that the format info matches a234* YUV format with 4:2:2 sub-sampling235* @info: format info236*237* Returns:238* A boolean indicating whether the format info matches a YUV format with 4:2:2239* sub-sampling.240*/241static inline bool242drm_format_info_is_yuv_sampling_422(const struct drm_format_info *info)243{244return info->is_yuv && info->hsub == 2 && info->vsub == 1;245}246247/**248* drm_format_info_is_yuv_sampling_444 - check that the format info matches a249* YUV format with 4:4:4 sub-sampling250* @info: format info251*252* Returns:253* A boolean indicating whether the format info matches a YUV format with 4:4:4254* sub-sampling.255*/256static inline bool257drm_format_info_is_yuv_sampling_444(const struct drm_format_info *info)258{259return info->is_yuv && info->hsub == 1 && info->vsub == 1;260}261262/**263* drm_format_info_plane_width - width of the plane given the first plane264* @info: pixel format info265* @width: width of the first plane266* @plane: plane index267*268* Returns:269* The width of @plane, given that the width of the first plane is @width.270*/271static inline272int drm_format_info_plane_width(const struct drm_format_info *info, int width,273int plane)274{275if (!info || plane >= info->num_planes)276return 0;277278if (plane == 0)279return width;280281return DIV_ROUND_UP(width, info->hsub);282}283284/**285* drm_format_info_plane_height - height of the plane given the first plane286* @info: pixel format info287* @height: height of the first plane288* @plane: plane index289*290* Returns:291* The height of @plane, given that the height of the first plane is @height.292*/293static inline294int drm_format_info_plane_height(const struct drm_format_info *info, int height,295int plane)296{297if (!info || plane >= info->num_planes)298return 0;299300if (plane == 0)301return height;302303return DIV_ROUND_UP(height, info->vsub);304}305306const struct drm_format_info *__drm_format_info(u32 format);307const struct drm_format_info *drm_format_info(u32 format);308const struct drm_format_info *309drm_get_format_info(struct drm_device *dev,310u32 pixel_format, u64 modifier);311uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth);312uint32_t drm_driver_legacy_fb_format(struct drm_device *dev,313uint32_t bpp, uint32_t depth);314uint32_t drm_driver_color_mode_format(struct drm_device *dev, unsigned int color_mode);315unsigned int drm_format_info_block_width(const struct drm_format_info *info,316int plane);317unsigned int drm_format_info_block_height(const struct drm_format_info *info,318int plane);319unsigned int drm_format_info_bpp(const struct drm_format_info *info, int plane);320uint64_t drm_format_info_min_pitch(const struct drm_format_info *info,321int plane, unsigned int buffer_width);322323#endif /* __DRM_FOURCC_H__ */324325326