[camera_android_camerax] Fix NV21 buffer over-allocation and optimize per-frame conversion - #12319
Conversation
|
/gemini review |
There was a problem hiding this comment.
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.
|
/gemini review |
There was a problem hiding this comment.
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.
planesToNV21was allocating the NV21 buffer based onyBuffer.remaining() instead ofwidth * 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 * heightas 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 viaSystem.arraycopy; the new code reads directly into the output buffer withyBuffer.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.getNv21Buffernow allocates the destinationbyte[]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
[shared_preferences]///).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-assistbot 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
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