src/video/SDL_egl.c
author Gabriel Jacobo <gabomdq@gmail.com>
Sat, 28 Jun 2014 13:05:16 -0300
changeset 8966 5771ab3290d6
parent 8964 d9ac759bb62a
child 8971 c30e826412d1
permissions -rw-r--r--
Better fix to support EGL_KHR_create_context and not fail to build on Android ...or Raspberry Pi (EGL < 1.4 systems)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     1
/*
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     2
 *  Simple DirectMedia Layer
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8105
diff changeset
     3
 *  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     4
 * 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     5
 *  This software is provided 'as-is', without any express or implied
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     6
 *  warranty.  In no event will the authors be held liable for any damages
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     7
 *  arising from the use of this software.
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     8
 * 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     9
 *  Permission is granted to anyone to use this software for any purpose,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    10
 *  including commercial applications, and to alter it and redistribute it
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    11
 *  freely, subject to the following restrictions:
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    12
 * 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    13
 *  1. The origin of this software must not be misrepresented; you must not
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    14
 *     claim that you wrote the original software. If you use this software
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    15
 *     in a product, an acknowledgment in the product documentation would be
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    16
 *     appreciated but is not required.
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    17
 *  2. Altered source versions must be plainly marked as such, and must not be
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    18
 *     misrepresented as being the original software.
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    19
 *  3. This notice may not be removed or altered from any source distribution.
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    20
 */
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 8062
diff changeset
    21
#include "../SDL_internal.h"
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    22
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    23
#if SDL_VIDEO_OPENGL_EGL
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    24
8734
cb61954403ca Fixed compiler warning
Sam Lantinga <slouken@libsdl.org>
parents: 8733
diff changeset
    25
#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
cb61954403ca Fixed compiler warning
Sam Lantinga <slouken@libsdl.org>
parents: 8733
diff changeset
    26
#include "../core/windows/SDL_windows.h"
cb61954403ca Fixed compiler warning
Sam Lantinga <slouken@libsdl.org>
parents: 8733
diff changeset
    27
#endif
cb61954403ca Fixed compiler warning
Sam Lantinga <slouken@libsdl.org>
parents: 8733
diff changeset
    28
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    29
#include "SDL_sysvideo.h"
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    30
#include "SDL_egl_c.h"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    31
#include "SDL_loadso.h"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    32
#include "SDL_hints.h"
7782
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    33
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    34
#if SDL_VIDEO_DRIVER_RPI
7782
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    35
/* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    36
#define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    37
#define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    38
#define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    39
#define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"
7782
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    40
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    41
#elif SDL_VIDEO_DRIVER_ANDROID
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    42
/* Android */
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    43
#define DEFAULT_EGL "libEGL.so"
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    44
#define DEFAULT_OGL_ES2 "libGLESv2.so"
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    45
#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    46
#define DEFAULT_OGL_ES "libGLESv1_CM.so"
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    47
8545
7cf4f8916997 WinRT: got the SDL-official OpenGL ES 2 changes working, in an experimental state
David Ludwig <dludwig@pobox.com>
parents: 8543
diff changeset
    48
#elif SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    49
/* EGL AND OpenGL ES support via ANGLE */
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    50
#define DEFAULT_EGL "libEGL.dll"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    51
#define DEFAULT_OGL_ES2 "libGLESv2.dll"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    52
#define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    53
#define DEFAULT_OGL_ES "libGLESv1_CM.dll"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    54
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    55
#else
7782
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    56
/* Desktop Linux */
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
    57
#define DEFAULT_OGL "libGL.so.1"
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    58
#define DEFAULT_EGL "libEGL.so.1"
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    59
#define DEFAULT_OGL_ES2 "libGLESv2.so.2"
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    60
#define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    61
#define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    62
#endif /* SDL_VIDEO_DRIVER_RPI */
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    63
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    64
#define LOAD_FUNC(NAME) \
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    65
*((void**)&_this->egl_data->NAME) = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    66
if (!_this->egl_data->NAME) \
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    67
{ \
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    68
    return SDL_SetError("Could not retrieve EGL function " #NAME); \
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    69
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    70
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    71
/* EGL implementation of SDL OpenGL ES support */
8962
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    72
static int SDL_EGL_HasExtension(_THIS, const char *ext)
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    73
{
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    74
    int i;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    75
    int len = 0;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    76
    int ext_len;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    77
    const char *exts;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    78
    const char *ext_word;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    79
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    80
    ext_len = SDL_strlen(ext);
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    81
    exts = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    82
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    83
    if(exts) {
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    84
        ext_word = exts;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    85
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    86
        for(i = 0; exts[i] != 0; i++) {
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    87
            if(exts[i] == ' ') {
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    88
                if(ext_len == len && !SDL_strncmp(ext_word, ext, len)) {
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    89
                    return 1;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    90
                }
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    91
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    92
                len = 0;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    93
                ext_word = &exts[i + 1];
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    94
            }
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    95
            else {
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    96
                len++;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    97
            }
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    98
        }
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
    99
    }
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   100
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   101
    return 0;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   102
}
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   103
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   104
void *
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   105
SDL_EGL_GetProcAddress(_THIS, const char *proc)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   106
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   107
    static char procname[1024];
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   108
    void *retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   109
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   110
    /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
8753
497e72bba21b eglGetProcAddress fails the same way on MIR as ANDROID does on arm. Thanks Sylvain Becker for the patch!
Brandon Schaefer <brandon.schaefer@canonical.com>
parents: 8740
diff changeset
   111
#if !defined(SDL_VIDEO_DRIVER_ANDROID) && !defined(SDL_VIDEO_DRIVER_MIR) 
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   112
    if (_this->egl_data->eglGetProcAddress) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   113
        retval = _this->egl_data->eglGetProcAddress(proc);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   114
        if (retval) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   115
            return retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   116
        }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   117
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   118
#endif
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   119
    
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   120
    retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   121
    if (!retval && SDL_strlen(proc) <= 1022) {
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   122
        procname[0] = '_';
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   123
        SDL_strlcpy(procname + 1, proc, 1022);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   124
        retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   125
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   126
    return retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   127
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   128
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   129
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   130
SDL_EGL_UnloadLibrary(_THIS)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   131
{
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   132
    if (_this->egl_data) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   133
        if (_this->egl_data->egl_display) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   134
            _this->egl_data->eglTerminate(_this->egl_data->egl_display);
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   135
            _this->egl_data->egl_display = NULL;
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   136
        }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   137
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   138
        if (_this->egl_data->dll_handle) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   139
            SDL_UnloadObject(_this->egl_data->dll_handle);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   140
            _this->egl_data->dll_handle = NULL;
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   141
        }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   142
        if (_this->egl_data->egl_dll_handle) {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   143
            SDL_UnloadObject(_this->egl_data->egl_dll_handle);
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   144
            _this->egl_data->egl_dll_handle = NULL;
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   145
        }
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   146
        
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   147
        SDL_free(_this->egl_data);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   148
        _this->egl_data = NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   149
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   150
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   151
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   152
int
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   153
SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display)
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   154
{
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   155
    void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   156
    char *path = NULL;
8545
7cf4f8916997 WinRT: got the SDL-official OpenGL ES 2 changes working, in an experimental state
David Ludwig <dludwig@pobox.com>
parents: 8543
diff changeset
   157
#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   158
    const char *d3dcompiler;
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   159
#endif
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   160
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   161
    if (_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   162
        return SDL_SetError("OpenGL ES context already created");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   163
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   164
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   165
    _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   166
    if (!_this->egl_data) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   167
        return SDL_OutOfMemory();
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   168
    }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   169
8545
7cf4f8916997 WinRT: got the SDL-official OpenGL ES 2 changes working, in an experimental state
David Ludwig <dludwig@pobox.com>
parents: 8543
diff changeset
   170
#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   171
    d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   172
    if (!d3dcompiler) {
8733
02b7b0448811 Don't try to load d3dcompiler_46.dll on Windows XP
Sam Lantinga <slouken@libsdl.org>
parents: 8663
diff changeset
   173
        if (WIN_IsWindowsVistaOrGreater()) {
02b7b0448811 Don't try to load d3dcompiler_46.dll on Windows XP
Sam Lantinga <slouken@libsdl.org>
parents: 8663
diff changeset
   174
            d3dcompiler = "d3dcompiler_46.dll";
02b7b0448811 Don't try to load d3dcompiler_46.dll on Windows XP
Sam Lantinga <slouken@libsdl.org>
parents: 8663
diff changeset
   175
        } else {
8740
fbe394d76273 Better compiler for Windows XP systems
Sam Lantinga <slouken@libsdl.org>
parents: 8734
diff changeset
   176
            d3dcompiler = "d3dcompiler_43.dll";
8733
02b7b0448811 Don't try to load d3dcompiler_46.dll on Windows XP
Sam Lantinga <slouken@libsdl.org>
parents: 8663
diff changeset
   177
        }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   178
    }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   179
    if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   180
        SDL_LoadObject(d3dcompiler);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   181
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   182
#endif
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   183
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   184
    /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   185
    path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   186
    if (path != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   187
        egl_dll_handle = SDL_LoadObject(path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   188
    }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   189
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   190
    if (egl_dll_handle == NULL) {
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   191
        if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   192
            if (_this->gl_config.major_version > 1) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   193
                path = DEFAULT_OGL_ES2;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   194
                egl_dll_handle = SDL_LoadObject(path);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   195
            }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   196
            else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   197
                path = DEFAULT_OGL_ES;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   198
                egl_dll_handle = SDL_LoadObject(path);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   199
                if (egl_dll_handle == NULL) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   200
                    path = DEFAULT_OGL_ES_PVR;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   201
                    egl_dll_handle = SDL_LoadObject(path);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   202
                }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   203
            }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   204
        }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   205
