-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathAnimationFrame.mm
More file actions
360 lines (324 loc) · 12.4 KB
/
Copy pathAnimationFrame.mm
File metadata and controls
360 lines (324 loc) · 12.4 KB
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
//
// AnimationFrame.mm
// NativeScript
//
#include "AnimationFrame.hpp"
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#include <memory>
#include <vector>
#include "Caches.h"
#include "Helpers.h"
#include "IsolateWrapper.h"
#include "ModuleBinding.hpp"
#include "Runtime.h"
#include "robin_hood.h"
/*
* Frame callbacks ride one CADisplayLink per isolate, created paused during
* isolate initialization on the isolate's home thread and attached to that
* thread's runloop in the common modes (so ticks keep arriving during
* scroll/tracking, matching the EventLoop's timers). Posting unpauses the
* link; an empty registry pauses it again so an idle isolate never wakes per
* frame. All access — post, remove, dispatch, teardown — happens on the home
* thread, so the registry needs no locking.
*
* Two front-ends share the registry, matching the Android runtime's contract:
* - requestAnimationFrame(fn) / cancelAnimationFrame(handle): the standard
* surface. Every request posts its own anonymous one-shot entry addressed
* only by the returned handle (no dedup by function), and fn receives a
* single DOMHighResTimeStamp on the isolate's performance timeline.
* cancelAnimationFrame may only touch entries carrying the raf flag.
* - __postFrameCallback(fn[, delayMillis]) / __removeFrameCallback(fn): the
* compatibility surface predating requestAnimationFrame. One-shot, deduped
* by function identity (the entry id is stamped on the function as a
* private value): re-posting a pending callback is a no-op, delay included.
* fn(frameTimeNanos, performanceMillis) gets the raw frame time as well. A
* delayed entry fires on the first frame after its delay elapses.
*
* Each tick dispatches only the entries scheduled before it (the order list
* is swapped out first), so a callback that re-posts runs again next frame,
* never in the same batch. Every callback in a batch observes the same
* timestamps. CADisplayLink.timestamp shares mach_absolute_time as its base
* with the V8 platform clock (and CACurrentMediaTime), so it maps onto the
* performance timeline via Runtime::TimeOriginMonotonicSeconds() — never
* re-sample the clock at dispatch, the vsync instant is the contract.
*/
using namespace v8;
namespace tns {
class AnimationFrameState;
}
@interface TNSAnimationFrameTarget : NSObject {
@public
tns::AnimationFrameState* state_;
}
- (void)onFrame:(CADisplayLink*)link;
@end
namespace tns {
static constexpr const char* kFrameCallbackIdKey = "_postFrameCallbackId";
struct FrameEntry {
v8::Persistent<v8::Function> callback;
// scheduled == false only while the entry's callback is being invoked; a
// re-post inside the callback flips it back and the entry survives the
// post-call retirement check
bool scheduled = false;
bool raf = false;
// earliest frame timestamp (CADisplayLink.timestamp base) this entry may
// fire at; 0 means the next frame
double notBeforeSeconds = 0;
};
class AnimationFrameState {
public:
explicit AnimationFrameState(v8::Isolate* isolate) : isolate_(isolate), wrapper_(isolate) {
@autoreleasepool {
target_ = [[TNSAnimationFrameTarget alloc] init];
target_->state_ = this;
displayLink_ = [[CADisplayLink displayLinkWithTarget:target_
selector:@selector(onFrame:)] retain];
displayLink_.paused = YES;
[displayLink_ addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
}
~AnimationFrameState() {
if (displayLink_ != nil) {
[displayLink_ invalidate];
[displayLink_ release];
displayLink_ = nil;
}
if (target_ != nil) {
target_->state_ = nullptr;
[target_ release];
target_ = nil;
}
// Caches teardown runs before Isolate::Dispose, which is what makes
// resetting the persistents here legal; guard anyway like TimerTask does
if (wrapper_.IsValid()) {
for (auto& entry : entries_) {
entry.second->callback.Reset();
}
}
entries_.clear();
order_.clear();
}
void Post(Isolate* isolate, const Local<v8::Function>& func, double delayMillis) {
double notBefore = delayMillis > 0 ? CACurrentMediaTime() + delayMillis / 1000.0 : 0;
Local<Value> existingId =
tns::GetPrivateValue(func, tns::ToV8String(isolate, kFrameCallbackIdKey));
if (!existingId.IsEmpty() && existingId->IsNumber()) {
// ids are never reused, so a hit is always this function's own entry;
// a miss means the entry already fired or was removed
auto it = entries_.find((uint64_t)existingId.As<Number>()->Value());
if (it != entries_.end()) {
// an already-pending entry keeps its slot and its delay
if (!it->second->scheduled) {
it->second->scheduled = true;
it->second->notBeforeSeconds = notBefore;
order_.push_back(it->first);
}
EnsureRunning();
return;
}
}
uint64_t id = ++nextId_;
auto entry = std::make_unique<FrameEntry>();
entry->callback.Reset(isolate, func);
#ifdef DEBUG
entry->callback.AnnotateStrongRetainer("frame_callback");
#endif
entry->scheduled = true;
entry->notBeforeSeconds = notBefore;
entries_.emplace(id, std::move(entry));
order_.push_back(id);
tns::SetPrivateValue(func, tns::ToV8String(isolate, kFrameCallbackIdKey),
v8::Number::New(isolate, (double)id));
EnsureRunning();
}
void Remove(Isolate* isolate, const Local<v8::Function>& func) {
Local<Value> existingId =
tns::GetPrivateValue(func, tns::ToV8String(isolate, kFrameCallbackIdKey));
if (existingId.IsEmpty() || !existingId->IsNumber()) {
return;
}
Erase((uint64_t)existingId.As<Number>()->Value());
}
uint64_t Request(Isolate* isolate, const Local<v8::Function>& func) {
uint64_t id = ++nextId_;
auto entry = std::make_unique<FrameEntry>();
entry->callback.Reset(isolate, func);
#ifdef DEBUG
entry->callback.AnnotateStrongRetainer("animation_frame");
#endif
entry->scheduled = true;
entry->raf = true;
entries_.emplace(id, std::move(entry));
order_.push_back(id);
EnsureRunning();
return id;
}
void Cancel(uint64_t id) {
auto it = entries_.find(id);
// handles only name requestAnimationFrame entries; a __postFrameCallback
// entry that happens to share the counter must stay untouchable from here
if (it == entries_.end() || !it->second->raf) {
return;
}
it->second->callback.Reset();
entries_.erase(it);
PauseIfIdle();
}
void Erase(uint64_t id) {
auto it = entries_.find(id);
if (it == entries_.end()) {
return;
}
// the id may still sit in a pending batch; dispatch tolerates the miss
it->second->callback.Reset();
entries_.erase(it);
PauseIfIdle();
}
void FireFrame(double timestampSeconds) {
Isolate* isolate = isolate_;
if (isolate == nullptr || !wrapper_.IsValid() || isolate->IsDead()) {
return;
}
Runtime* runtime = Runtime::GetRuntime(isolate);
if (runtime == nullptr) {
return;
}
double frameTimeNanos = timestampSeconds * 1e9;
double performanceMillis = (timestampSeconds - runtime->TimeOriginMonotonicSeconds()) * 1000.0;
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
std::vector<uint64_t> batch = std::move(order_);
order_.clear();
for (uint64_t id : batch) {
auto it = entries_.find(id);
if (it == entries_.end() || !it->second->scheduled) {
continue;
}
FrameEntry* entry = it->second.get();
if (entry->notBeforeSeconds > timestampSeconds) {
// delay not yet elapsed: stays scheduled, moves to the next batch
order_.push_back(id);
continue;
}
entry->scheduled = false;
Local<v8::Function> cb = entry->callback.Get(isolate);
Local<Context> context = cb->GetCreationContextChecked(v8::Isolate::GetCurrent());
Context::Scope contextScope(context);
if (entry->raf) {
Local<Value> argv[] = {v8::Number::New(isolate, performanceMillis)};
(void)cb->Call(context, context->Global(), 1, argv);
} else {
Local<Value> argv[] = {v8::Number::New(isolate, frameTimeNanos),
v8::Number::New(isolate, performanceMillis)};
(void)cb->Call(context, context->Global(), 2, argv);
}
// re-resolve: the callback may have removed entries (this one included)
// or re-posted itself
auto post = entries_.find(id);
if (post != entries_.end() && !post->second->scheduled) {
post->second->callback.Reset();
entries_.erase(post);
}
}
PauseIfIdle();
}
private:
void EnsureRunning() {
if (displayLink_ != nil && displayLink_.paused) {
displayLink_.paused = NO;
}
}
void PauseIfIdle() {
if (displayLink_ != nil && entries_.empty()) {
displayLink_.paused = YES;
}
}
v8::Isolate* isolate_;
IsolateWrapper wrapper_;
uint64_t nextId_ = 0;
robin_hood::unordered_map<uint64_t, std::unique_ptr<FrameEntry>> entries_;
// entry ids in post order; the upcoming tick's batch. An id appears at most
// once: it is pushed only on the not-scheduled -> scheduled transition (or
// carried over while its delay runs down)
std::vector<uint64_t> order_;
CADisplayLink* displayLink_ = nil;
TNSAnimationFrameTarget* target_ = nil;
};
static AnimationFrameState* StateFromInfo(const FunctionCallbackInfo<Value>& info) {
auto extData = info.Data().As<External>();
return reinterpret_cast<AnimationFrameState*>(extData->Value(v8::kExternalPointerTypeTagDefault));
}
static bool GetFunctionArg(const FunctionCallbackInfo<Value>& info, const char* message,
Local<v8::Function>& func) {
Isolate* isolate = info.GetIsolate();
if (info.Length() < 1 || !info[0]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(tns::ToV8String(isolate, message)));
return false;
}
func = info[0].As<v8::Function>();
return true;
}
void AnimationFrame::Init(Isolate* isolate, Local<ObjectTemplate> globalTemplate) {
auto state = new AnimationFrameState(isolate);
Caches::Get(isolate)->registerCacheBoundObject(state);
Local<v8::External> data = v8::External::New(isolate, state, v8::kExternalPointerTypeTagDefault);
tns::SetMethod(isolate, globalTemplate, "requestAnimationFrame",
AnimationFrame::RequestAnimationFrame, data);
tns::SetMethod(isolate, globalTemplate, "cancelAnimationFrame",
AnimationFrame::CancelAnimationFrame, data);
tns::SetMethod(isolate, globalTemplate, "__postFrameCallback", AnimationFrame::PostFrameCallback,
data);
tns::SetMethod(isolate, globalTemplate, "__removeFrameCallback",
AnimationFrame::RemoveFrameCallback, data);
}
void AnimationFrame::RequestAnimationFrame(const FunctionCallbackInfo<Value>& info) {
Local<v8::Function> func;
if (!GetFunctionArg(info, "Animation frame callback argument is not a function", func)) {
return;
}
uint64_t id = StateFromInfo(info)->Request(info.GetIsolate(), func);
info.GetReturnValue().Set((double)id);
}
void AnimationFrame::CancelAnimationFrame(const FunctionCallbackInfo<Value>& info) {
// per spec an unknown or malformed handle is a silent no-op
if (info.Length() < 1 || !info[0]->IsNumber()) {
return;
}
double id = info[0].As<Number>()->Value();
if (id <= 0 || !std::isfinite(id)) {
return;
}
StateFromInfo(info)->Cancel((uint64_t)id);
}
void AnimationFrame::PostFrameCallback(const FunctionCallbackInfo<Value>& info) {
Local<v8::Function> func;
if (!GetFunctionArg(info, "Frame callback argument is not a function", func)) {
return;
}
double delayMillis = 0;
if (info.Length() >= 2 && info[1]->IsNumber()) {
delayMillis = info[1].As<Number>()->Value();
if (!std::isfinite(delayMillis)) {
delayMillis = 0;
}
}
StateFromInfo(info)->Post(info.GetIsolate(), func, delayMillis);
}
void AnimationFrame::RemoveFrameCallback(const FunctionCallbackInfo<Value>& info) {
Local<v8::Function> func;
if (!GetFunctionArg(info, "Frame callback argument is not a function", func)) {
return;
}
StateFromInfo(info)->Remove(info.GetIsolate(), func);
}
} // namespace tns
@implementation TNSAnimationFrameTarget
- (void)onFrame:(CADisplayLink*)link {
if (state_ != nullptr) {
state_->FireFrame(link.timestamp);
}
}
@end
NODE_BINDING_PER_ISOLATE_INIT_OBJ(animationframe, tns::AnimationFrame::Init)