1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // GCCache.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh at debian.org>
4 // Copyright (c) 1997 - 2000, 2002 Bradley T Hughes <bhughes at trolltech.com>
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
33 #include "BaseDisplay.hh"
38 BGCCacheContext::~BGCCacheContext(void) {
40 XFreeGC(display->getXDisplay(), gc);
44 void BGCCacheContext::set(const BColor &_color,
45 const XFontStruct * const _font,
46 const int _function, const int _subwindow,
49 pixel = gcv.foreground = _color.pixel();
50 function = gcv.function = _function;
51 subwindow = gcv.subwindow_mode = _subwindow;
52 linewidth = gcv.line_width = _linewidth;
53 gcv.cap_style = CapProjecting;
55 unsigned long mask = GCForeground | GCFunction | GCSubwindowMode |
56 GCLineWidth | GCCapStyle;
59 fontid = gcv.font = _font->fid;
65 XChangeGC(display->getXDisplay(), gc, mask, &gcv);
69 void BGCCacheContext::set(const XFontStruct * const _font) {
76 fontid = gcv.font = _font->fid;
77 XChangeGC(display->getXDisplay(), gc, GCFont, &gcv);
81 BGCCache::BGCCache(const BaseDisplay * const _display)
82 : display(_display), context_count(128u),
83 cache_size(16u), cache_buckets(8u),
84 cache_total_size(cache_size * cache_buckets) {
86 contexts = new BGCCacheContext*[context_count];
88 for (i = 0; i < context_count; i++) {
89 contexts[i] = new BGCCacheContext(display);
92 cache = new BGCCacheItem*[cache_total_size];
93 for (i = 0; i < cache_total_size; ++i) {
94 cache[i] = new BGCCacheItem;
99 BGCCache::~BGCCache(void) {
100 std::for_each(contexts, contexts + context_count, PointerAssassin());
101 std::for_each(cache, cache + cache_total_size, PointerAssassin());
107 BGCCacheContext *BGCCache::nextContext(unsigned int scr) {
108 Window hd = display->getScreenInfo(scr)->getRootWindow();
112 for (unsigned int i = 0; i < context_count; ++i) {
116 c->gc = XCreateGC(display->getXDisplay(), hd, 0, 0);
120 if (! c->used && c->screen == scr) {
126 fprintf(stderr, "BGCCache: context fault!\n");
128 return (BGCCacheContext*) 0; // not reached
132 void BGCCache::release(BGCCacheContext *ctx) {
137 BGCCacheItem *BGCCache::find(const BColor &_color,
138 const XFontStruct * const _font,
139 int _function, int _subwindow, int _linewidth) {
140 const unsigned long pixel = _color.pixel();
141 const unsigned int screen = _color.screen();
142 const int key = _color.red() ^ _color.green() ^ _color.blue();
143 int k = (key % cache_size) * cache_buckets;
144 int i = 0; // loop variable
145 BGCCacheItem *c = cache[ k ], *prev = 0;
147 // this will either loop 8 times then return/abort or it will stop matching
149 (c->ctx->pixel != pixel || c->ctx->function != _function ||
150 c->ctx->subwindow != _subwindow || c->ctx->screen != screen ||
151 c->ctx->linewidth != _linewidth)) {
158 if (c->count == 0 && c->ctx->screen == screen) {
159 // use this cache item
160 c->ctx->set(_color, _font, _function, _subwindow, _linewidth);
167 fprintf(stderr, "BGCCache: cache fault\n");
171 const unsigned long fontid = _font ? _font->fid : 0;
173 // reuse existing context
174 if (fontid && fontid != c->ctx->fontid)
178 if (prev && c->hits > prev->hits) {
183 c->ctx = nextContext(screen);
184 c->ctx->set(_color, _font, _function, _subwindow, _linewidth);
194 void BGCCache::release(BGCCacheItem *_item) {
199 void BGCCache::purge(void) {
200 for (unsigned int i = 0; i < cache_total_size; ++i) {
201 BGCCacheItem *d = cache[ i ];
203 if (d->ctx && d->count == 0) {