BitmapSequence IPC decodes every frame from the collated transport
buffer into a new malloc-backed Bitmap. That keeps animated sequences
compact, but it also breaks the shared-memory chain for the common
single-frame image path. WebContent receives anonymous buffer data from
ImageDecoder, then has to allocate another anonymous buffer when the
image is sent to the Compositor.
Preserve the backing for single-frame sequences by wrapping the received
AnonymousBuffer directly in the decoded Bitmap after validating that it
exactly matches the frame metadata. That lets the decoded image keep its
shared-memory backing all the way through ImageDecoder -> WebContent ->
Compositor without another allocation and copy.
The total_buffer_size calculation could theoretically overflow if we
tried to encode a sequence of bitmaps with combined size > SIZE_MAX.
Use Checked<size_t> and VERIFY to catch this (unlikely) encoding bug.
When decoding a BitmapSequence received over IPC, we were creating an
AnonymousBuffer for each bitmap and then making a Gfx::Bitmap wrapper
around it.
This was unnecessarily using up one file descriptor per bitmap, and also
wasting a lot of memory for small bitmaps since we always allocated at
least one VM page.
This patch changes the BitmapSequence decoder to use malloc memory
instead, saving file descriptors and using less memory overall.
This would fail with EINVAL earlier, due to an attempt to create a
zero-length Core::AnonymousBuffer.
We fix this by transferring the buffer length separately, and only
going down the AnonymousBuffer allocation path if the length is
non-zero.