src/video/android/SDL_androidevents.c
author Sylvain Becker <sylvain.becker@gmail.com>
Thu, 03 Jan 2019 16:22:33 +0100
changeset 12497 9e13f3286831
parent 12496 93771c30420b
child 12498 c2e223dc5da9
permissions -rw-r--r--
Android: prevent the error message from SDL_EGL_CreateSurface() to be masked.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../../SDL_internal.h"
    22 
    23 #if SDL_VIDEO_DRIVER_ANDROID
    24 
    25 /* We're going to do this by default */
    26 #define SDL_ANDROID_BLOCK_ON_PAUSE  1
    27 
    28 #include "SDL_androidevents.h"
    29 #include "SDL_events.h"
    30 #include "SDL_androidwindow.h"
    31 
    32 #if !SDL_AUDIO_DISABLED
    33 /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
    34  * because of THIS redefinition */
    35 extern void ANDROIDAUDIO_ResumeDevices(void);
    36 extern void ANDROIDAUDIO_PauseDevices(void);
    37 #else
    38 static void ANDROIDAUDIO_ResumeDevices(void) {}
    39 static void ANDROIDAUDIO_PauseDevices(void) {}
    40 #endif
    41 
    42 static void
    43 android_egl_context_restore(SDL_Window *window)
    44 {
    45     SDL_Event event;
    46     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    47     if (SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context) < 0) {
    48         /* The context is no longer valid, create a new one */
    49         data->egl_context = (EGLContext) SDL_GL_CreateContext(window);
    50         SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context);
    51         event.type = SDL_RENDER_DEVICE_RESET;
    52         SDL_PushEvent(&event);
    53     }
    54 }
    55 
    56 static void
    57 android_egl_context_backup(SDL_Window *window)
    58 {
    59     /* Keep a copy of the EGL Context so we can try to restore it when we resume */
    60     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    61     data->egl_context = SDL_GL_GetCurrentContext();
    62     /* We need to do this so the EGLSurface can be freed */
    63     SDL_GL_MakeCurrent(window, NULL);
    64 }
    65 
    66 void
    67 Android_PumpEvents(_THIS)
    68 {
    69     static int isPaused = 0;
    70 #if SDL_ANDROID_BLOCK_ON_PAUSE
    71     static int isPausing = 0;
    72 #endif
    73     /* No polling necessary */
    74 
    75     /*
    76      * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
    77      * When the pause semaphore is signaled, if SDL_ANDROID_BLOCK_ON_PAUSE is defined the event loop will block until the resume signal is emitted.
    78      */
    79 
    80 #if SDL_ANDROID_BLOCK_ON_PAUSE
    81     if (isPaused && !isPausing) {
    82         /* Make sure this is the last thing we do before pausing */
    83         SDL_SemWait(Android_ActivitySem);
    84         android_egl_context_backup(Android_Window);
    85         SDL_SemPost(Android_ActivitySem);
    86 
    87         ANDROIDAUDIO_PauseDevices();
    88         if (SDL_SemWait(Android_ResumeSem) == 0) {
    89 #else
    90     if (isPaused) {
    91         if (SDL_SemTryWait(Android_ResumeSem) == 0) {
    92 #endif
    93             isPaused = 0;
    94             ANDROIDAUDIO_ResumeDevices();
    95             /* Restore the GL Context from here, as this operation is thread dependent */
    96             if (!SDL_HasEvent(SDL_QUIT)) {
    97                 SDL_SemWait(Android_ActivitySem);
    98                 android_egl_context_restore(Android_Window);
    99                 SDL_SemPost(Android_ActivitySem);
   100             }
   101         }
   102     }
   103     else {
   104 #if SDL_ANDROID_BLOCK_ON_PAUSE
   105         if (isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
   106             /* We've been signaled to pause, but before we block ourselves, 
   107             we need to make sure that certain key events have reached the app */
   108             if (SDL_HasEvent(SDL_WINDOWEVENT) || SDL_HasEvent(SDL_APP_WILLENTERBACKGROUND) || SDL_HasEvent(SDL_APP_DIDENTERBACKGROUND) ) {
   109                 isPausing = 1;
   110             }
   111             else {
   112                 isPausing = 0;
   113                 isPaused = 1;
   114             }
   115         }
   116 #else
   117         if (SDL_SemTryWait(Android_PauseSem) == 0) {
   118             SDL_SemWait(Android_ActivitySem);
   119             android_egl_context_backup(Android_Window);
   120             SDL_SemPost(Android_ActivitySem);
   121 
   122             ANDROIDAUDIO_PauseDevices();
   123             isPaused = 1;
   124         }
   125 #endif
   126     }
   127 }
   128 
   129 #endif /* SDL_VIDEO_DRIVER_ANDROID */
   130 
   131 /* vi: set ts=4 sw=4 expandtab: */