@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
|
62
|
-
view.draw(c);
|
61
|
+
final Canvas canvas = new Canvas(bitmap);
|
63
62
|
|
64
|
-
//
|
65
|
-
|
63
|
+
// Renders view with child views to canvas
|
64
|
+
renderViewToCanvas(canvas, view, paint);
|
66
65
|
|
67
|
-
|
68
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
if (
|
92
|
-
|
93
|
-
|
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
|
-
|
121
|
-
|
122
|
-
final ArrayList<View> viewArrayList = new ArrayList<>();
|
123
|
-
viewArrayList.add(v);
|
93
|
+
// Draw ourselves
|
94
|
+
view.draw(canvas);
|
124
95
|
|
125
|
-
|
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
|
-
|
102
|
+
// Draw children
|
103
|
+
for (int i = 0; i < group.getChildCount(); i++) {
|
104
|
+
View child = group.getChildAt(i);
|
129
105
|
|
130
|
-
|
131
|
-
|
132
|
-
View child = viewGroup.getChildAt(i);
|
106
|
+
// skip all invisible to user child views
|
107
|
+
if (child.getVisibility() != VISIBLE) continue;
|
133
108
|
|
134
|
-
|
135
|
-
|
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
|
-
|
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
|
-
@
|
146
|
-
|
147
|
-
final Matrix
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
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
|
-
|
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