#ifdef DEFAULT_OGL         
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   206
        else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   207
            path = DEFAULT_OGL;
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   208
            egl_dll_handle = SDL_LoadObject(path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   209
        }
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   210
#endif        
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   211
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   212
    _this->egl_data->egl_dll_handle = egl_dll_handle;
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   213
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   214
    if (egl_dll_handle == NULL) {
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   215
        return SDL_SetError("Could not initialize OpenGL / GLES library");
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   216
    }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   217
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   218
    /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   219
    if (egl_path != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   220
        dll_handle = SDL_LoadObject(egl_path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   221
    }   
8105
f74bf1bbe472 Make EGL loading more resilient.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   222
    /* Try loading a EGL symbol, if it does not work try the default library paths */
8872
5a6375d96834 Fixes #2583, clean up EGL initialization
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8763
diff changeset
   223
    if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   224
        if (dll_handle != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   225
            SDL_UnloadObject(dll_handle);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   226
        }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   227
        path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   228
        if (path == NULL) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   229
            path = DEFAULT_EGL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   230
        }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   231
        dll_handle = SDL_LoadObject(path);
8872
5a6375d96834 Fixes #2583, clean up EGL initialization
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8763
diff changeset
   232
        if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
5a6375d96834 Fixes #2583, clean up EGL initialization
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8763
diff changeset
   233
            if (dll_handle != NULL) {
5a6375d96834 Fixes #2583, clean up EGL initialization
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8763
diff changeset
   234
                SDL_UnloadObject(dll_handle);
5a6375d96834 Fixes #2583, clean up EGL initialization
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8763
diff changeset
   235
            }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   236
            return SDL_SetError("Could not load EGL library");
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   237
        }
