Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate camera/android from SurfaceTexture->SurfaceProducer. #6461

Merged
merged 15 commits into from
May 29, 2024

Conversation

matanlurey
Copy link
Contributor

WIP: We do not plan to land this PR until the next stable release (>= April 3rd 2024).

Work towards flutter/flutter#145930.

Details

Migrates uses of createSurfaceTexture to createSurfaceProducer, which is intended to have no change in behavior, but does change the backend rendering path, so it will require more testing (and we're also open to minor API renames or changes before it becomes stable).

Background

Android plugins previously requested a SurfaceTexture from the Android embedder, and used that to produce a Surface to render external textures on (i.e. video_player). This worked because 100% of Flutter applications on Android used OpenGLES (via our Skia backend), and SurfaceTexture is actually an (opaque) OpenGLES-texture.

Starting soon (roughly ~Q3, this is not a guarantee and just an estimate), Flutter on Android will start to use our new Impeller graphics backend, which on newer devices (>= API_VERSION_28), will default to the Vulkan, not OpenGLES. In other words, SurfaceTexture will cease to work (it is possible, but non-trivial, to map an OpenGLES texture over to Vulkan).

After consultation with the Android team, they helped us understand that vending SurfaceTexture (the consumer-side API) was never the right abstraction, and we should have been vending the producer-side API, or Surface directly. The new SurfaceProducer API is exactly that - it generates a Surface, and similar to our platform view strategy, picks the "right" consumer-side implementation details for the user/plugin packages.

The new SurfaceProducer API has 2 possible rendering types (as an implementation detail):

  • SurfaceTexture, for older OpenGLES devices, which works exactly as it does today.
  • ImageReader, for newer OpenGLES or Vulkan devices.

These are some subtle nuances in how these two APIs work differently (one example: flutter/flutter#144407), but our theory at this point is we don't expect these changes to be observed by any users, and we have other ideas if necessary.

Note

These invariants are tested on CI in flutter/engine.

Points of contact:

auto-submit bot pushed a commit to flutter/engine that referenced this pull request Apr 18, 2024
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/wiki/Flutter-Cherrypick-Process#automatically-creates-a-cherry-pick-request)
Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request.

### Issue Link:
What is the link to the issue this cherry-pick is addressing?

Test failures in Impeller migration PR: flutter/packages#6461

### Changelog Description:
Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/wiki/Hotfix-Documentation-Best-Practices) for examples

Ensure that Android platform view updates are posted on correct thread when platform view is created on background handler.

### Impact Description:
What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch)

Migration to Impeller plugin APIs is blocked.

### Workaround:
Is there a workaround for this issue?

Disable impeller

### Risk:
What is the risk level of this cherry-pick?

### Test Coverage:
Are you confident that your fix is well-tested by automated tests?

  However the tests are in flutter/packages and not flutter/engine.

### Validation Steps:
What are the steps to validate that this fix works?

Run the unit tests on the linked PR in flutter/packages
@matanlurey
Copy link
Contributor Author

Due to a release hiccup, the stable branch (3.22) is temporarily ahead of the master branch (3.22.0-35.0.pre.34), leading to CI failures (see discord discussion). I've merged back into HEAD and updated the pubspec/CHANGELOG - the rest might be done by someone else while I'm on vacation for a couple of weeks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file a local conflict resolution mistake?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably something went wrong applying the formatting diff.

@@ -104,7 +104,8 @@ public void createCaptureSession(
}

@Override
public void close() {}
public void close() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, that formatting didn't work either

@jonahwilliams
Copy link
Member

Thank you @stuartmorgan !

@jonahwilliams
Copy link
Member

Current test failure is:



java.lang.Exception: ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞═════════════════
The following PlatformException was thrown running a test:
PlatformException(error, Attempt to invoke virtual method 'int
android.hardware.camera2.CameraCaptureSession.capture(android.hardware.camera2.CaptureRequest,
android.hardware.camera2.CameraCaptureSession$CaptureCallback,
android.os.Handler)' on a null object reference, null,
java.lang.NullPointerException: Attempt to invoke virtual method
'int
android.hardware.camera2.CameraCaptureSession.capture(android.hardware.camera2.CaptureRequest,
android.hardware.camera2.CameraCaptureSession$CaptureCallback,
android.os.Handler)' on a null object reference
at
io.flutter.plugins.camera.Camera.runPrecaptureSequence(Camera.java:660)
at io.flutter.plugins.camera.Camera.takePicture(Camera.java:645)
at
io.flutter.plugins.camera.MethodCallHandlerImpl.onMethodCall(MethodCallHandlerImpl.java:112)
at
io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
at
io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
at
io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
at
io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown
Source:12)


