24
24
*/
25
25
26
26
#include " AndroidAppBase.hpp"
27
+ #include " Errors.hpp"
27
28
#include " Timer.hpp"
28
29
29
30
#include < android/sensor.h>
@@ -118,22 +119,44 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
118
119
AndroidAppBase* eng = (AndroidAppBase*)app->userData ;
119
120
switch (cmd)
120
121
{
121
- case APP_CMD_SAVE_STATE:
122
- break ;
123
-
124
122
case APP_CMD_INIT_WINDOW:
125
- // The window is being shown, get it ready.
126
- if (app->window != NULL )
123
+ LOG_INFO_MESSAGE (" APP_CMD_INIT_WINDOW" );
124
+ app->window = app->pendingWindow ;
125
+ if (app->window &&
126
+ ANativeWindow_getWidth (app->window ) &&
127
+ ANativeWindow_getHeight (app->window ))
127
128
{
129
+ LOG_INFO_MESSAGE (" INIT DISPLAY - HAS SURFACE" );
128
130
eng->InitDisplay ();
129
131
eng->DrawFrame ();
132
+ eng->AddAppStatusFlag (APP_STATUS_FLAG_HAS_REAL_SURFACE);
133
+ }
134
+ else
135
+ {
136
+ LOG_INFO_MESSAGE (" NO SURFACE" );
137
+ eng->RemoveAppStatusFlag (APP_STATUS_FLAG_HAS_REAL_SURFACE);
130
138
}
131
139
break ;
132
140
133
- case APP_CMD_CONFIG_CHANGED:
141
+ case APP_CMD_TERM_WINDOW:
142
+ LOG_INFO_MESSAGE (" APP_CMD_TERM_WINDOW - LOST SURFACE - TERM DISPLAY" );
143
+ {
144
+ eng->RemoveAppStatusFlag (APP_STATUS_FLAG_HAS_REAL_SURFACE);
145
+ }
146
+ eng->TermDisplay ();
147
+ break ;
148
+
149
+ // Note that as of NDK r21b (21.1.6352462), APP_CMD_CONTENT_RECT_CHANGED event is never
150
+ // generated by android_native_app_glue
151
+ case APP_CMD_CONTENT_RECT_CHANGED:
134
152
{
135
- // This callback is not reliable for handling orientation changes. Depending on the
136
- // device, it may be called before or after the surface has been actually resized.
153
+ LOG_INFO_MESSAGE (" APP_CMD_CONTENT_RECT_CHANGED" );
154
+
155
+ int32_t new_window_width =
156
+ app->contentRect .right - app->contentRect .left ;
157
+ int32_t new_window_height =
158
+ app->contentRect .bottom - app->contentRect .top ;
159
+ eng->WindowResize (new_window_width, new_window_height);
137
160
break ;
138
161
}
139
162
@@ -144,45 +167,82 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
144
167
// does not work either - the callback is only called once after the window has been created.
145
168
case APP_CMD_WINDOW_RESIZED:
146
169
{
147
- auto new_window_width = ANativeWindow_getWidth (app->window );
148
- auto new_window_height = ANativeWindow_getHeight (app->window );
149
- eng->WindowResize (new_window_width, new_window_height);
170
+ LOG_INFO_MESSAGE (" APP_CMD_WINDOW_RESIZED" );
171
+ if (app->window )
172
+ {
173
+ int32_t new_window_width = ANativeWindow_getWidth (app->window );
174
+ int32_t new_window_height = ANativeWindow_getHeight (app->window );
175
+ if (new_window_width && new_window_height)
176
+ {
177
+ eng->WindowResize (new_window_width, new_window_height);
178
+ }
179
+ }
150
180
break ;
151
181
}
152
182
153
- // Note that as of NDK r21b (21.1.6352462), APP_CMD_CONTENT_RECT_CHANGED event is never
154
- // generated by android_native_app_glue
155
- case APP_CMD_CONTENT_RECT_CHANGED:
156
- {
157
- auto new_window_width = app->contentRect .right - app->contentRect .left ;
158
- auto new_window_height = app->contentRect .bottom - app->contentRect .top ;
159
- eng->WindowResize (new_window_width, new_window_height);
183
+ case APP_CMD_GAINED_FOCUS:
184
+ LOG_INFO_MESSAGE (" APP_CMD_GAINED_FOCUS - HAS FOCUS" );
185
+ {
186
+ eng->AddAppStatusFlag (APP_STATUS_FLAG_FOCUSED);
187
+ }
188
+ eng->ResumeSensors ();
189
+ break ;
190
+
191
+ case APP_CMD_LOST_FOCUS:
192
+ LOG_INFO_MESSAGE (" APP_CMD_LOST_FOCUS - LOST FOCUS" );
193
+ {
194
+ eng->RemoveAppStatusFlag (APP_STATUS_FLAG_FOCUSED);
195
+ }
196
+ eng->SuspendSensors ();
197
+ break ;
198
+
199
+ case APP_CMD_RESUME:
200
+ LOG_INFO_MESSAGE (" APP_CMD_RESUME - IS ACTIVE" );
201
+ {
202
+ eng->AddAppStatusFlag (APP_STATUS_FLAG_ACTIVE);
203
+ }
204
+ break ;
205
+
206
+ case APP_CMD_START:
207
+ LOG_INFO_MESSAGE (" APP_CMD_START" );
160
208
break ;
161
- }
162
209
163
- case APP_CMD_TERM_WINDOW:
164
210
case APP_CMD_PAUSE:
165
- // The window is being hidden or closed, clean it up.
166
- eng->TermDisplay ();
167
- eng->has_focus_ = false ;
211
+ LOG_INFO_MESSAGE (" APP_CMD_PAUSE - IS NOT ACTIVE" );
212
+ {
213
+ eng->RemoveAppStatusFlag (APP_STATUS_FLAG_ACTIVE);
214
+ }
168
215
break ;
169
216
170
217
case APP_CMD_STOP:
218
+ LOG_INFO_MESSAGE (" APP_CMD_STOP" );
171
219
break ;
172
220
173
- case APP_CMD_GAINED_FOCUS:
174
- eng->ResumeSensors ();
175
- // Start animation
176
- eng->has_focus_ = true ;
221
+ case APP_CMD_CONFIG_CHANGED:
222
+ LOG_INFO_MESSAGE (" APP_CMD_CONFIG_CHANGED" );
223
+ // AConfiguration_fromAssetManager(app->config, app->activity->assetManager);
224
+ // This callback is not reliable for handling orientation changes. Depending on the
225
+ // device, it may be called before or after the surface has been actually resized.
177
226
break ;
178
227
179
- case APP_CMD_LOST_FOCUS:
180
- eng->SuspendSensors ();
181
- // Also stop animating.
182
- eng->has_focus_ = false ;
228
+ case APP_CMD_DESTROY:
229
+ LOG_INFO_MESSAGE (" APP_CMD_DESTROY - IS NOT RUNNING" );
230
+ {
231
+ eng->RemoveAppStatusFlag (APP_STATUS_FLAG_RUNNING);
232
+ }
233
+ break ;
234
+
235
+ case APP_CMD_WINDOW_REDRAW_NEEDED:
236
+ LOG_INFO_MESSAGE (" APP_CMD_WINDOW_REDRAW_NEEDED" );
237
+ if (eng->IsReady ()) eng->DrawFrame ();
238
+ break ;
239
+
240
+ case APP_CMD_SAVE_STATE:
241
+ LOG_INFO_MESSAGE (" APP_CMD_SAVE_STATE" );
183
242
break ;
184
243
185
244
case APP_CMD_LOW_MEMORY:
245
+ LOG_INFO_MESSAGE (" APP_CMD_LOW_MEMORY" );
186
246
// Free up GL resources
187
247
eng->TrimMemory ();
188
248
break ;
@@ -244,10 +304,11 @@ void AndroidAppBase::SetState(android_app* state, const char* native_activity_cl
244
304
245
305
bool AndroidAppBase::IsReady ()
246
306
{
247
- if (has_focus_)
248
- return true ;
249
-
250
- return false ;
307
+ APP_STATUS_FLAGS app_status = GetAppStatus ();
308
+ return ValueHasAppStatusFlag (app_status, APP_STATUS_FLAG_RUNNING) &&
309
+ ValueHasAppStatusFlag (app_status, APP_STATUS_FLAG_ACTIVE) &&
310
+ ValueHasAppStatusFlag (app_status, APP_STATUS_FLAG_FOCUSED) &&
311
+ ValueHasAppStatusFlag (app_status, APP_STATUS_FLAG_HAS_REAL_SURFACE);
251
312
}
252
313
253
314
// void Engine::TransformPosition( ndk_helper::Vec2& vec )
@@ -285,4 +346,37 @@ void AndroidAppBase::UpdateFPS(float fFPS)
285
346
return ;
286
347
}
287
348
349
+ AndroidAppBase::APP_STATUS_FLAGS AndroidAppBase::GetAppStatus ()
350
+ {
351
+ return app_status_.load ();
352
+ }
353
+
354
+ void AndroidAppBase::AddAppStatusFlag (AndroidAppBase::APP_STATUS_FLAGS Flag)
355
+ {
356
+ auto LastStoredValue = app_status_.load ();
357
+ while (!app_status_.compare_exchange_weak (LastStoredValue, static_cast <APP_STATUS_FLAGS>(LastStoredValue | Flag)))
358
+ {
359
+ // If exchange fails, LastStoredValue will hold the actual value of app_status_.
360
+ }
361
+ }
362
+
363
+ void AndroidAppBase::RemoveAppStatusFlag (AndroidAppBase::APP_STATUS_FLAGS Flag)
364
+ {
365
+ auto LastStoredValue = app_status_.load ();
366
+ while (!app_status_.compare_exchange_weak (LastStoredValue, static_cast <APP_STATUS_FLAGS>(LastStoredValue & ~Flag)))
367
+ {
368
+ // If exchange fails, LastStoredValue will hold the actual value of app_status_.
369
+ }
370
+ }
371
+
372
+ bool AndroidAppBase::ValueHasAppStatusFlag (AndroidAppBase::APP_STATUS_FLAGS Value, AndroidAppBase::APP_STATUS_FLAGS Flag)
373
+ {
374
+ return (Value & Flag) != APP_STATUS_FLAG_NONE;
375
+ }
376
+
377
+ bool AndroidAppBase::HasAppStatusFlag (AndroidAppBase::APP_STATUS_FLAGS Flag)
378
+ {
379
+ return ValueHasAppStatusFlag (app_status_.load (), Flag);
380
+ }
381
+
288
382
} // namespace Diligent
0 commit comments