-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BlackholeDecoder to simplify disk-only preloading. (#2599)
* Add BlackholeDecoder to simplify disk-only preloading. * Fix test. * Update API.
- Loading branch information
1 parent
37ef235
commit 8032fd9
Showing
6 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
coil-core/src/commonMain/kotlin/coil3/decode/BlackholeDecoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package coil3.decode | ||
|
||
import coil3.Canvas | ||
import coil3.Image | ||
import coil3.ImageLoader | ||
import coil3.annotation.ExperimentalCoilApi | ||
import coil3.fetch.SourceFetchResult | ||
import coil3.request.Options | ||
import kotlin.jvm.JvmField | ||
|
||
/** | ||
* A [Decoder] that ignores the [SourceFetchResult] and always returns the [Image] returned by | ||
* [imageFactory]. | ||
* | ||
* This is useful for skipping the decoding step, for instance when you only want to preload to disk | ||
* and do not want to decode the image into memory. | ||
*/ | ||
@ExperimentalCoilApi | ||
class BlackholeDecoder( | ||
private val imageFactory: () -> Image, | ||
) : Decoder { | ||
|
||
override suspend fun decode(): DecodeResult { | ||
return DecodeResult( | ||
image = imageFactory(), | ||
isSampled = false, | ||
) | ||
} | ||
|
||
class Factory( | ||
private val imageFactory: () -> Image = { EMPTY_IMAGE }, | ||
) : Decoder.Factory { | ||
|
||
override fun create( | ||
result: SourceFetchResult, | ||
options: Options, | ||
imageLoader: ImageLoader, | ||
) = BlackholeDecoder(imageFactory) | ||
|
||
companion object { | ||
@JvmField val EMPTY_IMAGE = object : Image { | ||
override val size get() = 0L | ||
override val width get() = -1 | ||
override val height get() = -1 | ||
override val shareable get() = true | ||
override fun draw(canvas: Canvas) { /* Draw nothing. */ } | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
coil-core/src/commonTest/kotlin/coil3/decode/BlackholeDecoderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package coil3.decode | ||
|
||
import coil3.ImageLoader | ||
import coil3.fetch.SourceFetchResult | ||
import coil3.request.Options | ||
import coil3.test.utils.RobolectricTest | ||
import coil3.test.utils.context | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlinx.coroutines.test.runTest | ||
import okio.Buffer | ||
import okio.fakefilesystem.FakeFileSystem | ||
|
||
class BlackholeDecoderTest : RobolectricTest() { | ||
|
||
@Test | ||
fun basic() = runTest { | ||
val decoderFactory = BlackholeDecoder.Factory() | ||
val bufferSize = 1024 | ||
val buffer = Buffer().apply { write(ByteArray(bufferSize)) } | ||
val fetchResult = SourceFetchResult( | ||
source = ImageSource( | ||
source = buffer, | ||
fileSystem = FakeFileSystem(), | ||
), | ||
mimeType = null, | ||
dataSource = DataSource.MEMORY, | ||
) | ||
val decoder = decoderFactory.create(fetchResult, Options(context), ImageLoader(context)) | ||
|
||
assertEquals(BlackholeDecoder.Factory.EMPTY_IMAGE, decoder.decode().image) | ||
assertEquals(bufferSize.toLong(), buffer.size) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters