Browse Source

pipeline: support android no thread build

Closes #1323
pmp-p 3 years ago
parent
commit
2208cc8bff

+ 10 - 0
panda/src/pipeline/threadDummyImpl.I

@@ -132,3 +132,13 @@ INLINE bool ThreadDummyImpl::
 get_context_switches(size_t &, size_t &) {
   return false;
 }
+
+#ifdef ANDROID
+/**
+ * Returns the JNIEnv object for the current thread.
+ */
+INLINE JNIEnv *ThreadDummyImpl::
+get_jni_env() const {
+  return _jni_env;
+}
+#endif

+ 39 - 0
panda/src/pipeline/threadDummyImpl.cxx

@@ -25,6 +25,13 @@
 #include <windows.h>
 #endif
 
+#ifdef ANDROID
+#include "config_express.h"
+#include <jni.h>
+
+static JavaVM *java_vm = nullptr;
+#endif
+
 /**
  *
  */
@@ -48,4 +55,36 @@ get_current_thread() {
   return Thread::get_main_thread();
 }
 
+#ifdef ANDROID
+/**
+ * Attaches the thread to the Java virtual machine.  If this returns true, a
+ * JNIEnv pointer can be acquired using get_jni_env().
+ */
+bool ThreadDummyImpl::
+attach_java_vm() {
+  assert(java_vm != nullptr);
+  JNIEnv *env;
+  JavaVMAttachArgs args;
+  args.version = JNI_VERSION_1_2;
+  args.name = "Main";
+  args.group = nullptr;
+  if (java_vm->AttachCurrentThread(&env, &args) != 0) {
+    thread_cat.error()
+      << "Failed to attach Java VM to thread ";
+      _jni_env = nullptr;
+    return false;
+  }
+  _jni_env = env;
+  return true;
+}
+
+/**
+ * Binds the Panda thread to the current thread, assuming that the current
+ * thread is already a valid attached Java thread.  Called by JNI_OnLoad.
+ */
+void ThreadDummyImpl::
+bind_java_thread() {
+}
+#endif  // ANDROID
+
 #endif  // THREAD_DUMMY_IMPL

+ 16 - 0
panda/src/pipeline/threadDummyImpl.h

@@ -31,6 +31,11 @@ class Thread;
 #include <windows.h>  // For Sleep().
 #endif
 
+#ifdef ANDROID
+#include <jni.h>
+typedef struct _JNIEnv _jni_env;
+#endif
+
 /**
  * A fake thread implementation for single-threaded applications.  This simply
  * fails whenever you try to start a thread.
@@ -58,7 +63,18 @@ public:
   INLINE static void yield();
   INLINE static void consider_yield();
 
+#ifdef ANDROID
+  INLINE JNIEnv *get_jni_env() const;
+  bool attach_java_vm();
+  static void bind_java_thread();
+#endif
+
   INLINE static bool get_context_switches(size_t &, size_t &);
+
+private:
+#ifdef ANDROID
+  JNIEnv *_jni_env = nullptr;
+#endif
 };
 
 #include "threadDummyImpl.I"