From 884b47892fe93e98d479399d7e83ef5a28614cfc Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Wed, 25 Sep 2024 09:21:45 -0700 Subject: [PATCH 1/3] Add a boolean that exposes rotation/crop metadata capability. --- .../engine/renderer/FlutterRenderer.java | 5 ++++ .../SurfaceTextureSurfaceProducer.java | 5 ++++ .../io/flutter/view/TextureRegistry.java | 22 ++++++++++++++++++ .../engine/renderer/FlutterRendererTest.java | 23 +++++++++++++++++++ 4 files changed, 55 insertions(+) diff --git a/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java b/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java index 532e692358981..7edaf4932fbb4 100644 --- a/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java +++ b/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java @@ -756,6 +756,11 @@ public void setCallback(Callback callback) { this.callback = callback; } + @Override + public boolean handlesCropAndRotation() { + return false; + } + @Override public long id() { return id; diff --git a/shell/platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureSurfaceProducer.java b/shell/platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureSurfaceProducer.java index cefd5774a117e..6f522a65c375e 100644 --- a/shell/platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureSurfaceProducer.java +++ b/shell/platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureSurfaceProducer.java @@ -60,6 +60,11 @@ public void setCallback(Callback callback) { // Intentionally blank: SurfaceTextures don't get platform notifications or cleanup. } + @Override + public boolean handlesCropAndRotation() { + return true; + } + @Override @NonNull public SurfaceTexture getSurfaceTexture() { diff --git a/shell/platform/android/io/flutter/view/TextureRegistry.java b/shell/platform/android/io/flutter/view/TextureRegistry.java index caeb799d7598a..fe67c587b1e92 100644 --- a/shell/platform/android/io/flutter/view/TextureRegistry.java +++ b/shell/platform/android/io/flutter/view/TextureRegistry.java @@ -124,6 +124,28 @@ interface Callback { /** This method is not officially part of the public API surface and will be deprecated. */ void scheduleFrame(); + + /** + * Returns whether the current rendering path handles crop and rotation metadata. + * + *

On newer Android devices, a {@link android.media.ImageReader} backend is used, which has + * more features, works in new graphic backends directly (such as Impeller's Vulkan backend), + * and is the Android recommended solution. However, crop and rotation metadata are + * not handled automatically, and require plugin authors to make appropriate + * changes ({@see https://github.com/flutter/flutter/issues/144407}). + * + *

{@code
+     * void example(SurfaceProducer producer) {
+     *   bool supported = producer.handlesCropAndRotation();
+     *   if (!supported) {
+     *       // Manually rotate/crop, either in the Android plugin or in the Dart framework layer.
+     *   }
+     * }
+     * }
+ * + * @return {@code true} if crop and rotation is handled automatically, {@code false} otherwise. + */ + boolean handlesCropAndRotation(); } /** A registry entry for a managed SurfaceTexture. */ diff --git a/shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java b/shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java index 0ed6a4754a589..7ab861ab40854 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java @@ -7,8 +7,10 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyFloat; import static org.mockito.ArgumentMatchers.anyInt; @@ -735,6 +737,27 @@ public void SurfaceTextureSurfaceProducerCreatesAConnectedTexture() { } } + @Test + public void SurfaceTextureSurfaceProducerDoesNotCropOrRotate() { + try { + FlutterRenderer.debugForceSurfaceProducerGlTextures = true; + FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer(); + TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer(); + + assertTrue(producer.handlesCropAndRotation()); + } finally { + FlutterRenderer.debugForceSurfaceProducerGlTextures = false; + } + } + + @Test + public void ImageReaderSurfaceProducerDoesNotCropOrRotate() { + FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer(); + TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer(); + + assertFalse(producer.handlesCropAndRotation()); + } + @Test public void ImageReaderSurfaceProducerIsDestroyedOnTrimMemory() { FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer(); From b85d52594db9fee55f9434311dbf62729fd90074 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Wed, 25 Sep 2024 09:30:19 -0700 Subject: [PATCH 2/3] Doc comments. --- .../platform/android/io/flutter/view/TextureRegistry.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shell/platform/android/io/flutter/view/TextureRegistry.java b/shell/platform/android/io/flutter/view/TextureRegistry.java index fe67c587b1e92..c1beabf2e1019 100644 --- a/shell/platform/android/io/flutter/view/TextureRegistry.java +++ b/shell/platform/android/io/flutter/view/TextureRegistry.java @@ -128,10 +128,10 @@ interface Callback { /** * Returns whether the current rendering path handles crop and rotation metadata. * - *

On newer Android devices, a {@link android.media.ImageReader} backend is used, which has - * more features, works in new graphic backends directly (such as Impeller's Vulkan backend), - * and is the Android recommended solution. However, crop and rotation metadata are - * not handled automatically, and require plugin authors to make appropriate + *

On most newer Android devices (API 29+), a {@link android.media.ImageReader} backend is + * used, which has more features, works in new graphic backends directly (such as Impeller's + * Vulkan backend), and is the Android recommended solution. However, crop and rotation metadata + * are not handled automatically, and require plugin authors to make appropriate * changes ({@see https://github.com/flutter/flutter/issues/144407}). * *

{@code

From 9d4a3d48ce9167f4d56f88a93279531989fe93fd Mon Sep 17 00:00:00 2001
From: Matan Lurey 
Date: Wed, 25 Sep 2024 11:17:19 -0700
Subject: [PATCH 3/3] Update fake.

---
 shell/platform/android/io/flutter/view/TextureRegistry.java  | 4 ++--
 .../flutter/plugin/platform/PlatformViewsControllerTest.java | 5 +++++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/shell/platform/android/io/flutter/view/TextureRegistry.java b/shell/platform/android/io/flutter/view/TextureRegistry.java
index c1beabf2e1019..c9c9836bf20d6 100644
--- a/shell/platform/android/io/flutter/view/TextureRegistry.java
+++ b/shell/platform/android/io/flutter/view/TextureRegistry.java
@@ -131,8 +131,8 @@ interface Callback {
      * 

On most newer Android devices (API 29+), a {@link android.media.ImageReader} backend is * used, which has more features, works in new graphic backends directly (such as Impeller's * Vulkan backend), and is the Android recommended solution. However, crop and rotation metadata - * are not handled automatically, and require plugin authors to make appropriate - * changes ({@see https://github.com/flutter/flutter/issues/144407}). + * are not handled automatically, and require plugin authors to make + * appropriate changes ({@see https://github.com/flutter/flutter/issues/144407}). * *

{@code
      * void example(SurfaceProducer producer) {
diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
index 310f5fe051f6f..f9ebf55087fd4 100644
--- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
+++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java
@@ -1651,6 +1651,11 @@ public Surface getSurface() {
                 return null;
               }
 
+              @Override
+              public boolean handlesCropAndRotation() {
+                return false;
+              }
+
               public void scheduleFrame() {}
             };
           }