Skip to content

Commit 945b567

Browse files
luskangmackall
andauthored
[Android] Skip the HardwareBuffer defect fallback for platform views (flutter#193441)
PR flutter#181931 (the fix for flutter#166481) made `FlutterRenderer.createSurfaceProducer()` return `SurfaceTextureSurfaceProducer` on Huawei devices with API <= 29. That fallback is for plugins like `video_player`, which pass the producer's surface straight to `MediaCodec`. Platform views call the same method for Texture Layer Hybrid Composition, so they got the fallback too. On Huawei JNY-LX1 / Android 10 this makes a TextureView with OpenGL content inside an `AndroidView` blank or frozen (flutter#188303). This keeps platform views on `ImageReaderSurfaceProducer` on API 29+, as it was from 3.29.2 to 3.41: - New `FlutterRenderer.createPlatformViewSurfaceProducer(SurfaceLifecycle)` creates a producer without the HardwareBuffer defect check. The API 29 requirement and `debugForceSurfaceProducerGlTextures` still apply. - `PlatformViewsController.makePlatformViewRenderTarget` uses it when the texture registry is a `FlutterRenderer`. - `createSurfaceProducer()` is unchanged, so plugins keep the flutter#166481 fallback. Apps do not need to change anything. Fixes flutter#188303. Related to flutter#166481 and flutter#181931. Testing: - `FlutterRendererTest`: platform view producer uses ImageReader on Huawei API 29 and SurfaceTexture below API 29, plugin producers with `resetInBackground` keep the fallback. - `PlatformViewsControllerTest`: texture layer composition asks `FlutterRenderer` for the platform view producer and not for the plugin one. - Both test classes pass with the Java embedding compiled from this branch. - Device: Huawei JNY-LX1 / Android 10, our app in release mode with the same change applied to the Flutter 3.47.5 embedding. The map platform view renders and keeps updating after panning. With the stock 3.47.5 embedding the same build shows the first map frame and then stops updating, panning moves the Flutter UI on top but not the map. - Device: the flutter#188303 repro app extended with `video_player` (https://github.com/luskan/flutter_textureview_platform_view), same device and 3.47.5. With this change both the TextureView platform view and the video keep updating. With stock 3.47.5 the platform view freezes. With a process wide override to ImageReader the video fails with the flutter#166481 ACodec error. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with ///). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md Co-authored-by: Gray Mackall <[email protected]>
1 parent 29625ca commit 945b567

4 files changed

Lines changed: 102 additions & 2 deletions

File tree

‎engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,33 @@ private void clearDeadListeners() {
223223
@NonNull
224224
@Override
225225
public SurfaceProducer createSurfaceProducer(SurfaceLifecycle lifecycle) {
226+
return createSurfaceProducerInternal(lifecycle, /* skipHardwareBufferDefectCheck= */ false);
227+
}
228+
229+
/**
230+
* Creates a {@link SurfaceProducer} for a platform view render target.
231+
*
232+
* <p>Unlike {@link #createSurfaceProducer(SurfaceLifecycle)}, this skips the HardwareBuffer
233+
* defect check. That check is a workaround for video decoders that write directly into the
234+
* producer's surface. Platform views draw into the surface through the Android view renderer
235+
* instead, and the workaround breaks them on some devices. See
236+
* https://github.com/flutter/flutter/issues/188303.
237+
*
238+
* <p>This is only meant for {@code PlatformViewsController}. Plugins should use {@link
239+
* #createSurfaceProducer(SurfaceLifecycle)}.
240+
*/
241+
@NonNull
242+
public SurfaceProducer createPlatformViewSurfaceProducer(@NonNull SurfaceLifecycle lifecycle) {
243+
return createSurfaceProducerInternal(lifecycle, /* skipHardwareBufferDefectCheck= */ true);
244+
}
245+
246+
@NonNull
247+
private SurfaceProducer createSurfaceProducerInternal(
248+
SurfaceLifecycle lifecycle, boolean skipHardwareBufferDefectCheck) {
226249
final SurfaceProducer entry;
227250
if (!debugForceSurfaceProducerGlTextures
228251
&& Build.VERSION.SDK_INT >= API_LEVELS.API_29
229-
&& !hasAndroidHardwareBufferDefect()) {
252+
&& (skipHardwareBufferDefectCheck || !hasAndroidHardwareBufferDefect())) {
230253
final long id = nextTextureId.getAndIncrement();
231254
final ImageReaderSurfaceProducer producer = new ImageReaderSurfaceProducer(id);
232255
boolean reset = lifecycle == SurfaceLifecycle.resetInBackground;

‎engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,9 @@ private static PlatformViewRenderTarget makePlatformViewRenderTarget(
10501050
? TextureRegistry.SurfaceLifecycle.resetInBackground
10511051
: TextureRegistry.SurfaceLifecycle.manual;
10521052
final TextureRegistry.SurfaceProducer textureEntry =
1053-
textureRegistry.createSurfaceProducer(lifecycle);
1053+
textureRegistry instanceof FlutterRenderer
1054+
? ((FlutterRenderer) textureRegistry).createPlatformViewSurfaceProducer(lifecycle)
1055+
: textureRegistry.createSurfaceProducer(lifecycle);
10541056
Log.i(TAG, "PlatformView is using SurfaceProducer backend");
10551057
return new SurfaceProducerPlatformViewRenderTarget(textureEntry);
10561058
}

‎engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,44 @@ public void createSurfaceProducer_usesSurfaceTextureWhenHardwareBufferDefect() {
842842
producer instanceof SurfaceTextureSurfaceProducer);
843843
}
844844

845+
@Test
846+
@Config(sdk = 29)
847+
public void createSurfaceProducer_resetInBackgroundKeepsHardwareBufferDefectFallback() {
848+
// Plugins can also ask for resetInBackground, they must keep the #166481 fallback.
849+
ShadowBuild.setManufacturer("HUAWEI");
850+
851+
FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
852+
TextureRegistry.SurfaceProducer producer =
853+
flutterRenderer.createSurfaceProducer(TextureRegistry.SurfaceLifecycle.resetInBackground);
854+
855+
assertTrue(producer instanceof SurfaceTextureSurfaceProducer);
856+
}
857+
858+
@Test
859+
@Config(sdk = 29)
860+
public void createPlatformViewSurfaceProducer_usesImageReaderWhenHardwareBufferDefect() {
861+
// See https://github.com/flutter/flutter/issues/188303.
862+
ShadowBuild.setManufacturer("HUAWEI");
863+
864+
FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
865+
TextureRegistry.SurfaceProducer producer =
866+
flutterRenderer.createPlatformViewSurfaceProducer(
867+
TextureRegistry.SurfaceLifecycle.resetInBackground);
868+
869+
assertTrue(producer instanceof FlutterRenderer.ImageReaderSurfaceProducer);
870+
}
871+
872+
@Test
873+
@Config(sdk = 28)
874+
public void createPlatformViewSurfaceProducer_usesSurfaceTextureBelowApi29() {
875+
FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
876+
TextureRegistry.SurfaceProducer producer =
877+
flutterRenderer.createPlatformViewSurfaceProducer(
878+
TextureRegistry.SurfaceLifecycle.resetInBackground);
879+
880+
assertTrue(producer instanceof SurfaceTextureSurfaceProducer);
881+
}
882+
845883
@Test
846884
public void ImageReaderSurfaceProducerDoesNotCropOrRotate() {
847885
FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();

‎engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewsControllerTest.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,43 @@ public PlatformView create(Context context, int viewId, Object args) {
192192
.createSurfaceProducer(TextureRegistry.SurfaceLifecycle.resetInBackground);
193193
}
194194

195+
@Test
196+
@Config(
197+
shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class},
198+
sdk = 29)
199+
public void itUsesPlatformViewSurfaceProducerOfFlutterRenderer() {
200+
PlatformViewsController platformViewsController = new PlatformViewsController();
201+
FlutterJNI jni = new FlutterJNI();
202+
platformViewsController.setFlutterJNI(jni);
203+
attach(jni, platformViewsController);
204+
FlutterRenderer renderer = spy(new FlutterRenderer(jni));
205+
doReturn(mock(TextureRegistry.SurfaceProducer.class))
206+
.when(renderer)
207+
.createPlatformViewSurfaceProducer(any());
208+
platformViewsController.textureRegistry = renderer;
209+
210+
PlatformViewRegistry registry = platformViewsController.getRegistry();
211+
registry.registerViewFactory(
212+
CountingPlatformView.VIEW_TYPE_ID,
213+
new PlatformViewFactory(StandardMessageCodec.INSTANCE) {
214+
@Override
215+
public PlatformView create(Context context, int viewId, Object args) {
216+
return new CountingPlatformView(context);
217+
}
218+
});
219+
220+
final PlatformViewCreationRequest request =
221+
new PlatformViewCreationRequest(
222+
0, CountingPlatformView.VIEW_TYPE_ID, 0, 0, 128, 128, View.LAYOUT_DIRECTION_LTR, null);
223+
PlatformView pView = platformViewsController.createPlatformView(request, true);
224+
platformViewsController.configureForTextureLayerComposition(pView, request);
225+
226+
// Platform views must not get the HardwareBuffer defect fallback meant for plugins.
227+
verify(renderer, times(1))
228+
.createPlatformViewSurfaceProducer(TextureRegistry.SurfaceLifecycle.resetInBackground);
229+
verify(renderer, never()).createSurfaceProducer(any());
230+
}
231+
195232
@Test
196233
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
197234
public void itNotifiesPlatformViewsOfEngineAttachmentAndDetachment() {

0 commit comments

Comments
 (0)