@shopify/react-native-skia 0.1.203 → 0.1.204

Sign up to get free protection for your applications and to get access to all the features.
@@ -62,62 +62,6 @@ protected:
62
62
  _skiaAndroidView->viewDidUnmount();
63
63
  }
64
64
 
65
- /**
66
- * Android specific method for rendering an offscreen GPU buffer to an Android
67
- * bitmap. The result can be used to render the first frame of the Skia render
68
- * to avoid flickering on android.
69
- */
70
- /*
71
- // TODO: Remove if we find another solution for first frame rendering
72
- // protected native Object renderToBitmap(Object bitmap, int width, int
73
- height); virtual jobject renderToBitmap(jobject bitmapIn, int width, int
74
- height) { auto platformContext = getSkiaManager()->getPlatformContext(); auto
75
- provider = std::make_shared<RNSkOffscreenCanvasProvider>( platformContext,
76
- []() {}, width, height);
77
-
78
- // Render into a gpu backed buffer
79
- _skiaAndroidView->getSkiaView()->getRenderer()->renderImmediate(provider);
80
- auto rect = SkRect::MakeXYWH(0, 0, width, height);
81
- auto image = provider->makeSnapshot(&rect);
82
-
83
- AndroidBitmapInfo infoIn;
84
- auto env = facebook::jni::Environment::current();
85
- void *pixels;
86
-
87
- // Get image info
88
- if (AndroidBitmap_getInfo(env, bitmapIn, &infoIn) !=
89
- ANDROID_BITMAP_RESULT_SUCCESS) {
90
- return env->NewStringUTF("failed");
91
- }
92
-
93
- // Check image
94
- if (infoIn.format != ANDROID_BITMAP_FORMAT_RGBA_8888 &&
95
- infoIn.format != ANDROID_BITMAP_FORMAT_RGB_565) {
96
- return env->NewStringUTF("Only support ANDROID_BITMAP_FORMAT_RGBA_8888 "
97
- "and ANDROID_BITMAP_FORMAT_RGB_565");
98
- }
99
-
100
- auto imageInfo = SkImageInfo::Make(image->width(), image->height(),
101
- image->colorType(), image->alphaType());
102
-
103
- // Lock all images
104
- if (AndroidBitmap_lockPixels(env, bitmapIn, &pixels) !=
105
- ANDROID_BITMAP_RESULT_SUCCESS) {
106
- return env->NewStringUTF("AndroidBitmap_lockPixels failed!");
107
- }
108
-
109
- // Set pixels from SkImage
110
- image->readPixels(imageInfo, pixels, imageInfo.minRowBytes(), 0, 0);
111
-
112
- // Unlocks everything
113
- AndroidBitmap_unlockPixels(env, bitmapIn);
114
-
115
- image = nullptr;
116
- provider = nullptr;
117
-
118
- return bitmapIn;
119
- }*/
120
-
121
65
  private:
122
66
  JniSkiaManager *_manager;
123
67
  std::shared_ptr<RNSkBaseAndroidView> _skiaAndroidView;
@@ -34,21 +34,18 @@ public:
34
34
  }
35
35
 
