Skip to content

Conversation

allenchen1154
Copy link
Contributor

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
Copy link

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
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
Contributor 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
Collaborator

gpeal commented Sep 28, 2025

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

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