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