From 731e0856ec2c8f0b4f1ee803b07333cc7df592d9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 2 Nov 2026 20:05:14 +0530 Subject: [PATCH] Fix #10794 --- docs/changelog.rst | 3 +++ kitty/screen.c | 45 ++++++++++++++++++++++++++++--------------- kitty/screen.h | 9 ++++++--- kitty/shaders.c | 4 ++-- kitty_tests/screen.py | 19 ++++++++++++++++-- 5 files changed, 57 insertions(+), 23 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 17ed73c13..36c1553cd 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -411,6 +411,9 @@ Detailed list of changes early. :ac:`detach_tab` now shows custom titles in the OS Window chooser (:pull:`10640`) +- Fix render jitter when scrolled back with a partial line and the client is + using the synchronised rendering escape code (:iss:`10794`) + - Fix bold or italic Nerd Font icons sometimes being rendered using glyphs from an unrelated font, the cursor trail not always being shown when focus moves to a different window, :opt:`watcher` entries not being removed on diff --git a/kitty/screen.c b/kitty/screen.c index 850df9f83..86e5d09f6 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -3829,6 +3829,8 @@ copy_selections(Selections *dest, const Selections *src) { return true; } +static Line* render_line_for_virtual_y(Screen *self, int y, Line *line, index_type *lnum, bool *is_history); + bool screen_pause_rendering(Screen *self, bool pause, int for_in_ms) { if (!pause) { @@ -3856,22 +3858,30 @@ screen_pause_rendering(Screen *self, bool pause, int for_in_ms) { self->paused_rendering.scrolled_by = self->scrolled_by; self->paused_rendering.cell_data_updated = false; self->paused_rendering.cursor_visible = self->modes.mDECTCEM; + // Preserve the pixel scroll geometry so that pausing does not cause the rendered content to jump, see #10794 + self->paused_rendering.pixel_scroll = live_pixel_scroll_enabled(self); + self->paused_rendering.pixel_scroll_offset_y = self->paused_rendering.pixel_scroll ? self->pixel_scroll_offset_y : 0; memcpy(&self->paused_rendering.cursor, self->cursor, sizeof(self->paused_rendering.cursor)); memcpy(&self->paused_rendering.color_profile, self->color_profile, sizeof(self->paused_rendering.color_profile)); - if (!self->paused_rendering.linebuf || self->paused_rendering.linebuf->xnum != self->columns || self->paused_rendering.linebuf->ynum != self->lines) { + const index_type row_offset = self->paused_rendering.pixel_scroll, num_rows = self->lines + row_offset; + if (!self->paused_rendering.linebuf || self->paused_rendering.linebuf->xnum != self->columns || self->paused_rendering.linebuf->ynum != num_rows) { if (self->paused_rendering.linebuf) Py_CLEAR(self->paused_rendering.linebuf); - self->paused_rendering.linebuf = alloc_linebuf(self->lines, self->columns, self->text_cache); + self->paused_rendering.linebuf = alloc_linebuf(num_rows, self->columns, self->text_cache); if (!self->paused_rendering.linebuf) { PyErr_Clear(); self->paused_rendering.expires_at = 0; return false; } } - for (index_type y = 0; y < self->lines; y++) { - Line *src = visual_line_(self, y); - linebuf_init_line(self->paused_rendering.linebuf, y); - copy_line(src, self->paused_rendering.linebuf->line); - self->paused_rendering.linebuf->line_attrs[y] = src->attrs; + Line line = {.text_cache = self->text_cache}; + for (index_type y = 0; y < num_rows; y++) { + index_type lnum; bool is_history; + Line *src = render_line_for_virtual_y(self, (int)y - (int)row_offset, &line, &lnum, &is_history); + if (src) { + linebuf_init_line(self->paused_rendering.linebuf, y); + copy_line(src, self->paused_rendering.linebuf->line); + self->paused_rendering.linebuf->line_attrs[y] = src->attrs; + } else linebuf_clear_line(self->paused_rendering.linebuf, y, true); } copy_selections(&self->paused_rendering.selections, &self->selections); copy_selections(&self->paused_rendering.url_ranges, &self->url_ranges); @@ -4125,12 +4135,15 @@ screen_update_cell_data(Screen *self, void *address, FONTS_DATA_HANDLE fonts_dat if (self->paused_rendering.expires_at) { if (!self->paused_rendering.cell_data_updated) { LineBuf *linebuf = self->paused_rendering.linebuf; - // The GPU buffer was invalidated so every row must be written. Pixel - // scrolling is off while paused, so lines == render_lines_for_screen(). - for (index_type y = 0; y < self->lines; y++) { + // The GPU buffer was invalidated so every row must be written. The + // paused linebuf has exactly render_lines_for_screen() rows. + const index_type row_offset = self->paused_rendering.pixel_scroll; + for (index_type y = 0; y < linebuf->ynum; y++) { linebuf_init_line(linebuf, y); if (linebuf->line->attrs.has_dirty_text) { - render_line(fonts_data, linebuf->line, y, &self->paused_rendering.cursor, self->disable_ligatures, self->lc); + // The extra leading row is never the cursor row + const index_type lnum = y < row_offset ? self->lines : y - row_offset; + render_line(fonts_data, linebuf->line, lnum, &self->paused_rendering.cursor, self->disable_ligatures, self->lc); // Cell images are not rendered here: this would modify the live grman using snapshot row numbers if (linebuf->line->attrs.has_dirty_text && screen_has_marker(self)) mark_text_in_line(self->marker, linebuf->line, &self->as_ansi_buf); linebuf_mark_line_clean(linebuf, y); @@ -4369,9 +4382,9 @@ apply_selection(Screen *self, uint8_t *data, Selection *s, uint8_t set_mask, int const int y_min = MAX(-extra_leading_rows - scrolled_by, s->last_rendered.y), y_limit = MIN(s->last_rendered.y_limit, (int)self->lines - scrolled_by); for (int y = y_min; y < y_limit; y++) { if (self->paused_rendering.expires_at) { - // paused_rendering.linebuf stores only the visible rows captured at pause time - const int paused_y = y + scrolled_by; - if (paused_y < 0 || paused_y >= (int)self->lines) continue; + // paused_rendering.linebuf stores only the rendered rows captured at pause time + const int paused_y = y + scrolled_by + extra_leading_rows; + if (paused_y < 0 || paused_y >= (int)self->paused_rendering.linebuf->ynum) continue; linebuf_init_line(self->paused_rendering.linebuf, paused_y); line = self->paused_rendering.linebuf->line; } else { @@ -4416,7 +4429,7 @@ screen_is_cell_selected(Screen *self, index_type x, index_type y) { // Every cell of a multi-cell character has the same selection highlight. const int first_row = cell.is_multicell ? row - cell.y : row; const int last_row = cell.is_multicell ? first_row + cell.scale : row + 1; - const int visible_start = -(int)self->scrolled_by - pixel_scroll_enabled(self); + const int visible_start = -(int)self->scrolled_by - live_pixel_scroll_enabled(self); const int visible_end = (int)self->lines - self->scrolled_by; for (size_t i = 0; i < self->selections.count; i++) { IterationData idata; @@ -6098,7 +6111,7 @@ scroll_by_pixels(Screen *self, double delta_pixels) { bool screen_apply_pixel_scroll(Screen *self, double delta_pixels) { - if (!pixel_scroll_enabled(self)) return false; + if (self->paused_rendering.expires_at || !live_pixel_scroll_enabled(self)) return false; if (!self->historybuf->count) return false; return scroll_by_pixels(self, delta_pixels); } diff --git a/kitty/screen.h b/kitty/screen.h index ae39cb111..afb542d79 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -211,8 +211,8 @@ typedef struct { monotonic_t expires_at; Cursor cursor; ColorProfile color_profile; - bool inverted, cell_data_updated, cursor_visible; - unsigned int scrolled_by; + bool inverted, cell_data_updated, cursor_visible, pixel_scroll; + unsigned int scrolled_by, pixel_scroll_offset_y; LineBuf *linebuf; GraphicsManager *grman; Selections selections, url_ranges; @@ -237,7 +237,10 @@ typedef struct { } dnd_chunking; } Screen; -#define pixel_scroll_enabled(screen) (OPT(pixel_scroll) && !screen->paused_rendering.expires_at && screen->linebuf == screen->main_linebuf) +#define live_pixel_scroll_enabled(screen) (OPT(pixel_scroll) && (screen)->linebuf == (screen)->main_linebuf) +// While rendering is paused the pixel scroll state captured at pause time is used so that pausing does not change what is rendered +#define pixel_scroll_enabled(screen) ((screen)->paused_rendering.expires_at ? (screen)->paused_rendering.pixel_scroll : live_pixel_scroll_enabled(screen)) +#define rendered_pixel_scroll_offset_y(screen) ((screen)->paused_rendering.expires_at ? (screen)->paused_rendering.pixel_scroll_offset_y : (screen)->pixel_scroll_offset_y) #define render_lines_for_screen(screen) (screen->lines + pixel_scroll_enabled(screen)) void screen_align(Screen *); diff --git a/kitty/shaders.c b/kitty/shaders.c index 8a3d7cb9a..133e326b0 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -60,13 +60,13 @@ typedef struct UIRenderData { static inline float row_offset_for_screen(const Screen *screen) { if (!pixel_scroll_enabled(screen) || !screen->cell_size.height) return 0.f; - return -1.f + (float)(screen->pixel_scroll_offset_y / (double)screen->cell_size.height); + return -1.f + (float)(rendered_pixel_scroll_offset_y(screen) / (double)screen->cell_size.height); } static inline float scroll_offset_lines_for_screen(const Screen *screen) { if (!pixel_scroll_enabled(screen) || !screen->cell_size.height) return 0.f; - return (float)(screen->pixel_scroll_offset_y / (double)screen->cell_size.height); + return (float)(rendered_pixel_scroll_offset_y(screen) / (double)screen->cell_size.height); } // Sprites {{{ diff --git a/kitty_tests/screen.py b/kitty_tests/screen.py index 40815592c..641d7513a 100644 --- a/kitty_tests/screen.py +++ b/kitty_tests/screen.py @@ -386,6 +386,20 @@ class TestScreen(BaseTest): s.scroll_to_absolute(float('inf')) self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (s.historybuf.count, 0)) + # Pausing rendering must not change the pixel scrolled geometry, + # otherwise programs using synchronized output cause jitter when the + # user is pixel scrolled into the scrollback, see #10794 + s.scroll_to_absolute(2.5) + self.ae((s.scrolled_by, s.pixel_scroll_offset_y), (2, 10)) + s.start_selection(0, 0) + s.update_selection(2, 1) + before = s.current_selections() + self.ae(len(before), (s.lines + 1) * s.columns) + self.assertTrue(s.pause_rendering(True, 5000)) + self.ae(s.current_selections(), before) + self.assertTrue(s.pause_rendering(False)) + self.ae(s.current_selections(), before) + def test_emoji_skin_tone_modifiers(self): s = self.create_screen() q = chr(0x1F469) + chr(0x1F3FD) @@ -1222,10 +1236,11 @@ class TestScreen(BaseTest): s.start_selection(0, 0) s.update_selection(2, 1) self.assertTrue(s.has_selection()) + before = s.current_selections() self.assertTrue(s.pause_rendering(True, 5000)) - # Must not crash and must return the visible-area buffer. + # Must not crash and must return the same render buffer as before pausing result = s.current_selections() - self.ae(len(result), s.lines * s.columns) + self.ae(result, before) # The visible portion of the selection must have at least one byte # marked (set_mask = 1 for plain selections). self.assertIn(1, result)