From 0b74124dce0fa128d865a04c89528c6fec2ec2ed Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 8 Nov 2026 08:12:27 +0530 Subject: [PATCH] Intern cell styles and store only CPUCells in the scrollback The formatting of cells (colors and SGR attributes) is now interned per screen in a StyleTable and cells store a 17 bit index into it in the previously unused bits of CPUCell. Render data (sprite and mark) is kept in a separate RenderCell array for screen lines and in a small render cache for history lines being displayed, so the scrollback stores only CPUCells, 12 bytes per cell instead of 32. GPUCell is now only the upload format, built from the CPUCell, its style and its RenderCell. When the table fills up, it is compacted at the start of drawing text, reducing the color precision of cells in the scrollback if needed, cells on screen always keep their exact styles. The map from styles to indices is a verstable set of indices with a small cache of recent styles in front of it. benchmark.py throughput: ascii +48%, unicode +29%, csi +10%, truecolor text -22%, truecolor half blocks -9%. --- benchmark.py | 2 +- kitty/cursor.c | 226 ++++++++++----------- kitty/data-types.h | 35 +++- kitty/fast_data_types.pyi | 5 + kitty/fonts.c | 54 +++-- kitty/history.c | 76 +++++-- kitty/history.h | 11 +- kitty/line-buf.c | 57 +++--- kitty/line-buf.h | 2 +- kitty/line.c | 118 ++++++----- kitty/line.h | 113 ++++++----- kitty/lineops.h | 56 ++--- kitty/resize.c | 43 ++-- kitty/screen.c | 415 +++++++++++++++++++++++++------------- kitty/screen.h | 12 +- kitty/shaders.c | 4 +- kitty/style-table.c | 312 ++++++++++++++++++++++++++++ kitty/style-table.h | 69 +++++++ kitty/text-cache.c | 14 ++ kitty/text-cache.h | 2 + kitty_tests/base.py | 10 + kitty_tests/styles.py | 326 ++++++++++++++++++++++++++++++ 22 files changed, 1476 insertions(+), 486 deletions(-) create mode 100644 kitty/style-table.c create mode 100644 kitty/style-table.h create mode 100644 kitty_tests/styles.py diff --git a/benchmark.py b/benchmark.py index 8acae2f70..9849f3fee 100755 --- a/benchmark.py +++ b/benchmark.py @@ -25,7 +25,7 @@ from kitty.fast_data_types import ChildMonitor, Region, Screen, safe_pipe, set_o from kitty.utils import read_screen_size BENCHMARK_WINDOW_ID = 1 -PARSING_BENCHMARKS = ('ascii', 'unicode', 'unique_unicode', 'csi', 'images', 'long_escape_codes') +PARSING_BENCHMARKS = ('ascii', 'unicode', 'unique_unicode', 'csi', 'images', 'long_escape_codes', 'truecolor_text', 'palette16_text', 'halfblocks') ALL_BENCHMARKS = PARSING_BENCHMARKS + ('tab_bar',) TAB_BAR_STYLES = ('fade', 'separator', 'powerline', 'slant') TAB_BAR_NUM_TABS = (10, 50, 200) diff --git a/kitty/cursor.c b/kitty/cursor.c index fc39dac76..39b578c0f 100644 --- a/kitty/cursor.c +++ b/kitty/cursor.c @@ -25,7 +25,7 @@ dealloc(Cursor *self) { #define EQ(x) (a->x == b->x) static int __eq__(Cursor *a, Cursor *b) { - return memcmp(&a->sgr, &b->sgr, sizeof(a->sgr)) == 0 && EQ(x) && EQ(y) && EQ(shape) && EQ(non_blinking); + return memcmp(&a->style, &b->style, sizeof(a->style)) == 0 && EQ(x) && EQ(y) && EQ(shape) && EQ(non_blinking); } #undef EQ @@ -41,21 +41,21 @@ repr(Cursor *self) { self->y, (self->shape < NUM_OF_CURSOR_SHAPES ? cursor_names[self->shape] : "INVALID"), BOOL(!self->non_blinking), - self->sgr.fg, - self->sgr.bg, - BOOL(self->sgr.bold), - BOOL(self->sgr.italic), - BOOL(self->sgr.reverse), - BOOL(self->sgr.strikethrough), - BOOL(self->sgr.dim), - self->sgr.decoration, - self->sgr.decoration_fg, - BOOL(self->sgr.blink)); + self->style.fg, + self->style.bg, + BOOL(self->style.attrs.bold), + BOOL(self->style.attrs.italic), + BOOL(self->style.attrs.reverse), + BOOL(self->style.attrs.strike), + BOOL(self->style.attrs.dim), + (int)self->style.attrs.decoration, + self->style.decoration_fg, + BOOL(self->style.attrs.blink)); } void cursor_reset_display_attrs(Cursor *self) { - zero_at_ptr(&self->sgr); + zero_at_ptr(&self->style); } @@ -100,40 +100,40 @@ cursor_from_sgr(Cursor *self, int *params, unsigned int count, bool is_group) { attr = params[i++]; switch (attr) { case 0: cursor_reset_display_attrs(self); break; - case 1: self->sgr.bold = true; break; - case 2: self->sgr.dim = true; break; - case 3: self->sgr.italic = true; break; + case 1: self->style.attrs.bold = true; break; + case 2: self->style.attrs.dim = true; break; + case 3: self->style.attrs.italic = true; break; case 4: if (is_group && i < count) { - self->sgr.decoration = MIN(5, params[i]); + self->style.attrs.decoration = MIN(5, params[i]); i++; - } else self->sgr.decoration = 1; + } else self->style.attrs.decoration = 1; break; - case 5: self->sgr.blink = true; break; - case 7: self->sgr.reverse = true; break; - case 9: self->sgr.strikethrough = true; break; - case 21: self->sgr.decoration = 2; break; - case 221: self->sgr.bold = false; break; - case 222: self->sgr.dim = false; break; + case 5: self->style.attrs.blink = true; break; + case 7: self->style.attrs.reverse = true; break; + case 9: self->style.attrs.strike = true; break; + case 21: self->style.attrs.decoration = 2; break; + case 221: self->style.attrs.bold = false; break; + case 222: self->style.attrs.dim = false; break; case 22: - self->sgr.bold = false; - self->sgr.dim = false; + self->style.attrs.bold = false; + self->style.attrs.dim = false; break; - case 23: self->sgr.italic = false; break; - case 24: self->sgr.decoration = 0; break; - case 25: self->sgr.blink = false; break; - case 27: self->sgr.reverse = false; break; - case 29: self->sgr.strikethrough = false; break; - case 30 ... 37: self->sgr.fg = ((attr - 30) << 8) | 1; break; - case 38: SET_COLOR(sgr.fg); - case 39: self->sgr.fg = 0; break; - case 40 ... 47: self->sgr.bg = ((attr - 40) << 8) | 1; break; - case 48: SET_COLOR(sgr.bg); - case 49: self->sgr.bg = 0; break; - case 90 ... 97: self->sgr.fg = ((attr - 90 + 8) << 8) | 1; break; - case 100 ... 107: self->sgr.bg = ((attr - 100 + 8) << 8) | 1; break; - case DECORATION_FG_CODE: SET_COLOR(sgr.decoration_fg); - case DECORATION_FG_CODE + 1: self->sgr.decoration_fg = 0; break; + case 23: self->style.attrs.italic = false; break; + case 24: self->style.attrs.decoration = 0; break; + case 25: self->style.attrs.blink = false; break; + case 27: self->style.attrs.reverse = false; break; + case 29: self->style.attrs.strike = false; break; + case 30 ... 37: self->style.fg = ((attr - 30) << 8) | 1; break; + case 38: SET_COLOR(style.fg); + case 39: self->style.fg = 0; break; + case 40 ... 47: self->style.bg = ((attr - 40) << 8) | 1; break; + case 48: SET_COLOR(style.bg); + case 49: self->style.bg = 0; break; + case 90 ... 97: self->style.fg = ((attr - 90 + 8) << 8) | 1; break; + case 100 ... 107: self->style.bg = ((attr - 100 + 8) << 8) | 1; break; + case DECORATION_FG_CODE: SET_COLOR(style.decoration_fg); + case DECORATION_FG_CODE + 1: self->style.decoration_fg = 0; break; } if (is_group) break; } @@ -141,43 +141,28 @@ cursor_from_sgr(Cursor *self, int *params, unsigned int count, bool is_group) { END_ALLOW_CASE_RANGE } -void -apply_sgr_to_cells(GPUCell *first_cell, unsigned int cell_count, int *params, unsigned int count, bool is_group) { -#define RANGE for (unsigned c = 0; c < cell_count; c++, cell++) +static void +apply_sgr_to_style(Style *st, int *params, unsigned int count, bool is_group) { + // Apply SGR params to a style as DECCARA does #define SET_COLOR(which) \ { \ color_type color = 0; \ parse_color(params, &i, count, &color); \ - if (color) { \ - RANGE { cell->which = color; } \ - } \ + if (color) st->which = color; \ } \ break; -#define SIMPLE(which, val) \ - RANGE { cell->which = (val); } \ +#define SIMPLE(which, val) \ + st->which = (val); \ break; -#define S(which, val) \ - RANGE { cell->attrs.which = (val); } \ +#define S(which, val) \ + st->attrs.which = (val); \ break; unsigned int i = 0, attr; - if (!count) { - params[0] = 0; - count = 1; - } while (i < count) { - GPUCell *cell = first_cell; attr = params[i++]; switch (attr) { - case 0: { - const CellAttrs remove_sgr_mask = {.val = ~SGR_MASK}; - RANGE { - cell->attrs.val &= remove_sgr_mask.val; - cell->fg = 0; - cell->bg = 0; - cell->decoration_fg = 0; - } - } break; + case 0: zero_at_ptr(st); break; case 1: S(bold, true); case 2: S(dim, true); case 3: S(italic, true); @@ -196,10 +181,8 @@ apply_sgr_to_cells(GPUCell *first_cell, unsigned int cell_count, int *params, un case 221: S(bold, false); case 222: S(dim, false); case 22: - RANGE { - cell->attrs.bold = false; - cell->attrs.dim = false; - } + st->attrs.bold = false; + st->attrs.dim = false; break; case 23: S(italic, false); case 24: S(decoration, 0); @@ -220,20 +203,34 @@ apply_sgr_to_cells(GPUCell *first_cell, unsigned int cell_count, int *params, un if (is_group) break; } #undef SET_COLOR -#undef RANGE #undef SIMPLE #undef S } +void +apply_sgr_to_cells(StyleTable *t, CPUCell *cells, unsigned int cell_count, int *params, unsigned int count, bool is_group) { + if (!count) { + params[0] = 0; + count = 1; + } + // Cells in a region usually have only a few distinct styles, remember the last mapping + uint32_t last_old = UINT32_MAX, last_new = 0; + for (unsigned c = 0; c < cell_count; c++) { + const uint32_t old = cells[c].style_idx; + if (old != last_old) { + Style st = *style_table_get(t, old); + apply_sgr_to_style(&st, params, count, is_group); + last_old = old; + last_new = style_table_intern(t, &st); + } + cells[c].style_idx = last_new; + } +} + const char * cursor_as_sgr(const Cursor *self) { - GPUCell blank_cell = {0}, cursor_cell = { - .attrs = cursor_to_attrs(self), - .fg = self->sgr.fg & COL_MASK, - .bg = self->sgr.bg & COL_MASK, - .decoration_fg = self->sgr.decoration_fg & COL_MASK, - }; - return cell_as_sgr(&cursor_cell, &blank_cell); + static const Style blank = {0}; + return style_as_sgr(&self->style, &blank); } static PyObject * @@ -260,7 +257,8 @@ cursor_copy_to(Cursor *src, Cursor *dest) { CCY(shape); CCY(non_blinking); #undef CCY - memcpy(&dest->sgr, &src->sgr, sizeof(dest->sgr)); + dest->style = src->style; + dest->style_idx = src->style_idx; } static PyObject *copy(Cursor *self, PyObject *); @@ -268,26 +266,24 @@ static PyObject *copy(Cursor *self, PyObject *); // Boilerplate {{{ -#define SGR_BOOL_GETSET(x) \ - static PyObject *x##_get(Cursor *self, void UNUSED *closure) { \ - PyObject *ans = self->sgr.x ? Py_True : Py_False; \ - Py_INCREF(ans); \ - return ans; \ - } \ - static int x##_set(Cursor *self, PyObject *value, void UNUSED *closure) { \ - if (value == NULL) { \ - PyErr_SetString(PyExc_TypeError, "Cannot delete attribute"); \ - return -1; \ - } \ - self->sgr.x = PyObject_IsTrue(value) ? true : false; \ - return 0; \ +#define SGR_BOOL_GETSET(x, field) \ + static PyObject *x##_get(Cursor *self, void UNUSED *closure) { return Py_NewRef(self->style.attrs.field ? Py_True : Py_False); } \ + static int x##_set(Cursor *self, PyObject *value, void UNUSED *closure) { \ + if (value == NULL) { \ + PyErr_SetString(PyExc_TypeError, "Cannot delete attribute"); \ + return -1; \ + } \ + int q = PyObject_IsTrue(value); \ + if (q < 0) return -1; \ + self->style.attrs.field = q ? 1 : 0; \ + return 0; \ } -SGR_BOOL_GETSET(bold) -SGR_BOOL_GETSET(italic) -SGR_BOOL_GETSET(reverse) -SGR_BOOL_GETSET(strikethrough) -SGR_BOOL_GETSET(dim) +SGR_BOOL_GETSET(bold, bold) +SGR_BOOL_GETSET(italic, italic) +SGR_BOOL_GETSET(reverse, reverse) +SGR_BOOL_GETSET(strikethrough, strike) +SGR_BOOL_GETSET(dim, dim) #undef SGR_BOOL_GETSET static PyObject * @@ -309,7 +305,7 @@ blink_set(Cursor *self, PyObject *value, void UNUSED *closure) { static PyObject * text_blink_get(Cursor *self, void UNUSED *closure) { - return Py_NewRef(self->sgr.blink ? Py_True : Py_False); + return Py_NewRef(self->style.attrs.blink ? Py_True : Py_False); } static int @@ -320,30 +316,32 @@ text_blink_set(Cursor *self, PyObject *value, void UNUSED *closure) { } int blink = PyObject_IsTrue(value); if (blink < 0) return -1; - self->sgr.blink = blink; + self->style.attrs.blink = blink; return 0; } -#define SGR_UINT_GETSET(x) \ - static PyObject *x##_get(Cursor *self, void UNUSED *closure) { return PyLong_FromUnsignedLong((unsigned long)self->sgr.x); } \ - static int x##_set(Cursor *self, PyObject *value, void UNUSED *closure) { \ - if (value == NULL) { \ - PyErr_SetString(PyExc_TypeError, "Cannot delete attribute"); \ - return -1; \ - } \ - if (!PyLong_Check(value)) { \ - PyErr_SetString(PyExc_TypeError, "value must be an int"); \ - return -1; \ - } \ - self->sgr.x = PyLong_AsUnsignedLong(value); \ - return 0; \ +#define SGR_UINT_GETSET(x, field) \ + static PyObject *x##_get(Cursor *self, void UNUSED *closure) { return PyLong_FromUnsignedLong((unsigned long)self->style.field); } \ + static int x##_set(Cursor *self, PyObject *value, void UNUSED *closure) { \ + if (value == NULL) { \ + PyErr_SetString(PyExc_TypeError, "Cannot delete attribute"); \ + return -1; \ + } \ + if (!PyLong_Check(value)) { \ + PyErr_SetString(PyExc_TypeError, "value must be an int"); \ + return -1; \ + } \ + unsigned long v = PyLong_AsUnsignedLong(value); \ + if (v == (unsigned long)-1 && PyErr_Occurred()) return -1; \ + self->style.field = v; \ + return 0; \ } -SGR_UINT_GETSET(decoration) -SGR_UINT_GETSET(fg) -SGR_UINT_GETSET(bg) -SGR_UINT_GETSET(decoration_fg) +SGR_UINT_GETSET(decoration, attrs.decoration) +SGR_UINT_GETSET(fg, fg) +SGR_UINT_GETSET(bg, bg) +SGR_UINT_GETSET(decoration_fg, decoration_fg) static PyMemberDef members[] = { {"x", T_UINT, offsetof(Cursor, x), 0, "x"}, diff --git a/kitty/data-types.h b/kitty/data-types.h index 2b79649b9..065bb60ae 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -92,6 +92,31 @@ typedef uint16_t glyph_index; typedef uint32_t pixel; typedef unsigned int index_type; typedef uint32_t sprite_index; +typedef union CellAttrs { + struct { + uint16_t decoration : 3; + uint16_t bold : 1; + uint16_t italic : 1; + uint16_t reverse : 1; + uint16_t strike : 1; + uint16_t dim : 1; + uint16_t blink : 1; + uint16_t mark : 2; + // named so that initializers zero it, see CPUCell + uint32_t padding : 21; + }; + uint32_t val; +} CellAttrs; +static_assert(sizeof(CellAttrs) == sizeof(uint32_t), "Fix the ordering of CellAttrs"); + +// The SGR formatting of a cell. All zero is the default style. Styles are +// interned per screen in a StyleTable, see style-table.h +typedef struct Style { + color_type fg, bg, decoration_fg; + CellAttrs attrs; // the mark bits are always zero +} Style; +static_assert(sizeof(Style) == 4 * sizeof(uint32_t), "Style must have no padding as it is compared and hashed bytewise"); + typedef enum CursorShapes { NO_CURSOR_SHAPE, CURSOR_BLOCK, CURSOR_BEAM, CURSOR_UNDERLINE, CURSOR_HOLLOW, NUM_OF_CURSOR_SHAPES } CursorShape; typedef enum { DISABLE_LIGATURES_NEVER, DISABLE_LIGATURES_CURSOR, DISABLE_LIGATURES_ALWAYS } DisableLigature; @@ -163,7 +188,7 @@ typedef struct ImageAnchorPosition { #define COPY_CELL(src, s, dest, d) \ (dest)->cpu_cells[d] = (src)->cpu_cells[s]; \ - (dest)->gpu_cells[d] = (src)->gpu_cells[s]; + if ((dest)->render_cells && (src)->render_cells) (dest)->render_cells[d] = (src)->render_cells[s]; #define COPY_SELF_CELL(s, d) COPY_CELL(self, s, self, d) @@ -258,11 +283,9 @@ typedef struct { bool non_blinking; CursorShape shape; - struct { - bool bold, italic, reverse, strikethrough, dim, blink; - uint8_t decoration; - color_type fg, bg, decoration_fg; - } sgr; + Style style; + // A cache of the index of style in the StyleTable of the screen, see screen_cursor_style_idx() + uint32_t style_idx; } Cursor; typedef struct { diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 600420071..d8dfec3e3 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1194,6 +1194,11 @@ class Screen: def test_create_write_buffer(self) -> memoryview: ... def test_commit_write_buffer(self, inp: memoryview, output: memoryview) -> int: ... def test_parse_written_data(self, dump_callback: None = None) -> None: ... + def test_verify_styles(self) -> list[tuple[str, int, int, int]]: ... + def cell_style_idx(self, y: int, x: int) -> int: ... + def style_table_info(self) -> dict[str, int]: ... + def test_gc_style_table(self, quantize_bits: int = 0) -> None: ... + def test_set_style_table_limit(self, limit: int) -> None: ... def test_draw_overlay_line(self, text: str, xstart: int, ynum: int, preedit_cursor: int = -1) -> int: ... def test_update_overlay_text(self, text: str, preedit_cursor: int = -1) -> int: ... def hyperlink_for_id(self, hyperlink_id: int) -> str: ... diff --git a/kitty/fonts.c b/kitty/fonts.c index 8c5dba511..6626c6750 100644 --- a/kitty/fonts.c +++ b/kitty/fonts.c @@ -2071,6 +2071,25 @@ shaped_groups_as_python(void) { return ans; } +static struct { + GPUCell *cells; + index_type capacity; +} render_scratch = {0}; + +static GPUCell * +expanded_line_for_render(const Line *line) { + // Rendering uses the style of cells and stores sprites in GPUCells, so + // render into a scratch line of GPUCells built from the line. + if (render_scratch.capacity < line->xnum) { + GPUCell *n = realloc(render_scratch.cells, line->xnum * sizeof(GPUCell)); + if (!n) fatal("Out of memory allocating render scratch space"); + render_scratch.cells = n; + render_scratch.capacity = line->xnum; + } + expand_cells_for_gpu(line_style_table(line), line->cpu_cells, line->render_cells, line->xnum, render_scratch.cells); + return render_scratch.cells; +} + static PyObject * test_shape(PyObject UNUSED *self, PyObject *args) { Line *line; @@ -2111,10 +2130,11 @@ test_shape(PyObject UNUSED *self, PyObject *args) { } RunFont rf = {0}; RAII_ListOfChars(lc); - shape_run(line->cpu_cells, line->gpu_cells, num, font, rf, fg, false, line->text_cache, &lc); + GPUCell *gpu_cells = expanded_line_for_render(line); + shape_run(line->cpu_cells, gpu_cells, num, font, rf, fg, false, line->text_cache, &lc); PyObject *ans = shaped_groups_as_python(); if (!ans) goto done; - shape_run(line->cpu_cells, line->gpu_cells, num, font, rf, fg, false, line->text_cache, &lc); + shape_run(line->cpu_cells, gpu_cells, num, font, rf, fg, false, line->text_cache, &lc); PyObject *cached = shaped_groups_as_python(); if (!cached) { Py_CLEAR(ans); @@ -2246,7 +2266,7 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, render_run( \ fg, \ line->cpu_cells + first_cell_in_run, \ - line->gpu_cells + first_cell_in_run, \ + gpu_cells + first_cell_in_run, \ i - first_cell_in_run, \ run_font, \ false, \ @@ -2256,7 +2276,10 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, line->text_cache, \ lc); \ } + if (!line->render_cells) return; // lines without render data cannot be rendered FontGroup *fg = (FontGroup *)fg_; + GPUCell *gpu_cells = expanded_line_for_render(line); + StyleTable *style_table = line_style_table(line); RunFont basic_font = {.scale = 1, .font_idx = NO_FONT}, run_font = basic_font, cell_font = basic_font; bool center_glyph = false; bool disable_ligature_at_cursor = cursor != NULL && disable_ligature_strategy == DISABLE_LIGATURES_CURSOR; @@ -2278,7 +2301,7 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, } text_in_cell(cpu_cell, line->text_cache, lc); bool is_main_font, is_emoji_presentation; - GPUCell *gpu_cell = line->gpu_cells + i; + GPUCell *gpu_cell = gpu_cells + i; const char_type first_ch = lc->chars[0]; cell_font.font_idx = font_for_cell(fg, cpu_cell, gpu_cell, &is_main_font, &is_emoji_presentation, line->text_cache, lc); CharProps cp = char_props_for(first_ch); @@ -2300,30 +2323,26 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, && num_spaces < MAX_NUM_EXTRA_GLYPHS_PUA && num_spaces + 1 < desired_cells) { num_spaces++; // We have a private use char followed by space(s), render it as a multi-cell ligature. - GPUCell *space_cell = line->gpu_cells + i + num_spaces; + GPUCell *space_cell = gpu_cells + i + num_spaces; // Ensure the space cell uses the foreground color from the PUA cell. // This is needed because there are applications like // Powerline that use PUA+space with different foreground colors // for the space and the PUA. See for example: https://github.com/kovidgoyal/kitty/issues/467 space_cell->fg = gpu_cell->fg; space_cell->decoration_fg = gpu_cell->decoration_fg; + // The style is also changed in the CPUCell as GPUCells are rebuilt from it when uploading + CPUCell *space_cpu_cell = line->cpu_cells + i + num_spaces; + Style st = *style_table_get(style_table, space_cpu_cell->style_idx); + st.fg = gpu_cell->fg; + st.decoration_fg = gpu_cell->decoration_fg; + space_cpu_cell->style_idx = style_table_intern(style_table, &st); } if (num_spaces) { center_glyph = true; RENDER center_glyph = false; render_run( - fg, - line->cpu_cells + i, - line->gpu_cells + i, - num_spaces + 1, - cell_font, - true, - center_glyph, - -1, - disable_ligature_strategy, - line->text_cache, - lc); + fg, line->cpu_cells + i, gpu_cells + i, num_spaces + 1, cell_font, true, center_glyph, -1, disable_ligature_strategy, line->text_cache, lc); run_font = basic_font; first_cell_in_run = i + num_spaces + 1; i += num_spaces; @@ -2338,6 +2357,7 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor, } RENDER #undef RENDER + for (index_type x = 0; x < line->xnum; x++) line->render_cells[x].sprite_idx = gpu_cells[x].sprite_idx; } StringCanvas @@ -2529,6 +2549,8 @@ load_fonts_data(double font_sz_in_pts, double dpi_x, double dpi_y) { static void finalize(void) { Py_CLEAR(python_send_to_gpu_impl); + free(render_scratch.cells); + zero_at_ptr(&render_scratch); clear_symbol_maps(); Py_CLEAR(descriptor_for_idx); free_font_groups(); diff --git a/kitty/history.c b/kitty/history.c index 8c48232a0..ab3d9d21c 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -20,9 +20,10 @@ static void add_segment(HistoryBuf *self, index_type num) { self->segments = realloc(self->segments, sizeof(HistoryBufSegment) * (self->num_segments + num)); if (self->segments == NULL) fatal("Out of memory allocating new history buffer segment"); + // Only CPUCells are stored, the style of cells is in their CPUCell and + // render data for lines being displayed is in the render cache. const size_t cpu_cells_size = self->xnum * SEGMENT_SIZE * sizeof(CPUCell); - const size_t gpu_cells_size = self->xnum * SEGMENT_SIZE * sizeof(GPUCell); - const size_t segment_size = cpu_cells_size + gpu_cells_size + SEGMENT_SIZE * sizeof(LineAttrs); + const size_t segment_size = cpu_cells_size + SEGMENT_SIZE * sizeof(LineAttrs); if (num > SIZE_MAX / segment_size) fatal("History buffer segment allocation is too large"); const size_t mmap_size = num * segment_size; // We use mmap to avoid fragmentation in libc malloc pool, see @@ -32,8 +33,7 @@ add_segment(HistoryBuf *self, index_type num) { char *needs_free = mem; for (HistoryBufSegment *s = self->segments + self->num_segments; s < self->segments + self->num_segments + num; s++, mem += segment_size) { s->cpu_cells = (CPUCell *)mem; - s->gpu_cells = (GPUCell *)(((uint8_t *)s->cpu_cells) + cpu_cells_size); - s->line_attrs = (LineAttrs *)(((uint8_t *)s->gpu_cells) + gpu_cells_size); + s->line_attrs = (LineAttrs *)(((uint8_t *)s->cpu_cells) + cpu_cells_size); s->mem = NULL; s->mmap_size = 0; } @@ -68,11 +68,6 @@ cpu_lineptr(HistoryBuf *self, index_type y) { seg_ptr(cpu_cells, self->xnum); } -static GPUCell * -gpu_lineptr(HistoryBuf *self, index_type y) { - seg_ptr(gpu_cells, self->xnum); -} - static LineAttrs * attrptr(HistoryBuf *self, index_type y) { @@ -170,6 +165,8 @@ dealloc(HistoryBuf *self) { Py_CLEAR(self->line); for (size_t i = 0; i < self->num_segments; i++) free_segment(self->segments + i); free(self->segments); + free(self->render_cache.cells); + free(self->render_cache.keys); free_pagerhist(self); tc_decref(self->text_cache); Py_TYPE(self)->tp_free((PyObject *)self); @@ -201,7 +198,7 @@ static void init_line(HistoryBuf *self, index_type num, Line *l) { // Initialize the line l, setting its pointer to the offsets for the line at index (buffer position) num l->cpu_cells = cpu_lineptr(self, num); - l->gpu_cells = gpu_lineptr(self, num); + l->render_cells = NULL; l->attrs = *attrptr(self, num); } @@ -210,6 +207,38 @@ historybuf_init_line(HistoryBuf *self, index_type lnum, Line *l) { init_line(self, index_of(self, lnum), l); } +static void +invalidate_render_cache(HistoryBuf *self) { + if (self->render_cache.keys) memset(self->render_cache.keys, 0, self->render_cache.num_rows * sizeof(self->render_cache.keys[0])); +} + +bool +historybuf_init_line_for_render(HistoryBuf *self, index_type lnum, Line *l, index_type num_rows) { + // Initialize l with render data from the render cache, which has room for + // num_rows lines. Returns true if the render data is not for this line and + // so the line must be rendered. The render data must also be re-computed + // if the line is dirty, as when its contents change it is marked dirty. + num_rows = MAX(1u, num_rows); + if (num_rows != self->render_cache.num_rows) { + free(self->render_cache.cells); + free(self->render_cache.keys); + self->render_cache.cells = calloc((size_t)num_rows * self->xnum, sizeof(RenderCell)); + self->render_cache.keys = calloc(num_rows, sizeof(self->render_cache.keys[0])); + if (!self->render_cache.cells || !self->render_cache.keys) fatal("Out of memory allocating history render cache"); + self->render_cache.num_rows = num_rows; + } + init_line(self, index_of(self, lnum), l); + // The lines on screen have consecutive absolute line numbers and so map + // to distinct rows and do not evict each other. If a line is removed and + // another pushed with the same absolute line number, it is marked dirty. + const uint64_t key = self->num_pushed - MIN(self->num_pushed, (uint64_t)lnum); + const index_type row = key % num_rows; + l->render_cells = self->render_cache.cells + (size_t)row * self->xnum; + if (self->render_cache.keys[row] == key) return false; + self->render_cache.keys[row] = key; + return true; +} + bool historybuf_is_line_continued(HistoryBuf *self, index_type lnum) { return hb_line_is_continued(self, index_of(self, lnum)); @@ -250,6 +279,7 @@ historybuf_clear(HistoryBuf *self) { self->segments = NULL; self->num_segments = 0; add_segment(self, 1); + invalidate_render_cache(self); } static bool @@ -317,6 +347,7 @@ pagerhist_push(HistoryBuf *self, ANSIBuf *as_ansi_buf) { static index_type historybuf_push(HistoryBuf *self, ANSIBuf *as_ansi_buf, bool *needs_clear) { index_type idx = (self->start_of_data + self->count) % self->ynum; + self->num_pushed++; if (self->count == self->ynum) { pagerhist_push(self, as_ansi_buf); self->start_of_data = (self->start_of_data + 1) % self->ynum; @@ -337,10 +368,8 @@ prefetch_line_for_write(HistoryBuf *self, index_type y) { // line, speeding up bulk text throughput by ~35% (see benchmark.py). if (y / SEGMENT_SIZE >= self->num_segments) return; // must not trigger segment allocation const char *cpu = (const char *)cpu_lineptr(self, y); - const char *gpu = (const char *)gpu_lineptr(self, y); const size_t cache_line_sz = 64; for (size_t i = 0; i < self->xnum * sizeof(CPUCell); i += cache_line_sz) __builtin_prefetch(cpu + i, 1, 0); - for (size_t i = 0; i < self->xnum * sizeof(GPUCell); i += cache_line_sz) __builtin_prefetch(gpu + i, 1, 0); } void @@ -349,7 +378,10 @@ historybuf_add_line(HistoryBuf *self, const Line *line, ANSIBuf *as_ansi_buf) { index_type idx = historybuf_push(self, as_ansi_buf, &needs_clear); init_line(self, idx, self->line); copy_line(line, self->line); - *attrptr(self, idx) = line->attrs; + LineAttrs *attrs = attrptr(self, idx); + *attrs = line->attrs; + // the render data of the line is not stored, so it must be rendered if displayed + attrs->has_dirty_text = true; prefetch_line_for_write(self, (self->start_of_data + self->count) % self->ynum); } @@ -359,22 +391,25 @@ historybuf_pop_line(HistoryBuf *self, Line *line) { index_type idx = (self->start_of_data + self->count - 1) % self->ynum; init_line(self, idx, line); self->count--; + self->num_pushed--; return true; } static void clear_multicell_cell(HistoryBuf *self, index_type lnum, index_type x) { - init_line(self, index_of(self, lnum), self->line); + const index_type idx = index_of(self, lnum); + init_line(self, idx, self->line); CPUCell *c = self->line->cpu_cells + x; cell_set_char(c, 0); cell_clear_multicell(c); - clear_sprite_position(self->line->gpu_cells[x]); + attrptr(self, idx)->has_dirty_text = true; } void historybuf_delete_newest_lines(HistoryBuf *self, index_type count) { if (!count) return; count = MIN(self->count, count); + self->num_pushed -= count; self->count -= count - 1; // now nuke multi cell chars that overlap onto the last line index_type idx = (self->start_of_data + self->count - 1) % self->ynum; @@ -435,11 +470,13 @@ historybuf_delete_lines(HistoryBuf *self, index_type num_newer, index_type count for (index_type i = num_newer; i-- > 0;) { const index_type src = index_of(self, i), dest = index_of(self, i + count); memcpy(cpu_lineptr(self, dest), cpu_lineptr(self, src), sizeof(CPUCell) * self->xnum); - memcpy(gpu_lineptr(self, dest), gpu_lineptr(self, src), sizeof(GPUCell) * self->xnum); *attrptr(self, dest) = *attrptr(self, src); attrptr(self, dest)->has_dirty_text = true; } self->count -= count; + // Lines older than the deleted ones keep their absolute line numbers and + // so their render cache rows, the moved newer lines are marked dirty above. + self->num_pushed -= count; } static PyObject * @@ -488,6 +525,10 @@ push(HistoryBuf *self, PyObject *args) { ANSIBuf as_ansi_buf = {0}; historybuf_add_line(self, line, &as_ansi_buf); free(as_ansi_buf.buf); + if (line->text_cache != self->text_cache) { + historybuf_init_line(self, 0, self->line); + remap_style_idx_range(tc_style_table(self->text_cache), tc_style_table(line->text_cache), self->line->cpu_cells, self->line->xnum); + } Py_RETURN_NONE; } @@ -768,7 +809,6 @@ historybuf_next_dest_line(HistoryBuf *self, ANSIBuf *as_ansi_buf, Line *src_line init_line(self, idx, dest_line); // the slot may hold a stale line from stolen segments zero_at_ptr_count(dest_line->cpu_cells, dest_line->xnum); - zero_at_ptr_count(dest_line->gpu_cells, dest_line->xnum); return dest_y + 1; } @@ -796,6 +836,7 @@ void historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src, index_type count, bool steal) { // dest must have been created by historybuf_alloc_for_rewrap() with the same width as src dest->count = MIN(count, src->count); + dest->num_pushed = dest->count; dest->start_of_data = src->start_of_data; if (steal) { SWAP(dest->segments, src->segments); @@ -805,7 +846,6 @@ historybuf_fast_rewrap(HistoryBuf *dest, HistoryBuf *src, index_type count, bool } else { for (index_type i = 0; i < src->num_segments; i++) { memcpy(dest->segments[i].cpu_cells, src->segments[i].cpu_cells, SEGMENT_SIZE * src->xnum * sizeof(CPUCell)); - memcpy(dest->segments[i].gpu_cells, src->segments[i].gpu_cells, SEGMENT_SIZE * src->xnum * sizeof(GPUCell)); memcpy(dest->segments[i].line_attrs, src->segments[i].line_attrs, SEGMENT_SIZE * sizeof(LineAttrs)); } } diff --git a/kitty/history.h b/kitty/history.h index 96cfb5c73..743e67359 100644 --- a/kitty/history.h +++ b/kitty/history.h @@ -10,7 +10,6 @@ #include "line.h" typedef struct { - GPUCell *gpu_cells; CPUCell *cpu_cells; LineAttrs *line_attrs; void *mem; @@ -34,6 +33,16 @@ typedef struct { Line *line; TextCache *text_cache; index_type start_of_data, count; + // The number of lines pushed minus the number popped, so that the newest + // line has the absolute line number num_pushed - 1, which does not change + // while it is in the buffer. + uint64_t num_pushed; + // Render data for history lines being displayed, see historybuf_init_line_for_render() + struct { + RenderCell *cells; + uint64_t *keys; // absolute line number + 1 of the line whose data is in each row, 0 if none + index_type num_rows; + } render_cache; } HistoryBuf; diff --git a/kitty/line-buf.c b/kitty/line-buf.c index 5cddb2715..a49e771e1 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -19,20 +19,20 @@ cpu_lineptr(LineBuf *linebuf, index_type y) { return linebuf->cpu_cell_buf + y * linebuf->xnum; } -static GPUCell * -gpu_lineptr(LineBuf *linebuf, index_type y) { - return linebuf->gpu_cell_buf + y * linebuf->xnum; +static RenderCell * +render_lineptr(LineBuf *linebuf, index_type y) { + return linebuf->render_cell_buf + y * linebuf->xnum; } static void clear_chars_to(LineBuf *linebuf, index_type y, char_type ch) { - clear_chars_in_line(cpu_lineptr(linebuf, y), gpu_lineptr(linebuf, y), linebuf->xnum, ch); + clear_chars_in_line(cpu_lineptr(linebuf, y), linebuf->xnum, ch); } void linebuf_clear(LineBuf *self, char_type ch) { zero_at_ptr_count(self->cpu_cell_buf, self->xnum * self->ynum); - zero_at_ptr_count(self->gpu_cell_buf, self->xnum * self->ynum); + zero_at_ptr_count(self->render_cell_buf, self->xnum * self->ynum); zero_at_ptr_count(self->line_attrs, self->ynum); for (index_type i = 0; i < self->ynum; i++) self->line_map[i] = i; if (ch != 0) { @@ -90,13 +90,13 @@ alloc_linebuf_(PyTypeObject *cls, unsigned int lines, unsigned int columns, Text self->xnum = columns; self->ynum = lines; self->cpu_cell_buf = - PyMem_Calloc(1, area * (sizeof(CPUCell) + sizeof(GPUCell)) + lines * (sizeof(index_type) + sizeof(index_type) + sizeof(LineAttrs))); + PyMem_Calloc(1, area * (sizeof(CPUCell) + sizeof(RenderCell)) + lines * (sizeof(index_type) + sizeof(index_type) + sizeof(LineAttrs))); if (!self->cpu_cell_buf) { Py_CLEAR(self); return NULL; } - self->gpu_cell_buf = (GPUCell *)(self->cpu_cell_buf + area); - self->line_map = (index_type *)(self->gpu_cell_buf + area); + self->render_cell_buf = (RenderCell *)(self->cpu_cell_buf + area); + self->line_map = (index_type *)(self->render_cell_buf + area); self->scratch = self->line_map + lines; self->text_cache = tc_incref(text_cache); self->line = alloc_line(self->text_cache); @@ -131,10 +131,10 @@ dealloc(LineBuf *self) { } void -linebuf_init_cells(LineBuf *lb, index_type idx, CPUCell **c, GPUCell **g) { +linebuf_init_cells(LineBuf *lb, index_type idx, CPUCell **c, RenderCell **r) { const index_type ynum = lb->line_map[idx]; *c = cpu_lineptr(lb, ynum); - *g = gpu_lineptr(lb, ynum); + if (r) *r = render_lineptr(lb, ynum); } CPUCell * @@ -146,7 +146,7 @@ linebuf_cpu_cells_for_line(LineBuf *lb, index_type idx) { static void init_line(LineBuf *lb, Line *l, index_type ynum) { l->cpu_cells = cpu_lineptr(lb, ynum); - l->gpu_cells = gpu_lineptr(lb, ynum); + l->render_cells = render_lineptr(lb, ynum); } void @@ -168,16 +168,14 @@ linebuf_clear_lines(LineBuf *self, const Cursor *cursor, index_type start, index #error This implementation is incorrect for BLANK_CHAR != 0 #endif #define lineptr(which, i) which##_lineptr(self, self->line_map[i]) - GPUCell *first_gpu_line = lineptr(gpu, start); - const GPUCell gc = cursor_as_gpu_cell(cursor); - memset_array(first_gpu_line, gc, self->xnum); - const size_t cpu_stride = sizeof(CPUCell) * self->xnum; - memset(lineptr(cpu, start), 0, cpu_stride); - const size_t gpu_stride = sizeof(GPUCell) * self->xnum; + const size_t cpu_stride = sizeof(CPUCell) * self->xnum, render_stride = sizeof(RenderCell) * self->xnum; + CPUCell *first_cpu_line = lineptr(cpu, start); + fill_blank_cpu_cells(first_cpu_line, self->xnum, cursor_style_idx_in(tc_style_table(self->text_cache), cursor)); + memset(lineptr(render, start), 0, render_stride); linebuf_clear_attrs_and_dirty(self, start); for (index_type i = start + 1; i < end; i++) { - memset(lineptr(cpu, i), 0, cpu_stride); - memcpy(lineptr(gpu, i), first_gpu_line, gpu_stride); + memcpy(lineptr(cpu, i), first_cpu_line, cpu_stride); + memset(lineptr(render, i), 0, render_stride); linebuf_clear_attrs_and_dirty(self, i); } #undef lineptr @@ -221,7 +219,7 @@ set_attribute(LineBuf *self, PyObject *args) { char *which; if (!PyArg_ParseTuple(args, "sI", &which, &val)) return NULL; for (index_type y = 0; y < self->ynum; y++) { - if (!set_named_attribute_on_line(gpu_lineptr(self, y), which, val, self->xnum)) { + if (!set_named_attribute_on_line(tc_style_table(self->text_cache), cpu_lineptr(self, y), render_lineptr(self, y), which, val, self->xnum)) { PyErr_SetString(PyExc_KeyError, "Unknown cell attribute"); return NULL; } @@ -258,16 +256,16 @@ static bool allocate_line_storage(Line *line, bool initialize) { if (initialize) { line->cpu_cells = PyMem_Calloc(line->xnum, sizeof(CPUCell)); - line->gpu_cells = PyMem_Calloc(line->xnum, sizeof(GPUCell)); - if (line->cpu_cells == NULL || line->gpu_cells == NULL) { + line->render_cells = PyMem_Calloc(line->xnum, sizeof(RenderCell)); + if (line->cpu_cells == NULL || line->render_cells == NULL) { PyErr_NoMemory(); return false; } - if (BLANK_CHAR != 0) clear_chars_in_line(line->cpu_cells, line->gpu_cells, line->xnum, BLANK_CHAR); + if (BLANK_CHAR != 0) clear_chars_in_line(line->cpu_cells, line->xnum, BLANK_CHAR); } else { line->cpu_cells = PyMem_Malloc(line->xnum * sizeof(CPUCell)); - line->gpu_cells = PyMem_Malloc(line->xnum * sizeof(GPUCell)); - if (line->cpu_cells == NULL || line->gpu_cells == NULL) { + line->render_cells = PyMem_Malloc(line->xnum * sizeof(RenderCell)); + if (line->cpu_cells == NULL || line->render_cells == NULL) { PyErr_NoMemory(); return false; } @@ -323,6 +321,8 @@ copy_line_to(LineBuf *self, PyObject *args) { dest->attrs = self->line_attrs[y]; init_line(self, &src, self->line_map[y]); copy_line(&src, dest); + if (self->text_cache != dest->text_cache) + remap_style_idx_range(tc_style_table(dest->text_cache), tc_style_table(self->text_cache), dest->cpu_cells, dest->xnum); Py_RETURN_NONE; } @@ -332,7 +332,7 @@ clear_line_(Line *l, index_type xnum) { #error This implementation is incorrect for BLANK_CHAR != 0 #endif zero_at_ptr_count(l->cpu_cells, xnum); - zero_at_ptr_count(l->gpu_cells, xnum); + if (l->render_cells) zero_at_ptr_count(l->render_cells, xnum); l->attrs.has_dirty_text = false; } @@ -343,9 +343,8 @@ linebuf_clear_line(LineBuf *self, index_type y, bool clear_attrs) { #endif index_type ym = self->line_map[y]; CPUCell *c = cpu_lineptr(self, ym); - GPUCell *g = gpu_lineptr(self, ym); zero_at_ptr_count(c, self->xnum); - zero_at_ptr_count(g, self->xnum); + zero_at_ptr_count(render_lineptr(self, ym), self->xnum); if (clear_attrs) self->line_attrs[y].val = 0; } @@ -618,6 +617,8 @@ copy_old(LineBuf *self, PyObject *y) { init_line(self, &sl, s); init_line(other, &ol, o); copy_line(&ol, &sl); + if (self->text_cache != other->text_cache) + remap_style_idx_range(tc_style_table(self->text_cache), tc_style_table(other->text_cache), sl.cpu_cells, sl.xnum); } Py_RETURN_NONE; } diff --git a/kitty/line-buf.h b/kitty/line-buf.h index 5e58c1da2..c785c71d4 100644 --- a/kitty/line-buf.h +++ b/kitty/line-buf.h @@ -13,7 +13,7 @@ typedef struct { PyObject_HEAD - GPUCell *gpu_cell_buf; + RenderCell *render_cell_buf; CPUCell *cpu_cell_buf; index_type xnum, ynum, *line_map, *scratch; LineAttrs *line_attrs; diff --git a/kitty/line.c b/kitty/line.c index a9437f123..fb75b6a3a 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -19,7 +19,7 @@ static void dealloc(Line *self) { if (self->needs_free) { PyMem_Free(self->cpu_cells); - PyMem_Free(self->gpu_cells); + PyMem_Free(self->render_cells); } tc_decref(self->text_cache); Py_TYPE(self)->tp_free((PyObject *)self); @@ -525,8 +525,11 @@ sprite_at(Line *self, PyObject *x) { PyErr_SetString(PyExc_IndexError, "Column number out of bounds"); return NULL; } - GPUCell *c = self->gpu_cells + xval; - return Py_BuildValue("I", (unsigned int)c->sprite_idx); + if (!self->render_cells) { + PyErr_SetString(PyExc_ValueError, "This line has no render data"); + return NULL; + } + return Py_BuildValue("I", (unsigned int)self->render_cells[xval].sprite_idx); } static void @@ -634,27 +637,27 @@ line_as_ansi(Line *self, ANSILineState *s, index_type start_at, index_type stop_ return s->escape_code_written; } - static const GPUCell blank_cell = {0}; - GPUCell *cell; - if (s->prev_gpu_cell == NULL) s->prev_gpu_cell = &blank_cell; - const CellAttrs mask_for_sgr = {.val = SGR_MASK}; - -#define CMP_ATTRS (cell->attrs.val & mask_for_sgr.val) != (s->prev_gpu_cell->attrs.val & mask_for_sgr.val) -#define CMP(x) (cell->x != s->prev_gpu_cell->x) + const StyleTable *style_table = line_style_table(self); + uint32_t prev_style_idx = UINT32_MAX; for (s->pos = start_at; s->pos < s->limit; s->pos++) { if (s->output_buf->hyperlink_pool) { hyperlink_id_type hid = self->cpu_cells[s->pos].hyperlink_id; if (hid != s->output_buf->active_hyperlink_id) write_hyperlink_to_ansi_buf(s, hid); } - cell = &self->gpu_cells[s->pos]; - if (CMP_ATTRS || CMP(fg) || CMP(bg) || CMP(decoration_fg)) { - const char *sgr = cell_as_sgr(cell, s->prev_gpu_cell); - if (*sgr) write_sgr_to_ansi_buf(s, sgr); + const uint32_t style_idx = self->cpu_cells[s->pos].style_idx; + if (style_idx != prev_style_idx) { + // consecutive cells usually have the same style + prev_style_idx = style_idx; + const Style *cs = style_table_get(style_table, style_idx); + if (!style_eq(cs, &s->prev_style)) { + const char *sgr = style_as_sgr(cs, &s->prev_style); + if (*sgr) write_sgr_to_ansi_buf(s, sgr); + s->prev_style = *cs; + } } index_type num_cells_to_skip_for_tab = text_in_cell_ansi(s, self->cpu_cells + s->pos, self->text_cache, skip_multiline_non_zero_lines); - s->prev_gpu_cell = cell; const CPUCell *next = self->cpu_cells + s->pos + 1; while (num_cells_to_skip_for_tab && s->pos + 1 < s->limit && cell_is_char(next, ' ')) { num_cells_to_skip_for_tab--; @@ -665,8 +668,6 @@ line_as_ansi(Line *self, ANSILineState *s, index_type start_at, index_type stop_ close_multicell(s); if (s->output_buf->active_hyperlink_id && s->limit < self->xnum && !self->cpu_cells[s->limit].hyperlink_id) write_hyperlink_to_ansi_buf(s, 0); return s->escape_code_written; -#undef CMP_ATTRS -#undef CMP } static PyObject * @@ -769,17 +770,11 @@ set_text(Line *self, PyObject *args) { PyErr_SetString(PyExc_ValueError, "Out of bounds offset/sz"); return NULL; } - CellAttrs attrs = cursor_to_attrs(cursor); - color_type fg = (cursor->sgr.fg & COL_MASK), bg = cursor->sgr.bg & COL_MASK; - color_type dfg = cursor->sgr.decoration_fg & COL_MASK; + const uint32_t style_idx = style_table_intern_cached(line_style_table(self), &cursor->style, &cursor->style_idx); for (index_type i = cursor->x; offset < limit && i < self->xnum; i++, offset++) { - self->cpu_cells[i] = (CPUCell){0}; + self->cpu_cells[i] = (CPUCell){.style_idx = style_idx}; self->cpu_cells[i].ch_or_idx = PyUnicode_READ(kind, buf, offset); - self->gpu_cells[i].attrs = attrs; - self->gpu_cells[i].fg = fg; - self->gpu_cells[i].bg = bg; - self->gpu_cells[i].decoration_fg = dfg; } Py_RETURN_NONE; @@ -804,19 +799,15 @@ cursor_from(Line *self, PyObject *args) { } ans->x = x; ans->y = y; - attrs_to_cursor(self->gpu_cells[x].attrs, ans); - ans->sgr.fg = self->gpu_cells[x].fg; - ans->sgr.bg = self->gpu_cells[x].bg; - ans->sgr.decoration_fg = self->gpu_cells[x].decoration_fg & COL_MASK; + ans->style = *line_cell_style(self, x); return (PyObject *)ans; } void line_clear_text(Line *self, unsigned int at, unsigned int num, char_type ch) { - const CPUCell cc = {.ch_or_idx = ch}; if (at + num > self->xnum) num = self->xnum > at ? self->xnum - at : 0; - memset_array(self->cpu_cells + at, cc, num); + for (CPUCell *c = self->cpu_cells + at, *limit = c + num; c < limit; c++) *c = (CPUCell){.ch_or_idx = ch, .style_idx = c->style_idx}; } static PyObject * @@ -831,20 +822,16 @@ clear_text(Line *self, PyObject *args) { void line_apply_cursor(Line *self, const Cursor *cursor, unsigned int at, unsigned int num, bool clear_char) { - GPUCell gc = cursor_as_gpu_cell(cursor); + const uint32_t style_idx = cursor_style_idx_in(line_style_table(self), cursor); if (clear_char) { #if BLANK_CHAR != 0 #error This implementation is incorrect for BLANK_CHAR != 0 #endif if (at + num > self->xnum) { num = at < self->xnum ? self->xnum - at : 0; } - memset(self->cpu_cells + at, 0, num * sizeof(CPUCell)); - memset_array(self->gpu_cells + at, gc, num); + fill_blank_cpu_cells(self->cpu_cells + at, num, style_idx); + if (self->render_cells) memset(self->render_cells + at, 0, num * sizeof(RenderCell)); } else { - for (index_type i = at; i < self->xnum && i < at + num; i++) { - gc.attrs.mark = self->gpu_cells[i].attrs.mark; - gc.sprite_idx = self->gpu_cells[i].sprite_idx; - memcpy(self->gpu_cells + i, &gc, sizeof(gc)); - } + for (index_type i = at; i < self->xnum && i < at + num; i++) self->cpu_cells[i].style_idx = style_idx; } } @@ -873,9 +860,10 @@ bool colors_for_cell(Line *self, const ColorProfile *cp, index_type *x, color_type *fg, color_type *bg, bool *reversed) { if (*x >= self->xnum) return false; while (self->cpu_cells[*x].is_multicell && self->cpu_cells[*x].x && *x) (*x)--; - *fg = resolve_color(cp, self->gpu_cells[*x].fg, *fg); - *bg = resolve_color(cp, self->gpu_cells[*x].bg, *bg); - if (self->gpu_cells[*x].attrs.reverse) { + const Style *st = line_cell_style(self, *x); + *fg = resolve_color(cp, st->fg, *fg); + *bg = resolve_color(cp, st->bg, *bg); + if (st->attrs.reverse) { color_type t = *fg; *fg = *bg; *bg = t; @@ -897,21 +885,17 @@ line_get_char(Line *self, index_type at) { static void line_set_char(Line *self, unsigned int at, uint32_t ch, Cursor *cursor, hyperlink_id_type hyperlink_id) { - GPUCell *g = self->gpu_cells + at; - if (cursor != NULL) { - g->attrs = cursor_to_attrs(cursor); - g->fg = cursor->sgr.fg & COL_MASK; - g->bg = cursor->sgr.bg & COL_MASK; - g->decoration_fg = cursor->sgr.decoration_fg & COL_MASK; - } + StyleTable *t = line_style_table(self); CPUCell *c = self->cpu_cells + at; + Style st = cursor ? cursor->style : *style_table_get(t, c->style_idx); *c = (CPUCell){0}; cell_set_char(c, ch); c->hyperlink_id = hyperlink_id; if (OPT(underline_hyperlinks) == UNDERLINE_ALWAYS && hyperlink_id) { - g->decoration_fg = ((OPT(url_color) & COL_MASK) << 8) | 2; - g->attrs.decoration = OPT(url_style); + st.decoration_fg = ((OPT(url_color) & COL_MASK) << 8) | 2; + st.attrs.decoration = OPT(url_style); } + c->style_idx = style_table_intern(t, &st); } static PyObject * @@ -943,7 +927,7 @@ set_attribute(Line *self, PyObject *args) { unsigned int val; char *which; if (!PyArg_ParseTuple(args, "sI", &which, &val)) return NULL; - if (!set_named_attribute_on_line(self->gpu_cells, which, val, self->xnum)) { + if (!set_named_attribute_on_line(line_style_table(self), self->cpu_cells, self->render_cells, which, val, self->xnum)) { PyErr_SetString(PyExc_KeyError, "Unknown cell attribute"); return NULL; } @@ -976,7 +960,7 @@ decoration_as_sgr(uint8_t decoration) { const char * -cell_as_sgr(const GPUCell *cell, const GPUCell *prev) { +style_as_sgr(const Style *cell, const Style *prev) { static char buf[128]; #define SZ sizeof(buf) - (p - buf) - 2 #define P(s) \ @@ -1026,14 +1010,24 @@ __len__(PyObject *self) { static int __eq__(Line *a, Line *b) { - return a->xnum == b->xnum && memcmp(a->cpu_cells, b->cpu_cells, sizeof(CPUCell) * a->xnum) == 0 && - memcmp(a->gpu_cells, b->gpu_cells, sizeof(GPUCell) * a->xnum) == 0; + if (a->xnum != b->xnum) return false; + if (a->text_cache == b->text_cache) return memcmp(a->cpu_cells, b->cpu_cells, sizeof(CPUCell) * a->xnum) == 0; + // Lines from different screens, compare styles by value + const StyleTable *ta = line_style_table(a), *tb = line_style_table(b); + for (index_type x = 0; x < a->xnum; x++) { + CPUCell ca = a->cpu_cells[x], cb = b->cpu_cells[x]; + if (!style_eq(style_table_get(ta, ca.style_idx), style_table_get(tb, cb.style_idx))) return false; + ca.style_idx = 0; + cb.style_idx = 0; + if (memcmp(&ca, &cb, sizeof(ca)) != 0) return false; + } + return true; } bool line_has_mark(Line *line, uint16_t mark) { for (index_type x = 0; x < line->xnum; x++) { - const uint16_t m = line->gpu_cells[x].attrs.mark; + const uint16_t m = line->render_cells ? line->render_cells[x].mark : 0; if (m && (!mark || mark == m)) return true; } return false; @@ -1050,7 +1044,7 @@ report_marker_error(PyObject *marker) { static void apply_mark(Line *line, const uint16_t mark, index_type *cell_pos, unsigned int *match_pos) { #define MARK \ - { line->gpu_cells[x].attrs.mark = mark; } + { line->render_cells[x].mark = mark; } index_type x = *cell_pos; MARK; (*match_pos)++; @@ -1103,21 +1097,22 @@ apply_marker(PyObject *marker, Line *line, const PyObject *text) { while (x < line->xnum && match_pos <= r) { apply_mark(line, am, &x, &match_pos); } } Py_DECREF(iter); - while (x < line->xnum) line->gpu_cells[x++].attrs.mark = 0; + while (x < line->xnum) line->render_cells[x++].mark = 0; if (PyErr_Occurred()) report_marker_error(marker); } void mark_text_in_line(PyObject *marker, Line *line, ANSIBuf *buf) { + if (!line->render_cells) return; // marks are render data if (!marker) { - for (index_type i = 0; i < line->xnum; i++) line->gpu_cells[i].attrs.mark = 0; + for (index_type i = 0; i < line->xnum; i++) line->render_cells[i].mark = 0; return; } PyObject *text = line_as_unicode(line, false, buf); if (PyUnicode_GET_LENGTH(text) > 0) { apply_marker(marker, line, text); } else { - for (index_type i = 0; i < line->xnum; i++) line->gpu_cells[i].attrs.mark = 0; + for (index_type i = 0; i < line->xnum; i++) line->render_cells[i].mark = 0; } Py_DECREF(text); } @@ -1166,7 +1161,7 @@ as_text_generic(PyObject *args, void *container, get_line_func get_line, index_t // pretty sad performance wise, but I guess it will remain as it // makes writing pagers easier. // see https://github.com/kovidgoyal/kitty/issues/2381 - s.prev_gpu_cell = NULL; + zero_at_ptr(&s.prev_style); line_as_ansi(line, &s, 0, line->xnum, 0, true); t = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, ansibuf->buf, ansibuf->len); if (t && ansibuf->len > 0) APPEND(sgr_reset); @@ -1248,6 +1243,7 @@ copy_char(Line *self, PyObject *args) { return NULL; } COPY_CELL(self, src, to, dest); + if (self->text_cache != to->text_cache) remap_style_idx_range(line_style_table(to), line_style_table(self), to->cpu_cells + dest, 1); Py_RETURN_NONE; } diff --git a/kitty/line.h b/kitty/line.h index 1ee98a335..6d00aff5a 100644 --- a/kitty/line.h +++ b/kitty/line.h @@ -8,23 +8,8 @@ #pragma once #include "text-cache.h" +#include "style-table.h" -typedef union CellAttrs { - struct { - uint16_t decoration : 3; - uint16_t bold : 1; - uint16_t italic : 1; - uint16_t reverse : 1; - uint16_t strike : 1; - uint16_t dim : 1; - uint16_t blink : 1; - uint16_t mark : 2; - // named so that initializers zero it, see CPUCell - uint32_t padding : 21; - }; - uint32_t val; -} CellAttrs; -static_assert(sizeof(CellAttrs) == sizeof(uint32_t), "Fix the ordering of CellAttrs"); #define WIDTH_MASK (3u) #define DECORATION_MASK (7u) @@ -41,6 +26,16 @@ typedef struct { CellAttrs attrs; } GPUCell; static_assert(sizeof(GPUCell) == 20, "Fix the ordering of GPUCell"); +// GPUCell is the format of cell data uploaded to the GPU, it is built from the +// CPUCell, the Style of the cell and the RenderCell of the cell. + +// Data computed when rendering a cell. The style of a cell is stored in its CPUCell. +typedef struct RenderCell { + sprite_index sprite_idx; + uint8_t mark; + uint8_t padding[3]; +} RenderCell; +static_assert(sizeof(RenderCell) == 8, "Fix the ordering of RenderCell"); #define SCALE_BITS 3 #define WIDTH_BITS 3 @@ -60,7 +55,7 @@ typedef union CPUCell { #error "Unsupported endianness" #endif // the fields below are ordered so that none of them straddles a 32 - // bit boundary, leaving all padding at the end + // bit boundary, the style index uses all the bits remaining at the end char_type hyperlink_id : sizeof(hyperlink_id_type) * 8; char_type scale : SCALE_BITS; char_type subscale_n : SUBSCALE_BITS; @@ -74,10 +69,8 @@ typedef union CPUCell { char_type y : SCALE_BITS; char_type width : WIDTH_BITS; char_type temp_flag : 1; - // The padding is named because unnamed bit-fields are left - // indeterminate by initializers, and clang on arm64 fills them with - // stack garbage, making cells with identical contents compare unequal. - char_type padding : 17; + // Index of the formatting of this cell in the StyleTable of the screen + char_type style_idx : STYLE_IDX_BITS; }; struct { char_type ch_and_idx : sizeof(char_type) * 8; @@ -103,7 +96,8 @@ static_assert(sizeof(LineAttrs) == sizeof(uint8_t), "Fix the ordering of LineAtt typedef struct { PyObject_HEAD - GPUCell *gpu_cells; + // Render data, NULL for lines in the history buffer that are not being rendered + RenderCell *render_cells; CPUCell *cpu_cells; index_type xnum, ynum; bool needs_free; @@ -117,7 +111,7 @@ typedef struct MultiCellCommand { } MultiCellCommand; typedef struct ANSILineOutput { - const GPUCell *prev_gpu_cell; + Style prev_style; // style of the previous cell output, zero (the default style) at the start const CPUCell *current_multicell_state; index_type pos, limit; ANSIBuf *output_buf; @@ -132,8 +126,8 @@ cleanup_ansibuf(ANSIBuf *b) { #define RAII_ANSIBuf(name) __attribute__((cleanup(cleanup_ansibuf))) ANSIBuf name = {0} Line *alloc_line(TextCache *text_cache); -void apply_sgr_to_cells(GPUCell *first_cell, unsigned int cell_count, int *params, unsigned int count, bool is_group); -const char *cell_as_sgr(const GPUCell *, const GPUCell *); +void apply_sgr_to_cells(StyleTable *t, CPUCell *cells, unsigned int cell_count, int *params, unsigned int count, bool is_group); +const char *style_as_sgr(const Style *, const Style *); static inline bool cell_has_text(const CPUCell *c) { return c->ch_and_idx != 0; @@ -230,32 +224,53 @@ cell_first_char(const CPUCell *c, const TextCache *tc) { return c->ch_or_idx; } -static inline CellAttrs -cursor_to_attrs(const Cursor *c) { - CellAttrs ans = { - .decoration = c->sgr.decoration, - .bold = c->sgr.bold, - .italic = c->sgr.italic, - .reverse = c->sgr.reverse, - .strike = c->sgr.strikethrough, - .dim = c->sgr.dim, - .blink = c->sgr.blink}; +static inline StyleTable * +line_style_table(const Line *l) { + return tc_style_table(l->text_cache); +} + +static inline const Style * +line_cell_style(const Line *l, index_type x) { + return style_table_get(line_style_table(l), l->cpu_cells[x].style_idx); +} + +static inline uint32_t +cursor_style_idx_in(StyleTable *t, const Cursor *cursor) { + // Uses the cache in the cursor without updating it + uint32_t idx = cursor->style_idx; + return style_table_intern_cached(t, &cursor->style, &idx); +} + +static inline void +fill_blank_cpu_cells(CPUCell *c, index_type num, uint32_t style_idx) { + if (style_idx) { + const CPUCell blank = {.style_idx = style_idx}; + for (index_type i = 0; i < num; i++) c[i] = blank; + } else memset(c, 0, num * sizeof(CPUCell)); +} + +static inline void +remap_style_idx_range(StyleTable *dest, const StyleTable *src, CPUCell *c, index_type num) { + // Change the style indices of cells copied from a buffer with a different style table + uint32_t cache = 0; + for (index_type i = 0; i < num; i++) c[i].style_idx = style_table_intern_cached(dest, style_table_get(src, c[i].style_idx), &cache); +} + +static inline GPUCell +gpu_cell_for(const Style *st, const RenderCell *r) { + GPUCell ans = {.fg = st->fg, .bg = st->bg, .decoration_fg = st->decoration_fg, .attrs = st->attrs}; + if (r) { + ans.sprite_idx = r->sprite_idx; + ans.attrs.mark = r->mark; + } return ans; } static inline void -attrs_to_cursor(const CellAttrs attrs, Cursor *c) { - c->sgr.decoration = attrs.decoration; - c->sgr.bold = attrs.bold; - c->sgr.italic = attrs.italic; - c->sgr.reverse = attrs.reverse; - c->sgr.strikethrough = attrs.strike; - c->sgr.dim = attrs.dim; - c->sgr.blink = attrs.blink; +expand_cells_for_gpu(const StyleTable *t, const CPUCell *c, const RenderCell *r, index_type num, GPUCell *dest) { + // Build the cell data uploaded to the GPU. dest may be write combined memory so it is only written to, sequentially. + for (index_type i = 0; i < num; i++) { + const GPUCell g = gpu_cell_for(style_table_get(t, c[i].style_idx), r ? r + i : NULL); + memcpy(dest + i, &g, sizeof(g)); + } } - -#define cursor_as_gpu_cell(cursor) \ - {.attrs = cursor_to_attrs(cursor), \ - .fg = (cursor->sgr.fg & COL_MASK), \ - .bg = (cursor->sgr.bg & COL_MASK), \ - .decoration_fg = cursor->sgr.decoration_fg & COL_MASK} diff --git a/kitty/lineops.h b/kitty/lineops.h index 4d60730a7..b0dc4b3c6 100644 --- a/kitty/lineops.h +++ b/kitty/lineops.h @@ -9,23 +9,27 @@ #include "history.h" #include "line-buf.h" -#define set_attribute_on_line(cells, which, val, xnum) \ - { \ - for (index_type i__ = 0; i__ < xnum; i__++) cells[i__].attrs.which = val; \ - } - static inline bool -set_named_attribute_on_line(GPUCell *cells, const char *which, uint16_t val, index_type xnum) { +set_named_attribute_on_line(StyleTable *t, CPUCell *cells, RenderCell *render_cells, const char *which, uint16_t val, index_type xnum) { // Set a single attribute on all cells in the line -#define s(q) \ - if (strcmp(#q, which) == 0) { \ - set_attribute_on_line(cells, q, val, xnum); \ - return true; \ + if (strcmp("mark", which) == 0) { + if (render_cells) + for (index_type i = 0; i < xnum; i++) render_cells[i].mark = val; + return true; + } + uint32_t cache = 0; +#define s(q) \ + if (strcmp(#q, which) == 0) { \ + for (index_type i = 0; i < xnum; i++) { \ + Style st = *style_table_get(t, cells[i].style_idx); \ + st.attrs.q = val; \ + cells[i].style_idx = style_table_intern_cached(t, &st, &cache); \ + } \ + return true; \ } s(reverse); s(strike); s(dim); - s(mark); s(bold); s(italic); s(decoration); @@ -36,20 +40,22 @@ set_named_attribute_on_line(GPUCell *cells, const char *which, uint16_t val, ind static inline void copy_line(const Line *src, Line *dest) { - memcpy(dest->cpu_cells, src->cpu_cells, sizeof(CPUCell) * MIN(src->xnum, dest->xnum)); - memcpy(dest->gpu_cells, src->gpu_cells, sizeof(GPUCell) * MIN(src->xnum, dest->xnum)); + // Copies the render data only if both lines have it, otherwise the render + // data of dest is stale and dest must be marked dirty. + const index_type n = MIN(src->xnum, dest->xnum); + memcpy(dest->cpu_cells, src->cpu_cells, sizeof(CPUCell) * n); + if (dest->render_cells) { + if (src->render_cells) memcpy(dest->render_cells, src->render_cells, sizeof(RenderCell) * n); + else memset(dest->render_cells, 0, sizeof(RenderCell) * n); + } } static inline void -clear_chars_in_line(CPUCell *cpu_cells, GPUCell *gpu_cells, index_type xnum, char_type ch) { +clear_chars_in_line(CPUCell *cpu_cells, index_type xnum, char_type ch) { // Clear only the char part of each cell, the rest must have been cleared by a memset or similar if (ch) { - static const CellAttrs empty = {0}; const CPUCell c = {.ch_or_idx = ch}; - for (index_type i = 0; i < xnum; i++) { - cpu_cells[i] = c; - gpu_cells[i].attrs = empty; - } + for (index_type i = 0; i < xnum; i++) cpu_cells[i] = c; } } @@ -61,14 +67,15 @@ xlimit_for_line(const Line *line) { } static inline void -line_save_cells(Line *line, index_type start, index_type num, GPUCell *gpu_cells, CPUCell *cpu_cells) { - memcpy(gpu_cells + start, line->gpu_cells + start, sizeof(GPUCell) * num); +line_save_cells(Line *line, index_type start, index_type num, RenderCell *render_cells, CPUCell *cpu_cells) { + if (line->render_cells) memcpy(render_cells + start, line->render_cells + start, sizeof(RenderCell) * num); + else memset(render_cells + start, 0, sizeof(RenderCell) * num); memcpy(cpu_cells + start, line->cpu_cells + start, sizeof(CPUCell) * num); } static inline void -line_reset_cells(Line *line, index_type start, index_type num, GPUCell *gpu_cells, CPUCell *cpu_cells) { - memcpy(line->gpu_cells + start, gpu_cells + start, sizeof(GPUCell) * num); +line_reset_cells(Line *line, index_type start, index_type num, RenderCell *render_cells, CPUCell *cpu_cells) { + if (line->render_cells) memcpy(line->render_cells + start, render_cells + start, sizeof(RenderCell) * num); memcpy(line->cpu_cells + start, cpu_cells + start, sizeof(CPUCell) * num); } @@ -111,7 +118,7 @@ PyObject *line_as_unicode(Line *, bool, ANSIBuf *); void linebuf_init_line(LineBuf *, index_type); void linebuf_init_line_at(LineBuf *, index_type, Line *); -void linebuf_init_cells(LineBuf *lb, index_type ynum, CPUCell **c, GPUCell **g); +void linebuf_init_cells(LineBuf *lb, index_type ynum, CPUCell **c, RenderCell **r); CPUCell *linebuf_cpu_cells_for_line(LineBuf *lb, index_type idx); void linebuf_clear(LineBuf *, char_type ch); void linebuf_clear_lines(LineBuf *self, const Cursor *cursor, index_type start, index_type end); @@ -133,6 +140,7 @@ void historybuf_add_line(HistoryBuf *self, const Line *line, ANSIBuf *); bool cells_have_multiline_chars(const CPUCell *cells, index_type count); bool historybuf_pop_line(HistoryBuf *, Line *); void historybuf_init_line(HistoryBuf *self, index_type num, Line *l); +bool historybuf_init_line_for_render(HistoryBuf *self, index_type lnum, Line *l, index_type num_rows); bool history_buf_endswith_wrap(HistoryBuf *self); CPUCell *historybuf_cpu_cells(HistoryBuf *self, index_type num); void historybuf_mark_line_clean(HistoryBuf *self, index_type y); diff --git a/kitty/resize.c b/kitty/resize.c index 7c13b72df..77f885563 100644 --- a/kitty/resize.c +++ b/kitty/resize.c @@ -153,10 +153,8 @@ next_dest_line(Rewrap *r, bool continued) { } if (r->sb->line_attrs[0].has_dirty_text) { CPUCell *cpu_cells; - GPUCell *gpu_cells; - linebuf_init_cells(r->sb, 0, &cpu_cells, &gpu_cells); + linebuf_init_cells(r->sb, 0, &cpu_cells, NULL); memcpy(r->dest.line.cpu_cells, cpu_cells, dest_xnum * sizeof(cpu_cells[0])); - memcpy(r->dest.line.gpu_cells, gpu_cells, dest_xnum * sizeof(gpu_cells[0])); r->current_dest_line_has_multiline_cells = true; } linebuf_index(r->sb, 0, r->sb->ynum - 1); @@ -219,8 +217,8 @@ find_space_in_dest(Rewrap *r, index_type num_cells) { static void copy_range(Line *src, index_type src_at, Line *dest, index_type dest_at, index_type num) { + // render data is not copied as all rewrapped lines are marked dirty memcpy(dest->cpu_cells + dest_at, src->cpu_cells + src_at, num * sizeof(CPUCell)); - memcpy(dest->gpu_cells + dest_at, src->gpu_cells + src_at, num * sizeof(GPUCell)); } static void @@ -317,7 +315,7 @@ rewrap(Rewrap *r) { memcpy(r->dest.lb->line_map, r->src.lb->line_map, sizeof(index_type) * r->src.lb->ynum); memcpy(r->dest.lb->line_attrs, r->src.lb->line_attrs, sizeof(LineAttrs) * r->src.lb->ynum); memcpy(r->dest.lb->cpu_cell_buf, r->src.lb->cpu_cell_buf, (size_t)r->src.lb->xnum * r->src.lb->ynum * sizeof(CPUCell)); - memcpy(r->dest.lb->gpu_cell_buf, r->src.lb->gpu_cell_buf, (size_t)r->src.lb->xnum * r->src.lb->ynum * sizeof(GPUCell)); + memcpy(r->dest.lb->render_cell_buf, r->src.lb->render_cell_buf, (size_t)r->src.lb->xnum * r->src.lb->ynum * sizeof(RenderCell)); r->num_content_lines_before = r->src.lb->ynum; if (r->dest.hb && r->src.hb) historybuf_fast_rewrap(r->dest.hb, r->src.hb, r->src.hb->count, r->consume_src_hb); r->dest.y = r->src.lb->ynum - 1; @@ -376,7 +374,6 @@ clear_trailing_blanks(HistoryBuf *hb, index_type count) { index_type limit = l.xnum; while (limit && l.cpu_cells[limit - 1].ch_and_idx == BLANK_CHAR) limit--; zero_at_ptr_count(l.cpu_cells + limit, l.xnum - limit); - zero_at_ptr_count(l.gpu_cells + limit, l.xnum - limit); } } @@ -451,19 +448,17 @@ resize_screen_buffers( } static void -nuke_in_line(CPUCell *cp, GPUCell *gp, index_type start, index_type x_limit) { +nuke_in_line(CPUCell *cp, index_type start, index_type x_limit) { for (index_type x = start; x < x_limit; x++) { cell_set_char(cp + x, 0); cell_clear_multicell(cp + x); - clear_sprite_position(gp[x]); } } static void nuke_multicell_char_at(LineBuf *lb, index_type x_, index_type y_) { CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(lb, y_, &cp, &gp); + linebuf_init_cells(lb, y_, &cp, NULL); const CPUCell ref = cp[x_]; // only the adjacent rows that continue the char, as it may be partial or torn index_type y_start = y_, y_limit = y_ + 1; @@ -475,8 +470,9 @@ nuke_multicell_char_at(LineBuf *lb, index_type x_, index_type y_) { while (cp[x_].x && x_ > 0) x_--; index_type x_limit = MIN(lb->xnum, x_ + mcd_x_limit(&cp[x_])); for (index_type y = y_start; y < y_limit; y++) { - linebuf_init_cells(lb, y, &cp, &gp); - nuke_in_line(cp, gp, x_, x_limit); + linebuf_init_cells(lb, y, &cp, NULL); + nuke_in_line(cp, x_, x_limit); + linebuf_mark_line_dirty(lb, y); } } @@ -500,19 +496,18 @@ resize_screen_buffer_without_rewrap(LineBuf *lb, index_type lines, index_type co ans.lb->line_attrs[y] = lb->line_attrs[y]; ans.lb->line_attrs[y].has_dirty_text = true; memcpy(ans.lb->line->cpu_cells, lb->line->cpu_cells, xcommon * sizeof(lb->line->cpu_cells[0])); - memcpy(ans.lb->line->gpu_cells, lb->line->gpu_cells, xcommon * sizeof(lb->line->gpu_cells[0])); + memcpy(ans.lb->line->render_cells, lb->line->render_cells, xcommon * sizeof(lb->line->render_cells[0])); if (xcommon > lb->line->xnum) { // extend the colors/styles of the last cell to edge - GPUCell e = lb->line->gpu_cells[xcommon - 1]; - clear_sprite_position(e); - for (index_type x = xcommon; x < ans.lb->line->xnum; x++) ans.lb->line->gpu_cells[x] = e; + const uint32_t style_idx = lb->line->cpu_cells[xcommon - 1].style_idx; + for (index_type x = xcommon; x < ans.lb->line->xnum; x++) ans.lb->line->cpu_cells[x].style_idx = style_idx; } else if (xcommon < lb->line->xnum) { // remove multicell chars that were split at the right edge index_type last_x = xcommon - 1; CPUCell *c = ans.lb->line->cpu_cells + last_x; if (c->is_multicell && c->x + 1u < mcd_x_limit(c)) { while (ans.lb->line->cpu_cells[last_x].x && last_x > 0) last_x--; - nuke_in_line(ans.lb->line->cpu_cells, ans.lb->line->gpu_cells, last_x, ans.lb->line->xnum); + nuke_in_line(ans.lb->line->cpu_cells, last_x, ans.lb->line->xnum); } } } @@ -523,12 +518,12 @@ resize_screen_buffer_without_rewrap(LineBuf *lb, index_type lines, index_type co uint32_t fill_bg = 0; bg_freq_map freq; vt_init(&freq); + const StyleTable *t = tc_style_table(lb->text_cache); for (index_type y = 0; y < ans.num_content_lines_after; y++) { CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(lb, y, &cp, &gp); + linebuf_init_cells(lb, y, &cp, NULL); for (index_type x = 0; x < lb->xnum; x++) { - bg_freq_map_itr itr = vt_get_or_insert(&freq, gp[x].bg, 0); + bg_freq_map_itr itr = vt_get_or_insert(&freq, style_table_get(t, cp[x].style_idx)->bg, 0); if (!vt_is_end(itr)) itr.data->val++; } } @@ -541,9 +536,15 @@ resize_screen_buffer_without_rewrap(LineBuf *lb, index_type lines, index_type co } vt_cleanup(&freq); if (fill_bg) { + StyleTable *t = tc_style_table(ans.lb->text_cache); + uint32_t style_cache = 0; for (index_type y = ans.num_content_lines_after; y < ans.lb->ynum; y++) { linebuf_init_line(ans.lb, y); - for (index_type x = 0; x < ans.lb->xnum; x++) ans.lb->line->gpu_cells[x].bg = fill_bg; + for (index_type x = 0; x < ans.lb->xnum; x++) { + Style st = *style_table_get(t, ans.lb->line->cpu_cells[x].style_idx); + st.bg = fill_bg; + ans.lb->line->cpu_cells[x].style_idx = style_table_intern_cached(t, &st, &style_cache); + } } } } diff --git a/kitty/screen.c b/kitty/screen.c index 0005868bf..a851adfb0 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -75,15 +75,15 @@ init_tabstops(bool *tabstops, index_type count) { static bool init_overlay_line(Screen *self, index_type columns, bool keep_active) { PyMem_Free(self->overlay_line.cpu_cells); - PyMem_Free(self->overlay_line.gpu_cells); + PyMem_Free(self->overlay_line.render_cells); PyMem_Free(self->overlay_line.original_line.cpu_cells); - PyMem_Free(self->overlay_line.original_line.gpu_cells); + PyMem_Free(self->overlay_line.original_line.render_cells); self->overlay_line.cpu_cells = PyMem_Calloc(columns, sizeof(CPUCell)); - self->overlay_line.gpu_cells = PyMem_Calloc(columns, sizeof(GPUCell)); + self->overlay_line.render_cells = PyMem_Calloc(columns, sizeof(RenderCell)); self->overlay_line.original_line.cpu_cells = PyMem_Calloc(columns, sizeof(CPUCell)); - self->overlay_line.original_line.gpu_cells = PyMem_Calloc(columns, sizeof(GPUCell)); - if (!self->overlay_line.cpu_cells || !self->overlay_line.gpu_cells || !self->overlay_line.original_line.cpu_cells || - !self->overlay_line.original_line.gpu_cells) { + self->overlay_line.original_line.render_cells = PyMem_Calloc(columns, sizeof(RenderCell)); + if (!self->overlay_line.cpu_cells || !self->overlay_line.render_cells || !self->overlay_line.original_line.cpu_cells || + !self->overlay_line.original_line.render_cells) { PyErr_NoMemory(); return false; } @@ -141,6 +141,7 @@ new_screen_object(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { Py_CLEAR(self); return PyErr_NoMemory(); } + self->style_table = tc_style_table(self->text_cache); self->reload_all_gpu_data = true; self->cell_size.width = cell_width; self->cell_size.height = cell_height; @@ -538,11 +539,11 @@ last_visited_prompt_viewport(const Screen *self, index_type *y, unsigned int *sc static void -nuke_in_line(CPUCell *cp, GPUCell *gp, index_type start, index_type x_limit, char_type ch) { +nuke_in_line(CPUCell *cp, index_type start, index_type x_limit, char_type ch) { + // The line must be marked dirty after this for (index_type x = start; x < x_limit; x++) { cell_set_char(cp + x, ch); cell_clear_multicell(cp + x); - clear_sprite_position(gp[x]); } } @@ -588,7 +589,7 @@ nuke_multicell_char_at(Screen *self, index_type x_, index_type y_, bool replace_ const char_type ch = replace_with_spaces ? ' ' : 0; for (int y = rows.start; y < rows.limit; y++) { Line *line = range_line_(self, y); - nuke_in_line(line->cpu_cells, line->gpu_cells, x_, x_limit, ch); + nuke_in_line(line->cpu_cells, x_, x_limit, ch); mark_range_line_dirty(self, y); } self->is_dirty = true; @@ -598,8 +599,7 @@ static void nuke_multiline_char_intersecting_with(Screen *self, index_type x_start, index_type x_limit, index_type y_start, index_type y_limit, bool replace_with_spaces) { for (index_type y = y_start; y < y_limit; y++) { CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(self->linebuf, y, &cp, &gp); + linebuf_init_cells(self->linebuf, y, &cp, NULL); for (index_type x = x_start; x < x_limit; x++) { if (cp[x].is_multicell && cp[x].scale > 1) nuke_multicell_char_at(self, x, y, replace_with_spaces); } @@ -701,8 +701,7 @@ static void nuke_multicell_char_intersecting_with(Screen *self, index_type x_start, index_type x_limit, index_type y_start, index_type y_limit, bool replace_with_spaces) { for (index_type y = y_start; y < y_limit; y++) { CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(self->linebuf, y, &cp, &gp); + linebuf_init_cells(self->linebuf, y, &cp, NULL); if (!has_multicell_in_range(cp, x_start, x_limit)) continue; for (index_type x = x_start; x < x_limit; x++) { if (cp[x].is_multicell) nuke_multicell_char_at(self, x, y, replace_with_spaces); @@ -732,15 +731,17 @@ nuke_split_multicell_char_at_right_boundary(Screen *self, index_type x, index_ty static void nuke_incomplete_single_line_multicell_chars_in_range(Screen *self, index_type start, index_type limit, index_type y, bool replace_with_spaces) { CPUCell *cpu_cells; - GPUCell *gpu_cells; - linebuf_init_cells(self->linebuf, y, &cpu_cells, &gpu_cells); + linebuf_init_cells(self->linebuf, y, &cpu_cells, NULL); for (index_type x = start; x < limit; x++) { if (cpu_cells[x].is_multicell) { // The end of the char, at least x + 1, so that the loop always makes // progress, even for fragments of chars whose first cell is missing const index_type w = mcd_x_limit(cpu_cells + x); const index_type char_limit = x + (w > cpu_cells[x].x ? w - cpu_cells[x].x : 1); - if (cpu_cells[x].x || char_limit > limit) nuke_in_line(cpu_cells, gpu_cells, x, MIN(char_limit, limit), replace_with_spaces ? ' ' : 0); + if (cpu_cells[x].x || char_limit > limit) { + nuke_in_line(cpu_cells, x, MIN(char_limit, limit), replace_with_spaces ? ' ' : 0); + linebuf_mark_line_dirty(self->linebuf, y); + } x = char_limit - 1; } } @@ -992,9 +993,9 @@ dealloc(Screen *self) { Py_CLEAR(self->color_profile); Py_CLEAR(self->marker); PyMem_Free(self->overlay_line.cpu_cells); - PyMem_Free(self->overlay_line.gpu_cells); + PyMem_Free(self->overlay_line.render_cells); PyMem_Free(self->overlay_line.original_line.cpu_cells); - PyMem_Free(self->overlay_line.original_line.gpu_cells); + PyMem_Free(self->overlay_line.original_line.render_cells); Py_CLEAR(self->overlay_line.overlay_text); PyMem_Free(self->main_tabstops); Py_CLEAR(self->paused_rendering.linebuf); @@ -1020,9 +1021,7 @@ dealloc(Screen *self) { typedef struct text_loop_state { bool image_placeholder_marked; const CPUCell cc; - const GPUCell g; CPUCell *cp; - GPUCell *gp; GraphemeSegmentationResult seg; struct { index_type x, y; @@ -1108,7 +1107,7 @@ init_text_loop_line(Screen *self, text_loop_state *s) { // for example, when drawing a long stream of unique multi-codepoint cells, // making the cache slower to use the larger it grows. if (tc_should_gc(self->text_cache)) screen_garbage_collect_text_cache(self); - linebuf_init_cells(self->linebuf, self->cursor->y, &s->cp, &s->gp); + linebuf_init_cells(self->linebuf, self->cursor->y, &s->cp, NULL); clear_intersecting_selections(self, self->cursor->y); linebuf_mark_line_dirty(self->linebuf, self->cursor->y); s->image_placeholder_marked = false; @@ -1124,12 +1123,6 @@ reinit_text_loop_line(Screen *self, text_loop_state *s) { s->seg = seg; } -static void -zero_cells(text_loop_state *s, CPUCell *c, GPUCell *g) { - *c = s->cc; - *g = s->g; -} - typedef Line *(linefunc_t)(Screen *, int); static void @@ -1218,13 +1211,10 @@ insert_characters(Screen *self, index_type at, index_type num, index_type y, boo nuke_multiline_char_intersecting_with(self, at, self->columns, y, y + 1, replace_with_spaces); nuke_split_multicell_char_at_left_boundary(self, at, y, replace_with_spaces); CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(self->linebuf, y, &cp, &gp); + linebuf_init_cells(self->linebuf, y, &cp, NULL); // right shift - for (index_type i = self->columns - 1; i >= at + num; i--) { - cp[i] = cp[i - num]; - gp[i] = gp[i - num]; - } + for (index_type i = self->columns - 1; i >= at + num; i--) cp[i] = cp[i - num]; + linebuf_mark_line_dirty(self->linebuf, y); nuke_incomplete_single_line_multicell_chars_in_range(self, at, at + num, y, replace_with_spaces); nuke_split_multicell_char_at_right_boundary(self, self->columns - 1, y, replace_with_spaces); } @@ -1241,12 +1231,10 @@ halve_multicell_width(Screen *self, index_type x_, index_type y_) { for (int y = rows.start; y < rows.limit; y++) { Line *line = range_line_(self, y); CPUCell *c = line->cpu_cells; - GPUCell *g = line->gpu_cells; for (index_type x = x_; x < half_x_limit; x++) c[x].width = new_width; for (index_type x = half_x_limit; x < x_limit; x++) { // preserve the soft wrap marker, which lives in the last cell of the line - c[x] = (CPUCell){.next_char_was_wrapped = c[x].next_char_was_wrapped}; - clear_sprite_position(g[x]); + c[x] = (CPUCell){.style_idx = c[x].style_idx, .next_char_was_wrapped = c[x].next_char_was_wrapped}; } mark_range_line_dirty(self, y); } @@ -1296,14 +1284,71 @@ screen_garbage_collect_text_cache(Screen *self) { text_cache_gc_process_linebuf(self->text_cache, gc, self->main_linebuf); text_cache_gc_process_linebuf(self->text_cache, gc, self->alt_linebuf); text_cache_gc_process_linebuf(self->text_cache, gc, self->paused_rendering.linebuf); - if (self->overlay_line.cpu_cells) text_cache_gc_process_cells(self->text_cache, gc, self->overlay_line.cpu_cells, self->overlay_line.xnum); + // The overlay buffers have columns cells, overlay_line.xnum is the length of the overlay text, which can be larger + if (self->overlay_line.cpu_cells) text_cache_gc_process_cells(self->text_cache, gc, self->overlay_line.cpu_cells, self->columns); if (self->overlay_line.original_line.cpu_cells) - text_cache_gc_process_cells(self->text_cache, gc, self->overlay_line.original_line.cpu_cells, self->overlay_line.xnum); + text_cache_gc_process_cells(self->text_cache, gc, self->overlay_line.original_line.cpu_cells, self->columns); tc_gc_end(self->text_cache, gc); // the GC remaps all cache indices so the memoized tab indices are stale memset(&self->tab_cache, 0, sizeof(self->tab_cache)); } +static void +style_gc_process_cells(StyleTable *t, StyleTableGC *gc, CPUCell *cells, size_t count, bool quantize) { + for (size_t i = 0; i < count; i++) cells[i].style_idx = style_table_gc_map(t, gc, cells[i].style_idx, quantize); +} + +static void +style_gc_process_linebuf(StyleTable *t, StyleTableGC *gc, LineBuf *lb) { + if (lb) style_gc_process_cells(t, gc, lb->cpu_cell_buf, (size_t)lb->ynum * lb->xnum, false); +} + +static bool +garbage_collect_style_table(Screen *self, unsigned quantize_bits, bool degraded_if_full) { + // Remap the style index of every live cell onto a fresh table, so styles + // that are no longer used are freed. With quantize_bits, styles of cells + // in the scrollback get colors of reduced precision. Must only be called + // when no style indices are held outside of cells. + StyleTable *t = self->style_table; + StyleTableGC *gc = style_table_gc_begin(t, quantize_bits); + if (!gc) return false; // allocation failure, table left unchanged + // Screen cells first, so they get exact styles even if the table overflows + style_gc_process_linebuf(t, gc, self->main_linebuf); + style_gc_process_linebuf(t, gc, self->alt_linebuf); + style_gc_process_linebuf(t, gc, self->paused_rendering.linebuf); + // The overlay buffers have columns cells, overlay_line.xnum is the length of the overlay text + if (self->overlay_line.cpu_cells) style_gc_process_cells(t, gc, self->overlay_line.cpu_cells, self->columns, false); + if (self->overlay_line.original_line.cpu_cells) style_gc_process_cells(t, gc, self->overlay_line.original_line.cpu_cells, self->columns, false); + for (index_type y = self->historybuf->count; y-- > 0;) { + style_gc_process_cells(t, gc, historybuf_cpu_cells(self->historybuf, y), self->historybuf->xnum, true); + } + const bool still_full = t->count > style_table_max_count(t) - style_table_max_count(t) / 8; + style_table_gc_end(t, gc, degraded_if_full && still_full); + return true; +} + +void +screen_garbage_collect_style_table(Screen *self) { + // Called when the style table is full. Tries collecting without loss of + // precision first and if that does not free at least an eighth of the + // table, reduces the color precision of cells in the scrollback. Cells on + // screen always keep their exact styles. + StyleTable *t = self->style_table; + const uint32_t threshold = style_table_max_count(t) - style_table_max_count(t) / 8; + // If the last collection had to reduce precision, start there, as the + // scrollback is likely still full of distinct styles + unsigned bits = style_table_last_gc_quantize_bits(t); + static const unsigned ladder[] = {0, 4, 3, 2}; + unsigned start = 0; + for (unsigned i = 0; i < arraysz(ladder); i++) + if (ladder[i] == bits) start = i; + for (unsigned i = start; i < arraysz(ladder); i++) { + const bool last = i + 1 == arraysz(ladder); + if (!garbage_collect_style_table(self, ladder[i], last)) return; + if (t->count <= threshold) break; + } +} + static bool add_combining_chars(Screen *self, const char_type *chars, size_t num_chars, index_type x, index_type y) { // Adds num_chars combining chars to the cell at x, y with a single lookup in the text cache @@ -1393,27 +1438,22 @@ move_cursor_past_multicell(Screen *self, index_type required_width) { } static void -move_widened_char_past_multiline_chars(Screen *self, text_loop_state *s, CPUCell *cpu_cell, GPUCell *gpu_cell, index_type xpos, index_type ypos) { +move_widened_char_past_multiline_chars(Screen *self, text_loop_state *s, CPUCell *cpu_cell, index_type xpos, index_type ypos) { index_type before = self->cursor->y; // Remove the char from its current cell before moving it, so that it is // not removed as a multicell char in the way and the cells it is moved to // can include its current cell const CPUCell c = *cpu_cell; - GPUCell g = *gpu_cell; *cpu_cell = (CPUCell){0}; - *gpu_cell = (GPUCell){0}; self->cursor->x = xpos; self->cursor->y = ypos; if (move_cursor_past_multicell(self, 2)) { CPUCell *cp; - GPUCell *gp; - clear_sprite_position(g); - linebuf_init_cells(self->linebuf, self->cursor->y, &cp, &gp); + linebuf_init_cells(self->linebuf, self->cursor->y, &cp, NULL); + linebuf_mark_line_dirty(self->linebuf, self->cursor->y); cp[self->cursor->x] = c; - gp[self->cursor->x] = g; self->cursor->x++; cp[self->cursor->x] = c; - gp[self->cursor->x] = g; cp[self->cursor->x].x = 1; self->cursor->x++; } @@ -1422,9 +1462,8 @@ move_widened_char_past_multiline_chars(Screen *self, text_loop_state *s, CPUCell } static void -widen_cell_to_two(Screen *self, text_loop_state *s, CPUCell *cp, GPUCell *gp, index_type xpos) { +widen_cell_to_two(Screen *self, text_loop_state *s, CPUCell *cp, index_type xpos) { CPUCell *cpu_cell = cp + xpos; - GPUCell *gpu_cell = gp + xpos; cpu_cell->is_multicell = true; cpu_cell->width = 2; cpu_cell->natural_width = true; @@ -1433,17 +1472,17 @@ widen_cell_to_two(Screen *self, text_loop_state *s, CPUCell *cp, GPUCell *gp, in CPUCell *second = cp + xpos + 1; if (second->is_multicell) { if (second->y) { - move_widened_char_past_multiline_chars(self, s, cpu_cell, gpu_cell, xpos, s->prev.y); + move_widened_char_past_multiline_chars(self, s, cpu_cell, xpos, s->prev.y); return; } nuke_multicell_char_at(self, xpos + 1, s->prev.y, false); } - zero_cells(s, second, gp + xpos + 1); + // both halves of the widened char have the style of the original cell self->cursor->x++; *second = *cpu_cell; second->x = 1; } else { - move_widened_char_past_multiline_chars(self, s, cpu_cell, gpu_cell, xpos, s->prev.y); + move_widened_char_past_multiline_chars(self, s, cpu_cell, xpos, s->prev.y); } } @@ -1455,15 +1494,14 @@ is_emoji_presentation_base(char_type ch) { static void draw_combining_char(Screen *self, text_loop_state *s, char_type ch) { CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(self->linebuf, s->prev.y, &cp, &gp); + linebuf_init_cells(self->linebuf, s->prev.y, &cp, NULL); index_type xpos = s->prev.x; while (xpos && cp[xpos].is_multicell && cp[xpos].x) xpos--; if (!add_combining_char(self, ch, xpos, s->prev.y) || self->lc->count < 2) return; unsigned base_pos = self->lc->count - 2; if (ch == VS16) { // emoji presentation variation marker makes default text presentation emoji (narrow emoji) into wide emoji if (self->lc->chars[base_pos + 1] == VS16 && !cp[xpos].is_multicell && is_emoji_presentation_base(self->lc->chars[base_pos])) { - widen_cell_to_two(self, s, cp, gp, xpos); + widen_cell_to_two(self, s, cp, xpos); } } else if (ch == VS15) { const CPUCell *cpu_cell = cp + xpos; @@ -1489,7 +1527,7 @@ draw_combining_char(Screen *self, text_loop_state *s, char_type ch) { // s->seg was just stepped with ch, and grapheme_segmentation_step() copies the // grapheme break property of ch into it, so no extra char_props_for() lookup is // needed to reject the overwhelmingly common case of a zero width combining char. - widen_cell_to_two(self, s, cp, gp, xpos); + widen_cell_to_two(self, s, cp, xpos); } } @@ -1580,8 +1618,7 @@ draw_combining_char_run(Screen *self, text_loop_state *s, char_type ch, CharProp return 0; } CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(self->linebuf, s->prev.y, &cp, &gp); + linebuf_init_cells(self->linebuf, s->prev.y, &cp, NULL); index_type xpos = s->prev.x; while (xpos && cp[xpos].is_multicell && cp[xpos].x) xpos--; add_combining_chars(self, run, n, xpos, s->prev.y); @@ -1661,10 +1698,10 @@ static inline WideCellWords wide_cell_words(const CPUCell *cc) { // Built from constants, as building cells with bitfield assignments makes // compilers do partial stores to the stack and reload them with wider loads, - // which stall on store forwarding. Only the hyperlink is taken from cc. + // which stall on store forwarding. Only the hyperlink and style are taken from cc. static const CPUCell first = {.is_multicell = true, .width = 2, .scale = 1, .natural_width = true}; static const CPUCell second = {.is_multicell = true, .width = 2, .scale = 1, .natural_width = true, .x = 1}; - static const CPUCell hyperlink = {.hyperlink_id = (hyperlink_id_type)~0u}; + static const CPUCell hyperlink = {.hyperlink_id = (hyperlink_id_type)~0u, .style_idx = MAX_NUM_OF_STYLES - 1}; #define W(cell, which) cell_word(cell, which) #define HL(which) (W(cc, which) & W(&hyperlink, which)) return (WideCellWords){ @@ -1701,8 +1738,6 @@ draw_non_ascii_run(Screen *self, text_loop_state *s, const uint32_t *chars, size const index_type columns = self->columns; index_type x = self->cursor->x, last_x = x; CPUCell *cp = s->cp; - GPUCell *gp = s->gp; - const GPUCell g = s->g; // Cells are written from these templates as 64 bit values built in registers. // The first word of a cell is its char. Assembling cells as CPUCell values or // word arrays makes compilers build them on the stack with partial stores and @@ -1740,11 +1775,9 @@ draw_non_ascii_run(Screen *self, text_loop_state *s, const uint32_t *chars, size const uint64_t first = pack_cell_words(ch_word, narrow1); memcpy(dest, &first, sizeof(first)); memcpy(dest + sizeof(first), &narrow2, sizeof(narrow2)); - gp[x++] = g; + x++; } else { write_wide_cells(cp + x, ch, &wide); - gp[x] = g; - gp[x + 1] = g; x += 2; } } @@ -1765,7 +1798,6 @@ draw_text_loop(Screen *self, const uint32_t *chars, size_t num_chars, text_loop_ int char_width; #define TEMPLATE_CELLS 16 CPUCell cpu_template[TEMPLATE_CELLS]; - GPUCell gpu_template[TEMPLATE_CELLS]; bool templates_initialized = false; for (size_t i = 0; i < num_chars; i++) { uint32_t ch = map_char(self, chars[i]); @@ -1788,18 +1820,13 @@ draw_text_loop(Screen *self, const uint32_t *chars, size_t num_chars, text_loop_ while (m < n && !cp[m].is_multicell) m++; n = m; if (n) { - GPUCell *gp = s->gp + self->cursor->x; const CPUCell cc = s->cc; - const GPUCell g = s->g; // fill full blocks of cells from templates, the constant sized // memcpy is inlined by the compiler as wide stores, the chars // then need only a single 32-bit store per cell if (n >= TEMPLATE_CELLS / 4 && !templates_initialized) { templates_initialized = true; - for (unsigned k = 0; k < TEMPLATE_CELLS; k++) { - cpu_template[k] = cc; - gpu_template[k] = g; - } + for (unsigned k = 0; k < TEMPLATE_CELLS; k++) cpu_template[k] = cc; } #define copy_template(dest, template, elem) \ { \ @@ -1817,7 +1844,6 @@ draw_text_loop(Screen *self, const uint32_t *chars, size_t num_chars, text_loop_ } size_t j; copy_template(cp, cpu_template, cc); - copy_template(gp, gpu_template, g); #undef copy_template for (j = 0; j < n; j++) cell_set_char(cp + j, chars[i + j]); self->last_graphic_char = chars[i + n - 1]; @@ -1907,14 +1933,12 @@ draw_text_loop(Screen *self, const uint32_t *chars, size_t num_chars, text_loop_ } const WideCellWords wide = wide_cell_words(&s->cc); write_wide_cells(fc, ch, &wide); - s->gp[self->cursor->x] = s->g; - s->gp[self->cursor->x + 1] = s->g; s->prev.y = self->cursor->y; s->prev.x = self->cursor->x; s->prev.cc = fc; self->cursor->x += 2; } else { - zero_cells(s, fc, s->gp + self->cursor->x); + *fc = s->cc; cell_set_char(fc, ch); s->prev.y = self->cursor->y; s->prev.x = self->cursor->x; @@ -1927,19 +1951,20 @@ draw_text_loop(Screen *self, const uint32_t *chars, size_t num_chars, text_loop_ #undef TEMPLATE_CELLS } -#define PREPARE_FOR_DRAW_TEXT \ - if (tc_should_gc(self->text_cache)) screen_garbage_collect_text_cache(self); \ - const bool force_underline = OPT(underline_hyperlinks) == UNDERLINE_ALWAYS && self->active_hyperlink_id != 0; \ - CellAttrs attrs = cursor_to_attrs(self->cursor); \ - if (force_underline) attrs.decoration = OPT(url_style); \ - text_loop_state s = { \ - .cc = (CPUCell){.hyperlink_id = self->active_hyperlink_id}, \ - .g = (GPUCell){ \ - .attrs = attrs, \ - .fg = self->cursor->sgr.fg & COL_MASK, \ - .bg = self->cursor->sgr.bg & COL_MASK, \ - .decoration_fg = force_underline ? ((OPT(url_color) & COL_MASK) << 8) | 2 : self->cursor->sgr.decoration_fg & COL_MASK, \ - }}; +#define PREPARE_FOR_DRAW_TEXT \ + if (tc_should_gc(self->text_cache)) screen_garbage_collect_text_cache(self); \ + if (style_table_needs_gc(self->style_table)) screen_garbage_collect_style_table(self); \ + const bool force_underline = OPT(underline_hyperlinks) == UNDERLINE_ALWAYS && self->active_hyperlink_id != 0; \ + Style draw_style = self->cursor->style; \ + uint32_t draw_style_idx; \ + if (force_underline) { \ + draw_style.attrs.decoration = OPT(url_style); \ + draw_style.decoration_fg = ((OPT(url_color) & COL_MASK) << 8) | 2; \ + draw_style_idx = style_table_intern_cached(self->style_table, &draw_style, &self->hyperlink_style_idx_cache); \ + } else draw_style_idx = screen_cursor_style_idx(self); \ + text_loop_state s = { \ + .cc = (CPUCell){.hyperlink_id = self->active_hyperlink_id, .style_idx = draw_style_idx}, \ + }; static void draw_text(Screen *self, const uint32_t *chars, size_t num_chars) { @@ -2000,6 +2025,7 @@ handle_fixed_width_multicell_command(Screen *self, CPUCell mcd, ListOfChars *lc) lc->count = MIN(lc->count, MAX_NUM_CODEPOINTS_PER_CELL); PREPARE_FOR_DRAW_TEXT; mcd.hyperlink_id = s.cc.hyperlink_id; + mcd.style_idx = s.cc.style_idx; cell_set_chars(&mcd, self->text_cache, lc); move_cursor_past_multicell(self, width); if (height > 1) { @@ -2024,14 +2050,13 @@ handle_fixed_width_multicell_command(Screen *self, CPUCell mcd, ListOfChars *lc) } } for (index_type y = self->cursor->y; y < self->cursor->y + height; y++) { - linebuf_init_cells(self->linebuf, y, &s.cp, &s.gp); + linebuf_init_cells(self->linebuf, y, &s.cp, NULL); linebuf_mark_line_dirty(self->linebuf, y); mcd.x = 0; mcd.y = y - self->cursor->y; for (index_type x = self->cursor->x; x < self->cursor->x + width; x++, mcd.x++) { if (s.cp[x].is_multicell) nuke_multicell_char_at(self, x, y, s.cp[x].x + s.cp[x].y > 0); s.cp[x] = mcd; - s.gp[x] = s.g; } } self->cursor->x += width; @@ -2136,7 +2161,7 @@ select_graphic_rendition(Screen *self, int *params, unsigned int count, bool is_ num = MIN(num, self->columns - x); for (index_type y = region.top; y < MIN(region.bottom + 1, self->lines); y++) { linebuf_init_line(self->linebuf, y); - apply_sgr_to_cells(self->linebuf->line->gpu_cells + x, num, params, count, is_group); + apply_sgr_to_cells(self->style_table, self->linebuf->line->cpu_cells + x, num, params, count, is_group); } } else { index_type x, num; @@ -2146,7 +2171,7 @@ select_graphic_rendition(Screen *self, int *params, unsigned int count, bool is_ x = MIN(region.left, self->columns - 1); num = region.right >= x ? region.right - x + 1 : 0; num = MIN(self->columns - x, num); - apply_sgr_to_cells(self->linebuf->line->gpu_cells + x, num, params, count, is_group); + apply_sgr_to_cells(self->style_table, self->linebuf->line->cpu_cells + x, num, params, count, is_group); } } else { for (index_type y = region.top; y < MIN(region.bottom + 1, self->lines); y++) { @@ -2161,13 +2186,13 @@ select_graphic_rendition(Screen *self, int *params, unsigned int count, bool is_ num = self->columns; } linebuf_init_line(self->linebuf, y); - apply_sgr_to_cells(self->linebuf->line->gpu_cells + x, num, params, count, is_group); + apply_sgr_to_cells(self->style_table, self->linebuf->line->cpu_cells + x, num, params, count, is_group); } } } } else { cursor_from_sgr(self->cursor, params, count, is_group); - self->sgr_blink_was_used |= self->cursor->sgr.blink; + self->sgr_blink_was_used |= self->cursor->style.attrs.blink; } } @@ -3396,9 +3421,8 @@ nuke_multiline_chars_split_by_erase(Screen *self, unsigned int how, index_type a for (int top = 1; top >= 0; top--) { if (top ? how == 3 : how != 1) continue; CPUCell *cp; - GPUCell *gp; index_type y = top ? a : b - 1; - linebuf_init_cells(self->linebuf, y, &cp, &gp); + linebuf_init_cells(self->linebuf, y, &cp, NULL); for (index_type x = 0; x < self->columns; x++) { if (!cp[x].is_multicell) continue; if (top ? cp[x].y > 0 : cp[x].y + 1 < cp[x].scale) nuke_multicell_char_at(self, x, y, false); @@ -3579,13 +3603,10 @@ remove_characters(Screen *self, index_type at, index_type num, index_type y, boo nuke_multiline_char_intersecting_with(self, at, self->columns, y, y + 1, replace_with_spaces); nuke_split_multicell_char_at_left_boundary(self, at, y, replace_with_spaces); CPUCell *cp; - GPUCell *gp; - linebuf_init_cells(self->linebuf, y, &cp, &gp); + linebuf_init_cells(self->linebuf, y, &cp, NULL); // left shift - for (index_type i = at; i < self->columns - num; i++) { - cp[i] = cp[i + num]; - gp[i] = gp[i + num]; - } + for (index_type i = at; i < self->columns - num; i++) cp[i] = cp[i + num]; + linebuf_mark_line_dirty(self->linebuf, y); nuke_incomplete_single_line_multicell_chars_in_range(self, at, self->columns, y, replace_with_spaces); } @@ -4443,13 +4464,13 @@ get_line_edge_colors_at_row(Screen *self, index_type y, color_type *left, color_ char_type left_char = line_get_char(line, cell_color_x); bool reversed = false; colors_for_cell(line, self->color_profile, &cell_color_x, &left_cell_fg, &left_cell_bg, &reversed); - if (left_is_default) *left_is_default = (line->gpu_cells[cell_color_x].bg & 0xff) == 0; + if (left_is_default) *left_is_default = (line_cell_style(line, cell_color_x)->bg & 0xff) == 0; if (left) *left = effective_cell_edge_color(left_char, left_cell_fg, left_cell_bg, true); if (line->xnum > 0) cell_color_x = line->xnum - 1; char_type right_char = line_get_char(line, cell_color_x); reversed = false; // reset: colors_for_cell only sets this flag, never clears it colors_for_cell(line, self->color_profile, &cell_color_x, &right_cell_fg, &right_cell_bg, &reversed); - if (right_is_default) *right_is_default = (line->gpu_cells[cell_color_x].bg & 0xff) == 0; + if (right_is_default) *right_is_default = (line_cell_style(line, cell_color_x)->bg & 0xff) == 0; if (right) *right = effective_cell_edge_color(right_char, right_cell_fg, right_cell_bg, false); return true; } @@ -4463,8 +4484,8 @@ get_line_edge_colors(Screen *self, color_type *left, color_type *right) { static void update_line_data(Line *line, unsigned int dest_y, uint8_t *data) { - size_t base = sizeof(GPUCell) * dest_y * line->xnum; - memcpy(data + base, line->gpu_cells, line->xnum * sizeof(GPUCell)); + GPUCell *dest = (GPUCell *)(data + sizeof(GPUCell) * dest_y * line->xnum); + expand_cells_for_gpu(line_style_table(line), line->cpu_cells, line->render_cells, line->xnum, dest); } static void @@ -4479,7 +4500,7 @@ render_line_for_virtual_y(Screen *self, int y, Line *line, index_type *lnum, boo if (y < (int)self->scrolled_by) { int idx = (int)self->scrolled_by - 1 - y; if (idx >= 0 && (unsigned)idx < self->historybuf->count) { - historybuf_init_line(self->historybuf, idx, line); + if (historybuf_init_line_for_render(self->historybuf, idx, line, render_lines_for_screen(self))) line->attrs.has_dirty_text = true; line->xnum = self->columns; line->ynum = (index_type)MIN(MAX(y, 0), (int)self->lines - 1); *lnum = (index_type)idx; @@ -4541,7 +4562,6 @@ screen_render_line_graphics(Screen *self, Line *line, int32_t row) { uint32_t prev_img_col = 0; for (i = 0; i < line->xnum; i++) { CPUCell *cpu_cell = line->cpu_cells + i; - GPUCell *gpu_cell = line->gpu_cells + i; uint32_t cur_img_id_lower24bits = 0; uint32_t cur_placement_id = 0; uint32_t cur_img_id_higher8bits = 0; @@ -4551,8 +4571,9 @@ screen_render_line_graphics(Screen *self, Line *line, int32_t row) { line->attrs.has_image_placeholders = true; // The lower 24 bits of the image id are encoded in the foreground // color, and the placement id is (optionally) in the underline color. - cur_img_id_lower24bits = color_to_id(gpu_cell->fg); - cur_placement_id = color_to_id(gpu_cell->decoration_fg); + const Style *st = style_table_get(self->style_table, cpu_cell->style_idx); + cur_img_id_lower24bits = color_to_id(st->fg); + cur_placement_id = color_to_id(st->decoration_fg); text_in_cell(cpu_cell, self->text_cache, self->lc); // If the char has diacritics, use them as row and column indices. if (self->lc->count > 1 && self->lc->chars[1]) cur_img_row = diacritic_to_rowcolumn(self->lc->chars[1]); @@ -5658,20 +5679,18 @@ screen_draw_overlay_line(Screen *self) { // dashed avoids colliding with the styles applications already use: curly // for spell checking and straight for hyperlinks. Saved and restored around // the draw, like the modes above. - const bool orig_italic = self->cursor->sgr.italic; - const uint8_t orig_decoration = self->cursor->sgr.decoration; - const color_type orig_decoration_fg = self->cursor->sgr.decoration_fg; - self->cursor->sgr.italic = true; - self->cursor->sgr.decoration = 5; // dashed - self->cursor->sgr.decoration_fg = ((colorprofile_to_color_with_fallback( - self->color_profile, - self->color_profile->overridden.highlight_bg, - self->color_profile->configured.highlight_bg, - self->color_profile->overridden.default_fg, - self->color_profile->configured.default_fg) & - COL_MASK) - << 8) | - 2; + const Style orig_style = self->cursor->style; + self->cursor->style.attrs.italic = true; + self->cursor->style.attrs.decoration = 5; // dashed + self->cursor->style.decoration_fg = ((colorprofile_to_color_with_fallback( + self->color_profile, + self->color_profile->overridden.highlight_bg, + self->color_profile->configured.highlight_bg, + self->color_profile->overridden.default_fg, + self->color_profile->configured.default_fg) & + COL_MASK) + << 8) | + 2; self->cursor->x = xstart; self->cursor->y = self->overlay_line.ynum; self->overlay_line.xnum = 0; @@ -5723,9 +5742,7 @@ screen_draw_overlay_line(Screen *self) { self->overlay_line.xnum += len; } self->overlay_line.cursor_x = overlay_line_cursor_x(self); - self->cursor->sgr.italic = orig_italic; - self->cursor->sgr.decoration = orig_decoration; - self->cursor->sgr.decoration_fg = orig_decoration_fg; + self->cursor->style = orig_style; self->cursor = orig_cursor; self->modes.mDECAWM = orig_line_wrap_mode; self->modes.mDECTCEM = orig_cursor_enable_mode; @@ -5757,11 +5774,11 @@ update_overlay_position(Screen *self) { static void render_overlay_line(Screen *self, Line *line, FONTS_DATA_HANDLE fonts_data) { #define ol self->overlay_line - line_save_cells(line, 0, line->xnum, ol.original_line.gpu_cells, ol.original_line.cpu_cells); + line_save_cells(line, 0, line->xnum, ol.original_line.render_cells, ol.original_line.cpu_cells); screen_draw_overlay_line(self); render_line(fonts_data, line, ol.ynum, self->cursor, self->disable_ligatures, self->lc); - line_save_cells(line, 0, line->xnum, ol.gpu_cells, ol.cpu_cells); - line_reset_cells(line, 0, line->xnum, ol.original_line.gpu_cells, ol.original_line.cpu_cells); + line_save_cells(line, 0, line->xnum, ol.render_cells, ol.cpu_cells); + line_reset_cells(line, 0, line->xnum, ol.original_line.render_cells, ol.original_line.cpu_cells); ol.is_dirty = false; const index_type x = screen_overlay_line_start(self), y = MIN(ol.ynum + self->scrolled_by, self->lines - 1); // The IME is anchored at the start of the pre-edit text, see prepare_ime_position_update_event() @@ -5777,7 +5794,7 @@ static void update_overlay_line_data(Screen *self, uint8_t *data) { const int render_row_offset = pixel_scroll_enabled(self); const size_t base = sizeof(GPUCell) * (self->overlay_line.ynum + self->scrolled_by + render_row_offset) * self->columns; - memcpy(data + base, self->overlay_line.gpu_cells, self->columns * sizeof(GPUCell)); + expand_cells_for_gpu(self->style_table, self->overlay_line.cpu_cells, self->overlay_line.render_cells, self->columns, (GPUCell *)(data + base)); } // }}} @@ -6022,7 +6039,6 @@ remove_multicell_fragments_cut_off_above_screen(Screen *self, index_type num_lin found = true; cell_set_char(c, 0); cell_clear_multicell(c); - clear_sprite_position(line->gpu_cells[x]); } } if (!found) break; // a multicell char cannot skip a line @@ -7411,10 +7427,8 @@ screen_mark_all(Screen *self) { linebuf_init_line(self->alt_linebuf, y); mark_text_in_line(self->marker, self->alt_linebuf->line, &self->as_ansi_buf); } - for (index_type y = 0; y < self->historybuf->count; y++) { - historybuf_init_line(self->historybuf, y, self->historybuf->line); - mark_text_in_line(self->marker, self->historybuf->line, &self->as_ansi_buf); - } + // Marks are render data, history lines are marked when rendered + for (index_type y = 0; y < self->historybuf->count; y++) historybuf_mark_line_dirty(self->historybuf, y); self->is_dirty = true; } @@ -7446,10 +7460,20 @@ scroll_to_next_mark(Screen *self, PyObject *args) { unsigned int mark = 0; if (!PyArg_ParseTuple(args, "|Ip", &mark, &backwards)) return NULL; if (!screen_has_marker(self) || self->linebuf == self->alt_linebuf) Py_RETURN_FALSE; + // History lines have no render data and so no marks, compute them in a scratch buffer + RAII_ALLOC(RenderCell, scratch, calloc(self->historybuf->xnum, sizeof(RenderCell))); + if (!scratch) return PyErr_NoMemory(); + bool found; +#define history_line_has_mark(y) \ + (historybuf_init_line(self->historybuf, y, self->historybuf->line), \ + self->historybuf->line->render_cells = scratch, \ + mark_text_in_line(self->marker, self->historybuf->line, &self->as_ansi_buf), \ + found = line_has_mark(self->historybuf->line, mark), \ + self->historybuf->line->render_cells = NULL, \ + found) if (backwards) { for (unsigned int y = self->scrolled_by; y < self->historybuf->count; y++) { - historybuf_init_line(self->historybuf, y, self->historybuf->line); - if (line_has_mark(self->historybuf->line, mark)) { + if (history_line_has_mark(y)) { screen_history_scroll(self, y - self->scrolled_by + 1, true); Py_RETURN_TRUE; } @@ -7458,8 +7482,11 @@ scroll_to_next_mark(Screen *self, PyObject *args) { Line *line; for (unsigned int y = self->scrolled_by; y > 0; y--) { if (y > self->lines) { - historybuf_init_line(self->historybuf, y - self->lines, self->historybuf->line); - line = self->historybuf->line; + if (history_line_has_mark(y - self->lines)) { + screen_history_scroll(self, self->scrolled_by - y + 1, false); + Py_RETURN_TRUE; + } + continue; } else { linebuf_init_line(self->linebuf, self->lines - y); line = self->linebuf->line; @@ -7470,6 +7497,7 @@ scroll_to_next_mark(Screen *self, PyObject *args) { } } } +#undef history_line_has_mark Py_RETURN_FALSE; } @@ -7480,8 +7508,7 @@ marked_cells(Screen *self, PyObject *o UNUSED) { for (index_type y = 0; y < self->lines; y++) { linebuf_init_line(self->linebuf, y); for (index_type x = 0; x < self->columns; x++) { - GPUCell *gpu_cell = self->linebuf->line->gpu_cells + x; - const unsigned int mark = gpu_cell->attrs.mark; + const unsigned int mark = self->linebuf->line->render_cells[x].mark; if (mark) { RAII_PyObject(t, Py_BuildValue("III", x, y, mark)); if (!t) { return NULL; } @@ -7817,6 +7844,43 @@ test_commit_write_buffer(Screen *screen, PyObject *args) { return PyLong_FromSize_t(s); } +static bool +verify_styles_in_line(const StyleTable *t, const char *which, index_type y, const CPUCell *c, index_type xnum, PyObject *ans) { + for (index_type x = 0; x < xnum; x++) { + if (c[x].style_idx >= t->count) { + RAII_PyObject(e, Py_BuildValue("sIII", which, y, x, (unsigned)c[x].style_idx)); + if (!e || PyList_Append(ans, e) != 0) return false; + if (PyList_GET_SIZE(ans) > 16) return true; + } + } + return true; +} + +static PyObject * +test_verify_styles(Screen *self, PyObject *a UNUSED) { + // Check that the style index of every cell is valid. Returns a list of invalid cells. + RAII_PyObject(ans, PyList_New(0)); + if (!ans) return NULL; + const StyleTable *t = self->style_table; + LineBuf *bufs[3] = {self->main_linebuf, self->alt_linebuf, self->paused_rendering.linebuf}; + const char *names[3] = {"main", "alt", "paused"}; + for (unsigned b = 0; b < arraysz(bufs); b++) { + LineBuf *lb = bufs[b]; + if (!lb) continue; + for (index_type y = 0; y < lb->ynum; y++) { + if (!verify_styles_in_line(t, names[b], y, linebuf_cpu_cells_for_line(lb, y), lb->xnum, ans)) return NULL; + } + } + for (index_type y = 0; y < self->historybuf->count; y++) { + if (!verify_styles_in_line(t, "history", y, historybuf_cpu_cells(self->historybuf, y), self->historybuf->xnum, ans)) return NULL; + } + if (self->overlay_line.cpu_cells && !verify_styles_in_line(t, "overlay", 0, self->overlay_line.cpu_cells, self->columns, ans)) return NULL; + if (self->overlay_line.original_line.cpu_cells && + !verify_styles_in_line(t, "overlay_original", 0, self->overlay_line.original_line.cpu_cells, self->columns, ans)) + return NULL; + return Py_NewRef(ans); +} + static PyObject * test_parse_written_data(Screen *screen, PyObject *args) { ParseData pd = {.now = monotonic()}; @@ -7939,6 +8003,68 @@ cpu_cells(Screen *self, PyObject *args) { return Py_NewRef(ans); } +static PyObject * +cell_style_idx(Screen *self, PyObject *args) { + int y; + unsigned int x; + if (!PyArg_ParseTuple(args, "iI", &y, &x)) return NULL; + if (y >= (int)self->lines || x >= self->columns) { + PyErr_SetString(PyExc_IndexError, "out of bounds"); + return NULL; + } + CPUCell *cells; + if (y >= 0) cells = linebuf_cpu_cells_for_line(self->linebuf, y); + else { + Line *l = self->linebuf == self->main_linebuf ? checked_range_line(self, y) : NULL; + if (!l) { + PyErr_SetString(PyExc_IndexError, "y out of bounds"); + return NULL; + } + cells = l->cpu_cells; + } + return PyLong_FromUnsignedLong(cells[x].style_idx); +} + +static PyObject * +style_table_info(Screen *self, PyObject *a UNUSED) { + return Py_BuildValue( + "{sI sI sI sK sO}", + "count", + self->style_table->count, + "max_count", + style_table_max_count(self->style_table), + "generation", + self->style_table->generation, + "gc_count", + (unsigned long long)style_table_gc_count(self->style_table), + "degraded", + style_table_is_degraded(self->style_table) ? Py_True : Py_False); +} + +static PyObject * +test_gc_style_table(Screen *self, PyObject *args) { + unsigned int quantize_bits = 0; + if (!PyArg_ParseTuple(args, "|I", &quantize_bits)) return NULL; + if (quantize_bits > 7) { + PyErr_SetString(PyExc_ValueError, "quantize_bits must be less than 8"); + return NULL; + } + if (!garbage_collect_style_table(self, quantize_bits, false)) return PyErr_NoMemory(); + Py_RETURN_NONE; +} + +static PyObject * +test_set_style_table_limit(Screen *self, PyObject *val) { + if (!PyLong_Check(val)) { + PyErr_SetString(PyExc_TypeError, "limit must be an int"); + return NULL; + } + unsigned long limit = PyLong_AsUnsignedLong(val); + if (PyErr_Occurred()) return NULL; + style_table_set_max_count(self->style_table, limit > UINT32_MAX ? UINT32_MAX : (uint32_t)limit); + Py_RETURN_NONE; +} + static PyObject * test_ch_and_idx(PyObject *self UNUSED, PyObject *val) { CPUCell c = {0}; @@ -7957,6 +8083,11 @@ static PyMethodDef methods[] = { METHODB(test_create_write_buffer, METH_NOARGS), METHODB(test_commit_write_buffer, METH_VARARGS), METHODB(test_parse_written_data, METH_VARARGS), + METHODB(test_verify_styles, METH_NOARGS), + METHODB(cell_style_idx, METH_VARARGS), + METHODB(style_table_info, METH_NOARGS), + METHODB(test_set_style_table_limit, METH_O), + METHODB(test_gc_style_table, METH_VARARGS), METHODB(test_draw_overlay_line, METH_VARARGS), METHODB(test_update_overlay_text, METH_VARARGS), METHODB(restore_scrollback, METH_O), diff --git a/kitty/screen.h b/kitty/screen.h index f9489688b..3e1fc2655 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -89,7 +89,7 @@ typedef struct { typedef struct { PyObject *overlay_text; CPUCell *cpu_cells; - GPUCell *gpu_cells; + RenderCell *render_cells; index_type xstart, ynum, xnum, cursor_x, text_len; // Position of the IME cursor inside the pre-edit text in unicode code points, negative if unknown int preedit_cursor; @@ -97,7 +97,7 @@ typedef struct { bool is_dirty; struct { CPUCell *cpu_cells; - GPUCell *gpu_cells; + RenderCell *render_cells; Cursor cursor; } original_line; struct { @@ -151,6 +151,8 @@ typedef struct { Savepoint main_savepoint, alt_savepoint; PyObject *callbacks, *test_child; TextCache *text_cache; + StyleTable *style_table; // owned by text_cache + uint32_t hyperlink_style_idx_cache; LineBuf *linebuf, *main_linebuf, *alt_linebuf; GraphicsManager *grman, *main_grman, *alt_grman; HistoryBuf *historybuf; @@ -277,6 +279,7 @@ typedef struct { void screen_align(Screen *); void screen_garbage_collect_text_cache(Screen *screen); +void screen_garbage_collect_style_table(Screen *screen); void screen_restore_cursor(Screen *); void screen_save_cursor(Screen *); void screen_restore_modes(Screen *); @@ -417,3 +420,8 @@ DECLARE_CH_SCREEN_HANDLER(tab) DECLARE_CH_SCREEN_HANDLER(linefeed) DECLARE_CH_SCREEN_HANDLER(carriage_return) #undef DECLARE_CH_SCREEN_HANDLER + +static inline uint32_t +screen_cursor_style_idx(Screen *self) { + return style_table_intern_cached(self->style_table, &self->cursor->style, &self->cursor->style_idx); +} diff --git a/kitty/shaders.c b/kitty/shaders.c index 9bc65d31b..91f25baff 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -1134,7 +1134,7 @@ cell_update_uniform_block( // and color the cursor, for example when it is over a wide character. overlay_line_for_cursor = *line_for_cursor; overlay_line_for_cursor.cpu_cells = screen->overlay_line.cpu_cells; - overlay_line_for_cursor.gpu_cells = screen->overlay_line.gpu_cells; + overlay_line_for_cursor.render_cells = screen->overlay_line.render_cells; line_for_cursor = &overlay_line_for_cursor; } } @@ -1163,7 +1163,7 @@ cell_update_uniform_block( } // If you change the following algorithm remember to change it in the cell shader for extra cursors too if (rd->main_cursor_is_special) { - if (line_for_cursor && line_for_cursor->gpu_cells[cell_color_x].attrs.dim) { + if (line_for_cursor && line_cell_style(line_for_cursor, cell_color_x)->attrs.dim) { cell_fg = dimmed_cursor_color(cell_fg, cell_bg, OPT(dim_opacity)); pick_cursor_color(cell_fg, cell_bg, &main_cursor_fg, &main_cursor_bg, rd->default_fg, rd->bg_colors0); } else if (cell_bg == cell_fg) { diff --git a/kitty/style-table.c b/kitty/style-table.c new file mode 100644 index 000000000..cd896eb0a --- /dev/null +++ b/kitty/style-table.c @@ -0,0 +1,312 @@ +/* + * style-table.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#include "style-table.h" +#include + +static uint64_t +hash_style(Style k) { + return XXH3_64bits(&k, sizeof(k)); +} + +static bool +cmpr_style(Style a, Style b) { + return style_eq(&a, &b); +} + +// Maps styles to indices, used only to find approximate matches when the table is full +#define NAME style_map +#define KEY_TY Style +#define VAL_TY uint32_t +#define HASH_FN hash_style +#define CMPR_FN cmpr_style +#include "kitty-verstable.h" + +// The main map is a set of style indices, hashing and comparing the styles +// they refer to, which keeps it four times smaller than a map with styles as +// keys. That matters as with many distinct styles, for example, text with a +// different 24-bit color for every char, a larger map evicts the data being +// parsed from the CPU cache. Hash and compare functions get no context, so the +// styles array of the table being operated on is in a static variable, set +// before every operation on the set. Tables are only used on the main thread. +// A style not in the table is looked up using the PROBE_KEY index which refers to probe_style. +#define PROBE_KEY UINT32_MAX +static const Style *ctx_styles = NULL; +static Style probe_style = {0}; + +static inline const Style * +resolve_key(uint32_t k) { + return k == PROBE_KEY ? &probe_style : ctx_styles + k; +} + +static uint64_t +hash_style_idx(uint32_t k) { + return XXH3_64bits(resolve_key(k), sizeof(Style)); +} + +static bool +cmpr_style_idx(uint32_t a, uint32_t b) { + return style_eq(resolve_key(a), resolve_key(b)); +} + +#define NAME style_set +#define KEY_TY uint32_t +#define HASH_FN hash_style_idx +#define CMPR_FN cmpr_style_idx +#include "kitty-verstable.h" + +// Number of slots in the cache of recently interned styles, a power of two +#define RECENT_CACHE_BITS 6u + +typedef struct StyleTableImpl { + StyleTable pub; // must be first + uint32_t capacity, max_count; + style_set map; + // Indices of recently interned styles, indexed by a cheap hash of the + // style, to avoid hashing and looking up styles that are used repeatedly. + // Entries need no invalidation as they are checked against the style. + uint32_t recent[1u << RECENT_CACHE_BITS]; + // Used to find approximate matches for new styles when the table is full + struct { + style_map fine, coarse; + bool valid; + } approx; + uint32_t misses_since_gc; + uint64_t gc_count; + unsigned last_gc_quantize_bits; + bool degraded, lossy_reported; +} StyleTableImpl; + +#define INITIAL_CAPACITY 256u +#define FINE_BITS 4u +#define COARSE_BITS 2u + +StyleTable * +style_table_alloc(void) { + StyleTableImpl *self = calloc(1, sizeof(StyleTableImpl)); + if (!self) return NULL; + self->capacity = INITIAL_CAPACITY; + self->pub.styles = calloc(self->capacity, sizeof(Style)); + if (!self->pub.styles) { + free(self); + return NULL; + } + self->pub.count = 1; // the default style + self->pub.generation = 1; + self->max_count = MAX_NUM_OF_STYLES; + vt_init(&self->map); + vt_init(&self->approx.fine); + vt_init(&self->approx.coarse); + return &self->pub; +} + +void +style_table_free(StyleTable *pub) { + if (!pub) return; + StyleTableImpl *self = (StyleTableImpl *)pub; + vt_cleanup(&self->map); + vt_cleanup(&self->approx.fine); + vt_cleanup(&self->approx.coarse); + free(self->pub.styles); + free(self); +} + +static void +invalidate_approx(StyleTableImpl *self) { + if (self->approx.valid) { + vt_clear(&self->approx.fine); + vt_clear(&self->approx.coarse); + self->approx.valid = false; + } +} + +static void +bump_generation(StyleTableImpl *self) { + if (++self->pub.generation == 0) self->pub.generation = 1; +} + +void +style_table_set_max_count(StyleTable *pub, uint32_t max_count) { + StyleTableImpl *self = (StyleTableImpl *)pub; + self->max_count = MAX(2u, MIN(max_count, MAX_NUM_OF_STYLES)); +} + +uint32_t +style_table_max_count(const StyleTable *pub) { + return ((const StyleTableImpl *)pub)->max_count; +} + +static color_type +quantize_color(color_type c, unsigned bits) { + // Reduce the precision of RGB colors to bits per channel, rounding to the + // middle of the range of values each reduced value represents + if ((c & 0xff) != 2) return c; + const uint32_t mask = (0xffu << (8 - bits)) & 0xffu, half = 1u << (7 - bits); + uint32_t ans = 2; + for (unsigned shift = 8; shift < 32; shift += 8) ans |= ((((c >> shift) & 0xff) & mask) | half) << shift; + return ans; +} + +Style +style_quantize(const Style *s, unsigned bits) { + if (!bits || bits >= 8) return *s; + Style ans = *s; + ans.fg = quantize_color(s->fg, bits); + ans.bg = quantize_color(s->bg, bits); + ans.decoration_fg = quantize_color(s->decoration_fg, bits); + return ans; +} + +static void +build_approx(StyleTableImpl *self) { + for (uint32_t i = 0; i < self->pub.count; i++) { + const Style *s = self->pub.styles + i; + Style q = style_quantize(s, FINE_BITS); + if (vt_is_end(vt_get_or_insert(&self->approx.fine, q, i))) fatal("Out of memory building approximate style map"); + q = style_quantize(s, COARSE_BITS); + if (vt_is_end(vt_get_or_insert(&self->approx.coarse, q, i))) fatal("Out of memory building approximate style map"); + } + self->approx.valid = true; +} + +static uint32_t +approximate_match(StyleTableImpl *self, const Style *style) { + // The table is full, use an existing style with the same attributes and close colors + if (!self->approx.valid) build_approx(self); + style_map_itr itr = vt_get(&self->approx.fine, style_quantize(style, FINE_BITS)); + if (!vt_is_end(itr)) return itr.data->val; + itr = vt_get(&self->approx.coarse, style_quantize(style, COARSE_BITS)); + if (!vt_is_end(itr)) return itr.data->val; + return 0; +} + +static inline uint32_t +recent_slot(const Style *s) { + const uint32_t h = (s->fg * 0x9E3779B1u) ^ (s->bg * 0x85EBCA77u) ^ (s->decoration_fg * 0xC2B2AE3Du) ^ (s->attrs.val * 0x27D4EB2Fu); + return h >> (32 - RECENT_CACHE_BITS); +} + +uint32_t +style_table_intern(StyleTable *pub, const Style *style) { + static const Style default_style = {0}; + if (style_eq(style, &default_style)) return 0; + StyleTableImpl *self = (StyleTableImpl *)pub; + const uint32_t slot = recent_slot(style), recent = self->recent[slot]; + if (recent < self->pub.count && style_eq(self->pub.styles + recent, style)) return recent; + ctx_styles = self->pub.styles; + probe_style = *style; + style_set_itr itr = vt_get(&self->map, PROBE_KEY); + if (!vt_is_end(itr)) return (self->recent[slot] = itr.data->key); + if (self->pub.count >= self->max_count) { + // The owner of the table compacts it when it is safe to do so, see style_table_needs_gc() + self->misses_since_gc++; + return approximate_match(self, style); + } + if (self->pub.count >= self->capacity) { + const uint32_t new_capacity = MIN(self->capacity * 2, MAX_NUM_OF_STYLES); + Style *n = realloc(self->pub.styles, new_capacity * sizeof(Style)); + if (!n) fatal("Out of memory growing the style table"); + self->pub.styles = n; + self->capacity = new_capacity; + } + const uint32_t idx = self->pub.count++; + self->pub.styles[idx] = *style; + ctx_styles = self->pub.styles; // may have been changed by realloc() above + if (vt_is_end(vt_insert(&self->map, idx))) fatal("Out of memory inserting into the style table"); + return (self->recent[slot] = idx); +} + +// Garbage collection {{{ + +bool +style_table_needs_gc(const StyleTable *pub) { + const StyleTableImpl *self = (const StyleTableImpl *)pub; + if (self->pub.count < self->max_count) return false; + // When even the styles on screen do not fit, collecting cannot free much, + // so wait until many new styles have been used before trying again + if (self->degraded) return self->misses_since_gc >= self->max_count / 2; + return true; +} + +struct StyleTableGC { + Style *old_styles; + uint32_t old_count, *remap, *quantized_remap; // old index -> new index + 1, 0 means not yet remapped + unsigned quantize_bits; +}; + +StyleTableGC * +style_table_gc_begin(StyleTable *pub, unsigned quantize_bits) { + StyleTableImpl *self = (StyleTableImpl *)pub; + StyleTableGC *gc = calloc(1, sizeof(StyleTableGC)); + if (!gc) return NULL; + gc->old_count = self->pub.count; + gc->quantize_bits = quantize_bits; + gc->old_styles = malloc(gc->old_count * sizeof(Style)); + gc->remap = calloc(gc->old_count, sizeof(uint32_t)); + if (quantize_bits) gc->quantized_remap = calloc(gc->old_count, sizeof(uint32_t)); + if (!gc->old_styles || !gc->remap || (quantize_bits && !gc->quantized_remap)) { + free(gc->old_styles); + free(gc->remap); + free(gc->quantized_remap); + free(gc); + return NULL; + } + memcpy(gc->old_styles, self->pub.styles, gc->old_count * sizeof(Style)); + vt_clear(&self->map); + self->pub.count = 1; + invalidate_approx(self); + return gc; +} + +uint32_t +style_table_gc_map(StyleTable *pub, StyleTableGC *gc, uint32_t old_idx, bool quantize) { + // Called for every live cell, returns its new style index. quantize is ignored unless + // the GC was started with quantize_bits. Cannot overflow the table as the number of + // styles re-interned is at most the number there were before. + if (!old_idx || old_idx >= gc->old_count) return 0; + uint32_t *map = gc->remap; + if (quantize && gc->quantized_remap) map = gc->quantized_remap; + if (!map[old_idx]) { + const Style s = map == gc->remap ? gc->old_styles[old_idx] : style_quantize(gc->old_styles + old_idx, gc->quantize_bits); + map[old_idx] = style_table_intern(pub, &s) + 1; + } + return map[old_idx] - 1; +} + +void +style_table_gc_end(StyleTable *pub, StyleTableGC *gc, bool degraded) { + StyleTableImpl *self = (StyleTableImpl *)pub; + bump_generation(self); + self->misses_since_gc = 0; + self->gc_count++; + self->degraded = degraded; + self->last_gc_quantize_bits = gc->quantize_bits; + if (gc->quantize_bits && !self->lossy_reported) { + self->lossy_reported = true; + log_error("Too many distinct cell styles, reducing the color precision of cells in the scrollback"); + } + free(gc->old_styles); + free(gc->remap); + free(gc->quantized_remap); + free(gc); +} + +unsigned +style_table_last_gc_quantize_bits(const StyleTable *pub) { + return ((const StyleTableImpl *)pub)->last_gc_quantize_bits; +} + +uint64_t +style_table_gc_count(const StyleTable *pub) { + return ((const StyleTableImpl *)pub)->gc_count; +} + +bool +style_table_is_degraded(const StyleTable *pub) { + return ((const StyleTableImpl *)pub)->degraded; +} +// }}} diff --git a/kitty/style-table.h b/kitty/style-table.h new file mode 100644 index 000000000..755f52609 --- /dev/null +++ b/kitty/style-table.h @@ -0,0 +1,69 @@ +/* + * style-table.h + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#pragma once + +#include "data-types.h" + +// The formatting (Style) of cells is interned per screen in a StyleTable and +// cells store only the index of their style. Index zero is always the default +// (all zero) style, so zeroed cells have the default style. +#define STYLE_IDX_BITS 17u +#define MAX_NUM_OF_STYLES (1u << STYLE_IDX_BITS) + +typedef struct StyleTable { + Style *styles; + uint32_t count; + // Incremented whenever existing indices are invalidated, never zero + uint32_t generation; +} StyleTable; + +StyleTable *style_table_alloc(void); +void style_table_free(StyleTable *self); +uint32_t style_table_intern(StyleTable *self, const Style *style); +// For testing +void style_table_set_max_count(StyleTable *self, uint32_t max_count); +uint32_t style_table_max_count(const StyleTable *self); +uint64_t style_table_gc_count(const StyleTable *self); +bool style_table_is_degraded(const StyleTable *self); + +// Reduce the precision of RGB colors in style to bits per channel +Style style_quantize(const Style *style, unsigned bits); + +// Garbage collection. When the table is full, style_table_intern() returns +// the index of an existing style close to the requested one. The owner of the +// table must then, at a time when no style indices are held outside of cells, +// call style_table_gc_begin(), style_table_gc_map() with the style index of +// every live cell, replacing it with the returned index, and +// style_table_gc_end(). With quantize_bits, cells for which quantize is true +// get styles with colors of reduced precision, to free up space. +bool style_table_needs_gc(const StyleTable *self); +unsigned style_table_last_gc_quantize_bits(const StyleTable *self); +typedef struct StyleTableGC StyleTableGC; +StyleTableGC *style_table_gc_begin(StyleTable *self, unsigned quantize_bits); +uint32_t style_table_gc_map(StyleTable *self, StyleTableGC *gc, uint32_t old_idx, bool quantize); +void style_table_gc_end(StyleTable *self, StyleTableGC *gc, bool degraded); + +static inline const Style * +style_table_get(const StyleTable *self, uint32_t idx) { + return self->styles + (idx < self->count ? idx : 0); +} + +static inline bool +style_eq(const Style *a, const Style *b) { + return a->fg == b->fg && a->bg == b->bg && a->decoration_fg == b->decoration_fg && a->attrs.val == b->attrs.val; +} + +// Intern style, using *idx as a cache of the index of the last style interned +// with it, to avoid hash lookups when the style is unchanged. The cache needs +// no invalidation: if the style at *idx equals style, *idx is a valid index +// for style, even after the table is compacted. +static inline uint32_t +style_table_intern_cached(StyleTable *self, const Style *style, uint32_t *idx) { + if (*idx >= self->count || !style_eq(style, self->styles + *idx)) *idx = style_table_intern(self, style); + return *idx; +} diff --git a/kitty/text-cache.c b/kitty/text-cache.c index 177e961fe..fd7a245c3 100644 --- a/kitty/text-cache.c +++ b/kitty/text-cache.c @@ -6,6 +6,7 @@ */ #include "data-types.h" +#include "style-table.h" typedef struct Chars { const char_type *chars; size_t count; @@ -36,6 +37,7 @@ typedef struct TextCache { unsigned refcnt; CharsMonotonicArena arena; unsigned adds_since_last_gc, adds_between_gcs; + StyleTable *style_table; } TextCache; static uint64_t hash_chars(Chars k) { @@ -60,6 +62,12 @@ tc_alloc(void) { ans = NULL; return ans; } + ans->style_table = style_table_alloc(); + if (!ans->style_table) { + free(ans->array.items); + free(ans); + return NULL; + } vt_init(&ans->map); ans->refcnt = 1; return ans; @@ -70,9 +78,15 @@ free_text_cache(TextCache *self) { vt_cleanup(&self->map); Chars_free_all(&self->arena); free(self->array.items); + style_table_free(self->style_table); free(self); } +StyleTable * +tc_style_table(const TextCache *self) { + return self->style_table; +} + TextCache * tc_incref(TextCache *self) { if (self) { self->refcnt++; } diff --git a/kitty/text-cache.h b/kitty/text-cache.h index 04d0431e3..83c382424 100644 --- a/kitty/text-cache.h +++ b/kitty/text-cache.h @@ -57,6 +57,8 @@ typedef struct { #endif TextCache *tc_alloc(void); +// The style table of the screen the text cache belongs to +struct StyleTable *tc_style_table(const TextCache *self); TextCache *tc_incref(TextCache *self); TextCache *tc_decref(TextCache *self); void tc_chars_at_index(const TextCache *self, char_type idx, ListOfChars *ans); diff --git a/kitty_tests/base.py b/kitty_tests/base.py index 0f284ee7a..a585a7588 100644 --- a/kitty_tests/base.py +++ b/kitty_tests/base.py @@ -299,6 +299,13 @@ class BaseTest(TestCase): print('Failed to delete the directory:', tdir, 'with error:', err, file=sys.stderr) def tearDown(self): + # Check that every cell of every screen created by the test has a + # valid index into the style table of the screen + screens, self._screens_to_verify_styles = getattr(self, '_screens_to_verify_styles', []), [] + for s in screens: + mismatches = s.test_verify_styles() + if mismatches: + self.fail(f'Cell style mismatches: {mismatches}') set_options(None) def set_options(self, options=None): @@ -321,6 +328,9 @@ class BaseTest(TestCase): c = Callbacks() s = Screen(c, lines, cols, scrollback, cell_width, cell_height, 0, c) c.color_profile = s.color_profile + if not hasattr(self, '_screens_to_verify_styles'): + self._screens_to_verify_styles = [] + self._screens_to_verify_styles.append(s) return s def create_pty( diff --git a/kitty_tests/styles.py b/kitty_tests/styles.py new file mode 100644 index 000000000..c9d77035a --- /dev/null +++ b/kitty_tests/styles.py @@ -0,0 +1,326 @@ +#!/usr/bin/env python +# License: GPL v3 Copyright: 2026, Kovid Goyal + +import random + +from kitty.fast_data_types import Cursor + +from .base import BaseTest, draw_multicell, parse_bytes + +RED_BOLD = b'\x1b[1;31m' +TRUECOLOR_BG = b'\x1b[4:3;48;2;1;2;3m' +RESET = b'\x1b[m' + + +class TestStyles(BaseTest): + def assert_styles_consistent(self, s, msg=''): + mismatches = s.test_verify_styles() + if mismatches: + self.fail(f'{msg} Cell style mismatches: {mismatches}') + + def test_style_interning(self): + s = self.create_screen(cols=10, lines=3) + self.ae(s.style_table_info()['count'], 1) + s.draw('ab') + self.ae(s.cell_style_idx(0, 0), 0) + parse_bytes(s, RED_BOLD) + s.draw('cd') + red = s.cell_style_idx(0, 2) + self.assertNotEqual(red, 0) + self.ae(s.cell_style_idx(0, 3), red) + parse_bytes(s, TRUECOLOR_BG) + s.draw('e') + tc = s.cell_style_idx(0, 4) + self.assertNotIn(tc, (0, red)) + parse_bytes(s, RESET + RED_BOLD) + s.draw('f') + self.ae(s.cell_style_idx(0, 5), red, 'the same style must be interned to the same index') + parse_bytes(s, RESET) + s.draw('g') + self.ae(s.cell_style_idx(0, 6), 0) + self.ae(s.style_table_info()['count'], 3) + # Changing the cursor via the Python API must be picked up + s.cursor.bold = True + s.draw('h') + self.assertNotIn(s.cell_style_idx(0, 7), (0, red, tc)) + s.cursor.bold = False + s.draw('i') + self.ae(s.cell_style_idx(0, 8), 0) + + def test_style_of_erased_cells(self): + s = self.create_screen(cols=6, lines=4) + s.draw('abcdef' * 4) + parse_bytes(s, b'\x1b[44m') + s.cursor_position(2, 1) + s.erase_in_line(0) + blue = s.cell_style_idx(1, 0) + self.assertNotEqual(blue, 0) + self.ae({s.cell_style_idx(1, x) for x in range(s.columns)}, {blue}) + s.cursor_position(3, 2) + s.erase_characters(2) + self.ae(s.cell_style_idx(2, 1), blue) + self.ae(s.cell_style_idx(2, 2), blue) + self.ae(s.cell_style_idx(2, 0), 0) + s.insert_characters(1) + self.ae(s.cell_style_idx(2, 1), blue) + s.delete_characters(1) + self.ae(s.cell_style_idx(2, s.columns - 1), blue) + s.erase_in_display(2) + self.ae({s.cell_style_idx(y, x) for y in range(s.lines) for x in range(s.columns)}, {blue}) + self.assert_styles_consistent(s) + + def test_style_of_widened_cell(self): + s = self.create_screen(cols=6, lines=2) + parse_bytes(s, RED_BOLD) + s.draw('❤') # a narrow char that becomes wide with VS16 + parse_bytes(s, RESET) + s.draw('️') + self.ae(s.cell_style_idx(0, 0), s.cell_style_idx(0, 1)) + self.assertNotEqual(s.cell_style_idx(0, 0), 0) + self.assert_styles_consistent(s) + + def test_style_after_resize(self): + for cols, lines in ((4, 3), (12, 8), (9, 2), (3, 6)): + with self.subTest(cols=cols, lines=lines): + s = self.create_screen(cols=8, lines=4, scrollback=10) + for i, prefix in enumerate((RED_BOLD, TRUECOLOR_BG, b'\x1b[44m', RESET)): + parse_bytes(s, prefix) + s.draw(f'{i}ab日cde') + s.carriage_return(), s.linefeed() + parse_bytes(s, b'\x1b[45m') + s.erase_in_line(0) + s.resize(lines, cols) + self.assert_styles_consistent(s) + + def test_style_with_deccara(self): + s = self.create_screen(cols=6, lines=3) + s.draw('abcdef' * 3) + parse_bytes(s, b'\x1b[2;2;3;4;1;31$r') + self.ae(s.cell_style_idx(0, 0), 0) + styled = s.cell_style_idx(1, 1) + self.assertNotEqual(styled, 0) + self.ae(s.line(1).cursor_from(1).bold, True) + parse_bytes(s, b'\x1b[2;2;3;4;22$r') + self.ae(s.line(1).cursor_from(1).bold, False) + self.assert_styles_consistent(s) + + def test_style_with_line_apis(self): + s = self.create_screen(cols=6, lines=3) + s.draw('abcdef') + line = s.line(0) + c = Cursor() + c.bold = True + c.fg = (2 << 8) | 1 + line.apply_cursor(c, 1, 2) + self.ae(line.cursor_from(1).bold, True) + self.ae(line.cursor_from(2).fg, (2 << 8) | 1) + self.ae(line.cursor_from(3).bold, False) + line.apply_cursor(c, 4, 1, True) + self.ae(str(line)[4], ' ') + self.ae(line.cursor_from(4).bold, True) + line.set_char(0, 'x', 1, c) + self.ae(line.cursor_from(0).bold, True) + line.set_attribute('italic', 1) + self.ae({line.cursor_from(x).italic for x in range(s.columns)}, {True}) + line.clear_text(0, s.columns) + self.ae(line.cursor_from(0).bold, True, 'clear_text must preserve formatting') + line = s.line(1) + c.x = 0 + line.set_text('hello', 0, 5, c) + self.ae(line.cursor_from(4).fg, (2 << 8) | 1) + self.assert_styles_consistent(s) + + def test_style_of_multicell(self): + s = self.create_screen(cols=8, lines=4) + parse_bytes(s, TRUECOLOR_BG) + draw_multicell(s, 'A', scale=2) + draw_multicell(s, 'B', width=2) + idx = s.cell_style_idx(0, 0) + self.assertNotEqual(idx, 0) + self.ae({s.cell_style_idx(y, x) for y in range(2) for x in range(2)}, {idx}) + self.ae({s.cell_style_idx(0, x) for x in range(2, 4)}, {idx}) + self.assert_styles_consistent(s) + + def all_cell_styles(self, s): + # The style of every cell in history and on screen, by value + ans = [] + for y in range(s.historybuf.count - 1, -1, -1): + line = s.historybuf.line(y) + ans.append(tuple(self.style_of(line.cursor_from(x)) for x in range(s.columns))) + for y in range(s.lines): + line = s.line(y) + ans.append(tuple(self.style_of(line.cursor_from(x)) for x in range(s.columns))) + return ans + + def style_of(self, c): + return (c.fg, c.bg, c.decoration_fg, c.bold, c.italic, c.reverse, c.strikethrough, c.dim, c.decoration) + + def test_style_lossless_gc(self): + s = self.create_screen(cols=6, lines=3, scrollback=4) + for i in range(12): + parse_bytes(s, f'\x1b[38;5;{i + 1};4{i % 8}m'.encode()) + s.draw('ab') + if i % 3 == 2: + s.carriage_return(), s.linefeed() + + def ansi(): + return [s.historybuf.line(y).as_ansi() for y in range(s.historybuf.count)] + [s.line(y).as_ansi() for y in range(s.lines)] + + before, ansi_before, count_before = self.all_cell_styles(s), ansi(), s.style_table_info()['count'] + s.test_gc_style_table() + self.ae(self.all_cell_styles(s), before) + self.ae(ansi(), ansi_before) + self.ae(s.style_table_info()['count'], count_before, 'all styles are live so none must be freed') + # Erase a line, freeing its styles + parse_bytes(s, RESET) + s.cursor_position(1, 1) + s.erase_in_line(2) + before = self.all_cell_styles(s) + s.test_gc_style_table() + self.ae(self.all_cell_styles(s), before) + self.ae(s.style_table_info()['count'], count_before - 3) + self.assert_styles_consistent(s) + + def test_style_gc_when_full(self): + s = self.create_screen(cols=6, lines=2, scrollback=0) + s.test_set_style_table_limit(6) + for i in range(5): + parse_bytes(s, f'\x1b[38;5;{i + 1}m'.encode()) + s.draw('x') + self.ae(s.style_table_info()['count'], 6) + parse_bytes(s, RESET) + s.erase_in_line(2) + s.cursor_position(2, 1) + # Drawing a new style when the table is full collects garbage, freeing the erased styles + for i in range(5): + parse_bytes(s, f'\x1b[38;5;{i + 100}m'.encode()) + s.draw('y') + self.ae([s.line(1).cursor_from(x).fg for x in range(5)], [((i + 100) << 8) | 1 for i in range(5)]) + self.assertGreaterEqual(s.style_table_info()['gc_count'], 1) + self.assertFalse(s.style_table_info()['degraded']) + self.assert_styles_consistent(s) + + def test_style_lossy_gc_of_scrollback(self): + s = self.create_screen(cols=4, lines=2, scrollback=50) + s.test_set_style_table_limit(32) + colors = [] + for i in range(60): + # Colors that are distinct but close, so that reducing their precision frees space + r, g, b = 100 + i, 50, 200 - i + colors.append((r << 24) | (g << 16) | (b << 8) | 2) + parse_bytes(s, f'\x1b[38;2;{r};{g};{b}m'.encode()) + s.draw(str(i % 10)) + s.carriage_return(), s.linefeed() + info = s.style_table_info() + self.assertGreaterEqual(info['gc_count'], 1) + self.assertFalse(info['degraded']) + # The cell on screen has its exact style + self.ae(s.line(0).cursor_from(0).fg, colors[-1]) + # Cells in the scrollback have their colors approximately + for y in range(s.historybuf.count): + fg = s.historybuf.line(y).cursor_from(0).fg + expected = colors[-2 - y] + self.ae(fg & 0xFF, 2, f'line {y}') + for shift in (8, 16, 24): + self.assertLessEqual(abs(((fg >> shift) & 0xFF) - ((expected >> shift) & 0xFF)), 8, f'line {y}') + self.assert_styles_consistent(s) + + def test_style_degraded(self): + s = self.create_screen(cols=24, lines=2, scrollback=0) + s.test_set_style_table_limit(16) + expected = [] + for i in range(24): + expected.append(((i * 10) << 24) | 2) + parse_bytes(s, f'\x1b[38;2;{i * 10};0;0m'.encode()) + s.draw('x') + info = s.style_table_info() + self.assertTrue(info['degraded']) + self.assertLessEqual(info['count'], 16) + # Styles that do not fit are replaced by the closest existing style or the default style + for x in range(24): + fg = s.line(0).cursor_from(x).fg + if x < 15: + self.ae(fg, expected[x]) + else: + self.assertTrue(fg == 0 or abs((fg >> 24) - (expected[x] >> 24)) < 64, f'{x}: {fg:x}') + self.assert_styles_consistent(s) + + def test_style_caches_after_gc(self): + s = self.create_screen(cols=8, lines=3, scrollback=2) + parse_bytes(s, b'\x1b[38;5;10m') + s.draw('a') + parse_bytes(s, b'\x1b7') # DECSC saves the style + parse_bytes(s, b'\x1b[38;5;20m') + s.draw('b') + parse_bytes(s, b'\x1b[38;5;30m') + s.draw('c') + s.erase_in_line(2) # free the styles, so indices change on collection + s.test_gc_style_table() + s.cursor_position(2, 1) + s.draw('d') + self.ae(s.line(1).cursor_from(0).fg, (30 << 8) | 1) + parse_bytes(s, b'\x1b8') # DECRC + s.draw('e') + self.ae(s.line(0).cursor_from(1).fg, (10 << 8) | 1) + self.assert_styles_consistent(s) + + def test_style_random_operations(self): + ops = ( + lambda s, r: parse_bytes(s, r.choice((RED_BOLD, TRUECOLOR_BG, RESET, b'\x1b[44m', b'\x1b[7m', b'\x1b[38;5;99m'))), + lambda s, r: s.draw(''.join(r.choice('ab日é́️︎❤😀 ') for _ in range(r.randint(1, 12)))), + lambda s, r: (s.carriage_return(), s.linefeed()), + lambda s, r: s.erase_in_line(r.randint(0, 2)), + lambda s, r: s.erase_in_display(r.randint(0, 2)), + lambda s, r: s.erase_characters(r.randint(1, 3)), + lambda s, r: s.insert_characters(r.randint(1, 3)), + lambda s, r: s.delete_characters(r.randint(1, 3)), + lambda s, r: s.cursor_position(r.randint(1, 5), r.randint(1, 8)), + lambda s, r: parse_bytes(s, f'\x1b[{r.randint(1, 3)};{r.randint(1, 4)};{r.randint(2, 5)};{r.randint(3, 8)};1;32$r'.encode()), + lambda s, r: parse_bytes(s, r.choice((b'\x1b[4h', b'\x1b[4l', b'\x1b[2b', b'\x1b[?1049h', b'\x1b[?1049l', b'\x1b[L', b'\x1b[M'))), + lambda s, r: draw_multicell(s, r.choice('A日'), scale=r.randint(1, 2)), + lambda s, r: s.resize(r.randint(2, 6), r.randint(3, 10)), + lambda s, r: s.test_gc_style_table(r.choice((0, 0, 4, 2))), + lambda s, r: parse_bytes(s, f'\x1b[38;2;{r.randint(0, 255)};{r.randint(0, 255)};{r.randint(0, 255)}m'.encode()), + ) + for seed in range(60): + r = random.Random(seed) + s = self.create_screen(cols=8, lines=5, scrollback=8) + if seed % 2: + s.test_set_style_table_limit(r.randint(3, 12)) + for i in range(80): + op = r.choice(ops) + op(s, r) + if i % 10 == 0: + self.assert_styles_consistent(s, f'seed={seed} op={i}') + self.assert_styles_consistent(s, f'seed={seed}') + + def test_style_copies_between_style_tables(self): + from kitty.fast_data_types import HistoryBuf, LineBuf + + def styled(bold=False, fg=0): + c = Cursor() + c.bold = bold + c.fg = fg + return c + + src, dest = LineBuf(2, 4), LineBuf(2, 4) + # Make the style indices of the two tables differ + dest.line(0).apply_cursor(styled(fg=(5 << 8) | 1), 0, 4) + red_bold = styled(True, (1 << 8) | 1) + src.line(1).apply_cursor(red_bold, 0, 2) + dest.copy_old(src) + line = dest.line(1) + for x in range(2): + self.ae((line.cursor_from(x).bold, line.cursor_from(x).fg), (True, (1 << 8) | 1)) + self.ae((line.cursor_from(2).bold, line.cursor_from(2).fg), (False, 0)) + other = LineBuf(1, 4) + other.line(0).apply_cursor(styled(fg=(6 << 8) | 1), 0, 4) + src.copy_line_to(1, other.line(0)) + self.ae(other.line(0).cursor_from(0).fg, (1 << 8) | 1) + src.line(1).copy_char(1, other.line(0), 3) + self.ae(other.line(0).cursor_from(3).bold, True) + hb = HistoryBuf(3, 4) + hb.push(LineBuf(1, 4).line(0)) + hb.push(src.line(1)) + self.ae(hb.line(0).cursor_from(0).fg, (1 << 8) | 1) + self.ae(hb.line(0).cursor_from(3).fg, 0)