shm: refactor: move away from a single, global, buffer list

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.
This commit is contained in:
Daniel Eklöf 2021-07-16 16:48:49 +02:00
parent 4efb34927e
commit 53851e13ec
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
6 changed files with 190 additions and 164 deletions

View file

@ -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 theyre 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