From 3f951dbf602242658b920068b97fb928381f9e9f Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Thu, 5 Oct 2017 20:36:18 +0300 Subject: [PATCH] Android launcher rework --- runtime/src/launcher/cpp/androidLauncher.cpp | 98 +++++++++++++------- 1 file changed, 63 insertions(+), 35 deletions(-) diff --git a/runtime/src/launcher/cpp/androidLauncher.cpp b/runtime/src/launcher/cpp/androidLauncher.cpp index ac660cf4dbd..5f436f1c4e2 100644 --- a/runtime/src/launcher/cpp/androidLauncher.cpp +++ b/runtime/src/launcher/cpp/androidLauncher.cpp @@ -45,55 +45,72 @@ extern "C" KInt Konan_start(const ObjHeader*); namespace { -int pipeC, pipeKonan; -NativeActivityState nativeActivityState; +typedef struct { + int pipeC; + int pipeKonan; + NativeActivityState nativeActivityState; +} LauncherState; + +LauncherState* launcherState = nullptr; + } extern "C" void getNativeActivityState(NativeActivityState* state) { - state->activity = nativeActivityState.activity; - state->savedState = nativeActivityState.savedState; - state->savedStateSize = nativeActivityState.savedStateSize; - state->looper = nativeActivityState.looper; + state->activity = launcherState->nativeActivityState.activity; + state->savedState = launcherState->nativeActivityState.savedState; + state->savedStateSize = launcherState->nativeActivityState.savedStateSize; + state->looper = launcherState->nativeActivityState.looper; } extern "C" void notifySysEventProcessed() { int8_t message; - write(pipeKonan, &message, sizeof(message)); + write(launcherState->pipeKonan, &message, sizeof(message)); } namespace { -void* entry(void* param) { - ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); - ALooper_addFd(looper, pipeKonan, LOOPER_ID_SYS, ALOOPER_EVENT_INPUT, NULL, NULL); - nativeActivityState.looper = looper; +void launchMain() { RuntimeState* state = InitRuntime(); - if (state == nullptr) { LOGE("Unable to init runtime\n"); - return nullptr; + return; } - KInt exitStatus; { ObjHolder args; AllocArrayInstance(theArrayTypeInfo, 0, args.slot()); - exitStatus = Konan_start(args.obj()); + Konan_start(args.obj()); } DeinitRuntime(state); +} + +void* entry(void* param) { + ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); + ALooper_addFd(looper, launcherState->pipeKonan, LOOPER_ID_SYS, ALOOPER_EVENT_INPUT, NULL, NULL); + launcherState->nativeActivityState.looper = looper; + + launchMain(); + return nullptr; } -void runKonan_start() { +void runKonan_start(bool startThread) { + if (!startThread) { + launchMain(); + return; + } + int pipes[2]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipes)) { LOGE("Could not create pipe: %s", strerror(errno)); return; } - pipeC = pipes[0]; - pipeKonan = pipes[1]; + launcherState->pipeC = pipes[0]; + launcherState->pipeKonan = pipes[1]; + + LOGE("runKonan_start() %d %d", launcherState->pipeC, launcherState->pipeKonan); pthread_attr_t attr; pthread_attr_init(&attr); @@ -104,13 +121,17 @@ void runKonan_start() { void putEventSynchronously(void* event) { auto value = reinterpret_cast(event); - if (write(pipeC, &value, sizeof(value)) != sizeof(value)) { + + LOGE("putEventSynchronously() %d %d", launcherState->pipeC, launcherState->pipeKonan); + + if (write(launcherState->pipeC, &value, sizeof(value)) != sizeof(value)) { LOGE("Failure writing event: %s\n", strerror(errno)); } int8_t response; - if (read(pipeC, &response, sizeof(response)) != sizeof(response)) { + if (read(launcherState->pipeC, &response, sizeof(response)) != sizeof(response)) { LOGE("Failure reading response: %s\n", strerror(errno)); } + LOGE("putEventSynchronously() done"); } void onDestroy(ANativeActivity* activity) { @@ -196,23 +217,30 @@ void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) { extern "C" void RUNTIME_USED Konan_main( ANativeActivity* activity, void* savedState, size_t savedStateSize) { - nativeActivityState = {activity, savedState, savedStateSize}; + bool launchThread = activity->instance == nullptr; + if (launchThread) { + launcherState = (LauncherState*)calloc(sizeof(LauncherState), 1); + launcherState->nativeActivityState = {activity, savedState, savedStateSize, nullptr}; - activity->callbacks->onDestroy = onDestroy; - activity->callbacks->onStart = onStart; - activity->callbacks->onResume = onResume; - activity->callbacks->onSaveInstanceState = onSaveInstanceState; - activity->callbacks->onPause = onPause; - activity->callbacks->onStop = onStop; - activity->callbacks->onConfigurationChanged = onConfigurationChanged; - activity->callbacks->onLowMemory = onLowMemory; - activity->callbacks->onWindowFocusChanged = onWindowFocusChanged; - activity->callbacks->onNativeWindowCreated = onNativeWindowCreated; - activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed; - activity->callbacks->onInputQueueCreated = onInputQueueCreated; - activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed; + activity->instance = launcherState; + activity->callbacks->onDestroy = onDestroy; + activity->callbacks->onStart = onStart; + activity->callbacks->onResume = onResume; + activity->callbacks->onSaveInstanceState = onSaveInstanceState; + activity->callbacks->onPause = onPause; + activity->callbacks->onStop = onStop; + activity->callbacks->onConfigurationChanged = onConfigurationChanged; + activity->callbacks->onLowMemory = onLowMemory; + activity->callbacks->onWindowFocusChanged = onWindowFocusChanged; + activity->callbacks->onNativeWindowCreated = onNativeWindowCreated; + activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed; + activity->callbacks->onInputQueueCreated = onInputQueueCreated; + activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed; + } else { + launcherState = (LauncherState*)activity->instance; + } - runKonan_start(); + runKonan_start(launchThread); } #endif // KONAN_ANDROID