mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-20 23:25:46 +00:00
Fix deleting displayed and base animation frames
This commit is contained in:
parent
8637b48ad2
commit
2dc9687573
3 changed files with 143 additions and 4 deletions
|
|
@ -1830,6 +1830,7 @@ update_current_frame(GraphicsManager *self, Image *img, const CoalescedFrameData
|
|||
data = &cfd;
|
||||
}
|
||||
upload_to_gpu(self, img, data->is_opaque, data->is_4byte_aligned, data->buf);
|
||||
img->displayed_frame_id = current_frame(img)->id;
|
||||
if (needs_load) free(data->buf);
|
||||
img->current_frame_shown_at = monotonic();
|
||||
}
|
||||
|
|
@ -2018,6 +2019,51 @@ handle_animation_frame_load_command(GraphicsManager *self, GraphicsCommand *g, I
|
|||
|
||||
#undef ABRT
|
||||
|
||||
static bool
|
||||
promote_dependents_to_keyframes(GraphicsManager *self, Image *img, const uint32_t base_id) {
|
||||
for (unsigned i = 0; i < img->extra_framecnt; i++) {
|
||||
Frame *f = img->extra_frames + i;
|
||||
if (f->base_frame_id != base_id) continue;
|
||||
CoalescedFrameData cfd = get_coalesced_frame_data(self, img, f);
|
||||
if (!cfd.buf) return false;
|
||||
const ImageAndFrame key = {.image_id = img->internal_id, .frame_id = f->id};
|
||||
void *frame_data = cfd.buf;
|
||||
const bool transient = f->transient || cfd.transient;
|
||||
bool added = add_to_cache(self, key, &frame_data, (size_t)(cfd.is_opaque ? 3 : 4) * img->width * img->height, transient);
|
||||
free(frame_data); // NULL if ownership was transferred to the cache
|
||||
if (!added) return false;
|
||||
f->transient = transient;
|
||||
f->alpha_blend = false;
|
||||
f->base_frame_id = 0;
|
||||
f->bgcolor = 0;
|
||||
f->is_opaque = cfd.is_opaque;
|
||||
f->is_4byte_aligned = cfd.is_4byte_aligned;
|
||||
f->x = 0;
|
||||
f->y = 0;
|
||||
f->width = img->width;
|
||||
f->height = img->height;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
dependents_fit_in_cache(GraphicsManager *self, Image *img, const uint32_t base_id, const size_t victim_sz) {
|
||||
const size_t full = (size_t)4 * img->width * img->height;
|
||||
size_t growth = 0;
|
||||
for (unsigned i = 0; i < img->extra_framecnt; i++) {
|
||||
const Frame *f = img->extra_frames + i;
|
||||
if (f->base_frame_id != base_id) continue;
|
||||
const size_t cur = (size_t)(f->is_opaque ? 3 : 4) * f->width * f->height;
|
||||
if (full > cur) growth += full - cur;
|
||||
}
|
||||
if (growth <= victim_sz) return true;
|
||||
growth -= victim_sz;
|
||||
const size_t limit = self->storage_limit * 5;
|
||||
if (cache_size(self) + growth <= limit) return true;
|
||||
remove_images(self, trim_predicate, img->internal_id);
|
||||
return cache_size(self) + growth <= limit;
|
||||
}
|
||||
|
||||
static Image *
|
||||
handle_delete_frame_command(GraphicsManager *self, const GraphicsCommand *g, bool *is_dirty) {
|
||||
if (!g->id && !g->image_number) {
|
||||
|
|
@ -2036,6 +2082,14 @@ handle_delete_frame_command(GraphicsManager *self, const GraphicsCommand *g, boo
|
|||
ImageAndFrame key = {.image_id = img->internal_id};
|
||||
bool remove_root = frame_number == 1;
|
||||
uint32_t removed_gap = 0;
|
||||
const uint32_t victim_id = remove_root ? img->root_frame.id : img->extra_frames[frame_number - 2].id;
|
||||
const Frame *victim = remove_root ? &img->root_frame : img->extra_frames + frame_number - 2;
|
||||
const size_t victim_sz = (size_t)(victim->is_opaque ? 3 : 4) * victim->width * victim->height;
|
||||
if (!dependents_fit_in_cache(self, img, victim_id, victim_sz) || !promote_dependents_to_keyframes(self, img, victim_id)) {
|
||||
if (PyErr_Occurred()) PyErr_Print();
|
||||
REPORT_ERROR("Failed to preserve frames that depend on deleted frame: %u", frame_number);
|
||||
return NULL;
|
||||
}
|
||||
if (remove_root) {
|
||||
key.frame_id = img->root_frame.id;
|
||||
remove_from_cache(self, key);
|
||||
|
|
@ -2059,8 +2113,9 @@ handle_delete_frame_command(GraphicsManager *self, const GraphicsCommand *g, boo
|
|||
update_current_frame(self, img, NULL);
|
||||
return NULL;
|
||||
}
|
||||
if (removed_idx == img->current_frame_index) update_current_frame(self, img, NULL);
|
||||
else if (removed_idx < img->current_frame_index) img->current_frame_index--;
|
||||
const unsigned removed_index = remove_root ? 0 : removed_idx + 1;
|
||||
if (img->current_frame_index > removed_index) img->current_frame_index--;
|
||||
else if (img->current_frame_index == removed_index) update_current_frame(self, img, NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
@ -2780,7 +2835,7 @@ image_as_dict(GraphicsManager *self, Image *img) {
|
|||
"{sI sI sI sI sI sI sI "
|
||||
"sO sI sO "
|
||||
"sI sI sI "
|
||||
"sI sy# sN}",
|
||||
"sI sI sy# sN}",
|
||||
"texture_id",
|
||||
texture_id_for_img(img),
|
||||
U(client_id),
|
||||
|
|
@ -2802,6 +2857,7 @@ image_as_dict(GraphicsManager *self, Image *img) {
|
|||
U(current_frame_index),
|
||||
|
||||
U(animation_duration),
|
||||
U(displayed_frame_id),
|
||||
"data",
|
||||
cfd.buf,
|
||||
(Py_ssize_t)((cfd.is_opaque ? 3 : 4) * img->width * img->height),
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ typedef struct {
|
|||
bool root_frame_data_loaded;
|
||||
id_type ref_id_counter;
|
||||
Frame *extra_frames, root_frame;
|
||||
uint32_t current_frame_index, frame_id_counter;
|
||||
uint32_t current_frame_index, frame_id_counter, displayed_frame_id;
|
||||
uint64_t animation_duration;
|
||||
size_t extra_framecnt;
|
||||
monotonic_t atime;
|
||||
|
|
|
|||
|
|
@ -1610,6 +1610,89 @@ class TestGraphics(BaseTest):
|
|||
delete('I', i=iid + 1)
|
||||
self.ae(s.grman.image_count, 1)
|
||||
|
||||
def test_animation_frame_delete_dependents(self):
|
||||
s = self.create_screen()
|
||||
g = s.grman
|
||||
li = make_send_command(s)
|
||||
|
||||
def t(**kw):
|
||||
self.assertEqual(li(**kw).code, 'OK')
|
||||
|
||||
def load_frames():
|
||||
t(a='t')
|
||||
for i in '234':
|
||||
t(payload=i * 36)
|
||||
|
||||
# Preserve the successor display
|
||||
load_frames()
|
||||
self.assertIsNone(li(a='a', i=1, c=3))
|
||||
img = g.image_for_client_id(1)
|
||||
self.assertEqual(img['displayed_frame_id'], img['extra_frames'][1]['id'])
|
||||
successor = img['extra_frames'][2]['id']
|
||||
self.assertIsNone(li(a='d', d='f', i=1, r=3))
|
||||
img = g.image_for_client_id(1)
|
||||
self.assertEqual(img['current_frame_index'], 2)
|
||||
self.assertEqual(img['displayed_frame_id'], successor)
|
||||
self.assertEqual([f['data'] for f in img['extra_frames']], [b'2' * 36, b'4' * 36])
|
||||
s.reset()
|
||||
|
||||
# Preserve the root successor display
|
||||
load_frames()
|
||||
successor = g.image_for_client_id(1)['extra_frames'][0]['id']
|
||||
self.assertIsNone(li(a='a', i=1, c=1))
|
||||
self.assertIsNone(li(a='d', d='f', i=1, r=1))
|
||||
img = g.image_for_client_id(1)
|
||||
self.assertEqual(img['current_frame_index'], 0)
|
||||
self.assertEqual(img['displayed_frame_id'], successor)
|
||||
s.reset()
|
||||
|
||||
# Preserve the last frame display
|
||||
load_frames()
|
||||
self.assertIsNone(li(a='a', i=1, c=4))
|
||||
self.assertIsNone(li(a='d', d='f', i=1, r=4))
|
||||
img = g.image_for_client_id(1)
|
||||
self.assertEqual(img['current_frame_index'], 2)
|
||||
self.assertEqual(img['displayed_frame_id'], img['extra_frames'][1]['id'])
|
||||
s.reset()
|
||||
|
||||
# Preserve dependent pixels
|
||||
t(a='t')
|
||||
t(payload='2' * 36)
|
||||
t(payload='4' * 12, c=2, s=2, v=2)
|
||||
expected = b'444444222222444444222222222222222222'
|
||||
self.assertEqual(g.image_for_client_id(1)['extra_frames'][1]['data'], expected)
|
||||
self.assertIsNone(li(a='d', d='f', i=1, r=2))
|
||||
img = g.image_for_client_id(1)
|
||||
self.assertEqual(len(img['extra_frames']), 1)
|
||||
self.assertEqual(img['extra_frames'][0]['data'], expected)
|
||||
s.reset()
|
||||
|
||||
# Preserve root dependents
|
||||
t(a='t')
|
||||
t(payload='4' * 12, c=1, s=2, v=2)
|
||||
expected = g.image_for_client_id(1)['extra_frames'][0]['data']
|
||||
self.assertIsNone(li(a='d', d='f', i=1, r=1))
|
||||
img = g.image_for_client_id(1)
|
||||
self.assertEqual(img['data'], expected)
|
||||
self.assertFalse(img['extra_frames'])
|
||||
s.reset()
|
||||
self.assertEqual(g.disk_cache.total_size, 0)
|
||||
|
||||
# Respect the cache quota
|
||||
g.storage_limit = 36 * 2
|
||||
t(a='t')
|
||||
for i in range(10):
|
||||
t(payload='4' * 12, c=1, s=2, v=2)
|
||||
before = g.image_for_client_id(1)
|
||||
size = g.disk_cache.total_size
|
||||
self.assertIsNone(li(a='d', d='f', i=1, r=1))
|
||||
after = g.image_for_client_id(1)
|
||||
self.assertEqual(g.disk_cache.total_size, size)
|
||||
self.assertEqual(after['data'], before['data'])
|
||||
self.assertEqual([f['data'] for f in after['extra_frames']], [f['data'] for f in before['extra_frames']])
|
||||
s.reset()
|
||||
self.assertEqual(g.disk_cache.total_size, 0)
|
||||
|
||||
def test_animation_frame_loading(self):
|
||||
s = self.create_screen()
|
||||
g = s.grman
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue