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

[camerax] Re-land SurfaceProducer migration with fix for camera preview rotation #6856

Merged
merged 27 commits into from
Aug 19, 2024

Conversation

camsim99
Copy link
Contributor

@camsim99 camsim99 commented Jun 3, 2024

Re-lands #6462 with the following change to buildPreview:

Switches on a fix for correctly rotating the camera preview

This fix is required for Surfaces not backed by a SurfaceTexture because they don't contain the transformation information needed to correctly rotate the camera preview. In that case, we use the logic described in https://developer.android.com/media/camera/camera2/camera-preview#orientation_calculation. This logic was added in a previous PR (#7044); this simply enables it for devices where Surfaces are not backed by a SurfaceTexture (see how this is determined).

Fixes flutter/flutter#149294.

Pre-launch Checklist

@stuartmorgan stuartmorgan added the triage-android Should be looked at in Android triage label Jun 3, 2024
@camsim99 camsim99 changed the title [camerax] Fix camera preview to support Impeller [camerax] Re-land SurfaceProducer migration with fixes for correcting camera preview Jun 4, 2024
@camsim99 camsim99 changed the title [camerax] Re-land SurfaceProducer migration with fixes for correcting camera preview [camerax] Re-land SurfaceProducer migration with fix for camera preview rotation Jun 4, 2024
@@ -10,7 +10,7 @@ import 'package:camera_platform_interface/camera_platform_interface.dart';
import 'package:flutter/services.dart'
show DeviceOrientation, PlatformException;
import 'package:flutter/widgets.dart'
show Size, Texture, Widget, visibleForTesting;
show RotatedBox, Size, Texture, Widget, visibleForTesting;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used RotatedBox to rotate the preview mostly because that is what is used in the plugin code, but if there is a better way to rotate it, let me know.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks reasonable to me. @jonahwilliams?

@camsim99
Copy link
Contributor Author

camsim99 commented Jun 6, 2024

@chinmaygarde if you get the chance to test this out and/or bring the landscape device to SVL next week I would love to see how it behaves with this change!

Other than that fix (I left TODOs in the code), this PR can start going through code review :)

@camsim99 camsim99 marked this pull request as ready for review June 6, 2024 21:13
@camsim99
Copy link
Contributor Author

camsim99 commented Jul 2, 2024

As discussed offline with @matanlurey, going to land the fixes to the camera preview rotation without Impeller support in #7044 first and land this (which will essentially only include re-lading Impeller support) later on.

