forked from Blendman974/faceclaw
Phone CPU usage optimization
This commit is contained in:
1 parent
44eddedc01
commit
9560f3ac8d
26 files changed
+606
-167
No files matched your search
-2
@@ -1,8 +1,6 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
interface FaceclawBleCommunicatorListener {
|
||||
fun onLog(line: String?): Unit
|
||||
|
||||
fun onStateChange(phase: String?, status: String?): Unit
|
||||
|
||||
fun onRingEvent(
|
||||
|
||||
+11
-11
@@ -574,16 +574,20 @@ class BleImageOptimizer {
|
||||
*/
|
||||
@JvmStatic
|
||||
fun rleEncode(pix: ByteArray): ByteArray {
|
||||
var n: Int = (pix.size * 2)
|
||||
var out: ByteArray = ByteArray(n)
|
||||
val n = pix.size * 2
|
||||
val out = ByteArray(n)
|
||||
var o: Int = 0
|
||||
var i: Int = 0
|
||||
while ((i < n)) {
|
||||
var v: Int = nibbleAt(pix, i)
|
||||
var j: Int = (i + 1)
|
||||
while (((j < n) && (nibbleAt(pix, j) == v))) {
|
||||
j++
|
||||
}
|
||||
val first = pix[i ushr 1].toInt() and 255
|
||||
val v = if (i and 1 == 0) first ushr 4 else first and 15
|
||||
// Start at a byte boundary, consuming the known low nibble
|
||||
// first if the previous run ended halfway through a byte.
|
||||
var j = i + (i and 1)
|
||||
val pair = ((v shl 4) or v).toByte()
|
||||
while (j < n && pix[j ushr 1] == pair) j += 2
|
||||
// A final high nibble can match even when its low nibble doesn't.
|
||||
if (j < n && (pix[j ushr 1].toInt() and 240) == v shl 4) j++
|
||||
var run: Int = (j - i)
|
||||
while ((run > 0)) {
|
||||
var c: Int = minOf(run, 0xffff)
|
||||
@@ -607,10 +611,6 @@ class BleImageOptimizer {
|
||||
return out.copyOf(o)
|
||||
}
|
||||
|
||||
private fun nibbleAt(pix: ByteArray, i: Int): Int {
|
||||
var b: Int = (pix[(i shr 1)] and 0xff)
|
||||
return (if (((i and 1) != 0)) (b and 0x0f) else (b shr 4))
|
||||
}
|
||||
}
|
||||
|
||||
constructor() {}
|
||||
|
||||
+49
-48
@@ -591,35 +591,26 @@ class TexturePlanner {
|
||||
left: Int,
|
||||
top: Int,
|
||||
): Boolean {
|
||||
run {
|
||||
var row: Int = 0
|
||||
while ((row < atlas.height)) {
|
||||
var y: Int = (top + row)
|
||||
if (((y < 0) || (y >= height))) {
|
||||
row++
|
||||
continue
|
||||
val pixels = atlas.nibbles
|
||||
val imageWidth = atlas.width
|
||||
val startX = maxOf(0, left)
|
||||
val endX = minOf(width, left + imageWidth)
|
||||
val endY = minOf(height, top + atlas.height)
|
||||
var y = maxOf(0, top)
|
||||
while (y < endY) {
|
||||
var src = (y - top) * imageWidth + startX - left
|
||||
val dstRow = y * stride
|
||||
var x = startX
|
||||
while (x < endX) {
|
||||
val source = pixels[src++].toInt() and 255
|
||||
if (source != 0) {
|
||||
val byte = packed[dstRow + (x ushr 1)].toInt() and 255
|
||||
val actual = if (x and 1 == 0) byte ushr 4 else byte and 15
|
||||
if (actual != source) return false
|
||||
}
|
||||
run {
|
||||
var col: Int = 0
|
||||
while ((col < atlas.width)) {
|
||||
var source: Int = atlas.nibbleAt(col, row)
|
||||
if ((source == 0)) {
|
||||
col++
|
||||
continue
|
||||
}
|
||||
var x: Int = (left + col)
|
||||
if (((x < 0) || (x >= width))) {
|
||||
col++
|
||||
continue
|
||||
}
|
||||
if ((nibbleAt(packed, stride, x, y) != source)) {
|
||||
return false
|
||||
}
|
||||
col++
|
||||
}
|
||||
}
|
||||
row++
|
||||
x++
|
||||
}
|
||||
y++
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -661,29 +652,39 @@ class TexturePlanner {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var atlas: ImageAtlas.Entry = sel.imageAtlas!!
|
||||
run {
|
||||
var row: Int = 0
|
||||
while ((row < atlas.height)) {
|
||||
var y: Int = (sel.draw.y + row)
|
||||
if (((y < 0) || (y >= height))) {
|
||||
row++
|
||||
continue
|
||||
}
|
||||
run {
|
||||
var col: Int = 0
|
||||
while ((col < atlas.width)) {
|
||||
if ((atlas.nibbleAt(col, row) == 0)) {
|
||||
col++
|
||||
continue
|
||||
}
|
||||
punchPixel(packed, stride, width, (sel.gx + col), y)
|
||||
col++
|
||||
}
|
||||
}
|
||||
row++
|
||||
punchImage(packed, stride, width, height, sel.imageAtlas!!, sel.gx, sel.draw.y)
|
||||
}
|
||||
}
|
||||
|
||||
/** Clear only visible nonzero image pixels, preserving the neighboring nibble. */
|
||||
private fun punchImage(
|
||||
packed: ByteArray,
|
||||
stride: Int,
|
||||
width: Int,
|
||||
height: Int,
|
||||
atlas: ImageAtlas.Entry,
|
||||
left: Int,
|
||||
top: Int,
|
||||
) {
|
||||
val pixels = atlas.nibbles
|
||||
val imageWidth = atlas.width
|
||||
val startX = maxOf(0, left)
|
||||
val endX = minOf(width, left + imageWidth)
|
||||
val endY = minOf(height, top + atlas.height)
|
||||
var y = maxOf(0, top)
|
||||
while (y < endY) {
|
||||
var src = (y - top) * imageWidth + startX - left
|
||||
val dstRow = y * stride
|
||||
var x = startX
|
||||
while (x < endX) {
|
||||
if (pixels[src++].toInt() != 0) {
|
||||
val index = dstRow + (x ushr 1)
|
||||
val mask = if (x and 1 == 0) 15 else 240
|
||||
packed[index] = (packed[index].toInt() and mask).toByte()
|
||||
}
|
||||
x++
|
||||
}
|
||||
y++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ class ImageAtlas {
|
||||
|
||||
/** The 4bpp value the mode-13 draw would write at (col, row); 0 = skipped. */
|
||||
fun nibbleAt(col: Int, row: Int): Int {
|
||||
return (nibbles[((row * width) + col)] and 0xff)
|
||||
return nibbles[row * width + col].toInt() and 255
|
||||
}
|
||||
|
||||
private fun encodeCachedImage(): ByteArray {
|
||||
|
||||
+10
-5
@@ -2,6 +2,7 @@ package com.faceclaw.app
|
||||
|
||||
import kotlin.jvm.JvmField
|
||||
import kotlin.jvm.JvmStatic
|
||||
import kotlin.jvm.JvmOverloads
|
||||
|
||||
/**
|
||||
* Retained-surface compositor: the phone-side model of what is on the glasses screen.
|
||||
@@ -30,7 +31,7 @@ import kotlin.jvm.JvmStatic
|
||||
* atomically under an internal lock, and each composite carries a monotonic sequence number so
|
||||
* callers can detect when a composite was superseded by a concurrent one before being acted on.
|
||||
*/
|
||||
class SurfaceCompositor {
|
||||
class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFrames: Boolean = true) {
|
||||
companion object {
|
||||
const val TRANSPARENCY_OPAQUE: Int = 0
|
||||
|
||||
@@ -206,7 +207,11 @@ class SurfaceCompositor {
|
||||
class Composite {
|
||||
@JvmField var screenGray: ByteArray
|
||||
@JvmField var shellScene: ShellScene = ShellScene.EMPTY
|
||||
/** Full-screen 8bpp grayscale pixels, screenWidth*screenHeight bytes. */
|
||||
/**
|
||||
* Full-screen preview pixels. When includePreviewInFrames is false,
|
||||
* submitted composites alias screenGray here; use previewComposite()
|
||||
* for the shell/selection-rendered preview, screenshots or recording.
|
||||
*/
|
||||
@JvmField val gray: ByteArray
|
||||
|
||||
@JvmField val width: Int
|
||||
@@ -573,7 +578,7 @@ class SurfaceCompositor {
|
||||
return null
|
||||
}
|
||||
val seq = nextCompositeSeq
|
||||
val result = compositeLocked()
|
||||
val result = compositeLocked(includePreview = true)
|
||||
nextCompositeSeq = seq
|
||||
return result
|
||||
}
|
||||
@@ -594,7 +599,7 @@ class SurfaceCompositor {
|
||||
return gray
|
||||
}
|
||||
|
||||
private fun compositeLocked(): Composite {
|
||||
private fun compositeLocked(includePreview: Boolean = includePreviewInFrames): Composite {
|
||||
var gray: ByteArray = ByteArray((screenWidth * screenHeight))
|
||||
if (blanked) {
|
||||
return Composite(
|
||||
@@ -662,7 +667,7 @@ class SurfaceCompositor {
|
||||
}
|
||||
}
|
||||
val scene = if (surfaces.values.any { it.visible && it.zOrder > 1 }) ShellScene.EMPTY else ShellScene(shellScene?.layers ?: emptyList(), selections)
|
||||
val preview = if (shellScene != null || selections.isNotEmpty()) scene.preview(gray, screenWidth, screenHeight) else gray
|
||||
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(),
|
||||
nextCompositeSeq++, draws.toTypedArray()).also { it.screenGray = gray; it.shellScene = scene }
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class BmpUtil {
|
||||
}
|
||||
|
||||
private val GRAY_TO_NIBBLE =
|
||||
ByteArray(256) { v -> if (v == 0) 0 else minOf(15, (v + 8) shr 4).toByte() }
|
||||
IntArray(256) { v -> minOf(15, (v + 8) shr 4) }
|
||||
|
||||
/**
|
||||
* The 4bpp level an 8-bit gray value packs to. Public so the resource-cache planner computes
|
||||
@@ -22,7 +22,7 @@ class BmpUtil {
|
||||
*/
|
||||
@JvmStatic
|
||||
fun nibbleForGray(gray: Int): Int {
|
||||
return (GRAY_TO_NIBBLE[(gray and 0xff)] and 0xff)
|
||||
return GRAY_TO_NIBBLE[gray and 0xff]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,32 +37,34 @@ class BmpUtil {
|
||||
if (((width <= 0) || (height <= 0))) {
|
||||
return ByteArray(0)
|
||||
}
|
||||
var stride: Int = ((width + 1) shr 1)
|
||||
var out: ByteArray = ByteArray((stride * height))
|
||||
val stride = (width + 1) ushr 1
|
||||
val out = ByteArray(stride * height)
|
||||
if (((gray8 == null) || (gray8.size < (width * height)))) {
|
||||
return out
|
||||
}
|
||||
var pairs: Int = (width shr 1)
|
||||
var src: Int = 0
|
||||
var dst: Int = 0
|
||||
run {
|
||||
var y: Int = 0
|
||||
while ((y < height)) {
|
||||
run {
|
||||
var i: Int = 0
|
||||
while ((i < pairs)) {
|
||||
out[dst++] =
|
||||
(((GRAY_TO_NIBBLE[(gray8[src] and 0xff)] shl 4) or
|
||||
(GRAY_TO_NIBBLE[(gray8[(src + 1)] and 0xff)] and 0xff)))
|
||||
.toByte()
|
||||
src += 2
|
||||
i++
|
||||
}
|
||||
// Work with Ints: Byte.and / Byte.shl in ByteSink.kt are ordinary
|
||||
// function calls, not Kotlin intrinsics. Avoid four calls per pair
|
||||
// even in an interpreted/debug ART build, and load the LUT once.
|
||||
val levels = GRAY_TO_NIBBLE
|
||||
var src = 0
|
||||
var dst = 0
|
||||
if (width and 1 == 0) {
|
||||
// No row padding: the common 640x480 frame is one linear loop.
|
||||
while (dst < out.size) {
|
||||
out[dst++] = ((levels[gray8[src].toInt() and 255] shl 4) or
|
||||
levels[gray8[src + 1].toInt() and 255]).toByte()
|
||||
src += 2
|
||||
}
|
||||
} else {
|
||||
repeat(height) {
|
||||
val end = src + width - 1
|
||||
while (src < end) {
|
||||
out[dst++] = ((levels[gray8[src].toInt() and 255] shl 4) or
|
||||
levels[gray8[src + 1].toInt() and 255]).toByte()
|
||||
src += 2
|
||||
}
|
||||
if (((width and 1) != 0)) {
|
||||
out[dst++] = ((GRAY_TO_NIBBLE[(gray8[src++] and 0xff)] shl 4)).toByte()
|
||||
}
|
||||
y++
|
||||
// Pad each odd-width row with a zero low nibble.
|
||||
out[dst++] = (levels[gray8[src++].toInt() and 255] shl 4).toByte()
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
||||
Reference in new issue
Block a user