-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathRNFChoreographerWrapper.cpp
More file actions
90 lines (71 loc) · 2.79 KB
/
Copy pathRNFChoreographerWrapper.cpp
File metadata and controls
90 lines (71 loc) · 2.79 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
//
// Created by Hanno Gödecke on 02.05.24.
//
#include "RNFChoreographerWrapper.h"
namespace margelo {
// Will be called on automatic removal (can happen when the runtime gets destroyed, and onRuntimeDestroyed would be called later)
ChoreographerWrapper::~ChoreographerWrapper() {
std::unique_lock lock(_mutex);
stopAndRemoveListeners();
}
void ChoreographerWrapper::start() {
std::unique_lock lock(_mutex);
pointee()->start();
}
void ChoreographerWrapper::stop() {
std::unique_lock lock(_mutex);
pointee()->stop();
}
std::shared_ptr<HybridListenerSpec> ChoreographerWrapper::addFrameCallbackListener(const std::function<void(const FrameInfo& /* frameInfo */)>& callback) {
std::unique_lock lock(_mutex);
margelo::Logger::log(HybridChoreographerSpec::TAG, "Adding frame callback listener");
std::weak_ptr<ChoreographerWrapper> weakThis = shared<ChoreographerWrapper>();
return pointee()->addOnFrameListener([weakThis, callback](double timestamp) {
auto sharedThis = weakThis.lock();
if (sharedThis) {
FrameInfo frameInfo = sharedThis->createFrameInfo(timestamp);
callback(frameInfo);
}
});
}
FrameInfo ChoreographerWrapper::createFrameInfo(double timestamp) {
std::unique_lock lock(_mutex);
if (_startTime == 0) {
[[unlikely]];
_startTime = timestamp;
}
double passedSeconds = (timestamp - _startTime) / 1e9;
double timeSinceLastFrame = (timestamp - _lastFrameTime) / 1e9;
_lastFrameTime = timestamp;
return FrameInfo(timestamp, _startTime, passedSeconds, timeSinceLastFrame);
}
void ChoreographerWrapper::stopAndRemoveListeners() {
// Its possible that the pointer was already released manually by the user
if (!HybridPointerHolder::getIsValid()) {
margelo::Logger::log(HybridChoreographerSpec::TAG, "stopAndRemoveListeners() called but Choreographer is invalid already!");
return;
}
margelo::Logger::log(HybridChoreographerSpec::TAG, "Stopping choreographer and removing listeners...");
pointee()->stop();
// Clear all listeners now - that will cause the listeners function destructors to be called
// When onRuntimeDestroyed gets called we still have time to stopAndRemoveListeners our jsi functions (RenderCallback):
pointee()->removeAllListeners();
}
// TODO: tries to overwrite release from PointerHolder
//void ChoreographerWrapper::release() {
// std::unique_lock lock(_mutex);
// stopAndRemoveListeners();
// PointerHolder::release();
//}
void ChoreographerWrapper::onRuntimeDestroyed(jsi::Runtime*) {
std::unique_lock lock(_mutex);
margelo::Logger::log(HybridChoreographerSpec::TAG, "Runtime destroyed...");
stopAndRemoveListeners();
}
std::shared_ptr<Choreographer> ChoreographerWrapper::getChoreographer() {
if (HybridPointerHolder::getIsValid()) {
return pointee();
}
return nullptr;
}
} // namespace margelo