summaryrefslogtreecommitdiffstats
path: root/src/egl/main/eglapi.c
blob: e8a5b18912041b8b907c4154a40ebbed068225a7 (plain)
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
/**
 * Public EGL API entrypoints
 *
 * Generally, we use the EGLDisplay parameter as a key to lookup the
 * appropriate device driver handle, then jump though the driver's
 * dispatch table to handle the function.
 *
 * That allows us the option of supporting multiple, simultaneous,
 * heterogeneous hardware devices in the future.
 *
 * The EGLDisplay, EGLConfig, EGLContext and EGLSurface types are
 * opaque handles implemented with 32-bit unsigned integers.
 * It's up to the driver function or fallback function to look up the
 * handle and get an object.
 * By using opaque handles, we leave open the possibility of having
 * indirect rendering in the future, like GLX.
 *
 *
 * Notes on naming conventions:
 *
 * eglFooBar    - public EGL function
 * EGL_FOO_BAR  - public EGL token
 * EGLDatatype  - public EGL datatype
 *
 * _eglFooBar   - private EGL function
 * _EGLDatatype - private EGL datatype, typedef'd struct
 * _egl_struct  - private EGL struct, non-typedef'd
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "eglcontext.h"
#include "egldisplay.h"
#include "egltypedefs.h"
#include "eglglobals.h"
#include "egldriver.h"
#include "eglsurface.h"
#include "eglconfig.h"
#include "eglscreen.h"
#include "eglmode.h"


/**
 * This is typically the first EGL function that an application calls.
 * We initialize our global vars and create a private _EGLDisplay object.
 */
EGLDisplay EGLAPIENTRY
eglGetDisplay(NativeDisplayType nativeDisplay)
{
   _EGLDisplay *dpy;
   dpy = _eglFindDisplay(nativeDisplay);
   if (!dpy) {
      dpy = _eglNewDisplay(nativeDisplay);
      if (dpy)
         _eglLinkDisplay(dpy);
   }
   return _eglGetDisplayHandle(dpy);
}


/**
 * This is typically the second EGL function that an application calls.
 * Here we load/initialize the actual hardware driver.
 */
EGLBoolean EGLAPIENTRY
eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLDriver *drv;
   EGLint major_int, minor_int;

   if (!disp)
      return _eglError(EGL_BAD_DISPLAY, __FUNCTION__);

   drv = disp->Driver;
   if (!drv) {
      drv = _eglOpenDriver(disp, disp->DriverName, disp->DriverArgs);
      if (!drv)
         return _eglError(EGL_NOT_INITIALIZED, __FUNCTION__);

      /* Initialize the particular display now */
      if (!drv->API.Initialize(drv, disp, &major_int, &minor_int)) {
         _eglCloseDriver(drv, disp);
         return _eglError(EGL_NOT_INITIALIZED, __FUNCTION__);
      }

      drv->APImajor = major_int;
      drv->APIminor = minor_int;
      snprintf(drv->Version, sizeof(drv->Version),
               "%d.%d (%s)", major_int, minor_int, drv->Name);

      disp->Driver = drv;
   } else {
      major_int = drv->APImajor;
      minor_int = drv->APIminor;
   }

   /* Update applications version of major and minor if not NULL */
   if ((major != NULL) && (minor != NULL)) {
      *major = major_int;
      *minor = minor_int;
   }

   return EGL_TRUE;
}


EGLBoolean EGLAPIENTRY
eglTerminate(EGLDisplay dpy)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLDriver *drv;

   if (!disp)
      return _eglError(EGL_BAD_DISPLAY, __FUNCTION__);

   drv = disp->Driver;
   if (drv) {
      /* TODO drv->API.Terminate should be called here */
      _eglCloseDriver(drv, disp);
      disp->Driver = NULL;
   }

   return EGL_TRUE;
}


/**
 * A bunch of check functions and declare macros to simply error checking.
 */
