/* * WiVRn VR streaming * Copyright (C) 2023 Guillaume Meunier * Copyright (C) 2023 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include #include #include #include #include #include namespace jni { class jni_thread { JNIEnv * jni_env = nullptr; JavaVM * vm = nullptr; jni_thread() = default; ~jni_thread() { if (vm) vm->DetachCurrentThread(); } static jni_thread & instance() { thread_local jni_thread inst; return inst; } public: static JNIEnv & env() { auto res = instance().jni_env; assert(res); return *res; } static void setup_thread(JavaVM * vm) { auto & inst = instance(); if (inst.vm) return; inst.vm = vm; inst.vm->AttachCurrentThread(&inst.jni_env, nullptr); } static void setup_thread(JNIEnv * env) { auto & inst = instance(); inst.jni_env = env; } static void detach() { instance() = jni_thread(); } }; struct Void { static std::string type() { return "V"; } constexpr static const auto call_method = &_JNIEnv::CallVoidMethod; constexpr static const auto call_static_method = &_JNIEnv::CallStaticVoidMethod; }; struct Int { constexpr static auto static_field = &_JNIEnv::GetStaticIntField; constexpr static auto obj_field = &_JNIEnv::GetIntField; constexpr static const auto call_method = &_JNIEnv::CallIntMethod; constexpr static const auto call_static_method = &_JNIEnv::CallStaticIntMethod; static std::string type() { return "I"; } int value; int handle() const { return value; } operator int() const { return value; } }; namespace details { struct deleter { void operator()(jobject j) { jni_thread::env().DeleteGlobalRef(j); } void operator()(jobjectArray j) { jni_thread::env().DeleteGlobalRef(j); } }; template std::string build_type(Args &&... args) { return (std::string() + ... + std::remove_cvref_t::type()); } template struct string_literal { constexpr string_literal(const char (&str)[N]) { std::copy_n(str, N, value); }; char value[N]; }; inline auto handle() { return std::tuple<>(); } template auto handle(const T & t, Args &&... args) { return std::tuple_cat(std::make_tuple(t.handle()), handle(std::forward(args)...)); } template struct type_map { using type = T; }; template <> struct type_map { using type = jni::Void; }; template <> struct type_map { using type = jni::Int; }; template using type_map_t = type_map::type; void handle_java_exception(); } // namespace details struct klass { std::unique_ptr, details::deleter> self; operator jclass() { return self.get(); } template klass(T & instance) { auto & env = jni_thread::env(); self.reset((jclass)env.NewGlobalRef(env.GetObjectClass(instance))); assert(self.get()); } klass(const char * name) { auto & env = jni_thread::env(); self.reset((jclass)env.NewGlobalRef(env.FindClass(name))); assert(self.get()); } template T field(const std::string & name) { auto & env = jni_thread::env(); jfieldID id = env.GetStaticFieldID(*this, name.c_str(), T::type().c_str()); return T((env.*T::static_field)(*this, id)); } template auto call(const char * method, Args &&... args) { using R1 = details::type_map_t; auto & env = jni_thread::env(); std::string signature = "(" + details::build_type(args...) + ")" + R1::type(); auto method_id = env.GetStaticMethodID(self.get(), method, signature.c_str()); assert(method_id); auto handles = details::handle(std::forward(args)...); return R(std::apply([&](auto &... t) { if constexpr (std::is_void_v) { (env.*R1::call_static_method)(*this, method_id, t...); details::handle_java_exception(); } else { auto res = (env.*R1::call_static_method)(*this, method_id, t...); details::handle_java_exception(); return res; } }, handles)); } template auto method(const char * method, Args &&... args) { using R1 = details::type_map_t; auto & env = jni_thread::env(); std::string signature = "(" + details::build_type(args...) + ")" + R1::type(); auto method_id = env.GetMethodID(self.get(), method, signature.c_str()); assert(method_id); return method_id; } }; struct Bool { static std::string type() { return "Z"; } constexpr static const auto call_method = &_JNIEnv::CallBooleanMethod; constexpr static const auto call_static_method = &_JNIEnv::CallStaticBooleanMethod; bool value; int handle() const { return value; } operator bool() const { return value; } }; template struct object { static std::string type() { return std::string("L") + Type.value + ";"; } std::unique_ptr, details::deleter> self; constexpr static auto obj_field = &_JNIEnv::GetObjectField; constexpr static const auto call_method = &_JNIEnv::CallObjectMethod; constexpr static const auto call_static_method = &_JNIEnv::CallStaticObjectMethod; object(jobject o) { auto & env = jni_thread::env(); if (o) self.reset(env.NewGlobalRef(o)); } template auto call(const char * method, Args &&... args) { using R1 = details::type_map_t; auto & env = jni_thread::env(); std::string signature = "(" + details::build_type(args...) + ")" + R1::type(); auto method_id = env.GetMethodID(klass(), method, signature.c_str()); assert(method_id); auto handles = details::handle(std::forward(args)...); return R(std::apply([&](auto &... t) { if constexpr (std::is_void_v) { (env.*R1::call_method)(*this, method_id, t...); details::handle_java_exception(); } else { auto res = (env.*R1::call_method)(*this, method_id, t...); details::handle_java_exception(); return res; } }, handles)); } template auto call(jmethodID method_id, Args &&... args) { using R1 = details::type_map_t; auto & env = jni_thread::env(); assert(method_id); auto handles = details::handle(std::forward(args)...); return R(std::apply([&](auto &... t) { if constexpr (std::is_void_v) { (env.*R1::call_method)(*this, method_id, t...); details::handle_java_exception(); } else { auto res = (env.*R1::call_method)(*this, method_id, t...); details::handle_java_exception(); return res; } }, handles)); } template T field(const std::string & name) { auto & env = jni_thread::env(); jfieldID id = env.GetFieldID(klass(), name.c_str(), T::type().c_str()); return T((env.*T::obj_field)(*this, id)); } jobject handle() const { return *this; } operator jobject() const { return self.get(); } klass klass() { return *this; } }; template static object new_object(Args &&... args) { auto & env = jni_thread::env(); auto klass = jni::klass(Type.value); std::string signature = "(" + details::build_type(args...) + ")V"; auto method_id = env.GetMethodID(klass, "", signature.c_str()); assert(method_id); auto handles = details::handle(std::forward(args)...); return object(std::apply([&](auto &... t) { auto res = env.NewObject(klass, method_id, t...); details::handle_java_exception(); return res; }, handles)); } using string_t = object<"java/lang/String">; struct string : public string_t { using string_t::type; constexpr static auto static_field = &_JNIEnv::GetStaticObjectField; constexpr static auto obj_field = &_JNIEnv::GetObjectField; operator jstring() { return (jstring)self.get(); } string(const char * str) : string_t(jni_thread::env().NewStringUTF(str)) {} string(jobject j) : string_t(j) {} operator std::string() { auto & env = jni_thread::env(); const char * dataString_c = env.GetStringUTFChars(*this, NULL); std::string res = dataString_c; env.ReleaseStringUTFChars(*this, dataString_c); return res; } }; template struct array { static std::string type() { return std::string("[") + T::type(); } std::unique_ptr, details::deleter> self; auto handle() const { return self.get(); } array(T & element) { auto & env = jni_thread::env(); self.reset((jobjectArray)env.NewGlobalRef(env.NewObjectArray(1, element.klass(), element))); } }; } // namespace jni