2 * EGL Configuration (pixel format) functions.
10 #include "eglconfig.h"
11 #include "egldisplay.h"
12 #include "egldriver.h"
13 #include "eglglobals.h"
16 #define MIN2(A, B) (((A) < (B)) ? (A) : (B))
20 SetConfigAttrib(_EGLConfig
*config
, EGLint attr
, EGLint val
)
22 config
->Attrib
[attr
- FIRST_ATTRIB
] = val
;
26 config
->glmode
.rgbBits
= val
;
29 config
->glmode
.alphaBits
= val
;
32 config
->glmode
.blueBits
= val
;
35 config
->glmode
.greenBits
= val
;
38 config
->glmode
.redBits
= val
;
41 config
->glmode
.depthBits
= val
;
43 case EGL_STENCIL_SIZE
:
44 config
->glmode
.stencilBits
= val
;
46 case EGL_CONFIG_CAVEAT
:
52 case EGL_MAX_PBUFFER_HEIGHT
:
54 case EGL_MAX_PBUFFER_PIXELS
:
56 case EGL_MAX_PBUFFER_WIDTH
:
58 case EGL_NATIVE_RENDERABLE
:
60 case EGL_NATIVE_VISUAL_ID
:
62 case EGL_NATIVE_VISUAL_TYPE
:
66 case EGL_SAMPLE_BUFFERS
:
68 case EGL_SURFACE_TYPE
:
69 config
->glmode
.drawableType
= val
;
71 case EGL_TRANSPARENT_TYPE
:
73 case EGL_TRANSPARENT_BLUE_VALUE
:
75 case EGL_TRANSPARENT_GREEN_VALUE
:
77 case EGL_TRANSPARENT_RED_VALUE
:
81 case EGL_BIND_TO_TEXTURE_RGB
:
83 case EGL_BIND_TO_TEXTURE_RGBA
:
85 case EGL_MIN_SWAP_INTERVAL
:
87 case EGL_MAX_SWAP_INTERVAL
:
96 * Init the given _EGLconfig to default values.
97 * \param id the configuration's ID.
100 _eglInitConfig(_EGLConfig
*config
, EGLint id
)
102 memset(config
, 0, sizeof(*config
));
104 SetConfigAttrib(config
, EGL_CONFIG_ID
, id
);
105 SetConfigAttrib(config
, EGL_BIND_TO_TEXTURE_RGB
, EGL_DONT_CARE
);
106 SetConfigAttrib(config
, EGL_BIND_TO_TEXTURE_RGBA
, EGL_DONT_CARE
);
107 SetConfigAttrib(config
, EGL_CONFIG_CAVEAT
, EGL_DONT_CARE
);
108 SetConfigAttrib(config
, EGL_NATIVE_RENDERABLE
, EGL_DONT_CARE
);
109 SetConfigAttrib(config
, EGL_NATIVE_VISUAL_TYPE
, EGL_DONT_CARE
);
110 SetConfigAttrib(config
, EGL_MIN_SWAP_INTERVAL
, EGL_DONT_CARE
);
111 SetConfigAttrib(config
, EGL_MAX_SWAP_INTERVAL
, EGL_DONT_CARE
);
112 SetConfigAttrib(config
, EGL_SURFACE_TYPE
,
113 EGL_SCREEN_BIT_MESA
| EGL_PBUFFER_BIT
|
114 EGL_PIXMAP_BIT
| EGL_WINDOW_BIT
);
115 SetConfigAttrib(config
, EGL_TRANSPARENT_TYPE
, EGL_NONE
);
116 SetConfigAttrib(config
, EGL_TRANSPARENT_RED_VALUE
, EGL_DONT_CARE
);
117 SetConfigAttrib(config
, EGL_TRANSPARENT_GREEN_VALUE
, EGL_DONT_CARE
);
118 SetConfigAttrib(config
, EGL_TRANSPARENT_BLUE_VALUE
, EGL_DONT_CARE
);
123 * Given an EGLConfig handle, return the corresponding _EGLConfig object.
126 _eglLookupConfig(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
)
129 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
130 for (i
= 0; i
< disp
->NumConfigs
; i
++) {
131 if (disp
->Configs
[i
].Handle
== config
) {
132 return disp
->Configs
+ i
;
140 * Add the given _EGLConifg to the given display.
143 _eglAddConfig(_EGLDisplay
*display
, const _EGLConfig
*config
)
145 _EGLConfig
*newConfigs
;
148 n
= display
->NumConfigs
;
150 newConfigs
= (_EGLConfig
*) realloc(display
->Configs
,
151 (n
+ 1) * sizeof(_EGLConfig
));
153 display
->Configs
= newConfigs
;
154 display
->Configs
[n
] = *config
; /* copy struct */
155 display
->NumConfigs
++;
156 return display
->Configs
+ n
;
165 * Parse the attrib_list to fill in the fields of the given _egl_config
166 * Return EGL_FALSE if any errors, EGL_TRUE otherwise.
169 _eglParseConfigAttribs(_EGLConfig
*config
, const EGLint
*attrib_list
)
173 /* XXX set all config attribs to EGL_DONT_CARE */
175 for (i
= 0; attrib_list
&& attrib_list
[i
] != EGL_NONE
; i
++) {
176 EGLint k
= attrib_list
[i
] - FIRST_ATTRIB
;
177 if (k
>= 0 && k
< MAX_ATTRIBS
) {
178 config
->Attrib
[k
] = attrib_list
[++i
];
181 _eglError(EGL_BAD_ATTRIBUTE
, "eglChooseConfig");
198 EGLint MatchCriteria
;
202 /* This encodes the info from Table 3.5 of the EGL spec, ordered by
205 static struct sort_info SortInfo
[] = {
206 { EGL_CONFIG_CAVEAT
, EXACT
, SPECIAL
},
207 { EGL_RED_SIZE
, ATLEAST
, SPECIAL
},
208 { EGL_GREEN_SIZE
, ATLEAST
, SPECIAL
},
209 { EGL_BLUE_SIZE
, ATLEAST
, SPECIAL
},
210 { EGL_ALPHA_SIZE
, ATLEAST
, SPECIAL
},
211 { EGL_BUFFER_SIZE
, ATLEAST
, SMALLER
},
212 { EGL_SAMPLE_BUFFERS
, ATLEAST
, SMALLER
},
213 { EGL_SAMPLES
, ATLEAST
, SMALLER
},
214 { EGL_DEPTH_SIZE
, ATLEAST
, SMALLER
},
215 { EGL_STENCIL_SIZE
, ATLEAST
, SMALLER
},
216 { EGL_NATIVE_VISUAL_TYPE
, EXACT
, SPECIAL
},
217 { EGL_CONFIG_ID
, EXACT
, SMALLER
},
218 { EGL_BIND_TO_TEXTURE_RGB
, EXACT
, NONE
},
219 { EGL_BIND_TO_TEXTURE_RGBA
, EXACT
, NONE
},
220 { EGL_LEVEL
, EXACT
, NONE
},
221 { EGL_NATIVE_RENDERABLE
, EXACT
, NONE
},
222 { EGL_MAX_SWAP_INTERVAL
, EXACT
, NONE
},
223 { EGL_MIN_SWAP_INTERVAL
, EXACT
, NONE
},
224 { EGL_SURFACE_TYPE
, MASK
, NONE
},
225 { EGL_TRANSPARENT_TYPE
, EXACT
, NONE
},
226 { EGL_TRANSPARENT_RED_VALUE
, EXACT
, NONE
},
227 { EGL_TRANSPARENT_GREEN_VALUE
, EXACT
, NONE
},
228 { EGL_TRANSPARENT_BLUE_VALUE
, EXACT
, NONE
},
234 * Return EGL_TRUE if the attributes of c meet or exceed the minimums
238 _eglConfigQualifies(const _EGLConfig
*c
, const _EGLConfig
*min
)
241 for (i
= 0; SortInfo
[i
].Attribute
!= 0; i
++) {
242 const EGLint mv
= GET_CONFIG_ATTRIB(min
, SortInfo
[i
].Attribute
);
243 if (mv
!= EGL_DONT_CARE
) {
244 const EGLint cv
= GET_CONFIG_ATTRIB(c
, SortInfo
[i
].Attribute
);
245 if (SortInfo
[i
].MatchCriteria
== EXACT
) {
250 else if (SortInfo
[i
].MatchCriteria
== ATLEAST
) {
256 assert(SortInfo
[i
].MatchCriteria
== MASK
);
257 if ((mv
& cv
) != mv
) {
268 * Compare configs 'a' and 'b' and return -1 if a belongs before b,
269 * 1 if a belongs after b, or 0 if they're equal.
272 _eglCompareConfigs(const _EGLConfig
*a
, const _EGLConfig
*b
)
275 for (i
= 0; SortInfo
[i
].Attribute
!= 0; i
++) {
276 const EGLint av
= GET_CONFIG_ATTRIB(a
, SortInfo
[i
].Attribute
);
277 const EGLint bv
= GET_CONFIG_ATTRIB(b
, SortInfo
[i
].Attribute
);
278 if (SortInfo
[i
].SortOrder
== SMALLER
) {
283 /* else, continue examining attribute values */
285 else if (SortInfo
[i
].SortOrder
== SPECIAL
) {
286 if (SortInfo
[i
].Attribute
== EGL_CONFIG_CAVEAT
) {
287 /* values are EGL_NONE, SLOW_CONFIG, or NON_CONFORMANT_CONFIG */
293 else if (SortInfo
[i
].Attribute
== EGL_RED_SIZE
||
294 SortInfo
[i
].Attribute
== EGL_GREEN_SIZE
||
295 SortInfo
[i
].Attribute
== EGL_BLUE_SIZE
||
296 SortInfo
[i
].Attribute
== EGL_ALPHA_SIZE
) {
303 assert(SortInfo
[i
].Attribute
== EGL_NATIVE_VISUAL_TYPE
);
311 assert(SortInfo
[i
].SortOrder
== NONE
);
312 /* continue examining attribute values */
320 * Typical fallback routine for eglChooseConfig
323 _eglChooseConfig(_EGLDriver
*drv
, EGLDisplay dpy
, const EGLint
*attrib_list
, EGLConfig
*configs
, EGLint config_size
, EGLint
*num_config
)
325 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
329 /* parse the attrib_list to initialize criteria */
330 if (!_eglParseConfigAttribs(&criteria
, attrib_list
)) {
335 for (i
= 0; i
< disp
->NumConfigs
; i
++) {
336 const _EGLConfig
*conf
= disp
->Configs
+ i
;
337 if (_eglConfigQualifies(conf
, &criteria
)) {
338 if (*num_config
< config_size
) {
340 configs
[*num_config
] = conf
->Handle
;
349 /* XXX sort the list here */
356 * Fallback for eglGetConfigAttrib.
359 _eglGetConfigAttrib(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig config
, EGLint attribute
, EGLint
*value
)
361 const _EGLConfig
*conf
= _eglLookupConfig(drv
, dpy
, config
);
362 const EGLint k
= attribute
- FIRST_ATTRIB
;
363 if (k
>= 0 && k
< MAX_ATTRIBS
) {
364 *value
= conf
->Attrib
[k
];
368 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetConfigAttrib");
375 * Fallback for eglGetConfigs.
378 _eglGetConfigs(_EGLDriver
*drv
, EGLDisplay dpy
, EGLConfig
*configs
, EGLint config_size
, EGLint
*num_config
)
380 _EGLDisplay
*disp
= _eglLookupDisplay(dpy
);
382 if (!drv
->Initialized
) {
383 _eglError(EGL_NOT_INITIALIZED
, "eglGetConfigs");
389 *num_config
= MIN2(disp
->NumConfigs
, config_size
);
390 for (i
= 0; i
< *num_config
; i
++) {
391 configs
[i
] = disp
->Configs
[i
].Handle
;
394 *num_config
= disp
->NumConfigs
;
401 * Creates a set of \c __GLcontextModes that a driver will expose.
403 * A set of \c __GLcontextModes will be created based on the supplied
404 * parameters. The number of modes processed will be 2 *
405 * \c num_depth_stencil_bits * \c num_db_modes.
407 * For the most part, data is just copied from \c depth_bits, \c stencil_bits,
408 * \c db_modes, and \c visType into each \c __GLcontextModes element.
409 * However, the meanings of \c fb_format and \c fb_type require further
410 * explanation. The \c fb_format specifies which color components are in
411 * each pixel and what the default order is. For example, \c GL_RGB specifies
412 * that red, green, blue are available and red is in the "most significant"
413 * position and blue is in the "least significant". The \c fb_type specifies
414 * the bit sizes of each component and the actual ordering. For example, if
415 * \c GL_UNSIGNED_SHORT_5_6_5_REV is specified with \c GL_RGB, bits [15:11]
416 * are the blue value, bits [10:5] are the green value, and bits [4:0] are
419 * One sublte issue is the combination of \c GL_RGB or \c GL_BGR and either
420 * of the \c GL_UNSIGNED_INT_8_8_8_8 modes. The resulting mask values in the
421 * \c __GLcontextModes structure is \b identical to the \c GL_RGBA or
422 * \c GL_BGRA case, except the \c alphaMask is zero. This means that, as
423 * far as this routine is concerned, \c GL_RGB with \c GL_UNSIGNED_INT_8_8_8_8
424 * still uses 32-bits.
426 * If in doubt, look at the tables used in the function.
428 * \param ptr_to_modes Pointer to a pointer to a linked list of
429 * \c __GLcontextModes. Upon completion, a pointer to
430 * the next element to be process will be stored here.
431 * If the function fails and returns \c GL_FALSE, this
432 * value will be unmodified, but some elements in the
433 * linked list may be modified.
434 * \param fb_format Format of the framebuffer. Currently only \c GL_RGB,
435 * \c GL_RGBA, \c GL_BGR, and \c GL_BGRA are supported.
436 * \param fb_type Type of the pixels in the framebuffer. Currently only
437 * \c GL_UNSIGNED_SHORT_5_6_5,
438 * \c GL_UNSIGNED_SHORT_5_6_5_REV,
439 * \c GL_UNSIGNED_INT_8_8_8_8, and
440 * \c GL_UNSIGNED_INT_8_8_8_8_REV are supported.
441 * \param depth_bits Array of depth buffer sizes to be exposed.
442 * \param stencil_bits Array of stencil buffer sizes to be exposed.
443 * \param num_depth_stencil_bits Number of entries in both \c depth_bits and
445 * \param db_modes Array of buffer swap modes. If an element has a
446 * value of \c GLX_NONE, then it represents a
447 * single-buffered mode. Other valid values are
448 * \c GLX_SWAP_EXCHANGE_OML, \c GLX_SWAP_COPY_OML, and
449 * \c GLX_SWAP_UNDEFINED_OML. See the
450 * GLX_OML_swap_method extension spec for more details.
451 * \param num_db_modes Number of entries in \c db_modes.
452 * \param visType GLX visual type. Usually either \c GLX_TRUE_COLOR or
453 * \c GLX_DIRECT_COLOR.
456 * \c GL_TRUE on success or \c GL_FALSE on failure. Currently the only
457 * cause of failure is a bad parameter (i.e., unsupported \c fb_format or
461 * There is currently no way to support packed RGB modes (i.e., modes with
462 * exactly 3 bytes per pixel) or floating-point modes. This could probably
463 * be done by creating some new, private enums with clever names likes
464 * \c GL_UNSIGNED_3BYTE_8_8_8, \c GL_4FLOAT_32_32_32_32,
465 * \c GL_4HALF_16_16_16_16, etc. We can cross that bridge when we come to it.
468 _eglFillInConfigs(_EGLConfig
* configs
,
469 GLenum fb_format
, GLenum fb_type
,
470 const u_int8_t
* depth_bits
, const u_int8_t
* stencil_bits
,
471 unsigned num_depth_stencil_bits
,
472 const GLenum
* db_modes
, unsigned num_db_modes
,
474 static const u_int8_t bits_table
[3][4] = {
476 { 5, 6, 5, 0 }, /* Any GL_UNSIGNED_SHORT_5_6_5 */
477 { 8, 8, 8, 0 }, /* Any RGB with any GL_UNSIGNED_INT_8_8_8_8 */
478 { 8, 8, 8, 8 } /* Any RGBA with any GL_UNSIGNED_INT_8_8_8_8 */
481 /* The following arrays are all indexed by the fb_type masked with 0x07.
482 * Given the four supported fb_type values, this results in valid array
483 * indices of 3, 4, 5, and 7.
485 static const u_int32_t masks_table_rgb
[8][4] = {
486 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
487 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
488 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
489 {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5 */
490 {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5_REV */
491 {0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000}, /* 8_8_8_8 */
492 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
493 {0x000000FF, 0x0000FF00, 0x00FF0000, 0x00000000} /* 8_8_8_8_REV */
496 static const u_int32_t masks_table_rgba
[8][4] = {
497 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
498 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
499 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
500 {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5 */
501 {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5_REV */
502 {0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF}, /* 8_8_8_8 */
503 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
504 {0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000}, /* 8_8_8_8_REV */
507 static const u_int32_t masks_table_bgr
[8][4] = {
508 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
509 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
510 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
511 {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5 */
512 {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5_REV */
513 {0x0000FF00, 0x00FF0000, 0xFF000000, 0x00000000}, /* 8_8_8_8 */
514 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
515 {0x00FF0000, 0x0000FF00, 0x000000FF, 0x00000000}, /* 8_8_8_8_REV */
518 static const u_int32_t masks_table_bgra
[8][4] = {
519 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
520 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
521 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
522 {0x0000001F, 0x000007E0, 0x0000F800, 0x00000000}, /* 5_6_5 */
523 {0x0000F800, 0x000007E0, 0x0000001F, 0x00000000}, /* 5_6_5_REV */
524 {0x0000FF00, 0x00FF0000, 0xFF000000, 0x000000FF}, /* 8_8_8_8 */
525 {0x00000000, 0x00000000, 0x00000000, 0x00000000},
526 {0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000}, /* 8_8_8_8_REV */
529 static const u_int8_t bytes_per_pixel
[8] = {
530 0, 0, 0, 2, 2, 4, 0, 4
533 const u_int8_t
* bits
;
534 const u_int32_t
* masks
;
535 const int index
= fb_type
& 0x07;
541 if ( bytes_per_pixel
[index
] == 0 ) {
542 fprintf(stderr
, "[%s:%u] Framebuffer type 0x%04x has 0 bytes per pixel.\n",
543 __FUNCTION__
, __LINE__
, fb_type
);
547 /* Valid types are GL_UNSIGNED_SHORT_5_6_5 and GL_UNSIGNED_INT_8_8_8_8 and
550 * Valid formats are GL_RGBA, GL_RGB, and GL_BGRA.
552 switch ( fb_format
) {
554 bits
= (bytes_per_pixel
[index
] == 2) ? bits_table
[0] : bits_table
[1];
555 masks
= masks_table_rgb
[index
];
559 bits
= (bytes_per_pixel
[index
] == 2) ? bits_table
[0] : bits_table
[2];
560 masks
= masks_table_rgba
[index
];
564 bits
= (bytes_per_pixel
[index
] == 2) ? bits_table
[0] : bits_table
[1];
565 masks
= masks_table_bgr
[index
];
569 bits
= (bytes_per_pixel
[index
] == 2) ? bits_table
[0] : bits_table
[2];
570 masks
= masks_table_bgra
[index
];
574 fprintf(stderr
, "[%s:%u] Framebuffer format 0x%04x is not GL_RGB, GL_RGBA, GL_BGR, or GL_BGRA.\n",
575 __FUNCTION__
, __LINE__
, fb_format
);
580 for (k
= 0; k
< num_depth_stencil_bits
; k
++) {
581 for (i
= 0; i
< num_db_modes
; i
++) {
582 for (j
= 0; j
< 2; j
++) {
584 SetConfigAttrib(config
, EGL_RED_SIZE
, bits
[0]);
585 SetConfigAttrib(config
, EGL_GREEN_SIZE
, bits
[1]);
586 SetConfigAttrib(config
, EGL_BLUE_SIZE
, bits
[2]);
587 SetConfigAttrib(config
, EGL_ALPHA_SIZE
, bits
[3]);
588 config
->glmode
.redMask
= masks
[0];
589 config
->glmode
.greenMask
= masks
[1];
590 config
->glmode
.blueMask
= masks
[2];
591 config
->glmode
.alphaMask
= masks
[3];
592 SetConfigAttrib(config
, EGL_BUFFER_SIZE
,
593 config
->glmode
.redBits
+ config
->glmode
.greenBits
+
594 config
->glmode
.blueBits
+ config
->glmode
.alphaBits
);
596 config
->glmode
.accumRedBits
= 16 * j
;
597 config
->glmode
.accumGreenBits
= 16 * j
;
598 config
->glmode
.accumBlueBits
= 16 * j
;
599 config
->glmode
.accumAlphaBits
= (masks
[3] != 0) ? 16 * j
: 0;
600 config
->glmode
.visualRating
= (j
== 0) ? GLX_NONE
: GLX_SLOW_CONFIG
;
602 SetConfigAttrib(config
, EGL_STENCIL_SIZE
, stencil_bits
[k
]);
603 SetConfigAttrib(config
, EGL_DEPTH_SIZE
, depth_bits
[k
]);
605 config
->glmode
.visualType
= visType
;
606 config
->glmode
.renderType
= GLX_RGBA_BIT
;
607 SetConfigAttrib(config
, EGL_SURFACE_TYPE
, EGL_SCREEN_BIT_MESA
|
608 EGL_PBUFFER_BIT
| EGL_PIXMAP_BIT
| EGL_WINDOW_BIT
);
610 config
->glmode
.rgbMode
= GL_TRUE
;
612 if (db_modes
[i
] == GLX_NONE
) {
613 config
->glmode
.doubleBufferMode
= GL_FALSE
;
615 config
->glmode
.doubleBufferMode
= GL_TRUE
;
616 config
->glmode
.swapMethod
= db_modes
[i
];
619 config
->glmode
.haveAccumBuffer
= ((config
->glmode
.accumRedBits
+
620 config
->glmode
.accumGreenBits
+
621 config
->glmode
.accumBlueBits
+
622 config
->glmode
.accumAlphaBits
) > 0);
623 config
->glmode
.haveDepthBuffer
= (config
->glmode
.depthBits
> 0);
624 config
->glmode
.haveStencilBuffer
= (config
->glmode
.stencilBits
> 0);