src/video/SDL_egl.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 15 Oct 2014 09:13:36 -0700
changeset 9162 69856cdc9da6
parent 8976 1a5d959d7b32
child 9164 494876610c49
permissions -rw-r--r--
Fixed bug where EGL function pointers wouldn't get saved in optimized gcc builds
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) \
9162
69856cdc9da6 Fixed bug where EGL function pointers wouldn't get saved in optimized gcc builds
Sam Lantinga <slouken@libsdl.org>
parents: 8976
diff changeset
    65
_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 */
8976
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
    72
#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
    73
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
    74
{
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 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
    76
    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
    77
    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
    78
    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
    79
    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
    80
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
    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
    82
    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
    83
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
    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
    85
        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
    86
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
        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
    88
            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
    89
                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
    90
                    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
    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
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
                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
    94
                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
    95
            }
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
            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
    97
                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
    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
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
    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
   103
}
8976
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   104
#endif /* EGL_KHR_create_context */
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   105
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   106
void *
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   107
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
   108
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   109
    static char procname[1024];
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   110
    void *retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   111
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   112
    /* 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
   113
#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
   114
    if (_this->egl_data->eglGetProcAddress) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   115
        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
   116
        if (retval) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   117
            return retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   118
        }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   119
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   120
#endif
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   121
    
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   122
    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
   123
    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
   124
        procname[0] = '_';
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   125
        SDL_strlcpy(procname + 1, proc, 1022);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   126
        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
   127
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   128
    return retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   129
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   130
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   131
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   132
SDL_EGL_UnloadLibrary(_THIS)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   133
{
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   134
    if (_this->egl_data) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   135
        if (_this->egl_data->egl_display) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   136
            _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
   137
            _this->egl_data->egl_display = NULL;
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   138
        }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   139
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   140
        if (_this->egl_data->dll_handle) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   141
            SDL_UnloadObject(_this->egl_data->dll_handle);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   142
            _this->egl_data->dll_handle = NULL;
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   143
        }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   144
        if (_this->egl_data->egl_dll_handle) {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   145
            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
   146
            _this->egl_data->egl_dll_handle = NULL;
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   147
        }
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   148
        
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   149
        SDL_free(_this->egl_data);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   150
        _this->egl_data = NULL;
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
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   153
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   154
int
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   155
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
   156
{
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   157
    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
   158
    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
   159
#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
   160
    const char *d3dcompiler;
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   161
#endif
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   162
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   163
    if (_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   164
        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
   165
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   166
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   167
    _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
   168
    if (!_this->egl_data) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   169
        return SDL_OutOfMemory();
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   170
    }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   171
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
   172
#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
   173
    d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   174
    if (!d3dcompiler) {
8733
02b7b0448811 Don't try to load d3dcompiler_46.dll on Windows XP
Sam Lantinga <slouken@libsdl.org>
parents: 8663
diff changeset
   175
        if (WIN_IsWindowsVistaOrGreater()) {
02b7b0448811 Don't try to load d3dcompiler_46.dll on Windows XP
Sam Lantinga <slouken@libsdl.org>
parents: 8663
diff changeset
   176
            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
   177
        } else {
8740
fbe394d76273 Better compiler for Windows XP systems
Sam Lantinga <slouken@libsdl.org>
parents: 8734
diff changeset
   178
            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
   179
        }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   180
    }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   181
    if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   182
        SDL_LoadObject(d3dcompiler);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   183
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   184
#endif
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   185
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   186
    /* 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
   187
    path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   188
    if (path != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   189
        egl_dll_handle = SDL_LoadObject(path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   190
    }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   191
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   192
    if (egl_dll_handle == NULL) {
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   193
        if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   194
            if (_this->gl_config.major_version > 1) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   195
                path = DEFAULT_OGL_ES2;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   196
                egl_dll_handle = SDL_LoadObject(path);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   197
            }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   198
            else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   199
                path = DEFAULT_OGL_ES;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   200
                egl_dll_handle = SDL_LoadObject(path);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   201
                if (egl_dll_handle == NULL) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   202
                    path = DEFAULT_OGL_ES_PVR;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   203
                    egl_dll_handle = SDL_LoadObject(path);
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
            }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   206
        }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   207
#ifdef DEFAULT_OGL         
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   208
        else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   209
            path = DEFAULT_OGL;
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   210
            egl_dll_handle = SDL_LoadObject(path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   211
        }
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   212
#endif        
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   213
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   214
    _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
   215
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   216
    if (egl_dll_handle == NULL) {
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   217
        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
   218
    }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   219
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   220
    /* 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
   221
    if (egl_path != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   222
        dll_handle = SDL_LoadObject(egl_path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   223
    }   
8105
f74bf1bbe472 Make EGL loading more resilient.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8093
diff changeset
   224
    /* 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
   225
    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
   226
        if (dll_handle != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   227
            SDL_UnloadObject(dll_handle);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   228
        }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   229
        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
   230
        if (path == NULL) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   231
            path = DEFAULT_EGL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   232
        }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   233
        dll_handle = SDL_LoadObject(path);
8872
5a6375d96834 Fixes #2583, clean up EGL initialization
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8763
diff changeset
   234
        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
   235
            if (dll_handle != NULL) {
5a6375d96834 Fixes #2583, clean up EGL initialization
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8763
diff changeset
   236
                SDL_UnloadObject(dll_handle);
5a6375d96834 Fixes #2583, clean up EGL initialization
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8763
diff changeset
   237
            }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   238
            return SDL_SetError("Could not load EGL library");
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   239
        }
8763
a8ef3ac69e3d Fixes #2519, clear error if using the default library path for EGL succeeds.
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8753
diff changeset
   240
        SDL_ClearError();
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   241
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   242
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   243
    _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
   244
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   245
    /* Load new function pointers */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   246
    LOAD_FUNC(eglGetDisplay);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   247
    LOAD_FUNC(eglInitialize);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   248
    LOAD_FUNC(eglTerminate);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   249
    LOAD_FUNC(eglGetProcAddress);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   250
    LOAD_FUNC(eglChooseConfig);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   251
    LOAD_FUNC(eglGetConfigAttrib);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   252
    LOAD_FUNC(eglCreateContext);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   253
    LOAD_FUNC(eglDestroyContext);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   254
    LOAD_FUNC(eglCreateWindowSurface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   255
    LOAD_FUNC(eglDestroySurface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   256
    LOAD_FUNC(eglMakeCurrent);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   257
    LOAD_FUNC(eglSwapBuffers);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   258
    LOAD_FUNC(eglSwapInterval);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   259
    LOAD_FUNC(eglWaitNative);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   260
    LOAD_FUNC(eglWaitGL);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   261
    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
   262
    LOAD_FUNC(eglQueryString);
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   263
    
8663
308e74493106 WinRT: Got OpenGL ES 2 working with the latest version of ANGLE/WinRT.
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   264
#if !defined(__WINRT__)
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   265
    _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
   266
    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
   267
        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
   268
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   269
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   270
    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
   271
        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
   272
    }
8663
308e74493106 WinRT: Got OpenGL ES 2 working with the latest version of ANGLE/WinRT.
David Ludwig <dludwig@pobox.com>
parents: 8583
diff changeset
   273
#endif
7659
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
    _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
   276
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   277
    if (path) {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   278
        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
   279
    } else {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   280
        *_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
   281
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   282
    
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   283
    return 0;
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   284
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   285
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   286
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   287
SDL_EGL_ChooseConfig(_THIS) 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   288
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   289
    /* 64 seems nice. */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   290
    EGLint attribs[64];
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   291
    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
   292
    /* 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
   293
    EGLConfig configs[128];
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   294
    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
   295
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   296
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   297
        /* 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
   298
        return -1;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   299
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   300
  
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   301
    /* Get a valid EGL configuration */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   302
    i = 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   303
    attribs[i++] = EGL_RED_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.red_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   305
    attribs[i++] = EGL_GREEN_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.green_size;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   307
    attribs[i++] = EGL_BLUE_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   308
    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
   309
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   310
    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
   311
        attribs[i++] = EGL_ALPHA_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   312
        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
   313
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   314
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   315
    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
   316
        attribs[i++] = EGL_BUFFER_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   317
        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
   318
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   319
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   320
    attribs[i++] = EGL_DEPTH_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   321
    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
   322
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   323
    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
   324
        attribs[i++] = EGL_STENCIL_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   325
        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
   326
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   327
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   328
    if (_this->gl_config.multisamplebuffers) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   329
        attribs[i++] = EGL_SAMPLE_BUFFERS;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   330
        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
   331
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   332
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   333
    if (_this->gl_config.multisamplesamples) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   334
        attribs[i++] = EGL_SAMPLES;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   335
        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
   336
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   337
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   338
    attribs[i++] = EGL_RENDERABLE_TYPE;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   339
    if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   340
        if (_this->gl_config.major_version == 2) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   341
            attribs[i++] = EGL_OPENGL_ES2_BIT;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   342
        } else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   343
            attribs[i++] = EGL_OPENGL_ES_BIT;
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
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   346
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   347
    else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   348
        attribs[i++] = EGL_OPENGL_BIT;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   349
        _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
   350
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   351
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   352
    attribs[i++] = EGL_NONE;
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   353
   
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   354
    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
   355
        attribs,
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   356
        configs, SDL_arraysize(configs),
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   357
        &found_configs) == EGL_FALSE ||
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   358
        found_configs == 0) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   359
        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
   360
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   361
    
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   362
    /* 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
   363
    /* 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
   364
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   365
    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
   366
        bitdiff = 0;
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   367
        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
   368
            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
   369
               break;
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   370
            }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   371
            
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   372
            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
   373
                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
   374
                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
   375
                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
   376
                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
   377
                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
   378
                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
   379
                _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
   380
                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
   381
            }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   382
        }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   383
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   384
        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
   385
            _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
   386
            
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   387
            best_bitdiff = bitdiff;
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   388
        }
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
        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
   391
    }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   392
    
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   393
    return 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   394
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   395
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   396
SDL_GLContext
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   397
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
   398
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   399
    EGLint context_attrib_list[] = {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   400
        EGL_CONTEXT_CLIENT_VERSION,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   401
        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
   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,
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
   404
        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
   405
        EGL_NONE,
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   406
        EGL_NONE
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   407
    };
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   408
    
8061
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   409
    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
   410
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   411
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   412
        /* 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
   413
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   414
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   415
    
8061
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   416
    if (_this->gl_config.share_with_current_context) {
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   417
        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
   418
    }
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   419
    
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   420
    /* Bind the API */
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   421
    if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   422
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   423
        if (_this->gl_config.major_version) {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   424
            context_attrib_list[1] = _this->gl_config.major_version;
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   425
        }
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   426
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   427
        egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   428
                                          _this->egl_data->egl_config,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   429
                                          share_context, context_attrib_list);
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   430
    }
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   431
    else {
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   432
        _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
   433
#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
   434
        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
   435
            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
   436
            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
   437
            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
   438
            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
   439
            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
   440
            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
   441
            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
   442
                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
   443
                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
   444
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
            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
   446
            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
   447
                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
   448
                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
   449
            }
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
        }
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
        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
   452
            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
   453
        }
