Skip to content

Commit

Permalink
Merge pull request #1650 from Shopify/fix/1633-android-avoid-double-r…
Browse files Browse the repository at this point in the history
…endering

Android/Snapshots: Fixed so that we don't render children twice
  • Loading branch information
chrfalch authored Jun 15, 2023
2 parents 2500adb + 80d2af3 commit 4c9dd2b
Showing 1 changed file with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,35 @@ private static void renderViewToCanvas(Canvas canvas, View view, Paint paint) {
canvas.save();
applyTransformations(canvas, view);

// Render view itself
view.draw(canvas);

// Draw children if the view has children
if ((view instanceof ViewGroup)) {
// Draw children
ViewGroup group = (ViewGroup) view;

// Hide visible children - this needs to be done because view.draw(canvas)
// will render all visible non-texture/surface views directly - causing
// views to be rendered twice - once by view.draw() and once when we
// enumerate children. We therefore need to turn off rendering of visible
// children before we call view.draw:
List<View> visibleChildren = new ArrayList<>();
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
if (child.getVisibility() == VISIBLE) {
visibleChildren.add(child);
child.setVisibility(View.INVISIBLE);
}
}

// Draw ourselves
view.draw(canvas);

// Enable children again
for (int i = 0; i < visibleChildren.size(); i++) {
View child = visibleChildren.get(i);
child.setVisibility(VISIBLE);
}

// Draw children
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);

Expand All @@ -90,7 +112,7 @@ private static void renderViewToCanvas(Canvas canvas, View view, Paint paint) {
tvChild.setOpaque(false); // <-- switch off background fill

canvas.save();
applyTransformations(canvas, view);
applyTransformations(canvas, child);

// TextureView should use bitmaps with matching size,
// otherwise content of the TextureView will be scaled to provided bitmap dimensions
Expand All @@ -108,7 +130,7 @@ private static void renderViewToCanvas(Canvas canvas, View view, Paint paint) {
try {
PixelCopy.request(svChild, childBitmapBuffer, copyResult -> {
canvas.save();
applyTransformations(canvas, view);
applyTransformations(canvas, child);
canvas.drawBitmap(childBitmapBuffer, 0, 0, paint);
canvas.restore();
latch.countDown();
Expand All @@ -121,19 +143,20 @@ private static void renderViewToCanvas(Canvas canvas, View view, Paint paint) {
Bitmap cache = svChild.getDrawingCache();
if (cache != null) {
canvas.save();
applyTransformations(canvas, view);
applyTransformations(canvas, child);
canvas.drawBitmap(svChild.getDrawingCache(), 0, 0, paint);
canvas.restore();
}
}
} else {
// Regular views needs to be rendered again to ensure correct z-index
// order with texture views and surface views. This is a bit stupid
// it'll result in rendering regular views twice - but it is the only
// way we can possibly render both surrounding and child views
// order with texture views and surface views.
renderViewToCanvas(canvas, child, paint);
}
}
} else {
// Draw ourselves
view.draw(canvas);
}

// Restore canvas
Expand Down

0 comments on commit 4c9dd2b

Please sign in to comment.