@shopify/react-native-skia 0.1.193 → 0.1.194

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -58,123 +58,124 @@ public class ViewScreenshotService {
58
58
  paint.setDither(true);
59
59
 
60
60
  // Render the main view and its children
61
- final Canvas c = new Canvas(bitmap);
62
- view.draw(c);
61
+ final Canvas canvas = new Canvas(bitmap);
63
62
 
64
- // Enumerate children
65
- final List<View> childrenList = getAllChildren(view);
63
+ // Renders view with child views to canvas
64
+ renderViewToCanvas(canvas, view, paint);
66
65
 
67
- for (final View child : childrenList) {
68
- // skip any child that we don't know how to process
69
- if (child instanceof TextureView) {
70
- // skip all invisible to user child views
71
- if (child.getVisibility() != VISIBLE) continue;
66
+ return bitmap;
67
+ }
72
68
 
73
- final TextureView tvChild = (TextureView) child;
74
- tvChild.setOpaque(false); // <-- switch off background fill
75
-
76
- // TextureView should use bitmaps with matching size,
77
- // otherwise content of the TextureView will be scaled to provided bitmap dimensions
78
- final Bitmap childBitmapBuffer = tvChild.getBitmap(Bitmap.createBitmap(child.getWidth(), child.getHeight(), Bitmap.Config.ARGB_8888));
79
-
80
- final int countCanvasSave = c.save();
81
- applyTransformations(c, view, child);
82
-
83
- // due to re-use of bitmaps for screenshot, we can get bitmap that is bigger in size than requested
84
- c.drawBitmap(childBitmapBuffer, 0, 0, paint);
85
-
86
- c.restoreToCount(countCanvasSave);
87
- } else if (child instanceof SurfaceView) {
88
- final SurfaceView svChild = (SurfaceView)child;
89
- final CountDownLatch latch = new CountDownLatch(1);
90
-
91
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
92
- final Bitmap childBitmapBuffer = Bitmap.createBitmap(child.getWidth(), child.getHeight(), Bitmap.Config.ARGB_8888);
93
- try {
94
- PixelCopy.request(svChild, childBitmapBuffer, new PixelCopy.OnPixelCopyFinishedListener() {
95
- @Override
96
- public void onPixelCopyFinished(int copyResult) {
97
- final int countCanvasSave = c.save();
98
- applyTransformations(c, view, child);
99
- c.drawBitmap(childBitmapBuffer, 0, 0, paint);
100
- c.restoreToCount(countCanvasSave);
101
- latch.countDown();
102
- }
103
- }, new Handler(Looper.getMainLooper()));
104
- latch.await(SURFACE_VIEW_READ_PIXELS_TIMEOUT, TimeUnit.SECONDS);
105
- } catch (Exception e) {
106
- Log.e(TAG, "Cannot PixelCopy for " + svChild, e);
107
- }
108
- } else {
109
- Bitmap cache = svChild.getDrawingCache();
110
- if (cache != null) {
111
- c.drawBitmap(svChild.getDrawingCache(), 0, 0, paint);
112
- }
69
+ private static void renderViewToCanvas(Canvas canvas, View view, Paint paint) {
70
+ // Apply transformations for the current view
71
+ canvas.save();
72
+ applyTransformations(canvas, view);
73
+
74
+ // Draw children if the view has children
75
+ if ((view instanceof ViewGroup)) {
76
+ // Draw children
77
+ ViewGroup group = (ViewGroup) view;
78
+
79
+ // Hide visible children - this needs to be done because view.draw(canvas)
80
+ // will render all visible non-texture/surface views directly - causing
81
+ // views to be rendered twice - once by view.draw() and once when we
82
+ // enumerate children. We therefore need to turn off rendering of visible
83
+ // children before we call view.draw:
84
+ List<View> visibleChildren = new ArrayList<>();
85
+ for (int i = 0; i < group.getChildCount(); i++) {
86
+ View child = group.getChildAt(i);
87
+ if (child.getVisibility() == VISIBLE) {
88
+ visibleChildren.add(child);
89
+ child.setVisibility(View.INVISIBLE);
113
90
  }
114
91
  }
115
- }
116
-
117
- return bitmap;
118
- }
119
92
 
120
- private static List<View> getAllChildren(@NonNull final View v) {
121
- if (!(v instanceof ViewGroup)) {
122
- final ArrayList<View> viewArrayList = new ArrayList<>();
123
- viewArrayList.add(v);
93
+ // Draw ourselves
94
+ view.draw(canvas);
124
95
 
125
- return viewArrayList;
126
- }
96
+ // Enable children again
97
+ for (int i = 0; i < visibleChildren.size(); i++) {
98
+ View child = visibleChildren.get(i);
99
+ child.setVisibility(VISIBLE);
100
+ }
127
101
 
128
- final ArrayList<View> result = new ArrayList<>();
102
+ // Draw children
103
+ for (int i = 0; i < group.getChildCount(); i++) {
104
+ View child = group.getChildAt(i);
129
105
 
130
- ViewGroup viewGroup = (ViewGroup) v;
131
- for (int i = 0; i < viewGroup.getChildCount(); i++) {
132
- View child = viewGroup.getChildAt(i);
106
+ // skip all invisible to user child views
107
+ if (child.getVisibility() != VISIBLE) continue;
133
108
 
134
- //Do not add any parents, just add child elements
135
- result.addAll(getAllChildren(child));
109
+ // skip any child that we don't know how to process
110
+ if (child instanceof TextureView) {
111
+ final TextureView tvChild = (TextureView) child;
112
+ tvChild.setOpaque(false); // <-- switch off background fill
113
+
114
+ canvas.save();
115
+ applyTransformations(canvas, child);
116
+
117
+ // TextureView should use bitmaps with matching size,
118
+ // otherwise content of the TextureView will be scaled to provided bitmap dimensions
119
+ final Bitmap childBitmapBuffer = tvChild.getBitmap(Bitmap.createBitmap(child.getWidth(), child.getHeight(), Bitmap.Config.ARGB_8888));
120
+ canvas.drawBitmap(childBitmapBuffer, 0, 0, paint);
121
+
122
+ canvas.restore();
123
+
124
+ } else if (child instanceof SurfaceView) {
125
+ final SurfaceView svChild = (SurfaceView) child;
126
+ final CountDownLatch latch = new CountDownLatch(1);
127
+
128
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
129
+ final Bitmap childBitmapBuffer = Bitmap.createBitmap(child.getWidth(), child.getHeight(), Bitmap.Config.ARGB_8888);
130
+ try {
131
+ PixelCopy.request(svChild, childBitmapBuffer, copyResult -> {
132
+ canvas.save();
133
+ applyTransformations(canvas, child);
134
+ canvas.drawBitmap(childBitmapBuffer, 0, 0, paint);
135
+ canvas.restore();
136
+ latch.countDown();
137
+ }, new Handler(Looper.getMainLooper()));
138
+ latch.await(SURFACE_VIEW_READ_PIXELS_TIMEOUT, TimeUnit.SECONDS);
139
+ } catch (Exception e) {
140
+ Log.e(TAG, "Cannot PixelCopy for " + svChild, e);
141
+ }
142
+ } else {
143
+ Bitmap cache = svChild.getDrawingCache();
144
+ if (cache != null) {
145
+ canvas.save();
146
+ applyTransformations(canvas, child);
147
+ canvas.drawBitmap(svChild.getDrawingCache(), 0, 0, paint);
148
+ canvas.restore();
149
+ }
150
+ }
151
+ } else {
152
+ // Regular views needs to be rendered again to ensure correct z-index
153
+ // order with texture views and surface views.
154
+ renderViewToCanvas(canvas, child, paint);
155
+ }
156
+ }
157
+ } else {
158
+ // Draw ourselves
159
+ view.draw(canvas);
136
160
  }
137
161
 
138
- return result;
162
+ // Restore canvas
163
+ canvas.restore();
139
164
  }
140
165
 
141
- /**
142
- * Concat all the transformation matrix's from parent to child.
143
- */
144
166
  @NonNull
145
- @SuppressWarnings("UnusedReturnValue")
146
- private static Matrix applyTransformations(final Canvas c, @NonNull final View root, @NonNull final View child) {
147
- final Matrix transform = new Matrix();
148
- final LinkedList<View> ms = new LinkedList<>();
149
-
150
- // find all parents of the child view
151
- View iterator = child;
152
- do {
153
- ms.add(iterator);
154
-
155
- iterator = (View) iterator.getParent();
156
- } while (iterator != root);
157
-
158
- // apply transformations from parent --> child order
159
- Collections.reverse(ms);
160
-
161
- for (final View v : ms) {
162
- c.save();
163
-
164
- // apply each view transformations, so each child will be affected by them
165
- final float dx = v.getLeft() + ((v != child) ? v.getPaddingLeft() : 0) + v.getTranslationX();
166
- final float dy = v.getTop() + ((v != child) ? v.getPaddingTop() : 0) + v.getTranslationY();
167
- c.translate(dx, dy);
168
- c.rotate(v.getRotation(), v.getPivotX(), v.getPivotY());
169
- c.scale(v.getScaleX(), v.getScaleY());
170
-
171
- // compute the matrix just for any future use
172
- transform.postTranslate(dx, dy);
173
- transform.postRotate(v.getRotation(), v.getPivotX(), v.getPivotY());
174
- transform.postScale(v.getScaleX(), v.getScaleY());
175
- }
176
-
177
- return transform;
167
+ private static void applyTransformations(final Canvas c, @NonNull final View view) {
168
+ // Get the transformation matrix of the view
169
+ final Matrix matrix = view.getMatrix();
170
+
171
+ // Create a new matrix for translation
172
+ final Matrix translateMatrix = new Matrix();
173
+ final float dx = view.getLeft() + view.getPaddingLeft() + view.getTranslationX();
174
+ final float dy = view.getTop() + view.getPaddingTop() + view.getTranslationY();
175
+ translateMatrix.setTranslate(dx, dy);
176
+
177
+ // Pre-concatenate the current matrix of the canvas with the translation and transformation matrices of the view
178
+ c.concat(translateMatrix);
179
+ c.concat(matrix);
178
180
  }
179
-
180
181
  }
@@ -30,7 +30,9 @@ protected:
30
30
  #endif
31
31
  // Save paint if the paint property is set
32
32
  if (_paintProp->isSet()) {
33
- draw(_paintProp->getUnsafeDerivedValue().get());
33
+ auto localCtx = _paintProp->getUnsafeDerivedValue().get();
34
+ localCtx->setCanvas(context->getCanvas());
35
+ draw(localCtx);
34
36
  } else {
35
37
  // Call abstract draw method
36
38
  draw(context);
@@ -77,8 +77,7 @@ void RNSkMetalCanvasProvider::renderToCanvas(
77
77
  // accessed from the main thread so we need to check here.
78
78
  if ([[NSThread currentThread] isMainThread]) {
79
79
  auto state = UIApplication.sharedApplication.applicationState;
80
- if (state == UIApplicationStateBackground ||
81
- state == UIApplicationStateInactive) {
80
+ if (state == UIApplicationStateBackground) {
82
81
  // Request a redraw in the next run loop callback
83
82
  _requestRedraw();
84
83
  // and don't draw now since it might cause errors in the metal renderer if
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.193",
10
+ "version": "0.1.194",
11
11
  "description": "High-performance React Native Graphics using Skia",
12
12
  "main": "lib/module/index.js",
13
13
  "files": [