Skip to content

Commit

Permalink
Android: concurrency issues, make sure Activity is in running State w…
Browse files Browse the repository at this point in the history
…hen calling

functions like SDL_CreateWindow, SDL_CreateRenderer, Android_GLES_CreateContext

Bugs 4694, 4681, 4142
  • Loading branch information
1bsyl committed Jun 28, 2019
1 parent cfed0b7 commit 22a2dec
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/core/android/SDL_android.c
Expand Up @@ -24,6 +24,7 @@
#include "SDL_hints.h"
#include "SDL_log.h"
#include "SDL_main.h"
#include "SDL_timer.h"

#ifdef __ANDROID__

Expand Down Expand Up @@ -720,6 +721,25 @@ void Android_ActivityMutex_Unlock() {
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 */
JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativeSetScreenResolution)(
JNIEnv *env, jclass jcls,
Expand Down
1 change: 1 addition & 0 deletions src/core/android/SDL_android.h
Expand Up @@ -133,6 +133,7 @@ SDL_bool SDL_IsDeXMode(void);

void Android_ActivityMutex_Lock(void);
void Android_ActivityMutex_Unlock(void);
void Android_ActivityMutex_Lock_Running(void);

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
Expand Down
14 changes: 14 additions & 0 deletions src/render/SDL_render.c
Expand Up @@ -29,6 +29,9 @@
#include "SDL_sysrender.h"
#include "software/SDL_render_sw_c.h"

#if defined(__ANDROID__)
# include "../core/android/SDL_android.h"
#endif

#define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"

Expand Down Expand Up @@ -837,6 +840,10 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
SDL_bool batching = SDL_TRUE;
const char *hint;

#if defined(__ANDROID__)
Android_ActivityMutex_Lock_Running();
#endif

if (!window) {
SDL_SetError("Invalid window");
goto error;
Expand Down Expand Up @@ -951,9 +958,16 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
"Created renderer: %s", renderer->info.name);

#if defined(__ANDROID__)
Android_ActivityMutex_Unlock();
#endif
return renderer;

error:

#if defined(__ANDROID__)
Android_ActivityMutex_Unlock();
#endif
return NULL;

#else
Expand Down
10 changes: 9 additions & 1 deletion src/video/android/SDL_androidgl.c
Expand Up @@ -49,7 +49,15 @@ Android_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
SDL_GLContext
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
Expand Down
2 changes: 1 addition & 1 deletion src/video/android/SDL_androidwindow.c
Expand Up @@ -42,7 +42,7 @@ Android_CreateWindow(_THIS, SDL_Window * window)
SDL_WindowData *data;
int retval = 0;

SDL_LockMutex(Android_ActivityMutex);
Android_ActivityMutex_Lock_Running();

if (Android_Window) {
retval = SDL_SetError("Android only supports one window");
Expand Down

0 comments on commit 22a2dec

Please sign in to comment.