Skip to content

Commit 17e89ca

Browse files
authored
refactor(Worklets): move UIScheduler (#6789)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please follow the template so that the reviewers can easily understand what the code changes affect. --> ## Summary This pull requests moves `UIScheduler`, a class responsible for scheduling operations to the Main Thread, to Worklets. While it seems that `Reanimated` should have some freedom scheduling to the Main Thread if it doesn't use the UI Runtime, I think it's better to move it now as is and add some secondary APIs later on. We'll have a better overview of what we need once more code is moved to Worklets. ## Test plan - CI - [x] Working 0.76 Paper/Fabric iOS/Android
1 parent 2a947a8 commit 17e89ca

File tree

24 files changed

+84
-80
lines changed

24 files changed

+84
-80
lines changed

packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,13 @@ ReanimatedModuleProxy::ReanimatedModuleProxy(
5454
const std::shared_ptr<WorkletsModuleProxy> &workletsModuleProxy,
5555
jsi::Runtime &rnRuntime,
5656
const std::shared_ptr<CallInvoker> &jsCallInvoker,
57-
const std::shared_ptr<UIScheduler> &uiScheduler,
5857
const PlatformDepMethodsHolder &platformDepMethodsHolder,
5958
const bool isBridgeless,
6059
const bool isReducedMotion)
6160
: ReanimatedModuleProxySpec(jsCallInvoker),
6261
isBridgeless_(isBridgeless),
6362
isReducedMotion_(isReducedMotion),
6463
workletsModuleProxy_(workletsModuleProxy),
65-
uiScheduler_(uiScheduler),
6664
valueUnpackerCode_(workletsModuleProxy->getValueUnpackerCode()),
6765
uiWorkletRuntime_(std::make_shared<WorkletRuntime>(
6866
rnRuntime,
@@ -207,7 +205,7 @@ void ReanimatedModuleProxy::scheduleOnUI(
207205
const jsi::Value &worklet) {
208206
auto shareableWorklet = extractShareableOrThrow<ShareableWorklet>(
209207
rt, worklet, "[Reanimated] Only worklets can be scheduled to run on UI.");
210-
uiScheduler_->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
208+
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
211209
#if JS_RUNTIME_HERMES
212210
// JSI's scope defined here allows for JSI-objects to be cleared up
213211
// after each runtime loop. Within these loops we typically create some
@@ -264,7 +262,7 @@ jsi::Value ReanimatedModuleProxy::registerEventHandler(
264262
rt, worklet, "[Reanimated] Event handler must be a worklet.");
265263
int emitterReactTagInt = emitterReactTag.asNumber();
266264

267-
uiScheduler_->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
265+
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
268266
auto handler = std::make_shared<WorkletEventHandler>(
269267
newRegistrationId, eventNameStr, emitterReactTagInt, handlerShareable);
270268
eventHandlerRegistry_->registerEventHandler(std::move(handler));
@@ -277,7 +275,7 @@ void ReanimatedModuleProxy::unregisterEventHandler(
277275
jsi::Runtime &,
278276
const jsi::Value &registrationId) {
279277
uint64_t id = registrationId.asNumber();
280-
uiScheduler_->scheduleOnUI(
278+
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(
281279
COPY_CAPTURE_WITH_THIS
282280

283281
{ eventHandlerRegistry_->unregisterEventHandler(id); });
@@ -349,7 +347,7 @@ jsi::Value ReanimatedModuleProxy::getViewProp(
349347
const auto funPtr = std::make_shared<jsi::Function>(
350348
callback.getObject(rnRuntime).asFunction(rnRuntime));
351349
const auto shadowNode = shadowNodeFromValue(rnRuntime, shadowNodeWrapper);
352-
uiScheduler_->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
350+
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(COPY_CAPTURE_WITH_THIS {
353351
jsi::Runtime &uiRuntime = uiWorkletRuntime_->getJSIRuntime();
354352
const auto resultStr =
355353
obtainPropFromShadowNode(uiRuntime, propNameStr, shadowNode);
@@ -377,7 +375,7 @@ jsi::Value ReanimatedModuleProxy::getViewProp(
377375

378376
const int viewTagInt = viewTag.asNumber();
379377

380-
uiScheduler_->scheduleOnUI(
378+
workletsModuleProxy_->getUIScheduler()->scheduleOnUI(
381379
COPY_CAPTURE_WITH_THIS
382380

383381
() {
@@ -859,7 +857,7 @@ void ReanimatedModuleProxy::initializeLayoutAnimationsProxy() {
859857
componentDescriptorRegistry,
860858
scheduler->getContextContainer(),
861859
uiWorkletRuntime_->getJSIRuntime(),
862-
uiScheduler_);
860+
workletsModuleProxy_->getUIScheduler());
863861
}
864862
}
865863

packages/react-native-reanimated/Common/cpp/reanimated/NativeModules/ReanimatedModuleProxy.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class ReanimatedModuleProxy : public ReanimatedModuleProxySpec {
3636
const std::shared_ptr<WorkletsModuleProxy> &workletsModuleProxy,
3737
jsi::Runtime &rnRuntime,
3838
const std::shared_ptr<CallInvoker> &jsCallInvoker,
39-
const std::shared_ptr<UIScheduler> &uiScheduler,
4039
const PlatformDepMethodsHolder &platformDepMethodsHolder,
4140
const bool isBridgeless,
4241
const bool isReducedMotion);
@@ -194,7 +193,6 @@ class ReanimatedModuleProxy : public ReanimatedModuleProxySpec {
194193
const bool isBridgeless_;
195194
const bool isReducedMotion_;
196195
const std::shared_ptr<WorkletsModuleProxy> workletsModuleProxy_;
197-
const std::shared_ptr<UIScheduler> uiScheduler_;
198196
const std::string valueUnpackerCode_;
199197
std::shared_ptr<WorkletRuntime> uiWorkletRuntime_;
200198

packages/react-native-reanimated/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ WorkletsModuleProxy::WorkletsModuleProxy(
2222
const std::string &valueUnpackerCode,
2323
const std::shared_ptr<MessageQueueThread> &jsQueue,
2424
const std::shared_ptr<CallInvoker> &jsCallInvoker,
25-
const std::shared_ptr<JSScheduler> &jsScheduler)
25+
const std::shared_ptr<JSScheduler> &jsScheduler,
26+
const std::shared_ptr<UIScheduler> &uiScheduler)
2627
: WorkletsModuleProxySpec(jsCallInvoker),
2728
valueUnpackerCode_(valueUnpackerCode),
2829
jsQueue_(jsQueue),
29-
jsScheduler_(jsScheduler) {}
30+
jsScheduler_(jsScheduler),
31+
uiScheduler_(uiScheduler) {}
3032

3133
WorkletsModuleProxy::~WorkletsModuleProxy() {}
3234

packages/react-native-reanimated/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <worklets/NativeModules/WorkletsModuleProxySpec.h>
55
#include <worklets/Tools/JSScheduler.h>
66
#include <worklets/Tools/SingleInstanceChecker.h>
7+
#include <worklets/Tools/UIScheduler.h>
78
#include <worklets/WorkletRuntime/WorkletRuntime.h>
89
#include <memory>
910
#include <string>
@@ -16,7 +17,8 @@ class WorkletsModuleProxy : public WorkletsModuleProxySpec {
1617
const std::string &valueUnpackerCode,
1718
const std::shared_ptr<MessageQueueThread> &jsQueue,
1819
const std::shared_ptr<CallInvoker> &jsCallInvoker,
19-
const std::shared_ptr<JSScheduler> &jsScheduler);
20+
const std::shared_ptr<JSScheduler> &jsScheduler,
21+
const std::shared_ptr<UIScheduler> &uiScheduler);
2022

2123
~WorkletsModuleProxy();
2224

@@ -38,10 +40,15 @@ class WorkletsModuleProxy : public WorkletsModuleProxySpec {
3840
return jsScheduler_;
3941
}
4042

43+
[[nodiscard]] inline std::shared_ptr<UIScheduler> getUIScheduler() const {
44+
return uiScheduler_;
45+
}
46+
4147
private:
4248
const std::string valueUnpackerCode_;
4349
const std::shared_ptr<MessageQueueThread> jsQueue_;
4450
const std::shared_ptr<JSScheduler> jsScheduler_;
51+
const std::shared_ptr<UIScheduler> uiScheduler_;
4552
#ifndef NDEBUG
4653
SingleInstanceChecker<WorkletsModuleProxy> singleInstanceChecker_;
4754
#endif // NDEBUG

packages/react-native-reanimated/android/src/fabric/java/com/swmansion/reanimated/NativeProxy.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class NativeProxy extends NativeProxyCommon {
4141
workletsModule,
4242
Objects.requireNonNull(context.getJavaScriptContextHolder()).get(),
4343
callInvokerHolder,
44-
mAndroidUIScheduler,
4544
LayoutAnimations,
4645
context.isBridgeless(),
4746
fabricUIManager);
@@ -58,7 +57,6 @@ private native HybridData initHybrid(
5857
WorkletsModule workletsModule,
5958
long jsContext,
6059
CallInvokerHolderImpl jsCallInvokerHolder,
61-
AndroidUIScheduler androidUIScheduler,
6260
LayoutAnimations LayoutAnimations,
6361
boolean isBridgeless,
6462
FabricUIManager fabricUIManager);

packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include <reanimated/LayoutAnimations/LayoutAnimationsManager.h>
22
#include <reanimated/RuntimeDecorators/RNRuntimeDecorator.h>
33
#include <reanimated/Tools/PlatformDepMethodsHolder.h>
4-
#include <reanimated/android/AndroidUIScheduler.h>
54
#include <reanimated/android/NativeProxy.h>
65

76
#include <worklets/Tools/ReanimatedJSIUtils.h>
87
#include <worklets/Tools/ReanimatedVersion.h>
98
#include <worklets/WorkletRuntime/ReanimatedRuntime.h>
109
#include <worklets/WorkletRuntime/WorkletRuntime.h>
1110
#include <worklets/WorkletRuntime/WorkletRuntimeCollector.h>
11+
#include <worklets/android/AndroidUIScheduler.h>
1212

1313
#include <android/log.h>
1414
#include <fbjni/fbjni.h>
@@ -31,7 +31,6 @@ NativeProxy::NativeProxy(
3131
const std::shared_ptr<WorkletsModuleProxy> &workletsModuleProxy,
3232
jsi::Runtime *rnRuntime,
3333
const std::shared_ptr<facebook::react::CallInvoker> &jsCallInvoker,
34-
const std::shared_ptr<UIScheduler> &uiScheduler,
3534
jni::global_ref<LayoutAnimations::javaobject> layoutAnimations,
3635
const bool isBridgeless
3736
#ifdef RCT_NEW_ARCH_ENABLED
@@ -46,7 +45,6 @@ NativeProxy::NativeProxy(
4645
workletsModuleProxy,
4746
*rnRuntime,
4847
jsCallInvoker,
49-
uiScheduler,
5048
getPlatformDependentMethods(),
5149
isBridgeless,
5250
getIsReducedMotion())),
@@ -91,7 +89,6 @@ jni::local_ref<NativeProxy::jhybriddata> NativeProxy::initHybrid(
9189
jlong jsContext,
9290
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject>
9391
jsCallInvokerHolder,
94-
jni::alias_ref<AndroidUIScheduler::javaobject> androidUiScheduler,
9592
jni::alias_ref<LayoutAnimations::javaobject> layoutAnimations,
9693
bool isBridgeless
9794
#ifdef RCT_NEW_ARCH_ENABLED
@@ -101,14 +98,12 @@ jni::local_ref<NativeProxy::jhybriddata> NativeProxy::initHybrid(
10198
#endif
10299
) {
103100
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
104-
auto uiScheduler = androidUiScheduler->cthis()->getUIScheduler();
105101
auto workletsModuleProxy = jWorkletsModule->cthis()->getWorkletsModuleProxy();
106102
return makeCxxInstance(
107103
jThis,
108104
workletsModuleProxy,
109105
(jsi::Runtime *)jsContext,
110106
jsCallInvoker,
111-
uiScheduler,
112107
make_global(layoutAnimations),
113108
isBridgeless
114109
#ifdef RCT_NEW_ARCH_ENABLED

packages/react-native-reanimated/android/src/main/cpp/reanimated/android/NativeProxy.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#pragma once
22

33
#include <reanimated/NativeModules/ReanimatedModuleProxy.h>
4-
#include <reanimated/android/AndroidUIScheduler.h>
54
#include <reanimated/android/JNIHelper.h>
65
#include <reanimated/android/LayoutAnimations.h>
76

8-
#include <worklets/Tools/UIScheduler.h>
97
#include <worklets/android/WorkletsModule.h>
108

119
#include <ReactCommon/CallInvokerHolder.h>
@@ -153,7 +151,6 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
153151
jlong jsContext,
154152
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject>
155153
jsCallInvokerHolder,
156-
jni::alias_ref<AndroidUIScheduler::javaobject> androidUiScheduler,
157154
jni::alias_ref<LayoutAnimations::javaobject> layoutAnimations,
158155
const bool isBridgeless
159156
#ifdef RCT_NEW_ARCH_ENABLED
@@ -268,7 +265,6 @@ class NativeProxy : public jni::HybridClass<NativeProxy> {
268265
const std::shared_ptr<WorkletsModuleProxy> &workletsModuleProxy,
269266
jsi::Runtime *rnRuntime,
270267
const std::shared_ptr<facebook::react::CallInvoker> &jsCallInvoker,
271-
const std::shared_ptr<UIScheduler> &uiScheduler,
272268
jni::global_ref<LayoutAnimations::javaobject> layoutAnimations,
273269
const bool isBridgeless
274270
#ifdef RCT_NEW_ARCH_ENABLED

packages/react-native-reanimated/android/src/main/cpp/reanimated/android/OnLoad.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <fbjni/fbjni.h>
22

3-
#include <reanimated/android/AndroidUIScheduler.h>
43
#include <reanimated/android/LayoutAnimations.h>
54
#include <reanimated/android/NativeProxy.h>
65

@@ -9,7 +8,6 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
98
reanimated::NativeProxy::registerNatives();
109
reanimated::AnimationFrameCallback::registerNatives();
1110
reanimated::EventHandler::registerNatives();
12-
reanimated::AndroidUIScheduler::registerNatives();
1311
reanimated::LayoutAnimations::registerNatives();
1412
reanimated::SensorSetter::registerNatives();
1513
reanimated::KeyboardWorkletWrapper::registerNatives();
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include <reanimated/android/AndroidUIScheduler.h>
1+
#include <worklets/android/AndroidUIScheduler.h>
22

33
#include <android/log.h>
44
#include <fbjni/fbjni.h>
55
#include <jsi/jsi.h>
66

7-
namespace reanimated {
7+
namespace worklets {
88

99
using namespace facebook;
1010
using namespace react;
@@ -57,4 +57,4 @@ void AndroidUIScheduler::registerNatives() {
5757
});
5858
}
5959

60-
} // namespace reanimated
60+
} // namespace worklets
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
#include <memory>
1212

13-
namespace reanimated {
13+
namespace worklets {
1414

1515
using namespace facebook;
1616
using namespace worklets;
1717

1818
class AndroidUIScheduler : public jni::HybridClass<AndroidUIScheduler> {
1919
public:
2020
static auto constexpr kJavaDescriptor =
21-
"Lcom/swmansion/reanimated/AndroidUIScheduler;";
21+
"Lcom/swmansion/worklets/AndroidUIScheduler;";
2222
static jni::local_ref<jhybriddata> initHybrid(
2323
jni::alias_ref<jhybridobject> jThis);
2424
static void registerNatives();
@@ -41,4 +41,4 @@ class AndroidUIScheduler : public jni::HybridClass<AndroidUIScheduler> {
4141
jni::alias_ref<AndroidUIScheduler::jhybridobject> jThis);
4242
};
4343

44-
} // namespace reanimated
44+
} // namespace worklets

0 commit comments

Comments
 (0)