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

This commit is contained in:
AntsyLich 2024-11-20 21:20:00 +07:00 committed by Ahmad Ansori Palembani
parent fbe2d8c701
commit 01fcd7d122
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
4 changed files with 50 additions and 56 deletions

View file

@ -50,7 +50,7 @@ class TachiyomiImageDecoder(private val resources: ImageSource, private val opti
if (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
options.bitmapConfig == Bitmap.Config.HARDWARE &&
maxOf(bitmap.width, bitmap.height) <= GLUtil.maxTextureSize
!ImageUtil.isMaxTextureSizeExceeded(bitmap)
) {
val hwBitmap = bitmap.copy(Bitmap.Config.HARDWARE, false)
if (hwBitmap != null) {

View file

@ -18,6 +18,7 @@ import androidx.annotation.CallSuper
import androidx.annotation.StyleRes
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.view.isVisible
import coil3.BitmapImage
import coil3.asDrawable
import coil3.dispose
import coil3.imageLoader
@ -225,20 +226,26 @@ open class ReaderPageImageView @JvmOverloads constructor(
},
)
val useCoilPipeline = if (isWebtoon) {
try { !ImageUtil.isMaxTextureSizeExceeded(data) } catch (_: IllegalStateException) { false }
} else {
false
when (data) {
is BitmapDrawable -> {
setImage(ImageSource.bitmap(data.bitmap))
isVisible = true
}
is BufferedSource -> {
if (!isWebtoon || !ImageUtil.isMaxTextureSizeExceeded(data)) {
setHardwareConfig(!ImageUtil.isMaxTextureSizeExceeded(data))
setImage(ImageSource.inputStream(data.inputStream()))
isVisible = true
return@apply
}
if (useCoilPipeline) {
val request = ImageRequest.Builder(context)
ImageRequest.Builder(context)
.data(data)
.memoryCachePolicy(CachePolicy.DISABLED)
.diskCachePolicy(CachePolicy.DISABLED)
.target(
onSuccess = { result ->
val image = result.asDrawable(context.resources) as BitmapDrawable
val image = result as BitmapImage
setImage(ImageSource.bitmap(image.bitmap))
isVisible = true
},
@ -252,14 +259,11 @@ open class ReaderPageImageView @JvmOverloads constructor(
.customDecoder(true)
.crossfade(false)
.build()
context.imageLoader.enqueue(request)
} else {
when (data) {
is BitmapDrawable -> setImage(ImageSource.bitmap(data.bitmap))
is BufferedSource -> setImage(ImageSource.inputStream(data.inputStream()))
else -> throw IllegalArgumentException("Not implemented for class ${data::class.simpleName}")
.let(context.imageLoader::enqueue)
}
else -> {
throw IllegalArgumentException("Not implemented for class ${data::class.simpleName}")
}
isVisible = true
}
}

View file

@ -776,27 +776,17 @@ object ImageUtil {
return options
}
fun isMaxTextureSizeExceeded(data: Any): Boolean {
val width: Int
val height: Int
when (data) {
is BufferedSource -> {
val opts = extractImageOptions(data)
width = opts.outWidth
height = opts.outHeight
}
is BitmapDrawable -> {
width = data.bitmap.width
height = data.bitmap.height
}
is Bitmap -> {
width = data.width
height = data.height
}
else -> throw IllegalArgumentException("Not implemented for class ${data::class.simpleName}")
}
fun isMaxTextureSizeExceeded(source: BufferedSource): Boolean =
extractImageOptions(source).let { opts -> isMaxTextureSizeExceeded(opts.outWidth, opts.outHeight) }
if (minOf(width, height) <= 0) throw IllegalStateException("Invalid bitmap size")
fun isMaxTextureSizeExceeded(drawable: BitmapDrawable): Boolean =
isMaxTextureSizeExceeded(drawable.bitmap)
fun isMaxTextureSizeExceeded(bitmap: Bitmap): Boolean =
isMaxTextureSizeExceeded(data.width, data.height)
private fun isMaxTextureSizeExceeded(width: Int, height: Int): Boolean {
if (minOf(width, height) <= 0) return false
return maxOf(width, height) > GLUtil.maxTextureSize
}

View file

@ -89,7 +89,7 @@ sqldelight-android-driver = { module = "app.cash.sqldelight:android-driver", ver
sqldelight-android-paging = { module = "app.cash.sqldelight:androidx-paging3-extensions", version.ref = "sqldelight" }
sqldelight-dialects-sql = { module = "app.cash.sqldelight:sqlite-3-38-dialect", version.ref = "sqldelight" }
subsamplingscaleimageview = { module = "com.github.null2264:subsampling-scale-image-view", version = "338caedb5f" }
subsamplingscaleimageview = { module = "com.github.null2264:subsampling-scale-image-view", version = "f7b674ebdd" }
shizuku-api = { module = "dev.rikka.shizuku:api", version.ref = "shizuku" }
shizuku-provider = { module = "dev.rikka.shizuku:provider", version.ref = "shizuku" }
taptargetview = { module = "com.getkeepsafe.taptargetview:taptargetview", version = "1.13.3" }