Skip to content

Commit

Permalink
Merge pull request #1 from callstack/feat/native-build
Browse files Browse the repository at this point in the history
[wip] base setup for basis universal
  • Loading branch information
okwasniewski authored Oct 14, 2024
2 parents 66e4b34 + cd2f572 commit 2c72be6
Show file tree
Hide file tree
Showing 97 changed files with 53,664 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ DerivedData
*.ipa
*.xcuserstate
project.xcworkspace
cpp/basisu
example/ios/.xcode.env
example/ios/*/content.xcworkspacedata

# Android/IJ
#
Expand All @@ -39,6 +42,7 @@ project.xcworkspace
.settings
local.properties
android.iml
bazel-*

# Cocoapods
#
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "externals/basis_universal"]
path = externals/basis_universal
url = https://github.com/BinomialLLC/basis_universal.git
13 changes: 13 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_apple//apple:apple.bzl", "apple_static_xcframework")

apple_static_xcframework(
name = "rn_basis_universal",
deps = ["@basis_universal"],
ios = {
"simulator": ["x86_64", "arm64"],
"device": ["arm64"]
},
minimum_os_versions = {
"ios": "11.0"
}
)
1,256 changes: 1,256 additions & 0 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

83 changes: 81 additions & 2 deletions cpp/react-native-basis-universal.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,91 @@
#include "react-native-basis-universal.h"

using namespace basist;
using namespace basisu;

namespace facebook::react {

class BasisEncoder : public jsi::NativeState {
public:
basis_compressor_params m_params;
};

std::shared_ptr<BasisEncoder> tryGetBasisEncoder(jsi::Runtime& rt, jsi::Object& basisEncoderObj) {
if (!basisEncoderObj.hasNativeState(rt)) {
return nullptr;
}

auto encoder = std::dynamic_pointer_cast<BasisEncoder>(basisEncoderObj.getNativeState(rt));
return encoder;
}

ReactNativeBasisUniversal::ReactNativeBasisUniversal(std::shared_ptr<CallInvoker> jsInvoker)
: NativeBasisUniversalCxxSpecJSI(jsInvoker), _callInvoker(jsInvoker) {}

double ReactNativeBasisUniversal::multiply(jsi::Runtime &rt, double a, double b) {
return a * b;
void ReactNativeBasisUniversal::initializeBasis(jsi::Runtime &rt)
{
if (basis_initialized_flag) {
return;
}

#if BASISU_SUPPORT_ENCODING
basisu_encoder_init();
#endif

basisu_transcoder_init();

basis_initialized_flag = true;
}

jsi::Object ReactNativeBasisUniversal::createBasisHandle(jsi::Runtime &rt) {
jsi::Object basisObject{rt};
basisObject.setNativeState(rt, std::make_shared<BasisEncoder>());
return basisObject;
}

void ReactNativeBasisUniversal::setDebug(jsi::Runtime &rt, jsi::Object handle, bool flag) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_debug = flag;
}

void ReactNativeBasisUniversal::setCreateKTX2File(jsi::Runtime &rt, jsi::Object handle, bool flag) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_create_ktx2_file = flag;
}

void ReactNativeBasisUniversal::setComputeStats(jsi::Runtime &rt, jsi::Object handle, bool flag) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_compute_stats = flag;
}

void ReactNativeBasisUniversal::setUASTC(jsi::Runtime &rt, jsi::Object handle, bool flag) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_uastc = flag;
}

void ReactNativeBasisUniversal::setKTX2UASTCSupercompression(jsi::Runtime &rt, jsi::Object handle, bool flag) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_ktx2_uastc_supercompression = flag ? basist::KTX2_SS_ZSTANDARD : basist::KTX2_SS_NONE;;
}

void ReactNativeBasisUniversal::setQualityLevel(jsi::Runtime &rt, jsi::Object handle, int qualityLevel) {

}

void ReactNativeBasisUniversal::setSliceSourceImage(jsi::Runtime &rt, jsi::Object handle, int sliceIndex, jsi::Object imageArray, int width, int height, bool isPng) {
if (imageArray.isArrayBuffer(rt)) {
throw jsi::JSError(rt, "Image Array needs to be ArrayBuffer");
}
auto arrayBuffer = imageArray.getArrayBuffer(rt);
auto data = arrayBuffer.data(rt);

Check warning on line 80 in cpp/react-native-basis-universal.cpp

View workflow job for this annotation

GitHub Actions / build-ios

unused variable 'data' [-Wunused-variable]
}

void ReactNativeBasisUniversal::setPackUASTCFlags(jsi::Runtime &rt, jsi::Object handle, int flags) {

}

void ReactNativeBasisUniversal::setCompressionLevel(jsi::Runtime &rt, jsi::Object handle, int level) {

}

}
25 changes: 22 additions & 3 deletions cpp/react-native-basis-universal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

#include <ReactCommon/TurboModule.h>
#include <RNBasisUniversalSpecJSI.h>
#include "rn_basis_universal/transcoder/basisu.h"
#include "rn_basis_universal/transcoder/basisu_transcoder.h"

#include "rn_basis_universal/encoder/basisu_comp.h"

#ifndef BASISU_SUPPORT_ENCODING
#define BASISU_SUPPORT_ENCODING 1
#endif

namespace facebook::react {

Expand All @@ -10,13 +18,24 @@ using namespace facebook;
class ReactNativeBasisUniversal : public NativeBasisUniversalCxxSpecJSI {
public:
explicit ReactNativeBasisUniversal(std::shared_ptr<CallInvoker> jsInvoker);

public:
double multiply(jsi::Runtime &rt, double a, double b) override;
constexpr static auto kModuleName = "BasisUniversal";

void initializeBasis(jsi::Runtime &rt) override;
jsi::Object createBasisHandle(jsi::Runtime &rt) override;
void setCreateKTX2File(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setDebug(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setComputeStats(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setUASTC(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setCompressionLevel(jsi::Runtime &rt, jsi::Object handle, int level) override;
void setKTX2UASTCSupercompression(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setSliceSourceImage(jsi::Runtime &rt, jsi::Object handle, int sliceIndex, jsi::Object imageArray, int width, int height, bool isPng) override;
void setPackUASTCFlags(jsi::Runtime &rt, jsi::Object handle, int flags) override;
void setQualityLevel(jsi::Runtime &rt, jsi::Object handle, int qualityLevel) override;


private:
std::shared_ptr<CallInvoker> _callInvoker;
bool basis_initialized_flag;
};

} // namespace facebook::react
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ SPEC CHECKSUMS:
React-logger: d79b704bf215af194f5213a6b7deec50ba8e6a9b
React-Mapbuffer: b982d5bba94a8bc073bda48f0d27c9b28417fae3
React-microtasksnativemodule: 8fa285fed833a04a754bf575f8ded65fc240b88d
react-native-basis-universal: 2e1160a6dfcde0aa6cd51503480e0ec982667f9e
react-native-basis-universal: 5a9b05e51cc23161be95911a7c442d0b204d189f
React-nativeconfig: 8c83d992b9cc7d75b5abe262069eaeea4349f794
React-NativeModulesApple: b8465afc883f5bf3fe8bac3767e394d581a5f123
React-perflogger: 59e1a3182dca2cee7b9f1f7aab204018d46d1914
Expand Down
31 changes: 26 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import { StyleSheet, View, Text } from 'react-native';
import { multiply } from '@callstack/react-native-basis-universal';

const result = multiply(3, 7);
import React from 'react';

Check failure on line 1 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'React' is declared but its value is never read.
import { StyleSheet, View, Button } from 'react-native';
import {
initializeBasis,
BasisEncoder,
} from '@callstack/react-native-basis-universal';

export default function App() {
return (
<View style={styles.container}>
<Text>Result: {result}</Text>
<Button title="Initialize Basis" onPress={initializeBasis} />
<Button
title="Encode"
onPress={() => {
const basisEncoder = new BasisEncoder();
basisEncoder.setCreateKTX2File(true);
basisEncoder.setDebug(true);
basisEncoder.setComputeStats(true);
// basisEncoder.setSliceSourceImage(0, new Uint8Array(), 0, 0, false);
// basisEncoder.setUASTC(true);
// basisEncoder.setKTX2UASTCSupercompression(true);
// basisEncoder.setPackUASTCFlags(0);
// basisEncoder.setQualityLevel(0);
// basisEncoder.setCompressionLevel(0);
// basisEncoder.setPerceptual(true);
// basisEncoder.setMipSRGB(true);
// basisEncoder.setKTX2SRGBTransferFunc(true);
// basisEncoder.setNormalMap();
}}
/>
</View>
);
}
Expand Down
2 changes: 0 additions & 2 deletions ios/BasisUniversal.mm
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#import "BasisUniversal.h"
#include "react-native-basis-universal.h"
#include <ReactCommon/CxxTurboModuleUtils.h>
#include <RNBasisUniversalSpecJSI.h>

@implementation BasisUniversal

Expand Down
40 changes: 40 additions & 0 deletions ios/libs/rn_basis_universal.xcframework/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AvailableLibraries</key>
<array>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>rn_basis_universal.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>rn_basis_universal.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
</array>
<key>CFBundlePackageType</key>
<string>XFWK</string>
<key>XCFrameworkFormatVersion</key>
<string>1.0</string>
</dict>
</plist>
Loading

0 comments on commit 2c72be6

Please sign in to comment.