2 * Copyright © 2016, 2019 Collabora, Ltd.
3 * Copyright (c) 2018 DisplayLink (UK) Ltd.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
24 * Author: Daniel Stone <daniels@collabora.com>
32 * Contains information about pixel formats, mapping format codes from
33 * wl_shm and drm_fourcc.h (which are deliberately identical, but for the
34 * special cases of WL_SHM_ARGB8888 and WL_SHM_XRGB8888) into various
35 * sets of information. Helper functions are provided for dealing with these
38 struct pixel_format_info {
39 /** DRM/wl_shm format code */
42 /** The DRM format name without the DRM_FORMAT_ prefix. */
43 const char *drm_format_name;
45 /** If true, is only for internal use and should not be advertised to
46 * clients to allow them to create buffers of this format. */
47 bool hide_from_clients;
49 /** If non-zero, number of planes in base (non-modified) format. */
52 /** If format contains alpha channel, opaque equivalent of format,
53 * i.e. alpha channel replaced with X. */
54 uint32_t opaque_substitute;
56 /** How the format should be sampled, expressed in terms of tokens
57 * from the EGL_WL_bind_wayland_display extension. If not set,
58 * assumed to be either RGB or RGBA, depending on whether or not
59 * the format contains an alpha channel. The samplers may still
60 * return alpha even for opaque formats; users must manually set
61 * the alpha channel to 1.0 (or ignore it) if the format is
63 uint32_t sampler_type;
65 /** GL internal format; to be used when creating FBO renderbuffers */
66 int gl_internalformat;
68 /** GL format, if data can be natively/directly uploaded. Note that
69 * whilst DRM formats are little-endian unless explicitly specified,
70 * (i.e. DRM_FORMAT_ARGB8888 is stored BGRA as sequential bytes in
71 * memory), GL uses the sequential byte order, so that format maps to
72 * GL_BGRA_EXT plus GL_UNSIGNED_BYTE. To add to the confusion, the
73 * explicitly-sized types (e.g. GL_UNSIGNED_SHORT_5_5_5_1) read in
74 * machine-endian order, so for these types, the correspondence
75 * depends on endianness. */
78 /** GL data type, if data can be natively/directly uploaded. */
81 /** Pixman data type, if it agrees exactly with the wl_shm format */
82 pixman_format_code_t pixman_format;
84 /** If set, this format can be used with the legacy drmModeAddFB()
85 * function (not AddFB2), using this and the bpp member. */
86 int addfb_legacy_depth;
88 /** Number of bits required to store a single pixel, for
89 * single-planar formats. */
92 /** Horizontal subsampling; if non-zero, divide the width by this
93 * member to obtain the number of columns in the source buffer for
94 * secondary planes only. Stride is not affected by horizontal
98 /** Vertical subsampling; if non-zero, divide the height by this
99 * member to obtain the number of rows in the source buffer for
100 * secondary planes only. */
103 /* Ordering of chroma components. */
109 /* If packed YUV (num_planes == 1), ordering of luma/chroma
112 ORDER_LUMA_CHROMA = 0,
116 /** How many significant bits each channel has, or zero if N/A. */
124 /** How channel bits are interpreted, fixed (uint) or floating-point */
126 PIXEL_COMPONENT_TYPE_FIXED = 0,
127 PIXEL_COMPONENT_TYPE_FLOAT,
132 * Get pixel format information for a DRM format code
134 * Given a DRM format code, return a pixel format info structure describing
135 * the properties of that format.
137 * @param format DRM format code to get info for
138 * @returns A pixel format structure (must not be freed), or NULL if the
139 * format could not be found
141 const struct pixel_format_info *
142 pixel_format_get_info(uint32_t format);
145 * Get pixel format information for a SHM format code
147 * Given a SHM format code, return a DRM pixel format info structure describing
148 * the properties of that format.
150 * @param format SHM format code to get info for.
151 * @returns A pixel format structure (must not be freed), or NULL if the
152 * format could not be found.
154 const struct pixel_format_info *
155 pixel_format_get_info_shm(uint32_t format);
158 * Get pixel format information by table index
160 * Given a 0-based index in the format table, return the corresponding
161 * DRM pixel format info structure.
163 * @param index Index of the pixel format in the table
164 * @returns A pixel format structure (must not be freed), or NULL if the
165 * index is out of range.
167 const struct pixel_format_info *
168 pixel_format_get_info_by_index(unsigned int index);
171 * Return the size of the pixel format table
173 * @returns The number of entries in the pixel format table
176 pixel_format_get_info_count(void);
179 * Get pixel format information for a named DRM format
181 * Given a DRM format name, return a pixel format info structure describing
182 * the properties of that format.
184 * The DRM format name is the preprocessor token name from drm_fourcc.h
185 * without the DRM_FORMAT_ prefix. The search is also case-insensitive.
186 * Both "xrgb8888" and "XRGB8888" searches will find DRM_FORMAT_XRGB8888
189 * @param drm_format_name DRM format name to get info for (not NULL)
190 * @returns A pixel format structure (must not be freed), or NULL if the
191 * name could not be found
193 const struct pixel_format_info *
194 pixel_format_get_info_by_drm_name(const char *drm_format_name);
197 * Get pixel format information for a Pixman format code
199 * Given a Pixman format code, return a pixel format info structure describing
200 * the properties of that format.
202 * @param pixman_format Pixman format code to get info for
203 * @returns A pixel format structure (must not be freed), or NULL if the
204 * format could not be found
206 const struct pixel_format_info *
207 pixel_format_get_info_by_pixman(pixman_format_code_t pixman_format);
210 * Get number of planes used by a pixel format
212 * Given a pixel format info structure, return the number of planes
213 * required for a buffer. Note that this is not necessarily identical to
214 * the number of samplers required to be bound, as two views into a single
215 * plane are sometimes required.
217 * @param format Pixel format info structure
218 * @returns Number of planes required for the format
221 pixel_format_get_plane_count(const struct pixel_format_info *format);
224 * Determine if a pixel format is opaque or contains alpha
226 * Returns whether or not the pixel format is opaque, or contains a
227 * significant alpha channel. Note that the suggested EGL sampler type may
228 * still sample undefined data into the alpha channel; users must consider
229 * alpha as 1.0 if the format is opaque, and not rely on the sampler to
230 * return this when sampling from the alpha channel.
232 * @param format Pixel format info structure
233 * @returns True if the format is opaque, or false if it has significant alpha
236 pixel_format_is_opaque(const struct pixel_format_info *format);
239 * Get compatible opaque equivalent for a format
241 * Given a pixel format info structure, return a format which is wholly
242 * compatible with the input format, but opaque, ignoring the alpha channel.
243 * If an alpha format is provided, but the content is known to all be opaque,
244 * then this can be used as a substitute to avoid blending.
246 * If the input format is opaque, this function will return the input format.
248 * @param format Pixel format info structure
249 * @returns A pixel format info structure for the compatible opaque substitute
251 const struct pixel_format_info *
252 pixel_format_get_opaque_substitute(const struct pixel_format_info *format);
255 * For an opaque format, get the equivalent format with alpha instead of an
258 * This is the opposite lookup from pixel_format_get_opaque_substitute().
259 * Finds the format whose opaque substitute is the given format.
261 * If the input format is not opaque or does not have ignored (X) bits, then
262 * the search cannot find a match.
264 * @param format DRM format code to search for
265 * @returns A pixel format info structure for the pixel format whose opaque
266 * substitute is the argument, or NULL if no match.
268 const struct pixel_format_info *
269 pixel_format_get_info_by_opaque_substitute(uint32_t format);
272 * Return the horizontal subsampling factor for a given plane
274 * When horizontal subsampling is effective, a sampler bound to a secondary
275 * plane must bind the sampler with a smaller effective width. This function
276 * returns the subsampling factor to use for the given plane.
278 * @param format Pixel format info structure
279 * @param plane Zero-indexed plane number
280 * @returns Horizontal subsampling factor for the given plane
283 pixel_format_hsub(const struct pixel_format_info *format,
287 * Return the vertical subsampling factor for a given plane
289 * When vertical subsampling is effective, a sampler bound to a secondary
290 * plane must bind the sampler with a smaller effective height. This function
291 * returns the subsampling factor to use for the given plane.
293 * @param format Pixel format info structure
294 * @param plane Zero-indexed plane number
295 * @returns Vertical subsampling factor for the given plane
298 pixel_format_vsub(const struct pixel_format_info *format,
302 * Return the effective sampling width for a given plane
304 * When horizontal subsampling is effective, a sampler bound to a secondary
305 * plane must bind the sampler with a smaller effective width. This function
306 * returns the effective width to use for the sampler, i.e. dividing by hsub.
308 * If horizontal subsampling is not in effect, this will be equal to the
311 * @param format Pixel format info structure
312 * @param plane Zero-indexed plane number
313 * @param width Width of the buffer
314 * @returns Effective width for sampling
317 pixel_format_width_for_plane(const struct pixel_format_info *format,
322 * Return the effective sampling height for a given plane
324 * When vertical subsampling is in effect, a sampler bound to a secondary
325 * plane must bind the sampler with a smaller effective height. This function
326 * returns the effective height to use for the sampler, i.e. dividing by vsub.
328 * If vertical subsampling is not in effect, this will be equal to the height.
330 * @param format Pixel format info structure
331 * @param plane Zero-indexed plane number
332 * @param height Height of the buffer
333 * @returns Effective width for sampling
336 pixel_format_height_for_plane(const struct pixel_format_info *format,
338 unsigned int height);
340 * Return a human-readable format modifier. Comprised from the modifier name,
341 * the vendor name, and the original encoded value in hexadecimal, using
342 * 'VENDOR_NAME_MODIFIER_NAME (modifier_encoded_value)' pattern. In case the
343 * modifier name (and the vendor name) isn't found, this returns the original
344 * encoded value, as a string value.
346 * @param modifier the modifier in question
347 * @returns a malloc'ed string, caller responsible for freeing after use.
350 pixel_format_get_modifier(uint64_t modifier);
353 * Return the wl_shm format code
355 * @param info Pixel format info structure
356 * @returns The wl_shm format code for this pixel format.
359 pixel_format_get_shm_format(const struct pixel_format_info *info);
362 * Get pixel format array for an array of DRM format codes
364 * Given an array of DRM format codes, return an array of corresponding pixel
365 * format info pointers.
367 * @param formats Array of DRM format codes to get info for
368 * @param formats_count Number of entries in formats.
369 * @returns An array of pixel format info pointers, or NULL if any format could
370 * not be found. Must be freed by the caller.
372 const struct pixel_format_info **
373 pixel_format_get_array(const uint32_t *formats, unsigned int formats_count);