Skip to content

Commit

Permalink
Fixed bug 4890 - Add hint for SDL that the graphics context is extern…
Browse files Browse the repository at this point in the history
…ally 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)
  • Loading branch information
slouken committed Dec 8, 2019
1 parent 135a905 commit 54748a3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
15 changes: 15 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -164,6 +164,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.
*
Expand Down
1 change: 1 addition & 0 deletions src/video/SDL_sysvideo.h
Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion src/video/SDL_video.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
23 changes: 15 additions & 8 deletions src/video/android/SDL_androidevents.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/video/android/SDL_androidwindow.c
Expand Up @@ -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) {
Expand Down

0 comments on commit 54748a3

Please sign in to comment.