src/video/SDL_egl.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 13 Dec 2013 09:48:12 -0300
changeset 8061 a5f8137ccf01
parent 8045 3c2694255705
child 8062 4fc5f66d63cc
permissions -rw-r--r--
Context sharing for EGL
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
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
     3
 *  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
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
 */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    21
#include "SDL_config.h"
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
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    25
#include "SDL_sysvideo.h"
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    26
#include "SDL_egl_c.h"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    27
#include "SDL_loadso.h"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    28
#include "SDL_hints.h"
7782
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    29
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    30
#if SDL_VIDEO_DRIVER_RPI
7782
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    31
/* 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
    32
#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
    33
#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
    34
#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
    35
#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
    36
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    37
#elif SDL_VIDEO_DRIVER_ANDROID
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    38
/* Android */
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    39
#define DEFAULT_EGL "libEGL.so"
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    40
#define DEFAULT_OGL_ES2 "libGLESv2.so"
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    41
#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    42
#define DEFAULT_OGL_ES "libGLESv1_CM.so"
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    43
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    44
#elif SDL_VIDEO_DRIVER_WINDOWS
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    45
/* EGL AND OpenGL ES support via ANGLE */
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    46
#define DEFAULT_EGL "libEGL.dll"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    47
#define DEFAULT_OGL_ES2 "libGLESv2.dll"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    48
#define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    49
#define DEFAULT_OGL_ES "libGLESv1_CM.dll"
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    50
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    51
#else
7782
e4ed6e54101e Fix EGL/OpenGL ES paths
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7753
diff changeset
    52
/* Desktop Linux */
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
    53
#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
    54
#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
    55
#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
    56
#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
    57
