-
Notifications
You must be signed in to change notification settings - Fork 46
Add Android window lifecycle support #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d8c056
Added Android window lifecycle support
Solessfir 2ee338f
Addressed Android window review
Solessfir beae5b2
Simplified Android callbacks and window startup
Solessfir 82fd004
Handle Android activity reopening
Solessfir 032532d
Exit the Android process after main returns
Solessfir 9d84f9e
Defer Android surface recreation to the swapchain
Solessfir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| #include "androidapi.h" | ||
|
|
||
| #ifdef __ANDROID__ | ||
|
|
||
| #include <android_native_app_glue.h> | ||
|
|
||
| #include <Tempest/Event> | ||
| #include <Tempest/Log> | ||
| #include <Tempest/Window> | ||
|
|
||
| #include <android/native_activity.h> | ||
| #include <android/native_window.h> | ||
|
|
||
| #include <atomic> | ||
| #include <cstdlib> | ||
| #include <dlfcn.h> | ||
| #include <exception> | ||
| #include <thread> | ||
|
|
||
| using namespace Tempest; | ||
|
|
||
| extern "C" void android_main(android_app* state); | ||
|
|
||
| static android_app* app = nullptr; | ||
| static Tempest::Window* mainWindow = nullptr; | ||
| static std::atomic_bool isExit = false; | ||
| static bool resumed = false; | ||
| static bool focused = false; | ||
| static bool active = false; | ||
| static bool hasWindow = false; | ||
|
|
||
| void AndroidApi::updateFocus() { | ||
| const bool next = resumed && focused; | ||
| if(active==next) | ||
| return; | ||
| active = next; | ||
| if(mainWindow!=nullptr) { | ||
| FocusEvent event(active,Event::FocusReason::UnknownReason); | ||
| AndroidApi::dispatchFocus(*mainWindow,event); | ||
| } | ||
| } | ||
|
|
||
| void AndroidApi::updateWindow() { | ||
| if(app==nullptr || app->window==nullptr) | ||
| return; | ||
|
|
||
| hasWindow = true; | ||
| if(mainWindow==nullptr) | ||
| return; | ||
|
|
||
| SizeEvent event(ANativeWindow_getWidth(app->window),ANativeWindow_getHeight(app->window)); | ||
| AndroidApi::dispatchResize(*mainWindow,event); | ||
| } | ||
|
|
||
| void AndroidApi::onAppCmd(void*, int32_t cmd) { | ||
| switch(cmd) { | ||
| case APP_CMD_INIT_WINDOW: | ||
| if(mainWindow!=nullptr) { | ||
| // TODO: handle native surface recreation in the Vulkan swapchain. | ||
| Log::e("Android native window recreation is not implemented"); | ||
| std::terminate(); | ||
| } | ||
| updateWindow(); | ||
| break; | ||
| case APP_CMD_TERM_WINDOW: | ||
| hasWindow = false; | ||
| break; | ||
| case APP_CMD_WINDOW_RESIZED: | ||
| case APP_CMD_CONFIG_CHANGED: | ||
| updateWindow(); | ||
| break; | ||
| case APP_CMD_GAINED_FOCUS: | ||
| focused = true; | ||
| updateFocus(); | ||
| break; | ||
| case APP_CMD_LOST_FOCUS: | ||
| focused = false; | ||
| updateFocus(); | ||
| break; | ||
| case APP_CMD_RESUME: | ||
| resumed = true; | ||
| updateFocus(); | ||
| break; | ||
| case APP_CMD_PAUSE: | ||
| resumed = false; | ||
| updateFocus(); | ||
| break; | ||
| case APP_CMD_DESTROY: | ||
| if(mainWindow!=nullptr) { | ||
| CloseEvent event; | ||
| AndroidApi::dispatchClose(*mainWindow,event); | ||
| } | ||
| isExit.store(true); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| static void pollAndroid(android_app* state, int timeout) { | ||
| int pending = 0; | ||
| android_poll_source* source = nullptr; | ||
| while(ALooper_pollOnce(timeout,nullptr,&pending,reinterpret_cast<void**>(&source))>=0) { | ||
| if(source!=nullptr) | ||
| source->process(state,source); | ||
| timeout = 0; | ||
| } | ||
| } | ||
|
|
||
| SystemApi::Window* AndroidApi::createAndroidWindow(Tempest::Window* owner) { | ||
| if(mainWindow!=nullptr) | ||
| return nullptr; | ||
| app->onAppCmd = [](android_app* state, int32_t cmd) { onAppCmd(state,cmd); }; | ||
| while(!hasWindow && !isExit.load() && app->destroyRequested==0) | ||
| pollAndroid(app,-1); | ||
| if(isExit.load() || app->destroyRequested!=0) | ||
| return nullptr; | ||
| mainWindow = owner; | ||
| return reinterpret_cast<SystemApi::Window*>(app->window); | ||
| } | ||
|
|
||
| SystemApi::Window* AndroidApi::implCreateWindow(Tempest::Window* owner, uint32_t, uint32_t) { | ||
| return createAndroidWindow(owner); | ||
| } | ||
|
|
||
| SystemApi::Window* AndroidApi::implCreateWindow(Tempest::Window* owner, ShowMode) { | ||
| return createAndroidWindow(owner); | ||
| } | ||
|
|
||
| void AndroidApi::implDestroyWindow(SystemApi::Window*) { | ||
| mainWindow = nullptr; | ||
| } | ||
|
|
||
| void AndroidApi::implExit() { | ||
| isExit.store(true); | ||
| ALooper_wake(app->looper); | ||
| } | ||
|
|
||
| Rect AndroidApi::implWindowClientRect(SystemApi::Window* w) { | ||
| const auto window = reinterpret_cast<ANativeWindow*>(w); | ||
| return Rect(0,0,ANativeWindow_getWidth(window),ANativeWindow_getHeight(window)); | ||
| } | ||
|
|
||
| bool AndroidApi::implSetAsFullscreen(SystemApi::Window*, bool) { | ||
| // TODO: toggle Android immersive mode. | ||
| return false; | ||
| } | ||
|
|
||
| bool AndroidApi::implIsFullscreen(SystemApi::Window*) { | ||
| // TODO: query Android immersive mode. | ||
| return true; | ||
| } | ||
|
|
||
| void AndroidApi::implSetCursorPosition(SystemApi::Window*, int, int) { | ||
| } | ||
|
|
||
| void AndroidApi::implShowCursor(SystemApi::Window*, CursorShape) { | ||
| } | ||
|
|
||
| bool AndroidApi::implIsRunning() { | ||
| return !isExit.load(); | ||
| } | ||
|
|
||
| int AndroidApi::implExec(AppCallBack& cb) { | ||
| while(!isExit.load()) { | ||
| implProcessEvents(cb); | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void AndroidApi::implProcessEvents(AppCallBack& cb) { | ||
| if(isExit.load()) | ||
| return; | ||
| pollAndroid(app,active && hasWindow ? 0 : -1); | ||
| if(isExit.load()) | ||
| return; | ||
| if(mainWindow!=nullptr && active && hasWindow) | ||
| dispatchRender(*mainWindow); | ||
| if(active && hasWindow && !isExit.load() && cb.onTimer()==0) | ||
| std::this_thread::yield(); | ||
| } | ||
|
|
||
| void AndroidApi::implSetWindowTitle(SystemApi::Window*, const char*) { | ||
| // TODO: update the activity title through JNI. | ||
| } | ||
|
Try marked this conversation as resolved.
|
||
|
|
||
| static int runMain() { | ||
| Dl_info module = {}; | ||
| if(dladdr(reinterpret_cast<void*>(&android_main),&module)==0) { | ||
| Log::e("Unable to locate the application library"); | ||
| return EXIT_FAILURE; | ||
| } | ||
| void* self = dlopen(module.dli_fname,RTLD_NOW); | ||
| if(self==nullptr) { | ||
| const char* error = dlerror(); | ||
| Log::e("Unable to open the application library: ",error==nullptr ? "unknown error" : error); | ||
| return EXIT_FAILURE; | ||
| } | ||
| using Main = int(*)(int,char**); | ||
| auto entry = reinterpret_cast<Main>(dlsym(self,"main")); | ||
| if(entry==nullptr) { | ||
| const char* error = dlerror(); | ||
| Log::e("Unable to find the application entry point: ",error==nullptr ? "unknown error" : error); | ||
| dlclose(self); | ||
| return EXIT_FAILURE; | ||
| } | ||
| // NativeActivity owns the library for the duration of android_main. | ||
| dlclose(self); | ||
| char arg0[] = "app"; | ||
| char* argv[] = {arg0,nullptr}; | ||
| return entry(1,argv); | ||
| } | ||
|
|
||
| extern "C" void android_main(android_app* state) { | ||
| static std::atomic_flag started = ATOMIC_FLAG_INIT; | ||
| if(started.test_and_set()) { | ||
| ANativeActivity_finish(state->activity); | ||
| while(state->destroyRequested==0) | ||
| pollAndroid(state,-1); | ||
| return; | ||
| } | ||
| app = state; | ||
| int result = EXIT_FAILURE; | ||
| try { | ||
| result = runMain(); | ||
| } | ||
| catch(const std::exception& e) { | ||
| Log::e("Unhandled native exception: ",e.what()); | ||
| } | ||
| catch(...) { | ||
| Log::e("Unhandled native exception"); | ||
| } | ||
|
|
||
| if(app->destroyRequested==0) | ||
| ANativeActivity_finish(app->activity); | ||
| // Re-entering main would reuse application statics from the previous run. | ||
| std::exit(result); | ||
| } | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #pragma once | ||
|
|
||
| #include <Tempest/SystemApi> | ||
|
|
||
| namespace Tempest { | ||
|
|
||
| class AndroidApi final : SystemApi { | ||
| private: | ||
| AndroidApi() = default; | ||
|
|
||
| static Window* createAndroidWindow(Tempest::Window* owner); | ||
| static void onAppCmd(void* app, int32_t cmd); | ||
| static void updateFocus(); | ||
| static void updateWindow(); | ||
|
|
||
| Window* implCreateWindow(Tempest::Window* owner, uint32_t width, uint32_t height) override; | ||
| Window* implCreateWindow(Tempest::Window* owner, ShowMode sm) override; | ||
| void implDestroyWindow(Window* w) override; | ||
| void implExit() override; | ||
|
|
||
| Rect implWindowClientRect(SystemApi::Window* w) override; | ||
| bool implSetAsFullscreen(SystemApi::Window* w, bool fullScreen) override; | ||
| bool implIsFullscreen(SystemApi::Window* w) override; | ||
|
|
||
| void implSetCursorPosition(SystemApi::Window* w, int x, int y) override; | ||
| void implShowCursor(SystemApi::Window* w, CursorShape cursor) override; | ||
|
|
||
| bool implIsRunning() override; | ||
| int implExec(AppCallBack& cb) override; | ||
| void implProcessEvents(AppCallBack& cb) override; | ||
|
|
||
| void implSetWindowTitle(SystemApi::Window* w, const char* utf8) override; | ||
|
|
||
| friend class SystemApi; | ||
| }; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,23 @@ | ||
| #include <android/log.h> | ||
| #include <android/native_activity.h> | ||
| #include <android/native_window.h> | ||
| #include <cstdint> | ||
| #include <Tempest/Application> | ||
| #include <Tempest/Event> | ||
| #include <Tempest/Log> | ||
| #include <Tempest/Window> | ||
|
|
||
| // A packaging smoke test using the platform activity, independent of Tempest's Android backend. | ||
| // Fold this into Examples/Empty once that backend is available upstream. | ||
| static void draw(ANativeActivity*, ANativeWindow* window) { | ||
| ANativeWindow_setBuffersGeometry(window,0,0,WINDOW_FORMAT_RGBA_8888); | ||
| ANativeWindow_Buffer buffer = {}; | ||
| if(ANativeWindow_lock(window,&buffer,nullptr)!=0) | ||
| return; | ||
| auto pixels = static_cast<uint32_t*>(buffer.bits); | ||
| for(int y=0; y<buffer.height; ++y) | ||
| for(int x=0; x<buffer.width; ++x) { | ||
| const bool center = x>buffer.width/3 && x<buffer.width*2/3 && | ||
| y>buffer.height/3 && y<buffer.height*2/3; | ||
| pixels[y*buffer.stride+x] = center ? 0xffa1c2d7u : 0xff593314u; | ||
| class Example final : public Tempest::Window { | ||
| private: | ||
| void resizeEvent(Tempest::SizeEvent& event) override { | ||
| Tempest::Log::i("Window resized: ",event.w,"x",event.h); | ||
| Tempest::Window::resizeEvent(event); | ||
| } | ||
| ANativeWindow_unlockAndPost(window); | ||
| } | ||
|
|
||
| extern "C" void ANativeActivity_onCreate(ANativeActivity* activity, void*, size_t) { | ||
| activity->callbacks->onNativeWindowCreated = draw; | ||
| activity->callbacks->onNativeWindowResized = draw; | ||
| activity->callbacks->onNativeWindowRedrawNeeded = draw; | ||
| __android_log_print(ANDROID_LOG_INFO,"TempestExample","Native packaging example started"); | ||
| void focusEvent(Tempest::FocusEvent& event) override { | ||
| Tempest::Log::i(event.in ? "Window resumed" : "Window paused"); | ||
| Tempest::Window::focusEvent(event); | ||
| } | ||
| }; | ||
|
|
||
| int main(int, char**) { | ||
| Tempest::Application app; | ||
| Example window; | ||
| return app.exec(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.