@solidnative-dev/fabric-host 0.0.0-stage → 0.1.0-beta.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.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,1080 @@
|
|
|
1
|
+
#include "SolidNativeUIWorklet.h"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <initializer_list>
|
|
6
|
+
#include <stdexcept>
|
|
7
|
+
#include <string_view>
|
|
8
|
+
#include <unordered_map>
|
|
9
|
+
#include <unordered_set>
|
|
10
|
+
#include <utility>
|
|
11
|
+
|
|
12
|
+
namespace solid_native::worklets {
|
|
13
|
+
namespace {
|
|
14
|
+
|
|
15
|
+
using Dynamic = folly::dynamic;
|
|
16
|
+
|
|
17
|
+
bool isIdentifier(std::string_view value) {
|
|
18
|
+
if (value.empty() || value.size() > 64) return false;
|
|
19
|
+
const auto isLetter = [](char character) {
|
|
20
|
+
return (character >= 'A' && character <= 'Z') ||
|
|
21
|
+
(character >= 'a' && character <= 'z');
|
|
22
|
+
};
|
|
23
|
+
if (!isLetter(value.front())) return false;
|
|
24
|
+
return std::all_of(
|
|
25
|
+
value.begin() + 1,
|
|
26
|
+
value.end(),
|
|
27
|
+
[&](char character) {
|
|
28
|
+
return isLetter(character) ||
|
|
29
|
+
(character >= '0' && character <= '9') || character == '.' ||
|
|
30
|
+
character == '_' || character == '-';
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
void requireExactObject(
|
|
35
|
+
const Dynamic &value,
|
|
36
|
+
std::initializer_list<std::string_view> keys,
|
|
37
|
+
const std::string &context) {
|
|
38
|
+
if (!value.isObject()) {
|
|
39
|
+
throw std::invalid_argument(context + " must be a plain object.");
|
|
40
|
+
}
|
|
41
|
+
if (value.size() != keys.size()) {
|
|
42
|
+
for (const auto &item : value.items()) {
|
|
43
|
+
if (!item.first.isString()) {
|
|
44
|
+
throw std::invalid_argument(
|
|
45
|
+
context + " contains a non-string field.");
|
|
46
|
+
}
|
|
47
|
+
const auto &candidate = item.first.getString();
|
|
48
|
+
if (std::none_of(
|
|
49
|
+
keys.begin(),
|
|
50
|
+
keys.end(),
|
|
51
|
+
[&](std::string_view key) { return candidate == key; })) {
|
|
52
|
+
throw std::invalid_argument(
|
|
53
|
+
context + " contains unsupported field " + candidate + ".");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
for (const auto key : keys) {
|
|
58
|
+
if (value.count(std::string{key}) == 0) {
|
|
59
|
+
throw std::invalid_argument(
|
|
60
|
+
context + " is missing field " + std::string{key} + ".");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (value.size() != keys.size()) {
|
|
64
|
+
throw std::invalid_argument(context + " contains duplicate fields.");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
double requireFiniteNumber(
|
|
69
|
+
const Dynamic &value,
|
|
70
|
+
const std::string &context) {
|
|
71
|
+
if (!value.isNumber()) {
|
|
72
|
+
throw std::invalid_argument(context + " must be a finite number.");
|
|
73
|
+
}
|
|
74
|
+
const auto number = value.asDouble();
|
|
75
|
+
if (!std::isfinite(number)) {
|
|
76
|
+
throw std::invalid_argument(context + " must be a finite number.");
|
|
77
|
+
}
|
|
78
|
+
return number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
std::string requireIdentifier(
|
|
82
|
+
const Dynamic &value,
|
|
83
|
+
const std::string &context) {
|
|
84
|
+
if (!value.isString() || !isIdentifier(value.getString())) {
|
|
85
|
+
throw std::invalid_argument(
|
|
86
|
+
context + " must be an identifier of 1 to 64 ASCII characters.");
|
|
87
|
+
}
|
|
88
|
+
return value.getString();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
std::pair<double, double> requireRange(
|
|
92
|
+
const Dynamic &value,
|
|
93
|
+
const std::string &context,
|
|
94
|
+
bool strictlyAscending) {
|
|
95
|
+
if (!value.isArray() || value.size() != 2) {
|
|
96
|
+
throw std::invalid_argument(
|
|
97
|
+
context + " must contain exactly two numbers.");
|
|
98
|
+
}
|
|
99
|
+
const auto lower = requireFiniteNumber(value.at(0), context + "[0]");
|
|
100
|
+
const auto upper = requireFiniteNumber(value.at(1), context + "[1]");
|
|
101
|
+
if (strictlyAscending && upper <= lower) {
|
|
102
|
+
throw std::invalid_argument(context + " must be strictly ascending.");
|
|
103
|
+
}
|
|
104
|
+
return {lower, upper};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
struct ParseBudget final {
|
|
108
|
+
size_t nodes{0};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
using Operation = UIWorkletGraph::Operation;
|
|
112
|
+
using Instruction = UIWorkletGraph::Instruction;
|
|
113
|
+
|
|
114
|
+
void parseExpression(
|
|
115
|
+
const Dynamic &value,
|
|
116
|
+
const std::unordered_map<std::string, size_t> &inputs,
|
|
117
|
+
ParseBudget &budget,
|
|
118
|
+
size_t depth,
|
|
119
|
+
const std::string &context,
|
|
120
|
+
std::vector<Instruction> &instructions) {
|
|
121
|
+
if (depth > MaximumUIWorkletExpressionDepth) {
|
|
122
|
+
throw std::invalid_argument(
|
|
123
|
+
context + " exceeds the maximum worklet expression depth of " +
|
|
124
|
+
std::to_string(MaximumUIWorkletExpressionDepth) + ".");
|
|
125
|
+
}
|
|
126
|
+
if (++budget.nodes > MaximumUIWorkletExpressionNodes) {
|
|
127
|
+
throw std::invalid_argument(
|
|
128
|
+
"The worklet graph exceeds " +
|
|
129
|
+
std::to_string(MaximumUIWorkletExpressionNodes) +
|
|
130
|
+
" expression nodes.");
|
|
131
|
+
}
|
|
132
|
+
if (!value.isObject() || value.count("kind") == 0 ||
|
|
133
|
+
!value.at("kind").isString()) {
|
|
134
|
+
throw std::invalid_argument(context + ".kind is unsupported.");
|
|
135
|
+
}
|
|
136
|
+
const auto &kind = value.at("kind").getString();
|
|
137
|
+
if (kind == "constant") {
|
|
138
|
+
requireExactObject(value, {"kind", "value"}, context);
|
|
139
|
+
instructions.push_back({
|
|
140
|
+
.operation = Operation::Constant,
|
|
141
|
+
.first = requireFiniteNumber(value.at("value"), context + ".value"),
|
|
142
|
+
});
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (kind == "input") {
|
|
146
|
+
requireExactObject(value, {"kind", "name"}, context);
|
|
147
|
+
const auto name = requireIdentifier(value.at("name"), context + ".name");
|
|
148
|
+
const auto input = inputs.find(name);
|
|
149
|
+
if (input == inputs.end()) {
|
|
150
|
+
throw std::invalid_argument(
|
|
151
|
+
context + " references undeclared worklet input " + name + ".");
|
|
152
|
+
}
|
|
153
|
+
instructions.push_back({
|
|
154
|
+
.operation = Operation::Input,
|
|
155
|
+
.input = input->second,
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (kind == "binary") {
|
|
160
|
+
requireExactObject(
|
|
161
|
+
value, {"kind", "operator", "left", "right"}, context);
|
|
162
|
+
if (!value.at("operator").isString()) {
|
|
163
|
+
throw std::invalid_argument(context + ".operator is unsupported.");
|
|
164
|
+
}
|
|
165
|
+
const auto &name = value.at("operator").getString();
|
|
166
|
+
Operation operation;
|
|
167
|
+
if (name == "add") {
|
|
168
|
+
operation = Operation::Add;
|
|
169
|
+
} else if (name == "subtract") {
|
|
170
|
+
operation = Operation::Subtract;
|
|
171
|
+
} else if (name == "multiply") {
|
|
172
|
+
operation = Operation::Multiply;
|
|
173
|
+
} else if (name == "divide") {
|
|
174
|
+
operation = Operation::Divide;
|
|
175
|
+
} else if (name == "min") {
|
|
176
|
+
operation = Operation::Minimum;
|
|
177
|
+
} else if (name == "max") {
|
|
178
|
+
operation = Operation::Maximum;
|
|
179
|
+
} else {
|
|
180
|
+
throw std::invalid_argument(context + ".operator is unsupported.");
|
|
181
|
+
}
|
|
182
|
+
parseExpression(
|
|
183
|
+
value.at("left"),
|
|
184
|
+
inputs,
|
|
185
|
+
budget,
|
|
186
|
+
depth + 1,
|
|
187
|
+
context + ".left",
|
|
188
|
+
instructions);
|
|
189
|
+
parseExpression(
|
|
190
|
+
value.at("right"),
|
|
191
|
+
inputs,
|
|
192
|
+
budget,
|
|
193
|
+
depth + 1,
|
|
194
|
+
context + ".right",
|
|
195
|
+
instructions);
|
|
196
|
+
instructions.push_back({.operation = operation});
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (kind == "abs" || kind == "negate") {
|
|
200
|
+
requireExactObject(value, {"kind", "value"}, context);
|
|
201
|
+
parseExpression(
|
|
202
|
+
value.at("value"),
|
|
203
|
+
inputs,
|
|
204
|
+
budget,
|
|
205
|
+
depth + 1,
|
|
206
|
+
context + ".value",
|
|
207
|
+
instructions);
|
|
208
|
+
instructions.push_back({
|
|
209
|
+
.operation = kind == "abs" ? Operation::Absolute : Operation::Negate,
|
|
210
|
+
});
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (kind == "clamp") {
|
|
214
|
+
requireExactObject(value, {"kind", "value", "min", "max"}, context);
|
|
215
|
+
const auto minimum =
|
|
216
|
+
requireFiniteNumber(value.at("min"), context + ".min");
|
|
217
|
+
const auto maximum =
|
|
218
|
+
requireFiniteNumber(value.at("max"), context + ".max");
|
|
219
|
+
if (maximum < minimum) {
|
|
220
|
+
throw std::invalid_argument(
|
|
221
|
+
context + " clamp bounds must be ascending.");
|
|
222
|
+
}
|
|
223
|
+
parseExpression(
|
|
224
|
+
value.at("value"),
|
|
225
|
+
inputs,
|
|
226
|
+
budget,
|
|
227
|
+
depth + 1,
|
|
228
|
+
context + ".value",
|
|
229
|
+
instructions);
|
|
230
|
+
instructions.push_back({
|
|
231
|
+
.operation = Operation::Clamp,
|
|
232
|
+
.first = minimum,
|
|
233
|
+
.second = maximum,
|
|
234
|
+
});
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (kind == "interpolate") {
|
|
238
|
+
requireExactObject(
|
|
239
|
+
value,
|
|
240
|
+
{"kind", "value", "inputRange", "outputRange", "extrapolate"},
|
|
241
|
+
context);
|
|
242
|
+
if (!value.at("extrapolate").isString() ||
|
|
243
|
+
(value.at("extrapolate").getString() != "clamp" &&
|
|
244
|
+
value.at("extrapolate").getString() != "extend")) {
|
|
245
|
+
throw std::invalid_argument(
|
|
246
|
+
context + ".extrapolate must be clamp or extend.");
|
|
247
|
+
}
|
|
248
|
+
const auto inputRange = requireRange(
|
|
249
|
+
value.at("inputRange"), context + ".inputRange", true);
|
|
250
|
+
const auto outputRange = requireRange(
|
|
251
|
+
value.at("outputRange"), context + ".outputRange", false);
|
|
252
|
+
parseExpression(
|
|
253
|
+
value.at("value"),
|
|
254
|
+
inputs,
|
|
255
|
+
budget,
|
|
256
|
+
depth + 1,
|
|
257
|
+
context + ".value",
|
|
258
|
+
instructions);
|
|
259
|
+
instructions.push_back({
|
|
260
|
+
.operation = Operation::Interpolate,
|
|
261
|
+
.first = inputRange.first,
|
|
262
|
+
.second = inputRange.second,
|
|
263
|
+
.third = outputRange.first,
|
|
264
|
+
.fourth = outputRange.second,
|
|
265
|
+
.clamp = value.at("extrapolate").getString() == "clamp",
|
|
266
|
+
});
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
throw std::invalid_argument(context + ".kind is unsupported.");
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
double pop(std::vector<double> &stack) {
|
|
273
|
+
if (stack.empty()) {
|
|
274
|
+
throw std::logic_error("The UI worklet instruction stack underflowed.");
|
|
275
|
+
}
|
|
276
|
+
const auto value = stack.back();
|
|
277
|
+
stack.pop_back();
|
|
278
|
+
return value;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
} // namespace
|
|
282
|
+
|
|
283
|
+
UIWorkletTiming UIWorkletTiming::parse(const Dynamic &value) {
|
|
284
|
+
requireExactObject(
|
|
285
|
+
value,
|
|
286
|
+
{"durationMilliseconds", "easing"},
|
|
287
|
+
"UI worklet timing");
|
|
288
|
+
const auto duration = requireFiniteNumber(
|
|
289
|
+
value.at("durationMilliseconds"),
|
|
290
|
+
"UI worklet timing duration");
|
|
291
|
+
if (duration <= 0 ||
|
|
292
|
+
duration > MaximumUIWorkletTimingDurationMilliseconds) {
|
|
293
|
+
throw std::invalid_argument(
|
|
294
|
+
"UI worklet timing duration must be greater than zero and at most " +
|
|
295
|
+
std::to_string(MaximumUIWorkletTimingDurationMilliseconds) +
|
|
296
|
+
" milliseconds.");
|
|
297
|
+
}
|
|
298
|
+
if (!value.at("easing").isString()) {
|
|
299
|
+
throw std::invalid_argument("UI worklet timing easing is unsupported.");
|
|
300
|
+
}
|
|
301
|
+
const auto &name = value.at("easing").getString();
|
|
302
|
+
UIWorkletTimingEasing easing;
|
|
303
|
+
if (name == "linear") {
|
|
304
|
+
easing = UIWorkletTimingEasing::Linear;
|
|
305
|
+
} else if (name == "ease-in") {
|
|
306
|
+
easing = UIWorkletTimingEasing::EaseIn;
|
|
307
|
+
} else if (name == "ease-out") {
|
|
308
|
+
easing = UIWorkletTimingEasing::EaseOut;
|
|
309
|
+
} else if (name == "ease-in-out") {
|
|
310
|
+
easing = UIWorkletTimingEasing::EaseInOut;
|
|
311
|
+
} else {
|
|
312
|
+
throw std::invalid_argument("UI worklet timing easing is unsupported.");
|
|
313
|
+
}
|
|
314
|
+
return {.durationMilliseconds = duration, .easing = easing};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
double UIWorkletTiming::evaluateProgress(double elapsedMilliseconds) const {
|
|
318
|
+
if (!std::isfinite(elapsedMilliseconds) || elapsedMilliseconds < 0) {
|
|
319
|
+
throw std::invalid_argument(
|
|
320
|
+
"UI worklet timing elapsed time must be finite and non-negative.");
|
|
321
|
+
}
|
|
322
|
+
const auto progress =
|
|
323
|
+
std::min(1.0, elapsedMilliseconds / durationMilliseconds);
|
|
324
|
+
switch (easing) {
|
|
325
|
+
case UIWorkletTimingEasing::Linear:
|
|
326
|
+
return progress;
|
|
327
|
+
case UIWorkletTimingEasing::EaseIn:
|
|
328
|
+
return progress * progress;
|
|
329
|
+
case UIWorkletTimingEasing::EaseOut:
|
|
330
|
+
return 1 - (1 - progress) * (1 - progress);
|
|
331
|
+
case UIWorkletTimingEasing::EaseInOut:
|
|
332
|
+
return progress < 0.5
|
|
333
|
+
? 2 * progress * progress
|
|
334
|
+
: 1 - ((-2 * progress + 2) * (-2 * progress + 2)) / 2;
|
|
335
|
+
}
|
|
336
|
+
throw std::logic_error("The UI worklet timing easing is invalid.");
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
UIWorkletTimingSequence UIWorkletTimingSequence::parse(
|
|
340
|
+
const Dynamic &value,
|
|
341
|
+
size_t inputCount) {
|
|
342
|
+
if (!value.isArray() || value.empty() ||
|
|
343
|
+
value.size() > MaximumUIWorkletTimingKeyframes) {
|
|
344
|
+
throw std::invalid_argument(
|
|
345
|
+
"UI worklet timing keyframes must contain between 1 and " +
|
|
346
|
+
std::to_string(MaximumUIWorkletTimingKeyframes) + " steps.");
|
|
347
|
+
}
|
|
348
|
+
std::vector<UIWorkletTimingKeyframe> keyframes;
|
|
349
|
+
keyframes.reserve(value.size());
|
|
350
|
+
double totalDurationMilliseconds = 0;
|
|
351
|
+
for (size_t index = 0; index < value.size(); ++index) {
|
|
352
|
+
const auto context =
|
|
353
|
+
"UI worklet timing keyframe " + std::to_string(index);
|
|
354
|
+
const auto &keyframe = value.at(index);
|
|
355
|
+
requireExactObject(keyframe, {"inputs", "timing"}, context);
|
|
356
|
+
const auto &inputValues = keyframe.at("inputs");
|
|
357
|
+
if (!inputValues.isArray() || inputValues.size() != inputCount) {
|
|
358
|
+
throw std::invalid_argument(
|
|
359
|
+
context + " inputs must match the installed graph.");
|
|
360
|
+
}
|
|
361
|
+
std::vector<double> inputs;
|
|
362
|
+
inputs.reserve(inputCount);
|
|
363
|
+
for (size_t inputIndex = 0; inputIndex < inputCount; ++inputIndex) {
|
|
364
|
+
inputs.push_back(requireFiniteNumber(
|
|
365
|
+
inputValues.at(inputIndex),
|
|
366
|
+
context + " input " + std::to_string(inputIndex)));
|
|
367
|
+
}
|
|
368
|
+
auto timing = UIWorkletTiming::parse(keyframe.at("timing"));
|
|
369
|
+
totalDurationMilliseconds += timing.durationMilliseconds;
|
|
370
|
+
if (!std::isfinite(totalDurationMilliseconds) ||
|
|
371
|
+
totalDurationMilliseconds >
|
|
372
|
+
MaximumUIWorkletTimingDurationMilliseconds) {
|
|
373
|
+
throw std::invalid_argument(
|
|
374
|
+
"UI worklet timing keyframes exceed the maximum total duration.");
|
|
375
|
+
}
|
|
376
|
+
keyframes.push_back(UIWorkletTimingKeyframe{
|
|
377
|
+
.inputs = std::move(inputs),
|
|
378
|
+
.timing = timing,
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
.keyframes = std::move(keyframes),
|
|
383
|
+
.totalDurationMilliseconds = totalDurationMilliseconds,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
UIWorkletTimingSequenceSample UIWorkletTimingSequence::evaluate(
|
|
388
|
+
const std::vector<double> &from,
|
|
389
|
+
double elapsedMilliseconds) const {
|
|
390
|
+
if (keyframes.empty() || !std::isfinite(elapsedMilliseconds) ||
|
|
391
|
+
elapsedMilliseconds < 0 ||
|
|
392
|
+
from.size() != keyframes.front().inputs.size()) {
|
|
393
|
+
throw std::invalid_argument(
|
|
394
|
+
"UI worklet timing keyframe evaluation is invalid.");
|
|
395
|
+
}
|
|
396
|
+
if (elapsedMilliseconds >= totalDurationMilliseconds) {
|
|
397
|
+
return {
|
|
398
|
+
.inputs = keyframes.back().inputs,
|
|
399
|
+
.progress = 1,
|
|
400
|
+
.settled = true,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
auto remaining = elapsedMilliseconds;
|
|
404
|
+
double completedDurationMilliseconds = 0;
|
|
405
|
+
const std::vector<double> *segmentFrom = &from;
|
|
406
|
+
for (size_t index = 0; index < keyframes.size(); ++index) {
|
|
407
|
+
const auto &keyframe = keyframes.at(index);
|
|
408
|
+
const auto last = index == keyframes.size() - 1;
|
|
409
|
+
if (!last && remaining >= keyframe.timing.durationMilliseconds) {
|
|
410
|
+
remaining -= keyframe.timing.durationMilliseconds;
|
|
411
|
+
completedDurationMilliseconds += keyframe.timing.durationMilliseconds;
|
|
412
|
+
segmentFrom = &keyframe.inputs;
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
const auto localProgress = keyframe.timing.evaluateProgress(remaining);
|
|
416
|
+
return {
|
|
417
|
+
.inputs = interpolateUIWorkletInputs(
|
|
418
|
+
*segmentFrom, keyframe.inputs, localProgress),
|
|
419
|
+
.progress =
|
|
420
|
+
(completedDurationMilliseconds +
|
|
421
|
+
localProgress * keyframe.timing.durationMilliseconds) /
|
|
422
|
+
totalDurationMilliseconds,
|
|
423
|
+
.settled = false,
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
throw std::logic_error("UI worklet timing keyframes are empty.");
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
UIWorkletSpring UIWorkletSpring::parse(const Dynamic &value) {
|
|
430
|
+
requireExactObject(
|
|
431
|
+
value,
|
|
432
|
+
{
|
|
433
|
+
"mass",
|
|
434
|
+
"stiffness",
|
|
435
|
+
"damping",
|
|
436
|
+
"initialVelocity",
|
|
437
|
+
"restSpeedThreshold",
|
|
438
|
+
"restDisplacementThreshold",
|
|
439
|
+
"maximumDurationMilliseconds",
|
|
440
|
+
},
|
|
441
|
+
"UI worklet spring");
|
|
442
|
+
const auto mass =
|
|
443
|
+
requireFiniteNumber(value.at("mass"), "UI worklet spring mass");
|
|
444
|
+
const auto stiffness = requireFiniteNumber(
|
|
445
|
+
value.at("stiffness"), "UI worklet spring stiffness");
|
|
446
|
+
const auto damping = requireFiniteNumber(
|
|
447
|
+
value.at("damping"), "UI worklet spring damping");
|
|
448
|
+
const auto initialVelocity = requireFiniteNumber(
|
|
449
|
+
value.at("initialVelocity"),
|
|
450
|
+
"UI worklet spring initial velocity");
|
|
451
|
+
const auto restSpeedThreshold = requireFiniteNumber(
|
|
452
|
+
value.at("restSpeedThreshold"),
|
|
453
|
+
"UI worklet spring rest speed threshold");
|
|
454
|
+
const auto restDisplacementThreshold = requireFiniteNumber(
|
|
455
|
+
value.at("restDisplacementThreshold"),
|
|
456
|
+
"UI worklet spring rest displacement threshold");
|
|
457
|
+
const auto maximumDurationMilliseconds = requireFiniteNumber(
|
|
458
|
+
value.at("maximumDurationMilliseconds"),
|
|
459
|
+
"UI worklet spring maximum duration");
|
|
460
|
+
if (mass < MinimumUIWorkletSpringMass ||
|
|
461
|
+
mass > MaximumUIWorkletSpringMass) {
|
|
462
|
+
throw std::invalid_argument(
|
|
463
|
+
"UI worklet spring mass is outside its bounded range.");
|
|
464
|
+
}
|
|
465
|
+
if (stiffness < MinimumUIWorkletSpringStiffness ||
|
|
466
|
+
stiffness > MaximumUIWorkletSpringStiffness) {
|
|
467
|
+
throw std::invalid_argument(
|
|
468
|
+
"UI worklet spring stiffness is outside its bounded range.");
|
|
469
|
+
}
|
|
470
|
+
if (damping < 0 || damping > MaximumUIWorkletSpringDamping) {
|
|
471
|
+
throw std::invalid_argument(
|
|
472
|
+
"UI worklet spring damping is outside its bounded range.");
|
|
473
|
+
}
|
|
474
|
+
if (std::abs(initialVelocity) >
|
|
475
|
+
MaximumUIWorkletSpringInitialVelocity) {
|
|
476
|
+
throw std::invalid_argument(
|
|
477
|
+
"UI worklet spring initial velocity is outside its bounded range.");
|
|
478
|
+
}
|
|
479
|
+
if (restSpeedThreshold <= 0 ||
|
|
480
|
+
restSpeedThreshold > MaximumUIWorkletSpringRestSpeed) {
|
|
481
|
+
throw std::invalid_argument(
|
|
482
|
+
"UI worklet spring rest speed threshold is outside its bounded range.");
|
|
483
|
+
}
|
|
484
|
+
if (restDisplacementThreshold <= 0 ||
|
|
485
|
+
restDisplacementThreshold >= 1) {
|
|
486
|
+
throw std::invalid_argument(
|
|
487
|
+
"UI worklet spring rest displacement threshold must be between zero and one.");
|
|
488
|
+
}
|
|
489
|
+
if (maximumDurationMilliseconds <= 0 ||
|
|
490
|
+
maximumDurationMilliseconds >
|
|
491
|
+
MaximumUIWorkletSpringDurationMilliseconds) {
|
|
492
|
+
throw std::invalid_argument(
|
|
493
|
+
"UI worklet spring maximum duration is outside its bounded range.");
|
|
494
|
+
}
|
|
495
|
+
return {
|
|
496
|
+
.mass = mass,
|
|
497
|
+
.stiffness = stiffness,
|
|
498
|
+
.damping = damping,
|
|
499
|
+
.initialVelocity = initialVelocity,
|
|
500
|
+
.restSpeedThreshold = restSpeedThreshold,
|
|
501
|
+
.restDisplacementThreshold = restDisplacementThreshold,
|
|
502
|
+
.maximumDurationMilliseconds = maximumDurationMilliseconds,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
UIWorkletSpringSample UIWorkletSpring::evaluate(
|
|
507
|
+
double elapsedMilliseconds) const {
|
|
508
|
+
if (!std::isfinite(elapsedMilliseconds) || elapsedMilliseconds < 0) {
|
|
509
|
+
throw std::invalid_argument(
|
|
510
|
+
"UI worklet spring elapsed time must be finite and non-negative.");
|
|
511
|
+
}
|
|
512
|
+
if (elapsedMilliseconds >= maximumDurationMilliseconds) {
|
|
513
|
+
return {.position = 1, .velocity = 0, .settled = true};
|
|
514
|
+
}
|
|
515
|
+
if (elapsedMilliseconds == 0) {
|
|
516
|
+
return {
|
|
517
|
+
.position = 0,
|
|
518
|
+
.velocity = initialVelocity,
|
|
519
|
+
.settled = false,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const auto elapsedSeconds = elapsedMilliseconds / 1000.0;
|
|
524
|
+
const auto naturalFrequency = std::sqrt(stiffness / mass);
|
|
525
|
+
const auto dampingRatio =
|
|
526
|
+
damping / (2 * std::sqrt(stiffness * mass));
|
|
527
|
+
constexpr double InitialDisplacement = -1;
|
|
528
|
+
double displacement;
|
|
529
|
+
double velocity;
|
|
530
|
+
if (dampingRatio < 1 - 1e-7) {
|
|
531
|
+
const auto dampedFrequency =
|
|
532
|
+
naturalFrequency * std::sqrt(1 - dampingRatio * dampingRatio);
|
|
533
|
+
const auto decay = std::exp(
|
|
534
|
+
-dampingRatio * naturalFrequency * elapsedSeconds);
|
|
535
|
+
const auto cosine = std::cos(dampedFrequency * elapsedSeconds);
|
|
536
|
+
const auto sine = std::sin(dampedFrequency * elapsedSeconds);
|
|
537
|
+
const auto sineCoefficient =
|
|
538
|
+
(initialVelocity +
|
|
539
|
+
dampingRatio * naturalFrequency * InitialDisplacement) /
|
|
540
|
+
dampedFrequency;
|
|
541
|
+
const auto oscillation =
|
|
542
|
+
InitialDisplacement * cosine + sineCoefficient * sine;
|
|
543
|
+
displacement = decay * oscillation;
|
|
544
|
+
velocity =
|
|
545
|
+
decay *
|
|
546
|
+
(-dampingRatio * naturalFrequency * oscillation -
|
|
547
|
+
InitialDisplacement * dampedFrequency * sine +
|
|
548
|
+
sineCoefficient * dampedFrequency * cosine);
|
|
549
|
+
} else if (dampingRatio <= 1 + 1e-7) {
|
|
550
|
+
const auto linearCoefficient =
|
|
551
|
+
initialVelocity + naturalFrequency * InitialDisplacement;
|
|
552
|
+
const auto decay = std::exp(-naturalFrequency * elapsedSeconds);
|
|
553
|
+
const auto linear =
|
|
554
|
+
InitialDisplacement + linearCoefficient * elapsedSeconds;
|
|
555
|
+
displacement = linear * decay;
|
|
556
|
+
velocity =
|
|
557
|
+
(linearCoefficient - naturalFrequency * linear) * decay;
|
|
558
|
+
} else {
|
|
559
|
+
const auto radical = std::sqrt(dampingRatio * dampingRatio - 1);
|
|
560
|
+
const auto slowRoot =
|
|
561
|
+
-naturalFrequency / (dampingRatio + radical);
|
|
562
|
+
const auto fastRoot =
|
|
563
|
+
-naturalFrequency * (dampingRatio + radical);
|
|
564
|
+
const auto slowCoefficient =
|
|
565
|
+
(initialVelocity - fastRoot * InitialDisplacement) /
|
|
566
|
+
(slowRoot - fastRoot);
|
|
567
|
+
const auto fastCoefficient = InitialDisplacement - slowCoefficient;
|
|
568
|
+
const auto slowTerm =
|
|
569
|
+
slowCoefficient * std::exp(slowRoot * elapsedSeconds);
|
|
570
|
+
const auto fastTerm =
|
|
571
|
+
fastCoefficient * std::exp(fastRoot * elapsedSeconds);
|
|
572
|
+
displacement = slowTerm + fastTerm;
|
|
573
|
+
velocity = slowRoot * slowTerm + fastRoot * fastTerm;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const auto position = 1 + displacement;
|
|
577
|
+
if (!std::isfinite(position) || !std::isfinite(velocity)) {
|
|
578
|
+
throw std::invalid_argument(
|
|
579
|
+
"UI worklet spring evaluation was not finite.");
|
|
580
|
+
}
|
|
581
|
+
const auto settled =
|
|
582
|
+
std::abs(displacement) <= restDisplacementThreshold &&
|
|
583
|
+
std::abs(velocity) <= restSpeedThreshold;
|
|
584
|
+
return settled
|
|
585
|
+
? UIWorkletSpringSample{.position = 1, .velocity = 0, .settled = true}
|
|
586
|
+
: UIWorkletSpringSample{
|
|
587
|
+
.position = position,
|
|
588
|
+
.velocity = velocity,
|
|
589
|
+
.settled = false,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
UIWorkletDecay UIWorkletDecay::parse(const Dynamic &value) {
|
|
594
|
+
requireExactObject(
|
|
595
|
+
value,
|
|
596
|
+
{
|
|
597
|
+
"deceleration",
|
|
598
|
+
"velocityThreshold",
|
|
599
|
+
"maximumDurationMilliseconds",
|
|
600
|
+
},
|
|
601
|
+
"UI worklet decay");
|
|
602
|
+
const auto deceleration = requireFiniteNumber(
|
|
603
|
+
value.at("deceleration"), "UI worklet decay deceleration");
|
|
604
|
+
const auto velocityThreshold = requireFiniteNumber(
|
|
605
|
+
value.at("velocityThreshold"),
|
|
606
|
+
"UI worklet decay velocity threshold");
|
|
607
|
+
const auto maximumDurationMilliseconds = requireFiniteNumber(
|
|
608
|
+
value.at("maximumDurationMilliseconds"),
|
|
609
|
+
"UI worklet decay maximum duration");
|
|
610
|
+
if (deceleration < MinimumUIWorkletDecayDeceleration ||
|
|
611
|
+
deceleration > MaximumUIWorkletDecayDeceleration) {
|
|
612
|
+
throw std::invalid_argument(
|
|
613
|
+
"UI worklet decay deceleration is outside its bounded range.");
|
|
614
|
+
}
|
|
615
|
+
if (velocityThreshold <= 0 ||
|
|
616
|
+
velocityThreshold > MaximumUIWorkletDecayVelocityThreshold) {
|
|
617
|
+
throw std::invalid_argument(
|
|
618
|
+
"UI worklet decay velocity threshold is outside its bounded range.");
|
|
619
|
+
}
|
|
620
|
+
if (maximumDurationMilliseconds <= 0 ||
|
|
621
|
+
maximumDurationMilliseconds >
|
|
622
|
+
MaximumUIWorkletDecayDurationMilliseconds) {
|
|
623
|
+
throw std::invalid_argument(
|
|
624
|
+
"UI worklet decay maximum duration is outside its bounded range.");
|
|
625
|
+
}
|
|
626
|
+
return {
|
|
627
|
+
.deceleration = deceleration,
|
|
628
|
+
.velocityThreshold = velocityThreshold,
|
|
629
|
+
.maximumDurationMilliseconds = maximumDurationMilliseconds,
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
UIWorkletDecaySample UIWorkletDecay::evaluate(
|
|
634
|
+
double initialSpeed,
|
|
635
|
+
double elapsedMilliseconds) const {
|
|
636
|
+
if (!std::isfinite(initialSpeed) || initialSpeed < 0 ||
|
|
637
|
+
initialSpeed > MaximumUIWorkletDecayVelocity) {
|
|
638
|
+
throw std::invalid_argument(
|
|
639
|
+
"UI worklet decay initial speed is outside its bounded range.");
|
|
640
|
+
}
|
|
641
|
+
if (!std::isfinite(elapsedMilliseconds) || elapsedMilliseconds < 0) {
|
|
642
|
+
throw std::invalid_argument(
|
|
643
|
+
"UI worklet decay elapsed time must be finite and non-negative.");
|
|
644
|
+
}
|
|
645
|
+
const auto thresholdTimeSeconds = initialSpeed <= velocityThreshold
|
|
646
|
+
? 0
|
|
647
|
+
: std::log(initialSpeed / velocityThreshold) / deceleration;
|
|
648
|
+
const auto terminalTimeSeconds = std::min(
|
|
649
|
+
thresholdTimeSeconds, maximumDurationMilliseconds / 1000.0);
|
|
650
|
+
const auto elapsedSeconds = elapsedMilliseconds / 1000.0;
|
|
651
|
+
const auto settled = elapsedSeconds >= terminalTimeSeconds;
|
|
652
|
+
const auto evaluatedSeconds =
|
|
653
|
+
settled ? terminalTimeSeconds : elapsedSeconds;
|
|
654
|
+
const auto velocityFactor =
|
|
655
|
+
std::exp(-deceleration * evaluatedSeconds);
|
|
656
|
+
const auto displacementFactorSeconds =
|
|
657
|
+
-std::expm1(-deceleration * evaluatedSeconds) / deceleration;
|
|
658
|
+
if (!std::isfinite(velocityFactor) ||
|
|
659
|
+
!std::isfinite(displacementFactorSeconds)) {
|
|
660
|
+
throw std::invalid_argument(
|
|
661
|
+
"UI worklet decay evaluation was not finite.");
|
|
662
|
+
}
|
|
663
|
+
return {
|
|
664
|
+
.displacementFactorSeconds = displacementFactorSeconds,
|
|
665
|
+
.velocityFactor = settled ? 0 : velocityFactor,
|
|
666
|
+
.speed = settled ? 0 : initialSpeed * velocityFactor,
|
|
667
|
+
.settled = settled,
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
std::vector<double> interpolateUIWorkletInputs(
|
|
672
|
+
const std::vector<double> &from,
|
|
673
|
+
const std::vector<double> &to,
|
|
674
|
+
double progress) {
|
|
675
|
+
if (from.size() != to.size() || !std::isfinite(progress)) {
|
|
676
|
+
throw std::invalid_argument(
|
|
677
|
+
"UI worklet animation inputs and position are invalid.");
|
|
678
|
+
}
|
|
679
|
+
std::vector<double> values;
|
|
680
|
+
values.reserve(from.size());
|
|
681
|
+
for (size_t index = 0; index < from.size(); ++index) {
|
|
682
|
+
const auto start = from.at(index);
|
|
683
|
+
const auto target = to.at(index);
|
|
684
|
+
if (!std::isfinite(start) || !std::isfinite(target)) {
|
|
685
|
+
throw std::invalid_argument(
|
|
686
|
+
"UI worklet timing inputs must contain finite numbers.");
|
|
687
|
+
}
|
|
688
|
+
const auto value = progress == 0
|
|
689
|
+
? start
|
|
690
|
+
: progress == 1
|
|
691
|
+
? target
|
|
692
|
+
: start * (1 - progress) + target * progress;
|
|
693
|
+
if (!std::isfinite(value)) {
|
|
694
|
+
throw std::invalid_argument(
|
|
695
|
+
"UI worklet timing interpolation was not finite.");
|
|
696
|
+
}
|
|
697
|
+
values.push_back(value);
|
|
698
|
+
}
|
|
699
|
+
return values;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
void UIWorkletFrameStatistics::reset() noexcept {
|
|
703
|
+
frameCount_ = 0;
|
|
704
|
+
firstFrameTimeNanoseconds_ = 0;
|
|
705
|
+
lastFrameTimeNanoseconds_ = 0;
|
|
706
|
+
minimumFrameIntervalNanoseconds_ = 0;
|
|
707
|
+
maximumFrameIntervalNanoseconds_ = 0;
|
|
708
|
+
totalFrameIntervalNanoseconds_ = 0;
|
|
709
|
+
intervalSamples_.clear();
|
|
710
|
+
droppedIntervalSampleCount_ = 0;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
void UIWorkletFrameStatistics::recordFrame(int64_t frameTimeNanoseconds) {
|
|
714
|
+
if (frameTimeNanoseconds < 0) {
|
|
715
|
+
throw std::invalid_argument(
|
|
716
|
+
"UI worklet frame timestamps must be non-negative.");
|
|
717
|
+
}
|
|
718
|
+
if (frameCount_ == 0) {
|
|
719
|
+
frameCount_ = 1;
|
|
720
|
+
firstFrameTimeNanoseconds_ = frameTimeNanoseconds;
|
|
721
|
+
lastFrameTimeNanoseconds_ = frameTimeNanoseconds;
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
if (frameTimeNanoseconds <= lastFrameTimeNanoseconds_) {
|
|
725
|
+
throw std::invalid_argument(
|
|
726
|
+
"UI worklet timing frame timestamps must be strictly increasing.");
|
|
727
|
+
}
|
|
728
|
+
const auto interval = frameTimeNanoseconds - lastFrameTimeNanoseconds_;
|
|
729
|
+
if (interval >
|
|
730
|
+
std::numeric_limits<int64_t>::max() -
|
|
731
|
+
totalFrameIntervalNanoseconds_) {
|
|
732
|
+
throw std::overflow_error(
|
|
733
|
+
"UI worklet timing frame intervals overflowed.");
|
|
734
|
+
}
|
|
735
|
+
frameCount_++;
|
|
736
|
+
lastFrameTimeNanoseconds_ = frameTimeNanoseconds;
|
|
737
|
+
totalFrameIntervalNanoseconds_ += interval;
|
|
738
|
+
if (minimumFrameIntervalNanoseconds_ == 0 ||
|
|
739
|
+
interval < minimumFrameIntervalNanoseconds_) {
|
|
740
|
+
minimumFrameIntervalNanoseconds_ = interval;
|
|
741
|
+
}
|
|
742
|
+
maximumFrameIntervalNanoseconds_ =
|
|
743
|
+
std::max(maximumFrameIntervalNanoseconds_, interval);
|
|
744
|
+
if (intervalSamples_.size() < MaximumUIWorkletFrameIntervalSamples) {
|
|
745
|
+
intervalSamples_.push_back(interval);
|
|
746
|
+
} else {
|
|
747
|
+
droppedIntervalSampleCount_++;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
bool UIWorkletFrameStatistics::empty() const noexcept {
|
|
752
|
+
return frameCount_ == 0;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
UIWorkletFrameStatisticsSnapshot UIWorkletFrameStatistics::snapshot() const {
|
|
756
|
+
if (empty()) {
|
|
757
|
+
throw std::logic_error(
|
|
758
|
+
"Empty UI worklet frame statistics cannot be inspected.");
|
|
759
|
+
}
|
|
760
|
+
auto samples = intervalSamples_;
|
|
761
|
+
std::sort(samples.begin(), samples.end());
|
|
762
|
+
const auto quantile = [&](double value) -> int64_t {
|
|
763
|
+
if (samples.empty()) return 0;
|
|
764
|
+
const auto index = static_cast<size_t>(std::max(
|
|
765
|
+
0.0,
|
|
766
|
+
std::ceil(static_cast<double>(samples.size()) * value) - 1));
|
|
767
|
+
return samples.at(index);
|
|
768
|
+
};
|
|
769
|
+
constexpr double NanosecondsToMilliseconds = 1.0 / 1000000.0;
|
|
770
|
+
const auto intervalCount = frameCount_ - 1;
|
|
771
|
+
return {
|
|
772
|
+
.frameCount = frameCount_,
|
|
773
|
+
.intervalCount = intervalCount,
|
|
774
|
+
.sampledIntervalCount = intervalSamples_.size(),
|
|
775
|
+
.droppedIntervalSampleCount = droppedIntervalSampleCount_,
|
|
776
|
+
.firstFrameTimeMilliseconds =
|
|
777
|
+
static_cast<double>(firstFrameTimeNanoseconds_) *
|
|
778
|
+
NanosecondsToMilliseconds,
|
|
779
|
+
.lastFrameTimeMilliseconds =
|
|
780
|
+
static_cast<double>(lastFrameTimeNanoseconds_) *
|
|
781
|
+
NanosecondsToMilliseconds,
|
|
782
|
+
.minimumFrameIntervalMilliseconds =
|
|
783
|
+
static_cast<double>(minimumFrameIntervalNanoseconds_) *
|
|
784
|
+
NanosecondsToMilliseconds,
|
|
785
|
+
.maximumFrameIntervalMilliseconds =
|
|
786
|
+
static_cast<double>(maximumFrameIntervalNanoseconds_) *
|
|
787
|
+
NanosecondsToMilliseconds,
|
|
788
|
+
.meanFrameIntervalMilliseconds = intervalCount == 0
|
|
789
|
+
? 0
|
|
790
|
+
: static_cast<double>(totalFrameIntervalNanoseconds_) /
|
|
791
|
+
static_cast<double>(intervalCount) *
|
|
792
|
+
NanosecondsToMilliseconds,
|
|
793
|
+
.p50FrameIntervalMilliseconds =
|
|
794
|
+
static_cast<double>(quantile(0.5)) * NanosecondsToMilliseconds,
|
|
795
|
+
.p95FrameIntervalMilliseconds =
|
|
796
|
+
static_cast<double>(quantile(0.95)) * NanosecondsToMilliseconds,
|
|
797
|
+
.p99FrameIntervalMilliseconds =
|
|
798
|
+
static_cast<double>(quantile(0.99)) * NanosecondsToMilliseconds,
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
UIWorkletGraph::UIWorkletGraph(
|
|
803
|
+
std::vector<std::string> inputNames,
|
|
804
|
+
std::vector<double> initialInputs,
|
|
805
|
+
std::vector<OutputProgram> outputs)
|
|
806
|
+
: inputNames_(std::move(inputNames)),
|
|
807
|
+
initialInputs_(std::move(initialInputs)),
|
|
808
|
+
outputs_(std::move(outputs)) {
|
|
809
|
+
outputNames_.reserve(outputs_.size());
|
|
810
|
+
for (const auto &output : outputs_) outputNames_.push_back(output.name);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
UIWorkletGraph UIWorkletGraph::parse(const Dynamic &value) {
|
|
814
|
+
requireExactObject(
|
|
815
|
+
value, {"protocolVersion", "inputs", "outputs"}, "UI worklet graph");
|
|
816
|
+
if (!value.at("protocolVersion").isNumber() ||
|
|
817
|
+
value.at("protocolVersion").asDouble() != UIWorkletProtocolVersion) {
|
|
818
|
+
throw std::invalid_argument(
|
|
819
|
+
"Unsupported UI worklet protocol version.");
|
|
820
|
+
}
|
|
821
|
+
const auto &inputValues = value.at("inputs");
|
|
822
|
+
if (!inputValues.isArray()) {
|
|
823
|
+
throw std::invalid_argument("UI worklet graph inputs must be an array.");
|
|
824
|
+
}
|
|
825
|
+
if (inputValues.size() > MaximumUIWorkletInputs) {
|
|
826
|
+
throw std::invalid_argument(
|
|
827
|
+
"UI worklet graphs support at most " +
|
|
828
|
+
std::to_string(MaximumUIWorkletInputs) + " inputs.");
|
|
829
|
+
}
|
|
830
|
+
std::vector<std::string> inputNames;
|
|
831
|
+
std::vector<double> initialInputs;
|
|
832
|
+
std::unordered_map<std::string, size_t> inputs;
|
|
833
|
+
inputNames.reserve(inputValues.size());
|
|
834
|
+
initialInputs.reserve(inputValues.size());
|
|
835
|
+
for (size_t index = 0; index < inputValues.size(); ++index) {
|
|
836
|
+
const auto context = "UI worklet input " + std::to_string(index);
|
|
837
|
+
const auto &input = inputValues.at(index);
|
|
838
|
+
requireExactObject(input, {"name", "initialValue"}, context);
|
|
839
|
+
auto name = requireIdentifier(input.at("name"), context + ".name");
|
|
840
|
+
if (inputs.contains(name)) {
|
|
841
|
+
throw std::invalid_argument("Duplicate UI worklet input " + name + ".");
|
|
842
|
+
}
|
|
843
|
+
inputs.emplace(name, index);
|
|
844
|
+
inputNames.push_back(std::move(name));
|
|
845
|
+
initialInputs.push_back(requireFiniteNumber(
|
|
846
|
+
input.at("initialValue"), context + ".initialValue"));
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
const auto &outputValues = value.at("outputs");
|
|
850
|
+
if (!outputValues.isArray() || outputValues.empty()) {
|
|
851
|
+
throw std::invalid_argument(
|
|
852
|
+
"UI worklet graph outputs must be a non-empty array.");
|
|
853
|
+
}
|
|
854
|
+
if (outputValues.size() > MaximumUIWorkletOutputs) {
|
|
855
|
+
throw std::invalid_argument(
|
|
856
|
+
"UI worklet graphs support at most " +
|
|
857
|
+
std::to_string(MaximumUIWorkletOutputs) + " outputs.");
|
|
858
|
+
}
|
|
859
|
+
std::unordered_set<std::string> outputNames;
|
|
860
|
+
std::vector<OutputProgram> outputs;
|
|
861
|
+
outputs.reserve(outputValues.size());
|
|
862
|
+
ParseBudget budget;
|
|
863
|
+
for (size_t index = 0; index < outputValues.size(); ++index) {
|
|
864
|
+
const auto context = "UI worklet output " + std::to_string(index);
|
|
865
|
+
const auto &output = outputValues.at(index);
|
|
866
|
+
requireExactObject(output, {"name", "expression"}, context);
|
|
867
|
+
auto name = requireIdentifier(output.at("name"), context + ".name");
|
|
868
|
+
if (!outputNames.insert(name).second) {
|
|
869
|
+
throw std::invalid_argument("Duplicate UI worklet output " + name + ".");
|
|
870
|
+
}
|
|
871
|
+
std::vector<Instruction> instructions;
|
|
872
|
+
parseExpression(
|
|
873
|
+
output.at("expression"),
|
|
874
|
+
inputs,
|
|
875
|
+
budget,
|
|
876
|
+
1,
|
|
877
|
+
context + ".expression",
|
|
878
|
+
instructions);
|
|
879
|
+
outputs.push_back({
|
|
880
|
+
.name = std::move(name),
|
|
881
|
+
.instructions = std::move(instructions),
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
return UIWorkletGraph(
|
|
885
|
+
std::move(inputNames),
|
|
886
|
+
std::move(initialInputs),
|
|
887
|
+
std::move(outputs));
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
size_t UIWorkletGraph::inputCount() const noexcept {
|
|
891
|
+
return inputNames_.size();
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
const std::vector<std::string> &UIWorkletGraph::inputNames() const noexcept {
|
|
895
|
+
return inputNames_;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
const std::vector<double> &UIWorkletGraph::initialInputs() const noexcept {
|
|
899
|
+
return initialInputs_;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
const std::vector<std::string> &UIWorkletGraph::outputNames() const noexcept {
|
|
903
|
+
return outputNames_;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
std::vector<UIWorkletOutputValue> UIWorkletGraph::evaluate(
|
|
907
|
+
const std::vector<double> &inputs) const {
|
|
908
|
+
if (inputs.size() != inputNames_.size() ||
|
|
909
|
+
std::any_of(inputs.begin(), inputs.end(), [](double value) {
|
|
910
|
+
return !std::isfinite(value);
|
|
911
|
+
})) {
|
|
912
|
+
throw std::invalid_argument(
|
|
913
|
+
"UI worklet input vectors must match the installed graph and contain finite numbers.");
|
|
914
|
+
}
|
|
915
|
+
std::vector<UIWorkletOutputValue> values;
|
|
916
|
+
values.reserve(outputs_.size());
|
|
917
|
+
std::vector<double> stack;
|
|
918
|
+
for (const auto &output : outputs_) {
|
|
919
|
+
stack.clear();
|
|
920
|
+
stack.reserve(output.instructions.size());
|
|
921
|
+
for (const auto &instruction : output.instructions) {
|
|
922
|
+
switch (instruction.operation) {
|
|
923
|
+
case Operation::Constant:
|
|
924
|
+
stack.push_back(instruction.first);
|
|
925
|
+
break;
|
|
926
|
+
case Operation::Input:
|
|
927
|
+
stack.push_back(inputs.at(instruction.input));
|
|
928
|
+
break;
|
|
929
|
+
case Operation::Absolute:
|
|
930
|
+
stack.push_back(std::abs(pop(stack)));
|
|
931
|
+
break;
|
|
932
|
+
case Operation::Negate:
|
|
933
|
+
stack.push_back(-pop(stack));
|
|
934
|
+
break;
|
|
935
|
+
case Operation::Clamp: {
|
|
936
|
+
const auto value = pop(stack);
|
|
937
|
+
stack.push_back(std::min(
|
|
938
|
+
instruction.second, std::max(instruction.first, value)));
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
case Operation::Interpolate: {
|
|
942
|
+
const auto value = pop(stack);
|
|
943
|
+
auto progress = (value - instruction.first) /
|
|
944
|
+
(instruction.second - instruction.first);
|
|
945
|
+
if (instruction.clamp) {
|
|
946
|
+
progress = std::min(1.0, std::max(0.0, progress));
|
|
947
|
+
}
|
|
948
|
+
stack.push_back(
|
|
949
|
+
instruction.third +
|
|
950
|
+
progress * (instruction.fourth - instruction.third));
|
|
951
|
+
break;
|
|
952
|
+
}
|
|
953
|
+
case Operation::Add:
|
|
954
|
+
case Operation::Subtract:
|
|
955
|
+
case Operation::Multiply:
|
|
956
|
+
case Operation::Divide:
|
|
957
|
+
case Operation::Minimum:
|
|
958
|
+
case Operation::Maximum: {
|
|
959
|
+
const auto right = pop(stack);
|
|
960
|
+
const auto left = pop(stack);
|
|
961
|
+
if (instruction.operation == Operation::Divide && right == 0) {
|
|
962
|
+
throw std::invalid_argument("UI worklet division by zero.");
|
|
963
|
+
}
|
|
964
|
+
if (instruction.operation == Operation::Add) {
|
|
965
|
+
stack.push_back(left + right);
|
|
966
|
+
} else if (instruction.operation == Operation::Subtract) {
|
|
967
|
+
stack.push_back(left - right);
|
|
968
|
+
} else if (instruction.operation == Operation::Multiply) {
|
|
969
|
+
stack.push_back(left * right);
|
|
970
|
+
} else if (instruction.operation == Operation::Divide) {
|
|
971
|
+
stack.push_back(left / right);
|
|
972
|
+
} else if (instruction.operation == Operation::Minimum) {
|
|
973
|
+
stack.push_back(std::min(left, right));
|
|
974
|
+
} else {
|
|
975
|
+
stack.push_back(std::max(left, right));
|
|
976
|
+
}
|
|
977
|
+
break;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
if (stack.size() != 1 || !std::isfinite(stack.back())) {
|
|
982
|
+
throw std::invalid_argument(
|
|
983
|
+
"UI worklet output " + output.name + " was not finite.");
|
|
984
|
+
}
|
|
985
|
+
values.push_back({.name = output.name, .value = stack.back()});
|
|
986
|
+
}
|
|
987
|
+
return values;
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
UIWorkletPanGesture UIWorkletPanGesture::parse(
|
|
991
|
+
const Dynamic &value,
|
|
992
|
+
const UIWorkletGraph &graph) {
|
|
993
|
+
if (!value.isObject()) {
|
|
994
|
+
throw std::invalid_argument(
|
|
995
|
+
"UI worklet pan gesture must be a plain object.");
|
|
996
|
+
}
|
|
997
|
+
const auto hasReleaseDecay = value.count("releaseDecay") != 0;
|
|
998
|
+
requireExactObject(
|
|
999
|
+
value,
|
|
1000
|
+
hasReleaseDecay
|
|
1001
|
+
? std::initializer_list<std::string_view>{
|
|
1002
|
+
"xInput",
|
|
1003
|
+
"yInput",
|
|
1004
|
+
"minX",
|
|
1005
|
+
"maxX",
|
|
1006
|
+
"minY",
|
|
1007
|
+
"maxY",
|
|
1008
|
+
"releaseDecay",
|
|
1009
|
+
}
|
|
1010
|
+
: std::initializer_list<std::string_view>{
|
|
1011
|
+
"xInput", "yInput", "minX", "maxX", "minY", "maxY"},
|
|
1012
|
+
"UI worklet pan gesture");
|
|
1013
|
+
const auto xInputName =
|
|
1014
|
+
requireIdentifier(value.at("xInput"), "UI worklet pan xInput");
|
|
1015
|
+
const auto yInputName =
|
|
1016
|
+
requireIdentifier(value.at("yInput"), "UI worklet pan yInput");
|
|
1017
|
+
if (xInputName == yInputName) {
|
|
1018
|
+
throw std::invalid_argument("UI worklet pan inputs must be distinct.");
|
|
1019
|
+
}
|
|
1020
|
+
const auto &inputNames = graph.inputNames();
|
|
1021
|
+
const auto xEntry = std::find(
|
|
1022
|
+
inputNames.begin(), inputNames.end(), xInputName);
|
|
1023
|
+
const auto yEntry = std::find(
|
|
1024
|
+
inputNames.begin(), inputNames.end(), yInputName);
|
|
1025
|
+
if (xEntry == inputNames.end()) {
|
|
1026
|
+
throw std::invalid_argument(
|
|
1027
|
+
"Unknown UI worklet pan input " + xInputName + ".");
|
|
1028
|
+
}
|
|
1029
|
+
if (yEntry == inputNames.end()) {
|
|
1030
|
+
throw std::invalid_argument(
|
|
1031
|
+
"Unknown UI worklet pan input " + yInputName + ".");
|
|
1032
|
+
}
|
|
1033
|
+
const auto minX =
|
|
1034
|
+
requireFiniteNumber(value.at("minX"), "UI worklet pan minX");
|
|
1035
|
+
const auto maxX =
|
|
1036
|
+
requireFiniteNumber(value.at("maxX"), "UI worklet pan maxX");
|
|
1037
|
+
const auto minY =
|
|
1038
|
+
requireFiniteNumber(value.at("minY"), "UI worklet pan minY");
|
|
1039
|
+
const auto maxY =
|
|
1040
|
+
requireFiniteNumber(value.at("maxY"), "UI worklet pan maxY");
|
|
1041
|
+
if (maxX < minX || maxY < minY) {
|
|
1042
|
+
throw std::invalid_argument(
|
|
1043
|
+
"UI worklet pan bounds must be ascending.");
|
|
1044
|
+
}
|
|
1045
|
+
return {
|
|
1046
|
+
.xInput = static_cast<size_t>(
|
|
1047
|
+
std::distance(inputNames.begin(), xEntry)),
|
|
1048
|
+
.yInput = static_cast<size_t>(
|
|
1049
|
+
std::distance(inputNames.begin(), yEntry)),
|
|
1050
|
+
.minX = minX,
|
|
1051
|
+
.maxX = maxX,
|
|
1052
|
+
.minY = minY,
|
|
1053
|
+
.maxY = maxY,
|
|
1054
|
+
.releaseDecay = hasReleaseDecay
|
|
1055
|
+
? std::optional<UIWorkletDecay>{
|
|
1056
|
+
UIWorkletDecay::parse(value.at("releaseDecay"))}
|
|
1057
|
+
: std::nullopt,
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
std::vector<double> UIWorkletPanGesture::evaluateInputs(
|
|
1062
|
+
const std::vector<double> ¤tInputs,
|
|
1063
|
+
double originX,
|
|
1064
|
+
double originY,
|
|
1065
|
+
double translationX,
|
|
1066
|
+
double translationY) const {
|
|
1067
|
+
if (xInput >= currentInputs.size() || yInput >= currentInputs.size() ||
|
|
1068
|
+
!std::isfinite(originX) || !std::isfinite(originY) ||
|
|
1069
|
+
!std::isfinite(translationX) || !std::isfinite(translationY)) {
|
|
1070
|
+
throw std::invalid_argument("UI worklet pan inputs are invalid.");
|
|
1071
|
+
}
|
|
1072
|
+
auto inputs = currentInputs;
|
|
1073
|
+
inputs.at(xInput) =
|
|
1074
|
+
std::min(maxX, std::max(minX, originX + translationX));
|
|
1075
|
+
inputs.at(yInput) =
|
|
1076
|
+
std::min(maxY, std::max(minY, originY + translationY));
|
|
1077
|
+
return inputs;
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
} // namespace solid_native::worklets
|