#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
    58
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    59
#define LOAD_FUNC(NAME) \
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    60
*((void**)&_this->egl_data->NAME) = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    61
if (!_this->egl_data->NAME) \
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    62
{ \
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    63
    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
    64
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    65
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    66
/* EGL implementation of SDL OpenGL ES support */
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
void *
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    69
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
    70
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    71
    static char procname[1024];
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    72
    void *retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    73
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    74
    /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    75
#if !defined(SDL_VIDEO_DRIVER_ANDROID) 
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    76
    if (_this->egl_data->eglGetProcAddress) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    77
        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
    78
        if (retval) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    79
            return retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    80
        }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    81
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    82
#endif
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    83
    
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    84
    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
    85
    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
    86
        procname[0] = '_';
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    87
        SDL_strlcpy(procname + 1, proc, 1022);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
    88
        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
    89
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    90
    return retval;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    91
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    92
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    93
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    94
SDL_EGL_UnloadLibrary(_THIS)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
    95
{
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
    96
    if (_this->egl_data) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
    97
        if (_this->egl_data->egl_display) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
    98
            _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
    99
            _this->egl_data->egl_display = NULL;
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   100
        }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   101
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   102
        if (_this->egl_data->dll_handle) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   103
            SDL_UnloadObject(_this->egl_data->dll_handle);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   104
            _this->egl_data->dll_handle = NULL;
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   105
        }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   106
        if (_this->egl_data->egl_dll_handle) {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   107
            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
   108
            _this->egl_data->egl_dll_handle = NULL;
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   109
        }
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   110
        
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   111
        SDL_free(_this->egl_data);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   112
        _this->egl_data = NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   113
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   114
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   115
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   116
int
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   117
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
   118
{
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   119
    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
   120
    char *path = NULL;
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   121
#if SDL_VIDEO_DRIVER_WINDOWS
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   122
    const char *d3dcompiler;
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   123
#endif
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   124
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   125
    if (_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   126
        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
   127
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   128
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   129
    _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
   130
    if (!_this->egl_data) {
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   131
        return SDL_OutOfMemory();
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   132
    }
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   133
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   134
#if SDL_VIDEO_DRIVER_WINDOWS
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   135
    d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   136
    if (!d3dcompiler) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   137
        /* By default we load the Vista+ compatible compiler */
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   138
        d3dcompiler = "d3dcompiler_46.dll";
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   139
    }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   140
    if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   141
        SDL_LoadObject(d3dcompiler);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   142
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   143
#endif
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   144
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   145
    /* 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
   146
    path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   147
    if (path != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   148
        egl_dll_handle = SDL_LoadObject(path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   149
    }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   150
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   151
    if (egl_dll_handle == NULL) {
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   152
        if (_this->gl_config.major_version > 1) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   153
            path = DEFAULT_OGL_ES2;
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   154
            egl_dll_handle = SDL_LoadObject(path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   155
        }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   156
        else {
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   157
            path = DEFAULT_OGL_ES;
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   158
            egl_dll_handle = SDL_LoadObject(path);
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   159
            if (egl_dll_handle == NULL) {
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   160
                path = DEFAULT_OGL_ES_PVR;
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   161
                egl_dll_handle = SDL_LoadObject(path);
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   162
            }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   163
        }
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   164
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   165
    _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
   166
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   167
    if (egl_dll_handle == NULL) {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   168
        return SDL_SetError("Could not initialize OpenGL ES library");
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   169
    }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   170
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   171
    /* 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
   172
    if (egl_path != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   173
        dll_handle = SDL_LoadObject(egl_path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   174
    }   
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   175
    /* Catch the case where the application isn't linked with EGL */
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   176
    if ((SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) && (egl_path == NULL)) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   177
        if (dll_handle != NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   178
            SDL_UnloadObject(dll_handle);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   179
        }
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   180
        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
   181
        if (path == NULL) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   182
            path = DEFAULT_EGL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   183
        }
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   184
        dll_handle = SDL_LoadObject(path);
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   185
        if (dll_handle == NULL) {
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   186
            return SDL_SetError("Could not load EGL library");
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   187
        }
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   188
    }
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   189
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   190
    _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
   191
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   192
    /* Load new function pointers */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   193
    LOAD_FUNC(eglGetDisplay);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   194
    LOAD_FUNC(eglInitialize);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   195
    LOAD_FUNC(eglTerminate);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   196
    LOAD_FUNC(eglGetProcAddress);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   197
    LOAD_FUNC(eglChooseConfig);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   198
    LOAD_FUNC(eglGetConfigAttrib);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   199
    LOAD_FUNC(eglCreateContext);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   200
    LOAD_FUNC(eglDestroyContext);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   201
    LOAD_FUNC(eglCreateWindowSurface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   202
    LOAD_FUNC(eglDestroySurface);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   203
    LOAD_FUNC(eglMakeCurrent);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   204
    LOAD_FUNC(eglSwapBuffers);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   205
    LOAD_FUNC(eglSwapInterval);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   206
    LOAD_FUNC(eglWaitNative);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   207
    LOAD_FUNC(eglWaitGL);
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   208
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   209
    _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
   210
    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
   211
        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
   212
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   213
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   214
    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
   215
        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
   216
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   217
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   218
    _this->egl_data->dll_handle = dll_handle;
7753
e4c38f17bfad Raspberry Pi support (also unified UDEV and EVDEV support)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7702
diff changeset
   219
    _this->egl_data->egl_dll_handle = egl_dll_handle;
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   220
    _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
   221
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   222
    if (path) {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   223
        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
   224
    } else {
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   225
        *_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
   226
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   227
    
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   228
    return 0;
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   229
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   230
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   231
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   232
SDL_EGL_ChooseConfig(_THIS) 
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   233
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   234
    /* 64 seems nice. */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   235
    EGLint attribs[64];
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   236
    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
   237
    /* 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
   238
    EGLConfig configs[128];
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   239
    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
   240
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   241
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   242
        /* 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
   243
        return -1;
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
  
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   246
    /* Get a valid EGL configuration */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   247
    i = 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   248
    attribs[i++] = EGL_RED_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   249
    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
   250
    attribs[i++] = EGL_GREEN_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   251
    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
   252
    attribs[i++] = EGL_BLUE_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   253
    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
   254
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   255
    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
   256
        attribs[i++] = EGL_ALPHA_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   257
        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
   258
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   259
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   260
    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
   261
        attribs[i++] = EGL_BUFFER_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   262
        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
   263
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   264
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   265
    attribs[i++] = EGL_DEPTH_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   266
    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
   267
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   268
    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
   269
        attribs[i++] = EGL_STENCIL_SIZE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   270
        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
   271
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   272
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   273
    if (_this->gl_config.multisamplebuffers) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   274
        attribs[i++] = EGL_SAMPLE_BUFFERS;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   275
        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
   276
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   277
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   278
    if (_this->gl_config.multisamplesamples) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   279
        attribs[i++] = EGL_SAMPLES;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   280
        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
   281
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   282
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   283
    attribs[i++] = EGL_RENDERABLE_TYPE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   284
    if (_this->gl_config.major_version == 2) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   285
        attribs[i++] = EGL_OPENGL_ES2_BIT;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   286
    } else {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   287
        attribs[i++] = EGL_OPENGL_ES_BIT;
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
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   290
    attribs[i++] = EGL_NONE;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   291
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   292
    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
   293
        attribs,
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   294
        configs, SDL_arraysize(configs),
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   295
        &found_configs) == EGL_FALSE ||
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   296
        found_configs == 0) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   297
        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
   298
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   299
    
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   300
    /* 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
   301
    /* 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
   302
8015
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   303
    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
   304
        bitdiff = 0;
8021
f96e12e0ade9 OpenGL ES support for Windows
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8015
diff changeset
   305
        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
   306
            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
   307
               break;
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   308
            }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   309
            
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   310
            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
   311
                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
   312
                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
   313
                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
   314
                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
   315
                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
   316
                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
   317
                _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
   318
                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
   319
            }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   320
        }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   321
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   322
        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
   323
            _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
   324
            
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   325
            best_bitdiff = bitdiff;
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   326
        }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   327
           
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   328
        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
   329
    }
ba26f042e36d Find the best EGL config available between those returned by eglChooseConfig
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7986
diff changeset
   330
    
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   331
    return 0;
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
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   334
SDL_GLContext
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   335
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
   336
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   337
    EGLint context_attrib_list[] = {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   338
        EGL_CONTEXT_CLIENT_VERSION,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   339
        1,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   340
        EGL_NONE
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   341
    };
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   342
    
8061
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   343
    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
   344
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   345
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   346
        /* 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
   347
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   348
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   349
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   350
    if (_this->gl_config.major_version) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   351
        context_attrib_list[1] = _this->gl_config.major_version;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   352
    }
8061
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   353
    
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   354
    if (_this->gl_config.share_with_current_context) {
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   355
        share_context = (EGLContext)SDL_GL_GetCurrentContext();
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   356
    }
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   357
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   358
    egl_context =
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   359
    _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   360
                                      _this->egl_data->egl_config,
8061
a5f8137ccf01 Context sharing for EGL
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8045
diff changeset
   361
                                      share_context, context_attrib_list);
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   362
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   363
    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
   364
        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
   365
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   366
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   367
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   368
    _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
   369
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   370
    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
   371
        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
   372
        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
   373
        return NULL;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   374
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   375
  
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   376
    return (SDL_GLContext) egl_context;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   377
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   378
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   379
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   380
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
   381
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   382
    EGLContext egl_context = (EGLContext) context;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   383
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   384
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   385
        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
   386
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   387
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   388
    /* 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
   389
     * 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
   390
     */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   391
    if (!egl_context || !egl_surface) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   392
         _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
   393
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   394
    else {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   395
        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
   396
            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
   397
            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
   398
        }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   399
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   400
      
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   401
    return 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   402
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   403
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   404
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   405
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
   406
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   407
    EGLBoolean status;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   408
    
