fix: Make HardwareBuffers compile optionally (opt-out in minSdkVersion <26) (#1910)

* fix: Make HardwareBuffers compile optionally

* chore: Format C++ code

* fix: Fix if
This commit is contained in:
Marc Rousavy 2023-10-03 13:18:41 +02:00 committed by GitHub
parent 83c0cdb030
commit 8d90dcc476
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 4 deletions

View File

@ -85,6 +85,7 @@ jsi::Value FrameHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pr
if (name == "toArrayBuffer") {
jsi::HostFunctionType toArrayBuffer = [=](jsi::Runtime& runtime, const jsi::Value& thisArg, const jsi::Value* args,
size_t count) -> jsi::Value {
#if __ANDROID_API__ >= 26
AHardwareBuffer* hardwareBuffer = this->frame->getHardwareBuffer();
AHardwareBuffer_Desc bufferDescription;
@ -120,6 +121,9 @@ jsi::Value FrameHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pr
AHardwareBuffer_release(hardwareBuffer);
return arrayBuffer;
#else
throw jsi::JSError(runtime, "Frame.toArrayBuffer() is only available if minSdkVersion is set to 26 or higher!");
#endif
};
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "toArrayBuffer"), 0, toArrayBuffer);
}

View File

@ -4,11 +4,11 @@
#include "JFrame.h"
#include <android/hardware_buffer_jni.h>
#include <fbjni/ByteBuffer.h>
#include <fbjni/fbjni.h>
#include <jni.h>
#include <android/hardware_buffer_jni.h>
namespace vision {
using namespace facebook;
@ -59,11 +59,13 @@ int JFrame::getBytesPerRow() const {
return getBytesPerRowMethod(self());
}
#if __ANDROID_API__ >= 26
AHardwareBuffer* JFrame::getHardwareBuffer() const {
static const auto getHardwareBufferMethod = getClass()->getMethod<jobject()>("getHardwareBufferBoxed");
auto hardwareBuffer = getHardwareBufferMethod(self());
return AHardwareBuffer_fromHardwareBuffer(jni::Environment::current(), hardwareBuffer.get());
}
#endif
void JFrame::incrementRefCount() {
static const auto incrementRefCountMethod = getClass()->getMethod<void()>("incrementRefCount");

View File

@ -4,11 +4,11 @@
#pragma once
#include <android/hardware_buffer.h>
#include <fbjni/ByteBuffer.h>
#include <fbjni/fbjni.h>
#include <jni.h>
#include <android/hardware_buffer.h>
namespace vision {
using namespace facebook;
@ -27,7 +27,10 @@ public:
jlong getTimestamp() const;
local_ref<JString> getOrientation() const;
local_ref<JString> getPixelFormat() const;
#if __ANDROID_API__ >= 26
AHardwareBuffer* getHardwareBuffer() const;
#endif
void incrementRefCount();
void decrementRefCount();
void close();