10#if defined(__ANDROID__)
18namespace pulp::android {
22inline JavaVM* g_vm =
nullptr;
29void init_asset_manager(JNIEnv* env, jobject context);
30std::vector<uint8_t> read_asset(
const std::string& path);
31std::string read_asset_text(
const std::string& path);
32bool has_asset_manager();
38 assert(g_vm &&
"JNI_OnLoad not called — System.loadLibrary missing?");
39 JNIEnv* env =
nullptr;
40 auto result = g_vm->GetEnv(
reinterpret_cast<void**
>(&env), JNI_VERSION_1_6);
41 if (result == JNI_EDETACHED) {
42 g_vm->AttachCurrentThread(&env,
nullptr);
54 LocalRef(JNIEnv* env, T ref) : env_(env), ref_(ref) {}
55 ~LocalRef() {
if (ref_) env_->DeleteLocalRef(ref_); }
57 LocalRef(
const LocalRef&) =
delete;
58 LocalRef& operator=(
const LocalRef&) =
delete;
59 LocalRef(LocalRef&& other) noexcept : env_(
other.env_), ref_(
other.ref_) {
other.ref_ =
nullptr; }
60 LocalRef& operator=(LocalRef&& other)
noexcept {
62 if (ref_) env_->DeleteLocalRef(ref_);
70 T get()
const {
return ref_; }
71 explicit operator bool()
const {
return ref_ !=
nullptr; }
72 T
release() { T
r = ref_; ref_ =
nullptr;
return r; }
82 jobject ref_ =
nullptr;
85 static inline std::atomic<int> g_count{0};
89 GlobalRef() =
default;
91 GlobalRef(JNIEnv* env, jobject local) {
93 ref_ = env->NewGlobalRef(local);
95 int count = g_count.fetch_add(1) + 1;
96 assert(count < 1000 &&
"Possible JNI Global Reference leak — over 1000 active refs");
103 get_env()->DeleteGlobalRef(ref_);
105 g_count.fetch_sub(1);
110 GlobalRef(
const GlobalRef&) =
delete;
111 GlobalRef& operator=(
const GlobalRef&) =
delete;
112 GlobalRef(GlobalRef&& other) noexcept : ref_(
other.ref_) {
other.ref_ =
nullptr; }
113 GlobalRef& operator=(GlobalRef&& other)
noexcept {
114 if (
this != &other) {
116 get_env()->DeleteGlobalRef(ref_);
118 g_count.fetch_sub(1);
122 other.ref_ =
nullptr;
127 jobject get()
const {
return ref_; }
128 explicit operator bool()
const {
return ref_ !=
nullptr; }
131 static int active_count() {
return g_count.load(); }
142 FloatArrayPin(JNIEnv* env, jfloatArray array)
143 : env_(env), array_(array)
144 , data_(env->GetFloatArrayElements(array, nullptr)) {}
147 if (data_) env_->ReleaseFloatArrayElements(array_, data_, 0);
150 FloatArrayPin(
const FloatArrayPin&) =
delete;
151 FloatArrayPin& operator=(
const FloatArrayPin&) =
delete;
153 jfloat* data() {
return data_; }
154 const jfloat* data()
const {
return data_; }
155 explicit operator bool()
const {
return data_ !=
nullptr; }
156 jsize
size()
const {
return env_->GetArrayLength(array_); }
163 inline jclass PulpBridge =
nullptr;
164 inline jclass PulpAudioEngine =
nullptr;
165 inline jclass PulpMidiManager =
nullptr;
166 inline jclass PulpFileProvider =
nullptr;
167 inline jclass PulpActivity =
nullptr;
171inline jclass cache_class(JNIEnv* env,
const char* name) {
172 jclass local = env->FindClass(name);
178 jclass global =
static_cast<jclass
>(env->NewGlobalRef(local));
179 env->DeleteLocalRef(local);
@ count
sentinel: number of real capabilities (keep LAST)
Definition canvas_capability.hpp:38
std::optional< std::string > get_env(std::string_view name)
Definition system.hpp:14
@ other
Definition udev_monitor.hpp:26
@ size
Definition config.hpp:216
@ r
Definition input_events.hpp:41