st/xorg: Work around cursor reference counting bugs in older X servers.
[mesa.git] / src / gallium / auxiliary / util / u_format.h
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 Vmware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #ifndef U_FORMAT_H
30 #define U_FORMAT_H
31
32
33 #include "pipe/p_format.h"
34 #include "util/u_debug.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40
41 /**
42 * Describe how to pack/unpack pixels into/from the prescribed format.
43 *
44 * XXX: This could be renamed to something like util_format_pack, or broke down
45 * in flags inside util_format_block that said exactly what we want.
46 */
47 enum util_format_layout {
48 /**
49 * Formats with util_format_block::width == util_format_block::height == 1
50 * that can be described as an ordinary data structure.
51 */
52 UTIL_FORMAT_LAYOUT_PLAIN = 0,
53
54 /**
55 * Formats with sub-sampled channels.
56 *
57 * This is for formats like YV12 where there is less than one sample per
58 * pixel.
59 *
60 * XXX: This could actually b
61 */
62 UTIL_FORMAT_LAYOUT_SUBSAMPLED = 3,
63
64 /**
65 * An unspecified compression algorithm.
66 */
67 UTIL_FORMAT_LAYOUT_COMPRESSED = 4
68 };
69
70
71 struct util_format_block
72 {
73 /** Block width in pixels */
74 unsigned width;
75
76 /** Block height in pixels */
77 unsigned height;
78
79 /** Block size in bits */
80 unsigned bits;
81 };
82
83
84 enum util_format_type {
85 UTIL_FORMAT_TYPE_VOID = 0,
86 UTIL_FORMAT_TYPE_UNSIGNED = 1,
87 UTIL_FORMAT_TYPE_SIGNED = 2,
88 UTIL_FORMAT_TYPE_FIXED = 3,
89 UTIL_FORMAT_TYPE_FLOAT = 4
90 };
91
92
93 enum util_format_swizzle {
94 UTIL_FORMAT_SWIZZLE_X = 0,
95 UTIL_FORMAT_SWIZZLE_Y = 1,
96 UTIL_FORMAT_SWIZZLE_Z = 2,
97 UTIL_FORMAT_SWIZZLE_W = 3,
98 UTIL_FORMAT_SWIZZLE_0 = 4,
99 UTIL_FORMAT_SWIZZLE_1 = 5,
100 UTIL_FORMAT_SWIZZLE_NONE = 6
101 };
102
103
104 enum util_format_colorspace {
105 UTIL_FORMAT_COLORSPACE_RGB = 0,
106 UTIL_FORMAT_COLORSPACE_SRGB = 1,
107 UTIL_FORMAT_COLORSPACE_YUV = 2,
108 UTIL_FORMAT_COLORSPACE_ZS = 3
109 };
110
111
112 struct util_format_channel_description
113 {
114 unsigned type:6;
115 unsigned normalized:1;
116 unsigned size:9;
117 };
118
119
120 struct util_format_description
121 {
122 enum pipe_format format;
123 const char *name;
124
125 /**
126 * Pixel block dimensions.
127 */
128 struct util_format_block block;
129
130 enum util_format_layout layout;
131
132 /**
133 * The number of channels.
134 */
135 unsigned nr_channels:3;
136
137 /**
138 * Whether all channels have the same number of (whole) bytes.
139 */
140 unsigned is_array:1;
141
142 /**
143 * Whether channels have mixed types (ignoring UTIL_FORMAT_TYPE_VOID).
144 */
145 unsigned is_mixed:1;
146
147 /**
148 * Input channel description.
149 *
150 * Only valid for UTIL_FORMAT_LAYOUT_PLAIN formats.
151 */
152 struct util_format_channel_description channel[4];
153
154 /**
155 * Output channel swizzle.
156 *
157 * The order is either:
158 * - RGBA
159 * - YUV(A)
160 * - ZS
161 * depending on the colorspace.
162 */
163 unsigned char swizzle[4];
164
165 /**
166 * Colorspace transformation.
167 */
168 enum util_format_colorspace colorspace;
169 };
170
171
172 extern const struct util_format_description
173 util_format_description_table[];
174
175
176 const struct util_format_description *
177 util_format_description(enum pipe_format format);
178
179
180 /*
181 * Format query functions.
182 */
183
184 static INLINE const char *
185 util_format_name(enum pipe_format format)
186 {
187 const struct util_format_description *desc = util_format_description(format);
188
189 assert(format);
190 if (!format) {
191 return "???";
192 }
193
194 return desc->name;
195 }
196
197 static INLINE boolean
198 util_format_is_compressed(enum pipe_format format)
199 {
200 const struct util_format_description *desc = util_format_description(format);
201
202 assert(format);
203 if (!format) {
204 return FALSE;
205 }
206
207 return desc->layout == UTIL_FORMAT_LAYOUT_COMPRESSED ? TRUE : FALSE;
208 }
209
210 static INLINE boolean
211 util_format_is_depth_or_stencil(enum pipe_format format)
212 {
213 const struct util_format_description *desc = util_format_description(format);
214
215 assert(format);
216 if (!format) {
217 return FALSE;
218 }
219
220 return desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ? TRUE : FALSE;
221 }
222
223 static INLINE boolean
224 util_format_is_depth_and_stencil(enum pipe_format format)
225 {
226 const struct util_format_description *desc = util_format_description(format);
227
228 assert(format);
229 if (!format) {
230 return FALSE;
231 }
232
233 if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS) {
234 return FALSE;
235 }
236
237 return (desc->swizzle[0] != UTIL_FORMAT_SWIZZLE_NONE &&
238 desc->swizzle[1] != UTIL_FORMAT_SWIZZLE_NONE) ? TRUE : FALSE;
239 }
240
241
242 /**
243 * Return total bits needed for the pixel format per block.
244 */
245 static INLINE uint
246 util_format_get_blocksizebits(enum pipe_format format)
247 {
248 const struct util_format_description *desc = util_format_description(format);
249
250 assert(format);
251 if (!format) {
252 return 0;
253 }
254
255 return desc->block.bits;
256 }
257
258 /**
259 * Return bytes per block (not pixel) for the given format.
260 */
261 static INLINE uint
262 util_format_get_blocksize(enum pipe_format format)
263 {
264 uint bits = util_format_get_blocksizebits(format);
265
266 assert(bits % 8 == 0);
267
268 return bits / 8;
269 }
270
271 static INLINE uint
272 util_format_get_blockwidth(enum pipe_format format)
273 {
274 const struct util_format_description *desc = util_format_description(format);
275
276 assert(format);
277 if (!format) {
278 return 1;
279 }
280
281 return desc->block.width;
282 }
283
284 static INLINE uint
285 util_format_get_blockheight(enum pipe_format format)
286 {
287 const struct util_format_description *desc = util_format_description(format);
288
289 assert(format);
290 if (!format) {
291 return 1;
292 }
293
294 return desc->block.height;
295 }
296
297 static INLINE unsigned
298 util_format_get_nblocksx(enum pipe_format format,
299 unsigned x)
300 {
301 unsigned blockwidth = util_format_get_blockwidth(format);
302 return (x + blockwidth - 1) / blockwidth;
303 }
304
305 static INLINE unsigned
306 util_format_get_nblocksy(enum pipe_format format,
307 unsigned y)
308 {
309 unsigned blockheight = util_format_get_blockheight(format);
310 return (y + blockheight - 1) / blockheight;
311 }
312
313 static INLINE unsigned
314 util_format_get_nblocks(enum pipe_format format,
315 unsigned width,
316 unsigned height)
317 {
318 return util_format_get_nblocksx(format, width) * util_format_get_nblocksy(format, height);
319 }
320
321 static INLINE size_t
322 util_format_get_stride(enum pipe_format format,
323 unsigned width)
324 {
325 return util_format_get_nblocksx(format, width) * util_format_get_blocksize(format);
326 }
327
328 static INLINE size_t
329 util_format_get_2d_size(enum pipe_format format,
330 size_t stride,
331 unsigned height)
332 {
333 return util_format_get_nblocksy(format, height) * stride;
334 }
335
336 static INLINE uint
337 util_format_get_component_bits(enum pipe_format format,
338 enum util_format_colorspace colorspace,
339 uint component)
340 {
341 const struct util_format_description *desc = util_format_description(format);
342 enum util_format_colorspace desc_colorspace;
343
344 assert(format);
345 if (!format) {
346 return 0;
347 }
348
349 assert(component < 4);
350
351 /* Treat RGB and SRGB as equivalent. */
352 if (colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
353 colorspace = UTIL_FORMAT_COLORSPACE_RGB;
354 }
355 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
356 desc_colorspace = UTIL_FORMAT_COLORSPACE_RGB;
357 } else {
358 desc_colorspace = desc->colorspace;
359 }
360
361 if (desc_colorspace != colorspace) {
362 return 0;
363 }
364
365 switch (desc->swizzle[component]) {
366 case UTIL_FORMAT_SWIZZLE_X:
367 return desc->channel[0].size;
368 case UTIL_FORMAT_SWIZZLE_Y:
369 return desc->channel[1].size;
370 case UTIL_FORMAT_SWIZZLE_Z:
371 return desc->channel[2].size;
372 case UTIL_FORMAT_SWIZZLE_W:
373 return desc->channel[3].size;
374 default:
375 return 0;
376 }
377 }
378
379 static INLINE boolean
380 util_format_has_alpha(enum pipe_format format)
381 {
382 const struct util_format_description *desc = util_format_description(format);
383
384 assert(format);
385 if (!format) {
386 return FALSE;
387 }
388
389 switch (desc->colorspace) {
390 case UTIL_FORMAT_COLORSPACE_RGB:
391 case UTIL_FORMAT_COLORSPACE_SRGB:
392 return desc->swizzle[3] != UTIL_FORMAT_SWIZZLE_1;
393 case UTIL_FORMAT_COLORSPACE_YUV:
394 return FALSE;
395 case UTIL_FORMAT_COLORSPACE_ZS:
396 return FALSE;
397 default:
398 assert(0);
399 return FALSE;
400 }
401 }
402
403
404 /*
405 * Format access functions.
406 */
407
408 void
409 util_format_read_4f(enum pipe_format format,
410 float *dst, unsigned dst_stride,
411 const void *src, unsigned src_stride,
412 unsigned x, unsigned y, unsigned w, unsigned h);
413
414 void
415 util_format_write_4f(enum pipe_format format,
416 const float *src, unsigned src_stride,
417 void *dst, unsigned dst_stride,
418 unsigned x, unsigned y, unsigned w, unsigned h);
419
420 void
421 util_format_read_4ub(enum pipe_format format,
422 uint8_t *dst, unsigned dst_stride,
423 const void *src, unsigned src_stride,
424 unsigned x, unsigned y, unsigned w, unsigned h);
425
426 void
427 util_format_write_4ub(enum pipe_format format,
428 const uint8_t *src, unsigned src_stride,
429 void *dst, unsigned dst_stride,
430 unsigned x, unsigned y, unsigned w, unsigned h);
431
432 #ifdef __cplusplus
433 } // extern "C" {
434 #endif
435
436 #endif /* ! U_FORMAT_H */