Skip to content

Commit

Permalink
Initial working version of new memory optimizations
Browse files Browse the repository at this point in the history
Very fragile right now, only works for basic IO

Part of xtermjs#791
  • Loading branch information
Tyriar committed Jul 15, 2017
1 parent 59d7cd5 commit 06ff1da
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
45 changes: 45 additions & 0 deletions src/CharAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @license MIT
*/

const DEFAULT_COLOR = 1 << 24;

export class CharAttributes {
constructor(
public x1: number,
public y1: number,
public x2: number,
public y2: number,
// TODO: Int32Array (Maybe Int8?)
// TODO: This should be private
public _data: [number, number, number]
) {
}

public get flags(): number {
return this._data[0];
}

public get isDefaultFg(): boolean {
return this._data[1] === DEFAULT_COLOR;
}

public get truecolorFg(): string {
return '#' + this._padLeft(this._data[1].toString(16), '0');
}

public get isDefaultBg(): boolean {
return this._data[2] === DEFAULT_COLOR;
}

public get truecolorBg(): string {
return '#' + this._padLeft(this._data[2].toString(16), '0');
}

private _padLeft(text: string, padChar: string): string {
while (text.length < 6) {
text = padChar + text;
}
return text;
}
}
8 changes: 8 additions & 0 deletions src/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { IInputHandler, ITerminal } from './Interfaces';
import { C0 } from './EscapeSequences';
import { DEFAULT_CHARSET } from './Charsets';
import { CharAttributes } from "./CharAttributes";

/**
* The terminal's standard implementation of IInputHandler, this handles all
Expand Down Expand Up @@ -1219,6 +1220,8 @@ export class InputHandler implements IInputHandler {
public charAttributes(params: number[]): void {
// Optimize a single SGR0.
if (params.length === 1 && params[0] === 0) {
this._terminal.finalizeCharAttributes();
console.log('Current char attr list:', this._terminal.charAttributes);
this._terminal.currentFlags = this._terminal.defaultFlags;
this._terminal.currentFgColor = this._terminal.defaultFgColor;
this._terminal.currentBgColor = this._terminal.defaultBgColor;
Expand Down Expand Up @@ -1326,6 +1329,11 @@ export class InputHandler implements IInputHandler {
this._terminal.error('Unknown SGR attribute: %d.', p);
}
}

this._terminal.finalizeCharAttributes();
this._terminal.currentCharAttributes = new CharAttributes(this._terminal.x, this._terminal.ybase + this._terminal.y, null, null, [this._terminal.currentFlags, this._terminal.currentFgColor, this._terminal.currentBgColor]);
this._terminal.charAttributes.push(this._terminal.currentCharAttributes);
console.log('Creating new style attr:', this._terminal.currentCharAttributes);
}

/**
Expand Down
55 changes: 45 additions & 10 deletions src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { ITerminal } from './Interfaces';
import { DomElementObjectPool } from './utils/DomElementObjectPool';
import { CharAttributes } from "./CharAttributes";

/**
* The maximum number of refresh frames to skip when the write buffer is non-
Expand Down Expand Up @@ -123,6 +124,17 @@ export class Renderer {
end = this._terminal.rows - 1;
}

let nextCharAttributeIndex: number = -1;
let currentCharAttributes: CharAttributes;
for (let i = 0; i < (<any>this._terminal).charAttributes.length; i++) {
const charAttribute = (<any>this._terminal).charAttributes[i];
if (charAttribute.y1 === start + this._terminal.ydisp || charAttribute.y2 >= start + this._terminal.ydisp) {
nextCharAttributeIndex = i;
console.log(`initial char attributes index:`, nextCharAttributeIndex);
break;
}
}

for (let y = start; y <= end; y++) {
let row = y + this._terminal.ydisp;
let line = this._terminal.lines.get(row);
Expand Down Expand Up @@ -161,6 +173,28 @@ export class Renderer {
continue;
}

if (currentCharAttributes && currentCharAttributes.x2 === i && currentCharAttributes.y2 === y + this._terminal.ydisp) {
currentCharAttributes = null;
nextCharAttributeIndex++;
if (nextCharAttributeIndex === (<any>this._terminal).charAttributes.length) {
nextCharAttributeIndex = -1;
}
}
if (nextCharAttributeIndex !== -1 &&
(<any>this._terminal).charAttributes[nextCharAttributeIndex].x1 === i &&
(<any>this._terminal).charAttributes[nextCharAttributeIndex].y1 === y + this._terminal.ydisp) {
currentCharAttributes = (<any>this._terminal).charAttributes[nextCharAttributeIndex];
console.log(`current char attributes ${i},${y}:`, currentCharAttributes);
}

// TODO: This is temporary to test new method
if (currentCharAttributes) {
flags = currentCharAttributes.flags;
// v Temporary v
fg = currentCharAttributes._data[1];
bg = currentCharAttributes._data[2];
}

// Force a refresh if the character is the cursor
const IS_CURSOR = -1;
if (i === cursorIndex) {
Expand Down Expand Up @@ -253,11 +287,12 @@ export class Renderer {

if (fg !== this._terminal.defaultFgColor) {
if (isTrueColorFg) {
let rgb = fg.toString(16);
while (rgb.length < 6) {
rgb = '0' + rgb;
}
currentElement.style.color = `#${rgb}`;
// let rgb = fg.toString(16);
// while (rgb.length < 6) {
// rgb = '0' + rgb;
// }
// currentElement.style.color = `#${rgb}`;
currentElement.style.color = currentCharAttributes.truecolorFg;
} else {
if (fg < 256) {
currentElement.classList.add(`xterm-color-${fg}`);
Expand All @@ -267,11 +302,11 @@ export class Renderer {

if (bg !== this._terminal.defaultBgColor) {
if (isTrueColorBg) {
let rgb = bg.toString(16);
while (rgb.length < 6) {
rgb = '0' + rgb;
}
currentElement.style.backgroundColor = `#${rgb}`;
// let rgb = bg.toString(16);
// while (rgb.length < 6) {
// rgb = '0' + rgb;
// }
currentElement.style.backgroundColor = currentCharAttributes.truecolorBg;
} else {
if (bg < 256) {
currentElement.classList.add(`xterm-bg-color-${bg}`);
Expand Down
12 changes: 11 additions & 1 deletion src/xterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ function Terminal(options) {
this.currentFlags = this.defaultFlags;
this.currentFgColor = this.defaultFgColor;
this.currentBgColor = this.defaultBgColor;

this.currentCharAttributes = null;
this.charAttributes = [];

this.params = [];
this.currentParam = 0;
Expand Down Expand Up @@ -1231,6 +1232,15 @@ Terminal.prototype.scroll = function(isWrapped) {
this.emit('scroll', this.ydisp);
};

Terminal.prototype.finalizeCharAttributes = function() {
if (!this.currentCharAttributes) {
return;
}
this.currentCharAttributes.x2 = this.x;
this.currentCharAttributes.y2 = this.ybase + this.y;
this.currentCharAttributes = null;
}

/**
* Scroll the display of the terminal
* @param {number} disp The number of lines to scroll down (negatives scroll up).
Expand Down

0 comments on commit 06ff1da

Please sign in to comment.