Skip to content

iOS: Convert platform views to points with the frame's pixel ratio - #196858

Merged
cbracken merged 2 commits into
flutter:masterfrom
cbracken:pv-refactor-4
Nov 19, 2026
Merged

cbracken merged 2 commits into
flutter:masterfrom
cbracken:pv-refactor-4

Conversation

@cbracken

@cbracken cbracken commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

FlutterPlatformViewsController converts platform views, their clips and backdrop filters, and any overlays layered above them from physical pixels into points by dividing by the scale of the main screen. The engine lays out each frame with the device pixel ratio of the Flutter view's screen, so the two differ when the Flutter view is on a screen with a different scale to the main screen (for example, an external display). In that case, platform views/clips/overlays will be positioned and sized incorrectly.

We now pass the device pixel ratio through several methods to ensure that it gets applied to platform views, mutators, and overlaps get converted to points correctly.

Also fixes a three tests that had been submitting a frame without having begun it, which had resulted in the device pixel ratio being zero. We now call beginFrameWithSize:devicePixelRatio: first.

There is no change to behaviour when the Flutter view is on the main screen. This fixes behaviour when on screens other than the main screen.

Issue: #112232

Pre-launch Checklist

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. 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.

`FlutterPlatformViewsController` converts platform views, their clips
and backdrop filters, and any overlays painted above them from physical
pixels into points by dividing by the scale of the main screen. The
engine lays out each frame with the device pixel ratio of the Flutter
view's screen, so the two differ when the Flutter view is on a screen
with a different scale to the main screen (for example, an external
display). In that case, platform views/clips/overlays will be positioned
and sized incorrectly.

We now pass the device pixel ratio through several methods to ensure
that it gets applied to platform views, mutators, and overlaps get
converted to points correctly.

Also fixes a three tests that had been submitting a frame without having
begun it, which had resulted in the device pixel ratio being zero. We
now call `beginFrameWithSize:devicePixelRatio:` first.

There is no change to behaviour when the Flutter view is on the main
screen. This fixes behaviour when on screens other than the main screen.

Issue: flutter#112232
@cbracken
cbracken requested a review from a team as a code owner November 18, 2026 05:24
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Nov 18, 2026
@github-actions github-actions Bot added platform-ios iOS applications specifically engine flutter/engine related. See also e: labels. team-ios Owned by iOS platform team labels Nov 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the iOS platform views implementation to use the device pixel ratio of the current frame instead of hardcoding UIScreen.mainScreen.scale. The devicePixelRatio is now passed through beginFrameWithSize:, performSubmit:, compositeLayer:, and related methods to scale layout frames, clips, and overlay views. Review feedback highlights potential division-by-zero vulnerabilities across several files if devicePixelRatio or screenScale is less than or equal to 0.0, and suggests implementing defensive fallbacks to UIScreen.mainScreen.scale.

- (void)beginFrameWithSize:(flutter::DlISize)frameSize;
/// @brief Mark the beginning of a frame and record the size of the onscreen and the device pixel
/// ratio.
- (void)beginFrameWithSize:(flutter::DlISize)frameSize devicePixelRatio:(double)devicePixelRatio;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

are device pixel ratio and screen scale the same? why do we use 2 terms?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point; they're exactly the same thing. This code called it devicePixelRatio but the mask view code was using screenScale. Cleaned it up to be consistent.

/// The size of the current onscreen surface in physical pixels.
@property(nonatomic, assign) DlISize frameSize;

/// The device pixel ratio of the current frame.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we be explicit of what the ratio is (a/b or b/a?)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done. Added a doc comment to clarify it's physical pixels per point.

/// The device pixel ratio of the current frame.
///
/// This state is only modified on the raster thread.
@property(nonatomic, assign) double devicePixelRatio;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: CGFloat

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

