mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 02:24:05 +09:00
btex: multithreaded finalising
This commit is contained in:
@@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion
|
|||||||
import com.badlogic.gdx.utils.Disposable
|
import com.badlogic.gdx.utils.Disposable
|
||||||
import net.torvald.terrarum.FlippingSpriteBatch
|
import net.torvald.terrarum.FlippingSpriteBatch
|
||||||
import net.torvald.terrarum.ceilToInt
|
import net.torvald.terrarum.ceilToInt
|
||||||
|
import net.torvald.terrarum.concurrent.ThreadExecutor
|
||||||
import net.torvald.terrarum.imagefont.TinyAlphNum
|
import net.torvald.terrarum.imagefont.TinyAlphNum
|
||||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.archivers.ClusteredFormatDOM
|
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.archivers.ClusteredFormatDOM
|
||||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.archivers.Clustfile
|
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.archivers.Clustfile
|
||||||
@@ -21,6 +22,7 @@ import net.torvald.terrarumsansbitmap.gdx.CodepointSequence
|
|||||||
import net.torvald.terrarumsansbitmap.gdx.TerrarumSansBitmap
|
import net.torvald.terrarumsansbitmap.gdx.TerrarumSansBitmap
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.RandomAccessFile
|
import java.io.RandomAccessFile
|
||||||
|
import java.util.concurrent.Callable
|
||||||
import java.util.zip.Deflater
|
import java.util.zip.Deflater
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -161,14 +163,35 @@ class BTeXDocument : Disposable {
|
|||||||
pageTextures = Array(pages.size) { null }
|
pageTextures = Array(pages.size) { null }
|
||||||
pagePixmaps = Array(pages.size) { null }
|
pagePixmaps = Array(pages.size) { null }
|
||||||
|
|
||||||
pages.forEachIndexed { pageNum, page ->
|
if (!multithread) {
|
||||||
val pixmap = Pixmap(pageDimensionWidth, pageDimensionHeight, Pixmap.Format.RGBA8888).also {
|
pages.forEachIndexed { pageNum, page ->
|
||||||
it.blending = Pixmap.Blending.SourceOver
|
val pixmap = Pixmap(pageDimensionWidth, pageDimensionHeight, Pixmap.Format.RGBA8888).also {
|
||||||
it.filter = Pixmap.Filter.NearestNeighbour
|
it.blending = Pixmap.Blending.SourceOver
|
||||||
|
it.filter = Pixmap.Filter.NearestNeighbour
|
||||||
|
}
|
||||||
|
page.renderToPixmap(pixmap, 0, 0, pageMarginH, pageMarginV)
|
||||||
|
printPageNumber(pixmap, pageNum, 0, 0)
|
||||||
|
pagePixmaps[pageNum] = pixmap
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
val jobs = pages.indices.map { pageNum -> Callable {
|
||||||
|
val page = pages[pageNum]
|
||||||
|
val pixmap = Pixmap(pageDimensionWidth, pageDimensionHeight, Pixmap.Format.RGBA8888).also {
|
||||||
|
it.blending = Pixmap.Blending.SourceOver
|
||||||
|
it.filter = Pixmap.Filter.NearestNeighbour
|
||||||
|
}
|
||||||
|
page.renderToPixmap(pixmap, 0, 0, pageMarginH, pageMarginV)
|
||||||
|
printPageNumber(pixmap, pageNum, 0, 0)
|
||||||
|
pagePixmaps[pageNum] = pixmap
|
||||||
|
} }
|
||||||
|
|
||||||
|
// my experiment tells 4, 8, 16, 32 threads all perform the same
|
||||||
|
ThreadExecutor(Runtime.getRuntime().availableProcessors().div(2).coerceIn(1..4)).also {
|
||||||
|
it.renew()
|
||||||
|
it.submitAll(jobs)
|
||||||
|
it.join()
|
||||||
}
|
}
|
||||||
page.renderToPixmap(pixmap, 0, 0, pageMarginH, pageMarginV)
|
|
||||||
printPageNumber(pixmap, pageNum, 0, 0)
|
|
||||||
pagePixmaps[pageNum] = pixmap
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isFinalised = true
|
isFinalised = true
|
||||||
|
|||||||
@@ -26,9 +26,9 @@ import kotlin.system.measureTimeMillis
|
|||||||
*/
|
*/
|
||||||
class BTeXTest : ApplicationAdapter() {
|
class BTeXTest : ApplicationAdapter() {
|
||||||
|
|
||||||
val filePath = "btex_ko.xml"
|
// val filePath = "btex_ko.xml"
|
||||||
// val filePath = "test.xml"
|
// val filePath = "test.xml"
|
||||||
// val filePath = "literature/en/daniel_defoe_robinson_crusoe.btxbook"
|
val filePath = "literature/en/daniel_defoe_robinson_crusoe.xml"
|
||||||
// val filePath = "literature/ruRU/anton_chekhov_palata_no_6.xml"
|
// val filePath = "literature/ruRU/anton_chekhov_palata_no_6.xml"
|
||||||
// val filePath = "literature/koKR/yisang_nalgae.btxbook"
|
// val filePath = "literature/koKR/yisang_nalgae.btxbook"
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ class BTeXTest : ApplicationAdapter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
measureTimeMillis {
|
measureTimeMillis {
|
||||||
document.finalise()
|
document.finalise(true)
|
||||||
}.also {
|
}.also {
|
||||||
println("Time spent on finalising [ms]: $it")
|
println("Time spent on finalising [ms]: $it")
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ class BTeXTest : ApplicationAdapter() {
|
|||||||
gdxClearAndEnableBlend(.063f, .070f, .086f, 1f)
|
gdxClearAndEnableBlend(.063f, .070f, .086f, 1f)
|
||||||
|
|
||||||
if (::document.isInitialized) {
|
if (::document.isInitialized) {
|
||||||
if (document.isFinalised) {
|
if (document.isFinalised || document.fromArchive) {
|
||||||
val drawX = (1280 - (pageGap + document.pageDimensionWidth * 2)) / 2
|
val drawX = (1280 - (pageGap + document.pageDimensionWidth * 2)) / 2
|
||||||
val drawY = 24
|
val drawY = 24
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user