static INLINE _EGLDriver *
_eglCheckDisplay(_EGLDisplay *disp, const char *msg)
{
   if (!disp) {
      _eglError(EGL_BAD_DISPLAY, msg);
      return NULL;
   }
   if (!disp->Driver) {
      _eglError(EGL_NOT_INITIALIZED, msg);
      return NULL;
   }
   return disp->Driver;
}


static INLINE _EGLDriver *
_eglCheckSurface(_EGLDisplay *disp, _EGLSurface *surf, const char *msg)
{
   _EGLDriver *drv = _eglCheckDisplay(disp, msg);
   if (!drv)
      return NULL;
   if (!surf) {
      _eglError(EGL_BAD_SURFACE, msg);
      return NULL;
   }
   return drv;
}


static INLINE _EGLDriver *
_eglCheckContext(_EGLDisplay *disp, _EGLContext *context, const char *msg)
{
   _EGLDriver *drv = _eglCheckDisplay(disp, msg);
   if (!drv)
      return NULL;
   if (!context) {
      _eglError(EGL_BAD_CONTEXT, msg);
      return NULL;
   }
   return drv;
}


static INLINE _EGLDriver *
_eglCheckConfig(_EGLDisplay *disp, _EGLConfig *conf, const char *msg)
{
   _EGLDriver *drv = _eglCheckDisplay(disp, msg);
   if (!drv)
      return NULL;
   if (!conf) {
      _eglError(EGL_BAD_CONFIG, msg);
      return NULL;
   }
   return drv;
}


#define _EGL_DECLARE_DD(dpy)                                   \
   _EGLDisplay *disp = _eglLookupDisplay(dpy);                 \
   _EGLDriver *drv;                                            \
   do {                                                        \
      drv = _eglCheckDisplay(disp, __FUNCTION__);              \
      if (!drv)                                                \
         return EGL_FALSE;                                     \
   } while (0)


#define _EGL_DECLARE_DD_AND_SURFACE(dpy, surface)              \
   _EGLDisplay *disp = _eglLookupDisplay(dpy);                 \
   _EGLSurface *surf = _eglLookupSurface((surface), disp);     \
   _EGLDriver *drv;                                            \
   do {                                                        \
      drv = _eglCheckSurface(disp, surf, __FUNCTION__);        \
      if (!drv)                                                \
         return EGL_FALSE;                                     \
   } while (0)


#define _EGL_DECLARE_DD_AND_CONTEXT(dpy, ctx)                  \
   _EGLDisplay *disp = _eglLookupDisplay(dpy);                 \
   _EGLContext *context = _eglLookupContext((ctx), disp);      \
   _EGLDriver *drv;                                            \
   do {                                                        \
      drv = _eglCheckContext(disp, context, __FUNCTION__);     \
      if (!drv)                                                \
         return EGL_FALSE;                                     \
   } while (0)


#ifdef EGL_MESA_screen_surface


static INLINE _EGLDriver *
_eglCheckScreen(_EGLDisplay *disp, _EGLScreen *scrn, const char *msg)
{
   _EGLDriver *drv = _eglCheckDisplay(disp, msg);
   if (!drv)
      return NULL;
   if (!scrn) {
      _eglError(EGL_BAD_SCREEN_MESA, msg);
      return NULL;
   }
   return drv;
}


static INLINE _EGLDriver *
_eglCheckMode(_EGLDisplay *disp, _EGLMode *m, const char *msg)
{
   _EGLDriver *drv = _eglCheckDisplay(disp, msg);
   if (!drv)
      return NULL;
   if (!m) {
      _eglError(EGL_BAD_MODE_MESA, msg);
      return NULL;
   }
   return drv;
}


#define _EGL_DECLARE_DD_AND_SCREEN(dpy, screen)                \
   _EGLDisplay *disp = _eglLookupDisplay(dpy);                 \
   _EGLScreen *scrn = _eglLookupScreen((screen), disp);        \
   _EGLDriver *drv;                                            \
   do {                                                        \
      drv = _eglCheckScreen(disp, scrn, __FUNCTION__);         \
      if (!drv)                                                \
         return EGL_FALSE;                                     \
   } while (0)


