Skip to content

Commit 051c13c

Browse files
serbandinreidbakercamsim99
authored
fix-huawei-imagereader-166481 (#181931)
Fixes video playback issues on Huawei devices running Android API level 29 or below by falling back to SurfaceTextureSurfaceProducer instead of ImageReaderSurfaceProducer. ### Problem On certain Huawei devices with Android 10 (API 29), video playback fails when using ImageReaderSurfaceProducer. This is due to issues with AHB (Android Hardware Buffer) imports on these devices, similar to the Vulkan fallback already implemented in android_context_dynamic_impeller.cc. Fixes flutter/flutter#166481 ### Solution Added a new JNI method shouldDisableImageReader() that detects Huawei devices by checking the ro.com.google.clientidbase system property for the value "android-huawei" when the API level is 29 or below. When this method returns true, FlutterRenderer.createSurfaceProducer() uses SurfaceTextureSurfaceProducer instead of ImageReaderSurfaceProducer, which resolves the video playback issue. ### Changes FlutterJNI.java: Added shouldDisableImageReader() method and native bridge platform_view_android_jni_impl.cc: Native implementation using __system_property_get() and android_get_device_api_level() FlutterRenderer.java: Integration in createSurfaceProducer() condition (line 230) FlutterRendererTest.java: Unit test for the new behavior ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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 documentation (doc comments with `///`). - [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. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **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 [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 [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: Reid Baker <[email protected]> Co-authored-by: Camille Simon <[email protected]>
1 parent 7fc8a87 commit 051c13c

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ public class FlutterRenderer implements TextureRegistry {
6363
*/
6464
@VisibleForTesting public static boolean debugForceSurfaceProducerGlTextures = false;
6565

66+
/**
67+
* Returns true if this device has a known {@link android.hardware.HardwareBuffer} defect.
68+
*
69+
* <p>Huawei devices on API 29 or below have {@link android.media.ImageReader} issues that cause
70+
* video playback failures due to defective HardwareBuffer implementations.
71+
*
72+
* @see <a href="https://github.com/flutter/flutter/issues/166481">#166481</a> See <a
73+
* href="https://github.com/flutter/flutter/issues/156459">#156459</a> See <a
74+
* href="https://github.com/flutter/flutter/issues/154068">#154068</a> See <a
75+
* href="https://github.com/flutter/engine/pull/54879">engine pr #54879</a>
76+
*/
77+
private static boolean hasAndroidHardwareBufferDefect() {
78+
// To test if some device should be added from this list app developers first should attempt to
79+
// play a video and see a failure in the logs.
80+
/// ```E/ACodec: Failed to allocate buffers after transitioning to IDLE state (error 0xfffffc0e)
81+
/// W/ACodec: [OMX.hisi.video.decoder.avc] setting nBufferCountActual to 12 failed: -1010
82+
// E/MediaCodecVideoRenderer: Video codec error```
83+
/// Then try setting `FlutterRenderer.debugForceSurfaceProducerGlTextures = true` in an example
84+
// app.
85+
/// If the video plays then that is a strong signal that this method should be updated to
86+
// include the tested device.
87+
return Build.VERSION.SDK_INT <= API_LEVELS.API_29
88+
&& "HUAWEI".equalsIgnoreCase(Build.MANUFACTURER);
89+
}
90+
6691
/** Whether to disable clearing of the Surface used to render platform views. */
6792
@VisibleForTesting public static boolean debugDisableSurfaceClear = false;
6893

@@ -199,7 +224,9 @@ private void clearDeadListeners() {
199224
@Override
200225
public SurfaceProducer createSurfaceProducer(SurfaceLifecycle lifecycle) {
201226
final SurfaceProducer entry;
202-
if (!debugForceSurfaceProducerGlTextures && Build.VERSION.SDK_INT >= API_LEVELS.API_29) {
227+
if (!debugForceSurfaceProducerGlTextures
228+
&& Build.VERSION.SDK_INT >= API_LEVELS.API_29
229+
&& !hasAndroidHardwareBufferDefect()) {
203230
final long id = nextTextureId.getAndIncrement();
204231
final ImageReaderSurfaceProducer producer = new ImageReaderSurfaceProducer(id);
205232
boolean reset = lifecycle == SurfaceLifecycle.resetInBackground;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.junit.Test;
4545
import org.junit.runner.RunWith;
4646
import org.mockito.ArgumentCaptor;
47+
import org.robolectric.annotation.Config;
48+
import org.robolectric.shadows.ShadowBuild;
4749

4850
@RunWith(AndroidJUnit4.class)
4951
public class FlutterRendererTest {
@@ -823,6 +825,23 @@ public void SurfaceTextureSurfaceProducerDoesNotCropOrRotate() {
823825
}
824826
}
825827

828+
@Test
829+
@Config(sdk = 29)
830+
public void createSurfaceProducer_usesSurfaceTextureWhenHardwareBufferDefect() {
831+
// Setup: Simulate a Huawei device on API 29 where HardwareBuffer has known defects.
832+
// See: https://github.com/flutter/flutter/issues/166481
833+
ShadowBuild.setManufacturer("HUAWEI");
834+
835+
// Execute the behavior under test.
836+
FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
837+
TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer();
838+
839+
// Verify: Should return SurfaceTextureSurfaceProducer instead of ImageReaderSurfaceProducer.
840+
assertTrue(
841+
"Expected SurfaceTextureSurfaceProducer on Huawei API <= 29 due to HardwareBuffer defect causing video playback failures",
842+
producer instanceof SurfaceTextureSurfaceProducer);
843+
}
844+
826845
@Test
827846
public void ImageReaderSurfaceProducerDoesNotCropOrRotate() {
828847
FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();

0 commit comments

Comments
 (0)