fix(android): capture the screen the way it is actually oriented - #693
Open
filip131311 wants to merge 1 commit into
Open
fix(android): capture the screen the way it is actually oriented#693filip131311 wants to merge 1 commit into
filip131311 wants to merge 1 commit into
Conversation
On a rotated Android device `screenshot` came back portrait-framed with the content lying sideways, while `describe` frames and gesture coordinates were already upright — uiautomator measures against the rotated display. So the capture was the one surface out of step, and an agent reading it saw an image whose geometry disagreed with every coordinate it was given. simulator-server already knows how to return an upright frame; it just has to be told which rotation to apply. The rotation is now queried from the device per capture, so `screenshot`, `screenshot-diff`'s live captures and the auto-attached screenshot all land in the same space as `describe`. Nothing is tracked. A cached orientation could only ever be staler than the ~40 ms probe, and serving a stale one is precisely the failure being fixed. When the rotation cannot be read the request is left exactly as it was rather than guessing: a wrong orientation yields a confidently wrong image, which is worse than a sideways one. iOS is deliberately untouched. There the whole surface — describe frames, gesture input and the capture — is consistently in the portrait-native space, so rotating only the capture would break that agreement rather than restore it. Also fixes the legacy uiautomator describe path, which was wrong on a rotated device for a related reason: `wm size` reports the UNROTATED size (measured on a landscape Pixel_9 still answering "Physical size: 1080x2424"), and dividing rotated bounds by it does not merely squash frames — nodes past the too-small width are pruned as off-screen, so the right-hand half of a landscape screen disappeared from the tree. The dump states its own rotation, so the divisor is oriented from bytes already in hand, with no extra round-trip on the path that runs precisely when the helper could not be reached. The rotation-name table is pinned by measurement against `adb exec-out screencap`, which is rotation-aware, scoring each candidate both as-is and rotated 180 degrees so an inverted mapping could not pass. It is the inverse of simulator-server's own convention because it compensates for a rotation applied during decode — which is why a table test alone is not enough, and the delivered image's aspect is checked against the rotation requested. Refs #609
This was referenced Aug 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #609.
What was wrong
On a rotated Android device
screenshotreturned a portrait-framed image with the content lying sideways.describeframes and gesture coordinates were already upright — uiautomator measures against the rotated display — so the capture was the one surface out of step. An agent reading that image saw geometry that disagreed with every coordinate it was given.Measured on a landscape Pixel_9 (API 36,
mCurrentOrientation=1):describe"Search Settings"centre(0.530, 0.234)"Display & touch"centre opens itscreenshot(default)The fix
simulator-server already applies
rotationcorrectly; nothing was telling it which one. The rotation is now queried from the device per capture, soscreenshot,screenshot-diff's live captures and the auto-attached "Screen after action" screenshot all land indescribe/tap space.Nothing is tracked. A cached orientation could only be staler than the ~40 ms probe, and serving a stale one is exactly the failure being fixed. When the rotation cannot be read the request is left byte-identical to before rather than guessing — a wrong orientation is a confidently wrong image, which is worse than a sideways one.
iOS is deliberately untouched. There the whole surface is consistently portrait-native, so rotating only the capture would break an agreement rather than restore one. The iOS half of #609 needs a different change and is not in this PR.
Also fixed: the legacy uiautomator describe path
Same root cause, worse symptom.
wm sizereports the unrotated size — on the landscape Pixel_9 it still answeredPhysical size: 1080x2424. Dividing rotated bounds by that does not merely squash frames:isVisibleRectprunes nodes whose left edge is past the screen width, so the right-hand half of a landscape screen vanished from the tree. The dump carries<hierarchy rotation="N">, so the divisor is oriented from bytes already in hand — no extra round-trip on the path that runs precisely when the helper could not be reached.On the rotation-name table
Pinned by measurement, never by what the names mean — the reporter of #609 found the same names are 180° inverted on iOS.
adb exec-out screencapis rotation-aware, so it is ground truth; each candidate was scored as mean absolute difference against it both as-is and rotated 180°, so an inverted mapping could not pass:This table is the inverse of simulator-server's own
90° → LandscapeRightconvention, because it compensates for a rotation that layer applies while decoding the scrcpy stream. That makes it a property of a particular simulator-server build rather than of Android — which is why a table test alone is not enough, and the delivered PNG's aspect is checked against the rotation requested, falling back to an unrotated capture if they disagree.Verified live
Branch tool-server against a real emulator:
describe's frame for the toolbar out of that default capture shows exactly the "Display & touch" toolbar — the two spaces agreePlus 27 new unit tests, including one that asserts the legacy path drops the right-hand node before the fix and keeps it after. Full suite 3125 passing; lint, prettier,
typecheck:testsand the tool-description gate all clean.Not in scope
rotateon Android silently no-ops when auto-rotate is enabled (it returned{orientation:"LandscapeLeft"}whiledumpsysstill readcur=1080x2424). Filing separately.screen-recordingstill records sideways — same root cause, different pipeline. Worth noting a session's screenshots are now upright while its video is not.