36
36
  static void registerNatives() {
37
- registerHybrid({
38
- makeNativeMethod("initHybrid", JniSkiaDomView::initHybrid),
39
- makeNativeMethod("surfaceAvailable", JniSkiaDomView::surfaceAvailable),
40
- makeNativeMethod("surfaceDestroyed", JniSkiaDomView::surfaceDestroyed),
41
- makeNativeMethod("surfaceSizeChanged",
42
- JniSkiaDomView::surfaceSizeChanged),
43
- makeNativeMethod("setMode", JniSkiaDomView::setMode),
44
- makeNativeMethod("setDebugMode", JniSkiaDomView::setDebugMode),
45
- makeNativeMethod("updateTouchPoints",
46
- JniSkiaDomView::updateTouchPoints),
47
- makeNativeMethod("registerView", JniSkiaDomView::registerView),
48
- makeNativeMethod("unregisterView", JniSkiaDomView::unregisterView)
49
- // TODO: Remove if we find another solution for first frame rendering
50
- // makeNativeMethod("renderToBitmap", JniSkiaDomView::renderToBitmap)
51
- });
37
+ registerHybrid(
38
+ {makeNativeMethod("initHybrid", JniSkiaDomView::initHybrid),
39
+ makeNativeMethod("surfaceAvailable", JniSkiaDomView::surfaceAvailable),
40
+ makeNativeMethod("surfaceDestroyed", JniSkiaDomView::surfaceDestroyed),
41
+ makeNativeMethod("surfaceSizeChanged",
42
+ JniSkiaDomView::surfaceSizeChanged),
43
+ makeNativeMethod("setMode", JniSkiaDomView::setMode),
44
+ makeNativeMethod("setDebugMode", JniSkiaDomView::setDebugMode),
45
+ makeNativeMethod("updateTouchPoints",
46
+ JniSkiaDomView::updateTouchPoints),
47
+ makeNativeMethod("registerView", JniSkiaDomView::registerView),
48
+ makeNativeMethod("unregisterView", JniSkiaDomView::unregisterView)});
52
49
  }
53
50
 
54
51
  protected:
@@ -76,11 +73,6 @@ protected:
76
73
 
77
74
  void unregisterView() override { JniSkiaBaseView::unregisterView(); }
78
75
 
79
- // TODO: Remove if we find another solution for first frame rendering
80
- /*jobject renderToBitmap(jobject bitmap, int width, int height) override {
81
- return JniSkiaBaseView::renderToBitmap(bitmap, width, height);
82
- }*/
83
-
84
76
  private:
85
77
  friend HybridBase;
86
78
 
@@ -33,21 +33,20 @@ public:
33
33
  }
34
34
 
35
35
  static void registerNatives() {
36
- registerHybrid({
37
- makeNativeMethod("initHybrid", JniSkiaDrawView::initHybrid),
38
- makeNativeMethod("surfaceAvailable", JniSkiaDrawView::surfaceAvailable),
39
- makeNativeMethod("surfaceDestroyed", JniSkiaDrawView::surfaceDestroyed),
40
- makeNativeMethod("surfaceSizeChanged",
41
- JniSkiaDrawView::surfaceSizeChanged),
42
- makeNativeMethod("setMode", JniSkiaDrawView::setMode),
43
- makeNativeMethod("setDebugMode", JniSkiaDrawView::setDebugMode),
44
- makeNativeMethod("updateTouchPoints",
45
- JniSkiaDrawView::updateTouchPoints),
46
- makeNativeMethod("registerView", JniSkiaDrawView::registerView),
47
- makeNativeMethod("unregisterView", JniSkiaDrawView::unregisterView),
48
- // TODO: Remove if we find another solution for first frame rendering
49
- // makeNativeMethod("renderToBitmap", JniSkiaDrawView::renderToBitmap)
50
- });
36
+ registerHybrid(
37
+ {makeNativeMethod("initHybrid", JniSkiaDrawView::initHybrid),
38
+ makeNativeMethod("surfaceAvailable",
39
+ JniSkiaDrawView::surfaceAvailable),
40
+ makeNativeMethod("surfaceDestroyed",
41
+ JniSkiaDrawView::surfaceDestroyed),
42
+ makeNativeMethod("surfaceSizeChanged",
43
+ JniSkiaDrawView::surfaceSizeChanged),
44
+ makeNativeMethod("setMode", JniSkiaDrawView::setMode),
45
+ makeNativeMethod("setDebugMode", JniSkiaDrawView::setDebugMode),
46
+ makeNativeMethod("updateTouchPoints",
47
+ JniSkiaDrawView::updateTouchPoints),
48
+ makeNativeMethod("registerView", JniSkiaDrawView::registerView),
49
+ makeNativeMethod("unregisterView", JniSkiaDrawView::unregisterView)});
51
50
  }
52
51
 
53
52
  protected:
@@ -75,11 +74,6 @@ protected:
75
74
 
76
75
  void unregisterView() override { JniSkiaBaseView::unregisterView(); }
77
76
 
78
- // TODO: Remove if we find another solution for first frame rendering
79
- /*jobject renderToBitmap(jobject bitmap, int width, int height) override {
80
- return JniSkiaBaseView::renderToBitmap(bitmap, width, height);
81
- }*/
82
-
83
77
  private:
84
78
  friend HybridBase;
85
79
 
@@ -33,24 +33,21 @@ public:
33
33
  }
34
34
 
35
35
  static void registerNatives() {
36
- registerHybrid({
37
- makeNativeMethod("initHybrid", JniSkiaPictureView::initHybrid),
38
- makeNativeMethod("surfaceAvailable",
39
- JniSkiaPictureView::surfaceAvailable),
40
- makeNativeMethod("surfaceDestroyed",
41
- JniSkiaPictureView::surfaceDestroyed),
42
- makeNativeMethod("surfaceSizeChanged",
43
- JniSkiaPictureView::surfaceSizeChanged),
44
- makeNativeMethod("setMode", JniSkiaPictureView::setMode),
45
- makeNativeMethod("setDebugMode", JniSkiaPictureView::setDebugMode),
46
- makeNativeMethod("updateTouchPoints",
47
- JniSkiaPictureView::updateTouchPoints),
48
- makeNativeMethod("registerView", JniSkiaPictureView::registerView),
49
- makeNativeMethod("unregisterView", JniSkiaPictureView::unregisterView),
50
- // TODO: Remove if we find another solution for first frame rendering
51
- // makeNativeMethod("renderToBitmap",
52
- // JniSkiaPictureView::renderToBitmap)
53
- });
36
+ registerHybrid(
37
+ {makeNativeMethod("initHybrid", JniSkiaPictureView::initHybrid),
38
+ makeNativeMethod("surfaceAvailable",
39
+ JniSkiaPictureView::surfaceAvailable),
40
+ makeNativeMethod("surfaceDestroyed",
41
+ JniSkiaPictureView::surfaceDestroyed),
42
+ makeNativeMethod("surfaceSizeChanged",
43
+ JniSkiaPictureView::surfaceSizeChanged),
44
+ makeNativeMethod("setMode", JniSkiaPictureView::setMode),
45
+ makeNativeMethod("setDebugMode", JniSkiaPictureView::setDebugMode),
46
+ makeNativeMethod("updateTouchPoints",
47
+ JniSkiaPictureView::updateTouchPoints),
48
+ makeNativeMethod("registerView", JniSkiaPictureView::registerView),
49
+ makeNativeMethod("unregisterView",
50
+ JniSkiaPictureView::unregisterView)});
54
51
  }
55
52
 
56
53
  protected:
@@ -78,12 +75,6 @@ protected:
78
75
 
79
76
  void unregisterView() override { JniSkiaBaseView::unregisterView(); }
80
77
 
81
- /*
82
- TODO: Remove if we find another solution for first frame rendering
83
- jobject renderToBitmap(jobject bitmap, int width, int height) override {
84
- return JniSkiaBaseView::renderToBitmap(bitmap, width, height);
85
- }*/
86
-
87
78
  private:
88
79
  friend HybridBase;
89
80
 
@@ -9,53 +9,51 @@ import android.view.TextureView;
9
9
 
10
10
  import com.facebook.jni.annotations.DoNotStrip;
11
11
  import com.facebook.react.views.view.ReactViewGroup;
