forked from Blendman974/faceclaw
Refactor texture cache usage to shared Kotlin for iOS
This commit is contained in:
1 parent
ccb320c66f
commit
07828db0bf
23 files changed
+552
-74
No files matched your search
+31
-10
@@ -9,6 +9,7 @@ import { hasCompatibleFirmware, firmwareIncompatibilityMessage, REQUIRED_FACECLA
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
import { decodeWearState, enableWearDetection, queryWearState } from './wear-protocol'
|
||||
import type { SessionTexturePlanner, TextureFrame } from './texture-planner'
|
||||
|
||||
declare function setTimeout(callback: () => void, ms: number): number
|
||||
declare function clearTimeout(id: number): void
|
||||
@@ -60,6 +61,7 @@ export class GlassesSession {
|
||||
private wanted = false
|
||||
private addresses: SessionAddresses | null = null
|
||||
private latest: Uint8Array | null = null
|
||||
private latestTextures: TextureFrame | null = null
|
||||
private displayed: Uint8Array | null = null
|
||||
private lastEnqueued: Uint8Array | null = null
|
||||
// Match Android: advance only for emitted deltas, skipping 0 / 0xffff.
|
||||
@@ -90,7 +92,8 @@ export class GlassesSession {
|
||||
private readonly onActivity: () => void = () => {},
|
||||
private readonly onCompass: (event: CompassEvent) => void = () => {},
|
||||
private readonly onWearState: (wearing: boolean) => void = () => {},
|
||||
onNotificationsChanged: (key?: string, popup?: boolean) => void = () => {}) {
|
||||
onNotificationsChanged: (key?: string, popup?: boolean) => void = () => {},
|
||||
private readonly textures?: SessionTexturePlanner) {
|
||||
this.notifications = new AncsClient(packet => this.writePackets('right', () => [packet]), onNotificationsChanged, message => this.log('ANCS: ' + message))
|
||||
this.off = transport.onEvent(event => this.receive(event))
|
||||
}
|
||||
@@ -428,9 +431,11 @@ export class GlassesSession {
|
||||
if (this.state.phase !== 'connected') return
|
||||
await this.request('right', protocol.SID.settings, magic => setBrightness(magic, level), 'Brightness')
|
||||
}
|
||||
setFrame(gray: Uint8Array): void {
|
||||
setFrame(gray: Uint8Array, textures?: TextureFrame): void {
|
||||
if (this.state.phase !== 'connected') return
|
||||
this.latest = protocol.packGray4(gray, 640, 480); this.schedule(0)
|
||||
this.latest = protocol.packGray4(gray, 640, 480)
|
||||
this.latestTextures = textures ?? null
|
||||
this.schedule(0)
|
||||
}
|
||||
/** Same mode-5 kind-4 sequencer payload and acknowledged image path as Android. */
|
||||
async playBuzzerSequence(payload: Uint8Array): Promise<void> {
|
||||
@@ -499,16 +504,27 @@ export class GlassesSession {
|
||||
while (this.canSendDisplay()) {
|
||||
if (!this.displaySending) {
|
||||
const packed = this.latest!; this.latest = null
|
||||
const textureFrame = this.latestTextures; this.latestTextures = null
|
||||
// Compare with the last enqueued image, not the last ACKed one. In an
|
||||
// A -> B -> A sequence, B may still be in flight when A is requested.
|
||||
if (this.lastEnqueued && packed.every((value, i) => value === this.lastEnqueued![i])) continue
|
||||
const delta = buildBoundingBoxPayload(this.lastEnqueued, packed, 640, 480, this.nextImageFrameId)
|
||||
const payload = delta ?? protocol.concat(new Uint8Array([6]), protocol.rle4(packed))
|
||||
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
const commands = payload.length <= CFW_MAX_MESSAGE ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
|
||||
if (payload.length > CFW_MAX_MESSAGE) for (const _ of commands)
|
||||
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`)
|
||||
const texturePlan = textureFrame ? this.textures?.plan(this.lastEnqueued, packed, textureFrame, this.nextImageFrameId) : null
|
||||
let commands: Uint8Array[]
|
||||
if (texturePlan) {
|
||||
// Uploads participate in the same ordered, acknowledged command window as
|
||||
// the image. Coalescing only happens before planning mutates residency.
|
||||
commands = [...texturePlan.uploads, texturePlan.payload]
|
||||
this.nextImageFrameId = texturePlan.nextFid
|
||||
this.log(`Display textures (${texturePlan.uploads.length} uploads, 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))
|
||||
if (delta) this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
commands = payload.length <= CFW_MAX_MESSAGE ? [payload] : buildFullFrameBands(packed, 640, 480, this.nextImageFrameId)
|
||||
if (payload.length > CFW_MAX_MESSAGE) for (const _ of commands)
|
||||
this.nextImageFrameId = this.nextImageFrameId >= 0xfffe ? 1 : this.nextImageFrameId + 1
|
||||
this.log(`Display ${delta ? 'bbox' : 'full'} (${commands.length} CFW commands)`)
|
||||
}
|
||||
this.displaySending = { packed, commands, offset: 0, pending: 0 }
|
||||
this.displayFrames.push(this.displaySending); this.lastEnqueued = packed
|
||||
}
|
||||
@@ -538,6 +554,10 @@ export class GlassesSession {
|
||||
this.pumping = true; const generation = this.generation
|
||||
try {
|
||||
const now = Date.now()
|
||||
// Firmware frees textures when its 90-second framebuffer lease expires.
|
||||
// After a long iOS suspension, reconnect rather than replaying pending
|
||||
// draws against unknown memory. Leave a margin for the two arm writes.
|
||||
if (now - this.lastLease >= 80_000) throw new Error('Display lease lapsed; restarting session')
|
||||
if (now - this.lastHeartbeat >= 4000) {
|
||||
const gap = now - this.lastHeartbeat
|
||||
await this.request('right', protocol.SID.hub, protocol.heartbeat, 'Heartbeat', 1500); this.lastHeartbeat = Date.now()
|
||||
@@ -571,6 +591,7 @@ export class GlassesSession {
|
||||
for (const item of custom) item.reject(new Error('Connection ended'))
|
||||
this.cfwTransports.clear()
|
||||
this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null
|
||||
this.latestTextures = null; this.textures?.reset()
|
||||
this.lastEnqueued = null; this.displayFrames = []; this.displaySending = null; this.displayInFlight = 0
|
||||
}
|
||||
private closeLinks(): void {
|
||||
|
||||
@@ -35,7 +35,9 @@ import { getStringSettingById, nightscoutSiteUrlSetting, nightscoutApiTokenSetti
|
||||
import { readPhoneBatteryState } from '../native/phone-battery'
|
||||
import { iosAppUnavailableReason } from '../apps/ios-availability'
|
||||
import { SurfaceCompositor } from '../graphics/surface-compositor'
|
||||
import { flattenPlanes, type Plane } from '../graphics/plane'
|
||||
import { flattenPlanesWithDraws, type Plane } from '../graphics/plane'
|
||||
import { prepareFrameDraws } from '../graphics/glyph-wire'
|
||||
import { IosTexturePlanner } from '../native/texture-planner.ios'
|
||||
import { G2_LENS_WIDTH, G2_LENS_HEIGHT } from '../graphics/image'
|
||||
import { previewPixels } from '../native/ios-graphics'
|
||||
import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/gestures'
|
||||
@@ -62,8 +64,8 @@ export class IosPreviewController {
|
||||
setScreenBlanked: async blanked => {
|
||||
this.compositor.setScreenBlanked(blanked); this.scheduleFrame()
|
||||
},
|
||||
submitSurfaceFrame: async (id, pixels, rect) => {
|
||||
this.compositor.submitSurfaceFrame(id, pixels, rect); this.scheduleFrame()
|
||||
submitSurfaceFrame: async (id, pixels, rect, _fingerprint, _paintMs, _frameId, draws) => {
|
||||
this.compositor.submitSurfaceFrame(id, pixels, rect, draws); this.scheduleFrame()
|
||||
},
|
||||
}
|
||||
private readonly glance = new GlanceHost({
|
||||
@@ -336,8 +338,8 @@ export class IosPreviewController {
|
||||
}
|
||||
}
|
||||
private async submit(id: string, planes: Plane[]): Promise<void> {
|
||||
const image = flattenPlanes(planes)
|
||||
this.compositor.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height })
|
||||
const { image, draws } = flattenPlanesWithDraws(planes)
|
||||
this.compositor.submitSurfaceFrame(id, image.pixels, { x: 0, y: 0, width: image.width, height: image.height }, prepareFrameDraws(draws))
|
||||
this.scheduleFrame()
|
||||
}
|
||||
private requestShellRender(): void { this.shellDirty = true; this.scheduleFrame() }
|
||||
@@ -350,12 +352,12 @@ export class IosPreviewController {
|
||||
try {
|
||||
if (this.shellDirty) {
|
||||
this.shellDirty = false
|
||||
const image = flattenPlanes(shell.paintSurface(), { width: 640, height: 480 })
|
||||
this.compositor.submitSurfaceFrame('shell', image.pixels, { x: 0, y: 0, width: 640, height: 480 })
|
||||
const { image, draws } = flattenPlanesWithDraws(shell.paintSurface(), { width: 640, height: 480 })
|
||||
this.compositor.submitSurfaceFrame('shell', image.pixels, { x: 0, y: 0, width: 640, height: 480 }, prepareFrameDraws(draws))
|
||||
this.compositor.setUnderlayDim(1, shell.underlayDim())
|
||||
}
|
||||
const pixels = this.compositor.composite()
|
||||
this.session?.setFrame(pixels)
|
||||
const { pixels, textures } = this.compositor.compositeFrame()
|
||||
this.session?.setFrame(pixels, textures)
|
||||
if (this.active) {
|
||||
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green')
|
||||
this.onFrame(image, this.glassesLocked ? 'Glasses locked' : this.glance.isVisible() ? 'Glanceboard'
|
||||
@@ -511,8 +513,8 @@ export class IosPreviewController {
|
||||
},
|
||||
setSurfaceVisible: (id, visible) => { this.compositor.setSurfaceVisible(id, visible); this.scheduleFrame() },
|
||||
removeSurface: id => { this.compositor.removeSurface(id); this.scheduleFrame() },
|
||||
submitPixels: (id, pixels, width, height) => {
|
||||
this.compositor.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }); this.scheduleFrame()
|
||||
submitPixels: (id, pixels, width, height, draws) => {
|
||||
this.compositor.submitSurfaceFrame(id, pixels, { x: 0, y: 0, width, height }, draws); this.scheduleFrame()
|
||||
},
|
||||
requestShellRender: () => this.requestShellRender(),
|
||||
openSettings: section => { void this.launchApp('settings', { section }) },
|
||||
@@ -560,7 +562,7 @@ export class IosPreviewController {
|
||||
this.refreshClock()
|
||||
}, receiveCompassEvent, wearing => this.handleWearState(wearing), (key, popup) => {
|
||||
iosNotificationsChanged(key, popup); this.requestShellRender()
|
||||
})
|
||||
}, new IosTexturePlanner())
|
||||
bindIosNotifications(this.session.notifications)
|
||||
bindCompassSession(this.session)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
/** Platform-owned snapshot of the same composite as the submitted pixels. */
|
||||
export type TextureFrame = { readonly native: object }
|
||||
export type TexturePlan = { uploads: Uint8Array[]; payload: Uint8Array; nextFid: number; usedBytes: number }
|
||||
/** Sessions own residency; frame snapshots may be coalesced without changing it. */
|
||||
export interface SessionTexturePlanner {
|
||||
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null
|
||||
reset(): void
|
||||
}
|
||||
Reference in new issue
Block a user