Restore window OpenGL state if creating an OpenGL renderer fails
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index f8fe0c4..a23d4f0 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -390,6 +390,7 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
GLint value;
Uint32 window_flags;
int profile_mask, major, minor;
+ SDL_bool changed_window = SDL_FALSE;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
@@ -398,32 +399,28 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
window_flags = SDL_GetWindowFlags(window);
if (!(window_flags & SDL_WINDOW_OPENGL) ||
profile_mask == SDL_GL_CONTEXT_PROFILE_ES || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
-
+
+ changed_window = SDL_TRUE;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
- /* Uh oh, better try to put it back... */
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
- SDL_RecreateWindow(window, window_flags);
- return NULL;
+ goto error;
}
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
- return NULL;
+ goto error;
}
data = (GL_RenderData *) SDL_calloc(1, sizeof(*data));
if (!data) {
GL_DestroyRenderer(renderer);
SDL_OutOfMemory();
- return NULL;
+ goto error;
}
renderer->WindowEvent = GL_WindowEvent;
@@ -456,16 +453,16 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
data->context = SDL_GL_CreateContext(window);
if (!data->context) {
GL_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
GL_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
if (GL_LoadFunctions(data) < 0) {
GL_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
#ifdef __MACOSX__
@@ -560,6 +557,16 @@ GL_CreateRenderer(SDL_Window * window, Uint32 flags)
GL_ResetState(renderer);
return renderer;
+
+error:
+ if (changed_window) {
+ /* Uh oh, better try to put it back... */
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
+ SDL_RecreateWindow(window, window_flags);
+ }
+ return NULL;
}
static void
diff --git a/src/render/opengles/SDL_render_gles.c b/src/render/opengles/SDL_render_gles.c
index dcfd41b..90a485b 100644
--- a/src/render/opengles/SDL_render_gles.c
+++ b/src/render/opengles/SDL_render_gles.c
@@ -285,42 +285,39 @@ GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
SDL_Renderer *renderer;
GLES_RenderData *data;
GLint value;
- Uint32 windowFlags;
+ Uint32 window_flags;
int profile_mask, major, minor;
+ SDL_bool changed_window = SDL_FALSE;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
- windowFlags = SDL_GetWindowFlags(window);
- if (!(windowFlags & SDL_WINDOW_OPENGL) ||
+ window_flags = SDL_GetWindowFlags(window);
+ if (!(window_flags & SDL_WINDOW_OPENGL) ||
profile_mask != SDL_GL_CONTEXT_PROFILE_ES || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
+ changed_window = SDL_TRUE;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
- if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
- /* Uh oh, better try to put it back... */
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
- SDL_RecreateWindow(window, windowFlags);
- return NULL;
+ if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
+ goto error;
}
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
- return NULL;
+ goto error;
}
data = (GLES_RenderData *) SDL_calloc(1, sizeof(*data));
if (!data) {
GLES_DestroyRenderer(renderer);
SDL_OutOfMemory();
- return NULL;
+ goto error;
}
renderer->WindowEvent = GLES_WindowEvent;
@@ -351,16 +348,16 @@ GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
data->context = SDL_GL_CreateContext(window);
if (!data->context) {
GLES_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
GLES_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
if (GLES_LoadFunctions(data) < 0) {
GLES_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
if (flags & SDL_RENDERER_PRESENTVSYNC) {
@@ -411,6 +408,16 @@ GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
GLES_ResetState(renderer);
return renderer;
+
+error:
+ if (changed_window) {
+ /* Uh oh, better try to put it back... */
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
+ SDL_RecreateWindow(window, window_flags);
+ }
+ return NULL;
}
static void
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index 2d7c7ba..d872b99 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -1889,30 +1889,27 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
#ifndef ZUNE_HD
GLboolean hasCompiler;
#endif
- Uint32 windowFlags;
+ Uint32 window_flags;
GLint window_framebuffer;
GLint value;
int profile_mask, major, minor;
+ SDL_bool changed_window = SDL_FALSE;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
- windowFlags = SDL_GetWindowFlags(window);
- if (!(windowFlags & SDL_WINDOW_OPENGL) ||
+ window_flags = SDL_GetWindowFlags(window);
+ if (!(window_flags & SDL_WINDOW_OPENGL) ||
profile_mask != SDL_GL_CONTEXT_PROFILE_ES || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
-
+
+ changed_window = SDL_TRUE;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
- if (SDL_RecreateWindow(window, windowFlags | SDL_WINDOW_OPENGL) < 0) {
- /* Uh oh, better try to put it back... */
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
- SDL_RecreateWindow(window, windowFlags);
- return NULL;
+ if (SDL_RecreateWindow(window, window_flags | SDL_WINDOW_OPENGL) < 0) {
+ goto error;
}
}
@@ -1920,14 +1917,14 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
renderer = (SDL_Renderer *)SDL_calloc(1, sizeof(SDL_Renderer));
if (!renderer) {
SDL_OutOfMemory();
- return NULL;
+ goto error;
}
data = (GLES2_DriverContext *)SDL_calloc(1, sizeof(GLES2_DriverContext));
if (!data) {
GLES2_DestroyRenderer(renderer);
SDL_OutOfMemory();
- return NULL;
+ goto error;
}
renderer->info = GLES2_RenderDriver.info;
renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
@@ -1936,19 +1933,18 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
/* Create an OpenGL ES 2.0 context */
data->context = SDL_GL_CreateContext(window);
- if (!data->context)
- {
+ if (!data->context) {
GLES2_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
if (SDL_GL_MakeCurrent(window, data->context) < 0) {
GLES2_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
if (GLES2_LoadFunctions(data) < 0) {
GLES2_DestroyRenderer(renderer);
- return NULL;
+ goto error;
}
#if __WINRT__
@@ -1996,7 +1992,7 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
{
GLES2_DestroyRenderer(renderer);
SDL_OutOfMemory();
- return NULL;
+ goto error;
}
data->shader_format_count = nFormats;
#ifdef ZUNE_HD
@@ -2040,6 +2036,16 @@ GLES2_CreateRenderer(SDL_Window *window, Uint32 flags)
GLES2_ResetState(renderer);
return renderer;
+
+error:
+ if (changed_window) {
+ /* Uh oh, better try to put it back... */
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
+ SDL_RecreateWindow(window, window_flags);
+ }
+ return NULL;
}
#endif /* SDL_VIDEO_RENDER_OGL_ES2 && !SDL_RENDER_DISABLED */
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 2e097d0..dc6cf37 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -1357,6 +1357,7 @@ SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
{
char *title = window->title;
SDL_Surface *icon = window->icon;
+ SDL_bool loaded_opengl = SDL_FALSE;
if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
return SDL_SetError("No OpenGL support in video driver");
@@ -1389,6 +1390,7 @@ SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
if (SDL_GL_LoadLibrary(NULL) < 0) {
return -1;
}
+ loaded_opengl = SDL_TRUE;
} else {
SDL_GL_UnloadLibrary();
}
@@ -1402,8 +1404,9 @@ SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
if (_this->CreateWindow(_this, window) < 0) {
- if ((flags & SDL_WINDOW_OPENGL) && !(window->flags & SDL_WINDOW_OPENGL)) {
+ if (loaded_opengl) {
SDL_GL_UnloadLibrary();
+ window->flags &= ~SDL_WINDOW_OPENGL;
}
return -1;
}