forked from Blendman974/faceclaw
Display-list based menu slide animation
This commit is contained in:
1 parent
92e8def0f0
commit
c8d835176f
28 files changed
+1546
-263
No files matched your search
+15
@@ -0,0 +1,15 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
private object DrawRedrawScheduler {
|
||||
val executor = ScheduledThreadPoolExecutor(1) { task ->
|
||||
Thread(task, "Faceclaw draw animation").apply { isDaemon = true }
|
||||
}.apply { removeOnCancelPolicy = true }
|
||||
}
|
||||
|
||||
internal actual fun scheduleDrawRedraw(delayMs: Int, action: () -> Unit): () -> Unit {
|
||||
val future = DrawRedrawScheduler.executor.schedule(action, delayMs.toLong(), TimeUnit.MILLISECONDS)
|
||||
return { future.cancel(false); Unit }
|
||||
}
|
||||
+260
@@ -0,0 +1,260 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Literal or branchless expression used by rounded-rectangle x/y in revision 27. */
|
||||
sealed class DrawValue {
|
||||
class Integer(val value: Int) : DrawValue()
|
||||
class Expression(val program: ByteArray) : DrawValue()
|
||||
|
||||
companion object {
|
||||
/** Smoothstep interpolation; time is measured from the next PRESENT. */
|
||||
fun animate(from: Int, to: Int, durationMs: Int = 500, elapsedMs: Int = 0): DrawValue {
|
||||
require(durationMs > 0 && elapsedMs >= 0)
|
||||
if (from == to || elapsedMs >= durationMs) return Integer(to)
|
||||
val code = ByteSink()
|
||||
fun integer(value: Int) {
|
||||
code.write(DrawExpression.PUSH_I32)
|
||||
code.write(ExtendedVarint.signed(value))
|
||||
}
|
||||
integer(from)
|
||||
code.write(DrawExpression.I2F)
|
||||
integer(to)
|
||||
code.write(DrawExpression.I2F)
|
||||
integer(durationMs - elapsedMs)
|
||||
code.write(DrawExpression.TIME)
|
||||
integer(elapsedMs)
|
||||
code.write(DrawExpression.IADD)
|
||||
code.write(DrawExpression.I2F)
|
||||
integer(durationMs)
|
||||
code.write(DrawExpression.I2F)
|
||||
code.write(DrawExpression.FDIV)
|
||||
code.write(DrawExpression.SMOOTHSTEP)
|
||||
code.write(DrawExpression.LERP)
|
||||
code.write(DrawExpression.F2I)
|
||||
return Expression(code.toByteArray())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Prefix payload is most-significant first; continuation bytes use all eight bits. */
|
||||
internal object ExtendedVarint {
|
||||
private val widths = intArrayOf(7, 14, 21, 28, 32)
|
||||
private val prefixes = intArrayOf(0, 0x80, 0xc0, 0xe0, 0xf0)
|
||||
|
||||
fun signed(value: Int): ByteArray {
|
||||
val size = widths.indexOfFirst { bits ->
|
||||
bits == 32 || value.toLong() in -(1L shl (bits - 1)) until (1L shl (bits - 1))
|
||||
} + 1
|
||||
return encode(value, size)
|
||||
}
|
||||
|
||||
fun unsigned(value: UInt): ByteArray {
|
||||
val size = widths.indexOfFirst { it == 32 || value.toLong() < (1L shl it) } + 1
|
||||
return encode(value.toInt(), size)
|
||||
}
|
||||
|
||||
private fun encode(value: Int, size: Int): ByteArray {
|
||||
val bytes = ByteArray(size)
|
||||
if (size == 5) {
|
||||
bytes[0] = 0xf0.toByte()
|
||||
} else {
|
||||
val mask = (1 shl (8 - size)) - 1
|
||||
bytes[0] = (prefixes[size - 1] or ((value ushr (8 * (size - 1))) and mask)).toByte()
|
||||
}
|
||||
for (i in 1 until size) bytes[i] = (value ushr (8 * (size - 1 - i))).toByte()
|
||||
return bytes
|
||||
}
|
||||
|
||||
fun read(reader: DrawReader, signed: Boolean, first: Int = reader.readU8()): Int {
|
||||
val size = when {
|
||||
first < 0x80 -> 1
|
||||
first < 0xc0 -> 2
|
||||
first < 0xe0 -> 3
|
||||
first < 0xf0 -> 4
|
||||
first == 0xf0 -> 5
|
||||
else -> error("Reserved extended-varint prefix")
|
||||
}
|
||||
var value = if (size == 5) 0 else first and ((1 shl (8 - size)) - 1)
|
||||
repeat(size - 1) { value = (value shl 8) or reader.readU8() }
|
||||
val bits = widths[size - 1]
|
||||
return if (signed && bits < 32) (value shl (32 - bits)) shr (32 - bits) else value
|
||||
}
|
||||
|
||||
fun write(value: DrawValue): ByteArray = when (value) {
|
||||
is DrawValue.Integer -> signed(value.value)
|
||||
is DrawValue.Expression -> {
|
||||
require(value.program.size <= DrawExpression.MAX_BYTES)
|
||||
byteArrayOf(0xff.toByte()) + unsigned(value.program.size.toUInt()) + value.program
|
||||
}
|
||||
}
|
||||
|
||||
fun evaluate(reader: DrawReader, frame: DrawEvaluation): Int {
|
||||
val first = reader.readU8()
|
||||
if (first != 0xff) return read(reader, true, first)
|
||||
val length = read(reader, false).toUInt()
|
||||
// Outer framing errors reject the call; errors inside a framed program yield zero.
|
||||
require(length <= reader.remaining.toUInt())
|
||||
val program = reader.readSlice(length.toInt())
|
||||
if (length > DrawExpression.MAX_BYTES.toUInt()) return 0
|
||||
return DrawExpression.evaluate(program, frame)
|
||||
}
|
||||
}
|
||||
|
||||
internal class DrawEvaluation(val elapsedMs: Long) {
|
||||
var animationPending = false
|
||||
}
|
||||
|
||||
/** Typed i32/f32 stack machine. Keep opcode IDs and semantics in sync with draw_expression.c. */
|
||||
internal object DrawExpression {
|
||||
const val MAX_BYTES = 1024
|
||||
const val MAX_STACK = 32
|
||||
const val PUSH_I32 = 1
|
||||
const val PUSH_F32 = 2
|
||||
const val DUP = 3
|
||||
const val DROP = 4
|
||||
const val SWAP = 5
|
||||
const val IADD = 16
|
||||
const val ISUB = 17
|
||||
const val IMUL = 18
|
||||
const val IDIV = 19
|
||||
const val IMOD = 20
|
||||
const val INEG = 21
|
||||
const val IMIN = 22
|
||||
const val IMAX = 23
|
||||
const val FADD = 32
|
||||
const val FSUB = 33
|
||||
const val FMUL = 34
|
||||
const val FDIV = 35
|
||||
const val FNEG = 36
|
||||
const val FMIN = 37
|
||||
const val FMAX = 38
|
||||
const val I2F = 48
|
||||
const val F2I = 49
|
||||
const val TIME = 50
|
||||
const val LERP = 64
|
||||
const val SMOOTHSTEP = 65
|
||||
const val EASE_IN_QUAD = 66
|
||||
const val EASE_OUT_QUAD = 67
|
||||
const val EASE_IN_OUT_QUAD = 68
|
||||
const val EASE_IN_CUBIC = 69
|
||||
const val EASE_OUT_CUBIC = 70
|
||||
const val EASE_IN_OUT_CUBIC = 71
|
||||
|
||||
fun evaluate(reader: DrawReader, frame: DrawEvaluation): Int {
|
||||
val bits = IntArray(MAX_STACK)
|
||||
val floats = BooleanArray(MAX_STACK)
|
||||
var size = 0
|
||||
var pending = false
|
||||
fun push(value: Int, float: Boolean = false) {
|
||||
require(size < MAX_STACK)
|
||||
bits[size] = value
|
||||
floats[size++] = float
|
||||
}
|
||||
fun integer(): Int {
|
||||
require(size > 0 && !floats[size - 1])
|
||||
return bits[--size]
|
||||
}
|
||||
fun float(): Float {
|
||||
require(size > 0 && floats[size - 1])
|
||||
return Float.fromBits(bits[--size])
|
||||
}
|
||||
fun pushFloat(value: Float) {
|
||||
require(value.isFinite())
|
||||
push(value.toBits(), true)
|
||||
}
|
||||
fun toInteger(value: Float): Int {
|
||||
require(value.isFinite() && value >= -2147483648f && value < 2147483648f)
|
||||
return value.toInt()
|
||||
}
|
||||
try {
|
||||
require(reader.remaining <= MAX_BYTES)
|
||||
while (reader.remaining > 0) {
|
||||
when (val op = reader.readU8()) {
|
||||
PUSH_I32 -> push(ExtendedVarint.read(reader, true))
|
||||
PUSH_F32 -> {
|
||||
val value = reader.readU8() or (reader.readU8() shl 8) or
|
||||
(reader.readU8() shl 16) or (reader.readU8() shl 24)
|
||||
pushFloat(Float.fromBits(value))
|
||||
}
|
||||
DUP -> {
|
||||
require(size > 0)
|
||||
push(bits[size - 1], floats[size - 1])
|
||||
}
|
||||
DROP -> { require(size > 0); size-- }
|
||||
SWAP -> {
|
||||
require(size >= 2)
|
||||
val value = bits[size - 1]
|
||||
val type = floats[size - 1]
|
||||
bits[size - 1] = bits[size - 2]
|
||||
floats[size - 1] = floats[size - 2]
|
||||
bits[size - 2] = value
|
||||
floats[size - 2] = type
|
||||
}
|
||||
IADD, ISUB, IMUL, IDIV, IMOD, IMIN, IMAX -> {
|
||||
val b = integer()
|
||||
val a = integer()
|
||||
push(when (op) {
|
||||
IADD -> a + b
|
||||
ISUB -> a - b
|
||||
IMUL -> a * b
|
||||
IDIV -> { require(b != 0); if (a == Int.MIN_VALUE && b == -1) a else a / b }
|
||||
IMOD -> { require(b != 0); if (a == Int.MIN_VALUE && b == -1) 0 else a % b }
|
||||
IMIN -> minOf(a, b)
|
||||
else -> maxOf(a, b)
|
||||
})
|
||||
}
|
||||
INEG -> push(-integer())
|
||||
FADD, FSUB, FMUL, FDIV, FMIN, FMAX -> {
|
||||
val b = float()
|
||||
val a = float()
|
||||
pushFloat(when (op) {
|
||||
FADD -> a + b
|
||||
FSUB -> a - b
|
||||
FMUL -> a * b
|
||||
FDIV -> { require(b != 0f); a / b }
|
||||
FMIN -> minOf(a, b)
|
||||
else -> maxOf(a, b)
|
||||
})
|
||||
}
|
||||
FNEG -> pushFloat(-float())
|
||||
I2F -> pushFloat(integer().toFloat())
|
||||
F2I -> push(toInteger(float()))
|
||||
TIME -> {
|
||||
val maximum = integer()
|
||||
require(maximum >= 0)
|
||||
val time = frame.elapsedMs.coerceIn(0, maximum.toLong()).toInt()
|
||||
if (time != maximum) pending = true
|
||||
push(time)
|
||||
}
|
||||
LERP -> {
|
||||
val t = float()
|
||||
val b = float()
|
||||
val a = float()
|
||||
pushFloat(a + (b - a) * t)
|
||||
}
|
||||
in SMOOTHSTEP..EASE_IN_OUT_CUBIC -> {
|
||||
val t = float().coerceIn(0f, 1f)
|
||||
val u = 1f - t
|
||||
pushFloat(when (op) {
|
||||
SMOOTHSTEP -> t * t * (3f - 2f * t)
|
||||
EASE_IN_QUAD -> t * t
|
||||
EASE_OUT_QUAD -> 1f - u * u
|
||||
EASE_IN_OUT_QUAD -> if (t < .5f) 2f * t * t else 1f - 2f * u * u
|
||||
EASE_IN_CUBIC -> t * t * t
|
||||
EASE_OUT_CUBIC -> 1f - u * u * u
|
||||
else -> if (t < .5f) 4f * t * t * t else 1f - 4f * u * u * u
|
||||
})
|
||||
}
|
||||
else -> error("Unknown expression opcode")
|
||||
}
|
||||
}
|
||||
require(size > 0)
|
||||
val result = if (floats[size - 1]) toInteger(float()) else integer()
|
||||
frame.animationPending = frame.animationPending || pending
|
||||
return result
|
||||
} catch (_: IllegalArgumentException) {
|
||||
return 0
|
||||
} catch (_: IllegalStateException) {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
+227
-40
@@ -1,6 +1,6 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Revision 26 wire grammar, shared by scene planning and the local renderer. */
|
||||
/** Revision 27 wire grammar, shared by scene planning and the local renderer. */
|
||||
object DrawProtocol {
|
||||
/** Mirrors g2flash/patches/zlib_glue.c. */
|
||||
const val DRAW = CFW_MSG_DRAW_CALLS
|
||||
@@ -18,87 +18,274 @@ object DrawProtocol {
|
||||
/** Mirrors g2flash/patches/display_list.c. */
|
||||
const val SCREEN = 65535
|
||||
const val CURRENT = 65534
|
||||
|
||||
/** Fixed-width resource/legacy fields, separate from the draw numeric codec. */
|
||||
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())
|
||||
|
||||
private fun encode(write: DrawWriter.() -> Unit): ByteArray = DrawWriter().apply(write).toByteArray()
|
||||
|
||||
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 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
|
||||
val flags = (if (target == null) 0 else DRAW_FLAG_RESOURCE_TARGET) or
|
||||
(if (depth == null) 0 else DRAW_FLAG_DEPTH)
|
||||
return encode {
|
||||
writeU8(op)
|
||||
writeU8(flags)
|
||||
if (target != null) writeU16(target)
|
||||
if (depth != null) writeS8(depth)
|
||||
writeBytes(args)
|
||||
}
|
||||
}
|
||||
|
||||
private fun call(op: Int, target: Int? = null, depth: Int? = null, write: DrawWriter.() -> Unit): ByteArray =
|
||||
call(op, encode(write), target, depth)
|
||||
|
||||
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 = 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 roundedRect(
|
||||
x: Int, y: Int, width: Int, height: Int, radius: Int, background: Int,
|
||||
border: Int = DRAW_ROUNDED_RECT_NO_BORDER, depth: Int? = null,
|
||||
): ByteArray = roundedRect(DrawValue.Integer(x), DrawValue.Integer(y), width, height, radius, background, border, depth)
|
||||
|
||||
fun roundedRect(
|
||||
x: DrawValue, y: DrawValue, 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)
|
||||
require(background in 0..15 && border in 0..DRAW_ROUNDED_RECT_NO_BORDER)
|
||||
return call(DRAW_OP_ROUNDED_RECT, depth = depth) {
|
||||
writeExtended(x)
|
||||
writeExtended(y)
|
||||
writeU16(width)
|
||||
writeU16(height)
|
||||
writeU16(radius)
|
||||
writeU8(background)
|
||||
writeU8(border)
|
||||
}
|
||||
}
|
||||
|
||||
fun sequence(calls: List<ByteArray>): ByteArray {
|
||||
require(calls.size <= 4096)
|
||||
val out = ByteSink(); out.write(word(calls.size))
|
||||
for (call in calls) { require(call.size <= 65535); out.write(word(call.size)); out.write(call) }
|
||||
return out.toByteArray()
|
||||
return encode {
|
||||
writeU16(calls.size)
|
||||
for (call in calls) {
|
||||
require(call.size <= 65535)
|
||||
writeU16(call.size)
|
||||
writeBytes(call)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 = 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 root(id: Int) = byteArrayOf(ROOT.toByte()) + encode { writeU16(id) }
|
||||
|
||||
fun image(
|
||||
id: Int, x: Int, y: Int, options: Int = CFW_TEXTURE_OPT_BRIGHTNESS_MASK,
|
||||
target: Int? = null, depth: Int? = null,
|
||||
): ByteArray = call(DRAW_OP_IMAGE, target, depth) {
|
||||
writeU16(id)
|
||||
writeS16(x)
|
||||
writeS16(y)
|
||||
writeU8(options)
|
||||
}
|
||||
|
||||
fun rectCopy(
|
||||
source: Int, x: Int, y: Int, width: Int, height: Int, dx: Int, dy: Int,
|
||||
target: Int? = null, depth: Int? = null,
|
||||
): ByteArray = call(DRAW_OP_RECT_COPY, target, depth) {
|
||||
writeU16(source)
|
||||
writeU16(x)
|
||||
writeU16(y)
|
||||
writeU16(width)
|
||||
writeU16(height)
|
||||
writeS16(dx)
|
||||
writeS16(dy)
|
||||
}
|
||||
|
||||
fun stockText(x: Int, y: Int, options: Int, text: ByteArray): ByteArray {
|
||||
require(text.size <= 255)
|
||||
return call(DRAW_OP_STOCK_FONT_STRING) {
|
||||
writeS16(x)
|
||||
writeS16(y)
|
||||
writeU8(options)
|
||||
writeU8(text.size)
|
||||
writeBytes(text)
|
||||
}
|
||||
}
|
||||
|
||||
fun text(id: Int, x: Int, y: Int, options: Int, text: ByteArray): ByteArray {
|
||||
require(text.size <= 255)
|
||||
return call(DRAW_OP_TEXT) {
|
||||
writeU16(id)
|
||||
writeS16(x)
|
||||
writeS16(y)
|
||||
writeU8(options)
|
||||
writeU8(text.size)
|
||||
writeBytes(text)
|
||||
}
|
||||
}
|
||||
|
||||
fun playList(id: Int, target: Int? = null, depth: Int? = null): ByteArray =
|
||||
call(DRAW_OP_DISPLAY_LIST, target, depth) { writeU16(id) }
|
||||
|
||||
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()
|
||||
(((i * 2 * factor / 256).coerceIn(0, 15) shl 4) or
|
||||
((i * 2 + 1) * factor / 256).coerceIn(0, 15)).toByte()
|
||||
}
|
||||
return call(DRAW_OP_REMAP_COLORS) {
|
||||
writeU16(0)
|
||||
writeU16(0)
|
||||
writeU16(width)
|
||||
writeU16(height)
|
||||
writeBytes(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)
|
||||
val result = byteArrayOf(LARGE.toByte()) + word(width) + word(height) + pixels
|
||||
require(result.size <= ResourceCacheState.MAX_RESOURCE_SIZE)
|
||||
return result
|
||||
}
|
||||
|
||||
private fun boundingBox(
|
||||
x: Int, y: Int, width: Int, height: Int, compact: Boolean, rle: ByteArray, target: Int? = null,
|
||||
): ByteArray = call(DRAW_OP_BOUNDING_BOX, target) {
|
||||
writeU8(if (compact) 0 else DRAW_BBOX_FLAG_U16)
|
||||
if (compact) {
|
||||
writeU8(x / 4)
|
||||
writeU8(y / 2)
|
||||
writeU8(width / 4)
|
||||
writeU8(height / 2)
|
||||
} else {
|
||||
writeU16(x)
|
||||
writeU16(y)
|
||||
writeU16(width)
|
||||
writeU16(height)
|
||||
}
|
||||
writeBytes(rle)
|
||||
}
|
||||
|
||||
/** Packed rows -> RLE of exactly width*height pixels, omitting odd-row padding. */
|
||||
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(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
|
||||
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
|
||||
// Pixel RLE is an opaque byte block, with its own fixed-width run lengths.
|
||||
val out = ByteSink()
|
||||
var color = -1
|
||||
var count = 0
|
||||
fun flush() {
|
||||
if (count == 0) return
|
||||
if (count <= 15) out.write((count shl 4) or color)
|
||||
else { out.write(color); if (count <= 255) out.write(count) else { out.write(0); out.write(word(count)) } }
|
||||
if (count <= 15) {
|
||||
out.write((count shl 4) or color)
|
||||
} else {
|
||||
out.write(color)
|
||||
if (count <= 255) {
|
||||
out.write(count)
|
||||
} else {
|
||||
out.write(0)
|
||||
out.write(word(count))
|
||||
}
|
||||
}
|
||||
}
|
||||
for (yy in y until y + height) for (xx in x until x + width) {
|
||||
val value = (packed[yy * stride + xx / 2].toInt() ushr (if (xx % 2 == 0) 4 else 0)) and 15
|
||||
if (value != color || count == 65535) { flush(); color = value; count = 0 }
|
||||
val shift = if (xx % 2 == 0) 4 else 0
|
||||
val value = (packed[yy * stride + xx / 2].toInt() ushr shift) and 15
|
||||
if (value != color || count == 65535) {
|
||||
flush()
|
||||
color = value
|
||||
count = 0
|
||||
}
|
||||
count++
|
||||
}
|
||||
flush()
|
||||
return call(DRAW_OP_BOUNDING_BOX, out.toByteArray(), target)
|
||||
return boundingBox(x, y, width, height, compact, out.toByteArray(), target)
|
||||
}
|
||||
/** Internal optimizer formats are translated here; they are never sent to revision 26. */
|
||||
|
||||
/**
|
||||
* Optimizer records retain their internal fixed-width format. Decode their
|
||||
* numeric fields here so every outgoing draw field goes through DrawWriter.
|
||||
*/
|
||||
fun fromOptimized(payload: ByteArray, width: Int = 640, height: Int = 480): List<ByteArray> {
|
||||
return when (val mode = payload[0].toInt() and CFW_MSG_TYPE_MASK) {
|
||||
val reader = ArrayByteReader(payload)
|
||||
fun u8() = reader.get().toInt() and 255
|
||||
fun u16() = reader.getShort().toInt() and 65535
|
||||
fun s16() = reader.getShort().toInt()
|
||||
fun bytes(count: Int): ByteArray = ByteArray(count).also { reader.get(it) }
|
||||
val result = when (val mode = u8() 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
|
||||
val calls = ArrayList<ByteArray>()
|
||||
repeat(u8()) {
|
||||
calls.addAll(fromOptimized(bytes(u16()), width, height))
|
||||
}
|
||||
calls
|
||||
}
|
||||
CFW_MSG_BOUNDING_BOX -> {
|
||||
val x = u8() * 4
|
||||
val y = u8() * 2
|
||||
val w = u8() * 4
|
||||
val h = u8() * 2
|
||||
u16() // Frame ID belongs only to the optimizer record.
|
||||
listOf(boundingBox(x, y, w, h, true, bytes(reader.remaining())))
|
||||
}
|
||||
CFW_MSG_FULL_FRAME -> listOf(boundingBox(0, 0, width, height, false, bytes(reader.remaining())))
|
||||
CFW_MSG_STOCK_FONT_STRING -> {
|
||||
val x = s16()
|
||||
val y = s16()
|
||||
val options = u8()
|
||||
listOf(stockText(x, y, options, bytes(u8())))
|
||||
}
|
||||
CFW_MSG_CACHED_IMAGE -> {
|
||||
val id = u16()
|
||||
val x = s16()
|
||||
val y = s16()
|
||||
val options = u8()
|
||||
listOf(image(id, x, y, options))
|
||||
}
|
||||
CFW_MSG_CACHED_TEXT -> {
|
||||
val id = u16()
|
||||
val x = s16()
|
||||
val y = s16()
|
||||
val options = u8()
|
||||
listOf(text(id, x, y, options, bytes(u8())))
|
||||
}
|
||||
CFW_MSG_RECT_COPY -> {
|
||||
val x = u16()
|
||||
val y = u16()
|
||||
val w = u16()
|
||||
val h = u16()
|
||||
val dx = s16()
|
||||
val dy = s16()
|
||||
listOf(rectCopy(CURRENT, x, y, w, h, dx, dy))
|
||||
}
|
||||
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")
|
||||
}
|
||||
require(reader.remaining() == 0)
|
||||
return result
|
||||
}
|
||||
|
||||
fun messages(calls: List<ByteArray>, maxBytes: Int = 65535): List<ByteArray> {
|
||||
val result = ArrayList<ByteArray>(); var batch = ArrayList<ByteArray>(); var size = 3
|
||||
fun headerSize(count: Int) = 1 + encode { writeU16(count) }.size
|
||||
val result = ArrayList<ByteArray>()
|
||||
var batch = ArrayList<ByteArray>()
|
||||
var bodySize = 0
|
||||
for (call in calls) {
|
||||
require(call.size + 5 <= maxBytes)
|
||||
if (size + 2 + call.size > maxBytes) { result.add(message(batch)); batch = ArrayList(); size = 3 }
|
||||
batch.add(call); size += call.size + 2
|
||||
require(call.size <= 65535)
|
||||
val framedSize = encode { writeU16(call.size) }.size + call.size
|
||||
require(headerSize(1) + framedSize <= maxBytes)
|
||||
if (headerSize(batch.size + 1) + bodySize + framedSize > maxBytes || batch.size == 4096) {
|
||||
result.add(message(batch))
|
||||
batch = ArrayList()
|
||||
bodySize = 0
|
||||
}
|
||||
batch.add(call)
|
||||
bodySize += framedSize
|
||||
}
|
||||
if (batch.isNotEmpty()) result.add(message(batch))
|
||||
return result
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/**
|
||||
* Numeric fields in the draw grammar go through this codec. Most fields still
|
||||
* uses fixed-width little-endian values; rounded-rectangle x/y use extended varints and expressions. Byte blocks
|
||||
* (RLE, text, LUTs and already-encoded calls) retain their own formats.
|
||||
*/
|
||||
internal class DrawReader(
|
||||
private val bytes: ByteArray,
|
||||
start: Int = 0,
|
||||
private val end: Int = bytes.size,
|
||||
) {
|
||||
var position: Int = start
|
||||
private set
|
||||
val remaining: Int get() = end - position
|
||||
|
||||
init {
|
||||
require(start >= 0 && start <= end && end <= bytes.size)
|
||||
}
|
||||
|
||||
fun readU8(): Int {
|
||||
require(remaining >= 1) { "Truncated draw field" }
|
||||
return bytes[position++].toInt() and 255
|
||||
}
|
||||
|
||||
fun readS8(): Int = readU8().toByte().toInt()
|
||||
|
||||
fun readU16(): Int {
|
||||
val low = readU8()
|
||||
return low or (readU8() shl 8)
|
||||
}
|
||||
|
||||
fun readS16(): Int = readU16().toShort().toInt()
|
||||
|
||||
fun readBytes(count: Int): ByteArray {
|
||||
require(count >= 0 && count <= remaining) { "Truncated draw data" }
|
||||
val result = bytes.copyOfRange(position, position + count)
|
||||
position += count
|
||||
return result
|
||||
}
|
||||
|
||||
/** A bounded view prevents a truncated call from consuming the next call. */
|
||||
fun readSlice(count: Int): DrawReader {
|
||||
require(count >= 0 && count <= remaining) { "Truncated draw call" }
|
||||
val result = DrawReader(bytes, position, position + count)
|
||||
position += count
|
||||
return result
|
||||
}
|
||||
|
||||
fun requireDone() {
|
||||
require(remaining == 0) { "Trailing draw data" }
|
||||
}
|
||||
}
|
||||
|
||||
internal class DrawWriter {
|
||||
private val out = ByteSink()
|
||||
|
||||
fun writeU8(value: Int) = out.write(value)
|
||||
fun writeS8(value: Int) = out.write(value)
|
||||
fun writeU16(value: Int) {
|
||||
out.write(value)
|
||||
out.write(value ushr 8)
|
||||
}
|
||||
fun writeS16(value: Int) = writeU16(value)
|
||||
fun writeBytes(value: ByteArray) = out.write(value)
|
||||
fun writeExtended(value: DrawValue) = out.write(ExtendedVarint.write(value))
|
||||
fun toByteArray(): ByteArray = out.toByteArray()
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/**
|
||||
* PRESENT resets the clock; timer redraws preserve it. The owner serializes
|
||||
* calls and supplies a cancellable 45 ms scheduler on its rendering thread.
|
||||
*/
|
||||
class DisplayListPlayer(
|
||||
private val renderer: DisplayListRenderer,
|
||||
private val screen: DisplayListRenderer.Target,
|
||||
private val composition: DisplayListRenderer.Target,
|
||||
private val clock: () -> Long,
|
||||
private val schedule: (Int, () -> Unit) -> (() -> Unit),
|
||||
private val displayed: () -> Unit,
|
||||
) {
|
||||
private var root = DrawProtocol.SCREEN
|
||||
private var origin = 0L
|
||||
private var generation = 0
|
||||
private var cancel: (() -> Unit)? = null
|
||||
|
||||
fun present(root: Int) {
|
||||
stop()
|
||||
this.root = root
|
||||
origin = clock()
|
||||
draw(generation)
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
generation++
|
||||
cancel?.invoke()
|
||||
cancel = null
|
||||
}
|
||||
|
||||
private fun draw(token: Int) {
|
||||
if (token != generation) return
|
||||
cancel = null
|
||||
renderer.render(root, screen, composition, (clock() - origin).coerceAtLeast(0))
|
||||
displayed()
|
||||
if (token == generation && renderer.animationPending) {
|
||||
cancel = schedule(45) { draw(token) }
|
||||
}
|
||||
}
|
||||
}
|
||||
+324
-137
@@ -1,68 +1,132 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Reference interpreter. It uses the firmware's packed rows, clipping, LUT and call grammar. */
|
||||
class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private val builtin: ((Int, Int) -> BuiltinGlyph)? = null, private val rightLens: Boolean = false) {
|
||||
class DisplayListRenderer(
|
||||
private val resources: Map<Int, ByteArray>,
|
||||
private val builtin: ((Int, Int) -> BuiltinGlyph)? = null,
|
||||
private val rightLens: Boolean = false,
|
||||
) {
|
||||
class BuiltinGlyph(val image: ByteArray, val advance: Int, val x: Int = 0, val y: Int = 0)
|
||||
class Target(val bytes: ByteArray, val width: Int, val height: Int, val offset: Int = 0, val shiftX: Int = 0) {
|
||||
|
||||
class Target(
|
||||
val bytes: ByteArray,
|
||||
val width: Int,
|
||||
val height: Int,
|
||||
val offset: Int = 0,
|
||||
val shiftX: Int = 0,
|
||||
) {
|
||||
val stride = (width + 1) / 2
|
||||
init { require(width > 0 && height > 0 && offset >= 0 && bytes.size - offset >= stride * height) }
|
||||
fun get(x: Int, y: Int) = (bytes[offset + y * stride + x / 2].toInt() ushr (if (x % 2 == 0) 4 else 0)) and 15
|
||||
|
||||
init {
|
||||
require(width > 0 && height > 0 && offset >= 0 && bytes.size - offset >= stride * height)
|
||||
}
|
||||
|
||||
fun get(x: Int, y: Int): Int {
|
||||
val shift = if (x % 2 == 0) 4 else 0
|
||||
return (bytes[offset + y * stride + x / 2].toInt() ushr shift) and 15
|
||||
}
|
||||
|
||||
fun shifted(dx: Int) = Target(bytes, width, height, offset, shiftX + dx)
|
||||
fun put(localX: Int, y: Int, v: Int) {
|
||||
|
||||
fun put(localX: Int, y: Int, value: Int) {
|
||||
val x = localX + shiftX
|
||||
if (x !in 0 until width || y !in 0 until height) return
|
||||
val index = offset + y * stride + x / 2; val old = bytes[index].toInt()
|
||||
bytes[index] = (if (x % 2 == 0) (old and 15) or (v shl 4) else (old and 240) or v).toByte()
|
||||
val index = offset + y * stride + x / 2
|
||||
val old = bytes[index].toInt()
|
||||
bytes[index] = if (x % 2 == 0) {
|
||||
((old and 15) or (value shl 4)).toByte()
|
||||
} else {
|
||||
((old and 240) or value).toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
private fun roundedContains(x: Int, y: Int, w: Int, h: Int, radius: Int): Boolean {
|
||||
if (x !in 0 until w || y !in 0 until h) return false
|
||||
val r = minOf(radius, w / 2, h / 2)
|
||||
val dx = maxOf(0, 2 * r - (2 * minOf(x, w - 1 - x) + 1))
|
||||
val dy = maxOf(0, 2 * r - (2 * minOf(y, h - 1 - y) + 1))
|
||||
return dx * dx + dy * dy <= 4 * r * r
|
||||
|
||||
/** Shared by nested calls, and reset between validation and drawing. */
|
||||
var animationPending: Boolean = false
|
||||
private set
|
||||
|
||||
private class Walk(val screen: Target, val apply: Boolean, val references: MutableSet<Int>, val frame: DrawEvaluation) {
|
||||
val ancestors = mutableListOf<Int>()
|
||||
var remainingCalls = 4096
|
||||
}
|
||||
|
||||
private class Image(val width: Int, val height: Int, val pixels: IntArray)
|
||||
private fun word(b: ByteArray, p: Int): Int { require(p >= 0 && p + 2 <= b.size); return DrawProtocol.u16(b, p) }
|
||||
private fun signed(b: ByteArray, p: Int) = word(b, p).toShort().toInt()
|
||||
|
||||
private fun resource(id: Int) = requireNotNull(resources[id]) { "Missing resource $id" }
|
||||
private fun decodeRle(b: ByteArray, start: Int, count: Int, exact: Boolean): IntArray {
|
||||
var p = start; var i = 0; val result = IntArray(count)
|
||||
while (i < count) {
|
||||
require(p < b.size); val token = b[p++].toInt() and 255; var n = token ushr 4
|
||||
if (n == 0) { require(p < b.size); n = b[p++].toInt() and 255
|
||||
if (n == 0) { n = word(b, p); p += 2 } }
|
||||
require(n > 0 && n <= count - i); result.fill(token and 15, i, i + n); i += n
|
||||
|
||||
// Resource headers, font offsets and RLE have their own fixed-width grammar;
|
||||
// they do not use the draw-call numeric codec.
|
||||
private fun readU8(reader: ByteReader): Int = reader.get().toInt() and 255
|
||||
private fun readU16(reader: ByteReader): Int = reader.getShort().toInt() and 65535
|
||||
|
||||
private fun decodeRle(reader: ByteReader, count: Int, exact: Boolean): IntArray {
|
||||
val pixels = IntArray(count)
|
||||
var index = 0
|
||||
while (index < count) {
|
||||
val token = readU8(reader)
|
||||
var run = token ushr 4
|
||||
if (run == 0) {
|
||||
run = readU8(reader)
|
||||
if (run == 0) run = readU16(reader)
|
||||
}
|
||||
require(run > 0 && run <= count - index)
|
||||
pixels.fill(token and 15, index, index + run)
|
||||
index += run
|
||||
}
|
||||
require(!exact || p == b.size)
|
||||
return result
|
||||
require(!exact || reader.remaining() == 0)
|
||||
return pixels
|
||||
}
|
||||
private fun image(b: ByteArray, start: Int = 0): Image {
|
||||
require(start in b.indices); val flags = b[start].toInt() and 255
|
||||
|
||||
private fun image(bytes: ByteArray, start: Int = 0): Image {
|
||||
require(start in bytes.indices)
|
||||
val reader = ArrayByteReader(bytes, start)
|
||||
val flags = readU8(reader)
|
||||
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 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)
|
||||
val large = flags and CFW_RESOURCE_FLAG_LARGE != 0
|
||||
val width = if (large) readU16(reader) else readU8(reader)
|
||||
val height = if (large) readU16(reader) else readU8(reader)
|
||||
require(width in 1..640 && height in 1..480)
|
||||
val pixels = if (flags and CFW_RESOURCE_FLAG_RLE != 0) {
|
||||
decodeRle(reader, width * height, false)
|
||||
} else {
|
||||
val offset = bytes.size - reader.remaining()
|
||||
val target = Target(bytes, width, height, offset)
|
||||
IntArray(width * height) { target.get(it % width, it / width) }
|
||||
}
|
||||
return Image(width, height, pixels)
|
||||
}
|
||||
|
||||
private fun target(id: Int): Target {
|
||||
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 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)
|
||||
val bytes = resource(id)
|
||||
val reader = ArrayByteReader(bytes)
|
||||
val flags = readU8(reader)
|
||||
require(flags and (CFW_RESOURCE_TYPE_MASK or CFW_RESOURCE_FLAG_RLE) == 0)
|
||||
image(bytes) // Validate the complete raw image before exposing a write target.
|
||||
val large = flags and CFW_RESOURCE_FLAG_LARGE != 0
|
||||
val width = if (large) readU16(reader) else readU8(reader)
|
||||
val height = if (large) readU16(reader) else readU8(reader)
|
||||
return Target(bytes, width, height, bytes.size - reader.remaining())
|
||||
}
|
||||
|
||||
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 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)
|
||||
val value = image.pixels[i]
|
||||
if (value == 0 && options and CFW_TEXTURE_OPT_TRANSPARENT != 0) continue
|
||||
val source = if (options and CFW_TEXTURE_OPT_INVERSE != 0) 15 - value else value
|
||||
val brightness = source * (options and CFW_TEXTURE_OPT_BRIGHTNESS_MASK) / 15
|
||||
target.put(x + i % image.width, y + i / image.width, brightness)
|
||||
}
|
||||
}
|
||||
|
||||
private fun roundedContains(x: Int, y: Int, width: Int, height: Int, radius: Int): Boolean {
|
||||
if (x !in 0 until width || y !in 0 until height) return false
|
||||
val r = minOf(radius, width / 2, height / 2)
|
||||
val dx = maxOf(0, 2 * r - (2 * minOf(x, width - 1 - x) + 1))
|
||||
val dy = maxOf(0, 2 * r - (2 * minOf(y, height - 1 - y) + 1))
|
||||
return dx * dx + dy * dy <= 4 * r * r
|
||||
}
|
||||
|
||||
private fun copyScreen(screen: Target, composition: Target) {
|
||||
val source = screen.bytes
|
||||
val destination = composition.bytes
|
||||
@@ -93,107 +157,230 @@ class DisplayListRenderer(private val resources: Map<Int, ByteArray>, private va
|
||||
dst += destinationStride
|
||||
}
|
||||
}
|
||||
|
||||
/** Validate the whole graph before changing any pixels. Resource writes remain call-scoped. */
|
||||
fun render(root: Int, screen: Target, composition: Target): Set<Int> {
|
||||
fun render(root: Int, screen: Target, composition: Target, elapsedMs: Long = 0): Set<Int> {
|
||||
animationPending = false
|
||||
val frame = DrawEvaluation(elapsedMs)
|
||||
val references = mutableSetOf<Int>()
|
||||
for (apply in listOf(false, true)) {
|
||||
if (apply) copyScreen(screen, composition)
|
||||
if (root != DrawProtocol.SCREEN) executeList(root, composition, screen, apply, mutableListOf(), intArrayOf(4096), references)
|
||||
}
|
||||
return references
|
||||
}
|
||||
fun execute(sequence: ByteArray, target: Target, screen: Target = target): Set<Int> {
|
||||
val references = mutableSetOf<Int>()
|
||||
for (apply in listOf(false, true)) executeSequence(sequence, target, screen, apply, mutableListOf(), intArrayOf(4096), references)
|
||||
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() == 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>) {
|
||||
val count = word(b, 0); var pos = 2
|
||||
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 and DRAW_FLAGS_MASK.inv() == 0)
|
||||
var t = inherited; var header = 2
|
||||
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()) {
|
||||
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
|
||||
val w = if (wide) word(p, 5) else (p[3].toInt() and 255) * 4
|
||||
val h = if (wide) word(p, 7) else (p[4].toInt() and 255) * 2
|
||||
require(w > 0 && h > 0 && x + w <= t.width && y + h <= t.height)
|
||||
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])
|
||||
}
|
||||
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)
|
||||
require(w > 0 && h > 0 && x + w <= src.width && y + h <= src.height)
|
||||
val dx = signed(p, 10); val dy = signed(p, 12)
|
||||
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]) }
|
||||
}
|
||||
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
|
||||
while (i < text.length) { val c = text[i++].code
|
||||
cps.add(if (c in 0xd800..0xdbff) { require(i < text.length); 0x10000 + ((c - 0xd800) shl 10) + text[i++].code - 0xdc00 } else c) }
|
||||
var x = signed(p, 0); val y = signed(p, 2)
|
||||
for (j in cps.indices) { val cp = cps[j]
|
||||
if (cp in 1..31) { x += cp - 11; continue }
|
||||
val next = cps.drop(j + 1).firstOrNull { it !in 1..31 } ?: 0
|
||||
val g = requireNotNull(builtin) { "Builtin font provider required" }(cp, next)
|
||||
draw(image(g.image), t, x + g.x, y + g.y, p[4].toInt() and 255, apply); x += g.advance
|
||||
}
|
||||
}
|
||||
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() == 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)
|
||||
val glyph = image(data, offset); draw(glyph, t, x, y, options, apply); x += glyph.width
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
if (xx + t.shiftX !in 0 until t.width) continue
|
||||
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) }
|
||||
}
|
||||
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 <= 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 < DRAW_ROUNDED_RECT_NO_BORDER) border else maxOf(fill, t.get(tx, ty)))
|
||||
}
|
||||
}
|
||||
DRAW_OP_DISPLAY_LIST -> { require(p.size == 2); executeList(word(p, 0), t, screen, apply, stack, budget, refs) }
|
||||
else -> error("Unknown draw opcode")
|
||||
if (root != DrawProtocol.SCREEN) {
|
||||
executeList(root, composition, Walk(screen, apply, references, frame))
|
||||
}
|
||||
}
|
||||
require(pos == b.size)
|
||||
animationPending = frame.animationPending
|
||||
return references
|
||||
}
|
||||
|
||||
fun execute(sequence: ByteArray, target: Target, screen: Target = target, elapsedMs: Long = 0): Set<Int> {
|
||||
animationPending = false
|
||||
val frame = DrawEvaluation(elapsedMs)
|
||||
val references = mutableSetOf<Int>()
|
||||
for (apply in listOf(false, true)) {
|
||||
executeSequence(DrawReader(sequence), target, Walk(screen, apply, references, frame))
|
||||
}
|
||||
animationPending = frame.animationPending
|
||||
return references
|
||||
}
|
||||
|
||||
private fun executeList(id: Int, target: Target, walk: Walk) {
|
||||
require(walk.ancestors.size < 8 && id !in walk.ancestors)
|
||||
val bytes = resource(id)
|
||||
require(bytes.size >= 3 && bytes[0].toInt() == CFW_RESOURCE_TYPE_DISPLAY_LIST)
|
||||
walk.references.add(id)
|
||||
walk.ancestors.add(id)
|
||||
executeSequence(DrawReader(bytes, start = 1), target, walk)
|
||||
walk.ancestors.removeAt(walk.ancestors.lastIndex)
|
||||
}
|
||||
|
||||
private fun executeSequence(reader: DrawReader, inherited: Target, walk: Walk) {
|
||||
val count = reader.readU16()
|
||||
repeat(count) {
|
||||
val length = reader.readU16()
|
||||
require(walk.remainingCalls > 0)
|
||||
walk.remainingCalls--
|
||||
executeCall(reader.readSlice(length), inherited, walk)
|
||||
}
|
||||
reader.requireDone()
|
||||
}
|
||||
|
||||
private fun executeCall(reader: DrawReader, inherited: Target, walk: Walk) {
|
||||
val opcode = reader.readU8()
|
||||
val flags = reader.readU8()
|
||||
require(flags and DRAW_FLAGS_MASK.inv() == 0)
|
||||
var target = inherited
|
||||
if (flags and DRAW_FLAG_RESOURCE_TARGET != 0) {
|
||||
val id = reader.readU16()
|
||||
walk.references.add(id)
|
||||
target = target(id).shifted(inherited.shiftX)
|
||||
}
|
||||
if (flags and DRAW_FLAG_DEPTH != 0) {
|
||||
target = target.shifted(DrawProtocol.depthOffset(reader.readS8(), rightLens))
|
||||
}
|
||||
when (opcode) {
|
||||
DRAW_OP_BOUNDING_BOX -> drawBoundingBox(reader, target, walk)
|
||||
DRAW_OP_RECT_COPY -> drawRectCopy(reader, target, walk)
|
||||
DRAW_OP_STOCK_FONT_STRING -> drawStockText(reader, target, walk)
|
||||
DRAW_OP_IMAGE -> drawImage(reader, target, walk)
|
||||
DRAW_OP_TEXT -> drawText(reader, target, walk)
|
||||
DRAW_OP_REMAP_COLORS -> remapColors(reader, target, walk)
|
||||
DRAW_OP_ROUNDED_RECT -> drawRoundedRect(reader, target, walk)
|
||||
DRAW_OP_DISPLAY_LIST -> {
|
||||
val id = reader.readU16()
|
||||
reader.requireDone()
|
||||
executeList(id, target, walk)
|
||||
}
|
||||
else -> error("Unknown draw opcode $opcode")
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawBoundingBox(reader: DrawReader, target: Target, walk: Walk) {
|
||||
val flags = reader.readU8()
|
||||
require(flags and DRAW_BBOX_FLAG_U16.inv() == 0)
|
||||
val wide = flags and DRAW_BBOX_FLAG_U16 != 0
|
||||
val x = if (wide) reader.readU16() else reader.readU8() * 4
|
||||
val y = if (wide) reader.readU16() else reader.readU8() * 2
|
||||
val width = if (wide) reader.readU16() else reader.readU8() * 4
|
||||
val height = if (wide) reader.readU16() else reader.readU8() * 2
|
||||
require(width > 0 && height > 0 && x + width <= target.width && y + height <= target.height)
|
||||
val rle = ArrayByteReader(reader.readBytes(reader.remaining))
|
||||
val pixels = decodeRle(rle, width * height, true)
|
||||
if (walk.apply) {
|
||||
for (i in pixels.indices) target.put(x + i % width, y + i / width, pixels[i])
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawRectCopy(reader: DrawReader, target: Target, walk: Walk) {
|
||||
val id = reader.readU16()
|
||||
val x = reader.readU16()
|
||||
val y = reader.readU16()
|
||||
val width = reader.readU16()
|
||||
val height = reader.readU16()
|
||||
val dx = reader.readS16()
|
||||
val dy = reader.readS16()
|
||||
reader.requireDone()
|
||||
val source = when (id) {
|
||||
DrawProtocol.SCREEN -> walk.screen
|
||||
DrawProtocol.CURRENT -> target
|
||||
else -> {
|
||||
walk.references.add(id)
|
||||
target(id)
|
||||
}
|
||||
}
|
||||
require(width > 0 && height > 0 && x + width <= source.width && y + height <= source.height)
|
||||
if (walk.apply) {
|
||||
// Snapshot the rectangle so overlapping copies have memmove semantics.
|
||||
val copy = IntArray(width * height) { source.get(x + it % width, y + it / width) }
|
||||
for (i in copy.indices) target.put(dx + i % width, dy + i / width, copy[i])
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawStockText(reader: DrawReader, target: Target, walk: Walk) {
|
||||
var x = reader.readS16()
|
||||
val y = reader.readS16()
|
||||
val options = reader.readU8()
|
||||
val bytes = reader.readBytes(reader.readU8())
|
||||
reader.requireDone()
|
||||
val text = bytes.decodeToString(throwOnInvalidSequence = true)
|
||||
val codePoints = ArrayList<Int>()
|
||||
var index = 0
|
||||
while (index < text.length) {
|
||||
val char = text[index++].code
|
||||
val codePoint = if (char in 0xd800..0xdbff) {
|
||||
require(index < text.length)
|
||||
0x10000 + ((char - 0xd800) shl 10) + text[index++].code - 0xdc00
|
||||
} else {
|
||||
char
|
||||
}
|
||||
codePoints.add(codePoint)
|
||||
}
|
||||
for (i in codePoints.indices) {
|
||||
val codePoint = codePoints[i]
|
||||
if (codePoint in 1..31) {
|
||||
x += codePoint - 11
|
||||
continue
|
||||
}
|
||||
val next = codePoints.drop(i + 1).firstOrNull { it !in 1..31 } ?: 0
|
||||
val glyph = requireNotNull(builtin) { "Builtin font provider required" }(codePoint, next)
|
||||
draw(image(glyph.image), target, x + glyph.x, y + glyph.y, options, walk.apply)
|
||||
x += glyph.advance
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawImage(reader: DrawReader, target: Target, walk: Walk) {
|
||||
val id = reader.readU16()
|
||||
val x = reader.readS16()
|
||||
val y = reader.readS16()
|
||||
val options = reader.readU8()
|
||||
reader.requireDone()
|
||||
walk.references.add(id)
|
||||
draw(image(resource(id)), target, x, y, options, walk.apply)
|
||||
}
|
||||
|
||||
private fun drawText(reader: DrawReader, target: Target, walk: Walk) {
|
||||
val id = reader.readU16()
|
||||
var x = reader.readS16()
|
||||
val y = reader.readS16()
|
||||
val options = reader.readU8()
|
||||
val text = reader.readBytes(reader.readU8())
|
||||
reader.requireDone()
|
||||
walk.references.add(id)
|
||||
val data = resource(id)
|
||||
require(data.size >= 193 && data[0].toInt() == CFW_RESOURCE_TYPE_FONT)
|
||||
for (byte in text) {
|
||||
val char = byte.toInt() and 255
|
||||
if (char in 1..31) {
|
||||
x += char - 11
|
||||
continue
|
||||
}
|
||||
require(char in 32..127)
|
||||
val offset = DrawProtocol.u16(data, 1 + (char - 32) * 2)
|
||||
require(offset >= 193)
|
||||
val glyph = image(data, offset)
|
||||
draw(glyph, target, x, y, options, walk.apply)
|
||||
x += glyph.width
|
||||
}
|
||||
}
|
||||
|
||||
private fun remapColors(reader: DrawReader, target: Target, walk: Walk) {
|
||||
val x = reader.readU16()
|
||||
val y = reader.readU16()
|
||||
val width = reader.readU16()
|
||||
val height = reader.readU16()
|
||||
val table = reader.readBytes(8)
|
||||
reader.requireDone()
|
||||
require(width > 0 && height > 0 && x + width <= target.width && y + height <= target.height)
|
||||
if (!walk.apply) return
|
||||
for (yy in y until y + height) for (xx in x until x + width) {
|
||||
if (xx + target.shiftX !in 0 until target.width) continue
|
||||
val value = target.get(xx + target.shiftX, yy)
|
||||
val shift = if (value % 2 == 0) 4 else 0
|
||||
target.put(xx, yy, (table[value / 2].toInt() ushr shift) and 15)
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawRoundedRect(reader: DrawReader, target: Target, walk: Walk) {
|
||||
val x = ExtendedVarint.evaluate(reader, walk.frame)
|
||||
val y = ExtendedVarint.evaluate(reader, walk.frame)
|
||||
val width = reader.readU16()
|
||||
val height = reader.readU16()
|
||||
val radius = reader.readU16()
|
||||
val fill = reader.readU8()
|
||||
val border = reader.readU8()
|
||||
reader.requireDone()
|
||||
require(width in 1..640 && height in 1..480 && fill <= 15 && border <= DRAW_ROUNDED_RECT_NO_BORDER)
|
||||
if (!walk.apply) return
|
||||
if (x < -width - target.shiftX || x >= target.width - target.shiftX ||
|
||||
y < -height || y >= target.height) return
|
||||
for (yy in 0 until height) for (xx in 0 until width) {
|
||||
if (!roundedContains(xx, yy, width, height, radius)) continue
|
||||
val tx = x + xx + target.shiftX
|
||||
val ty = y + yy
|
||||
if (tx !in 0 until target.width || ty !in 0 until target.height) continue
|
||||
val edge = !roundedContains(xx - 1, yy - 1, width - 2, height - 2, maxOf(0, radius - 1))
|
||||
val value = if (edge && border < DRAW_ROUNDED_RECT_NO_BORDER) border else maxOf(fill, target.get(tx, ty))
|
||||
target.put(x + xx, ty, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** One-shot scheduler; the owner acquires its rendering lock in [action]. */
|
||||
internal expect fun scheduleDrawRedraw(delayMs: Int, action: () -> Unit): () -> Unit
|
||||
|
||||
private val drawTimeOrigin = kotlin.time.TimeSource.Monotonic.markNow()
|
||||
internal fun drawAnimationTimeMs(): Long = drawTimeOrigin.elapsedNow().inWholeMilliseconds
|
||||
+28
-12
@@ -2,28 +2,36 @@ package com.faceclaw.app
|
||||
|
||||
/** A retained selection or image, replayed at stereo depth over the screen and shell surfaces. */
|
||||
class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val radius: Int,
|
||||
val background: Int, val border: Int, val depth: Int, val packed: ByteArray, val occlusions: List<IntArray> = emptyList(), val kind: Int = 3, val mask: List<IntArray> = emptyList()) {
|
||||
val background: Int, val border: Int, val depth: Int, val packed: ByteArray, val occlusions: List<IntArray> = emptyList(), val kind: Int = 3, val mask: List<IntArray> = emptyList(), val animation: Animation? = null) {
|
||||
class Animation(val dx: Int, val dy: Int, val startedAt: Long, val token: Int)
|
||||
val resource = CachedResource(DrawProtocol.rawImage(width, height, packed))
|
||||
val fingerprint = "$kind,${mask.joinToString { it.joinToString() }},$x,$y,$width,$height,$radius,$background,$border,$depth,${resource.hash},${occlusions.joinToString { it.joinToString() }}"
|
||||
fun translated(dx: Int, dy: Int) = MenuSelection(x + dx, y + dy, width, height, radius, background, border, depth, packed, occlusions.map { intArrayOf(it[0] + dx, it[1] + dy, it[2], it[3]) }, kind, mask)
|
||||
fun calls(id: Int): List<ByteArray> {
|
||||
val fingerprint = "${animation?.token},${animation?.dx},${animation?.dy},$kind,${mask.joinToString { it.joinToString() }},$x,$y,$width,$height,$radius,$background,$border,$depth,${resource.hash},${occlusions.joinToString { it.joinToString() }}"
|
||||
fun translated(dx: Int, dy: Int) = MenuSelection(x + dx, y + dy, width, height, radius, background, border, depth, packed, occlusions.map { intArrayOf(it[0] + dx, it[1] + dy, it[2], it[3]) }, kind, mask, animation)
|
||||
fun calls(id: Int, nowMs: Long = drawAnimationTimeMs()): List<ByteArray> {
|
||||
val elapsed = animation?.let { (nowMs - it.startedAt).coerceIn(0, 500).toInt() } ?: 500
|
||||
val highlightX = animation?.let { DrawValue.animate(x + it.dx, x, 500, elapsed) } ?: DrawValue.Integer(x)
|
||||
val highlightY = animation?.let { DrawValue.animate(y + it.dy, y, 500, elapsed) } ?: DrawValue.Integer(y)
|
||||
val image = when (kind) {
|
||||
3 -> listOf(DrawProtocol.roundedRect(x, y, width, height, radius, background, border, depth),
|
||||
3 -> listOf(DrawProtocol.roundedRect(highlightX, highlightY, width, height, radius, background, border, 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(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) }
|
||||
5 -> mask.map { rect ->
|
||||
DrawProtocol.rectCopy(id, rect[0], rect[1], rect[2], rect[3],
|
||||
x + rect[0], y + rect[1], depth = depth)
|
||||
}
|
||||
else -> error("Invalid presentation kind")
|
||||
}
|
||||
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])) }
|
||||
return image + occlusions.map { rect ->
|
||||
DrawProtocol.rectCopy(DrawProtocol.SCREEN, rect[0], rect[1], rect[2], rect[3], rect[0], rect[1])
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/** The caller has consumed the presentation tag (3, 4 or 5). Colors cross the bridge in gray8, pixels in gray8 rows. */
|
||||
/** The caller has consumed the presentation tag (3–6). Colors cross the bridge in gray8, pixels in gray8 rows. */
|
||||
fun read(reader: ByteReader, kind: Int = 3): MenuSelection {
|
||||
require(kind in 3..5)
|
||||
require(kind in 3..6)
|
||||
require(reader.remaining() >= 15)
|
||||
val x = reader.getShort().toInt(); val y = reader.getShort().toInt()
|
||||
val w = reader.getShort().toInt() and 65535; val h = reader.getShort().toInt() and 65535
|
||||
@@ -34,6 +42,14 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
|
||||
val count = reader.getShort().toInt() and 65535
|
||||
require(count <= 2048)
|
||||
require(w in 1..640 && h in 1..480 && 5 + (w + 1) / 2 * h <= 65536 && reader.remaining() >= w * h)
|
||||
val animation = if (kind == 6) {
|
||||
require(reader.remaining() >= 10)
|
||||
val dx = reader.getShort().toInt()
|
||||
val dy = reader.getShort().toInt()
|
||||
val elapsed = reader.getShort().toInt() and 65535
|
||||
require(elapsed <= 500)
|
||||
Animation(dx, dy, drawAnimationTimeMs() - elapsed, reader.getInt())
|
||||
} else null
|
||||
val gray = ByteArray(w * h); reader.get(gray)
|
||||
require(reader.remaining() >= count * 8)
|
||||
val occlusions = List(count) { IntArray(4) { reader.getShort().toInt() and 65535 } }
|
||||
@@ -50,7 +66,7 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
|
||||
}
|
||||
}
|
||||
}
|
||||
return MenuSelection(x, y, w, h, radius, background, border, depth, BmpUtil.pack4bppFromGray8(gray, w, h), occlusions, kind, mask)
|
||||
return MenuSelection(x, y, w, h, radius, background, border, depth, BmpUtil.pack4bppFromGray8(gray, w, h), occlusions, if (kind == 6) 3 else kind, mask, animation)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,12 @@ class ShellScene(val layers: List<Layer>, val selections: List<MenuSelection> =
|
||||
val fingerprint: String = layers.joinToString(";") { "${it.key},${it.x},${it.y},${it.width},${it.height},${it.dim},${it.depth},${CachedResource(it.packed).hash},${it.selections.joinToString { row -> row.fingerprint }}" } + selections.joinToString { it.fingerprint }
|
||||
fun calls(width: Int, height: Int, surfaces: IntArray, selected: IntArray): List<ByteArray> {
|
||||
val calls = ArrayList<ByteArray>(); var rowId = 0
|
||||
for (row in selections) calls.addAll(row.calls(selected[rowId++]))
|
||||
val now = drawAnimationTimeMs()
|
||||
for (row in selections) calls.addAll(row.calls(selected[rowId++], now))
|
||||
for ((index, layer) in layers.withIndex()) {
|
||||
if (layer.dim < 256) calls.add(DrawProtocol.lut(width, height, layer.dim))
|
||||
calls.add(DrawProtocol.image(surfaces[index], layer.x, layer.y, depth = layer.depth))
|
||||
for (row in layer.selections) calls.addAll(row.calls(selected[rowId++]))
|
||||
for (row in layer.selections) calls.addAll(row.calls(selected[rowId++], now))
|
||||
}
|
||||
return calls
|
||||
}
|
||||
@@ -27,6 +28,36 @@ class ShellScene(val layers: List<Layer>, val selections: List<MenuSelection> =
|
||||
val target = DisplayListRenderer.Target(output, width, height)
|
||||
return ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() }
|
||||
}
|
||||
internal class AnimatedPreview(val player: DisplayListPlayer, var pixels: ByteArray)
|
||||
|
||||
internal fun animatedPreview(
|
||||
screenGray: ByteArray, width: Int, height: Int,
|
||||
schedule: (Int, () -> Unit) -> (() -> Unit),
|
||||
): AnimatedPreview {
|
||||
val screen = BmpUtil.pack4bppFromGray8(screenGray, width, height)
|
||||
val output = ByteArray(screen.size)
|
||||
val resources = HashMap<Int, ByteArray>()
|
||||
val surfaces = IntArray(layers.size) { id ->
|
||||
val layer = layers[id]
|
||||
resources[id] = DrawProtocol.rawImage(layer.width, layer.height, layer.packed)
|
||||
id
|
||||
}
|
||||
val rows = IntArray(allSelections.size) { id ->
|
||||
resources[id + layers.size] = allSelections[id].resource.bytes
|
||||
id + layers.size
|
||||
}
|
||||
resources[511] = DrawProtocol.displayList(calls(width, height, surfaces, rows))
|
||||
val target = DisplayListRenderer.Target(output, width, height)
|
||||
lateinit var preview: AnimatedPreview
|
||||
val player = DisplayListPlayer(DisplayListRenderer(resources),
|
||||
DisplayListRenderer.Target(screen, width, height), target,
|
||||
clock = { drawAnimationTimeMs() }, schedule = schedule,
|
||||
displayed = { preview.pixels = ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() } })
|
||||
preview = AnimatedPreview(player, screenGray)
|
||||
player.present(511)
|
||||
return preview
|
||||
}
|
||||
|
||||
companion object {
|
||||
val EMPTY = ShellScene(emptyList())
|
||||
fun decode(reader: ByteReader): ShellScene {
|
||||
|
||||
+28
-3
@@ -51,7 +51,7 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
|
||||
var out: MutableList<ScreenDraw> = ArrayList()
|
||||
while ((cursor.remaining() >= 1)) {
|
||||
var kind: Int = (cursor.get() and 0xff)
|
||||
if (kind in 3..5) { out.add(ScreenDraw.image(0, 0, 0).also { it.selection = MenuSelection.read(cursor, kind) }); continue }
|
||||
if (kind in 3..6) { out.add(ScreenDraw.image(0, 0, 0).also { it.selection = MenuSelection.read(cursor, kind) }); continue }
|
||||
if (((kind == ScreenDraw.KIND_GLYPH) && (cursor.remaining() >= 11))) {
|
||||
var fontId: Int = (cursor.getShort().toInt() and 0xffff)
|
||||
var encoding: Int = cursor.getInt()
|
||||
@@ -287,6 +287,18 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
|
||||
}
|
||||
|
||||
private val lock = protocolPlatform().createLock()
|
||||
private var animatedPreview: ShellScene.AnimatedPreview? = null
|
||||
private var previewKey: String? = null
|
||||
|
||||
private fun previewScene(scene: ShellScene, gray: ByteArray, key: String): ByteArray {
|
||||
if (previewKey != key) {
|
||||
animatedPreview?.player?.stop()
|
||||
animatedPreview = scene.animatedPreview(gray, screenWidth, screenHeight,
|
||||
schedule = { delay, action -> scheduleDrawRedraw(delay) { lock.withLock(action) } })
|
||||
previewKey = key
|
||||
}
|
||||
return animatedPreview!!.pixels
|
||||
}
|
||||
|
||||
private var screenWidth: Int = 0
|
||||
|
||||
@@ -602,6 +614,9 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
|
||||
private fun compositeLocked(includePreview: Boolean = includePreviewInFrames): Composite {
|
||||
var gray: ByteArray = ByteArray((screenWidth * screenHeight))
|
||||
if (blanked) {
|
||||
animatedPreview?.player?.stop()
|
||||
animatedPreview = null
|
||||
previewKey = null
|
||||
return Composite(
|
||||
gray,
|
||||
screenWidth,
|
||||
@@ -667,8 +682,18 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
|
||||
}
|
||||
}
|
||||
val scene = if (surfaces.values.any { it.visible && it.zOrder > 1 }) ShellScene.EMPTY else ShellScene(shellScene?.layers ?: emptyList(), selections)
|
||||
val preview = if (includePreview && (shellScene != null || selections.isNotEmpty())) scene.preview(gray, screenWidth, screenHeight) else gray
|
||||
return Composite(preview, screenWidth, screenHeight, fingerprint.append("|shell:").append(scene.fingerprint).toString(),
|
||||
val sceneKey = fingerprint.append("|shell:").append(scene.fingerprint).toString()
|
||||
val preview = if (includePreview && (shellScene != null || selections.isNotEmpty())) {
|
||||
previewScene(scene, gray, sceneKey)
|
||||
} else {
|
||||
if (includePreview) {
|
||||
animatedPreview?.player?.stop()
|
||||
animatedPreview = null
|
||||
previewKey = null
|
||||
}
|
||||
gray
|
||||
}
|
||||
return Composite(preview, screenWidth, screenHeight, sceneKey,
|
||||
nextCompositeSeq++, draws.toTypedArray()).also { it.screenGray = gray; it.shellScene = scene }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.jvm.JvmOverloads
|
||||
|
||||
/** Sequential little-endian input. Platform adapters can read native buffers without a copy. */
|
||||
abstract class ByteReader {
|
||||
abstract fun remaining(): Int
|
||||
@@ -23,14 +25,28 @@ abstract class ByteReader {
|
||||
fun getInt(): Int = (getShort().toInt() and 65535) or (getShort().toInt() shl 16)
|
||||
}
|
||||
|
||||
class ArrayByteReader(private val bytes: ByteArray) : ByteReader() {
|
||||
private var position = 0
|
||||
class ArrayByteReader @JvmOverloads constructor(
|
||||
private val bytes: ByteArray,
|
||||
private val start: Int = 0,
|
||||
private val end: Int = bytes.size,
|
||||
) : ByteReader() {
|
||||
private var position = start
|
||||
|
||||
override fun remaining(): Int = bytes.size - position
|
||||
init {
|
||||
require(start >= 0 && start <= end && end <= bytes.size)
|
||||
}
|
||||
|
||||
override fun get(): Byte = bytes[position++]
|
||||
override fun remaining(): Int = end - position
|
||||
|
||||
override fun get(index: Int): Byte = bytes[index]
|
||||
override fun get(): Byte {
|
||||
require(remaining() > 0)
|
||||
return bytes[position++]
|
||||
}
|
||||
|
||||
override fun get(index: Int): Byte {
|
||||
require(index >= 0 && index < end - start)
|
||||
return bytes[start + index]
|
||||
}
|
||||
|
||||
override fun get(bytes: ByteArray, offset: Int, length: Int): ByteReader {
|
||||
require(
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import platform.darwin.DISPATCH_TIME_NOW
|
||||
import platform.darwin.dispatch_after
|
||||
import platform.darwin.dispatch_get_main_queue
|
||||
import platform.darwin.dispatch_time
|
||||
|
||||
internal actual fun scheduleDrawRedraw(delayMs: Int, action: () -> Unit): () -> Unit {
|
||||
val lock = protocolPlatform().createLock()
|
||||
var cancelled = false
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delayMs.toLong() * 1_000_000L), dispatch_get_main_queue()) {
|
||||
if (lock.withLock { !cancelled }) action()
|
||||
}
|
||||
return { lock.withLock { cancelled = true } }
|
||||
}
|
||||
Reference in new issue
Block a user