From 9c3258ccbf8f88e0f6656cd358e95f926a09160c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Thu, 12 Nov 2026 23:01:15 +0000 Subject: [PATCH] fix(engine): fast capture works on Chrome 157+, which wants content=drawable (#7626) --- .../drawElementCanvasForm.browser.test.ts | 77 +++++++++++++++++++ .../engine/src/services/drawElementService.ts | 18 +++-- .../src/services/htmlCompiler.test.ts | 15 ++++ .../producer/src/services/htmlCompiler.ts | 3 +- 4 files changed, 105 insertions(+), 8 deletions(-) create mode 100644 packages/engine/src/services/drawElementCanvasForm.browser.test.ts diff --git a/packages/engine/src/services/drawElementCanvasForm.browser.test.ts b/packages/engine/src/services/drawElementCanvasForm.browser.test.ts new file mode 100644 index 0000000000..5492b21f11 --- /dev/null +++ b/packages/engine/src/services/drawElementCanvasForm.browser.test.ts @@ -0,0 +1,77 @@ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import puppeteer, { type Browser, type Page } from "puppeteer-core"; +import { resolveHeadlessShellPath } from "./browserManager.js"; +import { closeTestChrome } from "./__fixtures__/loopback.js"; +import { injectDrawElementCanvas } from "./drawElementService.js"; + +const chromePath = process.env.HF_DRAWABLE_TEST_CHROME ?? resolveHeadlessShellPath(); +const COMPOSITION = '
'; +const ATTRIBUTE_ERROR = /requires the canvas to have|must have the 'drawable' attribute/; + +/** Makes drawElementImage check only the 157 marks, throwing as Chrome 157 does without them. */ +async function actLikeChrome157(page: Page): Promise { + await page.evaluate(() => { + delete (HTMLCanvasElement.prototype as unknown as Record).layoutSubtree; + (HTMLCanvasElement.prototype as unknown as Record).updateElementGeometry = + () => {}; + CanvasRenderingContext2D.prototype.drawElementImage = function (el: Element) { + if (this.canvas.getAttribute("content") !== "drawable") { + throw new DOMException( + "DrawElementImage requires the canvas to have the content=drawable attribute", + "InvalidStateError", + ); + } + if (!el.hasAttribute("drawable")) { + throw new DOMException( + "Descendants passed to DrawElementImage must have the 'drawable' attribute", + "InvalidStateError", + ); + } + }; + }); +} + +async function drawInjectedRoot(page: Page): Promise { + return page.evaluate(() => { + const canvas = document.getElementById("__hf_de_canvas") as HTMLCanvasElement; + const root = document.querySelector("[data-composition-id]") as Element; + try { + canvas.getContext("2d")!.drawElementImage(root, 0, 0); + return ""; + } catch (err) { + return String(err); + } + }); +} + +describe.skipIf(!chromePath)("drawElement capture canvas form", () => { + let browser: Browser; + beforeAll(async () => { + browser = await puppeteer.launch({ + executablePath: chromePath, + headless: true, + args: ["--no-sandbox", "--disable-dev-shm-usage", "--enable-features=CanvasDrawElement"], + }); + }); + afterAll(() => closeTestChrome(browser)); + + it.each([ + ["this Chrome", false], + ["a Chrome that wants content=drawable (157+)", true], + ])("is accepted by drawElementImage on %s", async (_name, like157) => { + const page = await browser.newPage(); + try { + await page.setContent(COMPOSITION); + // A Chrome without the API would "pass" on a TypeError, so the API must be there. + expect( + await page.evaluate(() => "drawElementImage" in CanvasRenderingContext2D.prototype), + ).toBe(true); + if (like157) await actLikeChrome157(page); + await injectDrawElementCanvas(page, 64, 32); + // "No cached paint record" is fine here: the marks were accepted, nothing has painted yet. + expect(await drawInjectedRoot(page)).not.toMatch(ATTRIBUTE_ERROR); + } finally { + await page.close(); + } + }); +}); diff --git a/packages/engine/src/services/drawElementService.ts b/packages/engine/src/services/drawElementService.ts index 25a948d52d..4d82564f03 100644 --- a/packages/engine/src/services/drawElementService.ts +++ b/packages/engine/src/services/drawElementService.ts @@ -15,6 +15,7 @@ */ import type { Page } from "puppeteer-core"; +import { htmlInCanvasForm, type HtmlInCanvasForm } from "@hyperframes/core/html-in-canvas"; import { SOFTWARE_WEBGL_RENDERER_TOKENS } from "./browserManager.js"; import type { CaptureMode } from "./browserLeasePool.js"; @@ -269,7 +270,7 @@ export async function detectSwiftShader(page: Page): Promise { } /** - * Inject a `` around the composition root. + * Inject a capture canvas (`layoutsubtree`, or `content="drawable"` on Chrome 157+) around the composition root. * * The canvas must wrap `[data-composition-id]` for drawElementImage to read * its paint records. Idempotent — skips injection if `__hf_de_canvas` exists. @@ -280,8 +281,9 @@ export async function injectDrawElementCanvas( width: number, height: number, ): Promise { + const form = (await page.evaluate(htmlInCanvasForm)) ?? "layoutsubtree"; await page.evaluate( - ({ w, h }: { w: number; h: number }) => { + ({ w, h, form }: { w: number; h: number; form: HtmlInCanvasForm }) => { const root = document.querySelector("[data-composition-id]") as HTMLElement | null; if (!root || document.getElementById("__hf_de_canvas")) return; // Record the root's base opacity (timeline at 0, before any entrance/ @@ -297,11 +299,13 @@ export async function injectDrawElementCanvas( } const parent = root.parentNode; if (!parent) throw new Error("drawElement: composition root has no parent node"); - const canvas = document.createElement("canvas") as HTMLCanvasElement & { - layoutsubtree: boolean; - }; + const canvas = document.createElement("canvas"); canvas.id = "__hf_de_canvas"; - canvas.setAttribute("layoutsubtree", ""); + if (form === "layoutsubtree") canvas.setAttribute("layoutsubtree", ""); + else { + canvas.setAttribute("content", "drawable"); + root.setAttribute("drawable", ""); + } canvas.width = w; canvas.height = h; canvas.style.cssText = "display:block;position:absolute;top:0;left:0;z-index:0"; @@ -349,7 +353,7 @@ export async function injectDrawElementCanvas( return false; }; }, - { w: width, h: height }, + { w: width, h: height, form }, ); } diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index 0aeba12981..dc14527659 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -791,6 +791,21 @@ describe("detectRenderModeHints", () => { expect(detectRenderModeHints(html, "off").reasons).toEqual([]); }); + it("detects html-in-canvas API via the content=drawable canvas of Chrome 157+", () => { + const html = ` + +
+ +
Glass content
+
+
+`; + + expect(detectRenderModeHints(html).reasons.map((reason) => reason.code)).toContain( + "htmlInCanvas", + ); + }); + it("does not flag htmlInCanvas for plain canvas elements without layoutsubtree", () => { const html = ` diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 7aff7406b5..8d611d0e81 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -38,6 +38,7 @@ import { type UnresolvedElement, } from "@hyperframes/core"; import { MAX_AUDIO_GAIN } from "@hyperframes/core/audio-gain"; +import { CAPTURE_CANVAS_SELECTOR } from "@hyperframes/core/html-in-canvas"; import { readPlaybackRate } from "@hyperframes/parsers/media-duration"; import { resolveRateSpec } from "@hyperframes/core/speed-ramp"; import { gsapCdnDist, normalizeGsapBaseUrl } from "@hyperframes/core/gsap-cdn"; @@ -379,7 +380,7 @@ export function detectRenderModeHints(html: string, pageMotionBlur?: "off"): Ren const reasons: RenderModeHint[] = []; const { document } = parseHTML(html); - if (document.querySelector("canvas[layoutsubtree]")) { + if (document.querySelector(CAPTURE_CANVAS_SELECTOR)) { reasons.push({ code: "htmlInCanvas", message: