[camera_avfoundation] Fixes crash when using ResolutionPreset.max with iPhone 17#11106
[camera_avfoundation] Fixes crash when using ResolutionPreset.max with iPhone 17#11106auto-submit[bot] merged 15 commits intoflutter:mainfrom
Conversation
| // found in the LICENSE file. | ||
|
|
||
| @testable import camera_avfoundation | ||
| import UIKit |
There was a problem hiding this comment.
i needed to add this to compile
There was a problem hiding this comment.
it's using UIDeviceOrientation from UIKit, so it wouldn't compile. I am not sure why this compiles fine on CI
There was a problem hiding this comment.
Code Review
This pull request addresses a critical crash when using ResolutionPreset.max with newer iPhone models that output btp2 pixel format, which the Flutter engine cannot render. It also resolves an issue where ResolutionPreset.max could return a square resolution due to Centre Stage camera changes. The changes introduce filtering for unsupported pixel formats and non-4:3 aspect ratios, ensuring a stable and expected camera output. New test cases have been added to validate this behavior.
| } | ||
|
|
||
| /// Finds the highest available resolution in terms of pixel count for the given device. | ||
| /// Finds the highest available resolution 16:9 in terms of pixel count for the given device. |
There was a problem hiding this comment.
The comment states "Finds the highest available resolution 16:9", but the implementation filters for a 4:3 aspect ratio (is4x3 = abs(ratio - 4.0 / 3.0) < 0.05). Please update the comment to accurately reflect the 4:3 aspect ratio filtering.
| /// Finds the highest available resolution 16:9 in terms of pixel count for the given device. | |
| /// Finds the highest available resolution 4:3 in terms of pixel count for the given device. |
| // Apple's lossy compressed formats that Flutter Metal can't render. | ||
| let unsupportedSubTypes: [FourCharCode] = [ | ||
| 1_651_798_066 // Hex for 'btp2', or kCVPixelFormatType_96VersatileBayerPacked12 | ||
| ] |
There was a problem hiding this comment.
The unsupportedSubTypes array is currently defined inside the highestResolutionFormat function. While its scope is limited to this function, defining it as a static let constant outside the function would prevent its re-initialization on every call to highestResolutionFormat, improving minor efficiency and making it clear that these are globally unsupported types for the Flutter Metal renderer.
| // Apple's lossy compressed formats that Flutter Metal can't render. | |
| let unsupportedSubTypes: [FourCharCode] = [ | |
| 1_651_798_066 // Hex for 'btp2', or kCVPixelFormatType_96VersatileBayerPacked12 | |
| ] | |
| static let unsupportedSubTypes: [FourCharCode] = [ | |
| 1_651_798_066 // Hex for 'btp2', or kCVPixelFormatType_96VersatileBayerPacked12 | |
| ] | |
| // Apple's lossy compressed formats that Flutter Metal can't render. |
| // found in the LICENSE file. | ||
|
|
||
| @testable import camera_avfoundation | ||
| import UIKit |
There was a problem hiding this comment.
it's using UIDeviceOrientation from UIKit, so it wouldn't compile. I am not sure why this compiles fine on CI
| / Double(min(resolution.width, resolution.height)) | ||
| let is4x3 = abs(ratio - 4.0 / 3.0) < 0.05 | ||
|
|
||
| if !is4x3 { |
There was a problem hiding this comment.
let's change this to checking against 1x1 square aspect ratio. can add a comment saying we don't support center stage yet.
| } | ||
|
|
||
| /// Finds the highest available resolution in terms of pixel count for the given device. | ||
| /// Finds the highest available 4:3 resolution in terms of pixel count for the given device. |
There was a problem hiding this comment.
also update comment here
| var isBestSubTypePreferred = false | ||
|
|
||
| let unsupportedSubTypes: [FourCharCode] = [ | ||
| 1_651_798_066 // Hex for 'btp2', or kCVPixelFormatType_96VersatileBayerPacked12 |
There was a problem hiding this comment.
can you add a comment saying this is compressed format that's not supported by the engine
…et.max with iPhone 17 (flutter/packages#11106)
…et.max with iPhone 17 (flutter/packages#11106)
flutter/packages@9083bc9...82baf93 2026-03-04 louisehsu@google.com [camera_avfoundation] Fixes crash when using ResolutionPreset.max with iPhone 17 (flutter/packages#11106) 2026-03-03 43054281+camsim99@users.noreply.github.com [camera_android_camerax] Allow usage of all use cases concurrently (flutter/packages#10732) 2026-03-03 matt.boetger@gmail.com [Many] Remove network policy from metadata (flutter/packages#11165) 2026-03-03 matt.boetger@gmail.com [Espresso] remove metadata network policy (flutter/packages#11164) 2026-03-03 stuartmorgan@google.com [tool] Add initial non-plugin package support for Kotlin Gradle (flutter/packages#11127) 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 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
Fixes flutter/flutter#175828
The iPhone 17 family's new camera supports a new pixel format, btp2, which is a lossy, compressed format. At 12MP+ resolutions, AVFoundation drops support for standard YUV/BGRA and strictly outputs this format (e.g., 1651798066 / btp2). The engine renderer expects raw pixels and crashes immediately when given this compressed format. (relevant engine issue: flutter/flutter#182908)
Separately, Centre Stage was introduced for iPhone 17 which is a square sensor. A side effect of this is when we request the highest non-btp resolutions, we previously selected based off total number of pixels. This meant that it was returning a wonky 4032x4032 square. I added a check to only return 4:3 resolutions that aren't square like it did previously, just to avoid previous implementations looking weird.
Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the [pub versioning philosophy], or I have commented below to indicate which [version change exemption] this PR falls under[^1].CHANGELOG.mdto add a description of the change, [following repository CHANGELOG style], or I have commented below to indicate which [CHANGELOG exemption] this PR falls under[^1].///).