• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 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 
33 void android_egl_context_backup();
34 void android_egl_context_restore();
35 
36 #if SDL_AUDIO_DRIVER_ANDROID
37 void ANDROIDAUDIO_ResumeDevices(void);
38 void ANDROIDAUDIO_PauseDevices(void);
39 #else
ANDROIDAUDIO_ResumeDevices(void)40 static void ANDROIDAUDIO_ResumeDevices(void) {}
ANDROIDAUDIO_PauseDevices(void)41 static void ANDROIDAUDIO_PauseDevices(void) {}
42 #endif
43 
44 void
android_egl_context_restore()45 android_egl_context_restore()
46 {
47     SDL_Event event;
48     SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
49     if (SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context) < 0) {
50         /* The context is no longer valid, create a new one */
51         data->egl_context = (EGLContext) SDL_GL_CreateContext(Android_Window);
52         SDL_GL_MakeCurrent(Android_Window, (SDL_GLContext) data->egl_context);
53         event.type = SDL_RENDER_DEVICE_RESET;
54         SDL_PushEvent(&event);
55     }
56 }
57 
58 void
android_egl_context_backup()59 android_egl_context_backup()
60 {
61     /* Keep a copy of the EGL Context so we can try to restore it when we resume */
62     SDL_WindowData *data = (SDL_WindowData *) Android_Window->driverdata;
63     data->egl_context = SDL_GL_GetCurrentContext();
64     /* We need to do this so the EGLSurface can be freed */
65     SDL_GL_MakeCurrent(Android_Window, NULL);
66 }
67 
68 void
Android_PumpEvents(_THIS)69 Android_PumpEvents(_THIS)
70 {
71     static int isPaused = 0;
72 #if SDL_ANDROID_BLOCK_ON_PAUSE
73     static int isPausing = 0;
74 #endif
75     /* No polling necessary */
76 
77     /*
78      * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
79      * 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.
80      */
81 
82 #if SDL_ANDROID_BLOCK_ON_PAUSE
83     if (isPaused && !isPausing) {
84         /* Make sure this is the last thing we do before pausing */
85         android_egl_context_backup();
86         ANDROIDAUDIO_PauseDevices();
87         if(SDL_SemWait(Android_ResumeSem) == 0) {
88 #else
89     if (isPaused) {
90         if(SDL_SemTryWait(Android_ResumeSem) == 0) {
91 #endif
92             isPaused = 0;
93             ANDROIDAUDIO_ResumeDevices();
94             /* Restore the GL Context from here, as this operation is thread dependent */
95             if (!SDL_HasEvent(SDL_QUIT)) {
96                 android_egl_context_restore();
97             }
98         }
99     }
100     else {
101 #if SDL_ANDROID_BLOCK_ON_PAUSE
102         if( isPausing || SDL_SemTryWait(Android_PauseSem) == 0 ) {
103             /* We've been signaled to pause, but before we block ourselves,
104             we need to make sure that certain key events have reached the app */
105             if (SDL_HasEvent(SDL_WINDOWEVENT) || SDL_HasEvent(SDL_APP_WILLENTERBACKGROUND) || SDL_HasEvent(SDL_APP_DIDENTERBACKGROUND) ) {
106                 isPausing = 1;
107             }
108             else {
109                 isPausing = 0;
110                 isPaused = 1;
111             }
112         }
113 #else
114         if(SDL_SemTryWait(Android_PauseSem) == 0) {
115             android_egl_context_backup();
116             ANDROIDAUDIO_PauseDevices();
117             isPaused = 1;
118         }
119 #endif
120     }
121 }
122 
123 #endif /* SDL_VIDEO_DRIVER_ANDROID */
124 
125 /* vi: set ts=4 sw=4 expandtab: */
126