Skip to content

Commit

Permalink
feat: jump to other thread to decode (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski authored Nov 18, 2024
1 parent 6f2fc7d commit c902966
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 10 deletions.
50 changes: 44 additions & 6 deletions cpp/react-native-draco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "draco/core/decoder_buffer.h"
#include "draco-state.h"
#include "draco-helpers.h"
#include <thread>
#include <future>

namespace facebook::react {

Expand Down Expand Up @@ -303,21 +305,57 @@ NativeDracoStatus ReactNativeDraco::DecodeBufferToPointCloud(jsi::Runtime &rt, j
auto buffer = tryGetDracoObject<DracoDecoderBuffer>(rt, bufferHandle);
auto pointCloud = tryGetDracoObject<DracoPointCloud>(rt, pointCloudHandle);

auto status = decoder->decoder_.DecodeBufferToGeometry(&buffer->buffer_, &pointCloud->pointCloud_);
decoder->last_status_ = draco::Status(std::move(status));
std::promise<draco::Status> resultPromise;
std::future<draco::Status> resultFuture = resultPromise.get_future();

return Bridging<NativeDracoStatus>::fromJs(rt, jsi::Value(status.code()));
std::thread decodingThread([&buffer, &pointCloud, &decoder, promise = std::move(resultPromise)]() mutable {
try {
auto status = decoder->decoder_.DecodeBufferToGeometry(&buffer->buffer_, &pointCloud->pointCloud_);
decoder->last_status_ = draco::Status(std::move(status));
promise.set_value(status);
} catch (...) {
promise.set_exception(std::current_exception());
}

});

decodingThread.detach();

try {
auto status = resultFuture.get();
return Bridging<NativeDracoStatus>::fromJs(rt, jsi::Value(status.code()));
} catch (const std::exception& e) {
throw jsi::JSError(rt, e.what());
}
}

NativeDracoStatus ReactNativeDraco::DecodeBufferToMesh(jsi::Runtime &rt, jsi::Object decoderHandle, jsi::Object bufferHandle, jsi::Object meshHandle) {
auto decoder = tryGetDracoObject<DracoDecoder>(rt, decoderHandle);
auto buffer = tryGetDracoObject<DracoDecoderBuffer>(rt, bufferHandle);
auto mesh = tryGetDracoObject<DracoMesh>(rt, meshHandle);

auto status = decoder->decoder_.DecodeBufferToGeometry(&buffer->buffer_, &mesh->mesh_);
decoder->last_status_ = draco::Status(std::move(status));
std::promise<draco::Status> resultPromise;
std::future<draco::Status> resultFuture = resultPromise.get_future();

return Bridging<NativeDracoStatus>::fromJs(rt, jsi::Value(status.code()));
std::thread decodingThread([&buffer, &mesh, &decoder, promise = std::move(resultPromise)]() mutable {
try {
auto status = decoder->decoder_.DecodeBufferToGeometry(&buffer->buffer_, &mesh->mesh_);
decoder->last_status_ = draco::Status(std::move(status));
promise.set_value(status);
} catch (...) {
promise.set_exception(std::current_exception());
}

});

decodingThread.detach();

try {
auto status = resultFuture.get();
return Bridging<NativeDracoStatus>::fromJs(rt, jsi::Value(status.code()));
} catch (const std::exception& e) {
throw jsi::JSError(rt, e.what());
}
}


Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-draco (0.1.0):
- react-native-draco (0.2.0):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1837,7 +1837,7 @@ SPEC CHECKSUMS:
React-Mapbuffer: b0b4ace5b62b269f3838df26ba2d8b4f39f90783
React-microtasksnativemodule: 0b7db04c18f6bb01ef5b1f9007c3229abecc35dd
react-native-blob-util: e6a3b23a99ac2c3d9fa48722db049a1e1808efc2
react-native-draco: 991258c154c8ad912b15d15707282636344ec198
react-native-draco: 1c96e89e0f6dc4b4bef27adf5d9a7dc2abf68c0a
React-nativeconfig: 72c10ff34863148ef90df9c9c8eacba99d2faaaa
React-NativeModulesApple: 5ec49182fa000b2215ee1bed03e2867f8323ccf5
React-perflogger: 073c7a8a436b3fe724f1df34e9d1f3db1d25fe74
Expand Down
91 changes: 91 additions & 0 deletions example/patches/@react-native+codegen+0.76.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
diff --git a/node_modules/@react-native/codegen/lib/generators/modules/GenerateModuleH.js b/node_modules/@react-native/codegen/lib/generators/modules/GenerateModuleH.js
index 13232a1..fe52764 100644
--- a/node_modules/@react-native/codegen/lib/generators/modules/GenerateModuleH.js
+++ b/node_modules/@react-native/codegen/lib/generators/modules/GenerateModuleH.js
@@ -424,7 +424,9 @@ function generateEnum(hasteModuleName, origEnumName, members, memberType) {
const nativeEnumMemberType =
memberType === 'StringTypeAnnotation' ? 'std::string' : 'int32_t';
const getMemberValueAppearance = value =>
- memberType === 'StringTypeAnnotation' ? `"${value}"` : `${value}`;
+ {
+ return memberType === 'StringTypeAnnotation' ? `"${value}"` : `${value}`;
+ };
const fromCases =
members
.map(
@@ -458,6 +460,7 @@ function generateEnum(hasteModuleName, origEnumName, members, memberType) {
function createEnums(hasteModuleName, enumMap, resolveAlias) {
return Object.entries(enumMap)
.map(([enumName, enumNode]) => {
+ console.log(enumMap)
return generateEnum(
hasteModuleName,
enumName,
diff --git a/node_modules/@react-native/codegen/lib/parsers/typescript/parser.js b/node_modules/@react-native/codegen/lib/parsers/typescript/parser.js
index da4afe3..69bbcb1 100644
--- a/node_modules/@react-native/codegen/lib/parsers/typescript/parser.js
+++ b/node_modules/@react-native/codegen/lib/parsers/typescript/parser.js
@@ -174,12 +174,22 @@ class TypeScriptParser {
_typeAnnotation$membe === void 0
? void 0
: _typeAnnotation$membe.initializer;
- const enumMembersType =
+ let enumMembersType =
!enumInitializer || enumInitializer.type === 'StringLiteral'
? 'StringTypeAnnotation'
: enumInitializer.type === 'NumericLiteral'
? 'NumberTypeAnnotation'
: null;
+
+ // Handle case where enum is negative numbers
+
+ if (enumMembersType === null) {
+ const isNegative = enumInitializer.type = 'UnaryExpression' && enumInitializer.operator === '-';
+ if (isNegative) {
+ enumMembersType = 'NumberTypeAnnotation';
+ }
+ }
+
if (!enumMembersType) {
throw new Error(
'Enum values must be either blank, number, or string values.',
@@ -194,9 +204,10 @@ class TypeScriptParser {
const enumInitializerType =
enumMembersType === 'StringTypeAnnotation'
? 'StringLiteral'
- : enumMembersType === 'NumberTypeAnnotation'
+ : (enumMembersType === 'NumberTypeAnnotation' || enumMembersType === 'UnaryExpression')
? 'NumericLiteral'
: null;
+
typeAnnotation.members.forEach(member => {
var _member$initializer$t, _member$initializer;
if (
@@ -209,6 +220,11 @@ class TypeScriptParser {
? _member$initializer$t
: 'StringLiteral') !== enumInitializerType
) {
+ if (member.initializer?.argument.type === 'NumericLiteral') {
+ return;
+ }
+
+ console.log(member.initializer);
throw new Error(
'Enum values can not be mixed. They all must be either blank, number, or string values.',
);
@@ -218,6 +234,15 @@ class TypeScriptParser {
parseEnumMembers(typeAnnotation) {
return typeAnnotation.members.map(member => {
var _member$initializer$v, _member$initializer2;
+ console.log("PARSE ENUM MEMBERS", member.id.name, member.initializer.type, member.initializer.value);
+ if (member.initializer.operator == '-') {
+ console.log('NEGATIVE NUMBER');
+ console.log(member.id.name, -member.initializer.argument.value);
+ return {
+ name: member.id.name,
+ value: -member.initializer.argument.value
+ }
+ }
return {
name: member.id.name,
value:
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
EncodedGeometryType,
GeometryAttribute,
Status,
} from 'react-native-draco';
} from '@callstack/react-native-draco';
import RNFetchBlob from 'react-native-blob-util';

const defaultAttributeIDs = {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"rootDir": ".",
"paths": {
"react-native-draco": ["./src/index"]
"@callstack/react-native-draco": ["./src/index"]
},
"allowUnreachableCode": false,
"allowUnusedLabels": false,
Expand Down

0 comments on commit c902966

Please sign in to comment.