Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Simplify cpp/ codebase into android/ios folders #2766

Merged
merged 6 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/build-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ on:
- main
paths:
- '.github/workflows/build-android.yml'
- 'package/cpp/**'
- 'package/android/**'
- 'package/example/android/**'
- 'package/yarn.lock'
- 'package/example/yarn.lock'
pull_request:
paths:
- '.github/workflows/build-android.yml'
- 'package/cpp/**'
- 'package/android/**'
- 'package/example/android/**'
- 'package/yarn.lock'
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/build-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ on:
- main
paths:
- '.github/workflows/build-ios.yml'
- 'package/cpp/**'
- 'package/ios/**'
- 'package/*.podspec'
- 'package/example/ios/**'
pull_request:
paths:
- '.github/workflows/build-ios.yml'
- 'package/cpp/**'
- 'package/ios/**'
- 'package/*.podspec'
- 'package/example/ios/**'
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/validate-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ on:
- main
paths:
- '.github/workflows/validate-cpp.yml'
- 'package/cpp/**'
- 'package/android/src/main/cpp/**'
- 'package/ios/**'
pull_request:
paths:
- '.github/workflows/validate-cpp.yml'
- 'package/cpp/**'
- 'package/android/src/main/cpp/**'
- 'package/ios/**'

Expand All @@ -23,7 +21,6 @@ jobs:
strategy:
matrix:
path:
- 'package/cpp'
- 'package/android/src/main/cpp'
- 'package/ios'
steps:
Expand All @@ -33,5 +30,5 @@ jobs:
with:
clang-format-version: '16'
check-path: ${{ matrix.path }}
clang-format-style-path: package/cpp/.clang-format
clang-format-style-path: package/.clang-format

2 changes: 1 addition & 1 deletion docs/docs/guides/FRAME_PROCESSORS_CREATE_PLUGIN_CPP.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ s.dependency "VisionCamera"
Then include it in your C++ code:

```cpp
#include "Frame Processor/FrameHostObject.h"
#include "FrameProcessors/FrameHostObject.h"
```

</TabItem>
Expand Down
1 change: 0 additions & 1 deletion package/cpp/.clang-format → package/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ BreakConstructorInitializers: BeforeColon
AllowShortFunctionsOnASingleLine: Empty
IndentCaseLabels: true
NamespaceIndentation: Inner

25 changes: 12 additions & 13 deletions package/VisionCamera.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ Pod::Spec.new do |s|

s.subspec 'Core' do |core|
# VisionCamera Core Swift codebase
core.source_files = "ios/Core/**/*.swift"
core.source_files = [
"ios/Core/**/*.swift"
]

