1 #include "RNSkManager.h"
2
3 #include <memory>
4 #include <utility>
5
6 #include <jsi/jsi.h>
7
8 #include <JsiSkApi.h>
9 #include <RNSkJsiViewApi.h>
10 #include <RNSkView.h>
11 #include <RNSkValueApi.h>
12
13 namespace RNSkia {
14 using namespace facebook;
15
RNSkManager(jsi::Runtime * jsRuntime,std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker,std::shared_ptr<RNSkPlatformContext> platformContext)16 RNSkManager::RNSkManager(
17 jsi::Runtime *jsRuntime,
18 std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker,
19 std::shared_ptr<RNSkPlatformContext> platformContext)
20 : _jsRuntime(jsRuntime), _jsCallInvoker(jsCallInvoker),
21 _platformContext(platformContext),
22 _viewApi(std::make_shared<RNSkJsiViewApi>(platformContext)) {
23
24 // Install bindings
25 installBindings();
26 }
27
~RNSkManager()28 RNSkManager::~RNSkManager() {
29 invalidate();
30 // Free up any references
31 _viewApi = nullptr;
32 _jsRuntime = nullptr;
33 _platformContext = nullptr;
34 _jsCallInvoker = nullptr;
35 }
36
invalidate()37 void RNSkManager::invalidate() {
38 if(_isInvalidated) {
39 return;
40 }
41 _isInvalidated = true;
42
43 // Invalidate members
44 _viewApi->invalidate();
45 _platformContext->invalidate();
46 }
47
registerSkiaView(size_t nativeId,std::shared_ptr<RNSkView> view)48 void RNSkManager::registerSkiaView(size_t nativeId, std::shared_ptr<RNSkView> view) {
49 if (!_isInvalidated && _viewApi != nullptr)
50 _viewApi->registerSkiaView(nativeId, view);
51 }
52
unregisterSkiaView(size_t nativeId)53 void RNSkManager::unregisterSkiaView(size_t nativeId) {
54 if (!_isInvalidated && _viewApi != nullptr)
55 _viewApi->unregisterSkiaView(nativeId);
56 }
57
setSkiaView(size_t nativeId,std::shared_ptr<RNSkView> view)58 void RNSkManager::setSkiaView(size_t nativeId, std::shared_ptr<RNSkView> view) {
59 if (!_isInvalidated && _viewApi != nullptr)
60 _viewApi->setSkiaView(nativeId, view);
61 }
62
installBindings()63 void RNSkManager::installBindings() {
64 // Create the API objects and install it on the global object in the
65 // provided runtime.
66
67 auto skiaApi = std::make_shared<JsiSkApi>(*_jsRuntime, _platformContext);
68 _jsRuntime->global().setProperty(
69 *_jsRuntime, "SkiaApi",
70 jsi::Object::createFromHostObject(*_jsRuntime, std::move(skiaApi)));
71
72 _jsRuntime->global().setProperty(
73 *_jsRuntime, "SkiaViewApi",
74 jsi::Object::createFromHostObject(*_jsRuntime, _viewApi));
75
76 auto skiaValueApi = std::make_shared<RNSkValueApi>(_platformContext);
77 _jsRuntime->global().setProperty(
78 *_jsRuntime, "SkiaValueApi",
79 jsi::Object::createFromHostObject(*_jsRuntime, std::move(skiaValueApi)));
80 }
81 } // namespace RNSkia
82