mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(engine): fast capture works on Chrome 157+, which wants content=drawable (#7626)
This commit is contained in:
@@ -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 = '<div data-composition-id="film" style="width:64px;height:32px"></div>';
|
||||
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<void> {
|
||||
await page.evaluate(() => {
|
||||
delete (HTMLCanvasElement.prototype as unknown as Record<string, unknown>).layoutSubtree;
|
||||
(HTMLCanvasElement.prototype as unknown as Record<string, unknown>).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<string> {
|
||||
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();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -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<boolean> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject a `<canvas layoutsubtree>` 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<void> {
|
||||
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 },
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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 = `<!DOCTYPE html>
|
||||
<html><body>
|
||||
<div data-composition-id="root" data-width="1920" data-height="1080">
|
||||
<canvas id="glass-canvas" content="drawable" width="1920" height="1080">
|
||||
<div class="panel" drawable>Glass content</div>
|
||||
</canvas>
|
||||
</div>
|
||||
</body></html>`;
|
||||
|
||||
expect(detectRenderModeHints(html).reasons.map((reason) => reason.code)).toContain(
|
||||
"htmlInCanvas",
|
||||
);
|
||||
});
|
||||
|
||||
it("does not flag htmlInCanvas for plain canvas elements without layoutsubtree", () => {
|
||||
const html = `<!DOCTYPE html>
|
||||
<html><body>
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user