[self compositeLayer:flutter::GetPlatformViewLayerFromEmbeddedViewParams(viewId, params,
screenScale)];
screenScale)
devicePixelRatio:screenScale];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is still getting scale from UIScreen.main.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep! This one is intentional. This is a test helper method (it's in the Testing category), and the methods that use it all build their mutators using the [UIScreen mainScreen scale. There are about 40 of those. I could update them to take devicePixelRatio but figured it's not worth it since I have a followup that deletes this altogether.

All real embedder code goes through performSubmit: which does use the frame's device pixel ratio.

@cbracken cbracken added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 19, 2026
@cbracken
cbracken added this pull request to the merge queue Nov 19, 2026
Merged via the queue into flutter:master with commit 312671e Nov 19, 2026
21 checks passed
@cbracken
cbracken deleted the pv-refactor-4 branch November 19, 2026 02:20
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 19, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 19, 2026
flutter/flutter@06f32f8...e8cc6f3

2026-11-19 [email protected] Roll Skia from 280e0965687d to 3dfb69d709c7 (1 revision) (flutter/flutter#196970)
2026-11-19 [email protected] Update outdated `_formattedValues` documentation (flutter/flutter#195765)
2026-11-19 [email protected] [Impeller] Fix Vulkan GPU frame time metrics in benchmarks (flutter/flutter#196339)
2026-11-19 [email protected] Windows: Enable D3D11 multithread protection on ANGLE's device (flutter/flutter#196897)
2026-11-19 [email protected] Roll Skia from 7356c7627e87 to 280e0965687d (1 revision) (flutter/flutter#196957)
2026-11-19 [email protected] Roll Dart SDK from 091f346ad95c to d8d40cf9c21b (2 revisions) (flutter/flutter#196949)
2026-11-19 [email protected] Roll Skia from 73b9219fd1fa to 7356c7627e87 (4 revisions) (flutter/flutter#196951)
2026-11-19 [email protected] Roll Skia from 7bca23228537 to 73b9219fd1fa (1 revision) (flutter/flutter#196948)
2026-11-19 [email protected] [libimpeller] Fix swapped x and y glyph coordinates (flutter/flutter#196552)
2026-11-19 [email protected] [ios][spm]When using cocoapods plugin, give instruction that plugins may just need update (flutter/flutter#195853)
2026-11-19 [email protected] [web] Let the browser handle Control+wheel zoom outside of macOS (flutter/flutter#193362)
2026-11-19 [email protected] Fix focus violation when canRequestFocus set to false for all siblings in same build (flutter/flutter#191536)
2026-11-19 [email protected] Roll Skia from 9fc4f9e23d56 to 7bca23228537 (10 revisions) (flutter/flutter#196941)
2026-11-19 [email protected] Roll Dart SDK from 753488ede427 to 091f346ad95c (2 revisions) (flutter/flutter#196940)
2026-11-19 [email protected] iOS: Convert platform views to points with the frame's pixel ratio (flutter/flutter#196858)
2026-11-19 [email protected] [engine] Defer default font manager resolution to first use for platforms that have a prefetching API only (flutter/flutter#196377)
2026-11-18 [email protected] [flutter_tools] Add example app to package template and decouple platform configurations (flutter/flutter#192441)
2026-11-18 [email protected] [flutter_tools] Rebuild asset bundle when generated manifest content changes (flutter/flutter#191638)
2026-11-18 [email protected] Roll Skia from b11d69796f71 to 9fc4f9e23d56 (2 revisions) (flutter/flutter#196909)
2026-11-18 [email protected] [flutter_tools] Bump cupertino_icons to ^2.0.0 in app and module templates (flutter/flutter#196828)
2026-11-18 [email protected] Update iOS plugin language instructions (flutter/flutter#196614)
2026-11-18 [email protected] iOS: Add tests for rounded rect clips with per-corner/elliptical radii (flutter/flutter#196854)
2026-11-18 [email protected] Revert: [flutter_tools] Migrate lib/runner.dart off runInContext and overrides (flutter/flutter#196926)
2026-11-18 [email protected] Revert: Move macOS desktop builds to ThinLTO (flutter/flutter#196917)
2026-11-18 [email protected] [FGP] Offset split-APK versionCodes from a manifest-only versionCode (flutter/flutter#196561)
2026-11-18 [email protected] [flutter_tools] Migrate lib/runner.dart off runInContext and overrides (flutter/flutter#196387)
2026-11-18 [email protected] docs: remove broken image references from dev/snippets/README.md (flutter/flutter#195587)
2026-11-18 [email protected] Remove the FGP AGP-public-API migration coordination docs (flutter/flutter#196733)
2026-11-18 [email protected] [web] Skip stale safeBlur focus transfer when the element was refocused (flutter/flutter#196550)
2026-11-18 [email protected] Roll Skia from 81ee7327e81c to b11d69796f71 (4 revisions) (flutter/flutter#196900)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: 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

CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-ios iOS applications specifically team-ios Owned by iOS platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants