1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 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 #include "SDL_androidevents.h"
26 #include "SDL_events.h"
27 #include "SDL_androidkeyboard.h"
28 #include "SDL_androidwindow.h"
29 #include "../SDL_sysvideo.h"
30 #include "../../events/SDL_events_c.h"
31 
32 /* Can't include sysaudio "../../audio/android/SDL_androidaudio.h"
33  * because of THIS redefinition */
34 
35 #if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_ANDROID
36 extern void ANDROIDAUDIO_ResumeDevices(void);
37 extern void ANDROIDAUDIO_PauseDevices(void);
38 #else
ANDROIDAUDIO_ResumeDevices(void)39 static void ANDROIDAUDIO_ResumeDevices(void) {}
ANDROIDAUDIO_PauseDevices(void)40 static void ANDROIDAUDIO_PauseDevices(void) {}
41 #endif
42 
43 #if !SDL_AUDIO_DISABLED && SDL_AUDIO_DRIVER_OPENSLES
44 extern void openslES_ResumeDevices(void);
45 extern void openslES_PauseDevices(void);
46 #else
openslES_ResumeDevices(void)47 static void openslES_ResumeDevices(void) {}
openslES_PauseDevices(void)48 static void openslES_PauseDevices(void) {}
49 #endif
50 
51 /* Number of 'type' events in the event queue */
52 static int
SDL_NumberOfEvents(Uint32 type)53 SDL_NumberOfEvents(Uint32 type)
54 {
55     return SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, type, type);
56 }
57 
58 static void
android_egl_context_restore(SDL_Window * window)59 android_egl_context_restore(SDL_Window *window)
60 {
61     if (window) {
62         SDL_Event event;
63         SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
64         if (SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context) < 0) {
65             /* The context is no longer valid, create a new one */
66             data->egl_context = (EGLContext) SDL_GL_CreateContext(window);
67             SDL_GL_MakeCurrent(window, (SDL_GLContext) data->egl_context);
68             event.type = SDL_RENDER_DEVICE_RESET;
69             SDL_PushEvent(&event);
70         }
71         data->backup_done = 0;
72     }
73 }
74 
75 static void
android_egl_context_backup(SDL_Window * window)76 android_egl_context_backup(SDL_Window *window)
77 {
78     if (window) {
79         /* Keep a copy of the EGL Context so we can try to restore it when we resume */
80         SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
81         data->egl_context = SDL_GL_GetCurrentContext();
82         /* We need to do this so the EGLSurface can be freed */
83         SDL_GL_MakeCurrent(window, NULL);
84         data->backup_done = 1;
85     }
86 }
87 
88 
89 /*
90  * Android_ResumeSem and Android_PauseSem are signaled from Java_org_libsdl_app_SDLActivity_nativePause and Java_org_libsdl_app_SDLActivity_nativeResume
91  * When the pause semaphore is signaled, if Android_PumpEvents_Blocking is used, the event loop will block until the resume signal is emitted.
92  *
93  * No polling necessary
94  */
95 
96 void
Android_PumpEvents_Blocking(_THIS)97 Android_PumpEvents_Blocking(_THIS)
98 {
99     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
100 
101     if (videodata->isPaused) {
102         SDL_bool isContextExternal = SDL_IsVideoContextExternal();
103 
104         /* Make sure this is the last thing we do before pausing */
105         if (!isContextExternal) {
106             SDL_LockMutex(Android_ActivityMutex);
107             android_egl_context_backup(Android_Window);
108             SDL_UnlockMutex(Android_ActivityMutex);
109         }
110 
111         ANDROIDAUDIO_PauseDevices();
112         openslES_PauseDevices();
113 
114         if (SDL_SemWait(Android_ResumeSem) == 0) {
115 
116             videodata->isPaused = 0;
117 
118             /* Android_ResumeSem was signaled */
119             SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
120             SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
121             SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
122 
123             ANDROIDAUDIO_ResumeDevices();
124             openslES_ResumeDevices();
125 
126             /* Restore the GL Context from here, as this operation is thread dependent */
127             if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
128                 SDL_LockMutex(Android_ActivityMutex);
129                 android_egl_context_restore(Android_Window);
130                 SDL_UnlockMutex(Android_ActivityMutex);
131             }
132 
133             /* Make sure SW Keyboard is restored when an app becomes foreground */
134             if (SDL_IsTextInputActive()) {
135                 Android_StartTextInput(_this); /* Only showTextInput */
136             }
137         }
138     } else {
139         if (videodata->isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
140 
141             /* Android_PauseSem was signaled */
142             if (videodata->isPausing == 0) {
143                 SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
144                 SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
145                 SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
146             }
147 
148             /* We've been signaled to pause (potentially several times), but before we block ourselves,
149              * we need to make sure that the very last event (of the first pause sequence, if several)
150              * has reached the app */
151             if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) {
152                 videodata->isPausing = 1;
153             } else {
154                 videodata->isPausing = 0;
155                 videodata->isPaused = 1;
156             }
157         }
158     }
159 }
160 
161 void
Android_PumpEvents_NonBlocking(_THIS)162 Android_PumpEvents_NonBlocking(_THIS)
163 {
164     SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
165     static int backup_context = 0;
166 
167     if (videodata->isPaused) {
168 
169         SDL_bool isContextExternal = SDL_IsVideoContextExternal();
170         if (backup_context) {
171 
172             if (!isContextExternal) {
173                 SDL_LockMutex(Android_ActivityMutex);
174                 android_egl_context_backup(Android_Window);
175                 SDL_UnlockMutex(Android_ActivityMutex);
176             }
177 
178             ANDROIDAUDIO_PauseDevices();
179             openslES_PauseDevices();
180 
181             backup_context = 0;
182         }
183 
184 
185         if (SDL_SemTryWait(Android_ResumeSem) == 0) {
186 
187             videodata->isPaused = 0;
188 
189             /* Android_ResumeSem was signaled */
190             SDL_SendAppEvent(SDL_APP_WILLENTERFOREGROUND);
191             SDL_SendAppEvent(SDL_APP_DIDENTERFOREGROUND);
192             SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_RESTORED, 0, 0);
193 
194             ANDROIDAUDIO_ResumeDevices();
195             openslES_ResumeDevices();
196 
197             /* Restore the GL Context from here, as this operation is thread dependent */
198             if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
199                 SDL_LockMutex(Android_ActivityMutex);
200                 android_egl_context_restore(Android_Window);
201                 SDL_UnlockMutex(Android_ActivityMutex);
202             }
203 
204             /* Make sure SW Keyboard is restored when an app becomes foreground */
205             if (SDL_IsTextInputActive()) {
206                 Android_StartTextInput(_this); /* Only showTextInput */
207             }
208         }
209     } else {
210         if (videodata->isPausing || SDL_SemTryWait(Android_PauseSem) == 0) {
211 
212             /* Android_PauseSem was signaled */
213             if (videodata->isPausing == 0) {
214                 SDL_SendWindowEvent(Android_Window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
215                 SDL_SendAppEvent(SDL_APP_WILLENTERBACKGROUND);
216                 SDL_SendAppEvent(SDL_APP_DIDENTERBACKGROUND);
217             }
218 
219             /* We've been signaled to pause (potentially several times), but before we block ourselves,
220              * we need to make sure that the very last event (of the first pause sequence, if several)
221              * has reached the app */
222             if (SDL_NumberOfEvents(SDL_APP_DIDENTERBACKGROUND) > SDL_SemValue(Android_PauseSem)) {
223                 videodata->isPausing = 1;
224             } else {
225                 videodata->isPausing = 0;
226                 videodata->isPaused = 1;
227                 backup_context = 1;
228             }
229         }
230     }
231 }
232 
233 #endif /* SDL_VIDEO_DRIVER_ANDROID */
234 
235 /* vi: set ts=4 sw=4 expandtab: */
236