From d55c854ebf671a7a50c57a6f8a41d9429b2a5d57 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Sun, 4 Sep 2022 21:16:20 +0600 Subject: [PATCH] Webtoon Split: Improve performance (#7947) --- .../tachiyomi/ui/reader/model/StencilPage.kt | 7 ++--- .../reader/viewer/webtoon/WebtoonAdapter.kt | 1 + .../viewer/webtoon/WebtoonPageHolder.kt | 27 ++++++++++--------- .../kanade/tachiyomi/util/system/ImageUtil.kt | 4 +-- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/StencilPage.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/StencilPage.kt index fb07bfab46..9bd24a344f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/StencilPage.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/model/StencilPage.kt @@ -1,15 +1,16 @@ package eu.kanade.tachiyomi.ui.reader.model -import eu.kanade.tachiyomi.util.system.ImageUtil +import java.io.InputStream class StencilPage( parent: ReaderPage, - val splitData: ImageUtil.SplitData, + stencilStream: () -> InputStream, ) : ReaderPage(parent.index, parent.url, parent.imageUrl) { override var chapter: ReaderChapter = parent.chapter init { - stream = parent.stream + status = READY + stream = stencilStream } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt index 9351008db9..6b663173a3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonAdapter.kt @@ -28,6 +28,7 @@ class WebtoonAdapter(val viewer: WebtoonViewer) : RecyclerView.Adapter) { + if (newStrips.isEmpty()) return if (currentStrip is StencilPage) return val placeAtIndex = items.indexOf(currentStrip) + 1 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt index 0d92d9890b..c415a4aeec 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonPageHolder.kt @@ -19,7 +19,6 @@ import eu.kanade.tachiyomi.ui.reader.viewer.ReaderPageImageView import eu.kanade.tachiyomi.ui.reader.viewer.ReaderProgressIndicator import eu.kanade.tachiyomi.ui.webview.WebViewActivity import eu.kanade.tachiyomi.util.system.ImageUtil -import eu.kanade.tachiyomi.util.system.ImageUtil.SplitData import eu.kanade.tachiyomi.util.system.dpToPx import rx.Observable import rx.Subscription @@ -286,27 +285,31 @@ class WebtoonPageHolder( if (viewer.config.longStripSplit) { if (page is StencilPage) { - val splitData = (page as StencilPage).splitData - return ImageUtil.splitStrip(imageStream, splitData) + return imageStream } - val isStripSplitNeeded = ImageUtil.isStripSplitNeeded(imageStream) if (isStripSplitNeeded) { - val splitData = onStripSplit(imageStream) - splitData?.let { return ImageUtil.splitStrip(imageStream, it) } + onStripSplit(imageStream)?.let { return it } } } return imageStream } - private fun onStripSplit(imageStream: BufferedInputStream): SplitData? { + private fun onStripSplit(imageStream: BufferedInputStream): InputStream? { val page = page ?: return null - val splitData = ImageUtil.getSplitDataForStream(imageStream).toMutableList() - val toReturn = splitData.removeFirstOrNull() - val newPages = splitData.map { StencilPage(page, it) } - viewer.onLongStripSplit(page, newPages) - return toReturn + val streamFn = page.stream ?: return null + val splitData = ImageUtil.getSplitDataForStream(imageStream) + if (splitData.size == 1) return imageStream + val newPages = splitData.map { + StencilPage(page) { ImageUtil.splitStrip(it, streamFn) } + }.toMutableList() + return newPages.removeFirst().stream?.invoke() + .also { + // Doing this first and then returning InputStream + // results in various issues with splitting + viewer.onLongStripSplit(page, newPages) + } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt index 41d491b04f..e4a6b43ebb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ImageUtil.kt @@ -268,8 +268,8 @@ object ImageUtil { /** * Split the imageStream according to the provided splitData */ - fun splitStrip(imageStream: InputStream, splitData: SplitData): InputStream { - val bitmapRegionDecoder = getBitmapRegionDecoder(imageStream) + fun splitStrip(splitData: SplitData, streamFn: () -> InputStream): InputStream { + val bitmapRegionDecoder = getBitmapRegionDecoder(streamFn()) ?: throw Exception("Failed to create new instance of BitmapRegionDecoder") logcat {