blob: 10b9084df32b65adf075b8b29263435e0f950571 [file] [log] [blame]
Daniel Stone903721a2017-04-04 17:54:20 +01001/*
2 * Copyright © 2016 Collabora, Ltd.
Pekka Paalanene7c91b62018-09-26 14:00:34 +03003 * Copyright (c) 2018 DisplayLink (UK) Ltd.
Daniel Stone903721a2017-04-04 17:54:20 +01004 *
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:
11 *
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
14 * Software.
15 *
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.
23 *
24 * Author: Daniel Stone <daniels@collabora.com>
25 */
26
27#include <inttypes.h>
28#include <stdbool.h>
29
30/**
31 * Contains information about pixel formats, mapping format codes from
32 * wl_shm and drm_fourcc.h (which are deliberately identical, but for the
33 * special cases of WL_SHM_ARGB8888 and WL_SHM_XRGB8888) into various
34 * sets of information. Helper functions are provided for dealing with these
35 * raw structures.
36 */
37struct pixel_format_info {
38 /** DRM/wl_shm format code */
39 uint32_t format;
40
Pekka Paalanene7c91b62018-09-26 14:00:34 +030041 /** The DRM format name without the DRM_FORMAT_ prefix. */
42 const char *drm_format_name;
43
Daniel Stone903721a2017-04-04 17:54:20 +010044 /** If non-zero, number of planes in base (non-modified) format. */
45 int num_planes;
46
47 /** If format contains alpha channel, opaque equivalent of format,
48 * i.e. alpha channel replaced with X. */
49 uint32_t opaque_substitute;
50
51 /** How the format should be sampled, expressed in terms of tokens
52 * from the EGL_WL_bind_wayland_display extension. If not set,
53 * assumed to be either RGB or RGBA, depending on whether or not
54 * the format contains an alpha channel. The samplers may still
55 * return alpha even for opaque formats; users must manually set
56 * the alpha channel to 1.0 (or ignore it) if the format is
57 * opaque. */
58 uint32_t sampler_type;
59
60 /** GL format, if data can be natively/directly uploaded. Note that
61 * whilst DRM formats are little-endian unless explicitly specified,
62 * (i.e. DRM_FORMAT_ARGB8888 is stored BGRA as sequential bytes in
63 * memory), GL uses the sequential byte order, so that format maps to
64 * GL_BGRA_EXT plus GL_UNSIGNED_BYTE. To add to the confusion, the
65 * explicitly-sized types (e.g. GL_UNSIGNED_SHORT_5_5_5_1) read in
66 * machine-endian order, so for these types, the correspondence
67 * depends on endianness. */
68 int gl_format;
69
70 /** GL data type, if data can be natively/directly uploaded. */
71 int gl_type;
72
73 /** If set, this format can be used with the legacy drmModeAddFB()
74 * function (not AddFB2), using this and the bpp member. */
75 int depth;
76
77 /** See 'depth' member above. */
78 int bpp;
79
80 /** Horizontal subsampling; if non-zero, divide the width by this
81 * member to obtain the number of columns in the source buffer for
82 * secondary planes only. Stride is not affected by horizontal
83 * subsampling. */
84 int hsub;
85
86 /** Vertical subsampling; if non-zero, divide the height by this
87 * member to obtain the number of rows in the source buffer for
88 * secondary planes only. */
89 int vsub;
90
91 /* Ordering of chroma components. */
92 enum {
93 ORDER_UV = 0,
94 ORDER_VU,
95 } chroma_order;
96
97 /* If packed YUV (num_planes == 1), ordering of luma/chroma
98 * components. */
99 enum {
100 ORDER_LUMA_CHROMA = 0,
101 ORDER_CHROMA_LUMA,
102 } luma_chroma_order;
103};
104
105/**
106 * Get pixel format information for a DRM format code
107 *
108 * Given a DRM format code, return a pixel format info structure describing
109 * the properties of that format.
110 *
111 * @param format DRM format code to get info for
112 * @returns A pixel format structure (must not be freed), or NULL if the
113 * format could not be found
114 */
115const struct pixel_format_info *pixel_format_get_info(uint32_t format);
116
117/**
118 * Get number of planes used by a pixel format
119 *
120 * Given a pixel format info structure, return the number of planes
121 * required for a buffer. Note that this is not necessarily identical to
122 * the number of samplers required to be bound, as two views into a single
123 * plane are sometimes required.
124 *
125 * @param format Pixel format info structure
126 * @returns Number of planes required for the format
127 */
128unsigned int
129pixel_format_get_plane_count(const struct pixel_format_info *format);
130
131/**
132 * Determine if a pixel format is opaque or contains alpha
133 *
134 * Returns whether or not the pixel format is opaque, or contains a
135 * significant alpha channel. Note that the suggested EGL sampler type may
136 * still sample undefined data into the alpha channel; users must consider
137 * alpha as 1.0 if the format is opaque, and not rely on the sampler to
138 * return this when sampling from the alpha channel.
139 *
140 * @param format Pixel format info structure
141 * @returns True if the format is opaque, or false if it has significant alpha
142 */
143bool pixel_format_is_opaque(const struct pixel_format_info *format);
144
145/**
146 * Get compatible opaque equivalent for a format
147 *
148 * Given a pixel format info structure, return a format which is wholly
149 * compatible with the input format, but opaque, ignoring the alpha channel.
150 * If an alpha format is provided, but the content is known to all be opaque,
151 * then this can be used as a substitute to avoid blending.
152 *
153 * If the input format is opaque, this function will return the input format.
154 *
155 * @param format Pixel format info structure
156 * @returns A pixel format info structure for the compatible opaque substitute
157 */
158const struct pixel_format_info *
159pixel_format_get_opaque_substitute(const struct pixel_format_info *format);
160
161/**
162 * Return the effective sampling width for a given plane
163 *
164 * When horizontal subsampling is effective, a sampler bound to a secondary
165 * plane must bind the sampler with a smaller effective width. This function
166 * returns the effective width to use for the sampler, i.e. dividing by hsub.
167 *
168 * If horizontal subsampling is not in effect, this will be equal to the
169 * width.
170 *
171 * @param format Pixel format info structure
172 * @param plane Zero-indexed plane number
173 * @param width Width of the buffer
174 * @returns Effective width for sampling
175 */
176unsigned int
177pixel_format_width_for_plane(const struct pixel_format_info *format,
178 unsigned int plane,
179 unsigned int width);
180
181/**
182 * Return the effective sampling height for a given plane
183 *
184 * When vertical subsampling is in effect, a sampler bound to a secondary
185 * plane must bind the sampler with a smaller effective height. This function
186 * returns the effective height to use for the sampler, i.e. dividing by vsub.
187 *
188 * If vertical subsampling is not in effect, this will be equal to the height.
189 *
190 * @param format Pixel format info structure
191 * @param plane Zero-indexed plane number
192 * @param height Height of the buffer
193 * @returns Effective width for sampling
194 */
195unsigned int
196pixel_format_height_for_plane(const struct pixel_format_info *format,
197 unsigned int plane,
198 unsigned int height);