core.pod_target_xcconfig = {
"SWIFT_ACTIVE_COMPILATION_CONDITIONS" => "$(inherited) #{enableLocation ? "VISION_CAMERA_ENABLE_LOCATION" : ""}",
Expand All @@ -80,25 +82,22 @@ Pod::Spec.new do |s|
s.subspec 'FrameProcessors' do |fp|
# VisionCamera Frame Processors C++ codebase (optional)
fp.source_files = [
# C++ sources
"ios/Frame Processor/**/*.{h,m,mm}",
"cpp/**/*.{h,cpp}",
"ios/FrameProcessors/**/*.{h,m,mm}"
]
fp.public_header_files = [
# Swift/Objective-C visible headers
"ios/Frame Processor/Frame.h",
"ios/Frame Processor/FrameProcessor.h",
"ios/Frame Processor/FrameProcessorPlugin.h",
"ios/Frame Processor/FrameProcessorPluginRegistry.h",
"ios/Frame Processor/SharedArray.h",
"ios/Frame Processor/VisionCameraProxyDelegate.h",
"ios/Frame Processor/VisionCameraProxyHolder.h",
"ios/Frame Processor/VisionCameraInstaller.h",
"ios/FrameProcessors/Frame.h",
"ios/FrameProcessors/FrameProcessor.h",
"ios/FrameProcessors/FrameProcessorPlugin.h",
"ios/FrameProcessors/FrameProcessorPluginRegistry.h",
"ios/FrameProcessors/SharedArray.h",
"ios/FrameProcessors/VisionCameraProxyDelegate.h",
"ios/FrameProcessors/VisionCameraProxyHolder.h",
"ios/FrameProcessors/VisionCameraInstaller.h",
]

fp.pod_target_xcconfig = {
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17",
"HEADER_SEARCH_PATHS" => "$(inherited) \"$(PODS_TARGET_SRCROOT)/cpp/\"/** "
}

fp.dependency "React"
Expand Down
3 changes: 0 additions & 3 deletions package/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ endif()
add_library(
${PACKAGE_NAME}
SHARED
# Shared C++
../cpp/MutableRawBuffer.cpp
# Java JNI
src/main/cpp/VisionCamera.cpp
src/main/cpp/MutableJByteBuffer.cpp
Expand All @@ -48,7 +46,6 @@ add_library(
target_include_directories(
${PACKAGE_NAME}
PRIVATE
"../cpp"
"src/main/cpp"
"src/main/cpp/frameprocessors"
"src/main/cpp/frameprocessors/java-bindings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#pragma once

#include <functional>
#include <jsi/jsi.h>
#include <memory>

Expand All @@ -19,13 +18,26 @@ using namespace facebook;
class MutableRawBuffer : public jsi::MutableBuffer {

public:
explicit MutableRawBuffer(size_t size);
explicit MutableRawBuffer(uint8_t* data, size_t size, bool freeOnDealloc);
~MutableRawBuffer();
explicit MutableRawBuffer(uint8_t* data, size_t size, bool freeOnDealloc) {
_size = size;
_data = data;
_freeOnDealloc = freeOnDealloc;
}
explicit MutableRawBuffer(size_t size) : MutableRawBuffer(new uint8_t[size], size, true) {}

~MutableRawBuffer() {
if (_freeOnDealloc) {
delete[] _data;
}
}

public:
uint8_t* data() override;
size_t size() const override;
uint8_t* data() override {
return _data;
}
size_t size() const override {
return _size;
}

private:
uint8_t* _data;
Expand Down
10 changes: 6 additions & 4 deletions package/android/src/main/cpp/frameprocessors/FrameHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include <android/hardware_buffer.h>
#include <android/hardware_buffer_jni.h>

#include "FinalAction.h"

namespace vision {

using namespace facebook;
Expand Down Expand Up @@ -145,7 +143,6 @@ jsi::Value FrameHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pr
#if __ANDROID_API__ >= 26
AHardwareBuffer* hardwareBuffer = this->frame->getHardwareBuffer();
AHardwareBuffer_acquire(hardwareBuffer);
finally([&]() { AHardwareBuffer_release(hardwareBuffer); });

AHardwareBuffer_Desc bufferDescription;
AHardwareBuffer_describe(hardwareBuffer, &bufferDescription);
Expand Down Expand Up @@ -176,12 +173,17 @@ jsi::Value FrameHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& pr
if (result != 0) {
throw jsi::JSError(runtime, "Failed to lock HardwareBuffer for reading!");
}
finally([&]() { AHardwareBuffer_unlock(hardwareBuffer, nullptr); });

// directly write to C++ JSI ArrayBuffer
auto destinationBuffer = arrayBuffer.data(runtime);
memcpy(destinationBuffer, buffer, sizeof(uint8_t) * size);

// unlock read lock
AHardwareBuffer_unlock(hardwareBuffer, nullptr);

// release JNI reference
AHardwareBuffer_release(hardwareBuffer);

return arrayBuffer;
#else
throw jsi::JSError(runtime, "Frame.toArrayBuffer() is only available if minSdkVersion is set to 26 or higher!");
Expand Down
34 changes: 0 additions & 34 deletions package/cpp/FinalAction.h

This file was deleted.

38 changes: 0 additions & 38 deletions package/cpp/MutableRawBuffer.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion package/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ SPEC CHECKSUMS:
RNStaticSafeAreaInsets: 055ddbf5e476321720457cdaeec0ff2ba40ec1b8
RNVectorIcons: 23b6e11af4aaf104d169b1b0afa7e5cf96c676ce
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
VisionCamera: 83ca8822d076bbdef9b5642c3285fee80b185a8c
VisionCamera: 63c688c266a44e2ef20b998e05021f582812b0c2
Yoga: 4c3aa327e4a6a23eeacd71f61c81df1bcdf677d5

PODFILE CHECKSUM: 66976ac26c778d788a06e6c1bab624e6a1233cdd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#pragma once

#import "../Frame Processor/SharedArray.h"
#import "SharedArray.h"
#import <React/RCTBridgeModule.h>
#import <ReactCommon/CallInvoker.h>
#import <jsi/jsi.h>
Expand Down
48 changes: 48 additions & 0 deletions package/ios/FrameProcessors/MutableRawBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// MutableRawBuffer.h
// VisionCamera
//
// Created by Marc Rousavy on 17.01.24.
// Copyright © 2024 mrousavy. All rights reserved.
//

#pragma once

#include <jsi/jsi.h>
#include <memory>

namespace vision {

using namespace facebook;

class MutableRawBuffer : public jsi::MutableBuffer {

public:
explicit MutableRawBuffer(uint8_t* data, size_t size, bool freeOnDealloc) {
_size = size;
_data = data;
_freeOnDealloc = freeOnDealloc;
}
explicit MutableRawBuffer(size_t size) : MutableRawBuffer(new uint8_t[size], size, true) {}

~MutableRawBuffer() {
if (_freeOnDealloc) {
delete[] _data;
}
}

public:
uint8_t* data() override {
return _data;
}
size_t size() const override {
return _size;
}

private:
uint8_t* _data;
size_t _size;
bool _freeOnDealloc;
};

} // namespace vision
2 changes: 0 additions & 2 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
"lib/commonjs",
"lib/module",
"lib/typescript",
"cpp/**/*.h",
"cpp/**/*.cpp",
"android/build.gradle",
"android/gradle.properties",
"android/CMakeLists.txt",
Expand Down
4 changes: 2 additions & 2 deletions package/scripts/clang-format.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash

if which clang-format >/dev/null; then
find cpp ios android/src/main/cpp -type f \( -name "*.h" -o -name "*.cpp" -o -name "*.m" -o -name "*.mm" \) -print0 | while read -d $'\0' file; do
clang-format -style=file:./cpp/.clang-format -i "$file"
find ios android/src/main/cpp -type f \( -name "*.h" -o -name "*.cpp" -o -name "*.m" -o -name "*.mm" \) -print0 | while read -d $'\0' file; do
clang-format -style=file:./.clang-format -i "$file"
done
else
echo "error: clang-format not installed, install with 'brew install clang-format' (or manually from https://clang.llvm.org/docs/ClangFormat.html)"
Expand Down
Loading