| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { RenderDebouncer } from 'browser/RenderDebouncer'; |
| 7 |
import { IRenderDebouncerWithCallback } from 'browser/Types'; |
| 8 |
import { IRenderDimensions, IRenderer } from 'browser/renderer/shared/Types'; |
| 9 |
import { ICharSizeService, ICoreBrowserService, IRenderService, IThemeService } from 'browser/services/Services'; |
| 10 |
import { Disposable, MutableDisposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 11 |
import { DebouncedIdleTask } from 'common/TaskQueue'; |
| 12 |
import { IBufferService, ICoreService, IDecorationService, IOptionsService } from 'common/services/Services'; |
| 13 |
import { Emitter } from 'vs/base/common/event'; |
| 14 |
|
| 15 |
interface ISelectionState { |
| 16 |
start: [number, number] | undefined; |
| 17 |
end: [number, number] | undefined; |
| 18 |
columnSelectMode: boolean; |
| 19 |
} |
| 20 |
|
| 21 |
const enum Constants { |
| 22 |
SYNCHRONIZED_OUTPUT_TIMEOUT_MS = 1000 |
| 23 |
} |
| 24 |
|
| 25 |
export class RenderService extends Disposable implements IRenderService { |
| 26 |
public serviceBrand: undefined; |
| 27 |
|
| 28 |
private _renderer: MutableDisposable<IRenderer> = this._register(new MutableDisposable()); |
| 29 |
private _renderDebouncer: IRenderDebouncerWithCallback; |
| 30 |
private _pausedResizeTask = new DebouncedIdleTask(); |
| 31 |
private _observerDisposable = this._register(new MutableDisposable()); |
| 32 |
|
| 33 |
private _isPaused: boolean = false; |
| 34 |
private _needsFullRefresh: boolean = false; |
| 35 |
private _isNextRenderRedrawOnly: boolean = true; |
| 36 |
private _needsSelectionRefresh: boolean = false; |
| 37 |
private _canvasWidth: number = 0; |
| 38 |
private _canvasHeight: number = 0; |
| 39 |
private _syncOutputHandler: SynchronizedOutputHandler; |
| 40 |
private _selectionState: ISelectionState = { |
| 41 |
start: undefined, |
| 42 |
end: undefined, |
| 43 |
columnSelectMode: false |
| 44 |
}; |
| 45 |
|
| 46 |
private readonly _onDimensionsChange = this._register(new Emitter<IRenderDimensions>()); |
| 47 |
public readonly onDimensionsChange = this._onDimensionsChange.event; |
| 48 |
private readonly _onRenderedViewportChange = this._register(new Emitter<{ start: number, end: number }>()); |
| 49 |
public readonly onRenderedViewportChange = this._onRenderedViewportChange.event; |
| 50 |
private readonly _onRender = this._register(new Emitter<{ start: number, end: number }>()); |
| 51 |
public readonly onRender = this._onRender.event; |
| 52 |
private readonly _onRefreshRequest = this._register(new Emitter<{ start: number, end: number }>()); |
| 53 |
public readonly onRefreshRequest = this._onRefreshRequest.event; |
| 54 |
|
| 55 |
public get dimensions(): IRenderDimensions { return this._renderer.value!.dimensions; } |
| 56 |
|
| 57 |
constructor( |
| 58 |
private _rowCount: number, |
| 59 |
screenElement: HTMLElement, |
| 60 |
@IOptionsService private readonly _optionsService: IOptionsService, |
| 61 |
@ICharSizeService private readonly _charSizeService: ICharSizeService, |
| 62 |
@ICoreService private readonly _coreService: ICoreService, |
| 63 |
@IDecorationService decorationService: IDecorationService, |
| 64 |
@IBufferService bufferService: IBufferService, |
| 65 |
@ICoreBrowserService private readonly _coreBrowserService: ICoreBrowserService, |
| 66 |
@IThemeService themeService: IThemeService |
| 67 |
) { |
| 68 |
super(); |
| 69 |
|
| 70 |
this._renderDebouncer = new RenderDebouncer((start, end) => this._renderRows(start, end), this._coreBrowserService); |
| 71 |
this._register(this._renderDebouncer); |
| 72 |
|
| 73 |
this._syncOutputHandler = new SynchronizedOutputHandler( |
| 74 |
this._coreBrowserService, |
| 75 |
this._coreService, |
| 76 |
() => this._fullRefresh() |
| 77 |
); |
| 78 |
this._register(toDisposable(() => this._syncOutputHandler.dispose())); |
| 79 |
|
| 80 |
this._register(this._coreBrowserService.onDprChange(() => this.handleDevicePixelRatioChange())); |
| 81 |
|
| 82 |
this._register(bufferService.onResize(() => this._fullRefresh())); |
| 83 |
this._register(bufferService.buffers.onBufferActivate(() => this._renderer.value?.clear())); |
| 84 |
this._register(this._optionsService.onOptionChange(() => this._handleOptionsChanged())); |
| 85 |
this._register(this._charSizeService.onCharSizeChange(() => this.handleCharSizeChanged())); |
| 86 |
|
| 87 |
// Do a full refresh whenever any decoration is added or removed. This may not actually result |
| 88 |
// in changes but since decorations should be used sparingly or added/removed all in the same |
| 89 |
// frame this should have minimal performance impact. |
| 90 |
this._register(decorationService.onDecorationRegistered(() => this._fullRefresh())); |
| 91 |
this._register(decorationService.onDecorationRemoved(() => this._fullRefresh())); |
| 92 |
|
| 93 |
// Clear the renderer when the a change that could affect glyphs occurs |
| 94 |
this._register(this._optionsService.onMultipleOptionChange([ |
| 95 |
'customGlyphs', |
| 96 |
'drawBoldTextInBrightColors', |
| 97 |
'letterSpacing', |
| 98 |
'lineHeight', |
| 99 |
'fontFamily', |
| 100 |
'fontSize', |
| 101 |
'fontWeight', |
| 102 |
'fontWeightBold', |
| 103 |
'minimumContrastRatio', |
| 104 |
'rescaleOverlappingGlyphs' |
| 105 |
], () => { |
| 106 |
this.clear(); |
| 107 |
this.handleResize(bufferService.cols, bufferService.rows); |
| 108 |
this._fullRefresh(); |
| 109 |
})); |
| 110 |
|
| 111 |
// Refresh the cursor line when the cursor changes |
| 112 |
this._register(this._optionsService.onMultipleOptionChange([ |
| 113 |
'cursorBlink', |
| 114 |
'cursorStyle' |
| 115 |
], () => this.refreshRows(bufferService.buffer.y, bufferService.buffer.y, true))); |
| 116 |
|
| 117 |
this._register(themeService.onChangeColors(() => this._fullRefresh())); |
| 118 |
|
| 119 |
this._registerIntersectionObserver(this._coreBrowserService.window, screenElement); |
| 120 |
this._register(this._coreBrowserService.onWindowChange((w) => this._registerIntersectionObserver(w, screenElement))); |
| 121 |
} |
| 122 |
|
| 123 |
private _registerIntersectionObserver(w: Window & typeof globalThis, screenElement: HTMLElement): void { |
| 124 |
// Detect whether IntersectionObserver is detected and enable renderer pause |
| 125 |
// and resume based on terminal visibility if so |
| 126 |
if ('IntersectionObserver' in w) { |
| 127 |
const observer = new w.IntersectionObserver(e => this._handleIntersectionChange(e[e.length - 1]), { threshold: 0 }); |
| 128 |
observer.observe(screenElement); |
| 129 |
this._observerDisposable.value = toDisposable(() => observer.disconnect()); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private _handleIntersectionChange(entry: IntersectionObserverEntry): void { |
| 134 |
this._isPaused = entry.isIntersecting === undefined ? (entry.intersectionRatio === 0) : !entry.isIntersecting; |
| 135 |
|
| 136 |
// Terminal was hidden on open |
| 137 |
if (!this._isPaused && !this._charSizeService.hasValidSize) { |
| 138 |
this._charSizeService.measure(); |
| 139 |
} |
| 140 |
|
| 141 |
if (!this._isPaused && this._needsFullRefresh) { |
| 142 |
this._pausedResizeTask.flush(); |
| 143 |
this.refreshRows(0, this._rowCount - 1); |
| 144 |
this._needsFullRefresh = false; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public refreshRows(start: number, end: number, isRedrawOnly: boolean = false): void { |
| 149 |
if (this._isPaused) { |
| 150 |
this._needsFullRefresh = true; |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
if (this._coreService.decPrivateModes.synchronizedOutput) { |
| 155 |
this._syncOutputHandler.bufferRows(start, end); |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
const buffered = this._syncOutputHandler.flush(); |
| 160 |
if (buffered) { |
| 161 |
start = Math.min(start, buffered.start); |
| 162 |
end = Math.max(end, buffered.end); |
| 163 |
} |
| 164 |
|
| 165 |
if (!isRedrawOnly) { |
| 166 |
this._isNextRenderRedrawOnly = false; |
| 167 |
} |
| 168 |
this._renderDebouncer.refresh(start, end, this._rowCount); |
| 169 |
} |
| 170 |
|
| 171 |
private _renderRows(start: number, end: number): void { |
| 172 |
if (!this._renderer.value) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
// Skip rendering if synchronized output mode is enabled. This check must happen here |
| 177 |
// (in addition to refreshRows) to handle renders that were queued before the mode was enabled. |
| 178 |
if (this._coreService.decPrivateModes.synchronizedOutput) { |
| 179 |
this._syncOutputHandler.bufferRows(start, end); |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
// Since this is debounced, a resize event could have happened between the time a refresh was |
| 184 |
// requested and when this triggers. Clamp the values of start and end to ensure they're valid |
| 185 |
// given the current viewport state. |
| 186 |
start = Math.min(start, this._rowCount - 1); |
| 187 |
end = Math.min(end, this._rowCount - 1); |
| 188 |
|
| 189 |
// Render |
| 190 |
this._renderer.value.renderRows(start, end); |
| 191 |
|
| 192 |
// Update selection if needed |
| 193 |
if (this._needsSelectionRefresh) { |
| 194 |
this._renderer.value.handleSelectionChanged(this._selectionState.start, this._selectionState.end, this._selectionState.columnSelectMode); |
| 195 |
this._needsSelectionRefresh = false; |
| 196 |
} |
| 197 |
|
| 198 |
// Fire render event only if it was not a redraw |
| 199 |
if (!this._isNextRenderRedrawOnly) { |
| 200 |
this._onRenderedViewportChange.fire({ start, end }); |
| 201 |
} |
| 202 |
this._onRender.fire({ start, end }); |
| 203 |
this._isNextRenderRedrawOnly = true; |
| 204 |
} |
| 205 |
|
| 206 |
public resize(cols: number, rows: number): void { |
| 207 |
this._rowCount = rows; |
| 208 |
this._fireOnCanvasResize(); |
| 209 |
} |
| 210 |
|
| 211 |
private _handleOptionsChanged(): void { |
| 212 |
if (!this._renderer.value) { |
| 213 |
return; |
| 214 |
} |
| 215 |
this.refreshRows(0, this._rowCount - 1); |
| 216 |
this._fireOnCanvasResize(); |
| 217 |
} |
| 218 |
|
| 219 |
private _fireOnCanvasResize(): void { |
| 220 |
if (!this._renderer.value) { |
| 221 |
return; |
| 222 |
} |
| 223 |
// Don't fire the event if the dimensions haven't changed |
| 224 |
if (this._renderer.value.dimensions.css.canvas.width === this._canvasWidth && this._renderer.value.dimensions.css.canvas.height === this._canvasHeight) { |
| 225 |
return; |
| 226 |
} |
| 227 |
this._onDimensionsChange.fire(this._renderer.value.dimensions); |
| 228 |
} |
| 229 |
|
| 230 |
public hasRenderer(): boolean { |
| 231 |
return !!this._renderer.value; |
| 232 |
} |
| 233 |
|
| 234 |
public setRenderer(renderer: IRenderer): void { |
| 235 |
this._renderer.value = renderer; |
| 236 |
// If the value was not set, the terminal is being disposed so ignore it |
| 237 |
if (this._renderer.value) { |
| 238 |
this._renderer.value.onRequestRedraw(e => this.refreshRows(e.start, e.end, true)); |
| 239 |
|
| 240 |
// Force a refresh |
| 241 |
this._needsSelectionRefresh = true; |
| 242 |
this._fullRefresh(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
public addRefreshCallback(callback: FrameRequestCallback): number { |
| 247 |
return this._renderDebouncer.addRefreshCallback(callback); |
| 248 |
} |
| 249 |
|
| 250 |
private _fullRefresh(): void { |
| 251 |
if (this._isPaused) { |
| 252 |
this._needsFullRefresh = true; |
| 253 |
} else { |
| 254 |
this.refreshRows(0, this._rowCount - 1); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
public clearTextureAtlas(): void { |
| 259 |
if (!this._renderer.value) { |
| 260 |
return; |
| 261 |
} |
| 262 |
this._renderer.value.clearTextureAtlas?.(); |
| 263 |
this._fullRefresh(); |
| 264 |
} |
| 265 |
|
| 266 |
public handleDevicePixelRatioChange(): void { |
| 267 |
// Force char size measurement as DomMeasureStrategy(getBoundingClientRect) is not stable |
| 268 |
// when devicePixelRatio changes |
| 269 |
this._charSizeService.measure(); |
| 270 |
|
| 271 |
if (!this._renderer.value) { |
| 272 |
return; |
| 273 |
} |
| 274 |
this._renderer.value.handleDevicePixelRatioChange(); |
| 275 |
this.refreshRows(0, this._rowCount - 1); |
| 276 |
} |
| 277 |
|
| 278 |
public handleResize(cols: number, rows: number): void { |
| 279 |
if (!this._renderer.value) { |
| 280 |
return; |
| 281 |
} |
| 282 |
if (this._isPaused) { |
| 283 |
this._pausedResizeTask.set(() => this._renderer.value?.handleResize(cols, rows)); |
| 284 |
} else { |
| 285 |
this._renderer.value.handleResize(cols, rows); |
| 286 |
} |
| 287 |
this._fullRefresh(); |
| 288 |
} |
| 289 |
|
| 290 |
// TODO: Is this useful when we have onResize? |
| 291 |
public handleCharSizeChanged(): void { |
| 292 |
this._renderer.value?.handleCharSizeChanged(); |
| 293 |
} |
| 294 |
|
| 295 |
public handleBlur(): void { |
| 296 |
this._renderer.value?.handleBlur(); |
| 297 |
} |
| 298 |
|
| 299 |
public handleFocus(): void { |
| 300 |
this._renderer.value?.handleFocus(); |
| 301 |
} |
| 302 |
|
| 303 |
public handleSelectionChanged(start: [number, number] | undefined, end: [number, number] | undefined, columnSelectMode: boolean): void { |
| 304 |
this._selectionState.start = start; |
| 305 |
this._selectionState.end = end; |
| 306 |
this._selectionState.columnSelectMode = columnSelectMode; |
| 307 |
this._renderer.value?.handleSelectionChanged(start, end, columnSelectMode); |
| 308 |
} |
| 309 |
|
| 310 |
public handleCursorMove(): void { |
| 311 |
this._renderer.value?.handleCursorMove(); |
| 312 |
} |
| 313 |
|
| 314 |
public clear(): void { |
| 315 |
this._renderer.value?.clear(); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Buffers row refresh requests during synchronized output mode (DEC mode 2026). |
| 321 |
* When the mode is disabled, the accumulated row range is flushed for rendering. |
| 322 |
* A safety timeout ensures rendering occurs even if the end sequence is not received. |
| 323 |
*/ |
| 324 |
class SynchronizedOutputHandler { |
| 325 |
private _start: number = 0; |
| 326 |
private _end: number = 0; |
| 327 |
private _timeout: number | undefined; |
| 328 |
private _isBuffering: boolean = false; |
| 329 |
|
| 330 |
constructor( |
| 331 |
private readonly _coreBrowserService: ICoreBrowserService, |
| 332 |
private readonly _coreService: ICoreService, |
| 333 |
private readonly _onTimeout: () => void |
| 334 |
) {} |
| 335 |
|
| 336 |
public bufferRows(start: number, end: number): void { |
| 337 |
if (!this._isBuffering) { |
| 338 |
this._start = start; |
| 339 |
this._end = end; |
| 340 |
this._isBuffering = true; |
| 341 |
} else { |
| 342 |
this._start = Math.min(this._start, start); |
| 343 |
this._end = Math.max(this._end, end); |
| 344 |
} |
| 345 |
|
| 346 |
if (this._timeout === undefined) { |
| 347 |
this._timeout = this._coreBrowserService.window.setTimeout(() => { |
| 348 |
this._timeout = undefined; |
| 349 |
this._coreService.decPrivateModes.synchronizedOutput = false; |
| 350 |
this._onTimeout(); |
| 351 |
}, Constants.SYNCHRONIZED_OUTPUT_TIMEOUT_MS); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
public flush(): { start: number, end: number } | undefined { |
| 356 |
if (this._timeout !== undefined) { |
| 357 |
this._coreBrowserService.window.clearTimeout(this._timeout); |
| 358 |
this._timeout = undefined; |
| 359 |
} |
| 360 |
|
| 361 |
if (!this._isBuffering) { |
| 362 |
return undefined; |
| 363 |
} |
| 364 |
|
| 365 |
const result = { start: this._start, end: this._end }; |
| 366 |
this._isBuffering = false; |
| 367 |
return result; |
| 368 |
} |
| 369 |
|
| 370 |
public dispose(): void { |
| 371 |
if (this._timeout !== undefined) { |
| 372 |
this._coreBrowserService.window.clearTimeout(this._timeout); |
| 373 |
this._timeout = undefined; |
| 374 |
} |
| 375 |
} |
| 376 |
} |