12
+
12
13
  public abstract class SkiaBaseView extends ReactViewGroup implements TextureView.SurfaceTextureListener {
13
14
 
14
15
  @DoNotStrip
15
16
  private Surface mSurface;
16
17
  private TextureView mTexture;
17
18
 
19
+ private String tag = "SkiaView";
20
+
18
21
  public SkiaBaseView(Context context) {
19
22
  super(context);
20
- // TODO: Remove if we find another solution for first frame rendering
21
- //setWillNotDraw(!shouldRenderFirstFrameAsBitmap());
22
23
  mTexture = new TextureView(context);
23
24
  mTexture.setSurfaceTextureListener(this);
24
25
  mTexture.setOpaque(false);
25
26
  addView(mTexture);
26
27
  }
27
28
 
28
- /*@Override
29
- TODO: Remove if we find another solution for first frame rendering
30
- protected void onDraw(Canvas canvas) {
31
- super.onDraw(canvas);
32
-
33
- // If we haven't got a surface yet, let's ask the view to
34
- // draw into a bitmap and then render the bitmap. This method
35
- // is typically only called once - for the first frame, and
36
- // then the surface will be available and all rendering will
37
- // be done directly to the surface itself.
38
- if (shouldRenderFirstFrameAsBitmap() && mSurface == null) {
39
- int width = getWidth();
40
- int height = getHeight();
41
-
42
- if (width > 0 && height > 0) {
43
- Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
44
- Bitmap result = (Bitmap) renderToBitmap(bitmap, width, height);
45
-
46
- canvas.drawBitmap(
47
- result,
48
- new Rect(0, 0, width, height),
49
- new Rect(0, 0, width, height),
50
- null);
51
-
52
- bitmap.recycle();
53
- }
29
+ public void destroySurface() {
30
+ Log.i(tag, "destroySurface");
31
+ surfaceDestroyed();
32
+ mSurface.release();
33
+ mSurface = null;
34
+ }
35
+
36
+ private void createSurfaceTexture() {
37
+ // This API Level is >= 26, we created our own SurfaceTexture to have a faster time to first frame
38
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
39
+ Log.i(tag, "Create SurfaceTexture");
40
+ SurfaceTexture surface = new SurfaceTexture(false);
41
+ mTexture.setSurfaceTexture(surface);
42
+ this.onSurfaceTextureAvailable(surface, this.getMeasuredWidth(), this.getMeasuredHeight());
43
+ }
44
+ }
45
+
46
+ @Override
47
+ protected void onAttachedToWindow() {
48
+ super.onAttachedToWindow();
49
+ if (this.getMeasuredWidth() == 0) {
50
+ createSurfaceTexture();
54
51
  }
55
- }*/
52
+ }
56
53
 
57
54
  @Override
58
55
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
56
+ Log.i(tag, "onLayout " + this.getMeasuredWidth() + "/" + this.getMeasuredHeight());
59
57
  super.onLayout(changed, left, top, right, bottom);
60
58
  mTexture.layout(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
61
59
  }
@@ -132,52 +130,23 @@ public abstract class SkiaBaseView extends ReactViewGroup implements TextureView
132
130
 
133
131
  @Override
134
132
  public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
135
- Log.i("SkiaBaseView", "onSurfaceTextureAvailable " + width + "/" + height);
133
+ Log.i(tag, "onSurfaceTextureAvailable " + width + "/" + height);
136
134
  mSurface = new Surface(surface);
137
135
  surfaceAvailable(mSurface, width, height);
138
-
139
- /*
140
- TODO: Remove if we find another solution for first frame rendering
141
- // Clear rendered bitmap when the surface texture has rendered
142
- // We'll post a message to the main loop asking to invalidate
143
- if (shouldRenderFirstFrameAsBitmap()) {
144
- postUpdate(new AtomicInteger());
145
- }*/
146
136
  }
147
137
 
148
- /**
149
- * This method is a way for us to clear the bitmap rendered on the first frame
150
- * after at least 16 frames have passed - to avoid seeing blinks on the screen caused by
151
- * TextureView frame sync issues. This is a hack to avoid those pesky blinks. Have no
152
- * idea on how to sync the TextureView OpenGL updates.
153
- * @param counter
154
- */
155
- /*
156
- TODO: Remove if we find another solution for first frame rendering
157
- void postUpdate(AtomicInteger counter) {
158
- counter.getAndIncrement();
159
- if (counter.get() > 16) {
160
- invalidate();
161
- } else {
162
- this.post(() -> {
163
- postUpdate(counter);
164
- });
165
- }
166
- }*/
167
-
168
138
  @Override
169
139
  public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
170
- Log.i("SkiaBaseView", "onSurfaceTextureSizeChanged " + width + "/" + height);
140
+ Log.i(tag, "onSurfaceTextureSizeChanged " + width + "/" + height);
171
141
  surfaceSizeChanged(width, height);
172
142
  }
173
143
 