#define _EGL_DECLARE_DD_AND_MODE(dpy, mode)                    \
   _EGLDisplay *disp = _eglLookupDisplay(dpy);                 \
   _EGLMode *m = _eglLookupMode((mode), disp);                 \
   _EGLDriver *drv;                                            \
   do {                                                        \
      drv = _eglCheckMode(disp, m, __FUNCTION__);              \
      if (!drv)                                                \
         return EGL_FALSE;                                     \
   } while (0)


#endif /* EGL_MESA_screen_surface */


const char * EGLAPIENTRY
eglQueryString(EGLDisplay dpy, EGLint name)
{
   _EGL_DECLARE_DD(dpy);
   return drv->API.QueryString(drv, disp, name);
}


EGLBoolean EGLAPIENTRY
eglGetConfigs(EGLDisplay dpy, EGLConfig *configs,
              EGLint config_size, EGLint *num_config)
{
   _EGL_DECLARE_DD(dpy);
   return drv->API.GetConfigs(drv, disp, configs, config_size, num_config);
}


EGLBoolean EGLAPIENTRY
eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs,
                EGLint config_size, EGLint *num_config)
{
   _EGL_DECLARE_DD(dpy);
   return drv->API.ChooseConfig(drv, disp, attrib_list, configs,
                                config_size, num_config);
}


EGLBoolean EGLAPIENTRY
eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
                   EGLint attribute, EGLint *value)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLConfig *conf = _eglLookupConfig(config, disp);
   _EGLDriver *drv;

   drv = _eglCheckConfig(disp, conf, __FUNCTION__);
   if (!drv)
      return EGL_FALSE;

   return drv->API.GetConfigAttrib(drv, disp, conf, attribute, value);
}


EGLContext EGLAPIENTRY
eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_list,
                 const EGLint *attrib_list)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLConfig *conf = _eglLookupConfig(config, disp);
   _EGLContext *share = _eglLookupContext(share_list, disp);
   _EGLDriver *drv;
   _EGLContext *context;

   drv = _eglCheckConfig(disp, conf, __FUNCTION__);
   if (!drv)
      return EGL_NO_CONTEXT;
   if (!share && share_list != EGL_NO_CONTEXT) {
      _eglError(EGL_BAD_CONTEXT, __FUNCTION__);
      return EGL_NO_CONTEXT;
   }

   context = drv->API.CreateContext(drv, disp, conf, share, attrib_list);
   if (context)
      return _eglLinkContext(context, disp);
   else
      return EGL_NO_CONTEXT;
}


EGLBoolean EGLAPIENTRY
eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
{
   _EGL_DECLARE_DD_AND_CONTEXT(dpy, ctx);
   _eglUnlinkContext(context);
   return drv->API.DestroyContext(drv, disp, context);
}


EGLBoolean EGLAPIENTRY
eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read,
               EGLContext ctx)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLContext *context = _eglLookupContext(ctx, disp);
   _EGLSurface *draw_surf = _eglLookupSurface(draw, disp);
   _EGLSurface *read_surf = _eglLookupSurface(read, disp);
   _EGLDriver *drv;

   drv = _eglCheckDisplay(disp, __FUNCTION__);
   if (!drv)
      return EGL_FALSE;
   if (!context && ctx != EGL_NO_CONTEXT)
      return _eglError(EGL_BAD_CONTEXT, __FUNCTION__);
   if ((!draw_surf && draw != EGL_NO_SURFACE) ||
       (!read_surf && read != EGL_NO_SURFACE))
      return _eglError(EGL_BAD_SURFACE, __FUNCTION__);

   return drv->API.MakeCurrent(drv, disp, draw_surf, read_surf, context);
}


EGLBoolean EGLAPIENTRY
eglQueryContext(EGLDisplay dpy, EGLContext ctx,
                EGLint attribute, EGLint *value)
{
   _EGL_DECLARE_DD_AND_CONTEXT(dpy, ctx);
   return drv->API.QueryContext(drv, disp, context, attribute, value);
}


