Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/CoreTextNode.canvasTextureLeak.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { CoreTextureManager } from './CoreTextureManager.js';
import { CoreNode } from './CoreNode.js';
import { CoreTextNode } from './CoreTextNode.js';
import { Texture, TextureType } from './textures/Texture.js';
import { makeCanvasTextCacheKey } from './text-rendering/Utils.js';
import { TextureMemoryManager } from './TextureMemoryManager.js';
import type { TextureMemoryManagerSettings } from './TextureMemoryManager.js';
import { Stage } from './Stage.js';
Expand Down Expand Up @@ -79,6 +80,7 @@ function makeCanvasTextRenderer() {
),
addQuads: vi.fn(),
renderQuads: vi.fn(),
getCacheKey: makeCanvasTextCacheKey,
} as unknown as TextRenderer;
}

Expand Down
36 changes: 31 additions & 5 deletions src/core/CoreTextNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ describe('CoreTextNode', () => {
type: 'canvas',
width: 100,
height: 20,
renderSource: () => ({} as ImageBitmap),
renderSource: () => ({}) as ImageBitmap,
hasRemainingText: false,
remainingLines: 0,
};
Expand Down Expand Up @@ -557,7 +557,7 @@ describe('CoreTextNode', () => {
type: 'canvas',
width: 100,
height: 20,
renderSource: () => ({} as ImageBitmap),
renderSource: () => ({}) as ImageBitmap,
hasRemainingText: false,
remainingLines: 0,
};
Expand All @@ -572,6 +572,32 @@ describe('CoreTextNode', () => {
expect(mockTextRenderer.renderQuads).not.toHaveBeenCalled();
expect(addQuad).toHaveBeenCalled();
});

it('keys the canvas texture with the renderer getCacheKey', () => {
const createTexture = vi.fn().mockReturnValue(stage.defaultTexture);
(stage as any).txManager = { createTexture };
const getCacheKey = vi.fn().mockReturnValue('custom-key');
const node = new CoreTextNode(stage, defaultTextProps, {
...mockTextRenderer,
type: 'canvas',
getCacheKey,
});

(node as any).handleRenderResult({
type: 'canvas',
width: 100,
height: 20,
renderSource: () => ({}) as ImageBitmap,
hasRemainingText: false,
remainingLines: 0,
});

expect(getCacheKey).toHaveBeenCalledWith(defaultTextProps);
expect(createTexture).toHaveBeenCalledWith(
'CanvasTextTexture',
expect.objectContaining({ key: 'custom-key' }),
);
});
});

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -661,9 +687,9 @@ describe('CoreTextNode', () => {
mockTextRenderer,
);

(node)[prop] = undefined;
node[prop] = undefined;

expect((node)[prop]).toBe(fallback);
expect(node[prop]).toBe(fallback);
},
);

Expand All @@ -679,7 +705,7 @@ describe('CoreTextNode', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
(node as any)[prop] = kept;

expect((node)[prop]).toBe(kept);
expect(node[prop]).toBe(kept);
},
);

