1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
12 #include "basedisplay.hh"
17 BGCCacheContext::~BGCCacheContext(void) {
19 XFreeGC(display->getXDisplay(), gc);
23 void BGCCacheContext::set(const BColor &_color,
24 const XFontStruct * const _font,
25 const int _function, const int _subwindow,
28 pixel = gcv.foreground = _color.pixel();
29 function = gcv.function = _function;
30 subwindow = gcv.subwindow_mode = _subwindow;
31 linewidth = gcv.line_width = _linewidth;
32 gcv.cap_style = CapProjecting;
34 unsigned long mask = GCForeground | GCFunction | GCSubwindowMode |
35 GCLineWidth | GCCapStyle;
38 fontid = gcv.font = _font->fid;
44 XChangeGC(display->getXDisplay(), gc, mask, &gcv);
48 void BGCCacheContext::set(const XFontStruct * const _font) {
55 fontid = gcv.font = _font->fid;
56 XChangeGC(display->getXDisplay(), gc, GCFont, &gcv);
60 BGCCache::BGCCache(const BaseDisplay * const _display,
61 unsigned int screen_count)
62 : display(_display), context_count(128u),
63 cache_size(16u), cache_buckets(8u * screen_count),
64 cache_total_size(cache_size * cache_buckets) {
66 contexts = new BGCCacheContext*[context_count];
68 for (i = 0; i < context_count; i++) {
69 contexts[i] = new BGCCacheContext(display);
72 cache = new BGCCacheItem*[cache_total_size];
73 for (i = 0; i < cache_total_size; ++i) {
74 cache[i] = new BGCCacheItem;
79 BGCCache::~BGCCache(void) {
80 std::for_each(contexts, contexts + context_count, PointerAssassin());
81 std::for_each(cache, cache + cache_total_size, PointerAssassin());
87 BGCCacheContext *BGCCache::nextContext(unsigned int scr) {
88 Window hd = display->getScreenInfo(scr)->getRootWindow();
92 for (unsigned int i = 0; i < context_count; ++i) {
96 c->gc = XCreateGC(display->getXDisplay(), hd, 0, 0);
100 if (! c->used && c->screen == scr)
104 fprintf(stderr, "BGCCache: context fault!\n");
106 return (BGCCacheContext*) 0; // not reached
110 void BGCCache::release(BGCCacheContext *ctx) {
115 BGCCacheItem *BGCCache::find(const BColor &_color,
116 const XFontStruct * const _font,
117 int _function, int _subwindow, int _linewidth) {
118 const unsigned long pixel = _color.pixel();
119 const unsigned int screen = _color.screen();
120 const int key = _color.red() ^ _color.green() ^ _color.blue();
121 int k = (key % cache_size) * cache_buckets;
122 unsigned int i = 0; // loop variable
123 BGCCacheItem *c = cache[ k ], *prev = 0;
126 this will either loop cache_buckets times then return/abort or
127 it will stop matching
130 (c->ctx->pixel != pixel || c->ctx->function != _function ||
131 c->ctx->subwindow != _subwindow || c->ctx->screen != screen ||
132 c->ctx->linewidth != _linewidth)) {
133 if (i < (cache_buckets - 1)) {
139 if (c->count == 0 && c->ctx->screen == screen) {
140 // use this cache item
141 c->ctx->set(_color, _font, _function, _subwindow, _linewidth);
148 fprintf(stderr, "BGCCache: cache fault, count: %d, screen: %d, item screen: %d\n", c->count, screen, c->ctx->screen);
153 // reuse existing context
154 if (_font && _font->fid && _font->fid != c->ctx->fontid)
158 if (prev && c->hits > prev->hits) {
163 c->ctx = nextContext(screen);
164 c->ctx->set(_color, _font, _function, _subwindow, _linewidth);
174 void BGCCache::release(BGCCacheItem *_item) {
179 void BGCCache::purge(void) {
180 for (unsigned int i = 0; i < cache_total_size; ++i) {
181 BGCCacheItem *d = cache[ i ];
183 if (d->ctx && d->count == 0) {