From fcb60abc132b12ccf8fb8ff215acc924a44773d6 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 4 Jul 2021 17:59:40 +0200 Subject: config: add locked-title=no|yes Closes #386 --- terminal.h | 1 + 1 file changed, 1 insertion(+) (limited to 'terminal.h') diff --git a/terminal.h b/terminal.h index 385b36e0..94c1cf75 100644 --- a/terminal.h +++ b/terminal.h @@ -382,6 +382,7 @@ struct terminal { bool sixel_cursor_right_of_graphics:1; } xtsave; + bool window_title_has_been_set; char *window_title; tll(char *) window_title_stack; -- cgit v1.3 From cf6d04f9f2fcb7a4f12d35edd0a6d4cd5591b59c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 11 Jul 2021 10:06:12 +0200 Subject: url-mode: fix crash when removing duplicate and/or overlapping URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removing overlaping and duplicated URLs is done by running two nested loops, that both iterate the same URL list. When a duplicate is found, one of the URLs is destroyed and removed from the list. Deleting and removing an item *is* safe, but only as long as _no other_ iterator has references to it. In this case, if we remove an item from e.g. the inner iterator, we’ll crash if the outer iterator’s *next* item is the item being removed. Closes #627 --- CHANGELOG.md | 2 ++ terminal.h | 1 + url-mode.c | 18 +++++++++++------- 3 files changed, 14 insertions(+), 7 deletions(-) (limited to 'terminal.h') diff --git a/CHANGELOG.md b/CHANGELOG.md index 44093dc5..acea5f9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,8 @@ last column, and `tweak.allow-overflowing-double-width-glyphs=yes`. * FD exhaustion when repeatedly entering/exiting URL mode with many URLs. +* Double free of URL while removing duplicated and/or overlapping URLs + in URL mode (https://codeberg.org/dnkl/foot/issues/627). ### Security diff --git a/terminal.h b/terminal.h index 94c1cf75..309f7db6 100644 --- a/terminal.h +++ b/terminal.h @@ -264,6 +264,7 @@ struct url { enum url_action action; bool url_mode_dont_change_url_attr; /* Entering/exiting URL mode doesn’t touch the cells’ attr.url */ bool osc8; + bool duplicate; }; typedef tll(struct url) url_list_t; diff --git a/url-mode.c b/url-mode.c index 8ac5631e..94c05b3a 100644 --- a/url-mode.c +++ b/url-mode.c @@ -470,16 +470,20 @@ remove_overlapping(url_list_t *urls, int cols) */ xassert(in->osc8 || out->osc8); - if (in->osc8) { - url_destroy(&outer->item); - tll_remove(*urls, outer); - } else { - url_destroy(&inner->item); - tll_remove(*urls, inner); - } + if (in->osc8) + outer->item.duplicate = true; + else + inner->item.duplicate = true; } } } + + tll_foreach(*urls, it) { + if (it->item.duplicate) { + url_destroy(&it->item); + tll_remove(*urls, it); + } + } } void -- cgit v1.3 From fcd989734235c25e64f8fb38ffcfdf381e933873 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 14 Jul 2021 19:17:44 +0200 Subject: csi: invert the meaning of DECSDM There has been some confusion whether enabling DECSDM (private mode 80) enables or disables sixel scrolling. Foot currently enables scrolling when DECSDM is set, and this patch changes this, such that setting DECSDM now *disables* scrolling. The confusion is apparently due to a documentation error in the VT340 manual, as described in https://github.com/dankamongmen/notcurses/issues/1782#issuecomment-863603641. And that makes sense, in a way: the SDM in DECSDM stands for Sixel Display Mode. I.e. it stands to reason that enabling that disables scrolling. Anyway, this lead to https://github.com/hackerb9/lsix/issues/41, where it was eventually proven (by testing on a real VT340), that foot, and a large number of other terminals (including XTerm) has it wrong: https://github.com/hackerb9/lsix/issues/41#issuecomment-873269599. --- CHANGELOG.md | 2 ++ csi.c | 8 ++++---- terminal.h | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) (limited to 'terminal.h') diff --git a/CHANGELOG.md b/CHANGELOG.md index f7bd40d8..20c52d1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,8 @@ * Non-empty lines are now considered to have a hard linebreak, _unless_ an actual word-wrap is inserted. +* Setting `DECSDM` now _disables_ sixel scrolling, while resetting it + _enables_ scrolling (https://codeberg.org/dnkl/foot/issues/631). ### Deprecated diff --git a/csi.c b/csi.c index fcc2800f..80ee75cd 100644 --- a/csi.c +++ b/csi.c @@ -402,7 +402,7 @@ decset_decrst(struct terminal *term, unsigned param, bool enable) break; case 80: - term->sixel.scrolling = enable; + term->sixel.scrolling = !enable; break; case 1000: @@ -611,7 +611,7 @@ decrqm(const struct terminal *term, unsigned param, bool *enabled) case 12: *enabled = term->cursor_blink.decset; return true; case 25: *enabled = !term->hide_cursor; return true; case 45: *enabled = term->reverse_wrap; return true; - case 80: *enabled = term->sixel.scrolling; return true; + case 80: *enabled = !term->sixel.scrolling; return true; case 1000: *enabled = term->mouse_tracking == MOUSE_CLICK; return true; case 1001: *enabled = false; return true; case 1002: *enabled = term->mouse_tracking == MOUSE_DRAG; return true; @@ -654,7 +654,7 @@ xtsave(struct terminal *term, unsigned param) case 25: term->xtsave.show_cursor = !term->hide_cursor; break; case 45: term->xtsave.reverse_wrap = term->reverse_wrap; break; case 47: term->xtsave.alt_screen = term->grid == &term->alt; break; - case 80: term->xtsave.sixel_scrolling = term->sixel.scrolling; break; + case 80: term->xtsave.sixel_display_mode = !term->sixel.scrolling; break; case 1000: term->xtsave.mouse_click = term->mouse_tracking == MOUSE_CLICK; break; case 1001: break; case 1002: term->xtsave.mouse_drag = term->mouse_tracking == MOUSE_DRAG; break; @@ -696,7 +696,7 @@ xtrestore(struct terminal *term, unsigned param) case 25: enable = term->xtsave.show_cursor; break; case 45: enable = term->xtsave.reverse_wrap; break; case 47: enable = term->xtsave.alt_screen; break; - case 80: enable = term->xtsave.sixel_scrolling; break; + case 80: enable = term->xtsave.sixel_display_mode; break; case 1000: enable = term->xtsave.mouse_click; break; case 1001: return; case 1002: enable = term->xtsave.mouse_drag; break; diff --git a/terminal.h b/terminal.h index 309f7db6..12a637a0 100644 --- a/terminal.h +++ b/terminal.h @@ -378,7 +378,7 @@ struct terminal { bool ime:1; bool app_sync_updates:1; - bool sixel_scrolling:1; + bool sixel_display_mode:1; bool sixel_private_palette:1; bool sixel_cursor_right_of_graphics:1; } xtsave; -- cgit v1.3 From 91801ae55da017c2dd0f44c04b8d316a9a2090ef Mon Sep 17 00:00:00 2001 From: Timur Celik Date: Tue, 15 Jun 2021 11:45:27 +0200 Subject: render: Allow cells to bleed into their neighbor This patch adds a `confined` flag to each cell to track if the last rendered glyph bled into it's right neighbor. To keep things simple, bleeding into any other neighbor cell than the immediate right one is not allowed. This should cover most use cases. Before rendering a row we now do a prepass and mark all cells unclean that are affected by a bleeding neighbor. If there are consecutive bleeding cells, the whole group must be re-rendered even if only a single cell has changed. The patch also deprecates both old overflowing glyph options *allow-overflowing-double-width-glyphs* and *pua-double-width* in favor of a single new one named *overflowing-glyphs*. --- CHANGELOG.md | 11 ++++++++ config.c | 17 ++++-------- config.h | 3 +-- doc/foot.ini.5.scd | 34 +++++++++--------------- render.c | 78 ++++++++++++++++++++++++++++-------------------------- terminal.h | 3 ++- 6 files changed, 71 insertions(+), 75 deletions(-) (limited to 'terminal.h') diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c2ed336..f12a8b53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,9 @@ * `locked-title=no|yes` to `foot.ini` (https://codeberg.org/dnkl/foot/issues/386). +* `tweak.overflowing-glyphs` option, which can be enabled to fix rendering + issues with glyphs of any width that appear cut-off + (https://codeberg.org/dnkl/foot/issues/592). ### Changed @@ -45,6 +48,12 @@ ### Deprecated ### Removed + +* The `tweak.allow-overflowing-double-width-glyphs` and + `tweak.pua-double-width` options (which have been superseded by + `tweak.overflowing-glyphs`). + + ### Fixed * Glyph offset not being taken into account when applying @@ -69,6 +78,8 @@ ### Security ### Contributors +* [clktmr](https://codeberg.org/clktmr) + ## 1.8.1 diff --git a/config.c b/config.c index 92582f2c..a9acb58e 100644 --- a/config.c +++ b/config.c @@ -2213,16 +2213,10 @@ parse_section_tweak( return false; } - else if (strcmp(key, "allow-overflowing-double-width-glyphs") == 0) { - conf->tweak.allow_overflowing_double_width_glyphs = str_to_bool(value); - if (!conf->tweak.allow_overflowing_double_width_glyphs) - LOG_WARN("tweak: disabled overflowing double-width glyphs"); - } - - else if (strcmp(key, "pua-double-width") == 0) { - conf->tweak.pua_double_width = str_to_bool(value); - if (conf->tweak.pua_double_width) - LOG_WARN("tweak: PUA double width glyphs enabled"); + else if (strcmp(key, "overflowing-glyphs") == 0) { + conf->tweak.overflowing_glyphs = str_to_bool(value); + if (!conf->tweak.overflowing_glyphs) + LOG_WARN("tweak: disabled overflowing glyphs"); } else if (strcmp(key, "damage-whole-window") == 0) { @@ -2833,7 +2827,7 @@ config_load(struct config *conf, const char *conf_path, .tweak = { .fcft_filter = FCFT_SCALING_FILTER_LANCZOS3, - .allow_overflowing_double_width_glyphs = true, + .overflowing_glyphs = true, .grapheme_shaping = false, .grapheme_width_method = GRAPHEME_WIDTH_DOUBLE, .delayed_render_lower_ns = 500000, /* 0.5ms */ @@ -2844,7 +2838,6 @@ config_load(struct config *conf, const char *conf_path, .damage_whole_window = false, .box_drawing_base_thickness = 0.04, .box_drawing_solid_shades = true, - .pua_double_width = false, }, .notifications = tll_init(), diff --git a/config.h b/config.h index 190ea427..729eba3e 100644 --- a/config.h +++ b/config.h @@ -245,7 +245,7 @@ struct config { struct { enum fcft_scaling_filter fcft_filter; - bool allow_overflowing_double_width_glyphs; + bool overflowing_glyphs; bool grapheme_shaping; enum {GRAPHEME_WIDTH_WCSWIDTH, GRAPHEME_WIDTH_DOUBLE} grapheme_width_method; bool render_timer_osd; @@ -256,7 +256,6 @@ struct config { off_t max_shm_pool_size; float box_drawing_base_thickness; bool box_drawing_solid_shades; - bool pua_double_width; } tweak; user_notifications_t notifications; diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 6ceaf3b9..8b2f1dc8 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -847,34 +847,24 @@ any of these options. Default: _lanczos3_. -*allow-overflowing-double-width-glyphs* - Boolean. When enabled, double width glyphs with a character width - of 1 are allowed to overflow into the neighbouring cell. +*overflowing-glyphs* + Boolean. When enabled, glyphs wider than their cell(s) are allowed + to render into one additional neighbouring cell. - One use case for this is fonts "icon" characters in the Unicode - private usage area, e.g. Nerd Fonts, or Powerline Fonts. Without - this option, such glyphs will appear "cut off". + One use case for this are fonts with wide italic characters that + "bend" into the next cell. Without this option, such glyphs will + appear "cut off". - Another use case are legacy emoji characters like *WHITE FROWNING - FACE*. + Another use case are fonts with "icon" characters in the Unicode + private usage area, e.g. Nerd Fonts, or Powerline Fonts and legacy + emoji characters like *WHITE FROWNING FACE*. - Note: this feature uses _heuristics_ to determine *which* glyphs - should be allowed to overflow. - - See also: *pua-double-width* + Note: might impact performance depending on the font used. + Especially small font sizes can cause many overflowing glyphs + because of subpixel rendering. Default: _yes_. -*pua-double-width* - Boolean. When enabled, Unicode code points from the private usage - area (PUA) are always considered to be double width, regardless of - the actual glyph width. - - Ignored if *allow-overflowing-double-width-glyphs* has been - disabled. - - Default: _no_. - *render-timer* Enables a frame rendering timer, that prints the time it takes to render each frame, in microseconds, either on-screen, to stderr, diff --git a/render.c b/render.c index 8e66bf00..41a20226 100644 --- a/render.c +++ b/render.c @@ -437,6 +437,20 @@ draw_cursor(const struct terminal *term, const struct cell *cell, } } +static inline void +render_cell_prepass(struct terminal *term, struct row *row, int col) +{ + for (; col < term->cols - 1; col++) { + if (row->cells[col].attrs.confined || + (row->cells[col].attrs.clean == row->cells[col + 1].attrs.clean)) { + break; + } + + row->cells[col].attrs.clean = 0; + row->cells[col + 1].attrs.clean = 0; + } +} + static int render_cell(struct terminal *term, pixman_image_t *pix, struct row *row, int col, int row_no, bool has_cursor) @@ -446,6 +460,7 @@ render_cell(struct terminal *term, pixman_image_t *pix, return 0; cell->attrs.clean = 1; + cell->attrs.confined = true; int width = term->cell_width; int height = term->cell_height; @@ -597,51 +612,32 @@ render_cell(struct terminal *term, pixman_image_t *pix, cell_cols = max(1, min(cell_cols, cols_left)); /* - * Hack! - * - * Deal with double-width glyphs for which wcwidth() returns - * 1. Typically Unicode private usage area characters, - * e.g. powerline, or nerd hack fonts. - * - * Users can enable a tweak option that lets this glyphs - * overflow/bleed into the neighbouring cell. - * - * We only apply this workaround if: - * - the user has explicitly enabled this feature - * - the *character* width is 1 - * - the *glyph* width is at least 1.5 cells - * - the *glyph* width is less than 3 cells - * - *this* column isn’t the last column - * - *this* cells is followed by an empty cell, or a space + * Determine cells that will bleed into their right neighbor and remember + * them for cleanup in the next frame. */ - if (term->conf->tweak.allow_overflowing_double_width_glyphs && - glyph_count > 0 && - cell_cols == 1 && - col < term->cols - 1 && - ((glyphs[0]->x + glyphs[0]->width >= term->cell_width * 15 / 10 && - glyphs[0]->x + glyphs[0]->width < 3 * term->cell_width) || - (term->conf->tweak.pua_double_width && - ((base >= 0x00e000 && base <= 0x00f8ff) || - (base >= 0x0f0000 && base <= 0x0ffffd) || - (base >= 0x100000 && base <= 0x10fffd)))) && - (row->cells[col + 1].wc == 0 || row->cells[col + 1].wc == L' ')) + int render_width = cell_cols * width; + if (term->conf->tweak.overflowing_glyphs && + glyph_count > 0) { - cell_cols = 2; + int glyph_width = 0, advance = 0; + for (size_t i = 0; i < glyph_count; i++) { + glyph_width = max(glyph_width, + advance + glyphs[i]->x + glyphs[i]->width); + advance += glyphs[i]->advance.x; + } - /* - * Ensure the cell we’re overflowing into gets re-rendered, to - * ensure it is erased if *this* cell is erased. Note that we - * do *not* mark the row as dirty - we don’t need to re-render - * the cell if nothing else on the row has changed. - */ - row->cells[col].attrs.clean = 0; - row->cells[col + 1].attrs.clean = 0; + if (glyph_width > render_width) { + render_width = min(glyph_width, render_width + width); + + for (int i = 0; i < cell_cols; i++) + row->cells[col + i].attrs.confined = false; + } } pixman_region32_t clip; pixman_region32_init_rect( &clip, x, y, - cell_cols * term->cell_width, term->cell_height); + render_width, term->cell_height); pixman_image_set_clip_region32(pix, &clip); /* Background */ @@ -771,6 +767,10 @@ static void render_row(struct terminal *term, pixman_image_t *pix, struct row *row, int row_no, int cursor_col) { + if (term->conf->tweak.overflowing_glyphs) + for (int col = term->cols - 1; col >= 0; col--) + render_cell_prepass(term, row, col); + for (int col = term->cols - 1; col >= 0; col--) render_cell(term, pix, row, col, row_no, cursor_col == col); } @@ -1192,8 +1192,10 @@ render_sixel(struct terminal *term, pixman_image_t *pix, (last_col_needs_erase && last_col)) { render_cell(term, pix, row, col, term_row_no, cursor_col == col); - } else + } else { cell->attrs.clean = 1; + cell->attrs.confined = 1; + } } } } diff --git a/terminal.h b/terminal.h index 12a637a0..5ffa6154 100644 --- a/terminal.h +++ b/terminal.h @@ -42,11 +42,12 @@ struct attributes { uint32_t fg:24; bool clean:1; + bool confined:1; bool have_fg:1; bool have_bg:1; uint32_t selected:2; bool url:1; - uint32_t reserved:2; + uint32_t reserved:1; uint32_t bg:24; }; static_assert(sizeof(struct attributes) == 8, "VT attribute struct too large"); -- cgit v1.3 From 53851e13ec976001cdec2077d7963c292bcd6d20 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 16 Jul 2021 16:48:49 +0200 Subject: shm: refactor: move away from a single, global, buffer list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Up until now, *all* buffers have been tracked in a single, global buffer list. We've used 'cookies' to separate buffers from different contexts (so that shm_get_buffer() doesn't try to re-use e.g. a search-box buffer for the main grid). This patch refactors this, and completely removes the global list. Instead of cookies, we now use 'chains'. A chain tracks both the properties to apply to newly created buffers (scrollable, number of pixman instances to instantiate etc), as well as the instantiated buffers themselves. This means there's strictly speaking not much use for shm_fini() anymore, since its up to the chain owner to call shm_chain_free(), which will also purge all buffers. However, since purging a buffer may be deferred, if the buffer is owned by the compositor at the time of the call to shm_purge() or shm_chain_free(), we still keep a global 'deferred' list, on to which deferred buffers are pushed. shm_fini() iterates this list and destroys the buffers _even_ if they are still owned by the compositor. This only happens at program termination, and not when destroying a terminal instance. I.e. closing a window in a “foot --server” does *not* trigger this. Each terminal instatiates a number of chains, and these chains are destroyed when the terminal instance is destroyed. Note that some buffers may be put on the deferred list, as mentioned above. --- render.c | 49 +++++++------- shm.c | 215 ++++++++++++++++++++++++++++++++----------------------------- shm.h | 39 ++++------- terminal.c | 25 +++++++ terminal.h | 10 +++ wayland.c | 16 ++--- 6 files changed, 190 insertions(+), 164 deletions(-) (limited to 'terminal.h') diff --git a/render.c b/render.c index c6835664..7bcad519 100644 --- a/render.c +++ b/render.c @@ -943,7 +943,7 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, if (try_shm_scroll) { did_shm_scroll = shm_scroll( - term->wl->shm, buf, dmg->lines * term->cell_height, + buf, dmg->lines * term->cell_height, term->margins.top, dmg->region.start * term->cell_height, term->margins.bottom, (term->rows - dmg->region.end) * term->cell_height); } @@ -1008,7 +1008,7 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, if (try_shm_scroll) { did_shm_scroll = shm_scroll( - term->wl->shm, buf, -dmg->lines * term->cell_height, + buf, -dmg->lines * term->cell_height, term->margins.top, dmg->region.start * term->cell_height, term->margins.bottom, (term->rows - dmg->region.end) * term->cell_height); } @@ -1587,9 +1587,8 @@ render_csd_title(struct terminal *term) xassert(info.width % term->scale == 0); xassert(info.height % term->scale == 0); - unsigned long cookie = shm_cookie_csd(term, CSD_SURF_TITLE); - struct buffer *buf = shm_get_buffer( - term->wl->shm, info.width, info.height, cookie, false, 1); + struct buffer_chain *chain = term->render.chains.csd[CSD_SURF_TITLE]; + struct buffer *buf = shm_get_buffer(chain, info.width, info.height); uint32_t _color = term->conf->colors.fg; uint16_t alpha = 0xffff; @@ -1622,9 +1621,8 @@ render_csd_border(struct terminal *term, enum csd_surface surf_idx) xassert(info.width % term->scale == 0); xassert(info.height % term->scale == 0); - unsigned long cookie = shm_cookie_csd(term, surf_idx); - struct buffer *buf = shm_get_buffer( - term->wl->shm, info.width, info.height, cookie, false, 1); + struct buffer_chain *chain = term->render.chains.csd[surf_idx]; + struct buffer *buf = shm_get_buffer(chain, info.width, info.height); pixman_color_t color = color_hex_to_pixman_with_alpha(0, 0); render_csd_part(term, surf, buf, info.width, info.height, &color); @@ -1808,9 +1806,8 @@ render_csd_button(struct terminal *term, enum csd_surface surf_idx) xassert(info.width % term->scale == 0); xassert(info.height % term->scale == 0); - unsigned long cookie = shm_cookie_csd(term, surf_idx); - struct buffer *buf = shm_get_buffer( - term->wl->shm, info.width, info.height, cookie, false, 1); + struct buffer_chain *chain = term->render.chains.csd[surf_idx]; + struct buffer *buf = shm_get_buffer(chain, info.width, info.height); uint32_t _color; uint16_t alpha = 0xffff; @@ -2084,9 +2081,8 @@ render_scrollback_position(struct terminal *term) return; } - unsigned long cookie = shm_cookie_scrollback_indicator(term); - struct buffer *buf = shm_get_buffer( - term->wl->shm, width, height, cookie, false, 1); + struct buffer_chain *chain = term->render.chains.scrollback_indicator; + struct buffer *buf = shm_get_buffer(chain, width, height); wl_subsurface_set_position( win->scrollback_indicator.sub, x / scale, y / scale); @@ -2117,9 +2113,8 @@ render_render_timer(struct terminal *term, struct timeval render_time) const int height = (2 * margin + term->cell_height + scale - 1) / scale * scale; - unsigned long cookie = shm_cookie_render_timer(term); - struct buffer *buf = shm_get_buffer( - term->wl->shm, width, height, cookie, false, 1); + struct buffer_chain *chain = term->render.chains.render_timer; + struct buffer *buf = shm_get_buffer(chain, width, height); wl_subsurface_set_position( win->render_timer.sub, @@ -2292,9 +2287,8 @@ grid_render(struct terminal *term) xassert(term->width > 0); xassert(term->height > 0); - unsigned long cookie = shm_cookie_grid(term); - struct buffer *buf = shm_get_buffer( - term->wl->shm, term->width, term->height, cookie, true, 1 + term->render.workers.count); + struct buffer_chain *chain = term->render.chains.grid; + struct buffer *buf = shm_get_buffer(chain, term->width, term->height); /* Dirty old and current cursor cell, to ensure they’re repainted */ dirty_old_cursor(term); @@ -2626,8 +2620,8 @@ render_search_box(struct terminal *term) const size_t visible_cells = (visible_width - 2 * margin) / term->cell_width; size_t glyph_offset = term->render.search_glyph_offset; - unsigned long cookie = shm_cookie_search(term); - struct buffer *buf = shm_get_buffer(term->wl->shm, width, height, cookie, false, 1); + struct buffer_chain *chain = term->render.chains.search; + struct buffer *buf = shm_get_buffer(chain, width, height); pixman_region32_t clip; pixman_region32_init_rect(&clip, 0, 0, width, height); @@ -2936,7 +2930,8 @@ render_urls(struct terminal *term) } info[tll_length(win->urls)]; /* For shm_get_many() */ - struct buffer_description shm_desc[tll_length(win->urls)]; + int widths[tll_length(win->urls)]; + int heights[tll_length(win->urls)]; size_t render_count = 0; @@ -3066,15 +3061,15 @@ render_urls(struct terminal *term) info[render_count].x = x; info[render_count].y = y; - shm_desc[render_count].width = width; - shm_desc[render_count].height = height; - shm_desc[render_count].cookie = shm_cookie_url(url); + widths[render_count] = width; + heights[render_count] = height; render_count++; } + struct buffer_chain *chain = term->render.chains.url; struct buffer *bufs[render_count]; - shm_get_many(term->wl->shm, render_count, shm_desc, bufs, 1); + shm_get_many(chain, render_count, widths, heights, bufs); uint32_t fg = term->conf->colors.use_custom.jump_label ? term->conf->colors.jump_label.fg diff --git a/shm.c b/shm.c index d5c94c8b..acc14a68 100644 --- a/shm.c +++ b/shm.c @@ -68,21 +68,28 @@ struct buffer_pool { size_t ref_count; }; +struct buffer_chain; struct buffer_private { struct buffer public; + struct buffer_chain *chain; size_t ref_count; - size_t size; bool busy; /* Owned by compositor */ - unsigned long cookie; struct buffer_pool *pool; off_t offset; /* Offset into memfd where data begins */ + size_t size; + + bool scrollable; +}; +struct buffer_chain { + tll(struct buffer_private *) bufs; + struct wl_shm *shm; + size_t pix_instances; bool scrollable; }; -static tll(struct buffer_private *) buffers; static tll(struct buffer_private *) deferred; #undef MEASURE_SHM_ALLOCS @@ -151,7 +158,7 @@ buffer_destroy(struct buffer_private *buf) } static bool -buffer_unref_no_remove_from_cache(struct buffer_private *buf) +buffer_unref_no_remove_from_chain(struct buffer_private *buf) { xassert(buf->ref_count > 0); buf->ref_count--; @@ -169,34 +176,13 @@ buffer_unref_no_remove_from_cache(struct buffer_private *buf) void shm_fini(void) { - size_t busy_count UNUSED = 0; - size_t non_busy_count UNUSED = 0; - - tll_foreach(buffers, it) { - if (it->item->busy) - busy_count++; - else - non_busy_count++; - - buffer_destroy(it->item); - tll_remove(buffers, it); - } - - xassert(busy_count == 0); - busy_count = tll_length(deferred); + LOG_DBG("deferred buffers: %zu", tll_length(deferred)); tll_foreach(deferred, it) { buffer_destroy(it->item); tll_remove(deferred, it); } - LOG_DBG("buffers left: busy=%zu, non-busy=%zu", busy_count, non_busy_count); - - if (non_busy_count > 0) { - BUG("%zu non-busy buffers remaining (%zu buffers in total)", - non_busy_count, busy_count + non_busy_count); - } - #if defined(MEASURE_SHM_ALLOCS) && MEASURE_SHM_ALLOCS LOG_INFO("max total allocations was: %zu MB", max_alloced / 1024 / 1024); #endif @@ -207,27 +193,25 @@ buffer_release(void *data, struct wl_buffer *wl_buffer) { struct buffer_private *buffer = data; - LOG_DBG("release: cookie=%lx (buf=%p, total buffer count: %zu)", - buffer->cookie, (void *)buffer, tll_length(buffers)); - xassert(buffer->public.wl_buf == wl_buffer); xassert(buffer->busy); buffer->busy = false; if (buffer->ref_count == 0) { - LOG_WARN("deferred delete"); - bool found = false; tll_foreach(deferred, it) { if (it->item == buffer) { found = true; - buffer_destroy(buffer); tll_remove(deferred, it); break; } } + buffer_destroy(buffer); + xassert(found); + if (!found) + LOG_WARN("deferred delete: buffer not on the 'deferred' list"); } } @@ -255,7 +239,7 @@ page_size(void) #endif static bool -instantiate_offset(struct wl_shm *shm, struct buffer_private *buf, off_t new_offset) +instantiate_offset(struct buffer_private *buf, off_t new_offset) { xassert(buf->public.data == NULL); xassert(buf->public.pix == NULL); @@ -314,12 +298,11 @@ err: } static void NOINLINE -get_new_buffers(struct wl_shm *shm, size_t count, - struct buffer_description info[static count], - struct buffer *bufs[static count], - size_t pix_instances, bool scrollable, bool immediate_purge) +get_new_buffers(struct buffer_chain *chain, size_t count, + int widths[static count], int heights[static count], + struct buffer *bufs[static count], bool immediate_purge) { - xassert(count == 1 || !scrollable); + xassert(count == 1 || !chain->scrollable); /* * No existing buffer available. Create a new one by: * @@ -335,8 +318,8 @@ get_new_buffers(struct wl_shm *shm, size_t count, size_t total_size = 0; for (size_t i = 0; i < count; i++) { - stride[i] = stride_for_format_and_width(PIXMAN_a8r8g8b8, info[i].width); - sizes[i] = stride[i] * info[i].height; + stride[i] = stride_for_format_and_width(PIXMAN_a8r8g8b8, widths[i]); + sizes[i] = stride[i] * heights[i]; total_size += sizes[i]; } @@ -363,14 +346,18 @@ get_new_buffers(struct wl_shm *shm, size_t count, } #if __SIZEOF_POINTER__ == 8 - off_t offset = scrollable && max_pool_size > 0 ? (max_pool_size / 4) & ~(page_size() - 1) : 0; - off_t memfd_size = scrollable && max_pool_size > 0 ? max_pool_size : total_size; + off_t offset = chain->scrollable && max_pool_size > 0 + ? (max_pool_size / 4) & ~(page_size() - 1) + : 0; + off_t memfd_size = chain->scrollable && max_pool_size > 0 + ? max_pool_size + : total_size; #else off_t offset = 0; off_t memfd_size = total_size; #endif - xassert(scrollable || (offset == 0 && memfd_size == total_size)); + xassert(chain->scrollable || (offset == 0 && memfd_size == total_size)); LOG_DBG("memfd-size: %lu, initial offset: %lu", memfd_size, offset); @@ -397,10 +384,10 @@ get_new_buffers(struct wl_shm *shm, size_t count, #endif } - if (scrollable && !can_punch_hole) { + if (chain->scrollable && !can_punch_hole) { offset = 0; memfd_size = total_size; - scrollable = false; + chain->scrollable = false; if (ftruncate(pool_fd, memfd_size) < 0) { LOG_ERRNO("failed to set size of SHM backing memory file"); @@ -428,13 +415,13 @@ get_new_buffers(struct wl_shm *shm, size_t count, } #endif - wl_pool = wl_shm_create_pool(shm, pool_fd, memfd_size); + wl_pool = wl_shm_create_pool(chain->shm, pool_fd, memfd_size); if (wl_pool == NULL) { LOG_ERR("failed to create SHM pool"); goto err; } - pool = malloc(sizeof(*pool)); + pool = xmalloc(sizeof(*pool)); if (pool == NULL) { LOG_ERRNO("failed to allocate buffer pool"); goto err; @@ -454,22 +441,22 @@ get_new_buffers(struct wl_shm *shm, size_t count, struct buffer_private *buf = xmalloc(sizeof(*buf)); *buf = (struct buffer_private){ .public = { - .width = info[i].width, - .height = info[i].height, + .width = widths[i], + .height = heights[i], .stride = stride[i], - .pix_instances = pix_instances, + .pix_instances = chain->pix_instances, .age = 1234, /* Force a full repaint */ }, - .cookie = info[i].cookie, + .chain = chain, .ref_count = immediate_purge ? 0 : 1, - .size = sizes[i], .busy = true, .pool = pool, - .scrollable = scrollable, .offset = 0, + .size = sizes[i], + .scrollable = chain->scrollable, }; - if (!instantiate_offset(shm, buf, offset)) { + if (!instantiate_offset(buf, offset)) { free(buf); goto err; } @@ -477,7 +464,7 @@ get_new_buffers(struct wl_shm *shm, size_t count, if (immediate_purge) tll_push_front(deferred, buf); else - tll_push_front(buffers, buf); + tll_push_front(chain->bufs, buf); pixman_region32_init(&buf->public.dirty); pool->ref_count++; @@ -518,28 +505,24 @@ err: } void -shm_get_many(struct wl_shm *shm, size_t count, - struct buffer_description info[static count], - struct buffer *bufs[static count], - size_t pix_instances) +shm_get_many(struct buffer_chain *chain, size_t count, + int widths[static count], int heights[static count], + struct buffer *bufs[static count]) { - get_new_buffers(shm, count, info, bufs, pix_instances, false, true); + get_new_buffers(chain, count, widths, heights, bufs, true); } struct buffer * -shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie, - bool scrollable, size_t pix_instances) +shm_get_buffer(struct buffer_chain *chain, int width, int height) { struct buffer_private *cached = NULL; - tll_foreach(buffers, it) { + tll_foreach(chain->bufs, it) { struct buffer_private *buf = it->item; if (buf->public.width != width) continue; if (buf->public.height != height) continue; - if (buf->cookie != cookie) - continue; if (buf->busy) buf->public.age++; @@ -551,13 +534,12 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie, #endif { if (cached == NULL) { - LOG_DBG("cookie=%lx: re-using buffer from cache (buf=%p)", - cookie, (void *)buf); + LOG_DBG("re-using buffer %p from cache", (void *)buf); buf->busy = true; pixman_region32_clear(&buf->public.dirty); free(buf->public.scroll_damage); buf->public.scroll_damage = NULL; - xassert(buf->public.pix_instances == pix_instances); + xassert(buf->public.pix_instances == chain->pix_instances); cached = it->item; } else { /* We have multiple buffers eligible for @@ -567,8 +549,8 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie, shm_unref(&cached->public); cached = buf; } else { - if (buffer_unref_no_remove_from_cache(buf)) - tll_remove(buffers, it); + if (buffer_unref_no_remove_from_chain(buf)) + tll_remove(chain->bufs, it); } } } @@ -578,23 +560,19 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie, return &cached->public; /* Mark old buffers associated with this cookie for purging */ - tll_foreach(buffers, it) { + tll_foreach(chain->bufs, it) { struct buffer_private *buf = it->item; - if (buf->cookie != cookie) - continue; - if (buf->public.width == width && buf->public.height == height) continue; - LOG_DBG("cookie=%lx: marking buffer %p for purging", cookie, (void *)buf); - if (buffer_unref_no_remove_from_cache(buf)) - tll_remove(buffers, it); + LOG_DBG("marking buffer %p for purging", (void *)buf); + if (buffer_unref_no_remove_from_chain(buf)) + tll_remove(chain->bufs, it); } struct buffer *ret; - get_new_buffers(shm, 1, &(struct buffer_description){width, height, cookie}, - &ret, pix_instances, scrollable, false); + get_new_buffers(chain, 1, &width, &height, &ret, false); return ret; } @@ -612,7 +590,7 @@ shm_can_scroll(const struct buffer *_buf) #if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE) static bool -wrap_buffer(struct wl_shm *shm, struct buffer_private *buf, off_t new_offset) +wrap_buffer(struct buffer_private *buf, off_t new_offset) { struct buffer_pool *pool = buf->pool; xassert(pool->ref_count == 1); @@ -649,11 +627,11 @@ wrap_buffer(struct wl_shm *shm, struct buffer_private *buf, off_t new_offset) /* Re-instantiate pixman+wl_buffer+raw pointersw */ buffer_destroy_dont_close(&buf->public); - return instantiate_offset(shm, buf, new_offset); + return instantiate_offset(buf, new_offset); } static bool -shm_scroll_forward(struct wl_shm *shm, struct buffer_private *buf, int rows, +shm_scroll_forward(struct buffer_private *buf, int rows, int top_margin, int top_keep_rows, int bottom_margin, int bottom_keep_rows) { @@ -675,7 +653,7 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer_private *buf, int rows, if (buf->offset + diff + buf->size > max_pool_size) { LOG_DBG("memfd offset wrap around"); - if (!wrap_buffer(shm, buf, 0)) + if (!wrap_buffer(buf, 0)) goto err; } @@ -684,6 +662,7 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer_private *buf, int rows, xassert(new_offset + buf->size <= max_pool_size); #if TIME_SCROLL + struct timeval tot; struct timeval time1; gettimeofday(&time1, NULL); @@ -731,7 +710,7 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer_private *buf, int rows, #endif /* Re-instantiate pixman+wl_buffer+raw pointersw */ - bool ret = instantiate_offset(shm, buf, new_offset); + bool ret = instantiate_offset(buf, new_offset); #if TIME_SCROLL struct timeval time4; @@ -768,7 +747,7 @@ err: } static bool -shm_scroll_reverse(struct wl_shm *shm, struct buffer_private *buf, int rows, +shm_scroll_reverse(struct buffer_private *buf, int rows, int top_margin, int top_keep_rows, int bottom_margin, int bottom_keep_rows) { @@ -780,7 +759,7 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer_private *buf, int rows, const off_t diff = rows * buf->public.stride; if (diff > buf->offset) { LOG_DBG("memfd offset reverse wrap-around"); - if (!wrap_buffer(shm, buf, (max_pool_size - buf->size) & ~(page_size() - 1))) + if (!wrap_buffer(buf, (max_pool_size - buf->size) & ~(page_size() - 1))) goto err; } @@ -837,7 +816,7 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer_private *buf, int rows, #endif /* Re-instantiate pixman+wl_buffer+raw pointers */ - bool ret = instantiate_offset(shm, buf, new_offset); + bool ret = instantiate_offset(buf, new_offset); #if TIME_SCROLL struct timeval time3; @@ -873,7 +852,7 @@ err: #endif /* FALLOC_FL_PUNCH_HOLE */ bool -shm_scroll(struct wl_shm *shm, struct buffer *_buf, int rows, +shm_scroll(struct buffer *_buf, int rows, int top_margin, int top_keep_rows, int bottom_margin, int bottom_keep_rows) { @@ -885,27 +864,22 @@ shm_scroll(struct wl_shm *shm, struct buffer *_buf, int rows, xassert(rows != 0); return rows > 0 - ? shm_scroll_forward(shm, buf, rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows) - : shm_scroll_reverse(shm, buf, -rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows); + ? shm_scroll_forward(buf, rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows) + : shm_scroll_reverse(buf, -rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows); #else return false; #endif } void -shm_purge(struct wl_shm *shm, unsigned long cookie) +shm_purge(struct buffer_chain *chain) { - LOG_DBG("cookie=%lx: purging all buffers", cookie); + LOG_DBG("chain: %p: purging all buffers", (void *)chain); /* Purge old buffers associated with this cookie */ - tll_foreach(buffers, it) { - struct buffer_private *buf = it->item; - - if (buf->cookie != cookie) - continue; - - if (buffer_unref_no_remove_from_cache(buf)) - tll_remove(buffers, it); + tll_foreach(chain->bufs, it) { + if (buffer_unref_no_remove_from_chain(it->item)) + tll_remove(chain->bufs, it); } } @@ -923,12 +897,47 @@ shm_unref(struct buffer *_buf) return; struct buffer_private *buf = (struct buffer_private *)_buf; - tll_foreach(buffers, it) { + struct buffer_chain *chain = buf->chain; + + tll_foreach(chain->bufs, it) { if (it->item != buf) continue; - if (buffer_unref_no_remove_from_cache(buf)) - tll_remove(buffers, it); + if (buffer_unref_no_remove_from_chain(buf)) + tll_remove(chain->bufs, it); break; } } + +void +shm_chain_purge(struct buffer_chain *chain) +{ + tll_foreach(chain->bufs, it) { + struct buffer_private *buf = it->item; + if (buffer_unref_no_remove_from_chain(buf)) + tll_remove(chain->bufs, it); + } +} + +struct buffer_chain * +shm_chain_new(struct wl_shm *shm, bool scrollable, size_t pix_instances) +{ + struct buffer_chain *chain = xmalloc(sizeof(*chain)); + *chain = (struct buffer_chain){ + .bufs = tll_init(), + .shm = shm, + .pix_instances = pix_instances, + .scrollable = scrollable, + }; + return chain; +} + +void +shm_chain_free(struct buffer_chain *chain) +{ + if (chain == NULL) + return; + + shm_chain_purge(chain); + free(chain); +} diff --git a/shm.h b/shm.h index 18941588..180c8525 100644 --- a/shm.h +++ b/shm.h @@ -7,6 +7,8 @@ #include #include +#include + struct damage; struct buffer { @@ -27,28 +29,24 @@ struct buffer { pixman_region32_t dirty; }; -struct buffer_description { - int width; - int height; - unsigned long cookie; -}; - void shm_fini(void); void shm_set_max_pool_size(off_t max_pool_size); +struct buffer_chain; +struct buffer_chain *shm_chain_new( + struct wl_shm *shm, bool scrollable, size_t pix_instances); +void shm_chain_free(struct buffer_chain *chain); + /* * Returns a single buffer. * * May returned a cached buffer. If so, the buffer’s age indicates how * many shm_get_buffer() calls have been made for the same - * width/height/cookie while the buffer was still busy. + * width/height while the buffer was still busy. * * A newly allocated buffer has an age of 1234. */ -struct buffer *shm_get_buffer( - struct wl_shm *shm, int width, int height, unsigned long cookie, - bool scrollable, size_t pix_instances); - +struct buffer *shm_get_buffer(struct buffer_chain *chain, int width, int height); /* * Returns many buffers, described by ‘info’, all sharing the same SHM * buffer pool. @@ -64,25 +62,16 @@ struct buffer *shm_get_buffer( * soon as the compositor releases them. */ void shm_get_many( - struct wl_shm *shm, size_t count, - struct buffer_description info[static count], - struct buffer *bufs[static count], size_t pix_instances); + struct buffer_chain *chain, size_t count, + int widths[static count], int heights[static count], + struct buffer *bufs[static count]); bool shm_can_scroll(const struct buffer *buf); -bool shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows, +bool shm_scroll(struct buffer *buf, int rows, int top_margin, int top_keep_rows, int bottom_margin, int bottom_keep_rows); void shm_addref(struct buffer *buf); void shm_unref(struct buffer *buf); -void shm_purge(struct wl_shm *shm, unsigned long cookie); - -struct terminal; -static inline unsigned long shm_cookie_grid(const struct terminal *term) { return (unsigned long)((uintptr_t)term + 0); } -static inline unsigned long shm_cookie_search(const struct terminal *term) { return (unsigned long)((uintptr_t)term + 1); } -static inline unsigned long shm_cookie_scrollback_indicator(const struct terminal *term) { return (unsigned long)(uintptr_t)term + 2; } -static inline unsigned long shm_cookie_render_timer(const struct terminal *term) { return (unsigned long)(uintptr_t)term + 3; } -static inline unsigned long shm_cookie_csd(const struct terminal *term, int n) { return (unsigned long)((uintptr_t)term + 4 + (n)); } -struct url; -static inline unsigned long shm_cookie_url(const struct url *url) { return (unsigned long)(uintptr_t)url; } +void shm_purge(struct buffer_chain *chain); diff --git a/terminal.c b/terminal.c index c7776f28..72ab1f2b 100644 --- a/terminal.c +++ b/terminal.c @@ -1144,6 +1144,23 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .tab_stops = tll_init(), .wl = wayl, .render = { + .chains = { + .grid = shm_chain_new(wayl->shm, true, 1 + conf->render_worker_count), + .search = shm_chain_new(wayl->shm, false, 1), + .scrollback_indicator = shm_chain_new(wayl->shm, false, 1), + .render_timer = shm_chain_new(wayl->shm, false, 1), + .url = shm_chain_new(wayl->shm, false, 1), + .csd = { + [CSD_SURF_TITLE] = shm_chain_new(wayl->shm, false, 1), + [CSD_SURF_LEFT] = shm_chain_new(wayl->shm, false, 1), + [CSD_SURF_RIGHT] = shm_chain_new(wayl->shm, false, 1), + [CSD_SURF_TOP] = shm_chain_new(wayl->shm, false, 1), + [CSD_SURF_BOTTOM] = shm_chain_new(wayl->shm, false, 1), + [CSD_SURF_MINIMIZE] = shm_chain_new(wayl->shm, false, 1), + [CSD_SURF_MAXIMIZE] = shm_chain_new(wayl->shm, false, 1), + [CSD_SURF_CLOSE] = shm_chain_new(wayl->shm, false, 1), + }, + }, .scrollback_lines = conf->scrollback.lines, .app_sync_updates.timer_fd = app_sync_updates_fd, .title = { @@ -1458,7 +1475,15 @@ term_destroy(struct terminal *term) sem_destroy(&term->render.workers.done); xassert(tll_length(term->render.workers.queue) == 0); tll_free(term->render.workers.queue); + shm_unref(term->render.last_buf); + shm_chain_free(term->render.chains.grid); + shm_chain_free(term->render.chains.search); + shm_chain_free(term->render.chains.scrollback_indicator); + shm_chain_free(term->render.chains.render_timer); + shm_chain_free(term->render.chains.url); + for (size_t i = 0; i < CSD_SURF_COUNT; i++) + shm_chain_free(term->render.chains.csd[i]); tll_free(term->tab_stops); diff --git a/terminal.h b/terminal.h index 5ffa6154..b02036a2 100644 --- a/terminal.h +++ b/terminal.h @@ -21,6 +21,7 @@ #include "fdm.h" #include "macros.h" #include "reaper.h" +#include "shm.h" #include "wayland.h" /* @@ -475,6 +476,15 @@ struct terminal { enum term_surface active_surface; struct { + struct { + struct buffer_chain *grid; + struct buffer_chain *search; + struct buffer_chain *scrollback_indicator; + struct buffer_chain *render_timer; + struct buffer_chain *url; + struct buffer_chain *csd[CSD_SURF_COUNT]; + } chains; + /* Scheduled for rendering, as soon-as-possible */ struct { bool grid; diff --git a/wayland.c b/wayland.c index 880a4cf3..38c80999 100644 --- a/wayland.c +++ b/wayland.c @@ -52,11 +52,10 @@ static void csd_destroy(struct wl_window *win) { struct terminal *term = win->term; - struct wl_shm *shm = term->wl->shm; for (size_t i = 0; i < ALEN(win->csd.surface); i++) { wayl_win_subsurface_destroy(&win->csd.surface[i]); - shm_purge(shm, shm_cookie_csd(term, i)); + shm_purge(term->render.chains.csd[i]); } } @@ -1417,7 +1416,6 @@ wayl_win_destroy(struct wl_window *win) return; struct terminal *term = win->term; - struct wl_shm *shm = term->wl->shm; if (win->csd.move_timeout_fd != -1) close(win->csd.move_timeout_fd); @@ -1472,7 +1470,7 @@ wayl_win_destroy(struct wl_window *win) tll_foreach(win->urls, it) { wayl_win_subsurface_destroy(&it->item.surf); - shm_purge(shm, shm_cookie_url(it->item.url)); + shm_purge(term->render.chains.url); tll_remove(win->urls, it); } @@ -1481,13 +1479,13 @@ wayl_win_destroy(struct wl_window *win) wayl_win_subsurface_destroy(&win->scrollback_indicator); wayl_win_subsurface_destroy(&win->render_timer); - shm_purge(shm, shm_cookie_search(term)); - shm_purge(shm, shm_cookie_scrollback_indicator(term)); - shm_purge(shm, shm_cookie_render_timer(term)); - shm_purge(shm, shm_cookie_grid(term)); + shm_purge(term->render.chains.search); + shm_purge(term->render.chains.scrollback_indicator); + shm_purge(term->render.chains.render_timer); + shm_purge(term->render.chains.grid); for (size_t i = 0; i < ALEN(win->csd.surface); i++) - shm_purge(shm, shm_cookie_csd(term, i)); + shm_purge(term->render.chains.csd[i]); #if defined(HAVE_XDG_ACTIVATION) if (win->xdg_activation_token != NULL) -- cgit v1.3 From 5f0ceb72f176caf7673ea1d4bc5195f26f4b4df7 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 16 Jul 2021 16:45:36 +0200 Subject: csi: erase scrollback: cancel selection if it touches the scrollback This breaks out the scrollback erasing logic for \E[3J from csi.c, and moves it to the new function term_erase_scrollback(), and changes the logic to calculate the start and end row (absolute) numbers of the scrollback, and only iterate those, instead of iterating *all* rows, filtering out those that are on-screen. It also adds an intersection range check of the selection range, and cancels the selection if it touches any of the deleted scrollback rows. This fixes a crash when trying to render the next frame, since the selection now references rows that have been freed. Closes #633 --- csi.c | 21 +-------------------- selection.c | 4 +--- terminal.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ terminal.h | 1 + 4 files changed, 66 insertions(+), 23 deletions(-) (limited to 'terminal.h') diff --git a/csi.c b/csi.c index 80ee75cd..dc5a018a 100644 --- a/csi.c +++ b/csi.c @@ -917,26 +917,7 @@ csi_dispatch(struct terminal *term, uint8_t final) case 3: { /* Erase scrollback */ - int end = (term->grid->offset + term->rows - 1) % term->grid->num_rows; - for (size_t i = 0; i < term->grid->num_rows; i++) { - if (end >= term->grid->offset) { - /* Not wrapped */ - if (i >= term->grid->offset && i <= end) - continue; - } else { - /* Wrapped */ - if (i >= term->grid->offset || i <= end) - continue; - } - - if (term->render.last_cursor.row == term->grid->rows[i]) - term->render.last_cursor.row = NULL; - - grid_row_free(term->grid->rows[i]); - term->grid->rows[i] = NULL; - } - term->grid->view = term->grid->offset; - term_damage_view(term); + term_erase_scrollback(term); break; } diff --git a/selection.c b/selection.c index 57ab8bf6..537614f6 100644 --- a/selection.c +++ b/selection.c @@ -69,10 +69,8 @@ selection_on_rows(const struct terminal *term, int row_start, int row_end) end = tmp; } - if (row_start >= start->row && row_end <= end->row) { - LOG_INFO("ON ROWS"); + if (row_start >= start->row && row_end <= end->row) return true; - } return false; } diff --git a/terminal.c b/terminal.c index 72ab1f2b..df6ddbed 100644 --- a/terminal.c +++ b/terminal.c @@ -2011,6 +2011,69 @@ term_erase(struct terminal *term, const struct coord *start, const struct coord sixel_overwrite_by_row(term, end->row, 0, end->col + 1); } +void +term_erase_scrollback(struct terminal *term) +{ + const int mask = term->grid->num_rows - 1; + const int start = (term->grid->offset + term->rows) & mask; + const int end = (term->grid->offset - 1) & mask; + const int sel_start = term->selection.start.row; + const int sel_end = term->selection.end.row; + + if (sel_end >= 0) { + /* + * Cancel selection if it touches any of the rows in the + * scrollback, since we can’t have the selection reference + * soon-to-be deleted rows. + * + * This is done by range checking the selection range against + * the scrollback range. + * + * To make this comparison simpler, the start/end absolute row + * numbers are “rebased” against the scrollback start, where + * row 0 is the *first* row in the scrollback. A high number + * thus means the row is further *down* in the scrollback, + * closer to the screen bottom. + */ + int scrollback_start = term->grid->offset + term->rows; + + int rel_sel_start = sel_start - scrollback_start + term->grid->num_rows; + int rel_sel_end = sel_end - scrollback_start + term->grid->num_rows; + + int rel_start = start - scrollback_start + term->grid->num_rows; + int rel_end = end - scrollback_start + term->grid->num_rows; + + rel_sel_start &= mask; + rel_sel_end &= mask; + rel_start &= mask; + rel_end &= mask; + + if ((rel_sel_start <= rel_start && rel_sel_end >= rel_start) || + (rel_sel_start <= rel_end && rel_sel_end >= rel_end) || + (rel_sel_start >= rel_start && rel_sel_end <= rel_end)) + { + selection_cancel(term); + } + } + + for (int i = start;; i = (i + 1) & mask) { + struct row *row = term->grid->rows[i]; + if (row != NULL) { + if (term->render.last_cursor.row == row) + term->render.last_cursor.row = NULL; + + grid_row_free(row); + term->grid->rows[i] = NULL; + } + + if (i == end) + break; + } + + term->grid->view = term->grid->offset; + term_damage_view(term); +} + int term_row_rel_to_abs(const struct terminal *term, int row) { diff --git a/terminal.h b/terminal.h index b02036a2..d699d644 100644 --- a/terminal.h +++ b/terminal.h @@ -662,6 +662,7 @@ void term_damage_scroll( void term_erase( struct terminal *term, const struct coord *start, const struct coord *end); +void term_erase_scrollback(struct terminal *term); int term_row_rel_to_abs(const struct terminal *term, int row); void term_cursor_home(struct terminal *term); -- cgit v1.3 From 20fc80e57e365f2e002eead7de110cb23ffa7adc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 18 Jul 2021 16:46:43 +0200 Subject: render: use a single backing SHM pool for CSD surface buffers --- render.c | 73 +++++++++++++++++++++++++++++++------------------------------- terminal.c | 14 ++---------- terminal.h | 2 +- wayland.c | 11 ++++------ 4 files changed, 44 insertions(+), 56 deletions(-) (limited to 'terminal.h') diff --git a/render.c b/render.c index 7bcad519..7750dd19 100644 --- a/render.c +++ b/render.c @@ -1575,20 +1575,16 @@ render_csd_part(struct terminal *term, } static void -render_csd_title(struct terminal *term) +render_csd_title(struct terminal *term, const struct csd_data *info, + struct buffer *buf) { xassert(term->window->csd_mode == CSD_YES); - struct csd_data info = get_csd_data(term, CSD_SURF_TITLE); struct wl_surface *surf = term->window->csd.surface[CSD_SURF_TITLE].surf; + xassert(info->width > 0 && info->height > 0); - xassert(info.width > 0 && info.height > 0); - - xassert(info.width % term->scale == 0); - xassert(info.height % term->scale == 0); - - struct buffer_chain *chain = term->render.chains.csd[CSD_SURF_TITLE]; - struct buffer *buf = shm_get_buffer(chain, info.width, info.height); + xassert(info->width % term->scale == 0); + xassert(info->height % term->scale == 0); uint32_t _color = term->conf->colors.fg; uint16_t alpha = 0xffff; @@ -1602,30 +1598,27 @@ render_csd_title(struct terminal *term) _color = color_dim(_color); pixman_color_t color = color_hex_to_pixman_with_alpha(_color, alpha); - render_csd_part(term, surf, buf, info.width, info.height, &color); + render_csd_part(term, surf, buf, info->width, info->height, &color); csd_commit(term, surf, buf); } static void -render_csd_border(struct terminal *term, enum csd_surface surf_idx) +render_csd_border(struct terminal *term, enum csd_surface surf_idx, + const struct csd_data *info, struct buffer *buf) { xassert(term->window->csd_mode == CSD_YES); xassert(surf_idx >= CSD_SURF_LEFT && surf_idx <= CSD_SURF_BOTTOM); - struct csd_data info = get_csd_data(term, surf_idx); struct wl_surface *surf = term->window->csd.surface[surf_idx].surf; - if (info.width == 0 || info.height == 0) + if (info->width == 0 || info->height == 0) return; - xassert(info.width % term->scale == 0); - xassert(info.height % term->scale == 0); - - struct buffer_chain *chain = term->render.chains.csd[surf_idx]; - struct buffer *buf = shm_get_buffer(chain, info.width, info.height); + xassert(info->width % term->scale == 0); + xassert(info->height % term->scale == 0); pixman_color_t color = color_hex_to_pixman_with_alpha(0, 0); - render_csd_part(term, surf, buf, info.width, info.height, &color); + render_csd_part(term, surf, buf, info->width, info->height, &color); csd_commit(term, surf, buf); } @@ -1792,22 +1785,19 @@ render_csd_button_close(struct terminal *term, struct buffer *buf) } static void -render_csd_button(struct terminal *term, enum csd_surface surf_idx) +render_csd_button(struct terminal *term, enum csd_surface surf_idx, + const struct csd_data *info, struct buffer *buf) { xassert(term->window->csd_mode == CSD_YES); xassert(surf_idx >= CSD_SURF_MINIMIZE && surf_idx <= CSD_SURF_CLOSE); - struct csd_data info = get_csd_data(term, surf_idx); struct wl_surface *surf = term->window->csd.surface[surf_idx].surf; - if (info.width == 0 || info.height == 0) + if (info->width == 0 || info->height == 0) return; - xassert(info.width % term->scale == 0); - xassert(info.height % term->scale == 0); - - struct buffer_chain *chain = term->render.chains.csd[surf_idx]; - struct buffer *buf = shm_get_buffer(chain, info.width, info.height); + xassert(info->width % term->scale == 0); + xassert(info->height % term->scale == 0); uint32_t _color; uint16_t alpha = 0xffff; @@ -1856,7 +1846,7 @@ render_csd_button(struct terminal *term, enum csd_surface surf_idx) _color = color_dim(_color); pixman_color_t color = color_hex_to_pixman_with_alpha(_color, alpha); - render_csd_part(term, surf, buf, info.width, info.height, &color); + render_csd_part(term, surf, buf, info->width, info->height, &color); switch (surf_idx) { case CSD_SURF_MINIMIZE: render_csd_button_minimize(term, buf); break; @@ -1880,12 +1870,16 @@ render_csd(struct terminal *term) if (term->window->is_fullscreen) return; + struct csd_data infos[CSD_SURF_COUNT]; + int widths[CSD_SURF_COUNT]; + int heights[CSD_SURF_COUNT]; + for (size_t i = 0; i < CSD_SURF_COUNT; i++) { - struct csd_data info = get_csd_data(term, i); - const int x = info.x; - const int y = info.y; - const int width = info.width; - const int height = info.height; + infos[i] = get_csd_data(term, i); + const int x = infos[i].x; + const int y = infos[i].y; + const int width = infos[i].width; + const int height = infos[i].height; struct wl_surface *surf = term->window->csd.surface[i].surf; struct wl_subsurface *sub = term->window->csd.surface[i].sub; @@ -1894,20 +1888,27 @@ render_csd(struct terminal *term) xassert(sub != NULL); if (width == 0 || height == 0) { + widths[i] = heights[i] = 0; wl_subsurface_set_position(sub, 0, 0); wl_surface_attach(surf, NULL, 0, 0); wl_surface_commit(surf); continue; } + widths[i] = width; + heights[i] = height; + wl_subsurface_set_position(sub, x / term->scale, y / term->scale); } + struct buffer *bufs[CSD_SURF_COUNT]; + shm_get_many(term->render.chains.csd, CSD_SURF_COUNT, widths, heights, bufs); + for (size_t i = CSD_SURF_LEFT; i <= CSD_SURF_BOTTOM; i++) - render_csd_border(term, i); + render_csd_border(term, i, &infos[i], bufs[i]); for (size_t i = CSD_SURF_MINIMIZE; i <= CSD_SURF_CLOSE; i++) - render_csd_button(term, i); - render_csd_title(term); + render_csd_button(term, i, &infos[i], bufs[i]); + render_csd_title(term, &infos[CSD_SURF_TITLE], bufs[CSD_SURF_TITLE]); } static void diff --git a/terminal.c b/terminal.c index af5172bb..46e7fc01 100644 --- a/terminal.c +++ b/terminal.c @@ -1150,16 +1150,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .scrollback_indicator = shm_chain_new(wayl->shm, false, 1), .render_timer = shm_chain_new(wayl->shm, false, 1), .url = shm_chain_new(wayl->shm, false, 1), - .csd = { - [CSD_SURF_TITLE] = shm_chain_new(wayl->shm, false, 1), - [CSD_SURF_LEFT] = shm_chain_new(wayl->shm, false, 1), - [CSD_SURF_RIGHT] = shm_chain_new(wayl->shm, false, 1), - [CSD_SURF_TOP] = shm_chain_new(wayl->shm, false, 1), - [CSD_SURF_BOTTOM] = shm_chain_new(wayl->shm, false, 1), - [CSD_SURF_MINIMIZE] = shm_chain_new(wayl->shm, false, 1), - [CSD_SURF_MAXIMIZE] = shm_chain_new(wayl->shm, false, 1), - [CSD_SURF_CLOSE] = shm_chain_new(wayl->shm, false, 1), - }, + .csd = shm_chain_new(wayl->shm, false, 1), }, .scrollback_lines = conf->scrollback.lines, .app_sync_updates.timer_fd = app_sync_updates_fd, @@ -1482,8 +1473,7 @@ term_destroy(struct terminal *term) shm_chain_free(term->render.chains.scrollback_indicator); shm_chain_free(term->render.chains.render_timer); shm_chain_free(term->render.chains.url); - for (size_t i = 0; i < CSD_SURF_COUNT; i++) - shm_chain_free(term->render.chains.csd[i]); + shm_chain_free(term->render.chains.csd); tll_free(term->tab_stops); diff --git a/terminal.h b/terminal.h index d699d644..688e42cc 100644 --- a/terminal.h +++ b/terminal.h @@ -482,7 +482,7 @@ struct terminal { struct buffer_chain *scrollback_indicator; struct buffer_chain *render_timer; struct buffer_chain *url; - struct buffer_chain *csd[CSD_SURF_COUNT]; + struct buffer_chain *csd; } chains; /* Scheduled for rendering, as soon-as-possible */ diff --git a/wayland.c b/wayland.c index 38c80999..0679dee3 100644 --- a/wayland.c +++ b/wayland.c @@ -53,10 +53,9 @@ csd_destroy(struct wl_window *win) { struct terminal *term = win->term; - for (size_t i = 0; i < ALEN(win->csd.surface); i++) { + for (size_t i = 0; i < ALEN(win->csd.surface); i++) wayl_win_subsurface_destroy(&win->csd.surface[i]); - shm_purge(term->render.chains.csd[i]); - } + shm_purge(term->render.chains.csd); } static void @@ -1470,7 +1469,6 @@ wayl_win_destroy(struct wl_window *win) tll_foreach(win->urls, it) { wayl_win_subsurface_destroy(&it->item.surf); - shm_purge(term->render.chains.url); tll_remove(win->urls, it); } @@ -1483,9 +1481,8 @@ wayl_win_destroy(struct wl_window *win) shm_purge(term->render.chains.scrollback_indicator); shm_purge(term->render.chains.render_timer); shm_purge(term->render.chains.grid); - - for (size_t i = 0; i < ALEN(win->csd.surface); i++) - shm_purge(term->render.chains.csd[i]); + shm_purge(term->render.chains.url); + shm_purge(term->render.chains.csd); #if defined(HAVE_XDG_ACTIVATION) if (win->xdg_activation_token != NULL) -- cgit v1.3