Codebergdnkl
foot
master
Showing 4000 of 5479 lines.
1#include "render.h"
3#include <limits.h>
4#include <signal.h>
5#include <string.h>
6#include <unistd.h>
8#include <sys/ioctl.h>
9#include <sys/time.h>
10#include <sys/timerfd.h>
11#include <sys/epoll.h>
12#include <pthread.h>
14#include "macros.h"
15#if HAS_INCLUDE(<pthread_np.h>)
16 #include <pthread_np.h>
17 #define pthread_setname_np(thread, name) (pthread_set_name_np(thread, name), 0)
18#elif defined(__NetBSD__)
19 #define pthread_setname_np(thread, name) pthread_setname_np(thread, "%s", (void *)name)
20#endif
22#include <presentation-time.h>
23#include <wayland-cursor.h>
24#include <xdg-shell.h>
25#include <xdg-toplevel-icon-v1.h>
27#include <fcft/fcft.h>
29#define LOG_MODULE "render"
30#define LOG_ENABLE_DBG 0
31#include "log.h"
32#include "box-drawing.h"
33#include "char32.h"
34#include "config.h"
35#include "cursor-shape.h"
36#include "grid.h"
37#include "ime.h"
38#include "input.h"
39#include "quirks.h"
40#include "search.h"
41#include "selection.h"
42#include "shm.h"
43#include "sixel.h"
44#include "srgb.h"
45#include "url-mode.h"
46#include "util.h"
47#include "xmalloc.h"
49#define TIME_SCROLL_DAMAGE 0
51struct renderer {
52 struct fdm *fdm;
53 struct wayland *wayl;
54};
56static struct {
57 size_t total;
58 size_t zero; /* commits presented in less than one frame interval */
59 size_t one; /* commits presented in one frame interval */
60 size_t two; /* commits presented in two or more frame intervals */
61} presentation_statistics = {0};
63static void fdm_hook_refresh_pending_terminals(struct fdm *fdm, void *data);
65struct renderer *
66render_init(struct fdm *fdm, struct wayland *wayl)
67{
68 struct renderer *renderer = malloc(sizeof(*renderer));
69 if (unlikely(renderer == NULL)) {
70 LOG_ERRNO("malloc() failed");
71 return NULL;
72 }
74 *renderer = (struct renderer) {
75 .fdm = fdm,
76 .wayl = wayl,
77 };
79 if (!fdm_hook_add(fdm, &fdm_hook_refresh_pending_terminals, renderer,
80 FDM_HOOK_PRIORITY_NORMAL))
81 {
82 LOG_ERR("failed to register FDM hook");
83 free(renderer);
84 return NULL;
85 }
87 return renderer;
88}
90void
91render_destroy(struct renderer *renderer)
92{
93 if (renderer == NULL)
94 return;
96 fdm_hook_del(renderer->fdm, &fdm_hook_refresh_pending_terminals,
97 FDM_HOOK_PRIORITY_NORMAL);
99 free(renderer);
100}
102static void DESTRUCTOR
103log_presentation_statistics(void)
104{
105 if (presentation_statistics.total == 0)
106 return;
108 const size_t total = presentation_statistics.total;
109 LOG_INFO("presentation statistics: zero=%f%%, one=%f%%, two=%f%%",
110 100. * presentation_statistics.zero / total,
111 100. * presentation_statistics.one / total,
112 100. * presentation_statistics.two / total);
113}
115static void
116sync_output(void *data,
117 struct wp_presentation_feedback *wp_presentation_feedback,
118 struct wl_output *output)
119{
120}
122struct presentation_context {
123 struct terminal *term;
124 struct timeval input;
125 struct timeval commit;
126};
128static void
129presented(void *data,
130 struct wp_presentation_feedback *wp_presentation_feedback,
131 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
132 uint32_t refresh, uint32_t seq_hi, uint32_t seq_lo, uint32_t flags)
133{
134 struct presentation_context *ctx = data;
135 struct terminal *term = ctx->term;
136 const struct timeval *input = &ctx->input;
137 const struct timeval *commit = &ctx->commit;
139 const struct timeval presented = {
140 .tv_sec = (uint64_t)tv_sec_hi << 32 | tv_sec_lo,
141 .tv_usec = tv_nsec / 1000,
142 };
144 bool use_input = (input->tv_sec > 0 || input->tv_usec > 0) &&
145 timercmp(&presented, input, >);
146 char msg[1024];
147 int chars = 0;
149 if (use_input && timercmp(&presented, input, <))
150 return;
151 else if (timercmp(&presented, commit, <))
152 return;
154 LOG_DBG("commit: %lu s %lu µs, presented: %lu s %lu µs",
155 commit->tv_sec, commit->tv_usec, presented.tv_sec, presented.tv_usec);
157 if (use_input) {
158 struct timeval diff;
159 timersub(commit, input, &diff);
160 chars += snprintf(
161 &msg[chars], sizeof(msg) - chars,
162 "input - %llu µs -> ", (unsigned long long)diff.tv_usec);
163 }
165 struct timeval diff;
166 timersub(&presented, commit, &diff);
167 chars += snprintf(
168 &msg[chars], sizeof(msg) - chars,
169 "commit - %llu µs -> ", (unsigned long long)diff.tv_usec);
171 if (use_input) {
172 xassert(timercmp(&presented, input, >));
173 timersub(&presented, input, &diff);
174 } else {
175 xassert(timercmp(&presented, commit, >));
176 timersub(&presented, commit, &diff);
177 }
179 chars += snprintf(
180 &msg[chars], sizeof(msg) - chars,
181 "presented (total: %llu µs)", (unsigned long long)diff.tv_usec);
183 unsigned frame_count = 0;
184 if (tll_length(term->window->on_outputs) > 0) {
185 const struct monitor *mon = tll_front(term->window->on_outputs);
186 frame_count = (diff.tv_sec * 1000000. + diff.tv_usec) / (1000000. / mon->refresh);
187 }
189 presentation_statistics.total++;
190 if (frame_count >= 2)
191 presentation_statistics.two++;
192 else if (frame_count >= 1)
193 presentation_statistics.one++;
194 else
195 presentation_statistics.zero++;
197#define _log_fmt "%s (more than %u frames)"
199 if (frame_count >= 2)
200 LOG_ERR(_log_fmt, msg, frame_count);
201 else if (frame_count >= 1)
202 LOG_WARN(_log_fmt, msg, frame_count);
203 else
204 LOG_INFO(_log_fmt, msg, frame_count);
206#undef _log_fmt
208 wp_presentation_feedback_destroy(wp_presentation_feedback);
209 free(ctx);
210}
212static void
213discarded(void *data, struct wp_presentation_feedback *wp_presentation_feedback)
214{
215 struct presentation_context *ctx = data;
216 wp_presentation_feedback_destroy(wp_presentation_feedback);
217 free(ctx);
218}
220static const struct wp_presentation_feedback_listener presentation_feedback_listener = {
221 .sync_output = &sync_output,
222 .presented = &presented,
223 .discarded = &discarded,
224};
226static struct fcft_font *
227attrs_to_font(const struct terminal *term, const struct attributes *attrs)
228{
229 int idx = attrs->italic << 1 | attrs->bold;
230 return term->fonts[idx];
231}
233static pixman_color_t
234color_hex_to_pixman_srgb(uint32_t color, uint16_t alpha)
235{
236 return (pixman_color_t){
237 .alpha = alpha, /* Consider alpha linear already? */
238 .red = srgb_decode_8_to_16((color >> 16) & 0xff),
239 .green = srgb_decode_8_to_16((color >> 8) & 0xff),
240 .blue = srgb_decode_8_to_16((color >> 0) & 0xff),
241 };
242}
244static inline pixman_color_t
245color_hex_to_pixman_with_alpha(uint32_t color, uint16_t alpha, bool srgb)
246{
247 pixman_color_t ret;
249 if (srgb)
250 ret = color_hex_to_pixman_srgb(color, alpha);
251 else {
252 ret = (pixman_color_t){
253 .red = ((color >> 16 & 0xff) | (color >> 8 & 0xff00)),
254 .green = ((color >> 8 & 0xff) | (color >> 0 & 0xff00)),
255 .blue = ((color >> 0 & 0xff) | (color << 8 & 0xff00)),
256 .alpha = alpha,
257 };
258 }
260 ret.red = (uint32_t)ret.red * alpha / 0xffff;
261 ret.green = (uint32_t)ret.green * alpha / 0xffff;
262 ret.blue = (uint32_t)ret.blue * alpha / 0xffff;
264 return ret;
265}
267static inline pixman_color_t
268color_hex_to_pixman(uint32_t color, bool srgb)
269{
270 /* Count on the compiler optimizing this */
271 return color_hex_to_pixman_with_alpha(color, 0xffff, srgb);
272}
274static inline int i_lerp(int from, int to, float t) {
275 return from + (to - from) * t;
276}
278static inline uint32_t
279color_blend_towards(uint32_t from, uint32_t to, float amount)
280{
281 if (unlikely(amount == 0))
282 return from;
283 float t = 1 - 1/amount;
285 uint32_t alpha = from & 0xff000000;
286 uint8_t r = i_lerp((from>>16)&0xff, (to>>16)&0xff, t);
287 uint8_t g = i_lerp((from>>8)&0xff, (to>>8)&0xff, t);
288 uint8_t b = i_lerp((from>>0)&0xff, (to>>0)&0xff, t);
290 return alpha | (r<<16) | (g<<8) | (b<<0);
291}
293static inline uint32_t
294color_dim(const struct terminal *term, uint32_t color)
295{
296 const struct config *conf = term->conf;
297 const uint8_t custom_dim = conf->colors_dark.use_custom.dim;
299 if (unlikely(custom_dim != 0)) {
300 for (size_t i = 0; i < 8; i++) {
301 if (((custom_dim >> i) & 1) == 0)
302 continue;
304 if (term->colors.table[0 + i] == color) {
305 /* "Regular" color, return the corresponding "dim" */
306 return conf->colors_dark.dim[i];
307 }
309 else if (term->colors.table[8 + i] == color) {
310 /* "Bright" color, return the corresponding "regular" */
311 return term->colors.table[i];
312 }
313 }
314 }
316 const struct color_theme *theme = term_theme_get(term);
318 return color_blend_towards(
319 color,
320 theme->dim_blend_towards == DIM_BLEND_TOWARDS_BLACK ? 0x00000000 : 0x00ffffff,
321 conf->dim.amount);
322}
324static inline uint32_t
325color_brighten(const struct terminal *term, uint32_t color)
326{
327 /*
328 * First try to match the color against the base 8 colors. If we
329 * find a match, return the corresponding bright color.
330 */
331 if (term->conf->bold_in_bright.palette_based) {
332 for (size_t i = 0; i < 8; i++) {
333 if (term->colors.table[i] == color)
334 return term->colors.table[i + 8];
335 }
336 return color;
337 }
339 return color_blend_towards(color, 0x00ffffff, term->conf->bold_in_bright.amount);
340}
342static void
343draw_hollow_block(const struct terminal *term, pixman_image_t *pix,
344 const pixman_color_t *color, int x, int y, int cell_cols)
345{
346 const int scale = (int)roundf(term->scale);
347 const int width = min(min(scale, term->cell_width), term->cell_height);
349 pixman_image_fill_rectangles(
350 PIXMAN_OP_SRC, pix, color, 4,
351 (pixman_rectangle16_t []){
352 {x, y, cell_cols * term->cell_width, width}, /* top */
353 {x, y, width, term->cell_height}, /* left */
354 {x + cell_cols * term->cell_width - width, y, width, term->cell_height}, /* right */
355 {x, y + term->cell_height - width, cell_cols * term->cell_width, width}, /* bottom */
356 });
357}
359static void
360draw_beam_cursor(const struct terminal *term, pixman_image_t *pix,
361 const struct fcft_font *font,
362 const pixman_color_t *color, int x, int y)
363{
364 int baseline = y + term->font_baseline - term->fonts[0]->ascent;
365 pixman_image_fill_rectangles(
366 PIXMAN_OP_SRC, pix, color,
367 1, &(pixman_rectangle16_t){
368 x, baseline,
369 term_pt_or_px_as_pixels(term, &term->conf->cursor.beam_thickness),
370 term->fonts[0]->ascent + term->fonts[0]->descent});
371}
373static int
374underline_offset(const struct terminal *term, const struct fcft_font *font)
375{
376 return term->font_baseline -
377 (term->conf->use_custom_underline_offset
378 ? -term_pt_or_px_as_pixels(term, &term->conf->underline_offset)
379 : font->underline.position);
380}
382static void
383draw_underline_cursor(const struct terminal *term, pixman_image_t *pix,
384 const struct fcft_font *font,
385 const pixman_color_t *color, int x, int y, int cols)
386{
387 int thickness = term->conf->cursor.underline_thickness.px >= 0
388 ? term_pt_or_px_as_pixels(
389 term, &term->conf->cursor.underline_thickness)
390 : font->underline.thickness;
392 /* Make sure the line isn't positioned below the cell */
393 const int y_ofs = min(underline_offset(term, font) + thickness,
394 term->cell_height - thickness);
396 pixman_image_fill_rectangles(
397 PIXMAN_OP_SRC, pix, color,
398 1, &(pixman_rectangle16_t){
399 x, y + y_ofs, cols * term->cell_width, thickness});
400}
402static void
403draw_underline(const struct terminal *term, pixman_image_t *pix,
404 const struct fcft_font *font,
405 const pixman_color_t *color, int x, int y, int cols)
406{
407 const int thickness = term->conf->underline_thickness.px >= 0
408 ? term_pt_or_px_as_pixels(
409 term, &term->conf->underline_thickness)
410 : font->underline.thickness;
412 /* Make sure the line isn't positioned below the cell */
413 const int y_ofs = min(underline_offset(term, font),
414 term->cell_height - thickness);
416 pixman_image_fill_rectangles(
417 PIXMAN_OP_SRC, pix, color,
418 1, &(pixman_rectangle16_t){
419 x, y + y_ofs, cols * term->cell_width, thickness});
420}
422static void
423draw_styled_underline(const struct terminal *term, pixman_image_t *pix,
424 const struct fcft_font *font,
425 const pixman_color_t *color,
426 enum underline_style style, int x, int y, int cols)
427{
428 xassert(style != UNDERLINE_NONE);
430 if (style == UNDERLINE_SINGLE) {
431 draw_underline(term, pix, font, color, x, y, cols);
432 return;
433 }
435 const int thickness = term->conf->underline_thickness.px >= 0
436 ? term_pt_or_px_as_pixels(
437 term, &term->conf->underline_thickness)
438 : font->underline.thickness;
440 int y_ofs;
442 /* Make sure the line isn't positioned below the cell */
443 switch (style) {
444 case UNDERLINE_DOUBLE:
445 case UNDERLINE_CURLY:
446 y_ofs = min(underline_offset(term, font),
447 term->cell_height - thickness * 3);
448 break;
450 case UNDERLINE_DASHED:
451 case UNDERLINE_DOTTED:
452 y_ofs = min(underline_offset(term, font),
453 term->cell_height - thickness);
454 break;
456 case UNDERLINE_NONE:
457 case UNDERLINE_SINGLE:
458 default:
459 BUG("unexpected underline style: %d", (int)style);
460 return;
461 }
463 const int ceil_w = cols * term->cell_width;
465 switch (style) {
466 case UNDERLINE_DOUBLE: {
467 const pixman_rectangle16_t rects[] = {
468 {x, y + y_ofs, ceil_w, thickness},
469 {x, y + y_ofs + thickness * 2, ceil_w, thickness}};
470 pixman_image_fill_rectangles(PIXMAN_OP_SRC, pix, color, 2, rects);
471 break;
472 }
474 case UNDERLINE_DASHED: {
475 const int ceil_w = cols * term->cell_width;
476 const int dash_w = ceil_w / 3 + (ceil_w % 3 > 0);
477 const pixman_rectangle16_t rects[] = {
478 {x, y + y_ofs, dash_w, thickness},
479 {x + dash_w * 2, y + y_ofs, dash_w, thickness},
480 };
481 pixman_image_fill_rectangles(
482 PIXMAN_OP_SRC, pix, color, 2, rects);
483 break;
484 }
486 case UNDERLINE_DOTTED: {
487 /* Number of dots per cell */
488 int per_cell = (term->cell_width / thickness) / 2;
489 if (per_cell == 0)
490 per_cell = 1;
492 xassert(per_cell >= 1);
494 /* Spacing between dots; start with the same width as the dots
495 themselves, then widen them if necessary, to consume unused
496 pixels */
497 int spacing[per_cell];
498 for (int i = 0; i < per_cell; i++)
499 spacing[i] = thickness;
501 /* Pixels remaining at the end of the cell */
502 int remaining = term->cell_width - (per_cell * 2) * thickness;
504 /* Spread out the left-over pixels across the spacing between
505 the dots */
506 for (int i = 0; remaining > 0; i = (i + 1) % per_cell, remaining--)
507 spacing[i]++;
509 xassert(remaining <= 0);
511 pixman_rectangle16_t rects[per_cell];
512 int dot_x = x;
513 for (int i = 0; i < per_cell; i++) {
514 rects[i] = (pixman_rectangle16_t){
515 dot_x, y + y_ofs, thickness, thickness
516 };
518 dot_x += thickness + spacing[i];
519 }
521 pixman_image_fill_rectangles(PIXMAN_OP_SRC, pix, color, per_cell, rects);
522 break;
523 }
525 case UNDERLINE_CURLY: {
526 const int top = y + y_ofs;
527 const int bot = top + thickness * 3;
528 const int half_x = x + ceil_w / 2.0, full_x = x + ceil_w;
530 const double bt_2 = (bot - top) * (bot - top);
531 const double th_2 = thickness * thickness;
532 const double hx_2 = ceil_w * ceil_w / 4.0;
533 const int th = round(sqrt(th_2 + (th_2 * bt_2 / hx_2)) / 2.);
535 #define I(x) pixman_int_to_fixed(x)
536 const pixman_trapezoid_t traps[] = {
537#if 0 /* characters sit within the "dips" of the curlies */
538 {
539 I(top), I(bot),
540 {{I(x), I(top + th)}, {I(half_x), I(bot + th)}},
541 {{I(x), I(top - th)}, {I(half_x), I(bot - th)}},
542 },
543 {
544 I(top), I(bot),
545 {{I(half_x), I(bot - th)}, {I(full_x), I(top - th)}},
546 {{I(half_x), I(bot + th)}, {I(full_x), I(top + th)}},
547 }
548#else /* characters sit on top of the curlies */
549 {
550 I(top), I(bot),
551 {{I(x), I(bot - th)}, {I(half_x), I(top - th)}},
552 {{I(x), I(bot + th)}, {I(half_x), I(top + th)}},
553 },
554 {
555 I(top), I(bot),
556 {{I(half_x), I(top + th)}, {I(full_x), I(bot + th)}},
557 {{I(half_x), I(top - th)}, {I(full_x), I(bot - th)}},
558 }
559#endif
560 };
562 pixman_image_t *fill = pixman_image_create_solid_fill(color);
563 pixman_composite_trapezoids(
564 PIXMAN_OP_OVER, fill, pix, PIXMAN_a8, 0, 0, 0, 0,
565 sizeof(traps) / sizeof(traps[0]), traps);
567 pixman_image_unref(fill);
568 break;
569 }
571 case UNDERLINE_NONE:
572 case UNDERLINE_SINGLE:
573 BUG("underline styles not supposed to be handled here");
574 break;
575 }
576}
578static void
579draw_strikeout(const struct terminal *term, pixman_image_t *pix,
580 const struct fcft_font *font,
581 const pixman_color_t *color, int x, int y, int cols)
582{
583 const int thickness = term->conf->strikeout_thickness.px >= 0
584 ? term_pt_or_px_as_pixels(
585 term, &term->conf->strikeout_thickness)
586 : font->strikeout.thickness;
588 /* Try to center custom strikeout */
589 const int position = term->conf->strikeout_thickness.px >= 0
590 ? font->strikeout.position - round(font->strikeout.thickness / 2.) + round(thickness / 2.)
591 : font->strikeout.position;
593 pixman_image_fill_rectangles(
594 PIXMAN_OP_SRC, pix, color,
595 1, &(pixman_rectangle16_t){
596 x, y + term->font_baseline - position,
597 cols * term->cell_width, thickness});
598}
600static void
601cursor_colors_for_cell(const struct terminal *term, const struct cell *cell,
602 const pixman_color_t *fg, const pixman_color_t *bg,
603 pixman_color_t *cursor_color, pixman_color_t *text_color,
604 bool gamma_correct)
605{
606 if (term->colors.cursor_bg >> 31)
607 *cursor_color = color_hex_to_pixman(term->colors.cursor_bg, gamma_correct);
608 else
609 *cursor_color = *fg;
611 if (term->colors.cursor_fg >> 31)
612 *text_color = color_hex_to_pixman(term->colors.cursor_fg, gamma_correct);
613 else {
614 xassert(bg->alpha == 0xffff);
615 *text_color = *bg;
616 }
618 if (unlikely(text_color->red == cursor_color->red &&
619 text_color->green == cursor_color->green &&
620 text_color->blue == cursor_color->blue))
621 {
622 *text_color = *bg;
623 *cursor_color = *fg;
625 if (text_color->red == cursor_color->red &&
626 text_color->green == cursor_color->green &&
627 text_color->blue == cursor_color->blue)
628 {
629 cursor_color->red = ~cursor_color->red;
630 cursor_color->green = ~cursor_color->green;
631 cursor_color->blue = ~cursor_color->blue;
632 }
633 }
634}
636static void
637draw_cursor(const struct terminal *term, const struct cell *cell,
638 const struct fcft_font *font, pixman_image_t *pix, pixman_color_t *fg,
639 const pixman_color_t *bg, int x, int y, int cols)
640{
641 pixman_color_t cursor_color;
642 pixman_color_t text_color;
643 cursor_colors_for_cell(term, cell, fg, bg, &cursor_color, &text_color,
644 wayl_do_linear_blending(term->wl, term->conf));
646 if (unlikely(!term->kbd_focus)) {
647 switch (term->conf->cursor.unfocused_style) {
648 case CURSOR_UNFOCUSED_UNCHANGED:
649 break;
651 case CURSOR_UNFOCUSED_HOLLOW:
652 draw_hollow_block(term, pix, &cursor_color, x, y, cols);
653 return;
655 case CURSOR_UNFOCUSED_NONE:
656 return;
657 }
658 }
660 switch (term->cursor_style) {
661 case CURSOR_BLOCK:
662 if (likely(term->cursor_blink.state == CURSOR_BLINK_ON) ||
663 !term->kbd_focus)
664 {
665 *fg = text_color;
666 pixman_image_fill_rectangles(
667 PIXMAN_OP_SRC, pix, &cursor_color, 1,
668 &(pixman_rectangle16_t){x, y, cols * term->cell_width, term->cell_height});
669 }
670 break;
672 case CURSOR_BEAM:
673 if (likely(term->cursor_blink.state == CURSOR_BLINK_ON ||
674 !term->kbd_focus))
675 {
676 draw_beam_cursor(term, pix, font, &cursor_color, x, y);
677 }
678 break;
680 case CURSOR_UNDERLINE:
681 if (likely(term->cursor_blink.state == CURSOR_BLINK_ON ||
682 !term->kbd_focus))
683 {
684 draw_underline_cursor(term, pix, font, &cursor_color, x, y, cols);
685 }
686 break;
688 case CURSOR_HOLLOW:
689 if (likely(term->cursor_blink.state == CURSOR_BLINK_ON))
690 draw_hollow_block(term, pix, &cursor_color, x, y, cols);
691 break;
692 }
693}
695static int
696render_cell(struct terminal *term, pixman_image_t *pix,
697 pixman_region32_t *damage, struct row *row, int row_no, int col,
698 bool has_cursor)
699{
700 struct cell *cell = &row->cells[col];
701 if (cell->attrs.clean)
702 return 0;
704 cell->attrs.clean = 1;
705 cell->attrs.confined = true;
707 int width = term->cell_width;
708 int height = term->cell_height;
709 const int x = term->margins.left + col * width;
710 const int y = term->margins.top + row_no * height;
712 uint32_t _fg = 0;
713 uint32_t _bg = 0;
715 uint16_t alpha = 0xffff;
716 const bool is_selected = cell->attrs.selected;
718 /* Use cell specific color, if set, otherwise the default colors (possible reversed) */
719 switch (cell->attrs.fg_src) {
720 case COLOR_RGB:
721 _fg = cell->attrs.fg;
722 break;
724 case COLOR_BASE16:
725 case COLOR_BASE256:
726 xassert(cell->attrs.fg < ALEN(term->colors.table));
727 _fg = term->colors.table[cell->attrs.fg];
728 break;
730 case COLOR_DEFAULT:
731 _fg = term->reverse ? term->colors.bg : term->colors.fg;
732 break;
733 }
735 switch (cell->attrs.bg_src) {
736 case COLOR_RGB:
737 _bg = cell->attrs.bg;
738 break;
740 case COLOR_BASE16:
741 case COLOR_BASE256:
742 xassert(cell->attrs.bg < ALEN(term->colors.table));
743 _bg = term->colors.table[cell->attrs.bg];
744 break;
746 case COLOR_DEFAULT:
747 _bg = term->reverse ? term->colors.fg : term->colors.bg;
748 break;
749 }
751 if (unlikely(is_selected)) {
752 const uint32_t cell_fg = _fg;
753 const uint32_t cell_bg = _bg;
755 const bool custom_fg = term->colors.selection_fg >> 24 == 0;
756 const bool custom_bg = term->colors.selection_bg >> 24 == 0;
757 const bool custom_both = custom_fg && custom_bg;
759 if (custom_both) {
760 _fg = term->colors.selection_fg;
761 _bg = term->colors.selection_bg;
762 } else if (custom_bg) {
763 _bg = term->colors.selection_bg;
764 _fg = cell->attrs.reverse ? cell_bg : cell_fg;
765 } else if (custom_fg) {
766 _fg = term->colors.selection_fg;
767 _bg = cell->attrs.reverse ? cell_fg : cell_bg;
768 } else {
769 _bg = cell_fg;
770 _fg = cell_bg;
771 }
773 if (unlikely(_fg == _bg)) {
774 /* Invert bg when selected/highlighted text has same fg/bg */
775 _bg = ~_bg;
776 alpha = 0xffff;
777 }
779 } else {
780 if (unlikely(cell->attrs.reverse)) {
781 uint32_t swap = _fg;
782 _fg = _bg;
783 _bg = swap;
784 }
786 else if (!term->window->is_fullscreen && term->colors.alpha != 0xffff) {
787 switch (term->conf->colors_dark.alpha_mode) {
788 case ALPHA_MODE_DEFAULT: {
789 if (cell->attrs.bg_src == COLOR_DEFAULT) {
790 alpha = term->colors.alpha;
791 }
792 break;
793 }
795 case ALPHA_MODE_MATCHING: {
796 if (cell->attrs.bg_src == COLOR_DEFAULT ||
797 ((cell->attrs.bg_src == COLOR_BASE16 ||
798 cell->attrs.bg_src == COLOR_BASE256) &&
799 term->colors.table[cell->attrs.bg] == term->colors.bg) ||
800 (cell->attrs.bg_src == COLOR_RGB &&
801 cell->attrs.bg == term->colors.bg))
802 {
803 alpha = term->colors.alpha;
804 }
805 break;
806 }
808 case ALPHA_MODE_ALL: {
809 alpha = term->colors.alpha;
810 break;
811 }
812 }
813 } else {
814 /*
815 * Note: disable transparency when fullscreened.
816 *
817 * This is because the wayland protocol mandates no screen
818 * content is shown behind the fullscreened window.
819 *
820 * The _intent_ of the specification is that a black (or
821 * other static color) should be used as background.
822 *
823 * There's a bit of gray area however, and some
824 * compositors have chosen to interpret the specification
825 * in a way that allows wallpapers to be seen through a
826 * fullscreen window.
827 *
828 * Given that a) the intent of the specification, and b)
829 * we don't know what the compositor will do, we simply
830 * disable transparency while in fullscreen.
831 *
832 * To see why, consider what happens if we keep our
833 * transparency. For example, if the background color is
834 * white, and alpha is 0.5, then the window will be drawn
835 * in a shade of gray while fullscreened.
836 *
837 * See
838 * https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/116
839 * for a discussion on whether transparent, fullscreen
840 * windows should be allowed in some way or not.
841 *
842 * NOTE: if changing this, also update render_margin()
843 */
844 xassert(alpha == 0xffff);
845 }
846 }
848 if (cell->attrs.dim)
849 _fg = color_dim(term, _fg);
850 if (term->conf->bold_in_bright.enabled && cell->attrs.bold)
851 _fg = color_brighten(term, _fg);
853 if (cell->attrs.blink && term->blink.state == BLINK_OFF)
854 _fg = color_blend_towards(_fg, 0x00000000, term->conf->dim.amount);
856 const bool gamma_correct = wayl_do_linear_blending(term->wl, term->conf);
857 pixman_color_t fg = color_hex_to_pixman(_fg, gamma_correct);
858 pixman_color_t bg = color_hex_to_pixman_with_alpha(_bg, alpha, gamma_correct);
860 struct fcft_font *font = attrs_to_font(term, &cell->attrs);
861 const struct composed *composed = NULL;
862 const struct fcft_grapheme *grapheme = NULL;
863 const struct fcft_glyph *single = NULL;
864 const struct fcft_glyph **glyphs = NULL;
865 unsigned glyph_count = 0;
867 char32_t base = cell->wc;
868 int cell_cols = 1;
870 if (base != 0) {
871 if (unlikely(
872 /* Classic box drawings */
873 (base >= GLYPH_BOX_DRAWING_FIRST &&
874 base <= GLYPH_BOX_DRAWING_LAST) ||
876 /* Braille */
877 (base >= GLYPH_BRAILLE_FIRST &&
878 base <= GLYPH_BRAILLE_LAST) ||
880 /*
881 * Unicode 13 "Symbols for Legacy Computing"
882 * sub-ranges below.
883 *
884 * Note, the full range is U+1FB00 - U+1FBF9
885 */
886 (base >= GLYPH_LEGACY_FIRST &&
887 base <= GLYPH_LEGACY_LAST) ||
889 /*
890 * Unicode 16 "Symbols for Legacy Computing Supplement"
891 *
892 * Note, the full range is U+1CC00 - U+1CEAF
893 */
894 (base >= GLYPH_OCTANTS_FIRST &&
895 base <= GLYPH_OCTANTS_LAST)) &&
897 likely(!term->conf->box_drawings_uses_font_glyphs))
898 {
899 struct fcft_glyph ***arr;
900 size_t count;
901 size_t idx;
903 if (base >= GLYPH_LEGACY_FIRST) {
904 arr = &term->custom_glyphs.legacy;
905 count = GLYPH_LEGACY_COUNT;
906 idx = base - GLYPH_LEGACY_FIRST;
907 } else if (base >= GLYPH_OCTANTS_FIRST) {
908 arr = &term->custom_glyphs.octants;
909 count = GLYPH_OCTANTS_COUNT;
910 idx = base - GLYPH_OCTANTS_FIRST;
911 } else if (base >= GLYPH_BRAILLE_FIRST) {
912 arr = &term->custom_glyphs.braille;
913 count = GLYPH_BRAILLE_COUNT;
914 idx = base - GLYPH_BRAILLE_FIRST;
915 } else {
916 arr = &term->custom_glyphs.box_drawing;
917 count = GLYPH_BOX_DRAWING_COUNT;
918 idx = base - GLYPH_BOX_DRAWING_FIRST;
919 }
921 if (unlikely(*arr == NULL))
922 *arr = xcalloc(count, sizeof((*arr)[0]));
924 if (likely((*arr)[idx] != NULL))
925 single = (*arr)[idx];
926 else {
927 mtx_lock(&term->render.workers.lock);
929 /* Other thread may have instantiated it while we
930 * acquired the lock */
931 single = (*arr)[idx];
932 if (likely(single == NULL))
933 single = (*arr)[idx] = box_drawing(term, base);
934 mtx_unlock(&term->render.workers.lock);
935 }
937 if (single != NULL) {
938 glyph_count = 1;
939 glyphs = &single;
940 cell_cols = single->cols;
941 }
942 }
944 else if (base >= CELL_COMB_CHARS_LO && base <= CELL_COMB_CHARS_HI)
945 {
946 composed = composed_lookup(term->composed, base - CELL_COMB_CHARS_LO);
947 base = composed->chars[0];
949 if (term->conf->can_shape_grapheme && term->conf->tweak.grapheme_shaping) {
950 grapheme = fcft_rasterize_grapheme_utf32(
951 font, composed->count, composed->chars, term->font_subpixel);
952 }
954 if (grapheme != NULL) {
955 const int forced_width = composed->forced_width;
957 cell_cols = forced_width > 0 ? forced_width : composed->width;
959 composed = NULL;
960 glyphs = grapheme->glyphs;
961 glyph_count = grapheme->count;
963 if (forced_width > 0)
964 glyph_count = min(glyph_count, forced_width);
965 }
966 }
968 if (single == NULL && grapheme == NULL) {
969 if (unlikely(base >= CELL_SPACER)) {
970 glyph_count = 0;
971 cell_cols = 1;
972 } else {
973 xassert(base != 0);
974 single = fcft_rasterize_char_utf32(font, base, term->font_subpixel);
975 if (single == NULL) {
976 glyph_count = 0;
977 cell_cols = 1;
978 } else {
979 glyph_count = 1;
980 glyphs = &single;
982 const size_t forced_width = composed != NULL ? composed->forced_width : 0;
983 cell_cols = forced_width > 0 ? forced_width : single->cols;
984 }
985 }
986 }
987 }
989 assert(glyph_count == 0 || glyphs != NULL);
991 const int cols_left = term->cols - col;
992 cell_cols = max(1, min(cell_cols, cols_left));
994 /*
995 * Determine cells that will bleed into their right neighbor and remember
996 * them for cleanup in the next frame.
997 */
998 int render_width = cell_cols * width;
999 if (term->conf->tweak.overflowing_glyphs &&
1000 glyph_count > 0 &&
1001 cols_left > cell_cols)
1002 {
1003 int glyph_width = 0, advance = 0;
1004 for (size_t i = 0; i < glyph_count; i++) {
1005 glyph_width = max(glyph_width,
1006 advance + glyphs[i]->x + glyphs[i]->width);
1007 advance += glyphs[i]->advance.x;
1008 }
1010 if (glyph_width > render_width) {
1011 render_width = min(glyph_width, render_width + width);
1013 for (int i = 0; i < cell_cols; i++)
1014 row->cells[col + i].attrs.confined = false;
1015 }
1016 }
1018 pixman_region32_t clip;
1019 pixman_region32_init_rect(
1020 &clip, x, y,
1021 render_width, term->cell_height);
1022 pixman_image_set_clip_region32(pix, &clip);
1024 if (damage != NULL) {
1025 pixman_region32_union_rect(
1026 damage, damage, x, y, render_width, term->cell_height);
1027 }
1029 pixman_region32_fini(&clip);
1031 /* Background */
1032 pixman_image_fill_rectangles(
1033 PIXMAN_OP_SRC, pix, &bg, 1,
1034 &(pixman_rectangle16_t){x, y, cell_cols * width, height});
1036 if (cell->attrs.blink && term->blink.fd < 0) {
1037 /* TODO: use a custom lock for this? */
1038 mtx_lock(&term->render.workers.lock);
1039 term_arm_blink_timer(term);
1040 mtx_unlock(&term->render.workers.lock);
1041 }
1043 if (unlikely(has_cursor && term->cursor_style == CURSOR_BLOCK && term->kbd_focus)) {
1044 const pixman_color_t bg_without_alpha = color_hex_to_pixman(_bg, gamma_correct);
1045 draw_cursor(term, cell, font, pix, &fg, &bg_without_alpha, x, y, cell_cols);
1046 }
1048 if (cell->wc == 0 || cell->wc >= CELL_SPACER || cell->wc == U'\t' ||
1049 (unlikely(cell->attrs.conceal) && !is_selected))
1050 {
1051 goto draw_cursor;
1052 }
1054 pixman_image_t *clr_pix = pixman_image_create_solid_fill(&fg);
1056 int pen_x = x;
1057 for (unsigned i = 0; i < glyph_count; i++) {
1058 const int letter_x_ofs = i == 0 ? term->font_x_ofs : 0;
1060 const struct fcft_glyph *glyph = glyphs[i];
1061 if (glyph == NULL)
1062 continue;
1064 int g_x = glyph->x;
1065 int g_y = glyph->y;
1067 if (i > 0 && glyph->x >= 0 && cell_cols == 1)
1068 g_x -= term->cell_width;
1070 if (unlikely(glyph->is_color_glyph)) {
1071 /* Glyph surface is a pre-rendered image (typically a color emoji...) */
1072 if (!(cell->attrs.blink && term->blink.state == BLINK_OFF)) {
1073 pixman_image_composite32(
1074 PIXMAN_OP_OVER, glyph->pix, NULL, pix, 0, 0, 0, 0,
1075 pen_x + letter_x_ofs + g_x, y + term->font_baseline - g_y,
1076 glyph->width, glyph->height);
1077 }
1078 } else {
1079 pixman_image_composite32(
1080 PIXMAN_OP_OVER, clr_pix, glyph->pix, pix, 0, 0, 0, 0,
1081 pen_x + letter_x_ofs + g_x, y + term->font_baseline - g_y,
1082 glyph->width, glyph->height);
1084 /* Combining characters */
1085 if (composed != NULL) {
1086 assert(glyph_count == 1);
1088 for (size_t j = 1; j < composed->count; j++) {
1089 const struct fcft_glyph *g = fcft_rasterize_char_utf32(
1090 font, composed->chars[j], term->font_subpixel);
1092 if (g == NULL)
1093 continue;
1095 /*
1096 * Fonts _should_ assume the pen position is now
1097 * *after* the base glyph, and thus use negative
1098 * offsets for combining glyphs.
1099 *
1100 * Not all fonts behave like this however, and we
1101 * try to accommodate both variants.
1102 *
1103 * Since we haven't moved our pen position yet, we
1104 * add a full cell width to the offset (or two, in
1105 * case of double-width characters).
1106 *
1107 * If the font does *not* use negative offsets,
1108 * we'd normally use an offset of 0. However, to
1109 * somewhat deal with double-width glyphs we use
1110 * an offset of *one* cell.
1111 */
1112 int x_ofs = cell_cols == 1
1113 ? g->x < 0
1114 ? cell_cols * term->cell_width
1115 : (cell_cols - 1) * term->cell_width
1116 : 0;
1118 if (cell_cols > 1)
1119 pen_x += term->cell_width;
1121 pixman_image_composite32(
1122 PIXMAN_OP_OVER, clr_pix, g->pix, pix, 0, 0, 0, 0,
1123 /* Some fonts use a negative offset, while others use a
1124 * "normal" offset */
1125 pen_x + letter_x_ofs + x_ofs + g->x,
1126 y + term->font_baseline - g->y, g->width, g->height);
1127 }
1128 }
1129 }
1131 pen_x += cell_cols > 1 ? term->cell_width : glyph->advance.x;
1132 }
1134 pixman_image_unref(clr_pix);
1136 /* Underline */
1137 if (cell->attrs.underline) {
1138 pixman_color_t underline_color = fg;
1139 enum underline_style underline_style = UNDERLINE_SINGLE;
1141 /* Check if cell has a styled underline. This lookup is fairly
1142 expensive... */
1143 if (row->extra != NULL) {
1144 for (int i = 0; i < row->extra->underline_ranges.count; i++) {
1145 const struct row_range *range = &row->extra->underline_ranges.v[i];
1147 if (range->start > col)
1148 break;
1150 if (range->start <= col && col <= range->end) {
1151 switch (range->underline.color_src) {
1152 case COLOR_BASE256:
1153 underline_color = color_hex_to_pixman(
1154 term->colors.table[range->underline.color], gamma_correct);
1155 break;
1157 case COLOR_RGB:
1158 underline_color =
1159 color_hex_to_pixman(range->underline.color, gamma_correct);
1160 break;
1162 case COLOR_DEFAULT:
1163 break;
1165 case COLOR_BASE16:
1166 BUG("underline color can't be base-16");
1167 break;
1168 }
1170 underline_style = range->underline.style;
1171 break;
1172 }
1173 }
1174 }
1176 draw_styled_underline(
1177 term, pix, font, &underline_color, underline_style, x, y, cell_cols);
1179 }
1181 if (cell->attrs.strikethrough)
1182 draw_strikeout(term, pix, font, &fg, x, y, cell_cols);
1184 if (unlikely(cell->attrs.url && term->conf->url.style != UNDERLINE_NONE)) {
1185 pixman_color_t url_color = color_hex_to_pixman(
1186 term->conf->colors_dark.use_custom.url
1187 ? term->conf->colors_dark.url
1188 : term->colors.table[3],
1189 gamma_correct);
1191 draw_styled_underline(
1192 term, pix, font, &url_color, term->conf->url.style,
1193 x, y, cell_cols);
1194 }
1196draw_cursor:
1197 if (has_cursor && (term->cursor_style != CURSOR_BLOCK || !term->kbd_focus)) {
1198 const pixman_color_t bg_without_alpha = color_hex_to_pixman(_bg, gamma_correct);
1199 draw_cursor(term, cell, font, pix, &fg, &bg_without_alpha, x, y, cell_cols);
1200 }
1202 pixman_image_set_clip_region32(pix, NULL);
1203 return cell_cols;
1204}
1206static void
1207render_row(struct terminal *term, pixman_image_t *pix,
1208 pixman_region32_t *damage, struct row *row,
1209 int row_no, int cursor_col)
1210{
1211 for (int col = term->cols - 1; col >= 0; col--)
1212 render_cell(term, pix, damage, row, row_no, col, cursor_col == col);
1213}
1215static void
1216render_urgency(struct terminal *term, struct buffer *buf)
1217{
1218 uint32_t red = term->colors.table[1];
1219 pixman_color_t bg = color_hex_to_pixman(
1220 red, wayl_do_linear_blending(term->wl, term->conf));
1222 int width = min(min(term->margins.left, term->margins.right),
1223 min(term->margins.top, term->margins.bottom));
1225 pixman_image_fill_rectangles(
1226 PIXMAN_OP_SRC, buf->pix[0], &bg, 4,
1227 (pixman_rectangle16_t[]){
1228 /* Top */
1229 {0, 0, term->width, width},
1231 /* Bottom */
1232 {0, term->height - width, term->width, width},
1234 /* Left */
1235 {0, width, width, term->height - 2 * width},
1237 /* Right */
1238 {term->width - width, width, width, term->height - 2 * width},
1239 });
1240}
1242static void
1243render_margin(struct terminal *term, struct buffer *buf,
1244 int start_line, int end_line, bool apply_damage)
1245{
1246 /* Fill area outside the cell grid with the default background color */
1247 const int rmargin = term->width - term->margins.right;
1248 const int bmargin = term->height - term->margins.bottom;
1249 const int line_count = end_line - start_line;
1251 const bool gamma_correct = wayl_do_linear_blending(term->wl, term->conf);
1252 const uint32_t _bg = !term->reverse ? term->colors.bg : term->colors.fg;
1253 uint16_t alpha = term->colors.alpha;
1255 if (term->window->is_fullscreen) {
1256 /* Disable alpha in fullscreen - see render_cell() for details */
1257 alpha = 0xffff;
1258 }
1260 pixman_color_t bg = color_hex_to_pixman_with_alpha(_bg, alpha, gamma_correct);
1262 pixman_image_fill_rectangles(
1263 PIXMAN_OP_SRC, buf->pix[0], &bg, 4,
1264 (pixman_rectangle16_t[]){
1265 /* Top */
1266 {0, 0, term->width, term->margins.top},
1268 /* Bottom */
1269 {0, bmargin, term->width, term->margins.bottom},
1271 /* Left */
1272 {0, term->margins.top + start_line * term->cell_height,
1273 term->margins.left, line_count * term->cell_height},
1275 /* Right */
1276 {rmargin, term->margins.top + start_line * term->cell_height,
1277 term->margins.right, line_count * term->cell_height},
1278 });
1280 if (term->render.urgency)
1281 render_urgency(term, buf);
1283 /* Ensure the updated regions are copied to the next frame's
1284 * buffer when we're double buffering */
1285 pixman_region32_union_rect(
1286 &buf->dirty[0], &buf->dirty[0], 0, 0, term->width, term->margins.top);
1287 pixman_region32_union_rect(
1288 &buf->dirty[0], &buf->dirty[0], 0, bmargin, term->width, term->margins.bottom);
1289 pixman_region32_union_rect(
1290 &buf->dirty[0], &buf->dirty[0], 0, 0, term->margins.left, term->height);
1291 pixman_region32_union_rect(
1292 &buf->dirty[0], &buf->dirty[0],
1293 rmargin, 0, term->margins.right, term->height);
1295 if (apply_damage) {
1296 /* Top */
1297 wl_surface_damage_buffer(
1298 term->window->surface.surf, 0, 0, term->width, term->margins.top);
1300 /* Bottom */
1301 wl_surface_damage_buffer(
1302 term->window->surface.surf, 0, bmargin, term->width, term->margins.bottom);
1304 /* Left */
1305 wl_surface_damage_buffer(
1306 term->window->surface.surf,
1307 0, term->margins.top + start_line * term->cell_height,
1308 term->margins.left, line_count * term->cell_height);
1310 /* Right */
1311 wl_surface_damage_buffer(
1312 term->window->surface.surf,
1313 rmargin, term->margins.top + start_line * term->cell_height,
1314 term->margins.right, line_count * term->cell_height);
1315 }
1316}
1318static void
1319grid_render_scroll(struct terminal *term, struct buffer *buf,
1320 const struct damage *dmg)
1321{
1322 LOG_DBG(
1323 "damage: SCROLL: %d-%d by %d lines",
1324 dmg->region.start, dmg->region.end, dmg->lines);
1326 const int region_size = dmg->region.end - dmg->region.start;
1328 if (dmg->lines >= region_size) {
1329 /* The entire scroll region will be scrolled out (i.e. replaced) */
1330 return;
1331 }
1333 const int height = (region_size - dmg->lines) * term->cell_height;
1334 xassert(height > 0);
1336#if TIME_SCROLL_DAMAGE
1337 struct timespec start_time;
1338 clock_gettime(CLOCK_MONOTONIC, &start_time);
1339#endif
1341 int dst_y = term->margins.top + (dmg->region.start + 0) * term->cell_height;
1342 int src_y = term->margins.top + (dmg->region.start + dmg->lines) * term->cell_height;
1344 /*
1345 * SHM scrolling can be *much* faster, but it depends on how many
1346 * lines we're scrolling, and how much repairing we need to do.
1347 *
1348 * In short, scrolling a *large* number of rows is faster with a
1349 * memmove, while scrolling a *small* number of lines is faster
1350 * with SHM scrolling.
1351 *
1352 * However, since we need to restore the scrolling regions when
1353 * SHM scrolling, we also need to take this into account.
1354 *
1355 * Finally, we also have to restore the window margins, and this
1356 * is a *huge* performance hit when scrolling a large number of
1357 * lines (in addition to the sloweness of SHM scrolling as
1358 * method).
1359 *
1360 * So, we need to figure out when to SHM scroll, and when to
1361 * memmove.
1362 *
1363 * For now, assume that the both methods perform roughly the same,
1364 * given an equal number of bytes to move/allocate, and use the
1365 * method that results in the least amount of bytes to touch.
1366 *
1367 * Since number of lines directly translates to bytes, we can
1368 * simply count lines.
1369 *
1370 * SHM scrolling needs to first "move" (punch hole + allocate)
1371 * dmg->lines number of lines, and then we need to restore
1372 * the bottom scroll region.
1373 *
1374 * If the total number of lines is less than half the screen - use
1375 * SHM. Otherwise use memmove.
1376 */
1377 bool try_shm_scroll =
1378 shm_can_scroll(buf) && (
1379 dmg->lines +
1380 dmg->region.start +
1381 (term->rows - dmg->region.end)) < term->rows / 2;
1383 bool did_shm_scroll = false;
1385 //try_shm_scroll = false;
1386 //try_shm_scroll = true;
1388 if (try_shm_scroll) {
1389 did_shm_scroll = shm_scroll(
1390 buf, dmg->lines * term->cell_height,
1391 term->margins.top, dmg->region.start * term->cell_height,
1392 term->margins.bottom, (term->rows - dmg->region.end) * term->cell_height);
1393 }
1395 if (did_shm_scroll) {
1396 /* Restore margins */
1397 render_margin(
1398 term, buf, dmg->region.end - dmg->lines, term->rows, false);
1399 } else {
1400 /* Fallback for when we either cannot do SHM scrolling, or it failed */
1401 uint8_t *raw = buf->data;
1402 memmove(raw + dst_y * buf->stride,
1403 raw + src_y * buf->stride,
1404 height * buf->stride);
1405 }
1407#if TIME_SCROLL_DAMAGE
1408 struct timespec end_time;
1409 clock_gettime(CLOCK_MONOTONIC, &end_time);
1411 struct timespec memmove_time;
1412 timespec_sub(&end_time, &start_time, &memmove_time);
1413 LOG_INFO("scrolled %dKB (%d lines) using %s in %lds %ldns",
1414 height * buf->stride / 1024, dmg->lines,
1415 did_shm_scroll ? "SHM" : try_shm_scroll ? "memmove (SHM failed)" : "memmove",
1416 (long)memmove_time.tv_sec, memmove_time.tv_nsec);
1417#endif
1419 wl_surface_damage_buffer(
1420 term->window->surface.surf, term->margins.left, dst_y,
1421 term->width - term->margins.left - term->margins.right, height);
1423 /*
1424 * TODO: remove this if re-enabling scroll damage when re-applying
1425 * last frame's damage (see reapply_old_damage()
1426 */
1427 pixman_region32_union_rect(
1428 &buf->dirty[0], &buf->dirty[0], 0, dst_y, buf->width, height);
1429}
1431static void
1432grid_render_scroll_reverse(struct terminal *term, struct buffer *buf,
1433 const struct damage *dmg)
1434{
1435 LOG_DBG(
1436 "damage: SCROLL REVERSE: %d-%d by %d lines",
1437 dmg->region.start, dmg->region.end, dmg->lines);
1439 const int region_size = dmg->region.end - dmg->region.start;
1441 if (dmg->lines >= region_size) {
1442 /* The entire scroll region will be scrolled out (i.e. replaced) */
1443 return;
1444 }
1446 const int height = (region_size - dmg->lines) * term->cell_height;
1447 xassert(height > 0);
1449#if TIME_SCROLL_DAMAGE
1450 struct timespec start_time;
1451 clock_gettime(CLOCK_MONOTONIC, &start_time);
1452#endif
1454 int src_y = term->margins.top + (dmg->region.start + 0) * term->cell_height;
1455 int dst_y = term->margins.top + (dmg->region.start + dmg->lines) * term->cell_height;
1457 bool try_shm_scroll =
1458 shm_can_scroll(buf) && (
1459 dmg->lines +
1460 dmg->region.start +
1461 (term->rows - dmg->region.end)) < term->rows / 2;
1463 bool did_shm_scroll = false;
1465 if (try_shm_scroll) {
1466 did_shm_scroll = shm_scroll(
1467 buf, -dmg->lines * term->cell_height,
1468 term->margins.top, dmg->region.start * term->cell_height,
1469 term->margins.bottom, (term->rows - dmg->region.end) * term->cell_height);
1470 }
1472 if (did_shm_scroll) {
1473 /* Restore margins */
1474 render_margin(
1475 term, buf, dmg->region.start, dmg->region.start + dmg->lines, false);
1476 } else {
1477 /* Fallback for when we either cannot do SHM scrolling, or it failed */
1478 uint8_t *raw = buf->data;
1479 memmove(raw + dst_y * buf->stride,
1480 raw + src_y * buf->stride,
1481 height * buf->stride);
1482 }
1484#if TIME_SCROLL_DAMAGE
1485 struct timespec end_time;
1486 clock_gettime(CLOCK_MONOTONIC, &end_time);
1488 struct timespec memmove_time;
1489 timespec_sub(&end_time, &start_time, &memmove_time);
1490 LOG_INFO("scrolled REVERSE %dKB (%d lines) using %s in %lds %ldns",
1491 height * buf->stride / 1024, dmg->lines,
1492 did_shm_scroll ? "SHM" : try_shm_scroll ? "memmove (SHM failed)" : "memmove",
1493 (long)memmove_time.tv_sec, memmove_time.tv_nsec);
1494#endif
1496 wl_surface_damage_buffer(
1497 term->window->surface.surf, term->margins.left, dst_y,
1498 term->width - term->margins.left - term->margins.right, height);
1500 /*
1501 * TODO: remove this if re-enabling scroll damage when re-applying
1502 * last frame's damage (see reapply_old_damage()
1503 */
1504 pixman_region32_union_rect(
1505 &buf->dirty[0], &buf->dirty[0], 0, dst_y, buf->width, height);
1506}
1508static void
1509render_sixel_chunk(struct terminal *term, pixman_image_t *pix,
1510 pixman_region32_t *damage, const struct sixel *sixel,
1511 int term_start_row, int img_start_row, int count)
1512{
1513 /* Translate row/column to x/y pixel values */
1514 const int x = term->margins.left + sixel->pos.col * term->cell_width;
1515 const int y = term->margins.top + term_start_row * term->cell_height;
1517 /* Width/height, in pixels - and don't touch the window margins */
1518 const int width = max(
1519 0,
1520 min(sixel->width,
1521 term->width - x - term->margins.right));
1522 const int height = max(
1523 0,
1524 min(
1525 min(count * term->cell_height, /* 'count' number of rows */
1526 sixel->height - img_start_row * term->cell_height), /* What remains of the sixel */
1527 term->height - y - term->margins.bottom));
1529 /* Verify we're not stepping outside the grid */
1530 xassert(x >= term->margins.left);
1531 xassert(y >= term->margins.top);
1532 xassert(width == 0 || x + width <= term->width - term->margins.right);
1533 xassert(height == 0 || y + height <= term->height - term->margins.bottom);
1535 //LOG_DBG("sixel chunk: %dx%d %dx%d", x, y, width, height);
1537 pixman_image_composite32(
1538 sixel->opaque ? PIXMAN_OP_SRC : PIXMAN_OP_OVER,
1539 sixel->pix,
1540 NULL,
1541 pix,
1542 0, img_start_row * term->cell_height,
1543 0, 0,
1544 x, y,
1545 width, height);
1547 if (damage != NULL)
1548 pixman_region32_union_rect(damage, damage, x, y, width, height);
1549}
1551static void
1552render_sixel(struct terminal *term, pixman_image_t *pix,
1553 pixman_region32_t *damage, const struct coord *cursor,
1554 const struct sixel *sixel)
1555{
1556 xassert(sixel->pix != NULL);
1557 xassert(sixel->width >= 0);
1558 xassert(sixel->height >= 0);
1560 const int view_end = (term->grid->view + term->rows - 1) & (term->grid->num_rows - 1);
1561 const bool last_row_needs_erase = sixel->height % term->cell_height != 0;
1562 const bool last_col_needs_erase = sixel->width % term->cell_width != 0;
1564 int chunk_img_start = -1; /* Image-relative start row of chunk */
1565 int chunk_term_start = -1; /* Viewport relative start row of chunk */
1566 int chunk_row_count = 0; /* Number of rows to emit */
1568#define maybe_emit_sixel_chunk_then_reset() \
1569 if (chunk_row_count != 0) { \
1570 render_sixel_chunk( \
1571 term, pix, damage, sixel, \
1572 chunk_term_start, chunk_img_start, chunk_row_count); \
1573 chunk_term_start = chunk_img_start = -1; \
1574 chunk_row_count = 0; \
1575 }
1577 /*
1578 * Iterate all sixel rows:
1579 *
1580 * - ignore rows that aren't visible on-screen
1581 * - ignore rows that aren't dirty (they have already been rendered)
1582 * - chunk consecutive dirty rows into a 'chunk'
1583 * - emit (render) chunk as soon as a row isn't visible, or is clean
1584 * - emit final chunk after we've iterated all rows
1585 *
1586 * The purpose of this is to reduce the amount of pixels that
1587 * needs to be composited and marked as damaged for the
1588 * compositor.
1589 *
1590 * Since we do CPU based composition, rendering is a slow and
1591 * heavy task for foot, and thus it is important to not re-render
1592 * things unnecessarily.
1593 */
1595 for (int _abs_row_no = sixel->pos.row;
1596 _abs_row_no < sixel->pos.row + sixel->rows;
1597 _abs_row_no++)
1598 {
1599 const int abs_row_no = _abs_row_no & (term->grid->num_rows - 1);
1600 const int term_row_no =
1601 (abs_row_no - term->grid->view + term->grid->num_rows) &
1602 (term->grid->num_rows - 1);
1604 /* Check if row is in the visible viewport */
1605 if (view_end >= term->grid->view) {
1606 /* Not wrapped */
1607 if (!(abs_row_no >= term->grid->view && abs_row_no <= view_end)) {
1608 /* Not visible */
1609 maybe_emit_sixel_chunk_then_reset();
1610 continue;
1611 }
1612 } else {
1613 /* Wrapped */
1614 if (!(abs_row_no >= term->grid->view || abs_row_no <= view_end)) {
1615 /* Not visible */
1616 maybe_emit_sixel_chunk_then_reset();
1617 continue;
1618 }
1619 }
1621 /* Is the row dirty? */
1622 struct row *row = term->grid->rows[abs_row_no];
1623 xassert(row != NULL); /* Should be visible */
1625 if (!row->dirty) {
1626 maybe_emit_sixel_chunk_then_reset();
1627 continue;
1628 }
1630 int cursor_col = cursor->row == term_row_no ? cursor->col : -1;
1632 /*
1633 * If image contains transparent parts, render all (dirty)
1634 * cells beneath it.
1635 *
1636 * If image is opaque, loop cells and set their 'clean' bit,
1637 * to prevent the grid rendered from overwriting the sixel
1638 *
1639 * If the last sixel row only partially covers the cell row,
1640 * 'erase' the cell by rendering them.
1641 *
1642 * In all cases, do *not* clear the 'dirty' bit on the row, to
1643 * ensure the regular renderer includes them in the damage
1644 * rect.
1645 */
1646 if (!sixel->opaque) {
1647 /* TODO: multithreading */
1648 render_row(term, pix, damage, row, term_row_no, cursor_col);
1649 } else {
1650 for (int col = sixel->pos.col;
1651 col < min(sixel->pos.col + sixel->cols, term->cols);
1652 col++)
1653 {
1654 struct cell *cell = &row->cells[col];
1656 if (!cell->attrs.clean) {
1657 bool last_row = abs_row_no == sixel->pos.row + sixel->rows - 1;
1658 bool last_col = col == sixel->pos.col + sixel->cols - 1;
1660 if ((last_row_needs_erase && last_row) ||
1661 (last_col_needs_erase && last_col))
1662 {
1663 render_cell(term, pix, damage, row, term_row_no, col, cursor_col == col);
1664 } else {
1665 cell->attrs.clean = 1;
1666 cell->attrs.confined = 1;
1667 }
1668 }
1669 }
1670 }
1672 if (chunk_term_start == -1) {
1673 xassert(chunk_img_start == -1);
1674 chunk_term_start = term_row_no;
1675 chunk_img_start = _abs_row_no - sixel->pos.row;
1676 chunk_row_count = 1;
1677 } else
1678 chunk_row_count++;
1679 }
1681 maybe_emit_sixel_chunk_then_reset();
1682#undef maybe_emit_sixel_chunk_then_reset
1683}
1685static void
1686render_sixel_images(struct terminal *term, pixman_image_t *pix,
1687 pixman_region32_t *damage, const struct coord *cursor)
1688{
1689 if (likely(tll_length(term->grid->sixel_images)) == 0)
1690 return;
1692 const int scrollback_end
1693 = (term->grid->offset + term->rows) & (term->grid->num_rows - 1);
1695 const int view_start
1696 = (term->grid->view
1697 - scrollback_end
1698 + term->grid->num_rows) & (term->grid->num_rows - 1);
1700 const int view_end = view_start + term->rows - 1;
1702 //LOG_DBG("SIXELS: %zu images, view=%d-%d",
1703 // tll_length(term->grid->sixel_images), view_start, view_end);
1705 tll_foreach(term->grid->sixel_images, it) {
1706 const struct sixel *six = &it->item;
1707 const int start
1708 = (six->pos.row
1709 - scrollback_end
1710 + term->grid->num_rows) & (term->grid->num_rows - 1);
1711 const int end = start + six->rows - 1;
1713 //LOG_DBG(" sixel: %d-%d", start, end);
1714 if (start > view_end) {
1715 /* Sixel starts after view ends, no need to try to render it */
1716 continue;
1717 } else if (end < view_start) {
1718 /* Image ends before view starts. Since the image list is
1719 * sorted, we can safely stop here */
1720 break;
1721 }
1723 sixel_sync_cache(term, &it->item);
1724 render_sixel(term, pix, damage, cursor, &it->item);
1725 }
1726}
1728#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
1729static void
1730render_ime_preedit_for_seat(struct terminal *term, struct seat *seat,
1731 struct buffer *buf)
1732{
1733 if (likely(seat->ime.preedit.cells == NULL))
1734 return;
1736 if (unlikely(term->is_searching))
1737 return;
1739 const bool gamma_correct = wayl_do_linear_blending(term->wl, term->conf);
1741 /* Adjust cursor position to viewport */
1742 struct coord cursor;
1743 cursor = term->grid->cursor.point;
1744 cursor.row += term->grid->offset;
1745 cursor.row -= term->grid->view;
1746 cursor.row &= term->grid->num_rows - 1;
1748 if (cursor.row < 0 || cursor.row >= term->rows)
1749 return;
1751 int cells_needed = seat->ime.preedit.count;
1753 if (seat->ime.preedit.cursor.start == cells_needed &&
1754 seat->ime.preedit.cursor.end == cells_needed)
1755 {
1756 /* Cursor will be drawn *after* the pre-edit string, i.e. in
1757 * the cell *after*. This means we need to copy, and dirty,
1758 * one extra cell from the original grid, or we'll leave
1759 * trailing "cursors" after us if the user deletes text while
1760 * pre-editing */
1761 cells_needed++;
1762 }
1764 int row_idx = cursor.row;
1765 int col_idx = cursor.col;
1766 int ime_ofs = 0; /* Offset into pre-edit string to start rendering at */
1768 int cells_left = term->cols - cursor.col;
1769 int cells_used = min(cells_needed, term->cols);
1771 /* Adjust start of pre-edit text to the left if string doesn't fit on row */
1772 if (cells_left < cells_used)
1773 col_idx -= cells_used - cells_left;
1775 if (cells_needed > cells_used) {
1776 int start = seat->ime.preedit.cursor.start;
1777 int end = seat->ime.preedit.cursor.end;
1779 if (start == end) {
1780 /* Ensure *end* of pre-edit string is visible */
1781 ime_ofs = cells_needed - cells_used;
1782 } else {
1783 /* Ensure the *beginning* of the cursor-area is visible */
1784 ime_ofs = start;
1786 /* Display as much as possible of the pre-edit string */
1787 if (cells_needed - ime_ofs < cells_used)
1788 ime_ofs = cells_needed - cells_used;
1789 }
1791 /* Make sure we don't start in the middle of a character */
1792 while (ime_ofs < cells_needed &&
1793 seat->ime.preedit.cells[ime_ofs].wc >= CELL_SPACER)
1794 {
1795 ime_ofs++;
1796 }
1797 }
1799 xassert(col_idx >= 0);
1800 xassert(col_idx < term->cols);
1802 struct row *row = grid_row_in_view(term->grid, row_idx);
1804 /* Don't start pre-edit text in the middle of a double-width character */
1805 while (col_idx > 0 && row->cells[col_idx].wc >= CELL_SPACER) {
1806 cells_used++;
1807 col_idx--;
1808 }
1810 /*
1811 * Copy original content (render_cell() reads cell data directly
1812 * from grid), and mark all cells as dirty. This ensures they are
1813 * re-rendered when the pre-edit text is modified or removed.
1814 */
1815 struct cell *real_cells = xmalloc(cells_used * sizeof(real_cells[0]));
1816 for (int i = 0; i < cells_used; i++) {
1817 xassert(col_idx + i < term->cols);
1818 real_cells[i] = row->cells[col_idx + i];
1819 real_cells[i].attrs.clean = 0;
1820 }
1821 row->dirty = true;
1823 /* Render pre-edit text */
1824 xassert(seat->ime.preedit.cells[ime_ofs].wc < CELL_SPACER);
1825 for (int i = 0, idx = ime_ofs; idx < seat->ime.preedit.count; i++, idx++) {
1826 const struct cell *cell = &seat->ime.preedit.cells[idx];
1828 if (cell->wc >= CELL_SPACER)
1829 continue;
1831 int width = max(1, c32width(cell->wc));
1832 if (col_idx + i + width > term->cols)
1833 break;
1835 row->cells[col_idx + i] = *cell;
1836 render_cell(term, buf->pix[0], NULL, row, row_idx, col_idx + i, false);
1837 }
1839 int start = seat->ime.preedit.cursor.start - ime_ofs;
1840 int end = seat->ime.preedit.cursor.end - ime_ofs;
1842 if (!seat->ime.preedit.cursor.hidden) {
1843 const struct cell *start_cell = &seat->ime.preedit.cells[0];
1845 pixman_color_t fg = color_hex_to_pixman(term->colors.fg, gamma_correct);
1846 pixman_color_t bg = color_hex_to_pixman(term->colors.bg, gamma_correct);
1848 pixman_color_t cursor_color, text_color;
1849 cursor_colors_for_cell(
1850 term, start_cell, &fg, &bg, &cursor_color, &text_color, gamma_correct);
1852 int x = term->margins.left + (col_idx + start) * term->cell_width;
1853 int y = term->margins.top + row_idx * term->cell_height;
1855 if (end == start) {
1856 /* Bar */
1857 if (start >= 0) {
1858 struct fcft_font *font = attrs_to_font(term, &start_cell->attrs);
1859 draw_beam_cursor(term, buf->pix[0], font, &cursor_color, x, y);
1860 }
1861 term_ime_set_cursor_rect(term, x, y, 1, term->cell_height);
1862 }
1864 else if (end > start) {
1865 /* Hollow cursor */
1866 if (start >= 0 && end <= term->cols) {
1867 int cols = end - start;
1868 draw_hollow_block(term, buf->pix[0], &cursor_color, x, y, cols);
1869 }
1871 term_ime_set_cursor_rect(
1872 term, x, y, (end - start) * term->cell_width, term->cell_height);
1873 }
1874 }
1876 /* Restore original content (but do not render) */
1877 for (int i = 0; i < cells_used; i++)
1878 row->cells[col_idx + i] = real_cells[i];
1879 free(real_cells);
1881 const int damage_x = term->margins.left + col_idx * term->cell_width;
1882 const int damage_y = term->margins.top + row_idx * term->cell_height;
1883 const int damage_w = cells_used * term->cell_width;
1884 const int damage_h = term->cell_height;
1886 wl_surface_damage_buffer(
1887 term->window->surface.surf,
1888 damage_x, damage_y, damage_w, damage_h);
1889}
1890#endif
1892static void
1893render_ime_preedit(struct terminal *term, struct buffer *buf)
1894{
1895#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
1896 tll_foreach(term->wl->seats, it) {
1897 if (it->item.kbd_focus == term)
1898 render_ime_preedit_for_seat(term, &it->item, buf);
1899 }
1900#endif
1901}
1903static void
1904render_overlay_single_pixel(struct terminal *term, enum overlay_style style,
1905 pixman_color_t color)
1906{
1907 struct wayland *wayl = term->wl;
1908 struct wayl_sub_surface *overlay = &term->window->overlay;
1909 struct wl_buffer *buf = NULL;
1911 /*
1912 * In an ideal world, we'd only update the surface (i.e. commit
1913 * any changes) if anything has actually changed.
1914 *
1915 * For technical reasons, we can't do that, since we can't
1916 * determine whether the last committed buffer is still valid
1917 * (i.e. does it correspond to the current overlay style, *and*
1918 * does last frame's size match the current size?)
1919 *
1920 * What we _can_ do is use the fact that single-pixel buffers
1921 * don't have a size; you have to use a viewport to "size" them.
1922 *
1923 * This means we can check if the last frame's overlay style is
1924 * the same as the current size. If so, then we *know* that the
1925 * currently attached buffer is valid, and we *don't* have to
1926 * create a new single-pixel buffer.
1927 *
1928 * What we do *not* know if the *size* is still valid. This means
1929 * we do have to do the viewport calls, and a surface commit.
1930 *
1931 * This is still better than *always* creating a new buffer.
1932 */
1934 assert(style == OVERLAY_UNICODE_MODE || style == OVERLAY_FLASH);
1935 assert(wayl->single_pixel_manager != NULL);
1936 assert(overlay->surface.viewport != NULL);
1938 quirk_weston_subsurface_desync_on(overlay->sub);
1940 if (style != term->render.last_overlay_style) {
1941 buf = wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer(
1942 wayl->single_pixel_manager,
1943 (double)color.red / 0xffff * 0xffffffff,
1944 (double)color.green / 0xffff * 0xffffffff,
1945 (double)color.blue / 0xffff * 0xffffffff,
1946 (double)color.alpha / 0xffff * 0xffffffff);
1948 wl_surface_set_buffer_scale(overlay->surface.surf, 1);
1949 wl_surface_attach(overlay->surface.surf, buf, 0, 0);
1950 }
1952 wp_viewport_set_destination(
1953 overlay->surface.viewport,
1954 roundf(term->width / term->scale),
1955 roundf(term->height / term->scale));
1957 wl_subsurface_set_position(overlay->sub, 0, 0);
1959 wl_surface_damage_buffer(
1960 overlay->surface.surf, 0, 0, term->width, term->height);
1962 wl_surface_commit(overlay->surface.surf);
1963 quirk_weston_subsurface_desync_off(overlay->sub);
1965 term->render.last_overlay_style = style;
1967 if (buf != NULL) {
1968 wl_buffer_destroy(buf);
1969 }
1970}
1972void
1973render_overlay(struct terminal *term)
1974{
1975 struct wayl_sub_surface *overlay = &term->window->overlay;
1976 const bool unicode_mode_active = term->unicode_mode.active;
1978 const enum overlay_style style =
1979 term->is_searching ? OVERLAY_SEARCH :
1980 term->flash.active ? OVERLAY_FLASH :
1981 unicode_mode_active ? OVERLAY_UNICODE_MODE :
1982 OVERLAY_NONE;
1984 if (likely(style == OVERLAY_NONE)) {
1985 if (term->render.last_overlay_style != OVERLAY_NONE) {
1986 /* Unmap overlay sub-surface */
1987 wl_surface_attach(overlay->surface.surf, NULL, 0, 0);
1988 wl_surface_commit(overlay->surface.surf);
1989 term->render.last_overlay_style = OVERLAY_NONE;
1990 term->render.last_overlay_buf = NULL;
1991 }
1992 return;
1993 }
1995 pixman_color_t color;
1997 switch (style) {
1998 case OVERLAY_SEARCH:
1999 case OVERLAY_UNICODE_MODE:
2000 color = (pixman_color_t){0, 0, 0, 0x7fff};
2001 break;
2003 case OVERLAY_FLASH:
2004 color = color_hex_to_pixman_with_alpha(
2005 term->conf->colors_dark.flash,
2006 term->conf->colors_dark.flash_alpha,
2007 wayl_do_linear_blending(term->wl, term->conf));
2008 break;
2010 case OVERLAY_NONE:
2011 xassert(false);
2012 break;
2013 }
2015 const bool single_pixel =
2016 (style == OVERLAY_UNICODE_MODE || style == OVERLAY_FLASH) &&
2017 term->wl->single_pixel_manager != NULL &&
2018 overlay->surface.viewport != NULL;
2020 if (single_pixel) {
2021 render_overlay_single_pixel(term, style, color);
2022 return;
2023 }
2025 struct buffer *buf = shm_get_buffer(
2026 term->render.chains.overlay, term->width, term->height);
2027 pixman_image_set_clip_region32(buf->pix[0], NULL);
2029 /* Bounding rectangle of damaged areas - for wl_surface_damage_buffer() */
2030 pixman_box32_t damage_bounds;
2032 if (style == OVERLAY_SEARCH) {
2033 /*
2034 * When possible, we only update the areas that have *changed*
2035 * since the last frame. That means:
2036 *
2037 * - clearing/erasing cells that are now selected, but weren't
2038 * in the last frame
2039 * - dimming cells that were selected, but aren't anymore
2040 *
2041 * To do this, we save the last frame's selected cells as a
2042 * pixman region.
2043 *
2044 * Then, we calculate the corresponding region for this
2045 * frame's selected cells.
2046 *
2047 * Last frame's region minus this frame's region gives us the
2048 * region that needs to be *dimmed* in this frame
2049 *
2050 * This frame's region minus last frame's region gives us the
2051 * region that needs to be *cleared* in this frame.
2052 *
2053 * Finally, the union of the two "diff" regions above, gives
2054 * us the total region affected by a change, in either way. We
2055 * use this as the bounding box for the
2056 * wl_surface_damage_buffer() call.
2057 */
2058 pixman_region32_t *see_through = &term->render.last_overlay_clip;
2059 pixman_region32_t old_see_through;
2060 const bool buffer_reuse =
2061 buf == term->render.last_overlay_buf &&
2062 style == term->render.last_overlay_style &&
2063 buf->age == 0;
2065 if (!buffer_reuse) {
2066 /* Can't reuse last frame's damage - set to full window,
2067 * to ensure *everything* is updated */
2068 pixman_region32_init_rect(
2069 &old_see_through, 0, 0, buf->width, buf->height);
2070 } else {
2071 /* Use last frame's saved region */
2072 pixman_region32_init(&old_see_through);
2073 pixman_region32_copy(&old_see_through, see_through);
2074 }
2076 pixman_region32_clear(see_through);
2078 /* Build region consisting of all current search matches */
2079 struct search_match_iterator iter = search_matches_new_iter(term);
2080 for (struct range match = search_matches_next(&iter);
2081 match.start.row >= 0;
2082 match = search_matches_next(&iter))
2083 {
2084 int r = match.start.row;
2085 int start_col = match.start.col;
2086 const int end_row = match.end.row;
2088 while (true) {
2089 const int end_col =
2090 r == end_row ? match.end.col : term->cols - 1;
2092 int x = term->margins.left + start_col * term->cell_width;
2093 int y = term->margins.top + r * term->cell_height;
2094 int width = (end_col + 1 - start_col) * term->cell_width;
2095 int height = 1 * term->cell_height;
2097 pixman_region32_union_rect(
2098 see_through, see_through, x, y, width, height);
2100 if (++r > end_row)
2101 break;
2103 start_col = 0;
2104 }
2105 }
2107 /* Areas that need to be cleared: cells that were dimmed in
2108 * the last frame but is now see-through */
2109 pixman_region32_t new_see_through;
2110 pixman_region32_init(&new_see_through);
2112 if (buffer_reuse)
2113 pixman_region32_subtract(&new_see_through, see_through, &old_see_through);
2114 else {
2115 /* Buffer content is unknown - explicitly clear *all*
2116 * current see-through areas */
2117 pixman_region32_copy(&new_see_through, see_through);
2118 }
2119 pixman_image_set_clip_region32(buf->pix[0], &new_see_through);
2121 /* Areas that need to be dimmed: cells that were cleared in
2122 * the last frame but is not anymore */
2123 pixman_region32_t new_dimmed;
2124 pixman_region32_init(&new_dimmed);
2125 pixman_region32_subtract(&new_dimmed, &old_see_through, see_through);
2126 pixman_region32_fini(&old_see_through);
2128 /* Total affected area */
2129 pixman_region32_t damage;
2130 pixman_region32_init(&damage);
2131 pixman_region32_union(&damage, &new_see_through, &new_dimmed);
2132 damage_bounds = damage.extents;
2134 /* Clear cells that became selected in this frame. */
2135 pixman_image_fill_rectangles(
2136 PIXMAN_OP_SRC, buf->pix[0], &(pixman_color_t){0}, 1,
2137 &(pixman_rectangle16_t){0, 0, term->width, term->height});
2139 /* Set clip region for the newly dimmed cells. The actual
2140 * paint call is done below */
2141 pixman_image_set_clip_region32(buf->pix[0], &new_dimmed);
2143 pixman_region32_fini(&new_see_through);
2144 pixman_region32_fini(&new_dimmed);
2145 pixman_region32_fini(&damage);
2146 }
2148 else if (buf == term->render.last_overlay_buf &&
2149 style == term->render.last_overlay_style)
2150 {
2151 xassert(style == OVERLAY_FLASH || style == OVERLAY_UNICODE_MODE);
2152 shm_did_not_use_buf(buf);
2153 return;
2154 } else {
2155 pixman_image_set_clip_region32(buf->pix[0], NULL);
2156 damage_bounds = (pixman_box32_t){0, 0, buf->width, buf->height};
2157 }
2159 pixman_image_fill_rectangles(
2160 PIXMAN_OP_SRC, buf->pix[0], &color, 1,
2161 &(pixman_rectangle16_t){0, 0, term->width, term->height});
2163 quirk_weston_subsurface_desync_on(overlay->sub);
2164 wayl_surface_scale(
2165 term->window, &overlay->surface, buf, term->scale);
2166 wl_subsurface_set_position(overlay->sub, 0, 0);
2167 wl_surface_attach(overlay->surface.surf, buf->wl_buf, 0, 0);
2169 wl_surface_damage_buffer(
2170 overlay->surface.surf,
2171 damage_bounds.x1, damage_bounds.y1,
2172 damage_bounds.x2 - damage_bounds.x1,
2173 damage_bounds.y2 - damage_bounds.y1);
2175 wl_surface_commit(overlay->surface.surf);
2176 quirk_weston_subsurface_desync_off(overlay->sub);
2178 buf->age = 0;
2179 term->render.last_overlay_buf = buf;
2180 term->render.last_overlay_style = style;
2181}
2183int
2184render_worker_thread(void *_ctx)
2185{
2186 struct render_worker_context *ctx = _ctx;
2187 struct terminal *term = ctx->term;
2188 const int my_id = ctx->my_id;
2189 free(ctx);
2191 sigset_t mask;
2192 sigfillset(&mask);
2193 pthread_sigmask(SIG_SETMASK, &mask, NULL);
2195 char proc_title[16];
2196 snprintf(proc_title, sizeof(proc_title), "foot:render:%d", my_id);
2198 if (pthread_setname_np(pthread_self(), proc_title) < 0)
2199 LOG_ERRNO("render worker %d: failed to set process title", my_id);
2201 sem_t *start = &term->render.workers.start;
2202 sem_t *done = &term->render.workers.done;
2203 mtx_t *lock = &term->render.workers.lock;
2205 while (true) {
2206 sem_wait(start);
2208 struct buffer *buf = term->render.workers.buf;
2210 bool frame_done = false;
2212 /* Translate offset-relative cursor row to view-relative */
2213 struct coord cursor = {-1, -1};
2214 if (!term->hide_cursor) {
2215 cursor = term->grid->cursor.point;
2216 cursor.row += term->grid->offset;
2217 cursor.row -= term->grid->view;
2218 cursor.row &= term->grid->num_rows - 1;
2219 }
2221 while (!frame_done) {
2222 mtx_lock(lock);
2223 xassert(tll_length(term->render.workers.queue) > 0);
2225 int row_no = tll_pop_front(term->render.workers.queue);
2226 mtx_unlock(lock);
2228 switch (row_no) {
2229 default: {
2230 struct row *row = grid_row_in_view(term->grid, row_no);
2231 int cursor_col = cursor.row == row_no ? cursor.col : -1;
2233 render_row(term, buf->pix[my_id], &buf->dirty[my_id],
2234 row, row_no, cursor_col);
2235 break;
2236 }
2238 case -1:
2239 frame_done = true;
2240 sem_post(done);
2241 break;
2243 case -2:
2244 return 0;
2246 case -3: {
2247 if (term->conf->tweak.render_timer != RENDER_TIMER_NONE)
2248 clock_gettime(CLOCK_MONOTONIC, &term->render.workers.preapplied_damage.start);
2250 mtx_lock(&term->render.workers.preapplied_damage.lock);
2251 buf = term->render.workers.preapplied_damage.buf;
2252 xassert(buf != NULL);
2254 if (likely(term->render.last_buf != NULL)) {
2255 mtx_unlock(&term->render.workers.preapplied_damage.lock);
2257 pixman_region32_t dmg;
2258 pixman_region32_init(&dmg);
2260 if (buf->age == 0)
2261 ; /* No need to do anything */
2262 else if (buf->age == 1)
2263 pixman_region32_copy(&dmg,
2264 &term->render.last_buf->dirty[0]);
2265 else
2266 pixman_region32_init_rect(&dmg, 0, 0, buf->width,
2267 buf->height);
2269 pixman_image_set_clip_region32(buf->pix[my_id], &dmg);
2270 pixman_image_composite32(PIXMAN_OP_SRC,
2271 term->render.last_buf->pix[my_id],
2272 NULL, buf->pix[my_id], 0, 0, 0, 0, 0,
2273 0, buf->width, buf->height);
2275 pixman_region32_fini(&dmg);
2277 buf->age = 0;
2278 shm_unref(term->render.last_buf);
2279 shm_addref(buf);
2280 term->render.last_buf = buf;
2282 mtx_lock(&term->render.workers.preapplied_damage.lock);
2283 }
2285 term->render.workers.preapplied_damage.buf = NULL;
2286 cnd_signal(&term->render.workers.preapplied_damage.cond);
2287 mtx_unlock(&term->render.workers.preapplied_damage.lock);
2289 if (term->conf->tweak.render_timer != RENDER_TIMER_NONE)
2290 clock_gettime(CLOCK_MONOTONIC, &term->render.workers.preapplied_damage.stop);
2292 frame_done = true;
2293 break;
2294 }
2295 }
2296 }
2297 };
2299 return -1;
2300}
2302void
2303render_wait_for_preapply_damage(struct terminal *term)
2304{
2305 if (!term->render.preapply_last_frame_damage)
2306 return;
2307 if (term->render.workers.preapplied_damage.buf == NULL)
2308 return;
2310 mtx_lock(&term->render.workers.preapplied_damage.lock);
2311 while (term->render.workers.preapplied_damage.buf != NULL) {
2312 cnd_wait(&term->render.workers.preapplied_damage.cond,
2313 &term->render.workers.preapplied_damage.lock);
2314 }
2315 mtx_unlock(&term->render.workers.preapplied_damage.lock);
2316}
2318struct csd_data
2319get_csd_data(const struct terminal *term, enum csd_surface surf_idx)
2320{
2321 xassert(term->window->csd_mode == CSD_YES);
2323 const bool borders_visible = wayl_win_csd_borders_visible(term->window);
2324 const bool title_visible = wayl_win_csd_titlebar_visible(term->window);
2326 const float scale = term->scale;
2328 const int border_width = borders_visible
2329 ? roundf(term->conf->csd.border_width * scale) : 0;
2331 const int title_height = title_visible
2332 ? roundf(term->conf->csd.title_height * scale) : 0;
2334 const int button_width = title_visible
2335 ? roundf(term->conf->csd.button_width * scale) : 0;
2337 int remaining_width = term->width;
2339 const int button_close_width = remaining_width >= button_width ? button_width : 0;
2340 remaining_width -= button_close_width;
2341 const int button_close_start = remaining_width;
2343 const int button_maximize_width = remaining_width >= button_width &&
2344 term->window->wm_capabilities.maximize ? button_width : 0;
2345 remaining_width -= button_maximize_width;
2346 const int button_maximize_start = remaining_width;
2348 const int button_minimize_width = remaining_width >= button_width &&
2349 term->window->wm_capabilities.minimize ? button_width : 0;
2350 remaining_width -= button_minimize_width;
2351 const int button_minimize_start = remaining_width;
2353 /*
2354 * With fractional scaling, we must ensure the offset, when
2355 * divided by the scale (in set_position()), and the scaled back
2356 * (by the compositor), matches the actual pixel count made up by
2357 * the titlebar and the border.
2358 */
2359 const int top_offset = roundf(
2360 scale * (roundf(-title_height / scale) - roundf(border_width / scale)));
2362 const int top_bottom_width = roundf(
2363 scale * (roundf(term->width / scale) + 2 * roundf(border_width / scale)));
2365 const int left_right_height = roundf(
2366 scale * (roundf(title_height / scale) + roundf(term->height / scale)));
2368 switch (surf_idx) {
2369 case CSD_SURF_TITLE: return (struct csd_data){ 0, -title_height, term->width, title_height};
2370 case CSD_SURF_LEFT: return (struct csd_data){-border_width, -title_height, border_width, left_right_height};
2371 case CSD_SURF_RIGHT: return (struct csd_data){ term->width, -title_height, border_width, left_right_height};
2372 case CSD_SURF_TOP: return (struct csd_data){-border_width, top_offset, top_bottom_width, border_width};
2373 case CSD_SURF_BOTTOM: return (struct csd_data){-border_width, term->height, top_bottom_width, border_width};
2375 /* Positioned relative to CSD_SURF_TITLE */
2376 case CSD_SURF_MINIMIZE: return (struct csd_data){button_minimize_start, 0, button_minimize_width, title_height};
2377 case CSD_SURF_MAXIMIZE: return (struct csd_data){button_maximize_start, 0, button_maximize_width, title_height};
2378 case CSD_SURF_CLOSE: return (struct csd_data){ button_close_start, 0, button_close_width, title_height};
2380 case CSD_SURF_COUNT:
2381 break;
2382 }
2384 BUG("Invalid csd_surface type");
2385 return (struct csd_data){0};
2386}
2388static void
2389csd_commit(struct terminal *term, struct wayl_surface *surf, struct buffer *buf)
2390{
2391 wayl_surface_scale(term->window, surf, buf, term->scale);
2392 wl_surface_attach(surf->surf, buf->wl_buf, 0, 0);
2393 wl_surface_damage_buffer(surf->surf, 0, 0, buf->width, buf->height);
2394 wl_surface_commit(surf->surf);
2395}
2397static void
2398render_csd_part(struct terminal *term,
2399 struct wl_surface *surf, struct buffer *buf,
2400 int width, int height, pixman_color_t *color)
2401{
2402 xassert(term->window->csd_mode == CSD_YES);
2404 pixman_image_fill_rectangles(
2405 PIXMAN_OP_SRC, buf->pix[0], color, 1,
2406 &(pixman_rectangle16_t){0, 0, buf->width, buf->height});
2407}
2409static void
2410render_osd(struct terminal *term, const struct wayl_sub_surface *sub_surf,
2411 struct fcft_font *font, struct buffer *buf,
2412 const char32_t *text, uint32_t _fg, uint32_t _bg,
2413 unsigned x)
2414{
2415 pixman_region32_t clip;
2416 pixman_region32_init_rect(&clip, 0, 0, buf->width, buf->height);
2417 pixman_image_set_clip_region32(buf->pix[0], &clip);
2418 pixman_region32_fini(&clip);
2420 const bool gamma_correct = wayl_do_linear_blending(term->wl, term->conf);
2421 uint16_t alpha = _bg >> 24 | (_bg >> 24 << 8);
2422 pixman_color_t bg = color_hex_to_pixman_with_alpha(_bg, alpha, gamma_correct);
2423 pixman_image_fill_rectangles(
2424 PIXMAN_OP_SRC, buf->pix[0], &bg, 1,
2425 &(pixman_rectangle16_t){0, 0, buf->width, buf->height});
2427 pixman_color_t fg = color_hex_to_pixman(_fg, gamma_correct);
2428 const int x_ofs = term->font_x_ofs;
2430 const size_t len = c32len(text);
2431 struct fcft_text_run *text_run = NULL;
2432 const struct fcft_glyph **glyphs = NULL;
2433 const struct fcft_glyph *_glyphs[len];
2434 size_t glyph_count = 0;
2436 if (fcft_capabilities() & FCFT_CAPABILITY_TEXT_RUN_SHAPING) {
2437 text_run = fcft_rasterize_text_run_utf32(
2438 font, len, (const char32_t *)text, term->font_subpixel);
2440 if (text_run != NULL) {
2441 glyphs = text_run->glyphs;
2442 glyph_count = text_run->count;
2443 }
2444 }
2446 if (glyphs == NULL) {
2447 for (size_t i = 0; i < len; i++) {
2448 const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(
2449 font, text[i], term->font_subpixel);
2451 if (glyph == NULL)
2452 continue;
2454 _glyphs[glyph_count++] = glyph;
2455 }
2457 glyphs = _glyphs;
2458 }
2460 pixman_image_t *src = pixman_image_create_solid_fill(&fg);
2462 /* Calculate baseline */
2463 unsigned y;
2464 {
2465 const int line_height = buf->height;
2466 const int font_height = max(font->height, font->ascent + font->descent);
2467 const int glyph_top_y = round((line_height - font_height) / 2.);
2468 y = term->font_y_ofs + glyph_top_y + font->ascent;
2469 }
2471 for (size_t i = 0; i < glyph_count; i++) {
2472 const struct fcft_glyph *glyph = glyphs[i];
2474 if (unlikely(glyph->is_color_glyph)) {
2475 pixman_image_composite32(
2476 PIXMAN_OP_OVER, glyph->pix, NULL, buf->pix[0], 0, 0, 0, 0,
2477 x + x_ofs + glyph->x, y - glyph->y,
2478 glyph->width, glyph->height);
2479 } else {
2480 pixman_image_composite32(
2481 PIXMAN_OP_OVER, src, glyph->pix, buf->pix[0], 0, 0, 0, 0,
2482 x + x_ofs + glyph->x, y - glyph->y,
2483 glyph->width, glyph->height);
2484 }
2486 x += glyph->advance.x;
2487 }
2489 fcft_text_run_destroy(text_run);
2490 pixman_image_unref(src);
2491 pixman_image_set_clip_region32(buf->pix[0], NULL);
2493 quirk_weston_subsurface_desync_on(sub_surf->sub);
2494 wayl_surface_scale(term->window, &sub_surf->surface, buf, term->scale);
2495 wl_surface_attach(sub_surf->surface.surf, buf->wl_buf, 0, 0);
2496 wl_surface_damage_buffer(sub_surf->surface.surf, 0, 0, buf->width, buf->height);
2498 if (alpha == 0xffff) {
2499 struct wl_region *region = wl_compositor_create_region(term->wl->compositor);
2500 if (region != NULL) {
2501 wl_region_add(region, 0, 0, buf->width, buf->height);
2502 wl_surface_set_opaque_region(sub_surf->surface.surf, region);
2503 wl_region_destroy(region);
2504 }
2505 } else
2506 wl_surface_set_opaque_region(sub_surf->surface.surf, NULL);
2508 wl_surface_commit(sub_surf->surface.surf);
2509 quirk_weston_subsurface_desync_off(sub_surf->sub);
2510}
2512static void
2513render_csd_title(struct terminal *term, const struct csd_data *info,
2514 struct buffer *buf)
2515{
2516 xassert(term->window->csd_mode == CSD_YES);
2518 struct wayl_sub_surface *surf = &term->window->csd.surface[CSD_SURF_TITLE];
2519 if (info->width == 0 || info->height == 0)
2520 return;
2522 uint32_t bg = term->conf->csd.color.title_set
2523 ? term->conf->csd.color.title
2524 : 0xffu << 24 | term->conf->colors_dark.fg;
2525 uint32_t fg = term->conf->csd.color.buttons_set
2526 ? term->conf->csd.color.buttons
2527 : term->conf->colors_dark.bg;
2529 if (!term->visual_focus) {
2530 bg = color_dim(term, bg);
2531 fg = color_dim(term, fg);
2532 }
2534 char32_t *_title_text = ambstoc32(term->window_title);
2535 const char32_t *title_text = _title_text != NULL ? _title_text : U"";
2537 struct wl_window *win = term->window;
2539 const struct fcft_glyph *M = fcft_rasterize_char_utf32(
2540 win->csd.font, U'M', term->font_subpixel);
2542 const int margin = M != NULL ? M->advance.x : win->csd.font->max_advance.x;
2544 render_osd(term, surf, win->csd.font, buf, title_text, fg, bg, margin);
2545 csd_commit(term, &surf->surface, buf);
2546 free(_title_text);
2547}
2549static void
2550render_csd_border(struct terminal *term, enum csd_surface surf_idx,
2551 const struct csd_data *info, struct buffer *buf)
2552{
2553 xassert(term->window->csd_mode == CSD_YES);
2554 xassert(surf_idx >= CSD_SURF_LEFT && surf_idx <= CSD_SURF_BOTTOM);
2556 struct wayl_surface *surf = &term->window->csd.surface[surf_idx].surface;
2558 if (info->width == 0 || info->height == 0)
2559 return;
2561 const bool gamma_correct = wayl_do_linear_blending(term->wl, term->conf);
2563 {
2564 /* Fully transparent - no need to do a color space transform */
2565 pixman_color_t color = color_hex_to_pixman_with_alpha(0, 0, gamma_correct);
2566 render_csd_part(term, surf->surf, buf, info->width, info->height, &color);
2567 }
2569 /*
2570 * The "visible" border.
2571 */
2573 float scale = term->scale;
2574 int bwidth = (int)roundf(term->conf->csd.border_width * scale);
2575 int vwidth = (int)roundf(term->conf->csd.border_width_visible * scale); /* Visible size */
2577 xassert(bwidth >= vwidth);
2579 if (vwidth > 0) {
2581 const struct config *conf = term->conf;
2582 int x = 0, y = 0, w = 0, h = 0;
2585 switch (surf_idx) {
2586 case CSD_SURF_TOP:
2587 case CSD_SURF_BOTTOM:
2588 x = bwidth - vwidth;
2589 y = surf_idx == CSD_SURF_TOP ? info->height - vwidth : 0;
2590 w = info->width - 2 * x;
2591 h = vwidth;
2592 break;
2594 case CSD_SURF_LEFT:
2595 case CSD_SURF_RIGHT:
2596 x = surf_idx == CSD_SURF_LEFT ? bwidth - vwidth : 0;
2597 y = 0;
2598 w = vwidth;
2599 h = info->height;
2600 break;
2602 case CSD_SURF_TITLE:
2603 case CSD_SURF_MINIMIZE:
2604 case CSD_SURF_MAXIMIZE:
2605 case CSD_SURF_CLOSE:
2606 case CSD_SURF_COUNT:
2607 BUG("unexpected CSD surface type");
2608 }
2610 xassert(x >= 0);
2611 xassert(y >= 0);
2612 xassert(w >= 0);
2613 xassert(h >= 0);
2615 xassert(x + w <= info->width);
2616 xassert(y + h <= info->height);
2618 uint32_t _color =
2619 conf->csd.color.border_set ? conf->csd.color.border :
2620 conf->csd.color.title_set ? conf->csd.color.title :
2621 0xffu << 24 | term->conf->colors_dark.fg;
2622 if (!term->visual_focus)
2623 _color = color_dim(term, _color);
2625 uint16_t alpha = _color >> 24 | (_color >> 24 << 8);
2626 pixman_color_t color =
2627 color_hex_to_pixman_with_alpha(_color, alpha, gamma_correct);
2629 pixman_image_fill_rectangles(
2630 PIXMAN_OP_SRC, buf->pix[0], &color, 1,
2631 &(pixman_rectangle16_t){x, y, w, h});
2632 }
2634 csd_commit(term, surf, buf);
2635}
2637static pixman_color_t
2638get_csd_button_fg_color(const struct terminal *term)
2639{
2640 const struct config *conf = term->conf;
2641 uint32_t _color = conf->colors_dark.bg;
2642 uint16_t alpha = 0xffff;
2644 if (conf->csd.color.buttons_set) {
2645 _color = conf->csd.color.buttons;
2646 alpha = _color >> 24 | (_color >> 24 << 8);
2647 }
2649 return color_hex_to_pixman_with_alpha(
2650 _color, alpha, wayl_do_linear_blending(term->wl, term->conf));
2651}
2653static void
2654render_csd_button_minimize(struct terminal *term, struct buffer *buf)
2655{
2656 pixman_color_t color = get_csd_button_fg_color(term);
2657 pixman_image_t *src = pixman_image_create_solid_fill(&color);
2659 const int max_height = buf->height / 3;
2660 const int max_width = buf->width / 3;
2662 int width = min(max_height, max_width);
2663 int thick = min(width / 2, 1 * term->scale);
2665 const int x_margin = (buf->width - width) / 2;
2666 const int y_margin = (buf->height - width) / 2;
2668 xassert(x_margin + width - thick >= 0);
2669 xassert(width - 2 * thick >= 0);
2670 xassert(y_margin + width - thick >= 0);
2671 pixman_image_fill_rectangles(
2672 PIXMAN_OP_SRC, buf->pix[0], &color, 1,
2673 (pixman_rectangle16_t[]) {
2674 {x_margin, y_margin + width - thick, width, thick}
2675 });
2677 pixman_image_unref(src);
2678}
2680static void
2681render_csd_button_maximize_maximized(
2682 struct terminal *term, struct buffer *buf)
2683{
2684 pixman_color_t color = get_csd_button_fg_color(term);
2685 pixman_image_t *src = pixman_image_create_solid_fill(&color);
2687 const int max_height = buf->height / 3;
2688 const int max_width = buf->width / 3;
2690 int width = min(max_height, max_width);
2691 int thick = min(width / 2, 1 * term->scale);
2693 const int x_margin = (buf->width - width) / 2;
2694 const int y_margin = (buf->height - width) / 2;
2695 const int shrink = 1;
2696 xassert(x_margin + width - thick >= 0);
2697 xassert(width - 2 * thick >= 0);
2698 xassert(y_margin + width - thick >= 0);
2700 pixman_image_fill_rectangles(
2701 PIXMAN_OP_SRC, buf->pix[0], &color, 4,
2702 (pixman_rectangle16_t[]){
2703 {x_margin + shrink, y_margin + shrink, width - 2 * shrink, thick},
2704 { x_margin + shrink, y_margin + thick, thick, width - 2 * thick - shrink },
2705 { x_margin + width - thick - shrink, y_margin + thick, thick, width - 2 * thick - shrink },
2706 { x_margin + shrink, y_margin + width - thick - shrink, width - 2 * shrink, thick }});
2708 pixman_image_unref(src);
2710}
2712static void
2713render_csd_button_maximize_window(
2714 struct terminal *term, struct buffer *buf)
2715{
2716 pixman_color_t color = get_csd_button_fg_color(term);
2717 pixman_image_t *src = pixman_image_create_solid_fill(&color);
2719 const int max_height = buf->height / 3;
2720 const int max_width = buf->width / 3;
2722 int width = min(max_height, max_width);
2723 int thick = min(width / 2, 1 * term->scale);
2725 const int x_margin = (buf->width - width) / 2;
2726 const int y_margin = (buf->height - width) / 2;
2728 xassert(x_margin + width - thick >= 0);
2729 xassert(width - 2 * thick >= 0);
2730 xassert(y_margin + width - thick >= 0);
2732 pixman_image_fill_rectangles(
2733 PIXMAN_OP_SRC, buf->pix[0], &color, 4,
2734 (pixman_rectangle16_t[]) {
2735 {x_margin, y_margin, width, thick},
2736 { x_margin, y_margin + thick, thick, width - 2 * thick },
2737 { x_margin + width - thick, y_margin + thick, thick, width - 2 * thick },
2738 { x_margin, y_margin + width - thick, width, thick }
2739 });
2741 pixman_image_unref(src);
2742}
2744static void
2745render_csd_button_maximize(struct terminal *term, struct buffer *buf)
2746{
2747 if (term->window->is_maximized)
2748 render_csd_button_maximize_maximized(term, buf);
2749 else
2750 render_csd_button_maximize_window(term, buf);
2751}
2753static void
2754render_csd_button_close(struct terminal *term, struct buffer *buf)
2755{
2756 pixman_color_t color = get_csd_button_fg_color(term);
2757 pixman_image_t *src = pixman_image_create_solid_fill(&color);
2759 const int max_height = buf->height / 3;
2760 const int max_width = buf->width / 3;
2762 int width = min(max_height, max_width);
2763 int thick = min(width / 2, 1 * term->scale);
2764 const int x_margin = (buf->width - width) / 2;
2765 const int y_margin = (buf->height - width) / 2;
2767 xassert(x_margin + width - thick >= 0);
2768 xassert(width - 2 * thick >= 0);
2769 xassert(y_margin + width - thick >= 0);
2771 pixman_triangle_t tri[4] = {
2772 {
2773 .p1 = {
2774 .x = pixman_int_to_fixed(x_margin),
2775 .y = pixman_int_to_fixed(y_margin + thick),
2776 },
2777 .p2 = {
2778 .x = pixman_int_to_fixed(x_margin + width - thick),
2779 .y = pixman_int_to_fixed(y_margin + width),
2780 },
2781 .p3 = {
2782 .x = pixman_int_to_fixed(x_margin + thick),
2783 .y = pixman_int_to_fixed(y_margin),
2784 },
2785 },
2787 {
2788 .p1 = {
2789 .x = pixman_int_to_fixed(x_margin + width),
2790 .y = pixman_int_to_fixed(y_margin + width - thick),
2791 },
2792 .p2 = {
2793 .x = pixman_int_to_fixed(x_margin + thick),
2794 .y = pixman_int_to_fixed(y_margin),
2795 },
2796 .p3 = {
2797 .x = pixman_int_to_fixed(x_margin + width - thick),
2798 .y = pixman_int_to_fixed(y_margin + width),
2799 },
2800 },
2802 {
2803 .p1 = {
2804 .x = pixman_int_to_fixed(x_margin),
2805 .y = pixman_int_to_fixed(y_margin + width - thick),
2806 },
2807 .p2 = {
2808 .x = pixman_int_to_fixed(x_margin + width),
2809 .y = pixman_int_to_fixed(y_margin + thick),
2810 },
2811 .p3 = {
2812 .x = pixman_int_to_fixed(x_margin + thick),
2813 .y = pixman_int_to_fixed(y_margin + width),
2814 },
2815 },
2817 {
2818 .p1 = {
2819 .x = pixman_int_to_fixed(x_margin + width),
2820 .y = pixman_int_to_fixed(y_margin + thick),
2821 },
2822 .p2 = {
2823 .x = pixman_int_to_fixed(x_margin),
2824 .y = pixman_int_to_fixed(y_margin + width - thick),
2825 },
2826 .p3 = {
2827 .x = pixman_int_to_fixed(x_margin + width - thick),
2828 .y = pixman_int_to_fixed(y_margin),
2829 },
2830 },
2831 };
2833 pixman_composite_triangles(
2834 PIXMAN_OP_OVER, src, buf->pix[0], PIXMAN_a1,
2835 0, 0, 0, 0, 4, tri);
2837 pixman_image_unref(src);
2838}
2840static bool
2841any_pointer_is_on_button(const struct terminal *term, enum csd_surface csd_surface)
2842{
2843 if (unlikely(tll_length(term->wl->seats) == 0))
2844 return false;
2846 tll_foreach(term->wl->seats, it) {
2847 const struct seat *seat = &it->item;
2849 if (seat->mouse.x < 0)
2850 continue;
2851 if (seat->mouse.y < 0)
2852 continue;
2854 struct csd_data info = get_csd_data(term, csd_surface);
2855 if (seat->mouse.x > info.width)
2856 continue;
2858 if (seat->mouse.y > info.height)
2859 continue;
2860 return true;
2861 }
2863 return false;
2864}
2866static void
2867render_csd_button(struct terminal *term, enum csd_surface surf_idx,
2868 const struct csd_data *info, struct buffer *buf)
2869{
2870 xassert(term->window->csd_mode == CSD_YES);
2871 xassert(surf_idx >= CSD_SURF_MINIMIZE && surf_idx <= CSD_SURF_CLOSE);
2873 struct wayl_surface *surf = &term->window->csd.surface[surf_idx].surface;
2875 if (info->width == 0 || info->height == 0)
2876 return;
2878 uint32_t _color;
2879 uint16_t alpha = 0xffff;
2880 bool is_active = false;
2881 bool is_set = false;
2882 const uint32_t *conf_color = NULL;
2884 switch (surf_idx) {
2885 case CSD_SURF_MINIMIZE:
2886 _color = term->conf->colors_dark.table[4]; /* blue */
2887 is_set = term->conf->csd.color.minimize_set;
2888 conf_color = &term->conf->csd.color.minimize;
2889 is_active = term->active_surface == TERM_SURF_BUTTON_MINIMIZE &&
2890 any_pointer_is_on_button(term, CSD_SURF_MINIMIZE);
2891 break;
2893 case CSD_SURF_MAXIMIZE:
2894 _color = term->conf->colors_dark.table[2]; /* green */
2895 is_set = term->conf->csd.color.maximize_set;
2896 conf_color = &term->conf->csd.color.maximize;
2897 is_active = term->active_surface == TERM_SURF_BUTTON_MAXIMIZE &&
2898 any_pointer_is_on_button(term, CSD_SURF_MAXIMIZE);
2899 break;
2901 case CSD_SURF_CLOSE:
2902 _color = term->conf->colors_dark.table[1]; /* red */
2903 is_set = term->conf->csd.color.close_set;
2904 conf_color = &term->conf->csd.color.quit;
2905 is_active = term->active_surface == TERM_SURF_BUTTON_CLOSE &&
2906 any_pointer_is_on_button(term, CSD_SURF_CLOSE);
2907 break;
2909 default:
2910 BUG("unhandled surface type: %u", (unsigned)surf_idx);
2911 break;
2912 }
2914 if (is_active) {
2915 if (is_set) {
2916 _color = *conf_color;
2917 alpha = _color >> 24 | (_color >> 24 << 8);
2918 }
2919 } else {
2920 _color = 0;
2921 alpha = 0;
2922 }
2924 if (!term->visual_focus)
2925 _color = color_dim(term, _color);
2927 const bool gamma_correct = wayl_do_linear_blending(term->wl, term->conf);
2928 pixman_color_t color = color_hex_to_pixman_with_alpha(_color, alpha, gamma_correct);
2929 render_csd_part(term, surf->surf, buf, info->width, info->height, &color);
2931 switch (surf_idx) {
2932 case CSD_SURF_MINIMIZE: render_csd_button_minimize(term, buf); break;
2933 case CSD_SURF_MAXIMIZE: render_csd_button_maximize(term, buf); break;
2934 case CSD_SURF_CLOSE: render_csd_button_close(term, buf); break;
2936 default:
2937 BUG("unhandled surface type: %u", (unsigned)surf_idx);
2938 break;
2939 }
2941 csd_commit(term, surf, buf);
2942}
2944static void
2945render_csd(struct terminal *term)
2946{
2947 xassert(term->window->csd_mode == CSD_YES);
2949 if (term->window->is_fullscreen)
2950 return;
2952 const float scale = term->scale;
2953 struct csd_data infos[CSD_SURF_COUNT];
2954 int widths[CSD_SURF_COUNT];
2955 int heights[CSD_SURF_COUNT];
2957 for (size_t i = 0; i < CSD_SURF_COUNT; i++) {
2958 infos[i] = get_csd_data(term, i);
2959 const int x = infos[i].x;
2960 const int y = infos[i].y;
2961 const int width = infos[i].width;
2962 const int height = infos[i].height;
2964 struct wl_surface *surf = term->window->csd.surface[i].surface.surf;
2965 struct wl_subsurface *sub = term->window->csd.surface[i].sub;
2967 xassert(surf != NULL);
2968 xassert(sub != NULL);
2970 if (width == 0 || height == 0) {
2971 widths[i] = heights[i] = 0;
2972 wl_subsurface_set_position(sub, 0, 0);
2973 wl_surface_attach(surf, NULL, 0, 0);
2974 wl_surface_commit(surf);
2975 continue;
2976 }
2978 widths[i] = width;
2979 heights[i] = height;
2980 wl_subsurface_set_position(sub, roundf(x / scale), roundf(y / scale));
2981 }
2983 struct buffer *bufs[CSD_SURF_COUNT];
2984 shm_get_many(term->render.chains.csd, CSD_SURF_COUNT, widths, heights, bufs);
2986 for (size_t i = CSD_SURF_LEFT; i <= CSD_SURF_BOTTOM; i++)
2987 render_csd_border(term, i, &infos[i], bufs[i]);
2988 for (size_t i = CSD_SURF_MINIMIZE; i <= CSD_SURF_CLOSE; i++)
2989 render_csd_button(term, i, &infos[i], bufs[i]);
2990 render_csd_title(term, &infos[CSD_SURF_TITLE], bufs[CSD_SURF_TITLE]);
2991}
2993static void
2994render_scrollback_position(struct terminal *term)
2995{
2996 if (term->conf->scrollback.indicator.position == SCROLLBACK_INDICATOR_POSITION_NONE)
2997 return;
2999 struct wl_window *win = term->window;
3001 if (term->grid->view == term->grid->offset) {
3002 if (win->scrollback_indicator.surface.surf != NULL)
3003 wayl_win_subsurface_destroy(&win->scrollback_indicator);
3004 return;
3005 }
3007 if (win->scrollback_indicator.surface.surf == NULL) {
3008 if (!wayl_win_subsurface_new(
3009 win, &win->scrollback_indicator, false))
3010 {
3011 LOG_ERR("failed to create scrollback indicator surface");
3012 return;
3013 }
3014 }
3016 xassert(win->scrollback_indicator.surface.surf != NULL);
3017 xassert(win->scrollback_indicator.sub != NULL);
3019 /* Find absolute row number of the scrollback start */
3020 int scrollback_start = term->grid->offset + term->rows;
3021 int empty_rows = 0;
3022 while (term->grid->rows[scrollback_start & (term->grid->num_rows - 1)] == NULL) {
3023 scrollback_start++;
3024 empty_rows++;
3025 }
3027 /* Rebase viewport against scrollback start (so that 0 is at
3028 * the beginning of the scrollback) */
3029 int rebased_view = term->grid->view - scrollback_start + term->grid->num_rows;
3030 rebased_view &= term->grid->num_rows - 1;
3032 /* How much of the scrollback is actually used? */
3033 int populated_rows = term->grid->num_rows - empty_rows;
3034 xassert(populated_rows > 0);
3035 xassert(populated_rows <= term->grid->num_rows);
3037 /*
3038 * How far down in the scrollback we are.
3039 *
3040 * 0% -> at the beginning of the scrollback
3041 * 100% -> at the bottom, i.e. where new lines are inserted
3042 */
3043 double percent =
3044 rebased_view + term->rows == populated_rows
3045 ? 1.0
3046 : (double)rebased_view / (populated_rows - term->rows);
3048 char32_t _text[64];
3049 const char32_t *text = _text;
3050 int cell_count = 0;
3052 /* *What* to render */
3053 switch (term->conf->scrollback.indicator.format) {
3054 case SCROLLBACK_INDICATOR_FORMAT_PERCENTAGE: {
3055 char percent_str[8];
3056 snprintf(percent_str, sizeof(percent_str), "%u%%", (int)(100 * percent));
3057 mbstoc32(_text, percent_str, ALEN(_text));
3058 cell_count = 3;
3059 break;
3060 }
3062 case SCROLLBACK_INDICATOR_FORMAT_LINENO: {
3063 char lineno_str[64];
3064 snprintf(lineno_str, sizeof(lineno_str), "%d", rebased_view + 1);
3065 mbstoc32(_text, lineno_str, ALEN(_text));
3066 cell_count = (int)ceilf(log10f(term->grid->num_rows));
3067 break;
3068 }
3070 case SCROLLBACK_INDICATOR_FORMAT_TEXT:
3071 text = term->conf->scrollback.indicator.text;
3072 cell_count = c32len(text);
3073 break;
3074 }
3076 const float scale = term->scale;
3077 const int margin = (int)roundf(3. * scale);
3079 int width = margin + cell_count * term->cell_width + margin;
3080 int height = margin + term->cell_height + margin;
3082 width = roundf(scale * ceilf(width / scale));
3083 height = roundf(scale * ceilf(height / scale));
3085 /* *Where* to render - parent relative coordinates */
3086 int surf_top = 0;
3087 switch (term->conf->scrollback.indicator.position) {
3088 case SCROLLBACK_INDICATOR_POSITION_NONE:
3089 BUG("Invalid scrollback indicator position type");
3090 return;
3092 case SCROLLBACK_INDICATOR_POSITION_FIXED:
3093 surf_top = term->cell_height - margin;
3094 break;
3096 case SCROLLBACK_INDICATOR_POSITION_RELATIVE: {
3097 int lines = term->rows - 2; /* Avoid using first and last rows */
3098 if (term->is_searching) {
3099 /* Make sure we don't collide with the scrollback search box */
3100 lines--;
3101 }
3103 lines = max(lines, 0);
3105 int pixels = max(lines * term->cell_height - height + 2 * margin, 0);
3106 surf_top = term->cell_height - margin + (int)(percent * pixels);
3107 break;
3108 }
3109 }
3111 int x = term->width - margin - width;
3112 int y = term->margins.top + surf_top;
3114 x = roundf(scale * ceilf(x / scale));
3115 y = roundf(scale * ceilf(y / scale));
3117 if (y + height > term->height) {
3118 wl_surface_attach(win->scrollback_indicator.surface.surf, NULL, 0, 0);
3119 wl_surface_commit(win->scrollback_indicator.surface.surf);
3120 return;
3121 }
3123 struct buffer_chain *chain = term->render.chains.scrollback_indicator;
3124 struct buffer *buf = shm_get_buffer(chain, width, height);
3126 wl_subsurface_set_position(
3127 win->scrollback_indicator.sub, roundf(x / scale), roundf(y / scale));
3129 uint32_t fg = term->colors.table[0];
3130 uint32_t bg = term->colors.table[8 + 4];
3131 if (term->conf->colors_dark.use_custom.scrollback_indicator) {
3132 fg = term->conf->colors_dark.scrollback_indicator.fg;
3133 bg = term->conf->colors_dark.scrollback_indicator.bg;
3134 }
3136 render_osd(
3137 term,
3138 &win->scrollback_indicator,
3139 term->fonts[0], buf, text,
3140 fg, 0xffu << 24 | bg,
3141 width - margin - c32len(text) * term->cell_width);
3142}
3144static void
3145render_render_timer(struct terminal *term, struct timespec render_time)
3146{
3147 struct wl_window *win = term->window;
3149 char usecs_str[256];
3150 double usecs = render_time.tv_sec * 1000000 + render_time.tv_nsec / 1000.0;
3151 snprintf(usecs_str, sizeof(usecs_str), "%.2f µs", usecs);
3153 char32_t text[256];
3154 mbstoc32(text, usecs_str, ALEN(text));
3156 const float scale = term->scale;
3157 const int cell_count = c32len(text);
3158 const int margin = (int)roundf(3. * scale);
3160 int width = margin + cell_count * term->cell_width + margin;
3161 int height = margin + term->cell_height + margin;
3163 width = roundf(scale * ceilf(width / scale));
3164 height = roundf(scale * ceilf(height / scale));
3166 struct buffer_chain *chain = term->render.chains.render_timer;
3167 struct buffer *buf = shm_get_buffer(chain, width, height);
3169 wl_subsurface_set_position(
3170 win->render_timer.sub,
3171 roundf(margin / scale),
3172 roundf((term->margins.top + term->cell_height - margin) / scale));
3174 render_osd(
3175 term,
3176 &win->render_timer,
3177 term->fonts[0], buf, text,
3178 term->colors.table[0], 0xffu << 24 | term->colors.table[8 + 1],
3179 margin);
3180}
3182static void frame_callback(
3183 void *data, struct wl_callback *wl_callback, uint32_t callback_data);
3185static const struct wl_callback_listener frame_listener = {
3186 .done = &frame_callback,
3187};
3189static void
3190force_full_repaint(struct terminal *term, struct buffer *buf)
3191{
3192 tll_free(term->grid->scroll_damage);
3193 render_margin(term, buf, 0, term->rows, true);
3194 term_damage_view(term);
3195}
3197static void
3198reapply_old_damage(struct terminal *term, struct buffer *new, struct buffer *old)
3199{
3200 if (new->age > 1) {
3201 memcpy(new->data, old->data, new->height * new->stride);
3202 return;
3203 }
3205 pixman_region32_t dirty;
3206 pixman_region32_init(&dirty);
3208 /*
3209 * Figure out current frame's damage region
3210 *
3211 * If current frame doesn't have any scroll damage, we can simply
3212 * subtract this frame's damage from the last frame's damage. That
3213 * way, we don't have to copy areas from the old frame that'll
3214 * just get overwritten by current frame.
3215 *
3216 * Note that this is row based. A "half damaged" row is not
3217 * excluded. I.e. the entire row will be copied from the old frame
3218 * to the new, and then when actually rendering the new frame, the
3219 * updated cells will overwrite parts of the copied row.
3220 *
3221 * Since we're scanning the entire viewport anyway, we also track
3222 * whether *all* cells are to be updated. In this case, just force
3223 * a full re-rendering, and don't copy anything from the old
3224 * frame.
3225 */
3226 bool full_repaint_needed = true;
3228 for (int r = 0; r < term->rows; r++) {
3229 const struct row *row = grid_row_in_view(term->grid, r);
3231 if (!row->dirty) {
3232 full_repaint_needed = false;
3233 continue;
3234 }
3236 bool row_all_dirty = true;
3237 for (int c = 0; c < term->cols; c++) {
3238 if (row->cells[c].attrs.clean) {
3239 row_all_dirty = false;
3240 full_repaint_needed = false;
3241 break;
3242 }
3243 }
3245 if (row_all_dirty) {
3246 pixman_region32_union_rect(
3247 &dirty, &dirty,
3248 term->margins.left,
3249 term->margins.top + r * term->cell_height,
3250 term->width - term->margins.left - term->margins.right,
3251 term->cell_height);
3252 }
3253 }
3255 if (full_repaint_needed) {
3256 force_full_repaint(term, new);
3257 return;
3258 }
3260 /*
3261 * TODO: re-apply last frame's scroll damage
3262 *
3263 * We used to do this, but it turned out to be buggy. If we decide
3264 * to re-add it, this is where to do it. Note that we'd also have
3265 * to remove the updates to buf->dirty from grid_render_scroll()
3266 * and grid_render_scroll_reverse().
3267 */
3269 if (tll_length(term->grid->scroll_damage) == 0) {
3270 /*
3271 * We can only subtract current frame's damage from the old
3272 * frame's if we don't have any scroll damage.
3273 *
3274 * If we do have scroll damage, the damage region we
3275 * calculated above is not (yet) valid - we need to apply the
3276 * current frame's scroll damage *first*. This is done later,
3277 * when rendering the frame.
3278 */
3279 pixman_region32_subtract(&dirty, &old->dirty[0], &dirty);
3280 pixman_image_set_clip_region32(new->pix[0], &dirty);
3281 } else {
3282 /* Copy *all* of last frame's damaged areas */
3283 pixman_image_set_clip_region32(new->pix[0], &old->dirty[0]);
3284 }
3286 pixman_image_composite32(
3287 PIXMAN_OP_SRC, old->pix[0], NULL, new->pix[0],
3288 0, 0, 0, 0, 0, 0, term->width, term->height);
3290 pixman_image_set_clip_region32(new->pix[0], NULL);
3291 pixman_region32_fini(&dirty);
3292}
3294static void
3295dirty_old_cursor(struct terminal *term)
3296{
3297 if (term->render.last_cursor.row != NULL && !term->render.last_cursor.hidden) {
3298 struct row *row = term->render.last_cursor.row;
3299 struct cell *cell = &row->cells[term->render.last_cursor.col];
3300 cell->attrs.clean = 0;
3301 row->dirty = true;
3302 }
3304 /* Remember current cursor position, for the next frame */
3305 term->render.last_cursor.row = grid_row(term->grid, term->grid->cursor.point.row);
3306 term->render.last_cursor.col = term->grid->cursor.point.col;
3307 term->render.last_cursor.hidden = term->hide_cursor;
3308}
3310static void
3311dirty_cursor(struct terminal *term)
3312{
3313 if (term->hide_cursor)
3314 return;
3316 const struct coord *cursor = &term->grid->cursor.point;
3318 struct row *row = grid_row(term->grid, cursor->row);
3319 struct cell *cell = &row->cells[cursor->col];
3320 cell->attrs.clean = 0;
3321 row->dirty = true;
3322}
3324static void
3325grid_render(struct terminal *term)
3326{
3327 if (term->shutdown.in_progress)
3328 return;
3330 struct timespec start_time;
3331 struct timespec start_wait_preapply = {0}, stop_wait_preapply = {0};
3332 struct timespec start_double_buffering = {0}, stop_double_buffering = {0};
3334 /* Might be a thread doing pre-applied damage */
3335 if (unlikely(term->render.preapply_last_frame_damage &&
3336 term->render.workers.preapplied_damage.buf != NULL))
3337 {
3338 clock_gettime(CLOCK_MONOTONIC, &start_wait_preapply);
3339 render_wait_for_preapply_damage(term);
3340 clock_gettime(CLOCK_MONOTONIC, &stop_wait_preapply);
3341 }
3343 if (term->conf->tweak.render_timer != RENDER_TIMER_NONE)
3344 clock_gettime(CLOCK_MONOTONIC, &start_time);
3346 xassert(term->width > 0);
3347 xassert(term->height > 0);
3349 struct buffer_chain *chain = term->render.chains.grid;
3350 struct buffer *buf = shm_get_buffer(chain, term->width, term->height);
3352 /* Dirty old and current cursor cell, to ensure they're repainted */
3353 dirty_old_cursor(term);
3354 dirty_cursor(term);
3356 LOG_DBG("buffer age: %u (%p)", buf->age, (void *)buf);
3358 if (term->render.last_buf == NULL ||
3359 term->render.last_buf->width != buf->width ||
3360 term->render.last_buf->height != buf->height ||
3361 term->render.margins)
3362 {
3363 force_full_repaint(term, buf);
3364 }
3366 else if (buf->age > 0) {
3367 LOG_DBG("buffer age: %u (%p)", buf->age, (void *)buf);
3369 xassert(term->render.last_buf != NULL);
3370 xassert(term->render.last_buf != buf);
3371 xassert(term->render.last_buf->width == buf->width);
3372 xassert(term->render.last_buf->height == buf->height);
3374 if (++term->render.frames_since_last_immediate_release > 10) {
3375 static bool have_warned = false;
3377 if (!term->render.preapply_last_frame_damage &&
3378 term->conf->tweak.preapply_damage &&
3379 term->render.workers.count > 0)
3380 {
3381 LOG_INFO("enabling pre-applied frame damage");
3382 term->render.preapply_last_frame_damage = true;
3383 } else if (!have_warned && !term->render.preapply_last_frame_damage) {
3384 LOG_WARN("compositor is not releasing buffers immediately; "
3385 "expect lower rendering performance");
3386 have_warned = true;
3387 }
3388 }
3390 clock_gettime(CLOCK_MONOTONIC, &start_double_buffering);
3391 reapply_old_damage(term, buf, term->render.last_buf);
3392 clock_gettime(CLOCK_MONOTONIC, &stop_double_buffering);
3393 } else if (!term->render.preapply_last_frame_damage) {
3394 term->render.frames_since_last_immediate_release = 0;
3395 }
3397 if (term->render.last_buf != NULL) {
3398 shm_unref(term->render.last_buf);
3399 term->render.last_buf = NULL;
3400 }
3402 term->render.last_buf = buf;
3403 shm_addref(buf);
3404 buf->age = 0;
3407 tll_foreach(term->grid->scroll_damage, it) {
3408 switch (it->item.type) {
3409 case DAMAGE_SCROLL:
3410 if (term->grid->view == term->grid->offset)
3411 grid_render_scroll(term, buf, &it->item);
3412 break;
3414 case DAMAGE_SCROLL_REVERSE:
3415 if (term->grid->view == term->grid->offset)
3416 grid_render_scroll_reverse(term, buf, &it->item);
3417 break;
3419 case DAMAGE_SCROLL_IN_VIEW:
3420 grid_render_scroll(term, buf, &it->item);
3421 break;
3423 case DAMAGE_SCROLL_REVERSE_IN_VIEW:
3424 grid_render_scroll_reverse(term, buf, &it->item);
3425 break;
3426 }
3428 tll_remove(term->grid->scroll_damage, it);
3429 }
3431 /*
3432 * Ensure selected cells have their 'selected' bit set. This is
3433 * normally "automatically" true - the bit is set when the
3434 * selection is made.
3435 *
3436 * However, if the cell is updated (printed to) while the
3437 * selection is active, the 'selected' bit is cleared. Checking
3438 * for this and re-setting the bit in term_print() is too
3439 * expensive performance wise.
3440 *
3441 * Instead, we synchronize the selection bits here and now. This
3442 * makes the performance impact linear to the number of selected
3443 * cells rather than to the number of updated cells.
3444 *
3445 * (note that selection_dirty_cells() will not set the dirty flag
3446 * on cells where the 'selected' bit is already set)
3447 */
3448 selection_dirty_cells(term);
3450 /* Translate offset-relative row to view-relative, unless cursor
3451 * is hidden, then we just set it to -1 */
3452 struct coord cursor = {-1, -1};
3453 if (!term->hide_cursor) {
3454 cursor = term->grid->cursor.point;
3455 cursor.row += term->grid->offset;
3456 cursor.row -= term->grid->view;
3457 cursor.row &= term->grid->num_rows - 1;
3458 }
3460 if (term->conf->tweak.overflowing_glyphs) {
3461 /*
3462 * Pre-pass to dirty cells affected by overflowing glyphs.
3463 *
3464 * Given any two pair of cells where the first cell is
3465 * overflowing into the second, *both* cells must be
3466 * re-rendered if any one of them is dirty.
3467 *
3468 * Thus, given a string of overflowing glyphs, with a single
3469 * dirty cell in the middle, we need to re-render the entire
3470 * string.
3471 */
3472 for (int r = 0; r < term->rows; r++) {
3473 struct row *row = grid_row_in_view(term->grid, r);
3475 if (!row->dirty)
3476 continue;
3478 /* Loop row from left to right, looking for dirty cells */
3479 for (struct cell *cell = &row->cells[0];
3480 cell < &row->cells[term->cols];
3481 cell++)
3482 {
3483 if (cell->attrs.clean)
3484 continue;
3486 /*
3487 * Cell is dirty, go back and dirty previous cells, if
3488 * they are overflowing.
3489 *
3490 * As soon as we see a non-overflowing cell we can
3491 * stop, since it isn't affecting the string of
3492 * overflowing glyphs that follows it.
3493 *
3494 * As soon as we see a dirty cell, we can stop, since
3495 * that means we've already handled it (remember the
3496 * outer loop goes from left to right).
3497 */
3498 for (struct cell *c = cell - 1; c >= &row->cells[0]; c--) {
3499 if (c->attrs.confined)
3500 break;
3501 if (!c->attrs.clean)
3502 break;
3503 c->attrs.clean = false;
3504 }
3506 /*
3507 * Now move forward, dirtying all cells until we hit a
3508 * non-overflowing cell.
3509 *
3510 * Note that the first non-overflowing cell must be
3511 * re-rendered as well, but any cell *after* that is
3512 * unaffected by the string of overflowing glyphs
3513 * we're dealing with right now.
3514 *
3515 * For performance, this iterates the *outer* loop's
3516 * cell pointer - no point in re-checking all these
3517 * glyphs again, in the outer loop.
3518 */
3519 for (; cell < &row->cells[term->cols]; cell++) {
3520 cell->attrs.clean = false;
3521 if (cell->attrs.confined)
3522 break;
3523 }
3524 }
3525 }
3526 }
3528#if defined(_DEBUG)
3529 for (int r = 0; r < term->rows; r++) {
3530 const struct row *row = grid_row_in_view(term->grid, r);
3532 if (row->dirty) {
3533 bool all_clean = true;
3534 for (int c = 0; c < term->cols; c++) {
3535 if (!row->cells[c].attrs.clean) {
3536 all_clean = false;
3537 break;
3538 }
3539 }
3540 if (all_clean)
3541 BUG("row #%d is dirty, but all cells are marked as clean", r);
3542 } else {
3543 for (int c = 0; c < term->cols; c++) {
3544 if (!row->cells[c].attrs.clean)
3545 BUG("row #%d is clean, but cell #%d is dirty", r, c);
3546 }
3547 }
3548 }
3549#endif
3551 pixman_region32_t damage;
3552 pixman_region32_init(&damage);
3554 render_sixel_images(term, buf->pix[0], &damage, &cursor);
3557 if (term->render.workers.count > 0) {
3558 mtx_lock(&term->render.workers.lock);
3559 term->render.workers.buf = buf;
3560 for (size_t i = 0; i < term->render.workers.count; i++)
3561 sem_post(&term->render.workers.start);
3563 xassert(tll_length(term->render.workers.queue) == 0);
3564 }
3566 for (int r = 0; r < term->rows; r++) {
3567 struct row *row = grid_row_in_view(term->grid, r);
3569 if (!row->dirty)
3570 continue;
3572 row->dirty = false;
3574 if (term->render.workers.count > 0)
3575 tll_push_back(term->render.workers.queue, r);
3577 else {
3578 /* TODO: damage region */
3579 int cursor_col = cursor.row == r ? cursor.col : -1;
3580 render_row(term, buf->pix[0], &damage, row, r, cursor_col);
3581 }
3582 }
3584 /* Signal workers the frame is done */
3585 if (term->render.workers.count > 0) {
3586 for (size_t i = 0; i < term->render.workers.count; i++)
3587 tll_push_back(term->render.workers.queue, -1);
3588 mtx_unlock(&term->render.workers.lock);
3590 for (size_t i = 0; i < term->render.workers.count; i++)
3591 sem_wait(&term->render.workers.done);
3592 term->render.workers.buf = NULL;
3593 }
3595 for (size_t i = 0; i < term->render.workers.count; i++)
3596 pixman_region32_union(&damage, &damage, &buf->dirty[i + 1]);
3598 pixman_region32_union(&buf->dirty[0], &buf->dirty[0], &damage);
3600 {
3601 int box_count = 0;
3602 pixman_box32_t *boxes = pixman_region32_rectangles(&damage, &box_count);
3604 for (size_t i = 0; i < box_count; i++) {
3605 wl_surface_damage_buffer(
3606 term->window->surface.surf,
3607 boxes[i].x1, boxes[i].y1,
3608 boxes[i].x2 - boxes[i].x1, boxes[i].y2 - boxes[i].y1);
3609 }
3610 }
3612 pixman_region32_fini(&damage);
3614 render_overlay(term);
3615 render_ime_preedit(term, buf);
3616 render_scrollback_position(term);
3618 if (term->conf->tweak.render_timer != RENDER_TIMER_NONE) {
3619 struct timespec end_time;
3620 clock_gettime(CLOCK_MONOTONIC, &end_time);
3622 struct timespec wait_time;
3623 timespec_sub(&stop_wait_preapply, &start_wait_preapply, &wait_time);
3625 struct timespec render_time;
3626 timespec_sub(&end_time, &start_time, &render_time);
3628 struct timespec double_buffering_time;
3629 timespec_sub(&stop_double_buffering, &start_double_buffering, &double_buffering_time);
3631 struct timespec preapply_damage;
3632 timespec_sub(&term->render.workers.preapplied_damage.stop,
3633 &term->render.workers.preapplied_damage.start,
3634 &preapply_damage);
3636 struct timespec total_render_time;
3637 timespec_add(&render_time, &double_buffering_time, &total_render_time);
3638 timespec_add(&wait_time, &total_render_time, &total_render_time);
3640 switch (term->conf->tweak.render_timer) {
3641 case RENDER_TIMER_LOG:
3642 case RENDER_TIMER_BOTH:
3643 LOG_INFO(
3644 "frame rendered in %lds %9ldns "
3645 "(%lds %9ldns wait, %lds %9ldns rendering, %lds %9ldns double buffering) not included: %lds %ldns pre-apply damage",
3646 (long)total_render_time.tv_sec,
3647 total_render_time.tv_nsec,
3648 (long)wait_time.tv_sec,
3649 wait_time.tv_nsec,
3650 (long)render_time.tv_sec,
3651 render_time.tv_nsec,
3652 (long)double_buffering_time.tv_sec,
3653 double_buffering_time.tv_nsec,
3654 (long)preapply_damage.tv_sec,
3655 preapply_damage.tv_nsec);
3656 break;
3658 case RENDER_TIMER_OSD:
3659 case RENDER_TIMER_NONE:
3660 break;
3661 }
3663 switch (term->conf->tweak.render_timer) {
3664 case RENDER_TIMER_OSD:
3665 case RENDER_TIMER_BOTH:
3666 render_render_timer(term, total_render_time);
3667 break;
3669 case RENDER_TIMER_LOG:
3670 case RENDER_TIMER_NONE:
3671 break;
3672 }
3673 }
3675 xassert(term->grid->offset >= 0 && term->grid->offset < term->grid->num_rows);
3676 xassert(term->grid->view >= 0 && term->grid->view < term->grid->num_rows);
3678 xassert(term->window->frame_callback == NULL);
3679 term->window->frame_callback = wl_surface_frame(term->window->surface.surf);
3680 wl_callback_add_listener(term->window->frame_callback, &frame_listener, term);
3682 wayl_win_scale(term->window, buf);
3684 if (term->wl->presentation != NULL && term->conf->presentation_timings) {
3685 struct timespec commit_time;
3686 clock_gettime(term->wl->presentation_clock_id, &commit_time);
3688 struct wp_presentation_feedback *feedback = wp_presentation_feedback(
3689 term->wl->presentation, term->window->surface.surf);
3691 if (feedback == NULL) {
3692 LOG_WARN("failed to create presentation feedback");
3693 } else {
3694 struct presentation_context *ctx = xmalloc(sizeof(*ctx));
3695 *ctx = (struct presentation_context){
3696 .term = term,
3697 .input.tv_sec = term->render.input_time.tv_sec,
3698 .input.tv_usec = term->render.input_time.tv_nsec / 1000,
3699 .commit.tv_sec = commit_time.tv_sec,
3700 .commit.tv_usec = commit_time.tv_nsec / 1000,
3701 };
3703 wp_presentation_feedback_add_listener(
3704 feedback, &presentation_feedback_listener, ctx);
3706 term->render.input_time.tv_sec = 0;
3707 term->render.input_time.tv_nsec = 0;
3708 }
3709 }
3711 if (term->conf->tweak.damage_whole_window) {
3712 wl_surface_damage_buffer(
3713 term->window->surface.surf, 0, 0, INT32_MAX, INT32_MAX);
3714 }
3716 wl_surface_attach(term->window->surface.surf, buf->wl_buf, 0, 0);
3717 wl_surface_commit(term->window->surface.surf);
3718}
3720static void
3721render_search_box(struct terminal *term)
3722{
3723 xassert(term->window->search.sub != NULL);
3725 /*
3726 * We treat the search box pretty much like a row of cells. That
3727 * is, a glyph is either 1 or 2 (or more) "cells" wide.
3728 *
3729 * The search 'length', and 'cursor' (position) is in
3730 * *characters*, not cells. This means we need to translate from
3731 * character count to cell count when calculating the length of
3732 * the search box, where in the search string we should start
3733 * rendering etc.
3734 */
3736#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
3737 /* TODO: do we want to/need to handle multi-seat? */
3738 struct seat *ime_seat = NULL;
3739 tll_foreach(term->wl->seats, it) {
3740 if (it->item.kbd_focus == term) {
3741 ime_seat = &it->item;
3742 break;
3743 }
3744 }
3746 size_t text_len = term->search.len;
3747 if (ime_seat != NULL && ime_seat->ime.preedit.text != NULL)
3748 text_len += c32len(ime_seat->ime.preedit.text);
3750 char32_t *text = xmalloc((text_len + 1) * sizeof(char32_t));
3751 text[0] = U'\0';
3753 /* Copy everything up to the cursor */
3754 c32ncpy(text, term->search.buf, term->search.cursor);
3755 text[term->search.cursor] = U'\0';
3757 /* Insert pre-edit text at cursor */
3758 if (ime_seat != NULL && ime_seat->ime.preedit.text != NULL)
3759 c32cat(text, ime_seat->ime.preedit.text);
3761 /* And finally everything after the cursor */
3762 c32ncat(text, &term->search.buf[term->search.cursor],
3763 term->search.len - term->search.cursor);
3764#else
3765 const char32_t *text = term->search.buf;
3766 const size_t text_len = term->search.len;
3767#endif
3769 /* Calculate the width of each character */
3770 int widths[text_len + 1];
3771 for (size_t i = 0; i < text_len; i++)
3772 widths[i] = max(0, c32width(text[i]));
3773 widths[text_len] = 0;
3775 const size_t total_cells = c32swidth(text, text_len);
3776 const size_t wanted_visible_cells = max(20, total_cells);
3778 const float scale = term->scale;
3779 xassert(scale >= 1.);
3780 const size_t margin = (size_t)roundf(3 * scale);
3782 size_t width = term->width - 2 * margin;
3783 size_t height = min(
3784 term->height - 2 * margin,
3785 margin + 1 * term->cell_height + margin);
3787 width = roundf(scale * ceilf((term->width - 2 * margin) / scale));
3788 height = roundf(scale * ceilf(height / scale));
3790 size_t visible_width = min(
3791 term->width - 2 * margin,
3792 margin + wanted_visible_cells * term->cell_width + margin);
3794 const size_t visible_cells = (visible_width - 2 * margin) / term->cell_width;
3795 size_t glyph_offset = term->render.search_glyph_offset;
3797 struct buffer_chain *chain = term->render.chains.search;
3798 struct buffer *buf = shm_get_buffer(chain, width, height);
3800 pixman_region32_t clip;
3801 pixman_region32_init_rect(&clip, 0, 0, width, height);
3802 pixman_image_set_clip_region32(buf->pix[0], &clip);
3803 pixman_region32_fini(&clip);
3805#define WINDOW_X(x) (margin + x)
3806#define WINDOW_Y(y) (term->height - margin - height + y)
3808 const bool is_match = term->search.match_len == text_len;
3809 const bool custom_colors = is_match
3810 ? term->conf->colors_dark.use_custom.search_box_match
3811 : term->conf->colors_dark.use_custom.search_box_no_match;
3813 /* Background - yellow on empty/match, red on mismatch (default) */
3814 const bool gamma_correct = wayl_do_linear_blending(term->wl, term->conf);
3815 const pixman_color_t color = color_hex_to_pixman(
3816 is_match
3817 ? (custom_colors
3818 ? term->conf->colors_dark.search_box.match.bg
3819 : term->colors.table[3])
3820 : (custom_colors
3821 ? term->conf->colors_dark.search_box.no_match.bg
3822 : term->colors.table[1]),
3823 gamma_correct);
3825 pixman_image_fill_rectangles(
3826 PIXMAN_OP_SRC, buf->pix[0], &color,
3827 1, &(pixman_rectangle16_t){width - visible_width, 0, visible_width, height});
3829 pixman_color_t transparent = color_hex_to_pixman_with_alpha(0, 0, gamma_correct);
3830 pixman_image_fill_rectangles(
3831 PIXMAN_OP_SRC, buf->pix[0], &transparent,
3832 1, &(pixman_rectangle16_t){0, 0, width - visible_width, height});
3834 struct fcft_font *font = term->fonts[0];
3835 const int x_left = width - visible_width + margin;
3836 const int x_ofs = term->font_x_ofs;
3837 int x = x_left;
3838 int y = margin;
3840 pixman_color_t fg = color_hex_to_pixman(
3841 custom_colors
3842 ? (is_match
3843 ? term->conf->colors_dark.search_box.match.fg
3844 : term->conf->colors_dark.search_box.no_match.fg)
3845 : term->colors.table[0],
3846 gamma_correct);
3848 /* Move offset we start rendering at, to ensure the cursor is visible */
3849 for (size_t i = 0, cell_idx = 0; i <= term->search.cursor; cell_idx += widths[i], i++) {
3850 if (i != term->search.cursor)
3851 continue;
3853#if (FOOT_IME_ENABLED) && FOOT_IME_ENABLED
3854 if (ime_seat != NULL && ime_seat->ime.preedit.cells != NULL) {
3855 if (ime_seat->ime.preedit.cursor.start == ime_seat->ime.preedit.cursor.end) {
3856 /* All IME's I've seen so far keeps the cursor at
3857 * index 0, so ensure the *end* of the pre-edit string
3858 * is visible */
3859 cell_idx += ime_seat->ime.preedit.count;
3860 } else {
3861 /* Try to predict in which direction we'll shift the text */
3862 if (cell_idx + ime_seat->ime.preedit.cursor.start > glyph_offset)
3863 cell_idx += ime_seat->ime.preedit.cursor.end;
3864 else
3865 cell_idx += ime_seat->ime.preedit.cursor.start;
3866 }
3867 }
3868#endif
3870 if (cell_idx < glyph_offset) {
3871 /* Shift to the *left*, making *this* character the
3872 * *first* visible one */
3873 term->render.search_glyph_offset = glyph_offset = cell_idx;
3874 }
3876 else if (cell_idx > glyph_offset + visible_cells) {
3877 /* Shift to the *right*, making *this* character the
3878 * *last* visible one */
3879 term->render.search_glyph_offset = glyph_offset =
3880 cell_idx - min(cell_idx, visible_cells);
3881 }
3883 /* Adjust offset if there is free space available */
3884 if (total_cells - glyph_offset < visible_cells) {
3885 term->render.search_glyph_offset = glyph_offset =
3886 total_cells - min(total_cells, visible_cells);
3887 }
3889 break;
3890 }
3892 /* Ensure offset is at a character boundary */
3893 for (size_t i = 0, cell_idx = 0; i <= text_len; cell_idx += widths[i], i++) {
3894 if (cell_idx >= glyph_offset) {
3895 term->render.search_glyph_offset = glyph_offset = cell_idx;
3896 break;
3897 }
3898 }
3900 /*
3901 * Render the search string, starting at 'glyph_offset'. Note that
3902 * glyph_offset is in cells, not characters
3903 */
3904 for (size_t i = 0,
3905 cell_idx = 0,
3906 width = widths[i],
3907 next_cell_idx = width;
3908 i < text_len;
3909 i++,
3910 cell_idx = next_cell_idx,
3911 width = widths[i],
3912 next_cell_idx += width)
3913 {
3914 /* Convert subsurface coordinates to window coordinates*/
3915 /* Render cursor */
3916 if (i == term->search.cursor) {
3917#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
3918 bool have_preedit =
3919 ime_seat != NULL && ime_seat->ime.preedit.cells != NULL;
3920 bool hidden =
3921 ime_seat != NULL && ime_seat->ime.preedit.cursor.hidden;
3923 if (have_preedit && !hidden) {
3924 /* Cursor may be outside the visible area:
3925 * cell_idx-glyph_offset can be negative */
3926 int cells_left = visible_cells - max(
3927 (ssize_t)(cell_idx - glyph_offset), 0);
3929 /* If cursor is outside the visible area, we need to
3930 * adjust our rectangle's position */
3931 int start = ime_seat->ime.preedit.cursor.start
3932 + min((ssize_t)(cell_idx - glyph_offset), 0);
3933 int end = ime_seat->ime.preedit.cursor.end
3934 + min((ssize_t)(cell_idx - glyph_offset), 0);
3936 if (start == end) {
3937 int count = min(ime_seat->ime.preedit.count, cells_left);
3939 /* Underline the entire (visible part of) pre-edit text */
3940 draw_underline(term, buf->pix[0], font, &fg, x, y, count);
3942 /* Bar-styled cursor, if in the visible area */
3943 if (start >= 0 && start <= visible_cells) {
3944 draw_beam_cursor(
3945 term, buf->pix[0], font, &fg,
3946 x + start * term->cell_width, y);
3947 }
3949 term_ime_set_cursor_rect(term,
3950 WINDOW_X(x + start * term->cell_width), WINDOW_Y(y),
3951 1, term->cell_height);
3952 } else {
3953 /* Underline everything before and after the cursor */
3954 int count1 = min(start, cells_left);
3955 int count2 = max(
3956 min(ime_seat->ime.preedit.count - ime_seat->ime.preedit.cursor.end,
3957 cells_left - end),
3958 0);
3959 draw_underline(term, buf->pix[0], font, &fg, x, y, count1);
3960 draw_underline(term, buf->pix[0], font, &fg, x + end * term->cell_width, y, count2);
3962 /* TODO: how do we handle a partially hidden rectangle? */
3963 if (start >= 0 && end <= visible_cells) {
3964 draw_hollow_block(
3965 term, buf->pix[0], &fg, x + start * term->cell_width, y, end - start);
3966 }
3967 term_ime_set_cursor_rect(term,
3968 WINDOW_X(x + start * term->cell_width), WINDOW_Y(y),
3969 term->cell_width * (end - start), term->cell_height);
3970 }
3971 } else if (!have_preedit)
3972#endif
3973 {
3974 /* Cursor *should* be in the visible area */
3975 xassert(cell_idx >= glyph_offset);
3976 xassert(cell_idx <= glyph_offset + visible_cells);
3977 draw_beam_cursor(term, buf->pix[0], font, &fg, x, y);
3978 term_ime_set_cursor_rect(
3979 term, WINDOW_X(x), WINDOW_Y(y), 1, term->cell_height);
3980 }
3981 }
3983 if (next_cell_idx >= glyph_offset && next_cell_idx - glyph_offset > visible_cells) {
3984 /* We're now beyond the visible area - nothing more to render */
3985 break;
3986 }
3988 if (cell_idx < glyph_offset) {
3989 /* We haven't yet reached the visible part of the string */
3990 cell_idx = next_cell_idx;
3991 continue;
3992 }
3994 const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(
3995 font, text[i], term->font_subpixel);
3997 if (glyph == NULL) {
3998 cell_idx = next_cell_idx;
3999 continue;
4000 }
Diagnostics5 git calls · 504 ms
CallStatusSizeTime0504 ms
discover200178 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB464 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=380 git.response.bytes=10322 git.response.cache=EXPIRED git.response.duration_ms=461 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchtree:0 · deepen 102009.3 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetchtree:02003.6 KiB · hit5 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=3708 git.response.cache=HIT git.response.duration_ms=5 git.fetch.filter=tree:0 git.fetch.wants=96f1228f4004ebd74530c7f0aea264108eff425f git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3708 git.pkt.payload.max=3681 git.sideband.band1.bytes=3681 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=3681 git.pack.trailer=731a22f61f25bdfdca1691647f2056631bcfdcf1Open pack map
fetch20040 KiB · hit33 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=28 git.response.bytes=40521 git.response.cache=HIT git.response.duration_ms=33 git.fetch.wants=2bc441463763a8d5e797aaecf0d2651033eeedaf git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=40521 git.pkt.payload.max=40494 git.sideband.band1.bytes=40494 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5e138b3ac0e18aa
- Request
- d6cd5906…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format