aboutsummaryrefslogtreecommitdiff
path: root/render.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-07-09 07:24:26 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-07-09 07:24:26 +0000
commit8cf7d7f7aba8f4324a66735b941ff3649c1901d1 (patch)
tree3e3c3b4e6307d614c49036a6b8a3e7b897aaf489 /render.c
parentcell: packing the cell struct seems to help performance (diff)
parentrender: store frame callback pointer in terminal struct (diff)
Merge branch 'no-more-linear'
Diffstat (limited to 'render.c')
-rw-r--r--render.c493
1 files changed, 191 insertions, 302 deletions
diff --git a/render.c b/render.c
index 0b833376..d42a04ec 100644
--- a/render.c
+++ b/render.c
@@ -10,6 +10,7 @@
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "shm.h"
+#include "grid.h"
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
@@ -27,227 +28,93 @@ struct glyph_sequence {
int count;
struct attributes attrs;
- struct rgba foreground;
+ struct rgb foreground;
};
static struct glyph_sequence gseq;
static void
-grid_render_update(struct terminal *term, struct buffer *buf, const struct damage *dmg)
+render_cell(struct terminal *term, struct buffer *buf, const struct cell *cell,
+ int col, int row)
{
- LOG_DBG("damage: UPDATE: %d -> %d (offset = %d)",
- (dmg->range.start - term->grid->offset) % term->grid->size,
- (dmg->range.start - term->grid->offset) % term->grid->size + dmg->range.length,
- term->grid->offset);
+ /* Cursor here? */
+ bool has_cursor
+ = (!term->hide_cursor &&
+ (term->cursor.col == col && term->cursor.row == row));
- int start = dmg->range.start;
- int length = dmg->range.length;
+ double width = term->cell_width;
+ double height = term->cell_height;
+ double x = col * width;
+ double y = row * height;
- if (start < term->grid->offset) {
- int end = start + length;
- if (end >= term->grid->offset) {
- start = term->grid->offset;
- length = end - start;
- } else
- return;
- }
-
- const int cols = term->cols;
-
- for (int linear_cursor = start,
- row = ((start - term->grid->offset) % term->grid->size) / cols,
- col = ((start - term->grid->offset) % term->grid->size) % cols;
- linear_cursor < start + length;
- linear_cursor++,
- col = col + 1 >= term->cols ? 0 : col + 1,
- row += col == 0 ? 1 : 0)
- {
-
- assert(row >= 0);
- assert(row < term->rows);
- assert(col >= 0);
- assert(col < term->cols);
-
- int cell_idx = linear_cursor % term->grid->size;
- if (cell_idx < 0)
- cell_idx += term->grid->size;
-
- assert(cell_idx >= 0);
- assert(cell_idx < term->rows * term->cols);
-
- const struct cell *cell = &term->grid->cells[cell_idx];
-
- /* Cursor here? */
- bool has_cursor
- = (!term->hide_cursor &&
- (term->cursor.linear == linear_cursor - term->grid->offset));
-
- int x = col * term->cell_width;
- int y = row * term->cell_height;
- int width = term->cell_width;
- int height = term->cell_height;
-
- struct rgba foreground = cell->attrs.have_foreground
- ? cell->attrs.foreground
- : !term->reverse ? term->foreground : term->background;
- struct rgba background = cell->attrs.have_background
- ? cell->attrs.background
- : !term->reverse ? term->background : term->foreground;
-
- if (has_cursor) {
- struct rgba swap = foreground;
- foreground = background;
- background = swap;
- }
-
- if (cell->attrs.reverse) {
- struct rgba swap = foreground;
- foreground = background;
- background = swap;
- }
-
- /* Background */
- cairo_set_source_rgba(
- buf->cairo, background.r, background.g, background.b, background.a);
- cairo_rectangle(buf->cairo, x, y, width, height);
- cairo_fill(buf->cairo);
-
- if (cell->c[0] == '\0' || cell->c[0] == ' ')
- continue;
-
- if (cell->attrs.conceal)
- continue;
-
- /*
- * cairo_show_glyphs() apparently works *much* faster when
- * called once with a large array of glyphs, compared to
- * multiple calls with a single glyph.
- *
- * So, collect glyphs until cell attributes change, then we
- * 'flush' (render) the glyphs.
- */
-
- if (memcmp(&cell->attrs, &gseq.attrs, sizeof(cell->attrs)) != 0 ||
- gseq.count >= sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]) - 10 ||
- memcmp(&gseq.foreground, &foreground, sizeof(foreground)) != 0)
- {
- if (gseq.count >= sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]) - 10)
- LOG_WARN("hit glyph limit");
- cairo_set_scaled_font(buf->cairo, attrs_to_font(term, &gseq.attrs));
- cairo_set_source_rgba(
- buf->cairo, gseq.foreground.r, gseq.foreground.g,
- gseq.foreground.b, gseq.foreground.a);
-
- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);
- cairo_show_glyphs(buf->cairo, gseq.glyphs, gseq.count);
-
- gseq.g = gseq.glyphs;
- gseq.count = 0;
- gseq.attrs = cell->attrs;
- gseq.foreground = foreground;
- }
+ const struct rgb *foreground = cell->attrs.have_foreground
+ ? &cell->attrs.foreground
+ : !term->reverse ? &term->foreground : &term->background;
+ const struct rgb *background = cell->attrs.have_background
+ ? &cell->attrs.background
+ : !term->reverse ? &term->background : &term->foreground;
- int new_glyphs
- = sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]) - gseq.count;
-
- cairo_status_t status = cairo_scaled_font_text_to_glyphs(
- attrs_to_font(term, &cell->attrs), x, y + term->fextents.ascent,
- cell->c, strlen(cell->c), &gseq.g, &new_glyphs,
- NULL, NULL, NULL);
-
- if (status != CAIRO_STATUS_SUCCESS)
- continue;
-
- gseq.g += new_glyphs;
- gseq.count += new_glyphs;
- assert(gseq.count <= sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]));
+ /* If *one* is set, we reverse */
+ if (has_cursor ^ cell->attrs.reverse) {
+ const struct rgb *swap = foreground;
+ foreground = background;
+ background = swap;
}
- wl_surface_damage_buffer(
- term->wl.surface,
- 0, ((dmg->range.start - term->grid->offset) / cols) * term->cell_height,
- buf->width, (dmg->range.length + cols - 1) / cols * term->cell_height);
-}
-
-static void
-grid_render_erase(struct terminal *term, struct buffer *buf, const struct damage *dmg)
-{
- LOG_DBG("damage: ERASE: %d -> %d (offset = %d)",
- (dmg->range.start - term->grid->offset) % term->grid->size,
- (dmg->range.start - term->grid->offset) % term->grid->size + dmg->range.length,
- term->grid->offset);
-
- assert(dmg->range.start >= term->grid->offset);
-
- const struct rgba *bg = !term->reverse ?
- &term->background : &term->foreground;
+ /* Background */
+ cairo_set_source_rgb(buf->cairo, background->r, background->g, background->b);
+ cairo_rectangle(buf->cairo, x, y, width, height);
+ cairo_fill(buf->cairo);
- cairo_set_source_rgba(buf->cairo, bg->r, bg->g, bg->b, bg->a);
-
- const int cols = term->cols;
-
- int start = (dmg->range.start - term->grid->offset) % term->grid->size;
- int left = dmg->range.length;
+ if (cell->c[0] == '\0' || cell->c[0] == ' ')
+ return;
- int row = start / cols;
- int col = start % cols;
+ if (cell->attrs.conceal)
+ return;
- /* Partial initial line */
- if (col != 0) {
- int cell_count = min(left, cols - col);
+ /*
+ * cairo_show_glyphs() apparently works *much* faster when
+ * called once with a large array of glyphs, compared to
+ * multiple calls with a single glyph.
+ *
+ * So, collect glyphs until cell attributes change, then we
+ * 'flush' (render) the glyphs.
+ */
- int x = col * term->cell_width;
- int y = row * term->cell_height;
- int width = cell_count * term->cell_width;
- int height = term->cell_height;
+ if (memcmp(&cell->attrs, &gseq.attrs, sizeof(cell->attrs)) != 0 ||
+ gseq.count >= sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]) - 10 ||
+ memcmp(&gseq.foreground, foreground, sizeof(*foreground)) != 0)
+ {
+ if (gseq.count >= sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]) - 10)
+ LOG_WARN("hit glyph limit");
- cairo_rectangle(buf->cairo, x, y, width, height);
- cairo_fill(buf->cairo);
- wl_surface_damage_buffer(term->wl.surface, x, y, width, height);
+ cairo_set_scaled_font(buf->cairo, attrs_to_font(term, &gseq.attrs));
+ cairo_set_source_rgb(
+ buf->cairo, gseq.foreground.r, gseq.foreground.g,
+ gseq.foreground.b);
- start += cell_count;
- left -= cell_count;
+ cairo_show_glyphs(buf->cairo, gseq.glyphs, gseq.count);
- row = start / cols;
- col = start % cols;
+ gseq.g = gseq.glyphs;
+ gseq.count = 0;
+ gseq.attrs = cell->attrs;
+ gseq.foreground = *foreground;
}
- assert(left == 0 || col == 0);
-
- /* One or more full lines left */
- if (left >= cols) {
- int line_count = left / cols;
+ int new_glyphs
+ = sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]) - gseq.count;
- int x = 0;
- int y = row * term->cell_height;
- int width = buf->width;
- int height = line_count * term->cell_height;
+ cairo_status_t status = cairo_scaled_font_text_to_glyphs(
+ attrs_to_font(term, &cell->attrs), x, y + term->fextents.ascent,
+ cell->c, strlen(cell->c), &gseq.g, &new_glyphs,
+ NULL, NULL, NULL);
- cairo_rectangle(buf->cairo, x, y, width, height);
- cairo_fill(buf->cairo);
- wl_surface_damage_buffer(term->wl.surface, x, y, width, height);
-
- start += line_count * cols;
- left -= line_count * cols;
-
- row += line_count;
- col = 0;
- }
-
- assert(left == 0 || col == 0);
- assert(left < cols);
-
- /* Partial last line */
- if (left > 0) {
- int x = 0;
- int y = row * term->cell_height;
- int width = left * term->cell_width;
- int height = term->cell_height;
+ if (status != CAIRO_STATUS_SUCCESS)
+ return;
- cairo_rectangle(buf->cairo, x, y, width, height);
- cairo_fill(buf->cairo);
- wl_surface_damage_buffer(term->wl.surface, x, y, width, height);
- }
+ gseq.g += new_glyphs;
+ gseq.count += new_glyphs;
+ assert(gseq.count <= sizeof(gseq.glyphs) / sizeof(gseq.glyphs[0]));
}
static void
@@ -277,19 +144,6 @@ grid_render_scroll(struct terminal *term, struct buffer *buf,
wl_surface_damage_buffer(term->wl.surface, 0, dst_y, width, height);
}
-
- const int cols = term->cols;
-
- struct damage erase = {
- .type = DAMAGE_ERASE,
- .range = {
- .start = term->grid->offset + max(dmg->scroll.region.end - dmg->scroll.lines,
- dmg->scroll.region.start) * cols,
- .length = min(dmg->scroll.region.end - dmg->scroll.region.start,
- dmg->scroll.lines) * cols,
- },
- };
- grid_render_erase(term, buf, &erase);
}
static void
@@ -319,18 +173,6 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf,
wl_surface_damage_buffer(term->wl.surface, 0, dst_y, width, height);
}
-
- const int cols = term->cols;
-
- struct damage erase = {
- .type = DAMAGE_ERASE,
- .range = {
- .start = term->grid->offset + dmg->scroll.region.start * cols,
- .length = min(dmg->scroll.region.end - dmg->scroll.region.start,
- dmg->scroll.lines) * cols,
- },
- };
- grid_render_erase(term, buf, &erase);
}
static void frame_callback(
@@ -345,13 +187,6 @@ grid_render(struct terminal *term)
{
static int last_cursor;
- if (tll_length(term->grid->damage) == 0 &&
- tll_length(term->grid->scroll_damage) == 0 &&
- last_cursor == term->grid->offset + term->cursor.linear)
- {
- return;
- }
-
assert(term->width > 0);
assert(term->height > 0);
@@ -369,9 +204,9 @@ grid_render(struct terminal *term)
int rmargin_width = term->width - rmargin;
int bmargin_height = term->height - bmargin;
- const struct rgba *bg = !term->reverse ?
+ const struct rgb *bg = !term->reverse ?
&term->background : &term->foreground;
- cairo_set_source_rgba(buf->cairo, bg->r, bg->g, bg->b, bg->a);
+ cairo_set_source_rgb(buf->cairo, bg->r, bg->g, bg->b);
cairo_rectangle(buf->cairo, rmargin, 0, rmargin_width, term->height);
cairo_rectangle(buf->cairo, 0, bmargin, term->width, bmargin_height);
@@ -388,6 +223,8 @@ grid_render(struct terminal *term)
last_buf = buf;
}
+ bool all_clean = tll_length(term->grid->scroll_damage) == 0;
+
tll_foreach(term->grid->scroll_damage, it) {
switch (it->item.type) {
case DAMAGE_SCROLL:
@@ -397,11 +234,6 @@ grid_render(struct terminal *term)
case DAMAGE_SCROLL_REVERSE:
grid_render_scroll_reverse(term, buf, &it->item);
break;
-
- case DAMAGE_UPDATE:
- case DAMAGE_ERASE:
- assert(false);
- break;
}
tll_remove(term->grid->scroll_damage, it);
@@ -410,57 +242,75 @@ grid_render(struct terminal *term)
gseq.g = gseq.glyphs;
gseq.count = 0;
- tll_foreach(term->grid->damage, it) {
- switch (it->item.type) {
- case DAMAGE_ERASE: grid_render_erase(term, buf, &it->item); break;
- case DAMAGE_UPDATE: grid_render_update(term, buf, &it->item); break;
+ for (int r = 0; r < term->rows; r++) {
+ struct row *row = grid_row(term->grid, r);
- case DAMAGE_SCROLL:
- case DAMAGE_SCROLL_REVERSE:
- assert(false);
- break;
- }
+ if (!row->dirty)
+ continue;
+
+ //LOG_WARN("rendering line: %d", r);
+
+ for (int col = 0; col < term->cols; col++)
+ render_cell(term, buf, &row->cells[col], col, r);
+
+ row->dirty = false;
+ all_clean = false;
- tll_remove(term->grid->damage, it);
+ wl_surface_damage_buffer(term->wl.surface, 0, r * term->cell_height, term->width, term->cell_height);
}
/* TODO: break out to function */
/* Re-render last cursor cell and current cursor cell */
/* Make sure previous cursor is refreshed (to avoid "ghost" cursors) */
- if (last_cursor != term->cursor.linear) {
- struct damage prev_cursor = {
- .type = DAMAGE_UPDATE,
- .range = {.start = last_cursor, .length = 1},
- };
- grid_render_update(term, buf, &prev_cursor);
+ int cursor_as_linear
+ = (term->grid->offset + term->cursor.row) * term->cols + term->cursor.col;
+
+ if (last_cursor != cursor_as_linear) {
+ int row = last_cursor / term->cols - term->grid->offset;
+ int col = last_cursor % term->cols;
+ if (row >= 0 && row < term->rows) {
+ render_cell(term, buf, &grid_row(term->grid, row)->cells[col], col, row);
+ all_clean = false;
+
+ wl_surface_damage_buffer(
+ term->wl.surface, col * term->cell_width, row * term->cell_height,
+ term->cell_width, term->cell_height);
+ }
+ last_cursor = cursor_as_linear;
+ }
+
+ if (all_clean) {
+ buf->busy = false;
+ return;
}
- struct damage cursor = {
- .type = DAMAGE_UPDATE,
- .range = {.start = term->grid->offset + term->cursor.linear, .length = 1},
- };
- grid_render_update(term, buf, &cursor);
- last_cursor = term->grid->offset + term->cursor.linear;
+ render_cell(
+ term, buf,
+ &grid_row(term->grid, term->cursor.row)->cells[term->cursor.col],
+ term->cursor.col, term->cursor.row);
+
+ wl_surface_damage_buffer(
+ term->wl.surface,
+ term->cursor.col * term->cell_width,
+ term->cursor.row * term->cell_height,
+ term->cell_width, term->cell_height);
if (gseq.count > 0) {
cairo_set_scaled_font(buf->cairo, attrs_to_font(term, &gseq.attrs));
- cairo_set_source_rgba(
+ cairo_set_source_rgb(
buf->cairo, gseq.foreground.r, gseq.foreground.g,
- gseq.foreground.b, gseq.foreground.a);
- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);
+ gseq.foreground.b);
cairo_show_glyphs(buf->cairo, gseq.glyphs, gseq.count);
}
- term->grid->offset %= term->grid->size;
- if (term->grid->offset < 0)
- term->grid->offset += term->grid->size;
+ assert(term->grid->offset >= 0 && term->grid->offset < term->grid->num_rows);
- //cairo_surface_flush(buf->cairo_surface);
+ cairo_surface_flush(buf->cairo_surface);
wl_surface_attach(term->wl.surface, buf->wl_buf, 0, 0);
- struct wl_callback *cb = wl_surface_frame(term->wl.surface);
- wl_callback_add_listener(cb, &frame_listener, term);
- term->frame_is_scheduled = true;
+ assert(term->frame_callback == NULL);
+ term->frame_callback = wl_surface_frame(term->wl.surface);
+ wl_callback_add_listener(term->frame_callback, &frame_listener, term);
wl_surface_commit(term->wl.surface);
}
@@ -470,11 +320,32 @@ frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_da
{
struct terminal *term = data;
- term->frame_is_scheduled = false;
+ assert(term->frame_callback == wl_callback);
wl_callback_destroy(wl_callback);
+ term->frame_callback = NULL;
grid_render(term);
}
+static void
+reflow(struct row **new_grid, int new_cols, int new_rows,
+ struct row *const *old_grid, int old_cols, int old_rows)
+{
+ /* TODO: actually reflow */
+ for (int r = 0; r < min(new_rows, old_rows); r++) {
+ size_t copy_cols = min(new_cols, old_cols);
+ size_t clear_cols = new_cols - copy_cols;
+
+ struct cell *new_cells = new_grid[r]->cells;
+ const struct cell *old_cells = old_grid[r]->cells;
+
+ memcpy(new_cells, old_cells, copy_cols * sizeof(new_cells[0]));
+ memset(&new_cells[copy_cols], 0, clear_cols * sizeof(new_cells[0]));
+ }
+
+ for (int r = min(new_rows, old_rows); r < new_rows; r++)
+ memset(new_grid[r]->cells, 0, new_cols * sizeof(new_grid[r]->cells[0]));
+}
+
/* Move to terminal.c? */
void
render_resize(struct terminal *term, int width, int height)
@@ -485,43 +356,59 @@ render_resize(struct terminal *term, int width, int height)
term->width = width;
term->height = height;
- const size_t old_rows = term->rows;
- const size_t normal_old_size = term->normal.size;
- const size_t alt_old_size = term->alt.size;
+ const int old_cols = term->cols;
+ const int old_rows = term->rows;
+ const int old_normal_grid_rows = term->normal.num_rows;
+ const int old_alt_grid_rows = term->alt.num_rows;
- term->cols = term->width / term->cell_width;
- term->rows = term->height / term->cell_height;
+ const int new_cols = term->width / term->cell_width;
+ const int new_rows = term->height / term->cell_height;
+ const int new_normal_grid_rows = new_rows;
+ const int new_alt_grid_rows = new_rows;
- term->normal.size = term->cols * term->rows;
- term->alt.size = term->cols * term->rows;
+ /* Allocate new 'normal' grid */
+ struct row **normal = malloc(new_normal_grid_rows * sizeof(normal[0]));
+ for (int r = 0; r < new_normal_grid_rows; r++) {
+ struct row *row = malloc(sizeof(*row));
+ row->cells = malloc(new_cols * sizeof(row->cells[0]));
+ normal[r] = row;
+ }
- term->normal.cells = realloc(
- term->normal.cells,
- term->normal.size * sizeof(term->normal.cells[0]));
- term->alt.cells = realloc(
- term->alt.cells,
- term->alt.size * sizeof(term->alt.cells[0]));
+ /* Allocate new 'alt' grid */
+ struct row **alt = malloc(new_alt_grid_rows * sizeof(alt[0]));
+ for (int r = 0; r < new_alt_grid_rows; r++) {
+ struct row *row = malloc(sizeof(*row));
+ row->cells = malloc(new_cols * sizeof(row->cells[0]));
+ alt[r] = row;
+ }
- term->normal.offset
- = (term->normal.offset + term->cols - 1) / term->cols * term->cols;
- term->alt.offset
- = (term->alt.offset + term->cols - 1) / term->cols * term->cols;
+ /* Reflow content */
+ reflow(normal, new_cols, new_normal_grid_rows,
+ term->normal.rows, old_cols, old_normal_grid_rows);
+ reflow(alt, new_cols, new_alt_grid_rows,
+ term->alt.rows, old_cols, old_alt_grid_rows);
- /* TODO: memset */
- for (size_t i = normal_old_size; i < term->normal.size; i++) {
- term->normal.cells[i] = (struct cell){
- .attrs = {.foreground = term->foreground,
- .background = term->background},
- };
+ /* Free old 'normal' grid */
+ for (int r = 0; r < term->normal.num_rows; r++) {
+ free(term->normal.rows[r]->cells);
+ free(term->normal.rows[r]);
}
+ free(term->normal.rows);
- /* TODO: memset */
- for (size_t i = alt_old_size; i < term->alt.size; i++) {
- term->alt.cells[i] = (struct cell){
- .attrs = {.foreground = term->foreground,
- .background = term->background},
- };
+ /* Free old 'alt' grid */
+ for (int r = 0; r < term->alt.num_rows; r++) {
+ free(term->alt.rows[r]->cells);
+ free(term->alt.rows[r]);
}
+ free(term->alt.rows);
+
+ term->cols = new_cols;
+ term->rows = new_rows;
+
+ term->normal.rows = normal;
+ term->normal.num_rows = new_normal_grid_rows;
+ term->alt.rows = alt;
+ term->alt.num_rows = new_alt_grid_rows;
LOG_INFO("resize: %dx%d, grid: cols=%d, rows=%d",
term->width, term->height, term->cols, term->rows);
@@ -537,17 +424,19 @@ render_resize(struct terminal *term, int width, int height)
LOG_ERRNO("TIOCSWINSZ");
}
- if (term->scroll_region.end == old_rows)
+ if (term->scroll_region.start >= term->rows)
+ term->scroll_region.start = 0;
+
+ if (term->scroll_region.end >= old_rows)
term->scroll_region.end = term->rows;
term_cursor_to(
term,
min(term->cursor.row, term->rows - 1),
min(term->cursor.col, term->cols - 1));
-
term_damage_all(term);
- if (!term->frame_is_scheduled)
+ if (term->frame_callback == NULL)
grid_render(term);
}