8763
a8ef3ac69e3d Fixes #2519, clear error if using the default library path for EGL succeeds.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8753
diff changeset
   238
        SDL_ClearError();
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   239
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   240
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   241
    _this->egl_data->dll_handle = dll_handle;
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   242
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   243
    /* Load new function pointers */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   244
    LOAD_FUNC(eglGetDisplay);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   245
    LOAD_FUNC(eglInitialize);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   246
    LOAD_FUNC(eglTerminate);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   247
    LOAD_FUNC(eglGetProcAddress);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   248
    LOAD_FUNC(eglChooseConfig);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   249
    LOAD_FUNC(eglGetConfigAttrib);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   250
    LOAD_FUNC(eglCreateContext);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   251
    LOAD_FUNC(eglDestroyContext);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   252
    LOAD_FUNC(eglCreateWindowSurface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   253
    LOAD_FUNC(eglDestroySurface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   254
    LOAD_FUNC(eglMakeCurrent);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   255
    LOAD_FUNC(eglSwapBuffers);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   256
    LOAD_FUNC(eglSwapInterval);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   257
    LOAD_FUNC(eglWaitNative);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   258
    LOAD_FUNC(eglWaitGL);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   259
    LOAD_FUNC(eglBindAPI);
8962
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   260
    LOAD_FUNC(eglQueryString);
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   261
    
8663
308e74493106 WinRT: Got OpenGL ES 2 working with the latest version of ANGLE/WinRT.
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   262
#if !defined(__WINRT__)
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   263
    _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   264
    if (!_this->egl_data->egl_display) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   265
        return SDL_SetError("Could not get EGL display");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   266
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   267
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   268
    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   269
        return SDL_SetError("Could not initialize EGL");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   270
    }
8663
308e74493106 WinRT: Got OpenGL ES 2 working with the latest version of ANGLE/WinRT.
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   271
#endif
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   272
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   273
    _this->gl_config.driver_loaded = 1;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   274
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   275
    if (path) {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   276
        SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   277
    } else {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   278
        *_this->gl_config.driver_path = '\0';
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   279
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   280
    
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   281
    return 0;
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   282
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   283
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   284
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   285
SDL_EGL_ChooseConfig(_THIS) 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   286
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   287
    /* 64 seems nice. */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   288
    EGLint attribs[64];
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   289
    EGLint found_configs = 0, value;
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   290
    /* 128 seems even nicer here */
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   291
    EGLConfig configs[128];
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   292
    int i, j, best_bitdiff = -1, bitdiff;
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   293
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   294
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   295
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   296
        return -1;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   297
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   298
  
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   299
    /* Get a valid EGL configuration */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   300
    i = 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   301
    attribs[i++] = EGL_RED_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   302
    attribs[i++] = _this->gl_config.red_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   303
    attribs[i++] = EGL_GREEN_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   304
    attribs[i++] = _this->gl_config.green_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   305
    attribs[i++] = EGL_BLUE_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   306
    attribs[i++] = _this->gl_config.blue_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   307
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   308
    if (_this->gl_config.alpha_size) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   309
        attribs[i++] = EGL_ALPHA_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   310
        attribs[i++] = _this->gl_config.alpha_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   311
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   312
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   313
    if (_this->gl_config.buffer_size) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   314
        attribs[i++] = EGL_BUFFER_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   315
        attribs[i++] = _this->gl_config.buffer_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   316
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   317
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   318
    attribs[i++] = EGL_DEPTH_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   319
    attribs[i++] = _this->gl_config.depth_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   320
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   321
    if (_this->gl_config.stencil_size) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   322
        attribs[i++] = EGL_STENCIL_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   323
        attribs[i++] = _this->gl_config.stencil_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   324
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   325
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   326
    if (_this->gl_config.multisamplebuffers) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   327
        attribs[i++] = EGL_SAMPLE_BUFFERS;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   328
        attribs[i++] = _this->gl_config.multisamplebuffers;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   329
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   330
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   331
    if (_this->gl_config.multisamplesamples) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   332
        attribs[i++] = EGL_SAMPLES;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   333
        attribs[i++] = _this->gl_config.multisamplesamples;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   334
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   335
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   336
    attribs[i++] = EGL_RENDERABLE_TYPE;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   337
    if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   338
        if (_this->gl_config.major_version == 2) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   339
            attribs[i++] = EGL_OPENGL_ES2_BIT;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   340
        } else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   341
            attribs[i++] = EGL_OPENGL_ES_BIT;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   342
        }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   343
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   344
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   345
    else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   346
        attribs[i++] = EGL_OPENGL_BIT;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   347
        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   348
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   349
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   350
    attribs[i++] = EGL_NONE;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   351
   
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   352
    if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   353
        attribs,
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   354
        configs, SDL_arraysize(configs),
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   355
        &found_configs) == EGL_FALSE ||
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   356
        found_configs == 0) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   357
        return SDL_SetError("Couldn't find matching EGL config");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   358
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   359
    
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   360
    /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   361
    /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   362
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   363
    for ( i=0; i<found_configs; i++ ) {
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   364
        bitdiff = 0;
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   365
        for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   366
            if (attribs[j] == EGL_NONE) {
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   367
               break;
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   368
            }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   369
            
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   370
            if ( attribs[j+1] != EGL_DONT_CARE && (
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   371
                attribs[j] == EGL_RED_SIZE ||
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   372
                attribs[j] == EGL_GREEN_SIZE ||
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   373
                attribs[j] == EGL_BLUE_SIZE ||
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   374
                attribs[j] == EGL_ALPHA_SIZE ||
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   375
                attribs[j] == EGL_DEPTH_SIZE ||
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   376
                attribs[j] == EGL_STENCIL_SIZE)) {
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   377
                _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   378
                bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   379
            }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   380
        }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   381
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   382
        if (bitdiff < best_bitdiff || best_bitdiff == -1) {
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   383
            _this->egl_data->egl_config = configs[i];
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   384
            
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   385
            best_bitdiff = bitdiff;
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   386
        }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   387
           
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   388
        if (bitdiff == 0) break; /* we found an exact match! */
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   389
    }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   390
    
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   391
    return 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   392
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   393
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   394
SDL_GLContext
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   395
SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   396
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   397
    EGLint context_attrib_list[] = {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   398
        EGL_CONTEXT_CLIENT_VERSION,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   399
        1,
8962
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   400
        EGL_NONE,
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   401
        EGL_NONE,
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   402
        EGL_NONE,
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   403
        EGL_NONE,
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   404
        EGL_NONE
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   405
    };
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   406
    
