forked from Blendman974/faceclaw
CPU usage optimizations
This commit is contained in:
1 parent
95b2ca8afa
commit
3601a998a3
17 files changed
+494
-94
No files matched your search
+12
-2
@@ -15,8 +15,18 @@ actual class Sha256Digest actual constructor() {
|
||||
|
||||
actual fun currentTimeMillis(): Long = System.currentTimeMillis()
|
||||
|
||||
actual fun formatLocalTime(epochMs: Long, pattern: String): String =
|
||||
SimpleDateFormat(pattern, Locale.US).format(Date(epochMs))
|
||||
// SimpleDateFormat is costly to build (pattern parse, Calendar, locale data) and not thread-safe,
|
||||
// so keep one per thread per pattern; the send loop formats a timestamp for every image it logs.
|
||||
private val localTimeFormats = object : ThreadLocal<HashMap<String, SimpleDateFormat>>() {
|
||||
override fun initialValue() = HashMap<String, SimpleDateFormat>()
|
||||
}
|
||||
|
||||
actual fun formatLocalTime(epochMs: Long, pattern: String): String {
|
||||
val format = localTimeFormats.get()!!.getOrPut(pattern) { SimpleDateFormat(pattern, Locale.US) }
|
||||
// Re-read the default zone each call, as a fresh instance would.
|
||||
format.timeZone = java.util.TimeZone.getDefault()
|
||||
return format.format(Date(epochMs))
|
||||
}
|
||||
|
||||
actual object PlatformLog {
|
||||
actual fun i(tag: String, message: String) {
|
||||
|
||||
+12
-20
@@ -274,15 +274,17 @@ class GlassesSessionCore(
|
||||
|
||||
internal val desiredTilesLock: ProtocolLock = platform.createLock()
|
||||
internal var desiredFingerprint = ""
|
||||
// Headerless packed 4bpp frame (see BmpUtil.pack4bppFromGray8) plus its
|
||||
// pixel dimensions.
|
||||
internal var desiredPacked: ByteArray? = ByteArray(0)
|
||||
// Composited 8bpp frame plus its pixel dimensions. The send loop packs it to
|
||||
// the headerless 4bpp wire format (BmpUtil.pack4bppFromGray8) when it picks
|
||||
// the frame up, so frames superseded before sending are never packed and
|
||||
// the packing stays off the submitting (main) thread.
|
||||
internal var desiredGray: ByteArray? = ByteArray(0)
|
||||
internal var desiredWidth = 0
|
||||
internal var desiredHeight = 0
|
||||
internal var desiredPaintMs = 0
|
||||
internal var desiredFrameId = 0
|
||||
// Screen-space deferred draws (glyphs + images) whose pixels are baked
|
||||
// into desiredPacked; the resource-cache planner may replay them as
|
||||
// into desiredGray; the resource-cache planner may replay them as
|
||||
// on-glasses cached draws.
|
||||
internal var desiredDraws: Array<SurfaceCompositor.ScreenDraw>? = arrayOf()
|
||||
// (frame, reason) of the last "waiting to send" line, so a frame that
|
||||
@@ -943,9 +945,7 @@ class GlassesSessionCore(
|
||||
val frameId = frameTimings.startFrame(
|
||||
"compositor:visible $id=$visible")
|
||||
compositor.setSurfaceVisible(id, visible)
|
||||
val composite = compositor.composite()
|
||||
val packed = BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height)
|
||||
storeDesiredComposite(composite, packed, 0, frameId)
|
||||
storeDesiredComposite(compositor.composite(), 0, frameId)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -957,9 +957,7 @@ class GlassesSessionCore(
|
||||
val frameId = frameTimings.startFrame(
|
||||
"compositor:" + (if (blanked) "blank" else "unblank"))
|
||||
compositor.setBlanked(blanked)
|
||||
val composite = compositor.composite()
|
||||
val packed = BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height)
|
||||
storeDesiredComposite(composite, packed, 0, frameId)
|
||||
storeDesiredComposite(compositor.composite(), 0, frameId)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -979,7 +977,7 @@ class GlassesSessionCore(
|
||||
fun submitShellScene(bytes: ByteReader, paintMs: Int, frameId: Int) {
|
||||
compositor.setShellScene(bytes)
|
||||
val composite = compositor.composite()
|
||||
storeDesiredComposite(composite, BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height), paintMs, frameId)
|
||||
storeDesiredComposite(composite, paintMs, frameId)
|
||||
}
|
||||
|
||||
/** Legacy compositor dimming for callers without a shell scene. */
|
||||
@@ -1016,17 +1014,11 @@ class GlassesSessionCore(
|
||||
val composite = compositor.applyAndComposite(
|
||||
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs)
|
||||
frameTimings.spanEnd(frameId, "composite")
|
||||
// Pack the composited 8bpp buffer down to the headerless 4bpp frame
|
||||
// format the wire planners consume; BMP framing is added later only for
|
||||
// the uncompressed fallback.
|
||||
frameTimings.spanStart(frameId, "pack-4bpp")
|
||||
val packed = BmpUtil.pack4bppFromGray8(composite.screenGray, composite.width, composite.height)
|
||||
frameTimings.spanEnd(frameId, "pack-4bpp")
|
||||
storeDesiredComposite(composite, packed, paintMs, frameId)
|
||||
storeDesiredComposite(composite, paintMs, frameId)
|
||||
}
|
||||
|
||||
/** Store a composite as the desired frame unless a newer one won the race. */
|
||||
internal fun storeDesiredComposite(composite: SurfaceCompositor.Composite, packed: ByteArray, paintMs: Int, frameId: Int) {
|
||||
internal fun storeDesiredComposite(composite: SurfaceCompositor.Composite, paintMs: Int, frameId: Int) {
|
||||
var supersededFrameId = 0
|
||||
var stale = false
|
||||
desiredTilesLock.locked {
|
||||
@@ -1037,7 +1029,7 @@ class GlassesSessionCore(
|
||||
} else {
|
||||
lastStoredCompositeSeq = composite.seq
|
||||
supersededFrameId = desiredFrameId
|
||||
desiredPacked = packed
|
||||
desiredGray = composite.screenGray
|
||||
desiredWidth = composite.width
|
||||
desiredHeight = composite.height
|
||||
desiredFingerprint = composite.fingerprint
|
||||
|
||||
+9
-3
@@ -711,7 +711,7 @@ internal fun GlassesSessionCore.enqueueAmbientLightControlLocked(payload: ByteAr
|
||||
|
||||
internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
val fingerprint: String
|
||||
val packedSnapshot: ByteArray?
|
||||
val graySnapshot: ByteArray?
|
||||
val width: Int
|
||||
val height: Int
|
||||
val paintMs: Int
|
||||
@@ -720,7 +720,7 @@ internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
val scene: ShellScene
|
||||
desiredTilesLock.locked {
|
||||
fingerprint = desiredFingerprint
|
||||
packedSnapshot = desiredPacked
|
||||
graySnapshot = desiredGray
|
||||
width = desiredWidth
|
||||
height = desiredHeight
|
||||
paintMs = desiredPaintMs
|
||||
@@ -729,7 +729,13 @@ internal fun GlassesSessionCore.enqueueDesiredImageLocked() {
|
||||
scene = desiredShellScene
|
||||
desiredFrameId = 0
|
||||
}
|
||||
val packedFrame: ByteArray = packedSnapshot ?: ByteArray(0)
|
||||
// Pack here rather than at submit: a frame superseded before the send loop
|
||||
// gets to it is never packed, and the main thread doesn't pay for it.
|
||||
frameTimings.spanStart(frameId, "pack-4bpp")
|
||||
val packedFrame: ByteArray =
|
||||
if (graySnapshot == null || graySnapshot.isEmpty()) ByteArray(0)
|
||||
else BmpUtil.pack4bppFromGray8(graySnapshot, width, height)
|
||||
frameTimings.spanEnd(frameId, "pack-4bpp")
|
||||
// Visibility belongs to this immutable composite, not the latest UI request.
|
||||
enqueueBrightnessLocked(!fingerprint.startsWith("blanked:"))
|
||||
if (customFirmwareDetected && packedFrame.size > 0) {
|
||||
|
||||
+71
-4
@@ -298,11 +298,78 @@ class DisplayListRenderer(
|
||||
}
|
||||
require(width > 0 && height > 0 && x + width <= source.width && y + height <= source.height)
|
||||
// Mirrors the firmware's overflow guard before the depth shift.
|
||||
if (walk.apply && dx in -65536..65536) {
|
||||
// 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])
|
||||
if (!walk.apply || dx !in -65536..65536) return
|
||||
// Clip the destination to the writable area, in shifted target pixels.
|
||||
// The source is read unshifted, like Target.get.
|
||||
val originX = dx + target.shiftX
|
||||
var left = maxOf(originX, 0)
|
||||
var right = minOf(originX + width, target.width)
|
||||
var top = maxOf(dy, 0)
|
||||
var bottom = minOf(dy.toLong() + height, target.height.toLong()).toInt()
|
||||
target.clip?.let { clip ->
|
||||
left = maxOf(left, clip[0]); top = maxOf(top, clip[1])
|
||||
right = minOf(right, clip[2]); bottom = minOf(bottom, clip[3])
|
||||
}
|
||||
if (left >= right || top >= bottom) return
|
||||
val count = right - left
|
||||
val rows = bottom - top
|
||||
var sourceX = x + (left - originX)
|
||||
var sourceBytes = source.bytes
|
||||
var sourceStride = source.stride
|
||||
var sourceRow = source.offset + (y + (top - dy)) * sourceStride
|
||||
if (sourceBytes === target.bytes) {
|
||||
// Snapshot the packed source span so overlapping copies have memmove semantics.
|
||||
val first = sourceX / 2
|
||||
val span = (sourceX + count - 1) / 2 - first + 1
|
||||
val snapshot = ByteArray(span * rows)
|
||||
for (row in 0 until rows) {
|
||||
val start = sourceRow + row * sourceStride + first
|
||||
sourceBytes.copyInto(snapshot, row * span, start, start + span)
|
||||
}
|
||||
sourceBytes = snapshot
|
||||
sourceStride = span
|
||||
sourceRow = 0
|
||||
sourceX = sourceX and 1
|
||||
}
|
||||
for (row in 0 until rows) {
|
||||
copyPixels(sourceBytes, sourceRow + row * sourceStride, sourceX,
|
||||
target.bytes, target.offset + (top + row) * target.stride, left, count)
|
||||
}
|
||||
}
|
||||
|
||||
/** Copy [count] packed 4bpp pixels between rows that must not overlap. */
|
||||
private fun copyPixels(source: ByteArray, sourceRow: Int, sourceX: Int,
|
||||
destination: ByteArray, destinationRow: Int, destinationX: Int, count: Int) {
|
||||
var sx = sourceX
|
||||
var dx = destinationX
|
||||
var remaining = count
|
||||
if (dx and 1 != 0) {
|
||||
putPixel(destination, destinationRow, dx++, getPixel(source, sourceRow, sx++))
|
||||
remaining--
|
||||
}
|
||||
val pairs = remaining / 2
|
||||
var si = sourceRow + sx / 2
|
||||
var di = destinationRow + dx / 2
|
||||
if (sx and 1 == 0) {
|
||||
source.copyInto(destination, di, si, si + pairs)
|
||||
} else {
|
||||
// Misaligned: each destination byte straddles two source bytes.
|
||||
repeat(pairs) {
|
||||
destination[di++] = ((source[si].toInt() shl 4) or ((source[++si].toInt() and 255) ushr 4)).toByte()
|
||||
}
|
||||
}
|
||||
if (remaining and 1 != 0) {
|
||||
putPixel(destination, destinationRow, dx + pairs * 2, getPixel(source, sourceRow, sx + pairs * 2))
|
||||
}
|
||||
}
|
||||
|
||||
private fun getPixel(bytes: ByteArray, row: Int, x: Int): Int =
|
||||
(bytes[row + x / 2].toInt() ushr (if (x and 1 == 0) 4 else 0)) and 15
|
||||
|
||||
private fun putPixel(bytes: ByteArray, row: Int, x: Int, value: Int) {
|
||||
val index = row + x / 2
|
||||
val old = bytes[index].toInt()
|
||||
bytes[index] = if (x and 1 == 0) ((old and 15) or (value shl 4)).toByte() else ((old and 240) or value).toByte()
|
||||
}
|
||||
|
||||
private fun drawStockText(reader: DrawReader, target: Target, walk: Walk) {
|
||||
|
||||
@@ -2,11 +2,17 @@ package com.faceclaw.app
|
||||
|
||||
/** Immutable shell snapshot. Keys identify writable surfaces across repaints, not their pixels. */
|
||||
class ShellScene(val layers: List<Layer>, val selections: List<RetainedDrawing> = emptyList(), val screenDepth: Int = 0) {
|
||||
class Layer(val key: Int, val x: Int, val y: Int, val width: Int, val height: Int, val dim: Int, val packed: ByteArray, val selections: List<RetainedDrawing> = emptyList(), val depth: Int = 0)
|
||||
class Layer(val key: Int, val x: Int, val y: Int, val width: Int, val height: Int, val dim: Int, val packed: ByteArray, val selections: List<RetainedDrawing> = emptyList(), val depth: Int = 0) {
|
||||
// Layers are reused by every composite until the shell repaints, and hashing packed
|
||||
// byte-by-byte on each one was a measurable share of the compositor's time.
|
||||
internal val fingerprint: String by lazy {
|
||||
"$key,$x,$y,$width,$height,$dim,$depth,${CachedResource(packed).hash},${selections.joinToString { row -> row.fingerprint }}"
|
||||
}
|
||||
}
|
||||
val allSelections = selections + layers.flatMap { it.selections }
|
||||
val retainedResources = allSelections.flatMap { it.resources }
|
||||
init { require(retainedResources.size + layers.size < 511 && screenDepth in -128..127) }
|
||||
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 } + "|depth:$screenDepth"
|
||||
val fingerprint: String = layers.joinToString(";") { it.fingerprint } + selections.joinToString { it.fingerprint } + "|depth:$screenDepth"
|
||||
fun calls(width: Int, height: Int, surfaces: IntArray, selected: IntArray): List<ByteArray> {
|
||||
val calls = ArrayList<ByteArray>(); var rowId = 0
|
||||
val now = drawAnimationTimeMs()
|
||||
|
||||
+5
-1
@@ -582,7 +582,11 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
|
||||
expectedBytes)
|
||||
)
|
||||
}
|
||||
run {
|
||||
if (rectX == 0 && rectWidth == surface.width) {
|
||||
// Full-width rect (every current submitter): one bulk read. Per-row reads from a
|
||||
// direct ByteBuffer each cross into native code, which showed up in profiles.
|
||||
pixels.get(surface.pixels, rectY * surface.width, expectedBytes)
|
||||
} else run {
|
||||
var row: Int = 0
|
||||
while ((row < rectHeight)) {
|
||||
var dstOffset: Int = (((rectY + row) * surface.width) + rectX)
|
||||
|
||||
Reference in new issue
Block a user