xref: /dflybsd-src/sys/dev/drm/drm_fourcc.c (revision 4be47400997875399098ad904fb4ca65b3244cca)
11dedbd3bSFrançois Tigeot /*
21dedbd3bSFrançois Tigeot  * Copyright (c) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
31dedbd3bSFrançois Tigeot  *
41dedbd3bSFrançois Tigeot  * DRM core format related functions
51dedbd3bSFrançois Tigeot  *
61dedbd3bSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
71dedbd3bSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
81dedbd3bSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
91dedbd3bSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
101dedbd3bSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
111dedbd3bSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
121dedbd3bSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
131dedbd3bSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
141dedbd3bSFrançois Tigeot  * is" without express or implied warranty.
151dedbd3bSFrançois Tigeot  *
161dedbd3bSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
171dedbd3bSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
181dedbd3bSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
191dedbd3bSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
201dedbd3bSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
211dedbd3bSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
221dedbd3bSFrançois Tigeot  * OF THIS SOFTWARE.
231dedbd3bSFrançois Tigeot  */
241dedbd3bSFrançois Tigeot 
251dedbd3bSFrançois Tigeot #include <linux/bug.h>
261dedbd3bSFrançois Tigeot #include <linux/ctype.h>
271dedbd3bSFrançois Tigeot #include <linux/export.h>
281dedbd3bSFrançois Tigeot #include <linux/kernel.h>
291dedbd3bSFrançois Tigeot 
301dedbd3bSFrançois Tigeot #include <drm/drmP.h>
311dedbd3bSFrançois Tigeot #include <drm/drm_fourcc.h>
321dedbd3bSFrançois Tigeot 
331dedbd3bSFrançois Tigeot static char printable_char(int c)
341dedbd3bSFrançois Tigeot {
351dedbd3bSFrançois Tigeot 	return isascii(c) && isprint(c) ? c : '?';
361dedbd3bSFrançois Tigeot }
371dedbd3bSFrançois Tigeot 
381dedbd3bSFrançois Tigeot /**
391dedbd3bSFrançois Tigeot  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
401dedbd3bSFrançois Tigeot  * @bpp: bits per pixels
411dedbd3bSFrançois Tigeot  * @depth: bit depth per pixel
421dedbd3bSFrançois Tigeot  *
431dedbd3bSFrançois Tigeot  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
441dedbd3bSFrançois Tigeot  * Useful in fbdev emulation code, since that deals in those values.
451dedbd3bSFrançois Tigeot  */
461dedbd3bSFrançois Tigeot uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
471dedbd3bSFrançois Tigeot {
481dedbd3bSFrançois Tigeot 	uint32_t fmt;
491dedbd3bSFrançois Tigeot 
501dedbd3bSFrançois Tigeot 	switch (bpp) {
511dedbd3bSFrançois Tigeot 	case 8:
521dedbd3bSFrançois Tigeot 		fmt = DRM_FORMAT_C8;
531dedbd3bSFrançois Tigeot 		break;
541dedbd3bSFrançois Tigeot 	case 16:
551dedbd3bSFrançois Tigeot 		if (depth == 15)
561dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB1555;
571dedbd3bSFrançois Tigeot 		else
581dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_RGB565;
591dedbd3bSFrançois Tigeot 		break;
601dedbd3bSFrançois Tigeot 	case 24:
611dedbd3bSFrançois Tigeot 		fmt = DRM_FORMAT_RGB888;
621dedbd3bSFrançois Tigeot 		break;
631dedbd3bSFrançois Tigeot 	case 32:
641dedbd3bSFrançois Tigeot 		if (depth == 24)
651dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB8888;
661dedbd3bSFrançois Tigeot 		else if (depth == 30)
671dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_XRGB2101010;
681dedbd3bSFrançois Tigeot 		else
691dedbd3bSFrançois Tigeot 			fmt = DRM_FORMAT_ARGB8888;
701dedbd3bSFrançois Tigeot 		break;
711dedbd3bSFrançois Tigeot 	default:
721dedbd3bSFrançois Tigeot 		DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
731dedbd3bSFrançois Tigeot 		fmt = DRM_FORMAT_XRGB8888;
741dedbd3bSFrançois Tigeot 		break;
751dedbd3bSFrançois Tigeot 	}
761dedbd3bSFrançois Tigeot 
771dedbd3bSFrançois Tigeot 	return fmt;
781dedbd3bSFrançois Tigeot }
791dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_mode_legacy_fb_format);
801dedbd3bSFrançois Tigeot 
811dedbd3bSFrançois Tigeot /**
82*4be47400SFrançois Tigeot  * drm_get_format_name - fill a string with a drm fourcc format's name
831dedbd3bSFrançois Tigeot  * @format: format to compute name of
84*4be47400SFrançois Tigeot  * @buf: caller-supplied buffer
851dedbd3bSFrançois Tigeot  */
86*4be47400SFrançois Tigeot const char *drm_get_format_name(uint32_t format, struct drm_format_name_buf *buf)
871dedbd3bSFrançois Tigeot {
88*4be47400SFrançois Tigeot 	snprintf(buf->str, sizeof(buf->str),
891dedbd3bSFrançois Tigeot 		 "%c%c%c%c %s-endian (0x%08x)",
901dedbd3bSFrançois Tigeot 		 printable_char(format & 0xff),
911dedbd3bSFrançois Tigeot 		 printable_char((format >> 8) & 0xff),
921dedbd3bSFrançois Tigeot 		 printable_char((format >> 16) & 0xff),
931dedbd3bSFrançois Tigeot 		 printable_char((format >> 24) & 0x7f),
941dedbd3bSFrançois Tigeot 		 format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
951dedbd3bSFrançois Tigeot 		 format);
961dedbd3bSFrançois Tigeot 
97*4be47400SFrançois Tigeot 	return buf->str;
981dedbd3bSFrançois Tigeot }
991dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_get_format_name);
1001dedbd3bSFrançois Tigeot 
101*4be47400SFrançois Tigeot /*
102*4be47400SFrançois Tigeot  * Internal function to query information for a given format. See
103*4be47400SFrançois Tigeot  * drm_format_info() for the public API.
1041dedbd3bSFrançois Tigeot  */
105*4be47400SFrançois Tigeot const struct drm_format_info *__drm_format_info(u32 format)
1061dedbd3bSFrançois Tigeot {
107*4be47400SFrançois Tigeot 	static const struct drm_format_info formats[] = {
108*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_C8,		.depth = 8,  .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
109*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGB332,		.depth = 8,  .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
110*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGR233,		.depth = 8,  .num_planes = 1, .cpp = { 1, 0, 0 }, .hsub = 1, .vsub = 1 },
111*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
112*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
113*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
114*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
115*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ARGB4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
116*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ABGR4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
117*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBA4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
118*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRA4444,	.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
119*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB1555,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
120*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR1555,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
121*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX5551,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
122*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX5551,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
123*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ARGB1555,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
124*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ABGR1555,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
125*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBA5551,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
126*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRA5551,	.depth = 15, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
127*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGB565,		.depth = 16, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
128*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGR565,		.depth = 16, .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 1, .vsub = 1 },
129*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGB888,		.depth = 24, .num_planes = 1, .cpp = { 3, 0, 0 }, .hsub = 1, .vsub = 1 },
130*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGR888,		.depth = 24, .num_planes = 1, .cpp = { 3, 0, 0 }, .hsub = 1, .vsub = 1 },
131*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB8888,	.depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
132*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR8888,	.depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
133*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX8888,	.depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
134*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX8888,	.depth = 24, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
135*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XRGB2101010,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
136*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_XBGR2101010,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
137*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBX1010102,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
138*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRX1010102,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
139*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ARGB2101010,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
140*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ABGR2101010,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
141*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBA1010102,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
142*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRA1010102,	.depth = 30, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
143*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ARGB8888,	.depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
144*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_ABGR8888,	.depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
145*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_RGBA8888,	.depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
146*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_BGRA8888,	.depth = 32, .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
147*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV410,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 4 },
148*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU410,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 4 },
149*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV411,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 1 },
150*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU411,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 4, .vsub = 1 },
151*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV420,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 2 },
152*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU420,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 2 },
153*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV422,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 1 },
154*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU422,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 2, .vsub = 1 },
155*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUV444,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 1, .vsub = 1 },
156*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVU444,		.depth = 0,  .num_planes = 3, .cpp = { 1, 1, 1 }, .hsub = 1, .vsub = 1 },
157*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV12,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 2 },
158*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV21,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 2 },
159*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV16,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 1 },
160*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV61,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 2, .vsub = 1 },
161*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV24,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 1, .vsub = 1 },
162*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_NV42,		.depth = 0,  .num_planes = 2, .cpp = { 1, 2, 0 }, .hsub = 1, .vsub = 1 },
163*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YUYV,		.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1 },
164*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_YVYU,		.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1 },
165*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_UYVY,		.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1 },
166*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_VYUY,		.depth = 0,  .num_planes = 1, .cpp = { 2, 0, 0 }, .hsub = 2, .vsub = 1 },
167*4be47400SFrançois Tigeot 		{ .format = DRM_FORMAT_AYUV,		.depth = 0,  .num_planes = 1, .cpp = { 4, 0, 0 }, .hsub = 1, .vsub = 1 },
168*4be47400SFrançois Tigeot 	};
1691dedbd3bSFrançois Tigeot 
170*4be47400SFrançois Tigeot 	unsigned int i;
171*4be47400SFrançois Tigeot 
172*4be47400SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(formats); ++i) {
173*4be47400SFrançois Tigeot 		if (formats[i].format == format)
174*4be47400SFrançois Tigeot 			return &formats[i];
1751dedbd3bSFrançois Tigeot 	}
176*4be47400SFrançois Tigeot 
177*4be47400SFrançois Tigeot 	return NULL;
1781dedbd3bSFrançois Tigeot }
179*4be47400SFrançois Tigeot 
180*4be47400SFrançois Tigeot /**
181*4be47400SFrançois Tigeot  * drm_format_info - query information for a given format
182*4be47400SFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
183*4be47400SFrançois Tigeot  *
184*4be47400SFrançois Tigeot  * The caller should only pass a supported pixel format to this function.
185*4be47400SFrançois Tigeot  * Unsupported pixel formats will generate a warning in the kernel log.
186*4be47400SFrançois Tigeot  *
187*4be47400SFrançois Tigeot  * Returns:
188*4be47400SFrançois Tigeot  * The instance of struct drm_format_info that describes the pixel format, or
189*4be47400SFrançois Tigeot  * NULL if the format is unsupported.
190*4be47400SFrançois Tigeot  */
191*4be47400SFrançois Tigeot const struct drm_format_info *drm_format_info(u32 format)
192*4be47400SFrançois Tigeot {
193*4be47400SFrançois Tigeot 	const struct drm_format_info *info;
194*4be47400SFrançois Tigeot 
195*4be47400SFrançois Tigeot 	info = __drm_format_info(format);
196*4be47400SFrançois Tigeot 	WARN_ON(!info);
197*4be47400SFrançois Tigeot 	return info;
198*4be47400SFrançois Tigeot }
199*4be47400SFrançois Tigeot EXPORT_SYMBOL(drm_format_info);
2001dedbd3bSFrançois Tigeot 
2011dedbd3bSFrançois Tigeot /**
2021dedbd3bSFrançois Tigeot  * drm_format_num_planes - get the number of planes for format
2031dedbd3bSFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
2041dedbd3bSFrançois Tigeot  *
2051dedbd3bSFrançois Tigeot  * Returns:
2061dedbd3bSFrançois Tigeot  * The number of planes used by the specified pixel format.
2071dedbd3bSFrançois Tigeot  */
2081dedbd3bSFrançois Tigeot int drm_format_num_planes(uint32_t format)
2091dedbd3bSFrançois Tigeot {
210*4be47400SFrançois Tigeot 	const struct drm_format_info *info;
211*4be47400SFrançois Tigeot 
212*4be47400SFrançois Tigeot 	info = drm_format_info(format);
213*4be47400SFrançois Tigeot 	return info ? info->num_planes : 1;
2141dedbd3bSFrançois Tigeot }
2151dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_num_planes);
2161dedbd3bSFrançois Tigeot 
2171dedbd3bSFrançois Tigeot /**
2181dedbd3bSFrançois Tigeot  * drm_format_plane_cpp - determine the bytes per pixel value
2191dedbd3bSFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
2201dedbd3bSFrançois Tigeot  * @plane: plane index
2211dedbd3bSFrançois Tigeot  *
2221dedbd3bSFrançois Tigeot  * Returns:
2231dedbd3bSFrançois Tigeot  * The bytes per pixel value for the specified plane.
2241dedbd3bSFrançois Tigeot  */
2251dedbd3bSFrançois Tigeot int drm_format_plane_cpp(uint32_t format, int plane)
2261dedbd3bSFrançois Tigeot {
227*4be47400SFrançois Tigeot 	const struct drm_format_info *info;
2281dedbd3bSFrançois Tigeot 
229*4be47400SFrançois Tigeot 	info = drm_format_info(format);
230*4be47400SFrançois Tigeot 	if (!info || plane >= info->num_planes)
2311dedbd3bSFrançois Tigeot 		return 0;
2321dedbd3bSFrançois Tigeot 
233*4be47400SFrançois Tigeot 	return info->cpp[plane];
2341dedbd3bSFrançois Tigeot }
2351dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_plane_cpp);
2361dedbd3bSFrançois Tigeot 
2371dedbd3bSFrançois Tigeot /**
2381dedbd3bSFrançois Tigeot  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
2391dedbd3bSFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
2401dedbd3bSFrançois Tigeot  *
2411dedbd3bSFrançois Tigeot  * Returns:
2421dedbd3bSFrançois Tigeot  * The horizontal chroma subsampling factor for the
2431dedbd3bSFrançois Tigeot  * specified pixel format.
2441dedbd3bSFrançois Tigeot  */
2451dedbd3bSFrançois Tigeot int drm_format_horz_chroma_subsampling(uint32_t format)
2461dedbd3bSFrançois Tigeot {
247*4be47400SFrançois Tigeot 	const struct drm_format_info *info;
248*4be47400SFrançois Tigeot 
249*4be47400SFrançois Tigeot 	info = drm_format_info(format);
250*4be47400SFrançois Tigeot 	return info ? info->hsub : 1;
2511dedbd3bSFrançois Tigeot }
2521dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
2531dedbd3bSFrançois Tigeot 
2541dedbd3bSFrançois Tigeot /**
2551dedbd3bSFrançois Tigeot  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
2561dedbd3bSFrançois Tigeot  * @format: pixel format (DRM_FORMAT_*)
2571dedbd3bSFrançois Tigeot  *
2581dedbd3bSFrançois Tigeot  * Returns:
2591dedbd3bSFrançois Tigeot  * The vertical chroma subsampling factor for the
2601dedbd3bSFrançois Tigeot  * specified pixel format.
2611dedbd3bSFrançois Tigeot  */
2621dedbd3bSFrançois Tigeot int drm_format_vert_chroma_subsampling(uint32_t format)
2631dedbd3bSFrançois Tigeot {
264*4be47400SFrançois Tigeot 	const struct drm_format_info *info;
265*4be47400SFrançois Tigeot 
266*4be47400SFrançois Tigeot 	info = drm_format_info(format);
267*4be47400SFrançois Tigeot 	return info ? info->vsub : 1;
2681dedbd3bSFrançois Tigeot }
2691dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
2701dedbd3bSFrançois Tigeot 
2711dedbd3bSFrançois Tigeot /**
2721dedbd3bSFrançois Tigeot  * drm_format_plane_width - width of the plane given the first plane
2731dedbd3bSFrançois Tigeot  * @width: width of the first plane
2741dedbd3bSFrançois Tigeot  * @format: pixel format
2751dedbd3bSFrançois Tigeot  * @plane: plane index
2761dedbd3bSFrançois Tigeot  *
2771dedbd3bSFrançois Tigeot  * Returns:
2781dedbd3bSFrançois Tigeot  * The width of @plane, given that the width of the first plane is @width.
2791dedbd3bSFrançois Tigeot  */
2801dedbd3bSFrançois Tigeot int drm_format_plane_width(int width, uint32_t format, int plane)
2811dedbd3bSFrançois Tigeot {
282*4be47400SFrançois Tigeot 	const struct drm_format_info *info;
283*4be47400SFrançois Tigeot 
284*4be47400SFrançois Tigeot 	info = drm_format_info(format);
285*4be47400SFrançois Tigeot 	if (!info || plane >= info->num_planes)
2861dedbd3bSFrançois Tigeot 		return 0;
2871dedbd3bSFrançois Tigeot 
2881dedbd3bSFrançois Tigeot 	if (plane == 0)
2891dedbd3bSFrançois Tigeot 		return width;
2901dedbd3bSFrançois Tigeot 
291*4be47400SFrançois Tigeot 	return width / info->hsub;
2921dedbd3bSFrançois Tigeot }
2931dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_plane_width);
2941dedbd3bSFrançois Tigeot 
2951dedbd3bSFrançois Tigeot /**
2961dedbd3bSFrançois Tigeot  * drm_format_plane_height - height of the plane given the first plane
2971dedbd3bSFrançois Tigeot  * @height: height of the first plane
2981dedbd3bSFrançois Tigeot  * @format: pixel format
2991dedbd3bSFrançois Tigeot  * @plane: plane index
3001dedbd3bSFrançois Tigeot  *
3011dedbd3bSFrançois Tigeot  * Returns:
3021dedbd3bSFrançois Tigeot  * The height of @plane, given that the height of the first plane is @height.
3031dedbd3bSFrançois Tigeot  */
3041dedbd3bSFrançois Tigeot int drm_format_plane_height(int height, uint32_t format, int plane)
3051dedbd3bSFrançois Tigeot {
306*4be47400SFrançois Tigeot 	const struct drm_format_info *info;
307*4be47400SFrançois Tigeot 
308*4be47400SFrançois Tigeot 	info = drm_format_info(format);
309*4be47400SFrançois Tigeot 	if (!info || plane >= info->num_planes)
3101dedbd3bSFrançois Tigeot 		return 0;
3111dedbd3bSFrançois Tigeot 
3121dedbd3bSFrançois Tigeot 	if (plane == 0)
3131dedbd3bSFrançois Tigeot 		return height;
3141dedbd3bSFrançois Tigeot 
315*4be47400SFrançois Tigeot 	return height / info->vsub;
3161dedbd3bSFrançois Tigeot }
3171dedbd3bSFrançois Tigeot EXPORT_SYMBOL(drm_format_plane_height);
318