Skip to content

Safeguard against very large Bitmap creation#2662

Merged
allenchen1154 merged 2 commits intomasterfrom
allen--guard-against-software-rendering-bitmap-oom
Oct 31, 2025
Merged

Safeguard against very large Bitmap creation#2662
allenchen1154 merged 2 commits intomasterfrom
allen--guard-against-software-rendering-bitmap-oom

Conversation

@allenchen1154
Copy link
Copy Markdown
Collaborator

Because we rely on the transform Matrix returned from the Canvas to size a Bitmap when using software rendering, an erroneous matrix value can cause a very large Bitmap to be allocated, triggering an OutOfMemory exception.

We've encountered such a situation when using layoutlib for screenshot tests, which uses a NopCanvas on initial render that can return an invalid `Matrix.

Because we rely on the transform `Matrix` returned from the `Canvas` to size a `Bitmap` when using software rendering, an erroneous matrix value can cause a very large Bitmap to be allocated, triggering an OutOfMemory exception.

We've encountered such a situation when using `layoutlib` for screenshot tests, which uses a `NopCanvas` [on initial render](https://cs.android.com/android/_/android/platform/frameworks/layoutlib/+/7b05b277beee599532606e9bb6d7a71f5ca2ab6e:bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java;l=543;bpv=1;bpt=0;drc=085857f145aac790e2a08cf6eb9546f98e26c338) that can return an invalid `Matrix.
@allenchen1154 allenchen1154 requested a review from gpeal September 24, 2025 22:53
@github-actions
Copy link
Copy Markdown

Snapshot Tests
API 23: Report Diff
API 35: Report Diff


if (renderWidth <= 0 || renderHeight <= 0) {
// Safeguard against errors during Bitmap creation by returning early if dimensions are invalid.
if (renderWidth <= 0 || renderHeight <= 0 || renderWidth > bounds.width() || renderHeight > bounds.height()) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wouldn't this not render an image if it scaled up so it covered the whole screen + 1px?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

In that case can we clip the bounds so that we only allocate a bitmap large enough to draw what's shown onscreen?
I'm trying to guard against cases where the Canvas matrix returns an invalid scale or translation that causes something like a 1,000,000 x 1,000,000 bitmap to be allocated.

@gpeal
Copy link
Copy Markdown
Collaborator

gpeal commented Sep 28, 2025

Could you take a look at the snapshot tests? Looks like there are a bunch of blanks now.

@github-actions
Copy link
Copy Markdown

Snapshot Tests
API 23: Report Diff
API 35: Report Diff

@allenchen1154 allenchen1154 merged commit 05ee1c1 into master Oct 31, 2025
11 of 13 checks passed
@allenchen1154 allenchen1154 deleted the allen--guard-against-software-rendering-bitmap-oom branch October 31, 2025 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants