From 5e791878c6d19cb153f64813c7a8a9b3e27cb285 Mon Sep 17 00:00:00 2001 From: "Matias N. Goldberg" Date: Fri, 12 Apr 2024 13:46:39 -0300 Subject: [PATCH] [Android] Add Swappy support to Samples --- .../Template/app/CMakeLists.txt | 8 ++++++++ .../Template/app/src/main/jni/AndroidMain.cpp | 18 ++++++++++++++++++ .../include/System/Android/AndroidSystems.h | 15 +++++++++++++-- Samples/2.0/Common/src/GraphicsSystem.cpp | 3 +++ .../src/System/Android/AndroidSystems.cpp | 14 +++++++++++++- 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/Samples/2.0/AndroidAppTemplate/Template/app/CMakeLists.txt b/Samples/2.0/AndroidAppTemplate/Template/app/CMakeLists.txt index 2467bd359b4..74801dcf0a5 100644 --- a/Samples/2.0/AndroidAppTemplate/Template/app/CMakeLists.txt +++ b/Samples/2.0/AndroidAppTemplate/Template/app/CMakeLists.txt @@ -24,6 +24,10 @@ endif() file( READ "${OGRE_BINARIES}/include/OgreBuildSettings.h" OGRE_BUILD_SETTINGS_STR ) string( FIND "${OGRE_BUILD_SETTINGS_STR}" "#define OGRE_BUILD_COMPONENT_ATMOSPHERE" OGRE_BUILD_COMPONENT_ATMOSPHERE ) +# Read OgreVulkanBuildSettings.h to link against optional libraries (only if set) +file( READ "${OGRE_BINARIES}/include/OgreVulkanBuildSettings.h" OGRE_VK_BUILD_SETTINGS_STR ) +string( FIND "${OGRE_VK_BUILD_SETTINGS_STR}" "#define OGRE_VULKAN_USE_SWAPPY" OGRE_VULKAN_USE_SWAPPY ) + include_directories( ${OGRE_SOURCE}/OgreMain/include ) include_directories( ${OGRE_SOURCE}/RenderSystems/Vulkan/include ) include_directories( ${OGRE_SOURCE}/Components/Overlay/include ) @@ -83,4 +87,8 @@ set( OGRE_LIBS ${OGRE_SOURCE}/DependenciesAndroid/lib/libzlib.a ${OGRE_SOURCE}/DependenciesAndroid/lib/libzziplib.a ) +if( OGRE_VULKAN_USE_SWAPPY ) + list( APPEND OGRE_LIBS ${OGRE_SOURCE}/DependenciesAndroid/lib/libswappy_static.a ) +endif() + target_link_libraries( %%sampleName%% app-glue log android vulkan ${OGRE_LIBS} cpufeatures ) diff --git a/Samples/2.0/AndroidAppTemplate/Template/app/src/main/jni/AndroidMain.cpp b/Samples/2.0/AndroidAppTemplate/Template/app/src/main/jni/AndroidMain.cpp index cf508cc311b..985292b8af0 100644 --- a/Samples/2.0/AndroidAppTemplate/Template/app/src/main/jni/AndroidMain.cpp +++ b/Samples/2.0/AndroidAppTemplate/Template/app/src/main/jni/AndroidMain.cpp @@ -141,9 +141,26 @@ namespace Demo mAccumulator += timeSinceLast; } }; + + class DemoJniProvider final : public Ogre::AndroidJniProvider + { + void acquire( JNIEnv **env, jobject *activity ) override + { + android_app *app = AndroidSystems::getAndroidApp(); + app->activity->vm->AttachCurrentThread( env, nullptr ); + *activity = app->activity->clazz; + } + + void release( JNIEnv * ) override + { + android_app *app = AndroidSystems::getAndroidApp(); + app->activity->vm->DetachCurrentThread(); + } + }; } // namespace Demo static Demo::AndroidAppController g_appController; +static Demo::DemoJniProvider g_demoJniProvider; // Process the next main command. void handle_cmd( android_app *app, int32_t cmd ) @@ -179,6 +196,7 @@ void android_main( struct android_app *app ) app->onAppCmd = handle_cmd; Demo::AndroidSystems::setAndroidApp( app ); + Demo::AndroidSystems::setJniProvider( &g_demoJniProvider ); // Used to poll the events in the main loop int events; diff --git a/Samples/2.0/Common/include/System/Android/AndroidSystems.h b/Samples/2.0/Common/include/System/Android/AndroidSystems.h index 90c3611fe80..d0e5bbc2f18 100644 --- a/Samples/2.0/Common/include/System/Android/AndroidSystems.h +++ b/Samples/2.0/Common/include/System/Android/AndroidSystems.h @@ -34,24 +34,35 @@ THE SOFTWARE. struct android_app; struct ANativeWindow; +namespace Ogre +{ + class AndroidJniProvider; +} + namespace Demo { /// Utility class to load plugins statically class AndroidSystems { #if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID - android_app * mAndroidApp; + android_app *mAndroidApp; ANativeWindow *mNativeWindow; + + Ogre::AndroidJniProvider *mJniProvider; #endif public: AndroidSystems(); - static void setAndroidApp( android_app *androidApp ); + static void setAndroidApp( android_app *androidApp ); + static android_app *getAndroidApp(); static void setNativeWindow( ANativeWindow *nativeWindow ); static ANativeWindow *getNativeWindow(); + static void setJniProvider( Ogre::AndroidJniProvider *provider ); + static Ogre::AndroidJniProvider *getJniProvider(); + /** On Android platforms: Opens a file in an APK and returns a DataStream smart pointer that can be read from diff --git a/Samples/2.0/Common/src/GraphicsSystem.cpp b/Samples/2.0/Common/src/GraphicsSystem.cpp index 3000804d4c3..623e75818d7 100644 --- a/Samples/2.0/Common/src/GraphicsSystem.cpp +++ b/Samples/2.0/Common/src/GraphicsSystem.cpp @@ -311,6 +311,9 @@ namespace Demo params.insert( std::make_pair( "ANativeWindow", Ogre::StringConverter::toString( (uintptr_t)AndroidSystems::getNativeWindow() ) ) ); + params.insert( std::make_pair( + "AndroidJniProvider", + Ogre::StringConverter::toString( (uintptr_t)AndroidSystems::getJniProvider() ) ) ); #endif params.insert( std::make_pair( "title", windowTitle ) ); diff --git a/Samples/2.0/Common/src/System/Android/AndroidSystems.cpp b/Samples/2.0/Common/src/System/Android/AndroidSystems.cpp index 958d6db442a..f00935c620b 100644 --- a/Samples/2.0/Common/src/System/Android/AndroidSystems.cpp +++ b/Samples/2.0/Common/src/System/Android/AndroidSystems.cpp @@ -44,13 +44,15 @@ namespace Demo #endif #if OGRE_PLATFORM == OGRE_PLATFORM_ANDROID - AndroidSystems::AndroidSystems() : mAndroidApp( 0 ), mNativeWindow( 0 ) {} + AndroidSystems::AndroidSystems() : mAndroidApp( 0 ), mNativeWindow( 0 ), mJniProvider( 0 ) {} //------------------------------------------------------------------------- void AndroidSystems::setAndroidApp( android_app *androidApp ) { g_andrSystem.mAndroidApp = androidApp; } //------------------------------------------------------------------------- + android_app *AndroidSystems::getAndroidApp() { return g_andrSystem.mAndroidApp; } + //------------------------------------------------------------------------- void AndroidSystems::setNativeWindow( ANativeWindow *nativeWindow ) { g_andrSystem.mNativeWindow = nativeWindow; @@ -58,6 +60,13 @@ namespace Demo //------------------------------------------------------------------------- ANativeWindow *AndroidSystems::getNativeWindow() { return g_andrSystem.mNativeWindow; } //------------------------------------------------------------------------- + void AndroidSystems::setJniProvider( Ogre::AndroidJniProvider *provider ) + { + g_andrSystem.mJniProvider = provider; + } + //------------------------------------------------------------------------- + Ogre::AndroidJniProvider *AndroidSystems::getJniProvider() { return g_andrSystem.mJniProvider; } + //------------------------------------------------------------------------- void AndroidSystems::registerArchiveFactories() { AAssetManager *assetMgr = g_andrSystem.mAndroidApp->activity->assetManager; @@ -105,8 +114,11 @@ namespace Demo //------------------------------------------------------------------------- AndroidSystems::AndroidSystems() {} void AndroidSystems::setAndroidApp( android_app * ) {} + android_app *AndroidSystems::getAndroidApp() { return 0; } void AndroidSystems::setNativeWindow( ANativeWindow * ) {} ANativeWindow *AndroidSystems::getNativeWindow() { return 0; } + void AndroidSystems::setJniProvider( Ogre::AndroidJniProvider *provider ) {} + Ogre::AndroidJniProvider *AndroidSystems::getJniProvider() { return 0; } std::string AndroidSystems::getFilesDir( const bool /*bInternal*/ ) { return ""; } bool AndroidSystems::isAndroid() { return false; } void AndroidSystems::registerArchiveFactories() {}