ImageDecoder: Use AssetFileDescriptor's length

Bug: 166069819
Test: ImageDecoderTest (cts)

The contained asset may only be a subset of the AssetFileDescriptor.
SkWebpCodec reads the entire stream into a contiguous block of memory.
(This is because libwebp does not provide a streaming API for creating
its demuxer.) If the AssetFileDescriptor contains data after the webp
file, this wastes memory. In some cases, there may be a *lot* of data
after the webp file, so this can use too much memory, particularly on
low memory devices.

Change-Id: I8d8e520f43a7ef0d7e4534ef165d8c7e4d2a0b55
This commit is contained in:
Leon Scroggins III
2020-09-10 14:57:11 -04:00
parent 92d0655dea
commit c3fda89b05
2 changed files with 13 additions and 6 deletions

View File

@@ -301,7 +301,7 @@ public final class ImageDecoder implements AutoCloseable {
ImageDecoder decoder = null;
try {
decoder = nCreate(fd, preferAnimation, source);
decoder = nCreate(fd, AssetFileDescriptor.UNKNOWN_LENGTH, preferAnimation, source);
} finally {
if (decoder == null) {
IoUtils.closeQuietly(stream);
@@ -349,7 +349,7 @@ public final class ImageDecoder implements AutoCloseable {
try {
try {
Os.lseek(fd, offset, SEEK_SET);
decoder = nCreate(fd, preferAnimation, source);
decoder = nCreate(fd, assetFd.getDeclaredLength(), preferAnimation, source);
} catch (ErrnoException e) {
decoder = createFromStream(new FileInputStream(fd), true, preferAnimation, source);
}
@@ -2008,7 +2008,7 @@ public final class ImageDecoder implements AutoCloseable {
private static native ImageDecoder nCreate(InputStream is, byte[] storage,
boolean preferAnimation, Source src) throws IOException;
// The fd must be seekable.
private static native ImageDecoder nCreate(FileDescriptor fd,
private static native ImageDecoder nCreate(FileDescriptor fd, long length,
boolean preferAnimation, Source src) throws IOException;
@NonNull
private static native Bitmap nDecodeBitmap(long nativePtr,