Expand Down
11 changes: 4 additions & 7 deletions src/core/CoreTextNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ import type {
import type { RectWithValid } from './lib/utils.js';
import type { CoreRenderer } from './renderers/CoreRenderer.js';
import type { TextureLoadedEventHandler } from './textures/Texture.js';
import {
makeCanvasTextCacheKey,
type CanvasTextTexture,
} from './textures/CanvasTextTexture.js';
import type { CanvasTextTexture } from './textures/CanvasTextTexture.js';
import { Matrix3d } from './lib/Matrix3d.js';
import type { WebGlCtxTexture } from './renderers/webgl/WebGlCtxTexture.js';
export interface CoreTextNodeProps extends CoreNodeProps, TrProps {
Expand Down Expand Up @@ -140,7 +137,7 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps {
* Release this node's reference to its canvas text texture.
*
* @remarks
* Textures are shared via the keyCache (see `makeCanvasTextCacheKey`), so
* Textures are shared via the keyCache (see `TextRenderer.getCacheKey`), so
* ownership/cancel decisions live on the texture itself
* (`CanvasTextTexture.releaseOwner`): this only drops the node-side
* reference. Safe to call multiple times.
Expand Down Expand Up @@ -284,7 +281,7 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps {
* Override CoreNode's update method to handle text-specific updates
*/
override update(delta: number, parentClippingRect: RectWithValid): void {
this.renderText(this.textProps.forceLoad)
this.renderText(this.textProps.forceLoad);
// First run the standard CoreNode update
super.update(delta, parentClippingRect);
}
Expand Down Expand Up @@ -362,7 +359,7 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps {
rich: this.textProps.richText === true,
// textProps is passed by reference (no copy): the key builder only
// reads it synchronously to build the key string.
key: makeCanvasTextCacheKey(tProps),
key: this.textRenderer.getCacheKey(tProps),
},
);
this._canvasTexture = newTexture;
Expand Down
2 changes: 2 additions & 0 deletions src/core/Stage.canvasTextLatency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CoreTextureManager } from './CoreTextureManager.js';
import { CoreNode, UpdateType } from './CoreNode.js';
import { CoreTextNode } from './CoreTextNode.js';
import { ImageTexture } from './textures/ImageTexture.js';
import { makeCanvasTextCacheKey } from './text-rendering/Utils.js';
import type { Texture } from './textures/Texture.js';
import { Stage } from './Stage.js';
import { createBound } from './lib/utils.js';
Expand Down Expand Up @@ -77,6 +78,7 @@ function makeCanvasTextRenderer() {
),
addQuads: vi.fn(),
renderQuads: vi.fn(),
getCacheKey: makeCanvasTextCacheKey,
} as unknown as TextRenderer;
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/text-rendering/CanvasTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type {
} from './TextRenderer.js';
import * as CanvasFontHandler from './CanvasFontHandler.js';
import type { CoreTextNodeProps } from '../CoreTextNode.js';
import { hasZeroWidthSpace } from './Utils.js';
import { hasZeroWidthSpace, makeCanvasTextCacheKey } from './Utils.js';
import { mapTextLayout } from './TextLayoutEngine.js';
import { parseRichText, ParseResult } from './RichTextParser.js';
import {
Expand Down Expand Up @@ -729,6 +729,7 @@ const CanvasTextRenderer = {
renderQuads,
init,
clearCache,
getCacheKey: makeCanvasTextCacheKey,
};

export default CanvasTextRenderer;
1 change: 1 addition & 0 deletions src/core/text-rendering/SdfTextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ const SdfTextRenderer = {
renderQuads,
init,
clearCache,
getCacheKey: getLayoutCacheKey,
};

export default SdfTextRenderer;
11 changes: 11 additions & 0 deletions src/core/text-rendering/TextRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,17 @@ export interface TextRenderer {
) => void;
init: (stage: Stage) => void;
clearCache: () => void;
/**
* Build the key under which this renderer's output for `props` is reused.
*
* @remarks
* Canvas, the keyCache key for the text raster.
* - it must cover every prop that changes the **drawn pixels**
* - return `null` for an uncached, single-owner texture
* SDF, the layout cache key.
* - it must cover every prop that changes the **layout**.
*/
getCacheKey: (props: CoreTextNodeProps) => string | null;
}

/**
Expand Down
51 changes: 51 additions & 0 deletions src/core/text-rendering/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,57 @@ export function tokenizeString(tokenRegex: RegExp, text: string): string[] {
return final.filter((word) => word != '');
}

/**
* Raster inputs that determine canvas text pixels.
*
* @remarks
* Keep in sync with what `CanvasTextRenderer` consumes: `contain`,
* `offsetY` and `verticalAlign` only affect node transforms (never pixels)
* and are intentionally excluded, as is `color` for plain text (plain
* rasters are white alpha masks tinted per node at draw time — rich text
* bakes span colors, so it includes `color`).
*/
export interface CanvasTextCacheKeyProps {
text: string;
richText: boolean;
fontFamily: string;
fontStyle: string;
fontSize: number;
letterSpacing: number;
lineHeight: number;
textAlign: string;
maxLines: number;
overflowSuffix: string;
wordBreak: string;
maxWidth: number;
maxHeight: number;
color: number;
}

/**
* Build a stable keyCache key for a canvas text raster.
*/
export function makeCanvasTextCacheKey(props: CanvasTextCacheKeyProps): string {
return JSON.stringify([
'CanvasText',
props.richText === true ? 1 : 0,
props.fontFamily,
props.fontStyle,
props.fontSize,
props.letterSpacing,
props.lineHeight,
props.textAlign,
props.maxLines,
props.overflowSuffix,
props.wordBreak,
props.maxWidth,
props.maxHeight,
// Rich text bakes the node color into uncolored spans; plain text does not.
props.richText === true ? props.color : 0,
props.text,
]);
}

export function getLayoutCacheKey(props: CoreTextNodeProps): string {
return `${props.text}-${props.fontFamily}-${props.fontSize}-${props.letterSpacing}-${props.lineHeight}-${props.maxHeight}-${props.maxWidth}-${props.textAlign}-${props.wordBreak}-${props.maxLines}-${props.overflowSuffix}-${props.richText}`;
}
56 changes: 3 additions & 53 deletions src/core/textures/CanvasTextTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export interface CanvasTextTextureProps {
*/
rich?: boolean;
/**
* Stable cache key for the raster inputs (see {@link makeCanvasTextCacheKey}).
* Stable cache key for the raster inputs, built by the text renderer's
* `getCacheKey`.
*
* @remarks
* Textures created with equal keys share one raster via the texture
Expand All @@ -45,57 +46,6 @@ export interface CanvasTextTextureProps {
key?: string | null;
}

/**
* Raster inputs that determine canvas text pixels.
*
* @remarks
* Keep in sync with what `CanvasTextRenderer` consumes: `contain`,
* `offsetY` and `verticalAlign` only affect node transforms (never pixels)
* and are intentionally excluded, as is `color` for plain text (plain
* rasters are white alpha masks tinted per node at draw time — rich text
* bakes span colors, so it includes `color`).
*/
export interface CanvasTextCacheKeyProps {
text: string;
richText: boolean;
fontFamily: string;
fontStyle: string;
fontSize: number;
letterSpacing: number;
lineHeight: number;
textAlign: string;
maxLines: number;
overflowSuffix: string;
wordBreak: string;
maxWidth: number;
maxHeight: number;
color: number;
}

/**
* Build a stable keyCache key for a canvas text raster.
*/
export function makeCanvasTextCacheKey(props: CanvasTextCacheKeyProps): string {
return JSON.stringify([
'CanvasText',
props.richText === true ? 1 : 0,
props.fontFamily,
props.fontStyle,
props.fontSize,
props.letterSpacing,
props.lineHeight,
props.textAlign,
props.maxLines,
props.overflowSuffix,
props.wordBreak,
props.maxWidth,
props.maxHeight,
// Rich text bakes the node color into uncolored spans; plain text does not.
props.richText === true ? props.color : 0,
props.text,
]);
}

/**
* Rasterized canvas text with a regenerable source. The draw callback must
* capture a snapshot of the text layout, not a mutable CoreTextNode's props.
Expand Down Expand Up @@ -182,7 +132,7 @@ export class CanvasTextTexture extends Texture {
this.disposeSource();
}

// Textures with an explicit stable key (see makeCanvasTextCacheKey) are
// Textures with an explicit stable key (see TextRenderer.getCacheKey) are
// shared via the manager's keyCache, which is stage-scoped; ownership is
// tracked per node via renderableOwners. Key-less textures stay uncached.
static override makeCacheKey(props: CanvasTextTextureProps): string | false {
Expand Down
Loading