DSCF

.co.uk

PC32

A ColorForth compiler and editor for x86-64
git clone https://dscf.co.uk/clone/PC32.git
Log | Files | Refs | README

egl_context.c (25469B)


      1 //========================================================================
      2 // GLFW 3.3 EGL - www.glfw.org
      3 //------------------------------------------------------------------------
      4 // Copyright (c) 2002-2006 Marcus Geelnard
      5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
      6 //
      7 // This software is provided 'as-is', without any express or implied
      8 // warranty. In no event will the authors be held liable for any damages
      9 // arising from the use of this software.
     10 //
     11 // Permission is granted to anyone to use this software for any purpose,
     12 // including commercial applications, and to alter it and redistribute it
     13 // freely, subject to the following restrictions:
     14 //
     15 // 1. The origin of this software must not be misrepresented; you must not
     16 //    claim that you wrote the original software. If you use this software
     17 //    in a product, an acknowledgment in the product documentation would
     18 //    be appreciated but is not required.
     19 //
     20 // 2. Altered source versions must be plainly marked as such, and must not
     21 //    be misrepresented as being the original software.
     22 //
     23 // 3. This notice may not be removed or altered from any source
     24 //    distribution.
     25 //
     26 //========================================================================
     27 // Please use C89 style variable declarations in this file because VS 2010
     28 //========================================================================
     29 
     30 #include "internal.h"
     31 
     32 #include <stdio.h>
     33 #include <string.h>
     34 #include <stdlib.h>
     35 #include <assert.h>
     36 
     37 
     38 // Return a description of the specified EGL error
     39 //
     40 static const char* getEGLErrorString(EGLint error)
     41 {
     42     switch (error)
     43     {
     44         case EGL_SUCCESS:
     45             return "Success";
     46         case EGL_NOT_INITIALIZED:
     47             return "EGL is not or could not be initialized";
     48         case EGL_BAD_ACCESS:
     49             return "EGL cannot access a requested resource";
     50         case EGL_BAD_ALLOC:
     51             return "EGL failed to allocate resources for the requested operation";
     52         case EGL_BAD_ATTRIBUTE:
     53             return "An unrecognized attribute or attribute value was passed in the attribute list";
     54         case EGL_BAD_CONTEXT:
     55             return "An EGLContext argument does not name a valid EGL rendering context";
     56         case EGL_BAD_CONFIG:
     57             return "An EGLConfig argument does not name a valid EGL frame buffer configuration";
     58         case EGL_BAD_CURRENT_SURFACE:
     59             return "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid";
     60         case EGL_BAD_DISPLAY:
     61             return "An EGLDisplay argument does not name a valid EGL display connection";
     62         case EGL_BAD_SURFACE:
     63             return "An EGLSurface argument does not name a valid surface configured for GL rendering";
     64         case EGL_BAD_MATCH:
     65             return "Arguments are inconsistent";
     66         case EGL_BAD_PARAMETER:
     67             return "One or more argument values are invalid";
     68         case EGL_BAD_NATIVE_PIXMAP:
     69             return "A NativePixmapType argument does not refer to a valid native pixmap";
     70         case EGL_BAD_NATIVE_WINDOW:
     71             return "A NativeWindowType argument does not refer to a valid native window";
     72         case EGL_CONTEXT_LOST:
     73             return "The application must destroy all contexts and reinitialise";
     74         default:
     75             return "ERROR: UNKNOWN EGL ERROR";
     76     }
     77 }
     78 
     79 // Returns the specified attribute of the specified EGLConfig
     80 //
     81 static int getEGLConfigAttrib(EGLConfig config, int attrib)
     82 {
     83     int value;
     84     eglGetConfigAttrib(_glfw.egl.display, config, attrib, &value);
     85     return value;
     86 }
     87 
     88 // Return the EGLConfig most closely matching the specified hints
     89 //
     90 static GLFWbool chooseEGLConfig(const _GLFWctxconfig* ctxconfig,
     91                                 const _GLFWfbconfig* desired,
     92                                 EGLConfig* result)
     93 {
     94     EGLConfig* nativeConfigs;
     95     _GLFWfbconfig* usableConfigs;
     96     const _GLFWfbconfig* closest;
     97     int i, nativeCount, usableCount;
     98 
     99     eglGetConfigs(_glfw.egl.display, NULL, 0, &nativeCount);
    100     if (!nativeCount)
    101     {
    102         _glfwInputError(GLFW_API_UNAVAILABLE, "EGL: No EGLConfigs returned");
    103         return GLFW_FALSE;
    104     }
    105 
    106     nativeConfigs = calloc(nativeCount, sizeof(EGLConfig));
    107     eglGetConfigs(_glfw.egl.display, nativeConfigs, nativeCount, &nativeCount);
    108 
    109     usableConfigs = calloc(nativeCount, sizeof(_GLFWfbconfig));
    110     usableCount = 0;
    111 
    112     for (i = 0;  i < nativeCount;  i++)
    113     {
    114         const EGLConfig n = nativeConfigs[i];
    115         _GLFWfbconfig* u = usableConfigs + usableCount;
    116 
    117         // Only consider RGB(A) EGLConfigs
    118         if (getEGLConfigAttrib(n, EGL_COLOR_BUFFER_TYPE) != EGL_RGB_BUFFER)
    119             continue;
    120 
    121         // Only consider window EGLConfigs
    122         if (!(getEGLConfigAttrib(n, EGL_SURFACE_TYPE) & EGL_WINDOW_BIT))
    123             continue;
    124 
    125 #if defined(_GLFW_X11)
    126         {
    127             XVisualInfo vi = {0};
    128 
    129             // Only consider EGLConfigs with associated Visuals
    130             vi.visualid = getEGLConfigAttrib(n, EGL_NATIVE_VISUAL_ID);
    131             if (!vi.visualid)
    132                 continue;
    133 
    134             if (desired->transparent)
    135             {
    136                 int count;
    137                 XVisualInfo* vis =
    138                     XGetVisualInfo(_glfw.x11.display, VisualIDMask, &vi, &count);
    139                 if (vis)
    140                 {
    141                     u->transparent = _glfwIsVisualTransparentX11(vis[0].visual);
    142                     XFree(vis);
    143                 }
    144             }
    145         }
    146 #endif // _GLFW_X11
    147 
    148         if (ctxconfig->client == GLFW_OPENGL_ES_API)
    149         {
    150             if (ctxconfig->major == 1)
    151             {
    152                 if (!(getEGLConfigAttrib(n, EGL_RENDERABLE_TYPE) & EGL_OPENGL_ES_BIT))
    153                     continue;
    154             }
    155             else
    156             {
    157                 if (!(getEGLConfigAttrib(n, EGL_RENDERABLE_TYPE) & EGL_OPENGL_ES2_BIT))
    158                     continue;
    159             }
    160         }
    161         else if (ctxconfig->client == GLFW_OPENGL_API)
    162         {
    163             if (!(getEGLConfigAttrib(n, EGL_RENDERABLE_TYPE) & EGL_OPENGL_BIT))
    164                 continue;
    165         }
    166 
    167         u->redBits = getEGLConfigAttrib(n, EGL_RED_SIZE);
    168         u->greenBits = getEGLConfigAttrib(n, EGL_GREEN_SIZE);
    169         u->blueBits = getEGLConfigAttrib(n, EGL_BLUE_SIZE);
    170 
    171         u->alphaBits = getEGLConfigAttrib(n, EGL_ALPHA_SIZE);
    172         u->depthBits = getEGLConfigAttrib(n, EGL_DEPTH_SIZE);
    173         u->stencilBits = getEGLConfigAttrib(n, EGL_STENCIL_SIZE);
    174 
    175         u->samples = getEGLConfigAttrib(n, EGL_SAMPLES);
    176         u->doublebuffer = desired->doublebuffer;
    177 
    178         u->handle = (uintptr_t) n;
    179         usableCount++;
    180     }
    181 
    182     closest = _glfwChooseFBConfig(desired, usableConfigs, usableCount);
    183     if (closest)
    184         *result = (EGLConfig) closest->handle;
    185 
    186     free(nativeConfigs);
    187     free(usableConfigs);
    188 
    189     return closest != NULL;
    190 }
    191 
    192 static void makeContextCurrentEGL(_GLFWwindow* window)
    193 {
    194     if (window)
    195     {
    196         if (!eglMakeCurrent(_glfw.egl.display,
    197                             window->context.egl.surface,
    198                             window->context.egl.surface,
    199                             window->context.egl.handle))
    200         {
    201             _glfwInputError(GLFW_PLATFORM_ERROR,
    202                             "EGL: Failed to make context current: %s",
    203                             getEGLErrorString(eglGetError()));
    204             return;
    205         }
    206     }
    207     else
    208     {
    209         if (!eglMakeCurrent(_glfw.egl.display,
    210                             EGL_NO_SURFACE,
    211                             EGL_NO_SURFACE,
    212                             EGL_NO_CONTEXT))
    213         {
    214             _glfwInputError(GLFW_PLATFORM_ERROR,
    215                             "EGL: Failed to clear current context: %s",
    216                             getEGLErrorString(eglGetError()));
    217             return;
    218         }
    219     }
    220 
    221     _glfwPlatformSetTls(&_glfw.contextSlot, window);
    222 }
    223 
    224 static void swapBuffersEGL(_GLFWwindow* window)
    225 {
    226     if (window != _glfwPlatformGetTls(&_glfw.contextSlot))
    227     {
    228         _glfwInputError(GLFW_PLATFORM_ERROR,
    229                         "EGL: The context must be current on the calling thread when swapping buffers");
    230         return;
    231     }
    232 
    233 #if defined(_GLFW_WAYLAND)
    234     // NOTE: Swapping buffers on a hidden window on Wayland makes it visible
    235     if (!window->wl.visible)
    236         return;
    237 #endif
    238 
    239     eglSwapBuffers(_glfw.egl.display, window->context.egl.surface);
    240 }
    241 
    242 static void swapIntervalEGL(int interval)
    243 {
    244     eglSwapInterval(_glfw.egl.display, interval);
    245 }
    246 
    247 static int extensionSupportedEGL(const char* extension)
    248 {
    249     const char* extensions = eglQueryString(_glfw.egl.display, EGL_EXTENSIONS);
    250     if (extensions)
    251     {
    252         if (_glfwStringInExtensionString(extension, extensions))
    253             return GLFW_TRUE;
    254     }
    255 
    256     return GLFW_FALSE;
    257 }
    258 
    259 static GLFWglproc getProcAddressEGL(const char* procname)
    260 {
    261     _GLFWwindow* window = _glfwPlatformGetTls(&_glfw.contextSlot);
    262 
    263     if (window->context.egl.client)
    264     {
    265         GLFWglproc proc = (GLFWglproc) _glfw_dlsym(window->context.egl.client,
    266                                                    procname);
    267         if (proc)
    268             return proc;
    269     }
    270 
    271     return eglGetProcAddress(procname);
    272 }
    273 
    274 static void destroyContextEGL(_GLFWwindow* window)
    275 {
    276 #if defined(_GLFW_X11)
    277     // NOTE: Do not unload libGL.so.1 while the X11 display is still open,
    278     //       as it will make XCloseDisplay segfault
    279     if (window->context.client != GLFW_OPENGL_API)
    280 #endif // _GLFW_X11
    281     {
    282         if (window->context.egl.client)
    283         {
    284             _glfw_dlclose(window->context.egl.client);
    285             window->context.egl.client = NULL;
    286         }
    287     }
    288 
    289     if (window->context.egl.surface)
    290     {
    291         eglDestroySurface(_glfw.egl.display, window->context.egl.surface);
    292         window->context.egl.surface = EGL_NO_SURFACE;
    293     }
    294 
    295     if (window->context.egl.handle)
    296     {
    297         eglDestroyContext(_glfw.egl.display, window->context.egl.handle);
    298         window->context.egl.handle = EGL_NO_CONTEXT;
    299     }
    300 }
    301 
    302 
    303 //////////////////////////////////////////////////////////////////////////
    304 //////                       GLFW internal API                      //////
    305 //////////////////////////////////////////////////////////////////////////
    306 
    307 // Initialize EGL
    308 //
    309 GLFWbool _glfwInitEGL(void)
    310 {
    311     int i;
    312     const char* sonames[] =
    313     {
    314 #if defined(_GLFW_EGL_LIBRARY)
    315         _GLFW_EGL_LIBRARY,
    316 #elif defined(_GLFW_WIN32)
    317         "libEGL.dll",
    318         "EGL.dll",
    319 #elif defined(_GLFW_COCOA)
    320         "libEGL.dylib",
    321 #elif defined(__CYGWIN__)
    322         "libEGL-1.so",
    323 #elif defined(__OpenBSD__) || defined(__NetBSD__)
    324         "libEGL.so",
    325 #else
    326         "libEGL.so.1",
    327 #endif
    328         NULL
    329     };
    330 
    331     if (_glfw.egl.handle)
    332         return GLFW_TRUE;
    333 
    334     for (i = 0;  sonames[i];  i++)
    335     {
    336         _glfw.egl.handle = _glfw_dlopen(sonames[i]);
    337         if (_glfw.egl.handle)
    338             break;
    339     }
    340 
    341     if (!_glfw.egl.handle)
    342     {
    343         _glfwInputError(GLFW_API_UNAVAILABLE, "EGL: Library not found");
    344         return GLFW_FALSE;
    345     }
    346 
    347     _glfw.egl.prefix = (strncmp(sonames[i], "lib", 3) == 0);
    348 
    349     _glfw.egl.GetConfigAttrib = (PFN_eglGetConfigAttrib)
    350         _glfw_dlsym(_glfw.egl.handle, "eglGetConfigAttrib");
    351     _glfw.egl.GetConfigs = (PFN_eglGetConfigs)
    352         _glfw_dlsym(_glfw.egl.handle, "eglGetConfigs");
    353     _glfw.egl.GetDisplay = (PFN_eglGetDisplay)
    354         _glfw_dlsym(_glfw.egl.handle, "eglGetDisplay");
    355     _glfw.egl.GetError = (PFN_eglGetError)
    356         _glfw_dlsym(_glfw.egl.handle, "eglGetError");
    357     _glfw.egl.Initialize = (PFN_eglInitialize)
    358         _glfw_dlsym(_glfw.egl.handle, "eglInitialize");
    359     _glfw.egl.Terminate = (PFN_eglTerminate)
    360         _glfw_dlsym(_glfw.egl.handle, "eglTerminate");
    361     _glfw.egl.BindAPI = (PFN_eglBindAPI)
    362         _glfw_dlsym(_glfw.egl.handle, "eglBindAPI");
    363     _glfw.egl.CreateContext = (PFN_eglCreateContext)
    364         _glfw_dlsym(_glfw.egl.handle, "eglCreateContext");
    365     _glfw.egl.DestroySurface = (PFN_eglDestroySurface)
    366         _glfw_dlsym(_glfw.egl.handle, "eglDestroySurface");
    367     _glfw.egl.DestroyContext = (PFN_eglDestroyContext)
    368         _glfw_dlsym(_glfw.egl.handle, "eglDestroyContext");
    369     _glfw.egl.CreateWindowSurface = (PFN_eglCreateWindowSurface)
    370         _glfw_dlsym(_glfw.egl.handle, "eglCreateWindowSurface");
    371     _glfw.egl.MakeCurrent = (PFN_eglMakeCurrent)
    372         _glfw_dlsym(_glfw.egl.handle, "eglMakeCurrent");
    373     _glfw.egl.SwapBuffers = (PFN_eglSwapBuffers)
    374         _glfw_dlsym(_glfw.egl.handle, "eglSwapBuffers");
    375     _glfw.egl.SwapInterval = (PFN_eglSwapInterval)
    376         _glfw_dlsym(_glfw.egl.handle, "eglSwapInterval");
    377     _glfw.egl.QueryString = (PFN_eglQueryString)
    378         _glfw_dlsym(_glfw.egl.handle, "eglQueryString");
    379     _glfw.egl.GetProcAddress = (PFN_eglGetProcAddress)
    380         _glfw_dlsym(_glfw.egl.handle, "eglGetProcAddress");
    381 
    382     if (!_glfw.egl.GetConfigAttrib ||
    383         !_glfw.egl.GetConfigs ||
    384         !_glfw.egl.GetDisplay ||
    385         !_glfw.egl.GetError ||
    386         !_glfw.egl.Initialize ||
    387         !_glfw.egl.Terminate ||
    388         !_glfw.egl.BindAPI ||
    389         !_glfw.egl.CreateContext ||
    390         !_glfw.egl.DestroySurface ||
    391         !_glfw.egl.DestroyContext ||
    392         !_glfw.egl.CreateWindowSurface ||
    393         !_glfw.egl.MakeCurrent ||
    394         !_glfw.egl.SwapBuffers ||
    395         !_glfw.egl.SwapInterval ||
    396         !_glfw.egl.QueryString ||
    397         !_glfw.egl.GetProcAddress)
    398     {
    399         _glfwInputError(GLFW_PLATFORM_ERROR,
    400                         "EGL: Failed to load required entry points");
    401 
    402         _glfwTerminateEGL();
    403         return GLFW_FALSE;
    404     }
    405 
    406     _glfw.egl.display = eglGetDisplay(_GLFW_EGL_NATIVE_DISPLAY);
    407     if (_glfw.egl.display == EGL_NO_DISPLAY)
    408     {
    409         _glfwInputError(GLFW_API_UNAVAILABLE,
    410                         "EGL: Failed to get EGL display: %s",
    411                         getEGLErrorString(eglGetError()));
    412 
    413         _glfwTerminateEGL();
    414         return GLFW_FALSE;
    415     }
    416 
    417     if (!eglInitialize(_glfw.egl.display, &_glfw.egl.major, &_glfw.egl.minor))
    418     {
    419         _glfwInputError(GLFW_API_UNAVAILABLE,
    420                         "EGL: Failed to initialize EGL: %s",
    421                         getEGLErrorString(eglGetError()));
    422 
    423         _glfwTerminateEGL();
    424         return GLFW_FALSE;
    425     }
    426 
    427     _glfw.egl.KHR_create_context =
    428         extensionSupportedEGL("EGL_KHR_create_context");
    429     _glfw.egl.KHR_create_context_no_error =
    430         extensionSupportedEGL("EGL_KHR_create_context_no_error");
    431     _glfw.egl.KHR_gl_colorspace =
    432         extensionSupportedEGL("EGL_KHR_gl_colorspace");
    433     _glfw.egl.KHR_get_all_proc_addresses =
    434         extensionSupportedEGL("EGL_KHR_get_all_proc_addresses");
    435     _glfw.egl.KHR_context_flush_control =
    436         extensionSupportedEGL("EGL_KHR_context_flush_control");
    437     _glfw.egl.EXT_present_opaque =
    438         extensionSupportedEGL("EGL_EXT_present_opaque");
    439 
    440     return GLFW_TRUE;
    441 }
    442 
    443 // Terminate EGL
    444 //
    445 void _glfwTerminateEGL(void)
    446 {
    447     if (_glfw.egl.display)
    448     {
    449         eglTerminate(_glfw.egl.display);
    450         _glfw.egl.display = EGL_NO_DISPLAY;
    451     }
    452 
    453     if (_glfw.egl.handle)
    454     {
    455         _glfw_dlclose(_glfw.egl.handle);
    456         _glfw.egl.handle = NULL;
    457     }
    458 }
    459 
    460 #define setAttrib(a, v) \
    461 { \
    462     assert(((size_t) index + 1) < sizeof(attribs) / sizeof(attribs[0])); \
    463     attribs[index++] = a; \
    464     attribs[index++] = v; \
    465 }
    466 
    467 // Create the OpenGL or OpenGL ES context
    468 //
    469 GLFWbool _glfwCreateContextEGL(_GLFWwindow* window,
    470                                const _GLFWctxconfig* ctxconfig,
    471                                const _GLFWfbconfig* fbconfig)
    472 {
    473     EGLint attribs[40];
    474     EGLConfig config;
    475     EGLContext share = NULL;
    476     int index = 0;
    477 
    478     if (!_glfw.egl.display)
    479     {
    480         _glfwInputError(GLFW_API_UNAVAILABLE, "EGL: API not available");
    481         return GLFW_FALSE;
    482     }
    483 
    484     if (ctxconfig->share)
    485         share = ctxconfig->share->context.egl.handle;
    486 
    487     if (!chooseEGLConfig(ctxconfig, fbconfig, &config))
    488     {
    489         _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
    490                         "EGL: Failed to find a suitable EGLConfig");
    491         return GLFW_FALSE;
    492     }
    493 
    494     if (ctxconfig->client == GLFW_OPENGL_ES_API)
    495     {
    496         if (!eglBindAPI(EGL_OPENGL_ES_API))
    497         {
    498             _glfwInputError(GLFW_API_UNAVAILABLE,
    499                             "EGL: Failed to bind OpenGL ES: %s",
    500                             getEGLErrorString(eglGetError()));
    501             return GLFW_FALSE;
    502         }
    503     }
    504     else
    505     {
    506         if (!eglBindAPI(EGL_OPENGL_API))
    507         {
    508             _glfwInputError(GLFW_API_UNAVAILABLE,
    509                             "EGL: Failed to bind OpenGL: %s",
    510                             getEGLErrorString(eglGetError()));
    511             return GLFW_FALSE;
    512         }
    513     }
    514 
    515     if (_glfw.egl.KHR_create_context)
    516     {
    517         int mask = 0, flags = 0;
    518 
    519         if (ctxconfig->client == GLFW_OPENGL_API)
    520         {
    521             if (ctxconfig->forward)
    522                 flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR;
    523 
    524             if (ctxconfig->profile == GLFW_OPENGL_CORE_PROFILE)
    525                 mask |= EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
    526             else if (ctxconfig->profile == GLFW_OPENGL_COMPAT_PROFILE)
    527                 mask |= EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR;
    528         }
    529 
    530         if (ctxconfig->debug)
    531             flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR;
    532 
    533         if (ctxconfig->robustness)
    534         {
    535             if (ctxconfig->robustness == GLFW_NO_RESET_NOTIFICATION)
    536             {
    537                 setAttrib(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR,
    538                           EGL_NO_RESET_NOTIFICATION_KHR);
    539             }
    540             else if (ctxconfig->robustness == GLFW_LOSE_CONTEXT_ON_RESET)
    541             {
    542                 setAttrib(EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR,
    543                           EGL_LOSE_CONTEXT_ON_RESET_KHR);
    544             }
    545 
    546             flags |= EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR;
    547         }
    548 
    549         if (ctxconfig->noerror)
    550         {
    551             if (_glfw.egl.KHR_create_context_no_error)
    552                 setAttrib(EGL_CONTEXT_OPENGL_NO_ERROR_KHR, GLFW_TRUE);
    553         }
    554 
    555         if (ctxconfig->major != 1 || ctxconfig->minor != 0)
    556         {
    557             setAttrib(EGL_CONTEXT_MAJOR_VERSION_KHR, ctxconfig->major);
    558             setAttrib(EGL_CONTEXT_MINOR_VERSION_KHR, ctxconfig->minor);
    559         }
    560 
    561         if (mask)
    562             setAttrib(EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, mask);
    563 
    564         if (flags)
    565             setAttrib(EGL_CONTEXT_FLAGS_KHR, flags);
    566     }
    567     else
    568     {
    569         if (ctxconfig->client == GLFW_OPENGL_ES_API)
    570             setAttrib(EGL_CONTEXT_CLIENT_VERSION, ctxconfig->major);
    571     }
    572 
    573     if (_glfw.egl.KHR_context_flush_control)
    574     {
    575         if (ctxconfig->release == GLFW_RELEASE_BEHAVIOR_NONE)
    576         {
    577             setAttrib(EGL_CONTEXT_RELEASE_BEHAVIOR_KHR,
    578                       EGL_CONTEXT_RELEASE_BEHAVIOR_NONE_KHR);
    579         }
    580         else if (ctxconfig->release == GLFW_RELEASE_BEHAVIOR_FLUSH)
    581         {
    582             setAttrib(EGL_CONTEXT_RELEASE_BEHAVIOR_KHR,
    583                       EGL_CONTEXT_RELEASE_BEHAVIOR_FLUSH_KHR);
    584         }
    585     }
    586 
    587     setAttrib(EGL_NONE, EGL_NONE);
    588 
    589     window->context.egl.handle = eglCreateContext(_glfw.egl.display,
    590                                                   config, share, attribs);
    591 
    592     if (window->context.egl.handle == EGL_NO_CONTEXT)
    593     {
    594         _glfwInputError(GLFW_VERSION_UNAVAILABLE,
    595                         "EGL: Failed to create context: %s",
    596                         getEGLErrorString(eglGetError()));
    597         return GLFW_FALSE;
    598     }
    599 
    600     // Set up attributes for surface creation
    601     index = 0;
    602 
    603     if (fbconfig->sRGB)
    604     {
    605         if (_glfw.egl.KHR_gl_colorspace)
    606             setAttrib(EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_SRGB_KHR);
    607     }
    608 
    609     if (!fbconfig->doublebuffer)
    610         setAttrib(EGL_RENDER_BUFFER, EGL_SINGLE_BUFFER);
    611 
    612     if (_glfw.egl.EXT_present_opaque)
    613         setAttrib(EGL_PRESENT_OPAQUE_EXT, !fbconfig->transparent);
    614 
    615     setAttrib(EGL_NONE, EGL_NONE);
    616 
    617     window->context.egl.surface =
    618         eglCreateWindowSurface(_glfw.egl.display,
    619                                config,
    620                                _GLFW_EGL_NATIVE_WINDOW,
    621                                attribs);
    622     if (window->context.egl.surface == EGL_NO_SURFACE)
    623     {
    624         _glfwInputError(GLFW_PLATFORM_ERROR,
    625                         "EGL: Failed to create window surface: %s",
    626                         getEGLErrorString(eglGetError()));
    627         return GLFW_FALSE;
    628     }
    629 
    630     window->context.egl.config = config;
    631 
    632     // Load the appropriate client library
    633     if (!_glfw.egl.KHR_get_all_proc_addresses)
    634     {
    635         int i;
    636         const char** sonames;
    637         const char* es1sonames[] =
    638         {
    639 #if defined(_GLFW_GLESV1_LIBRARY)
    640             _GLFW_GLESV1_LIBRARY,
    641 #elif defined(_GLFW_WIN32)
    642             "GLESv1_CM.dll",
    643             "libGLES_CM.dll",
    644 #elif defined(_GLFW_COCOA)
    645             "libGLESv1_CM.dylib",
    646 #elif defined(__OpenBSD__) || defined(__NetBSD__)
    647             "libGLESv1_CM.so",
    648 #else
    649             "libGLESv1_CM.so.1",
    650             "libGLES_CM.so.1",
    651 #endif
    652             NULL
    653         };
    654         const char* es2sonames[] =
    655         {
    656 #if defined(_GLFW_GLESV2_LIBRARY)
    657             _GLFW_GLESV2_LIBRARY,
    658 #elif defined(_GLFW_WIN32)
    659             "GLESv2.dll",
    660             "libGLESv2.dll",
    661 #elif defined(_GLFW_COCOA)
    662             "libGLESv2.dylib",
    663 #elif defined(__CYGWIN__)
    664             "libGLESv2-2.so",
    665 #elif defined(__OpenBSD__) || defined(__NetBSD__)
    666             "libGLESv2.so",
    667 #else
    668             "libGLESv2.so.2",
    669 #endif
    670             NULL
    671         };
    672         const char* glsonames[] =
    673         {
    674 #if defined(_GLFW_OPENGL_LIBRARY)
    675             _GLFW_OPENGL_LIBRARY,
    676 #elif defined(_GLFW_WIN32)
    677 #elif defined(_GLFW_COCOA)
    678 #elif defined(__OpenBSD__) || defined(__NetBSD__)
    679             "libGL.so",
    680 #else
    681             "libGL.so.1",
    682 #endif
    683             NULL
    684         };
    685 
    686         if (ctxconfig->client == GLFW_OPENGL_ES_API)
    687         {
    688             if (ctxconfig->major == 1)
    689                 sonames = es1sonames;
    690             else
    691                 sonames = es2sonames;
    692         }
    693         else
    694             sonames = glsonames;
    695 
    696         for (i = 0;  sonames[i];  i++)
    697         {
    698             // HACK: Match presence of lib prefix to increase chance of finding
    699             //       a matching pair in the jungle that is Win32 EGL/GLES
    700             if (_glfw.egl.prefix != (strncmp(sonames[i], "lib", 3) == 0))
    701                 continue;
    702 
    703             window->context.egl.client = _glfw_dlopen(sonames[i]);
    704             if (window->context.egl.client)
    705                 break;
    706         }
    707 
    708         if (!window->context.egl.client)
    709         {
    710             _glfwInputError(GLFW_API_UNAVAILABLE,
    711                             "EGL: Failed to load client library");
    712             return GLFW_FALSE;
    713         }
    714     }
    715 
    716     window->context.makeCurrent = makeContextCurrentEGL;
    717     window->context.swapBuffers = swapBuffersEGL;
    718     window->context.swapInterval = swapIntervalEGL;
    719     window->context.extensionSupported = extensionSupportedEGL;
    720     window->context.getProcAddress = getProcAddressEGL;
    721     window->context.destroy = destroyContextEGL;
    722 
    723     return GLFW_TRUE;
    724 }
    725 
    726 #undef setAttrib
    727 
    728 // Returns the Visual and depth of the chosen EGLConfig
    729 //
    730 #if defined(_GLFW_X11)
    731 GLFWbool _glfwChooseVisualEGL(const _GLFWwndconfig* wndconfig,
    732                               const _GLFWctxconfig* ctxconfig,
    733                               const _GLFWfbconfig* fbconfig,
    734                               Visual** visual, int* depth)
    735 {
    736     XVisualInfo* result;
    737     XVisualInfo desired;
    738     EGLConfig native;
    739     EGLint visualID = 0, count = 0;
    740     const long vimask = VisualScreenMask | VisualIDMask;
    741 
    742     if (!chooseEGLConfig(ctxconfig, fbconfig, &native))
    743     {
    744         _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
    745                         "EGL: Failed to find a suitable EGLConfig");
    746         return GLFW_FALSE;
    747     }
    748 
    749     eglGetConfigAttrib(_glfw.egl.display, native,
    750                        EGL_NATIVE_VISUAL_ID, &visualID);
    751 
    752     desired.screen = _glfw.x11.screen;
    753     desired.visualid = visualID;
    754 
    755     result = XGetVisualInfo(_glfw.x11.display, vimask, &desired, &count);
    756     if (!result)
    757     {
    758         _glfwInputError(GLFW_PLATFORM_ERROR,
    759                         "EGL: Failed to retrieve Visual for EGLConfig");
    760         return GLFW_FALSE;
    761     }
    762 
    763     *visual = result->visual;
    764     *depth = result->depth;
    765 
    766     XFree(result);
    767     return GLFW_TRUE;
    768 }
    769 #endif // _GLFW_X11
    770 
    771 
    772 //////////////////////////////////////////////////////////////////////////
    773 //////                        GLFW native API                       //////
    774 //////////////////////////////////////////////////////////////////////////
    775 
    776 GLFWAPI EGLDisplay glfwGetEGLDisplay(void)
    777 {
    778     _GLFW_REQUIRE_INIT_OR_RETURN(EGL_NO_DISPLAY);
    779     return _glfw.egl.display;
    780 }
    781 
    782 GLFWAPI EGLContext glfwGetEGLContext(GLFWwindow* handle)
    783 {
    784     _GLFWwindow* window = (_GLFWwindow*) handle;
    785     _GLFW_REQUIRE_INIT_OR_RETURN(EGL_NO_CONTEXT);
    786 
    787     if (window->context.source != GLFW_EGL_CONTEXT_API)
    788     {
    789         _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
    790         return EGL_NO_CONTEXT;
    791     }
    792 
    793     return window->context.egl.handle;
    794 }
    795 
    796 GLFWAPI EGLSurface glfwGetEGLSurface(GLFWwindow* handle)
    797 {
    798     _GLFWwindow* window = (_GLFWwindow*) handle;
    799     _GLFW_REQUIRE_INIT_OR_RETURN(EGL_NO_SURFACE);
    800 
    801     if (window->context.source != GLFW_EGL_CONTEXT_API)
    802     {
    803         _glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
    804         return EGL_NO_SURFACE;
    805     }
    806 
    807     return window->context.egl.surface;
    808 }
    809