Rework output/terminal_ncurses.c to be more performant

By scanning through the updates row-by-row rather than column-by-column,
a lot of color-changing updates are saved when using a gradient. This
seems to significantly reduce tearing when running Cava through urxvt on
an X11 server.
master
Tudor Brindus 6 years ago
parent db7f8d1ccb
commit 0c93dfdf45
  1. 206
      output/terminal_ncurses.c

@ -71,79 +71,79 @@ char* const bg_color_string, int predef_fg_color, int predef_bg_color, int gradi
start_color(); start_color();
use_default_colors(); use_default_colors();
getmaxyx(stdscr, *height, *width); getmaxyx(stdscr, *height, *width);
clear(); clear();
NCURSES_COLOR_T color_pair_number = 1; NCURSES_COLOR_T color_pair_number = 1;
NCURSES_COLOR_T bg_color_number; NCURSES_COLOR_T bg_color_number;
bg_color_number = change_color_definition(0, bg_color_string, predef_bg_color); bg_color_number = change_color_definition(0, bg_color_string, predef_bg_color);
if (!gradient) { if (!gradient) {
NCURSES_COLOR_T fg_color_number; NCURSES_COLOR_T fg_color_number;
fg_color_number = change_color_definition(1, fg_color_string, predef_fg_color); fg_color_number = change_color_definition(1, fg_color_string, predef_fg_color);
init_pair(color_pair_number, fg_color_number, bg_color_number); init_pair(color_pair_number, fg_color_number, bg_color_number);
} else if (gradient) { } else if (gradient) {
// 0 -> col1, 1-> col1<=>col2, 2 -> col2 and so on // 0 -> col1, 1-> col1<=>col2, 2 -> col2 and so on
short unsigned int rgb[2 * gradient_count - 1][3]; short unsigned int rgb[2 * gradient_count - 1][3];
char next_color[14]; char next_color[14];
gradient_size = *height; gradient_size = *height;
if (gradient_size > COLORS) gradient_size = COLORS - 1; if (gradient_size > COLORS) gradient_size = COLORS - 1;
if (gradient_size > COLOR_PAIRS) gradient_size = COLOR_PAIRS - 1; if (gradient_size > COLOR_PAIRS) gradient_size = COLOR_PAIRS - 1;
if (gradient_size > MAX_COLOR_REDEFINITION) gradient_size = MAX_COLOR_REDEFINITION - 1; if (gradient_size > MAX_COLOR_REDEFINITION) gradient_size = MAX_COLOR_REDEFINITION - 1;
for(int i = 0;i < gradient_count;i++){ for(int i = 0;i < gradient_count;i++){
int col = (i + 1)*2 - 2; int col = (i + 1)*2 - 2;
sscanf(gradient_colors[i]+1, "%02hx%02hx%02hx", &rgb[col][0], &rgb[col][1], &rgb[col][2]); sscanf(gradient_colors[i]+1, "%02hx%02hx%02hx", &rgb[col][0], &rgb[col][1], &rgb[col][2]);
} }
//sscanf(gradient_color_1 + 1, "%02hx%02hx%02hx", &rgb[0][0], &rgb[0][1], &rgb[0][2]); //sscanf(gradient_color_1 + 1, "%02hx%02hx%02hx", &rgb[0][0], &rgb[0][1], &rgb[0][2]);
//sscanf(gradient_color_2 + 1, "%02hx%02hx%02hx", &rgb[1][0], &rgb[1][1], &rgb[1][2]); //sscanf(gradient_color_2 + 1, "%02hx%02hx%02hx", &rgb[1][0], &rgb[1][1], &rgb[1][2]);
int individual_size = gradient_size/(gradient_count - 1); int individual_size = gradient_size/(gradient_count - 1);
int row = 0; int row = 0;
for(int i = 0;i < gradient_count - 1;i++){ for(int i = 0;i < gradient_count - 1;i++){
int col = (i + 1)* 2 - 2; int col = (i + 1)* 2 - 2;
if(i == gradient_count - 1) if(i == gradient_count - 1)
col = 2*(gradient_count - 1) - 2; col = 2*(gradient_count - 1) - 2;
for(int j = 0; j < individual_size;j++){ for(int j = 0; j < individual_size;j++){
for(int k = 0; k < 3; k++) { for(int k = 0; k < 3; k++) {
rgb[col+1][k] = rgb[col][k] + (rgb[col+2][k] - rgb[col][k]) * (j / (individual_size * 0.85)); rgb[col+1][k] = rgb[col][k] + (rgb[col+2][k] - rgb[col][k]) * (j / (individual_size * 0.85));
if (rgb[col+1][k] > 255) rgb[col][k] = 0; if (rgb[col+1][k] > 255) rgb[col][k] = 0;
if ( j > individual_size * 0.85 ) rgb[col+1][k] = rgb[col+2][k]; if ( j > individual_size * 0.85 ) rgb[col+1][k] = rgb[col+2][k];
} }
sprintf(next_color,"#%02x%02x%02x",rgb[col+1][0], rgb[col+1][1], rgb[col+1][2]); sprintf(next_color,"#%02x%02x%02x",rgb[col+1][0], rgb[col+1][1], rgb[col+1][2]);
change_color_definition(row + 1, next_color, row + 1); change_color_definition(row + 1, next_color, row + 1);
init_pair(color_pair_number++, row + 1, bg_color_number); init_pair(color_pair_number++, row + 1, bg_color_number);
row++; row++;
} }
} }
int left = individual_size * (gradient_count - 1); int left = individual_size * (gradient_count - 1);
int col = 2*(gradient_count) - 2; int col = 2*(gradient_count) - 2;
while(left < gradient_size){ while(left < gradient_size){
sprintf(next_color,"#%02x%02x%02x",rgb[col][0], rgb[col][1], rgb[col][2]); sprintf(next_color,"#%02x%02x%02x",rgb[col][0], rgb[col][1], rgb[col][2]);
change_color_definition(row + 1, next_color, row + 1); change_color_definition(row + 1, next_color, row + 1);
init_pair(color_pair_number++, row + 1, bg_color_number); init_pair(color_pair_number++, row + 1, bg_color_number);
row++; row++;
left++; left++;
} }
} }
if (bg_color_number != -1) if (bg_color_number != -1)
bkgd(COLOR_PAIR(color_pair_number)); bkgd(COLOR_PAIR(color_pair_number));
@ -153,16 +153,16 @@ char* const bg_color_string, int predef_fg_color, int predef_bg_color, int gradi
} }
void change_colors(int cur_height, int tot_height) { void change_colors(int cur_height, int tot_height) {
tot_height /= gradient_size ; tot_height /= gradient_size ;
if (tot_height < 1) tot_height = 1; if (tot_height < 1) tot_height = 1;
cur_height /= tot_height; cur_height /= tot_height;
if (cur_height > gradient_size - 1) cur_height = gradient_size - 1; if (cur_height > gradient_size - 1) cur_height = gradient_size - 1;
attron(COLOR_PAIR(cur_height + 1)); attron(COLOR_PAIR(cur_height + 1));
} }
void get_terminal_dim_ncurses(int* width, int* height) { void get_terminal_dim_ncurses(int* width, int* height) {
getmaxyx(stdscr, *height, *width); getmaxyx(stdscr, *height, *width);
gradient_size = *height; gradient_size = *height;
clear(); // clearing in case of resieze clear(); // clearing in case of resieze
} }
@ -171,10 +171,10 @@ void get_terminal_dim_ncurses(int* width, int* height) {
int draw_terminal_ncurses(int is_tty, int terminal_height, int terminal_width, int draw_terminal_ncurses(int is_tty, int terminal_height, int terminal_width,
int bars_count, int bar_width, int bar_spacing, int rest, const int f[200], int bars_count, int bar_width, int bar_spacing, int rest, const int f[200],
int flastd[200], int gradient) { int flastd[200], int gradient) {
const int height = terminal_height - 1;
const wchar_t* bar_heights[] = {L"\u2581", L"\u2582", L"\u2583", const wchar_t* bar_heights[] = {L"\u2581", L"\u2582", L"\u2583",
L"\u2584", L"\u2585", L"\u2586", L"\u2587", L"\u2588"}; L"\u2584", L"\u2585", L"\u2586", L"\u2587", L"\u2588"};
#define LAST ((sizeof(bar_heights) / sizeof(bar_heights[0])) - 1) int num_bar_heights = (sizeof(bar_heights) / sizeof(bar_heights[0]));
// output: check if terminal has been resized // output: check if terminal has been resized
if (!is_tty) { if (!is_tty) {
@ -182,54 +182,62 @@ int flastd[200], int gradient) {
return TERMINAL_RESIZED; return TERMINAL_RESIZED;
} }
} }
const int height = terminal_height - 1;
#define CURRENT_COLUMN bar*bar_width + width + bar*bar_spacing + rest // Compute how much of the screen we possibly need to update ahead-of-time.
int max_update_y = 0;
for (int bar = 0; bar < bars_count; bar++) { for (int bar = 0; bar < bars_count; bar++) {
if (f[bar] > flastd[bar]) { // higher then last frame max_update_y = max(max_update_y, max(f[bar], flastd[bar]));
if (is_tty) { }
for (int n = flastd[bar] / 8; n < f[bar] / 8; n++) {
if (gradient) change_colors(n, height); max_update_y = (max_update_y + num_bar_heights) / num_bar_heights;
for (int width = 0; width < bar_width; width++)
mvaddch((height - n), CURRENT_COLUMN, '8'); for (int y = 0; y < max_update_y; y++) {
} if (gradient) {
} else { change_colors(y, height);
for (int n = flastd[bar] / 8; n < f[bar] / 8; n++) { }
if (gradient) change_colors(n, height);
for (int width = 0; width < bar_width; width++) for (int bar = 0; bar < bars_count; bar++) {
mvaddwstr((height - n), CURRENT_COLUMN, if (f[bar] == flastd[bar]) {
bar_heights[LAST]); continue;
}
} }
if (gradient) change_colors(f[bar] / 8, height);
if (f[bar] % 8) { int cur_col = bar * bar_width + bar * bar_spacing + rest;
if (is_tty) { int f_cell = f[bar] / num_bar_heights;
for (int width = 0; width < bar_width; width++) int f_last_cell = flastd[bar] / num_bar_heights;
mvaddch((height - f[bar] / 8), CURRENT_COLUMN, '0' + (f[bar] % 8));
if (f_cell >= y) {
int bar_step;
if (f_cell == y) {
// The "cap" of the bar occurs at this [y].
bar_step = f[bar] % num_bar_heights;
} else if (f_last_cell <= y) {
// The bar is full at this [y].
bar_step = num_bar_heights - 1;
} else { } else {
for (int width = 0; width < bar_width; width++) // No update necessary since last frame.
mvaddwstr((height - f[bar] / 8), CURRENT_COLUMN, continue;
bar_heights[(f[bar] % 8) - 1]);
} }
}
} else if(f[bar] < flastd[bar]) { // lower then last frame for (int col = cur_col, i = 0; i < bar_width; i++, col++) {
for (int n = f[bar] / 8; n < flastd[bar]/8 + 1; n++) if (is_tty) {
for (int width = 0; width < bar_width; width++) mvaddch(height - y, col, '0' + bar_step);
mvaddstr((height - n), CURRENT_COLUMN, " "); } else {
if (f[bar] % 8) { mvaddwstr(height - y, col, bar_heights[bar_step]);
if (gradient) change_colors(f[bar] / 8, height); }
if (is_tty) { }
for (int width = 0; width < bar_width; width++) } else if (f_last_cell >= y) {
mvaddch((height - f[bar] / 8), CURRENT_COLUMN, '0' + (f[bar] % 8)); // This bar was taller during the last frame than during this frame, so
} else { // clear the excess characters.
for (int width = 0; width < bar_width; width++) for (int col = cur_col, i = 0; i < bar_width; i++, col++) {
mvaddwstr((height - f[bar] / 8), CURRENT_COLUMN, mvaddch(height - y, col, ' ');
bar_heights[(f[bar] % 8) - 1]);
} }
} }
} }
#undef CURRENT_COLUMN
flastd[bar] = f[bar]; // memory for falloff func
} }
memcpy(flastd, f, sizeof(*f)); // Memory for falloff func
refresh(); refresh();
return 0; return 0;
} }
@ -253,6 +261,6 @@ void cleanup_terminal_ncurses(void) {
*/ */
endwin(); endwin();
system("clear"); system("clear");
system("reset"); system("reset");
} }

Loading…
Cancel
Save