From bc7481b541c5abc28aa0057754e0d14fbc4209c8 Mon Sep 17 00:00:00 2001 From: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com> Date: Wed, 20 Jul 2022 19:25:16 -0700 Subject: [PATCH 1/6] Use PorterDuff blend mode when below Android 29 --- .../io/flutter/plugin/platform/PlatformViewWrapper.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java index c57d90f0162c1..68fb62e0c2eac 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java @@ -31,6 +31,7 @@ import io.flutter.util.ViewUtils; import io.flutter.view.TextureRegistry; import java.util.concurrent.atomic.AtomicLong; +import android.graphics.PorterDuff; /** * Wraps a platform view to intercept gestures and project this view onto a {@link SurfaceTexture}. @@ -308,7 +309,7 @@ public void draw(Canvas canvas) { if (Build.VERSION.SDK_INT >= 29) { surfaceCanvas.drawColor(Color.TRANSPARENT, BlendMode.CLEAR); } else { - surfaceCanvas.drawColor(Color.TRANSPARENT); + surfaceCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); } super.draw(surfaceCanvas); onFrameProduced(); From 19b620a69b9e203f090234c8e0cae9253c42f109 Mon Sep 17 00:00:00 2001 From: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com> Date: Wed, 20 Jul 2022 19:25:57 -0700 Subject: [PATCH 2/6] fix import order --- .../android/io/flutter/plugin/platform/PlatformViewWrapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java index 68fb62e0c2eac..579da458ceb49 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java @@ -13,6 +13,7 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; +import android.graphics.PorterDuff; import android.graphics.Rect; import android.graphics.SurfaceTexture; import android.os.Build; @@ -31,7 +32,6 @@ import io.flutter.util.ViewUtils; import io.flutter.view.TextureRegistry; import java.util.concurrent.atomic.AtomicLong; -import android.graphics.PorterDuff; /** * Wraps a platform view to intercept gestures and project this view onto a {@link SurfaceTexture}. From 81c5dd76f135dbcddc5a0fcb870b79d183d95333 Mon Sep 17 00:00:00 2001 From: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com> Date: Wed, 20 Jul 2022 19:40:41 -0700 Subject: [PATCH 3/6] test --- .../platform/PlatformViewWrapperTest.java | 47 ++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index 2893df22834f5..a0c8f022220b3 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -5,11 +5,11 @@ import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; -import android.annotation.TargetApi; import android.content.Context; import android.graphics.BlendMode; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.PorterDuff; import android.graphics.SurfaceTexture; import android.view.Surface; import android.view.View; @@ -26,7 +26,6 @@ import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; -@TargetApi(31) @RunWith(AndroidJUnit4.class) public class PlatformViewWrapperTest { private final Context ctx = ApplicationProvider.getApplicationContext(); @@ -120,6 +119,50 @@ public void draw(Canvas canvas) { verifyNoMoreInteractions(canvas); } + @Config(sdk = 28) + @Test + public void draw_clearsCanvasWithClearModeOnAndroidVersionsBelow29() { + final Surface surface = mock(Surface.class); + final PlatformViewWrapper wrapper = + new PlatformViewWrapper(ctx) { + @Override + protected Surface createSurface(@NonNull SurfaceTexture tx) { + return surface; + } + }; + + wrapper.addView( + new View(ctx) { + @Override + public void draw(Canvas canvas) { + super.draw(canvas); + canvas.drawColor(Color.RED); + } + }); + + final int size = 100; + wrapper.measure(size, size); + wrapper.layout(0, 0, size, size); + + final SurfaceTexture tx = mock(SurfaceTexture.class); + when(tx.isReleased()).thenReturn(false); + + when(surface.lockHardwareCanvas()).thenReturn(mock(Canvas.class)); + + wrapper.setTexture(tx); + + final Canvas canvas = mock(Canvas.class); + when(surface.lockHardwareCanvas()).thenReturn(canvas); + when(surface.isValid()).thenReturn(true); + + // Test. + wrapper.invalidate(); + wrapper.draw(new Canvas()); + + // Verify. + verify(canvas, times(1)).drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); + } + @Test @Config( shadows = { From 50eefcff0d1c5af9b4eeb63d4f945b3701cff045 Mon Sep 17 00:00:00 2001 From: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com> Date: Thu, 21 Jul 2022 09:01:24 -0700 Subject: [PATCH 4/6] use a single clear method --- .../plugin/platform/PlatformViewWrapper.java | 12 +---- .../platform/PlatformViewWrapperTest.java | 50 ++----------------- 2 files changed, 6 insertions(+), 56 deletions(-) diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java index 579da458ceb49..de70c9ffb2231 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java @@ -170,11 +170,7 @@ public void setTexture(@Nullable SurfaceTexture newTx) { // to the user until the platform view draws its first frame. final Canvas canvas = surface.lockHardwareCanvas(); try { - if (Build.VERSION.SDK_INT >= 29) { - canvas.drawColor(Color.TRANSPARENT, BlendMode.CLEAR); - } else { - canvas.drawColor(Color.TRANSPARENT); - } + canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); onFrameProduced(); } finally { surface.unlockCanvasAndPost(canvas); @@ -306,11 +302,7 @@ public void draw(Canvas canvas) { try { // Clear the current pixels in the canvas. // This helps when a WebView renders an HTML document with transparent background. - if (Build.VERSION.SDK_INT >= 29) { - surfaceCanvas.drawColor(Color.TRANSPARENT, BlendMode.CLEAR); - } else { - surfaceCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); - } + surfaceCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); super.draw(surfaceCanvas); onFrameProduced(); } finally { diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index a0c8f022220b3..24d6cecde7a76 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -5,6 +5,7 @@ import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; +import android.annotation.TargetApi; import android.content.Context; import android.graphics.BlendMode; import android.graphics.Canvas; @@ -26,6 +27,7 @@ import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; +@TargetApi(31) @RunWith(AndroidJUnit4.class) public class PlatformViewWrapperTest { private final Context ctx = ApplicationProvider.getApplicationContext(); @@ -64,7 +66,7 @@ protected Surface createSurface(@NonNull SurfaceTexture tx) { // Verify. verify(surface, times(1)).lockHardwareCanvas(); verify(surface, times(1)).unlockCanvasAndPost(canvas); - verify(canvas, times(1)).drawColor(Color.TRANSPARENT, BlendMode.CLEAR); + verify(canvas, times(1)).drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); verifyNoMoreInteractions(surface); verifyNoMoreInteractions(canvas); } @@ -111,7 +113,7 @@ public void draw(Canvas canvas) { wrapper.draw(new Canvas()); // Verify. - verify(canvas, times(1)).drawColor(Color.TRANSPARENT, BlendMode.CLEAR); + verify(canvas, times(1)).drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); verify(surface, times(1)).isValid(); verify(surface, times(1)).lockHardwareCanvas(); verify(surface, times(1)).unlockCanvasAndPost(canvas); @@ -119,50 +121,6 @@ public void draw(Canvas canvas) { verifyNoMoreInteractions(canvas); } - @Config(sdk = 28) - @Test - public void draw_clearsCanvasWithClearModeOnAndroidVersionsBelow29() { - final Surface surface = mock(Surface.class); - final PlatformViewWrapper wrapper = - new PlatformViewWrapper(ctx) { - @Override - protected Surface createSurface(@NonNull SurfaceTexture tx) { - return surface; - } - }; - - wrapper.addView( - new View(ctx) { - @Override - public void draw(Canvas canvas) { - super.draw(canvas); - canvas.drawColor(Color.RED); - } - }); - - final int size = 100; - wrapper.measure(size, size); - wrapper.layout(0, 0, size, size); - - final SurfaceTexture tx = mock(SurfaceTexture.class); - when(tx.isReleased()).thenReturn(false); - - when(surface.lockHardwareCanvas()).thenReturn(mock(Canvas.class)); - - wrapper.setTexture(tx); - - final Canvas canvas = mock(Canvas.class); - when(surface.lockHardwareCanvas()).thenReturn(canvas); - when(surface.isValid()).thenReturn(true); - - // Test. - wrapper.invalidate(); - wrapper.draw(new Canvas()); - - // Verify. - verify(canvas, times(1)).drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); - } - @Test @Config( shadows = { From b57b68212e52bb0d33c3b1f73e8363adff21a15c Mon Sep 17 00:00:00 2001 From: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com> Date: Thu, 21 Jul 2022 09:11:30 -0700 Subject: [PATCH 5/6] remove import --- .../android/io/flutter/plugin/platform/PlatformViewWrapper.java | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java index de70c9ffb2231..d55b0a6b7ce7f 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java @@ -9,7 +9,6 @@ import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; -import android.graphics.BlendMode; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; From a0ad7ec8fbbfdda2d294c0581ea8fad5b9bba30a Mon Sep 17 00:00:00 2001 From: Maurice Parrish <10687576+bparrishMines@users.noreply.github.com> Date: Thu, 21 Jul 2022 09:12:17 -0700 Subject: [PATCH 6/6] remove import --- .../test/io/flutter/plugin/platform/PlatformViewWrapperTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index 24d6cecde7a76..630504f9aaeef 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -7,7 +7,6 @@ import android.annotation.TargetApi; import android.content.Context; -import android.graphics.BlendMode; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.PorterDuff;