| 1 |
/** |
| 2 |
* Copyright (c) 2018 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IRenderDimensions } from 'browser/renderer/shared/Types'; |
| 7 |
import { IThemeService } from 'browser/services/Services'; |
| 8 |
import { ReadonlyColorSet } from 'browser/Types'; |
| 9 |
import { Attributes, FgFlags } from 'common/buffer/Constants'; |
| 10 |
import { Disposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 11 |
import { IColor } from 'common/Types'; |
| 12 |
import { Terminal } from '@xterm/xterm'; |
| 13 |
import { RENDER_MODEL_BG_OFFSET, RENDER_MODEL_FG_OFFSET, RENDER_MODEL_INDICIES_PER_CELL } from './RenderModel'; |
| 14 |
import { IRenderModel, IWebGL2RenderingContext, IWebGLVertexArrayObject } from './Types'; |
| 15 |
import { createProgram, expandFloat32Array, PROJECTION_MATRIX } from './WebglUtils'; |
| 16 |
import { throwIfFalsy } from 'browser/renderer/shared/RendererUtils'; |
| 17 |
|
| 18 |
const enum VertexAttribLocations { |
| 19 |
POSITION = 0, |
| 20 |
SIZE = 1, |
| 21 |
COLOR = 2, |
| 22 |
UNIT_QUAD = 3 |
| 23 |
} |
| 24 |
|
| 25 |
const vertexShaderSource = `#version 300 es |
| 26 |
layout (location = ${VertexAttribLocations.POSITION}) in vec2 a_position; |
| 27 |
layout (location = ${VertexAttribLocations.SIZE}) in vec2 a_size; |
| 28 |
layout (location = ${VertexAttribLocations.COLOR}) in vec4 a_color; |
| 29 |
layout (location = ${VertexAttribLocations.UNIT_QUAD}) in vec2 a_unitquad; |
| 30 |
|
| 31 |
uniform mat4 u_projection; |
| 32 |
|
| 33 |
out vec4 v_color; |
| 34 |
|
| 35 |
void main() { |
| 36 |
vec2 zeroToOne = a_position + (a_unitquad * a_size); |
| 37 |
gl_Position = u_projection * vec4(zeroToOne, 0.0, 1.0); |
| 38 |
v_color = a_color; |
| 39 |
}`; |
| 40 |
|
| 41 |
const fragmentShaderSource = `#version 300 es |
| 42 |
precision lowp float; |
| 43 |
|
| 44 |
in vec4 v_color; |
| 45 |
|
| 46 |
out vec4 outColor; |
| 47 |
|
| 48 |
void main() { |
| 49 |
outColor = v_color; |
| 50 |
}`; |
| 51 |
|
| 52 |
const INDICES_PER_RECTANGLE = 8; |
| 53 |
const BYTES_PER_RECTANGLE = INDICES_PER_RECTANGLE * Float32Array.BYTES_PER_ELEMENT; |
| 54 |
|
| 55 |
const INITIAL_BUFFER_RECTANGLE_CAPACITY = 20 * INDICES_PER_RECTANGLE; |
| 56 |
|
| 57 |
class Vertices { |
| 58 |
public attributes: Float32Array; |
| 59 |
public count: number; |
| 60 |
|
| 61 |
constructor() { |
| 62 |
this.attributes = new Float32Array(INITIAL_BUFFER_RECTANGLE_CAPACITY); |
| 63 |
this.count = 0; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// Work variables to avoid garbage collection |
| 68 |
let $rgba = 0; |
| 69 |
let $x1 = 0; |
| 70 |
let $y1 = 0; |
| 71 |
let $r = 0; |
| 72 |
let $g = 0; |
| 73 |
let $b = 0; |
| 74 |
let $a = 0; |
| 75 |
|
| 76 |
export class RectangleRenderer extends Disposable { |
| 77 |
|
| 78 |
private _program: WebGLProgram; |
| 79 |
private _vertexArrayObject: IWebGLVertexArrayObject; |
| 80 |
private _attributesBuffer: WebGLBuffer; |
| 81 |
private _projectionLocation: WebGLUniformLocation; |
| 82 |
private _bgFloat!: Float32Array; |
| 83 |
private _cursorFloat!: Float32Array; |
| 84 |
|
| 85 |
private _vertices: Vertices = new Vertices(); |
| 86 |
private _verticesCursor: Vertices = new Vertices(); |
| 87 |
|
| 88 |
constructor( |
| 89 |
private _terminal: Terminal, |
| 90 |
private _gl: IWebGL2RenderingContext, |
| 91 |
private _dimensions: IRenderDimensions, |
| 92 |
private readonly _themeService: IThemeService |
| 93 |
) { |
| 94 |
super(); |
| 95 |
|
| 96 |
const gl = this._gl; |
| 97 |
|
| 98 |
this._program = throwIfFalsy(createProgram(gl, vertexShaderSource, fragmentShaderSource)); |
| 99 |
this._register(toDisposable(() => gl.deleteProgram(this._program))); |
| 100 |
|
| 101 |
// Uniform locations |
| 102 |
this._projectionLocation = throwIfFalsy(gl.getUniformLocation(this._program, 'u_projection')); |
| 103 |
|
| 104 |
// Create and set the vertex array object |
| 105 |
this._vertexArrayObject = gl.createVertexArray(); |
| 106 |
gl.bindVertexArray(this._vertexArrayObject); |
| 107 |
|
| 108 |
// Setup a_unitquad, this defines the 4 vertices of a rectangle |
| 109 |
const unitQuadVertices = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]); |
| 110 |
const unitQuadVerticesBuffer = gl.createBuffer(); |
| 111 |
this._register(toDisposable(() => gl.deleteBuffer(unitQuadVerticesBuffer))); |
| 112 |
gl.bindBuffer(gl.ARRAY_BUFFER, unitQuadVerticesBuffer); |
| 113 |
gl.bufferData(gl.ARRAY_BUFFER, unitQuadVertices, gl.STATIC_DRAW); |
| 114 |
gl.enableVertexAttribArray(VertexAttribLocations.UNIT_QUAD); |
| 115 |
gl.vertexAttribPointer(VertexAttribLocations.UNIT_QUAD, 2, this._gl.FLOAT, false, 0, 0); |
| 116 |
|
| 117 |
// Setup the unit quad element array buffer, this points to indices in |
| 118 |
// unitQuadVertices to allow is to draw 2 triangles from the vertices via a |
| 119 |
// triangle strip |
| 120 |
const unitQuadElementIndices = new Uint8Array([0, 1, 2, 3]); |
| 121 |
const elementIndicesBuffer = gl.createBuffer(); |
| 122 |
this._register(toDisposable(() => gl.deleteBuffer(elementIndicesBuffer))); |
| 123 |
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elementIndicesBuffer); |
| 124 |
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, unitQuadElementIndices, gl.STATIC_DRAW); |
| 125 |
|
| 126 |
// Setup attributes |
| 127 |
this._attributesBuffer = throwIfFalsy(gl.createBuffer()); |
| 128 |
this._register(toDisposable(() => gl.deleteBuffer(this._attributesBuffer))); |
| 129 |
gl.bindBuffer(gl.ARRAY_BUFFER, this._attributesBuffer); |
| 130 |
gl.enableVertexAttribArray(VertexAttribLocations.POSITION); |
| 131 |
gl.vertexAttribPointer(VertexAttribLocations.POSITION, 2, gl.FLOAT, false, BYTES_PER_RECTANGLE, 0); |
| 132 |
gl.vertexAttribDivisor(VertexAttribLocations.POSITION, 1); |
| 133 |
gl.enableVertexAttribArray(VertexAttribLocations.SIZE); |
| 134 |
gl.vertexAttribPointer(VertexAttribLocations.SIZE, 2, gl.FLOAT, false, BYTES_PER_RECTANGLE, 2 * Float32Array.BYTES_PER_ELEMENT); |
| 135 |
gl.vertexAttribDivisor(VertexAttribLocations.SIZE, 1); |
| 136 |
gl.enableVertexAttribArray(VertexAttribLocations.COLOR); |
| 137 |
gl.vertexAttribPointer(VertexAttribLocations.COLOR, 4, gl.FLOAT, false, BYTES_PER_RECTANGLE, 4 * Float32Array.BYTES_PER_ELEMENT); |
| 138 |
gl.vertexAttribDivisor(VertexAttribLocations.COLOR, 1); |
| 139 |
|
| 140 |
this._updateCachedColors(_themeService.colors); |
| 141 |
this._register(this._themeService.onChangeColors(e => { |
| 142 |
this._updateCachedColors(e); |
| 143 |
this._updateViewportRectangle(); |
| 144 |
})); |
| 145 |
} |
| 146 |
|
| 147 |
public renderBackgrounds(): void { |
| 148 |
this._renderVertices(this._vertices); |
| 149 |
} |
| 150 |
|
| 151 |
public renderCursor(): void { |
| 152 |
this._renderVertices(this._verticesCursor); |
| 153 |
} |
| 154 |
|
| 155 |
private _renderVertices(vertices: Vertices): void { |
| 156 |
const gl = this._gl; |
| 157 |
|
| 158 |
gl.useProgram(this._program); |
| 159 |
|
| 160 |
gl.bindVertexArray(this._vertexArrayObject); |
| 161 |
|
| 162 |
gl.uniformMatrix4fv(this._projectionLocation, false, PROJECTION_MATRIX); |
| 163 |
|
| 164 |
// Bind attributes buffer and draw |
| 165 |
gl.bindBuffer(gl.ARRAY_BUFFER, this._attributesBuffer); |
| 166 |
gl.bufferData(gl.ARRAY_BUFFER, vertices.attributes, gl.DYNAMIC_DRAW); |
| 167 |
gl.drawElementsInstanced(this._gl.TRIANGLE_STRIP, 4, gl.UNSIGNED_BYTE, 0, vertices.count); |
| 168 |
} |
| 169 |
|
| 170 |
public handleResize(): void { |
| 171 |
this._updateViewportRectangle(); |
| 172 |
} |
| 173 |
|
| 174 |
public setDimensions(dimensions: IRenderDimensions): void { |
| 175 |
this._dimensions = dimensions; |
| 176 |
} |
| 177 |
|
| 178 |
private _updateCachedColors(colors: ReadonlyColorSet): void { |
| 179 |
this._bgFloat = this._colorToFloat32Array(colors.background); |
| 180 |
this._cursorFloat = this._colorToFloat32Array(colors.cursor); |
| 181 |
} |
| 182 |
|
| 183 |
private _updateViewportRectangle(): void { |
| 184 |
// Set first rectangle that clears the screen |
| 185 |
this._addRectangleFloat( |
| 186 |
this._vertices.attributes, |
| 187 |
0, |
| 188 |
0, |
| 189 |
0, |
| 190 |
this._terminal.cols * this._dimensions.device.cell.width, |
| 191 |
this._terminal.rows * this._dimensions.device.cell.height, |
| 192 |
this._bgFloat |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
public updateBackgrounds(model: IRenderModel): void { |
| 197 |
const terminal = this._terminal; |
| 198 |
const vertices = this._vertices; |
| 199 |
|
| 200 |
// Declare variable ahead of time to avoid garbage collection |
| 201 |
let rectangleCount = 1; |
| 202 |
let y: number; |
| 203 |
let x: number; |
| 204 |
let currentStartX: number; |
| 205 |
let currentBg: number; |
| 206 |
let currentFg: number; |
| 207 |
let currentInverse: boolean; |
| 208 |
let modelIndex: number; |
| 209 |
let bg: number; |
| 210 |
let fg: number; |
| 211 |
let inverse: boolean; |
| 212 |
let offset: number; |
| 213 |
|
| 214 |
for (y = 0; y < terminal.rows; y++) { |
| 215 |
currentStartX = -1; |
| 216 |
currentBg = 0; |
| 217 |
currentFg = 0; |
| 218 |
currentInverse = false; |
| 219 |
for (x = 0; x < terminal.cols; x++) { |
| 220 |
modelIndex = ((y * terminal.cols) + x) * RENDER_MODEL_INDICIES_PER_CELL; |
| 221 |
bg = model.cells[modelIndex + RENDER_MODEL_BG_OFFSET]; |
| 222 |
fg = model.cells[modelIndex + RENDER_MODEL_FG_OFFSET]; |
| 223 |
inverse = !!(fg & FgFlags.INVERSE); |
| 224 |
if (bg !== currentBg || (fg !== currentFg && (currentInverse || inverse))) { |
| 225 |
// A rectangle needs to be drawn if going from non-default to another color |
| 226 |
if (currentBg !== 0 || (currentInverse && currentFg !== 0)) { |
| 227 |
offset = rectangleCount++ * INDICES_PER_RECTANGLE; |
| 228 |
this._updateRectangle(vertices, offset, currentFg, currentBg, currentStartX, x, y); |
| 229 |
} |
| 230 |
currentStartX = x; |
| 231 |
currentBg = bg; |
| 232 |
currentFg = fg; |
| 233 |
currentInverse = inverse; |
| 234 |
} |
| 235 |
} |
| 236 |
// Finish rectangle if it's still going |
| 237 |
if (currentBg !== 0 || (currentInverse && currentFg !== 0)) { |
| 238 |
offset = rectangleCount++ * INDICES_PER_RECTANGLE; |
| 239 |
this._updateRectangle(vertices, offset, currentFg, currentBg, currentStartX, terminal.cols, y); |
| 240 |
} |
| 241 |
} |
| 242 |
vertices.count = rectangleCount; |
| 243 |
} |
| 244 |
|
| 245 |
public updateCursor(model: IRenderModel): void { |
| 246 |
const vertices = this._verticesCursor; |
| 247 |
const cursor = model.cursor; |
| 248 |
if (!cursor || cursor.style === 'block') { |
| 249 |
vertices.count = 0; |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
let offset: number; |
| 254 |
let rectangleCount = 0; |
| 255 |
|
| 256 |
if (cursor.style === 'bar' || cursor.style === 'outline') { |
| 257 |
// Left edge |
| 258 |
offset = rectangleCount++ * INDICES_PER_RECTANGLE; |
| 259 |
this._addRectangleFloat( |
| 260 |
vertices.attributes, |
| 261 |
offset, |
| 262 |
cursor.x * this._dimensions.device.cell.width, |
| 263 |
cursor.y * this._dimensions.device.cell.height, |
| 264 |
cursor.style === 'bar' ? cursor.dpr * cursor.cursorWidth : cursor.dpr, |
| 265 |
this._dimensions.device.cell.height, |
| 266 |
this._cursorFloat |
| 267 |
); |
| 268 |
} |
| 269 |
if (cursor.style === 'underline' || cursor.style === 'outline') { |
| 270 |
// Bottom edge |
| 271 |
offset = rectangleCount++ * INDICES_PER_RECTANGLE; |
| 272 |
this._addRectangleFloat( |
| 273 |
vertices.attributes, |
| 274 |
offset, |
| 275 |
cursor.x * this._dimensions.device.cell.width, |
| 276 |
(cursor.y + 1) * this._dimensions.device.cell.height - cursor.dpr, |
| 277 |
cursor.width * this._dimensions.device.cell.width, |
| 278 |
cursor.dpr, |
| 279 |
this._cursorFloat |
| 280 |
); |
| 281 |
} |
| 282 |
if (cursor.style === 'outline') { |
| 283 |
// Top edge |
| 284 |
offset = rectangleCount++ * INDICES_PER_RECTANGLE; |
| 285 |
this._addRectangleFloat( |
| 286 |
vertices.attributes, |
| 287 |
offset, |
| 288 |
cursor.x * this._dimensions.device.cell.width, |
| 289 |
cursor.y * this._dimensions.device.cell.height, |
| 290 |
cursor.width * this._dimensions.device.cell.width, |
| 291 |
cursor.dpr, |
| 292 |
this._cursorFloat |
| 293 |
); |
| 294 |
// Right edge |
| 295 |
offset = rectangleCount++ * INDICES_PER_RECTANGLE; |
| 296 |
this._addRectangleFloat( |
| 297 |
vertices.attributes, |
| 298 |
offset, |
| 299 |
(cursor.x + cursor.width) * this._dimensions.device.cell.width - cursor.dpr, |
| 300 |
cursor.y * this._dimensions.device.cell.height, |
| 301 |
cursor.dpr, |
| 302 |
this._dimensions.device.cell.height, |
| 303 |
this._cursorFloat |
| 304 |
); |
| 305 |
} |
| 306 |
|
| 307 |
vertices.count = rectangleCount; |
| 308 |
} |
| 309 |
|
| 310 |
private _updateRectangle(vertices: Vertices, offset: number, fg: number, bg: number, startX: number, endX: number, y: number): void { |
| 311 |
if (fg & FgFlags.INVERSE) { |
| 312 |
switch (fg & Attributes.CM_MASK) { |
| 313 |
case Attributes.CM_P16: |
| 314 |
case Attributes.CM_P256: |
| 315 |
$rgba = this._themeService.colors.ansi[fg & Attributes.PCOLOR_MASK].rgba; |
| 316 |
break; |
| 317 |
case Attributes.CM_RGB: |
| 318 |
$rgba = (fg & Attributes.RGB_MASK) << 8; |
| 319 |
break; |
| 320 |
case Attributes.CM_DEFAULT: |
| 321 |
default: |
| 322 |
$rgba = this._themeService.colors.foreground.rgba; |
| 323 |
} |
| 324 |
} else { |
| 325 |
switch (bg & Attributes.CM_MASK) { |
| 326 |
case Attributes.CM_P16: |
| 327 |
case Attributes.CM_P256: |
| 328 |
$rgba = this._themeService.colors.ansi[bg & Attributes.PCOLOR_MASK].rgba; |
| 329 |
break; |
| 330 |
case Attributes.CM_RGB: |
| 331 |
$rgba = (bg & Attributes.RGB_MASK) << 8; |
| 332 |
break; |
| 333 |
case Attributes.CM_DEFAULT: |
| 334 |
default: |
| 335 |
$rgba = this._themeService.colors.background.rgba; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
if (vertices.attributes.length < offset + 4) { |
| 340 |
vertices.attributes = expandFloat32Array(vertices.attributes, this._terminal.rows * this._terminal.cols * INDICES_PER_RECTANGLE); |
| 341 |
} |
| 342 |
$x1 = startX * this._dimensions.device.cell.width; |
| 343 |
$y1 = y * this._dimensions.device.cell.height; |
| 344 |
$r = (($rgba >> 24) & 0xFF) / 255; |
| 345 |
$g = (($rgba >> 16) & 0xFF) / 255; |
| 346 |
$b = (($rgba >> 8 ) & 0xFF) / 255; |
| 347 |
$a = 1; |
| 348 |
|
| 349 |
this._addRectangle(vertices.attributes, offset, $x1, $y1, (endX - startX) * this._dimensions.device.cell.width, this._dimensions.device.cell.height, $r, $g, $b, $a); |
| 350 |
} |
| 351 |
|
| 352 |
private _addRectangle(array: Float32Array, offset: number, x1: number, y1: number, width: number, height: number, r: number, g: number, b: number, a: number): void { |
| 353 |
array[offset ] = x1 / this._dimensions.device.canvas.width; |
| 354 |
array[offset + 1] = y1 / this._dimensions.device.canvas.height; |
| 355 |
array[offset + 2] = width / this._dimensions.device.canvas.width; |
| 356 |
array[offset + 3] = height / this._dimensions.device.canvas.height; |
| 357 |
array[offset + 4] = r; |
| 358 |
array[offset + 5] = g; |
| 359 |
array[offset + 6] = b; |
| 360 |
array[offset + 7] = a; |
| 361 |
} |
| 362 |
|
| 363 |
private _addRectangleFloat(array: Float32Array, offset: number, x1: number, y1: number, width: number, height: number, color: Float32Array): void { |
| 364 |
array[offset ] = x1 / this._dimensions.device.canvas.width; |
| 365 |
array[offset + 1] = y1 / this._dimensions.device.canvas.height; |
| 366 |
array[offset + 2] = width / this._dimensions.device.canvas.width; |
| 367 |
array[offset + 3] = height / this._dimensions.device.canvas.height; |
| 368 |
array[offset + 4] = color[0]; |
| 369 |
array[offset + 5] = color[1]; |
| 370 |
array[offset + 6] = color[2]; |
| 371 |
array[offset + 7] = color[3]; |
| 372 |
} |
| 373 |
|
| 374 |
private _colorToFloat32Array(color: IColor): Float32Array { |
| 375 |
return new Float32Array([ |
| 376 |
((color.rgba >> 24) & 0xFF) / 255, |
| 377 |
((color.rgba >> 16) & 0xFF) / 255, |
| 378 |
((color.rgba >> 8 ) & 0xFF) / 255, |
| 379 |
((color.rgba ) & 0xFF) / 255 |
| 380 |
]); |
| 381 |
} |
| 382 |
} |