summaryrefslogtreecommitdiff
path: root/src/glitz_format.c
blob: 0e4e02087f0e4860e1e08d6b907417f71896faf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * Copyright © 2004 David Reveman
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * David Reveman not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * David Reveman makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL DAVID REVEMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: David Reveman <davidr@novell.com>
 */

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#include "glitzint.h"

#include <stdlib.h>

static struct _texture_format {
    glitz_gl_int_t texture_format;
    glitz_format_t format;
} _texture_formats[] = {
    { GLITZ_GL_ALPHA4,   { 0, { GLITZ_FOURCC_RGB,  0,  0,  0,  4 } } },
    { GLITZ_GL_ALPHA8,   { 0, { GLITZ_FOURCC_RGB,  0,  0,  0,  8 } } },
    { GLITZ_GL_ALPHA12,  { 0, { GLITZ_FOURCC_RGB,  0,  0,  0, 12 } } },
    { GLITZ_GL_ALPHA16,  { 0, { GLITZ_FOURCC_RGB,  0,  0,  0, 16 } } },
    { GLITZ_GL_R3_G3_B2, { 0, { GLITZ_FOURCC_RGB,  3,  3,  2,  0 } } },
    { GLITZ_GL_RGB4,     { 0, { GLITZ_FOURCC_RGB,  4,  4,  4,  0 } } },
    { GLITZ_GL_RGB5,     { 0, { GLITZ_FOURCC_RGB,  5,  6,  5,  0 } } },
    { GLITZ_GL_RGB8,     { 0, { GLITZ_FOURCC_RGB,  8,  8,  8,  0 } } },
    { GLITZ_GL_RGB10,    { 0, { GLITZ_FOURCC_RGB, 10, 10, 10,  0 } } },
    { GLITZ_GL_RGB12,    { 0, { GLITZ_FOURCC_RGB, 12, 12, 12,  0 } } },
    { GLITZ_GL_RGB16,    { 0, { GLITZ_FOURCC_RGB, 16, 16, 16,  0 } } },
    { GLITZ_GL_RGBA2,    { 0, { GLITZ_FOURCC_RGB,  2,  2,  2,  2 } } },
    { GLITZ_GL_RGB5_A1,  { 0, { GLITZ_FOURCC_RGB,  5,  5,  5,  1 } } },
    { GLITZ_GL_RGBA4,    { 0, { GLITZ_FOURCC_RGB,  4,  4,  4,  4 } } },
    { GLITZ_GL_RGBA8,    { 0, { GLITZ_FOURCC_RGB,  8,  8,  8,  8 } } },
    { GLITZ_GL_RGB10_A2, { 0, { GLITZ_FOURCC_RGB, 10, 10, 10,  2 } } },
    { GLITZ_GL_RGBA12,   { 0, { GLITZ_FOURCC_RGB, 12, 12, 12, 12 } } },
    { GLITZ_GL_RGBA16,   { 0, { GLITZ_FOURCC_RGB, 16, 16, 16, 16 } } }
};

static glitz_format_t _texture_format_yv12 = {
    0, { GLITZ_FOURCC_YV12,  0,  0,  0,  0 }
};

static void
_glitz_add_texture_format (glitz_format_t **formats,
			   glitz_gl_int_t **texture_formats,
			   int            *n_formats,
			   glitz_gl_int_t texture_format,
			   glitz_format_t *format)
{
    *formats = realloc (*formats, sizeof (glitz_format_t) * (*n_formats + 1));
    *texture_formats = realloc (*texture_formats,
				sizeof (glitz_gl_enum_t) * (*n_formats + 1));

    if (*formats && *texture_formats) {
	(*texture_formats)[*n_formats] = texture_format;
	(*formats)[*n_formats] = *format;
	(*formats)[*n_formats].id = *n_formats;
	(*n_formats)++;
    } else
	*n_formats = 0;
}