7702
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   409
    if (!_this->egl_data) {
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   410
        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
   411
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   412
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   413
    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
   414
    if (status == EGL_TRUE) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   415
        _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
   416
        return 0;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   417
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   418
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   419
    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
   420
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   421
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   422
int
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   423
SDL_EGL_GetSwapInterval(_THIS)
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   424
{
7702
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   425
    if (!_this->egl_data) {
83878644c8e6 Fixes typo in EGL code (thanks jmcfarlane!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7679
diff changeset
   426
        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
   427
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   428
    
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   429
    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
   430
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   431
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   432
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   433
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
   434
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   435
    _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
   436
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   437
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   438
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   439
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
   440
{
7679
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   441
    EGLContext egl_context = (EGLContext) context;
b1fe132bc6a4 Fixes for -Wdeclaration-after-statement
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7659
diff changeset
   442
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   443
    /* Clean up GLES and EGL */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   444
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   445
        return;
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   446
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   447
    
7896
0bca3fe7c03f Fixes Bug #2191,incorrect test for egl_context validity
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7865
diff changeset
   448
    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
   449
        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
   450
        _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
   451
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   452
        
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   453
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   454
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   455
EGLSurface *
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   456
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
   457
{
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   458
    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
   459
        return EGL_NO_SURFACE;
8041
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   460
    }
afd62b3fda31 Select EGL config when creating the EGL surface
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8021
diff changeset
   461
    
7659
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   462
    return _this->egl_data->eglCreateWindowSurface(
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   463
            _this->egl_data->egl_display,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   464
            _this->egl_data->egl_config,
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   465
            nw, NULL);
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
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   468
void
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   469
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
   470
{
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   471
    if (!_this->egl_data) {
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   472
        return;
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
    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
   476
        _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
   477
    }
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   478
}
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   479
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   480
#endif /* SDL_VIDEO_OPENGL_EGL */
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   481
ac4ce59c40e7 Fixes bug #2037, common EGL code for Android and X11
Gabriel Jacobo <gabomdq@gmail.com>
parents:
diff changeset
   482
/* vi: set ts=4 sw=4 expandtab: */
7865
f2a42ca4ddf0 Better cleanup if OpenGL initialization fails
Sam Lantinga <slouken@libsdl.org>
parents: 7782
diff changeset
   483