Skip to content

Commit

Permalink
feat(Worklets): Create a stub of Worklets Module (software-mansion#6539)
Browse files Browse the repository at this point in the history
This pull request is one of many which replace

- software-mansion#6378

since that PR is extensive and too difficult to review.

## Summary

Adding a second Native Module for Worklets.

It's initial responsibility for now is forwarding `valueUnpackerCode` to
Reanimated Module. While it seems silly, it's a good start since it sets
up the whole pipeline of:

```tree
typescript
├── android
│   └── cpp
└── ios
    └── cpp
```
(I was too lazy to use mermaid, long live `mkdir -p`)

Follow up PRs will move more and more responsibilities from Reanimated
to Worklets.

Requires:

- software-mansion#6556
- software-mansion#6557 

## Test plan

- [x] All GitHub Actions pass
- [x] Compatibility Github Action passes
- [x] debug Android works
- [x] release Android works
- [x] debug iOS works
- [x] release iOS works
  • Loading branch information
tjzel authored Nov 19, 2024
1 parent ef812a1 commit 6fad03e
Show file tree
Hide file tree
Showing 47 changed files with 658 additions and 115 deletions.
24 changes: 23 additions & 1 deletion apps/fabric-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,28 @@ PODS:
- ReactCommon/turbomodule/core
- Yoga
- RNReanimated/worklets (3.17.0):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNReanimated/worklets/apple (= 3.17.0)
- Yoga
- RNReanimated/worklets/apple (3.17.0):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -2229,7 +2251,7 @@ SPEC CHECKSUMS:
RNCPicker: b978067931744f5a7316b48b8dcf145d4d722672
RNFlashList: 6f169ad83e52579b7754cbbcec1b004c27d82c93
RNGestureHandler: fc5ce5bf284640d3af6431c3a5c3bc121e98d045
RNReanimated: 2db902281618797873d75426f334ede4e503baf8
RNReanimated: c03f9fdff5fd382d1833c37fa0e6f64ffd9b8a92
RNScreens: 2fe13c8d610ef2d9d5ace2e7d85b716ec0f5217c
RNSVG: 536cd3c866c878faf72beaba166c8b02fe2b762b
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Expand Down
24 changes: 23 additions & 1 deletion apps/macos-example/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,28 @@ PODS:
- ReactCommon/turbomodule/core
- Yoga
- RNReanimated/worklets (3.17.0):
- DoubleConversion
- glog
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-jsi
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNReanimated/worklets/apple (= 3.17.0)
- Yoga
- RNReanimated/worklets/apple (3.17.0):
- DoubleConversion
- glog
- RCT-Folly (= 2024.01.01.00)
Expand Down Expand Up @@ -1843,7 +1865,7 @@ SPEC CHECKSUMS:
RNCAsyncStorage: ec53e44dc3e75b44aa2a9f37618a49c3bc080a7a
RNCPicker: f963e01f78e546a93b98aa201501713dbda14e94
RNGestureHandler: 82f59e40580e0c2f8262552c149716511131b412
RNReanimated: 8b9e602d3bc250cfd046c584454aa2a2efa8bd7e
RNReanimated: ed490424d3b8b9f2acd104577c73b374fc79310b
RNSVG: 8b1a777d54096b8c2a0fd38fc9d5a454332bbb4d
SocketRocket: 9ee265c4b5ae2382d18e4ee1d2dd2d7af0ff1ab5
Yoga: a7f65c1fd1394973b9ea40c9c270530e9d7cd2e7
Expand Down
24 changes: 23 additions & 1 deletion apps/paper-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,28 @@ PODS:
- ReactCommon/turbomodule/core
- Yoga
- RNReanimated/worklets (3.17.0):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNReanimated/worklets/apple (= 3.17.0)
- Yoga
- RNReanimated/worklets/apple (3.17.0):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -2019,7 +2041,7 @@ SPEC CHECKSUMS:
RNCPicker: f963e01f78e546a93b98aa201501713dbda14e94
RNFlashList: 115dd44377580761bff386a0caebf165424cf16f
RNGestureHandler: 511250b190a284388f9dd0d2e56c1df76f14cfb8
RNReanimated: 9ef3b1908a15b0b778edd6dacad9c71e7fe9e849
RNReanimated: 1d16252a3b482d884ba83b09800594fbfa773291
RNScreens: de948b09c9a30f3ea52f9840dd6f8ce92b4e33d3
RNSVG: 8b1a777d54096b8c2a0fd38fc9d5a454332bbb4d
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Expand Down
24 changes: 23 additions & 1 deletion apps/tvos-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,28 @@ PODS:
- ReactCommon/turbomodule/core
- Yoga
- RNReanimated/worklets (3.17.0):
- DoubleConversion
- glog
- hermes-engine
- RCT-Folly (= 2024.01.01.00)
- RCTRequired
- RCTTypeSafety
- React-Core
- React-debug
- React-Fabric
- React-featureflags
- React-graphics
- React-ImageManager
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
- React-utils
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNReanimated/worklets/apple (= 3.17.0)
- Yoga
- RNReanimated/worklets/apple (3.17.0):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1864,7 +1886,7 @@ SPEC CHECKSUMS:
React-utils: 3fce26d04c6776ddd47f255e7f3adc70d61b4491
ReactCodegen: 61cc9f25250fd53c91d30d82e84a1c6aafcf451a
ReactCommon: b927fd46115bd2acb146e24cf1a08f22abda8b3f
RNReanimated: b007fe1002350dd2ffb10fec214560a167794e12
RNReanimated: 3a5e1e235c940894097b0734aad9ebce45431ddd
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Yoga: 651e5fd560c7e408ab9d9ca44b8de1b622d7f0cc

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,30 @@ using namespace facebook;
namespace reanimated {

NativeReanimatedModule::NativeReanimatedModule(
const std::shared_ptr<NativeWorkletsModule> &nativeWorkletsModule,
jsi::Runtime &rnRuntime,
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::shared_ptr<MessageQueueThread> &jsQueue,
const std::shared_ptr<UIScheduler> &uiScheduler,
const PlatformDepMethodsHolder &platformDepMethodsHolder,
const std::string &valueUnpackerCode,
const bool isBridgeless,
const bool isReducedMotion)
: NativeReanimatedModuleSpec(
isBridgeless ? nullptr : jsScheduler->getJSCallInvoker()),
isBridgeless_(isBridgeless),
isReducedMotion_(isReducedMotion),
jsQueue_(jsQueue),
nativeWorkletsModule_(nativeWorkletsModule),
jsScheduler_(jsScheduler),
uiScheduler_(uiScheduler),
valueUnpackerCode_(nativeWorkletsModule->getValueUnpackerCode()),
uiWorkletRuntime_(std::make_shared<WorkletRuntime>(
rnRuntime,
jsQueue,
jsScheduler_,
"Reanimated UI runtime",
true /* supportsLocking */,
valueUnpackerCode)),
valueUnpackerCode_(valueUnpackerCode),
valueUnpackerCode_)),
eventHandlerRegistry_(std::make_unique<EventHandlerRegistry>()),
requestRender_(platformDepMethodsHolder.requestRender),
onRenderCallback_([this](const double timestampMs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <reanimated/LayoutAnimations/LayoutAnimationsProxy.h>
#endif // RCT_NEW_ARCH_ENABLED

#include <worklets/NativeModules/NativeWorkletsModule.h>
#include <worklets/Registries/EventHandlerRegistry.h>
#include <worklets/Tools/JSScheduler.h>
#include <worklets/Tools/UIScheduler.h>
Expand All @@ -32,12 +33,12 @@ namespace reanimated {
class NativeReanimatedModule : public NativeReanimatedModuleSpec {
public:
NativeReanimatedModule(
const std::shared_ptr<NativeWorkletsModule> &nativeWorkletsModule,
jsi::Runtime &rnRuntime,
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::shared_ptr<MessageQueueThread> &jsQueue,
const std::shared_ptr<UIScheduler> &uiScheduler,
const PlatformDepMethodsHolder &platformDepMethodsHolder,
const std::string &valueUnpackerCode,
const bool isBridgeless,
const bool isReducedMotion);

Expand Down Expand Up @@ -168,18 +169,23 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
return *layoutAnimationsManager_;
}

inline jsi::Runtime &getUIRuntime() const {
[[nodiscard]] inline jsi::Runtime &getUIRuntime() const {
return uiWorkletRuntime_->getJSIRuntime();
}

inline bool isBridgeless() const {
[[nodiscard]] inline bool isBridgeless() const {
return isBridgeless_;
}

inline bool isReducedMotion() const {
[[nodiscard]] inline bool isReducedMotion() const {
return isReducedMotion_;
}

[[nodiscard]] inline std::shared_ptr<NativeWorkletsModule>
getNativeWorkletsModule() const {
return nativeWorkletsModule_;
}

private:
void commonInit(const PlatformDepMethodsHolder &platformDepMethodsHolder);

Expand All @@ -195,10 +201,11 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
const bool isBridgeless_;
const bool isReducedMotion_;
const std::shared_ptr<MessageQueueThread> jsQueue_;
const std::shared_ptr<NativeWorkletsModule> nativeWorkletsModule_;
const std::shared_ptr<JSScheduler> jsScheduler_;
const std::shared_ptr<UIScheduler> uiScheduler_;
const std::string valueUnpackerCode_;
std::shared_ptr<WorkletRuntime> uiWorkletRuntime_;
std::string valueUnpackerCode_;

std::unique_ptr<EventHandlerRegistry> eventHandlerRegistry_;
const RequestRenderFunction requestRender_;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <string>

#ifdef RCT_NEW_ARCH_ENABLED
#include <react/renderer/uimanager/UIManagerBinding.h>
#include <react/renderer/uimanager/primitives.h>
#endif // RCT_NEW_ARCH_ENABLED

#include <worklets/NativeModules/NativeWorkletsModule.h>

#ifdef __ANDROID__
#include <fbjni/fbjni.h>
#endif // __ANDROID__

#include <jsi/jsi.h>

using namespace facebook;

namespace worklets {

NativeWorkletsModule::NativeWorkletsModule(const std::string &valueUnpackerCode)
: NativeWorkletsModuleSpec(nullptr),
valueUnpackerCode_(valueUnpackerCode) {}

NativeWorkletsModule::~NativeWorkletsModule() {}
} // namespace worklets
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <cxxreact/MessageQueueThread.h>
#include <worklets/NativeModules/NativeWorkletsModuleSpec.h>
#include <string>

namespace worklets {

class NativeWorkletsModule : public NativeWorkletsModuleSpec {
public:
explicit NativeWorkletsModule(const std::string &valueUnpackerCode);

~NativeWorkletsModule();

[[nodiscard]] inline std::string getValueUnpackerCode() const {
return valueUnpackerCode_;
}

private:
const std::string valueUnpackerCode_;
};

} // namespace worklets
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <worklets/NativeModules/NativeWorkletsModuleSpec.h>

namespace worklets {

NativeWorkletsModuleSpec::NativeWorkletsModuleSpec(
const std::shared_ptr<CallInvoker> jsInvoker)
: TurboModule("NativeWorklets", jsInvoker) {}

} // namespace worklets
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <ReactCommon/CallInvoker.h>
#include <ReactCommon/TurboModule.h>
#include <memory>

using namespace facebook;
using namespace react;

namespace worklets {

class JSI_EXPORT NativeWorkletsModuleSpec : public TurboModule {
protected:
explicit NativeWorkletsModuleSpec(
const std::shared_ptr<CallInvoker> jsInvoker);
};

} // namespace worklets
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h>

namespace worklets {

void RNRuntimeWorkletDecorator::decorate(
jsi::Runtime &rnRuntime,
const std::shared_ptr<NativeWorkletsModule> &nativeWorkletsModule) {
rnRuntime.global().setProperty(
rnRuntime,
"__workletsModuleProxy",
jsi::Object::createFromHostObject(rnRuntime, nativeWorkletsModule));
}

} // namespace worklets
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <jsi/jsi.h>
#include <worklets/NativeModules/NativeWorkletsModule.h>
#include <memory>

using namespace facebook;

namespace worklets {

class RNRuntimeWorkletDecorator {
// TODO: Rename to `RNRuntimeWorkletsDecorator` or something more suitable.
public:
static void decorate(
jsi::Runtime &rnRuntime,
const std::shared_ptr<NativeWorkletsModule> &nativeWorkletsModule);
};

} // namespace worklets
21 changes: 15 additions & 6 deletions packages/react-native-reanimated/RNReanimated.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ Pod::Spec.new do |s|
s.platforms = { :ios => ios_min_version, :tvos => "9.0", :osx => "10.14", :visionos => "1.0" }
s.source = { :git => "https://github.com/software-mansion/react-native-reanimated.git", :tag => "#{s.version}" }

s.subspec "worklets" do |ss|
ss.source_files = "Common/cpp/worklets/**/*.{cpp,h}"
ss.header_dir = "worklets"
ss.header_mappings_dir = "Common/cpp/worklets"

ss.subspec "apple" do |sss|
# Please be careful with the snakes.
# 🐍🐍🐍
# Thank you for your understanding.
sss.source_files = "apple/worklets/**/*.{mm,h,m}"
sss.header_dir = "worklets"
sss.header_mappings_dir = "apple/worklets"
end
end

s.subspec "reanimated" do |ss|
ss.source_files = "Common/cpp/reanimated/**/*.{cpp,h}"
ss.header_dir = "reanimated"
Expand All @@ -90,12 +105,6 @@ Pod::Spec.new do |s|
end
end

s.subspec "worklets" do |ss|
ss.source_files = "Common/cpp/worklets/**/*.{cpp,h}"
ss.header_dir = "worklets"
ss.header_mappings_dir = "Common/cpp/worklets"
end

gcc_debug_definitions = "$(inherited)"
if !is_release
gcc_debug_definitions << " HERMES_ENABLE_DEBUGGER=1"
Expand Down
Loading

0 comments on commit 6fad03e

Please sign in to comment.