void
glitz_create_surface_formats (glitz_gl_proc_address_list_t *gl,
			      glitz_format_t               **formats,
			      glitz_gl_int_t               **texture_formats,
			      int                          *n_formats,
			      unsigned long                features)
{
    glitz_gl_int_t value;
    int i, n_texture_formats;

    n_texture_formats =
	sizeof (_texture_formats) / sizeof (struct _texture_format);

    for (i = 0; i < n_texture_formats; i++) {
	switch (_texture_formats[i].format.color.fourcc) {
	case GLITZ_FOURCC_RGB:
	    gl->tex_image_2d (GLITZ_GL_PROXY_TEXTURE_2D, 0,
			      _texture_formats[i].texture_format, 1, 1, 0,
			      GLITZ_GL_RGBA, GLITZ_GL_UNSIGNED_BYTE, NULL);

	    if (_texture_formats[i].format.color.red_size) {
		gl->get_tex_level_parameter_iv (GLITZ_GL_PROXY_TEXTURE_2D, 0,
						GLITZ_GL_TEXTURE_RED_SIZE,
						&value);
		if (value != _texture_formats[i].format.color.red_size)
		    continue;
	    }

	    if (_texture_formats[i].format.color.green_size) {
		gl->get_tex_level_parameter_iv (GLITZ_GL_PROXY_TEXTURE_2D, 0,
						GLITZ_GL_TEXTURE_GREEN_SIZE,
						&value);
		if (value != _texture_formats[i].format.color.green_size)
		    continue;
	    }

	    if (_texture_formats[i].format.color.blue_size) {
		gl->get_tex_level_parameter_iv (GLITZ_GL_PROXY_TEXTURE_2D, 0,
						GLITZ_GL_TEXTURE_BLUE_SIZE,
						&value);
		if (value != _texture_formats[i].format.color.blue_size)
		    continue;
	    }

	    if (_texture_formats[i].format.color.alpha_size) {
		gl->get_tex_level_parameter_iv (GLITZ_GL_PROXY_TEXTURE_2D, 0,
						GLITZ_GL_TEXTURE_ALPHA_SIZE,
						&value);
		if (value != _texture_formats[i].format.color.alpha_size)
		    continue;
	    }
	    break;
	default:
	    continue;
	}

	_glitz_add_texture_format (formats,
				   texture_formats,
				   n_formats,
				   _texture_formats[i].texture_format,
				   &_texture_formats[i].format);
    }

    /* formats used for YUV surfaces */
    if (features & GLITZ_FEATURE_FRAGMENT_PROGRAM_MASK)
    {
	_glitz_add_texture_format (formats, texture_formats, n_formats,
				   GLITZ_GL_LUMINANCE8, &_texture_format_yv12);
    }
}

static void
_glitz_add_drawable_format (glitz_int_drawable_format_t *format,
			    glitz_int_drawable_format_t **formats,
			    int                         *n_formats)
{
    void *ptr;

    ptr = realloc (*formats,
		   sizeof (glitz_int_drawable_format_t) * (*n_formats + 1));
    if (ptr)
    {
	*formats = ptr;
	(*formats)[*n_formats] = *format;
	(*n_formats)++;
    }
}

/* TODO: Available drawable formats needs to be validated in a similar way
   as surface formats. */
void
_glitz_add_drawable_formats (glitz_gl_proc_address_list_t *gl,
			     unsigned long		  feature_mask,
			     glitz_int_drawable_format_t  **formats,
			     int                          *n_formats)
{
    if (feature_mask & GLITZ_FEATURE_FRAMEBUFFER_OBJECT_MASK)
    {
	glitz_int_drawable_format_t format;
	glitz_drawable_format_t     d[] = {
	    { 0, { GLITZ_FOURCC_RGB, 8, 8, 8, 0 }, 32, 0,  0, 1, 0, GLITZ_PIXEL_SCANLINE_ORDER_TOP_DOWN },
	    { 0, { GLITZ_FOURCC_RGB, 8, 8, 8, 8 }, 32, 0,  0, 1, 0, GLITZ_PIXEL_SCANLINE_ORDER_TOP_DOWN },
	    { 0, { GLITZ_FOURCC_RGB, 8, 8, 8, 0 }, 32, 24, 8, 1, 1, GLITZ_PIXEL_SCANLINE_ORDER_TOP_DOWN },
	    { 0, { GLITZ_FOURCC_RGB, 8, 8, 8, 8 }, 32, 24, 8, 1, 1, GLITZ_PIXEL_SCANLINE_ORDER_TOP_DOWN }
	};
	int			    i;

	format.types  = GLITZ_DRAWABLE_TYPE_FBO_MASK;
	format.caveat = 0;
	format.u.val  = 0;

	for (i = 0; i < sizeof (d) / sizeof (d[0]); i++)
	{
	    format.d    = d[i];
	    format.d.id = *n_formats;

	    _glitz_add_drawable_format (&format, formats, n_formats);
	}
    }
}