EGLSurface EGLAPIENTRY
eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
                       NativeWindowType window, const EGLint *attrib_list)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLConfig *conf = _eglLookupConfig(config, disp);
   _EGLDriver *drv;
   _EGLSurface *surf;

   drv = _eglCheckConfig(disp, conf, __FUNCTION__);
   if (!drv)
      return EGL_NO_SURFACE;

   surf = drv->API.CreateWindowSurface(drv, disp, conf, window, attrib_list);
   if (surf)
      return _eglLinkSurface(surf, disp);
   else
      return EGL_NO_SURFACE;
}


EGLSurface EGLAPIENTRY
eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
                       NativePixmapType pixmap, const EGLint *attrib_list)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLConfig *conf = _eglLookupConfig(config, disp);
   _EGLDriver *drv;
   _EGLSurface *surf;

   drv = _eglCheckConfig(disp, conf, __FUNCTION__);
   if (!drv)
      return EGL_NO_SURFACE;

   surf = drv->API.CreatePixmapSurface(drv, disp, conf, pixmap, attrib_list);
   if (surf)
      return _eglLinkSurface(surf, disp);
   else
      return EGL_NO_SURFACE;
}


EGLSurface EGLAPIENTRY
eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config,
                        const EGLint *attrib_list)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLConfig *conf = _eglLookupConfig(config, disp);
   _EGLDriver *drv;
   _EGLSurface *surf;

   drv = _eglCheckConfig(disp, conf, __FUNCTION__);
   if (!drv)
      return EGL_NO_SURFACE;

   surf = drv->API.CreatePbufferSurface(drv, disp, conf, attrib_list);
   if (surf)
      return _eglLinkSurface(surf, disp);
   else
      return EGL_NO_SURFACE;
}


EGLBoolean EGLAPIENTRY
eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
{
   _EGL_DECLARE_DD_AND_SURFACE(dpy, surface);
   _eglUnlinkSurface(surf);
   return drv->API.DestroySurface(drv, disp, surf);
}

EGLBoolean EGLAPIENTRY
eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
                EGLint attribute, EGLint *value)
{
   _EGL_DECLARE_DD_AND_SURFACE(dpy, surface);
   return drv->API.QuerySurface(drv, disp, surf, attribute, value);
}

EGLBoolean EGLAPIENTRY
eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
                 EGLint attribute, EGLint value)
{
   _EGL_DECLARE_DD_AND_SURFACE(dpy, surface);
   return drv->API.SurfaceAttrib(drv, disp, surf, attribute, value);
}


EGLBoolean EGLAPIENTRY
eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
{
   _EGL_DECLARE_DD_AND_SURFACE(dpy, surface);
   return drv->API.BindTexImage(drv, disp, surf, buffer);
}


EGLBoolean EGLAPIENTRY
eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
{
   _EGL_DECLARE_DD_AND_SURFACE(dpy, surface);
   return drv->API.ReleaseTexImage(drv, disp, surf, buffer);
}


EGLBoolean EGLAPIENTRY
eglSwapInterval(EGLDisplay dpy, EGLint interval)
{
   _EGL_DECLARE_DD(dpy);
   return drv->API.SwapInterval(drv, disp, interval);
}


EGLBoolean EGLAPIENTRY
eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
{
   _EGL_DECLARE_DD_AND_SURFACE(dpy, surface);
   return drv->API.SwapBuffers(drv, disp, surf);
}


EGLBoolean EGLAPIENTRY
eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, NativePixmapType target)
{
   _EGL_DECLARE_DD_AND_SURFACE(dpy, surface);
   return drv->API.CopyBuffers(drv, disp, surf, target);
}


EGLBoolean EGLAPIENTRY
eglWaitGL(void)
{
   _EGLDisplay *disp = _eglGetCurrentDisplay();
   _EGLDriver *drv;

   if (!disp)
      return EGL_TRUE;

   /* a current display is always initialized */
   drv = disp->Driver;

   return drv->API.WaitGL(drv, disp);
}


