Skip to content

Commit c7eb63d

Browse files
committed
Avoid extra layer update if clearing images has no effect
1 parent 4062365 commit c7eb63d

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

kitty/graphics.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -645,20 +645,32 @@ grman_update_layers(GraphicsManager *self, unsigned int scrolled_by, float scree
645645
// Image lifetime/scrolling {{{
646646

647647
static inline void
648-
filter_refs(GraphicsManager *self, const void* data, bool free_images, bool (*filter_func)(ImageRef*, Image*, const void*, CellPixelSize), CellPixelSize cell) {
649-
if (self->image_count) self->layers_dirty = true;
648+
filter_refs(GraphicsManager *self, const void* data, bool free_images, bool (*filter_func)(const ImageRef*, Image*, const void*, CellPixelSize), CellPixelSize cell) {
650649
for (size_t i = self->image_count; i-- > 0;) {
651650
Image *img = self->images + i;
652651
for (size_t j = img->refcnt; j-- > 0;) {
653652
ImageRef *ref = img->refs + j;
654653
if (filter_func(ref, img, data, cell)) {
655654
remove_i_from_array(img->refs, j, img->refcnt);
655+
self->layers_dirty = true;
656656
}
657657
}
658658
if (img->refcnt == 0 && (free_images || img->client_id == 0)) remove_image(self, i);
659659
}
660660
}
661661

662+
static inline void
663+
modify_refs(GraphicsManager *self, const void* data, bool free_images, bool (*filter_func)(ImageRef*, Image*, const void*, CellPixelSize), CellPixelSize cell) {
664+
for (size_t i = self->image_count; i-- > 0;) {
665+
Image *img = self->images + i;
666+
for (size_t j = img->refcnt; j-- > 0;) {
667+
if (filter_func(img->refs + j, img, data, cell)) remove_i_from_array(img->refs, j, img->refcnt);
668+
}
669+
if (img->refcnt == 0 && (free_images || img->client_id == 0)) remove_image(self, i);
670+
}
671+
}
672+
673+
662674
static inline bool
663675
scroll_filter_func(ImageRef *ref, Image UNUSED *img, const void *data, CellPixelSize cell UNUSED) {
664676
ScrollData *d = (ScrollData*)data;
@@ -667,12 +679,12 @@ scroll_filter_func(ImageRef *ref, Image UNUSED *img, const void *data, CellPixel
667679
}
668680

669681
static inline bool
670-
ref_within_region(ImageRef *ref, index_type margin_top, index_type margin_bottom) {
682+
ref_within_region(const ImageRef *ref, index_type margin_top, index_type margin_bottom) {
671683
return ref->start_row >= (int32_t)margin_top && ref->start_row + ref->effective_num_rows <= margin_bottom;
672684
}
673685

674686
static inline bool
675-
ref_outside_region(ImageRef *ref, index_type margin_top, index_type margin_bottom) {
687+
ref_outside_region(const ImageRef *ref, index_type margin_top, index_type margin_bottom) {
676688
return ref->start_row + ref->effective_num_rows <= margin_top || ref->start_row > (int32_t)margin_bottom;
677689
}
678690

@@ -709,16 +721,19 @@ scroll_filter_margins_func(ImageRef* ref, Image* img, const void* data, CellPixe
709721

710722
void
711723
grman_scroll_images(GraphicsManager *self, const ScrollData *data, CellPixelSize cell) {
712-
filter_refs(self, data, true, data->has_margins ? scroll_filter_margins_func : scroll_filter_func, cell);
724+
if (self->image_count) {
725+
self->layers_dirty = true;
726+
modify_refs(self, data, true, data->has_margins ? scroll_filter_margins_func : scroll_filter_func, cell);
727+
}
713728
}
714729

715730
static inline bool
716-
clear_filter_func(ImageRef *ref, Image UNUSED *img, const void UNUSED *data, CellPixelSize cell UNUSED) {
731+
clear_filter_func(const ImageRef *ref, Image UNUSED *img, const void UNUSED *data, CellPixelSize cell UNUSED) {
717732
return ref->start_row + (int32_t)ref->effective_num_rows > 0;
718733
}
719734

720735
static inline bool
721-
clear_all_filter_func(ImageRef *ref UNUSED, Image UNUSED *img, const void UNUSED *data, CellPixelSize cell UNUSED) {
736+
clear_all_filter_func(const ImageRef *ref UNUSED, Image UNUSED *img, const void UNUSED *data, CellPixelSize cell UNUSED) {
722737
return true;
723738
}
724739

@@ -728,37 +743,37 @@ grman_clear(GraphicsManager *self, bool all, CellPixelSize cell) {
728743
}
729744

730745
static inline bool
731-
id_filter_func(ImageRef UNUSED *ref, Image *img, const void *data, CellPixelSize cell UNUSED) {
746+
id_filter_func(const ImageRef UNUSED *ref, Image *img, const void *data, CellPixelSize cell UNUSED) {
732747
uint32_t iid = *(uint32_t*)data;
733748
return img->client_id == iid;
734749
}
735750

736751
static inline bool
737-
x_filter_func(ImageRef *ref, Image UNUSED *img, const void *data, CellPixelSize cell UNUSED) {
752+
x_filter_func(const ImageRef *ref, Image UNUSED *img, const void *data, CellPixelSize cell UNUSED) {
738753
const GraphicsCommand *g = data;
739754
return ref->start_column <= (int32_t)g->x_offset - 1 && ((int32_t)g->x_offset - 1) < ((int32_t)(ref->start_column + ref->effective_num_cols));
740755
}
741756

742757
static inline bool
743-
y_filter_func(ImageRef *ref, Image UNUSED *img, const void *data, CellPixelSize cell UNUSED) {
758+
y_filter_func(const ImageRef *ref, Image UNUSED *img, const void *data, CellPixelSize cell UNUSED) {
744759
const GraphicsCommand *g = data;
745760
return ref->start_row <= (int32_t)g->y_offset - 1 && ((int32_t)(g->y_offset - 1 < ref->start_row + ref->effective_num_rows));
746761
}
747762

748763
static inline bool
749-
z_filter_func(ImageRef *ref, Image UNUSED *img, const void *data, CellPixelSize cell UNUSED) {
764+
z_filter_func(const ImageRef *ref, Image UNUSED *img, const void *data, CellPixelSize cell UNUSED) {
750765
const GraphicsCommand *g = data;
751766
return ref->z_index == g->z_index;
752767
}
753768

754769

755770
static inline bool
756-
point_filter_func(ImageRef *ref, Image *img, const void *data, CellPixelSize cell) {
771+
point_filter_func(const ImageRef *ref, Image *img, const void *data, CellPixelSize cell) {
757772
return x_filter_func(ref, img, data, cell) && y_filter_func(ref, img, data, cell);
758773
}
759774

760775
static inline bool
761-
point3d_filter_func(ImageRef *ref, Image *img, const void *data, CellPixelSize cell) {
776+
point3d_filter_func(const ImageRef *ref, Image *img, const void *data, CellPixelSize cell) {
762777
return z_filter_func(ref, img, data, cell) && point_filter_func(ref, img, data, cell);
763778
}
764779

0 commit comments

Comments
 (0)