1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
* Copyright (C) 2019 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtktextbtree.h"
#include "gtktextbufferprivate.h"
#include "gtktextiterprivate.h"
#include "gtktextlinedisplaycacheprivate.h"
#include "gtkprivate.h"
#define DEFAULT_MRU_SIZE 250
#define BLOW_CACHE_TIMEOUT_SEC 20
#define DEBUG_LINE_DISPLAY_CACHE 0
struct _GtkTextLineDisplayCache
{
GSequence *sorted_by_line;
GHashTable *line_to_display;
GtkTextLine *cursor_line;
GQueue mru;
GSource *evict_source;
guint mru_size;
#if DEBUG_LINE_DISPLAY_CACHE
guint log_source;
int hits;
int misses;
int inval;
int inval_cursors;
int inval_by_line;
int inval_by_range;
int inval_by_y_range;
#endif
};
#if DEBUG_LINE_DISPLAY_CACHE
# define STAT_ADD(val,n) ((val) += n)
# define STAT_INC(val) STAT_ADD(val,1)
static gboolean
dump_stats (gpointer data)
{
GtkTextLineDisplayCache *cache = data;
g_printerr ("%p: size=%u hits=%d misses=%d inval_total=%d "
"inval_cursors=%d inval_by_line=%d "
"inval_by_range=%d inval_by_y_range=%d\n",
cache, g_hash_table_size (cache->line_to_display),
cache->hits, cache->misses,
cache->inval, cache->inval_cursors,
cache->inval_by_line, cache->inval_by_range,
cache->inval_by_y_range);
return G_SOURCE_CONTINUE;
}
#else
# define STAT_ADD(val,n)
# define STAT_INC(val)
#endif
GtkTextLineDisplayCache *
gtk_text_line_display_cache_new (void)
{
GtkTextLineDisplayCache *ret;
ret = g_slice_new0 (GtkTextLineDisplayCache);
ret->sorted_by_line = g_sequence_new ((GDestroyNotify)gtk_text_line_display_unref);
ret->line_to_display = g_hash_table_new (NULL, NULL);
ret->mru_size = DEFAULT_MRU_SIZE;
#if DEBUG_LINE_DISPLAY_CACHE
ret->log_source = g_timeout_add_seconds (1, dump_stats, ret);
#endif
return g_steal_pointer (&ret);
}
void
gtk_text_line_display_cache_free (GtkTextLineDisplayCache *cache)
{
#if DEBUG_LINE_DISPLAY_CACHE
g_clear_handle_id (&cache->log_source, g_source_remove);
#endif
gtk_text_line_display_cache_invalidate (cache);
g_clear_pointer (&cache->evict_source, g_source_destroy);
g_clear_pointer (&cache->sorted_by_line, g_sequence_free);
g_clear_pointer (&cache->line_to_display, g_hash_table_unref);
g_slice_free (GtkTextLineDisplayCache, cache);
}
static gboolean
gtk_text_line_display_cache_blow_cb (gpointer data)
{
GtkTextLineDisplayCache *cache = data;
g_assert (cache != NULL);
#if DEBUG_LINE_DISPLAY_CACHE
g_printerr ("Evicting GtkTextLineDisplayCache\n");
#endif
cache->evict_source = NULL;
gtk_text_line_display_cache_invalidate (cache);
return G_SOURCE_REMOVE;
}
void
gtk_text_line_display_cache_delay_eviction (GtkTextLineDisplayCache *cache)
{
g_assert (cache != NULL);
if (cache->evict_source != NULL)
{
gint64 deadline;
deadline = g_get_monotonic_time () + (BLOW_CACHE_TIMEOUT_SEC * G_USEC_PER_SEC);
g_source_set_ready_time (cache->evict_source, deadline);
}
else
{
guint tag;
tag = g_timeout_add_seconds (BLOW_CACHE_TIMEOUT_SEC,
gtk_text_line_display_cache_blow_cb,
cache);
cache->evict_source = g_main_context_find_source_by_id (NULL, tag);
g_source_set_static_name (cache->evict_source, "[gtk+] gtk_text_line_display_cache_blow_cb");
}
}
#if DEBUG_LINE_DISPLAY_CACHE
static void
check_disposition (GtkTextLineDisplayCache *cache,
GtkTextLayout *layout)
{
GSequenceIter *iter;
int last = G_MAXUINT;
g_assert (cache != NULL);
g_assert (cache->sorted_by_line != NULL);
g_assert (cache->line_to_display != NULL);
for (iter = g_sequence_get_begin_iter (cache->sorted_by_line);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkTextLineDisplay *display = g_sequence_get (iter);
GtkTextIter text_iter;
guint line;
gtk_text_layout_get_iter_at_line (layout, &text_iter, display->line, 0);
line = gtk_text_iter_get_line (&text_iter);
g_assert_cmpint (line, >, last);
last = line;
}
}
#endif
static void
gtk_text_line_display_cache_take_display (GtkTextLineDisplayCache *cache,
GtkTextLineDisplay *display,
GtkTextLayout *layout)
{
g_assert (cache != NULL);
g_assert (display != NULL);
g_assert (display->line != NULL);
g_assert (display->cache_iter == NULL);
g_assert (display->mru_link.data == display);
g_assert (display->mru_link.prev == NULL);
g_assert (display->mru_link.next == NULL);
g_assert (g_hash_table_lookup (cache->line_to_display, display->line) == NULL);
#if DEBUG_LINE_DISPLAY_CACHE
check_disposition (cache, layout);
#endif
display->cache_iter =
g_sequence_insert_sorted (cache->sorted_by_line,
display,
(GCompareDataFunc) gtk_text_line_display_compare,
layout);
g_hash_table_insert (cache->line_to_display, display->line, display);
g_queue_push_head_link (&cache->mru, &display->mru_link);
/* Cull the cache if we're at capacity */
while (cache->mru.length > cache->mru_size)
{
display = g_queue_peek_tail (&cache->mru);
gtk_text_line_display_cache_invalidate_display (cache, display, FALSE);
}
}
/*
* gtk_text_line_display_cache_invalidate_display:
* @cache: a GtkTextLineDisplayCache
* @display: a GtkTextLineDisplay
* @cursors_only: if only the cursor positions should be invalidated
*
* If @cursors_only is TRUE, then only the cursors are invalidated. Otherwise,
* @display is removed from the cache.
*
* Use this function when you already have access to a display as it reduces
* some overhead.
*/
void
gtk_text_line_display_cache_invalidate_display (GtkTextLineDisplayCache *cache,
GtkTextLineDisplay *display,
gboolean cursors_only)
{
g_assert (cache != NULL);
g_assert (display != NULL);
g_assert (display->line != NULL);
if (cursors_only)
{
g_clear_pointer (&display->cursors, g_array_unref);
g_clear_pointer (&display->node, gsk_render_node_unref);
display->cursors_invalid = TRUE;
display->has_block_cursor = FALSE;
}
else
{
GSequenceIter *iter = g_steal_pointer (&display->cache_iter);
if (cache->cursor_line == display->line)
cache->cursor_line = NULL;
g_hash_table_remove (cache->line_to_display, display->line);
g_queue_unlink (&cache->mru, &display->mru_link);
if (iter != NULL)
g_sequence_remove (iter);
}
STAT_INC (cache->inval);
}
/*
* gtk_text_line_display_cache_get:
* @cache: a `GtkTextLineDisplayCache`
* @layout: a `GtkTextLayout`
* @line: a `GtkTextLine`
* @size_only: if only line sizing is needed
*
* Gets a GtkTextLineDisplay for @line.
*
* If no cached display exists, a new display will be created.
*
* It's possible that calling this function will cause some existing
* cached displays to be released and destroyed.
*
* Returns: (transfer full) (not nullable): a `GtkTextLineDisplay`
*/
GtkTextLineDisplay *
gtk_text_line_display_cache_get (GtkTextLineDisplayCache *cache,
GtkTextLayout *layout,
GtkTextLine *line,
gboolean size_only)
{
GtkTextLineDisplay *display;
g_assert (cache != NULL);
g_assert (layout != NULL);
g_assert (line != NULL);
display = g_hash_table_lookup (cache->line_to_display, line);
if (display != NULL)
{
if (size_only || !display->size_only)
{
STAT_INC (cache->hits);
if (!size_only && display->line == cache->cursor_line)
gtk_text_layout_update_display_cursors (layout, display->line, display);
if (!size_only && display->has_children)
gtk_text_layout_update_children (layout, display);
/* Move to front of MRU */
g_queue_unlink (&cache->mru, &display->mru_link);
g_queue_push_head_link (&cache->mru, &display->mru_link);
return gtk_text_line_display_ref (display);
}
/* We need an updated display that includes more than just
* sizing, so we need to drop this entry and force the layout
* to create a new one.
*/
gtk_text_line_display_cache_invalidate_display (cache, display, FALSE);
}
STAT_INC (cache->misses);
g_assert (!g_hash_table_lookup (cache->line_to_display, line));
display = gtk_text_layout_create_display (layout, line, size_only);
g_assert (display != NULL);
g_assert (display->line == line);
if (!size_only)
{
if (line == cache->cursor_line)
gtk_text_layout_update_display_cursors (layout, line, display);
if (display->has_children)
gtk_text_layout_update_children (layout, display);
gtk_text_line_display_cache_take_display (cache,
gtk_text_line_display_ref (display),
layout);
}
return g_steal_pointer (&display);
}
void
gtk_text_line_display_cache_invalidate (GtkTextLineDisplayCache *cache)
{
g_assert (cache != NULL);
g_assert (cache->sorted_by_line != NULL);
g_assert (cache->line_to_display != NULL);
STAT_ADD (cache->inval, g_hash_table_size (cache->line_to_display));
cache->cursor_line = NULL;
while (cache->mru.head != NULL)
{
GtkTextLineDisplay *display = g_queue_peek_head (&cache->mru);
gtk_text_line_display_cache_invalidate_display (cache, display, FALSE);
}
g_assert (g_hash_table_size (cache->line_to_display) == 0);
g_assert (g_sequence_get_length (cache->sorted_by_line) == 0);
g_assert (cache->mru.length == 0);
}
void
gtk_text_line_display_cache_invalidate_cursors (GtkTextLineDisplayCache *cache,
GtkTextLine *line)
{
GtkTextLineDisplay *display;
g_assert (cache != NULL);
g_assert (line != NULL);
STAT_INC (cache->inval_cursors);
display = g_hash_table_lookup (cache->line_to_display, line);
if (display != NULL)
gtk_text_line_display_cache_invalidate_display (cache, display, TRUE);
}
/*
* gtk_text_line_display_cache_invalidate_line:
* @self: a GtkTextLineDisplayCache
* @line: a GtkTextLine
*
* Removes a cached display for @line.
*
* Compare to gtk_text_line_display_cache_invalidate_cursors() which
* only invalidates the cursors for this row.
*/
void
gtk_text_line_display_cache_invalidate_line (GtkTextLineDisplayCache *cache,
GtkTextLine *line)
{
GtkTextLineDisplay *display;
g_assert (cache != NULL);
g_assert (line != NULL);
display = g_hash_table_lookup (cache->line_to_display, line);
if (display != NULL)
gtk_text_line_display_cache_invalidate_display (cache, display, FALSE);
STAT_INC (cache->inval_by_line);
}
static GSequenceIter *
find_iter_at_text_iter (GtkTextLineDisplayCache *cache,
GtkTextLayout *layout,
const GtkTextIter *iter)
{
GSequenceIter *left;
GSequenceIter *right;
GSequenceIter *mid;
GSequenceIter *end;
GtkTextLine *target;
guint target_lineno;
g_assert (cache != NULL);
g_assert (iter != NULL);
if (g_sequence_is_empty (cache->sorted_by_line))
return NULL;
/* gtk_text_iter_get_line() will have cached value */
target_lineno = gtk_text_iter_get_line (iter);
target = _gtk_text_iter_get_text_line (iter);
/* Get some iters so we can work with pointer compare */
end = g_sequence_get_end_iter (cache->sorted_by_line);
left = g_sequence_get_begin_iter (cache->sorted_by_line);
right = g_sequence_iter_prev (end);
/* We already checked for empty above */
g_assert (!g_sequence_iter_is_end (left));
g_assert (!g_sequence_iter_is_end (right));
for (;;)
{
GtkTextLineDisplay *display;
guint lineno;
if (left == right)
mid = left;
else
mid = g_sequence_range_get_midpoint (left, right);
g_assert (mid != NULL);
g_assert (!g_sequence_iter_is_end (mid));
if (mid == end)
break;
display = g_sequence_get (mid);
g_assert (display != NULL);
g_assert (display->line != NULL);
g_assert (display->cache_iter != NULL);
if (target == display->line)
return mid;
if (right == left)
break;
lineno = _gtk_text_line_get_number (display->line);
if (target_lineno < lineno)
right = mid;
else if (target_lineno > lineno)
left = g_sequence_iter_next (mid);
else
g_assert_not_reached ();
}
return NULL;
}
/*
* gtk_text_line_display_cache_invalidate_range:
* @cache: a GtkTextLineDisplayCache
* @begin: the starting text iter
* @end: the ending text iter
*
* Removes all GtkTextLineDisplay that fall between or including
* @begin and @end.
*/
void
gtk_text_line_display_cache_invalidate_range (GtkTextLineDisplayCache *cache,
GtkTextLayout *layout,
const GtkTextIter *begin,
const GtkTextIter *end,
gboolean cursors_only)
{
GSequenceIter *begin_iter;
GSequenceIter *end_iter;
GSequenceIter *iter;
g_assert (cache != NULL);
g_assert (layout != NULL);
g_assert (begin != NULL);
g_assert (end != NULL);
STAT_INC (cache->inval_by_range);
/* Short-circuit, is_empty() is O(1) */
if (g_sequence_is_empty (cache->sorted_by_line))
return;
/* gtk_text_iter_order() preserving const */
if (gtk_text_iter_compare (begin, end) > 0)
{
const GtkTextIter *tmp = begin;
end = begin;
begin = tmp;
}
/* Common case, begin/end on same line. Just try to find the line by
* line number and invalidate it alone.
*/
if G_LIKELY (_gtk_text_iter_same_line (begin, end))
{
begin_iter = find_iter_at_text_iter (cache, layout, begin);
if (begin_iter != NULL)
{
GtkTextLineDisplay *display = g_sequence_get (begin_iter);
g_assert (display != NULL);
g_assert (display->line != NULL);
gtk_text_line_display_cache_invalidate_display (cache, display, cursors_only);
}
return;
}
/* Find GSequenceIter containing GtkTextLineDisplay that correspond
* to each of the text positions.
*/
begin_iter = find_iter_at_text_iter (cache, layout, begin);
end_iter = find_iter_at_text_iter (cache, layout, end);
/* Short-circuit if we found nothing */
if (begin_iter == NULL && end_iter == NULL)
return;
/* If nothing matches the end, we need to walk to the end of our
* cached displays. We know there is a non-zero number of items
* in the sequence at this point, so we can iter_prev() safely.
*/
if (end_iter == NULL)
end_iter = g_sequence_iter_prev (g_sequence_get_end_iter (cache->sorted_by_line));
/* If nothing matched the begin, we need to walk starting from
* the first display we have cached.
*/
if (begin_iter == NULL)
begin_iter = g_sequence_get_begin_iter (cache->sorted_by_line);
iter = begin_iter;
for (;;)
{
GtkTextLineDisplay *display = g_sequence_get (iter);
GSequenceIter *next = g_sequence_iter_next (iter);
gtk_text_line_display_cache_invalidate_display (cache, display, cursors_only);
if (iter == end_iter)
break;
iter = next;
}
}
static GSequenceIter *
find_iter_at_at_y (GtkTextLineDisplayCache *cache,
GtkTextLayout *layout,
int y)
{
GtkTextBTree *btree;
GSequenceIter *left;
GSequenceIter *right;
GSequenceIter *mid;
GSequenceIter *end;
g_assert (cache != NULL);
g_assert (layout != NULL);
if (g_sequence_is_empty (cache->sorted_by_line))
return NULL;
btree = _gtk_text_buffer_get_btree (layout->buffer);
/* Get some iters so we can work with pointer compare */
end = g_sequence_get_end_iter (cache->sorted_by_line);
left = g_sequence_get_begin_iter (cache->sorted_by_line);
right = g_sequence_iter_prev (end);
/* We already checked for empty above */
g_assert (!g_sequence_iter_is_end (left));
g_assert (!g_sequence_iter_is_end (right));
for (;;)
{
GtkTextLineDisplay *display;
int cache_y;
int cache_height;
if (left == right)
mid = left;
else
mid = g_sequence_range_get_midpoint (left, right);
g_assert (mid != NULL);
g_assert (!g_sequence_iter_is_end (mid));
if (mid == end)
break;
display = g_sequence_get (mid);
g_assert (display != NULL);
g_assert (display->line != NULL);
cache_y = _gtk_text_btree_find_line_top (btree, display->line, layout);
cache_height = display->height;
if (y >= cache_y && y <= (cache_y + cache_height))
return mid;
if (left == right)
break;
if (y < cache_y)
right = mid;
else if (y > (cache_y + cache_height))
left = g_sequence_iter_next (mid);
else
g_assert_not_reached ();
}
return NULL;
}
/*
* gtk_text_line_display_cache_invalidate_y_range:
* @cache: a GtkTextLineDisplayCache
* @y: the starting Y position
* @old_height: the old height of the range
* @new_height: the new height of the range
* @cursors_only: if only cursors should be invalidated
*
* Remove all GtkTextLineDisplay that fall into the range starting
* from the Y position to Y+Height.
*/
void
gtk_text_line_display_cache_invalidate_y_range (GtkTextLineDisplayCache *cache,
GtkTextLayout *layout,
int y,
int old_height,
int new_height,
gboolean cursors_only)
{
GSequenceIter *iter;
GtkTextBTree *btree;
g_assert (cache != NULL);
g_assert (layout != NULL);
STAT_INC (cache->inval_by_y_range);
/* A common pattern is to invalidate the whole buffer using y==0 and
* old_height==new_height. So special case that instead of walking through
* each display item one at a time.
*/
if (y < 0 || (y == 0 && old_height == new_height))
{
gtk_text_line_display_cache_invalidate (cache);
return;
}
btree = _gtk_text_buffer_get_btree (layout->buffer);
iter = find_iter_at_at_y (cache, layout, y);
if (iter == NULL)
return;
while (!g_sequence_iter_is_end (iter))
{
GtkTextLineDisplay *display;
int cache_y;
int cache_height;
display = g_sequence_get (iter);
iter = g_sequence_iter_next (iter);
cache_y = _gtk_text_btree_find_line_top (btree, display->line, layout);
cache_height = display->height;
if (cache_y + cache_height >= y && cache_y <= y + old_height)
{
gtk_text_line_display_cache_invalidate_display (cache, display, cursors_only);
y += cache_height;
old_height -= cache_height;
if (old_height > 0)
continue;
}
break;
}
}
void
gtk_text_line_display_cache_set_cursor_line (GtkTextLineDisplayCache *cache,
GtkTextLine *cursor_line)
{
GtkTextLineDisplay *display;
g_assert (cache != NULL);
if (cursor_line == cache->cursor_line)
return;
display = g_hash_table_lookup (cache->line_to_display, cache->cursor_line);
if (display != NULL)
gtk_text_line_display_cache_invalidate_display (cache, display, FALSE);
cache->cursor_line = cursor_line;
display = g_hash_table_lookup (cache->line_to_display, cache->cursor_line);
if (display != NULL)
gtk_text_line_display_cache_invalidate_display (cache, display, FALSE);
}
void
gtk_text_line_display_cache_set_mru_size (GtkTextLineDisplayCache *cache,
guint mru_size)
{
GtkTextLineDisplay *display;
g_assert (cache != NULL);
if (mru_size == 0)
mru_size = DEFAULT_MRU_SIZE;
if (mru_size != cache->mru_size)
{
cache->mru_size = mru_size;
while (cache->mru.length > cache->mru_size)
{
display = g_queue_peek_tail (&cache->mru);
gtk_text_line_display_cache_invalidate_display (cache, display, FALSE);
}
}
}
|