162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission to use, copy, modify, distribute, and sell this software and its
562306a36Sopenharmony_ci * documentation for any purpose is hereby granted without fee, provided that
662306a36Sopenharmony_ci * the above copyright notice appear in all copies and that both that copyright
762306a36Sopenharmony_ci * notice and this permission notice appear in supporting documentation, and
862306a36Sopenharmony_ci * that the name of the copyright holders not be used in advertising or
962306a36Sopenharmony_ci * publicity pertaining to distribution of the software without specific,
1062306a36Sopenharmony_ci * written prior permission.  The copyright holders make no representations
1162306a36Sopenharmony_ci * about the suitability of this software for any purpose.  It is provided "as
1262306a36Sopenharmony_ci * is" without express or implied warranty.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1562306a36Sopenharmony_ci * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1662306a36Sopenharmony_ci * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1762306a36Sopenharmony_ci * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1862306a36Sopenharmony_ci * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1962306a36Sopenharmony_ci * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
2062306a36Sopenharmony_ci * OF THIS SOFTWARE.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_ci#ifndef __DRM_FOURCC_H__
2362306a36Sopenharmony_ci#define __DRM_FOURCC_H__
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include <linux/types.h>
2662306a36Sopenharmony_ci#include <uapi/drm/drm_fourcc.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/**
2962306a36Sopenharmony_ci * DRM_FORMAT_MAX_PLANES - maximum number of planes a DRM format can have
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci#define DRM_FORMAT_MAX_PLANES	4u
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/*
3462306a36Sopenharmony_ci * DRM formats are little endian.  Define host endian variants for the
3562306a36Sopenharmony_ci * most common formats here, to reduce the #ifdefs needed in drivers.
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * Note that the DRM_FORMAT_BIG_ENDIAN flag should only be used in
3862306a36Sopenharmony_ci * case the format can't be specified otherwise, so we don't end up
3962306a36Sopenharmony_ci * with two values describing the same format.
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_ci#ifdef __BIG_ENDIAN
4262306a36Sopenharmony_ci# define DRM_FORMAT_HOST_XRGB1555     (DRM_FORMAT_XRGB1555         |	\
4362306a36Sopenharmony_ci				       DRM_FORMAT_BIG_ENDIAN)
4462306a36Sopenharmony_ci# define DRM_FORMAT_HOST_RGB565       (DRM_FORMAT_RGB565           |	\
4562306a36Sopenharmony_ci				       DRM_FORMAT_BIG_ENDIAN)
4662306a36Sopenharmony_ci# define DRM_FORMAT_HOST_XRGB8888     DRM_FORMAT_BGRX8888
4762306a36Sopenharmony_ci# define DRM_FORMAT_HOST_ARGB8888     DRM_FORMAT_BGRA8888
4862306a36Sopenharmony_ci#else
4962306a36Sopenharmony_ci# define DRM_FORMAT_HOST_XRGB1555     DRM_FORMAT_XRGB1555
5062306a36Sopenharmony_ci# define DRM_FORMAT_HOST_RGB565       DRM_FORMAT_RGB565
5162306a36Sopenharmony_ci# define DRM_FORMAT_HOST_XRGB8888     DRM_FORMAT_XRGB8888
5262306a36Sopenharmony_ci# define DRM_FORMAT_HOST_ARGB8888     DRM_FORMAT_ARGB8888
5362306a36Sopenharmony_ci#endif
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistruct drm_device;
5662306a36Sopenharmony_cistruct drm_mode_fb_cmd2;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/**
5962306a36Sopenharmony_ci * struct drm_format_info - information about a DRM format
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_cistruct drm_format_info {
6262306a36Sopenharmony_ci	/** @format: 4CC format identifier (DRM_FORMAT_*) */
6362306a36Sopenharmony_ci	u32 format;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	/**
6662306a36Sopenharmony_ci	 * @depth:
6762306a36Sopenharmony_ci	 *
6862306a36Sopenharmony_ci	 * Color depth (number of bits per pixel excluding padding bits),
6962306a36Sopenharmony_ci	 * valid for a subset of RGB formats only. This is a legacy field, do
7062306a36Sopenharmony_ci	 * not use in new code and set to 0 for new formats.
7162306a36Sopenharmony_ci	 */
7262306a36Sopenharmony_ci	u8 depth;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	/** @num_planes: Number of color planes (1 to 3) */
7562306a36Sopenharmony_ci	u8 num_planes;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	union {
7862306a36Sopenharmony_ci		/**
7962306a36Sopenharmony_ci		 * @cpp:
8062306a36Sopenharmony_ci		 *
8162306a36Sopenharmony_ci		 * Number of bytes per pixel (per plane), this is aliased with
8262306a36Sopenharmony_ci		 * @char_per_block. It is deprecated in favour of using the
8362306a36Sopenharmony_ci		 * triplet @char_per_block, @block_w, @block_h for better
8462306a36Sopenharmony_ci		 * describing the pixel format.
8562306a36Sopenharmony_ci		 */
8662306a36Sopenharmony_ci		u8 cpp[DRM_FORMAT_MAX_PLANES];
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci		/**
8962306a36Sopenharmony_ci		 * @char_per_block:
9062306a36Sopenharmony_ci		 *
9162306a36Sopenharmony_ci		 * Number of bytes per block (per plane), where blocks are
9262306a36Sopenharmony_ci		 * defined as a rectangle of pixels which are stored next to
9362306a36Sopenharmony_ci		 * each other in a byte aligned memory region. Together with
9462306a36Sopenharmony_ci		 * @block_w and @block_h this is used to properly describe tiles
9562306a36Sopenharmony_ci		 * in tiled formats or to describe groups of pixels in packed
9662306a36Sopenharmony_ci		 * formats for which the memory needed for a single pixel is not
9762306a36Sopenharmony_ci		 * byte aligned.
9862306a36Sopenharmony_ci		 *
9962306a36Sopenharmony_ci		 * @cpp has been kept for historical reasons because there are
10062306a36Sopenharmony_ci		 * a lot of places in drivers where it's used. In drm core for
10162306a36Sopenharmony_ci		 * generic code paths the preferred way is to use
10262306a36Sopenharmony_ci		 * @char_per_block, drm_format_info_block_width() and
10362306a36Sopenharmony_ci		 * drm_format_info_block_height() which allows handling both
10462306a36Sopenharmony_ci		 * block and non-block formats in the same way.
10562306a36Sopenharmony_ci		 *
10662306a36Sopenharmony_ci		 * For formats that are intended to be used only with non-linear
10762306a36Sopenharmony_ci		 * modifiers both @cpp and @char_per_block must be 0 in the
10862306a36Sopenharmony_ci		 * generic format table. Drivers could supply accurate
10962306a36Sopenharmony_ci		 * information from their drm_mode_config.get_format_info hook
11062306a36Sopenharmony_ci		 * if they want the core to be validating the pitch.
11162306a36Sopenharmony_ci		 */
11262306a36Sopenharmony_ci		u8 char_per_block[DRM_FORMAT_MAX_PLANES];
11362306a36Sopenharmony_ci	};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	/**
11662306a36Sopenharmony_ci	 * @block_w:
11762306a36Sopenharmony_ci	 *
11862306a36Sopenharmony_ci	 * Block width in pixels, this is intended to be accessed through
11962306a36Sopenharmony_ci	 * drm_format_info_block_width()
12062306a36Sopenharmony_ci	 */
12162306a36Sopenharmony_ci	u8 block_w[DRM_FORMAT_MAX_PLANES];
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/**
12462306a36Sopenharmony_ci	 * @block_h:
12562306a36Sopenharmony_ci	 *
12662306a36Sopenharmony_ci	 * Block height in pixels, this is intended to be accessed through
12762306a36Sopenharmony_ci	 * drm_format_info_block_height()
12862306a36Sopenharmony_ci	 */
12962306a36Sopenharmony_ci	u8 block_h[DRM_FORMAT_MAX_PLANES];
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	/** @hsub: Horizontal chroma subsampling factor */
13262306a36Sopenharmony_ci	u8 hsub;
13362306a36Sopenharmony_ci	/** @vsub: Vertical chroma subsampling factor */
13462306a36Sopenharmony_ci	u8 vsub;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	/** @has_alpha: Does the format embeds an alpha component? */
13762306a36Sopenharmony_ci	bool has_alpha;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	/** @is_yuv: Is it a YUV format? */
14062306a36Sopenharmony_ci	bool is_yuv;
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	/** @is_color_indexed: Is it a color-indexed format? */
14362306a36Sopenharmony_ci	bool is_color_indexed;
14462306a36Sopenharmony_ci};
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci/**
14762306a36Sopenharmony_ci * drm_format_info_is_yuv_packed - check that the format info matches a YUV
14862306a36Sopenharmony_ci * format with data laid in a single plane
14962306a36Sopenharmony_ci * @info: format info
15062306a36Sopenharmony_ci *
15162306a36Sopenharmony_ci * Returns:
15262306a36Sopenharmony_ci * A boolean indicating whether the format info matches a packed YUV format.
15362306a36Sopenharmony_ci */
15462306a36Sopenharmony_cistatic inline bool
15562306a36Sopenharmony_cidrm_format_info_is_yuv_packed(const struct drm_format_info *info)
15662306a36Sopenharmony_ci{
15762306a36Sopenharmony_ci	return info->is_yuv && info->num_planes == 1;
15862306a36Sopenharmony_ci}
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci/**
16162306a36Sopenharmony_ci * drm_format_info_is_yuv_semiplanar - check that the format info matches a YUV
16262306a36Sopenharmony_ci * format with data laid in two planes (luminance and chrominance)
16362306a36Sopenharmony_ci * @info: format info
16462306a36Sopenharmony_ci *
16562306a36Sopenharmony_ci * Returns:
16662306a36Sopenharmony_ci * A boolean indicating whether the format info matches a semiplanar YUV format.
16762306a36Sopenharmony_ci */
16862306a36Sopenharmony_cistatic inline bool
16962306a36Sopenharmony_cidrm_format_info_is_yuv_semiplanar(const struct drm_format_info *info)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	return info->is_yuv && info->num_planes == 2;
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci/**
17562306a36Sopenharmony_ci * drm_format_info_is_yuv_planar - check that the format info matches a YUV
17662306a36Sopenharmony_ci * format with data laid in three planes (one for each YUV component)
17762306a36Sopenharmony_ci * @info: format info
17862306a36Sopenharmony_ci *
17962306a36Sopenharmony_ci * Returns:
18062306a36Sopenharmony_ci * A boolean indicating whether the format info matches a planar YUV format.
18162306a36Sopenharmony_ci */
18262306a36Sopenharmony_cistatic inline bool
18362306a36Sopenharmony_cidrm_format_info_is_yuv_planar(const struct drm_format_info *info)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	return info->is_yuv && info->num_planes == 3;
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci/**
18962306a36Sopenharmony_ci * drm_format_info_is_yuv_sampling_410 - check that the format info matches a
19062306a36Sopenharmony_ci * YUV format with 4:1:0 sub-sampling
19162306a36Sopenharmony_ci * @info: format info
19262306a36Sopenharmony_ci *
19362306a36Sopenharmony_ci * Returns:
19462306a36Sopenharmony_ci * A boolean indicating whether the format info matches a YUV format with 4:1:0
19562306a36Sopenharmony_ci * sub-sampling.
19662306a36Sopenharmony_ci */
19762306a36Sopenharmony_cistatic inline bool
19862306a36Sopenharmony_cidrm_format_info_is_yuv_sampling_410(const struct drm_format_info *info)
19962306a36Sopenharmony_ci{
20062306a36Sopenharmony_ci	return info->is_yuv && info->hsub == 4 && info->vsub == 4;
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci/**
20462306a36Sopenharmony_ci * drm_format_info_is_yuv_sampling_411 - check that the format info matches a
20562306a36Sopenharmony_ci * YUV format with 4:1:1 sub-sampling
20662306a36Sopenharmony_ci * @info: format info
20762306a36Sopenharmony_ci *
20862306a36Sopenharmony_ci * Returns:
20962306a36Sopenharmony_ci * A boolean indicating whether the format info matches a YUV format with 4:1:1
21062306a36Sopenharmony_ci * sub-sampling.
21162306a36Sopenharmony_ci */
21262306a36Sopenharmony_cistatic inline bool
21362306a36Sopenharmony_cidrm_format_info_is_yuv_sampling_411(const struct drm_format_info *info)
21462306a36Sopenharmony_ci{
21562306a36Sopenharmony_ci	return info->is_yuv && info->hsub == 4 && info->vsub == 1;
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci/**
21962306a36Sopenharmony_ci * drm_format_info_is_yuv_sampling_420 - check that the format info matches a
22062306a36Sopenharmony_ci * YUV format with 4:2:0 sub-sampling
22162306a36Sopenharmony_ci * @info: format info
22262306a36Sopenharmony_ci *
22362306a36Sopenharmony_ci * Returns:
22462306a36Sopenharmony_ci * A boolean indicating whether the format info matches a YUV format with 4:2:0
22562306a36Sopenharmony_ci * sub-sampling.
22662306a36Sopenharmony_ci */
22762306a36Sopenharmony_cistatic inline bool
22862306a36Sopenharmony_cidrm_format_info_is_yuv_sampling_420(const struct drm_format_info *info)
22962306a36Sopenharmony_ci{
23062306a36Sopenharmony_ci	return info->is_yuv && info->hsub == 2 && info->vsub == 2;
23162306a36Sopenharmony_ci}
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci/**
23462306a36Sopenharmony_ci * drm_format_info_is_yuv_sampling_422 - check that the format info matches a
23562306a36Sopenharmony_ci * YUV format with 4:2:2 sub-sampling
23662306a36Sopenharmony_ci * @info: format info
23762306a36Sopenharmony_ci *
23862306a36Sopenharmony_ci * Returns:
23962306a36Sopenharmony_ci * A boolean indicating whether the format info matches a YUV format with 4:2:2
24062306a36Sopenharmony_ci * sub-sampling.
24162306a36Sopenharmony_ci */
24262306a36Sopenharmony_cistatic inline bool
24362306a36Sopenharmony_cidrm_format_info_is_yuv_sampling_422(const struct drm_format_info *info)
24462306a36Sopenharmony_ci{
24562306a36Sopenharmony_ci	return info->is_yuv && info->hsub == 2 && info->vsub == 1;
24662306a36Sopenharmony_ci}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci/**
24962306a36Sopenharmony_ci * drm_format_info_is_yuv_sampling_444 - check that the format info matches a
25062306a36Sopenharmony_ci * YUV format with 4:4:4 sub-sampling
25162306a36Sopenharmony_ci * @info: format info
25262306a36Sopenharmony_ci *
25362306a36Sopenharmony_ci * Returns:
25462306a36Sopenharmony_ci * A boolean indicating whether the format info matches a YUV format with 4:4:4
25562306a36Sopenharmony_ci * sub-sampling.
25662306a36Sopenharmony_ci */
25762306a36Sopenharmony_cistatic inline bool
25862306a36Sopenharmony_cidrm_format_info_is_yuv_sampling_444(const struct drm_format_info *info)
25962306a36Sopenharmony_ci{
26062306a36Sopenharmony_ci	return info->is_yuv && info->hsub == 1 && info->vsub == 1;
26162306a36Sopenharmony_ci}
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ci/**
26462306a36Sopenharmony_ci * drm_format_info_plane_width - width of the plane given the first plane
26562306a36Sopenharmony_ci * @info: pixel format info
26662306a36Sopenharmony_ci * @width: width of the first plane
26762306a36Sopenharmony_ci * @plane: plane index
26862306a36Sopenharmony_ci *
26962306a36Sopenharmony_ci * Returns:
27062306a36Sopenharmony_ci * The width of @plane, given that the width of the first plane is @width.
27162306a36Sopenharmony_ci */
27262306a36Sopenharmony_cistatic inline
27362306a36Sopenharmony_ciint drm_format_info_plane_width(const struct drm_format_info *info, int width,
27462306a36Sopenharmony_ci				int plane)
27562306a36Sopenharmony_ci{
27662306a36Sopenharmony_ci	if (!info || plane >= info->num_planes)
27762306a36Sopenharmony_ci		return 0;
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	if (plane == 0)
28062306a36Sopenharmony_ci		return width;
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	return width / info->hsub;
28362306a36Sopenharmony_ci}
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci/**
28662306a36Sopenharmony_ci * drm_format_info_plane_height - height of the plane given the first plane
28762306a36Sopenharmony_ci * @info: pixel format info
28862306a36Sopenharmony_ci * @height: height of the first plane
28962306a36Sopenharmony_ci * @plane: plane index
29062306a36Sopenharmony_ci *
29162306a36Sopenharmony_ci * Returns:
29262306a36Sopenharmony_ci * The height of @plane, given that the height of the first plane is @height.
29362306a36Sopenharmony_ci */
29462306a36Sopenharmony_cistatic inline
29562306a36Sopenharmony_ciint drm_format_info_plane_height(const struct drm_format_info *info, int height,
29662306a36Sopenharmony_ci				 int plane)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	if (!info || plane >= info->num_planes)
29962306a36Sopenharmony_ci		return 0;
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	if (plane == 0)
30262306a36Sopenharmony_ci		return height;
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	return height / info->vsub;
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ciconst struct drm_format_info *__drm_format_info(u32 format);
30862306a36Sopenharmony_ciconst struct drm_format_info *drm_format_info(u32 format);
30962306a36Sopenharmony_ciconst struct drm_format_info *
31062306a36Sopenharmony_cidrm_get_format_info(struct drm_device *dev,
31162306a36Sopenharmony_ci		    const struct drm_mode_fb_cmd2 *mode_cmd);
31262306a36Sopenharmony_ciuint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth);
31362306a36Sopenharmony_ciuint32_t drm_driver_legacy_fb_format(struct drm_device *dev,
31462306a36Sopenharmony_ci				     uint32_t bpp, uint32_t depth);
31562306a36Sopenharmony_ciunsigned int drm_format_info_block_width(const struct drm_format_info *info,
31662306a36Sopenharmony_ci					 int plane);
31762306a36Sopenharmony_ciunsigned int drm_format_info_block_height(const struct drm_format_info *info,
31862306a36Sopenharmony_ci					  int plane);
31962306a36Sopenharmony_ciunsigned int drm_format_info_bpp(const struct drm_format_info *info, int plane);
32062306a36Sopenharmony_ciuint64_t drm_format_info_min_pitch(const struct drm_format_info *info,
32162306a36Sopenharmony_ci				   int plane, unsigned int buffer_width);
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci#endif /* __DRM_FOURCC_H__ */
324