diff options
| author | 2024-02-05 06:07:29 -0500 | |
|---|---|---|
| committer | 2024-02-08 13:45:26 -0500 | |
| commit | e7c4c8b993ce27a50b7a56f90247056048d20f7d (patch) | |
| tree | b87c275f4ea92092f7c8b6cdcb0f50d32819490f /src/common/android | |
| parent | Merge pull request #12892 from liamwhite/serialization-stuff (diff) | |
| download | yuzu-e7c4c8b993ce27a50b7a56f90247056048d20f7d.tar.gz yuzu-e7c4c8b993ce27a50b7a56f90247056048d20f7d.tar.xz yuzu-e7c4c8b993ce27a50b7a56f90247056048d20f7d.zip | |
android: Move JNI setup and helpers to common
Diffstat (limited to '')
| -rw-r--r-- | src/common/android/android_common.cpp (renamed from src/android/app/src/main/jni/android_common/android_common.cpp) | 23 | ||||
| -rw-r--r-- | src/common/android/android_common.h (renamed from src/android/app/src/main/jni/android_common/android_common.h) | 4 | ||||
| -rw-r--r-- | src/common/android/applets/software_keyboard.cpp (renamed from src/android/app/src/main/jni/applets/software_keyboard.cpp) | 24 | ||||
| -rw-r--r-- | src/common/android/applets/software_keyboard.h (renamed from src/android/app/src/main/jni/applets/software_keyboard.h) | 4 | ||||
| -rw-r--r-- | src/common/android/id_cache.cpp (renamed from src/android/app/src/main/jni/id_cache.cpp) | 12 | ||||
| -rw-r--r-- | src/common/android/id_cache.h (renamed from src/android/app/src/main/jni/id_cache.h) | 26 |
6 files changed, 61 insertions, 32 deletions
diff --git a/src/android/app/src/main/jni/android_common/android_common.cpp b/src/common/android/android_common.cpp index 7018a52af..e79005658 100644 --- a/src/android/app/src/main/jni/android_common/android_common.cpp +++ b/src/common/android/android_common.cpp | |||
| @@ -1,15 +1,17 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include "jni/android_common/android_common.h" | 4 | #include "android_common.h" |
| 5 | 5 | ||
| 6 | #include <string> | 6 | #include <string> |
| 7 | #include <string_view> | 7 | #include <string_view> |
| 8 | 8 | ||
| 9 | #include <jni.h> | 9 | #include <jni.h> |
| 10 | 10 | ||
| 11 | #include "common/android/id_cache.h" | ||
| 11 | #include "common/string_util.h" | 12 | #include "common/string_util.h" |
| 12 | #include "jni/id_cache.h" | 13 | |
| 14 | namespace Common::Android { | ||
| 13 | 15 | ||
| 14 | std::string GetJString(JNIEnv* env, jstring jstr) { | 16 | std::string GetJString(JNIEnv* env, jstring jstr) { |
| 15 | if (!jstr) { | 17 | if (!jstr) { |
| @@ -18,7 +20,8 @@ std::string GetJString(JNIEnv* env, jstring jstr) { | |||
| 18 | 20 | ||
| 19 | const jchar* jchars = env->GetStringChars(jstr, nullptr); | 21 | const jchar* jchars = env->GetStringChars(jstr, nullptr); |
| 20 | const jsize length = env->GetStringLength(jstr); | 22 | const jsize length = env->GetStringLength(jstr); |
| 21 | const std::u16string_view string_view(reinterpret_cast<const char16_t*>(jchars), length); | 23 | const std::u16string_view string_view(reinterpret_cast<const char16_t*>(jchars), |
| 24 | static_cast<u32>(length)); | ||
| 22 | const std::string converted_string = Common::UTF16ToUTF8(string_view); | 25 | const std::string converted_string = Common::UTF16ToUTF8(string_view); |
| 23 | env->ReleaseStringChars(jstr, jchars); | 26 | env->ReleaseStringChars(jstr, jchars); |
| 24 | 27 | ||
| @@ -36,25 +39,27 @@ jstring ToJString(JNIEnv* env, std::u16string_view str) { | |||
| 36 | } | 39 | } |
| 37 | 40 | ||
| 38 | double GetJDouble(JNIEnv* env, jobject jdouble) { | 41 | double GetJDouble(JNIEnv* env, jobject jdouble) { |
| 39 | return env->GetDoubleField(jdouble, IDCache::GetDoubleValueField()); | 42 | return env->GetDoubleField(jdouble, GetDoubleValueField()); |
| 40 | } | 43 | } |
| 41 | 44 | ||
| 42 | jobject ToJDouble(JNIEnv* env, double value) { | 45 | jobject ToJDouble(JNIEnv* env, double value) { |
| 43 | return env->NewObject(IDCache::GetDoubleClass(), IDCache::GetDoubleConstructor(), value); | 46 | return env->NewObject(GetDoubleClass(), GetDoubleConstructor(), value); |
| 44 | } | 47 | } |
| 45 | 48 | ||
| 46 | s32 GetJInteger(JNIEnv* env, jobject jinteger) { | 49 | s32 GetJInteger(JNIEnv* env, jobject jinteger) { |
| 47 | return env->GetIntField(jinteger, IDCache::GetIntegerValueField()); | 50 | return env->GetIntField(jinteger, GetIntegerValueField()); |
| 48 | } | 51 | } |
| 49 | 52 | ||
| 50 | jobject ToJInteger(JNIEnv* env, s32 value) { | 53 | jobject ToJInteger(JNIEnv* env, s32 value) { |
| 51 | return env->NewObject(IDCache::GetIntegerClass(), IDCache::GetIntegerConstructor(), value); | 54 | return env->NewObject(GetIntegerClass(), GetIntegerConstructor(), value); |
| 52 | } | 55 | } |
| 53 | 56 | ||
| 54 | bool GetJBoolean(JNIEnv* env, jobject jboolean) { | 57 | bool GetJBoolean(JNIEnv* env, jobject jboolean) { |
| 55 | return env->GetBooleanField(jboolean, IDCache::GetBooleanValueField()); | 58 | return env->GetBooleanField(jboolean, GetBooleanValueField()); |
| 56 | } | 59 | } |
| 57 | 60 | ||
| 58 | jobject ToJBoolean(JNIEnv* env, bool value) { | 61 | jobject ToJBoolean(JNIEnv* env, bool value) { |
| 59 | return env->NewObject(IDCache::GetBooleanClass(), IDCache::GetBooleanConstructor(), value); | 62 | return env->NewObject(GetBooleanClass(), GetBooleanConstructor(), value); |
| 60 | } | 63 | } |
| 64 | |||
| 65 | } // namespace Common::Android | ||
diff --git a/src/android/app/src/main/jni/android_common/android_common.h b/src/common/android/android_common.h index 29a338c0a..d0ccb4ec2 100644 --- a/src/android/app/src/main/jni/android_common/android_common.h +++ b/src/common/android/android_common.h | |||
| @@ -8,6 +8,8 @@ | |||
| 8 | #include <jni.h> | 8 | #include <jni.h> |
| 9 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 10 | 10 | ||
| 11 | namespace Common::Android { | ||
| 12 | |||
| 11 | std::string GetJString(JNIEnv* env, jstring jstr); | 13 | std::string GetJString(JNIEnv* env, jstring jstr); |
| 12 | jstring ToJString(JNIEnv* env, std::string_view str); | 14 | jstring ToJString(JNIEnv* env, std::string_view str); |
| 13 | jstring ToJString(JNIEnv* env, std::u16string_view str); | 15 | jstring ToJString(JNIEnv* env, std::u16string_view str); |
| @@ -20,3 +22,5 @@ jobject ToJInteger(JNIEnv* env, s32 value); | |||
| 20 | 22 | ||
| 21 | bool GetJBoolean(JNIEnv* env, jobject jboolean); | 23 | bool GetJBoolean(JNIEnv* env, jobject jboolean); |
| 22 | jobject ToJBoolean(JNIEnv* env, bool value); | 24 | jobject ToJBoolean(JNIEnv* env, bool value); |
| 25 | |||
| 26 | } // namespace Common::Android | ||
diff --git a/src/android/app/src/main/jni/applets/software_keyboard.cpp b/src/common/android/applets/software_keyboard.cpp index 9943483e8..477e62b16 100644 --- a/src/android/app/src/main/jni/applets/software_keyboard.cpp +++ b/src/common/android/applets/software_keyboard.cpp | |||
| @@ -6,12 +6,12 @@ | |||
| 6 | 6 | ||
| 7 | #include <jni.h> | 7 | #include <jni.h> |
| 8 | 8 | ||
| 9 | #include "common/android/android_common.h" | ||
| 10 | #include "common/android/applets/software_keyboard.h" | ||
| 11 | #include "common/android/id_cache.h" | ||
| 9 | #include "common/logging/log.h" | 12 | #include "common/logging/log.h" |
| 10 | #include "common/string_util.h" | 13 | #include "common/string_util.h" |
| 11 | #include "core/core.h" | 14 | #include "core/core.h" |
| 12 | #include "jni/android_common/android_common.h" | ||
| 13 | #include "jni/applets/software_keyboard.h" | ||
| 14 | #include "jni/id_cache.h" | ||
| 15 | 15 | ||
| 16 | static jclass s_software_keyboard_class; | 16 | static jclass s_software_keyboard_class; |
| 17 | static jclass s_keyboard_config_class; | 17 | static jclass s_keyboard_config_class; |
| @@ -19,10 +19,10 @@ static jclass s_keyboard_data_class; | |||
| 19 | static jmethodID s_swkbd_execute_normal; | 19 | static jmethodID s_swkbd_execute_normal; |
| 20 | static jmethodID s_swkbd_execute_inline; | 20 | static jmethodID s_swkbd_execute_inline; |
| 21 | 21 | ||
| 22 | namespace SoftwareKeyboard { | 22 | namespace Common::Android::SoftwareKeyboard { |
| 23 | 23 | ||
| 24 | static jobject ToJKeyboardParams(const Core::Frontend::KeyboardInitializeParameters& config) { | 24 | static jobject ToJKeyboardParams(const Core::Frontend::KeyboardInitializeParameters& config) { |
| 25 | JNIEnv* env = IDCache::GetEnvForThread(); | 25 | JNIEnv* env = GetEnvForThread(); |
| 26 | jobject object = env->AllocObject(s_keyboard_config_class); | 26 | jobject object = env->AllocObject(s_keyboard_config_class); |
| 27 | 27 | ||
| 28 | env->SetObjectField(object, | 28 | env->SetObjectField(object, |
| @@ -78,7 +78,7 @@ static jobject ToJKeyboardParams(const Core::Frontend::KeyboardInitializeParamet | |||
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | AndroidKeyboard::ResultData AndroidKeyboard::ResultData::CreateFromFrontend(jobject object) { | 80 | AndroidKeyboard::ResultData AndroidKeyboard::ResultData::CreateFromFrontend(jobject object) { |
| 81 | JNIEnv* env = IDCache::GetEnvForThread(); | 81 | JNIEnv* env = GetEnvForThread(); |
| 82 | const jstring string = reinterpret_cast<jstring>(env->GetObjectField( | 82 | const jstring string = reinterpret_cast<jstring>(env->GetObjectField( |
| 83 | object, env->GetFieldID(s_keyboard_data_class, "text", "Ljava/lang/String;"))); | 83 | object, env->GetFieldID(s_keyboard_data_class, "text", "Ljava/lang/String;"))); |
| 84 | return ResultData{GetJString(env, string), | 84 | return ResultData{GetJString(env, string), |
| @@ -141,7 +141,7 @@ void AndroidKeyboard::ShowNormalKeyboard() const { | |||
| 141 | 141 | ||
| 142 | // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber. | 142 | // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber. |
| 143 | std::thread([&] { | 143 | std::thread([&] { |
| 144 | data = ResultData::CreateFromFrontend(IDCache::GetEnvForThread()->CallStaticObjectMethod( | 144 | data = ResultData::CreateFromFrontend(GetEnvForThread()->CallStaticObjectMethod( |
| 145 | s_software_keyboard_class, s_swkbd_execute_normal, ToJKeyboardParams(parameters))); | 145 | s_software_keyboard_class, s_swkbd_execute_normal, ToJKeyboardParams(parameters))); |
| 146 | }).join(); | 146 | }).join(); |
| 147 | 147 | ||
| @@ -183,8 +183,8 @@ void AndroidKeyboard::ShowInlineKeyboard( | |||
| 183 | // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber. | 183 | // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber. |
| 184 | m_is_inline_active = true; | 184 | m_is_inline_active = true; |
| 185 | std::thread([&] { | 185 | std::thread([&] { |
| 186 | IDCache::GetEnvForThread()->CallStaticVoidMethod( | 186 | GetEnvForThread()->CallStaticVoidMethod(s_software_keyboard_class, s_swkbd_execute_inline, |
| 187 | s_software_keyboard_class, s_swkbd_execute_inline, ToJKeyboardParams(parameters)); | 187 | ToJKeyboardParams(parameters)); |
| 188 | }).join(); | 188 | }).join(); |
| 189 | } | 189 | } |
| 190 | 190 | ||
| @@ -220,7 +220,7 @@ void AndroidKeyboard::SubmitInlineKeyboardText(std::u16string submitted_text) { | |||
| 220 | m_current_text += submitted_text; | 220 | m_current_text += submitted_text; |
| 221 | 221 | ||
| 222 | submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text, | 222 | submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text, |
| 223 | m_current_text.size()); | 223 | static_cast<int>(m_current_text.size())); |
| 224 | } | 224 | } |
| 225 | 225 | ||
| 226 | void AndroidKeyboard::SubmitInlineKeyboardInput(int key_code) { | 226 | void AndroidKeyboard::SubmitInlineKeyboardInput(int key_code) { |
| @@ -242,7 +242,7 @@ void AndroidKeyboard::SubmitInlineKeyboardInput(int key_code) { | |||
| 242 | case KEYCODE_DEL: | 242 | case KEYCODE_DEL: |
| 243 | m_current_text.pop_back(); | 243 | m_current_text.pop_back(); |
| 244 | submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text, | 244 | submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text, |
| 245 | m_current_text.size()); | 245 | static_cast<int>(m_current_text.size())); |
| 246 | break; | 246 | break; |
| 247 | } | 247 | } |
| 248 | } | 248 | } |
| @@ -274,4 +274,4 @@ void CleanupJNI(JNIEnv* env) { | |||
| 274 | env->DeleteGlobalRef(s_keyboard_data_class); | 274 | env->DeleteGlobalRef(s_keyboard_data_class); |
| 275 | } | 275 | } |
| 276 | 276 | ||
| 277 | } // namespace SoftwareKeyboard | 277 | } // namespace Common::Android::SoftwareKeyboard |
diff --git a/src/android/app/src/main/jni/applets/software_keyboard.h b/src/common/android/applets/software_keyboard.h index 2affc01f6..9fd09d27c 100644 --- a/src/android/app/src/main/jni/applets/software_keyboard.h +++ b/src/common/android/applets/software_keyboard.h | |||
| @@ -7,7 +7,7 @@ | |||
| 7 | 7 | ||
| 8 | #include "core/frontend/applets/software_keyboard.h" | 8 | #include "core/frontend/applets/software_keyboard.h" |
| 9 | 9 | ||
| 10 | namespace SoftwareKeyboard { | 10 | namespace Common::Android::SoftwareKeyboard { |
| 11 | 11 | ||
| 12 | class AndroidKeyboard final : public Core::Frontend::SoftwareKeyboardApplet { | 12 | class AndroidKeyboard final : public Core::Frontend::SoftwareKeyboardApplet { |
| 13 | public: | 13 | public: |
| @@ -66,7 +66,7 @@ void InitJNI(JNIEnv* env); | |||
| 66 | // Should be called in JNI_Unload | 66 | // Should be called in JNI_Unload |
| 67 | void CleanupJNI(JNIEnv* env); | 67 | void CleanupJNI(JNIEnv* env); |
| 68 | 68 | ||
| 69 | } // namespace SoftwareKeyboard | 69 | } // namespace Common::Android::SoftwareKeyboard |
| 70 | 70 | ||
| 71 | // Native function calls | 71 | // Native function calls |
| 72 | extern "C" { | 72 | extern "C" { |
diff --git a/src/android/app/src/main/jni/id_cache.cpp b/src/common/android/id_cache.cpp index f30100bd8..f39262db9 100644 --- a/src/android/app/src/main/jni/id_cache.cpp +++ b/src/common/android/id_cache.cpp | |||
| @@ -3,10 +3,10 @@ | |||
| 3 | 3 | ||
| 4 | #include <jni.h> | 4 | #include <jni.h> |
| 5 | 5 | ||
| 6 | #include "applets/software_keyboard.h" | ||
| 7 | #include "common/android/id_cache.h" | ||
| 6 | #include "common/assert.h" | 8 | #include "common/assert.h" |
| 7 | #include "common/fs/fs_android.h" | 9 | #include "common/fs/fs_android.h" |
| 8 | #include "jni/applets/software_keyboard.h" | ||
| 9 | #include "jni/id_cache.h" | ||
| 10 | #include "video_core/rasterizer_interface.h" | 10 | #include "video_core/rasterizer_interface.h" |
| 11 | 11 | ||
| 12 | static JavaVM* s_java_vm; | 12 | static JavaVM* s_java_vm; |
| @@ -67,7 +67,7 @@ static jfieldID s_boolean_value_field; | |||
| 67 | 67 | ||
| 68 | static constexpr jint JNI_VERSION = JNI_VERSION_1_6; | 68 | static constexpr jint JNI_VERSION = JNI_VERSION_1_6; |
| 69 | 69 | ||
| 70 | namespace IDCache { | 70 | namespace Common::Android { |
| 71 | 71 | ||
| 72 | JNIEnv* GetEnvForThread() { | 72 | JNIEnv* GetEnvForThread() { |
| 73 | thread_local static struct OwnedEnv { | 73 | thread_local static struct OwnedEnv { |
| @@ -276,8 +276,6 @@ jfieldID GetBooleanValueField() { | |||
| 276 | return s_boolean_value_field; | 276 | return s_boolean_value_field; |
| 277 | } | 277 | } |
| 278 | 278 | ||
| 279 | } // namespace IDCache | ||
| 280 | |||
| 281 | #ifdef __cplusplus | 279 | #ifdef __cplusplus |
| 282 | extern "C" { | 280 | extern "C" { |
| 283 | #endif | 281 | #endif |
| @@ -393,7 +391,7 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { | |||
| 393 | Common::FS::Android::RegisterCallbacks(env, s_native_library_class); | 391 | Common::FS::Android::RegisterCallbacks(env, s_native_library_class); |
| 394 | 392 | ||
| 395 | // Initialize applets | 393 | // Initialize applets |
| 396 | SoftwareKeyboard::InitJNI(env); | 394 | Common::Android::SoftwareKeyboard::InitJNI(env); |
| 397 | 395 | ||
| 398 | return JNI_VERSION; | 396 | return JNI_VERSION; |
| 399 | } | 397 | } |
| @@ -426,3 +424,5 @@ void JNI_OnUnload(JavaVM* vm, void* reserved) { | |||
| 426 | #ifdef __cplusplus | 424 | #ifdef __cplusplus |
| 427 | } | 425 | } |
| 428 | #endif | 426 | #endif |
| 427 | |||
| 428 | } // namespace Common::Android | ||
diff --git a/src/android/app/src/main/jni/id_cache.h b/src/common/android/id_cache.h index 00e48afc0..47802f96c 100644 --- a/src/android/app/src/main/jni/id_cache.h +++ b/src/common/android/id_cache.h | |||
| @@ -3,20 +3,40 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <future> | ||
| 6 | #include <jni.h> | 7 | #include <jni.h> |
| 7 | 8 | ||
| 8 | #include "video_core/rasterizer_interface.h" | 9 | #include "video_core/rasterizer_interface.h" |
| 9 | 10 | ||
| 10 | namespace IDCache { | 11 | namespace Common::Android { |
| 11 | 12 | ||
| 12 | JNIEnv* GetEnvForThread(); | 13 | JNIEnv* GetEnvForThread(); |
| 14 | |||
| 15 | /** | ||
| 16 | * Starts a new thread to run JNI. Intended to be used when you must run JNI from a fiber. | ||
| 17 | * @tparam T Typename of the return value for the work param | ||
| 18 | * @param work Lambda that runs JNI code. This function will take care of attaching this thread to | ||
| 19 | * the JVM | ||
| 20 | * @return The result from the work lambda param | ||
| 21 | */ | ||
| 22 | template <typename T = void> | ||
| 23 | T RunJNIOnFiber(const std::function<T(JNIEnv*)>& work) { | ||
| 24 | std::future<T> j_result = std::async(std::launch::async, [&] { | ||
| 25 | auto env = GetEnvForThread(); | ||
| 26 | return work(env); | ||
| 27 | }); | ||
| 28 | return j_result.get(); | ||
| 29 | } | ||
| 30 | |||
| 13 | jclass GetNativeLibraryClass(); | 31 | jclass GetNativeLibraryClass(); |
| 32 | |||
| 14 | jclass GetDiskCacheProgressClass(); | 33 | jclass GetDiskCacheProgressClass(); |
| 15 | jclass GetDiskCacheLoadCallbackStageClass(); | 34 | jclass GetDiskCacheLoadCallbackStageClass(); |
| 16 | jclass GetGameDirClass(); | 35 | jclass GetGameDirClass(); |
| 17 | jmethodID GetGameDirConstructor(); | 36 | jmethodID GetGameDirConstructor(); |
| 18 | jmethodID GetExitEmulationActivity(); | ||
| 19 | jmethodID GetDiskCacheLoadProgress(); | 37 | jmethodID GetDiskCacheLoadProgress(); |
| 38 | |||
| 39 | jmethodID GetExitEmulationActivity(); | ||
| 20 | jmethodID GetOnEmulationStarted(); | 40 | jmethodID GetOnEmulationStarted(); |
| 21 | jmethodID GetOnEmulationStopped(); | 41 | jmethodID GetOnEmulationStopped(); |
| 22 | jmethodID GetOnProgramChanged(); | 42 | jmethodID GetOnProgramChanged(); |
| @@ -65,4 +85,4 @@ jclass GetBooleanClass(); | |||
| 65 | jmethodID GetBooleanConstructor(); | 85 | jmethodID GetBooleanConstructor(); |
| 66 | jfieldID GetBooleanValueField(); | 86 | jfieldID GetBooleanValueField(); |
| 67 | 87 | ||
| 68 | } // namespace IDCache | 88 | } // namespace Common::Android |