174
144
  @Override
175
145
  public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
176
- Log.i("SkiaBaseView", "onSurfaceTextureDestroyed");
146
+ Log.i(tag, "onSurfaceTextureDestroyed");
177
147
  // https://developer.android.com/reference/android/view/TextureView.SurfaceTextureListener#onSurfaceTextureDestroyed(android.graphics.SurfaceTexture)
178
- surfaceDestroyed();
179
- mSurface.release();
180
- mSurface = null;
148
+ destroySurface();
149
+ createSurfaceTexture();
181
150
  return false;
182
151
  }
183
152
 
@@ -186,17 +155,6 @@ public abstract class SkiaBaseView extends ReactViewGroup implements TextureView
186
155
  // Nothing special to do here
187
156
  }
188
157
 
189
- /**
190
- * Returns true if the view is able to directly render on the
191
- * main thread. This can f.ex then be used to create a first frame
192
- * render of the view. Returns true by default - override if not.
193
- */
194
- /*
195
- TODO: Remove if we find another solution for first frame rendering
196
- protected boolean shouldRenderFirstFrameAsBitmap() {
197
- return false;
198
- }*/
199
-
200
158
  protected abstract void surfaceAvailable(Object surface, int width, int height);
201
159
 
202
160
  protected abstract void surfaceSizeChanged(int width, int height);
@@ -212,7 +170,4 @@ public abstract class SkiaBaseView extends ReactViewGroup implements TextureView
212
170
  protected abstract void registerView(int nativeId);
213
171
 
214
172
  protected abstract void unregisterView();
215
-
216
- // TODO: Remove if we find another solution for first frame rendering
217
- // protected native Object renderToBitmap(Object bitmap, int width, int height);
218
173
  }
@@ -30,5 +30,6 @@ public abstract class SkiaBaseViewManager extends ReactViewManager {
30
30
  public void onDropViewInstance(@NonNull ReactViewGroup view) {
31
31
  super.onDropViewInstance(view);
32
32
  ((SkiaBaseView)view).unregisterView();
33
+ ((SkiaBaseView)view).destroySurface();
33
34
  }
34
35
  }
@@ -41,7 +41,4 @@ public class SkiaDomView extends SkiaBaseView {
41
41
  protected native void registerView(int nativeId);
42
42
 
43
43
  protected native void unregisterView();
44
-
45
- // TODO: Remove if we find another solution for first frame rendering
46
- // protected native Object renderToBitmap(Object bitmap, int width, int height);
47
44
  }
@@ -41,7 +41,4 @@ public class SkiaDrawView extends SkiaBaseView {
41
41
  protected native void registerView(int nativeId);
42
42
 
43
43
  protected native void unregisterView();
44
-
45
- // TODO: Remove if we find another solution for first frame rendering
46
- // protected native Object renderToBitmap(Object bitmap, int width, int height);
47
44
  }
@@ -41,8 +41,4 @@ public class SkiaPictureView extends SkiaBaseView {
41
41
  protected native void registerView(int nativeId);
42
42
 
43
43
  protected native void unregisterView();
44
-
45
- // TODO: Remove if we find another solution for first frame rendering
46
- // protected native Object renderToBitmap(Object bitmap, int width, int height);
47
-
48
44
  }
@@ -19,7 +19,7 @@ class SkWriteBuffer;
19
19
  * of `SkColorFilters::Table`, and provides a way to share the table data between client code and
20
20
  * the returned SkColorFilter. Once created, an SkColorTable is immutable.
21
21
  */
22
- class SK_API SkColorTable : public SkRefCnt {
22
+ class SkColorTable : public SkRefCnt {
23
23
  public:
24
24
  // Creates a new SkColorTable with 'table' used for all four channels. The table is copied into
25
25
  // the SkColorTable.
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "setup-skia-web": "./scripts/setup-canvaskit.js"
8
8
  },
9
9
  "title": "React Native Skia",
10
- "version": "0.1.203",
10
+ "version": "0.1.204",
11
11
  "description": "High-performance React Native Graphics using Skia",
12
12
  "main": "lib/module/index.js",
13
13
  "files": [