Fixed bug 4890 - Add hint for SDL that the graphics context is externally managed Aaron Barany Add SDL_HINT_VIDEO_EXTERNAL_CONTEXT hint to notify SDL that the graphics context is external. This disables the automatic context save/restore behavior on Android and avoids using OpenGL by default when SDL_WINDOW_VUKLAN isn't set. When the application wishes to manage the OpenGL contexts on Android, this avoids cases where SDL unbinds the context and creates new contexts, which can interfere with the application's operation. When using Vulkan and Metal renderer implementations, this avoids SDL forcing OpenGL to be enabled on certain platforms. While using the SDL_WINDOW_VULKAN flag can be used to achieve the same thing, it also causes Vulkan to be loaded. If the application uses Vulkan directly, this is not necessary, and fails window creation when using Metal due to Vulkan not being present. (assuming MoltenVK isn't installed)
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
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index b8da077..b90c9dc 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -165,6 +165,21 @@ extern "C" {
#define SDL_HINT_VIDEO_ALLOW_SCREENSAVER "SDL_VIDEO_ALLOW_SCREENSAVER"
/**
+ * \brief A variable controlling whether the graphics context is externally managed.
+ *
+ * This variable can be set to the following values:
+ * "0" - SDL will manage graphics contexts that are attached to windows.
+ * "1" - Disable graphics context management on windows.
+ *
+ * By default SDL will manage OpenGL contexts in certain situations. For example, on Android the
+ * context will be automatically saved and restored when pausing the application. Additionally, some
+ * platforms will assume usage of OpenGL if Vulkan isn't used. Setting this to "1" will prevent this
+ * behavior, which is desireable when the application manages the graphics context, such as
+ * an externally managed OpenGL context or attaching a Vulkan surface to the window.
+ */
+#define SDL_HINT_VIDEO_EXTERNAL_CONTEXT "SDL_VIDEO_EXTERNAL_CONTEXT"
+
+/**
* \brief A variable controlling whether the X11 VidMode extension should be used.
*
* This variable can be set to the following values:
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 372cb6c..07bbc3f 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -439,6 +439,7 @@ extern int SDL_GetIndexOfDisplay(SDL_VideoDisplay *display);
extern SDL_VideoDisplay *SDL_GetDisplay(int displayIndex);
extern SDL_VideoDisplay *SDL_GetDisplayForWindow(SDL_Window *window);
extern void *SDL_GetDisplayDriverData( int displayIndex );
+extern SDL_bool SDL_IsVideoContextExternal(void);
extern void SDL_GL_DeduceMaxSupportedESProfile(int* major, int* minor);
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 7cabee3..f675e28 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -664,6 +664,12 @@ SDL_GetDisplayDriverData(int displayIndex)
return _this->displays[displayIndex].driverdata;
}
+SDL_bool
+SDL_IsVideoContextExternal(void)
+{
+ return SDL_GetHintBoolean(SDL_HINT_VIDEO_EXTERNAL_CONTEXT, SDL_FALSE);
+}
+
const char *
SDL_GetDisplayName(int displayIndex)
{
@@ -1437,7 +1443,7 @@ SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
/* Some platforms have OpenGL enabled by default */
#if (SDL_VIDEO_OPENGL && __MACOSX__) || __IPHONEOS__ || __ANDROID__ || __NACL__
- if (!_this->is_dummy && !(flags & SDL_WINDOW_VULKAN)) {
+ if (!_this->is_dummy && !(flags & SDL_WINDOW_VULKAN) && !SDL_IsVideoContextExternal()) {
flags |= SDL_WINDOW_OPENGL;
}
#endif
diff --git a/src/video/android/SDL_androidevents.c b/src/video/android/SDL_androidevents.c
index de560c9..87ad12f 100644
--- a/src/video/android/SDL_androidevents.c
+++ b/src/video/android/SDL_androidevents.c
@@ -26,6 +26,7 @@
#include "SDL_events.h"
#include "SDL_androidkeyboard.h"
#include "SDL_androidwindow.h"
+#include "../SDL_sysvideo.h"
/* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
* because of THIS redefinition */
@@ -95,11 +96,14 @@ Android_PumpEvents_Blocking(_THIS)
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
if (videodata->isPaused) {
+ SDL_bool isContextExternal = SDL_IsVideoContextExternal();
/* Make sure this is the last thing we do before pausing */
- SDL_LockMutex(Android_ActivityMutex);
- android_egl_context_backup(Android_Window);
- SDL_UnlockMutex(Android_ActivityMutex);
+ if (!isContextExternal) {
+ SDL_LockMutex(Android_ActivityMutex);
+ android_egl_context_backup(Android_Window);
+ SDL_UnlockMutex(Android_ActivityMutex);
+ }
ANDROIDAUDIO_PauseDevices();
openslES_PauseDevices();
@@ -112,7 +116,7 @@ Android_PumpEvents_Blocking(_THIS)
openslES_ResumeDevices();
/* Restore the GL Context from here, as this operation is thread dependent */
- if (!SDL_HasEvent(SDL_QUIT)) {
+ if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
SDL_LockMutex(Android_ActivityMutex);
android_egl_context_restore(Android_Window);
SDL_UnlockMutex(Android_ActivityMutex);
@@ -146,11 +150,14 @@ Android_PumpEvents_NonBlocking(_THIS)
if (videodata->isPaused) {
+ SDL_bool isContextExternal = SDL_IsVideoContextExternal();
if (backup_context) {
- SDL_LockMutex(Android_ActivityMutex);
- android_egl_context_backup(Android_Window);
- SDL_UnlockMutex(Android_ActivityMutex);
+ if (!isContextExternal) {
+ SDL_LockMutex(Android_ActivityMutex);
+ android_egl_context_backup(Android_Window);
+ SDL_UnlockMutex(Android_ActivityMutex);
+ }
ANDROIDAUDIO_PauseDevices();
openslES_PauseDevices();
@@ -167,7 +174,7 @@ Android_PumpEvents_NonBlocking(_THIS)
openslES_ResumeDevices();
/* Restore the GL Context from here, as this operation is thread dependent */
- if (!SDL_HasEvent(SDL_QUIT)) {
+ if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
SDL_LockMutex(Android_ActivityMutex);
android_egl_context_restore(Android_Window);
SDL_UnlockMutex(Android_ActivityMutex);
diff --git a/src/video/android/SDL_androidwindow.c b/src/video/android/SDL_androidwindow.c
index 64eff52..53cb6b9 100644
--- a/src/video/android/SDL_androidwindow.c
+++ b/src/video/android/SDL_androidwindow.c
@@ -82,7 +82,7 @@ Android_CreateWindow(_THIS, SDL_Window * window)
/* Do not create EGLSurface for Vulkan window since it will then make the window
incompatible with vkCreateAndroidSurfaceKHR */
- if ((window->flags & SDL_WINDOW_VULKAN) == 0) {
+ if ((window->flags & SDL_WINDOW_OPENGL) != 0) {
data->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType) data->native_window);
if (data->egl_surface == EGL_NO_SURFACE) {