Optimize phone CPU usage

This commit is contained in:
James Babcock committed 2026-09-27 23:16:41 -07:00
1 parent 449cb0bf73
commit 45d2ece0d2
19 files changed
+485 -103

No files matched your search

+47 -32
View File
@@ -277,17 +277,28 @@ export class GrayImage {
const destX = dx | 0;
const destY = dy | 0;
for (let row = 0; row < copyHeight; row++) {
const sy = srcY + row;
const ty = destY + row;
if (sy < 0 || sy >= source.height || ty < 0 || ty >= this.height) continue;
for (let col = 0; col < copyWidth; col++) {
const sx = srcX + col;
const tx = destX + col;
if (sx < 0 || sx >= source.width || tx < 0 || tx >= this.width) continue;
const value = source.pixels[sy * source.width + sx] ?? 0;
// Clip once, then copy row spans: flattening a frame blits every plane,
// and per-pixel bounds checks made that a sizeable share of a paint.
const colStart = Math.max(0, -destX);
const colEnd = Math.min(copyWidth, source.width - srcX, this.width - destX);
const rowStart = Math.max(0, -destY);
const rowEnd = Math.min(copyHeight, source.height - srcY, this.height - destY);
const span = colEnd - colStart;
const src = source.pixels;
const dst = this.pixels;
// A blit within one image copies pixel by pixel, forwards, as it always has.
const bulk = !opts.transparentZero && source !== this;
for (let row = rowStart; span > 0 && row < rowEnd; row++) {
const from = (srcY + row) * source.width + srcX + colStart;
const to = (destY + row) * this.width + destX + colStart;
if (bulk) {
dst.set(src.subarray(from, from + span), to);
continue;
}
for (let i = 0; i < span; i++) {
const value = src[from + i]!;
if (opts.transparentZero && value === 0) continue;
this.pixels[ty * this.width + tx] = value;
dst[to + i] = value;
}
}
@@ -398,7 +409,7 @@ export class GrayImage {
hash = mixInt(hash, placed.source.height);
hash = mixInt(hash, placed.source.sourceContentHash32());
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value);
if (p.displayList) for (const byte of encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)) hash = mixInt(hash, byte);
if (p.displayList) hash = mixInt(hash, bytesHash32(encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)));
}
} else {
hash = mixInt(hash, 0xf17e);
@@ -415,28 +426,9 @@ export class GrayImage {
return hash >>> 0;
}
/**
* FNV-1a over the raster pixels. Four bytes at a time where alignment
* allows: this runs on every frame's fingerprint, over the whole screen, on
* the thread that is about to hand the frame to the transport.
*/
/** Hash of the raster pixels; see bytesHash32. */
private pixelsHash32(): number {
const pixels = this.pixels;
let hash = 2166136261;
let i = 0;
if ((pixels.byteOffset & 3) === 0) {
const words = new Uint32Array(pixels.buffer, pixels.byteOffset, pixels.length >>> 2);
for (let w = 0; w < words.length; w++) {
hash ^= words[w]!;
hash = Math.imul(hash, 16777619);
}
i = words.length << 2;
}
for (; i < pixels.length; i++) {
hash ^= pixels[i]!;
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
return bytesHash32(this.pixels);
}
/**
@@ -744,6 +736,29 @@ function clampByte(value: number): number {
}
/** Fold a 32-bit int into an FNV-style running hash. */
/**
* FNV-1a over bytes. Four bytes at a time where alignment allows: this runs on
* every frame's fingerprint, over the whole screen, on the thread that is
* about to hand the frame to the transport.
*/
function bytesHash32(bytes: Uint8Array): number {
let hash = 2166136261;
let i = 0;
if ((bytes.byteOffset & 3) === 0) {
const words = new Uint32Array(bytes.buffer, bytes.byteOffset, bytes.length >>> 2);
for (let w = 0; w < words.length; w++) {
hash ^= words[w]!;
hash = Math.imul(hash, 16777619);
}
i = words.length << 2;
}
for (; i < bytes.length; i++) {
hash ^= bytes[i]!;
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
}
function mixInt(hash: number, value: number): number {
hash ^= value | 0;
return Math.imul(hash, 16777619);