2587
+ − 1 /* Efficient caching of X GCs (graphics contexts).
+ − 2 Copyright (C) 1993 Free Software Foundation, Inc.
+ − 3 Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
+ − 4
+ − 5 This file is part of XEmacs.
+ − 6
+ − 7 XEmacs is free software; you can redistribute it and/or modify it
+ − 8 under the terms of the GNU General Public License as published by the
+ − 9 Free Software Foundation; either version 2, or (at your option) any
+ − 10 later version.
+ − 11
+ − 12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
+ − 13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ − 14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ − 15 for more details.
+ − 16
+ − 17 You should have received a copy of the GNU General Public License
+ − 18 along with XEmacs; see the file COPYING. If not, write to
+ − 19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ − 20 Boston, MA 02111-1307, USA. */
+ − 21
+ − 22 /* Synched up with: Not in FSF. */
+ − 23
+ − 24 /* Emacs uses a lot of different display attributes; for example, assume
+ − 25 that only four fonts are in use (normal, bold, italic, and bold-italic).
+ − 26 Then assume that one stipple or background is used for text selections,
+ − 27 and another is used for highlighting mousable regions. That makes 16
+ − 28 GCs already. Add in the fact that another GC may be needed to display
+ − 29 the text cursor in any of those regions, and you've got 32. Add in
+ − 30 more fonts, and it keeps increasing exponentially.
+ − 31
+ − 32 We used to keep these GCs in a cache of merged (fully qualified) faces.
+ − 33 However, a lot of other code in xterm.c used XChangeGC of existing GCs,
+ − 34 which is kind of slow and kind of random. Also, managing the face cache
+ − 35 was tricky because it was hard to know when a face was no longer visible
+ − 36 on the frame -- we had to mark all frames as garbaged whenever a face
+ − 37 was changed, which caused an unpleasant amount of flicker (since faces are
+ − 38 created/destroyed (= changed) whenever a frame is created/destroyed.
+ − 39
+ − 40 So this code maintains a cache at the GC level instead of at the face
+ − 41 level. There is an upper limit on the size of the cache, after which we
+ − 42 will stop creating GCs and start reusing them (reusing the least-recently-
+ − 43 used ones first). So if faces get changed, their GCs will eventually be
+ − 44 recycled. Also more sharing of GCs is possible.
+ − 45
+ − 46 This code uses hash tables. It could be that, if the cache size is small
+ − 47 enough, a linear search might be faster; but I doubt it, since we need
+ − 48 `equal' comparisons, not `eq', and I expect that the optimal cache size
+ − 49 will be ~100.
+ − 50
+ − 51 Written by jwz, 14 jun 93
+ − 52 */
+ − 53
+ − 54 #include <config.h>
+ − 55 #include <X11/Xlib.h>
+ − 56 #include "xgccache.h"
+ − 57
+ − 58
+ − 59 #define GC_CACHE_SIZE 100
+ − 60
+ − 61 #define GCCACHE_HASH
+ − 62
+ − 63
+ − 64 #ifdef GCCACHE_HASH
+ − 65 #include "lisp.h"
+ − 66 #include "hash.h"
+ − 67 #endif
+ − 68
+ − 69 struct gcv_and_mask {
+ − 70 XGCValues gcv;
+ − 71 unsigned long mask;
+ − 72 };
+ − 73
+ − 74 struct gc_cache_cell {
+ − 75 GC gc;
+ − 76 struct gcv_and_mask gcvm;
+ − 77 struct gc_cache_cell *prev, *next;
+ − 78 };
+ − 79
+ − 80 struct gc_cache {
+ − 81 Display *dpy; /* used only as arg to XCreateGC/XFreeGC */
+ − 82 Window window; /* used only as arg to XCreateGC */
+ − 83 int size;
+ − 84 struct gc_cache_cell *head;
+ − 85 struct gc_cache_cell *tail;
+ − 86 #ifdef GCCACHE_HASH
+ − 87 struct hash_table *table;
+ − 88 #endif
+ − 89
+ − 90 int create_count;
+ − 91 int delete_count;
+ − 92 };
+ − 93
+ − 94 #ifdef GCCACHE_HASH
+ − 95 static Hashcode
+ − 96 gc_cache_hash (const void *arg)
+ − 97 {
+ − 98 const struct gcv_and_mask *gcvm = (const struct gcv_and_mask *) arg;
+ − 99 unsigned long *longs = (unsigned long *) &gcvm->gcv;
+ − 100 Hashcode hash = gcvm->mask;
+ − 101 int i;
+ − 102 /* This could look at the mask and only use the used slots in the
+ − 103 hash code. That would win in that we wouldn't have to initialize
+ − 104 every slot of the gcv when calling gc_cache_lookup. But we need
+ − 105 the hash function to be as fast as possible; some timings should
+ − 106 be done. */
+ − 107 for (i = 0; i < (int) (sizeof (XGCValues) / sizeof (unsigned long)); i++)
+ − 108 hash = (hash << 1) ^ *longs++;
+ − 109 return hash;
+ − 110 }
+ − 111
+ − 112 #endif /* GCCACHE_HASH */
+ − 113
+ − 114 static int
+ − 115 gc_cache_eql (const void *arg1, const void *arg2)
+ − 116 {
+ − 117 /* See comment in gc_cache_hash */
+ − 118 return !memcmp (arg1, arg2, sizeof (struct gcv_and_mask));
+ − 119 }
+ − 120
+ − 121 struct gc_cache *
+ − 122 make_gc_cache (Display *dpy, Window window)
+ − 123 {
+ − 124 struct gc_cache *cache = xnew (struct gc_cache);
+ − 125 cache->dpy = dpy;
+ − 126 cache->window = window;
+ − 127 cache->size = 0;
+ − 128 cache->head = cache->tail = 0;
+ − 129 cache->create_count = cache->delete_count = 0;
+ − 130 #ifdef GCCACHE_HASH
+ − 131 cache->table =
+ − 132 make_general_hash_table (GC_CACHE_SIZE, gc_cache_hash, gc_cache_eql);
+ − 133 #endif
+ − 134 return cache;
+ − 135 }
+ − 136
+ − 137 void
+ − 138 free_gc_cache (struct gc_cache *cache)
+ − 139 {
+ − 140 struct gc_cache_cell *rest, *next;
+ − 141 rest = cache->head;
+ − 142 while (rest)
+ − 143 {
+ − 144 XFreeGC (cache->dpy, rest->gc);
+ − 145 next = rest->next;
+ − 146 xfree (rest, struct gc_cache_cell *);
+ − 147 rest = next;
+ − 148 }
+ − 149 #ifdef GCCACHE_HASH
+ − 150 free_hash_table (cache->table);
+ − 151 #endif
+ − 152 xfree (cache, struct gc_cache *);
+ − 153 }
+ − 154
+ − 155 GC
+ − 156 gc_cache_lookup (struct gc_cache *cache, XGCValues *gcv, unsigned long mask)
+ − 157 {
+ − 158 struct gc_cache_cell *cell, *next, *prev;
+ − 159 struct gcv_and_mask gcvm;
+ − 160
3094
+ − 161 #ifdef DEBUG_XEMACS
+ − 162 (void) describe_gc_cache (cache, DGCCFLAG_DISABLE);
+ − 163 #endif
+ − 164
2587
+ − 165 if ((!!cache->head) != (!!cache->tail)) ABORT ();
+ − 166 if (cache->head && (cache->head->prev || cache->tail->next)) ABORT ();
+ − 167
+ − 168 gcvm.mask = mask;
+ − 169 gcvm.gcv = *gcv; /* this copies... */
+ − 170
+ − 171 #ifdef GCCACHE_HASH
+ − 172
+ − 173 /* The intermediate cast fools gcc into not outputting strict-aliasing
+ − 174 complaints */
+ − 175 if (gethash (&gcvm, cache->table, (const void **) (void *) &cell))
+ − 176
+ − 177 #else /* !GCCACHE_HASH */
+ − 178
+ − 179 cell = cache->tail; /* start at the end (most recently used) */
+ − 180 while (cell)
+ − 181 {
+ − 182 if (gc_cache_eql (&gcvm, &cell->gcvm))
+ − 183 break;
+ − 184 else
+ − 185 cell = cell->prev;
+ − 186 }
+ − 187
+ − 188 /* #### This whole file needs some serious overhauling. */
+ − 189 if (!(mask | GCTile) && cell->gc->values.tile)
+ − 190 cell = 0;
+ − 191 else if (!(mask | GCStipple) && cell->gc->values.stipple)
+ − 192 cell = 0;
+ − 193
+ − 194 if (cell)
+ − 195
+ − 196 #endif /* !GCCACHE_HASH */
+ − 197
+ − 198 {
+ − 199 /* Found a cell. Move this cell to the end of the list, so that it
+ − 200 will be less likely to be collected than a cell that was accessed
+ − 201 less recently.
+ − 202 */
3094
+ − 203 #if 0
+ − 204 debug_out ("Returning cached GC: %08lx\n", XE_GCONTEXT(cell));
+ − 205 #endif
2587
+ − 206 if (cell == cache->tail)
+ − 207 return cell->gc;
+ − 208
+ − 209 next = cell->next;
+ − 210 prev = cell->prev;
+ − 211 if (prev) prev->next = next;
+ − 212 if (next) next->prev = prev;
+ − 213 if (cache->head == cell) cache->head = next;
+ − 214 cell->next = 0;
+ − 215 cell->prev = cache->tail;
+ − 216 cache->tail->next = cell;
+ − 217 cache->tail = cell;
+ − 218 if (cache->head == cell) ABORT ();
+ − 219 if (cell->next) ABORT ();
+ − 220 if (cache->head->prev) ABORT ();
+ − 221 if (cache->tail->next) ABORT ();
+ − 222 return cell->gc;
+ − 223 }
+ − 224
+ − 225 /* else, cache miss. */
+ − 226
+ − 227 if (cache->size == GC_CACHE_SIZE)
+ − 228 /* Reuse the first cell on the list (least-recently-used).
+ − 229 Remove it from the list, and unhash it from the table.
+ − 230 */
+ − 231 {
+ − 232 cell = cache->head;
+ − 233 cache->head = cell->next;
+ − 234 cache->head->prev = 0;
+ − 235 if (cache->tail == cell) cache->tail = 0; /* only one */
3243
+ − 236 #if 0
3094
+ − 237 debug_out ("Cache full, freeing GC: %08lx\n ", XE_GCONTEXT(cell));
3243
+ − 238 #endif
2587
+ − 239 XFreeGC (cache->dpy, cell->gc);
+ − 240 cache->delete_count++;
+ − 241 #ifdef GCCACHE_HASH
+ − 242 remhash (&cell->gcvm, cache->table);
+ − 243 #endif
+ − 244 }
+ − 245 else if (cache->size > GC_CACHE_SIZE)
+ − 246 ABORT ();
+ − 247 else
+ − 248 {
+ − 249 /* Allocate a new cell (don't put it in the list or table yet). */
+ − 250 cell = xnew (struct gc_cache_cell);
+ − 251 cache->size++;
+ − 252 }
+ − 253
+ − 254 /* Now we've got a cell (new or reused). Fill it in. */
+ − 255 memcpy (&cell->gcvm.gcv, gcv, sizeof (XGCValues));
+ − 256 cell->gcvm.mask = mask;
+ − 257
+ − 258 /* Put the cell on the end of the list. */
+ − 259 cell->next = 0;
+ − 260 cell->prev = cache->tail;
+ − 261 if (cache->tail) cache->tail->next = cell;
+ − 262 cache->tail = cell;
+ − 263 if (! cache->head) cache->head = cell;
+ − 264
+ − 265 cache->create_count++;
+ − 266 #ifdef GCCACHE_HASH
+ − 267 /* Hash it in the table */
+ − 268 puthash (&cell->gcvm, cell, cache->table);
+ − 269 #endif
+ − 270
+ − 271 /* Now make and return the GC. */
+ − 272 cell->gc = XCreateGC (cache->dpy, cache->window, mask, gcv);
+ − 273
+ − 274 /* debug */
+ − 275 assert (cell->gc == gc_cache_lookup (cache, gcv, mask));
+ − 276
3094
+ − 277 #if 0
+ − 278 debug_out ("Returning new GC: %08lx\n ", XE_GCONTEXT(cell));
+ − 279 #endif
2587
+ − 280 return cell->gc;
+ − 281 }
+ − 282
+ − 283
+ − 284 #ifdef DEBUG_XEMACS
+ − 285
3094
+ − 286 /* FLAGS
+ − 287 The flags argument is a bitwise or of any of the following:
+ − 288
+ − 289 DGCCFLAG_SUMMARY Summary statistics for cache
+ − 290 DGCCFLAG_LIST_CELLS If summary is being printed, print cell IDs too.
+ − 291 DGCCFLAG_CELL_DETAILS If cell IDs are being printed, additionally
+ − 292 print the internal fields used and values.
+ − 293
+ − 294 DGCCFLAG_DEFAULT A predefined combination giving whatever the
+ − 295 maintainers are currently interested in seeing.
+ − 296 */
2587
+ − 297 void
3094
+ − 298 describe_gc_cache (struct gc_cache *cache, int flags)
2587
+ − 299 {
+ − 300 int count = 0;
+ − 301 struct gc_cache_cell *cell = cache->head;
3094
+ − 302
+ − 303 if (! flags & DGCCFLAG_SUMMARY) return;
+ − 304
2587
+ − 305 stderr_out ("\nsize: %d", cache->size);
+ − 306 stderr_out ("\ncreated: %d", cache->create_count);
+ − 307 stderr_out ("\ndeleted: %d", cache->delete_count);
+ − 308
3094
+ − 309 if (flags & DGCCFLAG_LIST_CELLS)
+ − 310 while (cell)
+ − 311 {
+ − 312 struct gc_cache_cell *cell2;
+ − 313 int i = 0;
+ − 314 stderr_out ("\n%d:\t0x%lx GC: 0x%08lx hash: 0x%08lx\n",
+ − 315 count, (long) cell, (long) XE_GCONTEXT(cell),
+ − 316 gc_cache_hash (&cell->gcvm));
+ − 317
+ − 318 for (cell2 = cache->head; cell2; cell2 = cell2->next, i++)
+ − 319 if (count != i &&
+ − 320 gc_cache_hash (&cell->gcvm) == gc_cache_hash (&cell2->gcvm))
+ − 321 stderr_out ("\tHASH COLLISION with cell %d\n", i);
+ − 322 stderr_out ("\tmask: %8lx\n", cell->gcvm.mask);
+ − 323
+ − 324 if (flags & DGCCFLAG_CELL_DETAILS)
+ − 325 {
2587
+ − 326 #define FROB(field) do { \
+ − 327 if ((int)cell->gcvm.gcv.field != (~0)) \
+ − 328 stderr_out ("\t%-12s%8x\n", #field ":", (int)cell->gcvm.gcv.field); \
+ − 329 } while (0)
3094
+ − 330 FROB (function);
+ − 331 FROB (plane_mask);
+ − 332 FROB (foreground);
+ − 333 FROB (background);
+ − 334 FROB (line_width);
+ − 335 FROB (line_style);
+ − 336 FROB (cap_style);
+ − 337 FROB (join_style);
+ − 338 FROB (fill_style);
+ − 339 FROB (fill_rule);
+ − 340 FROB (arc_mode);
+ − 341 FROB (tile);
+ − 342 FROB (stipple);
+ − 343 FROB (ts_x_origin);
+ − 344 FROB (ts_y_origin);
+ − 345 FROB (font);
+ − 346 FROB (subwindow_mode);
+ − 347 FROB (graphics_exposures);
+ − 348 FROB (clip_x_origin);
+ − 349 FROB (clip_y_origin);
+ − 350 FROB (clip_mask);
+ − 351 FROB (dash_offset);
2587
+ − 352 #undef FROB
3094
+ − 353 }
2587
+ − 354
3094
+ − 355 count++;
+ − 356 if (cell->next && cell == cache->tail)
+ − 357 stderr_out ("\nERROR! tail is here!\n\n");
+ − 358 else if (!cell->next && cell != cache->tail)
+ − 359 stderr_out ("\nERROR! tail is not at the end\n\n");
+ − 360 cell = cell->next;
+ − 361 } /* while (cell) */
+ − 362
2587
+ − 363 if (count != cache->size)
+ − 364 stderr_out ("\nERROR! count should be %d\n\n", cache->size);
+ − 365 }
+ − 366
+ − 367 #endif /* DEBUG_XEMACS */