Refactor texture cache usage to shared Kotlin for iOS

This commit is contained in:
James Babcock committed 2026-09-20 15:12:13 -07:00
1 parent ccb320c66f
commit 07828db0bf
23 files changed
+552 -74

No files matched your search

@@ -0,0 +1,99 @@
package com.faceclaw.app
import kotlin.test.*
class TextureCacheTest {
private fun u32(bytes: ByteArray, offset: Int): Int =
(0..3).fold(0) { n, i -> n or ((bytes[offset + i].toInt() and 255) shl (i * 8)) }
@Test
fun offsetsAbove64KiBAndLazyFontSlotsRoundTripOnBothPlatforms() {
val cache = TextureCacheState()
val large = ImageAtlas.Entry(255, 255, ByteArray(255 * 255) { (it % 15 + 1).toByte() })
repeat(3) { assertTrue(cache.ensureImage(it, large) >= 0) }
val glyph = GlyphAtlas.Glyph(1, 1, 0, 1, 0, null, byteArrayOf(15))
val offset = cache.ensureGlyph(42, 65, glyph)
val table = cache.fontTableOffset(42)
assertTrue(table > 65535)
assertEquals(table + 384, offset)
val memory = ByteArray(TextureCacheState.CACHE_SIZE)
for (upload in cache.drainUploadPayloads(3600)) {
assertEquals(18, upload[0].toInt())
var pos = 1
while (pos < upload.size) {
val address = u32(upload, pos)
val length = (upload[pos + 4].toInt() and 255) or ((upload[pos + 5].toInt() and 255) shl 8)
upload.copyInto(memory, address, pos + 6, pos + 6 + length)
pos += 6 + length
}
assertEquals(upload.size, pos)
}
assertEquals(offset, u32(memory, table + (65 - 32) * 4))
assertContentEquals(glyph.cachedBytes, memory.copyOfRange(offset, offset + glyph.cachedBytes.size))
assertEquals(offset, cache.ensureGlyph(42, 65, glyph))
assertFalse(cache.hasPendingUploads())
cache.reset()
assertEquals(0, cache.usedBytes())
assertEquals(384, cache.ensureGlyph(42, 65, glyph))
assertTrue(cache.hasPendingUploads())
}
@Test
fun plannerReusesResidencyAndReuploadsAfterReset() {
val cache = TextureCacheState()
val id = ImageAtlas.ensure("texture-test-white", 2, 2, ArrayByteReader(ByteArray(4) { -1 }))
val pixels = ByteArray(8 * 4)
for (y in 0..1) for (x in 0..1) pixels[y * 8 + x] = -1
val packed = BmpUtil.pack4bppFromGray8(pixels, 8, 4)
val draws = arrayOf(SurfaceCompositor.ScreenDraw.image(id, 0, 0))
fun plan() = assertNotNull(TexturePlanner.plan(null, packed, 8, 4, draws, cache, 1, true, 8, testPlatform()))
val cold = plan()
assertEquals(1, cold.drawnImages)
assertTrue(cold.uploads.isNotEmpty())
val warm = plan()
assertTrue(warm.uploads.isEmpty())
assertContentEquals(cold.payload, warm.payload)
cache.reset()
assertTrue(plan().uploads.isNotEmpty())
// Occlusion fails the correctness check and leaves the image baked.
assertNull(TexturePlanner.plan(null, ByteArray(packed.size), 8, 4, draws, cache, 1, true, 8, testPlatform()))
}
@Test
fun fullCacheResetsAndRetriesTheCurrentFrame() {
val cache = TextureCacheState()
val large = ImageAtlas.Entry(255, 255, ByteArray(255 * 255) { (it % 15 + 1).toByte() })
var fillerId = -1
while (cache.ensureImage(fillerId--, large) >= 0) {}
// Fill the remaining tail, forcing the next real allocation to fail.
val remaining = TextureCacheState.CACHE_SIZE - cache.usedBytes()
while (cache.ensureImage(fillerId--, ImageAtlas.Entry(1, 1, byteArrayOf(15))) >= 0) {}
assertTrue(remaining > 0)
val generation = cache.generation()
val id = ImageAtlas.ensure("texture-test-full", 2, 2, ArrayByteReader(ByteArray(4) { -1 }))
val result = assertNotNull(TexturePlanner.plan(null, ByteArray(4) { -1 }, 4, 2,
arrayOf(SurfaceCompositor.ScreenDraw.image(id, 0, 0)), cache, 1, true, 8, testPlatform()))
assertEquals(generation + 1, cache.generation())
assertEquals(1, result.drawnImages)
assertTrue(cache.usedBytes() < 100)
assertTrue(result.uploads.isNotEmpty())
}
@Test
fun workingSetLargerThanTheCacheFallsBackWithoutAResetLoop() {
val cache = TextureCacheState()
val raster = ByteArray(255 * 255) { if (it % 2 == 0) 16 else 32 }
val draws = (0..4).map {
val id = ImageAtlas.ensure("oversized-working-set-$it", 255, 255, ArrayByteReader(raster))
SurfaceCompositor.ScreenDraw.image(id, 0, 0)
}.toTypedArray()
val gray = ByteArray(256 * 256)
repeat(255) { raster.copyInto(gray, it * 256, it * 255, (it + 1) * 255) }
val result = assertNotNull(TexturePlanner.plan(null,
BmpUtil.pack4bppFromGray8(gray, 256, 256), 256, 256, draws, cache, 1, true, 8, testPlatform()))
assertEquals(1, cache.generation())
assertEquals(4, result.drawnImages)
assertEquals(1, result.bakedCandidates)
assertTrue(cache.usedBytes() <= TextureCacheState.CACHE_SIZE)
}
}
@@ -0,0 +1,86 @@
package com.faceclaw.app
import kotlin.test.*
class IosTextureApiTest {
private fun imageDraw(id: Int): ByteArray = byteArrayOf(
1, id.toByte(), (id shr 8).toByte(), (id shr 16).toByte(), (id shr 24).toByte(), 0, 0, 0, 0,
)
@Test
fun nativeSnapshotsPreserveDrawsAcrossCoalescingAndSessionResets() {
val atlas = IosTextureAtlas()
val white = ByteArray(4) { -1 }.data()
val id = atlas.imageId("ios-texture-api-white", 2, 2, white)
assertEquals(id, IosTextureAtlas().imageId("ios-texture-api-white", 2, 2, white))
val compositor = IosSurfaceCompositor(8, 4)
compositor.configure("app", 2, 0, 2, 2, 0, false)
compositor.submitDraws("app", white, 0, 0, 2, 2, imageDraw(id).data())
val frame = compositor.compositeFrame()
assertEquals(2, frame.composite.draws!!.single().x)
// A later update must not change the previously submitted snapshot.
compositor.submit("app", ByteArray(4).data(), 0, 0, 2, 2)
assertTrue(compositor.compositeFrame().composite.draws!!.isEmpty())
val next = IosProtocol().pack(frame.pixels, 8, 4)
val planner = IosTexturePlanner()
val cold = assertNotNull(planner.plan(null, next, frame, 1))
assertEquals(18, cold.uploads.first().byteArray()[0].toInt())
assertEquals(8, cold.payload.byteArray()[0].toInt())
val warm = assertNotNull(planner.plan(null, next, frame, 1))
assertTrue(warm.uploads.isEmpty())
assertContentEquals(cold.payload.byteArray(), warm.payload.byteArray())
planner.reset()
assertTrue(assertNotNull(planner.plan(null, next, frame, 1)).uploads.isNotEmpty())
// A partial update clears identities even if the caller supplies stale draws.
compositor.submitDraws("app", byteArrayOf(-1).data(), 0, 0, 1, 1, imageDraw(id).data())
assertTrue(compositor.compositeFrame().composite.draws!!.isEmpty())
}
@Test
fun glyphRegistrationAndDrawingUseTheSharedAsciiTable() {
val atlas = IosTextureAtlas()
val key = "ios-aa-texture-test"
val bytes = key.encodeToByteArray()
atlas.registerAaGlyphs((byteArrayOf(bytes.size.toByte()) + bytes +
byteArrayOf(2, 1, 0, 65, 0, 0, 0, 0, 0, 2, 2, -1, -1)).data())
val font = atlas.fontId(key)
val draws = byteArrayOf(0, font.toByte(), (font shr 8).toByte(), 65, 0, 0, 0, 0, 0, 0, 0, -1)
val compositor = IosSurfaceCompositor(4, 2)
compositor.configure("text", 0, 0, 2, 2, 0, false)
compositor.submitDraws("text", ByteArray(4) { -1 }.data(), 0, 0, 2, 2, draws.data())
val frame = compositor.compositeFrame()
val plan = assertNotNull(IosTexturePlanner().plan(null, IosProtocol().pack(frame.pixels, 4, 2), frame, 1))
assertTrue(plan.usedBytes > 384)
val payload = plan.payload.byteArray()
// The last mode-8 submessage is the shared mode-20 cached glyph run.
assertEquals(20, payload[payload.size - 12].toInt())
assertEquals(65, payload.last().toInt())
}
@Test
fun oversizedPlanDiscardsUnsentResidencyBeforePixelFallback() {
val atlas = IosTextureAtlas()
val white = ByteArray(4) { -1 }.data()
val id = atlas.imageId("ios-oversized-plan", 2, 2, white)
var random = 12345
val gray = ByteArray(640 * 480) {
random = random xor (random shl 13)
random = random xor (random ushr 17)
random = random xor (random shl 5)
random.toByte()
}
gray[0] = -1; gray[1] = -1; gray[640] = -1; gray[641] = -1
val compositor = IosSurfaceCompositor(640, 480)
compositor.configure("noise", 0, 0, 640, 480, 0, false)
compositor.submitDraws("noise", gray.data(), 0, 0, 640, 480, imageDraw(id).data())
val frame = compositor.compositeFrame()
val planner = IosTexturePlanner()
assertNull(planner.plan(null, IosProtocol().pack(frame.pixels, 640, 480), frame, 1))
val small = IosSurfaceCompositor(4, 2)
small.configure("icon", 0, 0, 2, 2, 0, false)
small.submitDraws("icon", white, 0, 0, 2, 2, imageDraw(id).data())
val next = small.compositeFrame()
val retry = assertNotNull(planner.plan(null, IosProtocol().pack(next.pixels, 4, 2), next, 1))
assertTrue(retry.uploads.isNotEmpty(), "the rejected plan's image was never uploaded")
}
}