auto-submit bot pushed a commit that referenced this pull request Jul 5, 2024
…evices and set up fix for Impeller support (#7044)

Partially lands #6856. This PR specifically includes:

#### 1: A fix for correctly rotating the camera preview
This fix is required for `Surface`s not backed by a `SurfaceTexture` because they don't contain the transformation information needed to correctly rotate the camera preview. In that case, we use the logic described in https://developer.android.com/media/camera/camera2/camera-preview#orientation_calculation.

The fix  is **not currently used** (the logic is not reachable) as Impeller support has not been added back to the plugin, but has been tested in #6856 and will be turned on when flutter/flutter#149294 is fixed.

Part of flutter/flutter#149294.

#### 2: A fix for correctly rotating the camera preview on naturally landscape-oriented devices
I believe this issue was caused because we assume that the natural orientation of the device is portrait up. We fix this here by adding an extra rotation for the camera preview based on the natural orientation of the device.

Fixes flutter/flutter#149177.
@camsim99 camsim99 marked this pull request as draft July 18, 2024 20:29
@camsim99 camsim99 requested a review from matanlurey August 13, 2024 18:44
@camsim99 camsim99 marked this pull request as ready for review August 13, 2024 18:44
@matanlurey
Copy link
Contributor

We need to add the lifecycle methods to correctly suspend/resume the camera surface:
https://docs.flutter.dev/release/breaking-changes/android-surface-plugins#migration-guide

(Unless I missed something and that's being handled elsewhere?)

@camsim99
Copy link
Contributor Author

We need to add the lifecycle methods to correctly suspend/resume the camera surface: https://docs.flutter.dev/release/breaking-changes/android-surface-plugins#migration-guide

(Unless I missed something and that's being handled elsewhere?)

I assume that the preview surface provider already handles that, but let me know if that doesn't cover everything!

@matanlurey
Copy link
Contributor

Despite the names those are indeed different things

@camsim99
Copy link
Contributor Author

camsim99 commented Aug 15, 2024

I just meant that the method will be called when a new surface is needed which causes CameraX to detach the surface from the camera and return a new surface (see our impl), effectively doing the same thing that doc describes, so I don't think there's anything else to do here.

@camsim99
Copy link
Contributor Author

To double check that this method is called if a surface gets destroyed, though, I can follow up internally :)

@matanlurey
Copy link
Contributor

I see, perhaps you're right - if it works it works!

@camsim99
Copy link
Contributor Author

camsim99 commented Aug 15, 2024

Followed up with the CameraX team and found out that we need to call SurfaceRequest.invalidate() (docs) in order for a new request for a surface to be called and thus, for the Preview.SurfaceProvider callback to handle creating a new surface.

This means we either have to save the requests we receive to invalidate it in the SurfaceProducer callback or we create a new SurfaceProvider instance every time a request for a surface comes in. I assume the latter is discouraged, can you confirm that @matanlurey ? edit: JK I think I found a middle ground :)

@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 19, 2024
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Aug 19, 2024
Copy link
Contributor

auto-submit bot commented Aug 19, 2024

auto label is removed for flutter/packages/6856, due to - The status or check suite Mac_arm64 custom_package_tests master has failed. Please fix the issues identified (or deflake) before re-applying this label.

@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 19, 2024
@auto-submit auto-submit bot merged commit 7740990 into flutter:main Aug 19, 2024
76 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Aug 20, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Aug 20, 2024
flutter/packages@7c1a05c...4d2d2e3

2024-08-20 [email protected] [webview] Add macOS support in implementation package (flutter/packages#6221)
2024-08-19 [email protected] Roll Flutter from a0c0453 to 6a28048 (22 revisions) (flutter/packages#7449)
2024-08-19 [email protected] [ci] Remove scorecard action (flutter/packages#7450)
2024-08-19 [email protected] [camerax] Re-land SurfaceProducer migration with fix for camera preview rotation (flutter/packages#6856)
2024-08-19 49699333+dependabot[bot]@users.noreply.github.com [camera]: Bump com.google.guava:guava from 33.2.1-android to 33.3.0-android in /packages/camera/camera_android_camerax/android (flutter/packages#7441)
2024-08-19 49699333+dependabot[bot]@users.noreply.github.com [sign_in]: Bump com.google.guava:guava from 33.2.1-android to 33.3.0-android in /packages/google_sign_in/google_sign_in_android/android (flutter/packages#7442)

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 [email protected],[email protected] 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
sfshaza2 added a commit to flutter/website that referenced this pull request Aug 20, 2024
_Description of what this PR is changing or adding, and why:_ 
Adds a note to the Impeller Android plugins migration to clarify that
plugin authors that create a camera preview may additionally need to
correct the rotation of the preview due to rotation information of
produced `Surface`s being lost.

_Issues fixed by this PR (if any):_ 
N/A

_PRs or commits this PR depends on (if any):_ 
Should land after flutter/packages#6856 as this
is mentioned as an example.

cc @navaronbracke

## Presubmit checklist

- [x] This PR is marked as draft with an explanation if not meant to
land until a future stable release.
- [x] This PR doesn’t contain automatically generated corrections
(Grammarly or similar).
- [x] This PR follows the [Google Developer Documentation Style
Guidelines](https://developers.google.com/style) — for example, it
doesn’t use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person).
- [x] This PR uses [semantic line
breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks)
of 80 characters or fewer.

---------

Co-authored-by: Parker Lougheed <[email protected]>
Co-authored-by: Shams Zakhour (ignore Sfshaza) <[email protected]>
Buchimi pushed a commit to Buchimi/flutter that referenced this pull request Sep 2, 2024
flutter/packages@7c1a05c...4d2d2e3

2024-08-20 [email protected] [webview] Add macOS support in implementation package (flutter/packages#6221)
2024-08-19 [email protected] Roll Flutter from a0c0453 to 6a28048 (22 revisions) (flutter/packages#7449)
2024-08-19 [email protected] [ci] Remove scorecard action (flutter/packages#7450)
2024-08-19 [email protected] [camerax] Re-land SurfaceProducer migration with fix for camera preview rotation (flutter/packages#6856)
2024-08-19 49699333+dependabot[bot]@users.noreply.github.com [camera]: Bump com.google.guava:guava from 33.2.1-android to 33.3.0-android in /packages/camera/camera_android_camerax/android (flutter/packages#7441)
2024-08-19 49699333+dependabot[bot]@users.noreply.github.com [sign_in]: Bump com.google.guava:guava from 33.2.1-android to 33.3.0-android in /packages/google_sign_in/google_sign_in_android/android (flutter/packages#7442)

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 [email protected],[email protected] 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 triage-android Should be looked at in Android triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[camera_android_camerax] Camera preview is rotated on the supported Impeller version
5 participants