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