Refactor: g2flash protocol doesn't have so many magic numbers

This commit is contained in:
James Babcock committed 2026-09-22 16:27:04 -07:00
1 parent 306accf06d
commit ea9bfae6af
22 files changed
+231 -99

No files matched your search

@@ -277,7 +277,7 @@ class BleImageOptimizer {
}
var compressed: ByteArray = rleEncode(region)
var out: ByteArray = ByteArray((7 + compressed.size))
out[0] = 3
out[0] = CFW_MSG_BOUNDING_BOX.toByte()
out[1] = ((left / 4)).toByte()
out[2] = ((top / 2)).toByte()
out[3] = ((boxWidth / 4)).toByte()
@@ -369,7 +369,7 @@ class BleImageOptimizer {
fid = (if ((fid >= 0xfffe)) 1 else (fid + 1))
}
var out: ByteArray = ByteArray(total)
out[0] = 8
out[0] = CFW_MSG_MULTI_SEGMENT.toByte()
out[1] = (rects.size).toByte()
var pos: Int = 2
for (sub in subs) {
@@ -553,7 +553,7 @@ class BleImageOptimizer {
}
var z: ByteArray = rleEncode(packed)
var out: ByteArray = ByteArray((z.size + 1))
out[0] = 6
out[0] = CFW_MSG_FULL_FRAME.toByte()
z.copyInto(out, 1, 0, 0 + z.size)
return out
}
@@ -89,7 +89,8 @@ class BleProtocol {
const val FACECLAW_WEAR_OP_QUERY: Int = 7
const val CFW_IMAGE_MODE_CLEANUP: Int = 11
/** Mirrors g2flash/patches/zlib_glue.c. */
const val CFW_IMAGE_MODE_CLEANUP: Int = CFW_MSG_CLEANUP
private const val FACECLAW_WAKE_EVENT: Int = 1
@@ -0,0 +1,38 @@
package com.faceclaw.app
/** Message IDs and header flags mirror g2flash/patches/zlib_glue.c. */
/** Retired wire ID; used internally by the phone optimizer. */
const val CFW_MSG_BOUNDING_BOX = 3
const val CFW_MSG_BUZZER = 5
/** Retired wire ID; used internally by the phone optimizer. */
const val CFW_MSG_FULL_FRAME = 6
const val CFW_MSG_DIAGNOSTICS = 7
/** Retired wire ID; used internally by the phone optimizer. */
const val CFW_MSG_MULTI_SEGMENT = 8
/** Retired wire ID; used internally by the phone optimizer. */
const val CFW_MSG_RECT_COPY = 9
const val CFW_MSG_COMPASS = 10
const val CFW_MSG_CLEANUP = 11
const val CFW_MSG_RETIRED_12 = 12
const val CFW_MSG_RETIRED_13 = 13
const val CFW_MSG_RETIRED_14 = 14
/** Retired wire ID; used internally by the phone optimizer. */
const val CFW_MSG_STOCK_FONT_STRING = 15
const val CFW_MSG_AMBIENT_LIGHT = 16
const val CFW_MSG_RING_BATTERY = 17
const val CFW_MSG_RETIRED_CACHE_WRITE = 18
/** Retired wire ID; used internally by the phone optimizer. */
const val CFW_MSG_CACHED_IMAGE = 19
/** Retired wire ID; used internally by the phone optimizer. */
const val CFW_MSG_CACHED_TEXT = 20
const val CFW_MSG_UPLOAD_RESOURCE = 21
const val CFW_MSG_EVICT_RESOURCE = 22
const val CFW_MSG_PANEL_READ = 23
const val CFW_MSG_PANEL_WRITE = 24
const val CFW_MSG_PANEL_PATTERN = 25
const val CFW_MSG_DRAW_CALLS = 26
const val CFW_MSG_SET_ROOT_DISPLAY_LIST = 27
const val CFW_MSG_PRESENT = 28
const val CFW_MSG_CREATE_SURFACE = 29
const val CFW_MSG_TYPE_MASK = 127
const val CFW_MSG_FLAG_LENSES_DIFFER = 128
@@ -48,6 +48,7 @@ class CfwTransport(private val platform: ProtocolPlatform) {
}
companion object {
/** Mirrors g2flash/patches/message_transport.h. */
const val SID = 0xf0
const val BOTH = 3
const val MAX_MESSAGE = 65535
@@ -0,0 +1,11 @@
package com.faceclaw.app
/** Draw-call wire IDs. Keep names and values in sync with cfw_draw_op in g2flash/patches/display_list.c. */
const val DRAW_OP_BOUNDING_BOX = 1
const val DRAW_OP_RECT_COPY = 2
const val DRAW_OP_STOCK_FONT_STRING = 3
const val DRAW_OP_IMAGE = 4
const val DRAW_OP_TEXT = 5
const val DRAW_OP_REMAP_COLORS = 6
const val DRAW_OP_DISPLAY_LIST = 7
const val DRAW_OP_ROUNDED_RECT = 8
@@ -0,0 +1,21 @@
package com.faceclaw.app
/**
* Draw-call flags, bounding-box flags, and the border sentinel mirror g2flash/patches/display_list.c.
* Image/text options mirror g2flash/patches/texture_draw.h.
*/
/** Common draw-call header bits. */
const val DRAW_FLAG_RESOURCE_TARGET = 1
const val DRAW_FLAG_DEPTH = 2
const val DRAW_FLAGS_MASK = DRAW_FLAG_RESOURCE_TARGET or DRAW_FLAG_DEPTH
/** Bounding-box payload flag: u16 coordinates/sizes instead of aligned compact units. */
const val DRAW_BBOX_FLAG_U16 = 1
/** Image/text options. */
const val CFW_TEXTURE_OPT_BRIGHTNESS_MASK = 15
const val CFW_TEXTURE_OPT_TRANSPARENT = 16
const val CFW_TEXTURE_OPT_INVERSE = 32
/** Rounded-rectangle border sentinel, not a color or flag bit. */
const val DRAW_ROUNDED_RECT_NO_BORDER = 16
@@ -2,31 +2,36 @@ package com.faceclaw.app
/** Revision 26 wire grammar, shared by scene planning and the local renderer. */
object DrawProtocol {
const val DRAW = 26
const val ROOT = 27
const val PRESENT = 28
const val CREATE = 29
const val IMAGE = 0
const val FONT = 1
const val LIST = 2
const val LARGE = 4
const val RLE = 8
/** Mirrors g2flash/patches/zlib_glue.c. */
const val DRAW = CFW_MSG_DRAW_CALLS
const val ROOT = CFW_MSG_SET_ROOT_DISPLAY_LIST
const val PRESENT = CFW_MSG_PRESENT
const val CREATE = CFW_MSG_CREATE_SURFACE
/** Mirrors g2flash/patches/resource_cache.h. */
const val IMAGE = CFW_RESOURCE_TYPE_IMAGE
const val FONT = CFW_RESOURCE_TYPE_FONT
const val LIST = CFW_RESOURCE_TYPE_DISPLAY_LIST
const val LARGE = CFW_RESOURCE_FLAG_LARGE
const val RLE = CFW_RESOURCE_FLAG_RLE
/** Mirrors g2flash/patches/display_list.c. */
const val SCREEN = 65535
const val CURRENT = 65534
fun u16(b: ByteArray, p: Int): Int = (b[p].toInt() and 255) or ((b[p + 1].toInt() and 255) shl 8)
fun word(n: Int) = byteArrayOf(n.toByte(), (n ushr 8).toByte())
fun call(op: Int, args: ByteArray, target: Int? = null, depth: Int? = null): ByteArray {
require(depth == null || depth in -128..127)
val flags = (if (target == null) 0 else 1) or (if (depth == null) 0 else 2)
val flags = (if (target == null) 0 else DRAW_FLAG_RESOURCE_TARGET) or (if (depth == null) 0 else DRAW_FLAG_DEPTH)
return byteArrayOf(op.toByte(), flags.toByte()) + (target?.let { word(it) } ?: byteArrayOf()) +
(depth?.let { byteArrayOf(it.toByte()) } ?: byteArrayOf()) + args
}
fun depthOffset(depth: Int, right: Boolean): Int =
if (right) -floorHalf(depth + 1) else floorHalf(depth)
private fun floorHalf(n: Int): Int = if (n < 0) (n - 1) / 2 else n / 2
fun roundedRect(x: Int, y: Int, width: Int, height: Int, radius: Int, background: Int, border: Int = 16, depth: Int? = null): ByteArray {
require(width in 1..640 && height in 1..480 && radius in 0..65535 && background in 0..15 && border in 0..16)
return call(8, word(x) + word(y) + word(width) + word(height) + word(radius) + byteArrayOf(background.toByte(), border.toByte()), depth = depth)
fun roundedRect(x: Int, y: Int, width: Int, height: Int, radius: Int, background: Int, border: Int = DRAW_ROUNDED_RECT_NO_BORDER, depth: Int? = null): ByteArray {
require(width in 1..640 && height in 1..480 && radius in 0..65535 && background in 0..15 && border in 0..DRAW_ROUNDED_RECT_NO_BORDER)
return call(DRAW_OP_ROUNDED_RECT, word(x) + word(y) + word(width) + word(height) + word(radius) + byteArrayOf(background.toByte(), border.toByte()), depth = depth)
}
fun sequence(calls: List<ByteArray>): ByteArray {
require(calls.size <= 4096)
@@ -37,13 +42,13 @@ object DrawProtocol {
fun message(calls: List<ByteArray>) = byteArrayOf(DRAW.toByte()) + sequence(calls)
fun displayList(calls: List<ByteArray>) = byteArrayOf(LIST.toByte()) + sequence(calls)
fun root(id: Int) = byteArrayOf(ROOT.toByte()) + word(id)
fun image(id: Int, x: Int, y: Int, options: Int = 15, target: Int? = null, depth: Int? = null) =
call(4, word(id) + word(x) + word(y) + byteArrayOf(options.toByte()), target, depth)
fun image(id: Int, x: Int, y: Int, options: Int = CFW_TEXTURE_OPT_BRIGHTNESS_MASK, target: Int? = null, depth: Int? = null) =
call(DRAW_OP_IMAGE, word(id) + word(x) + word(y) + byteArrayOf(options.toByte()), target, depth)
fun lut(width: Int, height: Int, factor: Int): ByteArray {
val table = ByteArray(8) { i ->
(((i * 2 * factor / 256).coerceIn(0, 15) shl 4) or ((i * 2 + 1) * factor / 256).coerceIn(0, 15)).toByte()
}
return call(6, word(0) + word(0) + word(width) + word(height) + table)
return call(DRAW_OP_REMAP_COLORS, word(0) + word(0) + word(width) + word(height) + table)
}
fun rawImage(width: Int, height: Int, pixels: ByteArray = ByteArray(((width + 1) / 2) * height)): ByteArray {
require(width in 1..640 && height in 1..480 && pixels.size == ((width + 1) / 2) * height)
@@ -55,7 +60,7 @@ object DrawProtocol {
fun bbox(packed: ByteArray, stride: Int, x: Int, y: Int, width: Int, height: Int, target: Int? = null): ByteArray {
val compact = x % 4 == 0 && y % 2 == 0 && width % 4 == 0 && height % 2 == 0 && x / 4 < 256 && y / 2 < 256 && width / 4 < 256 && height / 2 < 256
val header = if (compact) byteArrayOf(0, (x / 4).toByte(), (y / 2).toByte(), (width / 4).toByte(), (height / 2).toByte())
else byteArrayOf(1) + word(x) + word(y) + word(width) + word(height)
else byteArrayOf(DRAW_BBOX_FLAG_U16.toByte()) + word(x) + word(y) + word(width) + word(height)
val out = ByteSink(); out.write(header)
var color = -1; var count = 0
fun flush() {
@@ -69,22 +74,22 @@ object DrawProtocol {
count++
}
flush()
return call(1, out.toByteArray(), target)
return call(DRAW_OP_BOUNDING_BOX, out.toByteArray(), target)
}
/** Internal optimizer formats are translated here; they are never sent to revision 26. */
fun fromOptimized(payload: ByteArray, width: Int = 640, height: Int = 480): List<ByteArray> {
return when (val mode = payload[0].toInt() and 127) {
8 -> {
return when (val mode = payload[0].toInt() and CFW_MSG_TYPE_MASK) {
CFW_MSG_MULTI_SEGMENT -> {
val result = ArrayList<ByteArray>(); var pos = 2
repeat(payload[1].toInt() and 255) { val n = u16(payload, pos); pos += 2; result.addAll(fromOptimized(payload.copyOfRange(pos, pos + n), width, height)); pos += n }
require(pos == payload.size); result
}
3 -> listOf(call(1, byteArrayOf(0) + payload.copyOfRange(1, 5) + payload.copyOfRange(7, payload.size)))
6 -> listOf(call(1, byteArrayOf(1) + word(0) + word(0) + word(width) + word(height) + payload.copyOfRange(1, payload.size)))
15 -> listOf(call(3, payload.copyOfRange(1, payload.size)))
19 -> listOf(call(4, payload.copyOfRange(1, payload.size)))
20 -> listOf(call(5, payload.copyOfRange(1, payload.size)))
9 -> listOf(call(2, word(CURRENT) + payload.copyOfRange(1, 9) + payload.copyOfRange(9, 13)))
CFW_MSG_BOUNDING_BOX -> listOf(call(DRAW_OP_BOUNDING_BOX, byteArrayOf(0) + payload.copyOfRange(1, 5) + payload.copyOfRange(7, payload.size)))
CFW_MSG_FULL_FRAME -> listOf(call(DRAW_OP_BOUNDING_BOX, byteArrayOf(DRAW_BBOX_FLAG_U16.toByte()) + word(0) + word(0) + word(width) + word(height) + payload.copyOfRange(1, payload.size)))
CFW_MSG_STOCK_FONT_STRING -> listOf(call(DRAW_OP_STOCK_FONT_STRING, payload.copyOfRange(1, payload.size)))
CFW_MSG_CACHED_IMAGE -> listOf(call(DRAW_OP_IMAGE, payload.copyOfRange(1, payload.size)))
CFW_MSG_CACHED_TEXT -> listOf(call(DRAW_OP_TEXT, payload.copyOfRange(1, payload.size)))
CFW_MSG_RECT_COPY -> listOf(call(DRAW_OP_RECT_COPY, word(CURRENT) + payload.copyOfRange(1, 9) + payload.copyOfRange(9, 13)))
else -> error("Unsupported internal draw format $mode")
}
}
@@ -20,14 +20,17 @@ class CachedResource @JvmOverloads constructor(bytes: ByteArray, val owner: Stri
*/
class ResourceCacheState {
companion object {
/** Mirrors g2flash/patches/resource_cache.h. */
const val CACHE_SIZE = 196608
const val RESOURCE_COUNT = 512
const val TABLE_BYTES = RESOURCE_COUNT * 4
const val ARENA_BYTES = CACHE_SIZE - TABLE_BYTES
const val MAX_RESOURCE_SIZE = 65536
const val BLOCK_HEADER_BYTES = 16
const val UPLOAD_MODE = 21
const val EVICT_MODE = 22
/** Mirrors g2flash/patches/zlib_glue.c. */
const val UPLOAD_MODE = CFW_MSG_UPLOAD_RESOURCE
const val EVICT_MODE = CFW_MSG_EVICT_RESOURCE
fun allocationBytes(size: Int): Int = BLOCK_HEADER_BYTES + ((size + 3) and -4)
}
@@ -0,0 +1,10 @@
package com.faceclaw.app
/** Resource header types and flags; mirrored in g2flash/patches/resource_cache.h. */
const val CFW_RESOURCE_TYPE_IMAGE = 0
const val CFW_RESOURCE_TYPE_FONT = 1
const val CFW_RESOURCE_TYPE_DISPLAY_LIST = 2
const val CFW_RESOURCE_TYPE_MASK = 3
const val CFW_RESOURCE_FLAG_LARGE = 4
const val CFW_RESOURCE_FLAG_RLE = 8
const val CFW_RESOURCE_IMAGE_FLAGS_MASK = CFW_RESOURCE_FLAG_LARGE or CFW_RESOURCE_FLAG_RLE
@@ -47,7 +47,7 @@ class TexturePlanner {
private const val MAX_ADJUST_BYTES: Int = 4
/** Options: identity LUT (top 15) + transparent, for mode-19 image draws. */
private const val IMAGE_DRAW_OPTIONS: Int = 0x1f
private const val IMAGE_DRAW_OPTIONS: Int = CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT
/**
* Plan a cached-draw update. previous is the delta base (the frame the shadow currently
@@ -368,12 +368,12 @@ class TexturePlanner {
continue
}
var sub: ByteArray = ByteArray((7 + bytesToLastInk))
sub[0] = 15
sub[0] = CFW_MSG_STOCK_FONT_STRING.toByte()
sub[1] = ((startX and 0xff)).toByte()
sub[2] = (((startX shr 8) and 0xff)).toByte()
sub[3] = ((draw.y and 0xff)).toByte()
sub[4] = (((draw.y shr 8) and 0xff)).toByte()
sub[5] = ((top or 0x10)).toByte()
sub[5] = ((top or CFW_TEXTURE_OPT_TRANSPARENT)).toByte()
sub[6] = (bytesToLastInk).toByte()
var pos: Int = 7
run {
@@ -718,7 +718,7 @@ class TexturePlanner {
/** Mode-19 cached-image draw: [19][resourceId u16][x u16][y u16][options u8]. */
@JvmStatic
private fun encodeImageDraw(sel: Selected): ByteArray = byteArrayOf(
19, sel.resourceId.toByte(), (sel.resourceId ushr 8).toByte(),
CFW_MSG_CACHED_IMAGE.toByte(), sel.resourceId.toByte(), (sel.resourceId ushr 8).toByte(),
sel.draw.x.toByte(), (sel.draw.x ushr 8).toByte(),
sel.draw.y.toByte(), (sel.draw.y ushr 8).toByte(), IMAGE_DRAW_OPTIONS.toByte(),
)
@@ -781,14 +781,14 @@ class TexturePlanner {
if (!runGlyphs.isEmpty()) {
var stringBytes: ByteArray = string.toByteArray()
val run = ByteArray(9 + stringBytes.size)
run[0] = 20
run[0] = CFW_MSG_CACHED_TEXT.toByte()
run[1] = fontResource.toByte()
run[2] = (fontResource ushr 8).toByte()
run[3] = first.gx.toByte()
run[4] = (first.gx ushr 8).toByte()
run[5] = first.draw.y.toByte()
run[6] = (first.draw.y ushr 8).toByte()
run[7] = (first.top or 0x10).toByte()
run[7] = (first.top or CFW_TEXTURE_OPT_TRANSPARENT).toByte()
run[8] = stringBytes.size.toByte()
stringBytes.copyInto(run, 9)
outRuns.add(run)
@@ -825,7 +825,7 @@ class TexturePlanner {
total += (2 + sub.size)
}
var out: ByteArray = ByteArray(total)
out[0] = 8
out[0] = CFW_MSG_MULTI_SEGMENT.toByte()
out[1] = (subs.size).toByte()
var pos: Int = 2
for (sub in subs) {
@@ -39,28 +39,28 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
}
private fun image(b: ByteArray, start: Int = 0): Image {
require(start in b.indices); val flags = b[start].toInt() and 255
require(flags and 12 == flags)
val header = if (flags and 4 != 0) 5 else 3
require(flags and CFW_RESOURCE_IMAGE_FLAGS_MASK == flags)
val header = if (flags and CFW_RESOURCE_FLAG_LARGE != 0) 5 else 3
require(b.size - start >= header)
val w = if (header == 5) word(b, start + 1) else b[start + 1].toInt() and 255
val h = if (header == 5) word(b, start + 3) else b[start + 2].toInt() and 255
require(w in 1..640 && h in 1..480)
val pixels = if (flags and 8 != 0) decodeRle(b, start + header, w * h, false)
val pixels = if (flags and CFW_RESOURCE_FLAG_RLE != 0) decodeRle(b, start + header, w * h, false)
else { val t = Target(b, w, h, start + header); IntArray(w * h) { t.get(it % w, it / w) } }
return Image(w, h, pixels)
}
private fun target(id: Int): Target {
val b = resource(id); require(b.isNotEmpty() && b[0].toInt() and 11 == 0)
val b = resource(id); require(b.isNotEmpty() && b[0].toInt() and (CFW_RESOURCE_TYPE_MASK or CFW_RESOURCE_FLAG_RLE) == 0)
image(b) // Validate the complete raw image before exposing a write target.
return if (b[0].toInt() and 4 != 0) Target(b, word(b, 1), word(b, 3), 5)
return if (b[0].toInt() and CFW_RESOURCE_FLAG_LARGE != 0) Target(b, word(b, 1), word(b, 3), 5)
else Target(b, b[1].toInt() and 255, b[2].toInt() and 255, 3)
}
private fun draw(image: Image, target: Target, x: Int, y: Int, options: Int, apply: Boolean) {
if (!apply) return
for (i in image.pixels.indices) {
val v = image.pixels[i]; if (v == 0 && options and 16 != 0) continue
val source = if (options and 32 != 0) 15 - v else v
target.put(x + i % image.width, y + i / image.width, source * (options and 15) / 15)
val v = image.pixels[i]; if (v == 0 && options and CFW_TEXTURE_OPT_TRANSPARENT != 0) continue
val source = if (options and CFW_TEXTURE_OPT_INVERSE != 0) 15 - v else v
target.put(x + i % image.width, y + i / image.width, source * (options and CFW_TEXTURE_OPT_BRIGHTNESS_MASK) / 15)
}
}
private fun copyScreen(screen: Target, composition: Target) {
@@ -108,7 +108,7 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
return references
}
private fun executeList(id: Int, t: Target, screen: Target, apply: Boolean, stack: MutableList<Int>, budget: IntArray, refs: MutableSet<Int>) {
require(stack.size < 8 && id !in stack); val b = resource(id); require(b.size >= 3 && b[0].toInt() == 2)
require(stack.size < 8 && id !in stack); val b = resource(id); require(b.size >= 3 && b[0].toInt() == CFW_RESOURCE_TYPE_DISPLAY_LIST)
refs.add(id); stack.add(id); executeSequence(b.copyOfRange(1, b.size), t, screen, apply, stack, budget, refs); stack.removeAt(stack.lastIndex)
}
private fun executeSequence(b: ByteArray, inherited: Target, screen: Target, apply: Boolean, stack: MutableList<Int>, budget: IntArray, refs: MutableSet<Int>) {
@@ -116,14 +116,14 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
repeat(count) {
val n = word(b, pos); pos += 2; require(n >= 2 && n <= b.size - pos && budget[0]-- > 0)
val call = b.copyOfRange(pos, pos + n); pos += n
val flags = call[1].toInt() and 255; require(flags <= 3)
val flags = call[1].toInt() and 255; require(flags and DRAW_FLAGS_MASK.inv() == 0)
var t = inherited; var header = 2
if (flags and 1 != 0) { val id = word(call, 2); refs.add(id); t = target(id).shifted(inherited.shiftX); header += 2 }
if (flags and 2 != 0) { require(header < call.size); t = t.shifted(DrawProtocol.depthOffset(call[header++].toInt(), rightLens)) }
if (flags and DRAW_FLAG_RESOURCE_TARGET != 0) { val id = word(call, 2); refs.add(id); t = target(id).shifted(inherited.shiftX); header += 2 }
if (flags and DRAW_FLAG_DEPTH != 0) { require(header < call.size); t = t.shifted(DrawProtocol.depthOffset(call[header++].toInt(), rightLens)) }
val p = call.copyOfRange(header, call.size)
when (call[0].toInt()) {
1 -> {
require(p.isNotEmpty() && p[0].toInt() in 0..1); val wide = p[0].toInt() == 1
DRAW_OP_BOUNDING_BOX -> {
require(p.isNotEmpty() && (p[0].toInt() and 255) and DRAW_BBOX_FLAG_U16.inv() == 0); val wide = p[0].toInt() and DRAW_BBOX_FLAG_U16 != 0
require(p.size >= if (wide) 9 else 5)
val x = if (wide) word(p, 1) else (p[1].toInt() and 255) * 4
val y = if (wide) word(p, 3) else (p[2].toInt() and 255) * 2
@@ -133,7 +133,7 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
val pixels = decodeRle(p, if (wide) 9 else 5, w * h, true)
if (apply) for (i in pixels.indices) t.put(x + i % w, y + i / w, pixels[i])
}
2 -> {
DRAW_OP_RECT_COPY -> {
require(p.size == 14); val id = word(p, 0)
val src = when(id) { DrawProtocol.SCREEN -> screen; DrawProtocol.CURRENT -> t; else -> { refs.add(id); target(id) } }
val x = word(p, 2); val y = word(p, 4); val w = word(p, 6); val h = word(p, 8)
@@ -142,7 +142,7 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
if (apply) { val copy = IntArray(w * h) { src.get(x + it % w, y + it / w) }
for (i in copy.indices) t.put(dx + i % w, dy + i / w, copy[i]) }
}
3 -> {
DRAW_OP_STOCK_FONT_STRING -> {
require(p.size >= 6 && p.size == 6 + (p[5].toInt() and 255))
val text = p.copyOfRange(6, p.size).decodeToString(throwOnInvalidSequence = true)
val cps = ArrayList<Int>(); var i = 0
@@ -156,12 +156,12 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
draw(image(g.image), t, x + g.x, y + g.y, p[4].toInt() and 255, apply); x += g.advance
}
}
4, 5 -> {
val font = call[0].toInt() == 5; require(if(font) p.size >= 8 && p.size == 8 + (p[7].toInt() and 255) else p.size == 7)
DRAW_OP_IMAGE, DRAW_OP_TEXT -> {
val font = call[0].toInt() == DRAW_OP_TEXT; require(if(font) p.size >= 8 && p.size == 8 + (p[7].toInt() and 255) else p.size == 7)
val id = word(p, 0); refs.add(id); val data = resource(id)
var x = signed(p, 2); val y = signed(p, 4); val options = p[6].toInt() and 255
if (!font) draw(image(data), t, x, y, options, apply)
else { require(data.size >= 193 && data[0].toInt() == 1)
else { require(data.size >= 193 && data[0].toInt() == CFW_RESOURCE_TYPE_FONT)
for (j in 8 until p.size) { val ch = p[j].toInt() and 255
if(ch in 1..31) { x += ch - 11; continue }; require(ch in 32..127)
val offset = word(data, 1 + (ch - 32) * 2); require(offset >= 193)
@@ -169,7 +169,7 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
}
}
}
6 -> {
DRAW_OP_REMAP_COLORS -> {
require(p.size == 16); val x = word(p, 0); val y = word(p, 2); val w = word(p, 4); val h = word(p, 6)
require(w > 0 && h > 0 && x + w <= t.width && y + h <= t.height)
if (apply) for(yy in y until y + h) for(xx in x until x + w) {
@@ -177,20 +177,20 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
val v = t.get(xx + t.shiftX, yy)
t.put(xx, yy, (p[8 + v / 2].toInt() ushr (if(v % 2 == 0) 4 else 0)) and 15) }
}
8 -> {
DRAW_OP_ROUNDED_RECT -> {
require(p.size == 12)
val x = signed(p, 0); val y = signed(p, 2); val w = word(p, 4); val h = word(p, 6)
val radius = word(p, 8); val fill = p[10].toInt() and 255; val border = p[11].toInt() and 255
require(w in 1..640 && h in 1..480 && fill <= 15 && border <= 16)
require(w in 1..640 && h in 1..480 && fill <= 15 && border <= DRAW_ROUNDED_RECT_NO_BORDER)
if (apply) for (yy in 0 until h) for (xx in 0 until w) {
if (!roundedContains(xx, yy, w, h, radius)) continue
val tx = x + xx + t.shiftX; val ty = y + yy
if (tx !in 0 until t.width || ty !in 0 until t.height) continue
val edge = !roundedContains(xx - 1, yy - 1, w - 2, h - 2, maxOf(0, radius - 1))
t.put(x + xx, ty, if (edge && border < 16) border else maxOf(fill, t.get(tx, ty)))
t.put(x + xx, ty, if (edge && border < DRAW_ROUNDED_RECT_NO_BORDER) border else maxOf(fill, t.get(tx, ty)))
}
}
7 -> { require(p.size == 2); executeList(word(p, 0), t, screen, apply, stack, budget, refs) }
DRAW_OP_DISPLAY_LIST -> { require(p.size == 2); executeList(word(p, 0), t, screen, apply, stack, budget, refs) }
else -> error("Unknown draw opcode")
}
}
@@ -9,16 +9,16 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
fun calls(id: Int): List<ByteArray> {
val image = when (kind) {
3 -> listOf(DrawProtocol.roundedRect(x, y, width, height, radius, background, border, depth),
DrawProtocol.image(id, x, y, 31, depth = depth))
4 -> listOf(DrawProtocol.image(id, x, y, 31, depth = depth))
DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
4 -> listOf(DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
// Gray8 zero is transparent but gray8 one is opaque black. Rect copies preserve
// that distinction after packing, including the transparent corners of app menus.
5 -> mask.map { r -> DrawProtocol.call(2, DrawProtocol.word(id) +
5 -> mask.map { r -> DrawProtocol.call(DRAW_OP_RECT_COPY, DrawProtocol.word(id) +
r.flatMap { DrawProtocol.word(it).toList() }.toByteArray() +
DrawProtocol.word(x + r[0]) + DrawProtocol.word(y + r[1]), depth = depth) }
else -> error("Invalid presentation kind")
}
return image + occlusions.map { r -> DrawProtocol.call(2, DrawProtocol.word(DrawProtocol.SCREEN) + r.flatMap { DrawProtocol.word(it).toList() }.toByteArray() + DrawProtocol.word(r[0]) + DrawProtocol.word(r[1])) }
return image + occlusions.map { r -> DrawProtocol.call(DRAW_OP_RECT_COPY, DrawProtocol.word(DrawProtocol.SCREEN) + r.flatMap { DrawProtocol.word(it).toList() }.toByteArray() + DrawProtocol.word(r[0]) + DrawProtocol.word(r[1])) }
}
companion object {
/** The caller has consumed the presentation tag (3, 4 or 5). Colors cross the bridge in gray8, pixels in gray8 rows. */