]> Dogcows Code - chaz/openbox/blob - otk/rendercolor.cc
add rendercolor class, with a cache of gcs for the colors
[chaz/openbox] / otk / rendercolor.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "rendercolor.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10
11 namespace otk {
12
13 std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
14
15 void RenderColor::initialize()
16 {
17 _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
18 }
19
20 void RenderColor::destroy()
21 {
22 delete [] _cache;
23 }
24
25 RenderColor::RenderColor(int screen, unsigned char red,
26 unsigned char green, unsigned char blue)
27 : _screen(screen),
28 _red(red),
29 _green(green),
30 _blue(blue),
31 _gc(0)
32 {
33 unsigned long color = _blue | _green << 8 | _red << 16;
34
35 // try get a gc from the cache
36 CacheItem *item = _cache[_screen][color];
37
38 if (item) {
39 _gc = item->gc;
40 ++item->count;
41 } else {
42 XGCValues gcv;
43
44 // allocate a color and GC from the server
45 const ScreenInfo *info = display->screenInfo(_screen);
46
47 XColor xcol; // convert from 0-0xff to 0-0xffff
48 xcol.red = _red; xcol.red |= xcol.red << 8;
49 xcol.green = _green; xcol.green |= xcol.green << 8;
50 xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
51 xcol.pixel = 0;
52
53 if (! XAllocColor(**display, info->colormap(), &xcol)) {
54 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
55 _red, _green, _blue);
56 xcol.pixel = 0;
57 }
58
59 gcv.foreground = xcol.pixel;
60 _gc = XCreateGC(**display, info->rootWindow(), GCForeground, &gcv);
61 assert(_gc);
62
63 // insert into the cache
64 _cache[_screen][color] = new CacheItem(_gc);
65 }
66 }
67
68 RenderColor::~RenderColor()
69 {
70 unsigned long color = _blue | _green << 8 | _red << 16;
71
72 CacheItem *item = _cache[_screen][color];
73 assert(item); // it better be in the cache ...
74
75 if (--item->count <= 0) {
76 // remove from the cache
77 XFreeGC(**display, _gc);
78 _cache[_screen][color] = 0;
79 delete item;
80 }
81 }
82
83 }
This page took 0.045172 seconds and 5 git commands to generate.