From 5c0d65dd608f3e3c2281a0f31cb581d3f595c65b Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Tue, 25 Jun 2024 11:20:29 +0800 Subject: [PATCH] Fix crop borders not working --- .../coil/HardwareBitmapInterceptor.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/coil/HardwareBitmapInterceptor.kt b/app/src/main/kotlin/com/hippo/ehviewer/coil/HardwareBitmapInterceptor.kt index b0050e4b4..e6ae44cd1 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/coil/HardwareBitmapInterceptor.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/coil/HardwareBitmapInterceptor.kt @@ -3,6 +3,7 @@ package com.hippo.ehviewer.coil import android.graphics.Bitmap import android.os.Build import androidx.annotation.RequiresApi +import androidx.compose.ui.unit.IntRect import coil3.BitmapImage import coil3.Extras import coil3.asCoilImage @@ -29,19 +30,25 @@ object HardwareBitmapInterceptor : Interceptor { val result = chain.proceed() val request = result.request if (!request.allowHardware && result is SuccessResult) { - val image = when (val image = result.image) { - is BitmapImageWithRect -> image.image + var rect: IntRect? = null + val image = result.image + val bitmap = when (image) { + is BitmapImageWithRect -> image.image.also { rect = image.rect } is BitmapImage -> image else -> return result - } - val bitmap = image.bitmap + }.bitmap // Large hardware bitmaps have rendering issues (e.g. crash, empty) on some devices. // This is not ideal but I haven't figured out how to probe the threshold. // All we know is that it's less than the maximum texture size. if (maxOf(bitmap.width, bitmap.height) <= request.hardwareThreshold) { - bitmap.copy(Bitmap.Config.HARDWARE, false)?.let { + bitmap.copy(Bitmap.Config.HARDWARE, false)?.let { hwBitmap -> bitmap.recycle() - return result.copy(image = it.asCoilImage(), request = request, dataSource = result.dataSource) + val newImage = hwBitmap.asCoilImage() + return result.copy( + image = rect?.let { BitmapImageWithRect(it, newImage as BitmapImage) } ?: newImage, + request = request, + dataSource = result.dataSource, + ) } } }