Fixes #2308, recreate window if GL requirements for the renderer are not met
authorGabriel Jacobo <gabomdq@gmail.com>
Tue, 25 Feb 2014 17:42:34 -0300
changeset 8257a1563cbde7a5
parent 8256 bcfb214c2950
child 8258 569354dec4e9
Fixes #2308, recreate window if GL requirements for the renderer are not met

If the window has been created with values for SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_MAJOR_VERSION and SDL_GL_CONTEXT_MINOR_VERSION not matching those
required by the renderer, attempt to recreate the window.
This is needed on platforms where both GL and GLES 1/2 surfaces are supported
by the video backend, requiring that the window be recreated when switching
between context types.
src/render/opengl/SDL_render_gl.c
src/render/opengles/SDL_render_gles.c
src/render/opengles2/SDL_render_gles2.c
     1.1 --- a/src/render/opengl/SDL_render_gl.c	Thu Dec 19 06:01:18 2013 +0900
     1.2 +++ b/src/render/opengl/SDL_render_gl.c	Tue Feb 25 17:42:34 2014 -0300
     1.3 @@ -32,6 +32,8 @@
     1.4  #include <OpenGL/OpenGL.h>
     1.5  #endif
     1.6  
     1.7 +#define RENDERER_CONTEXT_MAJOR 2
     1.8 +#define RENDERER_CONTEXT_MINOR 0
     1.9  
    1.10  /* OpenGL renderer implementation */
    1.11  
    1.12 @@ -381,11 +383,24 @@
    1.13      const char *hint;
    1.14      GLint value;
    1.15      Uint32 window_flags;
    1.16 +    int profile_mask, major, minor;
    1.17 +
    1.18 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
    1.19 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
    1.20 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
    1.21 +    
    1.22 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    1.23 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
    1.24 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
    1.25  
    1.26      window_flags = SDL_GetWindowFlags(window);
    1.27 -    if (!(window_flags & SDL_WINDOW_OPENGL)) {
    1.28 +    if (!(window_flags & SDL_WINDOW_OPENGL) ||
    1.29 +        profile_mask != SDL_GL_CONTEXT_PROFILE_CORE || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
    1.30          if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
    1.31              /* Uh oh, better try to put it back... */
    1.32 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
    1.33 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
    1.34 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
    1.35              SDL_RecreateWindow(window, window_flags);
    1.36              return NULL;
    1.37          }
     2.1 --- a/src/render/opengles/SDL_render_gles.c	Thu Dec 19 06:01:18 2013 +0900
     2.2 +++ b/src/render/opengles/SDL_render_gles.c	Tue Feb 25 17:42:34 2014 -0300
     2.3 @@ -26,6 +26,9 @@
     2.4  #include "SDL_opengles.h"
     2.5  #include "../SDL_sysrender.h"
     2.6  
     2.7 +#define RENDERER_CONTEXT_MAJOR 1
     2.8 +#define RENDERER_CONTEXT_MINOR 1
     2.9 +
    2.10  #if defined(SDL_VIDEO_DRIVER_PANDORA)
    2.11  
    2.12  /* Empty function stub to get OpenGL ES 1.x support without  */
    2.13 @@ -279,24 +282,24 @@
    2.14      GLES_RenderData *data;
    2.15      GLint value;
    2.16      Uint32 windowFlags;
    2.17 -    int profileMask, majorVersion, minorVersion;
    2.18 +    int profile_mask, major, minor;
    2.19  
    2.20 -    SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profileMask);
    2.21 -    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &majorVersion);
    2.22 -    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minorVersion);
    2.23 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
    2.24 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
    2.25 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
    2.26  
    2.27      SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
    2.28 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
    2.29 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
    2.30 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
    2.31 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
    2.32  
    2.33      windowFlags = SDL_GetWindowFlags(window);
    2.34      if (!(windowFlags & SDL_WINDOW_OPENGL) ||
    2.35 -        profileMask != SDL_GL_CONTEXT_PROFILE_ES || majorVersion != 1 || minorVersion != 1) {
    2.36 +        profile_mask != SDL_GL_CONTEXT_PROFILE_ES || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
    2.37          if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
    2.38              /* Uh oh, better try to put it back... */
    2.39 -            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profileMask);
    2.40 -            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, majorVersion);
    2.41 -            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minorVersion);
    2.42 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
    2.43 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
    2.44 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
    2.45              SDL_RecreateWindow(window, windowFlags);
    2.46              return NULL;
    2.47          }
     3.1 --- a/src/render/opengles2/SDL_render_gles2.c	Thu Dec 19 06:01:18 2013 +0900
     3.2 +++ b/src/render/opengles2/SDL_render_gles2.c	Tue Feb 25 17:42:34 2014 -0300
     3.3 @@ -28,6 +28,9 @@
     3.4  #include "../../video/SDL_blit.h"
     3.5  #include "SDL_shaders_gles2.h"
     3.6  
     3.7 +#define RENDERER_CONTEXT_MAJOR 2
     3.8 +#define RENDERER_CONTEXT_MINOR 0
     3.9 +
    3.10  /* Used to re-create the window with OpenGL ES capability */
    3.11  extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
    3.12  
    3.13 @@ -1740,24 +1743,24 @@
    3.14      Uint32 windowFlags;
    3.15      GLint window_framebuffer;
    3.16      GLint value;
    3.17 -    int profileMask, majorVersion, minorVersion;
    3.18 +    int profile_mask, major, minor;
    3.19  
    3.20 -    SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profileMask);
    3.21 -    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &majorVersion);
    3.22 -    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minorVersion);
    3.23 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
    3.24 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
    3.25 +    SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
    3.26  
    3.27      SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
    3.28 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
    3.29 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
    3.30 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
    3.31 +    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
    3.32  
    3.33      windowFlags = SDL_GetWindowFlags(window);
    3.34      if (!(windowFlags & SDL_WINDOW_OPENGL) ||
    3.35 -        profileMask != SDL_GL_CONTEXT_PROFILE_ES || majorVersion != 2 || minorVersion != 0) {
    3.36 +        profile_mask != SDL_GL_CONTEXT_PROFILE_ES || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
    3.37          if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
    3.38              /* Uh oh, better try to put it back... */
    3.39 -            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profileMask);
    3.40 -            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, majorVersion);
    3.41 -            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minorVersion);
    3.42 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
    3.43 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
    3.44 +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
    3.45              SDL_RecreateWindow(window, windowFlags);
    3.46              return NULL;
    3.47          }