SkIcoCodec: Read the entire stream into data

Bug: 932080
Bug: b/142252770

An ICO file has a directory of images that are stored later in the file.
The directory contains the offset and size of the images. SkIcoCodec
uses these to create embedded SkPng/SkBmpCodecs. The old implementation
allocated a block of memory for each image and copied the stream into
those blocks so that the embedded SkCodecs could independently read
their encoded data.

Although SkIcoCodec checks for null, this still allows large (albeit
temporary - since we'll discard them if the stream does not contain
enough data to fill them) allocations and the potential for over-
commit.

Instead, read the entire stream into a contiguous buffer. If the stream
is already actually a buffer, just use that directly. In this case, the
new code will do less work. Otherwise, the memory we allocate is
limited by the size of the stream.

Note that this is a behavior change for a stream that contains two
consecutive ICOs, where the client expects to be able to read the second
one later. This was an issue for PNGs on Android (b/34073812), but I
suspect no one is relying on this behavior for ICO. Update Codec_end
test to remove the ICO test.

Alternatives to consider:
- only buffer the individual encoded images. This will allow us to
continue passing the Codec_end test.
- lazily read the embedded streams. Currently we read their start to
verify they are valid images (at least in the header) and read their
actual sizes and bit-depths, which could differ from that listed in
the directory. We use those to make a guess at the "best" image to use.
An image with mismatched sizes may now decode differently.

Change-Id: I30e5f6c8c2e5a0fa135348f61efe151a7f5d4756
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/277058
Auto-Submit: Leon Scroggins <scroggo@google.com>
Commit-Queue: Derek Sollenberger <djsollen@google.com>
Reviewed-by: Derek Sollenberger <djsollen@google.com>
4 files changed