-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
NativeProxy.cpp
382 lines (334 loc) · 13 KB
/
NativeProxy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <fbjni/fbjni.h>
#include <jsi/JSIDynamic.h>
#include <jsi/jsi.h>
#include <react/jni/ReadableNativeArray.h>
#include <react/jni/ReadableNativeMap.h>
#include <memory>
#include <string>
#if JS_RUNTIME_HERMES
#include <hermes/hermes.h>
#elif JS_RUNTIME_V8
#include <v8runtime/V8RuntimeFactory.h>
#else
#include <jsi/JSCRuntime.h>
#endif
#include <android/log.h>
#include "AndroidErrorHandler.h"
#include "AndroidScheduler.h"
#include "LayoutAnimationsProxy.h"
#include "NativeProxy.h"
#include "PlatformDepMethodsHolder.h"
namespace reanimated {
using namespace facebook;
using namespace react;
NativeProxy::NativeProxy(
jni::alias_ref<NativeProxy::javaobject> jThis,
jsi::Runtime *rt,
std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker,
std::shared_ptr<Scheduler> scheduler,
jni::global_ref<LayoutAnimations::javaobject> _layoutAnimations)
: javaPart_(jni::make_global(jThis)),
runtime_(rt),
jsCallInvoker_(jsCallInvoker),
scheduler_(scheduler),
layoutAnimations(std::move(_layoutAnimations)) {}
NativeProxy::~NativeProxy() {
// removed temporary, new event listener mechanism need fix on the RN side
// reactScheduler_->removeEventListener(eventListener_);
}
jni::local_ref<NativeProxy::jhybriddata> NativeProxy::initHybrid(
jni::alias_ref<jhybridobject> jThis,
jlong jsContext,
jni::alias_ref<facebook::react::CallInvokerHolder::javaobject>
jsCallInvokerHolder,
jni::alias_ref<AndroidScheduler::javaobject> androidScheduler,
jni::alias_ref<LayoutAnimations::javaobject> layoutAnimations) {
auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
auto scheduler = androidScheduler->cthis()->getScheduler();
scheduler->setJSCallInvoker(jsCallInvoker);
return makeCxxInstance(
jThis,
(jsi::Runtime *)jsContext,
jsCallInvoker,
scheduler,
make_global(layoutAnimations));
}
void NativeProxy::installJSIBindings() {
auto propUpdater = [this](
jsi::Runtime &rt,
int viewTag,
const jsi::Value &viewName,
const jsi::Object &props) {
// viewName is for iOS only, we skip it here
this->updateProps(rt, viewTag, props);
};
auto getCurrentTime = [this]() {
auto method =
javaPart_->getClass()->getMethod<local_ref<JString>()>("getUptime");
local_ref<JString> output = method(javaPart_.get());
return static_cast<double>(
std::strtoll(output->toStdString().c_str(), NULL, 10));
};
auto requestRender = [this, getCurrentTime](
std::function<void(double)> onRender,
jsi::Runtime &rt) {
// doNoUse -> NodesManager passes here a timestamp from choreographer which
// is useless for us as we use diffrent timer to better handle events. The
// lambda is translated to NodeManager.OnAnimationFrame and treated just
// like reanimated 1 frame callbacks which make use of the timestamp.
auto wrappedOnRender = [getCurrentTime, &rt, onRender](double doNotUse) {
jsi::Object global = rt.global();
jsi::String frameTimestampName =
jsi::String::createFromAscii(rt, "_frameTimestamp");
double frameTimestamp = getCurrentTime();
global.setProperty(rt, frameTimestampName, frameTimestamp);
onRender(frameTimestamp);
global.setProperty(rt, frameTimestampName, jsi::Value::undefined());
};
this->requestRender(std::move(wrappedOnRender));
};
auto propObtainer = [this](
jsi::Runtime &rt,
const int viewTag,
const jsi::String &propName) -> jsi::Value {
auto method =
javaPart_->getClass()
->getMethod<jni::local_ref<JString>(int, jni::local_ref<JString>)>(
"obtainProp");
local_ref<JString> propNameJStr =
jni::make_jstring(propName.utf8(rt).c_str());
auto result = method(javaPart_.get(), viewTag, propNameJStr);
std::string str = result->toStdString();
return jsi::Value(rt, jsi::String::createFromAscii(rt, str.c_str()));
};
auto measuringFunction =
[this](int viewTag) -> std::vector<std::pair<std::string, double>> {
return measure(viewTag);
};
auto scrollToFunction =
[this](int viewTag, double x, double y, bool animated) -> void {
scrollTo(viewTag, x, y, animated);
};
auto registerSensorFunction =
[this](int sensorType, int interval, std::function<void(double[])> setter)
-> int {
return this->registerSensor(sensorType, interval, std::move(setter));
};
auto unregisterSensorFunction = [this](int sensorId) {
unregisterSensor(sensorId);
};
auto setGestureStateFunction = [this](int handlerTag, int newState) -> void {
setGestureState(handlerTag, newState);
};
auto subscribeForKeyboardEventsFunction =
[this](std::function<void(int, int)> keyboardEventDataUpdater) -> int {
return subscribeForKeyboardEvents(std::move(keyboardEventDataUpdater));
};
auto unsubscribeFromKeyboardEventsFunction = [this](int listenerId) -> void {
unsubscribeFromKeyboardEvents(listenerId);
};
#if JS_RUNTIME_HERMES
auto config =
::hermes::vm::RuntimeConfig::Builder().withEnableSampleProfiling(false);
std::shared_ptr<jsi::Runtime> animatedRuntime =
facebook::hermes::makeHermesRuntime(config.build());
#elif JS_RUNTIME_V8
auto config = std::make_unique<rnv8::V8RuntimeConfig>();
config->enableInspector = false;
config->appName = "reanimated";
std::shared_ptr<jsi::Runtime> animatedRuntime =
rnv8::createSharedV8Runtime(runtime_, std::move(config));
#else
std::shared_ptr<jsi::Runtime> animatedRuntime =
facebook::jsc::makeJSCRuntime();
#endif
auto workletRuntimeValue =
runtime_->global()
.getProperty(*runtime_, "ArrayBuffer")
.asObject(*runtime_)
.asFunction(*runtime_)
.callAsConstructor(*runtime_, {static_cast<double>(sizeof(void *))});
uintptr_t *workletRuntimeData = reinterpret_cast<uintptr_t *>(
workletRuntimeValue.getObject(*runtime_).getArrayBuffer(*runtime_).data(
*runtime_));
workletRuntimeData[0] = reinterpret_cast<uintptr_t>(animatedRuntime.get());
runtime_->global().setProperty(
*runtime_, "_WORKLET_RUNTIME", workletRuntimeValue);
std::shared_ptr<ErrorHandler> errorHandler =
std::make_shared<AndroidErrorHandler>(scheduler_);
// Layout Animations Start
auto notifyAboutProgress = [=](int tag, jsi::Value progress) {
this->layoutAnimations->cthis()->notifyAboutProgress(progress, tag);
};
auto notifyAboutEnd = [=](int tag, bool isCancelled) {
this->layoutAnimations->cthis()->notifyAboutEnd(tag, (isCancelled) ? 1 : 0);
};
auto configurePropsFunction = [=](jsi::Runtime &rt,
const jsi::Value &uiProps,
const jsi::Value &nativeProps) {
this->configureProps(rt, uiProps, nativeProps);
};
std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy =
std::make_shared<LayoutAnimationsProxy>(
notifyAboutProgress, notifyAboutEnd);
std::weak_ptr<jsi::Runtime> wrt = animatedRuntime;
layoutAnimations->cthis()->setWeakUIRuntime(wrt);
// Layout Animations End
PlatformDepMethodsHolder platformDepMethodsHolder = {
requestRender,
propUpdater,
scrollToFunction,
measuringFunction,
getCurrentTime,
registerSensorFunction,
unregisterSensorFunction,
setGestureStateFunction,
configurePropsFunction,
subscribeForKeyboardEventsFunction,
unsubscribeFromKeyboardEventsFunction,
};
auto module = std::make_shared<NativeReanimatedModule>(
jsCallInvoker_,
scheduler_,
animatedRuntime,
errorHandler,
propObtainer,
layoutAnimationsProxy,
platformDepMethodsHolder);
_nativeReanimatedModule = module;
std::weak_ptr<NativeReanimatedModule> weakModule = module;
this->registerEventHandler(
[weakModule, getCurrentTime](
std::string eventName, std::string eventAsString) {
if (auto module = weakModule.lock()) {
jsi::Object global = module->runtime->global();
jsi::String eventTimestampName =
jsi::String::createFromAscii(*module->runtime, "_eventTimestamp");
global.setProperty(
*module->runtime, eventTimestampName, getCurrentTime());
module->onEvent(eventName, eventAsString);
global.setProperty(
*module->runtime, eventTimestampName, jsi::Value::undefined());
}
});
runtime_->global().setProperty(
*runtime_,
jsi::PropNameID::forAscii(*runtime_, "__reanimatedModuleProxy"),
jsi::Object::createFromHostObject(*runtime_, module));
}
bool NativeProxy::isAnyHandlerWaitingForEvent(std::string s) {
return _nativeReanimatedModule->isAnyHandlerWaitingForEvent(s);
}
void NativeProxy::registerNatives() {
registerHybrid(
{makeNativeMethod("initHybrid", NativeProxy::initHybrid),
makeNativeMethod("installJSIBindings", NativeProxy::installJSIBindings),
makeNativeMethod(
"isAnyHandlerWaitingForEvent",
NativeProxy::isAnyHandlerWaitingForEvent)});
}
void NativeProxy::requestRender(std::function<void(double)> onRender) {
static auto method =
javaPart_->getClass()
->getMethod<void(AnimationFrameCallback::javaobject)>(
"requestRender");
method(
javaPart_.get(),
AnimationFrameCallback::newObjectCxxArgs(std::move(onRender)).get());
}
void NativeProxy::registerEventHandler(
std::function<void(std::string, std::string)> handler) {
static auto method =
javaPart_->getClass()->getMethod<void(EventHandler::javaobject)>(
"registerEventHandler");
method(
javaPart_.get(),
EventHandler::newObjectCxxArgs(std::move(handler)).get());
}
void NativeProxy::updateProps(
jsi::Runtime &rt,
int viewTag,
const jsi::Object &props) {
auto method = javaPart_->getClass()
->getMethod<void(int, JMap<JString, JObject>::javaobject)>(
"updateProps");
method(
javaPart_.get(), viewTag, JNIHelper::ConvertToPropsMap(rt, props).get());
}
void NativeProxy::scrollTo(int viewTag, double x, double y, bool animated) {
auto method =
javaPart_->getClass()->getMethod<void(int, double, double, bool)>(
"scrollTo");
method(javaPart_.get(), viewTag, x, y, animated);
}
std::vector<std::pair<std::string, double>> NativeProxy::measure(int viewTag) {
auto method =
javaPart_->getClass()->getMethod<local_ref<JArrayFloat>(int)>("measure");
local_ref<JArrayFloat> output = method(javaPart_.get(), viewTag);
size_t size = output->size();
auto elements = output->getRegion(0, size);
std::vector<std::pair<std::string, double>> result;
result.push_back({"x", elements[0]});
result.push_back({"y", elements[1]});
result.push_back({"pageX", elements[2]});
result.push_back({"pageY", elements[3]});
result.push_back({"width", elements[4]});
result.push_back({"height", elements[5]});
return result;
}
int NativeProxy::registerSensor(
int sensorType,
int interval,
std::function<void(double[])> setter) {
static auto method =
javaPart_->getClass()->getMethod<int(int, int, SensorSetter::javaobject)>(
"registerSensor");
return method(
javaPart_.get(),
sensorType,
interval,
SensorSetter::newObjectCxxArgs(std::move(setter)).get());
}
void NativeProxy::unregisterSensor(int sensorId) {
auto method = javaPart_->getClass()->getMethod<void(int)>("unregisterSensor");
method(javaPart_.get(), sensorId);
}
void NativeProxy::setGestureState(int handlerTag, int newState) {
auto method =
javaPart_->getClass()->getMethod<void(int, int)>("setGestureState");
method(javaPart_.get(), handlerTag, newState);
}
void NativeProxy::configureProps(
jsi::Runtime &rt,
const jsi::Value &uiProps,
const jsi::Value &nativeProps) {
auto method = javaPart_->getClass()
->getMethod<void(
ReadableNativeArray::javaobject,
ReadableNativeArray::javaobject)>("configureProps");
method(
javaPart_.get(),
ReadableNativeArray::newObjectCxxArgs(
std::move(jsi::dynamicFromValue(rt, uiProps)))
.get(),
ReadableNativeArray::newObjectCxxArgs(
std::move(jsi::dynamicFromValue(rt, nativeProps)))
.get());
}
int NativeProxy::subscribeForKeyboardEvents(
std::function<void(int, int)> keyboardEventDataUpdater) {
auto method = javaPart_->getClass()
->getMethod<int(KeyboardEventDataUpdater::javaobject)>(
"subscribeForKeyboardEvents");
return method(
javaPart_.get(),
KeyboardEventDataUpdater::newObjectCxxArgs(
std::move(keyboardEventDataUpdater))
.get());
}
void NativeProxy::unsubscribeFromKeyboardEvents(int listenerId) {
auto method = javaPart_->getClass()->getMethod<void(int)>(
"unsubscribeFromKeyboardEvents");
method(javaPart_.get(), listenerId);
}
} // namespace reanimated