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
@@ -1,8 +1,12 @@
|
||||
/** Workers send baked grayscale frames to the main-thread iOS compositor. */
|
||||
/** Workers send baked pixels and shared-Kotlin atlas references together. */
|
||||
const display = {
|
||||
submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, _x: number, _y: number, width: number, height: number): void {
|
||||
submitSurfaceFrame(buffer: ArrayBuffer, surfaceId: string, _x: number, _y: number, width: number, height: number,
|
||||
_fingerprint?: string, _paintMs?: number, _frameId?: number, draws: ArrayBuffer | null = null): void {
|
||||
const data = NSData.dataWithBytesLength(interop.handleof(buffer), buffer.byteLength)
|
||||
global.postMessage({ type: 'surface-frame', surfaceId, width, height, pixels: data.base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions) })
|
||||
const drawData = draws ? NSData.dataWithBytesLength(interop.handleof(draws), draws.byteLength) : null
|
||||
global.postMessage({ type: 'surface-frame', surfaceId, width, height,
|
||||
pixels: data.base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions),
|
||||
draws: drawData?.base64EncodedStringWithOptions(0 as NSDataBase64EncodingOptions) })
|
||||
},
|
||||
}
|
||||
export function getActiveDisplay(): typeof display { return display }
|
||||
@@ -4,6 +4,8 @@ import { CfwTransport, parseCfwAcks } from '../g2/cfw-transport.ios'
|
||||
import { SurfaceCompositor } from '../graphics/surface-compositor.ios'
|
||||
import { buildBoundingBoxPayload } from '../g2/ble-image-optimizer.ios'
|
||||
import { lvglMetrics } from './lvgl-font.ios'
|
||||
import { textureImageId } from './texture-atlas.ios'
|
||||
import { IosTexturePlanner } from './texture-planner.ios'
|
||||
|
||||
/** Development-only checks of real NativeScript selectors and bulk data crossings. */
|
||||
export function runKotlinProtocolSmokeTest(): void {
|
||||
@@ -30,6 +32,18 @@ export function runKotlinProtocolSmokeTest(): void {
|
||||
assert(hex(c.composite()) === '0010ff203040', 'composition')
|
||||
assert(hex(protocol.packGray4(c.composite(), 3, 2)) === '01f02340', 'packing')
|
||||
c.setScreenBlanked(true); assert(c.composite().every(p => p === 0), 'blanking')
|
||||
const textures = new IosTexturePlanner(), textured = new SurfaceCompositor(4, 2)
|
||||
textured.configureSurface('icon', { x: 0, y: 0, width: 2, height: 2, zOrder: 0, transparency: 'opaque' })
|
||||
const white = new Uint8Array(4).fill(255)
|
||||
const icon = textureImageId('protocol-smoke-white', 2, 2, white)
|
||||
const draw = new DataView(new ArrayBuffer(9)); draw.setUint8(0, 1); draw.setUint32(1, icon, true)
|
||||
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.uploads[0]?.[0] === 18 && cold.payload[0] === 8, 'texture upload and draw')
|
||||
assert(textures.plan(null, packed, snapshot.textures, 1)?.uploads.length === 0, 'texture reuse')
|
||||
textures.reset()
|
||||
assert((textures.plan(null, packed, snapshot.textures, 1)?.uploads.length ?? 0) > 0, 'texture reset')
|
||||
const changed = new Uint8Array(16); changed[0] = 255
|
||||
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update')
|
||||
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { toData } from './kotlin-data'
|
||||
declare const FaceclawKitIosTextureAtlas: any
|
||||
const atlas = FaceclawKitIosTextureAtlas.new()
|
||||
|
||||
export function textureAtlasAvailable(): boolean { return true }
|
||||
export function textureFontId(key: string): number { return atlas.fontIdKey(key) }
|
||||
export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void {
|
||||
const data = toData(new Uint8Array(buffer))
|
||||
if (aa) atlas.registerAaGlyphsData(data)
|
||||
else atlas.registerGlyphsData(data)
|
||||
}
|
||||
export function registerFirmwareGlyphs(buffer: ArrayBuffer): void {
|
||||
atlas.registerFirmwareGlyphsData(toData(new Uint8Array(buffer)))
|
||||
}
|
||||
export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number {
|
||||
return atlas.imageIdKeyWidthHeightData(key, width, height, toData(pixels))
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
declare const com: any
|
||||
|
||||
/** Android bridge to the process-wide shared Kotlin atlases. */
|
||||
export function textureAtlasAvailable(): boolean { return !!global.isAndroid }
|
||||
export function textureFontId(key: string): number { return com.faceclaw.app.GlyphAtlas.fontId(key) }
|
||||
export function registerTextureGlyphs(buffer: ArrayBuffer, aa: boolean): void {
|
||||
const reader = new com.faceclaw.app.AndroidByteReader(buffer)
|
||||
if (aa) com.faceclaw.app.GlyphAtlas.registerAa(reader)
|
||||
else com.faceclaw.app.GlyphAtlas.register(reader)
|
||||
}
|
||||
export function registerFirmwareGlyphs(buffer: ArrayBuffer): void {
|
||||
com.faceclaw.app.FwGlyphAtlas.register(new com.faceclaw.app.AndroidByteReader(buffer))
|
||||
}
|
||||
export function textureImageId(key: string, width: number, height: number, pixels: Uint8Array): number {
|
||||
const buffer = pixels.byteOffset === 0 && pixels.byteLength === pixels.buffer.byteLength ? pixels.buffer : pixels.slice().buffer
|
||||
return com.faceclaw.app.ImageAtlas.ensure(key, width, height, new com.faceclaw.app.AndroidByteReader(buffer))
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { fromData, toData, nativeArray } from './kotlin-data'
|
||||
import type { SessionTexturePlanner, TextureFrame, TexturePlan } from '../g2/texture-planner'
|
||||
declare const FaceclawKitIosTexturePlanner: any
|
||||
|
||||
export class IosTexturePlanner implements SessionTexturePlanner {
|
||||
private readonly native = FaceclawKitIosTexturePlanner.new()
|
||||
reset(): void { this.native.reset() }
|
||||
plan(previous: Uint8Array | null, next: Uint8Array, frame: TextureFrame, firstId: number): TexturePlan | null {
|
||||
const result = this.native.planPreviousNextFrameFirstId(previous ? toData(previous) : null, toData(next), frame.native, firstId)
|
||||
return result ? { payload: fromData(result.payload), uploads: nativeArray(result.uploads, fromData),
|
||||
nextFid: result.nextFid, usedBytes: result.usedBytes } : null
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user