diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java index cbed7e39..c7aeef55 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java @@ -779,7 +779,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { // would compress even a random payload if we reused it across the run. byte[] payload = new byte[benchmarkMessageSize]; benchmarkRandom.nextBytes(payload); - payload[0] = 7; // CFW diagnostic-control mode... + payload[0] = CfwMessageTypeKt.CFW_MSG_DIAGNOSTICS; // CFW diagnostic-control mode... payload[1] = (byte) 0x7f; // ...with an unused sub-op: acked, no effect OutboundMessage message = messageBuilder.imagePayload( "bandwidth", @@ -859,7 +859,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { /** Request one CFW ambient-light report (image-handler mode 16 op 0). */ public void queryAmbientLight() { synchronized (lock) { - enqueueAmbientLightControlLocked(new byte[] { (byte) 16, (byte) 0 }, "als query", false); + enqueueAmbientLightControlLocked(new byte[] { (byte) CfwMessageTypeKt.CFW_MSG_AMBIENT_LIGHT, (byte) 0 }, "als query", false); } interruptibleSleep.interrupt(); } @@ -877,7 +877,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { int heartbeatMs, boolean bindToLease) { byte[] payload = enable ? new byte[] { - (byte) 16, + (byte) CfwMessageTypeKt.CFW_MSG_AMBIENT_LIGHT, (byte) 1, (byte) (bindToLease ? 1 : 0), (byte) (intervalMs & 0xff), @@ -887,7 +887,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { (byte) (heartbeatMs & 0xff), (byte) ((heartbeatMs >> 8) & 0xff), } - : new byte[] { (byte) 16, (byte) 2 }; + : new byte[] { (byte) CfwMessageTypeKt.CFW_MSG_AMBIENT_LIGHT, (byte) 2 }; synchronized (lock) { clearMessagesOfKindLocked("als-control"); enqueueAmbientLightControlLocked(payload, "als polling " + (enable ? "start" : "stop"), true); @@ -2887,7 +2887,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private void enqueueFirmwareDebugFlagsLocked() { boolean show = firmwareDebugFlagsEnabled; int sub = show ? 2 : 1; - byte[] payload = new byte[] { (byte) 7, (byte) sub }; + byte[] payload = new byte[] { (byte) CfwMessageTypeKt.CFW_MSG_DIAGNOSTICS, (byte) sub }; OutboundMessage message = messageBuilder.imagePayload( DASHBOARD_TILE, nextMapSessionId(), payload, "fw-debug-flags " + (show ? "show" : "hide"), @@ -2905,14 +2905,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { int sentState = enable ? 1 : 0; byte[] payload = enable ? new byte[] { - (byte) 10, + (byte) CfwMessageTypeKt.CFW_MSG_COMPASS, (byte) 2, (byte) (COMPASS_REPORT_INTERVAL_MS & 0xff), (byte) ((COMPASS_REPORT_INTERVAL_MS >> 8) & 0xff), (byte) (COMPASS_MIN_CHANGE_DEGREES & 0xff), (byte) ((COMPASS_MIN_CHANGE_DEGREES >> 8) & 0xff), } - : new byte[] { (byte) 10, (byte) 0 }; + : new byte[] { (byte) CfwMessageTypeKt.CFW_MSG_COMPASS, (byte) 0 }; OutboundMessage message = messageBuilder.imagePayload( "compass-control", DASHBOARD_TILE, diff --git a/app/g2/ble-image-optimizer.ts b/app/g2/ble-image-optimizer.ts index b874b53a..8ae18607 100644 --- a/app/g2/ble-image-optimizer.ts +++ b/app/g2/ble-image-optimizer.ts @@ -1,3 +1,4 @@ +import { CFW_MSG_BOUNDING_BOX } from "./cfw-message-type"; import { concat, rle4 } from './ble-protocol' /** Android BleImageOptimizer's mode-3 changed rectangle. Coordinates are in @@ -29,7 +30,7 @@ export function buildBoundingBoxPayload(previous: Uint8Array | null, next: Uint8 const start = (top + y) * stride + left / 2 region.set(next.subarray(start, start + regionStride), y * regionStride) } - return concat(new Uint8Array([3, left / 4, top / 2, boxWidth / 4, boxHeight / 2, + return concat(new Uint8Array([CFW_MSG_BOUNDING_BOX, left / 4, top / 2, boxWidth / 4, boxHeight / 2, frameId & 255, (frameId >>> 8) & 255]), rle4(region)) } @@ -41,7 +42,7 @@ export function buildFullFrameBands(packed: Uint8Array, width: number, height: n let id = firstId for (let top = 0; top < height; top += 64) { const rows = Math.min(64, height - top) - bands.push(concat(new Uint8Array([3, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]), + bands.push(concat(new Uint8Array([CFW_MSG_BOUNDING_BOX, 0, top / 2, width / 4, rows / 2, id & 255, id >>> 8]), rle4(packed.subarray(top * width / 2, (top + rows) * width / 2)))) id = id >= 0xfffe ? 1 : id + 1 } diff --git a/app/g2/cfw-message-type.ts b/app/g2/cfw-message-type.ts new file mode 100644 index 00000000..550fd658 --- /dev/null +++ b/app/g2/cfw-message-type.ts @@ -0,0 +1,36 @@ +/** Message IDs and header flags mirror g2flash/patches/zlib_glue.c. */ +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_BOUNDING_BOX = 3 +export const CFW_MSG_BUZZER = 5 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_FULL_FRAME = 6 +export const CFW_MSG_DIAGNOSTICS = 7 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_MULTI_SEGMENT = 8 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_RECT_COPY = 9 +export const CFW_MSG_COMPASS = 10 +export const CFW_MSG_CLEANUP = 11 +export const CFW_MSG_RETIRED_12 = 12 +export const CFW_MSG_RETIRED_13 = 13 +export const CFW_MSG_RETIRED_14 = 14 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_STOCK_FONT_STRING = 15 +export const CFW_MSG_AMBIENT_LIGHT = 16 +export const CFW_MSG_RING_BATTERY = 17 +export const CFW_MSG_RETIRED_CACHE_WRITE = 18 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_CACHED_IMAGE = 19 +/** Retired wire ID; used internally by the phone optimizer. */ +export const CFW_MSG_CACHED_TEXT = 20 +export const CFW_MSG_UPLOAD_RESOURCE = 21 +export const CFW_MSG_EVICT_RESOURCE = 22 +export const CFW_MSG_PANEL_READ = 23 +export const CFW_MSG_PANEL_WRITE = 24 +export const CFW_MSG_PANEL_PATTERN = 25 +export const CFW_MSG_DRAW_CALLS = 26 +export const CFW_MSG_SET_ROOT_DISPLAY_LIST = 27 +export const CFW_MSG_PRESENT = 28 +export const CFW_MSG_CREATE_SURFACE = 29 +export const CFW_MSG_TYPE_MASK = 127 +export const CFW_MSG_FLAG_LENSES_DIFFER = 128 diff --git a/app/g2/cfw-transport.ts b/app/g2/cfw-transport.ts index 922e0c20..8f6dd234 100644 --- a/app/g2/cfw-transport.ts +++ b/app/g2/cfw-transport.ts @@ -1,6 +1,7 @@ import { concat, crc16, SID } from './ble-protocol' declare const require: (name: string) => any +/** Mirrors CFW_MESSAGE_MAX in g2flash/patches/message_transport.h. */ export const CFW_MAX_MESSAGE = 65535 export type CfwAck = { nack: boolean; streamId: number; messageId: number; lens: number; size: number; checksum: number } const u16 = (data: Uint8Array, offset: number) => data[offset] | (data[offset + 1] << 8) diff --git a/app/g2/draw-protocol.ts b/app/g2/draw-protocol.ts index 1774b9be..4f5c3de1 100644 --- a/app/g2/draw-protocol.ts +++ b/app/g2/draw-protocol.ts @@ -1,14 +1,15 @@ +import { CFW_MSG_BOUNDING_BOX, CFW_MSG_DRAW_CALLS, CFW_MSG_FULL_FRAME, CFW_MSG_MULTI_SEGMENT, CFW_MSG_TYPE_MASK } from "./cfw-message-type"; /** Adapter for raster-only senders; optimized legacy layouts remain internal to the phone. */ export function drawMessages(payload: Uint8Array, width = 640, height = 480): Uint8Array[] { const word = (v: number) => [v & 255, (v >>> 8) & 255] - const mode = payload[0] & 127 + const mode = payload[0] & CFW_MSG_TYPE_MASK let calls: Uint8Array[] - if (mode === 8) { + if (mode === CFW_MSG_MULTI_SEGMENT) { const output: Uint8Array[] = []; let p=2 for(let i=0;i new Uint8Array([26,1,0,...word(call.length),...call])) + return calls.map(call => new Uint8Array([CFW_MSG_DRAW_CALLS,1,0,...word(call.length),...call])) } diff --git a/app/g2/glasses-session.ts b/app/g2/glasses-session.ts index 850d0327..fce88f76 100644 --- a/app/g2/glasses-session.ts +++ b/app/g2/glasses-session.ts @@ -1,3 +1,4 @@ +import { CFW_MSG_CLEANUP, CFW_MSG_COMPASS, CFW_MSG_EVICT_RESOURCE, CFW_MSG_FULL_FRAME, CFW_MSG_PRESENT } from "./cfw-message-type"; import { drawMessages } from './draw-protocol' import { setBrightness } from './brightness-protocol' import { AncsClient, ANCS_FIRMWARE_VERSION } from './ancs-client' @@ -481,7 +482,7 @@ export class GlassesSession { const enabled = !this.compassStopping && this.compassWanted if (this.compassSent === enabled) return // Same CFW mode 10, 100 ms report interval and zero minimum change as Android. - await this.requestCfw('left', new Uint8Array(enabled ? [10, 2, 100, 0, 0, 0] : [10, 0])) + await this.requestCfw('left', new Uint8Array(enabled ? [CFW_MSG_COMPASS, 2, 100, 0, 0, 0] : [CFW_MSG_COMPASS, 0])) if (generation === this.generation) this.compassSent = enabled }).catch(error => { if (generation === this.generation) this.fail(error, true) }) this.compassWork = work @@ -497,7 +498,7 @@ export class GlassesSession { this.timer = setTimeout(() => { this.timer = null; void this.pump() }, delay) } private canSendDisplay(): boolean { - if (this.displaySending?.commands[this.displaySending.offset][0] === 22 && this.cfwPending.length) return false + if (this.displaySending?.commands[this.displaySending.offset][0] === CFW_MSG_EVICT_RESOURCE && this.cfwPending.length) return false // Also bound completed frames retained behind a missing/out-of-order ACK. return this.displayInFlight < DISPLAY_WINDOW_SIZE && !!(this.displaySending || (this.latest && !this.charging && this.displayFrames.length < DISPLAY_WINDOW_SIZE)) @@ -520,12 +521,12 @@ export class GlassesSession { this.log(`Display textures (${texturePlan.resourceCommands.length} resource commands, cache=${texturePlan.usedBytes}B)`) } else { const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId) - const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed)) + const payload = delta ?? protocol.concat(new Uint8Array([CFW_MSG_FULL_FRAME]), protocol.rle4(packed)) if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1 commands = payload.length <= CFW_MAX_MESSAGE - 16 ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId) if (payload.length > CFW_MAX_MESSAGE - 16) for (const _ of commands) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1 - commands = [...commands.flatMap(command => drawMessages(command)), new Uint8Array([28])] + commands = [...commands.flatMap(command => drawMessages(command)), new Uint8Array([CFW_MSG_PRESENT])] this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`) } this.displaySending = { packed, commands, offset: 0, pending: 0 } @@ -533,7 +534,7 @@ export class GlassesSession { } const frame = this.displaySending // Drain earlier commands before evicting: recovery may replay any unresolved draw/upload. - if (frame.commands[frame.offset][0] === 22 && this.cfwPending.length) return + if (frame.commands[frame.offset][0] === CFW_MSG_EVICT_RESOURCE && this.cfwPending.length) return const payload = frame.commands[frame.offset++] frame.pending++; this.displayInFlight++ if (frame.offset >= frame.commands.length) this.displaySending = null @@ -634,7 +635,7 @@ export class GlassesSession { this.update('disconnecting', 'Disconnecting…'); this.reset() try { if (cleanup) { - await this.requestCfw('left', new Uint8Array([11])) + await this.requestCfw('left', new Uint8Array([CFW_MSG_CLEANUP])) } } catch (error) { this.log(`Disconnect cleanup: ${this.message(error)}`) } finally { this.reset(); this.closeLinks(); this.update('disconnected', 'Preview only') } diff --git a/app/native/kotlin-protocol-smoke.ios.ts b/app/native/kotlin-protocol-smoke.ios.ts index 1662c88a..f2221d5b 100644 --- a/app/native/kotlin-protocol-smoke.ios.ts +++ b/app/native/kotlin-protocol-smoke.ios.ts @@ -1,3 +1,4 @@ +import { CFW_MSG_BOUNDING_BOX, CFW_MSG_CREATE_SURFACE, CFW_MSG_PRESENT, CFW_MSG_UPLOAD_RESOURCE } from "../g2/cfw-message-type"; import { GrayImage } from "../graphics/image" import { encodeShellScene } from "../graphics/shell-scene" import { decodeImageBytes } from './image-bytes.ios' @@ -44,7 +45,7 @@ export function runKotlinProtocolSmokeTest(): void { textured.submitSurfaceFrame('icon', white, { x: 0, y: 0, width: 2, height: 2 }, draw.buffer) const snapshot = textured.compositeFrame(), packed = protocol.packGray4(snapshot.pixels, 4, 2) const cold = textures.plan(null, packed, snapshot.textures, 1) - assert(!!cold && cold.resourceCommands.some(command => command[0] === 21) && cold.payload[0] === 28, 'texture upload and draw') + assert(!!cold && cold.resourceCommands.some(command => command[0] === CFW_MSG_UPLOAD_RESOURCE) && cold.payload[0] === CFW_MSG_PRESENT, 'texture upload and draw') assert(textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length === 0, 'texture reuse') textures.reset() assert((textures.plan(null, packed, snapshot.textures, 1)?.resourceCommands.length ?? 0) > 0, 'texture reset') @@ -55,9 +56,9 @@ export function runKotlinProtocolSmokeTest(): void { const selectedSnapshot = selectedPreview.compositeFrame() assert(selectedSnapshot.pixels[10] === 240 && selectedSnapshot.pixels[9] === 0, 'menu selection depth and bridge') const selectedPlan = new IosTexturePlanner().plan(null, protocol.packGray4(selectedSnapshot.pixels, 8, 4), selectedSnapshot.textures, 1) - assert(!!selectedPlan && selectedPlan.payload[0] === 28 && selectedPlan.resourceCommands.some(c => c[0] === 29), 'menu resource planning') + assert(!!selectedPlan && selectedPlan.payload[0] === CFW_MSG_PRESENT && selectedPlan.resourceCommands.some(c => c[0] === CFW_MSG_CREATE_SURFACE), 'menu resource planning') const changed = new Uint8Array(16); changed[0] = 255 - assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update') + assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === CFW_MSG_BOUNDING_BOX, 'image update') assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge') const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0, payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) }) diff --git a/app/ui/sound-effects.ts b/app/ui/sound-effects.ts index 091b0861..bd127c4e 100644 --- a/app/ui/sound-effects.ts +++ b/app/ui/sound-effects.ts @@ -1,3 +1,4 @@ +import { CFW_MSG_BUZZER } from "../g2/cfw-message-type"; /** * Sound-effect catalog for the CFW buzzer tone sequencer (load_image_z mode 5 * kind 4). Ported from g2-kit-unofficial/examples/tunes.ts. @@ -19,14 +20,14 @@ export type Step = { freq: number; duty?: number; ms: number }; /** One or more phrases; each phrase is one sequencer message. */ export type Effect = Step[] | Step[][]; -/** Firmware cap: steps per mode-5 kind-4 message. */ +/** Mirrors CFW_SEQ_MAX in g2flash/patches/cfw_context.h. */ export const CFW_SEQ_MAX = 48; /** Build the wire buffer: [5][4][nSteps][ freqLo,freqHi,duty,msLo,msHi ]*n. */ export function buildSoundSequencePayload(steps: Step[]): Uint8Array { const count = Math.min(steps.length, CFW_SEQ_MAX); const out = new Uint8Array(3 + count * 5); - out[0] = 0x05; + out[0] = CFW_MSG_BUZZER; out[1] = 0x04; out[2] = count; for (let index = 0; index < count; index++) { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt index 2eeb86b3..ef4731fc 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleImageOptimizer.kt @@ -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 } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt index f8dd8058..cea67b83 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/BleProtocol.kt @@ -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 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageType.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageType.kt new file mode 100644 index 00000000..c07eee5c --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwMessageType.kt @@ -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 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt index 0ce887fd..75d47d36 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/CfwTransport.kt @@ -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 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawCallType.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawCallType.kt new file mode 100644 index 00000000..f8045e86 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawCallType.kt @@ -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 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawFlags.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawFlags.kt new file mode 100644 index 00000000..d2694afd --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawFlags.kt @@ -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 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawProtocol.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawProtocol.kt index d8812a89..3546b093 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawProtocol.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/DrawProtocol.kt @@ -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 { require(calls.size <= 4096) @@ -37,13 +42,13 @@ object DrawProtocol { fun message(calls: List) = byteArrayOf(DRAW.toByte()) + sequence(calls) fun displayList(calls: List) = 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 { - 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(); 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") } } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt index b9bccf6b..c9d4e670 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceCacheState.kt @@ -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) } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceFlags.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceFlags.kt new file mode 100644 index 00000000..e5f34ea8 --- /dev/null +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/ResourceFlags.kt @@ -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 diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt index fb1b2cd1..e132ac2e 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/g2protocol/TexturePlanner.kt @@ -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) { diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/DisplayListRenderer.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/DisplayListRenderer.kt index 68c8f60e..2166d3a5 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/DisplayListRenderer.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/DisplayListRenderer.kt @@ -39,28 +39,28 @@ class DisplayListRenderer(private val resources: Map, 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, private va return references } private fun executeList(id: Int, t: Target, screen: Target, apply: Boolean, stack: MutableList, budget: IntArray, refs: MutableSet) { - 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, budget: IntArray, refs: MutableSet) { @@ -116,14 +116,14 @@ class DisplayListRenderer(private val resources: Map, 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, 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, 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(); var i = 0 @@ -156,12 +156,12 @@ class DisplayListRenderer(private val resources: Map, 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, 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, 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") } } diff --git a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/MenuSelection.kt b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/MenuSelection.kt index c5630483..5a6c3e6b 100644 --- a/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/MenuSelection.kt +++ b/native/kotlin/shared/src/commonMain/kotlin/com/faceclaw/app/graphics/MenuSelection.kt @@ -9,16 +9,16 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val fun calls(id: Int): List { 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. */ diff --git a/tests/ios-app-host.test.cjs b/tests/ios-app-host.test.cjs index 2547a4a4..1a3b69cd 100644 --- a/tests/ios-app-host.test.cjs +++ b/tests/ios-app-host.test.cjs @@ -419,7 +419,7 @@ test('iOS compositor rejects missing and removed surfaces before crossing into K test('iOS welcome sound waits for a new acknowledged frame and is consumed once', async () => { let pending = true; - const played = [], sounds = load('app/ui/sound-effects.ts', {}); + const played = [], sounds = load('app/ui/sound-effects.ts', { require: name => { assert.equal(name, '../g2/cfw-message-type'); return load('app/g2/cfw-message-type.ts', {}); } }); const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', { require: id => ({ '../phone-ui/onboarding-state': { isWelcomeSoundPending: () => pending, setWelcomeSoundPending: value => { pending = value; } }, diff --git a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/DisplayListTest.kt b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/DisplayListTest.kt index e5177b95..385ba448 100644 --- a/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/DisplayListTest.kt +++ b/tests/kotlin/src/commonTest/kotlin/com/faceclaw/app/DisplayListTest.kt @@ -48,8 +48,8 @@ class DisplayListTest { val resources = mutableMapOf(10 to raw, 11 to hex("0802024f"), 12 to font) val nested = listOf(DrawProtocol.image(11, -1, 0), DrawProtocol.bbox(hex("0c000000"), 2, 1, 0, 1, 1, 10)) resources[20]=DrawProtocol.displayList(nested) - val calls = listOf(DrawProtocol.call(7,DrawProtocol.word(20)), DrawProtocol.image(10,3,1), - DrawProtocol.call(5,hex("0c00070003000f0141")), DrawProtocol.lut(8,4,128)) + val calls = listOf(DrawProtocol.call(DRAW_OP_DISPLAY_LIST,DrawProtocol.word(20)), DrawProtocol.image(10,3,1), + DrawProtocol.call(DRAW_OP_TEXT,hex("0c00070003000f0141")), DrawProtocol.lut(8,4,128)) resources[21]=DrawProtocol.displayList(calls) val screen=hex("123456789abcdef0123456789abcdef0"); val output=ByteArray(16) val renderer=DisplayListRenderer(resources) @@ -62,14 +62,14 @@ class DisplayListTest { } @Test fun graphValidationPrecedesMutationAndCopyPreservesOverlap() { val screen=hex("12345678"); val target=DisplayListRenderer.Target(screen,8,1) - val copy=DrawProtocol.call(2,hex("feff000000000600010002000000")) + val copy=DrawProtocol.call(DRAW_OP_RECT_COPY,hex("feff000000000600010002000000")) DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(copy)),target) assertContentEquals(hex("12123456"),screen) val valid=DrawProtocol.bbox(hex("ffffffff"),4,0,0,8,1) assertFails { DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(valid,byteArrayOf(99,0))),target) } assertContentEquals(hex("12123456"),screen) - val cyclic=mapOf(1 to DrawProtocol.displayList(listOf(DrawProtocol.call(7,DrawProtocol.word(2)))), - 2 to DrawProtocol.displayList(listOf(DrawProtocol.call(7,DrawProtocol.word(1))))) + val cyclic=mapOf(1 to DrawProtocol.displayList(listOf(DrawProtocol.call(DRAW_OP_DISPLAY_LIST,DrawProtocol.word(2)))), + 2 to DrawProtocol.displayList(listOf(DrawProtocol.call(DRAW_OP_DISPLAY_LIST,DrawProtocol.word(1))))) assertFails { DisplayListRenderer(cyclic).render(1,target,target) } } @Test fun roundedRectsAndNestedOddNegativeDepthMatchFirmwareOnBothLenses() { @@ -95,7 +95,7 @@ class DisplayListTest { val fill = DrawProtocol.roundedRect(-1,0,8,8,2,6,16,2) DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(fill)),target) assertEquals(15,target.get(2,3)); assertEquals(6,target.get(3,3)); assertEquals(1,target.get(0,0)) - val bbox = DrawProtocol.call(1,hex("00000002010f10"),depth=-1) + val bbox = DrawProtocol.call(DRAW_OP_BOUNDING_BOX,hex("00000002010f10"),depth=-1) DisplayListRenderer(emptyMap()).execute(DrawProtocol.sequence(listOf(bbox)),target) assertEquals(15,target.get(0,0));assertEquals(1,target.get(7,0)) val before=pixels.copyOf()