SDL  2.0
SDL_androidevents.c
Go to the documentation of this file.
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
39 static void ANDROIDAUDIO_ResumeDevices(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
47 static void openslES_ResumeDevices(void) {}
48 static void openslES_PauseDevices(void) {}
49 #endif
50 
51 /* Number of 'type' events in the event queue */
52 static int
53 SDL_NumberOfEvents(Uint32 type)
54 {
56 }
57 
58 static void
59 android_egl_context_restore(SDL_Window *window)
60 {
61  if (window) {
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);
68  event.type = SDL_RENDER_DEVICE_RESET;
70  }
71  data->backup_done = 0;
72  }
73 }
74 
75 static void
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;
82  /* We need to do this so the EGLSurface can be freed */
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
98 {
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) {
107  android_egl_context_backup(Android_Window);
109  }
110 
113 
114  if (SDL_SemWait(Android_ResumeSem) == 0) {
115 
116  videodata->isPaused = 0;
117 
118  /* Android_ResumeSem was signaled */
122 
125 
126  /* Restore the GL Context from here, as this operation is thread dependent */
127  if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
129  android_egl_context_restore(Android_Window);
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) {
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
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) {
174  android_egl_context_backup(Android_Window);
176  }
177 
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 */
193 
196 
197  /* Restore the GL Context from here, as this operation is thread dependent */
198  if (!isContextExternal && !SDL_HasEvent(SDL_QUIT)) {
200  android_egl_context_restore(Android_Window);
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) {
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: */
EGLContext
void * EGLContext
Definition: egl.h:60
SDL_events.h
SDL_VideoDevice::driverdata
void * driverdata
Definition: SDL_sysvideo.h:389
SDL_androidevents.h
SDL_APP_DIDENTERBACKGROUND
@ SDL_APP_DIDENTERBACKGROUND
Definition: SDL_events.h:75
SDL_APP_DIDENTERFOREGROUND
@ SDL_APP_DIDENTERFOREGROUND
Definition: SDL_events.h:83
Android_ActivityMutex
SDL_mutex * Android_ActivityMutex
if
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
Definition: pixman-arm-neon-asm.h:469
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
NULL
#define NULL
Definition: begin_code.h:167
SDL_GL_CreateContext
#define SDL_GL_CreateContext
Definition: SDL_dynapi_overrides.h:559
SDL_WindowData
Definition: SDL_androidwindow.h:39
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_APP_WILLENTERFOREGROUND
@ SDL_APP_WILLENTERFOREGROUND
Definition: SDL_events.h:79
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:75
SDL_SemTryWait
#define SDL_SemTryWait
Definition: SDL_dynapi_overrides.h:267
SDL_GLContext
void * SDL_GLContext
An opaque handle to an OpenGL context.
Definition: SDL_video.h:192
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2652
SDL_androidwindow.h
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
Android_PumpEvents_NonBlocking
void Android_PumpEvents_NonBlocking(_THIS)
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:125
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
SDL_PeepEvents
#define SDL_PeepEvents
Definition: SDL_dynapi_overrides.h:117
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
SDL_APP_WILLENTERBACKGROUND
@ SDL_APP_WILLENTERBACKGROUND
Definition: SDL_events.h:71
SDL_VideoData::isPausing
int isPausing
Definition: SDL_androidvideo.h:40
SDL_GL_GetCurrentContext
#define SDL_GL_GetCurrentContext
Definition: SDL_dynapi_overrides.h:562
SDL_VideoData::isPaused
int isPaused
Definition: SDL_androidvideo.h:39
SDL_WINDOWEVENT_MINIMIZED
@ SDL_WINDOWEVENT_MINIMIZED
Definition: SDL_video.h:158
Android_PauseSem
SDL_sem * Android_PauseSem
ANDROIDAUDIO_PauseDevices
void ANDROIDAUDIO_PauseDevices(void)
Definition: SDL_androidaudio.c:206
Android_PumpEvents_Blocking
void Android_PumpEvents_Blocking(_THIS)
SDL_IsVideoContextExternal
SDL_bool SDL_IsVideoContextExternal(void)
Definition: SDL_video.c:668
SDL_SemWait
#define SDL_SemWait
Definition: SDL_dynapi_overrides.h:266
SDL_WindowData::egl_context
EGLContext egl_context
Definition: SDL_androidwindow.h:41
SDL_RENDER_DEVICE_RESET
@ SDL_RENDER_DEVICE_RESET
Definition: SDL_events.h:155
SDL_GL_MakeCurrent
#define SDL_GL_MakeCurrent
Definition: SDL_dynapi_overrides.h:560
ANDROIDAUDIO_ResumeDevices
void ANDROIDAUDIO_ResumeDevices(void)
Definition: SDL_androidaudio.c:205
SDL_SendWindowEvent
int SDL_SendWindowEvent(SDL_Window *window, Uint8 windowevent, int data1, int data2)
Definition: SDL_windowevents.c:74
SDL_HasEvent
#define SDL_HasEvent
Definition: SDL_dynapi_overrides.h:118
SDL_SemValue
#define SDL_SemValue
Definition: SDL_dynapi_overrides.h:270
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
openslES_ResumeDevices
void openslES_ResumeDevices(void)
SDL_androidkeyboard.h
SDL_Event
General event structure.
Definition: SDL_events.h:559
SDL_WINDOWEVENT_RESTORED
@ SDL_WINDOWEVENT_RESTORED
Definition: SDL_video.h:160
SDL_IsTextInputActive
#define SDL_IsTextInputActive
Definition: SDL_dynapi_overrides.h:227
Android_StartTextInput
void Android_StartTextInput(_THIS)
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_SendAppEvent
int SDL_SendAppEvent(SDL_EventType eventType)
Definition: SDL_events.c:972
type
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
Android_Window
SDL_Window * Android_Window
SDL_PEEKEVENT
@ SDL_PEEKEVENT
Definition: SDL_events.h:617
SDL_VideoData
Definition: SDL_androidvideo.h:37
openslES_PauseDevices
void openslES_PauseDevices(void)
Android_ResumeSem
SDL_sem * Android_ResumeSem
Definition: SDL_androidvideo.h:45