8061
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   407
    EGLContext egl_context, share_context = EGL_NO_CONTEXT;
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   408
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   409
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   410
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   411
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   412
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   413
    
8061
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   414
    if (_this->gl_config.share_with_current_context) {
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   415
        share_context = (EGLContext)SDL_GL_GetCurrentContext();
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   416
    }
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   417
    
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   418
    /* Bind the API */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   419
    if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   420
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   421
        if (_this->gl_config.major_version) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   422
            context_attrib_list[1] = _this->gl_config.major_version;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   423
        }
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   424
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   425
        egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   426
                                          _this->egl_data->egl_config,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   427
                                          share_context, context_attrib_list);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   428
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   429
    else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   430
        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
8966
5771ab3290d6 Better fix to support EGL_KHR_create_context and not fail to build on Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8964
diff changeset
   431
#ifdef EGL_KHR_create_context        
8962
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   432
        if(SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   433
            context_attrib_list[0] = EGL_CONTEXT_MAJOR_VERSION_KHR;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   434
            context_attrib_list[1] = _this->gl_config.major_version;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   435
            context_attrib_list[2] = EGL_CONTEXT_MINOR_VERSION_KHR;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   436
            context_attrib_list[3] = _this->gl_config.minor_version;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   437
            context_attrib_list[4] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   438
            switch(_this->gl_config.profile_mask) {
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   439
            case SDL_GL_CONTEXT_PROFILE_COMPATIBILITY:
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   440
                context_attrib_list[5] = EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   441
                break;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   442
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   443
            case SDL_GL_CONTEXT_PROFILE_CORE:
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   444
            default:
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   445
                context_attrib_list[5] = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   446
                break;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   447
            }
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   448
        }
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   449
        else {
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   450
            context_attrib_list[0] = EGL_NONE;
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   451
        }
8966
5771ab3290d6 Better fix to support EGL_KHR_create_context and not fail to build on Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8964
diff changeset
   452
