diff --git a/src/androidTest/java/com/mapconductor/core/shapetile/ShapeTileGpuDeviceTest.kt b/src/androidTest/java/com/mapconductor/core/shapetile/ShapeTileGpuDeviceTest.kt new file mode 100644 index 0000000..23ac6d7 --- /dev/null +++ b/src/androidTest/java/com/mapconductor/core/shapetile/ShapeTileGpuDeviceTest.kt @@ -0,0 +1,260 @@ +package com.mapconductor.core.shapetile + +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.unit.dp +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.mapconductor.core.circle.CircleState +import com.mapconductor.core.circle.CircleTileFileStorage +import com.mapconductor.core.circle.CircleTilePainter +import com.mapconductor.core.circle.TileCircle +import com.mapconductor.core.circle.effectiveZIndex +import com.mapconductor.core.features.GeoPoint +import com.mapconductor.core.features.GeoPointInterface +import com.mapconductor.core.polygon.PolygonState +import com.mapconductor.core.polygon.PolygonTilePainter +import com.mapconductor.core.polygon.TilePolygon +import com.mapconductor.core.polyline.PolylineState +import com.mapconductor.core.polyline.PolylineTilePainter +import com.mapconductor.core.polyline.TilePolyline +import org.junit.After +import org.junit.Before +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.junit.runner.RunWith +import kotlin.math.abs +import kotlin.math.max +import android.graphics.Bitmap + +/** + * 図形タイルを GPU([GlShapeRasterizer])と Canvas で描き、同じ絵になることを確かめる。 + * + * GPU は MSAA、Canvas は Skia のアンチエイリアスなので、縁の画素は 1 段ずれる。見るのは、縁を + * 除いた所が一致すること。とくに食い違いやすい所: nonzero の塗り(穴)、半透明の線の継ぎ目と + * 自己交差(2 度塗りしない)、丸い角と端、1 点に縮む図形、渡した順の重なり、128 角形で描く円。 + * ios-sdk-core の `MetalShapeRasterizerTests` と `MetalPathRasterizerTests` に対応する。 + */ +@RunWith(AndroidJUnit4::class) +class ShapeTileGpuDeviceTest { + @get:Rule + val temporary = TemporaryFolder() + + @Before + fun drawEveryKindOnTheGpu() { + // Polygons and polylines are drawn on the canvas by default; turn them on + // here so that their GPU path is compared too. + ShapeTileRenderer.gpuPolygonsAndLines = true + } + + @After + fun restore() { + ShapeTileRenderer.useGpu = true + ShapeTileRenderer.gpuPolygonsAndLines = false + } + + private fun p( + latitude: Double, + longitude: Double, + ): GeoPointInterface = GeoPoint.fromLatLong(latitude, longitude) + + /** 同じタイルを GPU と Canvas で描く。 */ + private fun both( + renderer: () -> ShapeTileRenderer, + x: Int, + y: Int, + z: Int, + ratio: Int = 1, + ): Pair { + assertTrue("GLES 3 が無い", GlShapeRasterizer.isAvailable) + ShapeTileRenderer.useGpu = true + val gpu = renderer().renderBitmap(x, y, z, ratio)!! + ShapeTileRenderer.useGpu = false + val cpu = renderer().renderBitmap(x, y, z, ratio)!! + ShapeTileRenderer.useGpu = true + return gpu to cpu + } + + /** 画素の何割が、どれかのチャンネルで [tolerance] を超えて違うか。 */ + private fun mismatch( + a: Bitmap, + b: Bitmap, + tolerance: Int, + ): Double { + assertEquals(a.width, b.width) + val pa = IntArray(a.width * a.height).also { a.getPixels(it, 0, a.width, 0, 0, a.width, a.height) } + val pb = IntArray(b.width * b.height).also { b.getPixels(it, 0, b.width, 0, 0, b.width, b.height) } + var bad = 0 + for (i in pa.indices) { + var d = 0 + for (shift in intArrayOf(0, 8, 16, 24)) d = max(d, abs(((pa[i] ushr shift) and 0xFF) - ((pb[i] ushr shift) and 0xFF))) + if (d > tolerance) bad++ + } + return bad.toDouble() / pa.size + } + + private fun polygons(states: List) = + ShapeTileRenderer(tileSize = 256, pixelScale = { 1f }, painter = PolygonTilePainter).apply { + setItems(states.map(TilePolygon::from)) + } + + private fun polylines(states: List) = + ShapeTileRenderer(tileSize = 256, pixelScale = { 1f }, painter = PolylineTilePainter).apply { + setItems(states.map(TilePolyline::from)) + } + + private fun circles(states: List) = + ShapeTileRenderer(tileSize = 256, pixelScale = { 1f }, painter = CircleTilePainter).apply { + setItems(states.sortedBy(::effectiveZIndex).map(TileCircle::from)) + } + + private fun box( + south: Double, + west: Double, + north: Double, + east: Double, + ) = listOf(p(south, west), p(south, east), p(north, east), p(north, west)) + + private val colors = listOf(Color.Red, Color.Blue, Color.Green, Color(0xFFFF8800), Color(0xFF8800FF)) + + /** 穴と枠線。穴は抜け、枠線は外周だけ。 */ + @Test + fun polygonWithHolesMatchesTheCanvas() { + val state = + PolygonState( + points = box(-60.0, -120.0, 60.0, 120.0), + holes = listOf(box(-30.0, -80.0, 20.0, 0.0), box(-10.0, 20.0, 40.0, 90.0)), + fillColor = Color.Blue.copy(alpha = 0.6f), + strokeColor = Color.Red, + strokeWidth = 3.dp, + ) + val (gpu, cpu) = both({ polygons(listOf(state)) }, 0, 0, 0) + assertEquals(0, gpu.getPixel(80, 128) ushr 24) + assertTrue(mismatch(gpu, cpu, 48) < 0.02) + } + + /** 半透明のポリゴンが重なる。渡した順に重なる。 */ + @Test + fun translucentPolygonsStackInOrder() { + val states = + (0 until 60).map { i -> + val lat = -60.0 + (i * 37 % 110) + val lng = -150.0 + (i * 53 % 280) + PolygonState( + points = listOf(p(lat, lng), p(lat + 10, lng + 25), p(lat + 25, lng + 5)), + fillColor = colors[i % 5].copy(alpha = 0.5f), + strokeColor = colors[(i + 2) % 5].copy(alpha = 0.7f), + strokeWidth = (1 + i % 3).dp, + ) + } + val (gpu, cpu) = both({ polygons(states) }, 0, 0, 0) + assertTrue(mismatch(gpu, cpu, 48) < 0.03) + } + + /** 半透明の太い線が自分と交差する。交差も継ぎ目も 1 度だけ塗る。 */ + @Test + fun translucentSelfCrossingLineIsPaintedOnce() { + val line = + PolylineState( + points = listOf(p(0.0, -140.0), p(0.0, 100.0), p(50.0, -60.0), p(-60.0, 0.0), p(60.0, 0.0)), + strokeColor = Color.Red.copy(alpha = 0.5f), + strokeWidth = 12.dp, + ) + val (gpu, cpu) = both({ polylines(listOf(line)) }, 0, 0, 0) + val crossing = gpu.getPixel(128, 128) ushr 24 + assertTrue("交差は 1 度だけ: alpha=$crossing", abs(crossing - 128) <= 3) + assertTrue(mismatch(gpu, cpu, 48) < 0.02) + } + + /** 多くの線(255 本を超えて番号が一巡する)。Retina のタイル。 */ + @Test + fun manyLinesMatchTheCanvasOnRetinaTiles() { + val lines = + (0 until 600).map { i -> + val lat = -70.0 + (i * 13 % 140) + val lng = -170.0 + (i * 29 % 300) + PolylineState( + points = listOf(p(lat, lng), p(lat + 4, lng + 20), p(lat - 6, lng + 40), p(lat + 2, lng + 70)), + strokeColor = colors[i % 5].copy(alpha = if (i % 3 == 0) 0.6f else 1f), + strokeWidth = (1 + i % 5).dp, + ) + } + val (gpu, cpu) = both({ polylines(lines) }, 0, 0, 0, ratio = 2) + assertEquals(512, gpu.width) + assertTrue(mismatch(gpu, cpu, 48) < 0.04) + } + + /** 街の円(重なり・縁・半透明)。 */ + @Test + fun cityCirclesMatchTheCanvas() { + val states = + (0 until 400).map { i -> + CircleState( + center = p(42.30 + (i * 37 % 100) / 1000.0, -71.10 + (i * 53 % 100) / 1000.0), + radiusMeters = 20.0 + i * 7 % 300, + fillColor = colors[i % 5].copy(alpha = 0.3f + (i % 7) / 10f), + strokeColor = colors[(i + 1) % 5].copy(alpha = 0.8f), + strokeWidth = (1 + i % 3).dp, + ) + } + // ボストンを含む z13 のタイル。 + val (gpu, cpu) = both({ circles(states) }, 2479, 3030, 13) + assertTrue(mismatch(gpu, cpu, 48) < 0.03) + } + + /** 引いたズーム: 多くが 1 点。1 画素に重なった円は最後の 1 つ。 */ + @Test + fun zoomedOutCircleDotsMatchTheCanvas() { + val states = + (0 until 20_000).map { i -> + CircleState( + center = p(30.0 + (i * 7919 % 2000) / 100.0, -120.0 + (i * 104729 % 5000) / 100.0), + radiusMeters = 100.0 + i % 700, + fillColor = colors[i % 5].copy(alpha = 0.65f), + strokeColor = colors[i % 5].copy(alpha = 0.35f), + strokeWidth = 0.5.dp, + ) + } + val (gpu, cpu) = both({ circles(states) }, 0, 0, 1) + assertTrue(mismatch(gpu, cpu, 48) < 0.03) + } + + /** 高緯度の大きな円は 128 角形(ポリゴンと同じ道)で描く。 */ + @Test + fun bentCirclesAreDrawnAsTheirPolygon() { + val states = + listOf( + CircleState(center = p(68.0, 20.0), radiusMeters = 900_000.0, fillColor = Color.Blue.copy(alpha = 0.5f), strokeColor = Color.Red, strokeWidth = 3.dp), + CircleState(center = p(10.0, -40.0), radiusMeters = 50_000.0, fillColor = Color.Green, strokeColor = Color.Black, strokeWidth = 1.dp), + ) + val (gpu, cpu) = both({ circles(states) }, 0, 0, 0) + assertTrue(mismatch(gpu, cpu, 48) < 0.02) + } + + /** + * 全国規模の集合はファイルに置かれ、GPU は記録をバイト列から直接読む + * ([CircleTilePainter.recordAll])。1 つずつ読む Canvas と同じ絵になる。 + */ + @Test + fun fileBackedCirclesMatchTheCanvas() { + val states = + (0 until 30_000).map { i -> + CircleState( + center = p(30.0 + (i * 7919 % 2000) / 100.0, -120.0 + (i * 104729 % 5000) / 100.0), + radiusMeters = 100.0 + i % 9000, + fillColor = colors[i % 5].copy(alpha = 0.65f), + strokeColor = colors[(i + 1) % 5].copy(alpha = 0.6f), + strokeWidth = (i % 3).dp, + ) + }.sortedBy(::effectiveZIndex).map(TileCircle::from) + fun renderer() = + ShapeTileRenderer(tileSize = 256, pixelScale = { 1f }, painter = CircleTilePainter).apply { + setStorage(CircleTileFileStorage.create(temporary.newFolder(), states.size, { states[it] })) + } + for ((z, x, y) in listOf(Triple(1, 0, 0), Triple(4, 3, 6), Triple(6, 12, 25))) { + val (gpu, cpu) = both(::renderer, x, y, z) + assertTrue("z=$z", mismatch(gpu, cpu, 48) < 0.03) + } + } +} diff --git a/src/main/java/com/mapconductor/core/circle/CircleTileFileStorage.kt b/src/main/java/com/mapconductor/core/circle/CircleTileFileStorage.kt index 4744a43..9cca6b3 100644 --- a/src/main/java/com/mapconductor/core/circle/CircleTileFileStorage.kt +++ b/src/main/java/com/mapconductor/core/circle/CircleTileFileStorage.kt @@ -1,306 +1,373 @@ -package com.mapconductor.core.circle - -import com.mapconductor.core.InternalMapConductorApi -import com.mapconductor.core.shapetile.ShapeTileStorage -import java.io.EOFException -import java.io.File -import java.io.RandomAccessFile -import java.nio.ByteBuffer -import java.nio.ByteOrder -import kotlin.math.max - -/** - * Packed immutable circle geometry, read through a bounded block cache. - * No nationwide TileCircle or bounding-box object arrays are retained. - * Small updates share the file; a bounded override map is compacted when full. - */ -@InternalMapConductorApi -internal class CircleTileFileStorage private constructor( - private val file: SharedFile, - override val size: Int, - override val maxStroke: Float, - private val overrides: Map = emptyMap(), -) : ShapeTileStorage() { - init { - file.retain() - } - - internal val cacheFile: File get() = file.path - internal val cachedBytes: Int get() = file.cachedBytes - internal val allocatedReadBuffers: Int get() = file.allocatedReadBuffers - - override fun get(index: Int): TileCircle? = - when { - index !in 0 until size -> null - overrides.isNotEmpty() && overrides.containsKey(index) -> overrides[index] - else -> file.read(index) { bytes, offset -> decode(bytes, offset) } - } - - override fun bbox(index: Int): DoubleArray? = - when { - index !in 0 until size -> null - overrides.isNotEmpty() && overrides.containsKey(index) -> overrides[index]?.bbox - else -> - file.read(index) { bytes, p -> - if (bytes.getInt(p + 76) and 1 == 0) { - null - } else { - val x = bytes.getDouble(p + 24) - val half = bytes.getDouble(p + 40) - doubleArrayOf(x - half, bytes.getDouble(p + 48), x + half, bytes.getDouble(p + 56)) - } - } - } - - override fun updated(updates: List>): ShapeTileStorage { - var widest = maxStroke - for ((index, item) in updates) { - if (index in 0 until size && item != null) { - widest = max(widest, CircleTilePainter.strokeWidth(item)) - } - } - val changed = HashMap(overrides) - var compact = false - for ((index, item) in updates) if (index in 0 until size) { - if (index !in changed && changed.size == MAX_OVERRIDES) { - compact = true - break - } - changed[index] = item - } - if (!compact) return CircleTileFileStorage(file, size, widest, changed) - // Compact by streaming, never by materialising every decoded circle. - val next = create(file.path.parentFile!!, size, { get(it) }) - try { - next.file.patch(updates, size) - val result = CircleTileFileStorage(next.file, size, widest) - next.release() - return result - } catch (error: Throwable) { - next.release() - throw error - } - } - - override fun close() = file.release() - - @InternalMapConductorApi - private class SharedFile( - val path: File, - ) { - private val handle = RandomAccessFile(path, "rw") - private val channel = handle.channel - private var references = 0 - private val blocks = LinkedHashMap(MAX_BLOCKS, 0.75f, true) - var allocatedReadBuffers = 0 - private set - val cachedBytes: Int get() = synchronized(this) { blocks.values.sumOf { it.capacity() } } - - @Synchronized fun retain() { - references++ - } - - @Synchronized fun release() { - check(--references >= 0) - if (references == 0) dispose() - } - - @Synchronized fun dispose() { - try { - handle.close() - } finally { - blocks.clear() - synchronized(liveFiles) { - liveFiles.remove(path.absolutePath) - path.delete() - } - } - } - - @Synchronized fun read( - index: Int, - action: (ByteBuffer, Int) -> R, - ): R { - val block = index / RECORDS_PER_BLOCK - val bytes = - blocks[block] ?: run { - val buffer = if (blocks.size < MAX_BLOCKS) { - allocatedReadBuffers++ - ByteBuffer.allocate(BLOCK_BYTES).order(ByteOrder.LITTLE_ENDIAN) - } else { - // Reuse the eldest block's bytes. Allocating another - // 40 KiB array at every miss churns Android's large-object heap. - val iterator = blocks.entries.iterator() - val reusable = iterator.next().value - iterator.remove() - reusable.clear() - reusable - } - val start = HEADER_BYTES + block.toLong() * BLOCK_BYTES - val count = minOf(BLOCK_BYTES.toLong(), channel.size() - start).toInt() - if (count < RECORD_BYTES) throw EOFException("Truncated circle cache") - buffer.limit(count) - var at = start - while (buffer.hasRemaining()) { - val n = channel.read(buffer, at) - if (n <= 0) throw EOFException("Truncated circle cache") - at += n - } - buffer.flip() - blocks[block] = buffer - buffer - } - return action(bytes, index % RECORDS_PER_BLOCK * RECORD_BYTES) - } - - fun write( - size: Int, - itemAt: (Int) -> TileCircle?, - checkCancelled: () -> Unit, - ): Float { - val header = - ByteBuffer - .allocate(HEADER_BYTES) - .order(ByteOrder.LITTLE_ENDIAN) - .putInt(MAGIC) - .putInt(1) - .putInt(size) - .putInt(RECORD_BYTES) - header.flip() - while (header.hasRemaining()) channel.write(header) - val bytes = ByteBuffer.allocate(BLOCK_BYTES).order(ByteOrder.LITTLE_ENDIAN) - var widest = 0f - for (index in 0 until size) { - if (index % 4096 == 0) checkCancelled() - val item = itemAt(index) - encode(bytes, bytes.position(), item) - bytes.position(bytes.position() + RECORD_BYTES) - if (item != null) widest = max(widest, CircleTilePainter.strokeWidth(item)) - if (!bytes.hasRemaining()) { - bytes.flip() - while (bytes.hasRemaining()) channel.write(bytes) - bytes.clear() - } - } - bytes.flip() - while (bytes.hasRemaining()) channel.write(bytes) - return widest - } - - @Synchronized fun patch( - updates: List>, - size: Int, - ) { - check(references == 1) // Only an unpublished freshly compacted file may be written. - val bytes = ByteBuffer.allocate(RECORD_BYTES).order(ByteOrder.LITTLE_ENDIAN) - for ((index, item) in updates) { - if (index in 0 until size) { - bytes.clear() - encode(bytes, 0, item) - var at = HEADER_BYTES + index.toLong() * RECORD_BYTES - while (bytes.hasRemaining()) { - val n = channel.write(bytes, at) - check(n > 0) - at += n - } - } - } - blocks.clear() - } - } - - @InternalMapConductorApi - companion object { - private const val MAGIC = 0x5243434d // MCCR, little endian - private const val HEADER_BYTES = 16 - private const val RECORD_BYTES = 80 - private const val RECORDS_PER_BLOCK = 512 - private const val BLOCK_BYTES = RECORD_BYTES * RECORDS_PER_BLOCK - internal const val MAX_BLOCKS = 48 // At most 1.875 MiB per open geometry file. - private const val MAX_OVERRIDES = 2048 - private val liveFiles = HashSet() - - fun create( - directory: File, - size: Int, - itemAt: (Int) -> TileCircle?, - checkCancelled: () -> Unit = { - }, - ): CircleTileFileStorage { - require(size >= 0) - val path = - synchronized(liveFiles) { - check(directory.isDirectory || directory.mkdirs()) { "Cannot create circle cache directory" } - // These are disposable private cache files, not user data. Open - // files from this process stay registered until their last reader exits. - directory - .listFiles() - ?.filter { - it.name.startsWith("circle-geometry-") && - it.name.endsWith(".bin") && - it.absolutePath !in liveFiles - }?.forEach { it.delete() } - File.createTempFile("circle-geometry-", ".bin", directory).also { liveFiles.add(it.absolutePath) } - } - var file: SharedFile? = null - try { - file = SharedFile(path) - val widest = file.write(size, itemAt, checkCancelled) - return CircleTileFileStorage(file, size, widest) - } catch (error: Throwable) { - if (file != - null - ) { - file.dispose() - } else { - synchronized(liveFiles) { - liveFiles.remove(path.absolutePath) - path.delete() - } - } - throw error - } - } - - private fun encode( - bytes: ByteBuffer, - p: Int, - item: TileCircle?, - ) { - if (item == null) { - bytes.putInt(p + 76, 0) - return - } - bytes - .putDouble(p, item.latitude) - .putDouble(p + 8, item.longitude) - .putDouble(p + 16, item.radiusMeters) - .putDouble(p + 24, item.x) - .putDouble(p + 32, item.y) - .putDouble(p + 40, item.halfWidth) - .putDouble(p + 48, item.northY) - .putDouble(p + 56, item.southY) - .putInt(p + 64, item.fill) - .putInt(p + 68, item.stroke) - .putFloat(p + 72, item.strokeWidth) - .putInt(p + 76, if (item.geodesic) 3 else 1) - } - - private fun decode( - bytes: ByteBuffer, - p: Int, - ): TileCircle? { - val flags = bytes.getInt(p + 76) - if (flags and 1 == 0) return null - val x = bytes.getDouble(p + 24) - val half = bytes.getDouble(p + 40) - val north = bytes.getDouble(p + 48) - val south = bytes.getDouble(p + 56) - return TileCircle( - bytes.getDouble(p), bytes.getDouble(p + 8), bytes.getDouble(p + 16), flags and 2 != 0, - x, bytes.getDouble(p + 32), half, north, south, doubleArrayOf(x - half, north, x + half, south), - bytes.getInt(p + 64), bytes.getInt(p + 68), bytes.getFloat(p + 72), - ) - } - } -} +package com.mapconductor.core.circle + +import com.mapconductor.core.InternalMapConductorApi +import com.mapconductor.core.shapetile.ShapeTileStorage +import java.io.EOFException +import java.io.File +import java.io.RandomAccessFile +import java.nio.ByteBuffer +import java.nio.ByteOrder +import kotlin.math.max + +/** + * Packed immutable circle geometry, read through a bounded block cache. + * No nationwide TileCircle or bounding-box object arrays are retained. + * Small updates share the file; a bounded override map is compacted when full. + */ +@InternalMapConductorApi +internal class CircleTileFileStorage private constructor( + private val file: SharedFile, + override val size: Int, + override val maxStroke: Float, + private val overrides: Map = emptyMap(), +) : ShapeTileStorage() { + init { + file.retain() + } + + internal val cacheFile: File get() = file.path + internal val cachedBytes: Int get() = file.cachedBytes + internal val allocatedReadBuffers: Int get() = file.allocatedReadBuffers + + override fun get(index: Int): TileCircle? = + when { + index !in 0 until size -> null + overrides.isNotEmpty() && overrides.containsKey(index) -> overrides[index] + else -> file.read(index) { bytes, offset -> decode(bytes, offset) } + } + + /** + * Calls [action] for each of [indices] in order, with the record's bytes and + * offset in them, or with the overriding circle (bytes null). Records of one + * block are read under one lock, not one per circle: a nationwide tile reads + * hundreds of thousands, on several tile threads at once. + */ + internal fun forEachRecord( + indices: IntArray, + checkCancelled: () -> Unit, + action: RecordAction, + ) { + // The block is copied out under the lock and read outside it: other + // tiles read the same file on other threads, and must not wait while + // this one records its circles. + val scratch = ByteBuffer.allocate(BLOCK_BYTES).order(ByteOrder.LITTLE_ENDIAN) + var k = 0 + while (k < indices.size) { + checkCancelled() + val index = indices[k] + if (index !in 0 until size) { + k++ + continue + } + if (overrides.isNotEmpty() && overrides.containsKey(index)) { + action.record(k, null, 0, overrides[index]) + k++ + continue + } + val block = index / RECORDS_PER_BLOCK + file.copyBlock(block, scratch) + while (k < indices.size) { + val i = indices[k] + if (i / RECORDS_PER_BLOCK != block || i !in 0 until size) break + if (overrides.isNotEmpty() && overrides.containsKey(i)) break + action.record(k, scratch, i % RECORDS_PER_BLOCK * RECORD_BYTES, null) + k++ + } + } + } + + /** What [forEachRecord] calls per circle; primitive arguments, so nothing is boxed per call. */ + internal fun interface RecordAction { + /** [bytes] at [offset] is the record, or [bytes] is null and [override] the circle. */ + fun record( + k: Int, + bytes: ByteBuffer?, + offset: Int, + override: TileCircle?, + ) + } + + override fun bbox(index: Int): DoubleArray? = + when { + index !in 0 until size -> null + overrides.isNotEmpty() && overrides.containsKey(index) -> overrides[index]?.bbox + else -> + file.read(index) { bytes, p -> + if (bytes.getInt(p + 76) and 1 == 0) { + null + } else { + val x = bytes.getDouble(p + 24) + val half = bytes.getDouble(p + 40) + doubleArrayOf(x - half, bytes.getDouble(p + 48), x + half, bytes.getDouble(p + 56)) + } + } + } + + override fun updated(updates: List>): ShapeTileStorage { + var widest = maxStroke + for ((index, item) in updates) { + if (index in 0 until size && item != null) { + widest = max(widest, CircleTilePainter.strokeWidth(item)) + } + } + val changed = HashMap(overrides) + var compact = false + for ((index, item) in updates) if (index in 0 until size) { + if (index !in changed && changed.size == MAX_OVERRIDES) { + compact = true + break + } + changed[index] = item + } + if (!compact) return CircleTileFileStorage(file, size, widest, changed) + // Compact by streaming, never by materialising every decoded circle. + val next = create(file.path.parentFile!!, size, { get(it) }) + try { + next.file.patch(updates, size) + val result = CircleTileFileStorage(next.file, size, widest) + next.release() + return result + } catch (error: Throwable) { + next.release() + throw error + } + } + + override fun close() = file.release() + + @InternalMapConductorApi + private class SharedFile( + val path: File, + ) { + private val handle = RandomAccessFile(path, "rw") + private val channel = handle.channel + private var references = 0 + private val blocks = LinkedHashMap(MAX_BLOCKS, 0.75f, true) + var allocatedReadBuffers = 0 + private set + val cachedBytes: Int get() = synchronized(this) { blocks.values.sumOf { it.capacity() } } + + @Synchronized fun retain() { + references++ + } + + @Synchronized fun release() { + check(--references >= 0) + if (references == 0) dispose() + } + + @Synchronized fun dispose() { + try { + handle.close() + } finally { + blocks.clear() + synchronized(liveFiles) { + liveFiles.remove(path.absolutePath) + path.delete() + } + } + } + + /** Copies [block]'s bytes into [into] (its whole capacity, from 0). */ + @Synchronized fun copyBlock( + block: Int, + into: ByteBuffer, + ) { + val bytes = blockBytes(block) + into.clear() + into.put(bytes.array(), 0, bytes.limit()) + into.flip() + } + + @Synchronized fun read( + index: Int, + action: (ByteBuffer, Int) -> R, + ): R { + val block = index / RECORDS_PER_BLOCK + return action(blockBytes(block), index % RECORDS_PER_BLOCK * RECORD_BYTES) + } + + /** The bytes of [block], read into the cache if they are not there. Call with the lock held. */ + private fun blockBytes(block: Int): ByteBuffer { + val bytes = + blocks[block] ?: run { + val buffer = if (blocks.size < MAX_BLOCKS) { + allocatedReadBuffers++ + ByteBuffer.allocate(BLOCK_BYTES).order(ByteOrder.LITTLE_ENDIAN) + } else { + // Reuse the eldest block's bytes. Allocating another + // 40 KiB array at every miss churns Android's large-object heap. + val iterator = blocks.entries.iterator() + val reusable = iterator.next().value + iterator.remove() + reusable.clear() + reusable + } + val start = HEADER_BYTES + block.toLong() * BLOCK_BYTES + val count = minOf(BLOCK_BYTES.toLong(), channel.size() - start).toInt() + if (count < RECORD_BYTES) throw EOFException("Truncated circle cache") + buffer.limit(count) + var at = start + while (buffer.hasRemaining()) { + val n = channel.read(buffer, at) + if (n <= 0) throw EOFException("Truncated circle cache") + at += n + } + buffer.flip() + blocks[block] = buffer + buffer + } + return bytes + } + + fun write( + size: Int, + itemAt: (Int) -> TileCircle?, + checkCancelled: () -> Unit, + ): Float { + val header = + ByteBuffer + .allocate(HEADER_BYTES) + .order(ByteOrder.LITTLE_ENDIAN) + .putInt(MAGIC) + .putInt(1) + .putInt(size) + .putInt(RECORD_BYTES) + header.flip() + while (header.hasRemaining()) channel.write(header) + val bytes = ByteBuffer.allocate(BLOCK_BYTES).order(ByteOrder.LITTLE_ENDIAN) + var widest = 0f + for (index in 0 until size) { + if (index % 4096 == 0) checkCancelled() + val item = itemAt(index) + encode(bytes, bytes.position(), item) + bytes.position(bytes.position() + RECORD_BYTES) + if (item != null) widest = max(widest, CircleTilePainter.strokeWidth(item)) + if (!bytes.hasRemaining()) { + bytes.flip() + while (bytes.hasRemaining()) channel.write(bytes) + bytes.clear() + } + } + bytes.flip() + while (bytes.hasRemaining()) channel.write(bytes) + return widest + } + + @Synchronized fun patch( + updates: List>, + size: Int, + ) { + check(references == 1) // Only an unpublished freshly compacted file may be written. + val bytes = ByteBuffer.allocate(RECORD_BYTES).order(ByteOrder.LITTLE_ENDIAN) + for ((index, item) in updates) { + if (index in 0 until size) { + bytes.clear() + encode(bytes, 0, item) + var at = HEADER_BYTES + index.toLong() * RECORD_BYTES + while (bytes.hasRemaining()) { + val n = channel.write(bytes, at) + check(n > 0) + at += n + } + } + } + blocks.clear() + } + } + + @InternalMapConductorApi + companion object { + private const val MAGIC = 0x5243434d // MCCR, little endian + private const val HEADER_BYTES = 16 + private const val RECORD_BYTES = 80 + private const val RECORDS_PER_BLOCK = 512 + private const val BLOCK_BYTES = RECORD_BYTES * RECORDS_PER_BLOCK + internal const val MAX_BLOCKS = 48 // At most 1.875 MiB per open geometry file. + private const val MAX_OVERRIDES = 2048 + private val liveFiles = HashSet() + + fun create( + directory: File, + size: Int, + itemAt: (Int) -> TileCircle?, + checkCancelled: () -> Unit = { + }, + ): CircleTileFileStorage { + require(size >= 0) + val path = + synchronized(liveFiles) { + check(directory.isDirectory || directory.mkdirs()) { "Cannot create circle cache directory" } + // These are disposable private cache files, not user data. Open + // files from this process stay registered until their last reader exits. + directory + .listFiles() + ?.filter { + it.name.startsWith("circle-geometry-") && + it.name.endsWith(".bin") && + it.absolutePath !in liveFiles + }?.forEach { it.delete() } + File.createTempFile("circle-geometry-", ".bin", directory).also { liveFiles.add(it.absolutePath) } + } + var file: SharedFile? = null + try { + file = SharedFile(path) + val widest = file.write(size, itemAt, checkCancelled) + return CircleTileFileStorage(file, size, widest) + } catch (error: Throwable) { + if (file != + null + ) { + file.dispose() + } else { + synchronized(liveFiles) { + liveFiles.remove(path.absolutePath) + path.delete() + } + } + throw error + } + } + + private fun encode( + bytes: ByteBuffer, + p: Int, + item: TileCircle?, + ) { + if (item == null) { + bytes.putInt(p + 76, 0) + return + } + bytes + .putDouble(p, item.latitude) + .putDouble(p + 8, item.longitude) + .putDouble(p + 16, item.radiusMeters) + .putDouble(p + 24, item.x) + .putDouble(p + 32, item.y) + .putDouble(p + 40, item.halfWidth) + .putDouble(p + 48, item.northY) + .putDouble(p + 56, item.southY) + .putInt(p + 64, item.fill) + .putInt(p + 68, item.stroke) + .putFloat(p + 72, item.strokeWidth) + .putInt(p + 76, if (item.geodesic) 3 else 1) + } + + private fun decode( + bytes: ByteBuffer, + p: Int, + ): TileCircle? { + val flags = bytes.getInt(p + 76) + if (flags and 1 == 0) return null + val x = bytes.getDouble(p + 24) + val half = bytes.getDouble(p + 40) + val north = bytes.getDouble(p + 48) + val south = bytes.getDouble(p + 56) + return TileCircle( + bytes.getDouble(p), bytes.getDouble(p + 8), bytes.getDouble(p + 16), flags and 2 != 0, + x, bytes.getDouble(p + 32), half, north, south, doubleArrayOf(x - half, north, x + half, south), + bytes.getInt(p + 64), bytes.getInt(p + 68), bytes.getFloat(p + 72), + ) + } + } +} diff --git a/src/main/java/com/mapconductor/core/circle/CircleTileGeometry.kt b/src/main/java/com/mapconductor/core/circle/CircleTileGeometry.kt index 4769904..055c4a7 100644 --- a/src/main/java/com/mapconductor/core/circle/CircleTileGeometry.kt +++ b/src/main/java/com/mapconductor/core/circle/CircleTileGeometry.kt @@ -12,7 +12,11 @@ import com.mapconductor.core.polygon.isVisibleColor import com.mapconductor.core.polygon.simplifyRing import com.mapconductor.core.polygon.toMercatorRing import com.mapconductor.core.projection.Earth +import com.mapconductor.core.shapetile.GlShapeScene import com.mapconductor.core.shapetile.ShapeTileRenderer +import com.mapconductor.core.shapetile.ShapeTileStorage +import com.mapconductor.core.shapetile.toTilePixels +import java.util.BitSet import kotlin.math.PI import kotlin.math.abs import kotlin.math.asin @@ -320,4 +324,149 @@ internal object CircleTilePainter : ShapeTileRenderer.Painter { else -> return false } } + + override val drawsOnGpu: Boolean get() = true + + /** + * The circles of a file-backed set, read straight from the file's bytes + * ([CircleTileFileStorage.forEachRecord]): decoding a [TileCircle] per + * circle took about 3.5 µs, seconds for a tile of a nationwide set, against + * milliseconds to draw them. The same steps as [subpixelSlot], [simplifyCircle] + * and [record]; a circle bent into its 128-gon throws [GlShapeScene.NeedsBatches]. + */ + override fun recordAll( + scene: GlShapeScene, + storage: ShapeTileStorage, + candidates: IntArray, + active: BitSet?, + scale: Double, + originX: Double, + originY: Double, + tileSize: Int, + r: Float, + checkCancelled: () -> Unit, + ): Boolean { + if (storage !is CircleTileFileStorage) return false + fun slotOf(x: Double, y: Double, half: Double, north: Double, south: Double): Int { + val radius = max(half, max(y - north, south - y)) * scale + if (radius >= CIRCLE_DOT_RADIUS_PX) return -2 + val px = floor(x * scale) - originX + val py = floor(y * scale) - originY + return if (px >= 0 && px < tileSize && py >= 0 && py < tileSize) py.toInt() * tileSize + px.toInt() else -1 + } + val lastDot = IntArray(tileSize * tileSize) + storage.forEachRecord(candidates, checkCancelled) { k, bytes, p, override -> + val index = candidates[k] + if (active != null && !active[index]) return@forEachRecord + if (bytes == null) { + val item = override ?: return@forEachRecord + if (!isVisible(item)) return@forEachRecord + val slot = subpixelSlot(item, scale, originX, originY, tileSize) + if (slot >= 0) lastDot[slot] = index + 1 + return@forEachRecord + } + if (bytes.getInt(p + 76) and 1 == 0) return@forEachRecord + val fill = bytes.getInt(p + 64) + val stroke = bytes.getInt(p + 68) + val strokeWidth = bytes.getFloat(p + 72) + if (!isVisibleColor(fill) && !(strokeWidth > 0f && isVisibleColor(stroke))) return@forEachRecord + val slot = slotOf(bytes.getDouble(p + 24), bytes.getDouble(p + 32), bytes.getDouble(p + 40), bytes.getDouble(p + 48), bytes.getDouble(p + 56)) + if (slot >= 0) lastDot[slot] = index + 1 + } + storage.forEachRecord(candidates, checkCancelled) { k, bytes, p, override -> + val index = candidates[k] + if (active != null && !active[index]) return@forEachRecord + if (bytes == null) { + val item = override ?: return@forEachRecord + val slot = subpixelSlot(item, scale, originX, originY, tileSize) + if (slot == -1 || (slot >= 0 && lastDot[slot] != index + 1)) return@forEachRecord + record(scene, item, simplifyCircle(item, scale), originX, originY, r) + return@forEachRecord + } + if (bytes.getInt(p + 76) and 1 == 0) return@forEachRecord + val x = bytes.getDouble(p + 24) + val y = bytes.getDouble(p + 32) + val half = bytes.getDouble(p + 40) + val north = bytes.getDouble(p + 48) + val south = bytes.getDouble(p + 56) + val slot = slotOf(x, y, half, north, south) + if (slot == -1 || (slot >= 0 && lastDot[slot] != index + 1)) return@forEachRecord + val fill = bytes.getInt(p + 64) + val stroke = bytes.getInt(p + 68) + val strokeWidth = bytes.getFloat(p + 72) + val fillVisible = isVisibleColor(fill) + val strokeVisible = strokeWidth > 0f && isVisibleColor(stroke) + if (!fillVisible && !strokeVisible) return@forEachRecord + val cx = x * scale + val cy = y * scale + val rx = half * scale + val ryN = (y - north) * scale + val ryS = (south - y) * scale + val maxR = max(rx, max(ryN, ryS)) + if (maxR < CIRCLE_DOT_RADIUS_PX) { + scene.addCircle( + ((floor(cx) - originX) * r).toFloat(), ((floor(cy) - originY) * r).toFloat(), r, r, + if (fillVisible) fill else stroke, 0, kind = 1, + ) + return@forEachRecord + } + // Bent by the projection: its 128-gon, which the batched scene draws. + if (max(abs(ryN - rx), abs(ryS - rx)) > CIRCLE_ARC_TOLERANCE_PX) throw GlShapeScene.NeedsBatches() + val outlined = strokeVisible && 2 * maxR >= MIN_OUTLINED_PX + if (!fillVisible && !outlined) return@forEachRecord + scene.addCircle( + ((cx - originX) * r).toFloat(), ((cy - originY) * r).toFloat(), (rx * r).toFloat(), + if (outlined) strokeWidth * r else 0f, + if (fillVisible) fill else 0, if (outlined) stroke else 0, kind = 0, + ) + } + return true + } + + override fun record( + scene: GlShapeScene, + item: TileCircle, + shape: Any, + originX: Double, + originY: Double, + r: Float, + ): Boolean { + val fillVisible = item.fillVisible + val strokeVisible = item.strokeVisible + if (!fillVisible && !strokeVisible) return false + when (shape) { + is CircleTileShape.Dot -> { + val left = ((shape.x - originX) * r).toFloat() + val top = ((shape.y - originY) * r).toFloat() + scene.addCircle(left, top, r, r, if (fillVisible) item.fill else item.stroke, 0, kind = 1) + return true + } + is CircleTileShape.Arc -> { + val outlined = strokeVisible && shape.outlined + if (!fillVisible && !outlined) return false + scene.addCircle( + ((shape.cx - originX) * r).toFloat(), + ((shape.cy - originY) * r).toFloat(), + (shape.radius * r).toFloat(), + if (outlined) item.strokeWidth * r else 0f, + if (fillVisible) item.fill else 0, + if (outlined) item.stroke else 0, + kind = 0, + ) + return true + } + is CircleTileShape.Ring -> { + val outlined = strokeVisible && shape.outlined + if (!fillVisible && !outlined) return false + scene.addPolygon( + listOf(toTilePixels(shape.points, originX, originY, r)), + fill = if (fillVisible) item.fill else 0, + outline = if (outlined) item.stroke else 0, + strokeWidth = item.strokeWidth * r, + ) + return true + } + else -> return false + } + } } diff --git a/src/main/java/com/mapconductor/core/polygon/PolygonTileRenderer.kt b/src/main/java/com/mapconductor/core/polygon/PolygonTileRenderer.kt index a1eed80..3424ca4 100644 --- a/src/main/java/com/mapconductor/core/polygon/PolygonTileRenderer.kt +++ b/src/main/java/com/mapconductor/core/polygon/PolygonTileRenderer.kt @@ -2,7 +2,9 @@ package com.mapconductor.core.polygon import com.mapconductor.core.InternalMapConductorApi import com.mapconductor.core.ResourceProvider +import com.mapconductor.core.shapetile.GlShapeScene import com.mapconductor.core.shapetile.ShapeTileRenderer +import com.mapconductor.core.shapetile.toTilePixels import com.mapconductor.core.tileserver.TileProviderInterface import com.mapconductor.core.tileserver.TileRequest import android.graphics.Bitmap @@ -123,6 +125,40 @@ internal object PolygonTilePainter : ShapeTileRenderer.Painter { return drew } + override val drawsOnGpu: Boolean get() = ShapeTileRenderer.gpuPolygonsAndLines + + override fun record( + scene: GlShapeScene, + item: TilePolygon, + shape: Any, + originX: Double, + originY: Double, + r: Float, + ): Boolean { + val fillVisible = item.fillVisible + when (shape) { + is TileShape.Dot -> { + if (!fillVisible && !item.strokeVisible) return false + val left = ((shape.x - originX) * r).toFloat() + val top = ((shape.y - originY) * r).toFloat() + scene.addCircle(left, top, r, r, if (fillVisible) item.fill else item.stroke, 0, kind = 1) + return true + } + is TileShape.Rings -> { + val outlined = item.strokeVisible && shape.outlined + if (!fillVisible && !outlined) return false + scene.addPolygon( + shape.rings.map { toTilePixels(it, originX, originY, r) }, + fill = if (fillVisible) item.fill else 0, + outline = if (outlined) item.stroke else 0, + strokeWidth = item.strokeWidth * r, + ) + return true + } + } + return false + } + private fun addRing( path: Path, ring: DoubleArray, diff --git a/src/main/java/com/mapconductor/core/polyline/PolylineTileGeometry.kt b/src/main/java/com/mapconductor/core/polyline/PolylineTileGeometry.kt index d25aea3..82b56e2 100644 --- a/src/main/java/com/mapconductor/core/polyline/PolylineTileGeometry.kt +++ b/src/main/java/com/mapconductor/core/polyline/PolylineTileGeometry.kt @@ -6,7 +6,9 @@ import com.mapconductor.core.polygon.DP_TOLERANCE_PX import com.mapconductor.core.polygon.douglasPeucker import com.mapconductor.core.polygon.isVisibleColor import com.mapconductor.core.polygon.toMercatorRing +import com.mapconductor.core.shapetile.GlShapeScene import com.mapconductor.core.shapetile.ShapeTileRenderer +import com.mapconductor.core.shapetile.toTilePixels import kotlin.math.floor import kotlin.math.max import kotlin.math.min @@ -189,4 +191,26 @@ internal object PolylineTilePainter : ShapeTileRenderer.Painter { } return true } + + override val drawsOnGpu: Boolean get() = ShapeTileRenderer.gpuPolygonsAndLines + + override fun record( + scene: GlShapeScene, + item: TilePolyline, + shape: Any, + originX: Double, + originY: Double, + r: Float, + ): Boolean { + if (!item.visible) return false + when (shape) { + is PolylineTileShape.Dot -> { + val radius = max(item.strokeWidth * r, 1f) / 2f + scene.addLineDot(((shape.x - originX) * r).toFloat(), ((shape.y - originY) * r).toFloat(), radius, item.color) + } + is PolylineTileShape.Path -> scene.addLine(toTilePixels(shape.points, originX, originY, r), item.color, item.strokeWidth * r) + else -> return false + } + return true + } } diff --git a/src/main/java/com/mapconductor/core/shapetile/GlShapeRasterizer.kt b/src/main/java/com/mapconductor/core/shapetile/GlShapeRasterizer.kt new file mode 100644 index 0000000..377db43 --- /dev/null +++ b/src/main/java/com/mapconductor/core/shapetile/GlShapeRasterizer.kt @@ -0,0 +1,618 @@ +package com.mapconductor.core.shapetile + +import com.mapconductor.core.InternalMapConductorApi +import java.nio.ByteBuffer +import java.nio.ByteOrder +import java.nio.FloatBuffer +import java.util.ArrayDeque +import android.opengl.EGL14 +import android.opengl.EGLConfig +import android.opengl.EGLContext +import android.opengl.EGLDisplay +import android.opengl.EGLSurface +import android.opengl.GLES30 +import android.util.Log + +/** + * Draws a [GlShapeScene] with OpenGL ES 3.0 off screen and reads the pixels + * back: the GPU path of [ShapeTileRenderer], the counterpart of ios-sdk-core's + * `MetalShapeRasterizer` and `MetalPathRasterizer`. + * + * - 4x MSAA (or what the GPU offers up to 4) gives the antialiasing; the stencil + * is multisampled too, so a covered edge is the share of its samples drawn. + * - Premultiplied colours, blended source-over (`ONE, ONE_MINUS_SRC_ALPHA`), as an + * `ARGB_8888` bitmap holds them; the pixels come back premultiplied. + * - y is flipped in the vertex shaders, so the first row `glReadPixels` returns + * is the top of the tile, as a bitmap's first row is. + * + * Tiles are asked for on several of the tile server's threads at once. A GL + * context is current on one thread at a time, so there is a small pool of + * contexts, each with its own programs and targets; a tile borrows one and + * makes it current for the draw. Without GLES 3 (or when anything fails to set + * up) [render] returns null and the caller draws on the CPU. + */ +@InternalMapConductorApi +internal object GlShapeRasterizer { + private const val TAG = "GlShapeRasterizer" + private const val MAX_CONTEXTS = 4 + private const val EGL_OPENGL_ES3_BIT = 0x40 + + private val idle = ArrayDeque() + private var created = 0 + private val lock = Object() + + @Volatile + private var broken = false + + /** Whether GLES 3 drawing works here; false after the first failure to set it up. */ + val isAvailable: Boolean + get() { + if (broken) return false + val context = acquire() ?: return false + release(context) + return true + } + + /** + * Draws [scene] and returns its pixels: a direct buffer of `pixels * pixels` + * premultiplied RGBA, top row first. Null when the GPU cannot draw it. + */ + fun render(scene: GlShapeScene): ByteBuffer? { + if (scene.isEmpty) return null + val context = acquire() ?: return null + var ok = false + try { + val result = context.draw(scene) + ok = result != null + return result + } catch (error: RuntimeException) { + Log.w(TAG, "GPU tile failed; drawing on the CPU", error) + return null + } finally { + if (ok) release(context) else context.destroy() + } + } + + /** + * Makes every context of the pool and its shaders ahead of the first tiles, + * which otherwise wait for them (about a tenth of a second each on a tablet). + */ + fun prepare() { + if (broken) return + val made = ArrayList() + repeat(MAX_CONTEXTS) { + val context = acquire() ?: return@repeat + context.releaseCurrent() + made += context + } + for (context in made) { + synchronized(lock) { + idle.addLast(context) + lock.notifyAll() + } + } + } + + private fun acquire(): Context? { + synchronized(lock) { + if (broken) return null + idle.pollFirst()?.let { context -> + return if (context.makeCurrent()) context else null + } + if (created >= MAX_CONTEXTS) { + while (idle.isEmpty()) lock.wait() + return idle.pollFirst()?.takeIf { it.makeCurrent() } + } + created++ + } + val context = + try { + Context.create() + } catch (error: RuntimeException) { + Log.w(TAG, "GLES 3 unavailable; shape tiles are drawn on the CPU", error) + null + } + if (context == null) { + synchronized(lock) { + created-- + broken = true + lock.notifyAll() + } + } + return context + } + + private fun release(context: Context) { + context.releaseCurrent() + synchronized(lock) { + idle.addLast(context) + lock.notifyAll() + } + } + + private fun Context.destroy() { + destroyContext() + synchronized(lock) { + created-- + lock.notifyAll() + } + } + + private class Context private constructor( + private val display: EGLDisplay, + private val context: EGLContext, + private val surface: EGLSurface, + ) { + private val samples: Int + private val circleProgram: Int + private val pathProgram: Int + private val coverProgram: Int + private val lineProgram: Int + private val buffers = IntArray(8) + private val pathExtentAt: Int + private val coverExtentAt: Int + private val circleExtentAt: Int + private val lineExtentAt: Int + /** Direct buffers for uploads, grown as needed and kept. */ + private var uploadBytes: ByteBuffer = ByteBuffer.allocateDirect(1 shl 16).order(ByteOrder.nativeOrder()) + private var targets = HashMap() + + private class Targets( + val msaa: Int, + val resolve: Int, + ) + + init { + val max = IntArray(1) + GLES30.glGetIntegerv(GLES30.GL_MAX_SAMPLES, max, 0) + samples = minOf(4, max[0]).coerceAtLeast(1) + circleProgram = program(CIRCLE_VERTEX, CIRCLE_FRAGMENT) + pathProgram = program(PATH_VERTEX, PATH_FRAGMENT) + coverProgram = program(COVER_VERTEX, COVER_FRAGMENT) + lineProgram = program(LINE_VERTEX, COVER_FRAGMENT) + pathExtentAt = GLES30.glGetUniformLocation(pathProgram, "extent") + coverExtentAt = GLES30.glGetUniformLocation(coverProgram, "extent") + circleExtentAt = GLES30.glGetUniformLocation(circleProgram, "extent") + lineExtentAt = GLES30.glGetUniformLocation(lineProgram, "extent") + GLES30.glGenBuffers(buffers.size, buffers, 0) + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[CORNERS]) + val corners = floatArrayOf(0f, 0f, 1f, 0f, 0f, 1f, 1f, 0f, 1f, 1f, 0f, 1f) + GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, corners.size * 4, staticFloats(corners), GLES30.GL_STATIC_DRAW) + glOk("setup") + } + + fun makeCurrent(): Boolean = EGL14.eglMakeCurrent(display, surface, surface, context) + + fun releaseCurrent() { + EGL14.eglMakeCurrent(display, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT) + } + + fun destroyContext() { + releaseCurrent() + EGL14.eglDestroySurface(display, surface) + EGL14.eglDestroyContext(display, context) + } + + fun draw(scene: GlShapeScene): ByteBuffer? { + val pixels = scene.pixels + val target = targets.getOrPut(pixels) { createTargets(pixels) } + GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, target.msaa) + GLES30.glViewport(0, 0, pixels, pixels) + GLES30.glClearColor(0f, 0f, 0f, 0f) + GLES30.glClearStencil(0) + GLES30.glClearDepthf(1f) + GLES30.glDepthMask(true) + GLES30.glStencilMask(0xFF) + GLES30.glColorMask(true, true, true, true) + GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT or GLES30.GL_STENCIL_BUFFER_BIT or GLES30.GL_DEPTH_BUFFER_BIT) + GLES30.glEnable(GLES30.GL_BLEND) + GLES30.glBlendFuncSeparate(GLES30.GL_ONE, GLES30.GL_ONE_MINUS_SRC_ALPHA, GLES30.GL_ONE, GLES30.GL_ONE_MINUS_SRC_ALPHA) + GLES30.glEnable(GLES30.GL_STENCIL_TEST) + GLES30.glDisable(GLES30.GL_DEPTH_TEST) + GLES30.glDisable(GLES30.GL_CULL_FACE) + + upload(FILL, scene.fill) + upload(STROKE, scene.stroke) + upload(FILL_COVERS, scene.fillCovers) + upload(STROKE_COVERS, scene.strokeCovers) + upload(CIRCLES, scene.circles) + upload(LINES, scene.lineVertices) + if (scene.lineColors.size > 0) { + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[LINE_COLORS]) + GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, scene.lineColors.size * 4, ints(scene.lineColors.data, scene.lineColors.size), GLES30.GL_STREAM_DRAW) + } + + val extent = pixels.toFloat() + for (batch in scene.batches) { + if (!batch.fill.isEmpty()) { + GLES30.glColorMask(false, false, false, false) + GLES30.glStencilFunc(GLES30.GL_ALWAYS, 0, 0xFF) + // nonzero: front faces +1, back faces -1, both wrapping at 256. + GLES30.glStencilOpSeparate(GLES30.GL_FRONT, GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_INCR_WRAP) + GLES30.glStencilOpSeparate(GLES30.GL_BACK, GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_DECR_WRAP) + drawPath(FILL, batch.fill, extent) + GLES30.glColorMask(true, true, true, true) + } + if (!batch.fillCovers.isEmpty()) drawCovers(FILL_COVERS, batch.fillCovers, extent) + if (!batch.circles.isEmpty()) drawCircles(batch.circles, extent) + if (!batch.stroke.isEmpty()) { + GLES30.glColorMask(false, false, false, false) + GLES30.glStencilFunc(GLES30.GL_ALWAYS, 1, 0xFF) + GLES30.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_REPLACE) + drawPath(STROKE, batch.stroke, extent) + GLES30.glColorMask(true, true, true, true) + } + if (!batch.strokeCovers.isEmpty()) drawCovers(STROKE_COVERS, batch.strokeCovers, extent) + } + if (scene.lineCount > 0) drawLines(scene, extent) + + GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, target.msaa) + GLES30.glBindFramebuffer(GLES30.GL_DRAW_FRAMEBUFFER, target.resolve) + GLES30.glBlitFramebuffer(0, 0, pixels, pixels, 0, 0, pixels, pixels, GLES30.GL_COLOR_BUFFER_BIT, GLES30.GL_NEAREST) + GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER, target.resolve) + val out = ByteBuffer.allocateDirect(pixels * pixels * 4).order(ByteOrder.nativeOrder()) + GLES30.glReadPixels(0, 0, pixels, pixels, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, out) + GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0) + if (!glOk("draw")) return null + out.rewind() + return out + } + + private fun drawPath( + buffer: Int, + range: IntRange, + extent: Float, + ) { + GLES30.glUseProgram(pathProgram) + GLES30.glUniform1f(pathExtentAt, extent) + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[buffer]) + GLES30.glEnableVertexAttribArray(0) + GLES30.glVertexAttribPointer(0, 2, GLES30.GL_FLOAT, false, 8, 0) + GLES30.glDrawArrays(GLES30.GL_TRIANGLES, range.first, range.last - range.first + 1) + GLES30.glDisableVertexAttribArray(0) + } + + private fun drawCovers( + buffer: Int, + range: IntRange, + extent: Float, + ) { + // Paint where the stencil is not 0, and put it back to 0 for the next shapes. + GLES30.glStencilFunc(GLES30.GL_NOTEQUAL, 0, 0xFF) + GLES30.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_ZERO) + GLES30.glUseProgram(coverProgram) + GLES30.glUniform1f(coverExtentAt, extent) + bindCorners() + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[buffer]) + val stride = GlShapeScene.COVER_FLOATS * 4 + val offset = range.first * stride + instanced(1, 4, stride, offset) + instanced(2, 4, stride, offset + 16) + GLES30.glDrawArraysInstanced(GLES30.GL_TRIANGLES, 0, 6, range.last - range.first + 1) + disable(0, 1, 2) + } + + private fun drawCircles( + range: IntRange, + extent: Float, + ) { + GLES30.glStencilFunc(GLES30.GL_ALWAYS, 0, 0xFF) + GLES30.glStencilOp(GLES30.GL_KEEP, GLES30.GL_KEEP, GLES30.GL_KEEP) + GLES30.glUseProgram(circleProgram) + GLES30.glUniform1f(circleExtentAt, extent) + bindCorners() + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[CIRCLES]) + val stride = GlShapeScene.CIRCLE_FLOATS * 4 + val offset = range.first * stride + instanced(1, 4, stride, offset) + instanced(2, 4, stride, offset + 16) + instanced(3, 4, stride, offset + 32) + instanced(4, 1, stride, offset + 48) + GLES30.glDrawArraysInstanced(GLES30.GL_TRIANGLES, 0, 6, range.last - range.first + 1) + disable(0, 1, 2, 3, 4) + } + + private fun drawLines( + scene: GlShapeScene, + extent: Float, + ) { + // Every line in one draw: each paints only nearer than what is there, + // so once per line whatever its own joins and crossings overlap, and + // each later line (nearer) over the earlier ones. + GLES30.glDisable(GLES30.GL_STENCIL_TEST) + GLES30.glEnable(GLES30.GL_DEPTH_TEST) + GLES30.glDepthFunc(GLES30.GL_LESS) + GLES30.glDepthMask(true) + GLES30.glUseProgram(lineProgram) + GLES30.glUniform1f(lineExtentAt, extent) + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[LINES]) + GLES30.glEnableVertexAttribArray(0) + GLES30.glVertexAttribPointer(0, 3, GLES30.GL_FLOAT, false, 12, 0) + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[LINE_COLORS]) + GLES30.glEnableVertexAttribArray(1) + GLES30.glVertexAttribPointer(1, 4, GLES30.GL_UNSIGNED_BYTE, true, 4, 0) + GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, scene.lineVertices.size / 3) + GLES30.glDisableVertexAttribArray(0) + GLES30.glDisableVertexAttribArray(1) + GLES30.glDisable(GLES30.GL_DEPTH_TEST) + GLES30.glEnable(GLES30.GL_STENCIL_TEST) + } + + private fun bindCorners() { + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[CORNERS]) + GLES30.glEnableVertexAttribArray(0) + GLES30.glVertexAttribPointer(0, 2, GLES30.GL_FLOAT, false, 8, 0) + GLES30.glVertexAttribDivisor(0, 0) + } + + private fun instanced( + location: Int, + size: Int, + stride: Int, + offset: Int, + ) { + GLES30.glEnableVertexAttribArray(location) + GLES30.glVertexAttribPointer(location, size, GLES30.GL_FLOAT, false, stride, offset) + GLES30.glVertexAttribDivisor(location, 1) + } + + private fun disable(vararg locations: Int) { + for (location in locations) { + GLES30.glVertexAttribDivisor(location, 0) + GLES30.glDisableVertexAttribArray(location) + } + } + + private fun upload( + index: Int, + values: GlShapeScene.Floats, + ) { + if (values.size == 0) return + GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffers[index]) + GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, values.size * 4, floats(values.data, values.size), GLES30.GL_STREAM_DRAW) + } + + /** [values] in the shared upload buffer, grown when too small. */ + private fun floats( + values: FloatArray, + size: Int, + ): FloatBuffer { + val bytes = staging(size * 4) + val buffer = bytes.asFloatBuffer() + buffer.put(values, 0, size) + buffer.rewind() + return buffer + } + + private fun ints( + values: IntArray, + size: Int, + ): java.nio.IntBuffer { + val bytes = staging(size * 4) + val buffer = bytes.asIntBuffer() + buffer.put(values, 0, size) + buffer.rewind() + return buffer + } + + private fun staging(bytes: Int): ByteBuffer { + if (uploadBytes.capacity() < bytes) { + uploadBytes = ByteBuffer.allocateDirect(Integer.highestOneBit(bytes) shl 1).order(ByteOrder.nativeOrder()) + } + uploadBytes.clear() + return uploadBytes + } + + private fun createTargets(pixels: Int): Targets { + val renderbuffers = IntArray(3) + GLES30.glGenRenderbuffers(3, renderbuffers, 0) + GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, renderbuffers[0]) + GLES30.glRenderbufferStorageMultisample(GLES30.GL_RENDERBUFFER, samples, GLES30.GL_RGBA8, pixels, pixels) + GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, renderbuffers[1]) + GLES30.glRenderbufferStorageMultisample(GLES30.GL_RENDERBUFFER, samples, GLES30.GL_DEPTH24_STENCIL8, pixels, pixels) + GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, renderbuffers[2]) + GLES30.glRenderbufferStorage(GLES30.GL_RENDERBUFFER, GLES30.GL_RGBA8, pixels, pixels) + val framebuffers = IntArray(2) + GLES30.glGenFramebuffers(2, framebuffers, 0) + GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, framebuffers[0]) + GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_RENDERBUFFER, renderbuffers[0]) + GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_RENDERBUFFER, renderbuffers[1]) + check(GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER) == GLES30.GL_FRAMEBUFFER_COMPLETE) { "MSAA framebuffer incomplete" } + GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, framebuffers[1]) + GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_RENDERBUFFER, renderbuffers[2]) + check(GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER) == GLES30.GL_FRAMEBUFFER_COMPLETE) { "resolve framebuffer incomplete" } + GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0) + return Targets(framebuffers[0], framebuffers[1]) + } + + private fun program( + vertex: String, + fragment: String, + ): Int { + fun shader( + type: Int, + source: String, + ): Int { + val shader = GLES30.glCreateShader(type) + GLES30.glShaderSource(shader, source) + GLES30.glCompileShader(shader) + val ok = IntArray(1) + GLES30.glGetShaderiv(shader, GLES30.GL_COMPILE_STATUS, ok, 0) + check(ok[0] != 0) { "shader: " + GLES30.glGetShaderInfoLog(shader) } + return shader + } + val program = GLES30.glCreateProgram() + GLES30.glAttachShader(program, shader(GLES30.GL_VERTEX_SHADER, vertex)) + GLES30.glAttachShader(program, shader(GLES30.GL_FRAGMENT_SHADER, fragment)) + GLES30.glLinkProgram(program) + val ok = IntArray(1) + GLES30.glGetProgramiv(program, GLES30.GL_LINK_STATUS, ok, 0) + check(ok[0] != 0) { "program: " + GLES30.glGetProgramInfoLog(program) } + return program + } + + private fun glOk(where: String): Boolean { + val error = GLES30.glGetError() + if (error == GLES30.GL_NO_ERROR) return true + Log.w(TAG, "GL error 0x${Integer.toHexString(error)} in $where") + return false + } + + companion object { + const val FILL = 0 + const val STROKE = 1 + const val FILL_COVERS = 2 + const val STROKE_COVERS = 3 + const val CIRCLES = 4 + const val LINES = 5 + const val CORNERS = 6 + const val LINE_COLORS = 7 + + fun create(): Context { + val display = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY) + check(display != EGL14.EGL_NO_DISPLAY) { "no EGL display" } + val version = IntArray(2) + check(EGL14.eglInitialize(display, version, 0, version, 1)) { "eglInitialize" } + val attributes = + intArrayOf( + EGL14.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT, + EGL14.EGL_SURFACE_TYPE, EGL14.EGL_PBUFFER_BIT, + EGL14.EGL_RED_SIZE, 8, + EGL14.EGL_GREEN_SIZE, 8, + EGL14.EGL_BLUE_SIZE, 8, + EGL14.EGL_ALPHA_SIZE, 8, + EGL14.EGL_NONE, + ) + val configs = arrayOfNulls(1) + val count = IntArray(1) + check(EGL14.eglChooseConfig(display, attributes, 0, configs, 0, 1, count, 0) && count[0] > 0) { "no GLES 3 config" } + val config = configs[0] + val context = + EGL14.eglCreateContext(display, config, EGL14.EGL_NO_CONTEXT, intArrayOf(EGL14.EGL_CONTEXT_CLIENT_VERSION, 3, EGL14.EGL_NONE), 0) + check(context != EGL14.EGL_NO_CONTEXT) { "eglCreateContext" } + val surface = EGL14.eglCreatePbufferSurface(display, config, intArrayOf(EGL14.EGL_WIDTH, 1, EGL14.EGL_HEIGHT, 1, EGL14.EGL_NONE), 0) + check(surface != EGL14.EGL_NO_SURFACE) { "eglCreatePbufferSurface" } + check(EGL14.eglMakeCurrent(display, surface, surface, context)) { "eglMakeCurrent" } + return Context(display, context, surface) + } + + private fun staticFloats(values: FloatArray): FloatBuffer { + val buffer = ByteBuffer.allocateDirect(values.size * 4).order(ByteOrder.nativeOrder()).asFloatBuffer() + buffer.put(values) + buffer.rewind() + return buffer + } + + private const val CIRCLE_VERTEX = """#version 300 es +layout(location = 0) in vec2 corner; +layout(location = 1) in vec4 geometry; +layout(location = 2) in vec4 fillColor; +layout(location = 3) in vec4 strokeColor; +layout(location = 4) in float kind; +uniform float extent; +out vec2 pixel; +flat out vec4 vGeometry; +flat out vec4 vFill; +flat out vec4 vStroke; +flat out float vKind; +void main() { + vec2 lo; + vec2 hi; + if (kind > 0.5) { + lo = geometry.xy; + hi = lo + geometry.zw; + } else { + float reach = geometry.z + geometry.w * 0.5 + 1.0; + lo = geometry.xy - reach; + hi = geometry.xy + reach; + } + vec2 px = mix(lo, hi, corner); + vec2 unit = px / extent; + gl_Position = vec4(unit.x * 2.0 - 1.0, unit.y * 2.0 - 1.0, 0.0, 1.0); + pixel = px; + vGeometry = geometry; + vFill = fillColor; + vStroke = strokeColor; + vKind = kind; +} +""" + + private const val CIRCLE_FRAGMENT = """#version 300 es +precision highp float; +in vec2 pixel; +flat in vec4 vGeometry; +flat in vec4 vFill; +flat in vec4 vStroke; +flat in float vKind; +out vec4 color; +void main() { + if (vKind > 0.5) { + color = vFill; + return; + } + float d = length(pixel - vGeometry.xy); + float radius = vGeometry.z; + float halfWidth = vGeometry.w * 0.5; + float fillCoverage = clamp(radius - d + 0.5, 0.0, 1.0); + float strokeCoverage = clamp(radius + halfWidth - d + 0.5, 0.0, 1.0) - clamp(radius - halfWidth - d + 0.5, 0.0, 1.0); + vec4 fill = vFill * fillCoverage; + vec4 stroke = vStroke * strokeCoverage; + color = stroke + fill * (1.0 - stroke.a); +} +""" + + private const val PATH_VERTEX = """#version 300 es +layout(location = 0) in vec2 position; +uniform float extent; +void main() { + vec2 unit = position / extent; + gl_Position = vec4(unit.x * 2.0 - 1.0, unit.y * 2.0 - 1.0, 0.0, 1.0); +} +""" + + private const val LINE_VERTEX = """#version 300 es +layout(location = 0) in vec3 position; +layout(location = 1) in vec4 lineColor; +uniform float extent; +flat out vec4 vColor; +void main() { + vec2 unit = position.xy / extent; + gl_Position = vec4(unit.x * 2.0 - 1.0, unit.y * 2.0 - 1.0, position.z * 2.0 - 1.0, 1.0); + vColor = lineColor; +} +""" + + private const val COVER_VERTEX = """#version 300 es +layout(location = 0) in vec2 corner; +layout(location = 1) in vec4 rect; +layout(location = 2) in vec4 coverColor; +uniform float extent; +flat out vec4 vColor; +void main() { + vec2 px = mix(rect.xy, rect.zw, corner); + vec2 unit = px / extent; + gl_Position = vec4(unit.x * 2.0 - 1.0, unit.y * 2.0 - 1.0, 0.0, 1.0); + vColor = coverColor; +} +""" + + private const val PATH_FRAGMENT = """#version 300 es +precision mediump float; +uniform vec4 color; +out vec4 fragColor; +void main() { + fragColor = color; +} +""" + + private const val COVER_FRAGMENT = """#version 300 es +precision mediump float; +flat in vec4 vColor; +out vec4 fragColor; +void main() { + fragColor = vColor; +} +""" + } + } +} diff --git a/src/main/java/com/mapconductor/core/shapetile/GlShapeScene.kt b/src/main/java/com/mapconductor/core/shapetile/GlShapeScene.kt new file mode 100644 index 0000000..bac3eb7 --- /dev/null +++ b/src/main/java/com/mapconductor/core/shapetile/GlShapeScene.kt @@ -0,0 +1,453 @@ +package com.mapconductor.core.shapetile + +import com.mapconductor.core.InternalMapConductorApi +import kotlin.math.PI +import kotlin.math.ceil +import kotlin.math.cos +import kotlin.math.floor +import kotlin.math.max +import kotlin.math.min +import kotlin.math.sin +import kotlin.math.sqrt + +/** + * What one shape tile draws on the GPU ([GlShapeRasterizer]): plain float + * arrays, built here without touching GL so the building can be tested on the JVM. + * The counterpart of ios-sdk-core's `MetalPathRasterizer.Scene` and + * `PathSceneBuilder`; coordinates are tile pixels, y down. + * + * Three kinds of content, drawn as the CPU path ([android.graphics.Canvas]) draws them: + * - **Circles** (and one-pixel dots): one instance each, fill and outline from + * the distance to the centre. + * - **Polygons**: a nonzero fill by stencil-then-cover (a fan of triangles from + * one point per ring, +1 front / -1 back), then the outline as segment quads + * plus vertex discs written to the stencil and covered once -- a translucent + * outline is not painted twice where its joins overlap, as with `drawPath`. + * - **Polylines**: each its own depth, nearer for each later line, drawn with + * `GL_LESS`: a line's own overlapping triangles are at one depth and paint + * once, a later line passes over an earlier one, and all of them are one draw + * call. (ios-sdk-core does this with stencil numbers written by the fragment + * shader; GLES cannot write the stencil from a shader.) + * + * Shapes are drawn in the order added. Circles of a circle tile go in one + * instanced draw ([orderedCircles]), which keeps their order by itself; other + * shapes are grouped into batches of shapes whose boxes do not overlap + * (checked on an 8 px grid), where order does not show. + */ +@InternalMapConductorApi +internal class GlShapeScene( + val pixels: Int, + /** + * Circles go into one ordered list and nothing else may be added: a + * circle tile is mostly overlapping circles, which would split batches + * into thousands. [addPolygon] then throws [NeedsBatches]. + */ + val orderedCircles: Boolean = false, +) { + /** Thrown by [addPolygon] in [orderedCircles] mode: draw the tile again batched. */ + class NeedsBatches : RuntimeException(null, null, false, false) + + /** A growable float array. */ + class Floats(capacity: Int = 1024) { + var data = FloatArray(capacity) + private set + var size = 0 + private set + + fun add(value: Float) { + if (size == data.size) data = data.copyOf(max(16, data.size * 2)) + data[size++] = value + } + + fun clear() { + size = 0 + } + + fun add2( + a: Float, + b: Float, + ) { + if (size + 2 > data.size) data = data.copyOf(max(16, data.size * 2)) + data[size++] = a + data[size++] = b + } + } + + /** Ranges of each array that a batch draws, as `[start, end)` element indices. */ + class Batch( + val fill: IntRange, + val fillCovers: IntRange, + val circles: IntRange, + val stroke: IntRange, + val strokeCovers: IntRange, + ) + + /** Fill fans: `x, y` per vertex, triangles. */ + val fill = Floats() + + /** Polygon outlines: `x, y` per vertex, triangles. */ + val stroke = Floats() + + /** `left, top, right, bottom, r, g, b, a` (premultiplied) per cover. */ + val fillCovers = Floats(256) + val strokeCovers = Floats(256) + + /** + * [CIRCLE_FLOATS] per circle: `x, y, radius, strokeWidth, fill rgba, + * stroke rgba, kind` -- kind 0 a circle, 1 a rectangle at `x, y` of + * `radius` by `strokeWidth` (a pixel-sized dot). + */ + val circles = Floats() + val batches = ArrayList() + + /** Polyline triangles: `x, y, depth` per vertex. */ + val lineVertices = Floats() + + /** Per polyline vertex: premultiplied RGBA bytes, `r` lowest. */ + val lineColors = IntList() + + /** Polylines recorded. */ + var lineCount = 0 + private set + + private val lineScratch = Floats() + + val isEmpty: Boolean + get() = fillCovers.size == 0 && strokeCovers.size == 0 && circles.size == 0 && lineVertices.size == 0 + + private val cell = 8 + private val cells = (pixels + cell - 1) / cell + private val occupied = BooleanArray(cells * cells) + private val touched = IntList() + private var fillStart = 0 + private var fillCoverStart = 0 + private var circleStart = 0 + private var strokeStart = 0 + private var strokeCoverStart = 0 + + /** A circle (kind 0) or a pixel rectangle (kind 1); see [circles]. */ + fun addCircle( + x: Float, + y: Float, + radius: Float, + strokeWidth: Float, + fill: Int, + stroke: Int, + kind: Int, + ) { + if (!orderedCircles) { + val box = + if (kind == 1) { + floatArrayOf(x, y, x + radius, y + strokeWidth) + } else { + val reach = radius + strokeWidth / 2f + 1f + floatArrayOf(x - reach, y - reach, x + reach, y + reach) + } + if (!claim(box)) return + } + circles.add2(x, y) + circles.add2(radius, strokeWidth) + addColor(circles, fill) + addColor(circles, stroke) + circles.add(kind.toFloat()) + } + + /** + * A polygon: [rings] in tile pixels (`x, y, ...`, the outline first) filled + * nonzero with [fill], and the outline drawn [strokeWidth] wide with + * [outline]; a colour of 0 is not drawn. + */ + fun addPolygon( + rings: List, + fill: Int, + outline: Int, + strokeWidth: Float, + ) { + if (orderedCircles) throw NeedsBatches() + val first = rings.firstOrNull() ?: return + if (first.size < 6) return + val bounds = bounds(first) + val box = if (outline != 0) inflate(bounds, strokeWidth / 2f) else bounds + if (!claim(inflate(box, 1f))) return + if (fill != 0) { + val px = first[0] + val py = first[1] + for (ring in rings) { + val n = ring.size / 2 + if (n < 3) continue + for (i in 0 until n) { + val j = (i + 1) % n + this.fill.add2(px, py) + this.fill.add2(ring[i * 2], ring[i * 2 + 1]) + this.fill.add2(ring[j * 2], ring[j * 2 + 1]) + } + } + addCover(fillCovers, inflate(bounds, 1f), fill) + } + if (outline != 0) { + strokeGeometry(first, closed = true, halfWidth = strokeWidth / 2f, out = stroke) + addCover(strokeCovers, inflate(box, 1f), outline) + } + } + + /** A polyline in tile pixels (`x, y, ...`, two points or more). */ + fun addLine( + points: FloatArray, + color: Int, + strokeWidth: Float, + ) { + if (points.size < 4) return + lineScratch.clear() + strokeGeometry(points, closed = false, halfWidth = strokeWidth / 2f, out = lineScratch) + endLine(color) + } + + /** A polyline that collapsed to one point: a disc of [radius] round [x], [y]. */ + fun addLineDot( + x: Float, + y: Float, + radius: Float, + color: Int, + ) { + lineScratch.clear() + disc(x, y, radius, lineScratch) + endLine(color) + } + + /** Closes the last batch; the scene is complete. */ + fun finish(): GlShapeScene { + close() + return this + } + + /** Moves the line in [lineScratch] into [lineVertices] at the next depth. */ + private fun endLine(color: Int) { + val count = lineScratch.size / 2 + if (count == 0) return + lineCount++ + // Nearer for each later line; 2^-22 apart is several steps of a 24-bit depth. + val depth = 1f - lineCount / 4_194_304f + val packed = packColor(color) + val points = lineScratch.data + for (v in 0 until count) { + lineVertices.add2(points[v * 2], points[v * 2 + 1]) + lineVertices.add(depth) + lineColors.add(packed) + } + } + + private fun close() { + val batch = + Batch( + fill = fillStart / 2 until fill.size / 2, + fillCovers = fillCoverStart / COVER_FLOATS until fillCovers.size / COVER_FLOATS, + circles = circleStart / CIRCLE_FLOATS until circles.size / CIRCLE_FLOATS, + stroke = strokeStart / 2 until stroke.size / 2, + strokeCovers = strokeCoverStart / COVER_FLOATS until strokeCovers.size / COVER_FLOATS, + ) + if (!batch.fillCovers.isEmpty() || !batch.circles.isEmpty() || !batch.strokeCovers.isEmpty()) batches.add(batch) + for (k in 0 until touched.size) occupied[touched[k]] = false + touched.clear() + fillStart = fill.size + fillCoverStart = fillCovers.size + circleStart = circles.size + strokeStart = stroke.size + strokeCoverStart = strokeCovers.size + } + + /** + * Takes the cells of [box] (left, top, right, bottom) for the current batch, + * closing it first when one of them is taken. False when the box is off the tile. + */ + private fun claim(box: FloatArray): Boolean { + if (box[2] < 0f || box[3] < 0f || box[0] > pixels || box[1] > pixels) return false + val x0 = max(0, floor(box[0]).toInt() / cell) + val y0 = max(0, floor(box[1]).toInt() / cell) + val x1 = min(cells - 1, ceil(box[2]).toInt() / cell) + val y1 = min(cells - 1, ceil(box[3]).toInt() / cell) + if (x0 > x1 || y0 > y1) return false + var overlaps = false + loop@ for (y in y0..y1) { + for (x in x0..x1) { + if (occupied[y * cells + x]) { + overlaps = true + break@loop + } + } + } + if (overlaps) close() + for (y in y0..y1) { + for (x in x0..x1) { + val i = y * cells + x + if (!occupied[i]) { + occupied[i] = true + touched.add(i) + } + } + } + return true + } + + private fun addCover( + to: Floats, + box: FloatArray, + color: Int, + ) { + to.add2(box[0], box[1]) + to.add2(box[2], box[3]) + addColor(to, color) + } + + @InternalMapConductorApi + companion object { + const val COVER_FLOATS = 8 + const val CIRCLE_FLOATS = 13 + + /** Premultiplied RGBA bytes of an ARGB colour, `r` in the lowest byte. */ + fun packColor(argb: Int): Int { + val a = argb ushr 24 + fun channel(shift: Int) = (((argb shr shift) and 0xFF) * a + 127) / 255 + return channel(16) or (channel(8) shl 8) or (channel(0) shl 16) or (a shl 24) + } + + /** Premultiplied `r, g, b, a` of an ARGB colour. */ + fun addColor( + to: Floats, + argb: Int, + ) { + val a = (argb ushr 24) / 255f + to.add2(((argb shr 16) and 0xFF) / 255f * a, ((argb shr 8) and 0xFF) / 255f * a) + to.add2((argb and 0xFF) / 255f * a, a) + } + + private fun bounds(points: FloatArray): FloatArray { + val box = floatArrayOf(Float.MAX_VALUE, Float.MAX_VALUE, -Float.MAX_VALUE, -Float.MAX_VALUE) + var i = 0 + while (i < points.size) { + box[0] = min(box[0], points[i]) + box[2] = max(box[2], points[i]) + box[1] = min(box[1], points[i + 1]) + box[3] = max(box[3], points[i + 1]) + i += 2 + } + return box + } + + private fun inflate( + box: FloatArray, + d: Float, + ) = floatArrayOf(box[0] - d, box[1] - d, box[2] + d, box[3] + d) + + /** Unit circles (cos, sin pairs) for 6..32 segments, made once. */ + private val unitCircles: Array = + Array(33) { segments -> + if (segments < 6) { + FloatArray(0) + } else { + FloatArray(segments * 2) { i -> + val a = (i / 2).toDouble() / segments * 2 * PI + (if (i % 2 == 0) cos(a) else sin(a)).toFloat() + } + } + } + + /** Segments of a disc of [radius] px: enough for its round edge to look round. */ + private fun circleSegments(radius: Float): Int = (ceil(radius * 2f).toInt() + 4).coerceIn(6, 32) + + /** + * A quad per segment and a disc per vertex (round joins and caps), as + * triangles. They overlap; the stencil takes their union. + */ + fun strokeGeometry( + points: FloatArray, + closed: Boolean, + halfWidth: Float, + out: Floats, + ) { + val n = points.size / 2 + if (n < 2 || halfWidth <= 0f) return + val edges = if (closed) n else n - 1 + for (i in 0 until edges) { + val j = (i + 1) % n + val ax = points[i * 2] + val ay = points[i * 2 + 1] + val bx = points[j * 2] + val by = points[j * 2 + 1] + val dx = bx - ax + val dy = by - ay + val length = sqrt(dx * dx + dy * dy) + if (length <= 0f) continue + val nx = -dy / length * halfWidth + val ny = dx / length * halfWidth + out.add2(ax + nx, ay + ny) + out.add2(ax - nx, ay - ny) + out.add2(bx + nx, by + ny) + out.add2(ax - nx, ay - ny) + out.add2(bx - nx, by - ny) + out.add2(bx + nx, by + ny) + } + for (i in 0 until n) disc(points[i * 2], points[i * 2 + 1], halfWidth, out) + } + + /** A disc of [radius] round [cx], [cy], as a fan of triangles. */ + fun disc( + cx: Float, + cy: Float, + radius: Float, + out: Floats, + ) { + val unit = unitCircles[circleSegments(radius)] + val segments = unit.size / 2 + var px = cx + unit[(segments - 1) * 2] * radius + var py = cy + unit[(segments - 1) * 2 + 1] * radius + for (k in 0 until segments) { + val nx = cx + unit[k * 2] * radius + val ny = cy + unit[k * 2 + 1] * radius + out.add2(cx, cy) + out.add2(px, py) + out.add2(nx, ny) + px = nx + py = ny + } + } + } +} + +/** A growable int array. */ +@InternalMapConductorApi +internal class IntList( + capacity: Int = 256, +) { + var data = IntArray(capacity) + private set + var size = 0 + private set + + operator fun get(index: Int): Int = data[index] + + fun add(value: Int) { + if (size == data.size) data = data.copyOf(max(16, data.size * 2)) + data[size++] = value + } + + fun clear() { + size = 0 + } +} + +/** World-pixel points (`x, y, ...`) as tile pixels: [originX], [originY] at 0, [r] pixels per unit. */ +internal fun toTilePixels( + points: DoubleArray, + originX: Double, + originY: Double, + r: Float, +): FloatArray { + val out = FloatArray(points.size) + var i = 0 + while (i < points.size) { + out[i] = ((points[i] - originX) * r).toFloat() + out[i + 1] = ((points[i + 1] - originY) * r).toFloat() + i += 2 + } + return out +} diff --git a/src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt b/src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt index 0a4957f..c0daefc 100644 --- a/src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt +++ b/src/main/java/com/mapconductor/core/shapetile/ShapeTileRenderer.kt @@ -8,6 +8,7 @@ import com.mapconductor.core.tileserver.TilePngEncoder import com.mapconductor.core.tileserver.TileProviderInterface import com.mapconductor.core.tileserver.TileRequest import java.io.ByteArrayOutputStream +import java.nio.ByteBuffer import java.util.BitSet import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.atomic.AtomicReferenceArray @@ -98,6 +99,41 @@ internal class ShapeTileRenderer( r: Float, paints: Paints, ): Boolean + + /** Whether [record] can put this kind of shape in a [GlShapeScene]. */ + val drawsOnGpu: Boolean get() = false + + /** + * Records [shape] into [scene] for the GPU, exactly as [draw] would draw + * it on the canvas. True if anything was recorded. + */ + fun record( + scene: GlShapeScene, + item: T, + shape: Any, + originX: Double, + originY: Double, + r: Float, + ): Boolean = throw UnsupportedOperationException() + + /** + * Records every shape of [candidates] at once, reading [storage] + * directly instead of an item per shape; false when this painter or + * storage has no such path (then the renderer records them one by one). + * Must make the same choices as the one-by-one path. + */ + fun recordAll( + scene: GlShapeScene, + storage: ShapeTileStorage, + candidates: IntArray, + active: BitSet?, + scale: Double, + originX: Double, + originY: Double, + tileSize: Int, + r: Float, + checkCancelled: () -> Unit, + ): Boolean = false } /** Paints and a path, one set per tile drawn. */ @@ -306,12 +342,18 @@ internal class ShapeTileRenderer( val key = cacheKey(z, x, y, ratio) synchronized(cacheLock) { tileCache.get(key) }?.let { return it } - val bitmap = draw(m, x, y, z, ratio, isCancelled) ?: return null val bytes = - try { - encode(bitmap) - } finally { - releaseScratch(bitmap) + when (val gpu = drawOnGpu(m, x, y, z, ratio, isCancelled)) { + Gpu.Empty -> return null + is Gpu.Drawn -> encode(gpu.pixels, gpu.size) + Gpu.Unavailable -> { + val bitmap = draw(m, x, y, z, ratio, isCancelled) ?: return null + try { + encode(bitmap) + } finally { + releaseScratch(bitmap) + } + } } synchronized(cacheLock) { // The shapes may have changed while this tile was drawn. @@ -332,6 +374,12 @@ internal class ShapeTileRenderer( ): Bitmap? { val current = acquireModel() ?: return null return try { + when (val gpu = drawOnGpu(current, x, y, z, pixelRatio) { false }) { + Gpu.Empty -> return null + is Gpu.Drawn -> + return createBitmap(gpu.size, gpu.size).apply { copyPixelsFromBuffer(gpu.pixels) } + Gpu.Unavailable -> Unit + } draw(current, x, y, z, pixelRatio) { false }?.let { scratch -> try { scratch.copy(Bitmap.Config.ARGB_8888, false) @@ -344,29 +392,129 @@ internal class ShapeTileRenderer( } } - private fun draw( + /** What [drawOnGpu] did. */ + private sealed class Gpu { + /** Premultiplied RGBA, [size] pixels square, top row first. */ + class Drawn( + val pixels: ByteBuffer, + val size: Int, + ) : Gpu() + + /** Nothing of any shape is in the tile. */ + object Empty : Gpu() + + /** No GPU drawing here (or this kind of shape): draw on the canvas. */ + object Unavailable : Gpu() + } + + /** + * The tile drawn by [GlShapeRasterizer]: the same shapes as [drawInto], in + * the same order, recorded by the painter instead of drawn on a canvas. + */ + private fun drawOnGpu( m: Model, x: Int, y: Int, z: Int, pixelRatio: Int, isCancelled: () -> Boolean, - ): Bitmap? { + ): Gpu { + if (!useGpu || !painter.drawsOnGpu) return Gpu.Unavailable val scale = tileSize.toDouble() * (1L shl z).toDouble() val originX = x.toDouble() * tileSize val originY = y.toDouble() * tileSize + val candidates = candidatesFor(m, originX, originY, scale) + if (candidates.isEmpty()) return Gpu.Empty + val r = pixelScale() * pixelRatio + val px = (tileSize * r).roundToInt().coerceAtLeast(1) + // A circle tile is one ordered list of circles; a circle drawn as its + // 128-gon needs the batched scene instead. + val scene = + try { + val ordered = GlShapeScene(px, orderedCircles = painter.thinSubpixelOverlaps) + val checkCancelled = { if (isCancelled()) throw cancelled() } + if (painter.recordAll(ordered, m.storage, candidates, m.active, scale, originX, originY, tileSize, r, checkCancelled)) { + ordered.finish() + } else { + record(ordered, m, candidates, originX, originY, r, z, scale, isCancelled) + } + } catch (_: GlShapeScene.NeedsBatches) { + record(GlShapeScene(px), m, candidates, originX, originY, r, z, scale, isCancelled) + } + if (scene.isEmpty) return Gpu.Empty + if (isCancelled()) throw cancelled() + val pixels = GlShapeRasterizer.render(scene) ?: return Gpu.Unavailable + return Gpu.Drawn(pixels, px) + } + + /** [drawInto]'s loop, recording into [scene]. */ + private fun record( + scene: GlShapeScene, + m: Model, + candidates: IntArray, + originX: Double, + originY: Double, + r: Float, + z: Int, + scale: Double, + isCancelled: () -> Boolean, + ): GlShapeScene { + val perZoom = perZoomOf(m, z) + val lastDot = IntArray(if (painter.thinSubpixelOverlaps) tileSize * tileSize else 0) + if (painter.thinSubpixelOverlaps) { + for ((k, index) in candidates.withIndex()) { + if (k % CANCEL_CHECK_EVERY == 0 && isCancelled()) throw cancelled() + if (m.active != null && !m.active[index]) continue + val item = m.storage[index] ?: continue + if (!painter.isVisible(item)) continue + val slot = painter.subpixelSlot(item, scale, originX, originY, tileSize) + if (slot >= 0) lastDot[slot] = index + 1 + } + } + for ((k, index) in candidates.withIndex()) { + if (k % CANCEL_CHECK_EVERY == 0 && isCancelled()) throw cancelled() + if (m.active != null && !m.active[index]) continue + val item = m.storage[index] ?: continue + if (painter.thinSubpixelOverlaps) { + val slot = painter.subpixelSlot(item, scale, originX, originY, tileSize) + if (slot == -1 || (slot >= 0 && lastDot[slot] != index + 1)) continue + } + painter.record(scene, item, simplifiedAt(perZoom, index, item, scale), originX, originY, r) + } + return scene.finish() + } + + private fun candidatesFor( + m: Model, + originX: Double, + originY: Double, + scale: Double, + ): IntArray { // A stroke reaches half its width past the shape: take the shapes that // close to the tile too, and draw them whole (the canvas cuts them), so // the tile next door draws the same line at the same place. val pad = m.maxStroke / 2.0 + 1.0 - val candidates = - m.index.query( - { index -> if (m.active?.get(index) == false) null else m.storage.bbox(index) }, - (originX - pad) / scale, - (originY - pad) / scale, - (originX + tileSize + pad) / scale, - (originY + tileSize + pad) / scale, - ) + return m.index.query( + { index -> if (m.active?.get(index) == false) null else m.storage.bbox(index) }, + (originX - pad) / scale, + (originY - pad) / scale, + (originX + tileSize + pad) / scale, + (originY + tileSize + pad) / scale, + ) + } + + private fun draw( + m: Model, + x: Int, + y: Int, + z: Int, + pixelRatio: Int, + isCancelled: () -> Boolean, + ): Bitmap? { + val scale = tileSize.toDouble() * (1L shl z).toDouble() + val originX = x.toDouble() * tileSize + val originY = y.toDouble() * tileSize + val candidates = candidatesFor(m, originX, originY, scale) if (candidates.isEmpty()) return null val r = pixelScale() * pixelRatio @@ -474,6 +622,22 @@ internal class ShapeTileRenderer( scratchBitmaps.release(bitmap) } + /** The GPU's pixels as PNG: Rust when it can, else through a bitmap. */ + private fun encode( + pixels: ByteBuffer, + size: Int, + ): ByteArray { + TilePngEncoder.encode(pixels, size, size, premultiplied = true)?.let { return it } + pixels.rewind() + val bitmap = scratch(size) + try { + bitmap.copyPixelsFromBuffer(pixels) + return encode(bitmap) + } finally { + releaseScratch(bitmap) + } + } + private fun encode(bitmap: Bitmap): ByteArray { // Rust first, as for marker tiles: Bitmap.compress is several times slower. TilePngEncoder.encode(bitmap)?.let { return it } @@ -519,10 +683,39 @@ internal class ShapeTileRenderer( @InternalMapConductorApi class TileRenderCancelled : Exception("shape tile no longer wanted") + init { + if (useGpu && painter.drawsOnGpu) prepareGpu + } + @InternalMapConductorApi companion object { const val DEFAULT_CACHE_BYTES = 16 * 1024 * 1024 + /** + * Draw shape tiles on the GPU ([GlShapeRasterizer]) when it can. False + * draws every tile on the canvas, to compare the two. + */ + @Volatile + var useGpu: Boolean = true + + /** + * Polygons and polylines on the GPU too. Off: on a Pixel 5a and a + * Lenovo TB520FU, Skia drew their tiles as fast or faster (a polygon tile + * splits into thousands of small batches), while circles were 2x faster + * on the GPU. Kept for the comparison tests and for later work. + */ + @Volatile + var gpuPolygonsAndLines: Boolean = false + + /** + * The GL contexts and shaders are made on first use. Start that when + * shapes are about to be tiled, instead of making the first tiles the + * map asks for wait for it. + */ + private val prepareGpu: Unit by lazy { + Thread({ GlShapeRasterizer.prepare() }, "mc-shape-gpu-prepare").apply { isDaemon = true }.start() + } + /** Simplified copies kept, most recent zooms first. */ private const val ZOOMS_KEPT = 4 private const val MAX_ZOOM = 26