Switch to hardware bitmap in reader only if device can handle it

Closes #mihonapp/mihon1460

(cherry picked from commit e6d96bd348ea5d18a005d6465222ad5f5123103e)
This commit is contained in:
AntsyLich 2024-11-17 03:04:38 +06:00 committed by Cuong-Tran
parent 77863480c9
commit 9d3b99ec1a
No known key found for this signature in database
GPG key ID: 733AA7624B9315C2
4 changed files with 39 additions and 32 deletions

View file

@ -12,7 +12,6 @@ import coil3.fetch.SourceFetchResult
import coil3.request.Options import coil3.request.Options
import coil3.request.bitmapConfig import coil3.request.bitmapConfig
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.util.system.GLUtil
import mihon.core.archive.CbzCrypto import mihon.core.archive.CbzCrypto
import mihon.core.archive.CbzCrypto.getCoverStream import mihon.core.archive.CbzCrypto.getCoverStream
import mihon.core.archive.archiveReader import mihon.core.archive.archiveReader
@ -67,10 +66,7 @@ class TachiyomiImageDecoder(private val resources: ImageSource, private val opti
check(bitmap != null) { "Failed to decode image" } check(bitmap != null) { "Failed to decode image" }
if ( if (options.bitmapConfig == Bitmap.Config.HARDWARE && ImageUtil.canUseHardwareBitmap(bitmap)) {
options.bitmapConfig == Bitmap.Config.HARDWARE &&
maxOf(bitmap.width, bitmap.height) <= GLUtil.maxTextureSize
) {
val hwBitmap = bitmap.copy(Bitmap.Config.HARDWARE, false) val hwBitmap = bitmap.copy(Bitmap.Config.HARDWARE, false)
if (hwBitmap != null) { if (hwBitmap != null) {
bitmap.recycle() bitmap.recycle()

View file

@ -295,32 +295,34 @@ open class ReaderPageImageView @JvmOverloads constructor(
isVisible = true isVisible = true
} }
is BufferedSource -> { is BufferedSource -> {
if (!isWebtoon || !ImageUtil.canUseCoilToDecode(data)) { if (!isWebtoon) {
setHardwareConfig(ImageUtil.canUseHardwareBitmap(data))
setImage(ImageSource.inputStream(data.inputStream())) setImage(ImageSource.inputStream(data.inputStream()))
isVisible = true isVisible = true
} else { return@apply
val request = ImageRequest.Builder(context)
.data(data)
.memoryCachePolicy(CachePolicy.DISABLED)
.diskCachePolicy(CachePolicy.DISABLED)
.target(
onSuccess = { result ->
val image = result as BitmapImage
setImage(ImageSource.bitmap(image.bitmap))
isVisible = true
},
onError = {
this@ReaderPageImageView.onImageLoadError()
},
)
.size(ViewSizeResolver(this@ReaderPageImageView))
.precision(Precision.INEXACT)
.cropBorders(config.cropBorders)
.customDecoder(true)
.crossfade(false)
.build()
context.imageLoader.enqueue(request)
} }
ImageRequest.Builder(context)
.data(data)
.memoryCachePolicy(CachePolicy.DISABLED)
.diskCachePolicy(CachePolicy.DISABLED)
.target(
onSuccess = { result ->
val image = result as BitmapImage
setImage(ImageSource.bitmap(image.bitmap))
isVisible = true
},
onError = {
onImageLoadError()
},
)
.size(ViewSizeResolver(this@ReaderPageImageView))
.precision(Precision.INEXACT)
.cropBorders(config.cropBorders)
.customDecoder(true)
.crossfade(false)
.build()
.let(context.imageLoader::enqueue)
} }
else -> { else -> {
throw IllegalArgumentException("Not implemented for class ${data::class.simpleName}") throw IllegalArgumentException("Not implemented for class ${data::class.simpleName}")

View file

@ -360,9 +360,18 @@ object ImageUtil {
val bottomOffset = topOffset + splitHeight val bottomOffset = topOffset + splitHeight
} }
fun canUseCoilToDecode(imageSource: BufferedSource): Boolean { fun canUseHardwareBitmap(bitmap: Bitmap): Boolean {
val options = extractImageOptions(imageSource) return canUseHardwareBitmap(bitmap.width, bitmap.height)
return maxOf(options.outWidth, options.outHeight) <= GLUtil.maxTextureSize }
fun canUseHardwareBitmap(imageSource: BufferedSource): Boolean {
return with(extractImageOptions(imageSource)) {
canUseHardwareBitmap(outWidth, outHeight)
}
}
private fun canUseHardwareBitmap(width: Int, height: Int): Boolean {
return maxOf(width, height) <= GLUtil.maxTextureSize
} }
/** /**

View file

@ -48,7 +48,7 @@ coil-gif = { module = "io.coil-kt.coil3:coil-gif" }
coil-compose = { module = "io.coil-kt.coil3:coil-compose" } coil-compose = { module = "io.coil-kt.coil3:coil-compose" }
coil-network-okhttp = { module = "io.coil-kt.coil3:coil-network-okhttp" } coil-network-okhttp = { module = "io.coil-kt.coil3:coil-network-okhttp" }
subsamplingscaleimageview = "com.github.tachiyomiorg:subsampling-scale-image-view:b8e1b0ed2b" subsamplingscaleimageview = "com.github.tachiyomiorg:subsampling-scale-image-view:66e0db195d"
image-decoder = "com.github.tachiyomiorg:image-decoder:41c059e540" image-decoder = "com.github.tachiyomiorg:image-decoder:41c059e540"
natural-comparator = "com.github.gpanther:java-nat-sort:natural-comparator-1.1" natural-comparator = "com.github.gpanther:java-nat-sort:natural-comparator-1.1"