@shopify/react-native-skia 0.1.154 → 0.1.156

Sign up to get free protection for your applications and to get access to all the features.
Files changed (167) hide show
  1. package/android/cpp/jni/JniLoad.cpp +5 -5
  2. package/android/cpp/jni/JniPlatformContext.cpp +107 -119
  3. package/android/cpp/jni/JniSkiaManager.cpp +18 -20
  4. package/android/cpp/jni/include/JniPlatformContext.h +41 -45
  5. package/android/cpp/jni/include/JniSkiaBaseView.h +52 -55
  6. package/android/cpp/jni/include/JniSkiaDrawView.h +72 -77
  7. package/android/cpp/jni/include/JniSkiaManager.h +51 -53
  8. package/android/cpp/jni/include/JniSkiaPictureView.h +74 -78
  9. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +36 -45
  10. package/android/cpp/rnskia-android/RNSkAndroidView.h +87 -92
  11. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +62 -65
  12. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +20 -17
  13. package/android/cpp/rnskia-android/SkiaOpenGLRenderer.cpp +257 -313
  14. package/android/cpp/rnskia-android/SkiaOpenGLRenderer.h +107 -110
  15. package/cpp/api/JsiSkApi.h +66 -62
  16. package/cpp/api/JsiSkCanvas.h +38 -30
  17. package/cpp/api/JsiSkColor.h +58 -56
  18. package/cpp/api/JsiSkColorFilter.h +5 -3
  19. package/cpp/api/JsiSkColorFilterFactory.h +23 -21
  20. package/cpp/api/JsiSkContourMeasure.h +74 -85
  21. package/cpp/api/JsiSkContourMeasureIter.h +68 -75
  22. package/cpp/api/JsiSkData.h +16 -22
  23. package/cpp/api/JsiSkDataFactory.h +86 -79
  24. package/cpp/api/JsiSkFont.h +286 -311
  25. package/cpp/api/JsiSkHostObjects.h +15 -16
  26. package/cpp/api/JsiSkImage.h +107 -103
  27. package/cpp/api/JsiSkImageFactory.h +34 -36
  28. package/cpp/api/JsiSkImageFilter.h +5 -3
  29. package/cpp/api/JsiSkImageFilterFactory.h +71 -68
  30. package/cpp/api/JsiSkImageInfo.h +41 -38
  31. package/cpp/api/JsiSkMaskFilter.h +5 -3
  32. package/cpp/api/JsiSkMaskFilterFactory.h +2 -3
  33. package/cpp/api/JsiSkMatrix.h +26 -36
  34. package/cpp/api/JsiSkPaint.h +20 -13
  35. package/cpp/api/JsiSkPath.h +70 -85
  36. package/cpp/api/JsiSkPathEffect.h +5 -3
  37. package/cpp/api/JsiSkPathEffectFactory.h +33 -28
  38. package/cpp/api/JsiSkPathFactory.h +68 -67
  39. package/cpp/api/JsiSkPicture.h +28 -22
  40. package/cpp/api/JsiSkPictureFactory.h +13 -12
  41. package/cpp/api/JsiSkPictureRecorder.h +21 -19
  42. package/cpp/api/JsiSkPoint.h +6 -8
  43. package/cpp/api/JsiSkRRect.h +11 -7
  44. package/cpp/api/JsiSkRSXform.h +82 -85
  45. package/cpp/api/JsiSkRect.h +9 -9
  46. package/cpp/api/JsiSkRuntimeEffect.h +182 -186
  47. package/cpp/api/JsiSkRuntimeEffectFactory.h +10 -11
  48. package/cpp/api/JsiSkRuntimeShaderBuilder.h +64 -61
  49. package/cpp/api/JsiSkSVG.h +4 -5
  50. package/cpp/api/JsiSkSVGFactory.h +28 -27
  51. package/cpp/api/JsiSkShader.h +3 -2
  52. package/cpp/api/JsiSkShaderFactory.h +37 -25
  53. package/cpp/api/JsiSkSurface.h +44 -40
  54. package/cpp/api/JsiSkSurfaceFactory.h +22 -22
  55. package/cpp/api/JsiSkTextBlob.h +28 -31
  56. package/cpp/api/JsiSkTextBlobFactory.h +88 -87
  57. package/cpp/api/JsiSkTypeface.h +6 -5
  58. package/cpp/api/JsiSkTypefaceFactory.h +22 -21
  59. package/cpp/api/JsiSkVertices.h +137 -124
  60. package/cpp/api/third_party/CSSColorParser.cpp +161 -174
  61. package/cpp/api/third_party/CSSColorParser.h +172 -96
  62. package/cpp/jsi/JsiHostObject.cpp +11 -9
  63. package/cpp/jsi/JsiHostObject.h +31 -24
  64. package/cpp/jsi/JsiSimpleValueWrapper.h +74 -83
  65. package/cpp/jsi/JsiValueWrapper.h +52 -54
  66. package/cpp/rnskia/RNSkAnimation.h +26 -29
  67. package/cpp/rnskia/RNSkDispatchQueue.cpp +50 -61
  68. package/cpp/rnskia/RNSkDispatchQueue.h +3 -1
  69. package/cpp/rnskia/RNSkInfoParameter.h +12 -12
  70. package/cpp/rnskia/RNSkJsView.cpp +82 -81
  71. package/cpp/rnskia/RNSkJsView.h +45 -41
  72. package/cpp/rnskia/RNSkJsiViewApi.h +99 -89
  73. package/cpp/rnskia/RNSkManager.cpp +8 -7
  74. package/cpp/rnskia/RNSkManager.h +8 -6
  75. package/cpp/rnskia/RNSkPictureView.h +44 -37
  76. package/cpp/rnskia/RNSkPlatformContext.h +39 -28
  77. package/cpp/rnskia/RNSkValueApi.h +33 -34
  78. package/cpp/rnskia/RNSkView.h +108 -93
  79. package/cpp/rnskia/values/RNSkClockValue.h +63 -64
  80. package/cpp/rnskia/values/RNSkComputedValue.h +32 -30
  81. package/cpp/rnskia/values/RNSkReadonlyValue.h +60 -59
  82. package/cpp/rnskia/values/RNSkValue.h +38 -40
  83. package/cpp/utils/RNSkLog.h +9 -7
  84. package/cpp/utils/RNSkMeasureTime.h +7 -7
  85. package/cpp/utils/RNSkTimingInfo.h +27 -24
  86. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +8 -9
  87. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +24 -23
  88. package/ios/RNSkia-iOS/RNSkiOSView.h +16 -13
  89. package/ios/RNSkia-iOS/SkiaUIView.h +9 -8
  90. package/lib/commonjs/dom/nodes/JsiSkDOM.js +6 -0
  91. package/lib/commonjs/dom/nodes/JsiSkDOM.js.map +1 -1
  92. package/lib/commonjs/dom/nodes/LayerNode.js +43 -0
  93. package/lib/commonjs/dom/nodes/LayerNode.js.map +1 -0
  94. package/lib/commonjs/dom/nodes/RenderNode.js +1 -1
  95. package/lib/commonjs/dom/nodes/RenderNode.js.map +1 -1
  96. package/lib/commonjs/dom/types/NodeType.js +1 -0
  97. package/lib/commonjs/dom/types/NodeType.js.map +1 -1
  98. package/lib/commonjs/dom/types/SkDOM.js.map +1 -1
  99. package/lib/commonjs/renderer/HostComponents.js +3 -0
  100. package/lib/commonjs/renderer/HostComponents.js.map +1 -1
  101. package/lib/commonjs/renderer/components/Group.js +19 -4
  102. package/lib/commonjs/renderer/components/Group.js.map +1 -1
  103. package/lib/commonjs/renderer/components/Paint.js +6 -1
  104. package/lib/commonjs/renderer/components/Paint.js.map +1 -1
  105. package/lib/commonjs/views/SkiaPictureView.js +3 -2
  106. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  107. package/lib/commonjs/views/SkiaView.js +6 -3
  108. package/lib/commonjs/views/SkiaView.js.map +1 -1
  109. package/lib/module/dom/nodes/JsiSkDOM.js +5 -0
  110. package/lib/module/dom/nodes/JsiSkDOM.js.map +1 -1
  111. package/lib/module/dom/nodes/LayerNode.js +32 -0
  112. package/lib/module/dom/nodes/LayerNode.js.map +1 -0
  113. package/lib/module/dom/nodes/RenderNode.js +1 -1
  114. package/lib/module/dom/nodes/RenderNode.js.map +1 -1
  115. package/lib/module/dom/types/NodeType.js +1 -0
  116. package/lib/module/dom/types/NodeType.js.map +1 -1
  117. package/lib/module/dom/types/SkDOM.js.map +1 -1
  118. package/lib/module/renderer/HostComponents.js +3 -0
  119. package/lib/module/renderer/HostComponents.js.map +1 -1
  120. package/lib/module/renderer/components/Group.js +16 -3
  121. package/lib/module/renderer/components/Group.js.map +1 -1
  122. package/lib/module/renderer/components/Paint.js +7 -1
  123. package/lib/module/renderer/components/Paint.js.map +1 -1
  124. package/lib/module/views/SkiaPictureView.js +2 -2
  125. package/lib/module/views/SkiaPictureView.js.map +1 -1
  126. package/lib/module/views/SkiaView.js +4 -2
  127. package/lib/module/views/SkiaView.js.map +1 -1
  128. package/lib/typescript/src/dom/nodes/JsiSkDOM.d.ts +2 -0
  129. package/lib/typescript/src/dom/nodes/LayerNode.d.ts +8 -0
  130. package/lib/typescript/src/dom/types/NodeType.d.ts +1 -0
  131. package/lib/typescript/src/dom/types/SkDOM.d.ts +1 -0
  132. package/lib/typescript/src/renderer/HostComponents.d.ts +2 -1
  133. package/lib/typescript/src/renderer/components/Group.d.ts +5 -1
  134. package/lib/typescript/src/views/SkiaView.d.ts +3 -0
  135. package/libs/android/arm64-v8a/libskottie.a +0 -0
  136. package/libs/android/arm64-v8a/libsksg.a +0 -0
  137. package/libs/android/armeabi-v7a/libskottie.a +0 -0
  138. package/libs/android/armeabi-v7a/libsksg.a +0 -0
  139. package/libs/android/x86/libskottie.a +0 -0
  140. package/libs/android/x86/libsksg.a +0 -0
  141. package/libs/android/x86_64/libskottie.a +0 -0
  142. package/libs/android/x86_64/libsksg.a +0 -0
  143. package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
  144. package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
  145. package/libs/ios/libskottie.xcframework/Info.plist +42 -0
  146. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
  147. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
  148. package/libs/ios/libsksg.xcframework/Info.plist +42 -0
  149. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
  150. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
  151. package/libs/ios/libskshaper.xcframework/Info.plist +5 -5
  152. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
  153. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
  154. package/libs/ios/libsvg.xcframework/Info.plist +5 -5
  155. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
  156. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
  157. package/package.json +3 -1
  158. package/src/dom/nodes/JsiSkDOM.ts +5 -0
  159. package/src/dom/nodes/LayerNode.ts +35 -0
  160. package/src/dom/nodes/RenderNode.ts +4 -3
  161. package/src/dom/types/NodeType.ts +1 -0
  162. package/src/dom/types/SkDOM.ts +1 -0
  163. package/src/renderer/HostComponents.ts +3 -0
  164. package/src/renderer/components/Group.tsx +16 -3
  165. package/src/renderer/components/Paint.tsx +7 -1
  166. package/src/views/SkiaPictureView.tsx +2 -3
  167. package/src/views/SkiaView.tsx +2 -2