EGLBoolean EGLAPIENTRY
eglWaitNative(EGLint engine)
{
   _EGLDisplay *disp = _eglGetCurrentDisplay();
   _EGLDriver *drv;

   if (!disp)
      return EGL_TRUE;

   /* a current display is always initialized */
   drv = disp->Driver;

   return drv->API.WaitNative(drv, disp, engine);
}


EGLDisplay EGLAPIENTRY
eglGetCurrentDisplay(void)
{
   _EGLDisplay *dpy = _eglGetCurrentDisplay();
   return _eglGetDisplayHandle(dpy);
}


EGLContext EGLAPIENTRY
eglGetCurrentContext(void)
{
   _EGLContext *ctx = _eglGetCurrentContext();
   return _eglGetContextHandle(ctx);
}


EGLSurface EGLAPIENTRY
eglGetCurrentSurface(EGLint readdraw)
{
   _EGLSurface *s = _eglGetCurrentSurface(readdraw);
   return _eglGetSurfaceHandle(s);
}


EGLint EGLAPIENTRY
eglGetError(void)
{
   _EGLThreadInfo *t = _eglGetCurrentThread();
   EGLint e = t->LastError;
   if (!_eglIsCurrentThreadDummy())
      t->LastError = EGL_SUCCESS;
   return e;
}


void (* EGLAPIENTRY eglGetProcAddress(const char *procname))()
{
   typedef void (*genericFunc)();
   struct name_function {
      const char *name;
      _EGLProc function;
   };
   static struct name_function egl_functions[] = {
      /* alphabetical order */
      { "eglBindTexImage", (_EGLProc) eglBindTexImage },
      { "eglChooseConfig", (_EGLProc) eglChooseConfig },
      { "eglCopyBuffers", (_EGLProc) eglCopyBuffers },
      { "eglCreateContext", (_EGLProc) eglCreateContext },
      { "eglCreatePbufferSurface", (_EGLProc) eglCreatePbufferSurface },
      { "eglCreatePixmapSurface", (_EGLProc) eglCreatePixmapSurface },
      { "eglCreateWindowSurface", (_EGLProc) eglCreateWindowSurface },
      { "eglDestroyContext", (_EGLProc) eglDestroyContext },
      { "eglDestroySurface", (_EGLProc) eglDestroySurface },
      { "eglGetConfigAttrib", (_EGLProc) eglGetConfigAttrib },
      { "eglGetConfigs", (_EGLProc) eglGetConfigs },
      { "eglGetCurrentContext", (_EGLProc) eglGetCurrentContext },
      { "eglGetCurrentDisplay", (_EGLProc) eglGetCurrentDisplay },
      { "eglGetCurrentSurface", (_EGLProc) eglGetCurrentSurface },
      { "eglGetDisplay", (_EGLProc) eglGetDisplay },
      { "eglGetError", (_EGLProc) eglGetError },
      { "eglGetProcAddress", (_EGLProc) eglGetProcAddress },
      { "eglInitialize", (_EGLProc) eglInitialize },
      { "eglMakeCurrent", (_EGLProc) eglMakeCurrent },
      { "eglQueryContext", (_EGLProc) eglQueryContext },
      { "eglQueryString", (_EGLProc) eglQueryString },
      { "eglQuerySurface", (_EGLProc) eglQuerySurface },
      { "eglReleaseTexImage", (_EGLProc) eglReleaseTexImage },
      { "eglSurfaceAttrib", (_EGLProc) eglSurfaceAttrib },
      { "eglSwapBuffers", (_EGLProc) eglSwapBuffers },
      { "eglSwapInterval", (_EGLProc) eglSwapInterval },
      { "eglTerminate", (_EGLProc) eglTerminate },
      { "eglWaitGL", (_EGLProc) eglWaitGL },
      { "eglWaitNative", (_EGLProc) eglWaitNative },
      /* Extensions */
#ifdef EGL_MESA_screen_surface
      { "eglChooseModeMESA", (_EGLProc) eglChooseModeMESA },
      { "eglGetModesMESA", (_EGLProc) eglGetModesMESA },
      { "eglGetModeAttribMESA", (_EGLProc) eglGetModeAttribMESA },
      { "eglCopyContextMESA", (_EGLProc) eglCopyContextMESA },
      { "eglGetScreensMESA", (_EGLProc) eglGetScreensMESA },
      { "eglCreateScreenSurfaceMESA", (_EGLProc) eglCreateScreenSurfaceMESA },
      { "eglShowScreenSurfaceMESA", (_EGLProc) eglShowScreenSurfaceMESA },
      { "eglScreenPositionMESA", (_EGLProc) eglScreenPositionMESA },
      { "eglQueryScreenMESA", (_EGLProc) eglQueryScreenMESA },
      { "eglQueryScreenSurfaceMESA", (_EGLProc) eglQueryScreenSurfaceMESA },
      { "eglQueryScreenModeMESA", (_EGLProc) eglQueryScreenModeMESA },
      { "eglQueryModeStringMESA", (_EGLProc) eglQueryModeStringMESA },
#endif /* EGL_MESA_screen_surface */
#ifdef EGL_VERSION_1_2
      { "eglBindAPI", (_EGLProc) eglBindAPI },
      { "eglCreatePbufferFromClientBuffer", (_EGLProc) eglCreatePbufferFromClientBuffer },
      { "eglQueryAPI", (_EGLProc) eglQueryAPI },
      { "eglReleaseThread", (_EGLProc) eglReleaseThread },
      { "eglWaitClient", (_EGLProc) eglWaitClient },
#endif /* EGL_VERSION_1_2 */
      { NULL, NULL }
   };
   EGLint i;
   for (i = 0; egl_functions[i].name; i++) {
      if (strcmp(egl_functions[i].name, procname) == 0) {
         return (genericFunc) egl_functions[i].function;
      }
   }

   /* now loop over drivers to query their procs */
   for (i = 0; i < _eglGlobal.NumDrivers; i++) {
      _EGLProc p = _eglGlobal.Drivers[i]->API.GetProcAddress(procname);
      if (p)
         return p;
   }

   return NULL;
}


