From 79621fe3eef25cde15590723bc005abaca7b2bfc Mon Sep 17 00:00:00 2001 From: Jakub Grzywacz Date: Mon, 16 Sep 2024 11:27:23 +0200 Subject: [PATCH 1/2] fix: render G offscreen only when it's needed (opacity != 1) --- .../main/java/com/horcrux/svg/GroupView.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/GroupView.java b/android/src/main/java/com/horcrux/svg/GroupView.java index db20fd2ec..edfc341aa 100644 --- a/android/src/main/java/com/horcrux/svg/GroupView.java +++ b/android/src/main/java/com/horcrux/svg/GroupView.java @@ -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 (opacity != 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++) { @@ -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 (opacity != 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(); } From a92c9005566c67ec1aac78fd419211616e064940 Mon Sep 17 00:00:00 2001 From: Jakub Grzywacz Date: Mon, 16 Sep 2024 11:53:59 +0200 Subject: [PATCH 2/2] fix: typo - mOpacity instead of opacity --- android/src/main/java/com/horcrux/svg/GroupView.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/GroupView.java b/android/src/main/java/com/horcrux/svg/GroupView.java index edfc341aa..9075b7c17 100644 --- a/android/src/main/java/com/horcrux/svg/GroupView.java +++ b/android/src/main/java/com/horcrux/svg/GroupView.java @@ -98,7 +98,7 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) { final GroupView self = this; final RectF groupRect = new RectF(); - if (opacity != 1) { + if (mOpacity != 1) { if (mLayerBitmap == null) { mLayerBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888); @@ -162,7 +162,7 @@ void drawGroup(final Canvas canvas, final Paint paint, final float opacity) { } } - if (opacity != 1) { + if (mOpacity != 1) { // Restore copied canvas and temporary reset original canvas matrix to draw bitmap 1:1 mLayerCanvas.restore(); int saveCount = canvas.save();