blob: b16aae32397aec61db91c11134176183c474dada [file] [log] [blame]
Daniel Stone903721a2017-04-04 17:54:20 +01001/*
2 * Copyright © 2016 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Author: Daniel Stone <daniels@collabora.com>
24 */
25
26#include <inttypes.h>
27#include <stdbool.h>
28
29/**
30 * Contains information about pixel formats, mapping format codes from
31 * wl_shm and drm_fourcc.h (which are deliberately identical, but for the
32 * special cases of WL_SHM_ARGB8888 and WL_SHM_XRGB8888) into various
33 * sets of information. Helper functions are provided for dealing with these
34 * raw structures.
35 */
36struct pixel_format_info {
37 /** DRM/wl_shm format code */
38 uint32_t format;
39
40 /** If non-zero, number of planes in base (non-modified) format. */
41 int num_planes;
42
43 /** If format contains alpha channel, opaque equivalent of format,
44 * i.e. alpha channel replaced with X. */
45 uint32_t opaque_substitute;
46
47 /** How the format should be sampled, expressed in terms of tokens
48 * from the EGL_WL_bind_wayland_display extension. If not set,
49 * assumed to be either RGB or RGBA, depending on whether or not
50 * the format contains an alpha channel. The samplers may still
51 * return alpha even for opaque formats; users must manually set
52 * the alpha channel to 1.0 (or ignore it) if the format is
53 * opaque. */
54 uint32_t sampler_type;
55
56 /** GL format, if data can be natively/directly uploaded. Note that
57 * whilst DRM formats are little-endian unless explicitly specified,
58 * (i.e. DRM_FORMAT_ARGB8888 is stored BGRA as sequential bytes in
59 * memory), GL uses the sequential byte order, so that format maps to
60 * GL_BGRA_EXT plus GL_UNSIGNED_BYTE. To add to the confusion, the
61 * explicitly-sized types (e.g. GL_UNSIGNED_SHORT_5_5_5_1) read in
62 * machine-endian order, so for these types, the correspondence
63 * depends on endianness. */
64 int gl_format;
65
66 /** GL data type, if data can be natively/directly uploaded. */
67 int gl_type;
68
69 /** If set, this format can be used with the legacy drmModeAddFB()
70 * function (not AddFB2), using this and the bpp member. */
71 int depth;
72
73 /** See 'depth' member above. */
74 int bpp;
75
76 /** Horizontal subsampling; if non-zero, divide the width by this
77 * member to obtain the number of columns in the source buffer for
78 * secondary planes only. Stride is not affected by horizontal
79 * subsampling. */
80 int hsub;
81
82 /** Vertical subsampling; if non-zero, divide the height by this
83 * member to obtain the number of rows in the source buffer for
84 * secondary planes only. */
85 int vsub;
86
87 /* Ordering of chroma components. */
88 enum {
89 ORDER_UV = 0,
90 ORDER_VU,
91 } chroma_order;
92
93 /* If packed YUV (num_planes == 1), ordering of luma/chroma
94 * components. */
95 enum {
96 ORDER_LUMA_CHROMA = 0,
97 ORDER_CHROMA_LUMA,
98 } luma_chroma_order;
99};
100
101/**
102 * Get pixel format information for a DRM format code
103 *
104 * Given a DRM format code, return a pixel format info structure describing
105 * the properties of that format.
106 *
107 * @param format DRM format code to get info for
108 * @returns A pixel format structure (must not be freed), or NULL if the
109 * format could not be found
110 */
111const struct pixel_format_info *pixel_format_get_info(uint32_t format);
112
113/**
114 * Get number of planes used by a pixel format
115 *
116 * Given a pixel format info structure, return the number of planes
117 * required for a buffer. Note that this is not necessarily identical to
118 * the number of samplers required to be bound, as two views into a single
119 * plane are sometimes required.
120 *
121 * @param format Pixel format info structure
122 * @returns Number of planes required for the format
123 */
124unsigned int
125pixel_format_get_plane_count(const struct pixel_format_info *format);
126
127/**
128 * Determine if a pixel format is opaque or contains alpha
129 *
130 * Returns whether or not the pixel format is opaque, or contains a
131 * significant alpha channel. Note that the suggested EGL sampler type may
132 * still sample undefined data into the alpha channel; users must consider
133 * alpha as 1.0 if the format is opaque, and not rely on the sampler to
134 * return this when sampling from the alpha channel.
135 *
136 * @param format Pixel format info structure
137 * @returns True if the format is opaque, or false if it has significant alpha
138 */
139bool pixel_format_is_opaque(const struct pixel_format_info *format);
140
141/**
142 * Get compatible opaque equivalent for a format
143 *
144 * Given a pixel format info structure, return a format which is wholly
145 * compatible with the input format, but opaque, ignoring the alpha channel.
146 * If an alpha format is provided, but the content is known to all be opaque,
147 * then this can be used as a substitute to avoid blending.
148 *
149 * If the input format is opaque, this function will return the input format.
150 *
151 * @param format Pixel format info structure
152 * @returns A pixel format info structure for the compatible opaque substitute
153 */
154const struct pixel_format_info *
155pixel_format_get_opaque_substitute(const struct pixel_format_info *format);
156
157/**
158 * Return the effective sampling width for a given plane
159 *
160 * When horizontal subsampling is effective, a sampler bound to a secondary
161 * plane must bind the sampler with a smaller effective width. This function
162 * returns the effective width to use for the sampler, i.e. dividing by hsub.
163 *
164 * If horizontal subsampling is not in effect, this will be equal to the
165 * width.
166 *
167 * @param format Pixel format info structure
168 * @param plane Zero-indexed plane number
169 * @param width Width of the buffer
170 * @returns Effective width for sampling
171 */
172unsigned int
173pixel_format_width_for_plane(const struct pixel_format_info *format,
174 unsigned int plane,
175 unsigned int width);
176
177/**
178 * Return the effective sampling height for a given plane
179 *
180 * When vertical subsampling is in effect, a sampler bound to a secondary
181 * plane must bind the sampler with a smaller effective height. This function
182 * returns the effective height to use for the sampler, i.e. dividing by vsub.
183 *
184 * If vertical subsampling is not in effect, this will be equal to the height.
185 *
186 * @param format Pixel format info structure
187 * @param plane Zero-indexed plane number
188 * @param height Height of the buffer
189 * @returns Effective width for sampling
190 */
191unsigned int
192pixel_format_height_for_plane(const struct pixel_format_info *format,
193 unsigned int plane,
194 unsigned int height);