Debian Codegraph @

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

raw
1 /**
2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import type { ITerminalAddon, Terminal } from '@xterm/xterm';
7 import type { WebglAddon as IWebglApi } from '@xterm/addon-webgl';
8 import { ICharacterJoinerService, ICharSizeService, ICoreBrowserService, IRenderService, IThemeService } from 'browser/services/Services';
9 import { ITerminal } from 'browser/Types';
10 import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
11 import { getSafariVersion, isSafari } from 'common/Platform';
12 import { ICoreService, IDecorationService, ILogService, IOptionsService } from 'common/services/Services';
13 import { IWebGL2RenderingContext } from './Types';
14 import { WebglRenderer } from './WebglRenderer';
15 import { setTraceLogger } from 'common/services/LogService';
16 import { Emitter, Event } from 'vs/base/common/event';
17
18 export class WebglAddon extends Disposable implements ITerminalAddon , IWebglApi {
19 private _terminal?: Terminal;
20 private _renderer?: WebglRenderer;
21
22 private readonly _onChangeTextureAtlas = this._register(new Emitter<HTMLCanvasElement>());
23 public readonly onChangeTextureAtlas = this._onChangeTextureAtlas.event;
24 private readonly _onAddTextureAtlasCanvas = this._register(new Emitter<HTMLCanvasElement>());
25 public readonly onAddTextureAtlasCanvas = this._onAddTextureAtlasCanvas.event;
26 private readonly _onRemoveTextureAtlasCanvas = this._register(new Emitter<HTMLCanvasElement>());
27 public readonly onRemoveTextureAtlasCanvas = this._onRemoveTextureAtlasCanvas.event;
28 private readonly _onContextLoss = this._register(new Emitter<void>());
29 public readonly onContextLoss = this._onContextLoss.event;
30
31 constructor(
32 private _preserveDrawingBuffer?: boolean
33 ) {
34 if (isSafari && getSafariVersion() < 16) {
35 // Perform an extra check to determine if Webgl2 is manually enabled in developer settings
36 const contextAttributes = {
37 antialias: false,
38 depth: false,
39 preserveDrawingBuffer: true
40 };
41 const gl = document.createElement('canvas').getContext('webgl2', contextAttributes) as IWebGL2RenderingContext;
42 if (!gl) {
43 throw new Error('Webgl2 is only supported on Safari 16 and above');
44 }
45 }
46 super();
47 }
48
49 public activate(terminal: Terminal): void {
50 const core = (terminal as any)._core as ITerminal;
51 if (!terminal.element) {
52 this._register(core.onWillOpen(() => this.activate(terminal)));
53 return;
54 }
55
56 this._terminal = terminal;
57 const coreService: ICoreService = core.coreService;
58 const optionsService: IOptionsService = core.optionsService;
59
60 const unsafeCore = core as any;
61 const renderService: IRenderService = unsafeCore._renderService;
62 const characterJoinerService: ICharacterJoinerService = unsafeCore._characterJoinerService;
63 const charSizeService: ICharSizeService = unsafeCore._charSizeService;
64 const coreBrowserService: ICoreBrowserService = unsafeCore._coreBrowserService;
65 const decorationService: IDecorationService = unsafeCore._decorationService;
66 const logService: ILogService = unsafeCore._logService;
67 const themeService: IThemeService = unsafeCore._themeService;
68
69 // Set trace logger just in case it hasn't been yet which could happen when the addon is
70 // bundled separately to the core module
71 setTraceLogger(logService);
72
73 this._renderer = this._register(new WebglRenderer(
74 terminal,
75 characterJoinerService,
76 charSizeService,
77 coreBrowserService,
78 coreService,
79 decorationService,
80 optionsService,
81 themeService,
82 this._preserveDrawingBuffer
83 ));
84 this._register(Event.forward(this._renderer.onContextLoss, this._onContextLoss));
85 this._register(Event.forward(this._renderer.onChangeTextureAtlas, this._onChangeTextureAtlas));
86 this._register(Event.forward(this._renderer.onAddTextureAtlasCanvas, this._onAddTextureAtlasCanvas));
87 this._register(Event.forward(this._renderer.onRemoveTextureAtlasCanvas, this._onRemoveTextureAtlasCanvas));
88 renderService.setRenderer(this._renderer);
89
90 this._register(toDisposable(() => {
91 if ((this._terminal as any)._core._store._isDisposed) {
92 return;
93 }
94 const renderService: IRenderService = (this._terminal as any)._core._renderService;
95 renderService.setRenderer((this._terminal as any)._core._createRenderer());
96 renderService.handleResize(terminal.cols, terminal.rows);
97 }));
98 }
99
100 public get textureAtlas(): HTMLCanvasElement | undefined {
101 return this._renderer?.textureAtlas;
102 }
103
104 public clearTextureAtlas(): void {
105 this._renderer?.clearTextureAtlas();
106 }
107 }