Android: concurrency issues, make sure Activity is in running State when calling

functions like SDL_CreateWindow, SDL_CreateRenderer, Android_GLES_CreateContext

Bugs 4694, 4681, 4142
This commit is contained in:
Sylvain Becker 2019-06-28 16:38:42 +02:00
parent cfed0b7738
commit 22a2decf64
5 changed files with 45 additions and 2 deletions

View File

@ -24,6 +24,7 @@
#include "SDL_hints.h" #include "SDL_hints.h"
#include "SDL_log.h" #include "SDL_log.h"
#include "SDL_main.h" #include "SDL_main.h"
#include "SDL_timer.h"
#ifdef __ANDROID__ #ifdef __ANDROID__
@ -720,6 +721,25 @@ void Android_ActivityMutex_Unlock() {
SDL_UnlockMutex(Android_ActivityMutex); SDL_UnlockMutex(Android_ActivityMutex);
} }
/* Lock the Mutex when the Activity is in its 'Running' state */
void Android_ActivityMutex_Lock_Running() {
int pauseSignaled = 0;
int resumeSignaled = 0;
retry:
SDL_LockMutex(Android_ActivityMutex);
pauseSignaled = SDL_SemValue(Android_PauseSem);
resumeSignaled = SDL_SemValue(Android_ResumeSem);
if (pauseSignaled > resumeSignaled) {
SDL_UnlockMutex(Android_ActivityMutex);
SDL_Delay(50);
goto retry;
}
}
/* Set screen resolution */ /* Set screen resolution */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)( JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
JNIEnv *env, jclass jcls, JNIEnv *env, jclass jcls,

View File

@ -133,6 +133,7 @@ SDL_bool SDL_IsDeXMode(void);
void Android_ActivityMutex_Lock(void); void Android_ActivityMutex_Lock(void);
void Android_ActivityMutex_Unlock(void); void Android_ActivityMutex_Unlock(void);
void Android_ActivityMutex_Lock_Running(void);
/* Ends C function definitions when using C++ */ /* Ends C function definitions when using C++ */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -29,6 +29,9 @@
#include "SDL_sysrender.h" #include "SDL_sysrender.h"
#include "software/SDL_render_sw_c.h" #include "software/SDL_render_sw_c.h"
#if defined(__ANDROID__)
# include "../core/android/SDL_android.h"
#endif
#define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData" #define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
@ -837,6 +840,10 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
SDL_bool batching = SDL_TRUE; SDL_bool batching = SDL_TRUE;
const char *hint; const char *hint;
#if defined(__ANDROID__)
Android_ActivityMutex_Lock_Running();
#endif
if (!window) { if (!window) {
SDL_SetError("Invalid window"); SDL_SetError("Invalid window");
goto error; goto error;
@ -951,9 +958,16 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
"Created renderer: %s", renderer->info.name); "Created renderer: %s", renderer->info.name);
#if defined(__ANDROID__)
Android_ActivityMutex_Unlock();
#endif
return renderer; return renderer;
error: error:
#if defined(__ANDROID__)
Android_ActivityMutex_Unlock();
#endif
return NULL; return NULL;
#else #else

View File

@ -49,7 +49,15 @@ Android_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
SDL_GLContext SDL_GLContext
Android_GLES_CreateContext(_THIS, SDL_Window * window) Android_GLES_CreateContext(_THIS, SDL_Window * window)
{ {
return SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface); SDL_GLContext ret;
Android_ActivityMutex_Lock_Running();
ret = SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
SDL_UnlockMutex(Android_ActivityMutex);
return ret;
} }
int int

View File

@ -42,7 +42,7 @@ Android_CreateWindow(_THIS, SDL_Window * window)
SDL_WindowData *data; SDL_WindowData *data;
int retval = 0; int retval = 0;
SDL_LockMutex(Android_ActivityMutex); Android_ActivityMutex_Lock_Running();
if (Android_Window) { if (Android_Window) {
retval = SDL_SetError("Android only supports one window"); retval = SDL_SetError("Android only supports one window");