/*
 * EGL_MESA_screen extension
 */

EGLBoolean EGLAPIENTRY
eglChooseModeMESA(EGLDisplay dpy, EGLScreenMESA screen,
                  const EGLint *attrib_list, EGLModeMESA *modes,
                  EGLint modes_size, EGLint *num_modes)
{
   _EGL_DECLARE_DD_AND_SCREEN(dpy, screen);
   return drv->API.ChooseModeMESA(drv, disp, scrn, attrib_list,
                                  modes, modes_size, num_modes);
}


EGLBoolean EGLAPIENTRY
eglGetModesMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *modes,
                EGLint mode_size, EGLint *num_mode)
{
   _EGL_DECLARE_DD_AND_SCREEN(dpy, screen);
   return drv->API.GetModesMESA(drv, disp, scrn, modes, mode_size, num_mode);
}


EGLBoolean EGLAPIENTRY
eglGetModeAttribMESA(EGLDisplay dpy, EGLModeMESA mode,
                     EGLint attribute, EGLint *value)
{
   _EGL_DECLARE_DD_AND_MODE(dpy, mode);
   return drv->API.GetModeAttribMESA(drv, disp, m, attribute, value);
}


EGLBoolean EGLAPIENTRY
eglCopyContextMESA(EGLDisplay dpy, EGLContext source, EGLContext dest,
                   EGLint mask)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLContext *source_context = _eglLookupContext(source, disp);
   _EGLContext *dest_context = _eglLookupContext(dest, disp);
   _EGLDriver *drv;

   drv = _eglCheckContext(disp, source_context, __FUNCTION__);
   if (!drv || !dest_context) {
      if (drv)
         _eglError(EGL_BAD_CONTEXT, __FUNCTION__);
      return EGL_FALSE;
   }

   return drv->API.CopyContextMESA(drv, disp, source_context, dest_context,
                                   mask);
}