@jonahwilliams
Copy link
Member

@johnmccutchan @camsim99 PTAL

Copy link
Contributor

@camsim99 camsim99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

packages/camera/camera_android/CHANGELOG.md Outdated Show resolved Hide resolved
Co-authored-by: Camille Simon <43054281+camsim99@users.noreply.github.com>
@jonahwilliams jonahwilliams added the autosubmit Merge PR when tree becomes green via auto submit App label May 29, 2024
@auto-submit auto-submit bot merged commit 058851b into main May 29, 2024
74 checks passed
@auto-submit auto-submit bot deleted the migrate-surface-producer-camera-android branch May 29, 2024 18:53
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 30, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request May 30, 2024
flutter/packages@31d3329...910fabb

2024-05-29 58529443+srujzs@users.noreply.github.com Amend package:web tweaks to allow package:web roll (flutter/packages#6793)
2024-05-29 matanlurey@users.noreply.github.com Migrate `video_player/android` from `SurfaceTexture`->`SurfaceProducer`. (flutter/packages#6456)
2024-05-29 joonas.kerttula@codemate.com [google_maps_flutter] Undeprecate BitmapDescriptor methods (flutter/packages#6832)
2024-05-29 matanlurey@users.noreply.github.com Migrate CameraX from SurfaceTexture to SurfaceProducer. (flutter/packages#6462)
2024-05-29 matanlurey@users.noreply.github.com Migrate `camera/android` from `SurfaceTexture`->`SurfaceProducer`. (flutter/packages#6461)
2024-05-29 katelovett@google.com [dynamic_layouts] Remove the dynamic_layouts package (flutter/packages#6830)
2024-05-29 43054281+camsim99@users.noreply.github.com [camerax] Add notes about Android permissions (flutter/packages#6741)
2024-05-29 43054281+camsim99@users.noreply.github.com [Re-land] Bump legacy all_packages project AGP version to 7.0.0, Gradle version to 7.0.2 (flutter/packages#6742)
2024-05-29 engine-flutter-autoroll@skia.org Roll Flutter from a1a33e6 to c85fa6a (20 revisions) (flutter/packages#6829)
2024-05-29 katelovett@google.com [rfw] Migrate deprecated doc references (flutter/packages#6744)
2024-05-29 katelovett@google.com [flutter_adaptive_scaffold] Migrate MaterialStateProperty to WidgetStateProperty (flutter/packages#6743)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
victorsanni pushed a commit to victorsanni/flutter that referenced this pull request May 31, 2024
…er#149321)

flutter/packages@31d3329...910fabb

2024-05-29 58529443+srujzs@users.noreply.github.com Amend package:web tweaks to allow package:web roll (flutter/packages#6793)
2024-05-29 matanlurey@users.noreply.github.com Migrate `video_player/android` from `SurfaceTexture`->`SurfaceProducer`. (flutter/packages#6456)
2024-05-29 joonas.kerttula@codemate.com [google_maps_flutter] Undeprecate BitmapDescriptor methods (flutter/packages#6832)
2024-05-29 matanlurey@users.noreply.github.com Migrate CameraX from SurfaceTexture to SurfaceProducer. (flutter/packages#6462)
2024-05-29 matanlurey@users.noreply.github.com Migrate `camera/android` from `SurfaceTexture`->`SurfaceProducer`. (flutter/packages#6461)
2024-05-29 katelovett@google.com [dynamic_layouts] Remove the dynamic_layouts package (flutter/packages#6830)
2024-05-29 43054281+camsim99@users.noreply.github.com [camerax] Add notes about Android permissions (flutter/packages#6741)
2024-05-29 43054281+camsim99@users.noreply.github.com [Re-land] Bump legacy all_packages project AGP version to 7.0.0, Gradle version to 7.0.2 (flutter/packages#6742)
2024-05-29 engine-flutter-autoroll@skia.org Roll Flutter from a1a33e6 to c85fa6a (20 revisions) (flutter/packages#6829)
2024-05-29 katelovett@google.com [rfw] Migrate deprecated doc references (flutter/packages#6744)
2024-05-29 katelovett@google.com [flutter_adaptive_scaffold] Migrate MaterialStateProperty to WidgetStateProperty (flutter/packages#6743)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: camera platform-android
Projects
None yet
5 participants