|
5 | 5 | HF_COLOR_GRADING_CANVAS_ID_PREFIX, |
6 | 6 | HF_COLOR_GRADING_DETAIL_KEYS, |
7 | 7 | HF_COLOR_GRADING_EFFECT_KEYS, |
| 8 | + HF_COLOR_GRADING_SOURCE_TAGS, |
8 | 9 | hasHfColorGradingHueCurveValues, |
9 | 10 | hasHfColorGradingRgbCurveValues, |
10 | 11 | hasHfColorGradingSecondaryValues, |
@@ -42,9 +43,15 @@ import { copyMediaVisualStyles } from "../inline-scripts/parityContract"; |
42 | 43 | import { readVariablesForElement } from "./variableScope"; |
43 | 44 | import { swallow } from "./diagnostics"; |
44 | 45 | import { findInjectedRenderFrame } from "./renderFrameSibling"; |
45 | | -import { isElementNode, isHtmlElement, isImageElement, isVideoElement } from "./domRealm"; |
| 46 | +import { |
| 47 | + isCanvasElement, |
| 48 | + isElementNode, |
| 49 | + isHtmlElement, |
| 50 | + isImageElement, |
| 51 | + isVideoElement, |
| 52 | +} from "./domRealm"; |
46 | 53 |
|
47 | | -type ColorGradingMediaElement = HTMLVideoElement | HTMLImageElement; |
| 54 | +type ColorGradingMediaElement = HTMLVideoElement | HTMLImageElement | HTMLCanvasElement; |
48 | 55 |
|
49 | 56 | type EntrySource = "attribute" | "live"; |
50 | 57 |
|
@@ -1462,8 +1469,15 @@ const KUWAHARA_RESOLVE_FRAGMENT_SHADER = [ |
1462 | 1469 | "}", |
1463 | 1470 | ].join("\n"); |
1464 | 1471 |
|
| 1472 | +const COLOR_GRADING_SOURCE_SELECTOR = HF_COLOR_GRADING_SOURCE_TAGS.map( |
| 1473 | + (tag) => `${tag}[${HF_COLOR_GRADING_ATTR}]`, |
| 1474 | +).join(", "); |
| 1475 | + |
1465 | 1476 | function isColorGradingMediaElement(value: Element): value is ColorGradingMediaElement { |
1466 | | - return isVideoElement(value) || isImageElement(value); |
| 1477 | + return ( |
| 1478 | + isHtmlElement(value) && |
| 1479 | + (HF_COLOR_GRADING_SOURCE_TAGS as readonly string[]).includes(value.localName) |
| 1480 | + ); |
1467 | 1481 | } |
1468 | 1482 |
|
1469 | 1483 | function isVisibleForColorGrading(element: ColorGradingMediaElement): boolean { |
@@ -1781,7 +1795,7 @@ function hashStringSeed(value: string): number { |
1781 | 1795 | function seedForElement(element: ColorGradingMediaElement): number { |
1782 | 1796 | const key = |
1783 | 1797 | element.id || |
1784 | | - element.currentSrc || |
| 1798 | + (!isCanvasElement(element) && element.currentSrc) || |
1785 | 1799 | element.getAttribute("src") || |
1786 | 1800 | `${element.tagName}:${Array.prototype.indexOf.call(element.parentNode?.children ?? [], element)}`; |
1787 | 1801 | return hashStringSeed(key); |
@@ -2462,31 +2476,22 @@ function resolveTarget( |
2462 | 2476 | } |
2463 | 2477 |
|
2464 | 2478 | function readSourceSize(source: TexImageSource): { width: number; height: number } | null { |
2465 | | - if (isVideoElement(source)) { |
2466 | | - return source.videoWidth > 0 && source.videoHeight > 0 |
2467 | | - ? { width: source.videoWidth, height: source.videoHeight } |
2468 | | - : null; |
2469 | | - } |
2470 | | - if (isImageElement(source)) { |
2471 | | - return source.naturalWidth > 0 && source.naturalHeight > 0 |
2472 | | - ? { width: source.naturalWidth, height: source.naturalHeight } |
2473 | | - : null; |
2474 | | - } |
2475 | | - return null; |
| 2479 | + const [width, height] = isVideoElement(source) |
| 2480 | + ? [source.videoWidth, source.videoHeight] |
| 2481 | + : isImageElement(source) |
| 2482 | + ? [source.naturalWidth, source.naturalHeight] |
| 2483 | + : isCanvasElement(source) |
| 2484 | + ? [source.width, source.height] |
| 2485 | + : [0, 0]; |
| 2486 | + return width > 0 && height > 0 ? { width, height } : null; |
2476 | 2487 | } |
2477 | 2488 |
|
2478 | 2489 | function isDrawableSource(source: TexImageSource): boolean { |
2479 | | - if (isVideoElement(source)) { |
2480 | | - return ( |
2481 | | - source.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA && |
2482 | | - source.videoWidth > 0 && |
2483 | | - source.videoHeight > 0 |
2484 | | - ); |
2485 | | - } |
2486 | | - if (isImageElement(source)) { |
2487 | | - return source.complete && source.naturalWidth > 0 && source.naturalHeight > 0; |
| 2490 | + if (isVideoElement(source) && source.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) { |
| 2491 | + return false; |
2488 | 2492 | } |
2489 | | - return false; |
| 2493 | + if (isImageElement(source) && !source.complete) return false; |
| 2494 | + return readSourceSize(source) !== null; |
2490 | 2495 | } |
2491 | 2496 |
|
2492 | 2497 | function findRenderFrameImage(video: HTMLVideoElement): HTMLImageElement | null { |
@@ -3468,6 +3473,14 @@ function addListener( |
3468 | 3473 | entry.cleanup.push(() => target.removeEventListener(type, listener)); |
3469 | 3474 | } |
3470 | 3475 |
|
| 3476 | +function redrawsOnTransportTick(element: ColorGradingMediaElement): boolean { |
| 3477 | + if (isCanvasElement(element)) return true; |
| 3478 | + if (isVideoElement(element) && !element.paused && !element.ended) return false; |
| 3479 | + return ANIMATED_GRADING_PROPERTIES.some( |
| 3480 | + (property) => readAnimatedValue(element, property) !== null, |
| 3481 | + ); |
| 3482 | +} |
| 3483 | + |
3471 | 3484 | function cancelScheduledFrame(entry: ColorGradingEntry): void { |
3472 | 3485 | if (entry.animationFrame !== null) { |
3473 | 3486 | window.cancelAnimationFrame(entry.animationFrame); |
@@ -3759,9 +3772,7 @@ export function createColorGradingRuntime(pausedMediaLease?: { |
3759 | 3772 | const refresh = (): number => { |
3760 | 3773 | if (destroyed) return 0; |
3761 | 3774 | const attributeElements = new Set<ColorGradingMediaElement>(); |
3762 | | - const nodes = document.querySelectorAll( |
3763 | | - `video[${HF_COLOR_GRADING_ATTR}], img[${HF_COLOR_GRADING_ATTR}]`, |
3764 | | - ); |
| 3775 | + const nodes = document.querySelectorAll(COLOR_GRADING_SOURCE_SELECTOR); |
3765 | 3776 | nodes.forEach((node) => { |
3766 | 3777 | if (!isColorGradingMediaElement(node)) return; |
3767 | 3778 | attributeElements.add(node); |
@@ -3821,12 +3832,7 @@ export function createColorGradingRuntime(pausedMediaLease?: { |
3821 | 3832 | for (const element of trackedElements) { |
3822 | 3833 | const entry = entries.get(element); |
3823 | 3834 | if (!entry) continue; |
3824 | | - const hasAnimatedProperty = ANIMATED_GRADING_PROPERTIES.some( |
3825 | | - (property) => readAnimatedValue(element, property) !== null, |
3826 | | - ); |
3827 | | - if (!hasAnimatedProperty) continue; |
3828 | | - if (isVideoElement(element) && !element.paused && !element.ended) continue; |
3829 | | - if (drawEntry(entry)) drawn += 1; |
| 3835 | + if (redrawsOnTransportTick(element) && drawEntry(entry)) drawn += 1; |
3830 | 3836 | } |
3831 | 3837 | return drawn; |
3832 | 3838 | }; |
|
0 commit comments