Cheaper frosted-glass blurs: sigma 4 and one backdrop read per bar
Blur sigma 16 -> 4 keeps CanvasKit on its direct blur path; under the 84% fill the look is unchanged (max 13/255 inside the pieces). SegmentedBar's pieces share a BackdropGroup so Impeller reads the content behind them once. Adds a scroll frame-time bench, a backdrop census and filler rows to the full-app probe. CPU CanvasKit, 560x900: run timeline p50 31.8 -> 22.1 ms, runs list 25.3 -> 19.9 ms.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// A page-side census of the backdrop blurs on screen, for the glass scroll
|
||||
// bench (tool/perf/glass_scroll_bench.mjs): `window.__backdropCensus()`
|
||||
// returns `{ blurs, reads }`: the backdrop filter layers in the current scene
|
||||
// (offstage routes are not in it), and how many reads of the content behind
|
||||
// they take where the engine shares a `BackdropGroup`'s read (Impeller; the
|
||||
// web renderer ignores the grouping and reads once per blur).
|
||||
import 'dart:js_interop';
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
@JS('__backdropCensus')
|
||||
external set _backdropCensus(JSFunction census);
|
||||
|
||||
void installBackdropCensus() {
|
||||
_backdropCensus = (() {
|
||||
var blurs = 0;
|
||||
var ungrouped = 0;
|
||||
final keys = <BackdropKey>{};
|
||||
void visit(Layer layer) {
|
||||
if (layer is BackdropFilterLayer) {
|
||||
blurs++;
|
||||
final key = layer.backdropKey;
|
||||
if (key == null) {
|
||||
ungrouped++;
|
||||
} else {
|
||||
keys.add(key);
|
||||
}
|
||||
}
|
||||
if (layer is ContainerLayer) {
|
||||
for (
|
||||
var child = layer.firstChild;
|
||||
child != null;
|
||||
child = child.nextSibling
|
||||
) {
|
||||
visit(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (final view in RendererBinding.instance.renderViews) {
|
||||
// The scene's root layer.
|
||||
// ignore: invalid_use_of_protected_member
|
||||
final root = view.layer;
|
||||
if (root != null) visit(root);
|
||||
}
|
||||
return {'blurs': blurs, 'reads': ungrouped + keys.length}.jsify();
|
||||
}).toJS;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env node
|
||||
// Pixel comparison of two same-sized screenshots, decoded in headless Chrome
|
||||
// (no npm packages):
|
||||
//
|
||||
// node tool/perf/compare_shots.mjs before.png after.png [diff.png]
|
||||
//
|
||||
// Prints the share of pixels that differ (any channel > 2/255 and > 16/255),
|
||||
// the mean and maximum channel difference, and per 60px horizontal band the
|
||||
// mean difference, so a change confined to the bars shows where it is. With a
|
||||
// third path it writes a diff image (differences amplified 8x).
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { openBrowser } from '../capture/browser.mjs';
|
||||
|
||||
const [a, b, out] = process.argv.slice(2);
|
||||
if (!a || !b) throw new Error('usage: compare_shots.mjs <a.png> <b.png> [diff.png]');
|
||||
const browser = await openBrowser();
|
||||
try {
|
||||
const url = (buf) => `data:image/png;base64,${buf.toString('base64')}`;
|
||||
const expression = `(async () => {
|
||||
const load = (src) => new Promise((ok, fail) => { const i = new Image(); i.onload = () => ok(i); i.onerror = fail; i.src = src; });
|
||||
const [ia, ib] = await Promise.all([load(${JSON.stringify(url(await readFile(a)))}), load(${JSON.stringify(url(await readFile(b)))})]);
|
||||
if (ia.width !== ib.width || ia.height !== ib.height) return { error: 'size mismatch' };
|
||||
const w = ia.width, h = ia.height;
|
||||
const px = (img) => { const c = new OffscreenCanvas(w, h); const g = c.getContext('2d'); g.drawImage(img, 0, 0); return g.getImageData(0, 0, w, h).data; };
|
||||
const da = px(ia), db = px(ib);
|
||||
const diff = new OffscreenCanvas(w, h); const dg = diff.getContext('2d'); const dd = dg.createImageData(w, h);
|
||||
let over2 = 0, over16 = 0, sum = 0, max = 0;
|
||||
const bands = new Array(Math.ceil(h / 60)).fill(0);
|
||||
for (let i = 0; i < w * h; i++) {
|
||||
let m = 0;
|
||||
for (let k = 0; k < 3; k++) { const d = Math.abs(da[i * 4 + k] - db[i * 4 + k]); m = Math.max(m, d); sum += d; dd.data[i * 4 + k] = Math.min(255, d * 8); }
|
||||
dd.data[i * 4 + 3] = 255;
|
||||
if (m > 2) over2++; if (m > 16) over16++; max = Math.max(max, m);
|
||||
bands[Math.floor(i / w / 60)] += m;
|
||||
}
|
||||
dg.putImageData(dd, 0, 0);
|
||||
const blob = await diff.convertToBlob({ type: 'image/png' });
|
||||
const bytes = new Uint8Array(await blob.arrayBuffer());
|
||||
let bin = ''; for (const x of bytes) bin += String.fromCharCode(x);
|
||||
const r = (x) => Math.round(x * 1000) / 1000;
|
||||
return { pixels: w * h, over2: r(over2 / (w * h)), over16: r(over16 / (w * h)), mean: r(sum / (w * h * 3)), max,
|
||||
bands: bands.map((s, i) => r(s / (w * Math.min(60, h - i * 60)))), diff: btoa(bin) };
|
||||
})()`;
|
||||
const page = await browser.newPage();
|
||||
const res = await page.send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true });
|
||||
await page.close();
|
||||
const v = res.result.value;
|
||||
if (!v || v.error) throw new Error(v?.error ?? JSON.stringify(res.exceptionDetails ?? res));
|
||||
if (out) await writeFile(out, Buffer.from(v.diff, 'base64'));
|
||||
delete v.diff;
|
||||
console.log(JSON.stringify(v));
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
#!/usr/bin/env node
|
||||
// Frame-time bench: scrolls a long list behind the frosted bars of the
|
||||
// full-app fixture (tool/search_selection_probe_entry.dart) and reports the
|
||||
// browser's frame cadence.
|
||||
//
|
||||
// node tool/perf/glass_scroll_bench.mjs --build build/probe-app \
|
||||
// --out /tmp/perf-glass/before [--gpu] [--size 560x900] [--seconds 6] \
|
||||
// [--repeat 3] [--platform material|cupertino] [--theme light|dark] \
|
||||
// [--shots] [--only runs-list|run-timeline] [--flags="<extra Chrome flags>"]
|
||||
//
|
||||
// Per scenario (the Runs list: 3 glass pieces; a run's Timeline tab: 5) it
|
||||
// opens the route, wheels the list one notch per animation frame for
|
||||
// --seconds, and records Flutter's frames: the interval between them, the
|
||||
// main-thread time each takes (build, layout, paint and rasterisation, up to
|
||||
// the next task) and how many 60 Hz vsyncs the intervals would miss. Chrome
|
||||
// runs with vsync and its frame-rate limit off, so an interval is a frame's
|
||||
// cost rather than a display tick. Without --gpu there is no WebGL and
|
||||
// CanvasKit rasterises on the CPU, where the blur cost shows (a proxy for a
|
||||
// weak GPU); with --gpu a desktop GPU usually hides it. It writes a
|
||||
// screenshot per scenario and `bench-*.json`, and prints the backdrop census
|
||||
// (tool/perf/backdrop_census.dart). --shots skips timing and writes a still at
|
||||
// a fixed scroll offset per scenario, for comparing the bars' pixels with
|
||||
// tool/perf/compare_shots.mjs.
|
||||
import { spawn } from 'node:child_process';
|
||||
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { parseArgs } from 'node:util';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { openBrowser } from '../capture/browser.mjs';
|
||||
import { serveBuild } from '../capture/serve.mjs';
|
||||
|
||||
const { values: opts } = parseArgs({
|
||||
options: {
|
||||
build: { type: 'string' }, out: { type: 'string' }, gpu: { type: 'boolean' },
|
||||
size: { type: 'string', default: '560x900' }, seconds: { type: 'string', default: '6' },
|
||||
repeat: { type: 'string', default: '3' }, platform: { type: 'string', default: 'material' },
|
||||
theme: { type: 'string', default: 'light' }, only: { type: 'string' },
|
||||
flags: { type: 'string', default: '' }, shots: { type: 'boolean' },
|
||||
},
|
||||
});
|
||||
if (!opts.build || !opts.out) throw new Error('usage: --build <dir> --out <dir> [--gpu]');
|
||||
const [width, height] = opts.size.split('x').map(Number);
|
||||
|
||||
// Scenario: an app route plus the filler rows that make it long.
|
||||
const SCENARIOS = [
|
||||
{ name: 'runs-list', route: '/runs', extra: { runs: '400' } },
|
||||
// The Timeline tab (second page tab, Material layout at 560 wide).
|
||||
{ name: 'run-timeline', route: '/runs/7', extra: { steps: '400' }, click: [396, 853] },
|
||||
];
|
||||
|
||||
// Installed before the app starts. Flutter's own animation-frame callbacks
|
||||
// are timed (main-thread frame work); the driver wheels the list from the
|
||||
// page itself, one notch per frame, and notes each frame's timestamp, so the
|
||||
// cadence is the browser's and not a DevTools round trip.
|
||||
const RECORDER = `(() => {
|
||||
const raf = window.requestAnimationFrame.bind(window);
|
||||
const rec = { on: false, work: [], starts: [], ticks: [] };
|
||||
window.__bench = rec;
|
||||
// Flutter rasterises after its frame callback returns (in microtasks), so a
|
||||
// frame's work runs until a message posted at the callback's end arrives.
|
||||
const channel = new MessageChannel();
|
||||
const pending = [];
|
||||
channel.port1.onmessage = () => { const t0 = pending.shift(); if (rec.on) rec.work.push(performance.now() - t0); };
|
||||
window.requestAnimationFrame = (cb) => raf((ts) => {
|
||||
const t0 = performance.now();
|
||||
if (rec.on) rec.starts.push(t0);
|
||||
try { cb(ts); } finally { pending.push(t0); channel.port2.postMessage(0); }
|
||||
});
|
||||
rec.drive = (ms, x, y, dy) => new Promise((done) => {
|
||||
rec.work = []; rec.starts = []; rec.ticks = []; rec.on = true;
|
||||
const end = performance.now() + ms;
|
||||
const tick = (ts) => {
|
||||
rec.ticks.push(ts);
|
||||
if (ts > end) { rec.on = false; return done({ ticks: rec.ticks, work: rec.work, starts: rec.starts }); }
|
||||
const target = document.elementFromPoint(x, y);
|
||||
target.dispatchEvent(new WheelEvent('wheel', { deltaY: dy, deltaMode: 0, clientX: x, clientY: y,
|
||||
screenX: x, screenY: y, bubbles: true, cancelable: true, composed: true }));
|
||||
raf(tick);
|
||||
};
|
||||
raf(tick);
|
||||
});
|
||||
})();`;
|
||||
|
||||
async function launchChrome() {
|
||||
const profile = await mkdtemp(join(tmpdir(), 'pipelines-bench-chrome-'));
|
||||
const args = ['--headless=new', '--no-first-run', '--no-default-browser-check', '--hide-scrollbars',
|
||||
'--mute-audio', '--disable-frame-rate-limit', '--disable-gpu-vsync',
|
||||
'--disable-renderer-backgrounding', '--disable-background-timer-throttling',
|
||||
'--remote-debugging-port=0', `--user-data-dir=${profile}`, 'about:blank'];
|
||||
if (!opts.gpu) args.splice(3, 0, '--disable-gpu');
|
||||
args.splice(3, 0, ...opts.flags.split(' ').filter(Boolean));
|
||||
const chrome = spawn(process.env.CHROME_PATH ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
||||
args, { stdio: ['ignore', 'ignore', 'pipe'] });
|
||||
let stderr = '';
|
||||
chrome.stderr.on('data', (d) => { stderr += d; });
|
||||
for (let i = 0; i < 150 && !/DevTools listening on ws:\/\/127\.0\.0\.1:(\d+)/.test(stderr); i++) await delay(100);
|
||||
const port = stderr.match(/DevTools listening on ws:\/\/127\.0\.0\.1:(\d+)/)?.[1];
|
||||
const close = async () => { chrome.kill('SIGKILL'); await rm(profile, { recursive: true, force: true }).catch(() => {}); };
|
||||
if (!port) { await close(); throw new Error(`Chrome did not start: ${stderr.slice(-500)}`); }
|
||||
return { cdp: `http://127.0.0.1:${port}`, close };
|
||||
}
|
||||
|
||||
const pct = (sorted, p) => sorted[Math.min(sorted.length - 1, Math.floor(p * sorted.length))];
|
||||
function summarise({ ticks, work, starts }) {
|
||||
const intervals = ticks.slice(1).map((t, i) => t - ticks[i]).sort((a, b) => a - b);
|
||||
const seconds = (ticks.at(-1) - ticks[0]) / 1000;
|
||||
const flutterIntervals = starts.slice(1).map((t, i) => t - starts[i]).sort((a, b) => a - b);
|
||||
work = [...work].sort((a, b) => a - b);
|
||||
const r = (x) => Math.round(x * 100) / 100;
|
||||
const stats = (xs) => ({ p50: r(pct(xs, 0.5)), p90: r(pct(xs, 0.9)), p99: r(pct(xs, 0.99)) });
|
||||
return {
|
||||
frames: ticks.length,
|
||||
interval: stats(intervals),
|
||||
work: stats(work),
|
||||
flutterFrames: starts.length,
|
||||
flutterFps: r(starts.length / seconds),
|
||||
flutterInterval: stats(flutterIntervals),
|
||||
// Each extra vsync an interval spans is one dropped frame.
|
||||
dropped: intervals.reduce((n, d) => n + Math.max(0, Math.round(d / 16.67) - 1), 0),
|
||||
};
|
||||
}
|
||||
|
||||
await mkdir(opts.out, { recursive: true });
|
||||
const server = await serveBuild(opts.build);
|
||||
const chrome = await launchChrome();
|
||||
const browser = await openBrowser({ cdp: chrome.cdp });
|
||||
const { send } = browser;
|
||||
const evaluate = async (expression) => (await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true })).result.value;
|
||||
const results = { build: opts.build, gpu: Boolean(opts.gpu), size: opts.size, platform: opts.platform, theme: opts.theme, scenarios: {} };
|
||||
try {
|
||||
await send('Page.enable');
|
||||
await send('Runtime.enable');
|
||||
await send('Page.bringToFront');
|
||||
await send('Page.addScriptToEvaluateOnNewDocument', { source: RECORDER });
|
||||
await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: false });
|
||||
await send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: opts.theme }] });
|
||||
results.renderer = await evaluate(`(() => { const gl = document.createElement('canvas').getContext('webgl2');
|
||||
return gl ? gl.getParameter(gl.getExtension('WEBGL_debug_renderer_info').UNMASKED_RENDERER_WEBGL) : 'none (CPU)'; })()`);
|
||||
console.log('renderer:', results.renderer);
|
||||
for (const s of SCENARIOS.filter((s) => !opts.only || s.name === opts.only)) {
|
||||
const runs = [];
|
||||
for (let rep = 0; rep < Number(opts.repeat); rep++) {
|
||||
const url = new URL('/', server.origin);
|
||||
url.searchParams.set('route', s.route);
|
||||
url.searchParams.set('platform', opts.platform);
|
||||
url.searchParams.set('theme', opts.theme);
|
||||
for (const [k, v] of Object.entries(s.extra)) url.searchParams.set(k, v);
|
||||
await send('Page.navigate', { url: url.href });
|
||||
for (let i = 0; i < 300 && !(await evaluate("Boolean(document.querySelector('flt-glass-pane'))")); i++) await delay(100);
|
||||
await delay(2500);
|
||||
const x = width / 2;
|
||||
const y = height / 2;
|
||||
if (s.click) {
|
||||
for (const type of ['mouseMoved', 'mousePressed', 'mouseReleased']) {
|
||||
await send('Input.dispatchMouseEvent', { type, x: s.click[0], y: s.click[1], button: 'left', clickCount: 1 });
|
||||
}
|
||||
await delay(1500);
|
||||
}
|
||||
await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x, y });
|
||||
await delay(300);
|
||||
const census = await evaluate('window.__backdropCensus?.()');
|
||||
if (rep === 0) console.log(`${s.name} backdrops on screen:`, JSON.stringify(census));
|
||||
if (opts.shots) {
|
||||
// A still at a fixed scroll offset, for before/after comparison.
|
||||
await send('Input.dispatchMouseEvent', { type: 'mouseWheel', x, y, deltaX: 0, deltaY: 250 });
|
||||
await send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: 1, y: height / 2 });
|
||||
await delay(1500);
|
||||
const still = (await send('Page.captureScreenshot', { format: 'png' })).data;
|
||||
await writeFile(join(opts.out, `${s.name}-${opts.platform}-${opts.theme}-still.png`), Buffer.from(still, 'base64'));
|
||||
break;
|
||||
}
|
||||
const frames = await evaluate(`window.__bench.drive(${Number(opts.seconds) * 1000}, ${x}, ${y}, 6)`);
|
||||
runs.push({ ...summarise(frames), census });
|
||||
await delay(500);
|
||||
const shot = rep === 0 ? (await send('Page.captureScreenshot', { format: 'png' })).data : null;
|
||||
if (shot) await writeFile(join(opts.out, `${s.name}-${opts.platform}-${opts.theme}.png`), Buffer.from(shot, 'base64'));
|
||||
}
|
||||
results.scenarios[s.name] = runs;
|
||||
console.log(s.name, JSON.stringify(runs));
|
||||
}
|
||||
if (!opts.shots) await writeFile(join(opts.out, `bench-${opts.gpu ? 'gpu' : 'sw'}-${opts.platform}-${opts.theme}.json`), JSON.stringify(results, null, 2));
|
||||
} finally {
|
||||
await browser.close().catch(() => {});
|
||||
await chrome.close();
|
||||
await server.close();
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import 'package:flutter/widgets.dart';
|
||||
import 'package:web/web.dart' as web;
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
|
||||
import 'perf/backdrop_census.dart';
|
||||
import 'probe_folder_fixture.dart';
|
||||
|
||||
/// The run every run-scoped hit names. Its recorded sequences are deliberately
|
||||
@@ -79,9 +80,42 @@ JsonMap probeRunDetail() => {
|
||||
'started_at': '2026-10-20T08:12:00Z',
|
||||
'finished_at': '2026-10-20T08:14:00Z',
|
||||
},
|
||||
..._fillerSteps(),
|
||||
],
|
||||
};
|
||||
|
||||
/// Extra rows for scroll-performance captures (`?runs=N`, `?steps=N`). Both
|
||||
/// default to none, so every other capture sees the fixture unchanged.
|
||||
int _fillerRunCount = 0;
|
||||
int _fillerStepCount = 0;
|
||||
|
||||
List<JsonMap> _fillerSteps() => [
|
||||
for (var i = 0; i < _fillerStepCount; i++)
|
||||
{
|
||||
'stage': i.isEven ? 'build' : 'plan',
|
||||
'sequence': 100 + i,
|
||||
'status': 'completed',
|
||||
'outcome': 'success',
|
||||
'target': i.isEven ? 'plan' : 'build',
|
||||
'started_at': '2026-10-20T08:32:00Z',
|
||||
'finished_at': '2026-10-20T08:33:00Z',
|
||||
'diagnostic': 'filler step $i for scroll captures',
|
||||
},
|
||||
];
|
||||
|
||||
List<JsonMap> _fillerRuns() => [
|
||||
for (var i = 0; i < _fillerRunCount; i++)
|
||||
{
|
||||
'id': 1000 + i,
|
||||
'title': 'Filler run $i for scroll captures',
|
||||
'name': 'filler-$i',
|
||||
'status': i % 3 == 0 ? 'failed' : 'succeeded',
|
||||
'created_at': '2026-10-18T08:12:00Z',
|
||||
'finished_at': '2026-10-18T08:42:00Z',
|
||||
'pipeline_id': i.isEven ? 24 : 22,
|
||||
},
|
||||
];
|
||||
|
||||
List<JsonMap> probeRuns() => [
|
||||
{
|
||||
'id': 7,
|
||||
@@ -101,6 +135,7 @@ List<JsonMap> probeRuns() => [
|
||||
'finished_at': '2026-10-19T22:03:00Z',
|
||||
'pipeline_id': 22,
|
||||
},
|
||||
..._fillerRuns(),
|
||||
];
|
||||
|
||||
List<JsonMap> probePipelines() => [
|
||||
@@ -480,6 +515,8 @@ class _FixtureSink implements WebSocketSink {
|
||||
/// fallback for `/runs/7`) can still open a deep location:
|
||||
///
|
||||
/// `?route=/runs/7/stage/build%3Fvisit%3D1` opens that app location;
|
||||
/// `?runs=200` / `?steps=60` append filler runs / run-7 steps for scroll
|
||||
/// performance captures;
|
||||
/// `?q=release` types that query into Search once its field is shown;
|
||||
/// `?platform=cupertino` renders the Cupertino host (`material`, the
|
||||
/// default, renders Material as browsers do);
|
||||
@@ -489,6 +526,9 @@ class _FixtureSink implements WebSocketSink {
|
||||
/// app/tool/capture/ drives this entry generically; see its README.
|
||||
void main() {
|
||||
final params = Uri.base.queryParameters;
|
||||
installBackdropCensus();
|
||||
_fillerRunCount = int.tryParse(params['runs'] ?? '') ?? 0;
|
||||
_fillerStepCount = int.tryParse(params['steps'] ?? '') ?? 0;
|
||||
final cupertino = params['platform'] == 'cupertino';
|
||||
final route = params['route'];
|
||||
if (route != null && route.startsWith('/')) {
|
||||
|
||||
Reference in New Issue
Block a user