1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
13 #include "assassin.hh"
14 #include "screeninfo.hh"
18 BGCCacheContext::~BGCCacheContext(void) {
20 XFreeGC(OBDisplay::display, gc);
24 void BGCCacheContext::set(const BColor &_color,
25 const XFontStruct * const _font,
26 const int _function, const int _subwindow,
29 pixel = gcv.foreground = _color.pixel();
30 function = gcv.function = _function;
31 subwindow = gcv.subwindow_mode = _subwindow;
32 linewidth = gcv.line_width = _linewidth;
33 gcv.cap_style = CapProjecting;
35 unsigned long mask = GCForeground | GCFunction | GCSubwindowMode |
36 GCLineWidth | GCCapStyle;
39 fontid = gcv.font = _font->fid;
45 XChangeGC(OBDisplay::display, gc, mask, &gcv);
49 void BGCCacheContext::set(const XFontStruct * const _font) {
56 fontid = gcv.font = _font->fid;
57 XChangeGC(OBDisplay::display, gc, GCFont, &gcv);
61 BGCCache::BGCCache(unsigned int screen_count)
62 : context_count(128u), cache_size(16u), cache_buckets(8u * screen_count),
63 cache_total_size(cache_size * cache_buckets) {
65 contexts = new BGCCacheContext*[context_count];
67 for (i = 0; i < context_count; i++) {
68 contexts[i] = new BGCCacheContext();
71 cache = new BGCCacheItem*[cache_total_size];
72 for (i = 0; i < cache_total_size; ++i) {
73 cache[i] = new BGCCacheItem;
78 BGCCache::~BGCCache(void) {
79 std::for_each(contexts, contexts + context_count, PointerAssassin());
80 std::for_each(cache, cache + cache_total_size, PointerAssassin());
86 BGCCacheContext *BGCCache::nextContext(unsigned int scr) {
87 Window hd = OBDisplay::screenInfo(scr)->getRootWindow();
91 for (unsigned int i = 0; i < context_count; ++i) {
95 c->gc = XCreateGC(OBDisplay::display, hd, 0, 0);
99 if (! c->used && c->screen == scr)
103 fprintf(stderr, "BGCCache: context fault!\n");
105 return (BGCCacheContext*) 0; // not reached
109 void BGCCache::release(BGCCacheContext *ctx) {
114 BGCCacheItem *BGCCache::find(const BColor &_color,
115 const XFontStruct * const _font,
116 int _function, int _subwindow, int _linewidth) {
117 const unsigned long pixel = _color.pixel();
118 const unsigned int screen = _color.screen();
119 const int key = _color.red() ^ _color.green() ^ _color.blue();
120 int k = (key % cache_size) * cache_buckets;
121 unsigned int i = 0; // loop variable
122 BGCCacheItem *c = cache[ k ], *prev = 0;
125 this will either loop cache_buckets times then return/abort or
126 it will stop matching
129 (c->ctx->pixel != pixel || c->ctx->function != _function ||
130 c->ctx->subwindow != _subwindow || c->ctx->screen != screen ||
131 c->ctx->linewidth != _linewidth)) {
132 if (i < (cache_buckets - 1)) {
138 if (c->count == 0 && c->ctx->screen == screen) {
139 // use this cache item
140 c->ctx->set(_color, _font, _function, _subwindow, _linewidth);
147 fprintf(stderr, "BGCCache: cache fault, count: %d, screen: %d, item screen: %d\n", c->count, screen, c->ctx->screen);
152 // reuse existing context
153 if (_font && _font->fid && _font->fid != c->ctx->fontid)
157 if (prev && c->hits > prev->hits) {
162 c->ctx = nextContext(screen);
163 c->ctx->set(_color, _font, _function, _subwindow, _linewidth);
173 void BGCCache::release(BGCCacheItem *_item) {
178 void BGCCache::purge(void) {
179 for (unsigned int i = 0; i < cache_total_size; ++i) {
180 BGCCacheItem *d = cache[ i ];
182 if (d->ctx && d->count == 0) {