#else /* EGL_KHR_create_context*/
5771ab3290d6 Better fix to support EGL_KHR_create_context and not fail to build on Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8964
diff changeset
   453
        context_attrib_list[0] = EGL_NONE;
5771ab3290d6 Better fix to support EGL_KHR_create_context and not fail to build on Android
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8964
diff changeset
   454
#endif /* EGL_KHR_create_context */
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   455
        egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   456
                                          _this->egl_data->egl_config,
8962
c593cbef6d7d SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context
Knut Andre Tidemann <knut.tidemann@gmail.com>
parents: 8872
diff changeset
   457
                                          share_context, context_attrib_list);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   458
    }
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   459
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   460
    if (egl_context == EGL_NO_CONTEXT) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   461
        SDL_SetError("Could not create EGL context");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   462
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   463
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   464
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   465
    _this->egl_data->egl_swapinterval = 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   466
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   467
    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   468
        SDL_EGL_DeleteContext(_this, egl_context);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   469
        SDL_SetError("Could not make EGL context current");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   470
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   471
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   472
  
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   473
    return (SDL_GLContext) egl_context;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   474
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   475
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   476
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   477
SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   478
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   479
    EGLContext egl_context = (EGLContext) context;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   480
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   481
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   482
        return SDL_SetError("OpenGL not initialized");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   483
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   484
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   485
    /* The android emulator crashes badly if you try to eglMakeCurrent 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   486
     * with a valid context and invalid surface, so we have to check for both here.
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   487
     */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   488
    if (!egl_context || !egl_surface) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   489
         _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   490
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   491
    else {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   492
        if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   493
            egl_surface, egl_surface, egl_context)) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   494
            return SDL_SetError("Unable to make EGL context current");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   495
        }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   496
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   497
      
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   498
    return 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   499
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   500
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   501
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   502
SDL_EGL_SetSwapInterval(_THIS, int interval)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   503
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   504
    EGLBoolean status;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   505
    
7702
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   506
    if (!_this->egl_data) {
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   507
        return SDL_SetError("EGL not initialized");
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   508
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   509
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   510
    status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   511
    if (status == EGL_TRUE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   512
        _this->egl_data->egl_swapinterval = interval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   513
        return 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   514
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   515
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   516
    return SDL_SetError("Unable to set the EGL swap interval");
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   517
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   518
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   519
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   520
SDL_EGL_GetSwapInterval(_THIS)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   521
{
7702
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   522
    if (!_this->egl_data) {
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   523
        return SDL_SetError("EGL not initialized");
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   524
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   525
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   526
    return _this->egl_data->egl_swapinterval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   527
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   528
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   529
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   530
SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   531
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   532
    _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   533
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   534
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   535
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   536
SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   537
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   538
    EGLContext egl_context = (EGLContext) context;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   539
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   540
    /* Clean up GLES and EGL */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   541
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   542
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   543
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   544
    
7896
0bca3fe7c03f Fixes Bug #2191,incorrect test for egl_context validity
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7865
diff changeset
   545
    if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   546
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   547
        _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   548
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   549
        
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   550
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   551
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   552
EGLSurface *
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   553
SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   554
{
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   555
    if (SDL_EGL_ChooseConfig(_this) != 0) {
8045
3c2694255705 Fixed error return value in SDL_EGL_CreateSurface(), thanks to Mike Kasprzak
Sam Lantinga <slouken@libsdl.org>
parents: 8041
diff changeset
   556
        return EGL_NO_SURFACE;
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   557
    }
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   558
    
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   559
    return _this->egl_data->eglCreateWindowSurface(
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   560
            _this->egl_data->egl_display,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   561
            _this->egl_data->egl_config,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   562
            nw, NULL);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   563
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   564
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   565
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   566
SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   567
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   568
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   569
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   570
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   571
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   572
    if (egl_surface != EGL_NO_SURFACE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   573
        _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   574
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   575
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   576
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   577
#endif /* SDL_VIDEO_OPENGL_EGL */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   578
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   579
/* vi: set ts=4 sw=4 expandtab: */
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   580