]> Dogcows Code - chaz/openbox/blob - otk/rendercolor.cc
save/load the pixel in the cache
[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 create();
34 }
35
36 RenderColor::RenderColor(int screen, RGB rgb)
37 : _screen(screen),
38 _red(rgb.r),
39 _green(rgb.g),
40 _blue(rgb.b),
41 _gc(0)
42 {
43 create();
44 }
45
46 void RenderColor::create()
47 {
48 unsigned long color = _blue | _green << 8 | _red << 16;
49
50 printf("CREATE COLOR: %lx\n", color);
51
52 // try get a gc from the cache
53 CacheItem *item = _cache[_screen][color];
54
55 if (item) {
56 _gc = item->gc;
57 _pixel = item->pixel;
58 ++item->count;
59 } else {
60 XGCValues gcv;
61
62 // allocate a color and GC from the server
63 const ScreenInfo *info = display->screenInfo(_screen);
64
65 XColor xcol; // convert from 0-0xff to 0-0xffff
66 xcol.red = _red; xcol.red |= xcol.red << 8;
67 xcol.green = _green; xcol.green |= xcol.green << 8;
68 xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
69 xcol.pixel = 0;
70
71 if (! XAllocColor(**display, info->colormap(), &xcol)) {
72 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
73 _red, _green, _blue);
74 xcol.pixel = 0;
75 }
76
77 _pixel = xcol.pixel;
78 gcv.foreground = _pixel;
79 gcv.cap_style = CapProjecting;
80 _gc = XCreateGC(**display, info->rootWindow(),
81 GCForeground | GCCapStyle, &gcv);
82 assert(_gc);
83
84 // insert into the cache
85 item = new CacheItem(_gc, _pixel);
86 _cache[_screen][color] = item;
87 ++item->count;
88 }
89 }
90
91 RenderColor::~RenderColor()
92 {
93 unsigned long color = _blue | _green << 8 | _red << 16;
94
95 CacheItem *item = _cache[_screen][color];
96 assert(item); // it better be in the cache ...
97
98 printf("DESTROY COLOR: %lx %d\n", color, item->count);
99
100 if (--item->count <= 0) {
101 // remove from the cache
102 XFreeGC(**display, _gc);
103 _cache[_screen][color] = 0;
104 delete item;
105 }
106 }
107
108 }
This page took 0.041929 seconds and 5 git commands to generate.