forked from Blendman974/faceclaw
Fix several bugs that could lead to reconnect-looping
Fix misinterpretation of EvenHub event type 5. Notification modals stacked, in a way that's O(n) in device resource memory; replace with a queue.
This commit is contained in:
1 parent
e5303e7b3e
commit
38da8e6168
15 files changed
+396
-52
No files matched your search
+48
-12
@@ -2,9 +2,13 @@ package com.faceclaw.app
|
||||
|
||||
import kotlin.jvm.JvmOverloads
|
||||
|
||||
/** The delta base contains only app pixels. Shell resources are composed at presentation. */
|
||||
/**
|
||||
* The delta base contains only app pixels, except in frames that bake shell content (see [plan]).
|
||||
* Shell resources are composed at presentation.
|
||||
*/
|
||||
class ScenePlanner(private val cache: ResourceCacheState) {
|
||||
class Plan(val commands: List<ByteArray>, val nextFid: Int)
|
||||
/** [bakedLayers] and [bakedSelections] say what a frame over the resource budget baked into its pixels. */
|
||||
class Plan(val commands: List<ByteArray>, val nextFid: Int, val bakedLayers: Int = 0, val bakedSelections: Boolean = false)
|
||||
private val owned = HashMap<String, CachedResource>()
|
||||
private val pixels = HashMap<String, ByteArray>()
|
||||
private var previous: ByteArray? = null
|
||||
@@ -12,6 +16,14 @@ class ScenePlanner(private val cache: ResourceCacheState) {
|
||||
private var screenHeight = 0
|
||||
private var epoch = -1
|
||||
private var root = -1
|
||||
|
||||
/**
|
||||
* When the shell's surfaces and retained drawings don't all fit the resource budget, the frame
|
||||
* bakes the bottom of the scene into its screen pixels until the rest fits: first the scene's
|
||||
* own retained drawings, then layers from the bottom (the chrome) up, so the top overlay stays
|
||||
* a live surface longest. A frame that can't be planned even fully flattened throws, leaving
|
||||
* the cache and this planner as they were.
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun plan(screen: ByteArray, width: Int, height: Int, draws: Array<SurfaceCompositor.ScreenDraw>?, scene: ShellScene, firstId: Int, textures: Boolean = true, incremental: Boolean = true, multiRect: Boolean = true, maxRects: Int = 6): Plan {
|
||||
require(width in 1..640 && height in 1..480)
|
||||
@@ -19,42 +31,66 @@ class ScenePlanner(private val cache: ResourceCacheState) {
|
||||
require(screen.size == ((width + 1) / 2) * height)
|
||||
if (width != screenWidth || height != screenHeight) previous = null
|
||||
screenWidth = width; screenHeight = height
|
||||
val rollback = cache.checkpoint()
|
||||
try {
|
||||
planScene(screen, width, height, draws, scene, firstId, textures, incremental, multiRect, maxRects)?.let { return it }
|
||||
for (layerCount in 0..scene.layers.size) {
|
||||
if (layerCount == 0 && scene.selections.isEmpty()) continue
|
||||
rollback()
|
||||
val kept = ShellScene(scene.layers.drop(layerCount), screenDepth = scene.screenDepth)
|
||||
planScene(screen, width, height, draws, kept, firstId, textures, incremental, multiRect, maxRects) {
|
||||
scene.flatten(screen, width, height, layerCount)
|
||||
}?.let { return Plan(it.commands, it.nextFid, layerCount, scene.selections.isNotEmpty()) }
|
||||
}
|
||||
error("Shell scene exceeds the resource budget even fully flattened")
|
||||
} catch (t: Throwable) {
|
||||
rollback()
|
||||
throw t
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One attempt at [plan], or null if [scene] doesn't fit the budget. [baked] supplies the
|
||||
* screen pixels once the shell fits. Planner state changes only when the attempt succeeds.
|
||||
*/
|
||||
private fun planScene(screen: ByteArray, width: Int, height: Int, draws: Array<SurfaceCompositor.ScreenDraw>?, scene: ShellScene, firstId: Int, textures: Boolean, incremental: Boolean, multiRect: Boolean, maxRects: Int, baked: (() -> ByteArray)? = null): Plan? {
|
||||
val commands = ArrayList<ByteArray>()
|
||||
val keys = scene.layers.map { "shell:${it.key}:${it.width}x${it.height}" }
|
||||
val surfaces = scene.layers.mapIndexed { i, layer -> owned.getOrPut(keys[i]) {
|
||||
CachedResource(DrawProtocol.rawImage(layer.width, layer.height), keys[i])
|
||||
} }
|
||||
owned.keys.retainAll(keys.toSet()); pixels.keys.retainAll(keys.toSet())
|
||||
val surfaces = scene.layers.mapIndexed { i, layer ->
|
||||
owned[keys[i]] ?: CachedResource(DrawProtocol.rawImage(layer.width, layer.height), keys[i])
|
||||
}
|
||||
val selectedResources = scene.retainedResources
|
||||
cache.pin(surfaces + selectedResources)
|
||||
val absent = surfaces.map { cache.resourceId(it) < 0 }
|
||||
val prepared = cache.prepare(surfaces + selectedResources)
|
||||
if (prepared.any { it < 0 }) return null
|
||||
val ids = prepared.copyOfRange(0, surfaces.size)
|
||||
val selectedIds = prepared.copyOfRange(surfaces.size, prepared.size)
|
||||
check(prepared.all { it >= 0 }) { "Shell surfaces exceed the resource budget" }
|
||||
val frame = baked?.invoke() ?: screen
|
||||
commands.addAll(cache.drainCommands(3600))
|
||||
val rootCalls = scene.calls(width, height, ids, selectedIds)
|
||||
for ((i, layer) in scene.layers.withIndex()) {
|
||||
val old = if (absent[i]) null else pixels[keys[i]]
|
||||
commands.addAll(DrawProtocol.messages(pixelCalls(old, layer.packed, layer.width, layer.height, ids[i])))
|
||||
pixels[keys[i]] = layer.packed
|
||||
}
|
||||
val base = if (incremental) previous else null
|
||||
val texture = if (textures) TexturePlanner.plan(base, screen, width, height, draws, cache, firstId, multiRect, maxRects) else null
|
||||
val texture = if (textures) TexturePlanner.plan(base, frame, width, height, draws, cache, firstId, multiRect, maxRects) else null
|
||||
if (texture != null) {
|
||||
commands.addAll(texture.resourceCommands)
|
||||
commands.addAll(DrawProtocol.messages(DrawProtocol.fromOptimized(texture.payload, width, height)))
|
||||
} else commands.addAll(DrawProtocol.messages(pixelCalls(base, screen, width, height, null)))
|
||||
} else commands.addAll(DrawProtocol.messages(pixelCalls(base, frame, width, height, null)))
|
||||
val list = CachedResource(DrawProtocol.displayList(rootCalls))
|
||||
val rootId = cache.prepare(listOf(list))[0]
|
||||
check(rootId >= 0) { "No space for root display list" }
|
||||
if (rootId < 0) return null
|
||||
commands.addAll(cache.drainCommands(3600))
|
||||
cache.pin(surfaces + selectedResources + list)
|
||||
val evicted = commands.any { it[0].toInt() == ResourceCacheState.EVICT_MODE }
|
||||
if (evicted) commands.add(0, DrawProtocol.root(DrawProtocol.SCREEN))
|
||||
if (root != rootId || evicted) commands.add(DrawProtocol.root(rootId))
|
||||
commands.add(byteArrayOf(DrawProtocol.PRESENT.toByte()))
|
||||
root = rootId; previous = screen.copyOf()
|
||||
owned.keys.retainAll(keys.toSet()); pixels.keys.retainAll(keys.toSet())
|
||||
for ((i, key) in keys.withIndex()) { owned[key] = surfaces[i]; pixels[key] = scene.layers[i].packed }
|
||||
root = rootId; previous = frame.copyOf()
|
||||
return Plan(commands, texture?.nextFid ?: firstId)
|
||||
}
|
||||
private fun pixelCalls(old: ByteArray?, next: ByteArray, width: Int, height: Int, target: Int?): List<ByteArray> {
|
||||
|
||||
+14
-1
@@ -1580,13 +1580,26 @@ class GlassesSessionCore(
|
||||
updateSilentModeLocked(false)
|
||||
}
|
||||
if ("sys-event" == decoded.kind) {
|
||||
if (decoded.eventType == BleProtocol.EVENT_FOREGROUND_EXIT || decoded.eventType == BleProtocol.EVENT_ABNORMAL_EXIT || decoded.eventType == BleProtocol.EVENT_SYSTEM_EXIT) {
|
||||
if (decoded.eventType == BleProtocol.EVENT_ABNORMAL_EXIT || decoded.eventType == BleProtocol.EVENT_SYSTEM_EXIT) {
|
||||
if (shutdownRequested) {
|
||||
lastShutdownExitAtMs = now()
|
||||
}
|
||||
fixedLayoutCreated = false
|
||||
displayedFingerprint = ""
|
||||
clearAllMessagesLocked("firmware exit event")
|
||||
} else if (decoded.eventType == BleProtocol.EVENT_FOREGROUND_ENTER) {
|
||||
logLine("firmware overlay opened over the EvenHub page")
|
||||
} else if (decoded.eventType == BleProtocol.EVENT_FOREGROUND_EXIT) {
|
||||
// Despite its name this does not end the page: stock
|
||||
// evenhub_page_event_handler sends it when a firmware
|
||||
// overlay announced by FOREGROUND_ENTER closes, and
|
||||
// restores the page and its keepalive. Teardown is
|
||||
// always SYSTEM_EXIT/ABNORMAL_EXIT. Re-creating the
|
||||
// layout here sent a create the firmware ignores while
|
||||
// the page exists, so the session timed out (#42).
|
||||
// Re-present over whatever the overlay left instead.
|
||||
logLine("firmware overlay closed; re-presenting")
|
||||
lastEnqueuedFingerprint = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
-3
@@ -795,9 +795,25 @@ internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
// Visibility belongs to this immutable composite, not the latest UI request.
|
||||
enqueueBrightnessLocked(!fingerprint.startsWith("blanked:"))
|
||||
if (customFirmwareDetected && packedFrame.size > 0) {
|
||||
val rendered = scenePlanner.plan(packedFrame, width, height, draws, scene, nextImageFrameId,
|
||||
connectionOptions.TEXTURE_CACHE_FRAMES, connectionOptions.INCREMENTAL_FRAMES,
|
||||
connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS)
|
||||
val rendered = try {
|
||||
scenePlanner.plan(packedFrame, width, height, draws, scene, nextImageFrameId,
|
||||
connectionOptions.TEXTURE_CACHE_FRAMES, connectionOptions.INCREMENTAL_FRAMES,
|
||||
connectionOptions.MULTI_RECT_FRAMES, ConnectionOptions.MULTI_RECT_MAX_RECTS)
|
||||
} catch (e: Exception) {
|
||||
// Planning depends only on this frame, so reconnecting would fail
|
||||
// the same way (#42). Drop the frame (the planner and resource
|
||||
// cache are as they were) and wait for the scene to change.
|
||||
lastEnqueuedFingerprint = fingerprint
|
||||
logLine("frame plan failed; frame dropped: " + safeMessage(e))
|
||||
finishFrame(frameId, "discarded: frame plan failed")
|
||||
return
|
||||
}
|
||||
if (rendered.bakedLayers > 0 || rendered.bakedSelections) {
|
||||
val baked = "shell over resource budget: baked " + rendered.bakedLayers + "/" + scene.layers.size +
|
||||
" layers" + (if (rendered.bakedSelections) " and retained drawings" else "") + " into the frame"
|
||||
frameTimings.log(frameId, baked)
|
||||
logLine("$baked fingerprint=$fingerprint")
|
||||
}
|
||||
nextImageFrameId = rendered.nextFid
|
||||
val commands = rendered.commands
|
||||
for (i in 0 until commands.size - 1) enqueueResourceCommandLocked(commands[i])
|
||||
|
||||
+7
-2
@@ -1,10 +1,14 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** Reference interpreter. It uses the firmware's packed rows, clipping, LUT and call grammar. */
|
||||
/**
|
||||
* Reference interpreter. It uses the firmware's packed rows, clipping, LUT and call grammar.
|
||||
* [flat] ignores stereo depth, for pixels both lenses share (see ShellScene.flatten).
|
||||
*/
|
||||
class DisplayListRenderer(
|
||||
private val resources: Map<Int, ByteArray>,
|
||||
private val builtin: ((Int, Int) -> BuiltinGlyph)? = null,
|
||||
private val rightLens: Boolean = false,
|
||||
private val flat: Boolean = false,
|
||||
) {
|
||||
class BuiltinGlyph(val image: ByteArray, val advance: Int, val x: Int = 0, val y: Int = 0)
|
||||
|
||||
@@ -239,7 +243,8 @@ class DisplayListRenderer(
|
||||
target = target(id).shifted(inherited.shiftX)
|
||||
}
|
||||
if (flags and DRAW_FLAG_DEPTH != 0) {
|
||||
target = target.shifted(DrawProtocol.depthOffset(reader.readS8(), rightLens))
|
||||
val depth = reader.readS8()
|
||||
if (!flat) target = target.shifted(DrawProtocol.depthOffset(depth, rightLens))
|
||||
}
|
||||
if (flags and DRAW_FLAG_CLIP != 0) {
|
||||
target = target.clipped(reader.readS16(), reader.readS16(), reader.readU16(), reader.readU16())
|
||||
|
||||
+26
-16
@@ -45,15 +45,34 @@ class ShellScene(val layers: List<Layer>, val selections: List<RetainedDrawing>
|
||||
|
||||
fun preview(screenGray: ByteArray, width: Int, height: Int, rightLens: Boolean = false): ByteArray {
|
||||
val screen = BmpUtil.pack4bppFromGray8(screenGray, width, height)
|
||||
val output = ByteArray(screen.size); val resources = HashMap<Int, ByteArray>()
|
||||
val output = ByteArray(screen.size)
|
||||
DisplayListRenderer(rendererResources(width, height), rightLens = rightLens).render(511, DisplayListRenderer.Target(screen, width, height), DisplayListRenderer.Target(output, width, height))
|
||||
val target = DisplayListRenderer.Target(output, width, height)
|
||||
return ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() }
|
||||
}
|
||||
|
||||
/**
|
||||
* [screen] (packed 4bpp) with this scene's own retained drawings and its bottom [layerCount]
|
||||
* layers drawn in, for a frame whose shell exceeds the resource budget (see ScenePlanner.plan).
|
||||
* Both lenses share the result, so stereo depth is dropped, and animations are drawn settled.
|
||||
*/
|
||||
fun flatten(screen: ByteArray, width: Int, height: Int, layerCount: Int): ByteArray {
|
||||
val baked = ShellScene(layers.take(layerCount), selections)
|
||||
val output = ByteArray(screen.size)
|
||||
DisplayListRenderer(baked.rendererResources(width, height), flat = true).render(511,
|
||||
DisplayListRenderer.Target(screen, width, height), DisplayListRenderer.Target(output, width, height), SETTLED_MS)
|
||||
return output
|
||||
}
|
||||
|
||||
/** Layer surfaces, then retained resources, by index, and the root list at 511. */
|
||||
private fun rendererResources(width: Int, height: Int): HashMap<Int, ByteArray> {
|
||||
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(retainedResources.size) { id -> resources[id + layers.size] = retainedResources[id].bytes; id + layers.size }
|
||||
resources[511] = DrawProtocol.displayList(calls(width, height, surfaces, rows))
|
||||
DisplayListRenderer(resources, rightLens = rightLens).render(511, DisplayListRenderer.Target(screen, width, height), DisplayListRenderer.Target(output, width, height))
|
||||
val target = DisplayListRenderer.Target(output, width, height)
|
||||
return ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() }
|
||||
return resources
|
||||
}
|
||||
internal class AnimatedPreview(val player: DisplayListPlayer, var pixels: ByteArray)
|
||||
|
||||
@@ -63,20 +82,9 @@ class ShellScene(val layers: List<Layer>, val selections: List<RetainedDrawing>
|
||||
): 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(retainedResources.size) { id ->
|
||||
resources[id + layers.size] = retainedResources[id].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),
|
||||
val player = DisplayListPlayer(DisplayListRenderer(rendererResources(width, height)),
|
||||
DisplayListRenderer.Target(screen, width, height), target,
|
||||
clock = { drawAnimationTimeMs() }, schedule = schedule,
|
||||
displayed = { preview.pixels = ByteArray(width * height) { (target.get(it % width, it / width) * 16).toByte() } })
|
||||
@@ -87,6 +95,8 @@ class ShellScene(val layers: List<Layer>, val selections: List<RetainedDrawing>
|
||||
|
||||
companion object {
|
||||
val EMPTY = ShellScene(emptyList())
|
||||
/** Past every animation's end: TIME counts from PRESENT and clamps to the animation's length. */
|
||||
private const val SETTLED_MS = 3_600_000L
|
||||
fun decode(reader: ByteReader): ShellScene {
|
||||
val count = reader.getShort().toInt() and 65535; require(count <= 64)
|
||||
val layers = ArrayList<Layer>()
|
||||
|
||||
Reference in new issue
Block a user