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