EGLBoolean
eglGetScreensMESA(EGLDisplay dpy, EGLScreenMESA *screens,
                  EGLint max_screens, EGLint *num_screens)
{
   _EGL_DECLARE_DD(dpy);
   return drv->API.GetScreensMESA(drv, disp, screens,
                                  max_screens, num_screens);
}


EGLSurface
eglCreateScreenSurfaceMESA(EGLDisplay dpy, EGLConfig config,
                           const EGLint *attrib_list)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLConfig *conf = _eglLookupConfig(config, disp);
   _EGLDriver *drv;
   _EGLSurface *surf;

   drv = _eglCheckConfig(disp, conf, __FUNCTION__);
   if (!drv)
      return EGL_NO_SURFACE;

   surf = drv->API.CreateScreenSurfaceMESA(drv, disp, conf, attrib_list);
   if (surf)
      return _eglLinkSurface(surf, disp);
   else
      return EGL_NO_SURFACE;
}


EGLBoolean
eglShowScreenSurfaceMESA(EGLDisplay dpy, EGLint screen,
                         EGLSurface surface, EGLModeMESA mode)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLScreen *scrn = _eglLookupScreen((EGLScreenMESA) screen, disp);
   _EGLSurface *surf = _eglLookupSurface(surface, disp);
   _EGLMode *m = _eglLookupMode(mode, disp);
   _EGLDriver *drv;

   drv = _eglCheckScreen(disp, scrn, __FUNCTION__);
   if (!drv)
      return EGL_FALSE;
   if (!surf && surface != EGL_NO_SURFACE)
      return _eglError(EGL_BAD_SURFACE, __FUNCTION__);
   if (!m && mode != EGL_NO_MODE_MESA)
      return _eglError(EGL_BAD_MODE_MESA, __FUNCTION__);

   return drv->API.ShowScreenSurfaceMESA(drv, disp, scrn, surf, m);
}


EGLBoolean
eglScreenPositionMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLint x, EGLint y)
{
   _EGL_DECLARE_DD_AND_SCREEN(dpy, screen);
   return drv->API.ScreenPositionMESA(drv, disp, scrn, x, y);
}


EGLBoolean
eglQueryScreenMESA(EGLDisplay dpy, EGLScreenMESA screen,
                   EGLint attribute, EGLint *value)
{
   _EGL_DECLARE_DD_AND_SCREEN(dpy, screen);
   return drv->API.QueryScreenMESA(drv, disp, scrn, attribute, value);
}


EGLBoolean
eglQueryScreenSurfaceMESA(EGLDisplay dpy, EGLScreenMESA screen,
                          EGLSurface *surface)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLScreen *scrn = _eglLookupScreen((EGLScreenMESA) screen, disp);
   _EGLDriver *drv;
   _EGLSurface *surf;

   drv = _eglCheckScreen(disp, scrn, __FUNCTION__);
   if (!drv)
      return EGL_FALSE;

   if (drv->API.QueryScreenSurfaceMESA(drv, disp, scrn, &surf) != EGL_TRUE)
      surf = NULL;
   if (surface)
      *surface = _eglGetSurfaceHandle(surf);
   return (surf != NULL);
}


EGLBoolean
eglQueryScreenModeMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *mode)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLScreen *scrn = _eglLookupScreen((EGLScreenMESA) screen, disp);
   _EGLDriver *drv;
   _EGLMode *m;

   drv = _eglCheckScreen(disp, scrn, __FUNCTION__);
   if (!drv)
      return EGL_FALSE;

   if (drv->API.QueryScreenModeMESA(drv, disp, scrn, &m) != EGL_TRUE)
      m = NULL;
   if (mode)
      *mode = m->Handle;

   return (m != NULL);
}


const char *
eglQueryModeStringMESA(EGLDisplay dpy, EGLModeMESA mode)
{
   _EGL_DECLARE_DD_AND_MODE(dpy, mode);
   return drv->API.QueryModeStringMESA(drv, disp, m);
}


