Fix GIF animations.

`StreamBitmapDecoder` was handling GIF images and rendering them as static bitmaps. This change fixes that by adding a `StreamBitmapDecoder` wrapper that returns `handles=false` for images of type GIF and APNG, to enable `StreamFactoryGifDecoder` to decode GIF images.

- Resolves signalapp/Signal-Android#14300
This commit is contained in:
jeffrey-signal
2025-08-18 18:12:51 -04:00
committed by Jeffrey Starke
parent ff708eb4ee
commit a2444ffa69
6 changed files with 121 additions and 20 deletions

View File

@@ -16,7 +16,7 @@ import org.signal.glide.common.loader.Loader;
import java.io.IOException;
import java.nio.ByteBuffer;
public class ApngBufferCacheDecoder implements ResourceDecoder<ByteBuffer, APNGDecoder> {
public class ByteBufferApngDecoder implements ResourceDecoder<ByteBuffer, APNGDecoder> {
@Override
public boolean handles(@NonNull ByteBuffer source, @NonNull Options options) {

View File

@@ -16,14 +16,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
public class ApngStreamCacheDecoder implements ResourceDecoder<InputStream, APNGDecoder> {
public class StreamApngDecoder implements ResourceDecoder<InputStream, APNGDecoder> {
/** Set to match {@link com.bumptech.glide.load.data.InputStreamRewinder}'s read limit */
private static final int READ_LIMIT = 5 * 1024 * 1024;
private final ResourceDecoder<ByteBuffer, APNGDecoder> byteBufferDecoder;
public ApngStreamCacheDecoder(ResourceDecoder<ByteBuffer, APNGDecoder> byteBufferDecoder) {
public StreamApngDecoder(ResourceDecoder<ByteBuffer, APNGDecoder> byteBufferDecoder) {
this.byteBufferDecoder = byteBufferDecoder;
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2025 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.glide.cache
import android.content.Context
import android.graphics.Bitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.Registry
import com.bumptech.glide.load.ImageHeaderParser
import com.bumptech.glide.load.ImageHeaderParserUtils
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.ResourceDecoder
import com.bumptech.glide.load.engine.Resource
import org.signal.core.util.logging.Log
import java.io.IOException
import java.io.InputStream
typealias GlideStreamBitmapDecoder = com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder
typealias GlideDownsampler = com.bumptech.glide.load.resource.bitmap.Downsampler
class StreamBitmapDecoder(
context: Context,
glide: Glide,
registry: Registry
) : ResourceDecoder<InputStream, Bitmap> {
private val imageHeaderParsers = registry.imageHeaderParsers
private val arrayPool = glide.arrayPool
private val downsampler = GlideDownsampler(imageHeaderParsers, context.resources.displayMetrics, glide.bitmapPool, arrayPool)
private val delegate = GlideStreamBitmapDecoder(downsampler, arrayPool)
override fun handles(source: InputStream, options: Options): Boolean {
if (!delegate.handles(source, options)) {
return false
}
val imageType = try {
ImageHeaderParserUtils.getType(imageHeaderParsers, source, arrayPool)
} catch (e: IOException) {
Log.w(TAG, "Error checking image type.", e)
ImageHeaderParser.ImageType.UNKNOWN
}
return when (imageType) {
ImageHeaderParser.ImageType.GIF, ImageHeaderParser.ImageType.PNG_A -> false
ImageHeaderParser.ImageType.WEBP_A -> true
ImageHeaderParser.ImageType.ANIMATED_WEBP -> true
else -> true
}
}
override fun decode(source: InputStream, width: Int, height: Int, options: Options): Resource<Bitmap?>? {
return delegate.decode(source, width, height, options)
}
companion object {
private val TAG = Log.tag(StreamBitmapDecoder::class)
}
}

View File

@@ -17,15 +17,15 @@ import org.thoughtcrime.securesms.mms.InputStreamFactory
import java.nio.ByteBuffer
/**
* A variant of [ApngStreamCacheDecoder] that decodes animated PNGs from [org.thoughtcrime.securesms.mms.InputStreamFactory] sources.
* A variant of [StreamApngDecoder] that decodes animated PNGs from [InputStreamFactory] sources.
*/
class ApngStreamFactoryDecoder(
class StreamFactoryApngDecoder(
private val byteBufferDecoder: ResourceDecoder<ByteBuffer, APNGDecoder>
) : ResourceDecoder<InputStreamFactory, APNGDecoder> {
override fun handles(source: InputStreamFactory, options: Options): Boolean {
return if (options.get(ApngOptions.ANIMATE) == true) {
return APNGParser.isAPNG(LimitedReader(StreamReader(source.create()), GlideStreamConfig.markReadLimitBytes))
APNGParser.isAPNG(LimitedReader(StreamReader(source.create()), GlideStreamConfig.markReadLimitBytes))
} else {
false
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2025 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.glide.cache
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.ResourceDecoder
import com.bumptech.glide.load.engine.Resource
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.load.resource.gif.StreamGifDecoder
import org.thoughtcrime.securesms.mms.InputStreamFactory
/**
* A variant of [StreamGifDecoder] that decodes animated PNGs from [InputStreamFactory] sources.
*/
class StreamFactoryGifDecoder(
private val streamGifDecoder: StreamGifDecoder
) : ResourceDecoder<InputStreamFactory, GifDrawable> {
override fun handles(source: InputStreamFactory, options: Options): Boolean = true
override fun decode(
source: InputStreamFactory,
width: Int,
height: Int,
options: Options
): Resource<GifDrawable>? {
return streamGifDecoder.decode(source.create(), width, height, options)
}
}