void
glitz_drawable_format_copy (const glitz_drawable_format_t *src,
			    glitz_drawable_format_t	  *dst,
			    unsigned long		  mask)
{
    if (mask & GLITZ_FORMAT_ID_MASK)
	dst->id = src->id;

    if (mask & GLITZ_FORMAT_FOURCC_MASK)
	dst->color.fourcc = src->color.fourcc;

    if (mask & GLITZ_FORMAT_RED_SIZE_MASK)
	dst->color.red_size = src->color.red_size;

    if (mask & GLITZ_FORMAT_GREEN_SIZE_MASK)
	dst->color.green_size = src->color.green_size;

    if (mask & GLITZ_FORMAT_BLUE_SIZE_MASK)
	dst->color.blue_size = src->color.blue_size;

    if (mask & GLITZ_FORMAT_ALPHA_SIZE_MASK)
	dst->color.alpha_size = src->color.alpha_size;

    if (mask & GLITZ_FORMAT_DEPTH_SIZE_MASK)
	dst->depth_size = src->depth_size;

    if (mask & GLITZ_FORMAT_STENCIL_SIZE_MASK)
	dst->stencil_size = src->stencil_size;

    if (mask & GLITZ_FORMAT_DOUBLEBUFFER_MASK)
	dst->doublebuffer = src->doublebuffer;

    if (mask & GLITZ_FORMAT_SAMPLES_MASK)
	dst->samples = src->samples;
                    
    if (mask & GLITZ_FORMAT_SCANLINE_ORDER_MASK)
	dst->scanline_order = src->scanline_order;
    
    if (mask & GLITZ_FORMAT_DEPTH_MASK)
	dst->depth = src->depth;
}

glitz_drawable_format_t *
glitz_drawable_format_find (glitz_int_drawable_format_t       *formats,
			    int                               n_formats,
			    unsigned long                     mask,
			    const glitz_int_drawable_format_t *templ,
			    int                               count)
{
    for (; n_formats; n_formats--, formats++)
    {
	if (mask & GLITZ_FORMAT_ID_MASK)
	    if (templ->d.id != formats->d.id)
		continue;

	if (mask & GLITZ_FORMAT_FOURCC_MASK)
	    if (templ->d.color.fourcc != formats->d.color.fourcc)
		continue;

	if (mask & GLITZ_FORMAT_RED_SIZE_MASK)
	    if (templ->d.color.red_size != formats->d.color.red_size)
		continue;

	if (mask & GLITZ_FORMAT_GREEN_SIZE_MASK)
	    if (templ->d.color.green_size != formats->d.color.green_size)
		continue;

	if (mask & GLITZ_FORMAT_BLUE_SIZE_MASK)
	    if (templ->d.color.blue_size != formats->d.color.blue_size)
		continue;

	if (mask & GLITZ_FORMAT_ALPHA_SIZE_MASK)
	    if (templ->d.color.alpha_size != formats->d.color.alpha_size)
		continue;

	if (mask & GLITZ_FORMAT_DEPTH_SIZE_MASK)
	    if (templ->d.depth_size != formats->d.depth_size)
		continue;

	if (mask & GLITZ_FORMAT_STENCIL_SIZE_MASK)
	    if (templ->d.stencil_size != formats->d.stencil_size)
		continue;

	if (mask & GLITZ_FORMAT_DOUBLEBUFFER_MASK)
	    if (templ->d.doublebuffer != formats->d.doublebuffer)
		continue;

	if (mask & GLITZ_FORMAT_SAMPLES_MASK)
	    if (templ->d.samples != formats->d.samples)
		continue;
	
	if (mask & GLITZ_FORMAT_SCANLINE_ORDER_MASK)
	    if (templ->d.scanline_order != formats->d.scanline_order)
		continue;
	
	if (mask & GLITZ_FORMAT_DEPTH_MASK)
	    if (templ->d.depth != formats->d.depth)
		continue;

	if (mask & GLITZ_INT_FORMAT_WINDOW_MASK)
	    if ((templ->types   & GLITZ_DRAWABLE_TYPE_WINDOW_MASK) !=
		(formats->types & GLITZ_DRAWABLE_TYPE_WINDOW_MASK))
		continue;

	if (mask & GLITZ_INT_FORMAT_PBUFFER_MASK)
	    if ((templ->types   & GLITZ_DRAWABLE_TYPE_PBUFFER_MASK) !=
		(formats->types & GLITZ_DRAWABLE_TYPE_PBUFFER_MASK))
		continue;

	if (mask & GLITZ_INT_FORMAT_FBO_MASK)
	    if ((templ->types   & GLITZ_DRAWABLE_TYPE_FBO_MASK) !=
		(formats->types & GLITZ_DRAWABLE_TYPE_FBO_MASK))
		continue;

	if (count-- == 0)
	    return &formats->d;
    }

    return NULL;
}

