Fixes bug #2037, common EGL code for Android and X11
authorGabriel Jacobo <gabomdq@gmail.com>
Mon, 19 Aug 2013 16:29:46 -0300
changeset 7659ac4ce59c40e7
parent 7658 4033ba4fcc31
child 7660 78204ab75b0f
Fixes bug #2037, common EGL code for Android and X11
Android.mk
android-project/src/org/libsdl/app/SDLActivity.java
configure
configure.in
include/SDL_config.h.in
include/SDL_config_android.h
include/SDL_video.h
src/core/android/SDL_android.c
src/core/android/SDL_android.h
src/video/SDL_egl.c
src/video/SDL_egl.h
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/android/SDL_androidevents.c
src/video/android/SDL_androidgl.c
src/video/android/SDL_androidvideo.c
src/video/android/SDL_androidwindow.c
src/video/android/SDL_androidwindow.h
src/video/x11/SDL_x11opengl.c
src/video/x11/SDL_x11opengl.h
src/video/x11/SDL_x11opengles.c
src/video/x11/SDL_x11opengles.h
src/video/x11/SDL_x11video.c
src/video/x11/SDL_x11window.c
src/video/x11/SDL_x11window.h
     1.1 --- a/Android.mk	Mon Aug 19 11:02:44 2013 -0400
     1.2 +++ b/Android.mk	Mon Aug 19 16:29:46 2013 -0300
     1.3 @@ -44,6 +44,6 @@
     1.4  	$(wildcard $(LOCAL_PATH)/src/video/android/*.c))
     1.5  
     1.6  LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
     1.7 -LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog
     1.8 +LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
     1.9  
    1.10  include $(BUILD_SHARED_LIBRARY)
     2.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Aug 19 11:02:44 2013 -0400
     2.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Aug 19 16:29:46 2013 -0300
     2.3 @@ -1,11 +1,5 @@
     2.4  package org.libsdl.app;
     2.5  
     2.6 -import javax.microedition.khronos.egl.EGL10;
     2.7 -import javax.microedition.khronos.egl.EGLConfig;
     2.8 -import javax.microedition.khronos.egl.EGLContext;
     2.9 -import javax.microedition.khronos.egl.EGLDisplay;
    2.10 -import javax.microedition.khronos.egl.EGLSurface;
    2.11 -
    2.12  import android.app.*;
    2.13  import android.content.*;
    2.14  import android.view.*;
    2.15 @@ -43,13 +37,6 @@
    2.16      protected static Thread mAudioThread;
    2.17      protected static AudioTrack mAudioTrack;
    2.18  
    2.19 -    // EGL objects
    2.20 -    protected static EGLContext  mEGLContext;
    2.21 -    protected static EGLSurface  mEGLSurface;
    2.22 -    protected static EGLDisplay  mEGLDisplay;
    2.23 -    protected static EGLConfig   mEGLConfig;
    2.24 -    protected static int mGLMajor, mGLMinor;
    2.25 -
    2.26      // Load the .so
    2.27      static {
    2.28          System.loadLibrary("SDL2");
    2.29 @@ -70,9 +57,7 @@
    2.30          mSingleton = this;
    2.31  
    2.32          // Set up the surface
    2.33 -        mEGLSurface = EGL10.EGL_NO_SURFACE;
    2.34          mSurface = new SDLSurface(getApplication());
    2.35 -        mEGLContext = EGL10.EGL_NO_CONTEXT;
    2.36  
    2.37          mLayout = new AbsoluteLayout(this);
    2.38          mLayout.addView(mSurface);
    2.39 @@ -252,29 +237,12 @@
    2.40                                              int action, float x, 
    2.41                                              float y, float p);
    2.42      public static native void onNativeAccel(float x, float y, float z);
    2.43 -
    2.44 -    // Java functions called from C
    2.45 -
    2.46 -    public static boolean createGLContext(int majorVersion, int minorVersion, int[] attribs) {
    2.47 -        return initEGL(majorVersion, minorVersion, attribs);
    2.48 -    }
    2.49 -    
    2.50 -    public static void deleteGLContext() {
    2.51 -        if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLContext != EGL10.EGL_NO_CONTEXT) {
    2.52 -            EGL10 egl = (EGL10)EGLContext.getEGL();
    2.53 -            egl.eglMakeCurrent(SDLActivity.mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
    2.54 -            egl.eglDestroyContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLContext);
    2.55 -            SDLActivity.mEGLContext = EGL10.EGL_NO_CONTEXT;
    2.56 -
    2.57 -            if (SDLActivity.mEGLSurface != EGL10.EGL_NO_SURFACE) {
    2.58 -                egl.eglDestroySurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
    2.59 -                SDLActivity.mEGLSurface = EGL10.EGL_NO_SURFACE;
    2.60 -            }
    2.61 -        }
    2.62 -    }
    2.63 +    public static native void onNativeSurfaceChanged();
    2.64 +    public static native void onNativeSurfaceDestroyed();
    2.65 +    public static native void nativeFlipBuffers();
    2.66  
    2.67      public static void flipBuffers() {
    2.68 -        flipEGL();
    2.69 +        SDLActivity.nativeFlipBuffers();
    2.70      }
    2.71  
    2.72      public static boolean setActivityTitle(String title) {
    2.73 @@ -332,147 +300,9 @@
    2.74          // Transfer the task to the main thread as a Runnable
    2.75          return mSingleton.commandHandler.post(new ShowTextInputTask(x, y, w, h));
    2.76      }
    2.77 -
    2.78 -
    2.79 -    // EGL functions
    2.80 -    public static boolean initEGL(int majorVersion, int minorVersion, int[] attribs) {
    2.81 -        try {
    2.82 -            EGL10 egl = (EGL10)EGLContext.getEGL();
    2.83              
    2.84 -            if (SDLActivity.mEGLDisplay == null) {
    2.85 -                SDLActivity.mEGLDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
    2.86 -                int[] version = new int[2];
    2.87 -                egl.eglInitialize(SDLActivity.mEGLDisplay, version);
    2.88 -            }
    2.89 -            
    2.90 -            if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
    2.91 -                // No current GL context exists, we will create a new one.
    2.92 -                Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
    2.93 -                EGLConfig[] configs = new EGLConfig[128];
    2.94 -                int[] num_config = new int[1];
    2.95 -                if (!egl.eglChooseConfig(SDLActivity.mEGLDisplay, attribs, configs, 1, num_config) || num_config[0] == 0) {
    2.96 -                    Log.e("SDL", "No EGL config available");
    2.97 -                    return false;
    2.98 -                }
    2.99 -                EGLConfig config = null;
   2.100 -                int bestdiff = -1, bitdiff;
   2.101 -                int[] value = new int[1];
   2.102 -                
   2.103 -                // eglChooseConfig returns a number of configurations that match or exceed the requested attribs.
   2.104 -                // From those, we select the one that matches our requirements more closely
   2.105 -                Log.v("SDL", "Got " + num_config[0] + " valid modes from egl");
   2.106 -                for(int i = 0; i < num_config[0]; i++) {
   2.107 -                    bitdiff = 0;
   2.108 -                    // Go through some of the attributes and compute the bit difference between what we want and what we get.
   2.109 -                    for (int j = 0; ; j += 2) {
   2.110 -                        if (attribs[j] == EGL10.EGL_NONE)
   2.111 -                            break;
   2.112 -
   2.113 -                        if (attribs[j+1] != EGL10.EGL_DONT_CARE && (attribs[j] == EGL10.EGL_RED_SIZE ||
   2.114 -                            attribs[j] == EGL10.EGL_GREEN_SIZE ||
   2.115 -                            attribs[j] == EGL10.EGL_BLUE_SIZE ||
   2.116 -                            attribs[j] == EGL10.EGL_ALPHA_SIZE ||
   2.117 -                            attribs[j] == EGL10.EGL_DEPTH_SIZE ||
   2.118 -                            attribs[j] == EGL10.EGL_STENCIL_SIZE)) {
   2.119 -                            egl.eglGetConfigAttrib(SDLActivity.mEGLDisplay, configs[i], attribs[j], value);
   2.120 -                            bitdiff += value[0] - attribs[j + 1]; // value is always >= attrib
   2.121 -                        }
   2.122 -                    }
   2.123 -                    
   2.124 -                    if (bitdiff < bestdiff || bestdiff == -1) {
   2.125 -                        config = configs[i];
   2.126 -                        bestdiff = bitdiff;
   2.127 -                    }
   2.128 -                    
   2.129 -                    if (bitdiff == 0) break; // we found an exact match!
   2.130 -                }
   2.131 -                
   2.132 -                Log.d("SDL", "Selected mode with a total bit difference of " + bestdiff);
   2.133 -
   2.134 -                SDLActivity.mEGLConfig = config;
   2.135 -                SDLActivity.mGLMajor = majorVersion;
   2.136 -                SDLActivity.mGLMinor = minorVersion;
   2.137 -            }
   2.138 -            
   2.139 -            return SDLActivity.createEGLSurface();
   2.140 -
   2.141 -        } catch(Exception e) {
   2.142 -            Log.v("SDL", e + "");
   2.143 -            for (StackTraceElement s : e.getStackTrace()) {
   2.144 -                Log.v("SDL", s.toString());
   2.145 -            }
   2.146 -            return false;
   2.147 -        }
   2.148 -    }
   2.149 -
   2.150 -    public static boolean createEGLContext() {
   2.151 -        EGL10 egl = (EGL10)EGLContext.getEGL();
   2.152 -        int EGL_CONTEXT_CLIENT_VERSION=0x3098;
   2.153 -        int contextAttrs[] = new int[] { EGL_CONTEXT_CLIENT_VERSION, SDLActivity.mGLMajor, EGL10.EGL_NONE };
   2.154 -        SDLActivity.mEGLContext = egl.eglCreateContext(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, EGL10.EGL_NO_CONTEXT, contextAttrs);
   2.155 -        if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) {
   2.156 -            Log.e("SDL", "Couldn't create context");
   2.157 -            return false;
   2.158 -        }
   2.159 -        return true;
   2.160 -    }
   2.161 -
   2.162 -    public static boolean createEGLSurface() {
   2.163 -        if (SDLActivity.mEGLDisplay != null && SDLActivity.mEGLConfig != null) {
   2.164 -            EGL10 egl = (EGL10)EGLContext.getEGL();
   2.165 -            if (SDLActivity.mEGLContext == EGL10.EGL_NO_CONTEXT) createEGLContext();
   2.166 -
   2.167 -            if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) {
   2.168 -                Log.v("SDL", "Creating new EGL Surface");
   2.169 -                SDLActivity.mEGLSurface = egl.eglCreateWindowSurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLConfig, SDLActivity.mSurface, null);
   2.170 -                if (SDLActivity.mEGLSurface == EGL10.EGL_NO_SURFACE) {
   2.171 -                    Log.e("SDL", "Couldn't create surface");
   2.172 -                    return false;
   2.173 -                }
   2.174 -            }
   2.175 -            else Log.v("SDL", "EGL Surface remains valid");
   2.176 -
   2.177 -            if (egl.eglGetCurrentContext() != SDLActivity.mEGLContext) {
   2.178 -                if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface, SDLActivity.mEGLSurface, SDLActivity.mEGLContext)) {
   2.179 -                    Log.e("SDL", "Old EGL Context doesnt work, trying with a new one");
   2.180 -                    // TODO: Notify the user via a message that the old context could not be restored, and that textures need to be manually restored.
   2.181 -                    createEGLContext();
   2.182 -                    if (!egl.eglMakeCurrent(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface, SDLActivity.mEGLSurface, SDLActivity.mEGLContext)) {
   2.183 -                        Log.e("SDL", "Failed making EGL Context current");
   2.184 -                        return false;
   2.185 -                    }
   2.186 -                }
   2.187 -                else Log.v("SDL", "EGL Context made current");
   2.188 -            }
   2.189 -            else Log.v("SDL", "EGL Context remains current");
   2.190 -
   2.191 -            return true;
   2.192 -        } else {
   2.193 -            Log.e("SDL", "Surface creation failed, display = " + SDLActivity.mEGLDisplay + ", config = " + SDLActivity.mEGLConfig);
   2.194 -            return false;
   2.195 -        }
   2.196 -    }
   2.197 -
   2.198 -    // EGL buffer flip
   2.199 -    public static void flipEGL() {
   2.200 -        try {
   2.201 -            EGL10 egl = (EGL10)EGLContext.getEGL();
   2.202 -
   2.203 -            egl.eglWaitNative(EGL10.EGL_CORE_NATIVE_ENGINE, null);
   2.204 -
   2.205 -            // drawing here
   2.206 -
   2.207 -            egl.eglWaitGL();
   2.208 -
   2.209 -            egl.eglSwapBuffers(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
   2.210 -
   2.211 -
   2.212 -        } catch(Exception e) {
   2.213 -            Log.v("SDL", "flipEGL(): " + e);
   2.214 -            for (StackTraceElement s : e.getStackTrace()) {
   2.215 -                Log.v("SDL", s.toString());
   2.216 -            }
   2.217 -        }
   2.218 +    public static Surface getNativeSurface() {
   2.219 +        return SDLActivity.mSurface.getNativeSurface();
   2.220      }
   2.221  
   2.222      // Audio
   2.223 @@ -600,14 +430,16 @@
   2.224          mWidth = 1.0f;
   2.225          mHeight = 1.0f;
   2.226      }
   2.227 +    
   2.228 +    public Surface getNativeSurface() {
   2.229 +        return getHolder().getSurface();
   2.230 +    }
   2.231  
   2.232      // Called when we have a valid drawing surface
   2.233      @Override
   2.234      public void surfaceCreated(SurfaceHolder holder) {
   2.235          Log.v("SDL", "surfaceCreated()");
   2.236          holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
   2.237 -        // Set mIsSurfaceReady to 'true' *before* any call to handleResume
   2.238 -        SDLActivity.mIsSurfaceReady = true;
   2.239      }
   2.240  
   2.241      // Called when we lose the surface
   2.242 @@ -617,16 +449,7 @@
   2.243          // Call this *before* setting mIsSurfaceReady to 'false'
   2.244          SDLActivity.handlePause();
   2.245          SDLActivity.mIsSurfaceReady = false;
   2.246 -
   2.247 -        /* We have to clear the current context and destroy the egl surface here
   2.248 -         * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
   2.249 -         * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
   2.250 -         */
   2.251 -        
   2.252 -        EGL10 egl = (EGL10)EGLContext.getEGL();
   2.253 -        egl.eglMakeCurrent(SDLActivity.mEGLDisplay, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
   2.254 -        egl.eglDestroySurface(SDLActivity.mEGLDisplay, SDLActivity.mEGLSurface);
   2.255 -        SDLActivity.mEGLSurface = EGL10.EGL_NO_SURFACE;
   2.256 +        SDLActivity.onNativeSurfaceDestroyed();
   2.257      }
   2.258  
   2.259      // Called when the surface is resized
   2.260 @@ -687,6 +510,8 @@
   2.261  
   2.262          // Set mIsSurfaceReady to 'true' *before* making a call to handleResume
   2.263          SDLActivity.mIsSurfaceReady = true;
   2.264 +        SDLActivity.onNativeSurfaceChanged();
   2.265 +
   2.266  
   2.267          if (SDLActivity.mSDLThread == null) {
   2.268              // This is the entry point to the C app.
     3.1 --- a/configure	Mon Aug 19 11:02:44 2013 -0400
     3.2 +++ b/configure	Mon Aug 19 16:29:46 2013 -0300
     3.3 @@ -20547,9 +20547,9 @@
     3.4  CheckOpenGLESX11()
     3.5  {
     3.6      if test x$enable_video = xyes -a x$enable_video_opengles = xyes; then
     3.7 -        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES (EGL) support" >&5
     3.8 -$as_echo_n "checking for OpenGL ES (EGL) support... " >&6; }
     3.9 -        video_opengles=no
    3.10 +        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for EGL support" >&5
    3.11 +$as_echo_n "checking for EGL support... " >&6; }
    3.12 +        video_opengl_egl=no
    3.13          cat confdefs.h - <<_ACEOF >conftest.$ac_ext
    3.14  /* end confdefs.h.  */
    3.15  
    3.16 @@ -20566,81 +20566,86 @@
    3.17  _ACEOF
    3.18  if ac_fn_c_try_compile "$LINENO"; then :
    3.19  
    3.20 -        video_opengles=yes
    3.21 -
    3.22 -fi
    3.23 -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
    3.24 -        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles" >&5
    3.25 -$as_echo "$video_opengles" >&6; }
    3.26 -        if test x$video_opengles = xyes; then
    3.27 -            { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v1 headers" >&5
    3.28 +        video_opengl_egl=yes
    3.29 +
    3.30 +fi
    3.31 +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
    3.32 +        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengl_egl" >&5
    3.33 +$as_echo "$video_opengl_egl" >&6; }
    3.34 +        if test x$video_opengl_egl = xyes; then
    3.35 +
    3.36 +$as_echo "#define SDL_VIDEO_OPENGL_EGL 1" >>confdefs.h
    3.37 +
    3.38 +        fi
    3.39 +
    3.40 +        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v1 headers" >&5
    3.41  $as_echo_n "checking for OpenGL ES v1 headers... " >&6; }
    3.42 -            video_opengles_v1=no
    3.43 -            cat confdefs.h - <<_ACEOF >conftest.$ac_ext
    3.44 -/* end confdefs.h.  */
    3.45 -
    3.46 -             #include <GLES/gl.h>
    3.47 -             #include <GLES/glext.h>
    3.48 -
    3.49 -int
    3.50 -main ()
    3.51 -{
    3.52 -
    3.53 -
    3.54 -  ;
    3.55 -  return 0;
    3.56 -}
    3.57 -_ACEOF
    3.58 -if ac_fn_c_try_compile "$LINENO"; then :
    3.59 -
    3.60 -            video_opengles_v1=yes
    3.61 -
    3.62 -fi
    3.63 -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
    3.64 -            { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v1" >&5
    3.65 +        video_opengles_v1=no
    3.66 +        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
    3.67 +/* end confdefs.h.  */
    3.68 +
    3.69 +         #include <GLES/gl.h>
    3.70 +         #include <GLES/glext.h>
    3.71 +
    3.72 +int
    3.73 +main ()
    3.74 +{
    3.75 +
    3.76 +
    3.77 +  ;
    3.78 +  return 0;
    3.79 +}
    3.80 +_ACEOF
    3.81 +if ac_fn_c_try_compile "$LINENO"; then :
    3.82 +
    3.83 +        video_opengles_v1=yes
    3.84 +
    3.85 +fi
    3.86 +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
    3.87 +        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v1" >&5
    3.88  $as_echo "$video_opengles_v1" >&6; }
    3.89 -            if test x$video_opengles_v1 = xyes; then
    3.90 +        if test x$video_opengles_v1 = xyes; then
    3.91  
    3.92  $as_echo "#define SDL_VIDEO_OPENGL_ES 1" >>confdefs.h
    3.93  
    3.94  
    3.95  $as_echo "#define SDL_VIDEO_RENDER_OGL_ES 1" >>confdefs.h
    3.96  
    3.97 -            fi
    3.98 -            { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v2 headers" >&5
    3.99 +        fi
   3.100 +
   3.101 +        { $as_echo "$as_me:${as_lineno-$LINENO}: checking for OpenGL ES v2 headers" >&5
   3.102  $as_echo_n "checking for OpenGL ES v2 headers... " >&6; }
   3.103 -            video_opengles_v2=no
   3.104 -            cat confdefs.h - <<_ACEOF >conftest.$ac_ext
   3.105 -/* end confdefs.h.  */
   3.106 -
   3.107 -             #include <GLES2/gl2.h>
   3.108 -             #include <GLES2/gl2ext.h>
   3.109 -
   3.110 -int
   3.111 -main ()
   3.112 -{
   3.113 -
   3.114 -
   3.115 -  ;
   3.116 -  return 0;
   3.117 -}
   3.118 -_ACEOF
   3.119 -if ac_fn_c_try_compile "$LINENO"; then :
   3.120 -
   3.121 -            video_opengles_v2=yes
   3.122 -
   3.123 -fi
   3.124 -rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
   3.125 -            { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v2" >&5
   3.126 +        video_opengles_v2=no
   3.127 +        cat confdefs.h - <<_ACEOF >conftest.$ac_ext
   3.128 +/* end confdefs.h.  */
   3.129 +
   3.130 +         #include <GLES2/gl2.h>
   3.131 +         #include <GLES2/gl2ext.h>
   3.132 +
   3.133 +int
   3.134 +main ()
   3.135 +{
   3.136 +
   3.137 +
   3.138 +  ;
   3.139 +  return 0;
   3.140 +}
   3.141 +_ACEOF
   3.142 +if ac_fn_c_try_compile "$LINENO"; then :
   3.143 +
   3.144 +        video_opengles_v2=yes
   3.145 +
   3.146 +fi
   3.147 +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
   3.148 +        { $as_echo "$as_me:${as_lineno-$LINENO}: result: $video_opengles_v2" >&5
   3.149  $as_echo "$video_opengles_v2" >&6; }
   3.150 -            if test x$video_opengles_v2 = xyes; then
   3.151 +        if test x$video_opengles_v2 = xyes; then
   3.152  
   3.153  $as_echo "#define SDL_VIDEO_OPENGL_ES2 1" >>confdefs.h
   3.154  
   3.155  
   3.156  $as_echo "#define SDL_VIDEO_RENDER_OGL_ES2 1" >>confdefs.h
   3.157  
   3.158 -            fi
   3.159          fi
   3.160      fi
   3.161  }
     4.1 --- a/configure.in	Mon Aug 19 11:02:44 2013 -0400
     4.2 +++ b/configure.in	Mon Aug 19 16:29:46 2013 -0300
     4.3 @@ -1681,44 +1681,47 @@
     4.4  CheckOpenGLESX11()
     4.5  {
     4.6      if test x$enable_video = xyes -a x$enable_video_opengles = xyes; then
     4.7 -        AC_MSG_CHECKING(for OpenGL ES (EGL) support)
     4.8 -        video_opengles=no
     4.9 +        AC_MSG_CHECKING(for EGL support)
    4.10 +        video_opengl_egl=no
    4.11          AC_TRY_COMPILE([
    4.12           #include <EGL/egl.h>
    4.13          ],[
    4.14          ],[
    4.15 -        video_opengles=yes
    4.16 +        video_opengl_egl=yes
    4.17          ])
    4.18 -        AC_MSG_RESULT($video_opengles)
    4.19 -        if test x$video_opengles = xyes; then
    4.20 -            AC_MSG_CHECKING(for OpenGL ES v1 headers)
    4.21 -            video_opengles_v1=no
    4.22 -            AC_TRY_COMPILE([
    4.23 -             #include <GLES/gl.h>
    4.24 -             #include <GLES/glext.h>
    4.25 -            ],[
    4.26 -            ],[
    4.27 -            video_opengles_v1=yes
    4.28 -            ])
    4.29 -            AC_MSG_RESULT($video_opengles_v1)
    4.30 -            if test x$video_opengles_v1 = xyes; then
    4.31 -                AC_DEFINE(SDL_VIDEO_OPENGL_ES, 1, [ ])
    4.32 -                AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES, 1, [ ])
    4.33 -            fi
    4.34 -            AC_MSG_CHECKING(for OpenGL ES v2 headers)
    4.35 -            video_opengles_v2=no
    4.36 -            AC_TRY_COMPILE([
    4.37 -             #include <GLES2/gl2.h>
    4.38 -             #include <GLES2/gl2ext.h>
    4.39 -            ],[
    4.40 -            ],[
    4.41 -            video_opengles_v2=yes
    4.42 -            ])
    4.43 -            AC_MSG_RESULT($video_opengles_v2)
    4.44 -            if test x$video_opengles_v2 = xyes; then
    4.45 -                AC_DEFINE(SDL_VIDEO_OPENGL_ES2, 1, [ ])
    4.46 -                AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES2, 1, [ ])
    4.47 -            fi
    4.48 +        AC_MSG_RESULT($video_opengl_egl)
    4.49 +        if test x$video_opengl_egl = xyes; then
    4.50 +            AC_DEFINE(SDL_VIDEO_OPENGL_EGL, 1, [ ])
    4.51 +        fi
    4.52 +            
    4.53 +        AC_MSG_CHECKING(for OpenGL ES v1 headers)
    4.54 +        video_opengles_v1=no
    4.55 +        AC_TRY_COMPILE([
    4.56 +         #include <GLES/gl.h>
    4.57 +         #include <GLES/glext.h>
    4.58 +        ],[
    4.59 +        ],[
    4.60 +        video_opengles_v1=yes
    4.61 +        ])
    4.62 +        AC_MSG_RESULT($video_opengles_v1)
    4.63 +        if test x$video_opengles_v1 = xyes; then
    4.64 +            AC_DEFINE(SDL_VIDEO_OPENGL_ES, 1, [ ])
    4.65 +            AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES, 1, [ ])
    4.66 +        fi
    4.67 +        
    4.68 +        AC_MSG_CHECKING(for OpenGL ES v2 headers)
    4.69 +        video_opengles_v2=no
    4.70 +        AC_TRY_COMPILE([
    4.71 +         #include <GLES2/gl2.h>
    4.72 +         #include <GLES2/gl2ext.h>
    4.73 +        ],[
    4.74 +        ],[
    4.75 +        video_opengles_v2=yes
    4.76 +        ])
    4.77 +        AC_MSG_RESULT($video_opengles_v2)
    4.78 +        if test x$video_opengles_v2 = xyes; then
    4.79 +            AC_DEFINE(SDL_VIDEO_OPENGL_ES2, 1, [ ])
    4.80 +            AC_DEFINE(SDL_VIDEO_RENDER_OGL_ES2, 1, [ ])
    4.81          fi
    4.82      fi
    4.83  }
     5.1 --- a/include/SDL_config.h.in	Mon Aug 19 11:02:44 2013 -0400
     5.2 +++ b/include/SDL_config.h.in	Mon Aug 19 16:29:46 2013 -0300
     5.3 @@ -290,6 +290,7 @@
     5.4  #undef SDL_VIDEO_OPENGL_ES
     5.5  #undef SDL_VIDEO_OPENGL_BGL
     5.6  #undef SDL_VIDEO_OPENGL_CGL
     5.7 +#undef SDL_VIDEO_OPENGL_EGL
     5.8  #undef SDL_VIDEO_OPENGL_GLX
     5.9  #undef SDL_VIDEO_OPENGL_WGL
    5.10  #undef SDL_VIDEO_OPENGL_OSMESA
     6.1 --- a/include/SDL_config_android.h	Mon Aug 19 11:02:44 2013 -0400
     6.2 +++ b/include/SDL_config_android.h	Mon Aug 19 16:29:46 2013 -0300
     6.3 @@ -129,6 +129,7 @@
     6.4  
     6.5  /* Enable OpenGL ES */
     6.6  #define SDL_VIDEO_OPENGL_ES 1
     6.7 +#define SDL_VIDEO_OPENGL_EGL 1
     6.8  #define SDL_VIDEO_RENDER_OGL_ES 1
     6.9  #define SDL_VIDEO_RENDER_OGL_ES2    1
    6.10  
     7.1 --- a/include/SDL_video.h	Mon Aug 19 11:02:44 2013 -0400
     7.2 +++ b/include/SDL_video.h	Mon Aug 19 16:29:46 2013 -0300
     7.3 @@ -193,7 +193,7 @@
     7.4  {
     7.5      SDL_GL_CONTEXT_PROFILE_CORE           = 0x0001,
     7.6      SDL_GL_CONTEXT_PROFILE_COMPATIBILITY  = 0x0002,
     7.7 -    SDL_GL_CONTEXT_PROFILE_ES             = 0x0004
     7.8 +    SDL_GL_CONTEXT_PROFILE_ES             = 0x0004 /* GLX_CONTEXT_ES2_PROFILE_BIT_EXT */
     7.9  } SDL_GLprofile;
    7.10  
    7.11  typedef enum
     8.1 --- a/src/core/android/SDL_android.c	Mon Aug 19 11:02:44 2013 -0400
     8.2 +++ b/src/core/android/SDL_android.c	Mon Aug 19 16:29:46 2013 -0300
     8.3 @@ -33,6 +33,7 @@
     8.4  #include "../../video/android/SDL_androidkeyboard.h"
     8.5  #include "../../video/android/SDL_androidtouch.h"
     8.6  #include "../../video/android/SDL_androidvideo.h"
     8.7 +#include "../../video/android/SDL_androidwindow.h"
     8.8  
     8.9  #include <android/log.h>
    8.10  #include <pthread.h>
    8.11 @@ -67,8 +68,7 @@
    8.12  static jclass mActivityClass;
    8.13  
    8.14  // method signatures
    8.15 -static jmethodID midCreateGLContext;
    8.16 -static jmethodID midDeleteGLContext;
    8.17 +static jmethodID midGetNativeSurface;
    8.18  static jmethodID midFlipBuffers;
    8.19  static jmethodID midAudioInit;
    8.20  static jmethodID midAudioWriteShortBuffer;
    8.21 @@ -116,10 +116,8 @@
    8.22  
    8.23      mActivityClass = (jclass)((*mEnv)->NewGlobalRef(mEnv, cls));
    8.24  
    8.25 -    midCreateGLContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    8.26 -                                "createGLContext","(II[I)Z");
    8.27 -    midDeleteGLContext = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    8.28 -                                "deleteGLContext","()V");
    8.29 +    midGetNativeSurface = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    8.30 +                                "getNativeSurface","()Landroid/view/Surface;");
    8.31      midFlipBuffers = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    8.32                                  "flipBuffers","()V");
    8.33      midAudioInit = (*mEnv)->GetStaticMethodID(mEnv, mActivityClass,
    8.34 @@ -133,7 +131,7 @@
    8.35  
    8.36      bHasNewData = false;
    8.37  
    8.38 -    if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
    8.39 +    if(!midGetNativeSurface || !midFlipBuffers || !midAudioInit ||
    8.40         !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
    8.41          __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
    8.42      }
    8.43 @@ -148,6 +146,65 @@
    8.44      Android_SetScreenResolution(width, height, format);
    8.45  }
    8.46  
    8.47 +
    8.48 +// Surface Created
    8.49 +void Java_org_libsdl_app_SDLActivity_onNativeSurfaceChanged(JNIEnv* env, jclass jcls)
    8.50 +{
    8.51 +    SDL_WindowData *data;
    8.52 +    SDL_VideoDevice *_this;
    8.53 +
    8.54 +    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
    8.55 +        return;
    8.56 +    }
    8.57 +    
    8.58 +    _this =  SDL_GetVideoDevice();
    8.59 +    data =  (SDL_WindowData *) Android_Window->driverdata;
    8.60 +    
    8.61 +    /* If the surface has been previously destroyed by onNativeSurfaceDestroyed, recreate it here */
    8.62 +    if (data->egl_surface == EGL_NO_SURFACE) {
    8.63 +        if(data->native_window) {
    8.64 +            ANativeWindow_release(data->native_window);
    8.65 +        }
    8.66 +        data->native_window = Android_JNI_GetNativeWindow();
    8.67 +        data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
    8.68 +    }
    8.69 +    
    8.70 +    /* GL Context handling is done in the event loop because this function is run from the Java thread */
    8.71 +    
    8.72 +}
    8.73 +
    8.74 +// Surface Destroyed
    8.75 +void Java_org_libsdl_app_SDLActivity_onNativeSurfaceDestroyed(JNIEnv* env, jclass jcls)
    8.76 +{
    8.77 +    /* We have to clear the current context and destroy the egl surface here
    8.78 +     * Otherwise there's BAD_NATIVE_WINDOW errors coming from eglCreateWindowSurface on resume
    8.79 +     * Ref: http://stackoverflow.com/questions/8762589/eglcreatewindowsurface-on-ics-and-switching-from-2d-to-3d
    8.80 +     */
    8.81 +    SDL_WindowData *data;
    8.82 +    SDL_VideoDevice *_this;
    8.83 +    
    8.84 +    if (Android_Window == NULL || Android_Window->driverdata == NULL ) {
    8.85 +        return;
    8.86 +    }
    8.87 +    
    8.88 +    _this =  SDL_GetVideoDevice();
    8.89 +    data = (SDL_WindowData *) Android_Window->driverdata;
    8.90 +    
    8.91 +    if (data->egl_surface != EGL_NO_SURFACE) {
    8.92 +        SDL_EGL_MakeCurrent(_this, NULL, NULL);
    8.93 +        SDL_EGL_DestroySurface(_this, data->egl_surface);
    8.94 +        data->egl_surface = EGL_NO_SURFACE;
    8.95 +    }
    8.96 +    
    8.97 +    /* GL Context handling is done in the event loop because this function is run from the Java thread */
    8.98 +
    8.99 +}
   8.100 +
   8.101 +void Java_org_libsdl_app_SDLActivity_nativeFlipBuffers(JNIEnv* env, jclass jcls)
   8.102 +{
   8.103 +    SDL_GL_SwapWindow(Android_Window);
   8.104 +}
   8.105 +
   8.106  // Keydown
   8.107  void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
   8.108                                      JNIEnv* env, jclass jcls, jint keycode)
   8.109 @@ -317,47 +374,17 @@
   8.110      return s_active > 0;    
   8.111  }
   8.112  
   8.113 -
   8.114 -SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion,
   8.115 -                                int red, int green, int blue, int alpha,
   8.116 -                                int buffer, int depth, int stencil,
   8.117 -                                int buffers, int samples)
   8.118 +ANativeWindow* Android_JNI_GetNativeWindow(void)
   8.119  {
   8.120 +    ANativeWindow* anw;
   8.121 +    jobject s;
   8.122      JNIEnv *env = Android_JNI_GetEnv();
   8.123  
   8.124 -    jint attribs[] = {
   8.125 -        EGL_RED_SIZE, red,
   8.126 -        EGL_GREEN_SIZE, green,
   8.127 -        EGL_BLUE_SIZE, blue,
   8.128 -        EGL_ALPHA_SIZE, alpha,
   8.129 -        EGL_BUFFER_SIZE, buffer,
   8.130 -        EGL_DEPTH_SIZE, depth,
   8.131 -        EGL_STENCIL_SIZE, stencil,
   8.132 -        EGL_SAMPLE_BUFFERS, buffers,
   8.133 -        EGL_SAMPLES, samples,
   8.134 -        EGL_RENDERABLE_TYPE, (majorVersion == 1 ? EGL_OPENGL_ES_BIT : EGL_OPENGL_ES2_BIT),
   8.135 -        EGL_NONE
   8.136 -    };
   8.137 -    int len = SDL_arraysize(attribs);
   8.138 -
   8.139 -    jintArray array;
   8.140 -
   8.141 -    array = (*env)->NewIntArray(env, len);
   8.142 -    (*env)->SetIntArrayRegion(env, array, 0, len, attribs);
   8.143 -
   8.144 -    jboolean success = (*env)->CallStaticBooleanMethod(env, mActivityClass, midCreateGLContext, majorVersion, minorVersion, array);
   8.145 -
   8.146 -    (*env)->DeleteLocalRef(env, array);
   8.147 -
   8.148 -    return success ? SDL_TRUE : SDL_FALSE;
   8.149 -}
   8.150 -
   8.151 -SDL_bool Android_JNI_DeleteContext(void)
   8.152 -{
   8.153 -    /* There's only one context, so no parameter for now */
   8.154 -    JNIEnv *env = Android_JNI_GetEnv();
   8.155 -    (*env)->CallStaticVoidMethod(env, mActivityClass, midDeleteGLContext);
   8.156 -    return SDL_TRUE;
   8.157 +    s = (*env)->CallStaticObjectMethod(env, mActivityClass, midGetNativeSurface);
   8.158 +    anw = ANativeWindow_fromSurface(env, s);
   8.159 +    (*env)->DeleteLocalRef(env, s);
   8.160 +  
   8.161 +    return anw;
   8.162  }
   8.163  
   8.164  void Android_JNI_SwapWindow()
     9.1 --- a/src/core/android/SDL_android.h	Mon Aug 19 11:02:44 2013 -0400
     9.2 +++ b/src/core/android/SDL_android.h	Mon Aug 19 16:29:46 2013 -0300
     9.3 @@ -27,16 +27,20 @@
     9.4  /* *INDENT-ON* */
     9.5  #endif
     9.6  
     9.7 +#include <EGL/eglplatform.h>
     9.8 +#include <android/native_window_jni.h>
     9.9 +
    9.10  #include "SDL_rect.h"
    9.11  
    9.12  /* Interface from the SDL library into the Android Java activity */
    9.13 -extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion, int red, int green, int blue, int alpha, int buffer, int depth, int stencil, int buffers, int samples);
    9.14 -extern SDL_bool Android_JNI_DeleteContext(void);
    9.15 +/*extern SDL_bool Android_JNI_CreateContext(int majorVersion, int minorVersion, int red, int green, int blue, int alpha, int buffer, int depth, int stencil, int buffers, int samples);
    9.16 +extern SDL_bool Android_JNI_DeleteContext(void);*/
    9.17  extern void Android_JNI_SwapWindow();
    9.18  extern void Android_JNI_SetActivityTitle(const char *title);
    9.19  extern SDL_bool Android_JNI_GetAccelerometerValues(float values[3]);
    9.20  extern void Android_JNI_ShowTextInput(SDL_Rect *inputRect);
    9.21  extern void Android_JNI_HideTextInput();
    9.22 +extern ANativeWindow* Android_JNI_GetNativeWindow(void);
    9.23  
    9.24  /* Audio support */
    9.25  extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames);
    10.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2 +++ b/src/video/SDL_egl.c	Mon Aug 19 16:29:46 2013 -0300
    10.3 @@ -0,0 +1,407 @@
    10.4 +/*
    10.5 + *  Simple DirectMedia Layer
    10.6 + *  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
    10.7 + * 
    10.8 + *  This software is provided 'as-is', without any express or implied
    10.9 + *  warranty.  In no event will the authors be held liable for any damages
   10.10 + *  arising from the use of this software.
   10.11 + * 
   10.12 + *  Permission is granted to anyone to use this software for any purpose,
   10.13 + *  including commercial applications, and to alter it and redistribute it
   10.14 + *  freely, subject to the following restrictions:
   10.15 + * 
   10.16 + *  1. The origin of this software must not be misrepresented; you must not
   10.17 + *     claim that you wrote the original software. If you use this software
   10.18 + *     in a product, an acknowledgment in the product documentation would be
   10.19 + *     appreciated but is not required.
   10.20 + *  2. Altered source versions must be plainly marked as such, and must not be
   10.21 + *     misrepresented as being the original software.
   10.22 + *  3. This notice may not be removed or altered from any source distribution.
   10.23 + */
   10.24 +#include "SDL_config.h"
   10.25 +
   10.26 +#if SDL_VIDEO_OPENGL_EGL
   10.27 +
   10.28 +#include "SDL_sysvideo.h"
   10.29 +#include "SDL_egl.h"
   10.30 +
   10.31 +#define DEFAULT_EGL "libEGL.so"
   10.32 +#define DEFAULT_OGL_ES2 "libGLESv2.so"
   10.33 +#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
   10.34 +#define DEFAULT_OGL_ES "libGLESv1_CM.so"
   10.35 +
   10.36 +#define LOAD_FUNC(NAME) \
   10.37 +*((void**)&_this->egl_data->NAME) = dlsym(handle, #NAME); \
   10.38 +if (!_this->egl_data->NAME) \
   10.39 +{ \
   10.40 +    return SDL_SetError("Could not retrieve EGL function " #NAME); \
   10.41 +}
   10.42 +    
   10.43 +/* EGL implementation of SDL OpenGL ES support */
   10.44 +
   10.45 +void *
   10.46 +SDL_EGL_GetProcAddress(_THIS, const char *proc)
   10.47 +{
   10.48 +    static char procname[1024];
   10.49 +    void *handle;
   10.50 +    void *retval;
   10.51 +    
   10.52 +    /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
   10.53 +#if !defined(SDL_VIDEO_DRIVER_ANDROID)
   10.54 +    handle = _this->egl_data->egl_dll_handle;
   10.55 +    if (_this->egl_data->eglGetProcAddress) {
   10.56 +        retval = _this->egl_data->eglGetProcAddress(proc);
   10.57 +        if (retval) {
   10.58 +            return retval;
   10.59 +        }
   10.60 +    }
   10.61 +#endif
   10.62 +    
   10.63 +    handle = _this->gl_config.dll_handle;
   10.64 +    #if defined(__OpenBSD__) && !defined(__ELF__)
   10.65 +    #undef dlsym(x,y);
   10.66 +    #endif
   10.67 +    retval = dlsym(handle, proc);
   10.68 +    if (!retval && strlen(proc) <= 1022) {
   10.69 +        procname[0] = '_';
   10.70 +        strcpy(procname + 1, proc);
   10.71 +        retval = dlsym(handle, procname);
   10.72 +    }
   10.73 +    return retval;
   10.74 +}
   10.75 +
   10.76 +void
   10.77 +SDL_EGL_UnloadLibrary(_THIS)
   10.78 +{
   10.79 +    if ((_this->egl_data) && (_this->gl_config.driver_loaded)) {
   10.80 +        _this->egl_data->eglTerminate(_this->egl_data->egl_display);
   10.81 +        
   10.82 +        dlclose(_this->gl_config.dll_handle);
   10.83 +        dlclose(_this->egl_data->egl_dll_handle);
   10.84 +        
   10.85 +        SDL_free(_this->egl_data);
   10.86 +        _this->egl_data = NULL;
   10.87 +        
   10.88 +        _this->gl_config.dll_handle = NULL;
   10.89 +        _this->gl_config.driver_loaded = 0;
   10.90 +    }
   10.91 +}
   10.92 +
   10.93 +int
   10.94 +SDL_EGL_LoadLibrary(_THIS, const char *path, NativeDisplayType native_display)
   10.95 +{
   10.96 +    void *handle;
   10.97 +    int dlopen_flags;
   10.98 +    
   10.99 +    if (_this->egl_data) {
  10.100 +        return SDL_SetError("OpenGL ES context already created");
  10.101 +    }
  10.102 +    
  10.103 +    /* Unload the old driver and reset the pointers */
  10.104 +    SDL_EGL_UnloadLibrary(_this);
  10.105 +    
  10.106 +    #ifdef RTLD_GLOBAL
  10.107 +    dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
  10.108 +    #else
  10.109 +    dlopen_flags = RTLD_LAZY;
  10.110 +    #endif
  10.111 +    handle = dlopen(path, dlopen_flags);
  10.112 +    /* Catch the case where the application isn't linked with EGL */
  10.113 +    if ((dlsym(handle, "eglChooseConfig") == NULL) && (path == NULL)) {
  10.114 +        
  10.115 +        dlclose(handle);
  10.116 +        path = getenv("SDL_VIDEO_EGL_DRIVER");
  10.117 +        if (path == NULL) {
  10.118 +            path = DEFAULT_EGL;
  10.119 +        }
  10.120 +        handle = dlopen(path, dlopen_flags);
  10.121 +    }
  10.122 +
  10.123 +    if (handle == NULL) {
  10.124 +        return SDL_SetError("Could not load OpenGL ES/EGL library");
  10.125 +    }
  10.126 +    
  10.127 +    _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
  10.128 +    if (!_this->egl_data) {
  10.129 +        return SDL_OutOfMemory();
  10.130 +    }
  10.131 +    
  10.132 +    /* Load new function pointers */
  10.133 +    LOAD_FUNC(eglGetDisplay);
  10.134 +    LOAD_FUNC(eglInitialize);
  10.135 +    LOAD_FUNC(eglTerminate);
  10.136 +    LOAD_FUNC(eglGetProcAddress);
  10.137 +    LOAD_FUNC(eglChooseConfig);
  10.138 +    LOAD_FUNC(eglGetConfigAttrib);
  10.139 +    LOAD_FUNC(eglCreateContext);
  10.140 +    LOAD_FUNC(eglDestroyContext);
  10.141 +    LOAD_FUNC(eglCreateWindowSurface);
  10.142 +    LOAD_FUNC(eglDestroySurface);
  10.143 +    LOAD_FUNC(eglMakeCurrent);
  10.144 +    LOAD_FUNC(eglSwapBuffers);
  10.145 +    LOAD_FUNC(eglSwapInterval);
  10.146 +    LOAD_FUNC(eglWaitNative);
  10.147 +    LOAD_FUNC(eglWaitGL);
  10.148 +    
  10.149 +    _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
  10.150 +    
  10.151 +    if (!_this->egl_data->egl_display) {
  10.152 +        return SDL_SetError("Could not get EGL display");
  10.153 +    }
  10.154 +    
  10.155 +    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
  10.156 +        return SDL_SetError("Could not initialize EGL");
  10.157 +    }
  10.158 +
  10.159 +    _this->egl_data->egl_dll_handle = handle;
  10.160 +
  10.161 +    path = getenv("SDL_VIDEO_GL_DRIVER");
  10.162 +    handle = dlopen(path, dlopen_flags);
  10.163 +    if ((path == NULL) | (handle == NULL)) {
  10.164 +      if (_this->gl_config.major_version > 1) {
  10.165 +          path = DEFAULT_OGL_ES2;
  10.166 +          handle = dlopen(path, dlopen_flags);
  10.167 +      } else {
  10.168 +          path = DEFAULT_OGL_ES;
  10.169 +          handle = dlopen(path, dlopen_flags);
  10.170 +          if (handle == NULL) {
  10.171 +              path = DEFAULT_OGL_ES_PVR;
  10.172 +              handle = dlopen(path, dlopen_flags);
  10.173 +          }
  10.174 +      }
  10.175 +    }
  10.176 +
  10.177 +    if (handle == NULL) {
  10.178 +      return SDL_SetError("Could not initialize OpenGL ES library");
  10.179 +    }
  10.180 +
  10.181 +    _this->gl_config.dll_handle = handle;
  10.182 +    _this->gl_config.driver_loaded = 1;
  10.183 +
  10.184 +    if (path) {
  10.185 +      strncpy(_this->gl_config.driver_path, path,
  10.186 +              sizeof(_this->gl_config.driver_path) - 1);
  10.187 +    } else {
  10.188 +      strcpy(_this->gl_config.driver_path, "");
  10.189 +    }
  10.190 +    
  10.191 +    /* We need to select a config here to satisfy some video backends such as X11 */
  10.192 +    SDL_EGL_ChooseConfig(_this);
  10.193 +    
  10.194 +    return 0;
  10.195 +}
  10.196 +
  10.197 +int
  10.198 +SDL_EGL_ChooseConfig(_THIS) 
  10.199 +{
  10.200 +    /* 64 seems nice. */
  10.201 +    EGLint attribs[64];
  10.202 +    EGLint found_configs = 0;
  10.203 +    int i;
  10.204 +    
  10.205 +    if (!_this->egl_data) {
  10.206 +        /* The EGL library wasn't loaded, SDL_GetError() should have info */
  10.207 +        return -1;
  10.208 +    }
  10.209 +  
  10.210 +    /* Get a valid EGL configuration */
  10.211 +    i = 0;
  10.212 +    attribs[i++] = EGL_RED_SIZE;
  10.213 +    attribs[i++] = _this->gl_config.red_size;
  10.214 +    attribs[i++] = EGL_GREEN_SIZE;
  10.215 +    attribs[i++] = _this->gl_config.green_size;
  10.216 +    attribs[i++] = EGL_BLUE_SIZE;
  10.217 +    attribs[i++] = _this->gl_config.blue_size;
  10.218 +    
  10.219 +    if (_this->gl_config.alpha_size) {
  10.220 +        attribs[i++] = EGL_ALPHA_SIZE;
  10.221 +        attribs[i++] = _this->gl_config.alpha_size;
  10.222 +    }
  10.223 +    
  10.224 +    if (_this->gl_config.buffer_size) {
  10.225 +        attribs[i++] = EGL_BUFFER_SIZE;
  10.226 +        attribs[i++] = _this->gl_config.buffer_size;
  10.227 +    }
  10.228 +    
  10.229 +    attribs[i++] = EGL_DEPTH_SIZE;
  10.230 +    attribs[i++] = _this->gl_config.depth_size;
  10.231 +    
  10.232 +    if (_this->gl_config.stencil_size) {
  10.233 +        attribs[i++] = EGL_STENCIL_SIZE;
  10.234 +        attribs[i++] = _this->gl_config.stencil_size;
  10.235 +    }
  10.236 +    
  10.237 +    if (_this->gl_config.multisamplebuffers) {
  10.238 +        attribs[i++] = EGL_SAMPLE_BUFFERS;
  10.239 +        attribs[i++] = _this->gl_config.multisamplebuffers;
  10.240 +    }
  10.241 +    
  10.242 +    if (_this->gl_config.multisamplesamples) {
  10.243 +        attribs[i++] = EGL_SAMPLES;
  10.244 +        attribs[i++] = _this->gl_config.multisamplesamples;
  10.245 +    }
  10.246 +    
  10.247 +    attribs[i++] = EGL_RENDERABLE_TYPE;
  10.248 +    if (_this->gl_config.major_version == 2) {
  10.249 +        attribs[i++] = EGL_OPENGL_ES2_BIT;
  10.250 +    } else {
  10.251 +        attribs[i++] = EGL_OPENGL_ES_BIT;
  10.252 +    }
  10.253 +    
  10.254 +    attribs[i++] = EGL_NONE;
  10.255 +    
  10.256 +    if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
  10.257 +        attribs,
  10.258 +        &_this->egl_data->egl_config, 1,
  10.259 +        &found_configs) == EGL_FALSE ||
  10.260 +        found_configs == 0) {
  10.261 +        return SDL_SetError("Couldn't find matching EGL config");
  10.262 +    }
  10.263 +    
  10.264 +    return 0;
  10.265 +}
  10.266 +
  10.267 +SDL_GLContext
  10.268 +SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
  10.269 +{
  10.270 +    EGLint context_attrib_list[] = {
  10.271 +        EGL_CONTEXT_CLIENT_VERSION,
  10.272 +        1,
  10.273 +        EGL_NONE
  10.274 +    };
  10.275 +    
  10.276 +    EGLContext egl_context;
  10.277 +    
  10.278 +    if (!_this->egl_data) {
  10.279 +        /* The EGL library wasn't loaded, SDL_GetError() should have info */
  10.280 +        return NULL;
  10.281 +    }
  10.282 +    
  10.283 +    if (_this->gl_config.major_version) {
  10.284 +        context_attrib_list[1] = _this->gl_config.major_version;
  10.285 +    }
  10.286 +
  10.287 +    egl_context =
  10.288 +    _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
  10.289 +                                      _this->egl_data->egl_config,
  10.290 +                                      EGL_NO_CONTEXT, context_attrib_list);
  10.291 +    
  10.292 +    if (egl_context == EGL_NO_CONTEXT) {
  10.293 +        SDL_SetError("Could not create EGL context");
  10.294 +        return NULL;
  10.295 +    }
  10.296 +    
  10.297 +    _this->egl_data->egl_swapinterval = 0;
  10.298 +    
  10.299 +    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
  10.300 +        SDL_EGL_DeleteContext(_this, egl_context);
  10.301 +        SDL_SetError("Could not make EGL context current");
  10.302 +        return NULL;
  10.303 +    }
  10.304 +  
  10.305 +    return (SDL_GLContext) egl_context;
  10.306 +}
  10.307 +
  10.308 +int
  10.309 +SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
  10.310 +{
  10.311 +    if (!_this->egl_data) {
  10.312 +        return SDL_SetError("OpenGL not initialized");
  10.313 +    }
  10.314 +    
  10.315 +    EGLContext egl_context = (EGLContext) context;
  10.316 +    
  10.317 +    /* The android emulator crashes badly if you try to eglMakeCurrent 
  10.318 +     * with a valid context and invalid surface, so we have to check for both here.
  10.319 +     */
  10.320 +    if (!egl_context || !egl_surface) {
  10.321 +         _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  10.322 +    }
  10.323 +    else {
  10.324 +        if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
  10.325 +            egl_surface, egl_surface, egl_context)) {
  10.326 +            return SDL_SetError("Unable to make EGL context current");
  10.327 +        }
  10.328 +    }
  10.329 +      
  10.330 +    return 0;
  10.331 +}
  10.332 +
  10.333 +int
  10.334 +SDL_EGL_SetSwapInterval(_THIS, int interval)
  10.335 +{
  10.336 +    if (_this->egl_data) {
  10.337 +        return SDL_SetError("OpenGL ES context not active");
  10.338 +    }
  10.339 +    
  10.340 +    EGLBoolean status;
  10.341 +    status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
  10.342 +    if (status == EGL_TRUE) {
  10.343 +        _this->egl_data->egl_swapinterval = interval;
  10.344 +        return 0;
  10.345 +    }
  10.346 +    
  10.347 +    return SDL_SetError("Unable to set the EGL swap interval");
  10.348 +}
  10.349 +
  10.350 +int
  10.351 +SDL_EGL_GetSwapInterval(_THIS)
  10.352 +{
  10.353 +    if (_this->egl_data) {
  10.354 +        return SDL_SetError("OpenGL ES context not active");
  10.355 +    }
  10.356 +    
  10.357 +    return _this->egl_data->egl_swapinterval;
  10.358 +}
  10.359 +
  10.360 +void
  10.361 +SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
  10.362 +{
  10.363 +    _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
  10.364 +}
  10.365 +
  10.366 +void
  10.367 +SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
  10.368 +{
  10.369 +    /* Clean up GLES and EGL */
  10.370 +    if (!_this->egl_data) {
  10.371 +        return;
  10.372 +    }
  10.373 +    
  10.374 +    EGLContext egl_context = (EGLContext) context;
  10.375 +    
  10.376 +    if (!egl_context && egl_context != EGL_NO_CONTEXT) {
  10.377 +        SDL_EGL_MakeCurrent(_this, NULL, NULL);
  10.378 +        _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
  10.379 +    }
  10.380 +        
  10.381 +    /* FIXME: This "crappy fix" comes from the X11 code, 
  10.382 +     * it's required so you can create a GLX context, destroy it and create a EGL one */
  10.383 +    SDL_EGL_UnloadLibrary(_this);
  10.384 +}
  10.385 +
  10.386 +EGLSurface *
  10.387 +SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
  10.388 +{
  10.389 +    return _this->egl_data->eglCreateWindowSurface(
  10.390 +            _this->egl_data->egl_display,
  10.391 +            _this->egl_data->egl_config,
  10.392 +            nw, NULL);
  10.393 +}
  10.394 +
  10.395 +void
  10.396 +SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
  10.397 +{
  10.398 +    if (!_this->egl_data) {
  10.399 +        return;
  10.400 +    }
  10.401 +    
  10.402 +    if (egl_surface != EGL_NO_SURFACE) {
  10.403 +        _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
  10.404 +    }
  10.405 +}
  10.406 +
  10.407 +#endif /* SDL_VIDEO_OPENGL_EGL */
  10.408 +
  10.409 +/* vi: set ts=4 sw=4 expandtab: */
  10.410 +    
  10.411 \ No newline at end of file
    11.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2 +++ b/src/video/SDL_egl.h	Mon Aug 19 16:29:46 2013 -0300
    11.3 @@ -0,0 +1,132 @@
    11.4 +/*
    11.5 +  Simple DirectMedia Layer
    11.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
    11.7 +
    11.8 +  This software is provided 'as-is', without any express or implied
    11.9 +  warranty.  In no event will the authors be held liable for any damages
   11.10 +  arising from the use of this software.
   11.11 +
   11.12 +  Permission is granted to anyone to use this software for any purpose,
   11.13 +  including commercial applications, and to alter it and redistribute it
   11.14 +  freely, subject to the following restrictions:
   11.15 +
   11.16 +  1. The origin of this software must not be misrepresented; you must not
   11.17 +     claim that you wrote the original software. If you use this software
   11.18 +     in a product, an acknowledgment in the product documentation would be
   11.19 +     appreciated but is not required.
   11.20 +  2. Altered source versions must be plainly marked as such, and must not be
   11.21 +     misrepresented as being the original software.
   11.22 +  3. This notice may not be removed or altered from any source distribution.
   11.23 +*/
   11.24 +#include "SDL_config.h"
   11.25 +
   11.26 +#ifndef _SDL_egl_h
   11.27 +#define _SDL_egl_h
   11.28 +
   11.29 +#if SDL_VIDEO_OPENGL_EGL
   11.30 +
   11.31 +#include <EGL/egl.h>
   11.32 +
   11.33 +#include <dlfcn.h>
   11.34 +#if defined(__OpenBSD__) && !defined(__ELF__)
   11.35 +#define dlsym(x,y) dlsym(x, "_" y)
   11.36 +#endif
   11.37 +
   11.38 +#include "SDL_sysvideo.h"
   11.39 +
   11.40 +typedef struct SDL_EGL_VideoData
   11.41 +{
   11.42 +    void *egl_dll_handle;
   11.43 +    EGLDisplay egl_display;
   11.44 +    EGLConfig egl_config;
   11.45 +    int egl_swapinterval;
   11.46 +    
   11.47 +    EGLDisplay(*eglGetDisplay) (NativeDisplayType display);
   11.48 +    EGLBoolean(*eglInitialize) (EGLDisplay dpy, EGLint * major,
   11.49 +                                EGLint * minor);
   11.50 +    EGLBoolean(*eglTerminate) (EGLDisplay dpy);
   11.51 +    
   11.52 +    void *(*eglGetProcAddress) (const char * procName);
   11.53 +    
   11.54 +    EGLBoolean(*eglChooseConfig) (EGLDisplay dpy,
   11.55 +                                  const EGLint * attrib_list,
   11.56 +                                  EGLConfig * configs,
   11.57 +                                  EGLint config_size, EGLint * num_config);
   11.58 +    
   11.59 +    EGLContext(*eglCreateContext) (EGLDisplay dpy,
   11.60 +                                   EGLConfig config,
   11.61 +                                   EGLContext share_list,
   11.62 +                                   const EGLint * attrib_list);
   11.63 +    
   11.64 +    EGLBoolean(*eglDestroyContext) (EGLDisplay dpy, EGLContext ctx);
   11.65 +    
   11.66 +    EGLSurface(*eglCreateWindowSurface) (EGLDisplay dpy,
   11.67 +                                         EGLConfig config,
   11.68 +                                         NativeWindowType window,
   11.69 +                                         const EGLint * attrib_list);
   11.70 +    EGLBoolean(*eglDestroySurface) (EGLDisplay dpy, EGLSurface surface);
   11.71 +    
   11.72 +    EGLBoolean(*eglMakeCurrent) (EGLDisplay dpy, EGLSurface draw,
   11.73 +                                 EGLSurface read, EGLContext ctx);
   11.74 +    
   11.75 +    EGLBoolean(*eglSwapBuffers) (EGLDisplay dpy, EGLSurface draw);
   11.76 +    
   11.77 +    EGLBoolean(*eglSwapInterval) (EGLDisplay dpy, EGLint interval);
   11.78 +    
   11.79 +    const char *(*eglQueryString) (EGLDisplay dpy, EGLint name);
   11.80 +    
   11.81 +    EGLBoolean(*eglGetConfigAttrib) (EGLDisplay dpy, EGLConfig config,
   11.82 +                                     EGLint attribute, EGLint * value);
   11.83 +    
   11.84 +    EGLBoolean(*eglWaitNative) (EGLint  engine);
   11.85 +
   11.86 +    EGLBoolean(*eglWaitGL)(void);   
   11.87 +} SDL_EGL_VideoData;
   11.88 +
   11.89 +/* OpenGLES functions */
   11.90 +extern int SDL_EGL_GetAttribute(_THIS, SDL_GLattr attrib, int *value);
   11.91 +extern int SDL_EGL_LoadLibrary(_THIS, const char *path, NativeDisplayType native_display);
   11.92 +extern void *SDL_EGL_GetProcAddress(_THIS, const char *proc);
   11.93 +extern void SDL_EGL_UnloadLibrary(_THIS);
   11.94 +extern int SDL_EGL_ChooseConfig(_THIS);
   11.95 +extern int SDL_EGL_SetSwapInterval(_THIS, int interval);
   11.96 +extern int SDL_EGL_GetSwapInterval(_THIS);
   11.97 +extern void SDL_EGL_DeleteContext(_THIS, SDL_GLContext context);
   11.98 +extern EGLSurface *SDL_EGL_CreateSurface(_THIS, NativeWindowType nw);
   11.99 +extern void SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface);
  11.100 +
  11.101 +/* These need to be wrapped to get the surface for the window by the platform GLES implementation */
  11.102 +extern SDL_GLContext SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface);
  11.103 +extern int SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context);
  11.104 +extern void SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface);
  11.105 +
  11.106 +/* A few of useful macros */
  11.107 +
  11.108 +#define SDL_EGL_SwapWindow_impl(BACKEND) void \
  11.109 +BACKEND ## _GLES_SwapWindow(_THIS, SDL_Window * window) \
  11.110 +{\
  11.111 +    SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);\
  11.112 +}
  11.113 +
  11.114 +#define SDL_EGL_MakeCurrent_impl(BACKEND) int \
  11.115 +BACKEND ## _GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) \
  11.116 +{\
  11.117 +    if (window && context) { \
  11.118 +        return SDL_EGL_MakeCurrent(_this, ((SDL_WindowData *) window->driverdata)->egl_surface, context); \
  11.119 +    }\
  11.120 +    else {\
  11.121 +        return SDL_EGL_MakeCurrent(_this, NULL, NULL);\
  11.122 +    }\
  11.123 +}
  11.124 +
  11.125 +#define SDL_EGL_CreateContext_impl(BACKEND) SDL_GLContext \
  11.126 +BACKEND ## _GLES_CreateContext(_THIS, SDL_Window * window) \
  11.127 +{\
  11.128 +    return SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);\
  11.129 +}
  11.130 +
  11.131 +#endif /* SDL_VIDEO_OPENGL_EGL */
  11.132 +
  11.133 +#endif /* _SDL_egl_h */
  11.134 +
  11.135 +/* vi: set ts=4 sw=4 expandtab: */
    12.1 --- a/src/video/SDL_sysvideo.h	Mon Aug 19 11:02:44 2013 -0400
    12.2 +++ b/src/video/SDL_sysvideo.h	Mon Aug 19 16:29:46 2013 -0300
    12.3 @@ -313,7 +313,11 @@
    12.4      /* Data private to this driver */
    12.5      void *driverdata;
    12.6      struct SDL_GLDriverData *gl_data;
    12.7 -
    12.8 +    
    12.9 +#if SDL_VIDEO_OPENGL_EGL
   12.10 +    struct SDL_EGL_VideoData *egl_data;
   12.11 +#endif
   12.12 +    
   12.13  #if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
   12.14      struct SDL_PrivateGLESData *gles_data;
   12.15  #endif
    13.1 --- a/src/video/SDL_video.c	Mon Aug 19 11:02:44 2013 -0400
    13.2 +++ b/src/video/SDL_video.c	Mon Aug 19 16:29:46 2013 -0300
    13.3 @@ -487,12 +487,17 @@
    13.4  #elif SDL_VIDEO_OPENGL_ES
    13.5      _this->gl_config.major_version = 1;
    13.6      _this->gl_config.minor_version = 1;
    13.7 +#if SDL_VIDEO_OPENGL_EGL    
    13.8      _this->gl_config.use_egl = 1;
    13.9 +#endif    
   13.10  #elif SDL_VIDEO_OPENGL_ES2
   13.11      _this->gl_config.major_version = 2;
   13.12      _this->gl_config.minor_version = 0;
   13.13 +#if SDL_VIDEO_OPENGL_EGL    
   13.14      _this->gl_config.use_egl = 1;
   13.15  #endif
   13.16 +    
   13.17 +#endif
   13.18      _this->gl_config.flags = 0;
   13.19      _this->gl_config.profile_mask = 0;
   13.20      _this->gl_config.share_with_current_context = 0;
    14.1 --- a/src/video/android/SDL_androidevents.c	Mon Aug 19 11:02:44 2013 -0400
    14.2 +++ b/src/video/android/SDL_androidevents.c	Mon Aug 19 16:29:46 2013 -0300
    14.3 @@ -27,6 +27,32 @@
    14.4  
    14.5  #include "SDL_androidevents.h"
    14.6  #include "SDL_events.h"
    14.7 +#include "SDL_androidwindow.h"
    14.8 +
    14.9 +void android_egl_context_backup();
   14.10 +void android_egl_context_restore();
   14.11 +
   14.12 +void 
   14.13 +android_egl_context_restore() 
   14.14 +{
   14.15 +    SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
   14.16 +    if (SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context) < 0) {
   14.17 +        /* The context is no longer valid, create a new one */
   14.18 +        /* FIXME: Notify the user that the context changed and textures need to be re created*/
   14.19 +        data->egl_context = (EGLContext) SDL_GL_CreateContext(Android_Window);
   14.20 +        SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context);
   14.21 +    }
   14.22 +}
   14.23 +
   14.24 +void 
   14.25 +android_egl_context_backup() 
   14.26 +{
   14.27 +    /* Keep a copy of the EGL Context so we can try to restore it when we resume */
   14.28 +    SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
   14.29 +    data->egl_context = SDL_GL_GetCurrentContext();
   14.30 +    /* We need to do this so the EGLSurface can be freed */
   14.31 +    SDL_GL_MakeCurrent(Android_Window, NULL);
   14.32 +}
   14.33  
   14.34  void
   14.35  Android_PumpEvents(_THIS)
   14.36 @@ -52,13 +78,9 @@
   14.37          if(SDL_SemTryWait(Android_ResumeSem) == 0) {
   14.38  #endif
   14.39              isPaused = 0;
   14.40 -            /* TODO: Should we double check if we are on the same thread as the one that made the original GL context?
   14.41 -             * This call will go through the following chain of calls in Java:
   14.42 -             * SDLActivity::createGLContext -> SDLActivity:: initEGL -> SDLActivity::createEGLSurface -> SDLActivity::createEGLContext
   14.43 -             * SDLActivity::createEGLContext will attempt to restore the GL context first, and if that fails it will create a new one
   14.44 -             * If a new GL context is created, the user needs to restore the textures manually (TODO: notify the user that this happened with a message)
   14.45 -             */
   14.46 -            SDL_GL_CreateContext(Android_Window);
   14.47 +            
   14.48 +            /* Restore the GL Context from here, as this operation is thread dependent */
   14.49 +            android_egl_context_restore();
   14.50          }
   14.51      }
   14.52      else {
   14.53 @@ -70,13 +92,14 @@
   14.54                  isPausing = 1;
   14.55              }
   14.56              else {
   14.57 +                android_egl_context_backup();
   14.58                  isPausing = 0;
   14.59                  isPaused = 1;
   14.60              }
   14.61          }
   14.62  #else
   14.63          if(SDL_SemTryWait(Android_PauseSem) == 0) {
   14.64 -            /* If we fall in here, the system is/was paused */
   14.65 +            android_egl_context_backup();
   14.66              isPaused = 1;
   14.67          }
   14.68  #endif
    15.1 --- a/src/video/android/SDL_androidgl.c	Mon Aug 19 11:02:44 2013 -0400
    15.2 +++ b/src/video/android/SDL_androidgl.c	Mon Aug 19 16:29:46 2013 -0300
    15.3 @@ -25,6 +25,8 @@
    15.4  /* Android SDL video driver implementation */
    15.5  
    15.6  #include "SDL_video.h"
    15.7 +#include "../SDL_egl.h"
    15.8 +#include "SDL_androidwindow.h"
    15.9  
   15.10  #include "SDL_androidvideo.h"
   15.11  #include "../../core/android/SDL_android.h"
   15.12 @@ -33,95 +35,20 @@
   15.13  
   15.14  #include <dlfcn.h>
   15.15  
   15.16 -static void* Android_GLHandle = NULL;
   15.17 +SDL_EGL_CreateContext_impl(Android)
   15.18 +SDL_EGL_MakeCurrent_impl(Android)
   15.19  
   15.20 -/* GL functions */
   15.21 -int
   15.22 -Android_GL_LoadLibrary(_THIS, const char *path)
   15.23 +Android_GLES_SwapWindow(_THIS, SDL_Window * window)
   15.24  {
   15.25 -    if (!Android_GLHandle) {
   15.26 -        Android_GLHandle = dlopen("libGLESv1_CM.so",RTLD_GLOBAL);
   15.27 -        if (!Android_GLHandle) {
   15.28 -            return SDL_SetError("Could not initialize GL ES library\n");
   15.29 -        }
   15.30 -    }
   15.31 -    return 0;
   15.32 -}
   15.33 -
   15.34 -void *
   15.35 -Android_GL_GetProcAddress(_THIS, const char *proc)
   15.36 -{
   15.37 -    /*
   15.38 -       !!! FIXME: this _should_ use eglGetProcAddress(), but it appears to be
   15.39 -       !!! FIXME:  busted on Android at the moment...
   15.40 -       !!! FIXME:  http://code.google.com/p/android/issues/detail?id=7681
   15.41 -       !!! FIXME: ...so revisit this later.  --ryan.
   15.42 -    */
   15.43 -    return dlsym(Android_GLHandle, proc);
   15.44 -}
   15.45 -
   15.46 -void
   15.47 -Android_GL_UnloadLibrary(_THIS)
   15.48 -{
   15.49 -    if(Android_GLHandle) {
   15.50 -        dlclose(Android_GLHandle);
   15.51 -        Android_GLHandle = NULL;
   15.52 -    }
   15.53 -}
   15.54 -
   15.55 -SDL_GLContext
   15.56 -Android_GL_CreateContext(_THIS, SDL_Window * window)
   15.57 -{
   15.58 -    if (!Android_JNI_CreateContext(_this->gl_config.major_version,
   15.59 -                                   _this->gl_config.minor_version,
   15.60 -                                   _this->gl_config.red_size,
   15.61 -                                   _this->gl_config.green_size,
   15.62 -                                   _this->gl_config.blue_size,
   15.63 -                                   _this->gl_config.alpha_size,
   15.64 -                                   _this->gl_config.buffer_size,
   15.65 -                                   _this->gl_config.depth_size,
   15.66 -                                   _this->gl_config.stencil_size,
   15.67 -                                   _this->gl_config.multisamplebuffers,
   15.68 -                                   _this->gl_config.multisamplesamples)) {
   15.69 -        SDL_SetError("Couldn't create OpenGL context - see Android log for details");
   15.70 -        return NULL;
   15.71 -    }
   15.72 -    return (SDL_GLContext)1;
   15.73 +    /* FIXME: These two functions were in the Java code, do we really need them? */
   15.74 +    _this->egl_data->eglWaitNative(EGL_CORE_NATIVE_ENGINE);
   15.75 +    _this->egl_data->eglWaitGL();
   15.76 +    SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
   15.77  }
   15.78  
   15.79  int
   15.80 -Android_GL_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
   15.81 -{
   15.82 -    /* There's only one context, nothing to do... */
   15.83 -    return 0;
   15.84 -}
   15.85 -
   15.86 -int
   15.87 -Android_GL_SetSwapInterval(_THIS, int interval)
   15.88 -{
   15.89 -    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_SetSwapInterval\n");
   15.90 -    return 0;
   15.91 -}
   15.92 -
   15.93 -int
   15.94 -Android_GL_GetSwapInterval(_THIS)
   15.95 -{
   15.96 -    __android_log_print(ANDROID_LOG_INFO, "SDL", "[STUB] GL_GetSwapInterval\n");
   15.97 -    return 0;
   15.98 -}
   15.99 -
  15.100 -void
  15.101 -Android_GL_SwapWindow(_THIS, SDL_Window * window)
  15.102 -{
  15.103 -    Android_JNI_SwapWindow();
  15.104 -}
  15.105 -
  15.106 -void
  15.107 -Android_GL_DeleteContext(_THIS, SDL_GLContext context)
  15.108 -{
  15.109 -    if (context) {
  15.110 -        Android_JNI_DeleteContext();
  15.111 -    }
  15.112 +Android_GLES_LoadLibrary(_THIS, const char *path) {
  15.113 +    return SDL_EGL_LoadLibrary(_this, path, (NativeDisplayType) 0);
  15.114  }
  15.115  
  15.116  #endif /* SDL_VIDEO_DRIVER_ANDROID */
    16.1 --- a/src/video/android/SDL_androidvideo.c	Mon Aug 19 11:02:44 2013 -0400
    16.2 +++ b/src/video/android/SDL_androidvideo.c	Mon Aug 19 16:29:46 2013 -0300
    16.3 @@ -44,17 +44,17 @@
    16.4  static int Android_VideoInit(_THIS);
    16.5  static void Android_VideoQuit(_THIS);
    16.6  
    16.7 +#include "../SDL_egl.h"
    16.8  /* GL functions (SDL_androidgl.c) */
    16.9 -extern int Android_GL_LoadLibrary(_THIS, const char *path);
   16.10 -extern void *Android_GL_GetProcAddress(_THIS, const char *proc);
   16.11 -extern void Android_GL_UnloadLibrary(_THIS);
   16.12 -extern SDL_GLContext Android_GL_CreateContext(_THIS, SDL_Window * window);
   16.13 -extern int Android_GL_MakeCurrent(_THIS, SDL_Window * window,
   16.14 -                              SDL_GLContext context);
   16.15 -extern int Android_GL_SetSwapInterval(_THIS, int interval);
   16.16 -extern int Android_GL_GetSwapInterval(_THIS);
   16.17 -extern void Android_GL_SwapWindow(_THIS, SDL_Window * window);
   16.18 -extern void Android_GL_DeleteContext(_THIS, SDL_GLContext context);
   16.19 +extern SDL_GLContext Android_GLES_CreateContext(_THIS, SDL_Window * window);
   16.20 +extern int Android_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
   16.21 +extern void Android_GLES_SwapWindow(_THIS, SDL_Window * window);
   16.22 +extern int Android_GLES_LoadLibrary(_THIS, const char *path);
   16.23 +#define Android_GLES_GetProcAddress SDL_EGL_GetProcAddress
   16.24 +#define Android_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
   16.25 +#define Android_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
   16.26 +#define Android_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
   16.27 +#define Android_GLES_DeleteContext SDL_EGL_DeleteContext
   16.28  
   16.29  /* Android driver bootstrap functions */
   16.30  
   16.31 @@ -114,15 +114,15 @@
   16.32      device->free = Android_DeleteDevice;
   16.33  
   16.34      /* GL pointers */
   16.35 -    device->GL_LoadLibrary = Android_GL_LoadLibrary;
   16.36 -    device->GL_GetProcAddress = Android_GL_GetProcAddress;
   16.37 -    device->GL_UnloadLibrary = Android_GL_UnloadLibrary;
   16.38 -    device->GL_CreateContext = Android_GL_CreateContext;
   16.39 -    device->GL_MakeCurrent = Android_GL_MakeCurrent;
   16.40 -    device->GL_SetSwapInterval = Android_GL_SetSwapInterval;
   16.41 -    device->GL_GetSwapInterval = Android_GL_GetSwapInterval;
   16.42 -    device->GL_SwapWindow = Android_GL_SwapWindow;
   16.43 -    device->GL_DeleteContext = Android_GL_DeleteContext;
   16.44 +    device->GL_LoadLibrary = Android_GLES_LoadLibrary;
   16.45 +    device->GL_GetProcAddress = Android_GLES_GetProcAddress;
   16.46 +    device->GL_UnloadLibrary = Android_GLES_UnloadLibrary;
   16.47 +    device->GL_CreateContext = Android_GLES_CreateContext;
   16.48 +    device->GL_MakeCurrent = Android_GLES_MakeCurrent;
   16.49 +    device->GL_SetSwapInterval = Android_GLES_SetSwapInterval;
   16.50 +    device->GL_GetSwapInterval = Android_GLES_GetSwapInterval;
   16.51 +    device->GL_SwapWindow = Android_GLES_SwapWindow;
   16.52 +    device->GL_DeleteContext = Android_GLES_DeleteContext;
   16.53  
   16.54      /* Text input */
   16.55      device->StartTextInput = Android_StartTextInput;
    17.1 --- a/src/video/android/SDL_androidwindow.c	Mon Aug 19 11:02:44 2013 -0400
    17.2 +++ b/src/video/android/SDL_androidwindow.c	Mon Aug 19 16:29:46 2013 -0300
    17.3 @@ -29,15 +29,15 @@
    17.4  #include "SDL_androidvideo.h"
    17.5  #include "SDL_androidwindow.h"
    17.6  
    17.7 -#include "../../core/android/SDL_android.h"
    17.8 -
    17.9  int
   17.10  Android_CreateWindow(_THIS, SDL_Window * window)
   17.11  {
   17.12 +    SDL_WindowData *data;
   17.13 +    
   17.14      if (Android_Window) {
   17.15          return SDL_SetError("Android only supports one window");
   17.16      }
   17.17 -    Android_Window = window;
   17.18 +    
   17.19      Android_PauseSem = SDL_CreateSemaphore(0);
   17.20      Android_ResumeSem = SDL_CreateSemaphore(0);
   17.21  
   17.22 @@ -56,7 +56,29 @@
   17.23      /* One window, it always has focus */
   17.24      SDL_SetMouseFocus(window);
   17.25      SDL_SetKeyboardFocus(window);
   17.26 +    
   17.27 +    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
   17.28 +    if (!data) {
   17.29 +        return SDL_OutOfMemory();
   17.30 +    }
   17.31 +    
   17.32 +    data->native_window = Android_JNI_GetNativeWindow();
   17.33 +    
   17.34 +    if (!data->native_window) {
   17.35 +        SDL_free(data);
   17.36 +        return SDL_SetError("Could not fetch native window");
   17.37 +    }
   17.38 +    
   17.39 +    data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
   17.40  
   17.41 +    if (data->egl_surface == EGL_NO_SURFACE) {
   17.42 +        SDL_free(data);
   17.43 +        return SDL_SetError("Could not create GLES window surface");
   17.44 +    }
   17.45 +
   17.46 +    window->driverdata = data;
   17.47 +    Android_Window = window;
   17.48 +    
   17.49      return 0;
   17.50  }
   17.51  
   17.52 @@ -69,12 +91,23 @@
   17.53  void
   17.54  Android_DestroyWindow(_THIS, SDL_Window * window)
   17.55  {
   17.56 +    SDL_WindowData *data;
   17.57 +    
   17.58      if (window == Android_Window) {
   17.59          Android_Window = NULL;
   17.60          if (Android_PauseSem) SDL_DestroySemaphore(Android_PauseSem);
   17.61          if (Android_ResumeSem) SDL_DestroySemaphore(Android_ResumeSem);
   17.62          Android_PauseSem = NULL;
   17.63          Android_ResumeSem = NULL;
   17.64 +        
   17.65 +        if(window->driverdata) {
   17.66 +            data = (SDL_WindowData *) window->driverdata;
   17.67 +            if(data->native_window) {
   17.68 +                ANativeWindow_release(data->native_window);
   17.69 +            }
   17.70 +            SDL_free(window->driverdata);
   17.71 +            window->driverdata = NULL;
   17.72 +        }
   17.73      }
   17.74  }
   17.75  
    18.1 --- a/src/video/android/SDL_androidwindow.h	Mon Aug 19 11:02:44 2013 -0400
    18.2 +++ b/src/video/android/SDL_androidwindow.h	Mon Aug 19 16:29:46 2013 -0300
    18.3 @@ -23,10 +23,21 @@
    18.4  #ifndef _SDL_androidwindow_h
    18.5  #define _SDL_androidwindow_h
    18.6  
    18.7 +#include "../../core/android/SDL_android.h"
    18.8 +#include "../SDL_egl.h"
    18.9 +
   18.10  extern int Android_CreateWindow(_THIS, SDL_Window * window);
   18.11  extern void Android_SetWindowTitle(_THIS, SDL_Window * window);
   18.12  extern void Android_DestroyWindow(_THIS, SDL_Window * window);
   18.13  
   18.14 +typedef struct
   18.15 +{
   18.16 +    EGLSurface egl_surface;
   18.17 +    EGLContext egl_context; /* We use this to preserve the context when losing focus */
   18.18 +    ANativeWindow* native_window;
   18.19 +    
   18.20 +} SDL_WindowData;
   18.21 +
   18.22  #endif /* _SDL_androidwindow_h */
   18.23  
   18.24  /* vi: set ts=4 sw=4 expandtab: */
    19.1 --- a/src/video/x11/SDL_x11opengl.c	Mon Aug 19 11:02:44 2013 -0400
    19.2 +++ b/src/video/x11/SDL_x11opengl.c	Mon Aug 19 16:29:46 2013 -0300
    19.3 @@ -140,25 +140,6 @@
    19.4          return SDL_SetError("OpenGL context already created");
    19.5      }
    19.6  
    19.7 -    /* If SDL_GL_CONTEXT_EGL has been changed to 1, switch over to X11_GLES functions  */
    19.8 -    if (_this->gl_config.use_egl == 1) {
    19.9 -#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
   19.10 -        _this->GL_LoadLibrary = X11_GLES_LoadLibrary;
   19.11 -        _this->GL_GetProcAddress = X11_GLES_GetProcAddress;
   19.12 -        _this->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
   19.13 -        _this->GL_CreateContext = X11_GLES_CreateContext;
   19.14 -        _this->GL_MakeCurrent = X11_GLES_MakeCurrent;
   19.15 -        _this->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
   19.16 -        _this->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
   19.17 -        _this->GL_SwapWindow = X11_GLES_SwapWindow;
   19.18 -        _this->GL_DeleteContext = X11_GLES_DeleteContext;
   19.19 -        return X11_GLES_LoadLibrary(_this, path);
   19.20 -#else
   19.21 -        return SDL_SetError("SDL not configured with OpenGL ES/EGL support");
   19.22 -#endif
   19.23 -    }
   19.24 -
   19.25 -
   19.26      /* Load the OpenGL library */
   19.27      if (path == NULL) {
   19.28          path = SDL_getenv("SDL_OPENGL_LIBRARY");
   19.29 @@ -228,6 +209,38 @@
   19.30  
   19.31      /* Initialize extensions */
   19.32      X11_GL_InitExtensions(_this);
   19.33 +    
   19.34 +    /* If SDL_GL_CONTEXT_EGL has been changed to 1, and there's 
   19.35 +     * no GLX_EXT_create_context_es2_profile extension switch over to X11_GLES functions  */
   19.36 +    if (_this->gl_config.use_egl == 1) {
   19.37 +        if (_this->gl_data->HAS_GLX_EXT_create_context_es2_profile) {
   19.38 +            /* We cheat a little bit here by using GLX instead of EGL 
   19.39 +             * to improve our chances of getting hardware acceleration */
   19.40 +            _this->gl_config.use_egl = 0;
   19.41 +            _this->gl_config.profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
   19.42 +        } else {
   19.43 +#if SDL_VIDEO_OPENGL_EGL
   19.44 +            X11_GL_UnloadLibrary(_this);
   19.45 +            /* Better avoid conflicts! */
   19.46 +            if (_this->gl_config.dll_handle != NULL ) {
   19.47 +                GL_UnloadObject(_this->gl_config.dll_handle);
   19.48 +                _this->gl_config.dll_handle = NULL;
   19.49 +            }
   19.50 +            _this->GL_LoadLibrary = X11_GLES_LoadLibrary;
   19.51 +            _this->GL_GetProcAddress = X11_GLES_GetProcAddress;
   19.52 +            _this->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
   19.53 +            _this->GL_CreateContext = X11_GLES_CreateContext;
   19.54 +            _this->GL_MakeCurrent = X11_GLES_MakeCurrent;
   19.55 +            _this->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
   19.56 +            _this->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
   19.57 +            _this->GL_SwapWindow = X11_GLES_SwapWindow;
   19.58 +            _this->GL_DeleteContext = X11_GLES_DeleteContext;
   19.59 +            return X11_GLES_LoadLibrary(_this, NULL);
   19.60 +#else
   19.61 +            return SDL_SetError("SDL not configured with EGL support");
   19.62 +#endif
   19.63 +        }
   19.64 +    }
   19.65  
   19.66      return 0;
   19.67  }
   19.68 @@ -369,6 +382,11 @@
   19.69      if (HasExtension("GLX_EXT_visual_info", extensions)) {
   19.70          _this->gl_data->HAS_GLX_EXT_visual_info = SDL_TRUE;
   19.71      }
   19.72 +    
   19.73 +    /* Check for GLX_EXT_create_context_es2_profile */
   19.74 +    if (HasExtension("GLX_EXT_create_context_es2_profile", extensions)) {
   19.75 +        _this->gl_data->HAS_GLX_EXT_create_context_es2_profile = SDL_TRUE;
   19.76 +    }
   19.77  
   19.78      if (context) {
   19.79          _this->gl_data->glXMakeCurrent(display, None, NULL);
    20.1 --- a/src/video/x11/SDL_x11opengl.h	Mon Aug 19 11:02:44 2013 -0400
    20.2 +++ b/src/video/x11/SDL_x11opengl.h	Mon Aug 19 16:29:46 2013 -0300
    20.3 @@ -34,6 +34,7 @@
    20.4      SDL_bool HAS_GLX_EXT_visual_rating;
    20.5      SDL_bool HAS_GLX_EXT_visual_info;
    20.6      SDL_bool HAS_GLX_EXT_swap_control_tear;
    20.7 +    SDL_bool HAS_GLX_EXT_create_context_es2_profile;
    20.8  
    20.9      Bool (*glXQueryExtension) (Display*,int*,int*);
   20.10      void *(*glXGetProcAddress) (const GLubyte*);
    21.1 --- a/src/video/x11/SDL_x11opengles.c	Mon Aug 19 11:02:44 2013 -0400
    21.2 +++ b/src/video/x11/SDL_x11opengles.c	Mon Aug 19 16:29:46 2013 -0300
    21.3 @@ -20,86 +20,22 @@
    21.4  */
    21.5  #include "SDL_config.h"
    21.6  
    21.7 -#if SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_ES
    21.8 +#if SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_EGL
    21.9  
   21.10  #include "SDL_x11video.h"
   21.11  #include "SDL_x11opengles.h"
   21.12  #include "SDL_x11opengl.h"
   21.13  
   21.14 -#define DEFAULT_EGL "libEGL.so"
   21.15 -#define DEFAULT_OGL_ES2 "libGLESv2.so"
   21.16 -#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
   21.17 -#define DEFAULT_OGL_ES "libGLESv1_CM.so"
   21.18 -
   21.19 -#define LOAD_FUNC(NAME) \
   21.20 -    *((void**)&_this->gles_data->NAME) = dlsym(handle, #NAME); \
   21.21 -    if (!_this->gles_data->NAME) \
   21.22 -    { \
   21.23 -        return SDL_SetError("Could not retrieve EGL function " #NAME); \
   21.24 -    }
   21.25 -
   21.26 -/* GLES implementation of SDL OpenGL support */
   21.27 -
   21.28 -void *
   21.29 -X11_GLES_GetProcAddress(_THIS, const char *proc)
   21.30 -{
   21.31 -    static char procname[1024];
   21.32 -    void *handle;
   21.33 -    void *retval;
   21.34 -
   21.35 -    handle = _this->gles_data->egl_dll_handle;
   21.36 -    if (_this->gles_data->eglGetProcAddress) {
   21.37 -        retval = _this->gles_data->eglGetProcAddress(proc);
   21.38 -        if (retval) {
   21.39 -            return retval;
   21.40 -        }
   21.41 -    }
   21.42 -
   21.43 -    handle = _this->gl_config.dll_handle;
   21.44 -#if defined(__OpenBSD__) && !defined(__ELF__)
   21.45 -#undef dlsym(x,y);
   21.46 -#endif
   21.47 -    retval = dlsym(handle, proc);
   21.48 -    if (!retval && strlen(proc) <= 1022) {
   21.49 -        procname[0] = '_';
   21.50 -        strcpy(procname + 1, proc);
   21.51 -        retval = dlsym(handle, procname);
   21.52 -    }
   21.53 -    return retval;
   21.54 -}
   21.55 -
   21.56 -void
   21.57 -X11_GLES_UnloadLibrary(_THIS)
   21.58 -{
   21.59 -    if ((_this->gles_data) && (_this->gl_config.driver_loaded)) {
   21.60 -        _this->gles_data->eglTerminate(_this->gles_data->egl_display);
   21.61 -
   21.62 -        dlclose(_this->gl_config.dll_handle);
   21.63 -        dlclose(_this->gles_data->egl_dll_handle);
   21.64 -
   21.65 -        SDL_free(_this->gles_data);
   21.66 -        _this->gles_data = NULL;
   21.67 -
   21.68 -        _this->gl_config.dll_handle = NULL;
   21.69 -        _this->gl_config.driver_loaded = 0;
   21.70 -    }
   21.71 -}
   21.72 +/* EGL implementation of SDL OpenGL support */
   21.73  
   21.74  int
   21.75 -X11_GLES_LoadLibrary(_THIS, const char *path)
   21.76 -{
   21.77 -    void *handle;
   21.78 -    int dlopen_flags;
   21.79 -
   21.80 +X11_GLES_LoadLibrary(_THIS, const char *path) {
   21.81 +        
   21.82      SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   21.83  
   21.84 -    if (_this->gles_data) {
   21.85 -        return SDL_SetError("OpenGL ES context already created");
   21.86 -    }
   21.87 -
   21.88      /* If SDL_GL_CONTEXT_EGL has been changed to 0, switch over to X11_GL functions  */
   21.89      if (_this->gl_config.use_egl == 0) {
   21.90 -#if SDL_VIDEO_OPENGL_GLX
   21.91 +        #if SDL_VIDEO_OPENGL_GLX
   21.92          _this->GL_LoadLibrary = X11_GL_LoadLibrary;
   21.93          _this->GL_GetProcAddress = X11_GL_GetProcAddress;
   21.94          _this->GL_UnloadLibrary = X11_GL_UnloadLibrary;
   21.95 @@ -110,331 +46,63 @@
   21.96          _this->GL_SwapWindow = X11_GL_SwapWindow;
   21.97          _this->GL_DeleteContext = X11_GL_DeleteContext;
   21.98          return X11_GL_LoadLibrary(_this, path);
   21.99 -#else
  21.100 +        #else
  21.101          return SDL_SetError("SDL not configured with OpenGL/GLX support");
  21.102 -#endif
  21.103 +        #endif
  21.104      }
  21.105 -
  21.106 -#ifdef RTLD_GLOBAL
  21.107 -    dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
  21.108 -#else
  21.109 -    dlopen_flags = RTLD_LAZY;
  21.110 -#endif
  21.111 -    handle = dlopen(path, dlopen_flags);
  21.112 -    /* Catch the case where the application isn't linked with EGL */
  21.113 -    if ((dlsym(handle, "eglChooseConfig") == NULL) && (path == NULL)) {
  21.114 -
  21.115 -        dlclose(handle);
  21.116 -        path = getenv("SDL_VIDEO_EGL_DRIVER");
  21.117 -        if (path == NULL) {
  21.118 -            path = DEFAULT_EGL;
  21.119 -        }
  21.120 -        handle = dlopen(path, dlopen_flags);
  21.121 -    }
  21.122 -
  21.123 -    if (handle == NULL) {
  21.124 -        return SDL_SetError("Could not load OpenGL ES/EGL library");
  21.125 -    }
  21.126 -
  21.127 -    /* Unload the old driver and reset the pointers */
  21.128 -    X11_GLES_UnloadLibrary(_this);
  21.129 -
  21.130 -    _this->gles_data = (struct SDL_PrivateGLESData *) SDL_calloc(1, sizeof(SDL_PrivateGLESData));
  21.131 -    if (!_this->gles_data) {
  21.132 -        return SDL_OutOfMemory();
  21.133 -    }
  21.134 -
  21.135 -    /* Load new function pointers */
  21.136 -    LOAD_FUNC(eglGetDisplay);
  21.137 -    LOAD_FUNC(eglInitialize);
  21.138 -    LOAD_FUNC(eglTerminate);
  21.139 -    LOAD_FUNC(eglGetProcAddress);
  21.140 -    LOAD_FUNC(eglChooseConfig);
  21.141 -    LOAD_FUNC(eglGetConfigAttrib);
  21.142 -    LOAD_FUNC(eglCreateContext);
  21.143 -    LOAD_FUNC(eglDestroyContext);
  21.144 -    LOAD_FUNC(eglCreateWindowSurface);
  21.145 -    LOAD_FUNC(eglDestroySurface);
  21.146 -    LOAD_FUNC(eglMakeCurrent);
  21.147 -    LOAD_FUNC(eglSwapBuffers);
  21.148 -    LOAD_FUNC(eglSwapInterval);
  21.149 -
  21.150 -    _this->gles_data->egl_display =
  21.151 -        _this->gles_data->eglGetDisplay((NativeDisplayType) data->display);
  21.152 -
  21.153 -    if (!_this->gles_data->egl_display) {
  21.154 -        return SDL_SetError("Could not get EGL display");
  21.155 -    }
  21.156 -
  21.157 -    if (_this->gles_data->
  21.158 -        eglInitialize(_this->gles_data->egl_display, NULL,
  21.159 -                      NULL) != EGL_TRUE) {
  21.160 -        return SDL_SetError("Could not initialize EGL");
  21.161 -    }
  21.162 -
  21.163 -    _this->gles_data->egl_dll_handle = handle;
  21.164 -
  21.165 -    path = getenv("SDL_VIDEO_GL_DRIVER");
  21.166 -    handle = dlopen(path, dlopen_flags);
  21.167 -    if ((path == NULL) | (handle == NULL)) {
  21.168 -        if (_this->gl_config.major_version > 1) {
  21.169 -            path = DEFAULT_OGL_ES2;
  21.170 -            handle = dlopen(path, dlopen_flags);
  21.171 -        } else {
  21.172 -            path = DEFAULT_OGL_ES;
  21.173 -            handle = dlopen(path, dlopen_flags);
  21.174 -            if (handle == NULL) {
  21.175 -                path = DEFAULT_OGL_ES_PVR;
  21.176 -                handle = dlopen(path, dlopen_flags);
  21.177 -            }
  21.178 -        }
  21.179 -    }
  21.180 -
  21.181 -    if (handle == NULL) {
  21.182 -        return SDL_SetError("Could not initialize OpenGL ES library");
  21.183 -    }
  21.184 -
  21.185 -    _this->gl_config.dll_handle = handle;
  21.186 -    _this->gl_config.driver_loaded = 1;
  21.187 -
  21.188 -    if (path) {
  21.189 -        strncpy(_this->gl_config.driver_path, path,
  21.190 -                sizeof(_this->gl_config.driver_path) - 1);
  21.191 -    } else {
  21.192 -        strcpy(_this->gl_config.driver_path, "");
  21.193 -    }
  21.194 -    return 0;
  21.195 +    
  21.196 +    return SDL_EGL_LoadLibrary(_this, path, (NativeDisplayType) data->display);
  21.197  }
  21.198  
  21.199  XVisualInfo *
  21.200  X11_GLES_GetVisual(_THIS, Display * display, int screen)
  21.201  {
  21.202 -    /* 64 seems nice. */
  21.203 -    EGLint attribs[64];
  21.204 -    EGLint found_configs = 0;
  21.205 +   
  21.206 +    XVisualInfo *egl_visualinfo = NULL;
  21.207      EGLint visual_id;
  21.208 -    int i;
  21.209 +    XVisualInfo vi_in;
  21.210 +    int out_count;
  21.211  
  21.212 -    if (!_this->gles_data) {
  21.213 +    if (!_this->egl_data) {
  21.214          /* The EGL library wasn't loaded, SDL_GetError() should have info */
  21.215          return NULL;
  21.216      }
  21.217  
  21.218 -    i = 0;
  21.219 -    attribs[i++] = EGL_RED_SIZE;
  21.220 -    attribs[i++] = _this->gl_config.red_size;
  21.221 -    attribs[i++] = EGL_GREEN_SIZE;
  21.222 -    attribs[i++] = _this->gl_config.green_size;
  21.223 -    attribs[i++] = EGL_BLUE_SIZE;
  21.224 -    attribs[i++] = _this->gl_config.blue_size;
  21.225 -
  21.226 -    if (_this->gl_config.alpha_size) {
  21.227 -        attribs[i++] = EGL_ALPHA_SIZE;
  21.228 -        attribs[i++] = _this->gl_config.alpha_size;
  21.229 +    if (_this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
  21.230 +                                            _this->egl_data->egl_config,
  21.231 +                                            EGL_NATIVE_VISUAL_ID,
  21.232 +                                            &visual_id) == EGL_FALSE || !visual_id) {
  21.233 +        /* Use the default visual when all else fails */
  21.234 +        vi_in.screen = screen;
  21.235 +        egl_visualinfo = XGetVisualInfo(display,
  21.236 +                                        VisualScreenMask,
  21.237 +                                        &vi_in, &out_count);
  21.238 +    } else {
  21.239 +        vi_in.screen = screen;
  21.240 +        vi_in.visualid = visual_id;
  21.241 +        egl_visualinfo = XGetVisualInfo(display, VisualScreenMask | VisualIDMask, &vi_in, &out_count);
  21.242      }
  21.243  
  21.244 -    if (_this->gl_config.buffer_size) {
  21.245 -        attribs[i++] = EGL_BUFFER_SIZE;
  21.246 -        attribs[i++] = _this->gl_config.buffer_size;
  21.247 -    }
  21.248 -
  21.249 -    attribs[i++] = EGL_DEPTH_SIZE;
  21.250 -    attribs[i++] = _this->gl_config.depth_size;
  21.251 -
  21.252 -    if (_this->gl_config.stencil_size) {
  21.253 -        attribs[i++] = EGL_STENCIL_SIZE;
  21.254 -        attribs[i++] = _this->gl_config.stencil_size;
  21.255 -    }
  21.256 -
  21.257 -    if (_this->gl_config.multisamplebuffers) {
  21.258 -        attribs[i++] = EGL_SAMPLE_BUFFERS;
  21.259 -        attribs[i++] = _this->gl_config.multisamplebuffers;
  21.260 -    }
  21.261 -
  21.262 -    if (_this->gl_config.multisamplesamples) {
  21.263 -        attribs[i++] = EGL_SAMPLES;
  21.264 -        attribs[i++] = _this->gl_config.multisamplesamples;
  21.265 -    }
  21.266 -
  21.267 -    attribs[i++] = EGL_RENDERABLE_TYPE;
  21.268 -    if (_this->gl_config.major_version == 2) {
  21.269 -        attribs[i++] = EGL_OPENGL_ES2_BIT;
  21.270 -    } else {
  21.271 -        attribs[i++] = EGL_OPENGL_ES_BIT;
  21.272 -    }
  21.273 -
  21.274 -    attribs[i++] = EGL_NONE;
  21.275 -
  21.276 -    if (_this->gles_data->eglChooseConfig(_this->gles_data->egl_display,
  21.277 -                                          attribs,
  21.278 -                                          &_this->gles_data->egl_config, 1,
  21.279 -                                          &found_configs) == EGL_FALSE ||
  21.280 -        found_configs == 0) {
  21.281 -        SDL_SetError("Couldn't find matching EGL config");
  21.282 -        return NULL;
  21.283 -    }
  21.284 -
  21.285 -    if (_this->gles_data->eglGetConfigAttrib(_this->gles_data->egl_display,
  21.286 -                                             _this->gles_data->egl_config,
  21.287 -                                             EGL_NATIVE_VISUAL_ID,
  21.288 -                                             &visual_id) ==
  21.289 -        EGL_FALSE || !visual_id) {
  21.290 -        /* Use the default visual when all else fails */
  21.291 -        XVisualInfo vi_in;
  21.292 -        int out_count;
  21.293 -        vi_in.screen = screen;
  21.294 -
  21.295 -        _this->gles_data->egl_visualinfo = XGetVisualInfo(display,
  21.296 -                                                          VisualScreenMask,
  21.297 -                                                          &vi_in, &out_count);
  21.298 -    } else {
  21.299 -        XVisualInfo vi_in;
  21.300 -        int out_count;
  21.301 -
  21.302 -        vi_in.screen = screen;
  21.303 -        vi_in.visualid = visual_id;
  21.304 -        _this->gles_data->egl_visualinfo = XGetVisualInfo(display,
  21.305 -                                                          VisualScreenMask |
  21.306 -                                                          VisualIDMask,
  21.307 -                                                          &vi_in, &out_count);
  21.308 -    }
  21.309 -
  21.310 -    return _this->gles_data->egl_visualinfo;
  21.311 +    return egl_visualinfo;
  21.312  }
  21.313  
  21.314  SDL_GLContext
  21.315  X11_GLES_CreateContext(_THIS, SDL_Window * window)
  21.316  {
  21.317 -    EGLint context_attrib_list[] = {
  21.318 -        EGL_CONTEXT_CLIENT_VERSION,
  21.319 -        1,
  21.320 -        EGL_NONE
  21.321 -    };
  21.322 -
  21.323 +    SDL_GLContext context;
  21.324      SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  21.325      Display *display = data->videodata->display;
  21.326 -    SDL_GLContext context = (SDL_GLContext)1;
  21.327  
  21.328      XSync(display, False);
  21.329 -
  21.330 -    if (_this->gl_config.major_version) {
  21.331 -        context_attrib_list[1] = _this->gl_config.major_version;
  21.332 -    }
  21.333 -
  21.334 -    _this->gles_data->egl_context =
  21.335 -        _this->gles_data->eglCreateContext(_this->gles_data->egl_display,
  21.336 -                                           _this->gles_data->egl_config,
  21.337 -                                           EGL_NO_CONTEXT, context_attrib_list);
  21.338 +    context = SDL_EGL_CreateContext(_this, data->egl_surface);
  21.339      XSync(display, False);
  21.340  
  21.341 -    if (_this->gles_data->egl_context == EGL_NO_CONTEXT) {
  21.342 -        SDL_SetError("Could not create EGL context");
  21.343 -        return NULL;
  21.344 -    }
  21.345 -
  21.346 -    _this->gles_data->egl_swapinterval = 0;
  21.347 -
  21.348 -    if (X11_GLES_MakeCurrent(_this, window, context) < 0) {
  21.349 -        X11_GLES_DeleteContext(_this, context);
  21.350 -        return NULL;
  21.351 -    }
  21.352 -
  21.353      return context;
  21.354  }
  21.355  
  21.356 -int
  21.357 -X11_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
  21.358 -{
  21.359 -/*
  21.360 -    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  21.361 -    Display *display = data->videodata->display;
  21.362 -*/
  21.363 +SDL_EGL_SwapWindow_impl(X11)
  21.364 +SDL_EGL_MakeCurrent_impl(X11)
  21.365  
  21.366 -    if (!_this->gles_data) {
  21.367 -        return SDL_SetError("OpenGL not initialized");
  21.368 -    }
  21.369 -
  21.370 -    if (!_this->gles_data->eglMakeCurrent(_this->gles_data->egl_display,
  21.371 -                                          _this->gles_data->egl_surface,
  21.372 -                                          _this->gles_data->egl_surface,
  21.373 -                                          _this->gles_data->egl_context)) {
  21.374 -        return SDL_SetError("Unable to make EGL context current");
  21.375 -    }
  21.376 -
  21.377 -/*
  21.378 -    XSync(display, False);
  21.379 -*/
  21.380 -
  21.381 -    return 1;
  21.382 -}
  21.383 -
  21.384 -int
  21.385 -X11_GLES_SetSwapInterval(_THIS, int interval)
  21.386 -{
  21.387 -    if (_this->gles_data) {
  21.388 -        return SDL_SetError("OpenGL ES context not active");
  21.389 -    }
  21.390 -
  21.391 -    EGLBoolean status;
  21.392 -    status = _this->gles_data->eglSwapInterval(_this->gles_data->egl_display, interval);
  21.393 -    if (status == EGL_TRUE) {
  21.394 -        _this->gles_data->egl_swapinterval = interval;
  21.395 -        return 0;
  21.396 -    }
  21.397 -
  21.398 -    return SDL_SetError("Unable to set the EGL swap interval");
  21.399 -}
  21.400 -
  21.401 -int
  21.402 -X11_GLES_GetSwapInterval(_THIS)
  21.403 -{
  21.404 -    if (_this->gles_data) {
  21.405 -        return SDL_SetError("OpenGL ES context not active");
  21.406 -    }
  21.407 -
  21.408 -    return _this->gles_data->egl_swapinterval;
  21.409 -}
  21.410 -
  21.411 -void
  21.412 -X11_GLES_SwapWindow(_THIS, SDL_Window * window)
  21.413 -{
  21.414 -    _this->gles_data->eglSwapBuffers(_this->gles_data->egl_display,
  21.415 -                                     _this->gles_data->egl_surface);
  21.416 -}
  21.417 -
  21.418 -void
  21.419 -X11_GLES_DeleteContext(_THIS, SDL_GLContext context)
  21.420 -{
  21.421 -    /* Clean up GLES and EGL */
  21.422 -    if (!_this->gles_data) {
  21.423 -        return;
  21.424 -    }
  21.425 -
  21.426 -    if (_this->gles_data->egl_context != EGL_NO_CONTEXT ||
  21.427 -        _this->gles_data->egl_surface != EGL_NO_SURFACE) {
  21.428 -        _this->gles_data->eglMakeCurrent(_this->gles_data->egl_display,
  21.429 -                                         EGL_NO_SURFACE, EGL_NO_SURFACE,
  21.430 -                                         EGL_NO_CONTEXT);
  21.431 -
  21.432 -        if (_this->gles_data->egl_context != EGL_NO_CONTEXT) {
  21.433 -            _this->gles_data->eglDestroyContext(_this->gles_data->egl_display,
  21.434 -                                                _this->gles_data->
  21.435 -                                                egl_context);
  21.436 -            _this->gles_data->egl_context = EGL_NO_CONTEXT;
  21.437 -        }
  21.438 -
  21.439 -        if (_this->gles_data->egl_surface != EGL_NO_SURFACE) {
  21.440 -            _this->gles_data->eglDestroySurface(_this->gles_data->egl_display,
  21.441 -                                                _this->gles_data->
  21.442 -                                                egl_surface);
  21.443 -            _this->gles_data->egl_surface = EGL_NO_SURFACE;
  21.444 -        }
  21.445 -    }
  21.446 -
  21.447 -    /* crappy fix */
  21.448 -    X11_GLES_UnloadLibrary(_this);
  21.449 -}
  21.450 -
  21.451 -#endif /* SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_ES */
  21.452 +#endif /* SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_EGL */
  21.453  
  21.454  /* vi: set ts=4 sw=4 expandtab: */
    22.1 --- a/src/video/x11/SDL_x11opengles.h	Mon Aug 19 11:02:44 2013 -0400
    22.2 +++ b/src/video/x11/SDL_x11opengles.h	Mon Aug 19 16:29:46 2013 -0300
    22.3 @@ -23,81 +23,30 @@
    22.4  #ifndef _SDL_x11opengles_h
    22.5  #define _SDL_x11opengles_h
    22.6  
    22.7 -#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
    22.8 -#include <GLES/gl.h>
    22.9 -#include <GLES/egl.h>
   22.10 -#include <dlfcn.h>
   22.11 -#if defined(__OpenBSD__) && !defined(__ELF__)
   22.12 -#define dlsym(x,y) dlsym(x, "_" y)
   22.13 -#endif
   22.14 +#if SDL_VIDEO_OPENGL_EGL
   22.15  
   22.16  #include "../SDL_sysvideo.h"
   22.17 +#include "../SDL_egl.h"
   22.18  
   22.19  typedef struct SDL_PrivateGLESData
   22.20  {
   22.21 -    XVisualInfo *egl_visualinfo;
   22.22 -    void *egl_dll_handle;
   22.23 -    EGLDisplay egl_display;
   22.24 -    EGLContext egl_context;     /* Current GLES context */
   22.25 -    EGLSurface egl_surface;
   22.26 -    EGLConfig egl_config;
   22.27 -    int egl_swapinterval;
   22.28 -
   22.29 -      EGLDisplay(*eglGetDisplay) (NativeDisplayType display);
   22.30 -      EGLBoolean(*eglInitialize) (EGLDisplay dpy, EGLint * major,
   22.31 -                                  EGLint * minor);
   22.32 -      EGLBoolean(*eglTerminate) (EGLDisplay dpy);
   22.33 -
   22.34 -    void *(*eglGetProcAddress) (const char * procName);
   22.35 -
   22.36 -      EGLBoolean(*eglChooseConfig) (EGLDisplay dpy,
   22.37 -                                    const EGLint * attrib_list,
   22.38 -                                    EGLConfig * configs,
   22.39 -                                    EGLint config_size, EGLint * num_config);
   22.40 -
   22.41 -      EGLContext(*eglCreateContext) (EGLDisplay dpy,
   22.42 -                                     EGLConfig config,
   22.43 -                                     EGLContext share_list,
   22.44 -                                     const EGLint * attrib_list);
   22.45 -
   22.46 -      EGLBoolean(*eglDestroyContext) (EGLDisplay dpy, EGLContext ctx);
   22.47 -
   22.48 -      EGLSurface(*eglCreateWindowSurface) (EGLDisplay dpy,
   22.49 -                                           EGLConfig config,
   22.50 -                                           NativeWindowType window,
   22.51 -                                           const EGLint * attrib_list);
   22.52 -      EGLBoolean(*eglDestroySurface) (EGLDisplay dpy, EGLSurface surface);
   22.53 -
   22.54 -      EGLBoolean(*eglMakeCurrent) (EGLDisplay dpy, EGLSurface draw,
   22.55 -                                   EGLSurface read, EGLContext ctx);
   22.56 -
   22.57 -      EGLBoolean(*eglSwapBuffers) (EGLDisplay dpy, EGLSurface draw);
   22.58 -
   22.59 -      EGLBoolean(*eglSwapInterval) (EGLDisplay dpy, EGLint interval);
   22.60 -
   22.61 -    const char *(*eglQueryString) (EGLDisplay dpy, EGLint name);
   22.62 -
   22.63 -      EGLBoolean(*eglGetConfigAttrib) (EGLDisplay dpy, EGLConfig config,
   22.64 -                                       EGLint attribute, EGLint * value);
   22.65 -
   22.66  } SDL_PrivateGLESData;
   22.67  
   22.68  /* OpenGLES functions */
   22.69 +#define X11_GLES_GetAttribute SDL_EGL_GetAttribute
   22.70 +#define X11_GLES_GetProcAddress SDL_EGL_GetProcAddress
   22.71 +#define X11_GLES_UnloadLibrary SDL_EGL_UnloadLibrary
   22.72 +#define X11_GLES_SetSwapInterval SDL_EGL_SetSwapInterval
   22.73 +#define X11_GLES_GetSwapInterval SDL_EGL_GetSwapInterval
   22.74 +#define X11_GLES_DeleteContext SDL_EGL_DeleteContext
   22.75 +
   22.76 +extern int X11_GLES_LoadLibrary(_THIS, const char *path);
   22.77 +extern XVisualInfo *X11_GLES_GetVisual(_THIS, Display * display, int screen);
   22.78  extern SDL_GLContext X11_GLES_CreateContext(_THIS, SDL_Window * window);
   22.79 -extern XVisualInfo *X11_GLES_GetVisual(_THIS, Display * display, int screen);
   22.80 -extern int X11_GLES_MakeCurrent(_THIS, SDL_Window * window,
   22.81 -                                SDL_GLContext context);
   22.82 -extern int X11_GLES_GetAttribute(_THIS, SDL_GLattr attrib, int *value);
   22.83 -extern int X11_GLES_LoadLibrary(_THIS, const char *path);
   22.84 -extern void *X11_GLES_GetProcAddress(_THIS, const char *proc);
   22.85 -extern void X11_GLES_UnloadLibrary(_THIS);
   22.86 +extern void X11_GLES_SwapWindow(_THIS, SDL_Window * window);
   22.87 +extern int X11_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
   22.88  
   22.89 -extern int X11_GLES_SetSwapInterval(_THIS, int interval);
   22.90 -extern int X11_GLES_GetSwapInterval(_THIS);
   22.91 -extern void X11_GLES_SwapWindow(_THIS, SDL_Window * window);
   22.92 -extern void X11_GLES_DeleteContext(_THIS, SDL_GLContext context);
   22.93 -
   22.94 -#endif /* SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2 */
   22.95 +#endif /* SDL_VIDEO_OPENGL_EGL */
   22.96  
   22.97  #endif /* _SDL_x11opengles_h */
   22.98  
    23.1 --- a/src/video/x11/SDL_x11video.c	Mon Aug 19 11:02:44 2013 -0400
    23.2 +++ b/src/video/x11/SDL_x11video.c	Mon Aug 19 16:29:46 2013 -0300
    23.3 @@ -35,7 +35,7 @@
    23.4  #include "SDL_x11touch.h"
    23.5  #include "SDL_x11xinput2.h"
    23.6  
    23.7 -#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
    23.8 +#if SDL_VIDEO_OPENGL_EGL
    23.9  #include "SDL_x11opengles.h"
   23.10  #endif
   23.11  
   23.12 @@ -394,7 +394,7 @@
   23.13      device->GL_GetSwapInterval = X11_GL_GetSwapInterval;
   23.14      device->GL_SwapWindow = X11_GL_SwapWindow;
   23.15      device->GL_DeleteContext = X11_GL_DeleteContext;
   23.16 -#elif SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
   23.17 +#elif SDL_VIDEO_OPENGL_EGL
   23.18      device->GL_LoadLibrary = X11_GLES_LoadLibrary;
   23.19      device->GL_GetProcAddress = X11_GLES_GetProcAddress;
   23.20      device->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
    24.1 --- a/src/video/x11/SDL_x11window.c	Mon Aug 19 11:02:44 2013 -0400
    24.2 +++ b/src/video/x11/SDL_x11window.c	Mon Aug 19 16:29:46 2013 -0300
    24.3 @@ -34,7 +34,7 @@
    24.4  #include "SDL_x11shape.h"
    24.5  #include "SDL_x11xinput2.h"
    24.6  
    24.7 -#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
    24.8 +#if SDL_VIDEO_OPENGL_EGL
    24.9  #include "SDL_x11opengles.h"
   24.10  #endif
   24.11  
   24.12 @@ -363,11 +363,11 @@
   24.13      Atom XdndAware, xdnd_version = 5;
   24.14      Uint32 fevent = 0;
   24.15  
   24.16 -#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
   24.17 +#if SDL_VIDEO_OPENGL_GLX || SDL_VIDEO_OPENGL_EGL
   24.18      if (window->flags & SDL_WINDOW_OPENGL) {
   24.19          XVisualInfo *vinfo;
   24.20  
   24.21 -#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
   24.22 +#if SDL_VIDEO_OPENGL_EGL
   24.23          if (_this->gl_config.use_egl == 1) {
   24.24              vinfo = X11_GLES_GetVisual(_this, display, screen);
   24.25          } else
   24.26 @@ -481,26 +481,6 @@
   24.27      if (!w) {
   24.28          return SDL_SetError("Couldn't create window");
   24.29      }
   24.30 -#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
   24.31 -    if ((window->flags & SDL_WINDOW_OPENGL) && (_this->gl_config.use_egl == 1)) {
   24.32 -        if (!_this->gles_data) {
   24.33 -            XDestroyWindow(display, w);
   24.34 -            return -1;
   24.35 -        }
   24.36 -
   24.37 -        /* Create the GLES window surface */
   24.38 -        _this->gles_data->egl_surface =
   24.39 -            _this->gles_data->eglCreateWindowSurface(_this->gles_data->
   24.40 -                                                 egl_display,
   24.41 -                                                 _this->gles_data->egl_config,
   24.42 -                                                 (NativeWindowType) w, NULL);
   24.43 -
   24.44 -        if (_this->gles_data->egl_surface == EGL_NO_SURFACE) {
   24.45 -            XDestroyWindow(display, w);
   24.46 -            return SDL_SetError("Could not create GLES window surface");
   24.47 -        }
   24.48 -    }
   24.49 -#endif
   24.50  
   24.51      SetWindowBordered(display, screen, w,
   24.52                        (window->flags & SDL_WINDOW_BORDERLESS) == 0);
   24.53 @@ -568,6 +548,24 @@
   24.54          return -1;
   24.55      }
   24.56  
   24.57 +#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
   24.58 +    if ((window->flags & SDL_WINDOW_OPENGL) && (_this->gl_config.use_egl == 1)) {
   24.59 +        if (!_this->egl_data) {
   24.60 +            XDestroyWindow(display, w);
   24.61 +            return -1;
   24.62 +        }
   24.63 +
   24.64 +        /* Create the GLES window surface */
   24.65 +        ((SDL_WindowData *) window->driverdata)->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) w);
   24.66 +
   24.67 +        if (((SDL_WindowData *) window->driverdata)->egl_surface == EGL_NO_SURFACE) {
   24.68 +            XDestroyWindow(display, w);
   24.69 +            return SDL_SetError("Could not create GLES window surface");
   24.70 +        }
   24.71 +    }
   24.72 +#endif
   24.73 +    
   24.74 +
   24.75  #ifdef X_HAVE_UTF8_STRING
   24.76      if (SDL_X11_HAVE_UTF8) {
   24.77          pXGetICValues(((SDL_WindowData *) window->driverdata)->ic,
    25.1 --- a/src/video/x11/SDL_x11window.h	Mon Aug 19 11:02:44 2013 -0400
    25.2 +++ b/src/video/x11/SDL_x11window.h	Mon Aug 19 16:29:46 2013 -0300
    25.3 @@ -30,6 +30,10 @@
    25.4  #define PENDING_FOCUS_IN_TIME   200
    25.5  #define PENDING_FOCUS_OUT_TIME  200
    25.6  
    25.7 +#if SDL_VIDEO_OPENGL_EGL   
    25.8 +#include <EGL/egl.h>
    25.9 +#endif
   25.10 +
   25.11  typedef enum
   25.12  {
   25.13      PENDING_FOCUS_NONE,
   25.14 @@ -59,6 +63,9 @@
   25.15      struct SDL_VideoData *videodata;
   25.16      Atom xdnd_req;
   25.17      Window xdnd_source;
   25.18 +#if SDL_VIDEO_OPENGL_EGL  
   25.19 +    EGLSurface egl_surface;
   25.20 +#endif
   25.21  } SDL_WindowData;
   25.22  
   25.23  extern void X11_SetNetWMState(_THIS, Window xwindow, Uint32 flags);