Skip to content

Commit

Permalink
Add new allowConversionToBitmap flag, fixes #773 (#775)
Browse files Browse the repository at this point in the history
* Add new allowConversionToBitmap flag, fixes #773

This allows animated transformations and normal transformations to
coexist on the same request.  If this flag is set to false:

- A normal bitmap result will have normal transformations applied to it
- An animated result will be left intact which allows
  animatedTransformations to apply.

* Update api, add instructions to contributing.md

* Add binary compatibility copy method

* Add binary compatibility constructor too

* Move decoder to ImageLoader to fix tests on pre-28

* Fix ktlint errors

* Fix test failure on pre-21
  • Loading branch information
edenman authored Jun 11, 2021
1 parent 01c4f82 commit a29ca42
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ If you would like to contribute code you can do so through GitHub by forking the

When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. Please also make sure your code passes all tests by running `./test.sh`.

If you are making an API change, run `./gradlew apiDump` and include any changed files in your pull request.

*Modified from OkHttp's [Contributing](https://square.github.io/okhttp/contributing/) section.*
13 changes: 8 additions & 5 deletions coil-base/api/coil-base.api
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,14 @@ public final class coil/decode/InterruptibleSourceKt {

public final class coil/decode/Options {
public synthetic fun <init> (Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;)V
public fun <init> (Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;)V
public synthetic fun <init> (Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;)V
public synthetic fun <init> (Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final synthetic fun copy (Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;)Lcoil/decode/Options;
public final fun copy (Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;)Lcoil/decode/Options;
public final fun copy (Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;)Lcoil/decode/Options;
public static synthetic fun copy$default (Lcoil/decode/Options;Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;ILjava/lang/Object;)Lcoil/decode/Options;
public static synthetic fun copy$default (Lcoil/decode/Options;Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;ILjava/lang/Object;)Lcoil/decode/Options;
public static synthetic fun copy$default (Lcoil/decode/Options;Landroid/content/Context;Landroid/graphics/Bitmap$Config;Landroid/graphics/ColorSpace;Lcoil/size/Scale;ZZZZLokhttp3/Headers;Lcoil/request/Parameters;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;ILjava/lang/Object;)Lcoil/decode/Options;
public fun equals (Ljava/lang/Object;)Z
public final fun getAllowConversionToBitmap ()Z
public final fun getAllowInexactSize ()Z
public final fun getAllowRgb565 ()Z
public final fun getColorSpace ()Landroid/graphics/ColorSpace;
Expand Down Expand Up @@ -450,8 +451,9 @@ public final class coil/request/ErrorResult : coil/request/ImageResult {
}

public final class coil/request/ImageRequest {
public synthetic fun <init> (Landroid/content/Context;Ljava/lang/Object;Lcoil/target/Target;Lcoil/request/ImageRequest$Listener;Lcoil/memory/MemoryCache$Key;Lcoil/memory/MemoryCache$Key;Landroid/graphics/ColorSpace;Lkotlin/Pair;Lcoil/decode/Decoder;Ljava/util/List;Lokhttp3/Headers;Lcoil/request/Parameters;Landroidx/lifecycle/Lifecycle;Lcoil/size/SizeResolver;Lcoil/size/Scale;Lkotlinx/coroutines/CoroutineDispatcher;Lcoil/transition/Transition;Lcoil/size/Precision;Landroid/graphics/Bitmap$Config;ZZZLcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Ljava/lang/Integer;Landroid/graphics/drawable/Drawable;Ljava/lang/Integer;Landroid/graphics/drawable/Drawable;Ljava/lang/Integer;Landroid/graphics/drawable/Drawable;Lcoil/request/DefinedRequestOptions;Lcoil/request/DefaultRequestOptions;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Landroid/content/Context;Ljava/lang/Object;Lcoil/target/Target;Lcoil/request/ImageRequest$Listener;Lcoil/memory/MemoryCache$Key;Lcoil/memory/MemoryCache$Key;Landroid/graphics/ColorSpace;Lkotlin/Pair;Lcoil/decode/Decoder;Ljava/util/List;Lokhttp3/Headers;Lcoil/request/Parameters;Landroidx/lifecycle/Lifecycle;Lcoil/size/SizeResolver;Lcoil/size/Scale;Lkotlinx/coroutines/CoroutineDispatcher;Lcoil/transition/Transition;Lcoil/size/Precision;Landroid/graphics/Bitmap$Config;ZZZZLcoil/request/CachePolicy;Lcoil/request/CachePolicy;Lcoil/request/CachePolicy;Ljava/lang/Integer;Landroid/graphics/drawable/Drawable;Ljava/lang/Integer;Landroid/graphics/drawable/Drawable;Ljava/lang/Integer;Landroid/graphics/drawable/Drawable;Lcoil/request/DefinedRequestOptions;Lcoil/request/DefaultRequestOptions;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getAllowConversionToBitmap ()Z
public final fun getAllowHardware ()Z
public final fun getAllowRgb565 ()Z
public final fun getBitmapConfig ()Landroid/graphics/Bitmap$Config;
Expand Down Expand Up @@ -495,6 +497,7 @@ public final class coil/request/ImageRequest$Builder {
public fun <init> (Lcoil/request/ImageRequest;Landroid/content/Context;)V
public synthetic fun <init> (Lcoil/request/ImageRequest;Landroid/content/Context;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun addHeader (Ljava/lang/String;Ljava/lang/String;)Lcoil/request/ImageRequest$Builder;
public final fun allowConversionToBitmap (Z)Lcoil/request/ImageRequest$Builder;
public final fun allowHardware (Z)Lcoil/request/ImageRequest$Builder;
public final fun allowRgb565 (Z)Lcoil/request/ImageRequest$Builder;
public final fun bitmapConfig (Landroid/graphics/Bitmap$Config;)Lcoil/request/ImageRequest$Builder;
Expand Down
53 changes: 48 additions & 5 deletions coil-base/src/main/java/coil/decode/Options.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import okhttp3.Headers
* @param premultipliedAlpha True if the color (RGB) channels of the decoded image should be pre-multiplied by the
* alpha channel. The default behavior is to enable pre-multiplication but in some environments it can be necessary
* to disable this feature to leave the source pixels unmodified.
* @param allowConversionToBitmap True if animated result images will be converted to a Bitmap in order to run
* any requested transformations.
* @param headers The header fields to use for any network requests.
* @param parameters A map of custom parameters. These are used to pass custom data to a component.
* @param memoryCachePolicy Determines if this request is allowed to read/write from/to memory.
Expand All @@ -44,6 +46,7 @@ class Options(
val allowInexactSize: Boolean = false,
val allowRgb565: Boolean = false,
val premultipliedAlpha: Boolean = true,
val allowConversionToBitmap: Boolean = true,
val headers: Headers = EMPTY_HEADERS,
val parameters: Parameters = Parameters.EMPTY,
val memoryCachePolicy: CachePolicy = CachePolicy.ENABLED,
Expand All @@ -59,13 +62,14 @@ class Options(
allowInexactSize: Boolean = this.allowInexactSize,
allowRgb565: Boolean = this.allowRgb565,
premultipliedAlpha: Boolean = this.premultipliedAlpha,
allowConversionToBitmap: Boolean = this.allowConversionToBitmap,
headers: Headers = this.headers,
parameters: Parameters = this.parameters,
memoryCachePolicy: CachePolicy = this.memoryCachePolicy,
diskCachePolicy: CachePolicy = this.diskCachePolicy,
networkCachePolicy: CachePolicy = this.networkCachePolicy
) = Options(context, config, colorSpace, scale, allowInexactSize, allowRgb565, premultipliedAlpha,
headers, parameters, memoryCachePolicy, diskCachePolicy, networkCachePolicy)
allowConversionToBitmap, headers, parameters, memoryCachePolicy, diskCachePolicy, networkCachePolicy)

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -77,6 +81,7 @@ class Options(
allowInexactSize == other.allowInexactSize &&
allowRgb565 == other.allowRgb565 &&
premultipliedAlpha == other.premultipliedAlpha &&
allowConversionToBitmap == other.allowConversionToBitmap &&
headers == other.headers &&
parameters == other.parameters &&
memoryCachePolicy == other.memoryCachePolicy &&
Expand All @@ -92,6 +97,7 @@ class Options(
result = 31 * result + allowInexactSize.hashCode()
result = 31 * result + allowRgb565.hashCode()
result = 31 * result + premultipliedAlpha.hashCode()
result = 31 * result + allowConversionToBitmap.hashCode()
result = 31 * result + headers.hashCode()
result = 31 * result + parameters.hashCode()
result = 31 * result + memoryCachePolicy.hashCode()
Expand All @@ -103,8 +109,9 @@ class Options(
override fun toString(): String {
return "Options(context=$context, config=$config, colorSpace=$colorSpace, scale=$scale, " +
"allowInexactSize=$allowInexactSize, allowRgb565=$allowRgb565, premultipliedAlpha=$premultipliedAlpha, " +
"headers=$headers, parameters=$parameters, memoryCachePolicy=$memoryCachePolicy, " +
"diskCachePolicy=$diskCachePolicy, networkCachePolicy=$networkCachePolicy)"
"allowConversionToBitmap=$allowConversionToBitmap, headers=$headers, parameters=$parameters, " +
"memoryCachePolicy=$memoryCachePolicy, diskCachePolicy=$diskCachePolicy, " +
"networkCachePolicy=$networkCachePolicy)"
}

@Deprecated(message = "Kept for binary compatibility.", level = DeprecationLevel.HIDDEN)
Expand All @@ -124,6 +131,25 @@ class Options(
allowRgb565 = allowRgb565, headers = headers, parameters = parameters, memoryCachePolicy = memoryCachePolicy,
diskCachePolicy = diskCachePolicy, networkCachePolicy = networkCachePolicy)

@Deprecated(message = "Kept for binary compatibility.", level = DeprecationLevel.HIDDEN)
constructor(
context: Context,
config: Bitmap.Config,
colorSpace: ColorSpace?,
scale: Scale,
allowInexactSize: Boolean,
allowRgb565: Boolean,
premultipliedAlpha: Boolean,
headers: Headers,
parameters: Parameters,
memoryCachePolicy: CachePolicy,
diskCachePolicy: CachePolicy,
networkCachePolicy: CachePolicy
) : this(context = context, config = config, colorSpace = colorSpace, scale = scale,
allowInexactSize = allowInexactSize, allowRgb565 = allowRgb565, premultipliedAlpha = premultipliedAlpha,
headers = headers, parameters = parameters, memoryCachePolicy = memoryCachePolicy,
diskCachePolicy = diskCachePolicy, networkCachePolicy = networkCachePolicy)

@Deprecated(message = "Kept for binary compatibility.", level = DeprecationLevel.HIDDEN)
fun copy(
context: Context = this.context,
Expand All @@ -137,6 +163,23 @@ class Options(
memoryCachePolicy: CachePolicy = this.memoryCachePolicy,
diskCachePolicy: CachePolicy = this.diskCachePolicy,
networkCachePolicy: CachePolicy = this.networkCachePolicy
) = copy(context, config, colorSpace, scale, allowInexactSize, allowRgb565, premultipliedAlpha, headers, parameters,
memoryCachePolicy, diskCachePolicy, networkCachePolicy)
) = copy(context, config, colorSpace, scale, allowInexactSize, allowRgb565, premultipliedAlpha,
allowConversionToBitmap, headers, parameters, memoryCachePolicy, diskCachePolicy, networkCachePolicy)

@Deprecated(message = "Kept for binary compatibility.", level = DeprecationLevel.HIDDEN)
fun copy(
context: Context = this.context,
config: Bitmap.Config = this.config,
colorSpace: ColorSpace? = this.colorSpace,
scale: Scale = this.scale,
allowInexactSize: Boolean = this.allowInexactSize,
allowRgb565: Boolean = this.allowRgb565,
premultipliedAlpha: Boolean = this.premultipliedAlpha,
headers: Headers = this.headers,
parameters: Parameters = this.parameters,
memoryCachePolicy: CachePolicy = this.memoryCachePolicy,
diskCachePolicy: CachePolicy = this.diskCachePolicy,
networkCachePolicy: CachePolicy = this.networkCachePolicy
) = copy(context, config, colorSpace, scale, allowInexactSize, allowRgb565, premultipliedAlpha,
allowConversionToBitmap, headers, parameters, memoryCachePolicy, diskCachePolicy, networkCachePolicy)
}
13 changes: 10 additions & 3 deletions coil-base/src/main/java/coil/intercept/EngineInterceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,17 @@ internal class EngineInterceptor(
drawableDecoder.convert(result.drawable, options.config, size, options.scale, options.allowInexactSize)
}
} else {
logger?.log(TAG, Log.INFO) {
"Converting drawable of type ${result.drawable::class.java.canonicalName} to apply transformations: $transformations"
if (options.allowConversionToBitmap) {
logger?.log(TAG, Log.INFO) {
"Converting drawable of type ${result.drawable::class.java.canonicalName} to apply transformations: $transformations"
}
drawableDecoder.convert(result.drawable, options.config, size, options.scale, options.allowInexactSize)
} else {
logger?.log(TAG, Log.INFO) {
"AllowConversionToBitmap=false, skipping transformations for type ${result.drawable::class.java.canonicalName}"
}
return result
}
drawableDecoder.convert(result.drawable, options.config, size, options.scale, options.allowInexactSize)
}
eventListener.transformStart(request, input)
val output = transformations.foldIndices(input) { bitmap, transformation ->
Expand Down
1 change: 1 addition & 0 deletions coil-base/src/main/java/coil/memory/RequestService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ internal class RequestService(private val logger: Logger?) {
allowInexactSize = request.allowInexactSize,
allowRgb565 = allowRgb565,
premultipliedAlpha = request.premultipliedAlpha,
allowConversionToBitmap = request.allowConversionToBitmap,
headers = request.headers,
parameters = request.parameters,
memoryCachePolicy = request.memoryCachePolicy,
Expand Down
27 changes: 22 additions & 5 deletions coil-base/src/main/java/coil/request/ImageRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class ImageRequest private constructor(
/** @see Builder.premultipliedAlpha */
val premultipliedAlpha: Boolean,

/** @see Builder.allowConversionToBitmap */
val allowConversionToBitmap: Boolean,

/** @see Builder.memoryCachePolicy */
val memoryCachePolicy: CachePolicy,

Expand Down Expand Up @@ -181,6 +184,7 @@ class ImageRequest private constructor(
allowHardware == other.allowHardware &&
allowRgb565 == other.allowRgb565 &&
premultipliedAlpha == other.premultipliedAlpha &&
allowConversionToBitmap == other.allowConversionToBitmap &&
memoryCachePolicy == other.memoryCachePolicy &&
diskCachePolicy == other.diskCachePolicy &&
networkCachePolicy == other.networkCachePolicy &&
Expand Down Expand Up @@ -217,6 +221,7 @@ class ImageRequest private constructor(
result = 31 * result + allowHardware.hashCode()
result = 31 * result + allowRgb565.hashCode()
result = 31 * result + premultipliedAlpha.hashCode()
result = 31 * result + allowConversionToBitmap.hashCode()
result = 31 * result + memoryCachePolicy.hashCode()
result = 31 * result + diskCachePolicy.hashCode()
result = 31 * result + networkCachePolicy.hashCode()
Expand All @@ -238,11 +243,11 @@ class ImageRequest private constructor(
"headers=$headers, parameters=$parameters, lifecycle=$lifecycle, sizeResolver=$sizeResolver, " +
"scale=$scale, dispatcher=$dispatcher, transition=$transition, precision=$precision, " +
"bitmapConfig=$bitmapConfig, allowHardware=$allowHardware, allowRgb565=$allowRgb565, " +
"premultipliedAlpha=$premultipliedAlpha, memoryCachePolicy=$memoryCachePolicy, " +
"diskCachePolicy=$diskCachePolicy, networkCachePolicy=$networkCachePolicy, " +
"placeholderResId=$placeholderResId, placeholderDrawable=$placeholderDrawable, errorResId=$errorResId, " +
"errorDrawable=$errorDrawable, fallbackResId=$fallbackResId, fallbackDrawable=$fallbackDrawable, " +
"defined=$defined, defaults=$defaults)"
"premultipliedAlpha=$premultipliedAlpha, allowConversionToBitmap=$allowConversionToBitmap, " +
"memoryCachePolicy=$memoryCachePolicy, diskCachePolicy=$diskCachePolicy, " +
"networkCachePolicy=$networkCachePolicy, placeholderResId=$placeholderResId, " +
"placeholderDrawable=$placeholderDrawable, errorResId=$errorResId, errorDrawable=$errorDrawable, " +
"fallbackResId=$fallbackResId, fallbackDrawable=$fallbackDrawable, defined=$defined, defaults=$defaults)"
}

/**
Expand Down Expand Up @@ -304,6 +309,7 @@ class ImageRequest private constructor(
private var allowHardware: Boolean?
private var allowRgb565: Boolean?
private var premultipliedAlpha: Boolean
private var allowConversionToBitmap: Boolean
private var memoryCachePolicy: CachePolicy?
private var diskCachePolicy: CachePolicy?
private var networkCachePolicy: CachePolicy?
Expand Down Expand Up @@ -343,6 +349,7 @@ class ImageRequest private constructor(
allowHardware = null
allowRgb565 = null
premultipliedAlpha = true
allowConversionToBitmap = true
memoryCachePolicy = null
diskCachePolicy = null
networkCachePolicy = null
Expand Down Expand Up @@ -382,6 +389,7 @@ class ImageRequest private constructor(
allowHardware = request.defined.allowHardware
allowRgb565 = request.defined.allowRgb565
premultipliedAlpha = request.premultipliedAlpha
allowConversionToBitmap = request.allowConversionToBitmap
memoryCachePolicy = request.defined.memoryCachePolicy
diskCachePolicy = request.defined.diskCachePolicy
networkCachePolicy = request.defined.networkCachePolicy
Expand Down Expand Up @@ -591,6 +599,14 @@ class ImageRequest private constructor(
this.premultipliedAlpha = enable
}

/**
* Enable/disable conversion to Bitmap in order to run any requested transformations. Use this to disable
* static transformations in order to have animatedTransformations run on the unmodified result.
*/
fun allowConversionToBitmap(allow: Boolean) = apply {
this.allowConversionToBitmap = allow
}

/**
* Enable/disable reading/writing from/to the memory cache.
*/
Expand Down Expand Up @@ -834,6 +850,7 @@ class ImageRequest private constructor(
allowHardware = allowHardware ?: defaults.allowHardware,
allowRgb565 = allowRgb565 ?: defaults.allowRgb565,
premultipliedAlpha = premultipliedAlpha,
allowConversionToBitmap = allowConversionToBitmap,
memoryCachePolicy = memoryCachePolicy ?: defaults.memoryCachePolicy,
diskCachePolicy = diskCachePolicy ?: defaults.diskCachePolicy,
networkCachePolicy = networkCachePolicy ?: defaults.networkCachePolicy,
Expand Down
Loading

0 comments on commit a29ca42

Please sign in to comment.