@solidnative-dev/fabric-host 0.0.0-stage → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +87 -2
- package/dist/fatal-error-handler.d.ts +27 -0
- package/dist/fatal-error-handler.d.ts.map +1 -0
- package/dist/fatal-error-handler.js +106 -0
- package/dist/fatal-error-handler.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/native-binding.d.ts +183 -0
- package/dist/native-binding.d.ts.map +1 -0
- package/dist/native-binding.js +554 -0
- package/dist/native-binding.js.map +1 -0
- package/dist/native-fabric-host.d.ts +32 -0
- package/dist/native-fabric-host.d.ts.map +1 -0
- package/dist/native-fabric-host.js +275 -0
- package/dist/native-fabric-host.js.map +1 -0
- package/dist/native-transaction.d.ts +17 -0
- package/dist/native-transaction.d.ts.map +1 -0
- package/dist/native-transaction.js +124 -0
- package/dist/native-transaction.js.map +1 -0
- package/dist/react-native-0.87.d.ts +61 -0
- package/dist/react-native-0.87.d.ts.map +1 -0
- package/dist/react-native-0.87.js +77 -0
- package/dist/react-native-0.87.js.map +1 -0
- package/dist/react-native-0.88.d.ts +77 -0
- package/dist/react-native-0.88.d.ts.map +1 -0
- package/dist/react-native-0.88.js +79 -0
- package/dist/react-native-0.88.js.map +1 -0
- package/dist/surface-readiness.d.ts +18 -0
- package/dist/surface-readiness.d.ts.map +1 -0
- package/dist/surface-readiness.js +43 -0
- package/dist/surface-readiness.js.map +1 -0
- package/native/README.md +502 -0
- package/native/SolidNativeFabric.podspec +84 -0
- package/native/android/SolidNativeBindingsInstaller.cpp +4218 -0
- package/native/android/SolidNativeBindingsInstaller.h +8 -0
- package/native/android/SolidNativeRuntime.cmake +61 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeBindingsInstaller.kt +239 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeDebugModule.kt +191 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativePackage.kt +43 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativePlatformModule.kt +81 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeStartupFailureView.kt +93 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeSurface.kt +423 -0
- package/native/android/solid-native.gradle +90 -0
- package/native/apple/SolidNativeDebugRequest.mm +421 -0
- package/native/fabric/SolidNativeDebugRequest.h +12 -0
- package/native/fabric/SolidNativeDirtyNodes.h +26 -0
- package/native/fabric/SolidNativeFabricApi.cpp +224 -0
- package/native/fabric/SolidNativeFabricApi.h +107 -0
- package/native/fabric/SolidNativeFabricApplication.h +32 -0
- package/native/fabric/SolidNativeFabricApplication.mm +373 -0
- package/native/fabric/SolidNativeFabricJSIBinding.h +43 -0
- package/native/fabric/SolidNativeFabricJSIBinding.mm +4491 -0
- package/native/fabric/SolidNativeFabricSmokeApplication.h +26 -0
- package/native/fabric/SolidNativeFabricSmokeApplication.mm +331 -0
- package/native/fabric/SolidNativeFabricSurface.h +63 -0
- package/native/fabric/SolidNativeFabricSurface.mm +549 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinator.cpp +1275 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinator.h +90 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinatorApple.mm +97 -0
- package/native/fabric/SolidNativeRootViewController.h +20 -0
- package/native/fabric/SolidNativeRootViewController.mm +83 -0
- package/native/fabric/SolidNativeStagedNodeMap.h +104 -0
- package/native/fabric/SolidNativeStartupFailureView.h +17 -0
- package/native/fabric/SolidNativeStartupFailureView.mm +77 -0
- package/native/react-native-boundary.json +106 -0
- package/native/worklets/SolidNativeUIWorklet.cpp +1080 -0
- package/native/worklets/SolidNativeUIWorklet.h +245 -0
- package/package.json +41 -3
- package/scripts/verify-react-native-boundary.mjs +282 -0
|
@@ -0,0 +1,4218 @@
|
|
|
1
|
+
#include <algorithm>
|
|
2
|
+
#include <array>
|
|
3
|
+
#include <atomic>
|
|
4
|
+
#include <chrono>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include <functional>
|
|
9
|
+
#include <limits>
|
|
10
|
+
#include <memory>
|
|
11
|
+
#include <mutex>
|
|
12
|
+
#include <optional>
|
|
13
|
+
#include <stdexcept>
|
|
14
|
+
#include <string>
|
|
15
|
+
#include <unordered_map>
|
|
16
|
+
#include <unordered_set>
|
|
17
|
+
#include <utility>
|
|
18
|
+
#include <vector>
|
|
19
|
+
|
|
20
|
+
#include <fbjni/fbjni.h>
|
|
21
|
+
#include <folly/dynamic.h>
|
|
22
|
+
#include <jsi/jsi.h>
|
|
23
|
+
#include <react/renderer/core/EventListener.h>
|
|
24
|
+
#include <react/renderer/core/InstanceHandle.h>
|
|
25
|
+
#include <react/renderer/core/RawEvent.h>
|
|
26
|
+
#include <react/renderer/core/ShadowNodeFamily.h>
|
|
27
|
+
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
|
|
28
|
+
#include <react/renderer/runtimescheduler/RuntimeSchedulerBinding.h>
|
|
29
|
+
#include <react/renderer/uimanager/UIManager.h>
|
|
30
|
+
#include <react/renderer/uimanager/UIManagerBinding.h>
|
|
31
|
+
#include <react/renderer/uimanager/UIManagerMountHook.h>
|
|
32
|
+
#include <react/runtime/jni/JBindingsInstaller.h>
|
|
33
|
+
|
|
34
|
+
#include "SolidNativeBindingsInstaller.h"
|
|
35
|
+
#include "SolidNativeFabricTransactionCoordinator.h"
|
|
36
|
+
#include "SolidNativeUIWorklet.h"
|
|
37
|
+
|
|
38
|
+
namespace solid_native::android {
|
|
39
|
+
namespace {
|
|
40
|
+
|
|
41
|
+
using facebook::jsi::Array;
|
|
42
|
+
using facebook::jsi::Function;
|
|
43
|
+
using facebook::jsi::JSError;
|
|
44
|
+
using facebook::jsi::Object;
|
|
45
|
+
using facebook::jsi::PropNameID;
|
|
46
|
+
using facebook::jsi::Runtime;
|
|
47
|
+
using facebook::jsi::String;
|
|
48
|
+
using facebook::jsi::Value;
|
|
49
|
+
using solid_native::fabric::react_native::NodeIdentity;
|
|
50
|
+
using solid_native::fabric::react_native::NodeIdentityMap;
|
|
51
|
+
using solid_native::fabric::react_native::NodeMeasurement;
|
|
52
|
+
using solid_native::fabric::react_native::NativeResourceResolver;
|
|
53
|
+
using solid_native::fabric::react_native::ReactNativeVersion;
|
|
54
|
+
using solid_native::fabric::react_native::TransactionCoordinator;
|
|
55
|
+
|
|
56
|
+
constexpr char GlobalBindingName[] = "__solidNativeHost";
|
|
57
|
+
constexpr int HostContractVersion = 1;
|
|
58
|
+
constexpr size_t MaximumTransportDepth = 64;
|
|
59
|
+
constexpr size_t MaximumTransportValues = 100000;
|
|
60
|
+
constexpr size_t MaximumRetainedResources = 64;
|
|
61
|
+
constexpr size_t MaximumResourceKindLength = 128;
|
|
62
|
+
constexpr size_t MaximumActiveUIWorklets = 256;
|
|
63
|
+
constexpr size_t MaximumFatalErrorNameBytes = 512;
|
|
64
|
+
constexpr size_t MaximumFatalErrorMessageBytes = 8192;
|
|
65
|
+
constexpr double MaximumSafeInteger = 9007199254740991.0;
|
|
66
|
+
|
|
67
|
+
double wallClockMilliseconds() {
|
|
68
|
+
const auto now = std::chrono::system_clock::now().time_since_epoch();
|
|
69
|
+
return std::chrono::duration<double, std::milli>(now).count();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
double monotonicMilliseconds() {
|
|
73
|
+
const auto now = std::chrono::steady_clock::now().time_since_epoch();
|
|
74
|
+
return std::chrono::duration<double, std::milli>(now).count();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
int64_t requireSafeInteger(
|
|
78
|
+
Runtime &runtime,
|
|
79
|
+
const Value &value,
|
|
80
|
+
const char *name,
|
|
81
|
+
bool positive) {
|
|
82
|
+
if (!value.isNumber()) {
|
|
83
|
+
throw JSError(runtime, std::string{name} + " must be an integer.");
|
|
84
|
+
}
|
|
85
|
+
const auto number = value.getNumber();
|
|
86
|
+
if (!std::isfinite(number) || std::trunc(number) != number ||
|
|
87
|
+
std::abs(number) > MaximumSafeInteger || (positive && number <= 0)) {
|
|
88
|
+
throw JSError(
|
|
89
|
+
runtime,
|
|
90
|
+
std::string{name} +
|
|
91
|
+
(positive ? " must be a positive safe integer."
|
|
92
|
+
: " must be a safe integer."));
|
|
93
|
+
}
|
|
94
|
+
return static_cast<int64_t>(number);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
std::string requireResourceKind(Runtime &runtime, const Value &value) {
|
|
98
|
+
if (!value.isString()) {
|
|
99
|
+
throw JSError(runtime, "The native resource kind must be a string.");
|
|
100
|
+
}
|
|
101
|
+
const auto kind = value.getString(runtime).utf8(runtime);
|
|
102
|
+
const auto validStart = [](char character) {
|
|
103
|
+
return character >= 'a' && character <= 'z';
|
|
104
|
+
};
|
|
105
|
+
const auto validPart = [&](char character) {
|
|
106
|
+
return validStart(character) ||
|
|
107
|
+
(character >= '0' && character <= '9') || character == '.' ||
|
|
108
|
+
character == '-';
|
|
109
|
+
};
|
|
110
|
+
if (kind.empty() || kind.size() > MaximumResourceKindLength ||
|
|
111
|
+
!validStart(kind.front()) ||
|
|
112
|
+
!std::all_of(kind.begin() + 1, kind.end(), validPart)) {
|
|
113
|
+
throw JSError(
|
|
114
|
+
runtime,
|
|
115
|
+
"The native resource kind must contain 1-128 lowercase identifier "
|
|
116
|
+
"characters.");
|
|
117
|
+
}
|
|
118
|
+
return kind;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
std::string requireFatalErrorText(
|
|
122
|
+
Runtime &runtime,
|
|
123
|
+
const Value &value,
|
|
124
|
+
const char *name,
|
|
125
|
+
size_t maximumBytes) {
|
|
126
|
+
if (!value.isString()) {
|
|
127
|
+
throw JSError(runtime, std::string{name} + " must be a string.");
|
|
128
|
+
}
|
|
129
|
+
const auto text = value.getString(runtime).utf8(runtime);
|
|
130
|
+
if (text.empty() || text.size() > maximumBytes ||
|
|
131
|
+
text.find('\0') != std::string::npos) {
|
|
132
|
+
throw JSError(
|
|
133
|
+
runtime,
|
|
134
|
+
std::string{name} + " must be a bounded non-empty string without null bytes.");
|
|
135
|
+
}
|
|
136
|
+
return text;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
std::string childPath(const std::string &path, const std::string &name) {
|
|
140
|
+
return path + "." + name;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
std::string indexPath(const std::string &path, size_t index) {
|
|
144
|
+
return path + "[" + std::to_string(index) + "]";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
std::string requireTransportString(
|
|
148
|
+
std::string value,
|
|
149
|
+
const std::string &path) {
|
|
150
|
+
if (value.find('\0') != std::string::npos) {
|
|
151
|
+
throw std::invalid_argument(
|
|
152
|
+
path + ": strings containing a null byte are not supported.");
|
|
153
|
+
}
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
bool containsObject(
|
|
158
|
+
Runtime &runtime,
|
|
159
|
+
const std::vector<Value> &ancestors,
|
|
160
|
+
const Object &candidate) {
|
|
161
|
+
for (const auto &value : ancestors) {
|
|
162
|
+
auto ancestor = value.getObject(runtime);
|
|
163
|
+
if (Object::strictEquals(runtime, ancestor, candidate)) return true;
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
folly::dynamic dynamicFromJSI(
|
|
169
|
+
Runtime &runtime,
|
|
170
|
+
const Value &value,
|
|
171
|
+
const std::string &path,
|
|
172
|
+
size_t depth,
|
|
173
|
+
size_t &valueCount,
|
|
174
|
+
std::vector<Value> &ancestors) {
|
|
175
|
+
if (++valueCount > MaximumTransportValues) {
|
|
176
|
+
throw std::invalid_argument(
|
|
177
|
+
"The host transaction exceeds the transport value limit.");
|
|
178
|
+
}
|
|
179
|
+
if (depth > MaximumTransportDepth) {
|
|
180
|
+
throw std::invalid_argument(
|
|
181
|
+
path + ": the host transaction exceeds the nesting limit.");
|
|
182
|
+
}
|
|
183
|
+
if (value.isUndefined()) {
|
|
184
|
+
throw std::invalid_argument(
|
|
185
|
+
path + ": undefined is not a transport-safe value.");
|
|
186
|
+
}
|
|
187
|
+
if (value.isNull()) return nullptr;
|
|
188
|
+
if (value.isBool()) return value.getBool();
|
|
189
|
+
if (value.isNumber()) {
|
|
190
|
+
const auto number = value.getNumber();
|
|
191
|
+
if (!std::isfinite(number)) {
|
|
192
|
+
throw std::invalid_argument(path + ": numbers must be finite.");
|
|
193
|
+
}
|
|
194
|
+
return number;
|
|
195
|
+
}
|
|
196
|
+
if (value.isString()) {
|
|
197
|
+
return requireTransportString(
|
|
198
|
+
value.getString(runtime).utf8(runtime), path);
|
|
199
|
+
}
|
|
200
|
+
if (value.isBigInt()) {
|
|
201
|
+
throw std::invalid_argument(
|
|
202
|
+
path + ": bigint is not a transport-safe value.");
|
|
203
|
+
}
|
|
204
|
+
if (value.isSymbol()) {
|
|
205
|
+
throw std::invalid_argument(
|
|
206
|
+
path + ": symbol is not a transport-safe value.");
|
|
207
|
+
}
|
|
208
|
+
if (!value.isObject()) {
|
|
209
|
+
throw std::invalid_argument(path + ": unsupported JavaScript value.");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
auto object = value.getObject(runtime);
|
|
213
|
+
if (object.isFunction(runtime)) {
|
|
214
|
+
throw std::invalid_argument(
|
|
215
|
+
path + ": functions are not transport-safe values.");
|
|
216
|
+
}
|
|
217
|
+
if (containsObject(runtime, ancestors, object)) {
|
|
218
|
+
throw std::invalid_argument(
|
|
219
|
+
path + ": cyclic objects are not transport-safe values.");
|
|
220
|
+
}
|
|
221
|
+
ancestors.emplace_back(runtime, object);
|
|
222
|
+
|
|
223
|
+
if (object.isArray(runtime)) {
|
|
224
|
+
auto array = object.getArray(runtime);
|
|
225
|
+
folly::dynamic result = folly::dynamic::array;
|
|
226
|
+
const auto size = array.size(runtime);
|
|
227
|
+
result.reserve(size);
|
|
228
|
+
for (size_t index = 0; index < size; ++index) {
|
|
229
|
+
const auto child = array.getValueAtIndex(runtime, index);
|
|
230
|
+
result.push_back(dynamicFromJSI(
|
|
231
|
+
runtime,
|
|
232
|
+
child,
|
|
233
|
+
indexPath(path, index),
|
|
234
|
+
depth + 1,
|
|
235
|
+
valueCount,
|
|
236
|
+
ancestors));
|
|
237
|
+
}
|
|
238
|
+
ancestors.pop_back();
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
folly::dynamic result = folly::dynamic::object;
|
|
243
|
+
auto names = object.getPropertyNames(runtime);
|
|
244
|
+
const auto size = names.size(runtime);
|
|
245
|
+
for (size_t index = 0; index < size; ++index) {
|
|
246
|
+
auto nameString = names.getValueAtIndex(runtime, index).getString(runtime);
|
|
247
|
+
auto name = requireTransportString(nameString.utf8(runtime), path);
|
|
248
|
+
const auto child = object.getProperty(runtime, nameString);
|
|
249
|
+
result[name] = dynamicFromJSI(
|
|
250
|
+
runtime,
|
|
251
|
+
child,
|
|
252
|
+
childPath(path, name),
|
|
253
|
+
depth + 1,
|
|
254
|
+
valueCount,
|
|
255
|
+
ancestors);
|
|
256
|
+
}
|
|
257
|
+
ancestors.pop_back();
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
int64_t dynamicInteger(const folly::dynamic &value) {
|
|
262
|
+
if (!value.isInt() && !value.isDouble()) return 0;
|
|
263
|
+
const auto number = value.asDouble();
|
|
264
|
+
if (!std::isfinite(number) || std::trunc(number) != number ||
|
|
265
|
+
std::abs(number) > MaximumSafeInteger) {
|
|
266
|
+
return 0;
|
|
267
|
+
}
|
|
268
|
+
return static_cast<int64_t>(number);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
struct RoutedEvent final {
|
|
272
|
+
int64_t node;
|
|
273
|
+
std::string name;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
std::vector<std::string> semanticEventNames(const std::string &nativeName) {
|
|
277
|
+
auto name = nativeName;
|
|
278
|
+
if (name.size() > 3 && name.compare(0, 3, "top") == 0) {
|
|
279
|
+
name = name.substr(3);
|
|
280
|
+
if (!name.empty() && name[0] >= 'A' && name[0] <= 'Z') {
|
|
281
|
+
name[0] = static_cast<char>(name[0] - 'A' + 'a');
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (name == "click") return {"press"};
|
|
285
|
+
if (name == "touchStart" || name == "pointerDown") return {"pressIn"};
|
|
286
|
+
if (name == "touchMove" || name == "pointerMove") return {"pressMove"};
|
|
287
|
+
if (name == "pointerEnter") return {"hoverIn"};
|
|
288
|
+
if (name == "pointerLeave") return {"hoverOut"};
|
|
289
|
+
if (name == "touchEnd") return {"pressOut", "press"};
|
|
290
|
+
if (name == "touchCancel" || name == "pointerCancel") {
|
|
291
|
+
return {"pressCancel"};
|
|
292
|
+
}
|
|
293
|
+
if (name == "pointerUp") return {"pressOut"};
|
|
294
|
+
// TextInput and Switch share React Native's generic `change` event. The
|
|
295
|
+
// committed per-node subscription selects the portable semantic route.
|
|
296
|
+
if (name == "change") return {"changeText", "valueChange"};
|
|
297
|
+
if (name == "appear") return {"focus"};
|
|
298
|
+
if (name == "disappear") return {"blur"};
|
|
299
|
+
if (name == "dismissed") return {"dismiss"};
|
|
300
|
+
if (name == "nativeDismissCancelled") return {"nativeDismissCancel"};
|
|
301
|
+
if (name == "sheetDetentChanged") return {"sheetDetentChange"};
|
|
302
|
+
if (name == "finishTransitioning") return {"transitionEnd"};
|
|
303
|
+
return {name};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
bool eventBubbles(const std::string &name) {
|
|
307
|
+
return name == "endEditing" || name == "keyPress" ||
|
|
308
|
+
name == "longPress" || name == "press" || name == "pressCancel" ||
|
|
309
|
+
name == "pressIn" || name == "pressMove" || name == "pressOut" ||
|
|
310
|
+
name == "scroll" ||
|
|
311
|
+
name == "scrollBeginDrag" || name == "scrollEndDrag" ||
|
|
312
|
+
name == "valueChange";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const char *eventPriority(facebook::react::RawEvent::Category category) {
|
|
316
|
+
using Category = facebook::react::RawEvent::Category;
|
|
317
|
+
switch (category) {
|
|
318
|
+
case Category::Discrete:
|
|
319
|
+
case Category::ContinuousStart:
|
|
320
|
+
case Category::ContinuousEnd:
|
|
321
|
+
return "discrete";
|
|
322
|
+
case Category::Continuous:
|
|
323
|
+
return "continuous";
|
|
324
|
+
case Category::Unspecified:
|
|
325
|
+
case Category::Idle:
|
|
326
|
+
return "default";
|
|
327
|
+
}
|
|
328
|
+
return "default";
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
const char *semanticEventPriority(
|
|
332
|
+
const std::string &name,
|
|
333
|
+
facebook::react::RawEvent::Category category) {
|
|
334
|
+
if (name == "changeText" || name == "keyPress" || name == "valueChange" ||
|
|
335
|
+
name == "submitEditing" || name == "scrollBeginDrag" ||
|
|
336
|
+
name == "scrollEndDrag" || name == "accessibilityAction" ||
|
|
337
|
+
name == "accessibilityEscape" || name == "accessibilityTap" ||
|
|
338
|
+
name == "magicTap") {
|
|
339
|
+
return "discrete";
|
|
340
|
+
}
|
|
341
|
+
if (name == "contentSizeChange" || name == "insetsChange" ||
|
|
342
|
+
name == "layout" || name == "selectionChange") {
|
|
343
|
+
return "default";
|
|
344
|
+
}
|
|
345
|
+
if (name == "scroll") return "continuous";
|
|
346
|
+
if (name == "tabSelected" || name == "tabSelectionRejected" ||
|
|
347
|
+
name == "tabSelectionPrevented" ||
|
|
348
|
+
name == "pressHeaderBarButtonItem" ||
|
|
349
|
+
name == "pressHeaderBarButtonMenuItem") {
|
|
350
|
+
return "discrete";
|
|
351
|
+
}
|
|
352
|
+
// Android's legacy Event dispatcher sends coalescible Image completion
|
|
353
|
+
// events through dispatchUniqueEvent, which assigns Continuous even though
|
|
354
|
+
// load/error are asynchronous resource notifications rather than continuous
|
|
355
|
+
// user input. Screen visibility events are lifecycle notifications with the
|
|
356
|
+
// same default-priority semantics. Keep the framework-neutral host priority
|
|
357
|
+
// stable across the Android and iOS backend paths.
|
|
358
|
+
if (name == "load" || name == "error" || name == "focus" ||
|
|
359
|
+
name == "blur" || name == "transitionEnd" || name == "show" ||
|
|
360
|
+
name == "dismiss" || name == "orientationChange" ||
|
|
361
|
+
name == "requestClose" || name == "willAppear" ||
|
|
362
|
+
name == "didAppear" || name == "willDisappear" ||
|
|
363
|
+
name == "didDisappear" || name == "moreTabSelected" ||
|
|
364
|
+
name == "sheetDetentChange") {
|
|
365
|
+
return "default";
|
|
366
|
+
}
|
|
367
|
+
return eventPriority(category);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
class EventRoutingState final {
|
|
371
|
+
public:
|
|
372
|
+
struct LayoutRouteStageNode final {
|
|
373
|
+
int64_t node;
|
|
374
|
+
facebook::react::Tag tag;
|
|
375
|
+
std::optional<int64_t> previousNodeForTag;
|
|
376
|
+
std::optional<facebook::react::Tag> previousTagForNode;
|
|
377
|
+
std::optional<std::unordered_set<std::string>> previousEvents;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
struct LayoutRouteStage final {
|
|
381
|
+
bool active{false};
|
|
382
|
+
int64_t previousSurface{0};
|
|
383
|
+
int64_t previousSequence{0};
|
|
384
|
+
std::vector<LayoutRouteStageNode> nodes;
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
std::atomic<bool> active{true};
|
|
388
|
+
std::atomic<bool> handlerInstalled{false};
|
|
389
|
+
std::atomic<int64_t> surface{0};
|
|
390
|
+
std::atomic<int64_t> sequence{0};
|
|
391
|
+
|
|
392
|
+
std::optional<std::vector<RoutedEvent>> route(
|
|
393
|
+
facebook::react::Tag tag,
|
|
394
|
+
const std::string &nativeName) const {
|
|
395
|
+
std::lock_guard lock(mutex_);
|
|
396
|
+
const auto nodeEntry = nodesByTag_.find(tag);
|
|
397
|
+
if (nodeEntry == nodesByTag_.end()) return std::nullopt;
|
|
398
|
+
|
|
399
|
+
std::vector<RoutedEvent> events;
|
|
400
|
+
for (const auto &name : semanticEventNames(nativeName)) {
|
|
401
|
+
auto node = nodeEntry->second;
|
|
402
|
+
while (true) {
|
|
403
|
+
const auto eventsEntry = eventsByNode_.find(node);
|
|
404
|
+
if (eventsEntry != eventsByNode_.end() &&
|
|
405
|
+
eventsEntry->second.contains(name)) {
|
|
406
|
+
events.push_back(RoutedEvent{.node = node, .name = name});
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
if (!eventBubbles(name)) break;
|
|
410
|
+
const auto parentEntry = parentsByNode_.find(node);
|
|
411
|
+
if (parentEntry == parentsByNode_.end()) break;
|
|
412
|
+
node = parentEntry->second;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return events;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
bool isSubscribed(int64_t node, const std::string &name) const {
|
|
419
|
+
std::lock_guard lock(mutex_);
|
|
420
|
+
const auto entry = eventsByNode_.find(node);
|
|
421
|
+
return entry != eventsByNode_.end() && entry->second.contains(name);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
std::optional<facebook::react::Tag> tagForNode(int64_t node) const {
|
|
425
|
+
std::lock_guard lock(mutex_);
|
|
426
|
+
const auto entry = tagsByNode_.find(node);
|
|
427
|
+
if (entry == tagsByNode_.end()) return std::nullopt;
|
|
428
|
+
return entry->second;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
void reclaimNode(int64_t node) {
|
|
432
|
+
std::lock_guard lock(mutex_);
|
|
433
|
+
eventsByNode_.erase(node);
|
|
434
|
+
parentsByNode_.erase(node);
|
|
435
|
+
const auto tagEntry = tagsByNode_.find(node);
|
|
436
|
+
if (tagEntry == tagsByNode_.end()) return;
|
|
437
|
+
nodesByTag_.erase(tagEntry->second);
|
|
438
|
+
tagsByNode_.erase(tagEntry);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
LayoutRouteStage stageLayoutRoutes(
|
|
442
|
+
const folly::dynamic &transaction,
|
|
443
|
+
const NodeIdentityMap &identities) {
|
|
444
|
+
LayoutRouteStage stage;
|
|
445
|
+
std::unordered_map<int64_t, std::unordered_set<std::string>> layoutEvents;
|
|
446
|
+
if (transaction.isObject() && transaction.count("mutations") != 0 &&
|
|
447
|
+
transaction.at("mutations").isArray()) {
|
|
448
|
+
for (const auto &mutation : transaction.at("mutations")) {
|
|
449
|
+
if (!mutation.isObject() || mutation.count("type") == 0 ||
|
|
450
|
+
!mutation.at("type").isString() ||
|
|
451
|
+
mutation.at("type").getString() != "update-event-listeners" ||
|
|
452
|
+
mutation.count("node") == 0 || mutation.count("events") == 0 ||
|
|
453
|
+
!mutation.at("events").isArray()) {
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
std::unordered_set<std::string> events;
|
|
457
|
+
for (const auto &event : mutation.at("events")) {
|
|
458
|
+
if (event.isString()) events.insert(event.getString());
|
|
459
|
+
}
|
|
460
|
+
const auto node = dynamicInteger(mutation.at("node"));
|
|
461
|
+
if (events.contains("layout")) {
|
|
462
|
+
layoutEvents[node] = std::move(events);
|
|
463
|
+
} else {
|
|
464
|
+
layoutEvents.erase(node);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (layoutEvents.empty()) return stage;
|
|
469
|
+
|
|
470
|
+
std::lock_guard lock(mutex_);
|
|
471
|
+
stage.active = true;
|
|
472
|
+
stage.previousSurface = surface.load(std::memory_order_acquire);
|
|
473
|
+
stage.previousSequence = sequence.load(std::memory_order_acquire);
|
|
474
|
+
for (auto &[node, events] : layoutEvents) {
|
|
475
|
+
std::optional<facebook::react::Tag> tag;
|
|
476
|
+
const auto identity = identities.find(node);
|
|
477
|
+
if (identity != identities.end()) {
|
|
478
|
+
tag = identity->second.tag;
|
|
479
|
+
} else {
|
|
480
|
+
const auto existingTag = tagsByNode_.find(node);
|
|
481
|
+
if (existingTag != tagsByNode_.end()) tag = existingTag->second;
|
|
482
|
+
}
|
|
483
|
+
if (!tag.has_value()) continue;
|
|
484
|
+
|
|
485
|
+
LayoutRouteStageNode stagedNode{
|
|
486
|
+
.node = node,
|
|
487
|
+
.tag = *tag,
|
|
488
|
+
.previousNodeForTag = std::nullopt,
|
|
489
|
+
.previousTagForNode = std::nullopt,
|
|
490
|
+
.previousEvents = std::nullopt};
|
|
491
|
+
const auto previousNode = nodesByTag_.find(*tag);
|
|
492
|
+
if (previousNode != nodesByTag_.end()) {
|
|
493
|
+
stagedNode.previousNodeForTag = previousNode->second;
|
|
494
|
+
}
|
|
495
|
+
const auto previousTag = tagsByNode_.find(node);
|
|
496
|
+
if (previousTag != tagsByNode_.end()) {
|
|
497
|
+
stagedNode.previousTagForNode = previousTag->second;
|
|
498
|
+
}
|
|
499
|
+
const auto previousEvents = eventsByNode_.find(node);
|
|
500
|
+
if (previousEvents != eventsByNode_.end()) {
|
|
501
|
+
stagedNode.previousEvents = previousEvents->second;
|
|
502
|
+
}
|
|
503
|
+
nodesByTag_[*tag] = node;
|
|
504
|
+
tagsByNode_[node] = *tag;
|
|
505
|
+
eventsByNode_[node] = std::move(events);
|
|
506
|
+
stage.nodes.push_back(std::move(stagedNode));
|
|
507
|
+
}
|
|
508
|
+
if (stage.nodes.empty()) {
|
|
509
|
+
stage.active = false;
|
|
510
|
+
return stage;
|
|
511
|
+
}
|
|
512
|
+
if (transaction.count("surface") != 0) {
|
|
513
|
+
surface.store(
|
|
514
|
+
dynamicInteger(transaction.at("surface")),
|
|
515
|
+
std::memory_order_release);
|
|
516
|
+
}
|
|
517
|
+
if (transaction.count("sequence") != 0) {
|
|
518
|
+
sequence.store(
|
|
519
|
+
dynamicInteger(transaction.at("sequence")),
|
|
520
|
+
std::memory_order_release);
|
|
521
|
+
}
|
|
522
|
+
return stage;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
void finishLayoutRoutes(LayoutRouteStage &stage) {
|
|
526
|
+
stage.active = false;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
void rollbackLayoutRoutes(LayoutRouteStage &stage) {
|
|
530
|
+
if (!stage.active) return;
|
|
531
|
+
std::lock_guard lock(mutex_);
|
|
532
|
+
for (auto iterator = stage.nodes.rbegin();
|
|
533
|
+
iterator != stage.nodes.rend();
|
|
534
|
+
++iterator) {
|
|
535
|
+
const auto &node = *iterator;
|
|
536
|
+
if (node.previousNodeForTag.has_value()) {
|
|
537
|
+
nodesByTag_[node.tag] = *node.previousNodeForTag;
|
|
538
|
+
} else {
|
|
539
|
+
nodesByTag_.erase(node.tag);
|
|
540
|
+
}
|
|
541
|
+
if (node.previousTagForNode.has_value()) {
|
|
542
|
+
tagsByNode_[node.node] = *node.previousTagForNode;
|
|
543
|
+
} else {
|
|
544
|
+
tagsByNode_.erase(node.node);
|
|
545
|
+
}
|
|
546
|
+
if (node.previousEvents.has_value()) {
|
|
547
|
+
eventsByNode_[node.node] = *node.previousEvents;
|
|
548
|
+
} else {
|
|
549
|
+
eventsByNode_.erase(node.node);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
surface.store(stage.previousSurface, std::memory_order_release);
|
|
553
|
+
sequence.store(stage.previousSequence, std::memory_order_release);
|
|
554
|
+
stage.active = false;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
void publishCommit(
|
|
558
|
+
const folly::dynamic &transaction,
|
|
559
|
+
const NodeIdentityMap &identities,
|
|
560
|
+
int64_t committedSequence) {
|
|
561
|
+
std::lock_guard lock(mutex_);
|
|
562
|
+
for (const auto &[node, identity] : identities) {
|
|
563
|
+
nodesByTag_[identity.tag] = node;
|
|
564
|
+
tagsByNode_[node] = identity.tag;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (transaction.isObject() && transaction.count("mutations") != 0) {
|
|
568
|
+
const auto &mutations = transaction.at("mutations");
|
|
569
|
+
if (mutations.isArray()) {
|
|
570
|
+
for (const auto &mutation : mutations) {
|
|
571
|
+
if (!mutation.isObject() || mutation.count("type") == 0 ||
|
|
572
|
+
!mutation.at("type").isString()) {
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
const auto &type = mutation.at("type").getString();
|
|
576
|
+
if (type == "insert-child" || type == "remove-child") {
|
|
577
|
+
if (mutation.count("parent") == 0 ||
|
|
578
|
+
mutation.count("child") == 0) {
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
const auto parent = dynamicInteger(mutation.at("parent"));
|
|
582
|
+
const auto child = dynamicInteger(mutation.at("child"));
|
|
583
|
+
if (parent <= 0 || child <= 0) continue;
|
|
584
|
+
if (type == "insert-child") {
|
|
585
|
+
parentsByNode_[child] = parent;
|
|
586
|
+
} else {
|
|
587
|
+
const auto parentEntry = parentsByNode_.find(child);
|
|
588
|
+
if (parentEntry != parentsByNode_.end() &&
|
|
589
|
+
parentEntry->second == parent) {
|
|
590
|
+
parentsByNode_.erase(parentEntry);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
continue;
|
|
594
|
+
}
|
|
595
|
+
if (mutation.count("node") == 0) continue;
|
|
596
|
+
const auto node = dynamicInteger(mutation.at("node"));
|
|
597
|
+
if (node <= 0) continue;
|
|
598
|
+
if (type == "delete-node") {
|
|
599
|
+
eventsByNode_.erase(node);
|
|
600
|
+
parentsByNode_.erase(node);
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
if (type != "update-event-listeners" ||
|
|
604
|
+
mutation.count("events") == 0 ||
|
|
605
|
+
!mutation.at("events").isArray()) {
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
std::unordered_set<std::string> events;
|
|
609
|
+
for (const auto &event : mutation.at("events")) {
|
|
610
|
+
if (event.isString()) events.insert(event.getString());
|
|
611
|
+
}
|
|
612
|
+
eventsByNode_[node] = std::move(events);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
sequence.store(committedSequence, std::memory_order_release);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
private:
|
|
620
|
+
mutable std::mutex mutex_;
|
|
621
|
+
std::unordered_map<facebook::react::Tag, int64_t> nodesByTag_;
|
|
622
|
+
std::unordered_map<int64_t, facebook::react::Tag> tagsByNode_;
|
|
623
|
+
std::unordered_map<int64_t, int64_t> parentsByNode_;
|
|
624
|
+
std::unordered_map<int64_t, std::unordered_set<std::string>> eventsByNode_;
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
facebook::react::UIManager &uiManager(Runtime &runtime) {
|
|
628
|
+
auto binding = facebook::react::UIManagerBinding::getBinding(runtime);
|
|
629
|
+
if (!binding) {
|
|
630
|
+
throw JSError(
|
|
631
|
+
runtime, "React Native did not install its UIManager binding.");
|
|
632
|
+
}
|
|
633
|
+
return binding->getUIManager();
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
void installEmptySurfaceStopHandler(Runtime &runtime) {
|
|
637
|
+
auto global = runtime.global();
|
|
638
|
+
auto existing = global.getProperty(runtime, "RN$stopSurface");
|
|
639
|
+
if (existing.isObject() && existing.getObject(runtime).isFunction(runtime)) {
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
global.setProperty(
|
|
643
|
+
runtime,
|
|
644
|
+
"RN$stopSurface",
|
|
645
|
+
Function::createFromHostFunction(
|
|
646
|
+
runtime,
|
|
647
|
+
PropNameID::forAscii(runtime, "RN$stopSurface"),
|
|
648
|
+
1,
|
|
649
|
+
[](Runtime &,
|
|
650
|
+
const Value &,
|
|
651
|
+
const Value *,
|
|
652
|
+
size_t) -> Value { return Value(); }));
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
enum class AndroidViewOutput : size_t {
|
|
656
|
+
Opacity = 0,
|
|
657
|
+
TranslateX = 1,
|
|
658
|
+
TranslateY = 2,
|
|
659
|
+
ScaleX = 3,
|
|
660
|
+
ScaleY = 4,
|
|
661
|
+
Rotation = 5,
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
constexpr size_t AndroidViewOutputCount = 6;
|
|
665
|
+
|
|
666
|
+
AndroidViewOutput requireAndroidViewOutput(const std::string &name) {
|
|
667
|
+
if (name == "opacity") return AndroidViewOutput::Opacity;
|
|
668
|
+
if (name == "translateX") return AndroidViewOutput::TranslateX;
|
|
669
|
+
if (name == "translateY") return AndroidViewOutput::TranslateY;
|
|
670
|
+
if (name == "scaleX") return AndroidViewOutput::ScaleX;
|
|
671
|
+
if (name == "scaleY") return AndroidViewOutput::ScaleY;
|
|
672
|
+
if (name == "rotation") return AndroidViewOutput::Rotation;
|
|
673
|
+
throw std::invalid_argument(
|
|
674
|
+
"Android native view worklets do not support output channel " + name +
|
|
675
|
+
".");
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
struct AndroidUIWorkletTimingState final {
|
|
679
|
+
std::vector<double> from;
|
|
680
|
+
solid_native::worklets::UIWorkletTimingSequence definition;
|
|
681
|
+
std::optional<int64_t> startedAtNanoseconds;
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
struct AndroidUIWorkletSpringState final {
|
|
685
|
+
std::vector<double> from;
|
|
686
|
+
std::vector<double> to;
|
|
687
|
+
solid_native::worklets::UIWorkletSpring definition;
|
|
688
|
+
std::optional<int64_t> startedAtNanoseconds;
|
|
689
|
+
};
|
|
690
|
+
|
|
691
|
+
struct AndroidUIWorkletDecayState final {
|
|
692
|
+
std::vector<double> from;
|
|
693
|
+
std::vector<double> velocities;
|
|
694
|
+
double initialSpeed;
|
|
695
|
+
solid_native::worklets::UIWorkletDecay definition;
|
|
696
|
+
std::optional<int64_t> startedAtNanoseconds;
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
struct AndroidUIWorkletPanState final {
|
|
700
|
+
solid_native::worklets::UIWorkletPanGesture definition;
|
|
701
|
+
int64_t token;
|
|
702
|
+
bool active{false};
|
|
703
|
+
double originX{0};
|
|
704
|
+
double originY{0};
|
|
705
|
+
int64_t sequence{0};
|
|
706
|
+
std::optional<double> timestamp;
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
struct AndroidUIWorkletState final {
|
|
710
|
+
AndroidUIWorkletState(
|
|
711
|
+
int64_t handleValue,
|
|
712
|
+
int64_t targetNodeValue,
|
|
713
|
+
facebook::react::Tag targetTagValue,
|
|
714
|
+
solid_native::worklets::UIWorkletGraph graphValue,
|
|
715
|
+
std::vector<AndroidViewOutput> channelValues)
|
|
716
|
+
: handle(handleValue),
|
|
717
|
+
targetNode(targetNodeValue),
|
|
718
|
+
targetTag(targetTagValue),
|
|
719
|
+
graph(std::move(graphValue)),
|
|
720
|
+
channels(std::move(channelValues)),
|
|
721
|
+
inputs(graph.initialInputs()),
|
|
722
|
+
outputs(graph.evaluate(inputs)) {}
|
|
723
|
+
|
|
724
|
+
mutable std::mutex mutex;
|
|
725
|
+
const int64_t handle;
|
|
726
|
+
const int64_t targetNode;
|
|
727
|
+
const facebook::react::Tag targetTag;
|
|
728
|
+
const solid_native::worklets::UIWorkletGraph graph;
|
|
729
|
+
const std::vector<AndroidViewOutput> channels;
|
|
730
|
+
std::vector<double> inputs;
|
|
731
|
+
std::vector<solid_native::worklets::UIWorkletOutputValue> outputs;
|
|
732
|
+
bool active{true};
|
|
733
|
+
bool framePending{false};
|
|
734
|
+
std::optional<int64_t> frameToken;
|
|
735
|
+
bool hasTimestamp{false};
|
|
736
|
+
double timestamp{0};
|
|
737
|
+
int64_t sequence{0};
|
|
738
|
+
int64_t appliedSequence{0};
|
|
739
|
+
int64_t frameTimeNanoseconds{0};
|
|
740
|
+
std::optional<AndroidUIWorkletTimingState> timing;
|
|
741
|
+
double timingProgress{0};
|
|
742
|
+
solid_native::worklets::UIWorkletFrameStatistics timingFrameStatistics;
|
|
743
|
+
std::optional<AndroidUIWorkletSpringState> spring;
|
|
744
|
+
std::optional<double> springPosition;
|
|
745
|
+
std::optional<double> springVelocity;
|
|
746
|
+
solid_native::worklets::UIWorkletFrameStatistics springFrameStatistics;
|
|
747
|
+
std::optional<AndroidUIWorkletDecayState> decay;
|
|
748
|
+
std::optional<double> decayElapsedMilliseconds;
|
|
749
|
+
std::optional<double> decaySpeed;
|
|
750
|
+
solid_native::worklets::UIWorkletFrameStatistics decayFrameStatistics;
|
|
751
|
+
std::optional<AndroidUIWorkletPanState> pan;
|
|
752
|
+
bool applied{false};
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
Object makeUIWorkletFrameStatistics(
|
|
756
|
+
Runtime &runtime,
|
|
757
|
+
const solid_native::worklets::UIWorkletFrameStatisticsSnapshot &value) {
|
|
758
|
+
Object statistics(runtime);
|
|
759
|
+
statistics.setProperty(
|
|
760
|
+
runtime, "frameCount", static_cast<double>(value.frameCount));
|
|
761
|
+
statistics.setProperty(
|
|
762
|
+
runtime, "intervalCount", static_cast<double>(value.intervalCount));
|
|
763
|
+
statistics.setProperty(
|
|
764
|
+
runtime,
|
|
765
|
+
"sampledIntervalCount",
|
|
766
|
+
static_cast<double>(value.sampledIntervalCount));
|
|
767
|
+
statistics.setProperty(
|
|
768
|
+
runtime,
|
|
769
|
+
"droppedIntervalSampleCount",
|
|
770
|
+
static_cast<double>(value.droppedIntervalSampleCount));
|
|
771
|
+
statistics.setProperty(
|
|
772
|
+
runtime,
|
|
773
|
+
"firstFrameTimeMilliseconds",
|
|
774
|
+
value.firstFrameTimeMilliseconds);
|
|
775
|
+
statistics.setProperty(
|
|
776
|
+
runtime,
|
|
777
|
+
"lastFrameTimeMilliseconds",
|
|
778
|
+
value.lastFrameTimeMilliseconds);
|
|
779
|
+
statistics.setProperty(
|
|
780
|
+
runtime,
|
|
781
|
+
"minimumFrameIntervalMilliseconds",
|
|
782
|
+
value.minimumFrameIntervalMilliseconds);
|
|
783
|
+
statistics.setProperty(
|
|
784
|
+
runtime,
|
|
785
|
+
"maximumFrameIntervalMilliseconds",
|
|
786
|
+
value.maximumFrameIntervalMilliseconds);
|
|
787
|
+
statistics.setProperty(
|
|
788
|
+
runtime,
|
|
789
|
+
"meanFrameIntervalMilliseconds",
|
|
790
|
+
value.meanFrameIntervalMilliseconds);
|
|
791
|
+
statistics.setProperty(
|
|
792
|
+
runtime,
|
|
793
|
+
"p50FrameIntervalMilliseconds",
|
|
794
|
+
value.p50FrameIntervalMilliseconds);
|
|
795
|
+
statistics.setProperty(
|
|
796
|
+
runtime,
|
|
797
|
+
"p95FrameIntervalMilliseconds",
|
|
798
|
+
value.p95FrameIntervalMilliseconds);
|
|
799
|
+
statistics.setProperty(
|
|
800
|
+
runtime,
|
|
801
|
+
"p99FrameIntervalMilliseconds",
|
|
802
|
+
value.p99FrameIntervalMilliseconds);
|
|
803
|
+
return statistics;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
std::atomic<int64_t> nextUIWorkletHandle{1};
|
|
807
|
+
std::atomic<int64_t> nextUIWorkletFrameToken{1};
|
|
808
|
+
std::atomic<int64_t> nextUIWorkletPanToken{1};
|
|
809
|
+
std::mutex uiWorkletFrameMutex;
|
|
810
|
+
std::unordered_map<int64_t, std::shared_ptr<AndroidUIWorkletState>>
|
|
811
|
+
uiWorkletFrames;
|
|
812
|
+
std::mutex uiWorkletPanMutex;
|
|
813
|
+
std::unordered_map<int64_t, std::shared_ptr<AndroidUIWorkletState>>
|
|
814
|
+
uiWorkletPans;
|
|
815
|
+
|
|
816
|
+
void requestJavaUIWorkletFrame(int64_t token);
|
|
817
|
+
void discardUIWorkletFrame(int64_t token) noexcept;
|
|
818
|
+
bool attachJavaUIWorkletPan(int64_t token, facebook::react::Tag targetTag);
|
|
819
|
+
void detachJavaUIWorkletPan(int64_t token) noexcept;
|
|
820
|
+
bool applyJavaUIWorkletFrame(
|
|
821
|
+
int64_t handle,
|
|
822
|
+
facebook::react::Tag targetTag,
|
|
823
|
+
int64_t sequence,
|
|
824
|
+
int64_t frameTimeNanoseconds,
|
|
825
|
+
const std::array<double, AndroidViewOutputCount> &values);
|
|
826
|
+
|
|
827
|
+
int64_t retainUIWorkletPan(
|
|
828
|
+
const std::shared_ptr<AndroidUIWorkletState> &state) {
|
|
829
|
+
const auto token =
|
|
830
|
+
nextUIWorkletPanToken.fetch_add(1, std::memory_order_relaxed);
|
|
831
|
+
if (token <= 0 || token > static_cast<int64_t>(MaximumSafeInteger)) {
|
|
832
|
+
throw std::runtime_error(
|
|
833
|
+
"The Android UI worklet pan token space is exhausted.");
|
|
834
|
+
}
|
|
835
|
+
std::lock_guard lock(uiWorkletPanMutex);
|
|
836
|
+
if (uiWorkletPans.size() >= MaximumActiveUIWorklets) {
|
|
837
|
+
throw std::runtime_error(
|
|
838
|
+
"The Android UI worklet pan registry is full.");
|
|
839
|
+
}
|
|
840
|
+
uiWorkletPans.emplace(token, state);
|
|
841
|
+
return token;
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
void discardUIWorkletPan(int64_t token) noexcept {
|
|
845
|
+
std::lock_guard lock(uiWorkletPanMutex);
|
|
846
|
+
uiWorkletPans.erase(token);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
void deactivateAndroidUIWorklet(
|
|
850
|
+
const std::shared_ptr<AndroidUIWorkletState> &state) {
|
|
851
|
+
int64_t panToken = 0;
|
|
852
|
+
std::optional<int64_t> frameToken;
|
|
853
|
+
{
|
|
854
|
+
std::lock_guard lock(state->mutex);
|
|
855
|
+
state->active = false;
|
|
856
|
+
state->framePending = false;
|
|
857
|
+
frameToken = state->frameToken;
|
|
858
|
+
state->frameToken.reset();
|
|
859
|
+
if (state->pan.has_value()) {
|
|
860
|
+
state->pan->active = false;
|
|
861
|
+
panToken = state->pan->token;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
if (frameToken.has_value()) discardUIWorkletFrame(*frameToken);
|
|
865
|
+
if (panToken > 0) {
|
|
866
|
+
detachJavaUIWorkletPan(panToken);
|
|
867
|
+
discardUIWorkletPan(panToken);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
int64_t retainUIWorkletFrame(
|
|
872
|
+
const std::shared_ptr<AndroidUIWorkletState> &state) {
|
|
873
|
+
const auto token =
|
|
874
|
+
nextUIWorkletFrameToken.fetch_add(1, std::memory_order_relaxed);
|
|
875
|
+
if (token <= 0 || token > static_cast<int64_t>(MaximumSafeInteger)) {
|
|
876
|
+
throw std::runtime_error(
|
|
877
|
+
"The Android UI worklet frame token space is exhausted.");
|
|
878
|
+
}
|
|
879
|
+
std::lock_guard lock(uiWorkletFrameMutex);
|
|
880
|
+
if (uiWorkletFrames.size() >= MaximumActiveUIWorklets) {
|
|
881
|
+
throw std::runtime_error(
|
|
882
|
+
"The Android UI worklet frame queue is full.");
|
|
883
|
+
}
|
|
884
|
+
uiWorkletFrames.emplace(token, state);
|
|
885
|
+
return token;
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
void discardUIWorkletFrame(int64_t token) noexcept {
|
|
889
|
+
std::lock_guard lock(uiWorkletFrameMutex);
|
|
890
|
+
uiWorkletFrames.erase(token);
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
void scheduleUIWorkletFrame(
|
|
894
|
+
const std::shared_ptr<AndroidUIWorkletState> &state) {
|
|
895
|
+
int64_t token;
|
|
896
|
+
{
|
|
897
|
+
std::lock_guard lock(state->mutex);
|
|
898
|
+
if (!state->active || state->framePending) return;
|
|
899
|
+
state->framePending = true;
|
|
900
|
+
try {
|
|
901
|
+
token = retainUIWorkletFrame(state);
|
|
902
|
+
state->frameToken = token;
|
|
903
|
+
} catch (...) {
|
|
904
|
+
state->framePending = false;
|
|
905
|
+
state->frameToken.reset();
|
|
906
|
+
throw;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
try {
|
|
910
|
+
requestJavaUIWorkletFrame(token);
|
|
911
|
+
} catch (...) {
|
|
912
|
+
discardUIWorkletFrame(token);
|
|
913
|
+
std::lock_guard lock(state->mutex);
|
|
914
|
+
state->framePending = false;
|
|
915
|
+
state->frameToken.reset();
|
|
916
|
+
throw;
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
class AndroidUIWorkletRegistry final {
|
|
921
|
+
public:
|
|
922
|
+
int64_t install(
|
|
923
|
+
int64_t targetNode,
|
|
924
|
+
facebook::react::Tag targetTag,
|
|
925
|
+
solid_native::worklets::UIWorkletGraph graph) {
|
|
926
|
+
std::vector<AndroidViewOutput> channels;
|
|
927
|
+
channels.reserve(graph.outputNames().size());
|
|
928
|
+
for (const auto &name : graph.outputNames()) {
|
|
929
|
+
channels.push_back(requireAndroidViewOutput(name));
|
|
930
|
+
}
|
|
931
|
+
graph.evaluate(graph.initialInputs());
|
|
932
|
+
const auto handle =
|
|
933
|
+
nextUIWorkletHandle.fetch_add(1, std::memory_order_relaxed);
|
|
934
|
+
if (handle <= 0 || handle > static_cast<int64_t>(MaximumSafeInteger)) {
|
|
935
|
+
throw std::runtime_error(
|
|
936
|
+
"The Android UI worklet handle space is exhausted.");
|
|
937
|
+
}
|
|
938
|
+
auto state = std::make_shared<AndroidUIWorkletState>(
|
|
939
|
+
handle,
|
|
940
|
+
targetNode,
|
|
941
|
+
targetTag,
|
|
942
|
+
std::move(graph),
|
|
943
|
+
std::move(channels));
|
|
944
|
+
{
|
|
945
|
+
std::lock_guard lock(mutex_);
|
|
946
|
+
if (active_.size() >= MaximumActiveUIWorklets) {
|
|
947
|
+
throw std::runtime_error(
|
|
948
|
+
"The Android UI worklet registry is full.");
|
|
949
|
+
}
|
|
950
|
+
active_.emplace(handle, state);
|
|
951
|
+
issued_.insert(handle);
|
|
952
|
+
}
|
|
953
|
+
try {
|
|
954
|
+
scheduleUIWorkletFrame(state);
|
|
955
|
+
} catch (...) {
|
|
956
|
+
destroy(handle);
|
|
957
|
+
throw;
|
|
958
|
+
}
|
|
959
|
+
return handle;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
void update(
|
|
963
|
+
int64_t handle,
|
|
964
|
+
std::vector<double> inputs,
|
|
965
|
+
double timestamp) {
|
|
966
|
+
auto state = requireActive(handle);
|
|
967
|
+
{
|
|
968
|
+
std::lock_guard lock(state->mutex);
|
|
969
|
+
if (!state->active) {
|
|
970
|
+
throw std::invalid_argument(
|
|
971
|
+
"The Android UI worklet graph is no longer active.");
|
|
972
|
+
}
|
|
973
|
+
if (state->pan.has_value()) {
|
|
974
|
+
throw std::invalid_argument(
|
|
975
|
+
"The Android UI worklet input vector is owned by its native pan gesture.");
|
|
976
|
+
}
|
|
977
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
978
|
+
throw std::invalid_argument(
|
|
979
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
980
|
+
}
|
|
981
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
982
|
+
throw std::invalid_argument(
|
|
983
|
+
"UI worklet input timestamps must be monotonic.");
|
|
984
|
+
}
|
|
985
|
+
// Validate the entire candidate frame before publishing any input.
|
|
986
|
+
state->graph.evaluate(inputs);
|
|
987
|
+
state->inputs = std::move(inputs);
|
|
988
|
+
state->timestamp = timestamp;
|
|
989
|
+
state->hasTimestamp = true;
|
|
990
|
+
state->timing.reset();
|
|
991
|
+
state->timingProgress = 0;
|
|
992
|
+
state->timingFrameStatistics.reset();
|
|
993
|
+
state->spring.reset();
|
|
994
|
+
state->springPosition.reset();
|
|
995
|
+
state->springVelocity.reset();
|
|
996
|
+
state->springFrameStatistics.reset();
|
|
997
|
+
state->decay.reset();
|
|
998
|
+
state->decayElapsedMilliseconds.reset();
|
|
999
|
+
state->decaySpeed.reset();
|
|
1000
|
+
state->decayFrameStatistics.reset();
|
|
1001
|
+
}
|
|
1002
|
+
scheduleUIWorkletFrame(state);
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
void animate(
|
|
1006
|
+
int64_t handle,
|
|
1007
|
+
std::vector<double> inputs,
|
|
1008
|
+
double timestamp,
|
|
1009
|
+
solid_native::worklets::UIWorkletTiming timing) {
|
|
1010
|
+
auto state = requireActive(handle);
|
|
1011
|
+
{
|
|
1012
|
+
std::lock_guard lock(state->mutex);
|
|
1013
|
+
if (!state->active) {
|
|
1014
|
+
throw std::invalid_argument(
|
|
1015
|
+
"The Android UI worklet graph is no longer active.");
|
|
1016
|
+
}
|
|
1017
|
+
if (state->pan.has_value()) {
|
|
1018
|
+
throw std::invalid_argument(
|
|
1019
|
+
"The Android UI worklet input vector is owned by its native pan gesture.");
|
|
1020
|
+
}
|
|
1021
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
1022
|
+
throw std::invalid_argument(
|
|
1023
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
1024
|
+
}
|
|
1025
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
1026
|
+
throw std::invalid_argument(
|
|
1027
|
+
"UI worklet input timestamps must be monotonic.");
|
|
1028
|
+
}
|
|
1029
|
+
// Validate the complete target before replacing an active timing.
|
|
1030
|
+
state->graph.evaluate(inputs);
|
|
1031
|
+
std::vector<solid_native::worklets::UIWorkletTimingKeyframe> keyframes;
|
|
1032
|
+
keyframes.push_back({.inputs = std::move(inputs), .timing = timing});
|
|
1033
|
+
state->timing = AndroidUIWorkletTimingState{
|
|
1034
|
+
.from = state->inputs,
|
|
1035
|
+
.definition = {
|
|
1036
|
+
.keyframes = std::move(keyframes),
|
|
1037
|
+
.totalDurationMilliseconds = timing.durationMilliseconds,
|
|
1038
|
+
},
|
|
1039
|
+
};
|
|
1040
|
+
state->timestamp = timestamp;
|
|
1041
|
+
state->hasTimestamp = true;
|
|
1042
|
+
state->timingProgress = 0;
|
|
1043
|
+
state->timingFrameStatistics.reset();
|
|
1044
|
+
state->spring.reset();
|
|
1045
|
+
state->springPosition.reset();
|
|
1046
|
+
state->springVelocity.reset();
|
|
1047
|
+
state->springFrameStatistics.reset();
|
|
1048
|
+
state->decay.reset();
|
|
1049
|
+
state->decayElapsedMilliseconds.reset();
|
|
1050
|
+
state->decaySpeed.reset();
|
|
1051
|
+
state->decayFrameStatistics.reset();
|
|
1052
|
+
}
|
|
1053
|
+
scheduleUIWorkletFrame(state);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
void animateKeyframes(
|
|
1057
|
+
int64_t handle,
|
|
1058
|
+
solid_native::worklets::UIWorkletTimingSequence keyframes,
|
|
1059
|
+
double timestamp) {
|
|
1060
|
+
auto state = requireActive(handle);
|
|
1061
|
+
{
|
|
1062
|
+
std::lock_guard lock(state->mutex);
|
|
1063
|
+
if (!state->active) {
|
|
1064
|
+
throw std::invalid_argument(
|
|
1065
|
+
"The Android UI worklet graph is no longer active.");
|
|
1066
|
+
}
|
|
1067
|
+
if (state->pan.has_value()) {
|
|
1068
|
+
throw std::invalid_argument(
|
|
1069
|
+
"The Android UI worklet input vector is owned by its native pan gesture.");
|
|
1070
|
+
}
|
|
1071
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
1072
|
+
throw std::invalid_argument(
|
|
1073
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
1074
|
+
}
|
|
1075
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
1076
|
+
throw std::invalid_argument(
|
|
1077
|
+
"UI worklet input timestamps must be monotonic.");
|
|
1078
|
+
}
|
|
1079
|
+
// Reject any invalid terminal before replacing the active driver.
|
|
1080
|
+
for (const auto &keyframe : keyframes.keyframes) {
|
|
1081
|
+
state->graph.evaluate(keyframe.inputs);
|
|
1082
|
+
}
|
|
1083
|
+
state->timing = AndroidUIWorkletTimingState{
|
|
1084
|
+
.from = state->inputs,
|
|
1085
|
+
.definition = std::move(keyframes),
|
|
1086
|
+
};
|
|
1087
|
+
state->timestamp = timestamp;
|
|
1088
|
+
state->hasTimestamp = true;
|
|
1089
|
+
state->timingProgress = 0;
|
|
1090
|
+
state->timingFrameStatistics.reset();
|
|
1091
|
+
state->spring.reset();
|
|
1092
|
+
state->springPosition.reset();
|
|
1093
|
+
state->springVelocity.reset();
|
|
1094
|
+
state->springFrameStatistics.reset();
|
|
1095
|
+
state->decay.reset();
|
|
1096
|
+
state->decayElapsedMilliseconds.reset();
|
|
1097
|
+
state->decaySpeed.reset();
|
|
1098
|
+
state->decayFrameStatistics.reset();
|
|
1099
|
+
}
|
|
1100
|
+
scheduleUIWorkletFrame(state);
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
void spring(
|
|
1104
|
+
int64_t handle,
|
|
1105
|
+
std::vector<double> inputs,
|
|
1106
|
+
double timestamp,
|
|
1107
|
+
solid_native::worklets::UIWorkletSpring spring) {
|
|
1108
|
+
auto state = requireActive(handle);
|
|
1109
|
+
{
|
|
1110
|
+
std::lock_guard lock(state->mutex);
|
|
1111
|
+
if (!state->active) {
|
|
1112
|
+
throw std::invalid_argument(
|
|
1113
|
+
"The Android UI worklet graph is no longer active.");
|
|
1114
|
+
}
|
|
1115
|
+
if (state->pan.has_value()) {
|
|
1116
|
+
throw std::invalid_argument(
|
|
1117
|
+
"The Android UI worklet input vector is owned by its native pan gesture.");
|
|
1118
|
+
}
|
|
1119
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
1120
|
+
throw std::invalid_argument(
|
|
1121
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
1122
|
+
}
|
|
1123
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
1124
|
+
throw std::invalid_argument(
|
|
1125
|
+
"UI worklet input timestamps must be monotonic.");
|
|
1126
|
+
}
|
|
1127
|
+
state->graph.evaluate(inputs);
|
|
1128
|
+
state->timing.reset();
|
|
1129
|
+
state->timingProgress = 0;
|
|
1130
|
+
state->timingFrameStatistics.reset();
|
|
1131
|
+
state->spring = AndroidUIWorkletSpringState{
|
|
1132
|
+
.from = state->inputs,
|
|
1133
|
+
.to = std::move(inputs),
|
|
1134
|
+
.definition = spring,
|
|
1135
|
+
};
|
|
1136
|
+
state->springPosition = 0;
|
|
1137
|
+
state->springVelocity = spring.initialVelocity;
|
|
1138
|
+
state->springFrameStatistics.reset();
|
|
1139
|
+
state->decay.reset();
|
|
1140
|
+
state->decayElapsedMilliseconds.reset();
|
|
1141
|
+
state->decaySpeed.reset();
|
|
1142
|
+
state->decayFrameStatistics.reset();
|
|
1143
|
+
state->timestamp = timestamp;
|
|
1144
|
+
state->hasTimestamp = true;
|
|
1145
|
+
}
|
|
1146
|
+
scheduleUIWorkletFrame(state);
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
void decay(
|
|
1150
|
+
int64_t handle,
|
|
1151
|
+
std::vector<double> velocities,
|
|
1152
|
+
double timestamp,
|
|
1153
|
+
solid_native::worklets::UIWorkletDecay decay) {
|
|
1154
|
+
auto state = requireActive(handle);
|
|
1155
|
+
{
|
|
1156
|
+
std::lock_guard lock(state->mutex);
|
|
1157
|
+
if (!state->active) {
|
|
1158
|
+
throw std::invalid_argument(
|
|
1159
|
+
"The Android UI worklet graph is no longer active.");
|
|
1160
|
+
}
|
|
1161
|
+
if (state->pan.has_value()) {
|
|
1162
|
+
throw std::invalid_argument(
|
|
1163
|
+
"The Android UI worklet input vector is owned by its native pan gesture.");
|
|
1164
|
+
}
|
|
1165
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
1166
|
+
throw std::invalid_argument(
|
|
1167
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
1168
|
+
}
|
|
1169
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
1170
|
+
throw std::invalid_argument(
|
|
1171
|
+
"UI worklet input timestamps must be monotonic.");
|
|
1172
|
+
}
|
|
1173
|
+
if (velocities.size() != state->inputs.size()) {
|
|
1174
|
+
throw std::invalid_argument(
|
|
1175
|
+
"UI worklet decay velocities must match the input vector.");
|
|
1176
|
+
}
|
|
1177
|
+
double initialSpeed = 0;
|
|
1178
|
+
for (const auto velocity : velocities) {
|
|
1179
|
+
if (!std::isfinite(velocity) ||
|
|
1180
|
+
std::abs(velocity) >
|
|
1181
|
+
solid_native::worklets::MaximumUIWorkletDecayVelocity) {
|
|
1182
|
+
throw std::invalid_argument(
|
|
1183
|
+
"UI worklet decay velocity is outside its bounded range.");
|
|
1184
|
+
}
|
|
1185
|
+
initialSpeed = std::max(initialSpeed, std::abs(velocity));
|
|
1186
|
+
}
|
|
1187
|
+
const auto terminal = decay.evaluate(
|
|
1188
|
+
initialSpeed, decay.maximumDurationMilliseconds);
|
|
1189
|
+
auto terminalInputs = state->inputs;
|
|
1190
|
+
for (size_t index = 0; index < terminalInputs.size(); ++index) {
|
|
1191
|
+
terminalInputs.at(index) +=
|
|
1192
|
+
velocities.at(index) * terminal.displacementFactorSeconds;
|
|
1193
|
+
}
|
|
1194
|
+
state->graph.evaluate(terminalInputs);
|
|
1195
|
+
state->timing.reset();
|
|
1196
|
+
state->timingProgress = 0;
|
|
1197
|
+
state->timingFrameStatistics.reset();
|
|
1198
|
+
state->spring.reset();
|
|
1199
|
+
state->springPosition.reset();
|
|
1200
|
+
state->springVelocity.reset();
|
|
1201
|
+
state->springFrameStatistics.reset();
|
|
1202
|
+
state->decay = AndroidUIWorkletDecayState{
|
|
1203
|
+
.from = state->inputs,
|
|
1204
|
+
.velocities = std::move(velocities),
|
|
1205
|
+
.initialSpeed = initialSpeed,
|
|
1206
|
+
.definition = decay,
|
|
1207
|
+
};
|
|
1208
|
+
state->decayElapsedMilliseconds = 0;
|
|
1209
|
+
state->decaySpeed = initialSpeed;
|
|
1210
|
+
state->decayFrameStatistics.reset();
|
|
1211
|
+
state->timestamp = timestamp;
|
|
1212
|
+
state->hasTimestamp = true;
|
|
1213
|
+
}
|
|
1214
|
+
scheduleUIWorkletFrame(state);
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
std::vector<double> cancelAnimation(int64_t handle, double timestamp) {
|
|
1218
|
+
auto state = requireActive(handle);
|
|
1219
|
+
std::optional<int64_t> frameToken;
|
|
1220
|
+
std::vector<double> inputs;
|
|
1221
|
+
{
|
|
1222
|
+
std::lock_guard lock(state->mutex);
|
|
1223
|
+
if (!state->active) {
|
|
1224
|
+
throw std::invalid_argument(
|
|
1225
|
+
"The Android UI worklet graph is no longer active.");
|
|
1226
|
+
}
|
|
1227
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
1228
|
+
throw std::invalid_argument(
|
|
1229
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
1230
|
+
}
|
|
1231
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
1232
|
+
throw std::invalid_argument(
|
|
1233
|
+
"UI worklet input timestamps must be monotonic.");
|
|
1234
|
+
}
|
|
1235
|
+
const auto hadDriver = state->timing.has_value() ||
|
|
1236
|
+
state->spring.has_value() || state->decay.has_value();
|
|
1237
|
+
state->timing.reset();
|
|
1238
|
+
state->spring.reset();
|
|
1239
|
+
if (state->springVelocity.has_value()) state->springVelocity = 0;
|
|
1240
|
+
state->decay.reset();
|
|
1241
|
+
if (state->decaySpeed.has_value()) state->decaySpeed = 0;
|
|
1242
|
+
state->timestamp = timestamp;
|
|
1243
|
+
state->hasTimestamp = true;
|
|
1244
|
+
if (hadDriver) {
|
|
1245
|
+
state->framePending = false;
|
|
1246
|
+
frameToken = state->frameToken;
|
|
1247
|
+
state->frameToken.reset();
|
|
1248
|
+
}
|
|
1249
|
+
inputs = state->inputs;
|
|
1250
|
+
}
|
|
1251
|
+
if (frameToken.has_value()) discardUIWorkletFrame(*frameToken);
|
|
1252
|
+
return inputs;
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
std::shared_ptr<AndroidUIWorkletState> attachPan(
|
|
1256
|
+
int64_t handle,
|
|
1257
|
+
solid_native::worklets::UIWorkletPanGesture pan) {
|
|
1258
|
+
auto state = requireActive(handle);
|
|
1259
|
+
const auto token = retainUIWorkletPan(state);
|
|
1260
|
+
try {
|
|
1261
|
+
std::lock_guard lock(state->mutex);
|
|
1262
|
+
if (!state->active) {
|
|
1263
|
+
throw std::invalid_argument(
|
|
1264
|
+
"The Android UI worklet graph is no longer active.");
|
|
1265
|
+
}
|
|
1266
|
+
if (state->pan.has_value()) {
|
|
1267
|
+
throw std::invalid_argument(
|
|
1268
|
+
"The Android UI worklet graph already owns a native pan gesture.");
|
|
1269
|
+
}
|
|
1270
|
+
state->pan = AndroidUIWorkletPanState{
|
|
1271
|
+
.definition = std::move(pan),
|
|
1272
|
+
.token = token,
|
|
1273
|
+
};
|
|
1274
|
+
} catch (...) {
|
|
1275
|
+
discardUIWorkletPan(token);
|
|
1276
|
+
throw;
|
|
1277
|
+
}
|
|
1278
|
+
return state;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
void detachPan(int64_t handle) {
|
|
1282
|
+
auto state = requireActive(handle);
|
|
1283
|
+
int64_t token = 0;
|
|
1284
|
+
{
|
|
1285
|
+
std::lock_guard lock(state->mutex);
|
|
1286
|
+
if (state->pan.has_value()) {
|
|
1287
|
+
state->pan->active = false;
|
|
1288
|
+
token = state->pan->token;
|
|
1289
|
+
}
|
|
1290
|
+
state->pan.reset();
|
|
1291
|
+
}
|
|
1292
|
+
if (token > 0) {
|
|
1293
|
+
detachJavaUIWorkletPan(token);
|
|
1294
|
+
discardUIWorkletPan(token);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
std::vector<double> releasePan(int64_t handle, double timestamp) {
|
|
1299
|
+
auto state = requireActive(handle);
|
|
1300
|
+
int64_t panToken = 0;
|
|
1301
|
+
std::optional<int64_t> frameToken;
|
|
1302
|
+
std::vector<double> inputs;
|
|
1303
|
+
{
|
|
1304
|
+
std::lock_guard lock(state->mutex);
|
|
1305
|
+
if (!state->active) {
|
|
1306
|
+
throw std::invalid_argument(
|
|
1307
|
+
"The Android UI worklet graph is no longer active.");
|
|
1308
|
+
}
|
|
1309
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
1310
|
+
throw std::invalid_argument(
|
|
1311
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
1312
|
+
}
|
|
1313
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
1314
|
+
throw std::invalid_argument(
|
|
1315
|
+
"UI worklet input timestamps must be monotonic.");
|
|
1316
|
+
}
|
|
1317
|
+
if (!state->pan.has_value()) {
|
|
1318
|
+
throw std::invalid_argument(
|
|
1319
|
+
"The Android UI worklet graph does not own a native pan gesture.");
|
|
1320
|
+
}
|
|
1321
|
+
const auto hadDriver = state->timing.has_value() ||
|
|
1322
|
+
state->spring.has_value() || state->decay.has_value();
|
|
1323
|
+
state->pan->active = false;
|
|
1324
|
+
panToken = state->pan->token;
|
|
1325
|
+
state->pan.reset();
|
|
1326
|
+
state->timing.reset();
|
|
1327
|
+
state->spring.reset();
|
|
1328
|
+
if (state->springVelocity.has_value()) state->springVelocity = 0;
|
|
1329
|
+
state->decay.reset();
|
|
1330
|
+
if (state->decaySpeed.has_value()) state->decaySpeed = 0;
|
|
1331
|
+
state->timestamp = timestamp;
|
|
1332
|
+
state->hasTimestamp = true;
|
|
1333
|
+
if (hadDriver) {
|
|
1334
|
+
state->framePending = false;
|
|
1335
|
+
frameToken = state->frameToken;
|
|
1336
|
+
state->frameToken.reset();
|
|
1337
|
+
}
|
|
1338
|
+
inputs = state->inputs;
|
|
1339
|
+
}
|
|
1340
|
+
if (frameToken.has_value()) discardUIWorkletFrame(*frameToken);
|
|
1341
|
+
detachJavaUIWorkletPan(panToken);
|
|
1342
|
+
discardUIWorkletPan(panToken);
|
|
1343
|
+
return inputs;
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
void destroy(int64_t handle) {
|
|
1347
|
+
std::shared_ptr<AndroidUIWorkletState> state;
|
|
1348
|
+
{
|
|
1349
|
+
std::lock_guard lock(mutex_);
|
|
1350
|
+
if (issued_.erase(handle) == 0) {
|
|
1351
|
+
throw std::invalid_argument(
|
|
1352
|
+
"Unknown Android UI worklet graph handle " +
|
|
1353
|
+
std::to_string(handle) + ".");
|
|
1354
|
+
}
|
|
1355
|
+
const auto entry = active_.find(handle);
|
|
1356
|
+
if (entry != active_.end()) {
|
|
1357
|
+
state = std::move(entry->second);
|
|
1358
|
+
active_.erase(entry);
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
if (state) deactivateAndroidUIWorklet(state);
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
void cancelNode(int64_t targetNode) {
|
|
1365
|
+
std::vector<std::shared_ptr<AndroidUIWorkletState>> cancelled;
|
|
1366
|
+
{
|
|
1367
|
+
std::lock_guard lock(mutex_);
|
|
1368
|
+
for (auto iterator = active_.begin(); iterator != active_.end();) {
|
|
1369
|
+
if (iterator->second->targetNode != targetNode) {
|
|
1370
|
+
++iterator;
|
|
1371
|
+
continue;
|
|
1372
|
+
}
|
|
1373
|
+
cancelled.push_back(iterator->second);
|
|
1374
|
+
iterator = active_.erase(iterator);
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
for (const auto &state : cancelled) {
|
|
1378
|
+
deactivateAndroidUIWorklet(state);
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
void cancelAll() {
|
|
1383
|
+
std::vector<std::shared_ptr<AndroidUIWorkletState>> cancelled;
|
|
1384
|
+
{
|
|
1385
|
+
std::lock_guard lock(mutex_);
|
|
1386
|
+
for (const auto &[_, state] : active_) cancelled.push_back(state);
|
|
1387
|
+
active_.clear();
|
|
1388
|
+
issued_.clear();
|
|
1389
|
+
}
|
|
1390
|
+
for (const auto &state : cancelled) {
|
|
1391
|
+
deactivateAndroidUIWorklet(state);
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
size_t activeCount() const {
|
|
1396
|
+
std::lock_guard lock(mutex_);
|
|
1397
|
+
return active_.size();
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
std::pair<size_t, size_t> activityCounts() const {
|
|
1401
|
+
std::lock_guard lock(mutex_);
|
|
1402
|
+
size_t pendingFrameCount = 0;
|
|
1403
|
+
for (const auto &[_, state] : active_) {
|
|
1404
|
+
std::lock_guard lock(state->mutex);
|
|
1405
|
+
if (state->active && state->framePending) pendingFrameCount++;
|
|
1406
|
+
}
|
|
1407
|
+
return {active_.size(), pendingFrameCount};
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
std::shared_ptr<AndroidUIWorkletState> requireActive(int64_t handle) const {
|
|
1411
|
+
std::lock_guard lock(mutex_);
|
|
1412
|
+
const auto entry = active_.find(handle);
|
|
1413
|
+
if (entry == active_.end()) {
|
|
1414
|
+
throw std::invalid_argument(
|
|
1415
|
+
"Unknown or cancelled Android UI worklet graph handle " +
|
|
1416
|
+
std::to_string(handle) + ".");
|
|
1417
|
+
}
|
|
1418
|
+
return entry->second;
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
private:
|
|
1422
|
+
mutable std::mutex mutex_;
|
|
1423
|
+
std::unordered_map<int64_t, std::shared_ptr<AndroidUIWorkletState>> active_;
|
|
1424
|
+
std::unordered_set<int64_t> issued_;
|
|
1425
|
+
};
|
|
1426
|
+
|
|
1427
|
+
struct RetiredInstances final {
|
|
1428
|
+
int64_t fabricRevision;
|
|
1429
|
+
std::vector<int64_t> handles;
|
|
1430
|
+
};
|
|
1431
|
+
|
|
1432
|
+
class RuntimeState final : public facebook::jsi::NativeState {
|
|
1433
|
+
public:
|
|
1434
|
+
RuntimeState(
|
|
1435
|
+
Runtime &runtime,
|
|
1436
|
+
const Object &storage,
|
|
1437
|
+
std::shared_ptr<EventRoutingState> routing)
|
|
1438
|
+
: storage_(runtime, storage), routing_(std::move(routing)) {}
|
|
1439
|
+
|
|
1440
|
+
~RuntimeState() override {
|
|
1441
|
+
deactivateNativeWork();
|
|
1442
|
+
if (runtimeRetirementHandler_) runtimeRetirementHandler_();
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
void setRuntimeRetirementHandler(std::function<void()> handler) {
|
|
1446
|
+
runtimeRetirementHandler_ = std::move(handler);
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
Object storage(Runtime &runtime) const {
|
|
1450
|
+
auto value = storage_.lock(runtime);
|
|
1451
|
+
if (!value.isObject()) {
|
|
1452
|
+
throw std::runtime_error(
|
|
1453
|
+
"The Solid Native runtime storage was collected unexpectedly.");
|
|
1454
|
+
}
|
|
1455
|
+
return value.getObject(runtime);
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
Object instances(Runtime &runtime) const {
|
|
1459
|
+
return storage(runtime).getPropertyAsObject(runtime, "instances");
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
Object resources(Runtime &runtime) const {
|
|
1463
|
+
return storage(runtime).getPropertyAsObject(runtime, "resources");
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
int64_t retainResource(
|
|
1467
|
+
Runtime &runtime,
|
|
1468
|
+
const std::string &kind,
|
|
1469
|
+
const Value &value) {
|
|
1470
|
+
if (resourceKinds_.size() >= MaximumRetainedResources) {
|
|
1471
|
+
throw std::runtime_error(
|
|
1472
|
+
"The Solid Native runtime resource registry is full.");
|
|
1473
|
+
}
|
|
1474
|
+
if (nextResourceHandle_ > 9007199254740991LL) {
|
|
1475
|
+
throw std::runtime_error(
|
|
1476
|
+
"The Solid Native runtime exhausted native resource handles.");
|
|
1477
|
+
}
|
|
1478
|
+
const auto handle = nextResourceHandle_++;
|
|
1479
|
+
const auto key = std::to_string(handle);
|
|
1480
|
+
resources(runtime).setProperty(
|
|
1481
|
+
runtime, key.c_str(), Value(runtime, value));
|
|
1482
|
+
resourceKinds_.emplace(handle, kind);
|
|
1483
|
+
return handle;
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
Value resolveResource(
|
|
1487
|
+
Runtime &runtime,
|
|
1488
|
+
int64_t handle,
|
|
1489
|
+
const std::string &kind) const {
|
|
1490
|
+
const auto resource = resourceKinds_.find(handle);
|
|
1491
|
+
if (resource == resourceKinds_.end()) {
|
|
1492
|
+
throw std::invalid_argument(
|
|
1493
|
+
"Native resource " + std::to_string(handle) +
|
|
1494
|
+
" is not retained.");
|
|
1495
|
+
}
|
|
1496
|
+
if (resource->second != kind) {
|
|
1497
|
+
throw std::invalid_argument(
|
|
1498
|
+
"Native resource " + std::to_string(handle) +
|
|
1499
|
+
" has kind " + resource->second + ", not " + kind + ".");
|
|
1500
|
+
}
|
|
1501
|
+
const auto key = std::to_string(handle);
|
|
1502
|
+
return resources(runtime).getProperty(runtime, key.c_str());
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
void releaseResource(Runtime &runtime, int64_t handle) {
|
|
1506
|
+
if (resourceKinds_.erase(handle) == 0) {
|
|
1507
|
+
throw std::invalid_argument(
|
|
1508
|
+
"Native resource " + std::to_string(handle) +
|
|
1509
|
+
" is not retained.");
|
|
1510
|
+
}
|
|
1511
|
+
const auto key = std::to_string(handle);
|
|
1512
|
+
resources(runtime).deleteProperty(runtime, key.c_str());
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
void reclaimAllResources(Runtime &runtime) {
|
|
1516
|
+
const auto handles = resourceKinds_;
|
|
1517
|
+
for (const auto &[handle, _] : handles) {
|
|
1518
|
+
const auto key = std::to_string(handle);
|
|
1519
|
+
resources(runtime).deleteProperty(runtime, key.c_str());
|
|
1520
|
+
}
|
|
1521
|
+
resourceKinds_.clear();
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
size_t retainedResourceCount() const {
|
|
1525
|
+
return resourceKinds_.size();
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
bool hasInstance(Runtime &runtime, int64_t handle) const {
|
|
1529
|
+
const auto key = std::to_string(handle);
|
|
1530
|
+
return instances(runtime).hasProperty(runtime, key.c_str());
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
void retainInstance(Runtime &runtime, int64_t handle, Value &&value) {
|
|
1534
|
+
const auto key = std::to_string(handle);
|
|
1535
|
+
instances(runtime).setProperty(runtime, key.c_str(), std::move(value));
|
|
1536
|
+
retainedHandles_.insert(handle);
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
void releaseInstance(Runtime &runtime, int64_t handle) {
|
|
1540
|
+
if (retainedHandles_.erase(handle) == 0) return;
|
|
1541
|
+
uiWorklets_.cancelNode(handle);
|
|
1542
|
+
const auto key = std::to_string(handle);
|
|
1543
|
+
instances(runtime).deleteProperty(runtime, key.c_str());
|
|
1544
|
+
routing_->reclaimNode(handle);
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
size_t retainedNodeCount() const {
|
|
1548
|
+
return retainedHandles_.size();
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
int64_t installUIWorklet(
|
|
1552
|
+
Runtime &runtime,
|
|
1553
|
+
int64_t targetNode,
|
|
1554
|
+
const Value &graphValue) {
|
|
1555
|
+
if (!hasInstance(runtime, targetNode)) {
|
|
1556
|
+
throw std::invalid_argument(
|
|
1557
|
+
"A native UI worklet target must be a live Solid Native node.");
|
|
1558
|
+
}
|
|
1559
|
+
const auto targetTag = routing_->tagForNode(targetNode);
|
|
1560
|
+
if (!targetTag.has_value()) {
|
|
1561
|
+
throw std::invalid_argument(
|
|
1562
|
+
"A native UI worklet target must have a committed Fabric identity.");
|
|
1563
|
+
}
|
|
1564
|
+
size_t valueCount = 0;
|
|
1565
|
+
std::vector<Value> ancestors;
|
|
1566
|
+
auto graph = dynamicFromJSI(
|
|
1567
|
+
runtime,
|
|
1568
|
+
graphValue,
|
|
1569
|
+
"UI worklet graph",
|
|
1570
|
+
0,
|
|
1571
|
+
valueCount,
|
|
1572
|
+
ancestors);
|
|
1573
|
+
return uiWorklets_.install(
|
|
1574
|
+
targetNode,
|
|
1575
|
+
*targetTag,
|
|
1576
|
+
solid_native::worklets::UIWorkletGraph::parse(graph));
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
void updateUIWorklet(
|
|
1580
|
+
int64_t handle,
|
|
1581
|
+
std::vector<double> inputs,
|
|
1582
|
+
double timestamp) {
|
|
1583
|
+
uiWorklets_.update(handle, std::move(inputs), timestamp);
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
void animateUIWorklet(
|
|
1587
|
+
Runtime &runtime,
|
|
1588
|
+
int64_t handle,
|
|
1589
|
+
std::vector<double> inputs,
|
|
1590
|
+
double timestamp,
|
|
1591
|
+
const Value &timingValue) {
|
|
1592
|
+
size_t valueCount = 0;
|
|
1593
|
+
std::vector<Value> ancestors;
|
|
1594
|
+
auto timing = dynamicFromJSI(
|
|
1595
|
+
runtime,
|
|
1596
|
+
timingValue,
|
|
1597
|
+
"UI worklet timing",
|
|
1598
|
+
0,
|
|
1599
|
+
valueCount,
|
|
1600
|
+
ancestors);
|
|
1601
|
+
uiWorklets_.animate(
|
|
1602
|
+
handle,
|
|
1603
|
+
std::move(inputs),
|
|
1604
|
+
timestamp,
|
|
1605
|
+
solid_native::worklets::UIWorkletTiming::parse(timing));
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
void animateUIWorkletKeyframes(
|
|
1609
|
+
Runtime &runtime,
|
|
1610
|
+
int64_t handle,
|
|
1611
|
+
const Value &keyframesValue,
|
|
1612
|
+
double timestamp) {
|
|
1613
|
+
auto state = uiWorklets_.requireActive(handle);
|
|
1614
|
+
size_t valueCount = 0;
|
|
1615
|
+
std::vector<Value> ancestors;
|
|
1616
|
+
auto keyframes = dynamicFromJSI(
|
|
1617
|
+
runtime,
|
|
1618
|
+
keyframesValue,
|
|
1619
|
+
"UI worklet timing keyframes",
|
|
1620
|
+
0,
|
|
1621
|
+
valueCount,
|
|
1622
|
+
ancestors);
|
|
1623
|
+
uiWorklets_.animateKeyframes(
|
|
1624
|
+
handle,
|
|
1625
|
+
solid_native::worklets::UIWorkletTimingSequence::parse(
|
|
1626
|
+
keyframes, state->graph.inputCount()),
|
|
1627
|
+
timestamp);
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
void springUIWorklet(
|
|
1631
|
+
Runtime &runtime,
|
|
1632
|
+
int64_t handle,
|
|
1633
|
+
std::vector<double> inputs,
|
|
1634
|
+
double timestamp,
|
|
1635
|
+
const Value &springValue) {
|
|
1636
|
+
size_t valueCount = 0;
|
|
1637
|
+
std::vector<Value> ancestors;
|
|
1638
|
+
auto spring = dynamicFromJSI(
|
|
1639
|
+
runtime,
|
|
1640
|
+
springValue,
|
|
1641
|
+
"UI worklet spring",
|
|
1642
|
+
0,
|
|
1643
|
+
valueCount,
|
|
1644
|
+
ancestors);
|
|
1645
|
+
uiWorklets_.spring(
|
|
1646
|
+
handle,
|
|
1647
|
+
std::move(inputs),
|
|
1648
|
+
timestamp,
|
|
1649
|
+
solid_native::worklets::UIWorkletSpring::parse(spring));
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
void decayUIWorklet(
|
|
1653
|
+
Runtime &runtime,
|
|
1654
|
+
int64_t handle,
|
|
1655
|
+
std::vector<double> velocities,
|
|
1656
|
+
double timestamp,
|
|
1657
|
+
const Value &decayValue) {
|
|
1658
|
+
size_t valueCount = 0;
|
|
1659
|
+
std::vector<Value> ancestors;
|
|
1660
|
+
auto decay = dynamicFromJSI(
|
|
1661
|
+
runtime,
|
|
1662
|
+
decayValue,
|
|
1663
|
+
"UI worklet decay",
|
|
1664
|
+
0,
|
|
1665
|
+
valueCount,
|
|
1666
|
+
ancestors);
|
|
1667
|
+
uiWorklets_.decay(
|
|
1668
|
+
handle,
|
|
1669
|
+
std::move(velocities),
|
|
1670
|
+
timestamp,
|
|
1671
|
+
solid_native::worklets::UIWorkletDecay::parse(decay));
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
std::vector<double> cancelUIWorkletAnimation(
|
|
1675
|
+
int64_t handle,
|
|
1676
|
+
double timestamp) {
|
|
1677
|
+
return uiWorklets_.cancelAnimation(handle, timestamp);
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
std::shared_ptr<AndroidUIWorkletState> attachUIWorkletPan(
|
|
1681
|
+
Runtime &runtime,
|
|
1682
|
+
int64_t handle,
|
|
1683
|
+
const Value &panValue) {
|
|
1684
|
+
auto state = uiWorklets_.requireActive(handle);
|
|
1685
|
+
size_t valueCount = 0;
|
|
1686
|
+
std::vector<Value> ancestors;
|
|
1687
|
+
auto pan = dynamicFromJSI(
|
|
1688
|
+
runtime,
|
|
1689
|
+
panValue,
|
|
1690
|
+
"UI worklet pan gesture",
|
|
1691
|
+
0,
|
|
1692
|
+
valueCount,
|
|
1693
|
+
ancestors);
|
|
1694
|
+
return uiWorklets_.attachPan(
|
|
1695
|
+
handle,
|
|
1696
|
+
solid_native::worklets::UIWorkletPanGesture::parse(
|
|
1697
|
+
pan, state->graph));
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
void detachUIWorkletPan(int64_t handle) {
|
|
1701
|
+
uiWorklets_.detachPan(handle);
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
std::vector<double> releaseUIWorkletPan(
|
|
1705
|
+
int64_t handle,
|
|
1706
|
+
double timestamp) {
|
|
1707
|
+
return uiWorklets_.releasePan(handle, timestamp);
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1710
|
+
void destroyUIWorklet(int64_t handle) {
|
|
1711
|
+
uiWorklets_.destroy(handle);
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
std::shared_ptr<AndroidUIWorkletState> requireUIWorklet(
|
|
1715
|
+
int64_t handle) const {
|
|
1716
|
+
return uiWorklets_.requireActive(handle);
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
size_t activeUIWorkletCount() const {
|
|
1720
|
+
return uiWorklets_.activeCount();
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
std::pair<size_t, size_t> uiWorkletActivityCounts() const {
|
|
1724
|
+
return uiWorklets_.activityCounts();
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
void cancelAllUIWorklets() {
|
|
1728
|
+
uiWorklets_.cancelAll();
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
void deactivateNativeWork() {
|
|
1732
|
+
cancelAllUIWorklets();
|
|
1733
|
+
deactivateEvents();
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
void cancelDeletedUIWorklets(const std::vector<int64_t> &handles) {
|
|
1737
|
+
for (const auto handle : handles) uiWorklets_.cancelNode(handle);
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
void retireInstances(
|
|
1741
|
+
int64_t fabricRevision,
|
|
1742
|
+
const std::vector<int64_t> &handles) {
|
|
1743
|
+
std::vector<int64_t> retainedHandles;
|
|
1744
|
+
retainedHandles.reserve(handles.size());
|
|
1745
|
+
for (const auto handle : handles) {
|
|
1746
|
+
if (retainedHandles_.contains(handle) &&
|
|
1747
|
+
!isPendingRetirement(handle)) {
|
|
1748
|
+
retainedHandles.push_back(handle);
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
if (!retainedHandles.empty()) {
|
|
1752
|
+
retiredInstances_.push_back({
|
|
1753
|
+
.fabricRevision = fabricRevision,
|
|
1754
|
+
.handles = std::move(retainedHandles),
|
|
1755
|
+
});
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
void reclaimMountedInstances(
|
|
1760
|
+
Runtime &runtime,
|
|
1761
|
+
int64_t mountedFabricRevision) {
|
|
1762
|
+
auto iterator = retiredInstances_.begin();
|
|
1763
|
+
while (iterator != retiredInstances_.end()) {
|
|
1764
|
+
if (iterator->fabricRevision > mountedFabricRevision) {
|
|
1765
|
+
iterator++;
|
|
1766
|
+
continue;
|
|
1767
|
+
}
|
|
1768
|
+
for (const auto handle : iterator->handles) {
|
|
1769
|
+
releaseInstance(runtime, handle);
|
|
1770
|
+
}
|
|
1771
|
+
iterator = retiredInstances_.erase(iterator);
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
void reclaimAllInstances(Runtime &runtime) {
|
|
1776
|
+
const std::vector<int64_t> handles(
|
|
1777
|
+
retainedHandles_.begin(), retainedHandles_.end());
|
|
1778
|
+
for (const auto handle : handles) releaseInstance(runtime, handle);
|
|
1779
|
+
retiredInstances_.clear();
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
void setEventHandler(Runtime &runtime, const Value &handler) {
|
|
1783
|
+
storage(runtime).setProperty(
|
|
1784
|
+
runtime, "eventHandler", Value(runtime, handler));
|
|
1785
|
+
routing_->handlerInstalled.store(
|
|
1786
|
+
handler.isObject() && handler.getObject(runtime).isFunction(runtime),
|
|
1787
|
+
std::memory_order_release);
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
Value eventHandler(Runtime &runtime) const {
|
|
1791
|
+
return storage(runtime).getProperty(runtime, "eventHandler");
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
void publishCommit(
|
|
1795
|
+
const folly::dynamic &transaction,
|
|
1796
|
+
const NodeIdentityMap &identities,
|
|
1797
|
+
int64_t sequence) {
|
|
1798
|
+
routing_->surface.store(
|
|
1799
|
+
dynamicInteger(transaction.at("surface")),
|
|
1800
|
+
std::memory_order_release);
|
|
1801
|
+
routing_->publishCommit(transaction, identities, sequence);
|
|
1802
|
+
}
|
|
1803
|
+
|
|
1804
|
+
EventRoutingState::LayoutRouteStage stageLayoutRoutes(
|
|
1805
|
+
const folly::dynamic &transaction,
|
|
1806
|
+
const NodeIdentityMap &identities) {
|
|
1807
|
+
return routing_->stageLayoutRoutes(transaction, identities);
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
void finishLayoutRoutes(EventRoutingState::LayoutRouteStage &stage) {
|
|
1811
|
+
routing_->finishLayoutRoutes(stage);
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
void rollbackLayoutRoutes(EventRoutingState::LayoutRouteStage &stage) {
|
|
1815
|
+
routing_->rollbackLayoutRoutes(stage);
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
void setEventListener(
|
|
1819
|
+
std::shared_ptr<const facebook::react::EventListener> listener) {
|
|
1820
|
+
eventListener_ = std::move(listener);
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
void attachEventListener(facebook::react::UIManager &manager) {
|
|
1824
|
+
if (eventListenerAttached_ || !eventListener_) return;
|
|
1825
|
+
manager.addEventListener(eventListener_);
|
|
1826
|
+
eventListenerAttached_ = true;
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
void detachEventListener(facebook::react::UIManager &manager) {
|
|
1830
|
+
if (!eventListenerAttached_ || !eventListener_) return;
|
|
1831
|
+
manager.removeEventListener(eventListener_);
|
|
1832
|
+
eventListenerAttached_ = false;
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
void deactivateEvents() {
|
|
1836
|
+
routing_->active.store(false, std::memory_order_release);
|
|
1837
|
+
routing_->handlerInstalled.store(false, std::memory_order_release);
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
void setCommitLifecycleHandler(Runtime &runtime, const Value &handler) {
|
|
1841
|
+
storage(runtime).setProperty(
|
|
1842
|
+
runtime, "commitLifecycleHandler", Value(runtime, handler));
|
|
1843
|
+
commitLifecycleHandlerInstalled_ =
|
|
1844
|
+
handler.isObject() && handler.getObject(runtime).isFunction(runtime);
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
Value commitLifecycleHandler(Runtime &runtime) const {
|
|
1848
|
+
return storage(runtime).getProperty(runtime, "commitLifecycleHandler");
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
bool hasCommitLifecycleHandler() const {
|
|
1852
|
+
return commitLifecycleHandlerInstalled_;
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
bool active() const {
|
|
1856
|
+
return routing_->active.load(std::memory_order_acquire);
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
private:
|
|
1860
|
+
bool isPendingRetirement(int64_t handle) const {
|
|
1861
|
+
return std::any_of(
|
|
1862
|
+
retiredInstances_.begin(),
|
|
1863
|
+
retiredInstances_.end(),
|
|
1864
|
+
[handle](const RetiredInstances &retired) {
|
|
1865
|
+
return std::find(
|
|
1866
|
+
retired.handles.begin(),
|
|
1867
|
+
retired.handles.end(),
|
|
1868
|
+
handle) != retired.handles.end();
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1872
|
+
facebook::jsi::WeakObject storage_;
|
|
1873
|
+
std::shared_ptr<EventRoutingState> routing_;
|
|
1874
|
+
std::shared_ptr<const facebook::react::EventListener> eventListener_;
|
|
1875
|
+
bool eventListenerAttached_{false};
|
|
1876
|
+
std::unordered_set<int64_t> retainedHandles_;
|
|
1877
|
+
std::vector<RetiredInstances> retiredInstances_;
|
|
1878
|
+
int64_t nextResourceHandle_{1};
|
|
1879
|
+
std::unordered_map<int64_t, std::string> resourceKinds_;
|
|
1880
|
+
std::function<void()> runtimeRetirementHandler_;
|
|
1881
|
+
bool commitLifecycleHandlerInstalled_{false};
|
|
1882
|
+
AndroidUIWorkletRegistry uiWorklets_;
|
|
1883
|
+
};
|
|
1884
|
+
|
|
1885
|
+
std::shared_ptr<facebook::react::EventListener> createEventListener(
|
|
1886
|
+
const std::shared_ptr<EventRoutingState> &routing,
|
|
1887
|
+
const std::shared_ptr<facebook::react::RuntimeScheduler> &runtimeScheduler,
|
|
1888
|
+
const std::weak_ptr<RuntimeState> &runtimeState) {
|
|
1889
|
+
std::weak_ptr<facebook::react::RuntimeScheduler> weakScheduler =
|
|
1890
|
+
runtimeScheduler;
|
|
1891
|
+
return std::make_shared<facebook::react::EventListener>(
|
|
1892
|
+
[routing, weakScheduler, runtimeState](
|
|
1893
|
+
const facebook::react::RawEvent &event) -> bool {
|
|
1894
|
+
auto family = event.shadowNodeFamily.lock();
|
|
1895
|
+
if (!family || family->getSurfaceId() !=
|
|
1896
|
+
routing->surface.load(std::memory_order_acquire)) {
|
|
1897
|
+
return false;
|
|
1898
|
+
}
|
|
1899
|
+
auto route = routing->route(family->getTag(), event.type);
|
|
1900
|
+
if (!route.has_value()) return false;
|
|
1901
|
+
if (!routing->active.load(std::memory_order_acquire) ||
|
|
1902
|
+
!routing->handlerInstalled.load(std::memory_order_acquire) ||
|
|
1903
|
+
route->empty() || !event.eventPayload) {
|
|
1904
|
+
return true;
|
|
1905
|
+
}
|
|
1906
|
+
auto scheduler = weakScheduler.lock();
|
|
1907
|
+
if (!scheduler) return true;
|
|
1908
|
+
|
|
1909
|
+
const auto surface = family->getSurfaceId();
|
|
1910
|
+
const auto observedSequence =
|
|
1911
|
+
routing->sequence.load(std::memory_order_acquire);
|
|
1912
|
+
const auto timestamp =
|
|
1913
|
+
event.eventStartTimeStamp.toDOMHighResTimeStamp();
|
|
1914
|
+
const auto coalescible = event.isUnique;
|
|
1915
|
+
const auto payload = event.eventPayload;
|
|
1916
|
+
for (const auto &routedEvent : *route) {
|
|
1917
|
+
const auto node = routedEvent.node;
|
|
1918
|
+
const auto name = routedEvent.name;
|
|
1919
|
+
const auto priority =
|
|
1920
|
+
std::string(semanticEventPriority(name, event.category));
|
|
1921
|
+
const auto bubbles = eventBubbles(name);
|
|
1922
|
+
scheduler->scheduleWork(
|
|
1923
|
+
[routing,
|
|
1924
|
+
runtimeState,
|
|
1925
|
+
payload,
|
|
1926
|
+
surface,
|
|
1927
|
+
node,
|
|
1928
|
+
observedSequence,
|
|
1929
|
+
name,
|
|
1930
|
+
timestamp,
|
|
1931
|
+
priority,
|
|
1932
|
+
bubbles,
|
|
1933
|
+
coalescible](Runtime &runtime) {
|
|
1934
|
+
if (!routing->active.load(std::memory_order_acquire)) return;
|
|
1935
|
+
if (!routing->isSubscribed(node, name)) return;
|
|
1936
|
+
auto state = runtimeState.lock();
|
|
1937
|
+
if (!state) return;
|
|
1938
|
+
auto handlerValue = state->eventHandler(runtime);
|
|
1939
|
+
if (!handlerValue.isObject()) return;
|
|
1940
|
+
auto handlerObject = handlerValue.getObject(runtime);
|
|
1941
|
+
if (!handlerObject.isFunction(runtime)) return;
|
|
1942
|
+
|
|
1943
|
+
Object nativeEvent(runtime);
|
|
1944
|
+
nativeEvent.setProperty(
|
|
1945
|
+
runtime, "surface", static_cast<double>(surface));
|
|
1946
|
+
nativeEvent.setProperty(
|
|
1947
|
+
runtime, "target", static_cast<double>(node));
|
|
1948
|
+
nativeEvent.setProperty(
|
|
1949
|
+
runtime,
|
|
1950
|
+
"observedSequence",
|
|
1951
|
+
static_cast<double>(observedSequence));
|
|
1952
|
+
nativeEvent.setProperty(
|
|
1953
|
+
runtime,
|
|
1954
|
+
"name",
|
|
1955
|
+
String::createFromUtf8(runtime, name));
|
|
1956
|
+
nativeEvent.setProperty(runtime, "timestamp", timestamp);
|
|
1957
|
+
nativeEvent.setProperty(
|
|
1958
|
+
runtime,
|
|
1959
|
+
"priority",
|
|
1960
|
+
String::createFromUtf8(runtime, priority));
|
|
1961
|
+
nativeEvent.setProperty(runtime, "bubbles", bubbles);
|
|
1962
|
+
nativeEvent.setProperty(
|
|
1963
|
+
runtime, "coalescible", coalescible);
|
|
1964
|
+
nativeEvent.setProperty(
|
|
1965
|
+
runtime, "payload", payload->asJSIValue(runtime));
|
|
1966
|
+
handlerObject
|
|
1967
|
+
.getFunction(runtime)
|
|
1968
|
+
.call(runtime, nativeEvent);
|
|
1969
|
+
});
|
|
1970
|
+
}
|
|
1971
|
+
return true;
|
|
1972
|
+
});
|
|
1973
|
+
}
|
|
1974
|
+
|
|
1975
|
+
struct CommitMountObservation final {
|
|
1976
|
+
int64_t surface;
|
|
1977
|
+
int64_t sequence;
|
|
1978
|
+
int64_t fabricRevision;
|
|
1979
|
+
double commitStartedAt;
|
|
1980
|
+
double commitStartedMonotonic;
|
|
1981
|
+
bool notifyLifecycle;
|
|
1982
|
+
std::optional<std::string> causalOperationId;
|
|
1983
|
+
};
|
|
1984
|
+
|
|
1985
|
+
struct CommitFrameObservation final {
|
|
1986
|
+
std::weak_ptr<facebook::react::RuntimeScheduler> runtimeScheduler;
|
|
1987
|
+
std::weak_ptr<RuntimeState> runtimeState;
|
|
1988
|
+
std::shared_ptr<const CommitMountObservation> commit;
|
|
1989
|
+
double mountedAt;
|
|
1990
|
+
double mountedMonotonic;
|
|
1991
|
+
};
|
|
1992
|
+
|
|
1993
|
+
constexpr size_t MaximumPendingFrameObservations = 256;
|
|
1994
|
+
std::atomic<int64_t> nextFrameObservationToken{1};
|
|
1995
|
+
std::mutex frameObservationMutex;
|
|
1996
|
+
std::unordered_map<
|
|
1997
|
+
int64_t,
|
|
1998
|
+
std::shared_ptr<const CommitFrameObservation>>
|
|
1999
|
+
frameObservations;
|
|
2000
|
+
|
|
2001
|
+
void requestJavaCommitFrame(int64_t token);
|
|
2002
|
+
|
|
2003
|
+
int64_t retainFrameObservation(
|
|
2004
|
+
std::shared_ptr<const CommitFrameObservation> observation) {
|
|
2005
|
+
const auto token =
|
|
2006
|
+
nextFrameObservationToken.fetch_add(1, std::memory_order_relaxed);
|
|
2007
|
+
std::lock_guard lock(frameObservationMutex);
|
|
2008
|
+
if (frameObservations.size() >= MaximumPendingFrameObservations) {
|
|
2009
|
+
frameObservations.erase(frameObservations.begin());
|
|
2010
|
+
}
|
|
2011
|
+
frameObservations.emplace(token, std::move(observation));
|
|
2012
|
+
return token;
|
|
2013
|
+
}
|
|
2014
|
+
|
|
2015
|
+
void discardFrameObservation(int64_t token) noexcept {
|
|
2016
|
+
std::lock_guard lock(frameObservationMutex);
|
|
2017
|
+
frameObservations.erase(token);
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
void onJavaCommitFrame(
|
|
2021
|
+
facebook::jni::alias_ref<jclass>,
|
|
2022
|
+
jlong token) {
|
|
2023
|
+
std::shared_ptr<const CommitFrameObservation> frame;
|
|
2024
|
+
{
|
|
2025
|
+
std::lock_guard lock(frameObservationMutex);
|
|
2026
|
+
const auto entry = frameObservations.find(token);
|
|
2027
|
+
if (entry == frameObservations.end()) return;
|
|
2028
|
+
frame = std::move(entry->second);
|
|
2029
|
+
frameObservations.erase(entry);
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
auto runtimeScheduler = frame->runtimeScheduler.lock();
|
|
2033
|
+
if (!runtimeScheduler) return;
|
|
2034
|
+
const auto frameStartedAt = wallClockMilliseconds();
|
|
2035
|
+
const auto frameStartedMonotonic = monotonicMilliseconds();
|
|
2036
|
+
const auto frameLatency = std::max(
|
|
2037
|
+
0.0,
|
|
2038
|
+
frameStartedMonotonic - frame->commit->commitStartedMonotonic);
|
|
2039
|
+
const auto mountToFrameLatency =
|
|
2040
|
+
std::max(0.0, frameStartedMonotonic - frame->mountedMonotonic);
|
|
2041
|
+
runtimeScheduler->scheduleWork(
|
|
2042
|
+
[runtimeState = frame->runtimeState,
|
|
2043
|
+
observation = frame->commit,
|
|
2044
|
+
mountedAt = frame->mountedAt,
|
|
2045
|
+
frameStartedAt,
|
|
2046
|
+
frameLatency,
|
|
2047
|
+
mountToFrameLatency](Runtime &runtime) {
|
|
2048
|
+
auto state = runtimeState.lock();
|
|
2049
|
+
if (!state || !state->active() ||
|
|
2050
|
+
!state->hasCommitLifecycleHandler()) {
|
|
2051
|
+
return;
|
|
2052
|
+
}
|
|
2053
|
+
auto handlerValue = state->commitLifecycleHandler(runtime);
|
|
2054
|
+
if (!handlerValue.isObject()) return;
|
|
2055
|
+
auto handlerObject = handlerValue.getObject(runtime);
|
|
2056
|
+
if (!handlerObject.isFunction(runtime)) return;
|
|
2057
|
+
|
|
2058
|
+
Object event(runtime);
|
|
2059
|
+
event.setProperty(
|
|
2060
|
+
runtime,
|
|
2061
|
+
"type",
|
|
2062
|
+
String::createFromAscii(runtime, "commit-frame"));
|
|
2063
|
+
event.setProperty(
|
|
2064
|
+
runtime,
|
|
2065
|
+
"surface",
|
|
2066
|
+
static_cast<double>(observation->surface));
|
|
2067
|
+
event.setProperty(
|
|
2068
|
+
runtime,
|
|
2069
|
+
"sequence",
|
|
2070
|
+
static_cast<double>(observation->sequence));
|
|
2071
|
+
event.setProperty(
|
|
2072
|
+
runtime,
|
|
2073
|
+
"hostRevision",
|
|
2074
|
+
static_cast<double>(observation->fabricRevision));
|
|
2075
|
+
event.setProperty(runtime, "mountedAt", mountedAt);
|
|
2076
|
+
event.setProperty(runtime, "frameStartedAt", frameStartedAt);
|
|
2077
|
+
event.setProperty(runtime, "frameLatency", frameLatency);
|
|
2078
|
+
event.setProperty(
|
|
2079
|
+
runtime, "mountToFrameLatency", mountToFrameLatency);
|
|
2080
|
+
if (observation->causalOperationId.has_value()) {
|
|
2081
|
+
Object causalContext(runtime);
|
|
2082
|
+
causalContext.setProperty(
|
|
2083
|
+
runtime,
|
|
2084
|
+
"operationId",
|
|
2085
|
+
String::createFromUtf8(
|
|
2086
|
+
runtime, *observation->causalOperationId));
|
|
2087
|
+
event.setProperty(
|
|
2088
|
+
runtime, "causalContext", std::move(causalContext));
|
|
2089
|
+
}
|
|
2090
|
+
handlerObject.getFunction(runtime).call(runtime, event);
|
|
2091
|
+
});
|
|
2092
|
+
}
|
|
2093
|
+
|
|
2094
|
+
void onJavaUIWorkletFrame(
|
|
2095
|
+
facebook::jni::alias_ref<jclass>,
|
|
2096
|
+
jlong token,
|
|
2097
|
+
jlong frameTimeNanoseconds) {
|
|
2098
|
+
std::shared_ptr<AndroidUIWorkletState> state;
|
|
2099
|
+
{
|
|
2100
|
+
std::lock_guard lock(uiWorkletFrameMutex);
|
|
2101
|
+
const auto entry = uiWorkletFrames.find(token);
|
|
2102
|
+
if (entry == uiWorkletFrames.end()) return;
|
|
2103
|
+
state = std::move(entry->second);
|
|
2104
|
+
uiWorkletFrames.erase(entry);
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
bool continueAnimation = false;
|
|
2108
|
+
{
|
|
2109
|
+
std::lock_guard lock(state->mutex);
|
|
2110
|
+
if (!state->frameToken.has_value() || *state->frameToken != token) return;
|
|
2111
|
+
state->framePending = false;
|
|
2112
|
+
state->frameToken.reset();
|
|
2113
|
+
if (!state->active) return;
|
|
2114
|
+
try {
|
|
2115
|
+
auto nextInputs = state->inputs;
|
|
2116
|
+
auto nextTimingProgress = state->timingProgress;
|
|
2117
|
+
const auto recordsTimingFrame = state->timing.has_value();
|
|
2118
|
+
const auto recordsSpringFrame = state->spring.has_value();
|
|
2119
|
+
const auto recordsDecayFrame = state->decay.has_value();
|
|
2120
|
+
if (state->timing.has_value()) {
|
|
2121
|
+
auto &timing = *state->timing;
|
|
2122
|
+
if (!timing.startedAtNanoseconds.has_value()) {
|
|
2123
|
+
timing.startedAtNanoseconds = frameTimeNanoseconds;
|
|
2124
|
+
}
|
|
2125
|
+
const auto elapsedMilliseconds = static_cast<double>(
|
|
2126
|
+
frameTimeNanoseconds - *timing.startedAtNanoseconds) /
|
|
2127
|
+
1000000.0;
|
|
2128
|
+
const auto sample =
|
|
2129
|
+
timing.definition.evaluate(timing.from, elapsedMilliseconds);
|
|
2130
|
+
nextTimingProgress = sample.progress;
|
|
2131
|
+
nextInputs = sample.inputs;
|
|
2132
|
+
if (sample.settled) state->timing.reset();
|
|
2133
|
+
} else if (state->spring.has_value()) {
|
|
2134
|
+
auto &spring = *state->spring;
|
|
2135
|
+
if (!spring.startedAtNanoseconds.has_value()) {
|
|
2136
|
+
spring.startedAtNanoseconds = frameTimeNanoseconds;
|
|
2137
|
+
}
|
|
2138
|
+
const auto elapsedMilliseconds = static_cast<double>(
|
|
2139
|
+
frameTimeNanoseconds - *spring.startedAtNanoseconds) /
|
|
2140
|
+
1000000.0;
|
|
2141
|
+
const auto sample = spring.definition.evaluate(elapsedMilliseconds);
|
|
2142
|
+
nextInputs = solid_native::worklets::interpolateUIWorkletInputs(
|
|
2143
|
+
spring.from, spring.to, sample.position);
|
|
2144
|
+
state->springPosition = sample.position;
|
|
2145
|
+
state->springVelocity = sample.velocity;
|
|
2146
|
+
if (sample.settled) state->spring.reset();
|
|
2147
|
+
} else if (state->decay.has_value()) {
|
|
2148
|
+
auto &decay = *state->decay;
|
|
2149
|
+
if (!decay.startedAtNanoseconds.has_value()) {
|
|
2150
|
+
decay.startedAtNanoseconds = frameTimeNanoseconds;
|
|
2151
|
+
}
|
|
2152
|
+
const auto elapsedMilliseconds = static_cast<double>(
|
|
2153
|
+
frameTimeNanoseconds - *decay.startedAtNanoseconds) /
|
|
2154
|
+
1000000.0;
|
|
2155
|
+
const auto sample = decay.definition.evaluate(
|
|
2156
|
+
decay.initialSpeed, elapsedMilliseconds);
|
|
2157
|
+
nextInputs = decay.from;
|
|
2158
|
+
for (size_t index = 0; index < nextInputs.size(); ++index) {
|
|
2159
|
+
nextInputs.at(index) += decay.velocities.at(index) *
|
|
2160
|
+
sample.displacementFactorSeconds;
|
|
2161
|
+
}
|
|
2162
|
+
state->decayElapsedMilliseconds = elapsedMilliseconds;
|
|
2163
|
+
state->decaySpeed = sample.speed;
|
|
2164
|
+
if (sample.settled) state->decay.reset();
|
|
2165
|
+
}
|
|
2166
|
+
auto nextOutputs = state->graph.evaluate(nextInputs);
|
|
2167
|
+
if (recordsTimingFrame) {
|
|
2168
|
+
state->timingFrameStatistics.recordFrame(frameTimeNanoseconds);
|
|
2169
|
+
}
|
|
2170
|
+
if (recordsSpringFrame) {
|
|
2171
|
+
state->springFrameStatistics.recordFrame(frameTimeNanoseconds);
|
|
2172
|
+
}
|
|
2173
|
+
if (recordsDecayFrame) {
|
|
2174
|
+
state->decayFrameStatistics.recordFrame(frameTimeNanoseconds);
|
|
2175
|
+
}
|
|
2176
|
+
state->inputs = std::move(nextInputs);
|
|
2177
|
+
state->outputs = std::move(nextOutputs);
|
|
2178
|
+
state->timingProgress = nextTimingProgress;
|
|
2179
|
+
std::array<double, AndroidViewOutputCount> values;
|
|
2180
|
+
values.fill(std::numeric_limits<double>::quiet_NaN());
|
|
2181
|
+
for (size_t index = 0; index < state->outputs.size(); ++index) {
|
|
2182
|
+
values.at(static_cast<size_t>(state->channels.at(index))) =
|
|
2183
|
+
state->outputs.at(index).value;
|
|
2184
|
+
}
|
|
2185
|
+
state->sequence++;
|
|
2186
|
+
state->frameTimeNanoseconds = frameTimeNanoseconds;
|
|
2187
|
+
state->applied = applyJavaUIWorkletFrame(
|
|
2188
|
+
state->handle,
|
|
2189
|
+
state->targetTag,
|
|
2190
|
+
state->sequence,
|
|
2191
|
+
frameTimeNanoseconds,
|
|
2192
|
+
values);
|
|
2193
|
+
if (state->applied) state->appliedSequence = state->sequence;
|
|
2194
|
+
continueAnimation =
|
|
2195
|
+
state->timing.has_value() || state->spring.has_value() ||
|
|
2196
|
+
state->decay.has_value();
|
|
2197
|
+
} catch (...) {
|
|
2198
|
+
state->applied = false;
|
|
2199
|
+
state->timing.reset();
|
|
2200
|
+
state->spring.reset();
|
|
2201
|
+
state->decay.reset();
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
if (continueAnimation) {
|
|
2205
|
+
try {
|
|
2206
|
+
scheduleUIWorkletFrame(state);
|
|
2207
|
+
} catch (...) {
|
|
2208
|
+
std::lock_guard lock(state->mutex);
|
|
2209
|
+
state->applied = false;
|
|
2210
|
+
state->timing.reset();
|
|
2211
|
+
state->spring.reset();
|
|
2212
|
+
state->decay.reset();
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
|
|
2217
|
+
void onJavaUIWorkletPan(
|
|
2218
|
+
facebook::jni::alias_ref<jclass>,
|
|
2219
|
+
jlong token,
|
|
2220
|
+
jint phaseValue,
|
|
2221
|
+
jdouble translationX,
|
|
2222
|
+
jdouble translationY,
|
|
2223
|
+
jdouble timestamp,
|
|
2224
|
+
jdouble velocityX,
|
|
2225
|
+
jdouble velocityY) {
|
|
2226
|
+
std::shared_ptr<AndroidUIWorkletState> state;
|
|
2227
|
+
{
|
|
2228
|
+
std::lock_guard lock(uiWorkletPanMutex);
|
|
2229
|
+
const auto entry = uiWorkletPans.find(token);
|
|
2230
|
+
if (entry == uiWorkletPans.end()) return;
|
|
2231
|
+
state = entry->second;
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
bool scheduleReleaseDecay = false;
|
|
2235
|
+
{
|
|
2236
|
+
std::lock_guard lock(state->mutex);
|
|
2237
|
+
if (!state->active || !state->pan.has_value()) return;
|
|
2238
|
+
auto &pan = *state->pan;
|
|
2239
|
+
try {
|
|
2240
|
+
if (phaseValue < 0 || phaseValue > 3 ||
|
|
2241
|
+
!std::isfinite(translationX) || !std::isfinite(translationY) ||
|
|
2242
|
+
!std::isfinite(timestamp) || timestamp < 0 ||
|
|
2243
|
+
!std::isfinite(velocityX) || !std::isfinite(velocityY) ||
|
|
2244
|
+
(pan.timestamp.has_value() && timestamp < *pan.timestamp)) {
|
|
2245
|
+
throw std::invalid_argument(
|
|
2246
|
+
"The Android native UI worklet pan sample is invalid.");
|
|
2247
|
+
}
|
|
2248
|
+
const auto phase =
|
|
2249
|
+
static_cast<solid_native::worklets::UIWorkletPanGesturePhase>(
|
|
2250
|
+
phaseValue);
|
|
2251
|
+
if (phase ==
|
|
2252
|
+
solid_native::worklets::UIWorkletPanGesturePhase::Begin) {
|
|
2253
|
+
if (pan.active) {
|
|
2254
|
+
throw std::invalid_argument(
|
|
2255
|
+
"The Android native UI worklet pan is already active.");
|
|
2256
|
+
}
|
|
2257
|
+
pan.active = true;
|
|
2258
|
+
pan.originX = state->inputs.at(pan.definition.xInput);
|
|
2259
|
+
pan.originY = state->inputs.at(pan.definition.yInput);
|
|
2260
|
+
pan.timestamp = timestamp;
|
|
2261
|
+
state->timing.reset();
|
|
2262
|
+
state->timingProgress = 0;
|
|
2263
|
+
state->spring.reset();
|
|
2264
|
+
if (state->springPosition.has_value()) state->springPosition = 0;
|
|
2265
|
+
if (state->springVelocity.has_value()) state->springVelocity = 0;
|
|
2266
|
+
state->decay.reset();
|
|
2267
|
+
if (state->decayElapsedMilliseconds.has_value()) {
|
|
2268
|
+
state->decayElapsedMilliseconds = 0;
|
|
2269
|
+
}
|
|
2270
|
+
if (state->decaySpeed.has_value()) state->decaySpeed = 0;
|
|
2271
|
+
return;
|
|
2272
|
+
}
|
|
2273
|
+
if (!pan.active) {
|
|
2274
|
+
throw std::invalid_argument(
|
|
2275
|
+
"The Android native UI worklet pan is not active.");
|
|
2276
|
+
}
|
|
2277
|
+
if (phase ==
|
|
2278
|
+
solid_native::worklets::UIWorkletPanGesturePhase::Cancel) {
|
|
2279
|
+
pan.active = false;
|
|
2280
|
+
pan.timestamp = timestamp;
|
|
2281
|
+
return;
|
|
2282
|
+
}
|
|
2283
|
+
auto nextInputs = pan.definition.evaluateInputs(
|
|
2284
|
+
state->inputs,
|
|
2285
|
+
pan.originX,
|
|
2286
|
+
pan.originY,
|
|
2287
|
+
translationX,
|
|
2288
|
+
translationY);
|
|
2289
|
+
auto nextOutputs = state->graph.evaluate(nextInputs);
|
|
2290
|
+
std::optional<AndroidUIWorkletDecayState> releaseDecay;
|
|
2291
|
+
double releaseSpeed = 0;
|
|
2292
|
+
if (phase == solid_native::worklets::UIWorkletPanGesturePhase::End &&
|
|
2293
|
+
pan.definition.releaseDecay.has_value()) {
|
|
2294
|
+
if (std::abs(velocityX) >
|
|
2295
|
+
solid_native::worklets::MaximumUIWorkletDecayVelocity ||
|
|
2296
|
+
std::abs(velocityY) >
|
|
2297
|
+
solid_native::worklets::MaximumUIWorkletDecayVelocity) {
|
|
2298
|
+
throw std::invalid_argument(
|
|
2299
|
+
"The Android native UI worklet pan velocity is outside its bounded range.");
|
|
2300
|
+
}
|
|
2301
|
+
std::vector<double> velocities(state->inputs.size(), 0);
|
|
2302
|
+
velocities.at(pan.definition.xInput) = velocityX;
|
|
2303
|
+
velocities.at(pan.definition.yInput) = velocityY;
|
|
2304
|
+
releaseSpeed = std::max(std::abs(velocityX), std::abs(velocityY));
|
|
2305
|
+
const auto &definition = *pan.definition.releaseDecay;
|
|
2306
|
+
const auto terminal = definition.evaluate(
|
|
2307
|
+
releaseSpeed, definition.maximumDurationMilliseconds);
|
|
2308
|
+
auto terminalInputs = nextInputs;
|
|
2309
|
+
for (size_t index = 0; index < terminalInputs.size(); ++index) {
|
|
2310
|
+
terminalInputs.at(index) +=
|
|
2311
|
+
velocities.at(index) * terminal.displacementFactorSeconds;
|
|
2312
|
+
}
|
|
2313
|
+
// Reject a terminal graph failure before publishing the final pan.
|
|
2314
|
+
state->graph.evaluate(terminalInputs);
|
|
2315
|
+
releaseDecay = AndroidUIWorkletDecayState{
|
|
2316
|
+
.from = nextInputs,
|
|
2317
|
+
.velocities = std::move(velocities),
|
|
2318
|
+
.initialSpeed = releaseSpeed,
|
|
2319
|
+
.definition = definition,
|
|
2320
|
+
};
|
|
2321
|
+
}
|
|
2322
|
+
std::array<double, AndroidViewOutputCount> values;
|
|
2323
|
+
values.fill(std::numeric_limits<double>::quiet_NaN());
|
|
2324
|
+
for (size_t index = 0; index < nextOutputs.size(); ++index) {
|
|
2325
|
+
values.at(static_cast<size_t>(state->channels.at(index))) =
|
|
2326
|
+
nextOutputs.at(index).value;
|
|
2327
|
+
}
|
|
2328
|
+
state->inputs = std::move(nextInputs);
|
|
2329
|
+
state->outputs = std::move(nextOutputs);
|
|
2330
|
+
state->timing.reset();
|
|
2331
|
+
state->timingProgress = 0;
|
|
2332
|
+
state->spring.reset();
|
|
2333
|
+
if (state->springPosition.has_value()) state->springPosition = 0;
|
|
2334
|
+
if (state->springVelocity.has_value()) state->springVelocity = 0;
|
|
2335
|
+
state->decay = std::move(releaseDecay);
|
|
2336
|
+
if (state->decay.has_value()) {
|
|
2337
|
+
state->decayElapsedMilliseconds = 0;
|
|
2338
|
+
state->decaySpeed = releaseSpeed;
|
|
2339
|
+
state->decayFrameStatistics.reset();
|
|
2340
|
+
scheduleReleaseDecay = true;
|
|
2341
|
+
} else {
|
|
2342
|
+
if (state->decayElapsedMilliseconds.has_value()) {
|
|
2343
|
+
state->decayElapsedMilliseconds = 0;
|
|
2344
|
+
}
|
|
2345
|
+
if (state->decaySpeed.has_value()) state->decaySpeed = 0;
|
|
2346
|
+
}
|
|
2347
|
+
state->sequence++;
|
|
2348
|
+
state->frameTimeNanoseconds =
|
|
2349
|
+
static_cast<int64_t>(std::llround(timestamp * 1000000.0));
|
|
2350
|
+
state->applied = applyJavaUIWorkletFrame(
|
|
2351
|
+
state->handle,
|
|
2352
|
+
state->targetTag,
|
|
2353
|
+
state->sequence,
|
|
2354
|
+
state->frameTimeNanoseconds,
|
|
2355
|
+
values);
|
|
2356
|
+
if (state->applied) state->appliedSequence = state->sequence;
|
|
2357
|
+
pan.sequence++;
|
|
2358
|
+
pan.timestamp = timestamp;
|
|
2359
|
+
if (phase == solid_native::worklets::UIWorkletPanGesturePhase::End) {
|
|
2360
|
+
pan.active = false;
|
|
2361
|
+
}
|
|
2362
|
+
} catch (...) {
|
|
2363
|
+
pan.active = false;
|
|
2364
|
+
state->timing.reset();
|
|
2365
|
+
state->timingProgress = 0;
|
|
2366
|
+
state->spring.reset();
|
|
2367
|
+
if (state->springPosition.has_value()) state->springPosition = 0;
|
|
2368
|
+
if (state->springVelocity.has_value()) state->springVelocity = 0;
|
|
2369
|
+
state->decay.reset();
|
|
2370
|
+
if (state->decayElapsedMilliseconds.has_value()) {
|
|
2371
|
+
state->decayElapsedMilliseconds = 0;
|
|
2372
|
+
}
|
|
2373
|
+
if (state->decaySpeed.has_value()) state->decaySpeed = 0;
|
|
2374
|
+
state->applied = false;
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
if (scheduleReleaseDecay) {
|
|
2378
|
+
try {
|
|
2379
|
+
scheduleUIWorkletFrame(state);
|
|
2380
|
+
} catch (...) {
|
|
2381
|
+
std::lock_guard lock(state->mutex);
|
|
2382
|
+
state->decay.reset();
|
|
2383
|
+
state->decayElapsedMilliseconds = 0;
|
|
2384
|
+
state->decaySpeed = 0;
|
|
2385
|
+
state->applied = false;
|
|
2386
|
+
}
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
|
|
2390
|
+
class MountObservationState final
|
|
2391
|
+
: public facebook::react::UIManagerMountHook {
|
|
2392
|
+
public:
|
|
2393
|
+
MountObservationState(
|
|
2394
|
+
std::shared_ptr<facebook::react::RuntimeScheduler> runtimeScheduler,
|
|
2395
|
+
std::weak_ptr<RuntimeState> runtimeState)
|
|
2396
|
+
: runtimeScheduler_(std::move(runtimeScheduler)),
|
|
2397
|
+
runtimeState_(std::move(runtimeState)) {}
|
|
2398
|
+
|
|
2399
|
+
void attach(facebook::react::UIManager &manager, int32_t surface) {
|
|
2400
|
+
std::lock_guard registrationLock(registrationMutex_);
|
|
2401
|
+
{
|
|
2402
|
+
std::lock_guard lock(mutex_);
|
|
2403
|
+
if (attached_) {
|
|
2404
|
+
if (manager_ == &manager && surface_ == surface) return;
|
|
2405
|
+
throw std::logic_error(
|
|
2406
|
+
"The Android Fabric mount observer is already attached.");
|
|
2407
|
+
}
|
|
2408
|
+
manager_ = &manager;
|
|
2409
|
+
surface_ = surface;
|
|
2410
|
+
latestMountedRevision_ = 0;
|
|
2411
|
+
pending_.clear();
|
|
2412
|
+
}
|
|
2413
|
+
manager.registerMountHook(*this);
|
|
2414
|
+
{
|
|
2415
|
+
std::lock_guard lock(mutex_);
|
|
2416
|
+
attached_ = true;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
void detach() noexcept {
|
|
2421
|
+
std::lock_guard registrationLock(registrationMutex_);
|
|
2422
|
+
facebook::react::UIManager *manager = nullptr;
|
|
2423
|
+
{
|
|
2424
|
+
std::lock_guard lock(mutex_);
|
|
2425
|
+
if (!attached_) return;
|
|
2426
|
+
attached_ = false;
|
|
2427
|
+
manager = manager_;
|
|
2428
|
+
manager_ = nullptr;
|
|
2429
|
+
surface_ = 0;
|
|
2430
|
+
latestMountedRevision_ = 0;
|
|
2431
|
+
pending_.clear();
|
|
2432
|
+
}
|
|
2433
|
+
if (manager != nullptr) manager->unregisterMountHook(*this);
|
|
2434
|
+
}
|
|
2435
|
+
|
|
2436
|
+
void enqueue(
|
|
2437
|
+
std::shared_ptr<const CommitMountObservation> observation) noexcept {
|
|
2438
|
+
int64_t mountedRevision = 0;
|
|
2439
|
+
{
|
|
2440
|
+
std::lock_guard lock(mutex_);
|
|
2441
|
+
if (!attached_) return;
|
|
2442
|
+
if (latestMountedRevision_ < observation->fabricRevision) {
|
|
2443
|
+
pending_.push_back(std::move(observation));
|
|
2444
|
+
return;
|
|
2445
|
+
}
|
|
2446
|
+
mountedRevision = latestMountedRevision_;
|
|
2447
|
+
}
|
|
2448
|
+
scheduleObservation(std::move(observation), mountedRevision);
|
|
2449
|
+
}
|
|
2450
|
+
|
|
2451
|
+
void shadowTreeDidMount(
|
|
2452
|
+
const facebook::react::RootShadowNode::Shared &rootShadowNode,
|
|
2453
|
+
facebook::react::HighResTimeStamp) noexcept override {
|
|
2454
|
+
try {
|
|
2455
|
+
facebook::react::UIManager *manager = nullptr;
|
|
2456
|
+
int32_t surface = 0;
|
|
2457
|
+
{
|
|
2458
|
+
std::lock_guard lock(mutex_);
|
|
2459
|
+
if (!attached_ || rootShadowNode == nullptr ||
|
|
2460
|
+
rootShadowNode->getSurfaceId() != surface_) {
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2463
|
+
manager = manager_;
|
|
2464
|
+
surface = surface_;
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
int64_t mountedRevision = 0;
|
|
2468
|
+
manager->getShadowTreeRegistry().visit(
|
|
2469
|
+
surface,
|
|
2470
|
+
[&mountedRevision](const facebook::react::ShadowTree &shadowTree) {
|
|
2471
|
+
mountedRevision = shadowTree.getMountingCoordinator()
|
|
2472
|
+
->getBaseRevision()
|
|
2473
|
+
.number;
|
|
2474
|
+
});
|
|
2475
|
+
if (mountedRevision <= 0) return;
|
|
2476
|
+
|
|
2477
|
+
std::vector<std::shared_ptr<const CommitMountObservation>> ready;
|
|
2478
|
+
{
|
|
2479
|
+
std::lock_guard lock(mutex_);
|
|
2480
|
+
if (!attached_ || manager_ != manager || surface_ != surface) return;
|
|
2481
|
+
latestMountedRevision_ =
|
|
2482
|
+
std::max(latestMountedRevision_, mountedRevision);
|
|
2483
|
+
auto iterator = pending_.begin();
|
|
2484
|
+
while (iterator != pending_.end()) {
|
|
2485
|
+
if ((*iterator)->fabricRevision > latestMountedRevision_) {
|
|
2486
|
+
iterator++;
|
|
2487
|
+
continue;
|
|
2488
|
+
}
|
|
2489
|
+
ready.push_back(std::move(*iterator));
|
|
2490
|
+
iterator = pending_.erase(iterator);
|
|
2491
|
+
}
|
|
2492
|
+
mountedRevision = latestMountedRevision_;
|
|
2493
|
+
}
|
|
2494
|
+
|
|
2495
|
+
for (auto &observation : ready) {
|
|
2496
|
+
scheduleObservation(std::move(observation), mountedRevision);
|
|
2497
|
+
}
|
|
2498
|
+
} catch (...) {
|
|
2499
|
+
// Mount hooks are noexcept. A later mount can still satisfy any pending
|
|
2500
|
+
// observation if Fabric was temporarily unavailable during teardown.
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
|
|
2504
|
+
private:
|
|
2505
|
+
void scheduleObservation(
|
|
2506
|
+
std::shared_ptr<const CommitMountObservation> observation,
|
|
2507
|
+
int64_t mountedRevision) noexcept {
|
|
2508
|
+
const auto mountedAt = wallClockMilliseconds();
|
|
2509
|
+
const auto mountedMonotonic = monotonicMilliseconds();
|
|
2510
|
+
const auto mountLatency = std::max(
|
|
2511
|
+
0.0,
|
|
2512
|
+
mountedMonotonic - observation->commitStartedMonotonic);
|
|
2513
|
+
runtimeScheduler_->scheduleWork(
|
|
2514
|
+
[runtimeState = runtimeState_,
|
|
2515
|
+
observation,
|
|
2516
|
+
mountedRevision,
|
|
2517
|
+
mountedAt,
|
|
2518
|
+
mountLatency](Runtime &runtime) {
|
|
2519
|
+
auto state = runtimeState.lock();
|
|
2520
|
+
if (!state) return;
|
|
2521
|
+
state->reclaimMountedInstances(runtime, mountedRevision);
|
|
2522
|
+
if (!observation->notifyLifecycle || !state->active() ||
|
|
2523
|
+
!state->hasCommitLifecycleHandler()) {
|
|
2524
|
+
return;
|
|
2525
|
+
}
|
|
2526
|
+
auto handlerValue = state->commitLifecycleHandler(runtime);
|
|
2527
|
+
if (!handlerValue.isObject()) return;
|
|
2528
|
+
auto handlerObject = handlerValue.getObject(runtime);
|
|
2529
|
+
if (!handlerObject.isFunction(runtime)) return;
|
|
2530
|
+
|
|
2531
|
+
Object event(runtime);
|
|
2532
|
+
event.setProperty(
|
|
2533
|
+
runtime,
|
|
2534
|
+
"type",
|
|
2535
|
+
String::createFromAscii(runtime, "commit-mounted"));
|
|
2536
|
+
event.setProperty(
|
|
2537
|
+
runtime,
|
|
2538
|
+
"surface",
|
|
2539
|
+
static_cast<double>(observation->surface));
|
|
2540
|
+
event.setProperty(
|
|
2541
|
+
runtime,
|
|
2542
|
+
"sequence",
|
|
2543
|
+
static_cast<double>(observation->sequence));
|
|
2544
|
+
event.setProperty(
|
|
2545
|
+
runtime,
|
|
2546
|
+
"hostRevision",
|
|
2547
|
+
static_cast<double>(observation->fabricRevision));
|
|
2548
|
+
event.setProperty(
|
|
2549
|
+
runtime, "commitStartedAt", observation->commitStartedAt);
|
|
2550
|
+
event.setProperty(runtime, "mountedAt", mountedAt);
|
|
2551
|
+
event.setProperty(runtime, "mountLatency", mountLatency);
|
|
2552
|
+
if (observation->causalOperationId.has_value()) {
|
|
2553
|
+
Object causalContext(runtime);
|
|
2554
|
+
causalContext.setProperty(
|
|
2555
|
+
runtime,
|
|
2556
|
+
"operationId",
|
|
2557
|
+
String::createFromUtf8(
|
|
2558
|
+
runtime, *observation->causalOperationId));
|
|
2559
|
+
event.setProperty(
|
|
2560
|
+
runtime, "causalContext", std::move(causalContext));
|
|
2561
|
+
}
|
|
2562
|
+
handlerObject.getFunction(runtime).call(runtime, event);
|
|
2563
|
+
});
|
|
2564
|
+
if (observation->notifyLifecycle) {
|
|
2565
|
+
const auto token = retainFrameObservation(
|
|
2566
|
+
std::make_shared<const CommitFrameObservation>(
|
|
2567
|
+
CommitFrameObservation{
|
|
2568
|
+
.runtimeScheduler = runtimeScheduler_,
|
|
2569
|
+
.runtimeState = runtimeState_,
|
|
2570
|
+
.commit = observation,
|
|
2571
|
+
.mountedAt = mountedAt,
|
|
2572
|
+
.mountedMonotonic = mountedMonotonic,
|
|
2573
|
+
}));
|
|
2574
|
+
try {
|
|
2575
|
+
requestJavaCommitFrame(token);
|
|
2576
|
+
} catch (...) {
|
|
2577
|
+
discardFrameObservation(token);
|
|
2578
|
+
}
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
|
|
2582
|
+
std::mutex registrationMutex_;
|
|
2583
|
+
std::mutex mutex_;
|
|
2584
|
+
facebook::react::UIManager *manager_{nullptr};
|
|
2585
|
+
int32_t surface_{0};
|
|
2586
|
+
int64_t latestMountedRevision_{0};
|
|
2587
|
+
bool attached_{false};
|
|
2588
|
+
std::vector<std::shared_ptr<const CommitMountObservation>> pending_;
|
|
2589
|
+
std::shared_ptr<facebook::react::RuntimeScheduler> runtimeScheduler_;
|
|
2590
|
+
std::weak_ptr<RuntimeState> runtimeState_;
|
|
2591
|
+
};
|
|
2592
|
+
|
|
2593
|
+
struct PreparedNodeIdentities final {
|
|
2594
|
+
NodeIdentityMap identities;
|
|
2595
|
+
std::vector<std::pair<int64_t, Value>> retainedValues;
|
|
2596
|
+
};
|
|
2597
|
+
|
|
2598
|
+
class IdentityRetention final {
|
|
2599
|
+
public:
|
|
2600
|
+
IdentityRetention(
|
|
2601
|
+
Runtime &runtime,
|
|
2602
|
+
const std::shared_ptr<RuntimeState> &state,
|
|
2603
|
+
PreparedNodeIdentities &prepared)
|
|
2604
|
+
: runtime_(runtime), state_(state) {
|
|
2605
|
+
handles_.reserve(prepared.retainedValues.size());
|
|
2606
|
+
try {
|
|
2607
|
+
for (auto &[handle, value] : prepared.retainedValues) {
|
|
2608
|
+
if (state_->hasInstance(runtime_, handle)) {
|
|
2609
|
+
throw std::invalid_argument(
|
|
2610
|
+
"Node " + std::to_string(handle) +
|
|
2611
|
+
" already owns a JavaScript instance identity.");
|
|
2612
|
+
}
|
|
2613
|
+
state_->retainInstance(runtime_, handle, std::move(value));
|
|
2614
|
+
handles_.push_back(handle);
|
|
2615
|
+
}
|
|
2616
|
+
} catch (...) {
|
|
2617
|
+
rollback();
|
|
2618
|
+
throw;
|
|
2619
|
+
}
|
|
2620
|
+
}
|
|
2621
|
+
|
|
2622
|
+
~IdentityRetention() {
|
|
2623
|
+
if (!committed_) rollback();
|
|
2624
|
+
}
|
|
2625
|
+
|
|
2626
|
+
void commit() {
|
|
2627
|
+
committed_ = true;
|
|
2628
|
+
}
|
|
2629
|
+
|
|
2630
|
+
private:
|
|
2631
|
+
void rollback() {
|
|
2632
|
+
for (const auto handle : handles_) {
|
|
2633
|
+
state_->releaseInstance(runtime_, handle);
|
|
2634
|
+
}
|
|
2635
|
+
handles_.clear();
|
|
2636
|
+
}
|
|
2637
|
+
|
|
2638
|
+
Runtime &runtime_;
|
|
2639
|
+
std::shared_ptr<RuntimeState> state_;
|
|
2640
|
+
std::vector<int64_t> handles_;
|
|
2641
|
+
bool committed_{false};
|
|
2642
|
+
};
|
|
2643
|
+
|
|
2644
|
+
PreparedNodeIdentities prepareNodeIdentities(
|
|
2645
|
+
Runtime &runtime,
|
|
2646
|
+
const folly::dynamic &transaction,
|
|
2647
|
+
int64_t surface,
|
|
2648
|
+
const std::shared_ptr<RuntimeState> &state) {
|
|
2649
|
+
PreparedNodeIdentities prepared;
|
|
2650
|
+
if (!transaction.isObject() || transaction.count("mutations") == 0) {
|
|
2651
|
+
return prepared;
|
|
2652
|
+
}
|
|
2653
|
+
const auto &mutations = transaction.at("mutations");
|
|
2654
|
+
if (!mutations.isArray()) return prepared;
|
|
2655
|
+
|
|
2656
|
+
auto objectConstructor =
|
|
2657
|
+
runtime.global().getPropertyAsObject(runtime, "Object");
|
|
2658
|
+
auto freeze = objectConstructor.getPropertyAsFunction(runtime, "freeze");
|
|
2659
|
+
for (const auto &mutation : mutations) {
|
|
2660
|
+
if (!mutation.isObject() || mutation.count("type") == 0 ||
|
|
2661
|
+
!mutation.at("type").isString()) {
|
|
2662
|
+
continue;
|
|
2663
|
+
}
|
|
2664
|
+
const auto &type = mutation.at("type").getString();
|
|
2665
|
+
if (type != "create-element" && type != "create-text") continue;
|
|
2666
|
+
if (mutation.count("node") == 0) continue;
|
|
2667
|
+
const auto node = dynamicInteger(mutation.at("node"));
|
|
2668
|
+
if (node <= 0 || prepared.identities.contains(node)) continue;
|
|
2669
|
+
if (state->hasInstance(runtime, node)) {
|
|
2670
|
+
throw std::invalid_argument(
|
|
2671
|
+
"Node " + std::to_string(node) +
|
|
2672
|
+
" cannot reuse a retained JavaScript instance identity.");
|
|
2673
|
+
}
|
|
2674
|
+
|
|
2675
|
+
Object instance(runtime);
|
|
2676
|
+
instance.setProperty(runtime, "surface", static_cast<double>(surface));
|
|
2677
|
+
instance.setProperty(runtime, "node", static_cast<double>(node));
|
|
2678
|
+
freeze.call(runtime, instance);
|
|
2679
|
+
|
|
2680
|
+
const auto tag = TransactionCoordinator::allocateFabricTag();
|
|
2681
|
+
Value retainedValue(runtime, instance);
|
|
2682
|
+
auto instanceHandle = std::make_shared<facebook::react::InstanceHandle>(
|
|
2683
|
+
runtime, retainedValue, tag);
|
|
2684
|
+
prepared.identities.emplace(
|
|
2685
|
+
node, NodeIdentity{.tag = tag, .instanceHandle = instanceHandle});
|
|
2686
|
+
prepared.retainedValues.emplace_back(
|
|
2687
|
+
node, Value(runtime, retainedValue));
|
|
2688
|
+
}
|
|
2689
|
+
return prepared;
|
|
2690
|
+
}
|
|
2691
|
+
|
|
2692
|
+
std::vector<int64_t> deletedNodeHandles(
|
|
2693
|
+
const folly::dynamic &transaction) {
|
|
2694
|
+
std::vector<int64_t> handles;
|
|
2695
|
+
if (!transaction.isObject() || transaction.count("mutations") == 0) {
|
|
2696
|
+
return handles;
|
|
2697
|
+
}
|
|
2698
|
+
const auto &mutations = transaction.at("mutations");
|
|
2699
|
+
if (!mutations.isArray()) return handles;
|
|
2700
|
+
for (const auto &mutation : mutations) {
|
|
2701
|
+
if (!mutation.isObject() || mutation.count("type") == 0 ||
|
|
2702
|
+
!mutation.at("type").isString() ||
|
|
2703
|
+
mutation.at("type").getString() != "delete-node" ||
|
|
2704
|
+
mutation.count("node") == 0) {
|
|
2705
|
+
continue;
|
|
2706
|
+
}
|
|
2707
|
+
const auto node = dynamicInteger(mutation.at("node"));
|
|
2708
|
+
if (node > 0) handles.push_back(node);
|
|
2709
|
+
}
|
|
2710
|
+
return handles;
|
|
2711
|
+
}
|
|
2712
|
+
|
|
2713
|
+
std::optional<std::string> causalOperationId(
|
|
2714
|
+
const folly::dynamic &transaction) {
|
|
2715
|
+
if (!transaction.isObject() || transaction.count("causalContext") == 0) {
|
|
2716
|
+
return std::nullopt;
|
|
2717
|
+
}
|
|
2718
|
+
const auto &context = transaction.at("causalContext");
|
|
2719
|
+
if (!context.isObject() || context.count("operationId") == 0 ||
|
|
2720
|
+
!context.at("operationId").isString()) {
|
|
2721
|
+
return std::nullopt;
|
|
2722
|
+
}
|
|
2723
|
+
return context.at("operationId").getString();
|
|
2724
|
+
}
|
|
2725
|
+
|
|
2726
|
+
Object measurementObject(Runtime &runtime, const NodeMeasurement &measurement) {
|
|
2727
|
+
Object object(runtime);
|
|
2728
|
+
object.setProperty(runtime, "x", measurement.x);
|
|
2729
|
+
object.setProperty(runtime, "y", measurement.y);
|
|
2730
|
+
object.setProperty(runtime, "width", measurement.width);
|
|
2731
|
+
object.setProperty(runtime, "height", measurement.height);
|
|
2732
|
+
object.setProperty(runtime, "pageX", measurement.pageX);
|
|
2733
|
+
object.setProperty(runtime, "pageY", measurement.pageY);
|
|
2734
|
+
object.setProperty(
|
|
2735
|
+
runtime,
|
|
2736
|
+
"observedSequence",
|
|
2737
|
+
static_cast<double>(measurement.observedSequence));
|
|
2738
|
+
return object;
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
struct SurfaceSnapshot final {
|
|
2742
|
+
int32_t surface{0};
|
|
2743
|
+
std::shared_ptr<TransactionCoordinator> coordinator;
|
|
2744
|
+
std::shared_ptr<MountObservationState> mountObserver;
|
|
2745
|
+
};
|
|
2746
|
+
|
|
2747
|
+
class BindingState final {
|
|
2748
|
+
public:
|
|
2749
|
+
uint64_t installRuntimeState(
|
|
2750
|
+
const std::shared_ptr<RuntimeState> &runtimeState) {
|
|
2751
|
+
std::shared_ptr<RuntimeState> previous;
|
|
2752
|
+
std::shared_ptr<MountObservationState> previousMountObserver;
|
|
2753
|
+
std::shared_ptr<TransactionCoordinator> previousCoordinator;
|
|
2754
|
+
uint64_t generation;
|
|
2755
|
+
{
|
|
2756
|
+
std::lock_guard lock(mutex_);
|
|
2757
|
+
previous = runtimeState_.lock();
|
|
2758
|
+
if (previous && previous != runtimeState) {
|
|
2759
|
+
previousCoordinator = coordinator_;
|
|
2760
|
+
previousMountObserver = std::move(mountObserver_);
|
|
2761
|
+
}
|
|
2762
|
+
generation = ++runtimeGeneration_;
|
|
2763
|
+
runtimeState_ = runtimeState;
|
|
2764
|
+
}
|
|
2765
|
+
if (previousMountObserver) previousMountObserver->detach();
|
|
2766
|
+
if (previous && previous != runtimeState) {
|
|
2767
|
+
previous->deactivateNativeWork();
|
|
2768
|
+
if (previousCoordinator) previousCoordinator->reset();
|
|
2769
|
+
}
|
|
2770
|
+
return generation;
|
|
2771
|
+
}
|
|
2772
|
+
|
|
2773
|
+
void retireRuntime(uint64_t generation) {
|
|
2774
|
+
std::shared_ptr<MountObservationState> mountObserver;
|
|
2775
|
+
std::shared_ptr<TransactionCoordinator> coordinator;
|
|
2776
|
+
{
|
|
2777
|
+
std::lock_guard lock(mutex_);
|
|
2778
|
+
if (generation != runtimeGeneration_) return;
|
|
2779
|
+
runtimeState_.reset();
|
|
2780
|
+
mountObserver = std::move(mountObserver_);
|
|
2781
|
+
coordinator = coordinator_;
|
|
2782
|
+
}
|
|
2783
|
+
if (mountObserver) mountObserver->detach();
|
|
2784
|
+
// The logical tree owns JSI-backed Fabric InstanceHandles. Release it
|
|
2785
|
+
// from the retiring runtime's NativeState destructor, while Hermes is
|
|
2786
|
+
// still alive, rather than waiting for the replacement runtime install.
|
|
2787
|
+
if (coordinator) coordinator->reset();
|
|
2788
|
+
}
|
|
2789
|
+
|
|
2790
|
+
void publishSurface(int32_t surface) {
|
|
2791
|
+
if (surface <= 0) {
|
|
2792
|
+
throw std::invalid_argument(
|
|
2793
|
+
"A published Fabric surface must be positive.");
|
|
2794
|
+
}
|
|
2795
|
+
std::lock_guard lock(mutex_);
|
|
2796
|
+
if (surface_ == surface && coordinator_) return;
|
|
2797
|
+
if (surface_ != 0) {
|
|
2798
|
+
throw std::logic_error(
|
|
2799
|
+
"Solid Native already owns a different Fabric surface.");
|
|
2800
|
+
}
|
|
2801
|
+
surface_ = surface;
|
|
2802
|
+
coordinator_ = std::make_shared<TransactionCoordinator>(surface);
|
|
2803
|
+
}
|
|
2804
|
+
|
|
2805
|
+
void clearSurface(int32_t surface) {
|
|
2806
|
+
std::shared_ptr<MountObservationState> mountObserver;
|
|
2807
|
+
std::shared_ptr<RuntimeState> runtimeState;
|
|
2808
|
+
{
|
|
2809
|
+
std::lock_guard lock(mutex_);
|
|
2810
|
+
if (surface_ != surface) return;
|
|
2811
|
+
coordinator_.reset();
|
|
2812
|
+
surface_ = 0;
|
|
2813
|
+
mountObserver = mountObserver_;
|
|
2814
|
+
runtimeState = runtimeState_.lock();
|
|
2815
|
+
}
|
|
2816
|
+
if (mountObserver) mountObserver->detach();
|
|
2817
|
+
if (runtimeState) runtimeState->deactivateNativeWork();
|
|
2818
|
+
}
|
|
2819
|
+
|
|
2820
|
+
void deactivateSurface(int32_t surface) {
|
|
2821
|
+
std::shared_ptr<MountObservationState> mountObserver;
|
|
2822
|
+
std::shared_ptr<RuntimeState> runtimeState;
|
|
2823
|
+
{
|
|
2824
|
+
std::lock_guard lock(mutex_);
|
|
2825
|
+
if (surface_ != surface) return;
|
|
2826
|
+
mountObserver = mountObserver_;
|
|
2827
|
+
runtimeState = runtimeState_.lock();
|
|
2828
|
+
}
|
|
2829
|
+
if (mountObserver) mountObserver->detach();
|
|
2830
|
+
if (runtimeState) runtimeState->deactivateNativeWork();
|
|
2831
|
+
}
|
|
2832
|
+
|
|
2833
|
+
void installMountObserver(
|
|
2834
|
+
std::shared_ptr<MountObservationState> mountObserver) {
|
|
2835
|
+
std::shared_ptr<MountObservationState> previous;
|
|
2836
|
+
{
|
|
2837
|
+
std::lock_guard lock(mutex_);
|
|
2838
|
+
previous = std::exchange(mountObserver_, std::move(mountObserver));
|
|
2839
|
+
}
|
|
2840
|
+
if (previous) previous->detach();
|
|
2841
|
+
}
|
|
2842
|
+
|
|
2843
|
+
SurfaceSnapshot snapshot() const {
|
|
2844
|
+
std::lock_guard lock(mutex_);
|
|
2845
|
+
return {
|
|
2846
|
+
.surface = surface_,
|
|
2847
|
+
.coordinator = coordinator_,
|
|
2848
|
+
.mountObserver = mountObserver_,
|
|
2849
|
+
};
|
|
2850
|
+
}
|
|
2851
|
+
|
|
2852
|
+
private:
|
|
2853
|
+
mutable std::mutex mutex_;
|
|
2854
|
+
int32_t surface_{0};
|
|
2855
|
+
std::shared_ptr<TransactionCoordinator> coordinator_;
|
|
2856
|
+
std::shared_ptr<MountObservationState> mountObserver_;
|
|
2857
|
+
std::weak_ptr<RuntimeState> runtimeState_;
|
|
2858
|
+
uint64_t runtimeGeneration_{0};
|
|
2859
|
+
};
|
|
2860
|
+
|
|
2861
|
+
SurfaceSnapshot requireSurface(
|
|
2862
|
+
Runtime &runtime,
|
|
2863
|
+
const std::shared_ptr<BindingState> &state) {
|
|
2864
|
+
auto snapshot = state->snapshot();
|
|
2865
|
+
if (snapshot.surface <= 0 || !snapshot.coordinator) {
|
|
2866
|
+
throw JSError(runtime, "The Solid Native Fabric surface is not ready.");
|
|
2867
|
+
}
|
|
2868
|
+
return snapshot;
|
|
2869
|
+
}
|
|
2870
|
+
|
|
2871
|
+
void requestJavaSurfaceStop(int32_t surface) {
|
|
2872
|
+
facebook::jni::ThreadScope::WithClassLoader([surface] {
|
|
2873
|
+
static auto javaClass = facebook::jni::findClassStatic(
|
|
2874
|
+
"dev/solidnative/runtime/SolidNativeBindingsInstaller");
|
|
2875
|
+
static auto requestStop =
|
|
2876
|
+
javaClass->getStaticMethod<void(jint)>("requestSurfaceStop");
|
|
2877
|
+
requestStop(javaClass, surface);
|
|
2878
|
+
});
|
|
2879
|
+
}
|
|
2880
|
+
|
|
2881
|
+
void requestJavaFatalError(
|
|
2882
|
+
int32_t surface,
|
|
2883
|
+
const std::string &name,
|
|
2884
|
+
const std::string &message) {
|
|
2885
|
+
facebook::jni::ThreadScope::WithClassLoader([surface, name, message] {
|
|
2886
|
+
static auto javaClass = facebook::jni::findClassStatic(
|
|
2887
|
+
"dev/solidnative/runtime/SolidNativeBindingsInstaller");
|
|
2888
|
+
static auto requestFatal = javaClass->getStaticMethod<void(
|
|
2889
|
+
jint,
|
|
2890
|
+
jstring,
|
|
2891
|
+
jstring)>("requestFatalError");
|
|
2892
|
+
auto javaName = facebook::jni::make_jstring(name);
|
|
2893
|
+
auto javaMessage = facebook::jni::make_jstring(message);
|
|
2894
|
+
requestFatal(
|
|
2895
|
+
javaClass, surface, javaName.get(), javaMessage.get());
|
|
2896
|
+
});
|
|
2897
|
+
}
|
|
2898
|
+
|
|
2899
|
+
void requestJavaCommitFrame(int64_t token) {
|
|
2900
|
+
facebook::jni::ThreadScope::WithClassLoader([token] {
|
|
2901
|
+
static auto javaClass = facebook::jni::findClassStatic(
|
|
2902
|
+
"dev/solidnative/runtime/SolidNativeBindingsInstaller");
|
|
2903
|
+
static auto requestFrame =
|
|
2904
|
+
javaClass->getStaticMethod<void(jlong)>("requestCommitFrame");
|
|
2905
|
+
requestFrame(javaClass, token);
|
|
2906
|
+
});
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
void requestJavaUIWorkletFrame(int64_t token) {
|
|
2910
|
+
facebook::jni::ThreadScope::WithClassLoader([token] {
|
|
2911
|
+
static auto javaClass = facebook::jni::findClassStatic(
|
|
2912
|
+
"dev/solidnative/runtime/SolidNativeBindingsInstaller");
|
|
2913
|
+
static auto requestFrame =
|
|
2914
|
+
javaClass->getStaticMethod<void(jlong)>("requestUIWorkletFrame");
|
|
2915
|
+
requestFrame(javaClass, token);
|
|
2916
|
+
});
|
|
2917
|
+
}
|
|
2918
|
+
|
|
2919
|
+
bool attachJavaUIWorkletPan(
|
|
2920
|
+
int64_t token,
|
|
2921
|
+
facebook::react::Tag targetTag) {
|
|
2922
|
+
bool attached = false;
|
|
2923
|
+
facebook::jni::ThreadScope::WithClassLoader([token, targetTag, &attached] {
|
|
2924
|
+
static auto javaClass = facebook::jni::findClassStatic(
|
|
2925
|
+
"dev/solidnative/runtime/SolidNativeBindingsInstaller");
|
|
2926
|
+
static auto attach = javaClass->getStaticMethod<jboolean(jlong, jint)>(
|
|
2927
|
+
"attachUIWorkletPanGesture");
|
|
2928
|
+
attached = attach(javaClass, token, targetTag);
|
|
2929
|
+
});
|
|
2930
|
+
return attached;
|
|
2931
|
+
}
|
|
2932
|
+
|
|
2933
|
+
void detachJavaUIWorkletPan(int64_t token) noexcept {
|
|
2934
|
+
try {
|
|
2935
|
+
facebook::jni::ThreadScope::WithClassLoader([token] {
|
|
2936
|
+
static auto javaClass = facebook::jni::findClassStatic(
|
|
2937
|
+
"dev/solidnative/runtime/SolidNativeBindingsInstaller");
|
|
2938
|
+
static auto detach = javaClass->getStaticMethod<void(jlong)>(
|
|
2939
|
+
"detachUIWorkletPanGesture");
|
|
2940
|
+
detach(javaClass, token);
|
|
2941
|
+
});
|
|
2942
|
+
} catch (...) {
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
|
|
2946
|
+
bool applyJavaUIWorkletFrame(
|
|
2947
|
+
int64_t handle,
|
|
2948
|
+
facebook::react::Tag targetTag,
|
|
2949
|
+
int64_t sequence,
|
|
2950
|
+
int64_t frameTimeNanoseconds,
|
|
2951
|
+
const std::array<double, AndroidViewOutputCount> &values) {
|
|
2952
|
+
bool applied = false;
|
|
2953
|
+
facebook::jni::ThreadScope::WithClassLoader(
|
|
2954
|
+
[handle,
|
|
2955
|
+
targetTag,
|
|
2956
|
+
sequence,
|
|
2957
|
+
frameTimeNanoseconds,
|
|
2958
|
+
&values,
|
|
2959
|
+
&applied] {
|
|
2960
|
+
static auto javaClass = facebook::jni::findClassStatic(
|
|
2961
|
+
"dev/solidnative/runtime/SolidNativeBindingsInstaller");
|
|
2962
|
+
static auto applyFrame = javaClass->getStaticMethod<jboolean(
|
|
2963
|
+
jlong,
|
|
2964
|
+
jint,
|
|
2965
|
+
jlong,
|
|
2966
|
+
jlong,
|
|
2967
|
+
jdouble,
|
|
2968
|
+
jdouble,
|
|
2969
|
+
jdouble,
|
|
2970
|
+
jdouble,
|
|
2971
|
+
jdouble,
|
|
2972
|
+
jdouble)>("applyUIWorkletFrame");
|
|
2973
|
+
applied = applyFrame(
|
|
2974
|
+
javaClass,
|
|
2975
|
+
handle,
|
|
2976
|
+
targetTag,
|
|
2977
|
+
sequence,
|
|
2978
|
+
frameTimeNanoseconds,
|
|
2979
|
+
values.at(0),
|
|
2980
|
+
values.at(1),
|
|
2981
|
+
values.at(2),
|
|
2982
|
+
values.at(3),
|
|
2983
|
+
values.at(4),
|
|
2984
|
+
values.at(5));
|
|
2985
|
+
});
|
|
2986
|
+
return applied;
|
|
2987
|
+
}
|
|
2988
|
+
|
|
2989
|
+
class SolidNativeBindingsInstaller final
|
|
2990
|
+
: public facebook::jni::HybridClass<
|
|
2991
|
+
SolidNativeBindingsInstaller,
|
|
2992
|
+
facebook::react::JBindingsInstaller> {
|
|
2993
|
+
public:
|
|
2994
|
+
static constexpr auto kJavaDescriptor =
|
|
2995
|
+
"Ldev/solidnative/runtime/SolidNativeBindingsInstaller;";
|
|
2996
|
+
|
|
2997
|
+
static facebook::jni::local_ref<jhybriddata> initHybrid(
|
|
2998
|
+
facebook::jni::alias_ref<jclass>) {
|
|
2999
|
+
return makeCxxInstance();
|
|
3000
|
+
}
|
|
3001
|
+
|
|
3002
|
+
static void registerNatives() {
|
|
3003
|
+
registerHybrid({
|
|
3004
|
+
makeNativeMethod("initHybrid", SolidNativeBindingsInstaller::initHybrid),
|
|
3005
|
+
makeNativeMethod(
|
|
3006
|
+
"publishSurface", SolidNativeBindingsInstaller::publishSurface),
|
|
3007
|
+
makeNativeMethod(
|
|
3008
|
+
"clearSurface", SolidNativeBindingsInstaller::clearSurface),
|
|
3009
|
+
makeNativeMethod(
|
|
3010
|
+
"deactivateSurface",
|
|
3011
|
+
SolidNativeBindingsInstaller::deactivateSurface),
|
|
3012
|
+
});
|
|
3013
|
+
javaClassLocal()->registerNatives({
|
|
3014
|
+
makeNativeMethod("onCommitFrame", onJavaCommitFrame),
|
|
3015
|
+
makeNativeMethod("onUIWorkletFrame", onJavaUIWorkletFrame),
|
|
3016
|
+
makeNativeMethod("onUIWorkletPan", onJavaUIWorkletPan),
|
|
3017
|
+
});
|
|
3018
|
+
}
|
|
3019
|
+
|
|
3020
|
+
facebook::react::ReactInstance::BindingsInstallFunc getBindingsInstallFunc()
|
|
3021
|
+
override {
|
|
3022
|
+
auto bindingState = bindingState_;
|
|
3023
|
+
return [bindingState = std::move(bindingState)](Runtime &runtime) {
|
|
3024
|
+
const auto existing =
|
|
3025
|
+
runtime.global().getProperty(runtime, GlobalBindingName);
|
|
3026
|
+
if (!existing.isUndefined()) {
|
|
3027
|
+
throw JSError(
|
|
3028
|
+
runtime,
|
|
3029
|
+
"Solid Native cannot replace an existing __solidNativeHost "
|
|
3030
|
+
"binding.");
|
|
3031
|
+
}
|
|
3032
|
+
installEmptySurfaceStopHandler(runtime);
|
|
3033
|
+
|
|
3034
|
+
Object host(runtime);
|
|
3035
|
+
Object runtimeStorage(runtime);
|
|
3036
|
+
runtimeStorage.setProperty(runtime, "instances", Object(runtime));
|
|
3037
|
+
runtimeStorage.setProperty(runtime, "resources", Object(runtime));
|
|
3038
|
+
auto routing = std::make_shared<EventRoutingState>();
|
|
3039
|
+
auto runtimeState = std::make_shared<RuntimeState>(
|
|
3040
|
+
runtime, runtimeStorage, routing);
|
|
3041
|
+
const auto runtimeGeneration =
|
|
3042
|
+
bindingState->installRuntimeState(runtimeState);
|
|
3043
|
+
std::weak_ptr<BindingState> weakBindingState = bindingState;
|
|
3044
|
+
runtimeState->setRuntimeRetirementHandler(
|
|
3045
|
+
[weakBindingState, runtimeGeneration] {
|
|
3046
|
+
auto state = weakBindingState.lock();
|
|
3047
|
+
if (state) state->retireRuntime(runtimeGeneration);
|
|
3048
|
+
});
|
|
3049
|
+
std::weak_ptr<RuntimeState> weakRuntimeState = runtimeState;
|
|
3050
|
+
host.setNativeState(runtime, runtimeState);
|
|
3051
|
+
|
|
3052
|
+
auto objectConstructor =
|
|
3053
|
+
runtime.global().getPropertyAsObject(runtime, "Object");
|
|
3054
|
+
auto defineProperty =
|
|
3055
|
+
objectConstructor.getPropertyAsFunction(runtime, "defineProperty");
|
|
3056
|
+
Object runtimeStorageDescriptor(runtime);
|
|
3057
|
+
runtimeStorageDescriptor.setProperty(runtime, "value", runtimeStorage);
|
|
3058
|
+
runtimeStorageDescriptor.setProperty(runtime, "writable", false);
|
|
3059
|
+
runtimeStorageDescriptor.setProperty(runtime, "configurable", false);
|
|
3060
|
+
runtimeStorageDescriptor.setProperty(runtime, "enumerable", false);
|
|
3061
|
+
defineProperty.call(
|
|
3062
|
+
runtime,
|
|
3063
|
+
host,
|
|
3064
|
+
String::createFromAscii(runtime, "__runtimeStorage"),
|
|
3065
|
+
runtimeStorageDescriptor);
|
|
3066
|
+
|
|
3067
|
+
host.setProperty(runtime, "contractVersion", HostContractVersion);
|
|
3068
|
+
host.setProperty(runtime, "backend", "react-native-fabric");
|
|
3069
|
+
host.setProperty(runtime, "backendVersion", ReactNativeVersion);
|
|
3070
|
+
host.setProperty(runtime, "platform", "android");
|
|
3071
|
+
|
|
3072
|
+
host.setProperty(
|
|
3073
|
+
runtime,
|
|
3074
|
+
"getSurfaceInfo",
|
|
3075
|
+
Function::createFromHostFunction(
|
|
3076
|
+
runtime,
|
|
3077
|
+
PropNameID::forAscii(runtime, "getSurfaceInfo"),
|
|
3078
|
+
0,
|
|
3079
|
+
[bindingState, weakRuntimeState](
|
|
3080
|
+
Runtime &runtime,
|
|
3081
|
+
const Value &,
|
|
3082
|
+
const Value *,
|
|
3083
|
+
size_t count) -> Value {
|
|
3084
|
+
if (count != 0) {
|
|
3085
|
+
throw JSError(
|
|
3086
|
+
runtime,
|
|
3087
|
+
"getSurfaceInfo does not accept arguments.");
|
|
3088
|
+
}
|
|
3089
|
+
auto state = weakRuntimeState.lock();
|
|
3090
|
+
if (!state) {
|
|
3091
|
+
throw JSError(
|
|
3092
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3093
|
+
}
|
|
3094
|
+
const auto snapshot = bindingState->snapshot();
|
|
3095
|
+
Object info(runtime);
|
|
3096
|
+
info.setProperty(runtime, "ready", snapshot.surface > 0);
|
|
3097
|
+
info.setProperty(
|
|
3098
|
+
runtime, "surface", static_cast<double>(snapshot.surface));
|
|
3099
|
+
info.setProperty(
|
|
3100
|
+
runtime,
|
|
3101
|
+
"sequence",
|
|
3102
|
+
snapshot.coordinator
|
|
3103
|
+
? static_cast<double>(
|
|
3104
|
+
snapshot.coordinator->lastSequence())
|
|
3105
|
+
: 0.0);
|
|
3106
|
+
info.setProperty(
|
|
3107
|
+
runtime,
|
|
3108
|
+
"retainedNodeCount",
|
|
3109
|
+
static_cast<double>(state->retainedNodeCount()));
|
|
3110
|
+
info.setProperty(
|
|
3111
|
+
runtime,
|
|
3112
|
+
"retainedResourceCount",
|
|
3113
|
+
static_cast<double>(state->retainedResourceCount()));
|
|
3114
|
+
const auto uiWorkletActivity =
|
|
3115
|
+
state->uiWorkletActivityCounts();
|
|
3116
|
+
info.setProperty(
|
|
3117
|
+
runtime,
|
|
3118
|
+
"activeUIWorkletCount",
|
|
3119
|
+
static_cast<double>(uiWorkletActivity.first));
|
|
3120
|
+
info.setProperty(
|
|
3121
|
+
runtime,
|
|
3122
|
+
"pendingUIWorkletFrameCount",
|
|
3123
|
+
static_cast<double>(uiWorkletActivity.second));
|
|
3124
|
+
return info;
|
|
3125
|
+
}));
|
|
3126
|
+
|
|
3127
|
+
host.setProperty(
|
|
3128
|
+
runtime,
|
|
3129
|
+
"setEventHandler",
|
|
3130
|
+
Function::createFromHostFunction(
|
|
3131
|
+
runtime,
|
|
3132
|
+
PropNameID::forAscii(runtime, "setEventHandler"),
|
|
3133
|
+
1,
|
|
3134
|
+
[weakRuntimeState](
|
|
3135
|
+
Runtime &runtime,
|
|
3136
|
+
const Value &,
|
|
3137
|
+
const Value *arguments,
|
|
3138
|
+
size_t count) -> Value {
|
|
3139
|
+
if (count != 1 ||
|
|
3140
|
+
(!arguments[0].isNull() &&
|
|
3141
|
+
(!arguments[0].isObject() ||
|
|
3142
|
+
!arguments[0]
|
|
3143
|
+
.getObject(runtime)
|
|
3144
|
+
.isFunction(runtime)))) {
|
|
3145
|
+
throw JSError(
|
|
3146
|
+
runtime,
|
|
3147
|
+
"setEventHandler requires exactly one function or "
|
|
3148
|
+
"null.");
|
|
3149
|
+
}
|
|
3150
|
+
auto state = weakRuntimeState.lock();
|
|
3151
|
+
if (!state) {
|
|
3152
|
+
throw JSError(
|
|
3153
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3154
|
+
}
|
|
3155
|
+
if (arguments[0].isNull()) {
|
|
3156
|
+
const Value empty;
|
|
3157
|
+
state->setEventHandler(runtime, empty);
|
|
3158
|
+
} else {
|
|
3159
|
+
state->setEventHandler(runtime, arguments[0]);
|
|
3160
|
+
}
|
|
3161
|
+
return Value();
|
|
3162
|
+
}));
|
|
3163
|
+
|
|
3164
|
+
host.setProperty(
|
|
3165
|
+
runtime,
|
|
3166
|
+
"setCommitLifecycleHandler",
|
|
3167
|
+
Function::createFromHostFunction(
|
|
3168
|
+
runtime,
|
|
3169
|
+
PropNameID::forAscii(runtime, "setCommitLifecycleHandler"),
|
|
3170
|
+
1,
|
|
3171
|
+
[weakRuntimeState](
|
|
3172
|
+
Runtime &runtime,
|
|
3173
|
+
const Value &,
|
|
3174
|
+
const Value *arguments,
|
|
3175
|
+
size_t count) -> Value {
|
|
3176
|
+
if (count != 1 ||
|
|
3177
|
+
(!arguments[0].isNull() &&
|
|
3178
|
+
(!arguments[0].isObject() ||
|
|
3179
|
+
!arguments[0]
|
|
3180
|
+
.getObject(runtime)
|
|
3181
|
+
.isFunction(runtime)))) {
|
|
3182
|
+
throw JSError(
|
|
3183
|
+
runtime,
|
|
3184
|
+
"setCommitLifecycleHandler requires exactly one "
|
|
3185
|
+
"function or null.");
|
|
3186
|
+
}
|
|
3187
|
+
auto state = weakRuntimeState.lock();
|
|
3188
|
+
if (!state) {
|
|
3189
|
+
throw JSError(
|
|
3190
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3191
|
+
}
|
|
3192
|
+
if (arguments[0].isNull()) {
|
|
3193
|
+
const Value empty;
|
|
3194
|
+
state->setCommitLifecycleHandler(runtime, empty);
|
|
3195
|
+
} else {
|
|
3196
|
+
state->setCommitLifecycleHandler(runtime, arguments[0]);
|
|
3197
|
+
}
|
|
3198
|
+
return Value();
|
|
3199
|
+
}));
|
|
3200
|
+
|
|
3201
|
+
host.setProperty(
|
|
3202
|
+
runtime,
|
|
3203
|
+
"retainNativeResource",
|
|
3204
|
+
Function::createFromHostFunction(
|
|
3205
|
+
runtime,
|
|
3206
|
+
PropNameID::forAscii(runtime, "retainNativeResource"),
|
|
3207
|
+
2,
|
|
3208
|
+
[weakRuntimeState](
|
|
3209
|
+
Runtime &runtime,
|
|
3210
|
+
const Value &,
|
|
3211
|
+
const Value *arguments,
|
|
3212
|
+
size_t count) -> Value {
|
|
3213
|
+
if (count != 2 || !arguments[1].isObject() ||
|
|
3214
|
+
arguments[1].getObject(runtime).isFunction(runtime) ||
|
|
3215
|
+
arguments[1].getObject(runtime).isArray(runtime)) {
|
|
3216
|
+
throw JSError(
|
|
3217
|
+
runtime,
|
|
3218
|
+
"retainNativeResource requires a kind and one "
|
|
3219
|
+
"non-array object.");
|
|
3220
|
+
}
|
|
3221
|
+
auto state = weakRuntimeState.lock();
|
|
3222
|
+
if (!state) {
|
|
3223
|
+
throw JSError(
|
|
3224
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3225
|
+
}
|
|
3226
|
+
const auto kind = requireResourceKind(runtime, arguments[0]);
|
|
3227
|
+
return Value(static_cast<double>(state->retainResource(
|
|
3228
|
+
runtime, kind, arguments[1])));
|
|
3229
|
+
}));
|
|
3230
|
+
|
|
3231
|
+
host.setProperty(
|
|
3232
|
+
runtime,
|
|
3233
|
+
"releaseNativeResource",
|
|
3234
|
+
Function::createFromHostFunction(
|
|
3235
|
+
runtime,
|
|
3236
|
+
PropNameID::forAscii(runtime, "releaseNativeResource"),
|
|
3237
|
+
1,
|
|
3238
|
+
[weakRuntimeState](
|
|
3239
|
+
Runtime &runtime,
|
|
3240
|
+
const Value &,
|
|
3241
|
+
const Value *arguments,
|
|
3242
|
+
size_t count) -> Value {
|
|
3243
|
+
if (count != 1) {
|
|
3244
|
+
throw JSError(
|
|
3245
|
+
runtime,
|
|
3246
|
+
"releaseNativeResource requires exactly one handle.");
|
|
3247
|
+
}
|
|
3248
|
+
auto state = weakRuntimeState.lock();
|
|
3249
|
+
if (!state) {
|
|
3250
|
+
throw JSError(
|
|
3251
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3252
|
+
}
|
|
3253
|
+
const auto handle = requireSafeInteger(
|
|
3254
|
+
runtime,
|
|
3255
|
+
arguments[0],
|
|
3256
|
+
"The native resource handle",
|
|
3257
|
+
true);
|
|
3258
|
+
state->releaseResource(runtime, handle);
|
|
3259
|
+
return Value();
|
|
3260
|
+
}));
|
|
3261
|
+
|
|
3262
|
+
host.setProperty(
|
|
3263
|
+
runtime,
|
|
3264
|
+
"installUIWorklet",
|
|
3265
|
+
Function::createFromHostFunction(
|
|
3266
|
+
runtime,
|
|
3267
|
+
PropNameID::forAscii(runtime, "installUIWorklet"),
|
|
3268
|
+
2,
|
|
3269
|
+
[weakRuntimeState](
|
|
3270
|
+
Runtime &runtime,
|
|
3271
|
+
const Value &,
|
|
3272
|
+
const Value *arguments,
|
|
3273
|
+
size_t count) -> Value {
|
|
3274
|
+
if (count != 2 || !arguments[1].isObject()) {
|
|
3275
|
+
throw JSError(
|
|
3276
|
+
runtime,
|
|
3277
|
+
"installUIWorklet requires a target node and one graph object.");
|
|
3278
|
+
}
|
|
3279
|
+
auto state = weakRuntimeState.lock();
|
|
3280
|
+
if (!state) {
|
|
3281
|
+
throw JSError(
|
|
3282
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3283
|
+
}
|
|
3284
|
+
const auto target = requireSafeInteger(
|
|
3285
|
+
runtime,
|
|
3286
|
+
arguments[0],
|
|
3287
|
+
"The native UI worklet target",
|
|
3288
|
+
true);
|
|
3289
|
+
try {
|
|
3290
|
+
return Value(static_cast<double>(
|
|
3291
|
+
state->installUIWorklet(runtime, target, arguments[1])));
|
|
3292
|
+
} catch (const JSError &) {
|
|
3293
|
+
throw;
|
|
3294
|
+
} catch (const std::exception &error) {
|
|
3295
|
+
throw JSError(runtime, error.what());
|
|
3296
|
+
}
|
|
3297
|
+
}));
|
|
3298
|
+
|
|
3299
|
+
host.setProperty(
|
|
3300
|
+
runtime,
|
|
3301
|
+
"updateUIWorkletInputs",
|
|
3302
|
+
Function::createFromHostFunction(
|
|
3303
|
+
runtime,
|
|
3304
|
+
PropNameID::forAscii(runtime, "updateUIWorkletInputs"),
|
|
3305
|
+
3,
|
|
3306
|
+
[weakRuntimeState](
|
|
3307
|
+
Runtime &runtime,
|
|
3308
|
+
const Value &,
|
|
3309
|
+
const Value *arguments,
|
|
3310
|
+
size_t count) -> Value {
|
|
3311
|
+
if (count != 3 || !arguments[1].isObject() ||
|
|
3312
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
3313
|
+
!arguments[2].isNumber()) {
|
|
3314
|
+
throw JSError(
|
|
3315
|
+
runtime,
|
|
3316
|
+
"updateUIWorkletInputs requires a handle, input array, and timestamp.");
|
|
3317
|
+
}
|
|
3318
|
+
auto state = weakRuntimeState.lock();
|
|
3319
|
+
if (!state) {
|
|
3320
|
+
throw JSError(
|
|
3321
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3322
|
+
}
|
|
3323
|
+
const auto handle = requireSafeInteger(
|
|
3324
|
+
runtime,
|
|
3325
|
+
arguments[0],
|
|
3326
|
+
"The native UI worklet handle",
|
|
3327
|
+
true);
|
|
3328
|
+
auto array = arguments[1].getObject(runtime).getArray(runtime);
|
|
3329
|
+
if (array.size(runtime) >
|
|
3330
|
+
solid_native::worklets::MaximumUIWorkletInputs) {
|
|
3331
|
+
throw JSError(
|
|
3332
|
+
runtime, "The native UI worklet input vector is too large.");
|
|
3333
|
+
}
|
|
3334
|
+
std::vector<double> inputs;
|
|
3335
|
+
inputs.reserve(array.size(runtime));
|
|
3336
|
+
for (size_t index = 0; index < array.size(runtime); ++index) {
|
|
3337
|
+
auto value = array.getValueAtIndex(runtime, index);
|
|
3338
|
+
if (!value.isNumber() ||
|
|
3339
|
+
!std::isfinite(value.getNumber())) {
|
|
3340
|
+
throw JSError(
|
|
3341
|
+
runtime,
|
|
3342
|
+
"Native UI worklet inputs must be finite numbers.");
|
|
3343
|
+
}
|
|
3344
|
+
inputs.push_back(value.getNumber());
|
|
3345
|
+
}
|
|
3346
|
+
try {
|
|
3347
|
+
state->updateUIWorklet(
|
|
3348
|
+
handle,
|
|
3349
|
+
std::move(inputs),
|
|
3350
|
+
arguments[2].getNumber());
|
|
3351
|
+
return Value();
|
|
3352
|
+
} catch (const std::exception &error) {
|
|
3353
|
+
throw JSError(runtime, error.what());
|
|
3354
|
+
}
|
|
3355
|
+
}));
|
|
3356
|
+
|
|
3357
|
+
host.setProperty(
|
|
3358
|
+
runtime,
|
|
3359
|
+
"animateUIWorkletInputs",
|
|
3360
|
+
Function::createFromHostFunction(
|
|
3361
|
+
runtime,
|
|
3362
|
+
PropNameID::forAscii(runtime, "animateUIWorkletInputs"),
|
|
3363
|
+
4,
|
|
3364
|
+
[weakRuntimeState](
|
|
3365
|
+
Runtime &runtime,
|
|
3366
|
+
const Value &,
|
|
3367
|
+
const Value *arguments,
|
|
3368
|
+
size_t count) -> Value {
|
|
3369
|
+
if (count != 4 || !arguments[1].isObject() ||
|
|
3370
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
3371
|
+
!arguments[2].isNumber() || !arguments[3].isObject()) {
|
|
3372
|
+
throw JSError(
|
|
3373
|
+
runtime,
|
|
3374
|
+
"animateUIWorkletInputs requires a handle, input array, timestamp, and timing object.");
|
|
3375
|
+
}
|
|
3376
|
+
auto state = weakRuntimeState.lock();
|
|
3377
|
+
if (!state) {
|
|
3378
|
+
throw JSError(
|
|
3379
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3380
|
+
}
|
|
3381
|
+
const auto handle = requireSafeInteger(
|
|
3382
|
+
runtime,
|
|
3383
|
+
arguments[0],
|
|
3384
|
+
"The native UI worklet handle",
|
|
3385
|
+
true);
|
|
3386
|
+
auto array = arguments[1].getObject(runtime).getArray(runtime);
|
|
3387
|
+
if (array.size(runtime) >
|
|
3388
|
+
solid_native::worklets::MaximumUIWorkletInputs) {
|
|
3389
|
+
throw JSError(
|
|
3390
|
+
runtime, "The native UI worklet input vector is too large.");
|
|
3391
|
+
}
|
|
3392
|
+
std::vector<double> inputs;
|
|
3393
|
+
inputs.reserve(array.size(runtime));
|
|
3394
|
+
for (size_t index = 0; index < array.size(runtime); ++index) {
|
|
3395
|
+
auto value = array.getValueAtIndex(runtime, index);
|
|
3396
|
+
if (!value.isNumber() ||
|
|
3397
|
+
!std::isfinite(value.getNumber())) {
|
|
3398
|
+
throw JSError(
|
|
3399
|
+
runtime,
|
|
3400
|
+
"Native UI worklet inputs must be finite numbers.");
|
|
3401
|
+
}
|
|
3402
|
+
inputs.push_back(value.getNumber());
|
|
3403
|
+
}
|
|
3404
|
+
try {
|
|
3405
|
+
state->animateUIWorklet(
|
|
3406
|
+
runtime,
|
|
3407
|
+
handle,
|
|
3408
|
+
std::move(inputs),
|
|
3409
|
+
arguments[2].getNumber(),
|
|
3410
|
+
arguments[3]);
|
|
3411
|
+
return Value();
|
|
3412
|
+
} catch (const JSError &) {
|
|
3413
|
+
throw;
|
|
3414
|
+
} catch (const std::exception &error) {
|
|
3415
|
+
throw JSError(runtime, error.what());
|
|
3416
|
+
}
|
|
3417
|
+
}));
|
|
3418
|
+
|
|
3419
|
+
host.setProperty(
|
|
3420
|
+
runtime,
|
|
3421
|
+
"animateUIWorkletKeyframes",
|
|
3422
|
+
Function::createFromHostFunction(
|
|
3423
|
+
runtime,
|
|
3424
|
+
PropNameID::forAscii(runtime, "animateUIWorkletKeyframes"),
|
|
3425
|
+
3,
|
|
3426
|
+
[weakRuntimeState](
|
|
3427
|
+
Runtime &runtime,
|
|
3428
|
+
const Value &,
|
|
3429
|
+
const Value *arguments,
|
|
3430
|
+
size_t count) -> Value {
|
|
3431
|
+
if (count != 3 || !arguments[1].isObject() ||
|
|
3432
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
3433
|
+
!arguments[2].isNumber()) {
|
|
3434
|
+
throw JSError(
|
|
3435
|
+
runtime,
|
|
3436
|
+
"animateUIWorkletKeyframes requires a handle, keyframe array, and timestamp.");
|
|
3437
|
+
}
|
|
3438
|
+
auto state = weakRuntimeState.lock();
|
|
3439
|
+
if (!state) {
|
|
3440
|
+
throw JSError(
|
|
3441
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3442
|
+
}
|
|
3443
|
+
const auto handle = requireSafeInteger(
|
|
3444
|
+
runtime,
|
|
3445
|
+
arguments[0],
|
|
3446
|
+
"The native UI worklet handle",
|
|
3447
|
+
true);
|
|
3448
|
+
try {
|
|
3449
|
+
state->animateUIWorkletKeyframes(
|
|
3450
|
+
runtime,
|
|
3451
|
+
handle,
|
|
3452
|
+
arguments[1],
|
|
3453
|
+
arguments[2].getNumber());
|
|
3454
|
+
return Value();
|
|
3455
|
+
} catch (const JSError &) {
|
|
3456
|
+
throw;
|
|
3457
|
+
} catch (const std::exception &error) {
|
|
3458
|
+
throw JSError(runtime, error.what());
|
|
3459
|
+
}
|
|
3460
|
+
}));
|
|
3461
|
+
|
|
3462
|
+
host.setProperty(
|
|
3463
|
+
runtime,
|
|
3464
|
+
"springUIWorkletInputs",
|
|
3465
|
+
Function::createFromHostFunction(
|
|
3466
|
+
runtime,
|
|
3467
|
+
PropNameID::forAscii(runtime, "springUIWorkletInputs"),
|
|
3468
|
+
4,
|
|
3469
|
+
[weakRuntimeState](
|
|
3470
|
+
Runtime &runtime,
|
|
3471
|
+
const Value &,
|
|
3472
|
+
const Value *arguments,
|
|
3473
|
+
size_t count) -> Value {
|
|
3474
|
+
if (count != 4 || !arguments[1].isObject() ||
|
|
3475
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
3476
|
+
!arguments[2].isNumber() || !arguments[3].isObject()) {
|
|
3477
|
+
throw JSError(
|
|
3478
|
+
runtime,
|
|
3479
|
+
"springUIWorkletInputs requires a handle, input array, timestamp, and spring object.");
|
|
3480
|
+
}
|
|
3481
|
+
auto state = weakRuntimeState.lock();
|
|
3482
|
+
if (!state) {
|
|
3483
|
+
throw JSError(
|
|
3484
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3485
|
+
}
|
|
3486
|
+
const auto handle = requireSafeInteger(
|
|
3487
|
+
runtime,
|
|
3488
|
+
arguments[0],
|
|
3489
|
+
"The native UI worklet handle",
|
|
3490
|
+
true);
|
|
3491
|
+
auto array = arguments[1].getObject(runtime).getArray(runtime);
|
|
3492
|
+
if (array.size(runtime) >
|
|
3493
|
+
solid_native::worklets::MaximumUIWorkletInputs) {
|
|
3494
|
+
throw JSError(
|
|
3495
|
+
runtime, "The native UI worklet input vector is too large.");
|
|
3496
|
+
}
|
|
3497
|
+
std::vector<double> inputs;
|
|
3498
|
+
inputs.reserve(array.size(runtime));
|
|
3499
|
+
for (size_t index = 0; index < array.size(runtime); ++index) {
|
|
3500
|
+
auto value = array.getValueAtIndex(runtime, index);
|
|
3501
|
+
if (!value.isNumber() ||
|
|
3502
|
+
!std::isfinite(value.getNumber())) {
|
|
3503
|
+
throw JSError(
|
|
3504
|
+
runtime,
|
|
3505
|
+
"Native UI worklet inputs must be finite numbers.");
|
|
3506
|
+
}
|
|
3507
|
+
inputs.push_back(value.getNumber());
|
|
3508
|
+
}
|
|
3509
|
+
try {
|
|
3510
|
+
state->springUIWorklet(
|
|
3511
|
+
runtime,
|
|
3512
|
+
handle,
|
|
3513
|
+
std::move(inputs),
|
|
3514
|
+
arguments[2].getNumber(),
|
|
3515
|
+
arguments[3]);
|
|
3516
|
+
return Value();
|
|
3517
|
+
} catch (const JSError &) {
|
|
3518
|
+
throw;
|
|
3519
|
+
} catch (const std::exception &error) {
|
|
3520
|
+
throw JSError(runtime, error.what());
|
|
3521
|
+
}
|
|
3522
|
+
}));
|
|
3523
|
+
|
|
3524
|
+
host.setProperty(
|
|
3525
|
+
runtime,
|
|
3526
|
+
"decayUIWorkletInputs",
|
|
3527
|
+
Function::createFromHostFunction(
|
|
3528
|
+
runtime,
|
|
3529
|
+
PropNameID::forAscii(runtime, "decayUIWorkletInputs"),
|
|
3530
|
+
4,
|
|
3531
|
+
[weakRuntimeState](
|
|
3532
|
+
Runtime &runtime,
|
|
3533
|
+
const Value &,
|
|
3534
|
+
const Value *arguments,
|
|
3535
|
+
size_t count) -> Value {
|
|
3536
|
+
if (count != 4 || !arguments[1].isObject() ||
|
|
3537
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
3538
|
+
!arguments[2].isNumber() || !arguments[3].isObject()) {
|
|
3539
|
+
throw JSError(
|
|
3540
|
+
runtime,
|
|
3541
|
+
"decayUIWorkletInputs requires a handle, velocity array, timestamp, and decay object.");
|
|
3542
|
+
}
|
|
3543
|
+
auto state = weakRuntimeState.lock();
|
|
3544
|
+
if (!state) {
|
|
3545
|
+
throw JSError(
|
|
3546
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3547
|
+
}
|
|
3548
|
+
const auto handle = requireSafeInteger(
|
|
3549
|
+
runtime,
|
|
3550
|
+
arguments[0],
|
|
3551
|
+
"The native UI worklet handle",
|
|
3552
|
+
true);
|
|
3553
|
+
auto array = arguments[1].getObject(runtime).getArray(runtime);
|
|
3554
|
+
if (array.size(runtime) >
|
|
3555
|
+
solid_native::worklets::MaximumUIWorkletInputs) {
|
|
3556
|
+
throw JSError(
|
|
3557
|
+
runtime, "The native UI worklet velocity vector is too large.");
|
|
3558
|
+
}
|
|
3559
|
+
std::vector<double> velocities;
|
|
3560
|
+
velocities.reserve(array.size(runtime));
|
|
3561
|
+
for (size_t index = 0; index < array.size(runtime); ++index) {
|
|
3562
|
+
auto value = array.getValueAtIndex(runtime, index);
|
|
3563
|
+
if (!value.isNumber() ||
|
|
3564
|
+
!std::isfinite(value.getNumber())) {
|
|
3565
|
+
throw JSError(
|
|
3566
|
+
runtime,
|
|
3567
|
+
"Native UI worklet velocities must be finite numbers.");
|
|
3568
|
+
}
|
|
3569
|
+
velocities.push_back(value.getNumber());
|
|
3570
|
+
}
|
|
3571
|
+
try {
|
|
3572
|
+
state->decayUIWorklet(
|
|
3573
|
+
runtime,
|
|
3574
|
+
handle,
|
|
3575
|
+
std::move(velocities),
|
|
3576
|
+
arguments[2].getNumber(),
|
|
3577
|
+
arguments[3]);
|
|
3578
|
+
return Value();
|
|
3579
|
+
} catch (const JSError &) {
|
|
3580
|
+
throw;
|
|
3581
|
+
} catch (const std::exception &error) {
|
|
3582
|
+
throw JSError(runtime, error.what());
|
|
3583
|
+
}
|
|
3584
|
+
}));
|
|
3585
|
+
|
|
3586
|
+
host.setProperty(
|
|
3587
|
+
runtime,
|
|
3588
|
+
"cancelUIWorkletAnimation",
|
|
3589
|
+
Function::createFromHostFunction(
|
|
3590
|
+
runtime,
|
|
3591
|
+
PropNameID::forAscii(runtime, "cancelUIWorkletAnimation"),
|
|
3592
|
+
2,
|
|
3593
|
+
[weakRuntimeState](
|
|
3594
|
+
Runtime &runtime,
|
|
3595
|
+
const Value &,
|
|
3596
|
+
const Value *arguments,
|
|
3597
|
+
size_t count) -> Value {
|
|
3598
|
+
if (count != 2 || !arguments[1].isNumber()) {
|
|
3599
|
+
throw JSError(
|
|
3600
|
+
runtime,
|
|
3601
|
+
"cancelUIWorkletAnimation requires a handle and timestamp.");
|
|
3602
|
+
}
|
|
3603
|
+
auto state = weakRuntimeState.lock();
|
|
3604
|
+
if (!state) {
|
|
3605
|
+
throw JSError(
|
|
3606
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3607
|
+
}
|
|
3608
|
+
const auto handle = requireSafeInteger(
|
|
3609
|
+
runtime,
|
|
3610
|
+
arguments[0],
|
|
3611
|
+
"The native UI worklet handle",
|
|
3612
|
+
true);
|
|
3613
|
+
try {
|
|
3614
|
+
const auto inputs = state->cancelUIWorkletAnimation(
|
|
3615
|
+
handle, arguments[1].getNumber());
|
|
3616
|
+
Array result(runtime, inputs.size());
|
|
3617
|
+
for (size_t index = 0; index < inputs.size(); ++index) {
|
|
3618
|
+
result.setValueAtIndex(runtime, index, inputs.at(index));
|
|
3619
|
+
}
|
|
3620
|
+
return result;
|
|
3621
|
+
} catch (const std::exception &error) {
|
|
3622
|
+
throw JSError(runtime, error.what());
|
|
3623
|
+
}
|
|
3624
|
+
}));
|
|
3625
|
+
|
|
3626
|
+
host.setProperty(
|
|
3627
|
+
runtime,
|
|
3628
|
+
"attachUIWorkletPanGesture",
|
|
3629
|
+
Function::createFromHostFunction(
|
|
3630
|
+
runtime,
|
|
3631
|
+
PropNameID::forAscii(runtime, "attachUIWorkletPanGesture"),
|
|
3632
|
+
2,
|
|
3633
|
+
[weakRuntimeState](
|
|
3634
|
+
Runtime &runtime,
|
|
3635
|
+
const Value &,
|
|
3636
|
+
const Value *arguments,
|
|
3637
|
+
size_t count) -> Value {
|
|
3638
|
+
if (count != 2 || !arguments[1].isObject()) {
|
|
3639
|
+
throw JSError(
|
|
3640
|
+
runtime,
|
|
3641
|
+
"attachUIWorkletPanGesture requires a handle and one pan object.");
|
|
3642
|
+
}
|
|
3643
|
+
auto state = weakRuntimeState.lock();
|
|
3644
|
+
if (!state) {
|
|
3645
|
+
throw JSError(
|
|
3646
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3647
|
+
}
|
|
3648
|
+
const auto handle = requireSafeInteger(
|
|
3649
|
+
runtime,
|
|
3650
|
+
arguments[0],
|
|
3651
|
+
"The native UI worklet handle",
|
|
3652
|
+
true);
|
|
3653
|
+
bool attached = false;
|
|
3654
|
+
try {
|
|
3655
|
+
auto worklet =
|
|
3656
|
+
state->attachUIWorkletPan(runtime, handle, arguments[1]);
|
|
3657
|
+
int64_t token;
|
|
3658
|
+
{
|
|
3659
|
+
std::lock_guard lock(worklet->mutex);
|
|
3660
|
+
if (!worklet->active || !worklet->pan.has_value()) {
|
|
3661
|
+
throw std::runtime_error(
|
|
3662
|
+
"The Android native UI worklet pan attachment was cancelled.");
|
|
3663
|
+
}
|
|
3664
|
+
token = worklet->pan->token;
|
|
3665
|
+
}
|
|
3666
|
+
if (!attachJavaUIWorkletPan(token, worklet->targetTag)) {
|
|
3667
|
+
throw std::runtime_error(
|
|
3668
|
+
"The Android native UI worklet pan target is not mounted.");
|
|
3669
|
+
}
|
|
3670
|
+
attached = true;
|
|
3671
|
+
return Value();
|
|
3672
|
+
} catch (const JSError &) {
|
|
3673
|
+
if (!attached) {
|
|
3674
|
+
try {
|
|
3675
|
+
state->detachUIWorkletPan(handle);
|
|
3676
|
+
} catch (...) {
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3679
|
+
throw;
|
|
3680
|
+
} catch (const std::exception &error) {
|
|
3681
|
+
if (!attached) {
|
|
3682
|
+
try {
|
|
3683
|
+
state->detachUIWorkletPan(handle);
|
|
3684
|
+
} catch (...) {
|
|
3685
|
+
}
|
|
3686
|
+
}
|
|
3687
|
+
throw JSError(runtime, error.what());
|
|
3688
|
+
}
|
|
3689
|
+
}));
|
|
3690
|
+
|
|
3691
|
+
host.setProperty(
|
|
3692
|
+
runtime,
|
|
3693
|
+
"detachUIWorkletPanGesture",
|
|
3694
|
+
Function::createFromHostFunction(
|
|
3695
|
+
runtime,
|
|
3696
|
+
PropNameID::forAscii(runtime, "detachUIWorkletPanGesture"),
|
|
3697
|
+
2,
|
|
3698
|
+
[weakRuntimeState](
|
|
3699
|
+
Runtime &runtime,
|
|
3700
|
+
const Value &,
|
|
3701
|
+
const Value *arguments,
|
|
3702
|
+
size_t count) -> Value {
|
|
3703
|
+
if (count != 2 || !arguments[1].isNumber()) {
|
|
3704
|
+
throw JSError(
|
|
3705
|
+
runtime,
|
|
3706
|
+
"detachUIWorkletPanGesture requires a handle and timestamp.");
|
|
3707
|
+
}
|
|
3708
|
+
auto state = weakRuntimeState.lock();
|
|
3709
|
+
if (!state) {
|
|
3710
|
+
throw JSError(
|
|
3711
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3712
|
+
}
|
|
3713
|
+
const auto handle = requireSafeInteger(
|
|
3714
|
+
runtime,
|
|
3715
|
+
arguments[0],
|
|
3716
|
+
"The native UI worklet handle",
|
|
3717
|
+
true);
|
|
3718
|
+
try {
|
|
3719
|
+
const auto inputs = state->releaseUIWorkletPan(
|
|
3720
|
+
handle, arguments[1].getNumber());
|
|
3721
|
+
Array result(runtime, inputs.size());
|
|
3722
|
+
for (size_t index = 0; index < inputs.size(); ++index) {
|
|
3723
|
+
result.setValueAtIndex(runtime, index, inputs.at(index));
|
|
3724
|
+
}
|
|
3725
|
+
return result;
|
|
3726
|
+
} catch (const std::exception &error) {
|
|
3727
|
+
throw JSError(runtime, error.what());
|
|
3728
|
+
}
|
|
3729
|
+
}));
|
|
3730
|
+
|
|
3731
|
+
host.setProperty(
|
|
3732
|
+
runtime,
|
|
3733
|
+
"destroyUIWorklet",
|
|
3734
|
+
Function::createFromHostFunction(
|
|
3735
|
+
runtime,
|
|
3736
|
+
PropNameID::forAscii(runtime, "destroyUIWorklet"),
|
|
3737
|
+
1,
|
|
3738
|
+
[weakRuntimeState](
|
|
3739
|
+
Runtime &runtime,
|
|
3740
|
+
const Value &,
|
|
3741
|
+
const Value *arguments,
|
|
3742
|
+
size_t count) -> Value {
|
|
3743
|
+
if (count != 1) {
|
|
3744
|
+
throw JSError(
|
|
3745
|
+
runtime,
|
|
3746
|
+
"destroyUIWorklet requires exactly one handle.");
|
|
3747
|
+
}
|
|
3748
|
+
auto state = weakRuntimeState.lock();
|
|
3749
|
+
if (!state) {
|
|
3750
|
+
throw JSError(
|
|
3751
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3752
|
+
}
|
|
3753
|
+
const auto handle = requireSafeInteger(
|
|
3754
|
+
runtime,
|
|
3755
|
+
arguments[0],
|
|
3756
|
+
"The native UI worklet handle",
|
|
3757
|
+
true);
|
|
3758
|
+
try {
|
|
3759
|
+
state->destroyUIWorklet(handle);
|
|
3760
|
+
return Value();
|
|
3761
|
+
} catch (const std::exception &error) {
|
|
3762
|
+
throw JSError(runtime, error.what());
|
|
3763
|
+
}
|
|
3764
|
+
}));
|
|
3765
|
+
|
|
3766
|
+
host.setProperty(
|
|
3767
|
+
runtime,
|
|
3768
|
+
"getUIWorkletInfo",
|
|
3769
|
+
Function::createFromHostFunction(
|
|
3770
|
+
runtime,
|
|
3771
|
+
PropNameID::forAscii(runtime, "getUIWorkletInfo"),
|
|
3772
|
+
1,
|
|
3773
|
+
[weakRuntimeState](
|
|
3774
|
+
Runtime &runtime,
|
|
3775
|
+
const Value &,
|
|
3776
|
+
const Value *arguments,
|
|
3777
|
+
size_t count) -> Value {
|
|
3778
|
+
if (count != 1) {
|
|
3779
|
+
throw JSError(
|
|
3780
|
+
runtime,
|
|
3781
|
+
"getUIWorkletInfo requires exactly one handle.");
|
|
3782
|
+
}
|
|
3783
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
3784
|
+
if (!runtimeState) {
|
|
3785
|
+
throw JSError(
|
|
3786
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3787
|
+
}
|
|
3788
|
+
const auto handle = requireSafeInteger(
|
|
3789
|
+
runtime,
|
|
3790
|
+
arguments[0],
|
|
3791
|
+
"The native UI worklet handle",
|
|
3792
|
+
true);
|
|
3793
|
+
try {
|
|
3794
|
+
auto state = runtimeState->requireUIWorklet(handle);
|
|
3795
|
+
std::lock_guard lock(state->mutex);
|
|
3796
|
+
Object info(runtime);
|
|
3797
|
+
info.setProperty(
|
|
3798
|
+
runtime, "handle", static_cast<double>(state->handle));
|
|
3799
|
+
info.setProperty(
|
|
3800
|
+
runtime,
|
|
3801
|
+
"target",
|
|
3802
|
+
static_cast<double>(state->targetNode));
|
|
3803
|
+
info.setProperty(
|
|
3804
|
+
runtime,
|
|
3805
|
+
"sequence",
|
|
3806
|
+
static_cast<double>(state->sequence));
|
|
3807
|
+
info.setProperty(
|
|
3808
|
+
runtime,
|
|
3809
|
+
"appliedSequence",
|
|
3810
|
+
static_cast<double>(state->appliedSequence));
|
|
3811
|
+
info.setProperty(runtime, "applied", state->applied);
|
|
3812
|
+
info.setProperty(
|
|
3813
|
+
runtime,
|
|
3814
|
+
"frameTimeNanoseconds",
|
|
3815
|
+
static_cast<double>(state->frameTimeNanoseconds));
|
|
3816
|
+
if (state->hasTimestamp) {
|
|
3817
|
+
info.setProperty(runtime, "timestamp", state->timestamp);
|
|
3818
|
+
}
|
|
3819
|
+
info.setProperty(
|
|
3820
|
+
runtime, "timingActive", state->timing.has_value());
|
|
3821
|
+
info.setProperty(
|
|
3822
|
+
runtime, "timingProgress", state->timingProgress);
|
|
3823
|
+
if (!state->timingFrameStatistics.empty()) {
|
|
3824
|
+
info.setProperty(
|
|
3825
|
+
runtime,
|
|
3826
|
+
"timingFrameStatistics",
|
|
3827
|
+
makeUIWorkletFrameStatistics(
|
|
3828
|
+
runtime,
|
|
3829
|
+
state->timingFrameStatistics.snapshot()));
|
|
3830
|
+
}
|
|
3831
|
+
if (state->springPosition.has_value() &&
|
|
3832
|
+
state->springVelocity.has_value()) {
|
|
3833
|
+
info.setProperty(
|
|
3834
|
+
runtime,
|
|
3835
|
+
"springActive",
|
|
3836
|
+
state->spring.has_value());
|
|
3837
|
+
info.setProperty(
|
|
3838
|
+
runtime,
|
|
3839
|
+
"springPosition",
|
|
3840
|
+
*state->springPosition);
|
|
3841
|
+
info.setProperty(
|
|
3842
|
+
runtime,
|
|
3843
|
+
"springVelocity",
|
|
3844
|
+
*state->springVelocity);
|
|
3845
|
+
if (!state->springFrameStatistics.empty()) {
|
|
3846
|
+
info.setProperty(
|
|
3847
|
+
runtime,
|
|
3848
|
+
"springFrameStatistics",
|
|
3849
|
+
makeUIWorkletFrameStatistics(
|
|
3850
|
+
runtime,
|
|
3851
|
+
state->springFrameStatistics.snapshot()));
|
|
3852
|
+
}
|
|
3853
|
+
}
|
|
3854
|
+
if (state->decayElapsedMilliseconds.has_value() &&
|
|
3855
|
+
state->decaySpeed.has_value()) {
|
|
3856
|
+
info.setProperty(
|
|
3857
|
+
runtime,
|
|
3858
|
+
"decayActive",
|
|
3859
|
+
state->decay.has_value());
|
|
3860
|
+
info.setProperty(
|
|
3861
|
+
runtime,
|
|
3862
|
+
"decayElapsedMilliseconds",
|
|
3863
|
+
*state->decayElapsedMilliseconds);
|
|
3864
|
+
info.setProperty(
|
|
3865
|
+
runtime,
|
|
3866
|
+
"decaySpeed",
|
|
3867
|
+
*state->decaySpeed);
|
|
3868
|
+
if (!state->decayFrameStatistics.empty()) {
|
|
3869
|
+
info.setProperty(
|
|
3870
|
+
runtime,
|
|
3871
|
+
"decayFrameStatistics",
|
|
3872
|
+
makeUIWorkletFrameStatistics(
|
|
3873
|
+
runtime,
|
|
3874
|
+
state->decayFrameStatistics.snapshot()));
|
|
3875
|
+
}
|
|
3876
|
+
}
|
|
3877
|
+
if (state->pan.has_value()) {
|
|
3878
|
+
const auto &pan = *state->pan;
|
|
3879
|
+
info.setProperty(runtime, "gestureAttached", true);
|
|
3880
|
+
info.setProperty(runtime, "gestureActive", pan.active);
|
|
3881
|
+
info.setProperty(
|
|
3882
|
+
runtime,
|
|
3883
|
+
"gestureSequence",
|
|
3884
|
+
static_cast<double>(pan.sequence));
|
|
3885
|
+
if (pan.timestamp.has_value()) {
|
|
3886
|
+
info.setProperty(
|
|
3887
|
+
runtime, "gestureTimestamp", *pan.timestamp);
|
|
3888
|
+
}
|
|
3889
|
+
}
|
|
3890
|
+
Object outputs(runtime);
|
|
3891
|
+
for (const auto &output : state->outputs) {
|
|
3892
|
+
outputs.setProperty(
|
|
3893
|
+
runtime, output.name.c_str(), output.value);
|
|
3894
|
+
}
|
|
3895
|
+
info.setProperty(runtime, "outputs", outputs);
|
|
3896
|
+
return info;
|
|
3897
|
+
} catch (const std::exception &error) {
|
|
3898
|
+
throw JSError(runtime, error.what());
|
|
3899
|
+
}
|
|
3900
|
+
}));
|
|
3901
|
+
|
|
3902
|
+
host.setProperty(
|
|
3903
|
+
runtime,
|
|
3904
|
+
"measure",
|
|
3905
|
+
Function::createFromHostFunction(
|
|
3906
|
+
runtime,
|
|
3907
|
+
PropNameID::forAscii(runtime, "measure"),
|
|
3908
|
+
2,
|
|
3909
|
+
[bindingState](
|
|
3910
|
+
Runtime &runtime,
|
|
3911
|
+
const Value &,
|
|
3912
|
+
const Value *arguments,
|
|
3913
|
+
size_t count) -> Value {
|
|
3914
|
+
if (count < 1 || count > 2) {
|
|
3915
|
+
throw JSError(
|
|
3916
|
+
runtime,
|
|
3917
|
+
"measure requires a node and an optional commit "
|
|
3918
|
+
"sequence.");
|
|
3919
|
+
}
|
|
3920
|
+
const auto node = requireSafeInteger(
|
|
3921
|
+
runtime, arguments[0], "The measured node", true);
|
|
3922
|
+
const auto afterSequence = count == 2
|
|
3923
|
+
? requireSafeInteger(
|
|
3924
|
+
runtime,
|
|
3925
|
+
arguments[1],
|
|
3926
|
+
"The measurement sequence",
|
|
3927
|
+
false)
|
|
3928
|
+
: 0;
|
|
3929
|
+
if (afterSequence < 0) {
|
|
3930
|
+
throw JSError(
|
|
3931
|
+
runtime,
|
|
3932
|
+
"The measurement sequence must be non-negative.");
|
|
3933
|
+
}
|
|
3934
|
+
try {
|
|
3935
|
+
auto snapshot = requireSurface(runtime, bindingState);
|
|
3936
|
+
auto result = snapshot.coordinator->measure(
|
|
3937
|
+
uiManager(runtime), node, afterSequence);
|
|
3938
|
+
return measurementObject(runtime, result);
|
|
3939
|
+
} catch (const JSError &) {
|
|
3940
|
+
throw;
|
|
3941
|
+
} catch (const std::exception &error) {
|
|
3942
|
+
throw JSError(runtime, error.what());
|
|
3943
|
+
}
|
|
3944
|
+
}));
|
|
3945
|
+
|
|
3946
|
+
host.setProperty(
|
|
3947
|
+
runtime,
|
|
3948
|
+
"reportFatalError",
|
|
3949
|
+
Function::createFromHostFunction(
|
|
3950
|
+
runtime,
|
|
3951
|
+
PropNameID::forAscii(runtime, "reportFatalError"),
|
|
3952
|
+
2,
|
|
3953
|
+
[bindingState, weakRuntimeState](
|
|
3954
|
+
Runtime &runtime,
|
|
3955
|
+
const Value &,
|
|
3956
|
+
const Value *arguments,
|
|
3957
|
+
size_t count) -> Value {
|
|
3958
|
+
if (count != 2) {
|
|
3959
|
+
throw JSError(
|
|
3960
|
+
runtime,
|
|
3961
|
+
"reportFatalError requires exactly two arguments: name and message.");
|
|
3962
|
+
}
|
|
3963
|
+
auto state = weakRuntimeState.lock();
|
|
3964
|
+
if (!state) {
|
|
3965
|
+
throw JSError(
|
|
3966
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
3967
|
+
}
|
|
3968
|
+
const auto name = requireFatalErrorText(
|
|
3969
|
+
runtime,
|
|
3970
|
+
arguments[0],
|
|
3971
|
+
"The fatal error name",
|
|
3972
|
+
MaximumFatalErrorNameBytes);
|
|
3973
|
+
const auto message = requireFatalErrorText(
|
|
3974
|
+
runtime,
|
|
3975
|
+
arguments[1],
|
|
3976
|
+
"The fatal error message",
|
|
3977
|
+
MaximumFatalErrorMessageBytes);
|
|
3978
|
+
const auto snapshot = requireSurface(runtime, bindingState);
|
|
3979
|
+
requestJavaFatalError(snapshot.surface, name, message);
|
|
3980
|
+
return Value();
|
|
3981
|
+
}));
|
|
3982
|
+
|
|
3983
|
+
host.setProperty(
|
|
3984
|
+
runtime,
|
|
3985
|
+
"destroySurface",
|
|
3986
|
+
Function::createFromHostFunction(
|
|
3987
|
+
runtime,
|
|
3988
|
+
PropNameID::forAscii(runtime, "destroySurface"),
|
|
3989
|
+
0,
|
|
3990
|
+
[bindingState, weakRuntimeState](
|
|
3991
|
+
Runtime &runtime,
|
|
3992
|
+
const Value &,
|
|
3993
|
+
const Value *,
|
|
3994
|
+
size_t count) -> Value {
|
|
3995
|
+
if (count != 0) {
|
|
3996
|
+
throw JSError(
|
|
3997
|
+
runtime,
|
|
3998
|
+
"destroySurface does not accept arguments.");
|
|
3999
|
+
}
|
|
4000
|
+
auto state = weakRuntimeState.lock();
|
|
4001
|
+
if (!state) {
|
|
4002
|
+
throw JSError(
|
|
4003
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
4004
|
+
}
|
|
4005
|
+
auto snapshot = requireSurface(runtime, bindingState);
|
|
4006
|
+
if (!snapshot.coordinator->empty()) {
|
|
4007
|
+
throw JSError(
|
|
4008
|
+
runtime,
|
|
4009
|
+
"Cannot destroy a Fabric surface with live nodes.");
|
|
4010
|
+
}
|
|
4011
|
+
if (state->activeUIWorkletCount() != 0) {
|
|
4012
|
+
throw JSError(
|
|
4013
|
+
runtime,
|
|
4014
|
+
"Cannot destroy a Fabric surface with active native UI worklets.");
|
|
4015
|
+
}
|
|
4016
|
+
installEmptySurfaceStopHandler(runtime);
|
|
4017
|
+
if (snapshot.mountObserver) snapshot.mountObserver->detach();
|
|
4018
|
+
state->detachEventListener(uiManager(runtime));
|
|
4019
|
+
state->deactivateEvents();
|
|
4020
|
+
state->reclaimAllInstances(runtime);
|
|
4021
|
+
state->reclaimAllResources(runtime);
|
|
4022
|
+
requestJavaSurfaceStop(snapshot.surface);
|
|
4023
|
+
return Value();
|
|
4024
|
+
}));
|
|
4025
|
+
|
|
4026
|
+
host.setProperty(
|
|
4027
|
+
runtime,
|
|
4028
|
+
"commit",
|
|
4029
|
+
Function::createFromHostFunction(
|
|
4030
|
+
runtime,
|
|
4031
|
+
PropNameID::forAscii(runtime, "commit"),
|
|
4032
|
+
1,
|
|
4033
|
+
[bindingState, weakRuntimeState](
|
|
4034
|
+
Runtime &runtime,
|
|
4035
|
+
const Value &,
|
|
4036
|
+
const Value *arguments,
|
|
4037
|
+
size_t count) -> Value {
|
|
4038
|
+
if (count != 1 || !arguments[0].isObject()) {
|
|
4039
|
+
throw JSError(
|
|
4040
|
+
runtime,
|
|
4041
|
+
"commit requires exactly one transaction object.");
|
|
4042
|
+
}
|
|
4043
|
+
const auto commitStartedAt = wallClockMilliseconds();
|
|
4044
|
+
const auto commitStartedMonotonic =
|
|
4045
|
+
monotonicMilliseconds();
|
|
4046
|
+
try {
|
|
4047
|
+
auto state = weakRuntimeState.lock();
|
|
4048
|
+
if (!state) {
|
|
4049
|
+
throw JSError(
|
|
4050
|
+
runtime, "The Solid Native runtime has stopped.");
|
|
4051
|
+
}
|
|
4052
|
+
auto snapshot = requireSurface(runtime, bindingState);
|
|
4053
|
+
size_t valueCount = 0;
|
|
4054
|
+
std::vector<Value> ancestors;
|
|
4055
|
+
auto transaction = dynamicFromJSI(
|
|
4056
|
+
runtime,
|
|
4057
|
+
arguments[0],
|
|
4058
|
+
"transaction",
|
|
4059
|
+
0,
|
|
4060
|
+
valueCount,
|
|
4061
|
+
ancestors);
|
|
4062
|
+
if (!transaction.isObject()) {
|
|
4063
|
+
throw std::invalid_argument(
|
|
4064
|
+
"The host transaction must be an object.");
|
|
4065
|
+
}
|
|
4066
|
+
const auto surface = transaction.count("surface") != 0
|
|
4067
|
+
? dynamicInteger(transaction.at("surface"))
|
|
4068
|
+
: 0;
|
|
4069
|
+
auto prepared = prepareNodeIdentities(
|
|
4070
|
+
runtime, transaction, surface, state);
|
|
4071
|
+
const auto deletedHandles =
|
|
4072
|
+
deletedNodeHandles(transaction);
|
|
4073
|
+
IdentityRetention retention(runtime, state, prepared);
|
|
4074
|
+
const NativeResourceResolver resourceResolver =
|
|
4075
|
+
[state](Runtime &runtime,
|
|
4076
|
+
int64_t handle,
|
|
4077
|
+
const std::string &kind) -> Value {
|
|
4078
|
+
return state->resolveResource(runtime, handle, kind);
|
|
4079
|
+
};
|
|
4080
|
+
auto &manager = uiManager(runtime);
|
|
4081
|
+
state->attachEventListener(manager);
|
|
4082
|
+
if (!snapshot.mountObserver) {
|
|
4083
|
+
throw std::runtime_error(
|
|
4084
|
+
"The Android Fabric mount observer is unavailable.");
|
|
4085
|
+
}
|
|
4086
|
+
snapshot.mountObserver->attach(
|
|
4087
|
+
manager, snapshot.surface);
|
|
4088
|
+
const auto previousFabricRevision =
|
|
4089
|
+
snapshot.coordinator->lastFabricRevision();
|
|
4090
|
+
auto layoutRouteStage = state->stageLayoutRoutes(
|
|
4091
|
+
transaction, prepared.identities);
|
|
4092
|
+
int64_t sequence;
|
|
4093
|
+
try {
|
|
4094
|
+
sequence = snapshot.coordinator->apply(
|
|
4095
|
+
manager,
|
|
4096
|
+
transaction,
|
|
4097
|
+
runtime,
|
|
4098
|
+
prepared.identities,
|
|
4099
|
+
resourceResolver);
|
|
4100
|
+
} catch (...) {
|
|
4101
|
+
state->rollbackLayoutRoutes(layoutRouteStage);
|
|
4102
|
+
throw;
|
|
4103
|
+
}
|
|
4104
|
+
retention.commit();
|
|
4105
|
+
state->cancelDeletedUIWorklets(deletedHandles);
|
|
4106
|
+
state->publishCommit(
|
|
4107
|
+
transaction, prepared.identities, sequence);
|
|
4108
|
+
state->finishLayoutRoutes(layoutRouteStage);
|
|
4109
|
+
const auto fabricRevision =
|
|
4110
|
+
snapshot.coordinator->lastFabricRevision();
|
|
4111
|
+
const auto producedFabricRevision =
|
|
4112
|
+
fabricRevision > previousFabricRevision;
|
|
4113
|
+
if (producedFabricRevision) {
|
|
4114
|
+
state->retireInstances(fabricRevision, deletedHandles);
|
|
4115
|
+
const auto notifyLifecycle =
|
|
4116
|
+
state->hasCommitLifecycleHandler();
|
|
4117
|
+
if (notifyLifecycle || !deletedHandles.empty()) {
|
|
4118
|
+
snapshot.mountObserver->enqueue(
|
|
4119
|
+
std::make_shared<const CommitMountObservation>(
|
|
4120
|
+
CommitMountObservation{
|
|
4121
|
+
.surface = snapshot.surface,
|
|
4122
|
+
.sequence = sequence,
|
|
4123
|
+
.fabricRevision = fabricRevision,
|
|
4124
|
+
.commitStartedAt = commitStartedAt,
|
|
4125
|
+
.commitStartedMonotonic =
|
|
4126
|
+
commitStartedMonotonic,
|
|
4127
|
+
.notifyLifecycle = notifyLifecycle,
|
|
4128
|
+
.causalOperationId =
|
|
4129
|
+
causalOperationId(transaction),
|
|
4130
|
+
}));
|
|
4131
|
+
}
|
|
4132
|
+
}
|
|
4133
|
+
|
|
4134
|
+
Object result(runtime);
|
|
4135
|
+
result.setProperty(
|
|
4136
|
+
runtime,
|
|
4137
|
+
"surface",
|
|
4138
|
+
static_cast<double>(snapshot.surface));
|
|
4139
|
+
result.setProperty(
|
|
4140
|
+
runtime, "sequence", static_cast<double>(sequence));
|
|
4141
|
+
result.setProperty(runtime, "mounted", true);
|
|
4142
|
+
if (producedFabricRevision) {
|
|
4143
|
+
result.setProperty(
|
|
4144
|
+
runtime,
|
|
4145
|
+
"hostRevision",
|
|
4146
|
+
static_cast<double>(fabricRevision));
|
|
4147
|
+
}
|
|
4148
|
+
return result;
|
|
4149
|
+
} catch (const JSError &) {
|
|
4150
|
+
throw;
|
|
4151
|
+
} catch (const std::exception &error) {
|
|
4152
|
+
throw JSError(runtime, error.what());
|
|
4153
|
+
}
|
|
4154
|
+
}));
|
|
4155
|
+
|
|
4156
|
+
auto schedulerBinding =
|
|
4157
|
+
facebook::react::RuntimeSchedulerBinding::getBinding(runtime);
|
|
4158
|
+
if (!schedulerBinding) {
|
|
4159
|
+
throw JSError(
|
|
4160
|
+
runtime,
|
|
4161
|
+
"React Native did not install its RuntimeScheduler binding.");
|
|
4162
|
+
}
|
|
4163
|
+
auto runtimeScheduler = schedulerBinding->getRuntimeScheduler();
|
|
4164
|
+
if (!runtimeScheduler) {
|
|
4165
|
+
throw JSError(
|
|
4166
|
+
runtime, "React Native did not expose its RuntimeScheduler.");
|
|
4167
|
+
}
|
|
4168
|
+
auto eventListener = createEventListener(
|
|
4169
|
+
routing, runtimeScheduler, weakRuntimeState);
|
|
4170
|
+
runtimeState->setEventListener(eventListener);
|
|
4171
|
+
bindingState->installMountObserver(
|
|
4172
|
+
std::make_shared<MountObservationState>(
|
|
4173
|
+
runtimeScheduler, weakRuntimeState));
|
|
4174
|
+
|
|
4175
|
+
auto freeze = objectConstructor.getPropertyAsFunction(runtime, "freeze");
|
|
4176
|
+
freeze.call(runtime, host);
|
|
4177
|
+
|
|
4178
|
+
Object descriptor(runtime);
|
|
4179
|
+
descriptor.setProperty(runtime, "value", host);
|
|
4180
|
+
descriptor.setProperty(runtime, "writable", false);
|
|
4181
|
+
descriptor.setProperty(runtime, "configurable", false);
|
|
4182
|
+
descriptor.setProperty(runtime, "enumerable", false);
|
|
4183
|
+
defineProperty.call(
|
|
4184
|
+
runtime,
|
|
4185
|
+
runtime.global(),
|
|
4186
|
+
String::createFromAscii(runtime, GlobalBindingName),
|
|
4187
|
+
descriptor);
|
|
4188
|
+
};
|
|
4189
|
+
}
|
|
4190
|
+
|
|
4191
|
+
void publishSurface(jint surface) {
|
|
4192
|
+
bindingState_->publishSurface(surface);
|
|
4193
|
+
}
|
|
4194
|
+
|
|
4195
|
+
void clearSurface(jint surface) {
|
|
4196
|
+
bindingState_->clearSurface(surface);
|
|
4197
|
+
}
|
|
4198
|
+
|
|
4199
|
+
void deactivateSurface(jint surface) {
|
|
4200
|
+
bindingState_->deactivateSurface(surface);
|
|
4201
|
+
}
|
|
4202
|
+
|
|
4203
|
+
private:
|
|
4204
|
+
friend HybridBase;
|
|
4205
|
+
|
|
4206
|
+
SolidNativeBindingsInstaller()
|
|
4207
|
+
: bindingState_(std::make_shared<BindingState>()) {}
|
|
4208
|
+
|
|
4209
|
+
std::shared_ptr<BindingState> bindingState_;
|
|
4210
|
+
};
|
|
4211
|
+
|
|
4212
|
+
} // namespace
|
|
4213
|
+
|
|
4214
|
+
void registerSolidNativeBindingsInstaller() {
|
|
4215
|
+
SolidNativeBindingsInstaller::registerNatives();
|
|
4216
|
+
}
|
|
4217
|
+
|
|
4218
|
+
} // namespace solid_native::android
|