8971
c30e826412d1 Maybe fixes bug #2291 (red tinted screen on some Samsung Android devices)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8966
diff changeset
   454
#else /* EGL_KHR_create_context */
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
   455
        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
   456
#endif /* EGL_KHR_create_context */
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   457
        egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   458
                                          _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
   459
                                          share_context, context_attrib_list);
8062
4fc5f66d63cc Wayland support
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8061
diff changeset
   460
    }
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   461
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   462
    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
   463
        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
   464
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   465
    }
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
    _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
   468
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   469
    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
   470
        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
   471
        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
   472
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   473
    }
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
    return (SDL_GLContext) egl_context;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   476
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   477
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   478
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   479
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
   480
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   481
    EGLContext egl_context = (EGLContext) context;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   482
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   483
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   484
        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
   485
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   486
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   487
    /* 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
   488
     * 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
   489
     */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   490
    if (!egl_context || !egl_surface) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   491
         _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
   492
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   493
    else {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   494
        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
   495
            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
   496
            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
   497
        }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   498
    }
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
    return 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   501
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   502
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   503
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   504
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
   505
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   506
    EGLBoolean status;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   507
    
7702
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   508
    if (!_this->egl_data) {
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   509
        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
   510
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   511
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   512
    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
   513
    if (status == EGL_TRUE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   514
        _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
   515
        return 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   516
    }
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
    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
   519
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   520
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   521
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   522
SDL_EGL_GetSwapInterval(_THIS)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   523
{
7702
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   524
    if (!_this->egl_data) {
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   525
        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
   526
    }
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
    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
   529
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   530
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   531
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   532
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
   533
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   534
    _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
   535
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   536
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   537
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   538
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
   539
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   540
    EGLContext egl_context = (EGLContext) context;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   541
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   542
    /* Clean up GLES and EGL */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   543
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   544
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   545
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   546
    
7896
0bca3fe7c03f Fixes Bug #2191,incorrect test for egl_context validity
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7865
diff changeset
   547
    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
   548
        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
   549
        _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
   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
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   553
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   554
EGLSurface *
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   555
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
   556
{
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   557
    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
   558
        return EGL_NO_SURFACE;
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   559
    }
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   560
    
8971
c30e826412d1 Maybe fixes bug #2291 (red tinted screen on some Samsung Android devices)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8966
diff changeset
   561
#if __ANDROID__
8976
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   562
    {
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   563
        /* Android docs recommend doing this!
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   564
         * Ref: http://developer.android.com/reference/android/app/NativeActivity.html 
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   565
         */
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   566
        EGLint format;
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   567
        _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   568
                                            _this->egl_data->egl_config, 
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   569
                                            EGL_NATIVE_VISUAL_ID, &format);
8971
c30e826412d1 Maybe fixes bug #2291 (red tinted screen on some Samsung Android devices)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8966
diff changeset
   570
8976
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   571
        ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8971
diff changeset
   572
    }
8971
c30e826412d1 Maybe fixes bug #2291 (red tinted screen on some Samsung Android devices)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8966
diff changeset
   573
#endif    
c30e826412d1 Maybe fixes bug #2291 (red tinted screen on some Samsung Android devices)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8966
diff changeset
   574
    
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   575
    return _this->egl_data->eglCreateWindowSurface(
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   576
            _this->egl_data->egl_display,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   577
            _this->egl_data->egl_config,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   578
            nw, NULL);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   579
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   580
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   581
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   582
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
   583
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   584
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   585
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   586
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   587
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   588
    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
   589
        _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
   590
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   591
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   592
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   593
#endif /* SDL_VIDEO_OPENGL_EGL */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   594
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   595
/* vi: set ts=4 sw=4 expandtab: */
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   596