some initial EGL 1.2 work
[mesa.git] / src / egl / main / eglconfig.c
1 /**
2 * EGL Configuration (pixel format) functions.
3 */
4
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <assert.h>
10 #include "eglconfig.h"
11 #include "egldisplay.h"
12 #include "egldriver.h"
13 #include "eglglobals.h"
14 #include "egllog.h"
15
16
17 #define MIN2(A, B) (((A) < (B)) ? (A) : (B))
18
19
20 /**
21 * Convert an _EGLConfig to a __GLcontextModes object.
22 * NOTE: This routine may be incomplete - we're only making sure that
23 * the fields needed by Mesa (for _mesa_create_context/framebuffer) are
24 * set correctly.
25 */
26 void
27 _eglConfigToContextModesRec(const _EGLConfig *config, __GLcontextModes *mode)
28 {
29 memset(mode, 0, sizeof(*mode));
30
31 mode->rgbMode = GL_TRUE; /* no color index */
32 mode->colorIndexMode = GL_FALSE;
33 mode->doubleBufferMode = GL_TRUE; /* always DB for now */
34 mode->stereoMode = GL_FALSE;
35
36 mode->redBits = GET_CONFIG_ATTRIB(config, EGL_RED_SIZE);
37 mode->greenBits = GET_CONFIG_ATTRIB(config, EGL_GREEN_SIZE);
38 mode->blueBits = GET_CONFIG_ATTRIB(config, EGL_BLUE_SIZE);
39 mode->alphaBits = GET_CONFIG_ATTRIB(config, EGL_ALPHA_SIZE);
40 mode->rgbBits = GET_CONFIG_ATTRIB(config, EGL_BUFFER_SIZE);
41
42 /* no rgba masks - fix? */
43
44 mode->depthBits = GET_CONFIG_ATTRIB(config, EGL_DEPTH_SIZE);
45 mode->haveDepthBuffer = mode->depthBits > 0;
46
47 mode->stencilBits = GET_CONFIG_ATTRIB(config, EGL_STENCIL_SIZE);
48 mode->haveStencilBuffer = mode->stencilBits > 0;
49
50 /* no accum */
51
52 mode->level = GET_CONFIG_ATTRIB(config, EGL_LEVEL);
53 mode->samples = GET_CONFIG_ATTRIB(config, EGL_SAMPLES);
54 mode->sampleBuffers = GET_CONFIG_ATTRIB(config, EGL_SAMPLE_BUFFERS);
55
56 /* surface type - not really needed */
57 mode->visualType = GLX_TRUE_COLOR;
58 mode->renderType = GLX_RGBA_BIT;
59 }
60
61
62 void
63 _eglSetConfigAttrib(_EGLConfig *config, EGLint attr, EGLint val)
64 {
65 assert(attr >= FIRST_ATTRIB);
66 assert(attr < FIRST_ATTRIB + MAX_ATTRIBS);
67 config->Attrib[attr - FIRST_ATTRIB] = val;
68 }
69
70
71 /**
72 * Init the given _EGLconfig to default values.
73 * \param id the configuration's ID.
74 */
75 void
76 _eglInitConfig(_EGLConfig *config, EGLint id)
77 {
78 memset(config, 0, sizeof(*config));
79 config->Handle = id;
80 _eglSetConfigAttrib(config, EGL_CONFIG_ID, id);
81 _eglSetConfigAttrib(config, EGL_BIND_TO_TEXTURE_RGB, EGL_DONT_CARE);
82 _eglSetConfigAttrib(config, EGL_BIND_TO_TEXTURE_RGBA, EGL_DONT_CARE);
83 _eglSetConfigAttrib(config, EGL_CONFIG_CAVEAT, EGL_DONT_CARE);
84 _eglSetConfigAttrib(config, EGL_NATIVE_RENDERABLE, EGL_DONT_CARE);
85 _eglSetConfigAttrib(config, EGL_NATIVE_VISUAL_TYPE, EGL_DONT_CARE);
86 _eglSetConfigAttrib(config, EGL_MIN_SWAP_INTERVAL, EGL_DONT_CARE);
87 _eglSetConfigAttrib(config, EGL_MAX_SWAP_INTERVAL, EGL_DONT_CARE);
88 _eglSetConfigAttrib(config, EGL_SURFACE_TYPE,
89 EGL_SCREEN_BIT_MESA | EGL_PBUFFER_BIT |
90 EGL_PIXMAP_BIT | EGL_WINDOW_BIT);
91 _eglSetConfigAttrib(config, EGL_TRANSPARENT_TYPE, EGL_NONE);
92 _eglSetConfigAttrib(config, EGL_TRANSPARENT_RED_VALUE, EGL_DONT_CARE);
93 _eglSetConfigAttrib(config, EGL_TRANSPARENT_GREEN_VALUE, EGL_DONT_CARE);
94 _eglSetConfigAttrib(config, EGL_TRANSPARENT_BLUE_VALUE, EGL_DONT_CARE);
95 #ifdef EGL_VERSION_1_2
96 _eglSetConfigAttrib(config, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
97 _eglSetConfigAttrib(config, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT);
98 #endif /* EGL_VERSION_1_2 */
99 }
100
101
102 /**
103 * Given an EGLConfig handle, return the corresponding _EGLConfig object.
104 */
105 _EGLConfig *
106 _eglLookupConfig(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config)
107 {
108 EGLint i;
109 _EGLDisplay *disp = _eglLookupDisplay(dpy);
110 for (i = 0; i < disp->NumConfigs; i++) {
111 if (disp->Configs[i].Handle == config) {
112 return disp->Configs + i;
113 }
114 }
115 return NULL;
116 }
117
118
119 /**
120 * Add the given _EGLConfig to the given display.
121 */
122 _EGLConfig *
123 _eglAddConfig(_EGLDisplay *display, const _EGLConfig *config)
124 {
125 _EGLConfig *newConfigs;
126 EGLint n;
127
128 n = display->NumConfigs;
129
130 newConfigs = (_EGLConfig *) realloc(display->Configs,
131 (n + 1) * sizeof(_EGLConfig));
132 if (newConfigs) {
133 display->Configs = newConfigs;
134 display->Configs[n] = *config; /* copy struct */
135 display->Configs[n].Handle = n;
136 display->NumConfigs++;
137 return display->Configs + n;
138 }
139 else {
140 return NULL;
141 }
142 }
143
144
145 /**
146 * Parse the attrib_list to fill in the fields of the given _eglConfig
147 * Return EGL_FALSE if any errors, EGL_TRUE otherwise.
148 */
149 EGLBoolean
150 _eglParseConfigAttribs(_EGLConfig *config, const EGLint *attrib_list)
151 {
152 EGLint i;
153
154 /* set all config attribs to EGL_DONT_CARE */
155 for (i = 0; i < MAX_ATTRIBS; i++) {
156 config->Attrib[i] = EGL_DONT_CARE;
157 }
158
159 for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
160 const EGLint attr = attrib_list[i];
161 if (attr >= EGL_BUFFER_SIZE &&
162 attr <= EGL_MAX_SWAP_INTERVAL) {
163 EGLint k = attr - FIRST_ATTRIB;
164 assert(k >= 0);
165 assert(k < MAX_ATTRIBS);
166 config->Attrib[k] = attrib_list[++i];
167 }
168 #ifdef EGL_VERSION_1_2
169 else if (attr == EGL_COLOR_BUFFER_TYPE) {
170 EGLint bufType = attrib_list[++i];
171 if (bufType != EGL_RGB_BUFFER && bufType != EGL_LUMINANCE_BUFFER) {
172 _eglError(EGL_BAD_ATTRIBUTE, "eglChooseConfig");
173 return EGL_FALSE;
174 }
175 _eglSetConfigAttrib(config, EGL_COLOR_BUFFER_TYPE, bufType);
176 }
177 else if (attr == EGL_RENDERABLE_TYPE) {
178 EGLint renType = attrib_list[++i];
179 if (renType & ~(EGL_OPENGL_ES_BIT | EGL_OPENVG_BIT)) {
180 _eglError(EGL_BAD_ATTRIBUTE, "eglChooseConfig");
181 return EGL_FALSE;
182 }
183 _eglSetConfigAttrib(config, EGL_RENDERABLE_TYPE, renType);
184 }
185 else if (attr == EGL_ALPHA_MASK_SIZE ||
186 attr == EGL_LUMINANCE_SIZE) {
187 EGLint value = attrib_list[++i];
188 _eglSetConfigAttrib(config, attr, value);
189 }
190 #endif /* EGL_VERSION_1_2 */
191 else {
192 _eglError(EGL_BAD_ATTRIBUTE, "eglChooseConfig");
193 return EGL_FALSE;
194 }
195 }
196 return EGL_TRUE;
197 }
198
199
200 #define EXACT 1
201 #define ATLEAST 2
202 #define MASK 3
203 #define SMALLER 4
204 #define SPECIAL 5
205 #define NONE 6
206
207 struct sort_info {
208 EGLint Attribute;
209 EGLint MatchCriteria;
210 EGLint SortOrder;
211 };
212
213 /* This encodes the info from Table 3.5 of the EGL spec, ordered by
214 * Sort Priority.
215 *
216 * XXX To do: EGL 1.2 attribs
217 */
218 static struct sort_info SortInfo[] = {
219 { EGL_CONFIG_CAVEAT, EXACT, SPECIAL },
220 { EGL_RED_SIZE, ATLEAST, SPECIAL },
221 { EGL_GREEN_SIZE, ATLEAST, SPECIAL },
222 { EGL_BLUE_SIZE, ATLEAST, SPECIAL },
223 { EGL_ALPHA_SIZE, ATLEAST, SPECIAL },
224 { EGL_BUFFER_SIZE, ATLEAST, SMALLER },
225 { EGL_SAMPLE_BUFFERS, ATLEAST, SMALLER },
226 { EGL_SAMPLES, ATLEAST, SMALLER },
227 { EGL_DEPTH_SIZE, ATLEAST, SMALLER },
228 { EGL_STENCIL_SIZE, ATLEAST, SMALLER },
229 { EGL_NATIVE_VISUAL_TYPE, EXACT, SPECIAL },
230 { EGL_CONFIG_ID, EXACT, SMALLER },
231 { EGL_BIND_TO_TEXTURE_RGB, EXACT, NONE },
232 { EGL_BIND_TO_TEXTURE_RGBA, EXACT, NONE },
233 { EGL_LEVEL, EXACT, NONE },
234 { EGL_NATIVE_RENDERABLE, EXACT, NONE },
235 { EGL_MAX_SWAP_INTERVAL, EXACT, NONE },
236 { EGL_MIN_SWAP_INTERVAL, EXACT, NONE },
237 { EGL_SURFACE_TYPE, MASK, NONE },
238 { EGL_TRANSPARENT_TYPE, EXACT, NONE },
239 { EGL_TRANSPARENT_RED_VALUE, EXACT, NONE },
240 { EGL_TRANSPARENT_GREEN_VALUE, EXACT, NONE },
241 { EGL_TRANSPARENT_BLUE_VALUE, EXACT, NONE },
242 { 0, 0, 0 }
243 };
244
245
246 /**
247 * Return EGL_TRUE if the attributes of c meet or exceed the minimums
248 * specified by min.
249 */
250 static EGLBoolean
251 _eglConfigQualifies(const _EGLConfig *c, const _EGLConfig *min)
252 {
253 EGLint i;
254 for (i = 0; SortInfo[i].Attribute != 0; i++) {
255 const EGLint mv = GET_CONFIG_ATTRIB(min, SortInfo[i].Attribute);
256 if (mv != EGL_DONT_CARE) {
257 const EGLint cv = GET_CONFIG_ATTRIB(c, SortInfo[i].Attribute);
258 if (SortInfo[i].MatchCriteria == EXACT) {
259 if (cv != mv) {
260 return EGL_FALSE;
261 }
262 }
263 else if (SortInfo[i].MatchCriteria == ATLEAST) {
264 if (cv < mv) {
265 return EGL_FALSE;
266 }
267 }
268 else {
269 assert(SortInfo[i].MatchCriteria == MASK);
270 if ((mv & cv) != mv) {
271 return EGL_FALSE;
272 }
273 }
274 }
275 }
276 return EGL_TRUE;
277 }
278
279
280 /**
281 * Compare configs 'a' and 'b' and return -1 if a belongs before b,
282 * 1 if a belongs after b, or 0 if they're equal.
283 * Used by qsort().
284 */
285 static int
286 _eglCompareConfigs(const void *a, const void *b)
287 {
288 const _EGLConfig *aConfig = (const _EGLConfig *) a;
289 const _EGLConfig *bConfig = (const _EGLConfig *) b;
290 EGLint i;
291
292 for (i = 0; SortInfo[i].Attribute != 0; i++) {
293 const EGLint aVal = GET_CONFIG_ATTRIB(aConfig, SortInfo[i].Attribute);
294 const EGLint bVal = GET_CONFIG_ATTRIB(bConfig, SortInfo[i].Attribute);
295 if (SortInfo[i].SortOrder == SMALLER) {
296 if (aVal < bVal)
297 return -1;
298 else if (aVal > bVal)
299 return 1;
300 /* else, continue examining attribute values */
301 }
302 else if (SortInfo[i].SortOrder == SPECIAL) {
303 if (SortInfo[i].Attribute == EGL_CONFIG_CAVEAT) {
304 /* values are EGL_NONE, SLOW_CONFIG, or NON_CONFORMANT_CONFIG */
305 if (aVal < bVal)
306 return -1;
307 else if (aVal > bVal)
308 return 1;
309 }
310 else if (SortInfo[i].Attribute == EGL_RED_SIZE ||
311 SortInfo[i].Attribute == EGL_GREEN_SIZE ||
312 SortInfo[i].Attribute == EGL_BLUE_SIZE ||
313 SortInfo[i].Attribute == EGL_ALPHA_SIZE) {
314 if (aVal > bVal)
315 return -1;
316 else if (aVal < bVal)
317 return 1;
318 }
319 else {
320 assert(SortInfo[i].Attribute == EGL_NATIVE_VISUAL_TYPE);
321 if (aVal < bVal)
322 return -1;
323 else if (aVal > bVal)
324 return 1;
325 }
326 }
327 else {
328 assert(SortInfo[i].SortOrder == NONE);
329 /* continue examining attribute values */
330 }
331 }
332
333 /* all attributes identical */
334 return 0;
335 }
336
337
338 /**
339 * Typical fallback routine for eglChooseConfig
340 */
341 EGLBoolean
342 _eglChooseConfig(_EGLDriver *drv, EGLDisplay dpy, const EGLint *attrib_list,
343 EGLConfig *configs, EGLint config_size, EGLint *num_configs)
344 {
345 _EGLDisplay *disp = _eglLookupDisplay(dpy);
346 _EGLConfig **configList, criteria;
347 EGLint i, count;
348
349 /* parse the attrib_list to initialize criteria */
350 if (!_eglParseConfigAttribs(&criteria, attrib_list)) {
351 return EGL_FALSE;
352 }
353
354 /* allocate array of config pointers */
355 configList = (_EGLConfig **) malloc(config_size * sizeof(_EGLConfig *));
356 if (!configList) {
357 _eglError(EGL_BAD_CONFIG, "eglChooseConfig(out of memory)");
358 return EGL_FALSE;
359 }
360
361 /* make array of pointers to qualifying configs */
362 for (i = count = 0; i < disp->NumConfigs && count < config_size; i++) {
363 if (_eglConfigQualifies(disp->Configs + i, &criteria)) {
364 configList[count++] = disp->Configs + i;
365 }
366 }
367
368 /* sort array of pointers */
369 qsort(configList, count, sizeof(_EGLConfig *), _eglCompareConfigs);
370
371 /* copy config handles to output array */
372 for (i = 0; i < count; i++) {
373 configs[i] = configList[i]->Handle;
374 }
375
376 free(configList);
377
378 *num_configs = count;
379
380 return EGL_TRUE;
381 }
382
383
384 /**
385 * Fallback for eglGetConfigAttrib.
386 */
387 EGLBoolean
388 _eglGetConfigAttrib(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
389 EGLint attribute, EGLint *value)
390 {
391 const _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
392 const EGLint k = attribute - FIRST_ATTRIB;
393 if (k >= 0 && k < MAX_ATTRIBS) {
394 *value = conf->Attrib[k];
395 return EGL_TRUE;
396 }
397 else {
398 _eglError(EGL_BAD_ATTRIBUTE, "eglGetConfigAttrib");
399 return EGL_FALSE;
400 }
401 }
402
403
404 /**
405 * Fallback for eglGetConfigs.
406 */
407 EGLBoolean
408 _eglGetConfigs(_EGLDriver *drv, EGLDisplay dpy, EGLConfig *configs,
409 EGLint config_size, EGLint *num_config)
410 {
411 _EGLDisplay *disp = _eglLookupDisplay(dpy);
412
413 if (!drv->Initialized) {
414 _eglError(EGL_NOT_INITIALIZED, "eglGetConfigs");
415 return EGL_FALSE;
416 }
417
418 if (configs) {
419 EGLint i;
420 *num_config = MIN2(disp->NumConfigs, config_size);
421 for (i = 0; i < *num_config; i++) {
422 configs[i] = disp->Configs[i].Handle;
423 }
424 }
425 else {
426 /* just return total number of supported configs */
427 *num_config = disp->NumConfigs;
428 }
429
430 return EGL_TRUE;
431 }
432
433
434 /**
435 * Creates a set of \c __GLcontextModes that a driver will expose.
436 *
437 * A set of \c __GLcontextModes will be created based on the supplied
438 * parameters. The number of modes processed will be 2 *
439 * \c num_depth_stencil_bits * \c num_db_modes.
440 *
441 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
442 * \c db_modes, and \c visType into each \c __GLcontextModes element.
443 * However, the meanings of \c fb_format and \c fb_type require further
444 * explanation. The \c fb_format specifies which color components are in
445 * each pixel and what the default order is. For example, \c GL_RGB specifies
446 * that red, green, blue are available and red is in the "most significant"
447 * position and blue is in the "least significant". The \c fb_type specifies
448 * the bit sizes of each component and the actual ordering. For example, if
449 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
450 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
451 * the red value.
452 *
453 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
454 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
455 * \c __GLcontextModes structure is \b identical to the \c GL_RGBA or
456 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
457 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
458 * still uses 32-bits.
459 *
460 * If in doubt, look at the tables used in the function.
461 *
462 * \param ptr_to_modes Pointer to a pointer to a linked list of
463 * \c __GLcontextModes. Upon completion, a pointer to
464 * the next element to be process will be stored here.
465 * If the function fails and returns \c GL_FALSE, this
466 * value will be unmodified, but some elements in the
467 * linked list may be modified.
468 * \param fb_format Format of the framebuffer. Currently only \c GL_RGB,
469 * \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
470 * \param fb_type Type of the pixels in the framebuffer. Currently only
471 * \c GL_UNSIGNED_SHORT_5_6_5,
472 * \c GL_UNSIGNED_SHORT_5_6_5_REV,
473 * \c GL_UNSIGNED_INT_8_8_8_8, and
474 * \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
475 * \param depth_bits Array of depth buffer sizes to be exposed.
476 * \param stencil_bits Array of stencil buffer sizes to be exposed.
477 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
478 * \c stencil_bits.
479 * \param db_modes Array of buffer swap modes. If an element has a
480 * value of \c GLX_NONE, then it represents a
481 * single-buffered mode. Other valid values are
482 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
483 * \c GLX_SWAP_UNDEFINED_OML. See the
484 * GLX_OML_swap_method extension spec for more details.
485 * \param num_db_modes Number of entries in \c db_modes.
486 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
487 * \c GLX_DIRECT_COLOR.
488 *
489 * \returns
490 * \c GL_TRUE on success or \c GL_FALSE on failure. Currently the only
491 * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
492 * \c fb_type).
493 *
494 * \todo
495 * There is currently no way to support packed RGB modes (i.e., modes with
496 * exactly 3 bytes per pixel) or floating-point modes. This could probably
497 * be done by creating some new, private enums with clever names likes
498 * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
499 * \c GL_4HALF_16_16_16_16, etc. We can cross that bridge when we come to it.
500 */
501 GLboolean
502 _eglFillInConfigs(_EGLConfig * configs,
503 GLenum fb_format, GLenum fb_type,
504 const u_int8_t * depth_bits, const u_int8_t * stencil_bits,
505 unsigned num_depth_stencil_bits,
506 const GLenum * db_modes, unsigned num_db_modes,
507 int visType)
508 {
509 static const u_int8_t bits_table[3][4] = {
510 /* R G B A */
511 { 5, 6, 5, 0 }, /* Any GL_UNSIGNED_SHORT_5_6_5 */
512 { 8, 8, 8, 0 }, /* Any RGB with any GL_UNSIGNED_INT_8_8_8_8 */
513 { 8, 8, 8, 8 } /* Any RGBA with any GL_UNSIGNED_INT_8_8_8_8 */
514 };
515
516 /* The following arrays are all indexed by the fb_type masked with 0x07.
517 * Given the four supported fb_type values, this results in valid array
518 * indices of 3, 4, 5, and 7.
519 */
520 static const u_int32_t masks_table_rgb[8][4] = {
521 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
522 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
523 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
524 {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5 */
525 {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5_REV */
526 {0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000}, /* 8_8_8_8 */
527 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
528 {0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000} /* 8_8_8_8_REV */
529 };
530
531 static const u_int32_t masks_table_rgba[8][4] = {
532 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
533 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
534 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
535 {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5 */
536 {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5_REV */
537 {0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF}, /* 8_8_8_8 */
538 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
539 {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000}, /* 8_8_8_8_REV */
540 };
541
542 static const u_int32_t masks_table_bgr[8][4] = {
543 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
544 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
545 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
546 {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5 */
547 {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5_REV */
548 {0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000}, /* 8_8_8_8 */
549 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
550 {0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000}, /* 8_8_8_8_REV */
551 };
552
553 static const u_int32_t masks_table_bgra[8][4] = {
554 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
555 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
556 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
557 {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5 */
558 {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5_REV */
559 {0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF}, /* 8_8_8_8 */
560 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
561 {0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000}, /* 8_8_8_8_REV */
562 };
563
564 static const u_int8_t bytes_per_pixel[8] = {
565 0, 0, 0, 2, 2, 4, 0, 4
566 };
567
568 const u_int8_t * bits;
569 const u_int32_t * masks;
570 const int index = fb_type & 0x07;
571 _EGLConfig *config;
572 unsigned i;
573 unsigned j;
574 unsigned k;
575
576 if ( bytes_per_pixel[index] == 0 ) {
577 _eglLog(_EGL_INFO,
578 "[%s:%u] Framebuffer type 0x%04x has 0 bytes per pixel.",
579 __FUNCTION__, __LINE__, fb_type);
580 return GL_FALSE;
581 }
582
583 /* Valid types are GL_UNSIGNED_SHORT_5_6_5 and GL_UNSIGNED_INT_8_8_8_8 and
584 * the _REV versions.
585 *
586 * Valid formats are GL_RGBA, GL_RGB, and GL_BGRA.
587 */
588 switch ( fb_format ) {
589 case GL_RGB:
590 bits = (bytes_per_pixel[index] == 2) ? bits_table[0] : bits_table[1];
591 masks = masks_table_rgb[index];
592 break;
593
594 case GL_RGBA:
595 bits = (bytes_per_pixel[index] == 2) ? bits_table[0] : bits_table[2];
596 masks = masks_table_rgba[index];
597 break;
598
599 case GL_BGR:
600 bits = (bytes_per_pixel[index] == 2) ? bits_table[0] : bits_table[1];
601 masks = masks_table_bgr[index];
602 break;
603
604 case GL_BGRA:
605 bits = (bytes_per_pixel[index] == 2) ? bits_table[0] : bits_table[2];
606 masks = masks_table_bgra[index];
607 break;
608
609 default:
610 _eglLog(_EGL_WARNING,
611 "[%s:%u] Framebuffer format 0x%04x is not GL_RGB, GL_RGBA, GL_BGR, or GL_BGRA.",
612 __FUNCTION__, __LINE__, fb_format);
613 return GL_FALSE;
614 }
615
616 config = configs;
617 for (k = 0; k < num_depth_stencil_bits; k++) {
618 for (i = 0; i < num_db_modes; i++) {
619 for (j = 0; j < 2; j++) {
620 _eglSetConfigAttrib(config, EGL_RED_SIZE, bits[0]);
621 _eglSetConfigAttrib(config, EGL_GREEN_SIZE, bits[1]);
622 _eglSetConfigAttrib(config, EGL_BLUE_SIZE, bits[2]);
623 _eglSetConfigAttrib(config, EGL_ALPHA_SIZE, bits[3]);
624 _eglSetConfigAttrib(config, EGL_BUFFER_SIZE,
625 bits[0] + bits[1] + bits[2] + bits[3]);
626
627 _eglSetConfigAttrib(config, EGL_STENCIL_SIZE, stencil_bits[k]);
628 _eglSetConfigAttrib(config, EGL_DEPTH_SIZE, depth_bits[i]);
629
630 _eglSetConfigAttrib(config, EGL_SURFACE_TYPE, EGL_SCREEN_BIT_MESA |
631 EGL_PBUFFER_BIT | EGL_PIXMAP_BIT | EGL_WINDOW_BIT);
632
633 config++;
634 }
635 }
636 }
637 return GL_TRUE;
638 }