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