Skip to content

Commit

Permalink
fix: android inference with faster yuv->rgb conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Parker authored and Charles Parker committed Apr 18, 2024
1 parent 7ad0e38 commit 588f2e6
Show file tree
Hide file tree
Showing 25 changed files with 992 additions and 207 deletions.
26 changes: 26 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Config for clang-format version 16

# Standard
BasedOnStyle: llvm
Standard: c++14

# Indentation
IndentWidth: 2
ColumnLimit: 140

# Includes
SortIncludes: true
SortUsingDeclarations: true

# Pointer and reference alignment
PointerAlignment: Left
ReferenceAlignment: Left
ReflowComments: true

# Line breaking options
BreakBeforeBraces: Attach
BreakConstructorInitializers: BeforeColon
AllowShortFunctionsOnASingleLine: Empty
IndentCaseLabels: true
NamespaceIndentation: Inner

2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
"./tsconfig.json",
"docsite/tsconfig.json",
"examples/objectdetection/tsconfig.json",
"example/tsconfig.json",
],
ecmaFeatures: {
jsx: true,
Expand All @@ -25,7 +24,6 @@ module.exports = {
"*.config.js",
"jest.setup.js",
"coverage",
"example/index.js",
],
plugins: ["@typescript-eslint"],
extends: [
Expand Down
7 changes: 0 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ project.xcworkspace
local.properties
android.iml

# Cocoapods
#
example/ios/Pods

# Ruby
example/vendor/

# node.js
#
node_modules/
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libyuv"]
path = libyuv
url = https://android.googlesource.com/platform/external/libyuv/
36 changes: 36 additions & 0 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
project(ResizeConvertLib)
cmake_minimum_required(VERSION 3.9.0)

set (PACKAGE_NAME "ResizeConvertLib")
set (BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_CXX_STANDARD 17)

# Third party libraries (Prefabs)
find_package(fbjni REQUIRED CONFIG)
find_library(LOG_LIB log)

# libyuv
add_subdirectory(../libyuv libyuv)

add_library(${PACKAGE_NAME} SHARED
src/main/cpp/ResizeConvert.cpp
src/main/cpp/JImage.cpp
src/main/cpp/JImagePlane.cpp
src/main/cpp/ResizeConvertLib.cpp
)

# Specifies a path to native header files.
target_include_directories(
${PACKAGE_NAME} PRIVATE
src/main/cpp
../libyuv/include
)

target_link_libraries(
${PACKAGE_NAME}
${LOG_LIB} # <-- Logcat logger
android # <-- Android JNI core
fbjni::fbjni # <-- fbjni
yuv # <-- libyuv
)
42 changes: 42 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"
}

def reactNativeArchitectures() {
def value = project.getProperties().get("reactNativeArchitectures")
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

def getExtOrDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Mediapipe_" + name]
}
Expand Down Expand Up @@ -53,12 +58,30 @@ android {
}
}

buildFeatures {
prefab true
}

ndkVersion getExtOrDefault("ndkVersion")
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")

defaultConfig {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")

externalNativeBuild {
cmake {
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
abiFilters (*reactNativeArchitectures())
arguments "-DANDROID_STL=c++_shared"
}
}
}

externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}

buildTypes {
Expand All @@ -75,6 +98,25 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

// packagingOptions {
// excludes = [
// "**/libc++_shared.so",
// "**/libfbjni.so",
// "**/libjsi.so",
// "**/libfolly_json.so",
// "**/libfolly_runtime.so",
// "**/libglog.so",
// "**/libhermes.so",
// "**/libhermes-executor-debug.so",
// "**/libhermes_executor.so",
// "**/libreactnativejni.so",
// "**/libturbomodulejsijni.so",
// "**/libreact_nativemodule_core.so",
// "**/libjscexecutor.so"
// ]
// }

}

repositories {
Expand Down
33 changes: 33 additions & 0 deletions android/src/main/cpp/JImage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Created by Marc Rousavy on 25.01.24.
//

#include "JImage.h"

#include <fbjni/fbjni.h>
#include <jni.h>

namespace resizeconvert {

using namespace facebook;
using namespace jni;

int JImage::getWidth() const {
auto method = getClass()->getMethod<jint()>("getWidth");
auto result = method(self());
return result;
}

int JImage::getHeight() const {
auto method = getClass()->getMethod<jint()>("getHeight");
auto result = method(self());
return result;
}

jni::local_ref<jni::JArrayClass<JImagePlane>> JImage::getPlanes() const {
auto method = getClass()->getMethod<jni::JArrayClass<JImagePlane>()>("getPlanes");
auto result = method(self());
return result;
}

} // namespace resizeconvert
27 changes: 27 additions & 0 deletions android/src/main/cpp/JImage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by Marc Rousavy on 25.01.24.
//

#pragma once

#include "JImagePlane.h"
#include <fbjni/fbjni.h>
#include <jni.h>

namespace resizeconvert
{

using namespace facebook;
using namespace jni;

struct JImage : public JavaClass<JImage>
{
static constexpr auto kJavaDescriptor = "Landroid/media/Image;";

public:
int getWidth() const;
int getHeight() const;
jni::local_ref<jni::JArrayClass<JImagePlane>> getPlanes() const;
};

} // namespace resizeconvert
34 changes: 34 additions & 0 deletions android/src/main/cpp/JImagePlane.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Created by Marc Rousavy on 25.01.24.
//

#include "JImagePlane.h"

namespace resizeconvert
{

using namespace facebook;
using namespace jni;

int JImagePlane::getPixelStride() const
{
auto method = getClass()->getMethod<jint()>("getPixelStride");
auto result = method(self());
return result;
}

int JImagePlane::getRowStride() const
{
auto method = getClass()->getMethod<jint()>("getRowStride");
auto result = method(self());
return result;
}

jni::local_ref<JByteBuffer> JImagePlane::getBuffer() const
{
auto method = getClass()->getMethod<JByteBuffer()>("getBuffer");
auto result = method(self());
return result;
}

} // namespace resizeconvert
27 changes: 27 additions & 0 deletions android/src/main/cpp/JImagePlane.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by Marc Rousavy on 25.01.24.
//

#pragma once

#include <fbjni/ByteBuffer.h>
#include <fbjni/fbjni.h>
#include <jni.h>

namespace resizeconvert
{

using namespace facebook;
using namespace jni;

struct JImagePlane : public JavaClass<JImagePlane>
{
static constexpr auto kJavaDescriptor = "Landroid/media/Image$Plane;";

public:
jni::local_ref<JByteBuffer> getBuffer() const;
int getPixelStride() const;
int getRowStride() const;
};

} // namespace resizeconvert
Loading

0 comments on commit 588f2e6

Please sign in to comment.