@@ -2,8 +2,8 @@
2
2
 
3
3
  #include <exception>
4
4
  #include <functional>
5
- #include <mutex>
6
5
  #include <memory>
6
+ #include <mutex>
7
7
  #include <string>
8
8
  #include <thread>
9
9
  #include <unordered_map>
@@ -24,31 +24,31 @@
24
24
 
25
25
  namespace RNSkia {
26
26
 
27
- using namespace facebook;
27
+ namespace jsi = facebook::jsi;
28
+ namespace react = facebook::react;
28
29
 
29
30
  class RNSkPlatformContext {
30
31
  public:
31
32
  /**
32
33
  * Constructor
33
34
  */
34
- RNSkPlatformContext(
35
- jsi::Runtime *runtime, std::shared_ptr<react::CallInvoker> callInvoker,
36
- float pixelDensity)
35
+ RNSkPlatformContext(jsi::Runtime *runtime,
36
+ std::shared_ptr<react::CallInvoker> callInvoker,
37
+ float pixelDensity)
37
38
  : _pixelDensity(pixelDensity), _jsRuntime(runtime),
38
39
  _callInvoker(callInvoker),
39
- _dispatchQueue(std::make_unique<RNSkDispatchQueue>("skia-render-thread")) {
40
- _jsThreadId = std::this_thread::get_id();
41
- }
40
+ _dispatchQueue(
41
+ std::make_unique<RNSkDispatchQueue>("skia-render-thread")) {
42
+ _jsThreadId = std::this_thread::get_id();
43
+ }
42
44
 
43
45
  /**
44
46
  * Destructor
45
47
  */
46
- virtual ~RNSkPlatformContext() {
47
- invalidate();
48
- }
49
-
48
+ virtual ~RNSkPlatformContext() { invalidate(); }
49
+
50
50
  void invalidate() {
51
- if(!_isValid) {
51
+ if (!_isValid) {
52
52
  return;
53
53
  }
54
54
  // Stop the refresh loop
@@ -58,20 +58,22 @@ public:
58
58
  notifyDrawLoop(true);
59
59
  _isValid = false;
60
60
  }
61
-
61
+
62
62
  /*
63
63
  Returns true if the current execution context is the javascript thread.
64
64
  */
65
65
  bool isOnJavascriptThread() {
66
66
  return _jsThreadId == std::this_thread::get_id();
67
- };
68
-
67
+ }
68
+
69
69
  /**
70
70
  * Schedules the function to be run on the javascript thread async
71
71
  * @param func Function to run
72
72
  */
73
73
  void runOnJavascriptThread(std::function<void()> func) {
74
- if(!_isValid) { return; }
74
+ if (!_isValid) {
75
+ return;
76
+ }
75
77
  _callInvoker->invokeAsync(std::move(func));
76
78
  }
77
79
 
@@ -79,7 +81,9 @@ public:
79
81
  Runs the function on the render thread
80
82
  */
81
83
  void runOnRenderThread(std::function<void()> func) {
82
- if(!_isValid) { return; }
84
+ if (!_isValid) {
85
+ return;
86
+ }
83
87
  _dispatchQueue->dispatch(std::move(func));
84
88
  }
85
89
 
@@ -96,7 +100,7 @@ public:
96
100
  virtual void performStreamOperation(
97
101
  const std::string &sourceUri,
98
102
  const std::function<void(std::unique_ptr<SkStreamAsset>)> &op) = 0;
99
-
103
+
100
104
  /**
101
105
  * Raises an exception on the platform. This function does not necessarily
102
106
  * throw an exception and stop execution, so it is important to stop execution
@@ -118,7 +122,7 @@ public:
118
122
  /**
119
123
  * @return Current scale factor for pixels
120
124
  */
121
- float getPixelDensity() { return _pixelDensity; };
125
+ float getPixelDensity() { return _pixelDensity; }
122
126
 
123
127
  /**
124
128
  * Starts (if not started) a loop that will call back on display sync
@@ -126,7 +130,9 @@ public:
126
130
  * @returns Identifier of the draw loop entry
127
131
  */
128
132
  size_t beginDrawLoop(size_t nativeId, std::function<void(bool)> callback) {
129
- if(!_isValid) { return 0; }
133
+ if (!_isValid) {
134
+ return 0;
135
+ }
130
136
  auto shouldStart = false;
131
137
  {
132
138
  std::lock_guard<std::mutex> lock(_drawCallbacksLock);
@@ -146,7 +152,9 @@ public:
146
152
  * @param nativeId Identifier of view to end
147
153
  */
148
154
  void endDrawLoop(size_t nativeId) {
149
- if(!_isValid) { return; }
155
+ if (!_isValid) {
156
+ return;
157
+ }
150
158
  auto shouldStop = false;
151
159
  {
152
160
  std::lock_guard<std::mutex> lock(_drawCallbacksLock);
@@ -162,19 +170,22 @@ public:
162
170
 
163
171
  /**
164
172
  * Notifies all drawing callbacks
165
- * @param invalidated True if the context was invalidated, otherwise false. This
166
- * can be used to receive a notification that we have stopped the main drawloop
173
+ * @param invalidated True if the context was invalidated, otherwise false.
174
+ * This can be used to receive a notification that we have stopped the main
175
+ * drawloop
167
176
  */
168
177
  void notifyDrawLoop(bool invalidated) {
169
- if(!_isValid) { return; }
178
+ if (!_isValid) {
179
+ return;
180
+ }
170
181
  std::unordered_map<size_t, std::function<void(bool)>> tmp;
171
182
  {
172
183
  std::lock_guard<std::mutex> lock(_drawCallbacksLock);
173
- tmp.insert(_drawCallbacks.cbegin(), _drawCallbacks.cend());
184
+ tmp.insert(_drawCallbacks.cbegin(), _drawCallbacks.cend());
174
185
  }
175
186
  for (auto it = tmp.begin(); it != tmp.end(); it++) {
176
187
  it->second(invalidated);
177
- }
188
+ }
178
189
  }
179
190
 
180
191
  virtual void startDrawLoop() = 0;
@@ -182,7 +193,7 @@ public:
182
193
 
183
194
  private:
184
195
  float _pixelDensity;
185
-
196
+
186
197
  std::thread::id _jsThreadId;
187
198
 
188
199
  jsi::Runtime *_jsRuntime;
@@ -1,65 +1,64 @@
1
-
2
1
  #pragma once
3
2
 
3
+ #include <memory>
4
+
4
5
  #include <JsiHostObject.h>
6
+ #include <RNSkAnimation.h>
7
+ #include <RNSkComputedValue.h>
5
8
  #include <RNSkPlatformContext.h>
6
9
  #include <RNSkValue.h>
7
- #include <RNSkComputedValue.h>
8
- #include <RNSkAnimation.h>
9
10
  #include <jsi/jsi.h>
10
11
 
11
12
  namespace RNSkia {
12
- using namespace facebook;
13
13
 
14
- class RNSkValueApi : public JsiHostObject {
14
+ namespace jsi = facebook::jsi;
15
+
16
+ class RNSkValueApi : public RNJsi::JsiHostObject {
15
17
  public:
16
18
  /**
17
19
  * Constructor
18
20
  * @param platformContext Platform context
19
21
  */
20
- RNSkValueApi(std::shared_ptr<RNSkPlatformContext> platformContext)
22
+ explicit RNSkValueApi(std::shared_ptr<RNSkPlatformContext> platformContext)
21
23
  : JsiHostObject(), _platformContext(platformContext) {
22
- _valueIdentifier = 50000;
23
- }
24
+ _valueIdentifier = 50000;
25
+ }
24
26
 
25
27
  /**
26
28
  * Destructor
27
29
  */
28
- ~RNSkValueApi() {
29
- }
30
-
30
+ ~RNSkValueApi() {}
31
+
31
32
  JSI_HOST_FUNCTION(createValue) {
32
- return jsi::Object::createFromHostObject(runtime,
33
- std::make_shared<RNSkValue>(_platformContext, runtime, arguments, count));
33
+ return jsi::Object::createFromHostObject(
34
+ runtime, std::make_shared<RNSkValue>(_platformContext, runtime,
35
+ arguments, count));
34
36
  }
35
-
37
+
36
38
  JSI_HOST_FUNCTION(createComputedValue) {
37
- // Creation and initialization is done in two steps to be able to use weak references when setting
38
- // up dependencies - since weak_from_this needs our instance to be a shared_ptr before calling
39
- // weak_from_this().
40
- auto computedValue = std::make_shared<RNSkComputedValue>(_platformContext, runtime, arguments, count);
39
+ // Creation and initialization is done in two steps to be able to use weak
40
+ // references when setting up dependencies - since weak_from_this needs our
41
+ // instance to be a shared_ptr before calling weak_from_this().
42
+ auto computedValue = std::make_shared<RNSkComputedValue>(
43
+ _platformContext, runtime, arguments, count);
41
44
  computedValue->initializeDependencies(runtime, arguments, count);
42
45
  return jsi::Object::createFromHostObject(runtime, computedValue);
43
46
  }
44
-
47
+
45
48
  JSI_HOST_FUNCTION(createAnimation) {
46
- return jsi::Object::createFromHostObject(runtime,
47
- std::make_shared<RNSkAnimation>(_platformContext,
48
- ++_valueIdentifier,
49
- runtime,
50
- arguments,
51
- count));
49
+ return jsi::Object::createFromHostObject(
50
+ runtime,
51
+ std::make_shared<RNSkAnimation>(_platformContext, ++_valueIdentifier,
52
+ runtime, arguments, count));
52
53
  }
53
-
54
+
54
55
  JSI_HOST_FUNCTION(createClockValue) {
55
- return jsi::Object::createFromHostObject(runtime,
56
- std::make_shared<RNSkClockValue>(_platformContext,
57
- ++_valueIdentifier,
58
- runtime,
59
- arguments,
60
- count));
56
+ return jsi::Object::createFromHostObject(
57
+ runtime,
58
+ std::make_shared<RNSkClockValue>(_platformContext, ++_valueIdentifier,
59
+ runtime, arguments, count));
61
60
  }
62
-
61
+
63
62
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(RNSkValueApi, createValue),
64
63
  JSI_EXPORT_FUNC(RNSkValueApi, createComputedValue),
65
64
  JSI_EXPORT_FUNC(RNSkValueApi, createClockValue),
@@ -68,6 +67,6 @@ public:
68
67
  private:
69
68
  // Platform context
70
69
  std::shared_ptr<RNSkPlatformContext> _platformContext;
71
- std::atomic<long> _valueIdentifier;
70
+ std::atomic<long> _valueIdentifier;
72
71
  };
73
72
  } // namespace RNSkia
@@ -2,115 +2,127 @@
2
2
  #pragma once
3
3
 
4
4
  #include <memory>
5
+ #include <string>
6
+ #include <unordered_map>
7
+ #include <vector>
5
8
 
6
9
  #include <RNSkPlatformContext.h>
7
10
 
8
11
  #include <JsiValueWrapper.h>
9
12
 
10
- #include <JsiSkRect.h>
11
13
  #include <JsiSkImage.h>
14
+ #include <JsiSkRect.h>
12
15
 
13
16
  #pragma clang diagnostic push
14
17
  #pragma clang diagnostic ignored "-Wdocumentation"
15
18
 
16
- #include <SkSurface.h>
17
19
  #include <SkCanvas.h>
20
+ #include <SkSurface.h>
18
21
 
19
22
  #pragma clang diagnostic pop
20
23
 
21
24
  namespace RNSkia {
22
25
 
23
- using namespace facebook;
26
+ namespace jsi = facebook::jsi;
24
27
 
25
28
  class RNSkCanvasProvider {
26
29
  public:
27
- RNSkCanvasProvider(std::function<void()> requestRedraw): _requestRedraw(requestRedraw) {}
28
-
30
+ explicit RNSkCanvasProvider(std::function<void()> requestRedraw)
31
+ : _requestRedraw(requestRedraw) {}
32
+
29
33
  /**
30
34
  Returns the scaled width of the view
31
35
  */
32
36
  virtual float getScaledWidth() = 0;
33
-
37
+
34
38
  /**
35
39
  Returns the scaled height of the view
36
40
  */
37
41
  virtual float getScaledHeight() = 0;
38
-
42
+
39
43
  /**
40
44
  Render to a canvas
41
45
  */
42
- virtual void renderToCanvas(const std::function<void(SkCanvas*)>&) = 0;
46
+ virtual void renderToCanvas(const std::function<void(SkCanvas *)> &) = 0;
47
+
43
48
  protected:
44
49
  std::function<void()> _requestRedraw;
45
50
  };
46
51
 
47
52
  class RNSkRenderer {
48
53
  public:
49
- RNSkRenderer(std::function<void()> requestRedraw): _requestRedraw(requestRedraw) {}
50
-
54
+ explicit RNSkRenderer(std::function<void()> requestRedraw)
55
+ : _requestRedraw(requestRedraw) {}
56
+
51
57
  /**
52
- Tries to render the current set of drawing operations. If we're busy we'll return false so that the calling RNSkBaseDrawView
53
- can request a new render next frame. The tryRender method is typically called on each frame if there are any redraw
54
- requests. The method will be called from the main thread, so the implementor must make sure any thread requirements are
55
- met before rendering. This method will also allow the rendering to be dispatched to another thread.
58
+ Tries to render the current set of drawing operations. If we're busy we'll
59
+ return false so that the calling RNSkBaseDrawView can request a new render
60
+ next frame. The tryRender method is typically called on each frame if there
61
+ are any redraw requests. The method will be called from the main thread, so
62
+ the implementor must make sure any thread requirements are met before
63
+ rendering. This method will also allow the rendering to be dispatched to
64
+ another thread.
56
65
  */
57
- virtual bool tryRender(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
58
-
66
+ virtual bool
67
+ tryRender(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
68
+
59
69
  /**
60
- Renders directly to the canvas in the canvas provider. This method is called from a Javascript call to render a
61
- snapshot of the SkiaView to an image, and can therefore run outside the tryRender loop and directly in the
62
- javascript thread.
70
+ Renders directly to the canvas in the canvas provider. This method is called
71
+ from a Javascript call to render a snapshot of the SkiaView to an image, and
72
+ can therefore run outside the tryRender loop and directly in the javascript
73
+ thread.
63
74
  */
64
- virtual void renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
65
-
66
- void setShowDebugOverlays(bool showDebugOverlays) { _showDebugOverlays = showDebugOverlays; }
75
+ virtual void
76
+ renderImmediate(std::shared_ptr<RNSkCanvasProvider> canvasProvider) = 0;
77
+
78
+ void setShowDebugOverlays(bool showDebugOverlays) {
79
+ _showDebugOverlays = showDebugOverlays;
80
+ }
67
81
  bool getShowDebugOverlays() { return _showDebugOverlays; }
68
-
82
+
69
83
  protected:
70
84
  std::function<void()> _requestRedraw;
71
85
  bool _showDebugOverlays;
72
86
  };
73
87
 
74
- class RNSkImageCanvasProvider: public RNSkCanvasProvider {
88
+ class RNSkImageCanvasProvider : public RNSkCanvasProvider {
75
89
  public:
76
- RNSkImageCanvasProvider(std::function<void()> requestRedraw,
77
- float width,
78
- float height):
79
- RNSkCanvasProvider(requestRedraw),
80
- _width(width),
81
- _height(height) {
90
+ RNSkImageCanvasProvider(std::function<void()> requestRedraw, float width,
91
+ float height)
92
+ : RNSkCanvasProvider(requestRedraw), _width(width), _height(height) {
82
93
  _surface = SkSurface::MakeRasterN32Premul(_width, _height);
83
94
  }
84
-
95
+
85
96
  /**
86
97
  Returns a snapshot of the current surface/canvas
87
98
  */
88
99
  sk_sp<SkImage> makeSnapshot(std::shared_ptr<SkRect> bounds) {
89
- if(bounds != nullptr) {
90
- SkIRect b = SkIRect::MakeXYWH(bounds->x(), bounds->y(), bounds->width(), bounds->height());
100
+ if (bounds != nullptr) {
101
+ SkIRect b = SkIRect::MakeXYWH(bounds->x(), bounds->y(), bounds->width(),
102
+ bounds->height());
91
103
  return _surface->makeImageSnapshot(b);
92
104
  } else {
93
105
  return _surface->makeImageSnapshot();
94
106
  }
95
107
  }
96
-
108
+
97
109
  /**
98
110
  Returns the scaled width of the view
99
111
  */
100
112
  float getScaledWidth() override { return _width; };
101
-
113
+
102
114
  /**
103
115
  Returns the scaled height of the view
104
116
  */
105
117
  float getScaledHeight() override { return _height; };
106
-
118
+
107
119
  /**
108
120
  Render to a canvas
109
121
  */
110
- void renderToCanvas(const std::function<void(SkCanvas*)>& cb) override {
122
+ void renderToCanvas(const std::function<void(SkCanvas *)> &cb) override {
111
123
  cb(_surface->getCanvas());
112
124
  };
113
-
125
+
114
126
  private:
115
127
  float _width;
116
128
  float _height;
@@ -129,52 +141,50 @@ using RNSkTouchInfo = struct {
129
141
  long timestamp;
130
142
  };
131
143
 
132
- class RNSkView: public std::enable_shared_from_this<RNSkView> {
144
+ class RNSkView : public std::enable_shared_from_this<RNSkView> {
133
145
  public:
134
146
  /**
135
147
  * Constructor
136
148
  */
137
149
  RNSkView(std::shared_ptr<RNSkPlatformContext> context,
138
150
  std::shared_ptr<RNSkCanvasProvider> canvasProvider,
139
- std::shared_ptr<RNSkRenderer> renderer):
140
- _platformContext(context),
141
- _canvasProvider(canvasProvider),
142
- _renderer(renderer) {}
151
+ std::shared_ptr<RNSkRenderer> renderer)
152
+ : _platformContext(context), _canvasProvider(canvasProvider),
153
+ _renderer(renderer) {}
143
154
 
144
155
  /**
145
156
  Destructor
146
157
  */
147
- virtual ~RNSkView() {
148
- endDrawingLoop();
149
- }
150
-
158
+ virtual ~RNSkView() { endDrawingLoop(); }
159
+
151
160
  /**
152
- Sets custom properties. Custom properties are properties that are set directly from Javascript without having
153
- to go through the async bridge.
161
+ Sets custom properties. Custom properties are properties that are set
162
+ directly from Javascript without having to go through the async bridge.
154
163
  */
155
- virtual void setJsiProperties(std::unordered_map<std::string, RNJsi::JsiValueWrapper> &props) {
156
- throw std::runtime_error("The base Skia View does not support any custom properties.");
157
- };
158
-
164
+ virtual void setJsiProperties(
165
+ std::unordered_map<std::string, RNJsi::JsiValueWrapper> &props) {
166
+ throw std::runtime_error(
167
+ "The base Skia View does not support any custom properties.");
168
+ }
169
+
159
170
  /**
160
171
  Calls a custom action.
161
172
  */
162
- virtual jsi::Value callJsiMethod(jsi::Runtime& runtime,
163
- const std::string& name,
164
- const jsi::Value *arguments,
165
- size_t count) {
166
- throw std::runtime_error("The base Skia View does not support any commands. Command " + name + " not found.");
167
- };
173
+ virtual jsi::Value callJsiMethod(jsi::Runtime &runtime,
174
+ const std::string &name,
175
+ const jsi::Value *arguments, size_t count) {
176
+ throw std::runtime_error(
177
+ "The base Skia View does not support any commands. Command " + name +
178
+ " not found.");
179
+ }
168
180
 
169
181
  /**
170
182
  * Repaints the Skia view using the underlying context and the drawcallback.
171
183
  * This method schedules a draw request that will be run on the correct
172
184
  * thread and js runtime.
173
185
  */
174
- void requestRedraw() {
175
- _redrawRequestCounter++;
176
- }
177
-
186
+ void requestRedraw() { _redrawRequestCounter++; }
187
+
178
188
  /**
179
189
  Sets the native id of the view
180
190
  */
@@ -182,12 +192,12 @@ public:
182
192
  _nativeId = nativeId;
183
193
  beginDrawingLoop();
184
194
  }
185
-
195
+
186
196
  /**
187
197
  Returns the native id
188
198
  */
189
199
  size_t getNativeId() { return _nativeId; }
190
-
200
+
191
201
  /**
192
202
  Sets the drawing mode for the view
193
203
  */
@@ -207,34 +217,38 @@ public:
207
217
  /**
208
218
  Update touch state with new touch points
209
219
  */
210
- virtual void updateTouchState(std::vector<RNSkTouchInfo>&) {
220
+ virtual void updateTouchState(std::vector<RNSkTouchInfo> &) {
211
221
  requestRedraw();
212
222
  }
213
-
223
+
214
224
  /**
215
225
  Renders the view into an SkImage instead of the screen.
216
226
  */
217
227
  sk_sp<SkImage> makeImageSnapshot(std::shared_ptr<SkRect> bounds) {
218
- auto provider = std::make_shared<RNSkImageCanvasProvider>(std::bind(&RNSkView::requestRedraw, this),
219
- _canvasProvider->getScaledWidth(),
220
- _canvasProvider->getScaledHeight());
221
-
228
+ auto provider = std::make_shared<RNSkImageCanvasProvider>(
229
+ std::bind(&RNSkView::requestRedraw, this),
230
+ _canvasProvider->getScaledWidth(), _canvasProvider->getScaledHeight());
231
+
222
232
  _renderer->renderImmediate(provider);
223
233
  return provider->makeSnapshot(bounds);
224
234
  }
225
235
 
226
236
  protected:
227
- std::shared_ptr<RNSkPlatformContext> getPlatformContext() { return _platformContext; }
228
- std::shared_ptr<RNSkCanvasProvider> getCanvasProvider() { return _canvasProvider; }
237
+ std::shared_ptr<RNSkPlatformContext> getPlatformContext() {
238
+ return _platformContext;
239
+ }
240
+ std::shared_ptr<RNSkCanvasProvider> getCanvasProvider() {
241
+ return _canvasProvider;
242
+ }
229
243
  std::shared_ptr<RNSkRenderer> getRenderer() { return _renderer; }
230
244
 
231
245
  /**
232
- Ends an ongoing beginDrawCallback loop for this view. This method is made protected if
233
- the drawing loop should be stopped before reaching the destructor (like we do for Android
234
- views)
246
+ Ends an ongoing beginDrawCallback loop for this view. This method is made
247
+ protected if the drawing loop should be stopped before reaching the
248
+ destructor (like we do for Android views)
235
249
  */
236
250
  void endDrawingLoop() {
237
- if(_drawingLoopId != 0) {
251
+ if (_drawingLoopId != 0) {
238
252
  _drawingLoopId = 0;
239
253
  _platformContext->endDrawLoop(_nativeId);
240
254
  }
@@ -249,39 +263,40 @@ private:
249
263
  return;
250
264
  }
251
265
  // Set to zero to avoid calling beginDrawLoop before we return
252
- _drawingLoopId = _platformContext->beginDrawLoop(_nativeId,
253
- [weakSelf = weak_from_this()](bool invalidated) {
254
- auto self = weakSelf.lock();
255
- if(self) {
256
- self->drawLoopCallback(invalidated);
257
- }
258
- });
266
+ _drawingLoopId = _platformContext->beginDrawLoop(
267
+ _nativeId, [weakSelf = weak_from_this()](bool invalidated) {
268
+ auto self = weakSelf.lock();
269
+ if (self) {
270
+ self->drawLoopCallback(invalidated);
271
+ }
272
+ });
259
273
  }
260
274
 
261
275
  /**
262
276
  Draw loop callback
263
277
  */
264
278
  void drawLoopCallback(bool invalidated) {
265
- if(_redrawRequestCounter > 0 || _drawingMode == RNSkDrawingMode::Continuous) {
266
- _redrawRequestCounter = 0;
267
-
268
- if(!_renderer->tryRender(_canvasProvider)) {
269
- // The renderer could not render cause it was busy, just schedule redrawing
270
- // on the next frame.
279
+ if (_redrawRequestCounter > 0 ||
280
+ _drawingMode == RNSkDrawingMode::Continuous) {
281
+ _redrawRequestCounter = 0;
282
+
283
+ if (!_renderer->tryRender(_canvasProvider)) {
284
+ // The renderer could not render cause it was busy, just schedule
285
+ // redrawing on the next frame.
271
286
  requestRedraw();
272
287
  }
273
288
  }
274
289
  }
275
-
290
+
276
291
  std::shared_ptr<RNSkPlatformContext> _platformContext;
277
292
  std::shared_ptr<RNSkCanvasProvider> _canvasProvider;
278
293
  std::shared_ptr<RNSkRenderer> _renderer;
279
294
 
280
295
  RNSkDrawingMode _drawingMode;
281
296
  size_t _nativeId;
282
-
297
+
283
298
  size_t _drawingLoopId = 0;
284
- std::atomic<int> _redrawRequestCounter = { 1 };
299
+ std::atomic<int> _redrawRequestCounter = {1};
285
300
  };
286
301
 
287
302
  } // namespace RNSkia