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