1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
26 **************************************************************************/
30 * This is an EGL driver that wraps GLX. This gives the benefit of being
31 * completely agnostic of the direct rendering implementation.
33 * Authors: Alan Hourihane <alanh@tungstengraphics.com>
42 #include "eglconfig.h"
43 #include "eglcontext.h"
44 #include "egldefines.h"
45 #include "egldisplay.h"
46 #include "egldriver.h"
47 #include "eglcurrent.h"
49 #include "eglsurface.h"
51 #define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
53 #ifndef GLX_VERSION_1_4
54 #error "GL/glx.h must be equal to or greater than GLX 1.4"
58 typedef GLXContext (*GLXCREATECONTEXTPROC
)( Display
*dpy
, XVisualInfo
*vis
, GLXContext shareList
, Bool direct
);
59 typedef void (*GLXDESTROYCONTEXTPROC
)( Display
*dpy
, GLXContext ctx
);
60 typedef Bool (*GLXMAKECURRENTPROC
)( Display
*dpy
, GLXDrawable drawable
, GLXContext ctx
);
61 typedef void (*GLXSWAPBUFFERSPROC
)( Display
*dpy
, GLXDrawable drawable
);
62 typedef GLXPixmap (*GLXCREATEGLXPIXMAPPROC
)( Display
*dpy
, XVisualInfo
*visual
, Pixmap pixmap
);
63 typedef void (*GLXDESTROYGLXPIXMAPPROC
)( Display
*dpy
, GLXPixmap pixmap
);
64 typedef Bool (*GLXQUERYVERSIONPROC
)( Display
*dpy
, int *maj
, int *min
);
65 typedef int (*GLXGETCONFIGPROC
)( Display
*dpy
, XVisualInfo
*visual
, int attrib
, int *value
);
66 typedef void (*GLXWAITGLPROC
)( void );
67 typedef void (*GLXWAITXPROC
)( void );
70 typedef const char *(*GLXQUERYEXTENSIONSSTRINGPROC
)( Display
*dpy
, int screen
);
71 typedef const char *(*GLXQUERYSERVERSTRINGPROC
)( Display
*dpy
, int screen
, int name
);
72 typedef const char *(*GLXGETCLIENTSTRINGPROC
)( Display
*dpy
, int name
);
74 /** subclass of _EGLDriver */
77 _EGLDriver Base
; /**< base class */
82 GLXCREATECONTEXTPROC glXCreateContext
;
83 GLXDESTROYCONTEXTPROC glXDestroyContext
;
84 GLXMAKECURRENTPROC glXMakeCurrent
;
85 GLXSWAPBUFFERSPROC glXSwapBuffers
;
86 GLXCREATEGLXPIXMAPPROC glXCreateGLXPixmap
;
87 GLXDESTROYGLXPIXMAPPROC glXDestroyGLXPixmap
;
88 GLXQUERYVERSIONPROC glXQueryVersion
;
89 GLXGETCONFIGPROC glXGetConfig
;
90 GLXWAITGLPROC glXWaitGL
;
91 GLXWAITXPROC glXWaitX
;
94 GLXQUERYEXTENSIONSSTRINGPROC glXQueryExtensionsString
;
95 GLXQUERYSERVERSTRINGPROC glXQueryServerString
;
96 GLXGETCLIENTSTRINGPROC glXGetClientString
;
98 /* GLX 1.3 or (GLX_SGI_make_current_read and GLX_SGIX_fbconfig) */
99 PFNGLXGETFBCONFIGSPROC glXGetFBConfigs
;
100 PFNGLXGETFBCONFIGATTRIBPROC glXGetFBConfigAttrib
;
101 PFNGLXGETVISUALFROMFBCONFIGPROC glXGetVisualFromFBConfig
;
102 PFNGLXCREATEWINDOWPROC glXCreateWindow
;
103 PFNGLXDESTROYWINDOWPROC glXDestroyWindow
;
104 PFNGLXCREATEPIXMAPPROC glXCreatePixmap
;
105 PFNGLXDESTROYPIXMAPPROC glXDestroyPixmap
;
106 PFNGLXCREATEPBUFFERPROC glXCreatePbuffer
;
107 PFNGLXDESTROYPBUFFERPROC glXDestroyPbuffer
;
108 PFNGLXCREATENEWCONTEXTPROC glXCreateNewContext
;
109 PFNGLXMAKECONTEXTCURRENTPROC glXMakeContextCurrent
;
111 /* GLX 1.4 or GLX_ARB_get_proc_address */
112 PFNGLXGETPROCADDRESSPROC glXGetProcAddress
;
114 /* GLX_SGIX_pbuffer */
115 PFNGLXCREATEGLXPBUFFERSGIXPROC glXCreateGLXPbufferSGIX
;
116 PFNGLXDESTROYGLXPBUFFERSGIXPROC glXDestroyGLXPbufferSGIX
;
120 /** driver data of _EGLDisplay */
121 struct GLX_egl_display
124 XVisualInfo
*visuals
;
125 GLXFBConfig
*fbconfigs
;
127 int glx_maj
, glx_min
;
129 const char *extensions
;
131 EGLBoolean have_make_current_read
;
132 EGLBoolean have_fbconfig
;
133 EGLBoolean have_pbuffer
;
135 /* workaround quirks of different GLX implementations */
136 EGLBoolean single_buffered_quirk
;
137 EGLBoolean glx_window_quirk
;
141 /** subclass of _EGLContext */
142 struct GLX_egl_context
144 _EGLContext Base
; /**< base class */
150 /** subclass of _EGLSurface */
151 struct GLX_egl_surface
153 _EGLSurface Base
; /**< base class */
156 GLXDrawable glx_drawable
;
158 void (*destroy
)(Display
*, GLXDrawable
);
162 /** subclass of _EGLConfig */
163 struct GLX_egl_config
165 _EGLConfig Base
; /**< base class */
166 EGLBoolean double_buffered
;
170 /* standard typecasts */
171 _EGL_DRIVER_STANDARD_TYPECASTS(GLX_egl
)
174 GLX_egl_config_index(_EGLConfig
*conf
)
176 struct GLX_egl_config
*GLX_conf
= GLX_egl_config(conf
);
177 return GLX_conf
->index
;
181 static const struct {
184 } fbconfig_attributes
[] = {
185 /* table 3.1 of GLX 1.4 */
186 { GLX_FBCONFIG_ID
, 0 },
187 { GLX_BUFFER_SIZE
, EGL_BUFFER_SIZE
},
188 { GLX_LEVEL
, EGL_LEVEL
},
189 { GLX_DOUBLEBUFFER
, 0 },
191 { GLX_AUX_BUFFERS
, 0 },
192 { GLX_RED_SIZE
, EGL_RED_SIZE
},
193 { GLX_GREEN_SIZE
, EGL_GREEN_SIZE
},
194 { GLX_BLUE_SIZE
, EGL_BLUE_SIZE
},
195 { GLX_ALPHA_SIZE
, EGL_ALPHA_SIZE
},
196 { GLX_DEPTH_SIZE
, EGL_DEPTH_SIZE
},
197 { GLX_STENCIL_SIZE
, EGL_STENCIL_SIZE
},
198 { GLX_ACCUM_RED_SIZE
, 0 },
199 { GLX_ACCUM_GREEN_SIZE
, 0 },
200 { GLX_ACCUM_BLUE_SIZE
, 0 },
201 { GLX_ACCUM_ALPHA_SIZE
, 0 },
202 { GLX_SAMPLE_BUFFERS
, EGL_SAMPLE_BUFFERS
},
203 { GLX_SAMPLES
, EGL_SAMPLES
},
204 { GLX_RENDER_TYPE
, 0 },
205 { GLX_DRAWABLE_TYPE
, EGL_SURFACE_TYPE
},
206 { GLX_X_RENDERABLE
, EGL_NATIVE_RENDERABLE
},
207 { GLX_X_VISUAL_TYPE
, EGL_NATIVE_VISUAL_TYPE
},
208 { GLX_CONFIG_CAVEAT
, EGL_CONFIG_CAVEAT
},
209 { GLX_TRANSPARENT_TYPE
, EGL_TRANSPARENT_TYPE
},
210 { GLX_TRANSPARENT_INDEX_VALUE
, 0 },
211 { GLX_TRANSPARENT_RED_VALUE
, EGL_TRANSPARENT_RED_VALUE
},
212 { GLX_TRANSPARENT_GREEN_VALUE
, EGL_TRANSPARENT_GREEN_VALUE
},
213 { GLX_TRANSPARENT_BLUE_VALUE
, EGL_TRANSPARENT_BLUE_VALUE
},
214 { GLX_MAX_PBUFFER_WIDTH
, EGL_MAX_PBUFFER_WIDTH
},
215 { GLX_MAX_PBUFFER_HEIGHT
, EGL_MAX_PBUFFER_HEIGHT
},
216 { GLX_MAX_PBUFFER_PIXELS
, EGL_MAX_PBUFFER_PIXELS
},
217 { GLX_VISUAL_ID
, EGL_NATIVE_VISUAL_ID
}
222 convert_fbconfig(struct GLX_egl_driver
*GLX_drv
,
223 struct GLX_egl_display
*GLX_dpy
, GLXFBConfig fbconfig
,
224 struct GLX_egl_config
*GLX_conf
)
226 Display
*dpy
= GLX_dpy
->dpy
;
230 /* must have rgba bit */
231 err
= GLX_drv
->glXGetFBConfigAttrib(dpy
, fbconfig
, GLX_RENDER_TYPE
, &val
);
232 if (err
|| !(val
& GLX_RGBA_BIT
))
235 /* must know whether it is double-buffered */
236 err
= GLX_drv
->glXGetFBConfigAttrib(dpy
, fbconfig
, GLX_DOUBLEBUFFER
, &val
);
239 GLX_conf
->double_buffered
= val
;
241 GLX_conf
->Base
.RenderableType
= EGL_OPENGL_BIT
;
242 GLX_conf
->Base
.Conformant
= EGL_OPENGL_BIT
;
244 for (i
= 0; i
< ARRAY_SIZE(fbconfig_attributes
); i
++) {
245 EGLint egl_attr
, egl_val
;
247 attr
= fbconfig_attributes
[i
].attr
;
248 egl_attr
= fbconfig_attributes
[i
].egl_attr
;
252 err
= GLX_drv
->glXGetFBConfigAttrib(dpy
, fbconfig
, attr
, &val
);
254 if (err
== GLX_BAD_ATTRIBUTE
) {
262 case EGL_SURFACE_TYPE
:
264 if (val
& GLX_WINDOW_BIT
)
265 egl_val
|= EGL_WINDOW_BIT
;
266 /* pixmap and pbuffer surfaces must be single-buffered in EGL */
267 if (!GLX_conf
->double_buffered
) {
268 if (val
& GLX_PIXMAP_BIT
)
269 egl_val
|= EGL_PIXMAP_BIT
;
270 if (val
& GLX_PBUFFER_BIT
)
271 egl_val
|= EGL_PBUFFER_BIT
;
274 case EGL_NATIVE_VISUAL_TYPE
:
279 case GLX_DIRECT_COLOR
:
280 egl_val
= DirectColor
;
282 case GLX_PSEUDO_COLOR
:
283 egl_val
= PseudoColor
;
285 case GLX_STATIC_COLOR
:
286 egl_val
= StaticColor
;
291 case GLX_STATIC_GRAY
:
292 egl_val
= StaticGray
;
299 case EGL_CONFIG_CAVEAT
:
301 if (val
== GLX_SLOW_CONFIG
) {
302 egl_val
= EGL_SLOW_CONFIG
;
304 else if (val
== GLX_NON_CONFORMANT_CONFIG
) {
305 GLX_conf
->Base
.Conformant
&= ~EGL_OPENGL_BIT
;
309 case EGL_TRANSPARENT_TYPE
:
310 egl_val
= (val
== GLX_TRANSPARENT_RGB
) ?
311 EGL_TRANSPARENT_RGB
: EGL_NONE
;
318 _eglSetConfigKey(&GLX_conf
->Base
, egl_attr
, egl_val
);
323 if (!GLX_conf
->Base
.SurfaceType
)
329 static const struct {
332 } visual_attributes
[] = {
333 /* table 3.7 of GLX 1.4 */
335 { GLX_BUFFER_SIZE
, EGL_BUFFER_SIZE
},
336 { GLX_LEVEL
, EGL_LEVEL
},
338 { GLX_DOUBLEBUFFER
, 0 },
340 { GLX_AUX_BUFFERS
, 0 },
341 { GLX_RED_SIZE
, EGL_RED_SIZE
},
342 { GLX_GREEN_SIZE
, EGL_GREEN_SIZE
},
343 { GLX_BLUE_SIZE
, EGL_BLUE_SIZE
},
344 { GLX_ALPHA_SIZE
, EGL_ALPHA_SIZE
},
345 { GLX_DEPTH_SIZE
, EGL_DEPTH_SIZE
},
346 { GLX_STENCIL_SIZE
, EGL_STENCIL_SIZE
},
347 { GLX_ACCUM_RED_SIZE
, 0 },
348 { GLX_ACCUM_GREEN_SIZE
, 0 },
349 { GLX_ACCUM_BLUE_SIZE
, 0 },
350 { GLX_ACCUM_ALPHA_SIZE
, 0 },
351 { GLX_SAMPLE_BUFFERS
, EGL_SAMPLE_BUFFERS
},
352 { GLX_SAMPLES
, EGL_SAMPLES
},
353 { GLX_FBCONFIG_ID
, 0 },
354 /* GLX_EXT_visual_rating */
355 { GLX_VISUAL_CAVEAT_EXT
, EGL_CONFIG_CAVEAT
}
359 convert_visual(struct GLX_egl_driver
*GLX_drv
,
360 struct GLX_egl_display
*GLX_dpy
, XVisualInfo
*vinfo
,
361 struct GLX_egl_config
*GLX_conf
)
363 Display
*dpy
= GLX_dpy
->dpy
;
367 /* the visual must support OpenGL and RGBA buffer */
368 err
= GLX_drv
->glXGetConfig(dpy
, vinfo
, GLX_USE_GL
, &val
);
370 err
= GLX_drv
->glXGetConfig(dpy
, vinfo
, GLX_RGBA
, &val
);
374 /* must know whether it is double-buffered */
375 err
= GLX_drv
->glXGetConfig(dpy
, vinfo
, GLX_DOUBLEBUFFER
, &val
);
378 GLX_conf
->double_buffered
= val
;
380 GLX_conf
->Base
.RenderableType
= EGL_OPENGL_BIT
;
381 GLX_conf
->Base
.Conformant
= EGL_OPENGL_BIT
;
382 GLX_conf
->Base
.SurfaceType
= EGL_WINDOW_BIT
;
383 /* pixmap surfaces must be single-buffered in EGL */
384 if (!GLX_conf
->double_buffered
)
385 GLX_conf
->Base
.SurfaceType
|= EGL_PIXMAP_BIT
;
387 GLX_conf
->Base
.NativeVisualID
= vinfo
->visualid
;
388 GLX_conf
->Base
.NativeVisualType
= vinfo
->class;
389 GLX_conf
->Base
.NativeRenderable
= EGL_TRUE
;
391 for (i
= 0; i
< ARRAY_SIZE(visual_attributes
); i
++) {
392 EGLint egl_attr
, egl_val
;
394 attr
= visual_attributes
[i
].attr
;
395 egl_attr
= visual_attributes
[i
].egl_attr
;
399 err
= GLX_drv
->glXGetConfig(dpy
, vinfo
, attr
, &val
);
401 if (err
== GLX_BAD_ATTRIBUTE
) {
409 case EGL_CONFIG_CAVEAT
:
411 if (val
== GLX_SLOW_VISUAL_EXT
) {
412 egl_val
= EGL_SLOW_CONFIG
;
414 else if (val
== GLX_NON_CONFORMANT_VISUAL_EXT
) {
415 GLX_conf
->Base
.Conformant
&= ~EGL_OPENGL_BIT
;
424 _eglSetConfigKey(&GLX_conf
->Base
, egl_attr
, egl_val
);
427 return (err
) ? EGL_FALSE
: EGL_TRUE
;
432 fix_config(struct GLX_egl_display
*GLX_dpy
, struct GLX_egl_config
*GLX_conf
)
434 _EGLConfig
*conf
= &GLX_conf
->Base
;
436 if (!GLX_conf
->double_buffered
&& GLX_dpy
->single_buffered_quirk
) {
437 /* some GLX impls do not like single-buffered window surface */
438 conf
->SurfaceType
&= ~EGL_WINDOW_BIT
;
439 /* pbuffer bit is usually not set */
440 if (GLX_dpy
->have_pbuffer
)
441 conf
->SurfaceType
|= EGL_PBUFFER_BIT
;
444 /* no visual attribs unless window bit is set */
445 if (!(conf
->SurfaceType
& EGL_WINDOW_BIT
)) {
446 conf
->NativeVisualID
= 0;
447 conf
->NativeVisualType
= EGL_NONE
;
450 if (conf
->TransparentType
!= EGL_TRANSPARENT_RGB
) {
451 /* some impls set them to -1 (GLX_DONT_CARE) */
452 conf
->TransparentRedValue
= 0;
453 conf
->TransparentGreenValue
= 0;
454 conf
->TransparentBlueValue
= 0;
457 /* make sure buffer size is set correctly */
459 conf
->RedSize
+ conf
->GreenSize
+ conf
->BlueSize
+ conf
->AlphaSize
;
464 create_configs(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint screen
)
466 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
467 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(dpy
);
468 EGLint num_configs
= 0, i
;
471 if (GLX_dpy
->have_fbconfig
) {
473 GLX_drv
->glXGetFBConfigs(GLX_dpy
->dpy
, screen
, &num_configs
);
476 XVisualInfo vinfo_template
;
479 vinfo_template
.screen
= screen
;
480 mask
= VisualScreenMask
;
481 GLX_dpy
->visuals
= XGetVisualInfo(GLX_dpy
->dpy
, mask
, &vinfo_template
,
488 for (i
= 0; i
< num_configs
; i
++) {
489 struct GLX_egl_config
*GLX_conf
, template;
492 memset(&template, 0, sizeof(template));
493 _eglInitConfig(&template.Base
, dpy
, id
);
494 if (GLX_dpy
->have_fbconfig
) {
495 ok
= convert_fbconfig(GLX_drv
, GLX_dpy
,
496 GLX_dpy
->fbconfigs
[i
], &template);
499 ok
= convert_visual(GLX_drv
, GLX_dpy
,
500 &GLX_dpy
->visuals
[i
], &template);
505 fix_config(GLX_dpy
, &template);
506 if (!_eglValidateConfig(&template.Base
, EGL_FALSE
)) {
507 _eglLog(_EGL_DEBUG
, "GLX: failed to validate config %d", i
);
511 GLX_conf
= CALLOC_STRUCT(GLX_egl_config
);
513 memcpy(GLX_conf
, &template, sizeof(template));
516 _eglLinkConfig(&GLX_conf
->Base
);
526 check_extensions(struct GLX_egl_driver
*GLX_drv
,
527 struct GLX_egl_display
*GLX_dpy
, EGLint screen
)
529 GLX_dpy
->extensions
=
530 GLX_drv
->glXQueryExtensionsString(GLX_dpy
->dpy
, screen
);
531 if (GLX_dpy
->extensions
) {
532 if (strstr(GLX_dpy
->extensions
, "GLX_SGI_make_current_read")) {
533 /* GLX 1.3 entry points are used */
534 GLX_dpy
->have_make_current_read
= EGL_TRUE
;
537 if (strstr(GLX_dpy
->extensions
, "GLX_SGIX_fbconfig")) {
538 /* GLX 1.3 entry points are used */
539 GLX_dpy
->have_fbconfig
= EGL_TRUE
;
542 if (strstr(GLX_dpy
->extensions
, "GLX_SGIX_pbuffer")) {
543 if (GLX_drv
->glXCreateGLXPbufferSGIX
&&
544 GLX_drv
->glXDestroyGLXPbufferSGIX
&&
545 GLX_dpy
->have_fbconfig
)
546 GLX_dpy
->have_pbuffer
= EGL_TRUE
;
550 if (GLX_dpy
->glx_maj
== 1 && GLX_dpy
->glx_min
>= 3) {
551 GLX_dpy
->have_1_3
= EGL_TRUE
;
552 GLX_dpy
->have_make_current_read
= EGL_TRUE
;
553 GLX_dpy
->have_fbconfig
= EGL_TRUE
;
554 GLX_dpy
->have_pbuffer
= EGL_TRUE
;
560 check_quirks(struct GLX_egl_driver
*GLX_drv
,
561 struct GLX_egl_display
*GLX_dpy
, EGLint screen
)
565 GLX_dpy
->single_buffered_quirk
= EGL_TRUE
;
566 GLX_dpy
->glx_window_quirk
= EGL_TRUE
;
568 vendor
= GLX_drv
->glXGetClientString(GLX_dpy
->dpy
, GLX_VENDOR
);
569 if (vendor
&& strstr(vendor
, "NVIDIA")) {
570 vendor
= GLX_drv
->glXQueryServerString(GLX_dpy
->dpy
, screen
, GLX_VENDOR
);
571 if (vendor
&& strstr(vendor
, "NVIDIA")) {
572 _eglLog(_EGL_DEBUG
, "disable quirks");
573 GLX_dpy
->single_buffered_quirk
= EGL_FALSE
;
574 GLX_dpy
->glx_window_quirk
= EGL_FALSE
;
581 * Called via eglInitialize(), GLX_drv->API.Initialize().
584 GLX_eglInitialize(_EGLDriver
*drv
, _EGLDisplay
*disp
,
585 EGLint
*major
, EGLint
*minor
)
587 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
588 struct GLX_egl_display
*GLX_dpy
;
590 if (disp
->Platform
!= _EGL_PLATFORM_X11
)
593 GLX_dpy
= CALLOC_STRUCT(GLX_egl_display
);
595 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
597 GLX_dpy
->dpy
= (Display
*) disp
->PlatformDisplay
;
599 GLX_dpy
->dpy
= XOpenDisplay(NULL
);
601 _eglLog(_EGL_WARNING
, "GLX: XOpenDisplay failed");
607 if (!GLX_drv
->glXQueryVersion(GLX_dpy
->dpy
,
608 &GLX_dpy
->glx_maj
, &GLX_dpy
->glx_min
)) {
609 _eglLog(_EGL_WARNING
, "GLX: glXQueryVersion failed");
610 if (!disp
->PlatformDisplay
)
611 XCloseDisplay(GLX_dpy
->dpy
);
616 disp
->DriverData
= (void *) GLX_dpy
;
617 disp
->ClientAPIsMask
= EGL_OPENGL_BIT
;
619 check_extensions(GLX_drv
, GLX_dpy
, DefaultScreen(GLX_dpy
->dpy
));
620 check_quirks(GLX_drv
, GLX_dpy
, DefaultScreen(GLX_dpy
->dpy
));
622 create_configs(drv
, disp
, DefaultScreen(GLX_dpy
->dpy
));
623 if (!_eglGetArraySize(disp
->Configs
)) {
624 _eglLog(_EGL_WARNING
, "GLX: failed to create any config");
625 if (!disp
->PlatformDisplay
)
626 XCloseDisplay(GLX_dpy
->dpy
);
631 /* we're supporting EGL 1.4 */
640 * Called via eglTerminate(), drv->API.Terminate().
643 GLX_eglTerminate(_EGLDriver
*drv
, _EGLDisplay
*disp
)
645 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(disp
);
647 _eglReleaseDisplayResources(drv
, disp
);
648 _eglCleanupDisplay(disp
);
650 if (GLX_dpy
->visuals
)
651 XFree(GLX_dpy
->visuals
);
652 if (GLX_dpy
->fbconfigs
)
653 XFree(GLX_dpy
->fbconfigs
);
655 if (!disp
->PlatformDisplay
)
656 XCloseDisplay(GLX_dpy
->dpy
);
659 disp
->DriverData
= NULL
;
666 * Called via eglCreateContext(), drv->API.CreateContext().
669 GLX_eglCreateContext(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLConfig
*conf
,
670 _EGLContext
*share_list
, const EGLint
*attrib_list
)
672 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
673 struct GLX_egl_context
*GLX_ctx
= CALLOC_STRUCT(GLX_egl_context
);
674 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(disp
);
675 struct GLX_egl_context
*GLX_ctx_shared
= GLX_egl_context(share_list
);
678 _eglError(EGL_BAD_ALLOC
, "eglCreateContext");
682 if (!_eglInitContext(&GLX_ctx
->Base
, disp
, conf
, attrib_list
)) {
687 if (GLX_dpy
->have_fbconfig
) {
688 GLX_ctx
->context
= GLX_drv
->glXCreateNewContext(GLX_dpy
->dpy
,
689 GLX_dpy
->fbconfigs
[GLX_egl_config_index(conf
)],
691 GLX_ctx_shared
? GLX_ctx_shared
->context
: NULL
,
695 GLX_ctx
->context
= GLX_drv
->glXCreateContext(GLX_dpy
->dpy
,
696 &GLX_dpy
->visuals
[GLX_egl_config_index(conf
)],
697 GLX_ctx_shared
? GLX_ctx_shared
->context
: NULL
,
700 if (!GLX_ctx
->context
) {
705 return &GLX_ctx
->Base
;
710 * Destroy a surface. The display is allowed to be uninitialized.
713 destroy_surface(_EGLDisplay
*disp
, _EGLSurface
*surf
)
715 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(disp
);
716 struct GLX_egl_surface
*GLX_surf
= GLX_egl_surface(surf
);
718 if (GLX_surf
->destroy
)
719 GLX_surf
->destroy(GLX_dpy
->dpy
, GLX_surf
->glx_drawable
);
726 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
729 GLX_eglMakeCurrent(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*dsurf
,
730 _EGLSurface
*rsurf
, _EGLContext
*ctx
)
732 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
733 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(disp
);
734 struct GLX_egl_surface
*GLX_dsurf
= GLX_egl_surface(dsurf
);
735 struct GLX_egl_surface
*GLX_rsurf
= GLX_egl_surface(rsurf
);
736 struct GLX_egl_context
*GLX_ctx
= GLX_egl_context(ctx
);
737 _EGLContext
*old_ctx
;
738 _EGLSurface
*old_dsurf
, *old_rsurf
;
739 GLXDrawable ddraw
, rdraw
;
741 EGLBoolean ret
= EGL_FALSE
;
743 /* make new bindings */
744 if (!_eglBindContext(ctx
, dsurf
, rsurf
, &old_ctx
, &old_dsurf
, &old_rsurf
))
747 ddraw
= (GLX_dsurf
) ? GLX_dsurf
->glx_drawable
: None
;
748 rdraw
= (GLX_rsurf
) ? GLX_rsurf
->glx_drawable
: None
;
749 cctx
= (GLX_ctx
) ? GLX_ctx
->context
: NULL
;
751 if (GLX_dpy
->have_make_current_read
)
752 ret
= GLX_drv
->glXMakeContextCurrent(GLX_dpy
->dpy
, ddraw
, rdraw
, cctx
);
753 else if (ddraw
== rdraw
)
754 ret
= GLX_drv
->glXMakeCurrent(GLX_dpy
->dpy
, ddraw
, cctx
);
757 if (_eglPutSurface(old_dsurf
))
758 destroy_surface(disp
, old_dsurf
);
759 if (_eglPutSurface(old_rsurf
))
760 destroy_surface(disp
, old_rsurf
);
762 _eglPutContext(old_ctx
);
765 /* undo the previous _eglBindContext */
766 _eglBindContext(old_ctx
, old_dsurf
, old_rsurf
, &ctx
, &dsurf
, &rsurf
);
767 assert(&GLX_ctx
->Base
== ctx
&&
768 &GLX_dsurf
->Base
== dsurf
&&
769 &GLX_rsurf
->Base
== rsurf
);
771 _eglPutSurface(dsurf
);
772 _eglPutSurface(rsurf
);
775 _eglPutSurface(old_dsurf
);
776 _eglPutSurface(old_rsurf
);
777 _eglPutContext(old_ctx
);
783 /** Get size of given window */
785 get_drawable_size(Display
*dpy
, Drawable d
, uint
*width
, uint
*height
)
790 unsigned int w
, h
, bw
, depth
;
791 stat
= XGetGeometry(dpy
, d
, &root
, &xpos
, &ypos
, &w
, &h
, &bw
, &depth
);
798 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
801 GLX_eglCreateWindowSurface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
802 _EGLConfig
*conf
, EGLNativeWindowType window
,
803 const EGLint
*attrib_list
)
805 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
806 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(disp
);
807 struct GLX_egl_surface
*GLX_surf
;
810 GLX_surf
= CALLOC_STRUCT(GLX_egl_surface
);
812 _eglError(EGL_BAD_ALLOC
, "eglCreateWindowSurface");
816 if (!_eglInitSurface(&GLX_surf
->Base
, disp
, EGL_WINDOW_BIT
,
817 conf
, attrib_list
)) {
822 GLX_surf
->drawable
= window
;
824 if (GLX_dpy
->have_1_3
&& !GLX_dpy
->glx_window_quirk
) {
825 GLX_surf
->glx_drawable
= GLX_drv
->glXCreateWindow(GLX_dpy
->dpy
,
826 GLX_dpy
->fbconfigs
[GLX_egl_config_index(conf
)],
827 GLX_surf
->drawable
, NULL
);
830 GLX_surf
->glx_drawable
= GLX_surf
->drawable
;
833 if (!GLX_surf
->glx_drawable
) {
838 if (GLX_dpy
->have_1_3
&& !GLX_dpy
->glx_window_quirk
)
839 GLX_surf
->destroy
= GLX_drv
->glXDestroyWindow
;
841 get_drawable_size(GLX_dpy
->dpy
, window
, &width
, &height
);
842 GLX_surf
->Base
.Width
= width
;
843 GLX_surf
->Base
.Height
= height
;
845 return &GLX_surf
->Base
;
849 GLX_eglCreatePixmapSurface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
850 _EGLConfig
*conf
, EGLNativePixmapType pixmap
,
851 const EGLint
*attrib_list
)
853 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
854 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(disp
);
855 struct GLX_egl_surface
*GLX_surf
;
858 GLX_surf
= CALLOC_STRUCT(GLX_egl_surface
);
860 _eglError(EGL_BAD_ALLOC
, "eglCreatePixmapSurface");
864 if (!_eglInitSurface(&GLX_surf
->Base
, disp
, EGL_PIXMAP_BIT
,
865 conf
, attrib_list
)) {
870 GLX_surf
->drawable
= pixmap
;
872 if (GLX_dpy
->have_1_3
) {
873 GLX_surf
->glx_drawable
= GLX_drv
->glXCreatePixmap(GLX_dpy
->dpy
,
874 GLX_dpy
->fbconfigs
[GLX_egl_config_index(conf
)],
875 GLX_surf
->drawable
, NULL
);
877 else if (GLX_dpy
->have_fbconfig
) {
878 GLXFBConfig fbconfig
= GLX_dpy
->fbconfigs
[GLX_egl_config_index(conf
)];
881 vinfo
= GLX_drv
->glXGetVisualFromFBConfig(GLX_dpy
->dpy
, fbconfig
);
883 GLX_surf
->glx_drawable
= GLX_drv
->glXCreateGLXPixmap(GLX_dpy
->dpy
,
884 vinfo
, GLX_surf
->drawable
);
889 GLX_surf
->glx_drawable
= GLX_drv
->glXCreateGLXPixmap(GLX_dpy
->dpy
,
890 &GLX_dpy
->visuals
[GLX_egl_config_index(conf
)],
894 if (!GLX_surf
->glx_drawable
) {
899 GLX_surf
->destroy
= (GLX_dpy
->have_1_3
) ?
900 GLX_drv
->glXDestroyPixmap
: GLX_drv
->glXDestroyGLXPixmap
;
902 get_drawable_size(GLX_dpy
->dpy
, pixmap
, &width
, &height
);
903 GLX_surf
->Base
.Width
= width
;
904 GLX_surf
->Base
.Height
= height
;
906 return &GLX_surf
->Base
;
910 GLX_eglCreatePbufferSurface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
911 _EGLConfig
*conf
, const EGLint
*attrib_list
)
913 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
914 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(disp
);
915 struct GLX_egl_surface
*GLX_surf
;
919 GLX_surf
= CALLOC_STRUCT(GLX_egl_surface
);
921 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
925 if (!_eglInitSurface(&GLX_surf
->Base
, disp
, EGL_PBUFFER_BIT
,
926 conf
, attrib_list
)) {
934 GLX_surf
->drawable
= None
;
936 if (GLX_dpy
->have_1_3
) {
937 /* put geometry in attribs */
938 if (GLX_surf
->Base
.Width
) {
939 attribs
[i
++] = GLX_PBUFFER_WIDTH
;
940 attribs
[i
++] = GLX_surf
->Base
.Width
;
942 if (GLX_surf
->Base
.Height
) {
943 attribs
[i
++] = GLX_PBUFFER_HEIGHT
;
944 attribs
[i
++] = GLX_surf
->Base
.Height
;
948 GLX_surf
->glx_drawable
= GLX_drv
->glXCreatePbuffer(GLX_dpy
->dpy
,
949 GLX_dpy
->fbconfigs
[GLX_egl_config_index(conf
)], attribs
);
951 else if (GLX_dpy
->have_pbuffer
) {
952 GLX_surf
->glx_drawable
= GLX_drv
->glXCreateGLXPbufferSGIX(GLX_dpy
->dpy
,
953 GLX_dpy
->fbconfigs
[GLX_egl_config_index(conf
)],
954 GLX_surf
->Base
.Width
,
955 GLX_surf
->Base
.Height
,
959 if (!GLX_surf
->glx_drawable
) {
964 GLX_surf
->destroy
= (GLX_dpy
->have_1_3
) ?
965 GLX_drv
->glXDestroyPbuffer
: GLX_drv
->glXDestroyGLXPbufferSGIX
;
967 return &GLX_surf
->Base
;
972 GLX_eglDestroySurface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
976 if (_eglPutSurface(surf
))
977 destroy_surface(disp
, surf
);
984 GLX_eglSwapBuffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*draw
)
986 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
987 struct GLX_egl_display
*GLX_dpy
= GLX_egl_display(disp
);
988 struct GLX_egl_surface
*GLX_surf
= GLX_egl_surface(draw
);
990 GLX_drv
->glXSwapBuffers(GLX_dpy
->dpy
, GLX_surf
->glx_drawable
);
996 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
999 GLX_eglGetProcAddress(_EGLDriver
*drv
, const char *procname
)
1001 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
1003 return (_EGLProc
) GLX_drv
->glXGetProcAddress((const GLubyte
*) procname
);
1007 GLX_eglWaitClient(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLContext
*ctx
)
1009 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
1014 GLX_drv
->glXWaitGL();
1019 GLX_eglWaitNative(_EGLDriver
*drv
, _EGLDisplay
*dpy
, EGLint engine
)
1021 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
1025 if (engine
!= EGL_CORE_NATIVE_ENGINE
)
1026 return _eglError(EGL_BAD_PARAMETER
, "eglWaitNative");
1027 GLX_drv
->glXWaitX();
1032 GLX_Unload(_EGLDriver
*drv
)
1034 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
1036 if (GLX_drv
->handle
)
1037 dlclose(GLX_drv
->handle
);
1043 GLX_Load(_EGLDriver
*drv
)
1045 struct GLX_egl_driver
*GLX_drv
= GLX_egl_driver(drv
);
1048 handle
= dlopen("libGL.so", RTLD_LAZY
| RTLD_LOCAL
);
1052 GLX_drv
->glXGetProcAddress
= dlsym(handle
, "glXGetProcAddress");
1053 if (!GLX_drv
->glXGetProcAddress
)
1054 GLX_drv
->glXGetProcAddress
= dlsym(handle
, "glXGetProcAddressARB");
1055 if (!GLX_drv
->glXGetProcAddress
)
1058 #define GET_PROC(proc_type, proc_name, check) \
1060 GLX_drv->proc_name = (proc_type) \
1061 GLX_drv->glXGetProcAddress((const GLubyte *) #proc_name); \
1062 if (check && !GLX_drv->proc_name) goto fail; \
1066 GET_PROC(GLXCREATECONTEXTPROC
, glXCreateContext
, EGL_TRUE
);
1067 GET_PROC(GLXDESTROYCONTEXTPROC
, glXDestroyContext
, EGL_TRUE
);
1068 GET_PROC(GLXMAKECURRENTPROC
, glXMakeCurrent
, EGL_TRUE
);
1069 GET_PROC(GLXSWAPBUFFERSPROC
, glXSwapBuffers
, EGL_TRUE
);
1070 GET_PROC(GLXCREATEGLXPIXMAPPROC
, glXCreateGLXPixmap
, EGL_TRUE
);
1071 GET_PROC(GLXDESTROYGLXPIXMAPPROC
, glXDestroyGLXPixmap
, EGL_TRUE
);
1072 GET_PROC(GLXQUERYVERSIONPROC
, glXQueryVersion
, EGL_TRUE
);
1073 GET_PROC(GLXGETCONFIGPROC
, glXGetConfig
, EGL_TRUE
);
1074 GET_PROC(GLXWAITGLPROC
, glXWaitGL
, EGL_TRUE
);
1075 GET_PROC(GLXWAITXPROC
, glXWaitX
, EGL_TRUE
);
1078 GET_PROC(GLXQUERYEXTENSIONSSTRINGPROC
, glXQueryExtensionsString
, EGL_TRUE
);
1079 GET_PROC(GLXQUERYSERVERSTRINGPROC
, glXQueryServerString
, EGL_TRUE
);
1080 GET_PROC(GLXGETCLIENTSTRINGPROC
, glXGetClientString
, EGL_TRUE
);
1083 GET_PROC(PFNGLXGETFBCONFIGSPROC
, glXGetFBConfigs
, EGL_FALSE
);
1084 GET_PROC(PFNGLXGETFBCONFIGATTRIBPROC
, glXGetFBConfigAttrib
, EGL_FALSE
);
1085 GET_PROC(PFNGLXGETVISUALFROMFBCONFIGPROC
, glXGetVisualFromFBConfig
, EGL_FALSE
);
1086 GET_PROC(PFNGLXCREATEWINDOWPROC
, glXCreateWindow
, EGL_FALSE
);
1087 GET_PROC(PFNGLXDESTROYWINDOWPROC
, glXDestroyWindow
, EGL_FALSE
);
1088 GET_PROC(PFNGLXCREATEPIXMAPPROC
, glXCreatePixmap
, EGL_FALSE
);
1089 GET_PROC(PFNGLXDESTROYPIXMAPPROC
, glXDestroyPixmap
, EGL_FALSE
);
1090 GET_PROC(PFNGLXCREATEPBUFFERPROC
, glXCreatePbuffer
, EGL_FALSE
);
1091 GET_PROC(PFNGLXDESTROYPBUFFERPROC
, glXDestroyPbuffer
, EGL_FALSE
);
1092 GET_PROC(PFNGLXCREATENEWCONTEXTPROC
, glXCreateNewContext
, EGL_FALSE
);
1093 GET_PROC(PFNGLXMAKECONTEXTCURRENTPROC
, glXMakeContextCurrent
, EGL_FALSE
);
1095 /* GLX_SGIX_pbuffer */
1096 GET_PROC(PFNGLXCREATEGLXPBUFFERSGIXPROC
,
1097 glXCreateGLXPbufferSGIX
, EGL_FALSE
);
1098 GET_PROC(PFNGLXDESTROYGLXPBUFFERSGIXPROC
,
1099 glXDestroyGLXPbufferSGIX
, EGL_FALSE
);
1102 GLX_drv
->handle
= handle
;
1114 * This is the main entrypoint into the driver, called by libEGL.
1115 * Create a new _EGLDriver object and init its dispatch table.
1118 _EGL_MAIN(const char *args
)
1120 struct GLX_egl_driver
*GLX_drv
= CALLOC_STRUCT(GLX_egl_driver
);
1127 if (!GLX_Load(&GLX_drv
->Base
)) {
1128 _eglLog(_EGL_WARNING
, "GLX: failed to load GLX");
1133 _eglInitDriverFallbacks(&GLX_drv
->Base
);
1134 GLX_drv
->Base
.API
.Initialize
= GLX_eglInitialize
;
1135 GLX_drv
->Base
.API
.Terminate
= GLX_eglTerminate
;
1136 GLX_drv
->Base
.API
.CreateContext
= GLX_eglCreateContext
;
1137 GLX_drv
->Base
.API
.MakeCurrent
= GLX_eglMakeCurrent
;
1138 GLX_drv
->Base
.API
.CreateWindowSurface
= GLX_eglCreateWindowSurface
;
1139 GLX_drv
->Base
.API
.CreatePixmapSurface
= GLX_eglCreatePixmapSurface
;
1140 GLX_drv
->Base
.API
.CreatePbufferSurface
= GLX_eglCreatePbufferSurface
;
1141 GLX_drv
->Base
.API
.DestroySurface
= GLX_eglDestroySurface
;
1142 GLX_drv
->Base
.API
.SwapBuffers
= GLX_eglSwapBuffers
;
1143 GLX_drv
->Base
.API
.GetProcAddress
= GLX_eglGetProcAddress
;
1144 GLX_drv
->Base
.API
.WaitClient
= GLX_eglWaitClient
;
1145 GLX_drv
->Base
.API
.WaitNative
= GLX_eglWaitNative
;
1147 GLX_drv
->Base
.Name
= "GLX";
1148 GLX_drv
->Base
.Unload
= GLX_Unload
;
1150 return &GLX_drv
->Base
;