Display lists

This commit is contained in:
James Babcock committed 2026-09-22 00:25:40 -07:00
1 parent a7af84e231
commit 4320021222
44 files changed
+823 -284

No files matched your search

+2
View File
@@ -15,6 +15,8 @@ import { GrayImage, type DeferredDraw } from "./image";
* existing incremental-update and screen-preview pipeline.
*/
export type Plane = {
shellKey?: number;
dimUnderneath?: number;
image: GrayImage;
/**
* Offset of the plane's top-left within the submitted frame. Applied
+33
View File
@@ -0,0 +1,33 @@
import { GrayImage } from './image'
import type { Plane } from './plane'
/** Crop one independently owned, opaque shell surface; never split it into tiles. */
export function shellCrop(image: GrayImage, x: number, y: number, width: number, height: number, key: number): Plane {
const baked = image.withDrawsBaked(), crop = new GrayImage(width, height, 0)
for (let row = 0; row < height; row++) crop.pixels.set(baked.pixels.subarray((y + row) * image.width + x, (y + row) * image.width + x + width), row * width)
return { image: crop, x, y, shellKey: key }
}
/** Shared Kotlin receives original 8-bit pixels and quantizes once, before composing. */
export function encodeShellScene(planes: readonly Plane[]): Uint8Array {
const layers: { image: GrayImage; x: number; y: number; key: number; dim: number }[] = []
for (const plane of planes) {
const image = plane.image.withDrawsBaked()
let left = image.width, top = image.height, right = -1, bottom = -1
for (let y = 0; y < image.height; y++) for (let x = 0; x < image.width; x++) {
if (image.pixels[y * image.width + x] === 0) continue
left = Math.min(left, x); right = Math.max(right, x); top = Math.min(top, y); bottom = Math.max(bottom, y)
}
if (right < left) continue
const w = right - left + 1, h = bottom - top + 1
if (5 + Math.ceil(w / 2) * h > 65536) throw new Error(`Shell surface ${plane.shellKey} exceeds 64 KiB (${w}×${h})`)
const cropped = shellCrop(image, left, top, w, h, plane.shellKey!)
layers.push({ image: cropped.image, x: plane.x + left, y: plane.y + top, key: plane.shellKey!, dim: Math.round((plane.dimUnderneath ?? 1) * 256) })
}
const result = new Uint8Array(2 + layers.reduce((n, l) => n + 14 + l.image.pixels.length, 0))
const view = new DataView(result.buffer); let p = 0
const word = (n: number) => { view.setUint16(p, n, true); p += 2 }
word(layers.length)
for (const l of layers) { word(l.key); word(l.x); word(l.y); word(l.image.width); word(l.image.height); word(l.dim); word(0); result.set(l.image.pixels, p); p += l.image.pixels.length }
return result
}
+1
View File
@@ -33,6 +33,7 @@ export class SurfaceCompositor {
this.native.submitDrawsIdDataXYWidthHeightDraws(id, toData(pixels), rect.x, rect.y, rect.width, rect.height,
draws ? toData(new Uint8Array(draws)) : null)
}
setShellScene(bytes: Uint8Array): void { this.native.shellData(toData(bytes)) }
composite(): Uint8Array { return fromData(this.native.composite()) }
compositeFrame(): { pixels: Uint8Array; textures: TextureFrame } {
const native = this.native.compositeFrame()
+19 -2
View File
@@ -9,6 +9,8 @@ type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean }
export class SurfaceCompositor {
private readonly surfaces = new Map<string, Surface>()
private shellScene: Uint8Array | null = null
setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes) }
private dimBelow = 0
private dimFactor = 1
private blanked = false
@@ -55,11 +57,11 @@ export class SurfaceCompositor {
composite(): Uint8Array {
const output = new Uint8Array(this.width * this.height)
if (this.blanked) return output
const surfaces = [...this.surfaces.values()].filter(s => s.visible).sort((a, b) => a.zOrder - b.zOrder)
const surfaces = [...this.surfaces.values()].filter(s => s.visible && !(this.shellScene && s === this.surfaces.get('shell'))).sort((a, b) => a.zOrder - b.zOrder)
for (const surface of surfaces) {
const left = Math.max(0, surface.x), right = Math.min(this.width, surface.x + surface.width)
const top = Math.max(0, surface.y), bottom = Math.min(this.height, surface.y + surface.height)
const dim = surface.zOrder < this.dimBelow ? this.dimFactor : 1
const dim = !this.shellScene && surface.zOrder < this.dimBelow ? this.dimFactor : 1
for (let y = top; y < bottom; y++) {
let source = (y - surface.y) * surface.width + left - surface.x
let target = y * this.width + left
@@ -72,6 +74,21 @@ export class SurfaceCompositor {
}
}
}
if (this.shellScene && !surfaces.some(s => s.zOrder > 1)) {
const quantize = (v: number) => Math.min(15, (v + 8) >> 4)
for (let i=0;i<output.length;i++) output[i]=quantize(output[i])*16
const bytes=this.shellScene, view=new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
let p=2
for(let i=0;i<view.getUint16(0,true);i++) {
const x=view.getInt16(p+2,true), y=view.getInt16(p+4,true), w=view.getUint16(p+6,true), h=view.getUint16(p+8,true), dim=view.getUint16(p+10,true)
p+=14
if(dim<256) for(let j=0;j<output.length;j++) output[j]=Math.floor((output[j]/16)*dim/256)*16
for(let yy=0;yy<h;yy++) for(let xx=0;xx<w;xx++) {
if(x+xx>=0 && y+yy>=0 && x+xx<this.width && y+yy<this.height) output[(y+yy)*this.width+x+xx]=quantize(bytes[p+yy*w+xx])*16
}
p+=w*h
}
}
return output
}
}