forked from Blendman974/faceclaw
Resource management for the texture cache
This commit is contained in:
1 parent
146fe66bdd
commit
0c3d7517bc
28 files changed
+569
-524
No files matched your search
+30
-6
@@ -306,14 +306,14 @@ const ack = (transport, sent, overrides) => transport.ack(sent.id, sent.message,
|
||||
const tick = () => new Promise(resolve => setImmediate(resolve));
|
||||
const noisy = () => Uint8Array.from({ length: 640 * 480 }, (_, i) => (i % 2) * 255);
|
||||
|
||||
test('texture uploads precede their frame, share ACK accounting, and coalesce pixels with draw identities', async t => {
|
||||
test('resource evictions and uploads precede their frame, share ACK accounting, and coalesce pixels with draw identities', async t => {
|
||||
const plans = [];
|
||||
let resets = 0;
|
||||
const textures = {
|
||||
reset() { resets++; },
|
||||
plan(previous, next, frame, firstId) {
|
||||
plans.push({ previous, next, frame, firstId });
|
||||
return { uploads: plans.length === 1 ? [new Uint8Array([18, 1]), new Uint8Array([18, 2])] : [],
|
||||
return { resourceCommands: plans.length === 1 ? [new Uint8Array([22, 1]), new Uint8Array([21, 2])] : [],
|
||||
payload: new Uint8Array([8, plans.length]), nextFid: firstId + 2, usedBytes: 100 };
|
||||
},
|
||||
};
|
||||
@@ -323,7 +323,7 @@ test('texture uploads precede their frame, share ACK accounting, and coalesce pi
|
||||
h.session.setFrame(new Uint8Array(640 * 480), a);
|
||||
await until(() => imageMessages(h.transport).length === 3);
|
||||
const sent = imageMessages(h.transport);
|
||||
assert.deepEqual(sent.map(s => s.message.payload[0]), [18, 18, 8]);
|
||||
assert.deepEqual(sent.map(s => s.message.payload[0]), [22, 21, 8]);
|
||||
h.session.setFrame(new Uint8Array(640 * 480).fill(80), b);
|
||||
h.session.setFrame(new Uint8Array(640 * 480).fill(160), c);
|
||||
assert.equal(plans.length, 1, 'coalesced frames must not allocate textures');
|
||||
@@ -340,15 +340,39 @@ test('texture uploads precede their frame, share ACK accounting, and coalesce pi
|
||||
assert.ok(resets > 0);
|
||||
});
|
||||
|
||||
test('a failed texture upload invalidates residency and the pipelined frame base', async t => {
|
||||
test('evictions drain earlier replayable draws before resource IDs can be reused', async t => {
|
||||
let plans = 0;
|
||||
const textures = {
|
||||
reset() {},
|
||||
plan() { return { resourceCommands: ++plans === 1 ? [] : [new Uint8Array([22]), new Uint8Array([21])],
|
||||
payload: new Uint8Array([8]), nextFid: 1, usedBytes: 100 }; },
|
||||
};
|
||||
const h = harness(t, new FakeTransport(), textures); await h.session.start(addresses);
|
||||
h.transport.hold = true;
|
||||
h.session.setFrame(new Uint8Array(640 * 480), { native: {} });
|
||||
await until(() => imageMessages(h.transport).length === 1);
|
||||
h.session.setFrame(new Uint8Array(640 * 480).fill(80), { native: {} });
|
||||
await until(() => plans === 2); await tick();
|
||||
assert.equal(imageMessages(h.transport).length, 1, 'eviction cannot enter an unresolved window');
|
||||
h.session.recoverCfw(); await tick();
|
||||
assert.equal(imageMessages(h.transport).length, 1);
|
||||
ack(h.transport, imageMessages(h.transport)[0]);
|
||||
await until(() => imageMessages(h.transport).length === 4);
|
||||
assert.deepEqual(imageMessages(h.transport).map(s => s.message.payload[0]), [8, 22, 21, 8]);
|
||||
h.transport.hold = false;
|
||||
for (const sent of imageMessages(h.transport).slice(1)) ack(h.transport, sent);
|
||||
await until(() => h.session.state.frames === 2);
|
||||
});
|
||||
|
||||
test('a failed resource upload invalidates residency and the pipelined frame base', async t => {
|
||||
let resets = 0;
|
||||
const textures = {
|
||||
reset() { resets++; },
|
||||
plan() { return { uploads: [new Uint8Array([18])], payload: new Uint8Array([8]), nextFid: 1, usedBytes: 4 }; },
|
||||
plan() { return { resourceCommands: [new Uint8Array([21])], payload: new Uint8Array([8]), nextFid: 1, usedBytes: 4 }; },
|
||||
};
|
||||
const h = harness(t, new FakeTransport(), textures); await h.session.start(addresses);
|
||||
const original = h.session.requestCfw.bind(h.session);
|
||||
h.session.requestCfw = (role, payload) => payload[0] === 18 ? Promise.reject(new Error('texture upload failed')) : original(role, payload);
|
||||
h.session.requestCfw = (role, payload) => payload[0] === 21 ? Promise.reject(new Error('texture upload failed')) : original(role, payload);
|
||||
h.session.setFrame(new Uint8Array(640 * 480), { native: {} });
|
||||
await until(() => h.session.state.phase === 'retrying');
|
||||
assert.equal(resets, 1); assert.equal(h.session.lastEnqueued, null);
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/** Android's Java communicator and iOS share the same resource API and wire commands. */
|
||||
public class ResourceCacheProtocolTest {
|
||||
public static void main(String[] args) {
|
||||
ResourceCacheState cache = new ResourceCacheState();
|
||||
CachedResource image = new CachedResource(new byte[]{1, 1, 31});
|
||||
assert cache.prepare(Collections.singletonList(image))[0] == 0;
|
||||
List<byte[]> commands = cache.drainCommands(3600);
|
||||
assert commands.size() == 2 && commands.get(0)[0] == 22;
|
||||
assert Arrays.equals(commands.get(1), new byte[]{21,1,0,0,0,3,0,0,0,0,0,3,0,1,1,31});
|
||||
assert cache.prepare(Collections.singletonList(image))[0] == 0;
|
||||
assert cache.drainCommands(3600).isEmpty();
|
||||
cache.reset();
|
||||
assert cache.prepare(Collections.singletonList(image))[0] == 0;
|
||||
assert cache.drainCommands(3600).get(0)[0] == 22;
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
|
||||
public class TextureCacheProtocolTest {
|
||||
static int u16(byte[] b, int p) { return (b[p]&255) | (b[p+1]&255)<<8; }
|
||||
static int u32(byte[] b, int p) { return u16(b,p) | u16(b,p+2)<<16; }
|
||||
public static void main(String[] args) throws Exception {
|
||||
TextureCacheState cache = new TextureCacheState();
|
||||
byte[] pixels = new byte[255*255];
|
||||
for (int i=0;i<pixels.length;i++) pixels[i]=(byte)(i%15+1);
|
||||
ImageAtlas.Entry big = new ImageAtlas.Entry(255,255,pixels);
|
||||
for (int i=0;i<3;i++) assert cache.ensureImage(i,big)>=0;
|
||||
ImageAtlas.Entry tiny = new ImageAtlas.Entry(1,1,new byte[]{15});
|
||||
int imageOffset = cache.ensureImage(100,tiny);
|
||||
assert imageOffset>65535;
|
||||
GlyphAtlas.Glyph glyph = new GlyphAtlas.Glyph(1,1,0,1,0,null,new byte[]{15});
|
||||
int glyphOffset = cache.ensureGlyph(42,'A',glyph);
|
||||
int table = cache.fontTableOffset(42);
|
||||
assert table>65535 && glyphOffset==table+96*4;
|
||||
byte[] memory = new byte[262144];
|
||||
for (byte[] upload: cache.drainUploadPayloads(3600)) {
|
||||
assert upload[0]==18;
|
||||
int pos=1;
|
||||
while (pos<upload.length) {
|
||||
int offset=u32(upload,pos), n=u16(upload,pos+4);
|
||||
System.arraycopy(upload,pos+6,memory,offset,n); pos+=6+n;
|
||||
}
|
||||
assert pos==upload.length;
|
||||
}
|
||||
assert memory[imageOffset]==1 && memory[imageOffset+2]==0x1f;
|
||||
assert u32(memory,table+('A'-32)*4)==glyphOffset;
|
||||
assert !cache.hasPendingUploads();
|
||||
// Exercise the production planner encoders with offsets above 64 KiB.
|
||||
Class<?> selected=Class.forName("com.faceclaw.app.TexturePlanner$Selected");
|
||||
Constructor<?> ctor=selected.getDeclaredConstructors()[0]; ctor.setAccessible(true);
|
||||
Object image=ctor.newInstance(SurfaceCompositor.ScreenDraw.image(100,321,123),null,tiny,321,15);
|
||||
Method encode=TexturePlanner.class.getDeclaredMethod("encodeImageDraw",TextureCacheState.class,selected);
|
||||
encode.setAccessible(true);
|
||||
byte[] draw=(byte[])encode.invoke(null,cache,image);
|
||||
assert draw.length==10 && draw[0]==19 && u32(draw,1)==imageOffset;
|
||||
assert u16(draw,5)==321 && u16(draw,7)==123 && draw[9]==31;
|
||||
Object letter=ctor.newInstance(SurfaceCompositor.ScreenDraw.glyph(42,'A',321,123,255),glyph,null,321,15);
|
||||
Method runs=TexturePlanner.class.getDeclaredMethod("buildRuns",TextureCacheState.class,List.class,List.class);
|
||||
runs.setAccessible(true); List<byte[]> out=new ArrayList<>();
|
||||
runs.invoke(null,cache,Collections.singletonList(letter),out);
|
||||
byte[] run=out.get(0);
|
||||
assert run.length==12 && run[0]==20 && u32(run,1)==table;
|
||||
assert u16(run,5)==321 && u16(run,7)==123 && run[9]==31 && run[10]==1 && run[11]=='A';
|
||||
int id=200;
|
||||
while (cache.ensureImage(id++,big)>=0) {}
|
||||
int before=cache.usedBytes();
|
||||
assert cache.ensureImage(id,big)==-1 && cache.usedBytes()==before;
|
||||
cache.reset(); assert cache.usedBytes()==0 && !cache.hasPendingUploads();
|
||||
assert cache.ensureImage(id,tiny)==0;
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ class JavaCompatibilityTest {
|
||||
|
||||
@Test fun gestures() = FaceclawGestureEventTest.main(emptyArray())
|
||||
|
||||
@Test fun textureCache() = TextureCacheProtocolTest.main(emptyArray())
|
||||
@Test fun resourceCache() = ResourceCacheProtocolTest.main(emptyArray())
|
||||
}
|
||||
|
||||
class AndroidByteReaderTest {
|
||||
|
||||
@@ -198,6 +198,20 @@ class ProtocolTest {
|
||||
assertEquals(1, callbacks)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun evictionWaitsForAllEarlierReplayableCommands() {
|
||||
val earlier = message(input = byteArrayOf(19, 0, 0))
|
||||
val eviction = message(input = byteArrayOf(22, 1, 0, 0, 0))
|
||||
val upload = message(input = byteArrayOf(21, 1, 0))
|
||||
assertFalse(CfwMessageWindow.canSend(listOf(earlier), eviction))
|
||||
earlier.cfwAckLenses = 3 // Still retained behind an unresolved window head.
|
||||
assertFalse(CfwMessageWindow.canSend(listOf(earlier), eviction))
|
||||
assertTrue(CfwMessageWindow.canSend(emptyList(), eviction))
|
||||
assertTrue(CfwMessageWindow.canSend(listOf(message(sid = 1)), eviction))
|
||||
assertTrue(CfwMessageWindow.canSend(listOf(earlier), upload))
|
||||
assertTrue(CfwMessageWindow.canSend(listOf(eviction), upload))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun orderedCompletionAndWholeWindowRecovery() {
|
||||
val head = message()
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
class ResourceCacheTest {
|
||||
private fun resource(size: Int, value: Int) = CachedResource(ByteArray(size) { value.toByte() })
|
||||
private fun u16(bytes: ByteArray, offset: Int) = (bytes[offset].toInt() and 255) or ((bytes[offset + 1].toInt() and 255) shl 8)
|
||||
private fun u32(bytes: ByteArray, offset: Int) = u16(bytes, offset) or (u16(bytes, offset + 2) shl 16)
|
||||
private fun uploaded(commands: List<ByteArray>): Map<Int, ByteArray> {
|
||||
val resources = HashMap<Int, ByteArray>()
|
||||
val received = HashMap<Int, Int>()
|
||||
for (command in commands) {
|
||||
assertTrue(command.size <= 3600)
|
||||
if (command[0].toInt() != 21) continue
|
||||
var pos = 3
|
||||
val seen = HashSet<Int>()
|
||||
repeat(u16(command, 1)) {
|
||||
val id = u16(command, pos)
|
||||
assertTrue(id in 0..511 && seen.add(id))
|
||||
val total = u32(command, pos + 2)
|
||||
val offset = u16(command, pos + 6)
|
||||
val length = u16(command, pos + 8)
|
||||
assertEquals(received[id] ?: 0, offset)
|
||||
val bytes = resources.getOrPut(id) { ByteArray(total) }
|
||||
command.copyInto(bytes, offset, pos + 10, pos + 10 + length)
|
||||
received[id] = offset + length
|
||||
pos += 10 + length
|
||||
}
|
||||
assertEquals(command.size, pos)
|
||||
}
|
||||
resources.forEach { (id, data) -> assertEquals(data.size, received[id]) }
|
||||
return resources
|
||||
}
|
||||
|
||||
@Test fun contentAddressingAndWireVector() {
|
||||
val cache = ResourceCacheState()
|
||||
val image = CachedResource(byteArrayOf(1, 1, 31))
|
||||
val ids = cache.prepare(listOf(image, CachedResource(image.bytes)))
|
||||
assertContentEquals(intArrayOf(0, 0), ids)
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertEquals(22, commands.first()[0].toInt())
|
||||
assertEquals(512, u16(commands.first(), 1))
|
||||
assertEquals(511, u16(commands.first(), 1025))
|
||||
assertContentEquals(byteArrayOf(21, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 1, 1, 31), commands.last())
|
||||
assertContentEquals(image.bytes, uploaded(commands).getValue(0))
|
||||
assertEquals(2068, cache.usedBytes())
|
||||
assertEquals(0, cache.prepare(listOf(image))[0])
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
cache.reset(); cache.prepare(listOf(image))
|
||||
assertEquals(22, cache.drainCommands(3600).first()[0].toInt())
|
||||
}
|
||||
|
||||
@Test fun exactly64KiBUploadsInBoundedContiguousChunks() {
|
||||
val cache = ResourceCacheState()
|
||||
val data = CachedResource(ByteArray(65536) { (it * 37).toByte() })
|
||||
assertEquals(0, cache.prepare(listOf(data))[0])
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertTrue(commands.count { it[0].toInt() == 21 } > 1)
|
||||
assertContentEquals(data.bytes, uploaded(commands).getValue(0))
|
||||
assertEquals(-1, cache.prepare(listOf(resource(65537, 1)))[0])
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
assertEquals(0, cache.resourceId(data))
|
||||
}
|
||||
|
||||
@Test fun lruEvictsByBytesAndProtectsAllCurrentFrameHits() {
|
||||
val cache = ResourceCacheState()
|
||||
val a = resource(65536, 1); val b = resource(65536, 2); val c = resource(65536, 3); val d = resource(65536, 4)
|
||||
assertContentEquals(intArrayOf(0, 1, 2), cache.prepare(listOf(a, b, c)))
|
||||
cache.drainCommands(3600)
|
||||
cache.prepare(listOf(a)); cache.drainCommands(3600) // b is oldest now.
|
||||
assertContentEquals(intArrayOf(1, 2), cache.prepare(listOf(d, c))) // protect later hit c before d.
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertContentEquals(byteArrayOf(22, 1, 0, 1, 0), commands.first())
|
||||
assertEquals(-1, cache.resourceId(b))
|
||||
assertEquals(0, cache.resourceId(a)); assertEquals(2, cache.resourceId(c))
|
||||
assertEquals(1, cache.resourceId(d))
|
||||
assertContentEquals(d.bytes, uploaded(commands).getValue(1))
|
||||
val tooLarge = cache.prepare(listOf(a, c, d, b))
|
||||
assertEquals(-1, tooLarge.last())
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
assertEquals(3, cache.residentCount())
|
||||
}
|
||||
|
||||
@Test fun idPressureUsesLruEvenWhenBytesAreAvailable() {
|
||||
val cache = ResourceCacheState()
|
||||
val resources = (0..512).map { CachedResource(byteArrayOf(it.toByte(), (it ushr 8).toByte())) }
|
||||
assertContentEquals(IntArray(512) { it }, cache.prepare(resources.take(512)))
|
||||
cache.drainCommands(3600)
|
||||
cache.prepare(listOf(resources[0])); cache.drainCommands(3600)
|
||||
assertEquals(1, cache.prepare(listOf(resources[512]))[0])
|
||||
val commands = cache.drainCommands(3600)
|
||||
assertContentEquals(byteArrayOf(22, 1, 0, 1, 0), commands.first())
|
||||
assertEquals(0, cache.resourceId(resources[0]))
|
||||
assertEquals(-1, cache.resourceId(resources[1]))
|
||||
}
|
||||
|
||||
@Test fun rejectedPlansRestoreResidencyAndLru() {
|
||||
val cache = ResourceCacheState()
|
||||
val resources = (0..3).map { resource(65536, it) }
|
||||
cache.prepare(resources.take(3)); cache.drainCommands(3600)
|
||||
val rollback = cache.checkpoint()
|
||||
cache.prepare(listOf(resources[3]))
|
||||
assertEquals(-1, cache.resourceId(resources[0]))
|
||||
rollback()
|
||||
assertEquals(0, cache.resourceId(resources[0])); assertEquals(-1, cache.resourceId(resources[3]))
|
||||
assertTrue(cache.drainCommands(3600).isEmpty())
|
||||
cache.prepare(listOf(resources[3]))
|
||||
assertContentEquals(byteArrayOf(22, 1, 0, 0, 0), cache.drainCommands(3600).first())
|
||||
}
|
||||
|
||||
@Test fun imagePlannerEmitsResourceIdsAndReusesResidency() {
|
||||
val cache = ResourceCacheState()
|
||||
val id = ImageAtlas.ensure("resource-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)
|
||||
assertContentEquals(byteArrayOf(19, 0, 0, 0, 0, 0, 0, 31), cold.payload.takeLast(8).toByteArray())
|
||||
assertTrue(cold.resourceCommands.isNotEmpty())
|
||||
val warm = plan()
|
||||
assertTrue(warm.resourceCommands.isEmpty()); assertContentEquals(cold.payload, warm.payload)
|
||||
cache.reset(); assertTrue(plan().resourceCommands.isNotEmpty())
|
||||
assertNull(TexturePlanner.plan(null, ByteArray(packed.size), 8, 4, draws, cache, 1, true, 8, testPlatform()))
|
||||
}
|
||||
|
||||
private fun registerFont(key: String, encodings: List<Int>, size: Int = 2): Int {
|
||||
val out = ByteSink(); val name = key.encodeToByteArray()
|
||||
out.write(name.size); out.write(name, 0, name.size); out.write(size)
|
||||
out.write(encodings.size); out.write(0)
|
||||
for (encoding in encodings) {
|
||||
out.write(encoding); repeat(3) { out.write(0) }
|
||||
out.write(0); out.write(0); out.write(size); out.write(size)
|
||||
repeat(size * ((size + 1) / 2)) { out.write(0x12) }
|
||||
}
|
||||
GlyphAtlas.registerAa(ArrayByteReader(out.toByteArray()))
|
||||
return GlyphAtlas.fontId(key)
|
||||
}
|
||||
|
||||
@Test fun fontTablesUseRelativeU16OffsetsAndGrowOnDemand() {
|
||||
val id = registerFont("resource-font-relative", listOf(65, 66))
|
||||
val first = FontResourceAtlas.get(id, setOf(65))
|
||||
assertEquals(192, u16(first.resource.bytes, (65 - 32) * 2))
|
||||
assertEquals(0, u16(first.resource.bytes, (66 - 32) * 2))
|
||||
assertSame(first, FontResourceAtlas.get(id, setOf(65)))
|
||||
val second = FontResourceAtlas.get(id, setOf(66))
|
||||
assertTrue(second.encodings.containsAll(setOf(65, 66)))
|
||||
assertEquals(192 + GlyphAtlas.get(id, 65)!!.cachedBytes.size, u16(second.resource.bytes, (66 - 32) * 2))
|
||||
val cache = ResourceCacheState()
|
||||
cache.prepare(listOf(second.resource)); val commands = cache.drainCommands(3600)
|
||||
assertContentEquals(second.resource.bytes, uploaded(commands).getValue(0))
|
||||
}
|
||||
|
||||
@Test fun oversizedFontsPrioritizeTheCurrentFrameWithin64KiB() {
|
||||
val id = registerFont("resource-font-limit", listOf(65, 66), 255)
|
||||
val a = FontResourceAtlas.get(id, setOf(65))
|
||||
assertEquals(setOf(65), a.encodings)
|
||||
val b = FontResourceAtlas.get(id, setOf(66))
|
||||
assertEquals(setOf(66), b.encodings)
|
||||
assertTrue(b.resource.bytes.size <= 65536)
|
||||
assertEquals(0, u16(b.resource.bytes, (65 - 32) * 2))
|
||||
assertEquals(192, u16(b.resource.bytes, (66 - 32) * 2))
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -24,13 +24,14 @@ class IosTextureApiTest {
|
||||
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(22, cold.resourceCommands.first().byteArray()[0].toInt())
|
||||
assertTrue(cold.resourceCommands.any { it.byteArray()[0].toInt() == 21 })
|
||||
assertEquals(8, cold.payload.byteArray()[0].toInt())
|
||||
val warm = assertNotNull(planner.plan(null, next, frame, 1))
|
||||
assertTrue(warm.uploads.isEmpty())
|
||||
assertTrue(warm.resourceCommands.isEmpty())
|
||||
assertContentEquals(cold.payload.byteArray(), warm.payload.byteArray())
|
||||
planner.reset()
|
||||
assertTrue(assertNotNull(planner.plan(null, next, frame, 1)).uploads.isNotEmpty())
|
||||
assertTrue(assertNotNull(planner.plan(null, next, frame, 1)).resourceCommands.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())
|
||||
@@ -50,10 +51,10 @@ class IosTextureApiTest {
|
||||
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)
|
||||
assertTrue(plan.usedBytes > 2048 + 192)
|
||||
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(20, payload[payload.size - 10].toInt())
|
||||
assertEquals(65, payload.last().toInt())
|
||||
}
|
||||
|
||||
@@ -81,6 +82,6 @@ class IosTextureApiTest {
|
||||
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")
|
||||
assertTrue(retry.resourceCommands.isNotEmpty(), "the rejected plan's image was never uploaded")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user