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(-) 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(-) 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(-) 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 74c3b4be1ad846aeff1ae372317e1a0857c63e44 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 14:17:55 +0200 Subject: main: free 'cwd' at exit This fixes an issue where e.g. failing to initialize the Wayland backend would leave cwd un-free:d --- main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 02bd7fcd..590e01c0 100644 --- a/main.c +++ b/main.c @@ -403,10 +403,10 @@ main(int argc, char *const *argv) if (!as_server && (term = term_init( &conf, fdm, reaper, wayl, "foot", cwd, argc, argv, &term_shutdown_cb, &shutdown_ctx)) == NULL) { - free(cwd); goto out; } free(cwd); + cwd = NULL; if (as_server && (server = server_init(&conf, fdm, reaper, wayl)) == NULL) goto out; @@ -439,6 +439,7 @@ main(int argc, char *const *argv) ret = aborted || tll_length(wayl->terms) == 0 ? EXIT_SUCCESS : EXIT_FAILURE; out: + free(cwd); server_destroy(server); term_destroy(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(-) 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(-) 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 From 3958d84069426fee2784a116a71a294cdb5fed5d Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 15:01:09 +0200 Subject: term: set_fonts: describe why we force resize --- terminal.c | 1 + 1 file changed, 1 insertion(+) diff --git a/terminal.c b/terminal.c index 3a679b5d..1ed042bf 100644 --- a/terminal.c +++ b/terminal.c @@ -552,6 +552,7 @@ term_set_fonts(struct terminal *term, struct fcft_font *fonts[static 4]) term->fonts[0]->ascent + term->fonts[0]->descent); LOG_INFO("cell width=%d, height=%d", term->cell_width, term->cell_height); + /* Use force, since cell-width/height may have changed */ render_resize_force(term, term->width / term->scale, term->height / term->scale); return true; } -- cgit v1.3 From d169e9634603a2ec84bb18c975cdb06f5e3551dc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 15:01:27 +0200 Subject: wayland: configure: do a regular resize instead of a force resize The commit log says that was needed to get tiling in GNOME working. However, I don't know *which* extension that was. Force resizing in a configure event means we'll trash a perfectly valid buffer on e.g. 'activated' state changes. So, let's revert this for now, and if this breaks GNOME, let's try to find another solution. If worse comes to worse, we can detect if we're running under GNOME and do a force-resize then, but not on sane compositors. --- wayland.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/wayland.c b/wayland.c index 1320fc3a..40f4d3e8 100644 --- a/wayland.c +++ b/wayland.c @@ -546,7 +546,11 @@ xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, win->is_fullscreen = win->configure.is_fullscreen; xdg_surface_ack_configure(xdg_surface, serial); - bool resized = render_resize_force(term, win->configure.width, win->configure.height); + + /* TODO: check with GNOME and tiling - presumably that didn't work + * unless we presented a *new* buffer, hence we used to do a force + * resize here */ + bool resized = render_resize(term, win->configure.width, win->configure.height); if (win->configure.is_activated) term_visual_focus_in(term); @@ -556,7 +560,7 @@ xdg_surface_configure(void *data, struct xdg_surface *xdg_surface, /* TODO: remove - shouldn't be necessary with render_resize_force() */ if (!resized) { /* - * If we didn't resize, we won't be commit a new surface + * If we didn't resize, we won't be committing a new surface * anytime soon. Some compositors require a commit in * combination with an ack - make them happy. */ -- cgit v1.3 From 18827ebe0b7a5a314a36dea7b58e0357395b3019 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 13 Jul 2020 15:10:03 +0200 Subject: changelog: mention fix for 'too many wl_surface_damage_buffer()' calls --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 306a1cc1..36ae5308 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -91,6 +91,8 @@ * Sixel images being erased when printing text next to them. * Sixel handling when resizing window. * Sixel handling when scrollback wraps around. +* Foot now issues much fewer `wl_surface_damage_buffer()` calls + (https://codeberg.org/dnkl/foot/issues/35). ### Security -- cgit v1.3