@alpha2-sh/editor 0.2.2 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -38,7 +38,7 @@ editor.dispose();
38
38
 
39
39
  ## Browser bridge
40
40
 
41
- `@alpha2-sh/editor/browser` connects a session to the engine in the browser: import, preview, playback with audio, and export to an MP4 file through WebCodecs.
41
+ `@alpha2-sh/editor/browser` connects a session to the engine in the browser: import, preview, playback with audio, and export to an MP4, MOV, MKV or WebM file through WebCodecs.
42
42
 
43
43
  ```js
44
44
  import { createEditor, createEmptyDocument } from "@alpha2-sh/editor";
@@ -75,7 +75,7 @@ const placed = editor.dispatch({
75
75
  if (!placed.ok) throw placed.error;
76
76
  await bridge.seek(0);
77
77
 
78
- const artifact = await bridge.startExport({ videoCodec: "avc" }).result;
78
+ const artifact = await bridge.startExport({ container: "webm" }).result;
79
79
  // artifact.kind "file": a temporary file in the origin's private storage; release it once downloaded.
80
80
  if (artifact.kind === "file") await artifact.release();
81
81
 
@@ -86,7 +86,7 @@ await editor.dispose();
86
86
  - Import checks each file with the engine (media, fonts, `.cube` LUTs, SRT and WebVTT captions through `importCaptions`) and adds it to the document in one undoable step. `importSequence` imports numbered PNG or OpenEXR files as one image sequence.
87
87
  - `bindPreview` draws on a canvas. With `runtimeAssets.workerFactory`, a worker running the `@alpha2-sh/editor/preview-worker` entry renders the preview off the main thread; without one, or when the worker fails, the main thread does. `hashWorkerFactory` (`@alpha2-sh/editor/hash-worker`) and `extensionsWorkerFactory` (`@alpha2-sh/editor/extensions-worker`, with `extensionsWasmUrl`) move file hashing and SVG, animated image, Lottie and OpenEXR reading into workers too.
88
88
  - `seek`, `play` and `pause` move the playhead; with audio, playback follows the audio clock. Audio preview needs a cross-origin isolated page; editing and video preview do not.
89
- - `startExport` returns a job with `result`, progress (`subscribe`) and `cancel`. The output goes to a temporary file in the origin's private storage by default (`release()` deletes it), to a file the user picked (`output: { kind: "file", handle }`), or to memory (`output: { kind: "memory", maxBytes }`). Whether the browser encodes a codec is the browser's to say: `bridge.videoEncoders()` asks ahead, and an export with a codec the browser does not encode is refused before it renders.
89
+ - `startExport` returns a job with `result`, progress (`subscribe`) and `cancel`. `container` (`mp4` by default, `mov`, `mkv` or `webm`) names the file and decides the codecs: an absent `videoCodec` or `audioCodec` is the container's default (H.264 and AAC, or VP9 and Opus for WebM, with Opus where a browser cannot encode AAC in MP4 or MKV), and one the container does not offer is refused. The output goes to a temporary file in the origin's private storage by default (`release()` deletes it), to a file the user picked (`output: { kind: "file", handle }`), or to memory (`output: { kind: "memory", maxBytes }`). Whether the browser encodes a codec is the browser's to say: `bridge.videoEncoders()` asks ahead, and an export with a codec the browser does not encode is refused before it renders.
90
90
 
91
91
  ## React
92
92
 
@@ -2,6 +2,7 @@ import { frameAt, frameStart } from "../core/time.js";
2
2
  import { compileEditorDocument, EditorError, sequenceEnd, } from "../core/document.js";
3
3
  import { captionsAsset } from "../core/captions.js";
4
4
  import { MAX_CAPTION_FILE_BYTES } from "alpha2/project";
5
+ import { containerPolicy } from "alpha2/containers";
5
6
  import { drawnEffectResources, shownSources } from "../core/compositions.js";
6
7
  import { ResourceRegistry } from "./resources.js";
7
8
  import { ExportJob, requireExportFrameRate } from "./jobs.js";
@@ -197,7 +198,8 @@ export function createMediaAdapter({ extensionsWasmUrl, wasmUrl, hashWorkerFacto
197
198
  * @returns {import("./types.js").BrowserBridge}
198
199
  */
199
200
  export function createBrowserBridge(options) {
200
- const { editor, runtimeAssets = {}, mediaAdapter = createMediaAdapter(runtimeAssets), resourceLimit = 128, audio = true, onError = () => { }, assetDecisions, } = options;
201
+ const { editor, runtimeAssets = {}, mediaAdapter = createMediaAdapter(runtimeAssets), resourceLimit = 128, audio = true, onError = () => { }, assetDecisions, previewScale: initialPreviewScale = 0.5, } = options;
202
+ let previewScale = checkedPreviewScale(initialPreviewScale);
201
203
  // A test's stand-in for origin storage, kept out of the public options.
202
204
  const proxyStore =
203
205
  /** @type {{ proxyStore?: ProxyStore }} */ (options).proxyStore ??
@@ -428,7 +430,12 @@ export function createBrowserBridge(options) {
428
430
  // How the current or last playback was asked for, so a recovered preview plays on alike.
429
431
  playRequest = null, renderScheduled = false, raf = 0, playClock = null, loadRevision = 0, loadedAssets = new Map(), loadedSelection = "", loadedFonts = null, audioKey = null,
430
432
  // What the preview worker holds from its last load.
431
- workerSent = new Map(), workerFonts = null, refreshFrame = 0, monitorVolume = 1, playGeneration = 0, sessionGeneration = editor.generation;
433
+ workerSent = new Map(), workerFonts = null, refreshFrame = 0, monitorVolume = 1,
434
+ // Tracing the drawing renderer applies, kept for every renderer a binding makes.
435
+ passTiming = false, traceUploads = false,
436
+ // The worker's last renderer diagnostics snapshot, and counters since `bindPreview`: playback
437
+ // refreshes that asked for no frame, and frames copied to the bound canvas.
438
+ workerDiagnostics = null, skippedRequests = 0, bitmapTransfers = 0, playGeneration = 0, sessionGeneration = editor.generation;
432
439
  /**
433
440
  * Preview's media for a source of `document`: what the engine selected, the original unless it is
434
441
  * offline, opened as the document's source says.
@@ -744,11 +751,11 @@ export function createBrowserBridge(options) {
744
751
  reportRefusedFonts(refused, "preview");
745
752
  }
746
753
  engine.loadProject(compiled.project);
747
- engine.resize(compiled.project.settings.width, compiled.project.settings.height);
748
- if (boundCanvas.width !== compiled.project.settings.width)
749
- boundCanvas.width = compiled.project.settings.width;
750
- if (boundCanvas.height !== compiled.project.settings.height)
751
- boundCanvas.height = compiled.project.settings.height;
754
+ // Frames render at the document's size times the preview scale, and the surfaces are that
755
+ // size: the engine rounds it.
756
+ let renderSize = engine.previewSize;
757
+ engine.resize(renderSize[0], renderSize[1]);
758
+ // Sources are fitted in document space; the render scales them.
752
759
  sources.setOutputSize(compiled.project.settings.width, compiled.project.settings.height);
753
760
  audioKey = null;
754
761
  if (workerPreview) {
@@ -792,15 +799,18 @@ export function createBrowserBridge(options) {
792
799
  workerSent = new Map();
793
800
  workerFonts = null;
794
801
  try {
795
- await workerPreview.replaceProject(compiled.project, ++loadRevision, workerAssets, sendFonts ? allFonts : null, keep);
802
+ const reply = await workerPreview.replaceProject(compiled.project, ++loadRevision, workerAssets, sendFonts ? allFonts : null, keep);
796
803
  workerSent = new Map(used);
797
804
  workerFonts = allFonts;
805
+ if (reply?.renderSize)
806
+ renderSize = reply.renderSize;
798
807
  }
799
808
  catch (error) {
800
809
  // The main renderer has just loaded the same project.
801
810
  await abandonWorker(error);
802
811
  }
803
812
  }
813
+ sizeBoundCanvas(renderSize);
804
814
  renderKey = key;
805
815
  }
806
816
  if (settled && audioKey !== key) {
@@ -1036,10 +1046,15 @@ export function createBrowserBridge(options) {
1036
1046
  if (workerPreview) {
1037
1047
  try {
1038
1048
  const result = await workerPreview.seek(at);
1039
- if (result.bitmap)
1049
+ if (result.engineDiagnostics)
1050
+ workerDiagnostics = result.engineDiagnostics;
1051
+ // An unchanged frame comes without a bitmap: the canvas still shows it.
1052
+ if (result.bitmap) {
1040
1053
  boundCanvas
1041
1054
  .getContext("bitmaprenderer")
1042
1055
  .transferFromImageBitmap(result.bitmap);
1056
+ bitmapTransfers += 1;
1057
+ }
1043
1058
  return result.rendered;
1044
1059
  }
1045
1060
  catch (error) {
@@ -1049,16 +1064,38 @@ export function createBrowserBridge(options) {
1049
1064
  await abandonWorker(error);
1050
1065
  }
1051
1066
  }
1052
- const rendered = await engine.render(at / 120000, {
1067
+ const { rendered, unchanged } = await engine.render(at / 120000, {
1053
1068
  generation: engine.generation,
1054
1069
  context: context(project, library),
1055
1070
  });
1056
- if (rendered && copySurface)
1071
+ // An unchanged frame was not presented: the bound canvas still shows the last copy.
1072
+ if (rendered && !unchanged && copySurface) {
1057
1073
  boundCanvas
1058
1074
  .getContext("bitmaprenderer")
1059
1075
  .transferFromImageBitmap(engine.canvas.transferToImageBitmap());
1076
+ bitmapTransfers += 1;
1077
+ }
1060
1078
  return rendered;
1061
1079
  }
1080
+ /** Gives the bound canvas the size frames render at, which `size` is, unless it has it. */
1081
+ function sizeBoundCanvas(size) {
1082
+ if (!boundCanvas || !size)
1083
+ return;
1084
+ if (boundCanvas.width !== size[0])
1085
+ boundCanvas.width = size[0];
1086
+ if (boundCanvas.height !== size[1])
1087
+ boundCanvas.height = size[1];
1088
+ }
1089
+ /** Applies the bridge's tracing to the renderers drawing now. */
1090
+ function applyTracing() {
1091
+ if (engine) {
1092
+ engine.traceUploads = traceUploads;
1093
+ engine.setPassTiming?.(passTiming);
1094
+ }
1095
+ void workerPreview
1096
+ ?.setTracing({ passTiming, traceUploads })
1097
+ .catch(() => { });
1098
+ }
1062
1099
  /**
1063
1100
  * Gives up the preview worker after `error`, whether it failed to start, to load a project or to
1064
1101
  * draw: the main renderer, which loads every project the worker does, draws from then on, off
@@ -1179,7 +1216,7 @@ export function createBrowserBridge(options) {
1179
1216
  playing = true;
1180
1217
  playRequest = { rate, loop, policy };
1181
1218
  const clock = (playClock = engine.playbackClock(policy, project.settings.frame_rate, rate, tick, performance.now()));
1182
- // For a policy that waits for frames: the tick last asked for, and whether it has been shown.
1219
+ // The tick last asked for, and for a policy that waits for frames, whether it has been shown.
1183
1220
  let requested = null, presented = false;
1184
1221
  const step = (now) => {
1185
1222
  if (!playing || disposed || generation !== playGeneration)
@@ -1208,15 +1245,17 @@ export function createBrowserBridge(options) {
1208
1245
  .catch(onError);
1209
1246
  return;
1210
1247
  }
1211
- // A waiting policy asks for each frame once and moves on only after it is shown; the others
1212
- // ask for the newest time and let late frames go.
1213
- const due = waits ? requested !== tick : true;
1214
- if (due && !renderScheduled) {
1248
+ // The clock asks for frame starts, so every policy asks for each frame once however often
1249
+ // the display refreshes; a waiting policy also moves on only after it is shown, and the
1250
+ // others let late frames go.
1251
+ const due = requested !== tick;
1252
+ if (!due)
1253
+ skippedRequests += 1;
1254
+ else if (!renderScheduled) {
1215
1255
  renderScheduled = true;
1216
- if (waits) {
1217
- requested = tick;
1256
+ requested = tick;
1257
+ if (waits)
1218
1258
  presented = false;
1219
- }
1220
1259
  const at = tick;
1221
1260
  void enqueue(() => generation !== playGeneration ? false : renderAt(at, project))
1222
1261
  .then(() => {
@@ -1254,21 +1293,27 @@ export function createBrowserBridge(options) {
1254
1293
  extensionsWasmUrl: runtimeAssets.extensionsWasmUrl,
1255
1294
  });
1256
1295
  const { Engine } = await import("./runtime/engine.js");
1296
+ // Until a project loads and gives the render size (see `loaded`).
1257
1297
  canvas.width = editor.document.project.settings.width;
1258
1298
  canvas.height = editor.document.project.settings.height;
1259
1299
  boundCanvas = canvas;
1260
1300
  copySurface = false;
1261
1301
  await workerPreview?.dispose();
1262
1302
  workerPreview = null;
1303
+ workerDiagnostics = null;
1263
1304
  const useWorker = features.worker.available && typeof canvas.getContext === "function";
1264
- engine = new Engine(useWorker ? new OffscreenCanvas(canvas.width, canvas.height) : canvas, sources, library, runtimeAssets);
1305
+ engine = new Engine(useWorker ? new OffscreenCanvas(canvas.width, canvas.height) : canvas, sources, library, runtimeAssets,
1306
+ // A frame shown at once matters more in a preview than the engine's conversion of it.
1307
+ { unreadableFrames: "present" });
1265
1308
  mixer = new EngineAudioMixer(runtimeAssets, { enabled: audio });
1266
1309
  mixer.setMasterVolume(monitorVolume);
1267
1310
  stalledAudio = null;
1268
1311
  supplier = new AudioWindowSupplier(library);
1269
1312
  try {
1270
1313
  await engine.start();
1314
+ engine.setPreviewScale(previewScale);
1271
1315
  capabilities = capabilityReport(engine.capabilityReport());
1316
+ await registerKeptEffects();
1272
1317
  // The surface already shows copied frames (its context is a bitmap renderer's).
1273
1318
  if (useWorker && workerGivenUp)
1274
1319
  copySurface = true;
@@ -1290,11 +1335,16 @@ export function createBrowserBridge(options) {
1290
1335
  // A worker that cannot start leaves the surface to the main renderer.
1291
1336
  try {
1292
1337
  await workerPreview.ready;
1338
+ await workerPreview.setPreviewScale(previewScale);
1339
+ if (effectDefinitions.size)
1340
+ await workerPreview.registerEffects(keptEffects());
1293
1341
  }
1294
1342
  catch (error) {
1295
1343
  await abandonWorker(error);
1296
1344
  }
1297
1345
  }
1346
+ if (passTiming || traceUploads)
1347
+ applyTracing();
1298
1348
  if (disposed || token !== binding) {
1299
1349
  await engine.dispose();
1300
1350
  engine = null;
@@ -1346,6 +1396,68 @@ export function createBrowserBridge(options) {
1346
1396
  }
1347
1397
  return true;
1348
1398
  }
1399
+ /**
1400
+ * Runtime effects `registerEffects` kept, by `id@version`, with their JSON text, in the order they
1401
+ * came: every renderer the bridge makes registers them.
1402
+ */
1403
+ const effectDefinitions = new Map();
1404
+ const keptEffects = () => [...effectDefinitions.values()].map(({ definition }) => definition);
1405
+ function registerEffects(definitions) {
1406
+ return enqueue(async () => {
1407
+ const added = new Map();
1408
+ for (const definition of definitions) {
1409
+ if (typeof definition?.id !== "string" ||
1410
+ !Number.isInteger(definition.version))
1411
+ throw new EditorError("invalid_effect", "An effect definition needs a string id and an integer version", "effects");
1412
+ const key = `${definition.id}@${definition.version}`;
1413
+ const text = JSON.stringify(definition);
1414
+ const known = (added.get(key) ?? effectDefinitions.get(key))?.text;
1415
+ if (known === text)
1416
+ continue;
1417
+ if (known !== undefined)
1418
+ throw new EditorError("effect_conflict", `Effect ${key} is registered with another definition; a changed effect takes another version`, "effects", key);
1419
+ added.set(key, { definition, text });
1420
+ }
1421
+ if (!added.size)
1422
+ return;
1423
+ const fresh = [...added.values()].map(({ definition }) => definition);
1424
+ // The main renderer loads every project the worker does, so both hold every effect.
1425
+ if (engine)
1426
+ await engine.registerEffects(fresh);
1427
+ if (workerPreview) {
1428
+ try {
1429
+ await workerPreview.registerEffects(fresh);
1430
+ }
1431
+ catch (error) {
1432
+ // The main renderer has just registered the same definitions.
1433
+ await abandonWorker(error);
1434
+ }
1435
+ }
1436
+ for (const [key, entry] of added)
1437
+ effectDefinitions.set(key, entry);
1438
+ // A frame that drew one of them failed; it draws now.
1439
+ if (engine)
1440
+ refreshPreview();
1441
+ });
1442
+ }
1443
+ /**
1444
+ * Registers the kept runtime effects on the main renderer `buildPreview` just started, one by
1445
+ * one: one its device refuses (kept while no preview was bound, so never compiled) is dropped and
1446
+ * reported, and the others draw. A device lost meanwhile drops none: the next render makes the
1447
+ * preview again, which registers them again.
1448
+ */
1449
+ async function registerKeptEffects() {
1450
+ const kept = [...effectDefinitions];
1451
+ const results = await Promise.allSettled(kept.map(([, { definition }]) => engine.registerEffects([definition])));
1452
+ if (engine.deviceLost)
1453
+ return;
1454
+ for (const [index, result] of results.entries()) {
1455
+ if (result.status === "fulfilled")
1456
+ continue;
1457
+ effectDefinitions.delete(kept[index][0]);
1458
+ onError(result.reason);
1459
+ }
1460
+ }
1349
1461
  /** Whether a renderer is ready, making it again first if a lost GPU left none. */
1350
1462
  async function previewReady() {
1351
1463
  if (engine)
@@ -1354,11 +1466,43 @@ export function createBrowserBridge(options) {
1354
1466
  return false;
1355
1467
  return recoverPreview();
1356
1468
  }
1469
+ /**
1470
+ * Renders the preview at `scale` times the document's size from now on, sizing the surfaces to
1471
+ * it, and draws the current frame again. Resolves once that frame is drawn.
1472
+ */
1473
+ async function setPreviewScale(scale) {
1474
+ const next = checkedPreviewScale(scale);
1475
+ if (next === previewScale)
1476
+ return;
1477
+ previewScale = next;
1478
+ await enqueue(async () => {
1479
+ if (!engine || disposed)
1480
+ return;
1481
+ engine.setPreviewScale(previewScale);
1482
+ let size = renderKey === null ? null : engine.previewSize;
1483
+ if (size)
1484
+ engine.resize(size[0], size[1]);
1485
+ if (workerPreview) {
1486
+ try {
1487
+ const reply = await workerPreview.setPreviewScale(previewScale);
1488
+ if (reply?.renderSize)
1489
+ size = reply.renderSize;
1490
+ }
1491
+ catch (error) {
1492
+ await abandonWorker(error);
1493
+ }
1494
+ }
1495
+ sizeBoundCanvas(size);
1496
+ await renderAt(tick, await loaded());
1497
+ });
1498
+ }
1357
1499
  async function bindPreview(canvas) {
1358
1500
  const token = ++binding;
1359
1501
  await enqueue(async () => {
1360
1502
  recoveryPending = false;
1361
1503
  workerGivenUp = false;
1504
+ skippedRequests = 0;
1505
+ bitmapTransfers = 0;
1362
1506
  if (!(await buildPreview(canvas, token)))
1363
1507
  return;
1364
1508
  // The surface is bound; a failing first frame is reported like any later one.
@@ -1595,8 +1739,11 @@ export function createBrowserBridge(options) {
1595
1739
  if (jobs.size)
1596
1740
  throw new EditorError("export_busy", "Only one export may run per bridge", "export");
1597
1741
  requireExportFrameRate(options.frameRate);
1742
+ const videoDecode = options.videoDecode ?? "auto";
1743
+ if (videoDecode !== "auto" && videoDecode !== "software")
1744
+ throw new EditorError("invalid_video_decode", `Export videoDecode must be "auto" or "software"; received ${JSON.stringify(videoDecode)}`, "export");
1598
1745
  materializeText(editor.document);
1599
- const snapshot = editor.document, revision = editor.revision;
1746
+ const snapshot = editor.document, revision = editor.revision, effects = keptEffects();
1600
1747
  const compiled = compileEditorDocument(snapshot);
1601
1748
  if (compiled.diagnostics.length)
1602
1749
  throw compiled.diagnostics[0];
@@ -1640,6 +1787,7 @@ export function createBrowserBridge(options) {
1640
1787
  const project = compiled.project, lib = { get: (id) => map.get(id) };
1641
1788
  const pixels = new SourcePixels(lib, {
1642
1789
  extensionsWasmUrl: runtimeAssets.extensionsWasmUrl,
1790
+ videoDecode,
1643
1791
  });
1644
1792
  const renderer = new Engine(new OffscreenCanvas(project.settings.width, project.settings.height), pixels, lib, runtimeAssets);
1645
1793
  const audio = new AudioWindowSupplier(lib);
@@ -1647,6 +1795,8 @@ export function createBrowserBridge(options) {
1647
1795
  signal.throwIfAborted();
1648
1796
  await renderer.start();
1649
1797
  signal.throwIfAborted();
1798
+ await renderer.registerEffects(effects, "export");
1799
+ signal.throwIfAborted();
1650
1800
  renderer.loadProject(project, "export");
1651
1801
  const refused = renderer.setCaptionFonts(fontStack);
1652
1802
  // Heard once per job, not once per attempt.
@@ -1662,7 +1812,6 @@ export function createBrowserBridge(options) {
1662
1812
  if (range.end <= range.start)
1663
1813
  throw new EditorError("empty_export", "Project has no exportable range", "export");
1664
1814
  const settings = project.settings;
1665
- const name = `${project.name}.mp4`;
1666
1815
  const { written, output, result } = await exportTimeline({
1667
1816
  engine: renderer.wasm,
1668
1817
  project,
@@ -1679,9 +1828,10 @@ export function createBrowserBridge(options) {
1679
1828
  audioSupplier: audio,
1680
1829
  range: { start: range.start / 120000, end: range.end / 120000 },
1681
1830
  options: attemptOptions(options, attempt, project.settings),
1682
- openOutput: (estimate) => openExportOutput({
1831
+ openOutput: ({ container, ...estimate }) => openExportOutput({
1683
1832
  jobId: attempt.id.replace(/[^\w-]/g, "_"),
1684
- name,
1833
+ name: `${project.name}.${containerPolicy(container).extensions[0]}`,
1834
+ container,
1685
1835
  target: options.output,
1686
1836
  ...estimate,
1687
1837
  }),
@@ -1710,7 +1860,11 @@ export function createBrowserBridge(options) {
1710
1860
  return {
1711
1861
  ...artifact,
1712
1862
  media: Object.fromEntries(selection),
1713
- report: { ...result, decodeFallbacks: pixels.decodeFallbacks },
1863
+ report: {
1864
+ ...result,
1865
+ videoDecode,
1866
+ decodeFallbacks: pixels.decodeFallbacks,
1867
+ },
1714
1868
  };
1715
1869
  }
1716
1870
  finally {
@@ -2161,11 +2315,33 @@ export function createBrowserBridge(options) {
2161
2315
  return monitorVolume;
2162
2316
  },
2163
2317
  setTraceUploads(value) {
2164
- if (engine)
2165
- engine.traceUploads = value;
2318
+ traceUploads = value === true;
2319
+ applyTracing();
2166
2320
  },
2321
+ setPassTiming(enabled) {
2322
+ passTiming = enabled === true;
2323
+ applyTracing();
2324
+ },
2325
+ get previewScale() {
2326
+ return previewScale;
2327
+ },
2328
+ setPreviewScale,
2167
2329
  get diagnostics() {
2168
- return engine?.lastDiagnostics ?? null;
2330
+ // The renderer that draws: in worker mode the main-thread one does not.
2331
+ const snapshot = workerPreview
2332
+ ? workerDiagnostics
2333
+ : (engine?.lastDiagnostics ?? null);
2334
+ if (!snapshot)
2335
+ return null;
2336
+ return {
2337
+ ...snapshot,
2338
+ host: {
2339
+ ...snapshot.host,
2340
+ skippedRequests,
2341
+ bitmapTransfers,
2342
+ previewScale,
2343
+ },
2344
+ };
2169
2345
  },
2170
2346
  get tick() {
2171
2347
  return tick;
@@ -2196,6 +2372,7 @@ export function createBrowserBridge(options) {
2196
2372
  get catalog() {
2197
2373
  return engine?.catalog ?? null;
2198
2374
  },
2375
+ registerEffects,
2199
2376
  // A source's own media while it is online, whichever preview reads: its size, file and peaks
2200
2377
  // are the original's.
2201
2378
  getAsset: (id) => (preferredPreviewMedia === "proxies" &&
@@ -2203,6 +2380,15 @@ export function createBrowserBridge(options) {
2203
2380
  library.get(id),
2204
2381
  };
2205
2382
  }
2383
+ /**
2384
+ * `scale` as a preview resolution scale: finite, above 0 and at most 8, as the engine's quality
2385
+ * profile takes it.
2386
+ */
2387
+ function checkedPreviewScale(scale) {
2388
+ if (typeof scale !== "number" || !(scale > 0 && scale <= 8))
2389
+ throw new EditorError("invalid_preview_scale", "The preview scale must be a number above 0 and at most 8", "preview");
2390
+ return scale;
2391
+ }
2206
2392
  /**
2207
2393
  * Whether the engine draws a source itself, so no file backs it: a generated source or a
2208
2394
  * composition.
@@ -3,13 +3,17 @@
3
3
  * render queue that collapses to the newest request.
4
4
  */
5
5
  export class Engine {
6
- constructor(canvas: any, sources: any, library: any, runtimeAssets?: {});
6
+ constructor(canvas: any, sources: any, library: any, runtimeAssets?: {}, { unreadableFrames }?: {});
7
7
  runtimeAssets: {};
8
+ /** What the frame supply does with a frame the browser cannot read back (`FrameSupply`). */
9
+ unreadableFrames: any;
8
10
  canvas: any;
9
11
  sources: any;
10
12
  library: any;
11
13
  kind: string;
12
14
  traceUploads: boolean;
15
+ /** Which preview draws with this renderer, as its diagnostics report it. */
16
+ backend: string;
13
17
  lastDiagnostics: any;
14
18
  lastRenderMs: number;
15
19
  onDiagnostics: any;
@@ -19,6 +23,19 @@ export class Engine {
19
23
  effectCatalog: any;
20
24
  }>;
21
25
  catalog: any;
26
+ /**
27
+ * Turns per-pass GPU and CPU timing on or off; the passes then appear in `lastDiagnostics`.
28
+ * Off by default: timing is profiling cost, not rendering work.
29
+ */
30
+ setPassTiming(enabled: any): void;
31
+ /**
32
+ * Sets the preview resolution scale (the engine's `QualityProfile` resolution scale; 1 renders
33
+ * at the document's size) and returns the loaded project's render size as `[width, height]`, or
34
+ * null with no project loaded. Throws for a scale the engine refuses.
35
+ */
36
+ setPreviewScale(scale: any): any[] | null;
37
+ /** The loaded project's render size at the preview scale, `[width, height]`, or null. */
38
+ get previewSize(): any[] | null;
22
39
  /** The renderer's part of the capability report: GPU, color, and playback. */
23
40
  capabilityReport(): any;
24
41
  /** A clock for one run of playback; see the engine's `PlaybackPolicy`. */
@@ -27,7 +44,10 @@ export class Engine {
27
44
  audible(policy: any, rate: any): boolean;
28
45
  get ready(): boolean;
29
46
  get deviceLost(): any;
30
- /** Raw source video the frame supply copied through script memory, cumulative. */
47
+ /**
48
+ * Raw source video the frame supply copied through script memory, and the frames it could not
49
+ * read back and how it recovered them; cumulative.
50
+ */
31
51
  get sourceTransfer(): any;
32
52
  /** The wasm object, for callers that drive audio directly. */
33
53
  get wasm(): null;
@@ -46,6 +66,15 @@ export class Engine {
46
66
  * failure stay known, so the next load neither leaks nor repeats them.
47
67
  */
48
68
  loadProject(project: any, operation?: string): void;
69
+ /**
70
+ * Compiles runtime effect definitions (`alpha2/effects` `EffectDefinition`s) on this renderer's
71
+ * device and registers them, all or none. An `id@version` this renderer holds already is skipped:
72
+ * a definition never changes under its version.
73
+ *
74
+ * Throws `invalid_effect` naming the first definition the engine refuses, with its diagnostics;
75
+ * then none of the call's definitions is registered.
76
+ */
77
+ registerEffects(definitions: any, operation?: string): Promise<void>;
49
78
  /**
50
79
  * Registers the fonts caption tracks draw with, in fallback order. An SVG's text draws with the
51
80
  * same fonts, as the native host draws it with its caption fonts. Returns the fonts the engine
@@ -77,7 +106,9 @@ export class Engine {
77
106
  get generation(): number;
78
107
  /**
79
108
  * Requests one frame. Concurrent requests collapse to the most recent one so that scrubbing never
80
- * queues a backlog of stale frames.
109
+ * queues a backlog of stale frames. Resolves `{ rendered, unchanged }`: `rendered` is false when
110
+ * the request was superseded, and `unchanged` says the frame equals the one already shown, so
111
+ * nothing was drawn or presented and the surface still shows it.
81
112
  */
82
113
  render(seconds: any, { generation, context }?: {
83
114
  generation?: null | undefined;