static glitz_format_t *
_glitz_format_find (glitz_format_t       *formats,
		    int                  n_formats,
		    unsigned long        mask,
		    const glitz_format_t *templ,
		    int                  count)
{
    for (; n_formats; n_formats--, formats++) {
	if (mask & GLITZ_FORMAT_ID_MASK)
	    if (templ->id != formats->id)
		continue;

	if (mask & GLITZ_FORMAT_FOURCC_MASK)
	    if (templ->color.fourcc != formats->color.fourcc)
		continue;

	if (mask & GLITZ_FORMAT_RED_SIZE_MASK)
	    if (templ->color.red_size != formats->color.red_size)
		continue;

	if (mask & GLITZ_FORMAT_GREEN_SIZE_MASK)
	    if (templ->color.green_size != formats->color.green_size)
		continue;

	if (mask & GLITZ_FORMAT_BLUE_SIZE_MASK)
	    if (templ->color.blue_size != formats->color.blue_size)
		continue;

	if (mask & GLITZ_FORMAT_ALPHA_SIZE_MASK)
	    if (templ->color.alpha_size != formats->color.alpha_size)
		continue;

	if (count-- == 0)
	    return formats;
    }

    return NULL;
}

glitz_format_t *
glitz_find_format (glitz_drawable_t     *drawable,
		   unsigned long        mask,
		   const glitz_format_t *templ,
		   int                  count)
{
    return _glitz_format_find (drawable->backend->formats,
			       drawable->backend->n_formats,
			       mask, templ, count);
}

glitz_format_t *
glitz_find_standard_format (glitz_drawable_t    *drawable,
			    glitz_format_name_t format_name)
{
    glitz_format_t templ;
    unsigned long mask = GLITZ_FORMAT_RED_SIZE_MASK |
	GLITZ_FORMAT_GREEN_SIZE_MASK | GLITZ_FORMAT_BLUE_SIZE_MASK |
	GLITZ_FORMAT_ALPHA_SIZE_MASK | GLITZ_FORMAT_FOURCC_MASK;

    templ.color.fourcc = GLITZ_FOURCC_RGB;
    templ.color.red_size = 0;
    templ.color.green_size = 0;
    templ.color.blue_size = 0;
    templ.color.alpha_size = 0;

    switch (format_name) {
    case GLITZ_STANDARD_ARGB32:
	templ.color.red_size = 8;
	templ.color.green_size = 8;
	templ.color.blue_size = 8;
	templ.color.alpha_size = 8;
	break;
    case GLITZ_STANDARD_RGB24:
	templ.color.red_size = 8;
	templ.color.green_size = 8;
	templ.color.blue_size = 8;
	break;
    case GLITZ_STANDARD_A8:
	templ.color.alpha_size = 8;
	break;
    case GLITZ_STANDARD_A1:
	templ.color.alpha_size = 1;
	break;
    }

    return glitz_find_format (drawable, mask, &templ, 0);
}

glitz_drawable_format_t *
glitz_find_drawable_format (glitz_drawable_t              *other,
			    unsigned long                 mask,
			    const glitz_drawable_format_t *templ,
			    int                           count)
{
    glitz_int_drawable_format_t itempl;

    glitz_drawable_format_copy (templ, &itempl.d, mask);

    itempl.types = GLITZ_DRAWABLE_TYPE_FBO_MASK;
    mask |= GLITZ_INT_FORMAT_FBO_MASK;

    return glitz_drawable_format_find (other->backend->drawable_formats,
				       other->backend->n_drawable_formats,
				       mask, &itempl, count);
}
slim_hidden_def(glitz_find_drawable_format);

glitz_drawable_format_t *
glitz_find_pbuffer_format (glitz_drawable_t              *other,
			   unsigned long                 mask,
			   const glitz_drawable_format_t *templ,
			   int                           count)
{
    glitz_int_drawable_format_t itempl;

    glitz_drawable_format_copy (templ, &itempl.d, mask);

    itempl.types = GLITZ_DRAWABLE_TYPE_PBUFFER_MASK;
    mask |= GLITZ_INT_FORMAT_PBUFFER_MASK;

    return glitz_drawable_format_find (other->backend->drawable_formats,
				       other->backend->n_drawable_formats,
				       mask, &itempl, count);
}
slim_hidden_def(glitz_find_pbuffer_format);

glitz_drawable_format_t *
glitz_find_pixmap_format (glitz_drawable_t              *other,
			   unsigned long                 mask,
			   const glitz_drawable_format_t *templ,
			   int                           count)
{
    glitz_int_drawable_format_t itempl;

    glitz_drawable_format_copy (templ, &itempl.d, mask);

    itempl.types = GLITZ_DRAWABLE_TYPE_WINDOW_MASK;
    mask |= GLITZ_DRAWABLE_TYPE_WINDOW_MASK;

    return glitz_drawable_format_find (other->backend->drawable_formats,
				       other->backend->n_drawable_formats,
				       mask, &itempl, count);
}
slim_hidden_def(glitz_find_pixmap_format);