From 9f21799cb2acbd9d1377c3b16616f689b52913b7 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 13:44:52 +0200 Subject: render: merge wl surface damage for consecutive dirty rows --- render.c | 93 ++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 38 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index d745975f..97b0ad4f 100644 --- a/render.c +++ b/render.c @@ -865,6 +865,15 @@ render_worker_thread(void *_ctx) struct buffer *buf = term->render.workers.buf; bool frame_done = false; + /* Translate offset-relative cursor row to view-relative */ + struct coord cursor = {-1, -1}; + if (!term->hide_cursor) { + cursor = term->grid->cursor.point; + cursor.row += term->grid->offset; + cursor.row -= term->grid->view; + cursor.row &= term->grid->num_rows - 1; + } + while (!frame_done) { mtx_lock(lock); assert(tll_length(term->render.workers.queue) > 0); @@ -872,18 +881,12 @@ render_worker_thread(void *_ctx) int row_no = tll_pop_front(term->render.workers.queue); mtx_unlock(lock); - /* Translate offset-relative cursor row to view-relative */ - struct coord cursor = term->grid->cursor.point; - cursor.row += term->grid->offset; - cursor.row -= term->grid->view; - cursor.row &= term->grid->num_rows - 1; - switch (row_no) { default: { assert(buf != NULL); struct row *row = grid_row_in_view(term->grid, row_no); - int cursor_col = !term->hide_cursor && cursor.row == row_no ? cursor.col : -1; + int cursor_col = cursor.row == row_no ? cursor.col : -1; render_row(term, buf->pix[my_id], row, row_no, cursor_col); break; @@ -1412,31 +1415,67 @@ grid_render(struct terminal *term) row->dirty = true; } - if (term->render.workers.count > 0) { + /* Translate offset-relative row to view-relative, unless cursor + * is hidden, then we just set it to -1 */ + struct coord cursor = {-1, -1}; + if (!term->hide_cursor) { + cursor = term->grid->cursor.point; + cursor.row += term->grid->offset; + cursor.row -= term->grid->view; + cursor.row &= term->grid->num_rows - 1; + } + if (term->render.workers.count > 0) { mtx_lock(&term->render.workers.lock); - term->render.workers.buf = buf; for (size_t i = 0; i < term->render.workers.count; i++) sem_post(&term->render.workers.start); assert(tll_length(term->render.workers.queue) == 0); + } + + int first_dirty_row = -1; + for (int r = 0; r < term->rows; r++) { + struct row *row = grid_row_in_view(term->grid, r); + + if (!row->dirty) { + if (first_dirty_row >= 0) { + wl_surface_damage_buffer( + term->window->surface, + term->margins.left, + term->margins.top + first_dirty_row * term->cell_height, + term->width - term->margins.left - term->margins.right, + (r - first_dirty_row) * term->cell_height); + } + first_dirty_row = -1; + continue; + } - for (int r = 0; r < term->rows; r++) { - struct row *row = grid_row_in_view(term->grid, r); + if (first_dirty_row < 0) + first_dirty_row = r; - if (!row->dirty) - continue; + row->dirty = false; + if (term->render.workers.count > 0) tll_push_back(term->render.workers.queue, r); - row->dirty = false; + else { + int cursor_col = cursor.row == r ? cursor.col : -1; + render_row(term, buf->pix[0], row, r, cursor_col); + } + + if (first_dirty_row >= 0) { wl_surface_damage_buffer( term->window->surface, - term->margins.left, term->margins.top + r * term->cell_height, - term->width - term->margins.left - term->margins.right, term->cell_height); + term->margins.left, + term->margins.top + first_dirty_row * term->cell_height, + term->width - term->margins.left - term->margins.right, + (term->rows - first_dirty_row) * term->cell_height); } + } + /* Signal workers the frame is done */ + if (term->render.workers.count > 0) { for (size_t i = 0; i < term->render.workers.count; i++) tll_push_back(term->render.workers.queue, -1); mtx_unlock(&term->render.workers.lock); @@ -1444,28 +1483,6 @@ grid_render(struct terminal *term) for (size_t i = 0; i < term->render.workers.count; i++) sem_wait(&term->render.workers.done); term->render.workers.buf = NULL; - } else { - for (int r = 0; r < term->rows; r++) { - struct row *row = grid_row_in_view(term->grid, r); - - if (!row->dirty) - continue; - - /* Translate offset-relative row to view-relative */ - struct coord cursor = term->grid->cursor.point; - cursor.row += term->grid->offset; - cursor.row -= term->grid->view; - cursor.row &= term->grid->num_rows - 1; - - int cursor_col = !term->hide_cursor && cursor.row == r ? cursor.col : -1; - render_row(term, buf->pix[0], row, r, cursor_col); - row->dirty = false; - - wl_surface_damage_buffer( - term->window->surface, - term->margins.left, term->margins.top + r * term->cell_height, - term->width - term->margins.left - term->margins.right, term->cell_height); - } } render_sixel_images(term, buf->pix[0]); -- cgit v1.3 From 0336f47f0561e2ba5793b988c1737393caff21ff Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 14:03:58 +0200 Subject: render: render_margin(): remove top/bottom arguments All calls to render_margin() set top=true and bottom=true anyway. --- render.c | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 97b0ad4f..80f37139 100644 --- a/render.c +++ b/render.c @@ -503,8 +503,8 @@ draw_cursor: } static void -render_margin(struct terminal *term, struct buffer *buf, int start_line, int end_line, - bool top, bool bottom) +render_margin(struct terminal *term, struct buffer *buf, + int start_line, int end_line) { /* Fill area outside the cell grid with the default background color */ const int rmargin = term->width - term->margins.right; @@ -517,25 +517,15 @@ render_margin(struct terminal *term, struct buffer *buf, int start_line, int end if (term->is_searching) color_dim(&bg); - if (top) { - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix[0], &bg, 1, - &(pixman_rectangle16_t){0, 0, term->width, term->margins.top}); - wl_surface_damage_buffer( - term->window->surface, 0, 0, term->width, term->margins.top); - } - - if (bottom) { - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix[0], &bg, 1, - &(pixman_rectangle16_t){0, bmargin, term->width, term->margins.bottom}); - wl_surface_damage_buffer( - term->window->surface, 0, bmargin, term->width, term->margins.bottom); - } - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix[0], &bg, 2, + PIXMAN_OP_SRC, buf->pix[0], &bg, 4, (pixman_rectangle16_t[]){ + /* Top */ + {0, 0, term->width, term->margins.top}, + + /* Bottom */ + {0, bmargin, term->width, term->margins.bottom}, + /* Left */ {0, term->margins.top + start_line * term->cell_height, term->margins.left, line_count * term->cell_height}, @@ -545,6 +535,14 @@ render_margin(struct terminal *term, struct buffer *buf, int start_line, int end term->margins.right, line_count * term->cell_height}, }); + /* Top */ + wl_surface_damage_buffer( + term->window->surface, 0, 0, term->width, term->margins.top); + + /* Bottom */ + wl_surface_damage_buffer( + term->window->surface, 0, bmargin, term->width, term->margins.bottom); + /* Left */ wl_surface_damage_buffer( term->window->surface, @@ -633,8 +631,7 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, if (did_shm_scroll) { /* Restore margins */ render_margin( - term, buf, dmg->region.end - dmg->lines, term->rows, - true, true); + term, buf, dmg->region.end - dmg->lines, term->rows); } else { /* Fallback for when we either cannot do SHM scrolling, or it failed */ uint8_t *raw = buf->mmapped; @@ -699,8 +696,7 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, if (did_shm_scroll) { /* Restore margins */ render_margin( - term, buf, dmg->region.start, dmg->region.start + dmg->lines, - true, true); + term, buf, dmg->region.start, dmg->region.start + dmg->lines); } else { /* Fallback for when we either cannot do SHM scrolling, or it failed */ uint8_t *raw = buf->mmapped; @@ -1341,7 +1337,7 @@ grid_render(struct terminal *term) else { tll_free(term->grid->scroll_damage); - render_margin(term, buf, 0, term->rows, true, true); + render_margin(term, buf, 0, term->rows); term_damage_view(term); } -- cgit v1.3 From ed620f1854cd740b493f87c5487378139fab3c27 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 14:06:02 +0200 Subject: render: render_margin: add 'damage_{top,bottom,left,right}' arguments --- render.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 80f37139..7f565844 100644 --- a/render.c +++ b/render.c @@ -504,7 +504,9 @@ draw_cursor: static void render_margin(struct terminal *term, struct buffer *buf, - int start_line, int end_line) + int start_line, int end_line, + bool damage_top, bool damage_bottom, + bool damage_left, bool damage_right) { /* Fill area outside the cell grid with the default background color */ const int rmargin = term->width - term->margins.right; @@ -535,25 +537,25 @@ render_margin(struct terminal *term, struct buffer *buf, term->margins.right, line_count * term->cell_height}, }); - /* Top */ - wl_surface_damage_buffer( - term->window->surface, 0, 0, term->width, term->margins.top); + if (damage_top) + wl_surface_damage_buffer( + term->window->surface, 0, 0, term->width, term->margins.top); - /* Bottom */ - wl_surface_damage_buffer( - term->window->surface, 0, bmargin, term->width, term->margins.bottom); + if (damage_bottom) + wl_surface_damage_buffer( + term->window->surface, 0, bmargin, term->width, term->margins.bottom); - /* Left */ - wl_surface_damage_buffer( - term->window->surface, - 0, term->margins.top + start_line * term->cell_height, - term->margins.left, line_count * term->cell_height); + if (damage_left) + wl_surface_damage_buffer( + term->window->surface, + 0, term->margins.top + start_line * term->cell_height, + term->margins.left, line_count * term->cell_height); - /* Right */ - wl_surface_damage_buffer( - term->window->surface, - rmargin, term->margins.top + start_line * term->cell_height, - term->margins.right, line_count * term->cell_height); + if (damage_right) + wl_surface_damage_buffer( + term->window->surface, + rmargin, term->margins.top + start_line * term->cell_height, + term->margins.right, line_count * term->cell_height); } static void @@ -631,7 +633,8 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, if (did_shm_scroll) { /* Restore margins */ render_margin( - term, buf, dmg->region.end - dmg->lines, term->rows); + term, buf, dmg->region.end - dmg->lines, term->rows, + true, true, true, true); } else { /* Fallback for when we either cannot do SHM scrolling, or it failed */ uint8_t *raw = buf->mmapped; @@ -696,7 +699,8 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, if (did_shm_scroll) { /* Restore margins */ render_margin( - term, buf, dmg->region.start, dmg->region.start + dmg->lines); + term, buf, dmg->region.start, dmg->region.start + dmg->lines, + true, true, true, true); } else { /* Fallback for when we either cannot do SHM scrolling, or it failed */ uint8_t *raw = buf->mmapped; @@ -1337,7 +1341,7 @@ grid_render(struct terminal *term) else { tll_free(term->grid->scroll_damage); - render_margin(term, buf, 0, term->rows); + render_margin(term, buf, 0, term->rows, true, true, true, true); term_damage_view(term); } -- cgit v1.3 From 96f480b14d60e40e5e244aa61306754ad5aa7a95 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 14:18:43 +0200 Subject: render: doh! flush the last surface damage *outside* of the rendering loop --- render.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 7f565844..90856bef 100644 --- a/render.c +++ b/render.c @@ -1463,15 +1463,15 @@ grid_render(struct terminal *term) int cursor_col = cursor.row == r ? cursor.col : -1; render_row(term, buf->pix[0], row, r, cursor_col); } + } - if (first_dirty_row >= 0) { - wl_surface_damage_buffer( - term->window->surface, - term->margins.left, - term->margins.top + first_dirty_row * term->cell_height, - term->width - term->margins.left - term->margins.right, - (term->rows - first_dirty_row) * term->cell_height); - } + if (first_dirty_row >= 0) { + wl_surface_damage_buffer( + term->window->surface, + term->margins.left, + term->margins.top + first_dirty_row * term->cell_height, + term->width - term->margins.left - term->margins.right, + (term->rows - first_dirty_row) * term->cell_height); } /* Signal workers the frame is done */ -- cgit v1.3 From 3f55cf3d14f0009d2036fec480e1cb7649c34f14 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 14:19:07 +0200 Subject: render: render_margin: don't damage margins when rendering scroll damage When applying scroll damage, we may have to re-render the margins. This is because when we SHM-scroll, we actually move the entire surface, and thus we end up overwriting the top margin area with old window content, and scroll in uninitialized memory in the bottom margin. But, we don't have to tell the compositor about this - the last frame always contains correct margins; i.e. there's no change between the last frame and current frame being rendered. TODO: we _could_ micro-optimize and only damage the margins after a surface resize. We do it slightly more often now, when dealing with state changes in screen flash or scrollback searching. But I don't think it's worth the effort. --- render.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'render.c') diff --git a/render.c b/render.c index 90856bef..99ca03e3 100644 --- a/render.c +++ b/render.c @@ -504,9 +504,7 @@ draw_cursor: static void render_margin(struct terminal *term, struct buffer *buf, - int start_line, int end_line, - bool damage_top, bool damage_bottom, - bool damage_left, bool damage_right) + int start_line, int end_line, bool apply_damage) { /* Fill area outside the cell grid with the default background color */ const int rmargin = term->width - term->margins.right; @@ -537,25 +535,27 @@ render_margin(struct terminal *term, struct buffer *buf, term->margins.right, line_count * term->cell_height}, }); - if (damage_top) + if (apply_damage) { + /* Top */ wl_surface_damage_buffer( term->window->surface, 0, 0, term->width, term->margins.top); - if (damage_bottom) + /* Bottom */ wl_surface_damage_buffer( term->window->surface, 0, bmargin, term->width, term->margins.bottom); - if (damage_left) + /* Left */ wl_surface_damage_buffer( term->window->surface, 0, term->margins.top + start_line * term->cell_height, term->margins.left, line_count * term->cell_height); - if (damage_right) + /* Right */ wl_surface_damage_buffer( term->window->surface, rmargin, term->margins.top + start_line * term->cell_height, term->margins.right, line_count * term->cell_height); + } } static void @@ -633,8 +633,7 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, if (did_shm_scroll) { /* Restore margins */ render_margin( - term, buf, dmg->region.end - dmg->lines, term->rows, - true, true, true, true); + term, buf, dmg->region.end - dmg->lines, term->rows, false); } else { /* Fallback for when we either cannot do SHM scrolling, or it failed */ uint8_t *raw = buf->mmapped; @@ -699,8 +698,7 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, if (did_shm_scroll) { /* Restore margins */ render_margin( - term, buf, dmg->region.start, dmg->region.start + dmg->lines, - true, true, true, true); + term, buf, dmg->region.start, dmg->region.start + dmg->lines, false); } else { /* Fallback for when we either cannot do SHM scrolling, or it failed */ uint8_t *raw = buf->mmapped; @@ -1341,7 +1339,7 @@ grid_render(struct terminal *term) else { tll_free(term->grid->scroll_damage); - render_margin(term, buf, 0, term->rows, true, true, true, true); + render_margin(term, buf, 0, term->rows, true); term_damage_view(term); } -- cgit v1.3