Skip to content

Commit

Permalink
fix: render G offscreen only when it's needed (opacity != 1) (#2450)
Browse files Browse the repository at this point in the history
# Summary

Improve #2417 by applying offscreen canvas only when opacity attribute
is set

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| Android |    ✅      |
  • Loading branch information
jakex7 authored Sep 16, 2024
1 parent d05f69e commit 51a4e02
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions android/src/main/java/com/horcrux/svg/GroupView.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,24 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) {
final SvgView svg = getSvgView();
final GroupView self = this;
final RectF groupRect = new RectF();
if (mLayerBitmap == null) {
mLayerBitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
mLayerCanvas = new Canvas(mLayerBitmap);

if (mOpacity != 1) {
if (mLayerBitmap == null) {
mLayerBitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
mLayerCanvas = new Canvas(mLayerBitmap);
} else {
mLayerBitmap.recycle();
mLayerBitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
mLayerCanvas.setBitmap(mLayerBitmap);
}
// Copy current matrix from original canvas
mLayerCanvas.save();
mLayerCanvas.setMatrix(canvas.getMatrix());
} else {
mLayerBitmap.recycle();
mLayerBitmap =
Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
mLayerCanvas.setBitmap(mLayerBitmap);
mLayerCanvas = canvas;
}
// Copy current matrix from original canvas
int saveCount = mLayerCanvas.save();
mLayerCanvas.setMatrix(canvas.getMatrix());

elements = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
Expand Down Expand Up @@ -157,13 +162,15 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) {
}
}

// Restore copied canvas and temporary reset original canvas matrix to draw bitmap 1:1
mLayerCanvas.restoreToCount(saveCount);
saveCount = canvas.save();
canvas.setMatrix(null);
mLayerPaint.setAlpha((int) (mOpacity * 255));
canvas.drawBitmap(mLayerBitmap, 0, 0, mLayerPaint);
canvas.restoreToCount(saveCount);
if (mOpacity != 1) {
// Restore copied canvas and temporary reset original canvas matrix to draw bitmap 1:1
mLayerCanvas.restore();
int saveCount = canvas.save();
canvas.setMatrix(null);
mLayerPaint.setAlpha((int) (mOpacity * 255));
canvas.drawBitmap(mLayerBitmap, 0, 0, mLayerPaint);
canvas.restoreToCount(saveCount);
}
this.setClientRect(groupRect);
popGlyphContext();
}
Expand Down

0 comments on commit 51a4e02

Please sign in to comment.