Model renditions explicitly -- 25% speedup

This commit is contained in:
Keith Winstein
2011-02-05 18:09:19 -05:00
parent 2f964f80f4
commit e0065bfe5c
7 changed files with 92 additions and 90 deletions
-2
View File
@@ -21,5 +21,3 @@ template class vector<wchar_t>;
template class vector<int>; template class vector<int>;
template class map<string, Function>; template class map<string, Function>;
template class vector<bool>; template class vector<bool>;
template class list<int>;
template void std::list<int, std::allocator<int> >::remove_if<bool (*)(int const&)>(bool (*)(int const&));
+1 -9
View File
@@ -152,15 +152,7 @@ void Emulator::debug_printout( int fd )
Cell *cell = fb.get_cell( y, x ); Cell *cell = fb.get_cell( y, x );
/* print renditions */ /* print renditions */
screen.append( "\033[0" ); screen.append( cell->renditions.sgr() );
char rendition[ 32 ];
for ( std::list<int>::iterator i = cell->renditions.begin();
i != cell->renditions.end();
i++ ) {
snprintf( rendition, 32, ";%d", *i );
screen.append( rendition );
}
screen.append( "m" );
/* clear cell */ /* clear cell */
screen.append( "\033[X" ); screen.append( "\033[X" );
+2 -2
View File
@@ -16,12 +16,12 @@ namespace Terminal {
private: private:
bool initialized; bool initialized;
Framebuffer last_frame; Framebuffer last_frame;
std::list<int> current_renditions; std::string current_rendition_string;
public: public:
Display( int width, int height ) Display( int width, int height )
: initialized( false ), last_frame( width, height ), : initialized( false ), last_frame( width, height ),
current_renditions() current_rendition_string()
{} {}
std::string new_frame( Framebuffer &f ); std::string new_frame( Framebuffer &f );
+6 -18
View File
@@ -39,11 +39,10 @@ std::string Display::new_frame( Framebuffer &f )
|| (f.ds.get_width() != last_frame.ds.get_width()) || (f.ds.get_width() != last_frame.ds.get_width())
|| (f.ds.get_height() != last_frame.ds.get_height()) ) { || (f.ds.get_height() != last_frame.ds.get_height()) ) {
/* clear screen */ /* clear screen */
screen.append( "\033[0m;\033[H\033[2J" ); screen.append( "\033[0m\033[H\033[2J" );
initialized = false; initialized = false;
cursor_was_moved = true; cursor_was_moved = true;
current_renditions.clear(); current_rendition_string = "\033[0m";
current_renditions.push_back( 0 );
} }
int cursor_x = -1, cursor_y = -1; int cursor_x = -1, cursor_y = -1;
@@ -69,23 +68,12 @@ std::string Display::new_frame( Framebuffer &f )
cursor_x = x; cursor_x = x;
cursor_y = y; cursor_y = y;
std::list<int> cell_print_renditions; std::string rendition_str = cell->renditions.sgr();
cell_print_renditions = cell->renditions;
cell_print_renditions.insert( cell_print_renditions.begin(), 0 );
if ( cell_print_renditions != current_renditions ) { if ( current_rendition_string != rendition_str ) {
/* print renditions */ /* print renditions */
screen.append( "\033[0" ); screen.append( rendition_str );
char rendition[ 32 ]; current_rendition_string = rendition_str;
for ( std::list<int>::iterator i = cell->renditions.begin();
i != cell->renditions.end();
i++ ) {
snprintf( rendition, 32, ";%d", *i );
screen.append( rendition );
}
screen.append( "m" );
current_renditions = cell_print_renditions;
} }
if ( cell->contents.empty() ) { if ( cell->contents.empty() ) {
+52 -44
View File
@@ -9,8 +9,7 @@ void Cell::reset( void )
contents.clear(); contents.clear();
fallback = false; fallback = false;
width = 1; width = 1;
renditions.clear(); renditions = Renditions();
need_back_color_erase = true;
} }
DrawState::DrawState( int s_width, int s_height ) DrawState::DrawState( int s_width, int s_height )
@@ -200,7 +199,6 @@ std::vector<int> DrawState::get_tabs( void )
void Framebuffer::apply_renditions_to_current_cell( void ) void Framebuffer::apply_renditions_to_current_cell( void )
{ {
get_cell()->need_back_color_erase = false;
get_cell()->renditions = ds.get_renditions(); get_cell()->renditions = ds.get_renditions();
} }
@@ -297,7 +295,7 @@ void Framebuffer::soft_reset( void )
ds.cursor_visible = true; /* per xterm and gnome-terminal */ ds.cursor_visible = true; /* per xterm and gnome-terminal */
ds.application_mode_cursor_keys = false; ds.application_mode_cursor_keys = false;
ds.set_scrolling_region( 0, ds.get_height() - 1 ); ds.set_scrolling_region( 0, ds.get_height() - 1 );
ds.clear_renditions(); ds.add_rendition( 0 );
ds.clear_saved_cursor(); ds.clear_saved_cursor();
} }
@@ -344,21 +342,6 @@ void DrawState::resize( int s_width, int s_height )
} }
} }
int DrawState::get_background_rendition( void )
{
int color = -1;
for ( std::list<int>::iterator i = renditions.begin();
i != renditions.end();
i++ ) {
int r = *i;
if ( (40 <= r) && (r <= 49) ) {
color = r;
}
}
return color;
}
void Framebuffer::back_color_erase( void ) void Framebuffer::back_color_erase( void )
{ {
int bg_color = ds.get_background_rendition(); int bg_color = ds.get_background_rendition();
@@ -366,40 +349,65 @@ void Framebuffer::back_color_erase( void )
for ( int row = 0; row < ds.get_height(); row++ ) { for ( int row = 0; row < ds.get_height(); row++ ) {
for ( int col = 0; col < ds.get_width(); col++ ) { for ( int col = 0; col < ds.get_width(); col++ ) {
Cell *cell = get_cell( row, col ); Cell *cell = get_cell( row, col );
if ( cell->need_back_color_erase ) { if ( cell->renditions.background_color == -1 ) {
// assert( cell->renditions.empty() ); cell->renditions.background_color = bg_color;
if ( bg_color > 0 ) {
cell->renditions.push_back( bg_color );
}
cell->need_back_color_erase = false;
} }
} }
} }
} }
static bool fg_colorval( const int &x ) { return (30 <= x) && (x <= 39); } Renditions::Renditions()
static bool bg_colorval( const int &x ) { return (40 <= x) && (x <= 49); } : bold( false ), underlined( false ), blink( false ),
inverse( false ), invisible( false ), foreground_color( 0 ),
background_color( -1 )
{}
void DrawState::add_rendition( int x ) void Renditions::set_rendition( int num )
{ {
/* Filter out older renditions that we know if ( num == 0 ) {
will now be reset */ bold = underlined = blink = inverse = invisible = false;
foreground_color = background_color = 0;
renditions.remove( x ); return;
switch ( x ) {
case 1: case 22: renditions.remove( 1 ); renditions.remove( 22 ); break; /* bold */
case 4: case 24: renditions.remove( 4 ); renditions.remove( 24 ); break; /* underlined */
case 5: case 25: renditions.remove( 5 ); renditions.remove( 25 ); break; /* blink */
case 7: case 27: renditions.remove( 7 ); renditions.remove( 27 ); break; /* inverse */
case 8: case 28: renditions.remove( 8 ); renditions.remove( 28 ); break; /* invisible */
} }
if ( (30 <= x) && (x <= 39) ) { /* foreground color */ if ( (30 <= num) && (num <= 39) ) { /* foreground color */
renditions.remove_if( fg_colorval ); foreground_color = num;
} else if ( (40 <= x) && (x <= 49) ) { /* background color */ return;
renditions.remove_if( bg_colorval ); } else if ( (40 <= num) && (num <= 49) ) { /* background color */
background_color = num;
return;
} }
renditions.push_back( x ); switch ( num ) {
case 1: case 22: bold = (num == 1); break;
case 4: case 24: underlined = (num == 4); break;
case 5: case 25: blink = (num == 5); break;
case 7: case 27: inverse = (num == 7); break;
case 8: case 28: invisible = (num == 8); break;
}
}
std::string Renditions::sgr( void )
{
std::string ret;
ret.append( "\033[0" );
if ( bold ) ret.append( ";1" );
if ( underlined ) ret.append( ";4" );
if ( blink ) ret.append( ";5" );
if ( inverse ) ret.append( ";7" );
if ( invisible ) ret.append( ";8" );
if ( foreground_color ) {
char col[ 8 ];
snprintf( col, 8, ";%d", foreground_color );
ret.append( col );
}
if ( background_color ) {
char col[ 8 ];
snprintf( col, 8, ";%d", background_color );
ret.append( col );
}
ret.append( "m" );
return ret;
} }
+30 -10
View File
@@ -9,20 +9,41 @@
/* Terminal framebuffer */ /* Terminal framebuffer */
namespace Terminal { namespace Terminal {
class Renditions {
public:
bool bold, underlined, blink, inverse, invisible;
int foreground_color;
int background_color;
Renditions();
void set_rendition( int num );
std::string sgr( void );
void back_color_erase( int num )
{
if ( background_color == -1 ) background_color = num;
}
bool operator==( const Renditions &x )
{
return (bold == x.bold) && (underlined == x.underlined)
&& (blink == x.blink) && (inverse == x.inverse)
&& (invisible == x.invisible) && (foreground_color == x.foreground_color)
&& (background_color == x.background_color);
}
};
class Cell { class Cell {
public: public:
std::vector<wchar_t> contents; std::vector<wchar_t> contents;
char fallback; /* first character is combining character */ char fallback; /* first character is combining character */
int width; int width;
std::list<int> renditions; /* e.g., bold, blinking, etc. */ Renditions renditions;
bool need_back_color_erase;
Cell() Cell()
: contents(), : contents(),
fallback( false ), fallback( false ),
width( 1 ), width( 1 ),
renditions(), renditions()
need_back_color_erase( true )
{} {}
void reset( void ); void reset( void );
@@ -51,7 +72,7 @@ namespace Terminal {
class SavedCursor { class SavedCursor {
public: public:
int cursor_col, cursor_row; int cursor_col, cursor_row;
std::list<int> renditions; Renditions renditions;
/* character set shift state */ /* character set shift state */
bool auto_wrap_mode; bool auto_wrap_mode;
bool origin_mode; bool origin_mode;
@@ -74,7 +95,7 @@ namespace Terminal {
int scrolling_region_top_row, scrolling_region_bottom_row; int scrolling_region_top_row, scrolling_region_bottom_row;
std::list<int> renditions; Renditions renditions;
SavedCursor save; SavedCursor save;
@@ -114,10 +135,9 @@ namespace Terminal {
int limit_top( void ); int limit_top( void );
int limit_bottom( void ); int limit_bottom( void );
void clear_renditions( void ) { renditions.clear(); } void add_rendition( int x ) { renditions.set_rendition( x ); }
void add_rendition( int x ); Renditions get_renditions( void ) { return renditions; }
const std::list<int> get_renditions( void ) { return renditions; } int get_background_rendition( void ) { return renditions.background_color; }
int get_background_rendition( void );
void save_cursor( void ); void save_cursor( void );
void restore_cursor( void ); void restore_cursor( void );
-4
View File
@@ -322,12 +322,8 @@ void CSI_SGR( Framebuffer *fb, Dispatcher *dispatch )
bce = true; bce = true;
} }
if ( rendition == 0 ) {
fb->ds.clear_renditions();
} else {
fb->ds.add_rendition( rendition ); fb->ds.add_rendition( rendition );
} }
}
} }
static Function func_CSI_SGR( CSI, "m", CSI_SGR ); static Function func_CSI_SGR( CSI, "m", CSI_SGR );