Skip to content

[camera_android_camerax] Fix NV21 buffer over-allocation and optimize per-frame conversion - #12319

Draft
Mairramer wants to merge 5 commits into
flutter:mainfrom
Mairramer:fix/camera-android-camerax-nv21-buffer-over-allocation
Draft

[camera_android_camerax] Fix NV21 buffer over-allocation and optimize per-frame conversion#12319
Mairramer wants to merge 5 commits into
flutter:mainfrom
Mairramer:fix/camera-android-camerax-nv21-buffer-over-allocation

Conversation

@Mairramer

@Mairramer Mairramer commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

planesToNV21 was allocating the NV21 buffer based on yBuffer.remaining() instead of width * height. On some devices, the Y plane is backed by a larger shared direct buffer, which caused the output buffer to be over-allocated and the resulting NV21 size to be incorrect.

This PR fixes that by using width * height as the canonical Y size. It also removes a secondary inefficiency in the padded-row path: the old code allocated a temporary row array per row and copied it via System.arraycopy; the new code reads directly into the output buffer with yBuffer.get(outBuffer, position, width), eliminating both the allocation and the copy.

A new overload planesToNV21(planes, width, height, byte[] outBuffer) is added, which accepts a caller-owned output buffer. ImageProxyUtilsProxyApi.getNv21Buffer now allocates the destination byte[] once and passes it directly to this overload, so the frame data is written in-place with no intermediate buffer and no extra copy.

Fixes #flutter/flutter190299

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

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.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2

@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 optimizes NV21 buffer allocation and conversion in the camera_android_camerax package by passing the ImageProxy directly to getNv21Buffer and limiting plane buffers to prevent over-allocation. Feedback suggests avoiding the stateful nv21Buffer cache in ImageProxyUtilsProxyApi by directly allocating the destination array to eliminate an extra copy and maintain thread safety. Additionally, it is recommended to avoid temporary row array allocations in ImageProxyUtils.planesToNV21 by reading directly into the output buffer.

@Mairramer

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 modifies the camera_android_camerax package to support writing NV21 data directly into a caller-provided buffer, updates ImageProxyProxyApi to limit plane buffers to their valid bytes, and adapts the Pigeon interface and tests. Feedback highlights a potential IllegalArgumentException in ImageProxyUtils due to unconditional buffer position adjustments on the last row of the Y plane, and notes that ImageProxyUtilsProxyApi still allocates a new byte array on every call instead of caching and reusing the buffer to reduce GC pressure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[camera_android_camerax] OOM due to high memory allocations when transmitting NV21 frame buffers over Pigeon

1 participant