egl: error checking
[mesa.git] / src / egl / drivers / glx / egl_glx.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * This is an EGL driver that wraps GLX. This gives the benefit of being
31 * completely agnostic of the direct rendering implementation.
32 *
33 * Authors: Alan Hourihane <alanh@tungstengraphics.com>
34 */
35
36 /*
37 * TODO:
38 *
39 * test eglBind/ReleaseTexImage
40 */
41
42
43 #include <assert.h>
44 #include <stdio.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include "dlfcn.h"
49 #include <X11/Xlib.h>
50 #include <GL/gl.h>
51 #include "glxclient.h"
52
53 #define _EGL_PLATFORM_X
54
55 #include "eglconfig.h"
56 #include "eglcontext.h"
57 #include "egldisplay.h"
58 #include "egldriver.h"
59 #include "eglglobals.h"
60 #include "eglhash.h"
61 #include "egllog.h"
62 #include "eglsurface.h"
63
64 #include <GL/gl.h>
65
66 #define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
67
68 static const EGLint all_apis = (EGL_OPENGL_ES_BIT
69 | EGL_OPENGL_ES2_BIT
70 | EGL_OPENVG_BIT
71 /* | EGL_OPENGL_BIT */); /* can't do */
72
73 struct visual_attribs
74 {
75 /* X visual attribs */
76 int id;
77 int klass;
78 int depth;
79 int redMask, greenMask, blueMask;
80 int colormapSize;
81 int bitsPerRGB;
82
83 /* GL visual attribs */
84 int supportsGL;
85 int transparentType;
86 int transparentRedValue;
87 int transparentGreenValue;
88 int transparentBlueValue;
89 int transparentAlphaValue;
90 int transparentIndexValue;
91 int bufferSize;
92 int level;
93 int render_type;
94 int doubleBuffer;
95 int stereo;
96 int auxBuffers;
97 int redSize, greenSize, blueSize, alphaSize;
98 int depthSize;
99 int stencilSize;
100 int accumRedSize, accumGreenSize, accumBlueSize, accumAlphaSize;
101 int numSamples, numMultisample;
102 int visualCaveat;
103 };
104
105 /** subclass of _EGLDriver */
106 struct GLX_egl_driver
107 {
108 _EGLDriver Base; /**< base class */
109
110 XVisualInfo *visuals;
111 GLXFBConfig *fbconfigs;
112
113 int glx_maj, glx_min;
114 };
115
116
117 /** subclass of _EGLContext */
118 struct GLX_egl_context
119 {
120 _EGLContext Base; /**< base class */
121
122 GLXContext context;
123 };
124
125
126 /** subclass of _EGLSurface */
127 struct GLX_egl_surface
128 {
129 _EGLSurface Base; /**< base class */
130
131 GLXDrawable drawable;
132 };
133
134
135 /** subclass of _EGLConfig */
136 struct GLX_egl_config
137 {
138 _EGLConfig Base; /**< base class */
139 };
140
141 /** cast wrapper */
142 static struct GLX_egl_driver *
143 GLX_egl_driver(_EGLDriver *drv)
144 {
145 return (struct GLX_egl_driver *) drv;
146 }
147
148 static struct GLX_egl_context *
149 GLX_egl_context(_EGLContext *ctx)
150 {
151 return (struct GLX_egl_context *) ctx;
152 }
153
154 static struct GLX_egl_surface *
155 GLX_egl_surface(_EGLSurface *surf)
156 {
157 return (struct GLX_egl_surface *) surf;
158 }
159
160 static GLboolean
161 get_visual_attribs(Display *dpy, XVisualInfo *vInfo,
162 struct visual_attribs *attribs)
163 {
164 const char *ext = glXQueryExtensionsString(dpy, vInfo->screen);
165 int rgba;
166
167 memset(attribs, 0, sizeof(struct visual_attribs));
168
169 attribs->id = vInfo->visualid;
170 #if defined(__cplusplus) || defined(c_plusplus)
171 attribs->klass = vInfo->c_class;
172 #else
173 attribs->klass = vInfo->class;
174 #endif
175 attribs->depth = vInfo->depth;
176 attribs->redMask = vInfo->red_mask;
177 attribs->greenMask = vInfo->green_mask;
178 attribs->blueMask = vInfo->blue_mask;
179 attribs->colormapSize = vInfo->colormap_size;
180 attribs->bitsPerRGB = vInfo->bits_per_rgb;
181
182 if (glXGetConfig(dpy, vInfo, GLX_USE_GL, &attribs->supportsGL) != 0 ||
183 !attribs->supportsGL)
184 return GL_FALSE;
185 glXGetConfig(dpy, vInfo, GLX_BUFFER_SIZE, &attribs->bufferSize);
186 glXGetConfig(dpy, vInfo, GLX_LEVEL, &attribs->level);
187 glXGetConfig(dpy, vInfo, GLX_RGBA, &rgba);
188 if (!rgba)
189 return GL_FALSE;
190 attribs->render_type = GLX_RGBA_BIT;
191
192 glXGetConfig(dpy, vInfo, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
193 if (!attribs->doubleBuffer)
194 return GL_FALSE;
195
196 glXGetConfig(dpy, vInfo, GLX_STEREO, &attribs->stereo);
197 glXGetConfig(dpy, vInfo, GLX_AUX_BUFFERS, &attribs->auxBuffers);
198 glXGetConfig(dpy, vInfo, GLX_RED_SIZE, &attribs->redSize);
199 glXGetConfig(dpy, vInfo, GLX_GREEN_SIZE, &attribs->greenSize);
200 glXGetConfig(dpy, vInfo, GLX_BLUE_SIZE, &attribs->blueSize);
201 glXGetConfig(dpy, vInfo, GLX_ALPHA_SIZE, &attribs->alphaSize);
202 glXGetConfig(dpy, vInfo, GLX_DEPTH_SIZE, &attribs->depthSize);
203 glXGetConfig(dpy, vInfo, GLX_STENCIL_SIZE, &attribs->stencilSize);
204 glXGetConfig(dpy, vInfo, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
205 glXGetConfig(dpy, vInfo, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
206 glXGetConfig(dpy, vInfo, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
207 glXGetConfig(dpy, vInfo, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
208
209 /* get transparent pixel stuff */
210 glXGetConfig(dpy, vInfo,GLX_TRANSPARENT_TYPE, &attribs->transparentType);
211 if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
212 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_RED_VALUE, &attribs->transparentRedValue);
213 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_GREEN_VALUE, &attribs->transparentGreenValue);
214 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_BLUE_VALUE, &attribs->transparentBlueValue);
215 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_ALPHA_VALUE, &attribs->transparentAlphaValue);
216 }
217 else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
218 glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_INDEX_VALUE, &attribs->transparentIndexValue);
219 }
220
221 /* multisample attribs */
222 #ifdef GLX_ARB_multisample
223 if (ext && strstr(ext, "GLX_ARB_multisample")) {
224 glXGetConfig(dpy, vInfo, GLX_SAMPLE_BUFFERS_ARB, &attribs->numMultisample);
225 glXGetConfig(dpy, vInfo, GLX_SAMPLES_ARB, &attribs->numSamples);
226 }
227 #endif
228 else {
229 attribs->numSamples = 0;
230 attribs->numMultisample = 0;
231 }
232
233 #if defined(GLX_EXT_visual_rating)
234 if (ext && strstr(ext, "GLX_EXT_visual_rating")) {
235 glXGetConfig(dpy, vInfo, GLX_VISUAL_CAVEAT_EXT, &attribs->visualCaveat);
236 }
237 else {
238 attribs->visualCaveat = GLX_NONE_EXT;
239 }
240 #else
241 attribs->visualCaveat = 0;
242 #endif
243
244 return GL_TRUE;
245 }
246
247 #ifdef GLX_VERSION_1_3
248
249 static int
250 glx_token_to_visual_class(int visual_type)
251 {
252 switch (visual_type) {
253 case GLX_TRUE_COLOR:
254 return TrueColor;
255 case GLX_DIRECT_COLOR:
256 return DirectColor;
257 case GLX_PSEUDO_COLOR:
258 return PseudoColor;
259 case GLX_STATIC_COLOR:
260 return StaticColor;
261 case GLX_GRAY_SCALE:
262 return GrayScale;
263 case GLX_STATIC_GRAY:
264 return StaticGray;
265 case GLX_NONE:
266 default:
267 return None;
268 }
269 }
270 static GLboolean
271 get_fbconfig_attribs(Display *dpy, GLXFBConfig fbconfig,
272 struct visual_attribs *attribs)
273 {
274 int visual_type;
275
276 memset(attribs, 0, sizeof(struct visual_attribs));
277
278 /* We don't use the GLX_FBCONFIG_ID here */
279 glXGetFBConfigAttrib(dpy, fbconfig, GLX_VISUAL_ID, &attribs->id);
280 if (attribs->id == 0)
281 return GL_FALSE;
282
283 #if 0
284 attribs->depth = vInfo->depth;
285 attribs->redMask = vInfo->red_mask;
286 attribs->greenMask = vInfo->green_mask;
287 attribs->blueMask = vInfo->blue_mask;
288 attribs->colormapSize = vInfo->colormap_size;
289 attribs->bitsPerRGB = vInfo->bits_per_rgb;
290 #endif
291
292 glXGetFBConfigAttrib(dpy, fbconfig, GLX_X_VISUAL_TYPE, &visual_type);
293 attribs->klass = glx_token_to_visual_class(visual_type);
294
295 glXGetFBConfigAttrib(dpy, fbconfig, GLX_BUFFER_SIZE, &attribs->bufferSize);
296 glXGetFBConfigAttrib(dpy, fbconfig, GLX_LEVEL, &attribs->level);
297 glXGetFBConfigAttrib(dpy, fbconfig, GLX_RENDER_TYPE, &attribs->render_type);
298 if (!(attribs->render_type & GLX_RGBA_BIT))
299 return GL_FALSE;
300
301 glXGetFBConfigAttrib(dpy, fbconfig, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
302 if (!attribs->doubleBuffer)
303 return GL_FALSE;
304
305 glXGetFBConfigAttrib(dpy, fbconfig, GLX_STEREO, &attribs->stereo);
306 glXGetFBConfigAttrib(dpy, fbconfig, GLX_AUX_BUFFERS, &attribs->auxBuffers);
307
308 glXGetFBConfigAttrib(dpy, fbconfig, GLX_RED_SIZE, &attribs->redSize);
309 glXGetFBConfigAttrib(dpy, fbconfig, GLX_GREEN_SIZE, &attribs->greenSize);
310 glXGetFBConfigAttrib(dpy, fbconfig, GLX_BLUE_SIZE, &attribs->blueSize);
311 glXGetFBConfigAttrib(dpy, fbconfig, GLX_ALPHA_SIZE, &attribs->alphaSize);
312 glXGetFBConfigAttrib(dpy, fbconfig, GLX_DEPTH_SIZE, &attribs->depthSize);
313 glXGetFBConfigAttrib(dpy, fbconfig, GLX_STENCIL_SIZE, &attribs->stencilSize);
314
315 glXGetFBConfigAttrib(dpy, fbconfig, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
316 glXGetFBConfigAttrib(dpy, fbconfig, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
317 glXGetFBConfigAttrib(dpy, fbconfig, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
318 glXGetFBConfigAttrib(dpy, fbconfig, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
319
320 /* get transparent pixel stuff */
321 glXGetFBConfigAttrib(dpy, fbconfig,GLX_TRANSPARENT_TYPE, &attribs->transparentType);
322 if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
323 glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_RED_VALUE, &attribs->transparentRedValue);
324 glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_GREEN_VALUE, &attribs->transparentGreenValue);
325 glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_BLUE_VALUE, &attribs->transparentBlueValue);
326 glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_ALPHA_VALUE, &attribs->transparentAlphaValue);
327 }
328 else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
329 glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_INDEX_VALUE, &attribs->transparentIndexValue);
330 }
331
332 glXGetFBConfigAttrib(dpy, fbconfig, GLX_SAMPLE_BUFFERS, &attribs->numMultisample);
333 glXGetFBConfigAttrib(dpy, fbconfig, GLX_SAMPLES, &attribs->numSamples);
334
335 glXGetFBConfigAttrib(dpy, fbconfig, GLX_CONFIG_CAVEAT, &attribs->visualCaveat);
336
337 return GL_TRUE;
338 }
339
340 #endif
341
342 static EGLBoolean
343 create_configs(_EGLDisplay *disp, struct GLX_egl_driver *GLX_drv)
344 {
345 XVisualInfo theTemplate;
346 int numVisuals;
347 long mask;
348 int i;
349 int egl_configs = 1;
350 struct visual_attribs attribs;
351
352 GLX_drv->fbconfigs = NULL;
353
354 #ifdef GLX_VERSION_1_3
355 /* get list of all fbconfigs on this screen */
356 GLX_drv->fbconfigs = glXGetFBConfigs(disp->Xdpy, DefaultScreen(disp->Xdpy), &numVisuals);
357
358 if (numVisuals == 0) {
359 GLX_drv->fbconfigs = NULL;
360 goto xvisual;
361 }
362
363 for (i = 0; i < numVisuals; i++) {
364 struct GLX_egl_config *config;
365
366 if (!get_fbconfig_attribs(disp->Xdpy, GLX_drv->fbconfigs[i], &attribs))
367 continue;
368
369 config = CALLOC_STRUCT(GLX_egl_config);
370
371 _eglInitConfig(&config->Base, egl_configs++);
372 SET_CONFIG_ATTRIB(&config->Base, EGL_NATIVE_VISUAL_ID, attribs.id);
373 SET_CONFIG_ATTRIB(&config->Base, EGL_BUFFER_SIZE, attribs.bufferSize);
374 SET_CONFIG_ATTRIB(&config->Base, EGL_RED_SIZE, attribs.redSize);
375 SET_CONFIG_ATTRIB(&config->Base, EGL_GREEN_SIZE, attribs.greenSize);
376 SET_CONFIG_ATTRIB(&config->Base, EGL_BLUE_SIZE, attribs.blueSize);
377 SET_CONFIG_ATTRIB(&config->Base, EGL_ALPHA_SIZE, attribs.alphaSize);
378 SET_CONFIG_ATTRIB(&config->Base, EGL_DEPTH_SIZE, attribs.depthSize);
379 SET_CONFIG_ATTRIB(&config->Base, EGL_STENCIL_SIZE, attribs.stencilSize);
380 SET_CONFIG_ATTRIB(&config->Base, EGL_SAMPLES, attribs.numSamples);
381 SET_CONFIG_ATTRIB(&config->Base, EGL_SAMPLE_BUFFERS, attribs.numMultisample);
382 SET_CONFIG_ATTRIB(&config->Base, EGL_CONFORMANT, all_apis);
383 SET_CONFIG_ATTRIB(&config->Base, EGL_RENDERABLE_TYPE, all_apis);
384 SET_CONFIG_ATTRIB(&config->Base, EGL_SURFACE_TYPE,
385 (EGL_WINDOW_BIT | EGL_PBUFFER_BIT | EGL_PIXMAP_BIT));
386
387 /* XXX possibly other things to init... */
388
389 _eglAddConfig(disp, &config->Base);
390 }
391
392 goto end;
393 #endif
394
395 xvisual:
396 /* get list of all visuals on this screen */
397 theTemplate.screen = DefaultScreen(disp->Xdpy);
398 mask = VisualScreenMask;
399 GLX_drv->visuals = XGetVisualInfo(disp->Xdpy, mask, &theTemplate, &numVisuals);
400
401 for (i = 0; i < numVisuals; i++) {
402 struct GLX_egl_config *config;
403
404 if (!get_visual_attribs(disp->Xdpy, &GLX_drv->visuals[i], &attribs))
405 continue;
406
407 config = CALLOC_STRUCT(GLX_egl_config);
408
409 _eglInitConfig(&config->Base, egl_configs++);
410 SET_CONFIG_ATTRIB(&config->Base, EGL_NATIVE_VISUAL_ID, attribs.id);
411 SET_CONFIG_ATTRIB(&config->Base, EGL_BUFFER_SIZE, attribs.bufferSize);
412 SET_CONFIG_ATTRIB(&config->Base, EGL_RED_SIZE, attribs.redSize);
413 SET_CONFIG_ATTRIB(&config->Base, EGL_GREEN_SIZE, attribs.greenSize);
414 SET_CONFIG_ATTRIB(&config->Base, EGL_BLUE_SIZE, attribs.blueSize);
415 SET_CONFIG_ATTRIB(&config->Base, EGL_ALPHA_SIZE, attribs.alphaSize);
416 SET_CONFIG_ATTRIB(&config->Base, EGL_DEPTH_SIZE, attribs.depthSize);
417 SET_CONFIG_ATTRIB(&config->Base, EGL_STENCIL_SIZE, attribs.stencilSize);
418 SET_CONFIG_ATTRIB(&config->Base, EGL_SAMPLES, attribs.numSamples);
419 SET_CONFIG_ATTRIB(&config->Base, EGL_SAMPLE_BUFFERS, attribs.numMultisample);
420 SET_CONFIG_ATTRIB(&config->Base, EGL_CONFORMANT, all_apis);
421 SET_CONFIG_ATTRIB(&config->Base, EGL_RENDERABLE_TYPE, all_apis);
422 SET_CONFIG_ATTRIB(&config->Base, EGL_SURFACE_TYPE,
423 (EGL_WINDOW_BIT /*| EGL_PBUFFER_BIT | EGL_PIXMAP_BIT*/));
424
425 /* XXX possibly other things to init... */
426
427 _eglAddConfig(disp, &config->Base);
428 }
429
430 end:
431 return EGL_TRUE;
432 }
433
434 /**
435 * Called via eglInitialize(), GLX_drv->API.Initialize().
436 */
437 static EGLBoolean
438 GLX_eglInitialize(_EGLDriver *drv, EGLDisplay dpy,
439 EGLint *minor, EGLint *major)
440 {
441 struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
442 _EGLDisplay *disp = _eglLookupDisplay(dpy);
443
444 _eglLog(_EGL_DEBUG, "GLX: eglInitialize");
445
446 if (!disp->Xdpy) {
447 disp->Xdpy = XOpenDisplay(NULL);
448 if (!disp->Xdpy) {
449 _eglLog(_EGL_WARNING, "GLX: XOpenDisplay failed");
450 return EGL_FALSE;
451 }
452 }
453
454 glXQueryVersion(disp->Xdpy, &GLX_drv->glx_maj, &GLX_drv->glx_min);
455
456 GLX_drv->Base.Initialized = EGL_TRUE;
457
458 GLX_drv->Base.Name = "GLX";
459
460 /* we're supporting EGL 1.4 */
461 *minor = 1;
462 *major = 4;
463
464 create_configs(disp, GLX_drv);
465
466 return EGL_TRUE;
467 }
468
469 /*
470 * Do some clean-up that normally occurs in XCloseDisplay().
471 * We do this here because we're about to unload a dynamic library
472 * that has added some per-display extension data and callbacks.
473 * If we don't do this here we'll crash in XCloseDisplay() because it'll
474 * try to call functions that went away when the driver library was unloaded.
475 */
476 static void
477 FreeDisplayExt(Display *dpy)
478 {
479 _XExtension *ext, *next;
480
481 for (ext = dpy->ext_procs; ext; ext = next) {
482 next = ext->next;
483 if (ext->close_display) {
484 ext->close_display(dpy, &ext->codes);
485 ext->close_display = NULL;
486 }
487 if (ext->name)
488 Xfree(ext->name);
489 Xfree(ext);
490 }
491 dpy->ext_procs = NULL;
492
493 _XFreeExtData (dpy->ext_data);
494 dpy->ext_data = NULL;
495 }
496
497 /**
498 * Called via eglTerminate(), drv->API.Terminate().
499 */
500 static EGLBoolean
501 GLX_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
502 {
503 _EGLDisplay *disp = _eglLookupDisplay(dpy);
504
505 _eglLog(_EGL_DEBUG, "GLX: eglTerminate");
506
507 FreeDisplayExt(disp->Xdpy);
508
509 return EGL_TRUE;
510 }
511
512
513 /**
514 * Called via eglCreateContext(), drv->API.CreateContext().
515 */
516 static EGLContext
517 GLX_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
518 EGLContext share_list, const EGLint *attrib_list)
519 {
520 _EGLDisplay *disp = _eglLookupDisplay(dpy);
521 struct GLX_egl_context *GLX_ctx = CALLOC_STRUCT(GLX_egl_context);
522 struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
523 struct GLX_egl_context *GLX_ctx_shared = NULL;
524 _EGLConfig *conf;
525
526 if (!GLX_ctx)
527 return EGL_NO_CONTEXT;
528
529 if (!_eglInitContext(drv, dpy, &GLX_ctx->Base, config, attrib_list)) {
530 free(GLX_ctx);
531 return EGL_NO_CONTEXT;
532 }
533
534 if (share_list != EGL_NO_CONTEXT) {
535 _EGLContext *shareCtx = _eglLookupContext(share_list);
536 if (!shareCtx) {
537 _eglError(EGL_BAD_CONTEXT, "eglCreateContext(share_list)");
538 return EGL_FALSE;
539 }
540 GLX_ctx_shared = GLX_egl_context(shareCtx);
541 }
542
543 conf = _eglLookupConfig(drv, dpy, config);
544 assert(conf);
545
546 #ifdef GLX_VERSION_1_3
547 if (GLX_drv->fbconfigs)
548 GLX_ctx->context = glXCreateNewContext(disp->Xdpy, GLX_drv->fbconfigs[(int)config-1], GLX_RGBA_TYPE, GLX_ctx_shared ? GLX_ctx_shared->context : NULL, GL_TRUE);
549 else
550 #endif
551 GLX_ctx->context = glXCreateContext(disp->Xdpy, &GLX_drv->visuals[(int)config-1], GLX_ctx_shared ? GLX_ctx_shared->context : NULL, GL_TRUE);
552 if (!GLX_ctx->context)
553 return EGL_FALSE;
554
555 #if 1
556 /* (maybe?) need to have a direct rendering context */
557 if (!glXIsDirect(disp->Xdpy, GLX_ctx->context))
558 return EGL_FALSE;
559 #endif
560
561 return _eglGetContextHandle(&GLX_ctx->Base);
562 }
563
564
565 /**
566 * Called via eglMakeCurrent(), drv->API.MakeCurrent().
567 */
568 static EGLBoolean
569 GLX_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface d,
570 EGLSurface r, EGLContext context)
571 {
572 _EGLDisplay *disp = _eglLookupDisplay(dpy);
573 _EGLContext *ctx = _eglLookupContext(context);
574 _EGLSurface *dsurf = _eglLookupSurface(d);
575 _EGLSurface *rsurf = _eglLookupSurface(r);
576 struct GLX_egl_surface *GLX_dsurf = GLX_egl_surface(dsurf);
577 struct GLX_egl_surface *GLX_rsurf = GLX_egl_surface(rsurf);
578 struct GLX_egl_context *GLX_ctx = GLX_egl_context(ctx);
579
580 if (!_eglMakeCurrent(drv, dpy, d, r, context))
581 return EGL_FALSE;
582
583 #ifdef GLX_VERSION_1_3
584 if (!glXMakeContextCurrent(disp->Xdpy, GLX_dsurf ? GLX_dsurf->drawable : 0, GLX_rsurf ? GLX_rsurf->drawable : 0, GLX_ctx ? GLX_ctx->context : NULL))
585 #endif
586 if (!glXMakeCurrent(disp->Xdpy, GLX_dsurf ? GLX_dsurf->drawable : 0, GLX_ctx ? GLX_ctx->context : NULL))
587 return EGL_FALSE;
588
589 return EGL_TRUE;
590 }
591
592 /** Get size of given window */
593 static Status
594 get_drawable_size(Display *dpy, Drawable d, uint *width, uint *height)
595 {
596 Window root;
597 Status stat;
598 int xpos, ypos;
599 unsigned int w, h, bw, depth;
600 stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
601 *width = w;
602 *height = h;
603 return stat;
604 }
605
606 /**
607 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
608 */
609 static EGLSurface
610 GLX_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
611 NativeWindowType window, const EGLint *attrib_list)
612 {
613 _EGLDisplay *disp = _eglLookupDisplay(dpy);
614 struct GLX_egl_surface *GLX_surf;
615 uint width, height;
616
617 GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
618 if (!GLX_surf)
619 return EGL_NO_SURFACE;
620
621 if (!_eglInitSurface(drv, dpy, &GLX_surf->Base, EGL_WINDOW_BIT,
622 config, attrib_list)) {
623 free(GLX_surf);
624 return EGL_FALSE;
625 }
626
627 _eglSaveSurface(&GLX_surf->Base);
628
629 GLX_surf->drawable = window;
630 get_drawable_size(disp->Xdpy, window, &width, &height);
631 GLX_surf->Base.Width = width;
632 GLX_surf->Base.Height = height;
633
634 return _eglGetSurfaceHandle(&GLX_surf->Base);
635 }
636
637 #ifdef GLX_VERSION_1_3
638 static EGLSurface
639 GLX_eglCreatePixmapSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
640 NativePixmapType pixmap, const EGLint *attrib_list)
641 {
642 struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
643 _EGLDisplay *disp = _eglLookupDisplay(dpy);
644 struct GLX_egl_surface *GLX_surf;
645 int i;
646
647 GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
648 if (!GLX_surf)
649 return EGL_NO_SURFACE;
650
651 if (!_eglInitSurface(drv, dpy, &GLX_surf->Base, EGL_PIXMAP_BIT,
652 config, attrib_list)) {
653 free(GLX_surf);
654 return EGL_FALSE;
655 }
656
657 _eglSaveSurface(&GLX_surf->Base);
658
659 for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
660 switch (attrib_list[i]) {
661 /* no attribs at this time */
662 default:
663 _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePixmapSurface");
664 return EGL_NO_SURFACE;
665 }
666 }
667
668 GLX_surf->drawable = glXCreatePixmap(disp->Xdpy, GLX_drv->fbconfigs[(int)config-1], pixmap, NULL);
669
670 return _eglGetSurfaceHandle(&GLX_surf->Base);
671 }
672
673 static EGLSurface
674 GLX_eglCreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
675 const EGLint *attrib_list)
676 {
677 struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
678 _EGLDisplay *disp = _eglLookupDisplay(dpy);
679 struct GLX_egl_surface *GLX_surf;
680 int attribs[5];
681 int i = 0, j = 0;
682
683 GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
684 if (!GLX_surf)
685 return EGL_NO_SURFACE;
686
687 if (!_eglInitSurface(drv, dpy, &GLX_surf->Base, EGL_PBUFFER_BIT,
688 config, attrib_list)) {
689 free(GLX_surf);
690 return EGL_NO_SURFACE;
691 }
692
693 _eglSaveSurface(&GLX_surf->Base);
694
695 while(attrib_list[i] != EGL_NONE) {
696 switch (attrib_list[i]) {
697 case EGL_WIDTH:
698 attribs[j++] = GLX_PBUFFER_WIDTH;
699 attribs[j++] = attrib_list[i+1];
700 break;
701 case EGL_HEIGHT:
702 attribs[j++] = GLX_PBUFFER_HEIGHT;
703 attribs[j++] = attrib_list[i+1];
704 break;
705 }
706 i++;
707 }
708 attribs[j++] = 0;
709
710 GLX_surf->drawable = glXCreatePbuffer(disp->Xdpy, GLX_drv->fbconfigs[(int)config-1], attribs);
711
712 return _eglGetSurfaceHandle(&GLX_surf->Base);
713 }
714 #endif
715
716 static EGLBoolean
717 GLX_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
718 {
719 _EGLDisplay *disp = _eglLookupDisplay(dpy);
720 _EGLSurface *surf = _eglLookupSurface(surface);
721 return EGL_TRUE;
722 if (surf) {
723 _eglHashRemove(_eglGlobal.Surfaces, (EGLuint) surface);
724 if (surf->IsBound) {
725 surf->DeletePending = EGL_TRUE;
726 }
727 else {
728 free(surf);
729 }
730
731 return EGL_TRUE;
732 }
733 else {
734 _eglError(EGL_BAD_SURFACE, "eglDestroySurface");
735 return EGL_FALSE;
736 }
737 }
738
739
740 static EGLBoolean
741 GLX_eglBindTexImage(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface,
742 EGLint buffer)
743 {
744 _EGLDisplay *disp = _eglLookupDisplay(dpy);
745 _EGLSurface *surf = _eglLookupSurface(surface);
746 struct GLX_egl_surface *GLX_surf = GLX_egl_surface(surf);
747
748 /* buffer ?? */
749 glXBindTexImageEXT(disp->Xdpy, GLX_surf->drawable, GLX_FRONT_LEFT_EXT, NULL);
750
751 return EGL_TRUE;
752 }
753
754
755 static EGLBoolean
756 GLX_eglReleaseTexImage(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface,
757 EGLint buffer)
758 {
759 _EGLDisplay *disp = _eglLookupDisplay(dpy);
760 _EGLSurface *surf = _eglLookupSurface(surface);
761 struct GLX_egl_surface *GLX_surf = GLX_egl_surface(surf);
762
763 /* buffer ?? */
764 glXReleaseTexImageEXT(disp->Xdpy, GLX_surf->drawable, GLX_FRONT_LEFT_EXT);
765
766 return EGL_TRUE;
767 }
768
769
770 static EGLBoolean
771 GLX_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
772 {
773 _EGLDisplay *disp = _eglLookupDisplay(dpy);
774 _EGLSurface *surf = _eglLookupSurface(draw);
775 struct GLX_egl_surface *GLX_surf = GLX_egl_surface(surf);
776
777 _eglLog(_EGL_DEBUG, "GLX: EGL SwapBuffers 0x%x",draw);
778
779 /* error checking step: */
780 if (!_eglSwapBuffers(drv, dpy, draw))
781 return EGL_FALSE;
782
783 glXSwapBuffers(disp->Xdpy, GLX_surf->drawable);
784
785 return EGL_TRUE;
786 }
787
788 /*
789 * Called from eglGetProcAddress() via drv->API.GetProcAddress().
790 */
791 static _EGLProc
792 GLX_eglGetProcAddress(const char *procname)
793 {
794 return (_EGLProc)glXGetProcAddress((const GLubyte *)procname);
795 }
796
797
798 /**
799 * This is the main entrypoint into the driver, called by libEGL.
800 * Create a new _EGLDriver object and init its dispatch table.
801 */
802 _EGLDriver *
803 _eglMain(_EGLDisplay *disp, const char *args)
804 {
805 struct GLX_egl_driver *GLX_drv = CALLOC_STRUCT(GLX_egl_driver);
806 char *env;
807 int maj = 0, min = 0;
808
809 if (!GLX_drv)
810 return NULL;
811
812 _eglInitDriverFallbacks(&GLX_drv->Base);
813 GLX_drv->Base.API.Initialize = GLX_eglInitialize;
814 GLX_drv->Base.API.Terminate = GLX_eglTerminate;
815 GLX_drv->Base.API.CreateContext = GLX_eglCreateContext;
816 GLX_drv->Base.API.MakeCurrent = GLX_eglMakeCurrent;
817 GLX_drv->Base.API.CreateWindowSurface = GLX_eglCreateWindowSurface;
818 #ifdef GLX_VERSION_1_3
819 if (GLX_drv->glx_maj == 1 && GLX_drv->glx_min >= 3) {
820 GLX_drv->Base.API.CreatePixmapSurface = GLX_eglCreatePixmapSurface;
821 GLX_drv->Base.API.CreatePbufferSurface = GLX_eglCreatePbufferSurface;
822 printf("GLX: Pbuffer and Pixmap support\n");
823 } else {
824 printf("GLX: No pbuffer or pixmap support\n");
825 }
826 #endif
827 GLX_drv->Base.API.DestroySurface = GLX_eglDestroySurface;
828 GLX_drv->Base.API.BindTexImage = GLX_eglBindTexImage;
829 GLX_drv->Base.API.ReleaseTexImage = GLX_eglReleaseTexImage;
830 GLX_drv->Base.API.SwapBuffers = GLX_eglSwapBuffers;
831 GLX_drv->Base.API.GetProcAddress = GLX_eglGetProcAddress;
832
833 GLX_drv->Base.ClientAPIsMask = all_apis;
834 GLX_drv->Base.Name = "GLX";
835
836 _eglLog(_EGL_DEBUG, "GLX: main(%s)", args);
837
838 /* set new DRI path to pick up EGL version (which doesn't contain any mesa
839 * code), but don't override if one is already set.
840 */
841 env = getenv("LIBGL_DRIVERS_PATH");
842 if (env) {
843 if (!strstr(env, "egl")) {
844 sprintf(env, "%s/egl", env);
845 setenv("LIBGL_DRIVERS_PATH", env, 1);
846 }
847 } else
848 setenv("LIBGL_DRIVERS_PATH", DEFAULT_DRIVER_DIR"/egl", 0);
849
850 return &GLX_drv->Base;
851 }