Debian Codegraph @

node/@xterm/addon-webgl/src/CharAtlasCache.ts

raw
1 /**
2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { TextureAtlas } from './TextureAtlas';
7 import { ITerminalOptions, Terminal } from '@xterm/xterm';
8 import { ITerminal, ReadonlyColorSet } from 'browser/Types';
9 import { ICharAtlasConfig, ITextureAtlas } from './Types';
10 import { generateConfig, configEquals } from './CharAtlasUtils';
11
12 interface ITextureAtlasCacheEntry {
13 atlas: ITextureAtlas;
14 config: ICharAtlasConfig;
15 // N.B. This implementation potentially holds onto copies of the terminal forever, so
16 // this may cause memory leaks.
17 ownedBy: Terminal[];
18 }
19
20 const charAtlasCache: ITextureAtlasCacheEntry[] = [];
21
22 /**
23 * Acquires a char atlas, either generating a new one or returning an existing
24 * one that is in use by another terminal.
25 */
26 export function acquireTextureAtlas(
27 terminal: Terminal,
28 options: Required<ITerminalOptions>,
29 colors: ReadonlyColorSet,
30 deviceCellWidth: number,
31 deviceCellHeight: number,
32 deviceCharWidth: number,
33 deviceCharHeight: number,
34 devicePixelRatio: number,
35 deviceMaxTextureSize: number
36 ): ITextureAtlas {
37 const newConfig = generateConfig(deviceCellWidth, deviceCellHeight, deviceCharWidth, deviceCharHeight, options, colors, devicePixelRatio, deviceMaxTextureSize);
38
39 // Check to see if the terminal already owns this config
40 for (let i = 0; i < charAtlasCache.length; i++) {
41 const entry = charAtlasCache[i];
42 const ownedByIndex = entry.ownedBy.indexOf(terminal);
43 if (ownedByIndex >= 0) {
44 if (configEquals(entry.config, newConfig)) {
45 return entry.atlas;
46 }
47 // The configs differ, release the terminal from the entry
48 if (entry.ownedBy.length === 1) {
49 entry.atlas.dispose();
50 charAtlasCache.splice(i, 1);
51 } else {
52 entry.ownedBy.splice(ownedByIndex, 1);
53 }
54 break;
55 }
56 }
57
58 // Try match a char atlas from the cache
59 for (let i = 0; i < charAtlasCache.length; i++) {
60 const entry = charAtlasCache[i];
61 if (configEquals(entry.config, newConfig)) {
62 // Add the terminal to the cache entry and return
63 entry.ownedBy.push(terminal);
64 return entry.atlas;
65 }
66 }
67
68 const core: ITerminal = (terminal as any)._core;
69 const newEntry: ITextureAtlasCacheEntry = {
70 atlas: new TextureAtlas(document, newConfig, core.unicodeService),
71 config: newConfig,
72 ownedBy: [terminal]
73 };
74 charAtlasCache.push(newEntry);
75 return newEntry.atlas;
76 }
77
78 /**
79 * Removes a terminal reference from the cache, allowing its memory to be freed.
80 * @param terminal The terminal to remove.
81 */
82 export function removeTerminalFromCache(terminal: Terminal): void {
83 for (let i = 0; i < charAtlasCache.length; i++) {
84 const index = charAtlasCache[i].ownedBy.indexOf(terminal);
85 if (index !== -1) {
86 if (charAtlasCache[i].ownedBy.length === 1) {
87 // Remove the cache entry if it's the only terminal
88 charAtlasCache[i].atlas.dispose();
89 charAtlasCache.splice(i, 1);
90 } else {
91 // Remove the reference from the cache entry
92 charAtlasCache[i].ownedBy.splice(index, 1);
93 }
94 break;
95 }
96 }
97 }