Android: concurrency issue with egl_surface EGL_BAD_SURFACE - (bug 4142)
authorSylvain Becker <sylvain.becker@gmail.com>
Thu, 03 Jan 2019 23:22:50 +0100
changeset 12499e6130a4cca7a
parent 12498 c2e223dc5da9
child 12500 42a61bf08873
Android: concurrency issue with egl_surface EGL_BAD_SURFACE - (bug 4142)

Occurs when application goes to background:
- Java activity is destroying SurfaceView holder and "egl_surface" (in onNativeSurfaceDestroyed())
- While native thread is in Android_GLES_SwapWindow(), prepared to call SDL_EGL_SwapBuffers()

The error is "call to eglSwapBuffers failed, reporting an error of EGL_BAD_SURFACE"

It an be reproduced easily by adding a SDL_Delay(100) at the begining of SDL_EGL_SwapBuffers(),
and putting the application into background.
src/video/android/SDL_androidgl.c
     1.1 --- a/src/video/android/SDL_androidgl.c	Thu Jan 03 20:18:29 2019 +0100
     1.2 +++ b/src/video/android/SDL_androidgl.c	Thu Jan 03 23:22:50 2019 +0100
     1.3 @@ -42,14 +42,22 @@
     1.4  int
     1.5  Android_GLES_SwapWindow(_THIS, SDL_Window * window)
     1.6  {
     1.7 +    int retval;
     1.8 +
     1.9 +    SDL_LockMutex(Android_ActivityMutex);
    1.10 +
    1.11      /* The following two calls existed in the original Java code
    1.12       * If you happen to have a device that's affected by their removal,
    1.13       * please report to Bugzilla. -- Gabriel
    1.14       */
    1.15 -    
    1.16 +
    1.17      /*_this->egl_data->eglWaitNative(EGL_CORE_NATIVE_ENGINE);
    1.18      _this->egl_data->eglWaitGL();*/
    1.19 -    return SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
    1.20 +    retval = SDL_EGL_SwapBuffers(_this, ((SDL_WindowData *) window->driverdata)->egl_surface);
    1.21 +
    1.22 +    SDL_UnlockMutex(Android_ActivityMutex);
    1.23 +
    1.24 +    return retval;
    1.25  }
    1.26  
    1.27  int