VirtualBox

source: vbox/trunk/src/VBox/Additions/common/crOpenGL/egl.c@ 65498

Last change on this file since 65498 was 65498, checked in by vboxsync, 8 years ago

bugref:8748: Additions/Graphics/Wayland: investigate EGLStreams support feasibility: better document a test done at eglGetDisplay() time.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.4 KB
Line 
1/* $Id: egl.c 65498 2017-01-28 18:22:45Z vboxsync $ */
2
3/** @file
4 * VBox OpenGL EGL implentation.
5 */
6
7/*
8 * Copyright (C) 2009-2016 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/cdefs.h>
23#include <iprt/types.h>
24
25#include <EGL/egl.h>
26#include <GL/glx.h>
27#include <X11/Xlib.h>
28
29#include <dlfcn.h>
30#include <pthread.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#define EGL_ASSERT(expr) \
36 if (!(expr)) { printf("Assertion failed: %s\n", #expr); exit(1); }
37
38/*******************************************************************************
39* Structures and Typedefs *
40*******************************************************************************/
41
42struct VBEGLTLS
43{
44 /** The last EGL error. */
45 EGLint cErr;
46 /** The EGL API currently bound to this thread. */
47 EGLenum enmAPI;
48 /** The current context. */
49 EGLContext hCurrent;
50 /** The display bound to the current context. */
51 EGLDisplay hCurrentDisplay;
52 /** The draw surface bound to the current context. */
53 EGLSurface hCurrentDraw;
54 /** The read surface bound to the current context. */
55 EGLSurface hCurrentRead;
56};
57
58/*******************************************************************************
59* Defined Constants And Macros *
60*******************************************************************************/
61/** @note IDs returned for surfaces should always be lower than these constants.
62 */
63/** This is OR-ed with a surface ID to mark it as a window, as GLX needs to
64 * know. */
65#define VBEGL_WINDOW_SURFACE 0x20000000
66/** This is OR-ed with a surface ID to mark it as a pbuffer, as GLX needs to
67 * know. */
68#define VBEGL_PBUFFER_SURFACE 0x40000000
69/** This is OR-ed with a surface ID to mark it as a pixmap, as GLX needs to
70 * know. */
71#define VBEGL_PIXMAP_SURFACE 0x80000000
72#define VBEGL_ANY_SURFACE (VBEGL_WINDOW_SURFACE | VBEGL_PBUFFER_SURFACE | VBEGL_PIXMAP_SURFACE)
73
74/*******************************************************************************
75* Global variables *
76*******************************************************************************/
77
78static pthread_key_t g_tls;
79static pthread_once_t g_tlsOnce = PTHREAD_ONCE_INIT;
80static Display *g_pDefaultDisplay = NULL;
81static pthread_once_t g_defaultDisplayOnce = PTHREAD_ONCE_INIT;
82
83static void tlsInitOnce(void)
84{
85 pthread_key_create(&g_tls, NULL);
86}
87
88static struct VBEGLTLS *getTls(void)
89{
90 struct VBEGLTLS *pTls;
91
92 pthread_once(&g_tlsOnce, tlsInitOnce);
93 pTls = (struct VBEGLTLS *)pthread_getspecific(g_tls);
94 if (RT_LIKELY(pTls))
95 return pTls;
96 pTls = (struct VBEGLTLS *)malloc(sizeof(*pTls));
97 if (!VALID_PTR(pTls))
98 return NULL;
99 pTls->cErr = EGL_SUCCESS;
100 pTls->enmAPI = EGL_NONE;
101 pTls->hCurrent = EGL_NO_CONTEXT;
102 pTls->hCurrentDisplay = EGL_NO_DISPLAY;
103 pTls->hCurrentDraw = EGL_NO_SURFACE;
104 pTls->hCurrentRead = EGL_NO_SURFACE;
105 if (pthread_setspecific(g_tls, pTls) == 0)
106 return pTls;
107 free(pTls);
108 return NULL;
109}
110
111static void defaultDisplayInitOnce(void)
112{
113 g_pDefaultDisplay = XOpenDisplay(NULL);
114}
115
116static EGLBoolean clearEGLError(void)
117{
118 struct VBEGLTLS *pTls = getTls();
119
120 if (!VALID_PTR(pTls))
121 return EGL_FALSE;
122 pTls->cErr = EGL_SUCCESS;
123 return EGL_TRUE;
124}
125
126static EGLBoolean setEGLError(EGLint cErr)
127{
128 struct VBEGLTLS *pTls = getTls();
129
130 if (pTls)
131 pTls->cErr = cErr;
132 return EGL_FALSE;
133}
134
135static EGLBoolean testValidDisplay(EGLNativeDisplayType hDisplay)
136{
137 void *pSymbol = dlsym(NULL, "gbm_create_device");
138
139 if (hDisplay == EGL_DEFAULT_DISPLAY)
140 return EGL_TRUE;
141 if ((void *)hDisplay == NULL)
142 return EGL_FALSE;
143 /* This is the test that Mesa uses to see if this is a GBM "display". Not
144 * very pretty, but since no one can afford to break Mesa it should be
145 * safe. We need this to detect when the X server tries to load us. */
146 if (pSymbol != NULL && *(void **)hDisplay == pSymbol)
147 return EGL_FALSE;
148 return EGL_TRUE;
149}
150
151DECLEXPORT(EGLDisplay) eglGetDisplay(EGLNativeDisplayType hDisplay)
152{
153 Display *pDisplay;
154
155 if (!testValidDisplay(hDisplay))
156 return EGL_NO_DISPLAY;
157 if (!clearEGLError()) /* Set up our tls. */
158 return EGL_NO_DISPLAY;
159 if (hDisplay != EGL_DEFAULT_DISPLAY)
160 pDisplay = hDisplay;
161 else
162 {
163 pthread_once(&g_defaultDisplayOnce, defaultDisplayInitOnce);
164 pDisplay = g_pDefaultDisplay;
165 }
166 if (pDisplay && !strcmp(glXGetClientString(pDisplay, GLX_VENDOR), "Chromium"))
167 return (EGLDisplay) pDisplay;
168 return EGL_NO_DISPLAY;
169}
170
171DECLEXPORT(EGLint) eglGetError(void)
172{
173 struct VBEGLTLS *pTls = getTls();
174
175 if (pTls)
176 return pTls->cErr;
177 return EGL_NOT_INITIALIZED;
178}
179
180DECLEXPORT(EGLBoolean) eglInitialize (EGLDisplay hDisplay, EGLint *pcMajor, EGLint *pcMinor)
181{
182 if (hDisplay == EGL_NO_DISPLAY)
183 return EGL_FALSE;
184 if (!VALID_PTR(hDisplay))
185 return setEGLError(EGL_BAD_DISPLAY);
186 if (pcMajor)
187 *pcMajor = 1;
188 if (pcMinor)
189 *pcMinor = 4;
190 return clearEGLError();
191}
192
193/** @todo This function should terminate all allocated resources. */
194DECLEXPORT(EGLBoolean) eglTerminate(EGLDisplay hDisplay)
195{
196 if (!VALID_PTR(hDisplay))
197 return EGL_FALSE;
198 return EGL_TRUE;
199}
200
201DECLEXPORT(const char *) eglQueryString(EGLDisplay hDisplay, EGLint name)
202{
203 RT_NOREF(hDisplay);
204 switch (name)
205 {
206 case EGL_CLIENT_APIS:
207 return "OpenGL";
208 case EGL_VENDOR:
209 return "Chromium";
210 case EGL_VERSION:
211 return "1.4 Chromium";
212 case EGL_EXTENSIONS:
213 return "";
214 default:
215 return NULL;
216 }
217}
218
219DECLEXPORT(EGLBoolean) eglGetConfigs (EGLDisplay hDisplay, EGLConfig *paConfigs, EGLint caConfigs, EGLint *pcaConfigs)
220{
221 Display *pDisplay = (Display *)hDisplay;
222 GLXFBConfig *paFBConfigs;
223 int caFBConfigs, i;
224
225 if (!VALID_PTR(pDisplay))
226 return setEGLError(EGL_NOT_INITIALIZED);
227 if (!VALID_PTR(pcaConfigs))
228 return setEGLError(EGL_BAD_PARAMETER);
229 if (caConfigs > 0 && !VALID_PTR(paConfigs))
230 return setEGLError(EGL_BAD_PARAMETER);
231 paFBConfigs = glXGetFBConfigs(pDisplay, DefaultScreen(pDisplay), &caFBConfigs);
232 if (!VALID_PTR(paFBConfigs))
233 return setEGLError(EGL_BAD_PARAMETER);
234 if (caFBConfigs > caConfigs)
235 caFBConfigs = caConfigs;
236 *pcaConfigs = caFBConfigs;
237 for (i = 0; i < caFBConfigs; ++i)
238 paConfigs[i] = (EGLConfig)paFBConfigs[i];
239 XFree(paFBConfigs);
240 return clearEGLError();
241}
242
243static int convertEGLAttribToGLX(EGLint a_EGLAttrib)
244{
245 switch (a_EGLAttrib)
246 {
247 case EGL_BUFFER_SIZE:
248 return GLX_BUFFER_SIZE;
249 case EGL_RED_SIZE:
250 return GLX_RED_SIZE;
251 case EGL_GREEN_SIZE:
252 return GLX_GREEN_SIZE;
253 case EGL_BLUE_SIZE:
254 return GLX_BLUE_SIZE;
255 case EGL_LUMINANCE_SIZE:
256 return GLX_RED_SIZE;
257 case EGL_ALPHA_SIZE:
258 return GLX_ALPHA_SIZE;
259 /* case EGL_ALPHA_MASK_SIZE: */
260 /* case EGL_BIND_TO_TEXTURE_RGB: */
261 /* case EGL_BIND_TO_TEXTURE_RGBA: */
262 /* case EGL_COLOR_BUFFER_TYPE: */
263 /* case EGL_CONFIG_CAVEAT: */
264 case EGL_CONFIG_ID:
265 return GLX_FBCONFIG_ID;
266 /* case EGL_CONFORMANT: */
267 case EGL_DEPTH_SIZE:
268 return GLX_DEPTH_SIZE;
269 case EGL_LEVEL:
270 return GLX_LEVEL;
271 case EGL_MAX_PBUFFER_WIDTH:
272 return GLX_MAX_PBUFFER_WIDTH;
273 case EGL_MAX_PBUFFER_HEIGHT:
274 return GLX_MAX_PBUFFER_HEIGHT;
275 case EGL_MAX_PBUFFER_PIXELS:
276 return GLX_MAX_PBUFFER_PIXELS;
277 /* case EGL_MATCH_NATIVE_PIXMAP: */
278 /* case EGL_MAX_SWAP_INTERVAL: */
279 /* case EGL_MIN_SWAP_INTERVAL: */
280 case EGL_NATIVE_RENDERABLE:
281 return GLX_X_RENDERABLE;
282 case EGL_NATIVE_VISUAL_ID:
283 return GLX_VISUAL_ID;
284 /* case EGL_NATIVE_VISUAL_TYPE: */
285 /* case EGL_RENDERABLE_TYPE: */
286 case EGL_SAMPLE_BUFFERS:
287 return GLX_SAMPLE_BUFFERS;
288 case EGL_SAMPLES:
289 return GLX_SAMPLES;
290 case EGL_STENCIL_SIZE:
291 return GLX_STENCIL_SIZE;
292 /* case EGL_SURFACE_TYPE: */
293 /* case EGL_TRANSPARENT_TYPE: */
294 case EGL_TRANSPARENT_RED_VALUE:
295 return GLX_TRANSPARENT_RED_VALUE;
296 case EGL_TRANSPARENT_GREEN_VALUE:
297 return GLX_TRANSPARENT_GREEN_VALUE;
298 case EGL_TRANSPARENT_BLUE_VALUE:
299 return GLX_TRANSPARENT_BLUE_VALUE;
300 default:
301 return None;
302 }
303}
304
305DECLEXPORT(EGLBoolean) eglChooseConfig (EGLDisplay hDisplay, const EGLint *paAttribs, EGLConfig *paConfigs, EGLint caConfigs,
306 EGLint *pcConfigs)
307{
308 Display *pDisplay = (Display *)hDisplay;
309 int aAttribList[256]; /* The list cannot be this long. */
310 unsigned cAttribs = 0, i;
311 const EGLint *pAttrib, *pAttrib2;
312 EGLint cRenderableType = EGL_OPENGL_ES_BIT;
313 unsigned cConfigCaveat = GLX_DONT_CARE, cConformant = GLX_DONT_CARE;
314 GLXFBConfig *paFBConfigs;
315 int caFBConfigs;
316
317 if (!VALID_PTR(hDisplay))
318 return setEGLError(EGL_NOT_INITIALIZED);
319 if (!VALID_PTR(pcConfigs))
320 return setEGLError(EGL_BAD_PARAMETER);
321 if (caConfigs > 0 && !VALID_PTR(paConfigs))
322 return setEGLError(EGL_BAD_PARAMETER);
323 for (pAttrib = paAttribs; pAttrib != NULL && *pAttrib != EGL_NONE; pAttrib += 2)
324 {
325 bool fSkip = false;
326 int cGLXAttrib;
327
328 /* Check for illegal values. */
329 if ((*pAttrib == EGL_LEVEL || *pAttrib == EGL_MATCH_NATIVE_PIXMAP) && pAttrib[1] == EGL_DONT_CARE)
330 return setEGLError(EGL_BAD_ATTRIBUTE);
331 /* Check for values we can't handle. */
332 if ( (*pAttrib == EGL_ALPHA_MASK_SIZE)
333 && pAttrib[1] != EGL_DONT_CARE && pAttrib[1] != 0)
334 return setEGLError(EGL_BAD_ACCESS);
335 /** @todo try creating a pixmap from a native one with the configurations returned. */
336 if (*pAttrib == EGL_MATCH_NATIVE_PIXMAP)
337 return setEGLError(EGL_BAD_ACCESS);
338 if ( ( *pAttrib == EGL_MIN_SWAP_INTERVAL || *pAttrib == EGL_MAX_SWAP_INTERVAL
339 || *pAttrib == EGL_BIND_TO_TEXTURE_RGB || *pAttrib == EGL_BIND_TO_TEXTURE_RGBA)
340 && pAttrib[1] != EGL_DONT_CARE)
341 return setEGLError(EGL_BAD_ACCESS);
342 /* Ignore attributes which are repeated later. */
343 for (pAttrib2 = pAttrib + 2; *pAttrib2 != EGL_NONE; pAttrib2 += 2)
344 if (*pAttrib2 == *pAttrib)
345 fSkip == true;
346 if (fSkip)
347 continue;
348 cGLXAttrib = convertEGLAttribToGLX(*pAttrib);
349 if (cGLXAttrib != None)
350 {
351 aAttribList[cAttribs] = cGLXAttrib;
352 if (pAttrib[1] == EGL_DONT_CARE)
353 aAttribList[cAttribs + 1] = GLX_DONT_CARE;
354 else
355 aAttribList[cAttribs + 1] = pAttrib[1];
356 cAttribs += 2;
357 }
358 else
359 {
360 switch (*pAttrib)
361 {
362 case EGL_COLOR_BUFFER_TYPE:
363 aAttribList[cAttribs] = GLX_X_VISUAL_TYPE;
364 aAttribList[cAttribs + 1] = pAttrib[1] == EGL_DONT_CARE ? GLX_DONT_CARE
365 : pAttrib[1] == EGL_RGB_BUFFER ? GLX_TRUE_COLOR
366 : pAttrib[1] == EGL_LUMINANCE_BUFFER ? GLX_GRAY_SCALE
367 : GL_FALSE;
368 if ( *pAttrib == EGL_COLOR_BUFFER_TYPE
369 && pAttrib[1] != EGL_DONT_CARE && pAttrib[1] != EGL_RGB_BUFFER)
370 return setEGLError(EGL_BAD_ACCESS);
371 break;
372 case EGL_CONFIG_CAVEAT:
373 cConfigCaveat = pAttrib[1] == EGL_DONT_CARE ? GLX_DONT_CARE
374 : pAttrib[1] == EGL_NONE ? GLX_NONE
375 : pAttrib[1] == EGL_SLOW_CONFIG ? GLX_SLOW_CONFIG
376 : pAttrib[1] == EGL_NON_CONFORMANT_CONFIG ? GLX_NON_CONFORMANT_CONFIG
377 : GL_FALSE;
378 if (!cConfigCaveat)
379 return setEGLError(EGL_BAD_ATTRIBUTE);
380 cAttribs -= 2;
381 break;
382 case EGL_CONFORMANT:
383 if (pAttrib[1] != EGL_OPENGL_BIT && pAttrib[1] != 0)
384 return setEGLError(EGL_BAD_ACCESS);
385 cConformant = pAttrib[1] == EGL_OPENGL_BIT ? GL_TRUE : GL_FALSE;
386 cAttribs -= 2;
387 break;
388 case EGL_NATIVE_VISUAL_TYPE:
389 aAttribList[cAttribs] = GLX_X_VISUAL_TYPE;
390 aAttribList[cAttribs + 1] = pAttrib[1] == EGL_DONT_CARE ? GLX_DONT_CARE
391 : pAttrib[1] == StaticGray ? GLX_STATIC_GRAY
392 : pAttrib[1] == StaticColor ? GLX_STATIC_COLOR
393 : pAttrib[1] == TrueColor ? GLX_TRUE_COLOR
394 : pAttrib[1] == GrayScale ? GLX_GRAY_SCALE
395 : pAttrib[1] == PseudoColor ? GLX_PSEUDO_COLOR
396 : pAttrib[1] == DirectColor ? GLX_DIRECT_COLOR
397 : GL_FALSE;
398 break;
399 case EGL_RENDERABLE_TYPE:
400 cRenderableType = pAttrib[1];
401 break;
402 case EGL_SURFACE_TYPE:
403 if (pAttrib[1] & ~(EGL_PBUFFER_BIT | EGL_PIXMAP_BIT | EGL_WINDOW_BIT))
404 return setEGLError(EGL_BAD_ACCESS);
405 aAttribList[cAttribs] = GLX_DRAWABLE_TYPE;
406 aAttribList[cAttribs + 1] = (pAttrib[1] & EGL_PBUFFER_BIT ? GLX_PBUFFER_BIT : 0)
407 | (pAttrib[1] & EGL_PIXMAP_BIT ? GLX_PIXMAP_BIT : 0)
408 | (pAttrib[1] & EGL_WINDOW_BIT ? GLX_WINDOW_BIT : 0);
409 break;
410 case EGL_TRANSPARENT_TYPE:
411 aAttribList[cAttribs] = GLX_TRANSPARENT_TYPE;
412 aAttribList[cAttribs + 1] = pAttrib[1] == EGL_DONT_CARE ? GLX_DONT_CARE
413 : pAttrib[1] == EGL_NONE ? GLX_NONE
414 : pAttrib[1] == EGL_TRANSPARENT_RGB ? GLX_TRANSPARENT_RGB
415 : GL_FALSE;
416 break;
417 default:
418 return setEGLError(EGL_BAD_ATTRIBUTE);
419 }
420 cAttribs += 2;
421 }
422 }
423 if (cConfigCaveat != GLX_DONT_CARE || cConformant != GLX_DONT_CARE)
424 {
425 aAttribList[cAttribs] = GLX_CONFIG_CAVEAT;
426 aAttribList[cAttribs + 1] = cConformant == GL_FALSE ? GLX_NON_CONFORMANT_CONFIG
427 : cConfigCaveat == EGL_SLOW_CONFIG ? GLX_SLOW_CONFIG
428 : GLX_NONE;
429 cAttribs += 2;
430 }
431 aAttribList[cAttribs] = GLX_RENDER_TYPE;
432 aAttribList[cAttribs + 1] = GLX_RGBA_BIT;
433 cAttribs += 2;
434 if (paAttribs != NULL)
435 {
436 aAttribList[cAttribs] = None;
437 EGL_ASSERT(cAttribs < RT_ELEMENTS(aAttribList));
438 if (!(cRenderableType & EGL_OPENGL_BIT))
439 return setEGLError(EGL_BAD_ACCESS);
440 }
441 paFBConfigs = glXChooseFBConfig(pDisplay, DefaultScreen(pDisplay), paAttribs != NULL ? aAttribList : NULL, &caFBConfigs);
442 if (paFBConfigs == NULL)
443 return setEGLError(EGL_BAD_ACCESS);
444 *pcConfigs = caFBConfigs;
445 for (i = 0; (GLint)i < caConfigs && (GLint)i < caFBConfigs; ++i)
446 paConfigs[i] = (EGLConfig)paFBConfigs[i];
447 XFree(paFBConfigs);
448 return clearEGLError();
449}
450
451DECLEXPORT(EGLBoolean) eglGetConfigAttrib (EGLDisplay hDisplay, EGLConfig cConfig, EGLint cAttribute, EGLint *pValue)
452{
453 Display *pDisplay = (Display *)hDisplay;
454 int cGLXAttribute = convertEGLAttribToGLX(cAttribute);
455 int cValue;
456
457 if (!VALID_PTR(hDisplay))
458 return setEGLError(EGL_NOT_INITIALIZED);
459 if (!VALID_PTR(pValue))
460 return setEGLError(EGL_BAD_PARAMETER);
461 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_FBCONFIG_ID, &cValue))
462 return setEGLError(EGL_BAD_CONFIG);
463 if (cGLXAttribute != None)
464 {
465 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, cGLXAttribute, &cValue))
466 return setEGLError(EGL_BAD_ACCESS);
467 *pValue = cValue;
468 return clearEGLError();
469 }
470 switch (cAttribute)
471 {
472 case EGL_ALPHA_MASK_SIZE:
473 *pValue = 0;
474 return clearEGLError();
475 case EGL_LUMINANCE_SIZE:
476 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_X_VISUAL_TYPE, &cValue))
477 return setEGLError(EGL_BAD_ACCESS);
478 if (cValue == GLX_STATIC_GRAY || cValue == GLX_GRAY_SCALE)
479 {
480 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_RED_SIZE, &cValue))
481 return setEGLError(EGL_BAD_ACCESS);
482 *pValue = cValue;
483 }
484 else
485 *pValue = 0;
486 return clearEGLError();
487 case EGL_COLOR_BUFFER_TYPE:
488 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_X_VISUAL_TYPE, &cValue))
489 return setEGLError(EGL_BAD_ACCESS);
490 if (cValue == GLX_STATIC_GRAY || cValue == GLX_GRAY_SCALE)
491 *pValue = EGL_LUMINANCE_BUFFER;
492 else
493 *pValue = EGL_RGB_BUFFER;
494 return clearEGLError();
495 case EGL_CONFIG_CAVEAT:
496 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_CONFIG_CAVEAT, &cValue))
497 return setEGLError(EGL_BAD_ACCESS);
498 *pValue = cValue == GLX_NONE ? EGL_NONE : cValue == GLX_SLOW_CONFIG ? EGL_SLOW_CONFIG : GLX_NON_CONFORMANT_CONFIG;
499 return clearEGLError();
500 case EGL_CONFORMANT:
501 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_CONFIG_CAVEAT, &cValue))
502 return setEGLError(EGL_BAD_ACCESS);
503 *pValue = cValue == GLX_NON_CONFORMANT_CONFIG ? 0 : EGL_OPENGL_BIT;
504 return clearEGLError();
505 case EGL_MATCH_NATIVE_PIXMAP:
506 case EGL_MIN_SWAP_INTERVAL:
507 case EGL_MAX_SWAP_INTERVAL:
508 return setEGLError(EGL_BAD_ACCESS);
509 case EGL_NATIVE_VISUAL_TYPE:
510 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_X_VISUAL_TYPE, &cValue))
511 return setEGLError(EGL_BAD_ACCESS);
512 *pValue = cValue == GLX_STATIC_GRAY ? StaticGray
513 : cValue == GLX_STATIC_COLOR ? StaticColor
514 : cValue == GLX_TRUE_COLOR ? TrueColor
515 : cValue == GLX_GRAY_SCALE ? GrayScale
516 : cValue == GLX_PSEUDO_COLOR ? PseudoColor
517 : cValue == GLX_DIRECT_COLOR ? DirectColor
518 : -1;
519 return clearEGLError();
520 case EGL_RENDERABLE_TYPE:
521 *pValue = EGL_OPENGL_BIT;
522 return clearEGLError();
523 case EGL_SURFACE_TYPE:
524 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_DRAWABLE_TYPE, &cValue))
525 return setEGLError(EGL_BAD_ACCESS);
526 *pValue = (cValue & GLX_PBUFFER_BIT ? EGL_PBUFFER_BIT : 0)
527 | (cValue & GLX_PIXMAP_BIT ? EGL_PIXMAP_BIT : 0)
528 | (cValue & GLX_WINDOW_BIT ? EGL_WINDOW_BIT : 0);
529 return clearEGLError();
530 case EGL_TRANSPARENT_TYPE:
531 if (glXGetFBConfigAttrib(pDisplay, (GLXFBConfig)cConfig, GLX_TRANSPARENT_TYPE, &cValue))
532 return setEGLError(EGL_BAD_ACCESS);
533 *pValue = cValue == GLX_NONE ? EGL_NONE
534 : cValue == GLX_TRANSPARENT_RGB ? EGL_TRANSPARENT_RGB
535 : EGL_FALSE;
536 return *pValue != EGL_FALSE ? clearEGLError() : setEGLError(EGL_BAD_ACCESS);
537 default:
538 return setEGLError(EGL_BAD_ATTRIBUTE);
539 }
540 return clearEGLError();
541}
542
543DECLEXPORT(EGLSurface) eglCreateWindowSurface(EGLDisplay hDisplay, EGLConfig config, EGLNativeWindowType hWindow,
544 const EGLint *paAttributes)
545{
546 Display *pDisplay = (Display *)hDisplay;
547 GLXWindow hGLXWindow;
548
549 if (!VALID_PTR(hDisplay))
550 {
551 setEGLError(EGL_NOT_INITIALIZED);
552 return EGL_NO_SURFACE;
553 }
554 if (paAttributes != NULL) /* Sanity test only. */
555 while (*paAttributes != EGL_NONE)
556 {
557 if (*paAttributes != EGL_RENDER_BUFFER)
558 {
559 setEGLError(EGL_BAD_MATCH);
560 return EGL_NO_SURFACE;
561 }
562 paAttributes += 2;
563 }
564 hGLXWindow = glXCreateWindow(pDisplay, (GLXFBConfig)config, (Window)hWindow, NULL);
565 if (hGLXWindow == None)
566 {
567 setEGLError(EGL_BAD_ALLOC);
568 return EGL_NO_SURFACE;
569 }
570 EGL_ASSERT(hGLXWindow < VBEGL_WINDOW_SURFACE); /* Greater than the maximum XID. */
571 clearEGLError();
572 return (EGLSurface)(hGLXWindow | VBEGL_WINDOW_SURFACE);
573}
574
575static void setAttribute(int *pcStoreIndex, int *pcCurIndex, int *paGLXAttributes, int cAttribute, int cValue)
576{
577 if (*pcStoreIndex < 0)
578 {
579 *pcStoreIndex = *pcCurIndex;
580 *pcCurIndex += 2;
581 paGLXAttributes[*pcStoreIndex] = cAttribute;
582 }
583 paGLXAttributes[*pcStoreIndex + 1] = cValue;
584}
585
586DECLEXPORT(EGLSurface) eglCreatePbufferSurface(EGLDisplay hDisplay, EGLConfig config, EGLint const *paAttributes)
587{
588 Display *pDisplay = (Display *)hDisplay;
589 enum { CPS_WIDTH = 0, CPS_HEIGHT, CPS_LARGEST, CPS_PRESERVED, CPS_TEX_FORMAT, CPS_TEX_TARGET, CPS_MIPMAP_TEX, CPS_END };
590 int acIndices[CPS_END];
591 int aAttributes[CPS_END * 2];
592 int cIndex = 0;
593 unsigned i;
594 GLXPbuffer hPbuffer;
595
596 if (!VALID_PTR(hDisplay))
597 {
598 setEGLError(EGL_NOT_INITIALIZED);
599 return EGL_NO_SURFACE;
600 }
601 for (i = 0; i < RT_ELEMENTS(acIndices); ++i)
602 acIndices[i] = -1;
603 if (paAttributes != NULL)
604 while (*paAttributes != EGL_NONE)
605 {
606 switch (*paAttributes)
607 {
608 case EGL_WIDTH:
609 setAttribute(&acIndices[CPS_WIDTH], &cIndex, aAttributes, GLX_PBUFFER_WIDTH, paAttributes[1]);
610 break;
611 case EGL_HEIGHT:
612 setAttribute(&acIndices[CPS_HEIGHT], &cIndex, aAttributes, GLX_LARGEST_PBUFFER, paAttributes[1]);
613 break;
614 case EGL_LARGEST_PBUFFER:
615 setAttribute(&acIndices[CPS_LARGEST], &cIndex, aAttributes, GLX_PBUFFER_HEIGHT, paAttributes[1]);
616 break;
617 case EGL_BUFFER_PRESERVED:
618 setAttribute(&acIndices[CPS_PRESERVED], &cIndex, aAttributes, GLX_PRESERVED_CONTENTS, paAttributes[1]);
619 break;
620 case EGL_TEXTURE_FORMAT:
621 setAttribute(&acIndices[CPS_TEX_FORMAT], &cIndex, aAttributes, GLX_TEXTURE_FORMAT_EXT, paAttributes[1]);
622 break;
623 case EGL_TEXTURE_TARGET:
624 setAttribute(&acIndices[CPS_TEX_TARGET], &cIndex, aAttributes, GLX_TEXTURE_TARGET_EXT, paAttributes[1]);
625 break;
626 case EGL_MIPMAP_TEXTURE:
627 setAttribute(&acIndices[CPS_MIPMAP_TEX], &cIndex, aAttributes, GLX_MIPMAP_TEXTURE_EXT, paAttributes[1]);
628 break;
629 case EGL_VG_ALPHA_FORMAT:
630 case EGL_VG_COLORSPACE:
631 {
632 setEGLError(EGL_BAD_MATCH);
633 return EGL_NO_SURFACE;
634 }
635 }
636 paAttributes += 2;
637 }
638 EGL_ASSERT((unsigned)cIndex < RT_ELEMENTS(aAttributes) - 1U);
639 aAttributes[cIndex + 1] = None;
640 hPbuffer = glXCreatePbuffer(pDisplay, (GLXFBConfig)config, aAttributes);
641 if (hPbuffer == None)
642 {
643 setEGLError(EGL_BAD_ALLOC);
644 return EGL_NO_SURFACE;
645 }
646 EGL_ASSERT(hPbuffer < VBEGL_WINDOW_SURFACE); /* Greater than the maximum XID. */
647 clearEGLError();
648 return (EGLSurface)(hPbuffer | VBEGL_PBUFFER_SURFACE);
649}
650
651DECLEXPORT(EGLSurface) eglCreatePixmapSurface(EGLDisplay hDisplay, EGLConfig config, EGLNativePixmapType hPixmap,
652 const EGLint *paAttributes)
653{
654 Display *pDisplay = (Display *)hDisplay;
655 GLXPixmap hGLXPixmap;
656
657 if (!VALID_PTR(hDisplay))
658 {
659 setEGLError(EGL_NOT_INITIALIZED);
660 return EGL_NO_SURFACE;
661 }
662 if (paAttributes != NULL) /* Sanity test only. */
663 if (*paAttributes != EGL_NONE)
664 {
665 if (*paAttributes == EGL_VG_COLORSPACE || *paAttributes == EGL_VG_ALPHA_FORMAT)
666 {
667 setEGLError(EGL_BAD_MATCH);
668 return EGL_NO_SURFACE;
669 }
670 else
671 {
672 setEGLError(EGL_BAD_ATTRIBUTE);
673 return EGL_NO_SURFACE;
674 }
675 }
676 hGLXPixmap = glXCreatePixmap(pDisplay, (GLXFBConfig)config, (Pixmap)hPixmap, NULL);
677 if (hGLXPixmap == None)
678 {
679 setEGLError(EGL_BAD_MATCH);
680 return EGL_NO_SURFACE;
681 }
682 EGL_ASSERT(hGLXPixmap < VBEGL_WINDOW_SURFACE); /* Greater than the maximum XID. */
683 clearEGLError();
684 return (EGLSurface)(hGLXPixmap | VBEGL_PIXMAP_SURFACE);
685}
686
687DECLEXPORT(EGLBoolean) eglDestroySurface(EGLDisplay hDisplay, EGLSurface hSurface)
688{
689 Display *pDisplay = (Display *)hDisplay;
690
691 if (!VALID_PTR(hDisplay))
692 return setEGLError(EGL_NOT_INITIALIZED);
693 switch ((GLXDrawable)hSurface & VBEGL_ANY_SURFACE)
694 {
695 case VBEGL_WINDOW_SURFACE:
696 glXDestroyWindow(pDisplay, (GLXWindow)hSurface & ~VBEGL_WINDOW_SURFACE);
697 return clearEGLError();
698 case VBEGL_PBUFFER_SURFACE:
699 glXDestroyPbuffer(pDisplay, (GLXPbuffer)hSurface & ~VBEGL_PBUFFER_SURFACE);
700 return clearEGLError();
701 case VBEGL_PIXMAP_SURFACE:
702 glXDestroyPixmap(pDisplay, (GLXPixmap)hSurface & ~VBEGL_PIXMAP_SURFACE);
703 return clearEGLError();
704 default:
705 return setEGLError(EGL_BAD_SURFACE);
706 }
707}
708
709DECLEXPORT(EGLBoolean) eglSurfaceAttrib(EGLDisplay hDisplay, EGLSurface hSurface, EGLint cAttribute, EGLint cValue)
710{
711 NOREF(hDisplay);
712 NOREF(hSurface);
713 NOREF(cValue);
714 switch (cAttribute)
715 {
716 case EGL_MIPMAP_LEVEL:
717 case EGL_MULTISAMPLE_RESOLVE:
718 case EGL_SWAP_BEHAVIOR:
719 return setEGLError(EGL_BAD_MATCH);
720 default:
721 return setEGLError(EGL_BAD_ATTRIBUTE);
722 }
723}
724
725DECLEXPORT(EGLBoolean) eglQuerySurface(EGLDisplay hDisplay, EGLSurface hSurface, EGLint cAttribute, EGLint *cValue)
726{
727 NOREF(hDisplay);
728 NOREF(hSurface);
729 NOREF(cAttribute);
730 NOREF(cValue);
731 return setEGLError(EGL_BAD_MATCH);
732}
733
734DECLEXPORT(EGLBoolean) eglBindTexImage(EGLDisplay hDisplay, EGLSurface hSurface, EGLint cBuffer)
735{
736 NOREF(hDisplay);
737 NOREF(hSurface);
738 NOREF(cBuffer);
739 return setEGLError(EGL_BAD_MATCH);
740}
741
742DECLEXPORT(EGLBoolean) eglReleaseTexImage(EGLDisplay hDisplay, EGLSurface hSurface, EGLint cBuffer)
743{
744 NOREF(hDisplay);
745 NOREF(hSurface);
746 NOREF(cBuffer);
747 return setEGLError(EGL_BAD_MATCH);
748}
749
750DECLEXPORT(EGLBoolean) eglBindAPI(EGLenum enmApi)
751{
752 return enmApi == EGL_OPENGL_API ? clearEGLError() : setEGLError(EGL_BAD_PARAMETER);
753}
754
755DECLEXPORT(EGLenum) eglQueryAPI(void)
756{
757 return EGL_OPENGL_API;
758}
759
760DECLEXPORT(EGLContext) eglCreateContext(EGLDisplay hDisplay, EGLConfig hConfig, EGLContext hSharedContext,
761 const EGLint *paAttribs)
762{
763 Display *pDisplay = (Display *)hDisplay;
764 GLXContext hNewContext;
765
766 if (!VALID_PTR(hDisplay))
767 {
768 setEGLError(EGL_NOT_INITIALIZED);
769 return EGL_NO_CONTEXT;
770 }
771 if (paAttribs != NULL && *paAttribs != EGL_NONE)
772 {
773 setEGLError(EGL_BAD_ATTRIBUTE);
774 return EGL_NO_CONTEXT;
775 }
776 hNewContext = glXCreateNewContext(pDisplay, (GLXFBConfig)hConfig, GLX_RGBA_TYPE, (GLXContext)hSharedContext, true);
777 if (hNewContext)
778 {
779 clearEGLError();
780 return (EGLContext)hNewContext;
781 }
782 setEGLError(EGL_BAD_MATCH);
783 return EGL_NO_CONTEXT;
784}
785
786DECLEXPORT(EGLBoolean) eglDestroyContext(EGLDisplay hDisplay, EGLContext hContext)
787{
788 Display *pDisplay = (Display *)hDisplay;
789
790 if (!VALID_PTR(hDisplay))
791 return setEGLError(EGL_NOT_INITIALIZED);
792 glXDestroyContext(pDisplay, (GLXContext) hContext);
793 return clearEGLError();
794}
795
796DECLEXPORT(EGLBoolean) eglMakeCurrent(EGLDisplay hDisplay, EGLSurface hDraw, EGLSurface hRead, EGLContext hContext)
797{
798 Display *pDisplay = (Display *)hDisplay;
799 GLXDrawable hGLXDraw = hDraw == EGL_NO_SURFACE ? None : (GLXDrawable)hDraw & ~VBEGL_ANY_SURFACE;
800 GLXDrawable hGLXRead = hRead == EGL_NO_SURFACE ? None : (GLXDrawable)hRead & ~VBEGL_ANY_SURFACE;
801 GLXContext hGLXContext = hContext == EGL_NO_CONTEXT ? None : (GLXContext)hContext;
802 struct VBEGLTLS *pTls = getTls();
803
804 if (!VALID_PTR(hDisplay) || !VALID_PTR(pTls))
805 return setEGLError(EGL_NOT_INITIALIZED);
806 if (glXMakeContextCurrent(pDisplay, hGLXDraw, hGLXRead, hGLXContext))
807 {
808 pTls->hCurrent = hContext;
809 pTls->hCurrentDraw = hDraw;
810 pTls->hCurrentRead = hRead;
811 return clearEGLError();
812 }
813 else
814 return setEGLError(EGL_BAD_MATCH);
815}
816
817DECLEXPORT(EGLContext) eglGetCurrentContext(void)
818{
819 struct VBEGLTLS *pTls = getTls();
820
821 if (!VALID_PTR(pTls))
822 return EGL_NO_CONTEXT;
823 clearEGLError();
824 return pTls->hCurrent;
825}
826
827DECLEXPORT(EGLSurface) eglGetCurrentSurface(EGLint cOp)
828{
829 struct VBEGLTLS *pTls = getTls();
830
831 if (!VALID_PTR(pTls))
832 return EGL_NO_SURFACE;
833 clearEGLError();
834 switch (cOp)
835 {
836 case EGL_DRAW:
837 return pTls->hCurrentDraw;
838 case EGL_READ:
839 return pTls->hCurrentRead;
840 default:
841 setEGLError(EGL_BAD_PARAMETER);
842 return EGL_NO_SURFACE;
843 }
844}
845
846DECLEXPORT(EGLDisplay) eglGetCurrentDisplay(void)
847{
848 struct VBEGLTLS *pTls;
849
850 pTls = getTls();
851 if (!VALID_PTR(pTls))
852 return EGL_NO_DISPLAY;
853 clearEGLError();
854 return pTls->hCurrentDisplay;
855}
856
857DECLEXPORT(EGLBoolean) eglQueryContext(EGLDisplay hDisplay, EGLContext hContext, EGLint cAttribute, EGLint *pcValue)
858{
859 Display *pDisplay = (Display *)hDisplay;
860
861 if (!VALID_PTR(hDisplay))
862 return setEGLError(EGL_NOT_INITIALIZED);
863 if (!VALID_PTR(pcValue))
864 return setEGLError(EGL_BAD_PARAMETER);
865 switch (cAttribute)
866 {
867 case EGL_CONFIG_ID:
868 {
869 int cValue = 0;
870
871 if (glXQueryContext(pDisplay, (GLXContext)hContext, GLX_FBCONFIG_ID, &cValue) == Success)
872 {
873 *pcValue = cValue;
874 return clearEGLError();
875 }
876 return setEGLError(EGL_BAD_MATCH);
877 }
878 case EGL_CONTEXT_CLIENT_TYPE:
879 *pcValue = EGL_OPENGL_API;
880 return clearEGLError();
881 case EGL_CONTEXT_CLIENT_VERSION:
882 *pcValue = 0;
883 return clearEGLError();
884 case EGL_RENDER_BUFFER:
885 *pcValue = EGL_BACK_BUFFER;
886 return clearEGLError();
887 default:
888 return setEGLError(EGL_BAD_ATTRIBUTE);
889 }
890}
891
892DECLEXPORT(EGLBoolean) eglWaitClient(void)
893{
894 glXWaitGL();
895 return clearEGLError();
896}
897
898DECLEXPORT(EGLBoolean) eglWaitGL(void)
899{
900 return setEGLError(EGL_BAD_PARAMETER); /* OpenGL ES only. */
901}
902
903DECLEXPORT(EGLBoolean) eglWaitNative(EGLint cEngine)
904{
905 if (cEngine != EGL_CORE_NATIVE_ENGINE)
906 return setEGLError(EGL_BAD_PARAMETER);
907 glXWaitX();
908 return clearEGLError();
909}
910
911DECLEXPORT(EGLBoolean) eglSwapBuffers(EGLDisplay hDisplay, EGLSurface hSurface)
912{
913 Display *pDisplay = (Display *)hDisplay;
914
915 if (!VALID_PTR(hDisplay))
916 return setEGLError(EGL_NOT_INITIALIZED);
917 glXSwapBuffers(pDisplay, (GLXDrawable)hSurface & ~VBEGL_ANY_SURFACE);
918 return clearEGLError();
919}
920
921/** @todo Work out how this fits over what Chromium has to offer. */
922DECLEXPORT(EGLBoolean) eglCopyBuffers(EGLDisplay hDisplay, EGLSurface hSurface, EGLNativePixmapType hPixmap)
923{
924 Display *pDisplay = (Display *)hDisplay;
925
926 if (!VALID_PTR(pDisplay))
927 return setEGLError(EGL_NOT_INITIALIZED);
928
929 NOREF(hSurface);
930 NOREF(hPixmap);
931 return setEGLError(EGL_BAD_MATCH);
932}
933
934DECLEXPORT(EGLBoolean) eglSwapInterval (EGLDisplay dpy, EGLint interval)
935{
936 NOREF(dpy);
937 NOREF(interval);
938 return EGL_TRUE;
939}
940
941typedef void (*VBEGLFuncPtr)(void);
942DECLEXPORT(VBEGLFuncPtr)eglGetProcAddress(const char *pszName)
943{
944 clearEGLError();
945 return glXGetProcAddress((const GLubyte *)pszName);
946}
947
948DECLEXPORT(EGLBoolean) eglReleaseThread()
949{
950 struct VBEGLTLS *pTls = getTls();
951
952 if (!(pTls))
953 return EGL_TRUE;
954 free(pTls);
955 /* Can this fail with ENOMEM? */
956 pthread_setspecific(g_tls, NULL);
957 return EGL_TRUE;
958}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette