(Android) Better lifecycle management - getting close to restoring state

This commit is contained in:
twinaphex 2012-11-03 19:06:55 +01:00
parent 2e58e25dbe
commit 46a4efd885
4 changed files with 60 additions and 19 deletions

View File

@ -24,6 +24,7 @@ struct saved_state
{ {
int32_t x; int32_t x;
int32_t y; int32_t y;
uint64_t input_state;
}; };
struct droid struct droid

View File

@ -296,6 +296,8 @@ static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* wind
static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window)
{ {
RARCH_LOG("NativeWindowDestroyed: %p -- %p\n", activity, window); RARCH_LOG("NativeWindowDestroyed: %p -- %p\n", activity, window);
android_app_set_window((struct android_app*)activity->instance, NULL); android_app_set_window((struct android_app*)activity->instance, NULL);
} }
@ -386,6 +388,13 @@ void engine_handle_cmd(struct android_app* android_app, int32_t cmd)
/* The window is being shown, get it ready. */ /* The window is being shown, get it ready. */
android_app->window = android_app->pendingWindow; android_app->window = android_app->pendingWindow;
if (g_android.input_state & (1ULL << RARCH_REENTRANT))
{
gfx_ctx_init();
g_extern.audio_active = true;
g_extern.video_active = true;
}
pthread_cond_broadcast(&android_app->cond); pthread_cond_broadcast(&android_app->cond);
pthread_mutex_unlock(&android_app->mutex); pthread_mutex_unlock(&android_app->mutex);
@ -422,6 +431,17 @@ void engine_handle_cmd(struct android_app* android_app, int32_t cmd)
android_app->activityState = cmd; android_app->activityState = cmd;
pthread_cond_broadcast(&android_app->cond); pthread_cond_broadcast(&android_app->cond);
pthread_mutex_unlock(&android_app->mutex); pthread_mutex_unlock(&android_app->mutex);
/* EXEC */
if(g_android.input_state & (1ULL << RARCH_QUIT_KEY)) { }
else
{
/* Setting reentrancy */
RARCH_LOG("Setting up RetroArch re-entrancy...\n");
g_android.input_state |= (1ULL << RARCH_REENTRANT);
g_extern.audio_active = false;
g_extern.video_active = false;
}
break; break;
case APP_CMD_STOP: case APP_CMD_STOP:
RARCH_LOG("engine_handle_cmd: APP_CMD_STOP.\n"); RARCH_LOG("engine_handle_cmd: APP_CMD_STOP.\n");
@ -449,6 +469,8 @@ void engine_handle_cmd(struct android_app* android_app, int32_t cmd)
/* EXEC */ /* EXEC */
/* The window is being hidden or closed, clean it up. */ /* The window is being hidden or closed, clean it up. */
/* terminate display/EGL context here */ /* terminate display/EGL context here */
if(g_android.input_state & (1ULL << RARCH_REENTRANT))
gfx_ctx_destroy();
/* POSTEXEC */ /* POSTEXEC */
android_app->window = NULL; android_app->window = NULL;
@ -459,7 +481,6 @@ void engine_handle_cmd(struct android_app* android_app, int32_t cmd)
RARCH_LOG("engine_handle_cmd: APP_CMD_GAINED_FOCUS.\n"); RARCH_LOG("engine_handle_cmd: APP_CMD_GAINED_FOCUS.\n");
/* EXEC */ /* EXEC */
// When our app gains focus, we start monitoring the accelerometer.
break; break;
case APP_CMD_LOST_FOCUS: case APP_CMD_LOST_FOCUS:
RARCH_LOG("engine_handle_cmd: APP_CMD_LOST_FOCUS.\n"); RARCH_LOG("engine_handle_cmd: APP_CMD_LOST_FOCUS.\n");
@ -510,10 +531,7 @@ static void android_get_char_argv(char *argv, size_t sizeof_argv, const char *ar
*/ */
void android_main(struct android_app* state) void android_main(struct android_app* state)
{ {
rarch_main_clear_state(); memset(&g_android, 0, sizeof(g_android));
RARCH_LOG("Native Activity started.\n");
g_android.app = state; g_android.app = state;
struct android_app* android_app = g_android.app; struct android_app* android_app = g_android.app;
@ -541,14 +559,30 @@ void android_main(struct android_app* state)
argv[argc++] = strdup(libretro_path); argv[argc++] = strdup(libretro_path);
argv[argc++] = strdup("-v"); argv[argc++] = strdup("-v");
g_extern.verbose = true;
if (state->savedState != NULL) if (state->savedState != NULL)
{ {
// We are starting with a previous saved state; restore from it. // We are starting with a previous saved state; restore from it.
RARCH_LOG("Restoring reentrant savestate.\n");
g_android.state = *(struct saved_state*)state->savedState; g_android.state = *(struct saved_state*)state->savedState;
g_android.input_state = g_android.state.input_state;
} }
bool rarch_reentrant = (g_android.input_state & (1ULL << RARCH_REENTRANT));
if(rarch_reentrant)
{
RARCH_LOG("Native Activity started (reentrant).\n");
}
else
{
RARCH_LOG("Native Activity started.\n");
rarch_main_clear_state();
}
g_extern.verbose = true;
while(!(g_android.input_state & (1ULL << RARCH_WINDOW_READY))) while(!(g_android.input_state & (1ULL << RARCH_WINDOW_READY)))
{ {
// Read all pending events. // Read all pending events.
@ -579,23 +613,32 @@ void android_main(struct android_app* state)
} }
} }
int init_ret; int init_ret;
RARCH_LOG("Initializing RetroArch...\n"); if (rarch_reentrant)
if ((init_ret = rarch_main_init(argc, argv)) == 0)
{ {
RARCH_LOG("Starting RetroArch...\n"); init_ret = 0;
rarch_init_msg_queue();
while (rarch_main_iterate()); /* We've done everything state-wise needed for RARCH_REENTRANT,
* get rid of it now */
g_android.input_state |= ~(1ULL << RARCH_REENTRANT);
} }
else else if ((init_ret = rarch_main_init(argc, argv)) != 0)
{ {
RARCH_LOG("Initialization failed.\n"); RARCH_LOG("Initialization failed.\n");
g_android.input_state |= (1ULL << RARCH_QUIT_KEY); g_android.input_state |= (1ULL << RARCH_QUIT_KEY);
g_android.input_state |= (1ULL << RARCH_KILL); g_android.input_state |= (1ULL << RARCH_KILL);
} }
if (init_ret == 0)
{
RARCH_LOG("Initializing succeeded.\n");
RARCH_LOG("RetroArch started.\n");
rarch_init_msg_queue();
while (rarch_main_iterate());
RARCH_LOG("RetroArch stopped.\n");
}
if(g_android.input_state & (1ULL << RARCH_QUIT_KEY)) if(g_android.input_state & (1ULL << RARCH_QUIT_KEY))
{ {
RARCH_LOG("Deinitializing RetroArch...\n"); RARCH_LOG("Deinitializing RetroArch...\n");

View File

@ -97,6 +97,9 @@ enum // RetroArch specific bind IDs.
RARCH_KILL, RARCH_KILL,
RARCH_WINDOW_READY, RARCH_WINDOW_READY,
#endif #endif
#if defined(ANDROID) || defined(RARCH_CONSOLE)
RARCH_REENTRANT,
#endif
RARCH_BIND_LIST_END, RARCH_BIND_LIST_END,
RARCH_BIND_LIST_END_NULL RARCH_BIND_LIST_END_NULL

View File

@ -254,10 +254,8 @@ static void recording_dump_frame(const void *data, unsigned width, unsigned heig
static void video_frame(const void *data, unsigned width, unsigned height, size_t pitch) static void video_frame(const void *data, unsigned width, unsigned height, size_t pitch)
{ {
#if !defined(RARCH_PERFORMANCE_MODE)
if (!g_extern.video_active) if (!g_extern.video_active)
return; return;
#endif
if (g_extern.system.pix_fmt == RETRO_PIXEL_FORMAT_0RGB1555 && data) if (g_extern.system.pix_fmt == RETRO_PIXEL_FORMAT_0RGB1555 && data)
{ {
@ -2555,10 +2553,6 @@ void rarch_main_clear_state(void)
free(g_extern.system.environment_split); free(g_extern.system.environment_split);
memset(&g_extern, 0, sizeof(g_extern)); memset(&g_extern, 0, sizeof(g_extern));
#ifdef ANDROID
memset(&g_android, 0, sizeof(g_android));
#endif
init_state(); init_state();
} }