Skip to content

Commit

Permalink
feat: Rewrite Android C++ part (VisionCameraProxy + JFrame) (#1661)
Browse files Browse the repository at this point in the history
* First Android rewrite

* Rewrite Android C++ backend

* Pass `ReadableNativeMap`, fix build error

* fix: Fix FrameProcessor init

* Make a bunch of stuff const reference to avoid copies

* Indents

* Cleanup

* indents

* docs: Update Android docs

* Update CameraView.kt

* fix: Format C++ code
  • Loading branch information
mrousavy authored Jul 21, 2023
1 parent 44ed42d commit 86dd703
Show file tree
Hide file tree
Showing 45 changed files with 984 additions and 858 deletions.
14 changes: 8 additions & 6 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ add_library(
${PACKAGE_NAME}
SHARED
../cpp/JSITypedArray.cpp
src/main/cpp/VisionCamera.cpp
src/main/cpp/JSIJNIConversion.cpp
src/main/cpp/FrameHostObject.cpp
src/main/cpp/FrameProcessorRuntimeManager.cpp
src/main/cpp/CameraView.cpp
src/main/cpp/VisionCameraScheduler.cpp
src/main/cpp/FrameProcessorPluginHostObject.cpp
src/main/cpp/JSIJNIConversion.cpp
src/main/cpp/VisionCamera.cpp
src/main/cpp/VisionCameraProxy.cpp
src/main/cpp/java-bindings/JFrame.cpp
src/main/cpp/java-bindings/JFrameProcessor.cpp
src/main/cpp/java-bindings/JFrameProcessorPlugin.cpp
src/main/cpp/java-bindings/JImageProxy.cpp
src/main/cpp/java-bindings/JHashMap.cpp
src/main/cpp/java-bindings/JVisionCameraProxy.cpp
src/main/cpp/java-bindings/JVisionCameraScheduler.cpp
)

# Header Search Paths (includes)
Expand Down
58 changes: 0 additions & 58 deletions android/src/main/cpp/CameraView.cpp

This file was deleted.

43 changes: 0 additions & 43 deletions android/src/main/cpp/CameraView.h

This file was deleted.

2 changes: 1 addition & 1 deletion android/src/main/cpp/FrameHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace vision {

using namespace facebook;

FrameHostObject::FrameHostObject(jni::alias_ref<JImageProxy::javaobject> image): frame(make_global(image)), _refCount(0) { }
FrameHostObject::FrameHostObject(const jni::alias_ref<JFrame::javaobject>& frame): frame(make_global(frame)), _refCount(0) { }

FrameHostObject::~FrameHostObject() {
// Hermes' Garbage Collector (Hades GC) calls destructors on a separate Thread
Expand Down
6 changes: 3 additions & 3 deletions android/src/main/cpp/FrameHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
#include <string>
#include <mutex>

#include "java-bindings/JImageProxy.h"
#include "java-bindings/JFrame.h"

namespace vision {

using namespace facebook;

class JSI_EXPORT FrameHostObject : public jsi::HostObject {
explicit FrameHostObject(const jni::alias_ref<JFrame::javaobject>& frame);
public:
explicit FrameHostObject(jni::alias_ref<JImageProxy::javaobject> image);
~FrameHostObject();

public:
jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) override;
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;

public:
jni::global_ref<JImageProxy> frame;
jni::global_ref<JFrame> frame;

private:
static auto constexpr TAG = "VisionCamera";
Expand Down
53 changes: 53 additions & 0 deletions android/src/main/cpp/FrameProcessorPluginHostObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by Marc Rousavy on 21.07.23.
//

#include "FrameProcessorPluginHostObject.h"
#include <vector>
#include "FrameHostObject.h"
#include "JSIJNIConversion.h"
#include <string>

namespace vision {

using namespace facebook;

std::vector<jsi::PropNameID> FrameProcessorPluginHostObject::getPropertyNames(jsi::Runtime& runtime) {
std::vector<jsi::PropNameID> result;
result.push_back(jsi::PropNameID::forUtf8(runtime, std::string("call")));
return result;
}

jsi::Value FrameProcessorPluginHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propName) {
auto name = propName.utf8(runtime);

if (name == "call") {
return jsi::Function::createFromHostFunction(runtime,
jsi::PropNameID::forUtf8(runtime, "call"),
2,
[=](jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
// Frame is first argument
auto frameHostObject = arguments[0].asObject(runtime).asHostObject<FrameHostObject>(runtime);
auto frame = frameHostObject->frame;

// Options are second argument (possibly undefined)
local_ref<react::ReadableNativeMap::javaobject> options = nullptr;
if (count > 1) {
options = JSIJNIConversion::convertJSIObjectToJNIMap(runtime, arguments[1].asObject(runtime));
}

// Call actual plugin
auto result = _plugin->callback(frame, options);

// Convert result value to jsi::Value (possibly undefined)
return JSIJNIConversion::convertJNIObjectToJSIValue(runtime, result);
});
}

return jsi::Value::undefined();
}

} // namespace vision
32 changes: 32 additions & 0 deletions android/src/main/cpp/FrameProcessorPluginHostObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by Marc Rousavy on 21.07.23.
//

#pragma once

#include <jsi/jsi.h>
#include "java-bindings/JFrameProcessorPlugin.h"
#include <memory>
#include <ReactCommon/CallInvoker.h>
#include <fbjni/fbjni.h>
#include <vector>

namespace vision {

using namespace facebook;

class FrameProcessorPluginHostObject: public jsi::HostObject {
public:
explicit FrameProcessorPluginHostObject(jni::alias_ref<JFrameProcessorPlugin::javaobject> plugin):
_plugin(make_global(plugin)) { }
~FrameProcessorPluginHostObject() { }

public:
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& runtime) override;
jsi::Value get(jsi::Runtime& runtime, const jsi::PropNameID& name) override;

private:
jni::global_ref<JFrameProcessorPlugin::javaobject> _plugin;
};

} // namespace vision
Loading

0 comments on commit 86dd703

Please sign in to comment.