/**
 ** EGL 1.2
 **/

#ifdef EGL_VERSION_1_2


/**
 * Specify the client API to use for subsequent calls including:
 *  eglCreateContext()
 *  eglGetCurrentContext()
 *  eglGetCurrentDisplay()
 *  eglGetCurrentSurface()
 *  eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
 *  eglWaitClient()
 *  eglWaitNative()
 * See section 3.7 "Rendering Context" in the EGL specification for details.
 */
EGLBoolean
eglBindAPI(EGLenum api)
{
   _EGLThreadInfo *t = _eglGetCurrentThread();

   if (_eglIsCurrentThreadDummy())
      return _eglError(EGL_BAD_ALLOC, "eglBindAPI");

   if (!_eglIsApiValid(api))
      return _eglError(EGL_BAD_PARAMETER, "eglBindAPI");

   switch (api) {
#ifdef EGL_VERSION_1_4
   case EGL_OPENGL_API:
      if (_eglGlobal.ClientAPIsMask & EGL_OPENGL_BIT) {
         t->CurrentAPIIndex = _eglConvertApiToIndex(api);
         return EGL_TRUE;
      }
      _eglError(EGL_BAD_PARAMETER, "eglBindAPI");
      return EGL_FALSE;
#endif
   case EGL_OPENGL_ES_API:
      if (_eglGlobal.ClientAPIsMask & (EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT)) {
         t->CurrentAPIIndex = _eglConvertApiToIndex(api);
         return EGL_TRUE;
      }
      _eglError(EGL_BAD_PARAMETER, "eglBindAPI");
      return EGL_FALSE;
   case EGL_OPENVG_API:
      if (_eglGlobal.ClientAPIsMask & EGL_OPENVG_BIT) {
         t->CurrentAPIIndex = _eglConvertApiToIndex(api);
         return EGL_TRUE;
      }
      _eglError(EGL_BAD_PARAMETER, "eglBindAPI");
      return EGL_FALSE;
   default:
      return EGL_FALSE;
   }
   return EGL_TRUE;
}


/**
 * Return the last value set with eglBindAPI().
 */
EGLenum
eglQueryAPI(void)
{
   /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
   _EGLThreadInfo *t = _eglGetCurrentThread();
   return _eglConvertApiFromIndex(t->CurrentAPIIndex);
}


EGLSurface
eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
                                 EGLClientBuffer buffer, EGLConfig config,
                                 const EGLint *attrib_list)
{
   _EGLDisplay *disp = _eglLookupDisplay(dpy);
   _EGLConfig *conf = _eglLookupConfig(config, disp);
   _EGLDriver *drv;
   _EGLSurface *surf;

   drv = _eglCheckConfig(disp, conf, __FUNCTION__);
   if (!drv)
      return EGL_NO_SURFACE;

   surf = drv->API.CreatePbufferFromClientBuffer(drv, disp, buftype, buffer,
                                                 conf, attrib_list);
   if (surf)
      return _eglLinkSurface(surf, disp);
   else
      return EGL_NO_SURFACE;
}


EGLBoolean
eglReleaseThread(void)
{
   /* unbind current context */
   if (!_eglIsCurrentThreadDummy()) {
      _EGLDisplay *disp = _eglGetCurrentDisplay();
      _EGLDriver *drv;
      if (disp) {
         drv = disp->Driver;
         (void) drv->API.MakeCurrent(drv, disp, NULL, NULL, NULL);
      }
   }

   _eglDestroyCurrentThread();
   return EGL_TRUE;
}


EGLBoolean
eglWaitClient(void)
{
   _EGLDisplay *disp = _eglGetCurrentDisplay();
   _EGLDriver *drv;

   if (!disp)
      return EGL_TRUE;

   /* a current display is always initialized */
   drv = disp->Driver;

   return drv->API.WaitClient(drv, disp);
}


#endif /* EGL_VERSION_1_2 */