Skip to content
TypeScript

perf(desktop): picture-in-picture stops re-encoding an unchanged page (#20070)

Julius Marminge3962ae858633
diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts
index 6d585a88641..e6e4329d29c 100644
--- a/apps/desktop/src/preview/Manager.test.ts
+++ b/apps/desktop/src/preview/Manager.test.ts
@@ -329,6 +329,8 @@ const withManager = <A>(
interface TestCapturedPreviewImage {
readonly toJPEG: () => Buffer;
+ /** Only fixtures that deliver picture-in-picture frames need pixels to fingerprint. */
+ readonly toBitmap?: () => Buffer;
readonly getSize: () => { readonly width: number; readonly height: number };
}
@@ -3454,6 +3456,7 @@ describe("PreviewManager", () => {
const jpeg = Buffer.from("shared-preview-frame");
const capturePage = vi.fn(async () => ({
toJPEG: () => jpeg,
+ toBitmap: () => jpeg,
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue({
@@ -3612,6 +3615,7 @@ describe("PreviewManager", () => {
const jpeg = Buffer.from("shared-preview-frame");
const capturePage = vi.fn(async () => ({
toJPEG: () => jpeg,
+ toBitmap: () => jpeg,
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
@@ -3687,6 +3691,7 @@ describe("PreviewManager", () => {
const jpeg = Buffer.from("retry-preview-frame");
const capturePage = vi.fn(async () => ({
toJPEG: () => jpeg,
+ toBitmap: () => jpeg,
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
@@ -3718,6 +3723,7 @@ describe("PreviewManager", () => {
const jpeg = Buffer.from("reloaded-preview-frame");
const capturePage = vi.fn(async () => ({
toJPEG: () => jpeg,
+ toBitmap: () => jpeg,
getSize: () => ({ width: 1280, height: 720 }),
}));
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
@@ -3741,11 +3747,62 @@ describe("PreviewManager", () => {
),
);
+ effectIt.effect("encodes a picture-in-picture frame only when the page changes", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ let page = "static-page";
+ let pixelsReadable = true;
+ const toJPEG = vi.fn((pixels: string) => Buffer.from(`jpeg:${pixels}`));
+ const capturePage = vi.fn(async () => {
+ const pixels = page;
+ return {
+ toJPEG: () => toJPEG(pixels),
+ toBitmap: () => (pixelsReadable ? Buffer.from(pixels) : Buffer.alloc(0)),
+ getSize: () => ({ width: 1280, height: 720 }),
+ };
+ });
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+ const { pictureInPictureWindow, send } = makeTestPictureInPictureWindow();
+ browserWindowConstructor.mockImplementation(function () {
+ return pictureInPictureWindow;
+ });
+
+ yield* manager.createTab("tab_pip_unchanged");
+ yield* manager.registerWebview("tab_pip_unchanged", 42);
+ yield* manager.openPictureInPicture("tab_pip_unchanged");
+ yield* TestClock.adjust(100);
+
+ expect(capturePage).toHaveBeenCalledTimes(2);
+ expect(toJPEG).toHaveBeenCalledOnce();
+ expect(send).toHaveBeenCalledOnce();
+
+ page = "changed-page";
+ yield* TestClock.adjust(100);
+
+ expect(toJPEG).toHaveBeenCalledTimes(2);
+ expect(send).toHaveBeenCalledTimes(2);
+ expect(send).toHaveBeenLastCalledWith(
+ "desktop:preview-pip-frame",
+ expect.objectContaining({ data: Buffer.from("jpeg:changed-page").toString("base64") }),
+ );
+
+ // A frame whose pixels cannot be read has no fingerprint and is still delivered.
+ pixelsReadable = false;
+ yield* TestClock.adjust(100);
+
+ expect(toJPEG).toHaveBeenCalledTimes(3);
+ expect(send).toHaveBeenCalledTimes(3);
+ yield* manager.closePictureInPicture("tab_pip_unchanged");
+ }),
+ ),
+ );
+
effectIt.effect("drops empty frames before picture-in-picture delivery", () =>
withManager((manager) =>
Effect.gen(function* () {
const validImage: TestCapturedPreviewImage = {
toJPEG: () => Buffer.from("valid-preview-frame"),
+ toBitmap: () => Buffer.from("valid-preview-frame"),
getSize: () => ({ width: 1280, height: 720 }),
};
const capturePage = vi.fn(async () => validImage);
diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts
index 2d26ce591a9..a12ae3390e8 100644
--- a/apps/desktop/src/preview/Manager.ts
+++ b/apps/desktop/src/preview/Manager.ts
@@ -5,6 +5,7 @@
* elements live in the renderer; we only attach listeners and forward state
* here). Single layer-scoped browser session partition.
*/
+import * as NodeZlib from "node:zlib";
import {
DesktopPreviewRecordingInputSchema,
DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER,
@@ -318,6 +319,33 @@ const cropAnnotationScreenshot = (
return { dataUrl: cropped.toDataURL(), width: size.width, height: size.height, cropRect };
};
+/**
+ * Electron converts `toBitmap()` output to sRGB by default. Captures on P3 Macs are
+ * P3, so that conversion costs more per tick than the JPEG encode the fingerprint
+ * replaces. An invalid target color space has no Skia color space, so `readPixels`
+ * copies the pixels without a color transform. The fingerprint only compares frames
+ * with each other, so color accuracy is irrelevant.
+ */
+const FINGERPRINT_BITMAP_OPTIONS: Electron.ToBitmapOptions = {
+ colorSpace: { primaries: "invalid", transfer: "invalid", matrix: "invalid", range: "invalid" },
+};
+
+/**
+ * Identity of a captured frame: cheap enough to compute every tick, unlike a JPEG
+ * encode. Null when the pixels could not be read (Electron reports a failed
+ * `readPixels` as an empty buffer), so the caller treats the frame as changed.
+ */
+const fingerprintCapturedFrame = (image: Electron.NativeImage, size: Electron.Size) => {
+ let bitmap: Buffer;
+ try {
+ bitmap = image.toBitmap(FINGERPRINT_BITMAP_OPTIONS);
+ } catch {
+ return null;
+ }
+ if (bitmap.byteLength === 0) return null;
+ return `${size.width}x${size.height}:${NodeZlib.crc32(bitmap)}`;
+};
+
const findZoomStep = (current: number): number => {
const index = ZOOM_LEVELS.findIndex(
(level) => Math.abs(level - current) < ZOOM_EPSILON || level > current,
@@ -370,7 +398,7 @@ interface FrameCaptureSession {
readonly scope: Scope.Closeable | null;
readonly consumers: ReadonlySet<FrameCaptureConsumer>;
readonly unthrottledWebContentsIds: ReadonlySet<number>;
- readonly lastPictureInPictureFrame: Buffer | null;
+ readonly lastPictureInPictureFingerprint: string | null;
}
interface PictureInPictureSession {
@@ -793,8 +821,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
...current,
scope: consumer === "picture-in-picture" ? null : current.scope,
consumers,
- lastPictureInPictureFrame:
- consumer === "picture-in-picture" ? null : current.lastPictureInPictureFrame,
+ lastPictureInPictureFingerprint:
+ consumer === "picture-in-picture"
+ ? null
+ : current.lastPictureInPictureFingerprint,
});
}),
] as const;
@@ -2584,6 +2614,16 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
) {
return;
}
+ const frameSession = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId);
+ if (frameSession?.scope !== captureSession.scope) return;
+ // Fingerprint the raw pixels before encoding so an unchanged page skips the
+ // synchronous JPEG encode, the base64 copy and the IPC send. An unreadable
+ // frame has no fingerprint and is always delivered.
+ const fingerprint = fingerprintCapturedFrame(image, size);
+ const pictureInPicture =
+ frameSession.consumers.has("picture-in-picture") &&
+ (fingerprint === null || frameSession.lastPictureInPictureFingerprint !== fingerprint);
+ if (!pictureInPicture) return;
const encoded = yield* attempt(
{
operation: "frameCapture.encodeFrame",
@@ -2592,12 +2632,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
},
() => image.toJPEG(PICTURE_IN_PICTURE_JPEG_QUALITY),
);
- const frameSession = (yield* SynchronizedRef.get(frameCaptureSessionsRef)).get(tabId);
- if (frameSession?.scope !== captureSession.scope) return;
- const pictureInPicture =
- frameSession.consumers.has("picture-in-picture") &&
- frameSession.lastPictureInPictureFrame?.equals(encoded) !== true;
- if (!pictureInPicture) return;
const receivedAt = yield* currentIso;
const frame: DesktopPreviewRecordingFrame = {
tabId,
@@ -2662,7 +2696,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
return replaceMap(sessions, (copy) => {
copy.set(tabId, {
...frameSession,
- lastPictureInPictureFrame: encoded,
+ lastPictureInPictureFingerprint: fingerprint,
});
});
});
@@ -2754,7 +2788,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
scope,
consumers: new Set([consumer]),
unthrottledWebContentsIds: new Set([wc.id]),
- lastPictureInPictureFrame: null,
+ lastPictureInPictureFingerprint: null,
});
}),
] as const;
@@ -2920,7 +2954,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
const current = sessions.get(tabId);
if (!current?.consumers.has("picture-in-picture")) return sessions;
return replaceMap(sessions, (copy) => {
- copy.set(tabId, { ...current, lastPictureInPictureFrame: null });
+ copy.set(tabId, { ...current, lastPictureInPictureFingerprint: null });
});
}),
);