2 * Copyright (c) 2016 Laurent Pinchart <laurent.pinchart@ideasonboard.com>
4 * DRM core format related functions
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting documentation, and
10 * that the name of the copyright holders not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. The copyright holders make no representations
13 * about the suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 #include <linux/bug.h>
26 #include <linux/ctype.h>
27 #include <linux/export.h>
28 #include <linux/kernel.h>
31 #include <drm/drm_fourcc.h>
33 static char printable_char(int c
)
35 return isascii(c
) && isprint(c
) ? c
: '?';
39 * drm_get_format_name - return a string for drm fourcc format
40 * @format: format to compute name of
42 * Note that the buffer used by this function is globally shared and owned by
43 * the function itself.
45 * FIXME: This isn't really multithreading safe.
47 const char *drm_get_format_name(uint32_t format
)
51 snprintf(buf
, sizeof(buf
),
52 "%c%c%c%c %s-endian (0x%08x)",
53 printable_char(format
& 0xff),
54 printable_char((format
>> 8) & 0xff),
55 printable_char((format
>> 16) & 0xff),
56 printable_char((format
>> 24) & 0x7f),
57 format
& DRM_FORMAT_BIG_ENDIAN
? "big" : "little",
62 EXPORT_SYMBOL(drm_get_format_name
);
65 * drm_fb_get_bpp_depth - get the bpp/depth values for format
66 * @format: pixel format (DRM_FORMAT_*)
67 * @depth: storage for the depth value
68 * @bpp: storage for the bpp value
70 * This only supports RGB formats here for compat with code that doesn't use
71 * pixel formats directly yet.
73 void drm_fb_get_bpp_depth(uint32_t format
, unsigned int *depth
,
78 case DRM_FORMAT_RGB332
:
79 case DRM_FORMAT_BGR233
:
83 case DRM_FORMAT_XRGB1555
:
84 case DRM_FORMAT_XBGR1555
:
85 case DRM_FORMAT_RGBX5551
:
86 case DRM_FORMAT_BGRX5551
:
87 case DRM_FORMAT_ARGB1555
:
88 case DRM_FORMAT_ABGR1555
:
89 case DRM_FORMAT_RGBA5551
:
90 case DRM_FORMAT_BGRA5551
:
94 case DRM_FORMAT_RGB565
:
95 case DRM_FORMAT_BGR565
:
99 case DRM_FORMAT_RGB888
:
100 case DRM_FORMAT_BGR888
:
104 case DRM_FORMAT_XRGB8888
:
105 case DRM_FORMAT_XBGR8888
:
106 case DRM_FORMAT_RGBX8888
:
107 case DRM_FORMAT_BGRX8888
:
111 case DRM_FORMAT_XRGB2101010
:
112 case DRM_FORMAT_XBGR2101010
:
113 case DRM_FORMAT_RGBX1010102
:
114 case DRM_FORMAT_BGRX1010102
:
115 case DRM_FORMAT_ARGB2101010
:
116 case DRM_FORMAT_ABGR2101010
:
117 case DRM_FORMAT_RGBA1010102
:
118 case DRM_FORMAT_BGRA1010102
:
122 case DRM_FORMAT_ARGB8888
:
123 case DRM_FORMAT_ABGR8888
:
124 case DRM_FORMAT_RGBA8888
:
125 case DRM_FORMAT_BGRA8888
:
130 DRM_DEBUG_KMS("unsupported pixel format %s\n",
131 drm_get_format_name(format
));
137 EXPORT_SYMBOL(drm_fb_get_bpp_depth
);
140 * drm_format_num_planes - get the number of planes for format
141 * @format: pixel format (DRM_FORMAT_*)
144 * The number of planes used by the specified pixel format.
146 int drm_format_num_planes(uint32_t format
)
149 case DRM_FORMAT_YUV410
:
150 case DRM_FORMAT_YVU410
:
151 case DRM_FORMAT_YUV411
:
152 case DRM_FORMAT_YVU411
:
153 case DRM_FORMAT_YUV420
:
154 case DRM_FORMAT_YVU420
:
155 case DRM_FORMAT_YUV422
:
156 case DRM_FORMAT_YVU422
:
157 case DRM_FORMAT_YUV444
:
158 case DRM_FORMAT_YVU444
:
160 case DRM_FORMAT_NV12
:
161 case DRM_FORMAT_NV21
:
162 case DRM_FORMAT_NV16
:
163 case DRM_FORMAT_NV61
:
164 case DRM_FORMAT_NV24
:
165 case DRM_FORMAT_NV42
:
171 EXPORT_SYMBOL(drm_format_num_planes
);
174 * drm_format_plane_cpp - determine the bytes per pixel value
175 * @format: pixel format (DRM_FORMAT_*)
176 * @plane: plane index
179 * The bytes per pixel value for the specified plane.
181 int drm_format_plane_cpp(uint32_t format
, int plane
)
186 if (plane
>= drm_format_num_planes(format
))
190 case DRM_FORMAT_YUYV
:
191 case DRM_FORMAT_YVYU
:
192 case DRM_FORMAT_UYVY
:
193 case DRM_FORMAT_VYUY
:
195 case DRM_FORMAT_NV12
:
196 case DRM_FORMAT_NV21
:
197 case DRM_FORMAT_NV16
:
198 case DRM_FORMAT_NV61
:
199 case DRM_FORMAT_NV24
:
200 case DRM_FORMAT_NV42
:
201 return plane
? 2 : 1;
202 case DRM_FORMAT_YUV410
:
203 case DRM_FORMAT_YVU410
:
204 case DRM_FORMAT_YUV411
:
205 case DRM_FORMAT_YVU411
:
206 case DRM_FORMAT_YUV420
:
207 case DRM_FORMAT_YVU420
:
208 case DRM_FORMAT_YUV422
:
209 case DRM_FORMAT_YVU422
:
210 case DRM_FORMAT_YUV444
:
211 case DRM_FORMAT_YVU444
:
214 drm_fb_get_bpp_depth(format
, &depth
, &bpp
);
218 EXPORT_SYMBOL(drm_format_plane_cpp
);
221 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
222 * @format: pixel format (DRM_FORMAT_*)
225 * The horizontal chroma subsampling factor for the
226 * specified pixel format.
228 int drm_format_horz_chroma_subsampling(uint32_t format
)
231 case DRM_FORMAT_YUV411
:
232 case DRM_FORMAT_YVU411
:
233 case DRM_FORMAT_YUV410
:
234 case DRM_FORMAT_YVU410
:
236 case DRM_FORMAT_YUYV
:
237 case DRM_FORMAT_YVYU
:
238 case DRM_FORMAT_UYVY
:
239 case DRM_FORMAT_VYUY
:
240 case DRM_FORMAT_NV12
:
241 case DRM_FORMAT_NV21
:
242 case DRM_FORMAT_NV16
:
243 case DRM_FORMAT_NV61
:
244 case DRM_FORMAT_YUV422
:
245 case DRM_FORMAT_YVU422
:
246 case DRM_FORMAT_YUV420
:
247 case DRM_FORMAT_YVU420
:
253 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling
);
256 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
257 * @format: pixel format (DRM_FORMAT_*)
260 * The vertical chroma subsampling factor for the
261 * specified pixel format.
263 int drm_format_vert_chroma_subsampling(uint32_t format
)
266 case DRM_FORMAT_YUV410
:
267 case DRM_FORMAT_YVU410
:
269 case DRM_FORMAT_YUV420
:
270 case DRM_FORMAT_YVU420
:
271 case DRM_FORMAT_NV12
:
272 case DRM_FORMAT_NV21
:
278 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling
);
281 * drm_format_plane_width - width of the plane given the first plane
282 * @width: width of the first plane
283 * @format: pixel format
284 * @plane: plane index
287 * The width of @plane, given that the width of the first plane is @width.
289 int drm_format_plane_width(int width
, uint32_t format
, int plane
)
291 if (plane
>= drm_format_num_planes(format
))
297 return width
/ drm_format_horz_chroma_subsampling(format
);
299 EXPORT_SYMBOL(drm_format_plane_width
);
302 * drm_format_plane_height - height of the plane given the first plane
303 * @height: height of the first plane
304 * @format: pixel format
305 * @plane: plane index
308 * The height of @plane, given that the height of the first plane is @height.
310 int drm_format_plane_height(int height
, uint32_t format
, int plane
)
312 if (plane
>= drm_format_num_planes(format
))
318 return height
/ drm_format_vert_chroma_subsampling(format
);
320 EXPORT_SYMBOL(drm_format_plane_height
);