From e238ab7fa9bcf8f274027c209e06f0630bc112fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 23 Nov 2026 10:41:23 +0000 Subject: [PATCH] fix(studio): full-size composition stills count their real memory, so the cache lets them go (#8576) --- .../components/CompositionThumbnail.test.ts | 34 ++++++++++++++++++- .../components/CompositionThumbnail.tsx | 13 +++---- .../src/player/components/thumbnailUtils.ts | 10 +++--- .../src/player/lib/thumbnailImageDecoder.ts | 7 ++-- .../src/player/lib/thumbnailScheduler.ts | 2 ++ .../src/player/lib/thumbnailVideoDecoder.ts | 3 +- 6 files changed, 52 insertions(+), 17 deletions(-) diff --git a/packages/studio/src/player/components/CompositionThumbnail.test.ts b/packages/studio/src/player/components/CompositionThumbnail.test.ts index 2ca8f10c1b..ea87277a60 100644 --- a/packages/studio/src/player/components/CompositionThumbnail.test.ts +++ b/packages/studio/src/player/components/CompositionThumbnail.test.ts @@ -5,7 +5,12 @@ import { createRoot, type Root } from "react-dom/client"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { MockResizeObserver, reportResize } from "../../hooks/resizeObserverTestUtils"; import { thumbnailScheduler } from "../lib/thumbnailScheduler"; -import { buildCompositionThumbnailUrl, CompositionThumbnail } from "./CompositionThumbnail"; +import { TIMELINE_VIEWPORT_BUDGETS } from "../lib/timelineViewportBudgets"; +import { + buildCompositionThumbnailUrl, + CompositionThumbnail, + compositionThumbnailRequest, +} from "./CompositionThumbnail"; Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", { configurable: true, @@ -255,3 +260,30 @@ describe("CompositionThumbnail", () => { expect(MockImage.instances[0]?.src).toBe("blob:composition-thumbnail"); }); }); + +describe("compositionThumbnailRequest", () => { + // fails without the fix: each full-size still was weighed as a 240x135 poster, so 96 of them (8.3 MB each decoded) + // stayed cached before the 64 MiB byte budget ever counted them full. + it("keeps no more full-size stills than the byte budget holds once their cards are gone", async () => { + let made = 0; + URL.createObjectURL = vi.fn(() => `blob:still-${++made}`); + const project = "/api/projects/demo/preview"; + for (let write = 1; write <= 40; write++) { + const request = compositionThumbnailRequest( + `/api/projects/demo/thumbnail/index.html?v=${write}`, + project, + ); + const lease = thumbnailScheduler.acquire(request, () => {}); + await vi.waitFor(() => expect(MockImage.instances.length).toBe(write)); + const probe = MockImage.instances[write - 1]!; + probe.naturalWidth = 1920; + probe.naturalHeight = 1080; + probe.onload?.(); + await vi.waitFor(() => expect(thumbnailScheduler.getSnapshot(request).status).toBe("ready")); + lease.release(); + } + const kept = made - vi.mocked(URL.revokeObjectURL).mock.calls.length; + const fit = Math.floor(TIMELINE_VIEWPORT_BUDGETS.thumbnailCacheBytes / (1920 * 1080 * 4)); + expect(kept).toBeLessThanOrEqual(fit); + }); +}); diff --git a/packages/studio/src/player/components/CompositionThumbnail.tsx b/packages/studio/src/player/components/CompositionThumbnail.tsx index fe7844a009..dd58f14deb 100644 --- a/packages/studio/src/player/components/CompositionThumbnail.tsx +++ b/packages/studio/src/player/components/CompositionThumbnail.tsx @@ -3,12 +3,12 @@ import { useThumbnailLease } from "../../hooks/useThumbnailLease"; import { useThumbnailStripSize } from "../../hooks/useThumbnailStripSize"; import { createThumbnailKey, + decodedImageBytes, type ThumbnailPriority, type ThumbnailRequest, } from "../lib/thumbnailScheduler"; -import { TIMELINE_VIEWPORT_BUDGETS } from "../lib/timelineViewportBudgets"; import { ThumbnailTiles } from "./ThumbnailTiles"; -import { computeThumbnailStrip, probeImageAspect } from "./thumbnailUtils"; +import { computeThumbnailStrip, probeImageSize } from "./thumbnailUtils"; import { studioApiFetch } from "../../utils/studioApiFetch"; interface CompositionThumbnailProps { @@ -114,13 +114,10 @@ async function loadCompositionImage(url: string, signal: AbortSignal) { if (signal.aborted) throw new DOMException("Aborted", "AbortError"); const objectUrl = URL.createObjectURL(blob); try { - const aspect = await probeImageAspect(objectUrl, signal); + const { width, height } = await probeImageSize(objectUrl, signal); return { - value: { kind: "image" as const, url: objectUrl, aspect }, - weight: - TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalWidth * - TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalHeight * - 4, + value: { kind: "image" as const, url: objectUrl, aspect: width / height }, + weight: decodedImageBytes(width, height), dispose: () => URL.revokeObjectURL(objectUrl), }; } catch (error) { diff --git a/packages/studio/src/player/components/thumbnailUtils.ts b/packages/studio/src/player/components/thumbnailUtils.ts index bde62d4160..253c4d0d21 100644 --- a/packages/studio/src/player/components/thumbnailUtils.ts +++ b/packages/studio/src/player/components/thumbnailUtils.ts @@ -36,11 +36,11 @@ export function thumbnailFrameForTile( * abort signal, so an unmounted clip cannot leave Blink retaining a pending * image request and its former React tree. */ -export function probeImageAspect( +export function probeImageSize( imageSrc: string, signal: AbortSignal, tolerateSvgError = false, -): Promise { +): Promise<{ width: number; height: number }> { return new Promise((resolve, reject) => { const image = new Image(); const cleanup = () => { @@ -63,13 +63,13 @@ export function probeImageAspect( cleanup(); resolve( image.naturalWidth > 0 && image.naturalHeight > 0 - ? image.naturalWidth / image.naturalHeight - : 16 / 9, + ? { width: image.naturalWidth, height: image.naturalHeight } + : { width: 1920, height: 1080 }, ); }; image.onerror = () => { cleanup(); - if (tolerateSvgError && /\.svg($|\?)/i.test(imageSrc)) resolve(16 / 9); + if (tolerateSvgError && /\.svg($|\?)/i.test(imageSrc)) resolve({ width: 1920, height: 1080 }); else reject(new Error("Image thumbnail failed to load")); }; signal.addEventListener("abort", onAbort, { once: true }); diff --git a/packages/studio/src/player/lib/thumbnailImageDecoder.ts b/packages/studio/src/player/lib/thumbnailImageDecoder.ts index f3b6775e7b..6d2b1af619 100644 --- a/packages/studio/src/player/lib/thumbnailImageDecoder.ts +++ b/packages/studio/src/player/lib/thumbnailImageDecoder.ts @@ -1,4 +1,4 @@ -import type { ThumbnailLoadedResult } from "./thumbnailScheduler"; +import { decodedImageBytes, type ThumbnailLoadedResult } from "./thumbnailScheduler"; import { TIMELINE_VIEWPORT_BUDGETS } from "./timelineViewportBudgets"; function thumbnailSource(source: string): string { @@ -66,7 +66,10 @@ export async function decodeImageThumbnail( signal.throwIfAborted(); const width = image.naturalWidth || TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalWidth; const height = image.naturalHeight || TIMELINE_VIEWPORT_BUDGETS.posterMaxPhysicalHeight; - return { value: { kind: "image", url, aspect: width / height }, weight: width * height * 4 }; + return { + value: { kind: "image", url, aspect: width / height }, + weight: decodedImageBytes(width, height), + }; } finally { image.src = ""; } diff --git a/packages/studio/src/player/lib/thumbnailScheduler.ts b/packages/studio/src/player/lib/thumbnailScheduler.ts index e8a370ef38..6b1eb060f4 100644 --- a/packages/studio/src/player/lib/thumbnailScheduler.ts +++ b/packages/studio/src/player/lib/thumbnailScheduler.ts @@ -8,6 +8,8 @@ export type ThumbnailValue = | { kind: "filmstrip"; urls: readonly string[]; aspect: number } | { kind: "waveform"; peaks: readonly number[] }; +export const decodedImageBytes = (width: number, height: number) => width * height * 4; + export interface ThumbnailLoadedResult { value: ThumbnailValue; /** Estimated decoded/object-URL bytes retained by this result. */ diff --git a/packages/studio/src/player/lib/thumbnailVideoDecoder.ts b/packages/studio/src/player/lib/thumbnailVideoDecoder.ts index e1dc928384..db4712572b 100644 --- a/packages/studio/src/player/lib/thumbnailVideoDecoder.ts +++ b/packages/studio/src/player/lib/thumbnailVideoDecoder.ts @@ -1,6 +1,7 @@ import { TIMELINE_VIEWPORT_BUDGETS, type TimelineViewportBudgets } from "./timelineViewportBudgets"; import { createThumbnailKey, + decodedImageBytes, type ThumbnailLoadedResult, type ThumbnailValue, } from "./thumbnailScheduler"; @@ -236,7 +237,7 @@ function loadedResult( : { kind: "filmstrip", urls, aspect }; return { value, - weight: width * height * 4 * decoded.length, + weight: decodedImageBytes(width, height) * decoded.length, dispose: () => releaseDecodedResources(resources), }; }