1ae4df11aSLaurent Pinchart /* 2ae4df11aSLaurent Pinchart * Copyright (c) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com> 3ae4df11aSLaurent Pinchart * 4ae4df11aSLaurent Pinchart * Permission to use, copy, modify, distribute, and sell this software and its 5ae4df11aSLaurent Pinchart * documentation for any purpose is hereby granted without fee, provided that 6ae4df11aSLaurent Pinchart * the above copyright notice appear in all copies and that both that copyright 7ae4df11aSLaurent Pinchart * notice and this permission notice appear in supporting documentation, and 8ae4df11aSLaurent Pinchart * that the name of the copyright holders not be used in advertising or 9ae4df11aSLaurent Pinchart * publicity pertaining to distribution of the software without specific, 10ae4df11aSLaurent Pinchart * written prior permission. The copyright holders make no representations 11ae4df11aSLaurent Pinchart * about the suitability of this software for any purpose. It is provided "as 12ae4df11aSLaurent Pinchart * is" without express or implied warranty. 13ae4df11aSLaurent Pinchart * 14ae4df11aSLaurent Pinchart * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 15ae4df11aSLaurent Pinchart * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 16ae4df11aSLaurent Pinchart * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 17ae4df11aSLaurent Pinchart * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 18ae4df11aSLaurent Pinchart * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 19ae4df11aSLaurent Pinchart * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 20ae4df11aSLaurent Pinchart * OF THIS SOFTWARE. 21ae4df11aSLaurent Pinchart */ 22ae4df11aSLaurent Pinchart #ifndef __DRM_FOURCC_H__ 23ae4df11aSLaurent Pinchart #define __DRM_FOURCC_H__ 24ae4df11aSLaurent Pinchart 25ae4df11aSLaurent Pinchart #include <linux/types.h> 26ae4df11aSLaurent Pinchart #include <uapi/drm/drm_fourcc.h> 27ae4df11aSLaurent Pinchart 28279cc2e9SThomas Zimmermann /** 29279cc2e9SThomas Zimmermann * DRM_FORMAT_MAX_PLANES - maximum number of planes a DRM format can have 30279cc2e9SThomas Zimmermann */ 31279cc2e9SThomas Zimmermann #define DRM_FORMAT_MAX_PLANES 4u 32279cc2e9SThomas Zimmermann 33ec2fae25SGerd Hoffmann /* 34ec2fae25SGerd Hoffmann * DRM formats are little endian. Define host endian variants for the 35ec2fae25SGerd Hoffmann * most common formats here, to reduce the #ifdefs needed in drivers. 36ec2fae25SGerd Hoffmann * 37ec2fae25SGerd Hoffmann * Note that the DRM_FORMAT_BIG_ENDIAN flag should only be used in 38ec2fae25SGerd Hoffmann * case the format can't be specified otherwise, so we don't end up 39ec2fae25SGerd Hoffmann * with two values describing the same format. 40ec2fae25SGerd Hoffmann */ 41ec2fae25SGerd Hoffmann #ifdef __BIG_ENDIAN 42ec2fae25SGerd Hoffmann # define DRM_FORMAT_HOST_XRGB1555 (DRM_FORMAT_XRGB1555 | \ 43ec2fae25SGerd Hoffmann DRM_FORMAT_BIG_ENDIAN) 44ec2fae25SGerd Hoffmann # define DRM_FORMAT_HOST_RGB565 (DRM_FORMAT_RGB565 | \ 45ec2fae25SGerd Hoffmann DRM_FORMAT_BIG_ENDIAN) 46ec2fae25SGerd Hoffmann # define DRM_FORMAT_HOST_XRGB8888 DRM_FORMAT_BGRX8888 47ec2fae25SGerd Hoffmann # define DRM_FORMAT_HOST_ARGB8888 DRM_FORMAT_BGRA8888 48ec2fae25SGerd Hoffmann #else 49ec2fae25SGerd Hoffmann # define DRM_FORMAT_HOST_XRGB1555 DRM_FORMAT_XRGB1555 50ec2fae25SGerd Hoffmann # define DRM_FORMAT_HOST_RGB565 DRM_FORMAT_RGB565 51ec2fae25SGerd Hoffmann # define DRM_FORMAT_HOST_XRGB8888 DRM_FORMAT_XRGB8888 52ec2fae25SGerd Hoffmann # define DRM_FORMAT_HOST_ARGB8888 DRM_FORMAT_ARGB8888 53ec2fae25SGerd Hoffmann #endif 54ec2fae25SGerd Hoffmann 556a0f9ebfSVille Syrjälä struct drm_device; 566a0f9ebfSVille Syrjälä struct drm_mode_fb_cmd2; 576a0f9ebfSVille Syrjälä 5884770cc2SLaurent Pinchart /** 5984770cc2SLaurent Pinchart * struct drm_format_info - information about a DRM format 6084770cc2SLaurent Pinchart */ 6184770cc2SLaurent Pinchart struct drm_format_info { 629c71a668SAlexandru Gheorghe /** @format: 4CC format identifier (DRM_FORMAT_*) */ 6384770cc2SLaurent Pinchart u32 format; 649c71a668SAlexandru Gheorghe 659c71a668SAlexandru Gheorghe /** 669c71a668SAlexandru Gheorghe * @depth: 679c71a668SAlexandru Gheorghe * 689c71a668SAlexandru Gheorghe * Color depth (number of bits per pixel excluding padding bits), 699c71a668SAlexandru Gheorghe * valid for a subset of RGB formats only. This is a legacy field, do 709c71a668SAlexandru Gheorghe * not use in new code and set to 0 for new formats. 719c71a668SAlexandru Gheorghe */ 7284770cc2SLaurent Pinchart u8 depth; 739c71a668SAlexandru Gheorghe 749c71a668SAlexandru Gheorghe /** @num_planes: Number of color planes (1 to 3) */ 7584770cc2SLaurent Pinchart u8 num_planes; 769c71a668SAlexandru Gheorghe 77042bf753SAlexandru Gheorghe union { 78042bf753SAlexandru Gheorghe /** 79042bf753SAlexandru Gheorghe * @cpp: 80042bf753SAlexandru Gheorghe * 81042bf753SAlexandru Gheorghe * Number of bytes per pixel (per plane), this is aliased with 82042bf753SAlexandru Gheorghe * @char_per_block. It is deprecated in favour of using the 83042bf753SAlexandru Gheorghe * triplet @char_per_block, @block_w, @block_h for better 84042bf753SAlexandru Gheorghe * describing the pixel format. 85042bf753SAlexandru Gheorghe */ 86279cc2e9SThomas Zimmermann u8 cpp[DRM_FORMAT_MAX_PLANES]; 879c71a668SAlexandru Gheorghe 88042bf753SAlexandru Gheorghe /** 89042bf753SAlexandru Gheorghe * @char_per_block: 90042bf753SAlexandru Gheorghe * 91042bf753SAlexandru Gheorghe * Number of bytes per block (per plane), where blocks are 92042bf753SAlexandru Gheorghe * defined as a rectangle of pixels which are stored next to 93042bf753SAlexandru Gheorghe * each other in a byte aligned memory region. Together with 94042bf753SAlexandru Gheorghe * @block_w and @block_h this is used to properly describe tiles 95042bf753SAlexandru Gheorghe * in tiled formats or to describe groups of pixels in packed 96042bf753SAlexandru Gheorghe * formats for which the memory needed for a single pixel is not 97042bf753SAlexandru Gheorghe * byte aligned. 98042bf753SAlexandru Gheorghe * 99042bf753SAlexandru Gheorghe * @cpp has been kept for historical reasons because there are 100042bf753SAlexandru Gheorghe * a lot of places in drivers where it's used. In drm core for 101042bf753SAlexandru Gheorghe * generic code paths the preferred way is to use 102042bf753SAlexandru Gheorghe * @char_per_block, drm_format_info_block_width() and 103042bf753SAlexandru Gheorghe * drm_format_info_block_height() which allows handling both 104042bf753SAlexandru Gheorghe * block and non-block formats in the same way. 105042bf753SAlexandru Gheorghe * 106042bf753SAlexandru Gheorghe * For formats that are intended to be used only with non-linear 107042bf753SAlexandru Gheorghe * modifiers both @cpp and @char_per_block must be 0 in the 108042bf753SAlexandru Gheorghe * generic format table. Drivers could supply accurate 109042bf753SAlexandru Gheorghe * information from their drm_mode_config.get_format_info hook 110042bf753SAlexandru Gheorghe * if they want the core to be validating the pitch. 111042bf753SAlexandru Gheorghe */ 112279cc2e9SThomas Zimmermann u8 char_per_block[DRM_FORMAT_MAX_PLANES]; 113042bf753SAlexandru Gheorghe }; 114042bf753SAlexandru Gheorghe 115042bf753SAlexandru Gheorghe /** 116042bf753SAlexandru Gheorghe * @block_w: 117042bf753SAlexandru Gheorghe * 118042bf753SAlexandru Gheorghe * Block width in pixels, this is intended to be accessed through 119042bf753SAlexandru Gheorghe * drm_format_info_block_width() 120042bf753SAlexandru Gheorghe */ 121279cc2e9SThomas Zimmermann u8 block_w[DRM_FORMAT_MAX_PLANES]; 122042bf753SAlexandru Gheorghe 123042bf753SAlexandru Gheorghe /** 124042bf753SAlexandru Gheorghe * @block_h: 125042bf753SAlexandru Gheorghe * 126042bf753SAlexandru Gheorghe * Block height in pixels, this is intended to be accessed through 127042bf753SAlexandru Gheorghe * drm_format_info_block_height() 128042bf753SAlexandru Gheorghe */ 129279cc2e9SThomas Zimmermann u8 block_h[DRM_FORMAT_MAX_PLANES]; 130042bf753SAlexandru Gheorghe 1319c71a668SAlexandru Gheorghe /** @hsub: Horizontal chroma subsampling factor */ 13284770cc2SLaurent Pinchart u8 hsub; 1339c71a668SAlexandru Gheorghe /** @vsub: Vertical chroma subsampling factor */ 13484770cc2SLaurent Pinchart u8 vsub; 1359c71a668SAlexandru Gheorghe 1369c71a668SAlexandru Gheorghe /** @has_alpha: Does the format embeds an alpha component? */ 1374cc4e1b4SMaxime Ripard bool has_alpha; 1389c71a668SAlexandru Gheorghe 1399c71a668SAlexandru Gheorghe /** @is_yuv: Is it a YUV format? */ 140ce2d5461SAyan Kumar Halder bool is_yuv; 14184770cc2SLaurent Pinchart }; 14284770cc2SLaurent Pinchart 143b3c11ac2SEric Engestrom /** 14441c8c210SPaul Kocialkowski * drm_format_info_is_yuv_packed - check that the format info matches a YUV 14541c8c210SPaul Kocialkowski * format with data laid in a single plane 14641c8c210SPaul Kocialkowski * @info: format info 14741c8c210SPaul Kocialkowski * 14841c8c210SPaul Kocialkowski * Returns: 14941c8c210SPaul Kocialkowski * A boolean indicating whether the format info matches a packed YUV format. 15041c8c210SPaul Kocialkowski */ 15141c8c210SPaul Kocialkowski static inline bool 15241c8c210SPaul Kocialkowski drm_format_info_is_yuv_packed(const struct drm_format_info *info) 15341c8c210SPaul Kocialkowski { 15441c8c210SPaul Kocialkowski return info->is_yuv && info->num_planes == 1; 15541c8c210SPaul Kocialkowski } 15641c8c210SPaul Kocialkowski 15741c8c210SPaul Kocialkowski /** 15841c8c210SPaul Kocialkowski * drm_format_info_is_yuv_semiplanar - check that the format info matches a YUV 15941c8c210SPaul Kocialkowski * format with data laid in two planes (luminance and chrominance) 16041c8c210SPaul Kocialkowski * @info: format info 16141c8c210SPaul Kocialkowski * 16241c8c210SPaul Kocialkowski * Returns: 16341c8c210SPaul Kocialkowski * A boolean indicating whether the format info matches a semiplanar YUV format. 16441c8c210SPaul Kocialkowski */ 16541c8c210SPaul Kocialkowski static inline bool 16641c8c210SPaul Kocialkowski drm_format_info_is_yuv_semiplanar(const struct drm_format_info *info) 16741c8c210SPaul Kocialkowski { 16841c8c210SPaul Kocialkowski return info->is_yuv && info->num_planes == 2; 16941c8c210SPaul Kocialkowski } 17041c8c210SPaul Kocialkowski 17141c8c210SPaul Kocialkowski /** 17241c8c210SPaul Kocialkowski * drm_format_info_is_yuv_planar - check that the format info matches a YUV 17341c8c210SPaul Kocialkowski * format with data laid in three planes (one for each YUV component) 17441c8c210SPaul Kocialkowski * @info: format info 17541c8c210SPaul Kocialkowski * 17641c8c210SPaul Kocialkowski * Returns: 17741c8c210SPaul Kocialkowski * A boolean indicating whether the format info matches a planar YUV format. 17841c8c210SPaul Kocialkowski */ 17941c8c210SPaul Kocialkowski static inline bool 18041c8c210SPaul Kocialkowski drm_format_info_is_yuv_planar(const struct drm_format_info *info) 18141c8c210SPaul Kocialkowski { 18241c8c210SPaul Kocialkowski return info->is_yuv && info->num_planes == 3; 18341c8c210SPaul Kocialkowski } 18441c8c210SPaul Kocialkowski 185a211e56eSPaul Kocialkowski /** 186a211e56eSPaul Kocialkowski * drm_format_info_is_yuv_sampling_410 - check that the format info matches a 187a211e56eSPaul Kocialkowski * YUV format with 4:1:0 sub-sampling 188a211e56eSPaul Kocialkowski * @info: format info 189a211e56eSPaul Kocialkowski * 190a211e56eSPaul Kocialkowski * Returns: 191a211e56eSPaul Kocialkowski * A boolean indicating whether the format info matches a YUV format with 4:1:0 192a211e56eSPaul Kocialkowski * sub-sampling. 193a211e56eSPaul Kocialkowski */ 194a211e56eSPaul Kocialkowski static inline bool 195a211e56eSPaul Kocialkowski drm_format_info_is_yuv_sampling_410(const struct drm_format_info *info) 196a211e56eSPaul Kocialkowski { 197a211e56eSPaul Kocialkowski return info->is_yuv && info->hsub == 4 && info->vsub == 4; 198a211e56eSPaul Kocialkowski } 199a211e56eSPaul Kocialkowski 200a211e56eSPaul Kocialkowski /** 201a211e56eSPaul Kocialkowski * drm_format_info_is_yuv_sampling_411 - check that the format info matches a 202a211e56eSPaul Kocialkowski * YUV format with 4:1:1 sub-sampling 203a211e56eSPaul Kocialkowski * @info: format info 204a211e56eSPaul Kocialkowski * 205a211e56eSPaul Kocialkowski * Returns: 206a211e56eSPaul Kocialkowski * A boolean indicating whether the format info matches a YUV format with 4:1:1 207a211e56eSPaul Kocialkowski * sub-sampling. 208a211e56eSPaul Kocialkowski */ 209a211e56eSPaul Kocialkowski static inline bool 210a211e56eSPaul Kocialkowski drm_format_info_is_yuv_sampling_411(const struct drm_format_info *info) 211a211e56eSPaul Kocialkowski { 212a211e56eSPaul Kocialkowski return info->is_yuv && info->hsub == 4 && info->vsub == 1; 213a211e56eSPaul Kocialkowski } 214a211e56eSPaul Kocialkowski 215a211e56eSPaul Kocialkowski /** 216a211e56eSPaul Kocialkowski * drm_format_info_is_yuv_sampling_420 - check that the format info matches a 217a211e56eSPaul Kocialkowski * YUV format with 4:2:0 sub-sampling 218a211e56eSPaul Kocialkowski * @info: format info 219a211e56eSPaul Kocialkowski * 220a211e56eSPaul Kocialkowski * Returns: 221a211e56eSPaul Kocialkowski * A boolean indicating whether the format info matches a YUV format with 4:2:0 222a211e56eSPaul Kocialkowski * sub-sampling. 223a211e56eSPaul Kocialkowski */ 224a211e56eSPaul Kocialkowski static inline bool 225a211e56eSPaul Kocialkowski drm_format_info_is_yuv_sampling_420(const struct drm_format_info *info) 226a211e56eSPaul Kocialkowski { 227a211e56eSPaul Kocialkowski return info->is_yuv && info->hsub == 2 && info->vsub == 2; 228a211e56eSPaul Kocialkowski } 229a211e56eSPaul Kocialkowski 230a211e56eSPaul Kocialkowski /** 231a211e56eSPaul Kocialkowski * drm_format_info_is_yuv_sampling_422 - check that the format info matches a 232a211e56eSPaul Kocialkowski * YUV format with 4:2:2 sub-sampling 233a211e56eSPaul Kocialkowski * @info: format info 234a211e56eSPaul Kocialkowski * 235a211e56eSPaul Kocialkowski * Returns: 236a211e56eSPaul Kocialkowski * A boolean indicating whether the format info matches a YUV format with 4:2:2 237a211e56eSPaul Kocialkowski * sub-sampling. 238a211e56eSPaul Kocialkowski */ 239a211e56eSPaul Kocialkowski static inline bool 240a211e56eSPaul Kocialkowski drm_format_info_is_yuv_sampling_422(const struct drm_format_info *info) 241a211e56eSPaul Kocialkowski { 242a211e56eSPaul Kocialkowski return info->is_yuv && info->hsub == 2 && info->vsub == 1; 243a211e56eSPaul Kocialkowski } 244a211e56eSPaul Kocialkowski 245a211e56eSPaul Kocialkowski /** 246a211e56eSPaul Kocialkowski * drm_format_info_is_yuv_sampling_444 - check that the format info matches a 247a211e56eSPaul Kocialkowski * YUV format with 4:4:4 sub-sampling 248a211e56eSPaul Kocialkowski * @info: format info 249a211e56eSPaul Kocialkowski * 250a211e56eSPaul Kocialkowski * Returns: 251a211e56eSPaul Kocialkowski * A boolean indicating whether the format info matches a YUV format with 4:4:4 252a211e56eSPaul Kocialkowski * sub-sampling. 253a211e56eSPaul Kocialkowski */ 254a211e56eSPaul Kocialkowski static inline bool 255a211e56eSPaul Kocialkowski drm_format_info_is_yuv_sampling_444(const struct drm_format_info *info) 256a211e56eSPaul Kocialkowski { 257a211e56eSPaul Kocialkowski return info->is_yuv && info->hsub == 1 && info->vsub == 1; 258a211e56eSPaul Kocialkowski } 259a211e56eSPaul Kocialkowski 26024c478eaSMaxime Ripard /** 261bf39607cSMaxime Ripard * drm_format_info_plane_width - width of the plane given the first plane 2627793a108SMaxime Ripard * @info: pixel format info 263bf39607cSMaxime Ripard * @width: width of the first plane 264bf39607cSMaxime Ripard * @plane: plane index 265bf39607cSMaxime Ripard * 266bf39607cSMaxime Ripard * Returns: 267bf39607cSMaxime Ripard * The width of @plane, given that the width of the first plane is @width. 268bf39607cSMaxime Ripard */ 269bf39607cSMaxime Ripard static inline 270bf39607cSMaxime Ripard int drm_format_info_plane_width(const struct drm_format_info *info, int width, 271bf39607cSMaxime Ripard int plane) 272bf39607cSMaxime Ripard { 273bf39607cSMaxime Ripard if (!info || plane >= info->num_planes) 274bf39607cSMaxime Ripard return 0; 275bf39607cSMaxime Ripard 276bf39607cSMaxime Ripard if (plane == 0) 277bf39607cSMaxime Ripard return width; 278bf39607cSMaxime Ripard 279bf39607cSMaxime Ripard return width / info->hsub; 280bf39607cSMaxime Ripard } 281bf39607cSMaxime Ripard 282bf39607cSMaxime Ripard /** 283bf39607cSMaxime Ripard * drm_format_info_plane_height - height of the plane given the first plane 2847793a108SMaxime Ripard * @info: pixel format info 285bf39607cSMaxime Ripard * @height: height of the first plane 286bf39607cSMaxime Ripard * @plane: plane index 287bf39607cSMaxime Ripard * 288bf39607cSMaxime Ripard * Returns: 289bf39607cSMaxime Ripard * The height of @plane, given that the height of the first plane is @height. 290bf39607cSMaxime Ripard */ 291bf39607cSMaxime Ripard static inline 292bf39607cSMaxime Ripard int drm_format_info_plane_height(const struct drm_format_info *info, int height, 293bf39607cSMaxime Ripard int plane) 294bf39607cSMaxime Ripard { 295bf39607cSMaxime Ripard if (!info || plane >= info->num_planes) 296bf39607cSMaxime Ripard return 0; 297bf39607cSMaxime Ripard 298bf39607cSMaxime Ripard if (plane == 0) 299bf39607cSMaxime Ripard return height; 300bf39607cSMaxime Ripard 301bf39607cSMaxime Ripard return height / info->vsub; 302bf39607cSMaxime Ripard } 303bf39607cSMaxime Ripard 304333d2da5SLaurent Pinchart const struct drm_format_info *__drm_format_info(u32 format); 30584770cc2SLaurent Pinchart const struct drm_format_info *drm_format_info(u32 format); 3066a0f9ebfSVille Syrjälä const struct drm_format_info * 3076a0f9ebfSVille Syrjälä drm_get_format_info(struct drm_device *dev, 3086a0f9ebfSVille Syrjälä const struct drm_mode_fb_cmd2 *mode_cmd); 309ec5e3047SDaniel Vetter uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth); 310059b5eb5SGerd Hoffmann uint32_t drm_driver_legacy_fb_format(struct drm_device *dev, 311059b5eb5SGerd Hoffmann uint32_t bpp, uint32_t depth); 312042bf753SAlexandru Gheorghe unsigned int drm_format_info_block_width(const struct drm_format_info *info, 313042bf753SAlexandru Gheorghe int plane); 314042bf753SAlexandru Gheorghe unsigned int drm_format_info_block_height(const struct drm_format_info *info, 315042bf753SAlexandru Gheorghe int plane); 316*96dc635dSGeert Uytterhoeven unsigned int drm_format_info_bpp(const struct drm_format_info *info, int plane); 317042bf753SAlexandru Gheorghe uint64_t drm_format_info_min_pitch(const struct drm_format_info *info, 318042bf753SAlexandru Gheorghe int plane, unsigned int buffer_width); 319ae4df11aSLaurent Pinchart 320ae4df11aSLaurent Pinchart #endif /* __DRM_FOURCC_H__ */ 321