]> Dogcows Code - chaz/openbox/commitdiff
add rendercolor class, with a cache of gcs for the colors
authorDana Jansens <danakj@orodu.net>
Mon, 20 Jan 2003 07:07:13 +0000 (07:07 +0000)
committerDana Jansens <danakj@orodu.net>
Mon, 20 Jan 2003 07:07:13 +0000 (07:07 +0000)
otk/.cvsignore
otk/Makefile.am
otk/application.cc
otk/rendercolor.cc [new file with mode: 0644]
otk/rendercolor.hh [new file with mode: 0644]
otk/surface.cc
otk/surface.hh
otk/truerendercontrol.cc
src/openbox.cc

index d46a65b59c830f1678db37c695c3da81fe093fc9..3e85c1e52e25224615899295b8deb4491b194fbb 100644 (file)
@@ -37,3 +37,4 @@ rendertexture.lo
 rendertest
 renderstyle.lo
 rendercontrol.lo
+rendercolor.lo
index dd70fa7928b8fa83d0e063b3098d8b6ad24961e6..643f4d2f022ab5e331e9b2337452e4f14b98f912 100644 (file)
@@ -9,7 +9,7 @@ INCLUDES= -I../src
 noinst_LTLIBRARIES=libotk.la
 
 libotk_la_SOURCES=rendercontrol.cc truerendercontrol.cc surface.cc \
-                  rendertexture.cc renderstyle.cc \
+                  rendertexture.cc renderstyle.cc rendercolor.cc  \
                   color.cc display.cc font.cc gccache.cc image.cc \
                   property.cc imagecontrol.cc rect.cc screeninfo.cc \
                   texture.cc timer.cc style.cc \
index f101a5a3f7e6ebf1f74f22afa1b95ac31770b98e..e325a58218bc824557bcf4b5573a85b4b9bff9f3 100644 (file)
@@ -9,6 +9,7 @@
 #include "widget.hh"
 #include "timer.hh"
 #include "property.hh"
+#include "rendercolor.hh"
 
 extern "C" {
 #ifdef HAVE_STDLIB_H
@@ -32,6 +33,7 @@ Application::Application(int argc, char **argv)
   const ScreenInfo *s_info = _display.screenInfo(DefaultScreen(*_display));
 
   Timer::initialize();
+  RenderColor::initialize();
   Property::initialize();
   _img_ctrl = new ImageControl(s_info, True, 4, 5, 200);
   _style_conf = new Configuration(False);
@@ -45,6 +47,7 @@ Application::~Application()
   delete _style_conf;
   delete _img_ctrl;
   delete _style;
+  RenderColor::destroy();
   Timer::destroy();
 }
 
diff --git a/otk/rendercolor.cc b/otk/rendercolor.cc
new file mode 100644 (file)
index 0000000..99dd334
--- /dev/null
@@ -0,0 +1,83 @@
+// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
+
+#ifdef    HAVE_CONFIG_H
+#  include "../config.h"
+#endif // HAVE_CONFIG_H
+
+#include "rendercolor.hh"
+#include "display.hh"
+#include "screeninfo.hh"
+
+namespace otk {
+
+std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
+
+void RenderColor::initialize()
+{
+  _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
+}
+
+void RenderColor::destroy()
+{
+  delete [] _cache;
+}
+  
+RenderColor::RenderColor(int screen, unsigned char red,
+                        unsigned char green, unsigned char blue)
+  : _screen(screen),
+    _red(red),
+    _green(green),
+    _blue(blue),
+    _gc(0)
+{
+  unsigned long color = _blue | _green << 8 | _red << 16;
+  
+  // try get a gc from the cache
+  CacheItem *item = _cache[_screen][color];
+
+  if (item) {
+    _gc = item->gc;
+    ++item->count;
+  } else {
+    XGCValues gcv;
+
+    // allocate a color and GC from the server
+    const ScreenInfo *info = display->screenInfo(_screen);
+
+    XColor xcol;    // convert from 0-0xff to 0-0xffff
+    xcol.red = _red; xcol.red |= xcol.red << 8;
+    xcol.green = _green; xcol.green |= xcol.green << 8;
+    xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
+    xcol.pixel = 0;
+
+    if (! XAllocColor(**display, info->colormap(), &xcol)) {
+      fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
+             _red, _green, _blue);
+      xcol.pixel = 0;
+    }
+
+    gcv.foreground = xcol.pixel;
+    _gc = XCreateGC(**display, info->rootWindow(), GCForeground, &gcv);
+    assert(_gc);
+
+    // insert into the cache
+    _cache[_screen][color] = new CacheItem(_gc);
+  }
+}
+
+RenderColor::~RenderColor()
+{
+  unsigned long color = _blue | _green << 8 | _red << 16;
+  
+  CacheItem *item = _cache[_screen][color];
+  assert(item); // it better be in the cache ...
+
+  if (--item->count <= 0) {
+    // remove from the cache
+    XFreeGC(**display, _gc);
+    _cache[_screen][color] = 0;
+    delete item;
+  }
+}
+
+}
diff --git a/otk/rendercolor.hh b/otk/rendercolor.hh
new file mode 100644 (file)
index 0000000..fbfe2ae
--- /dev/null
@@ -0,0 +1,45 @@
+// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
+#ifndef __rendercolor_hh
+#define __rendercolor_hh
+
+extern "C" {
+#include <X11/Xlib.h>
+}
+
+#include <map>
+
+namespace otk {
+
+class RenderColor {
+  struct CacheItem {
+    GC gc;
+    int count;
+    CacheItem(GC g) : gc(g), count(0) {}
+  };
+  static std::map<unsigned long, CacheItem*> *_cache;
+
+  int _screen;
+  unsigned char _red;
+  unsigned char _green;
+  unsigned char _blue;
+
+  GC _gc;
+
+public:
+  static void initialize();
+  static void destroy();
+  
+  RenderColor(int screen, unsigned char red,
+             unsigned char green, unsigned char blue);
+  virtual ~RenderColor();
+
+  inline int screen() const { return _screen; }
+  inline unsigned char red() const { return _red; }
+  inline unsigned char green() const { return _green; }
+  inline unsigned char blue() const { return _blue; }
+  inline GC gc() const { return _gc; }
+};
+
+}
+
+#endif // __rendercolor_hh
index 99fa82b04bf7e329569c7e71278378d7c2782b00..4f6ef3862453015fddc0c53ae89d1d0a5f537b47 100644 (file)
@@ -7,7 +7,7 @@
 #include "surface.hh"
 #include "display.hh"
 #include "screeninfo.hh"
-#include "gccache.hh"
+#include "rendercolor.hh"
 
 extern "C" {
 #include <X11/Xutil.h>
@@ -28,13 +28,12 @@ Surface::~Surface()
   destroyObjects();
 }
 
-void Surface::setPixmap(const Color &color)
+void Surface::setPixmap(const RenderColor &color)
 {
   if (_pixmap == None)
     createObjects();
 
-  Pen p(color);
-  XFillRectangle(**display, _pixmap, p.gc(), 0, 0,
+  XFillRectangle(**display, _pixmap, color.gc(), 0, 0,
                  _size.x(), _size.y());
 }
 
index d325b3937a59da6c3f93217a9e1e1da53d6cd0c0..18733517aaefc52a10fdb493fea5a7081f41e87e 100644 (file)
@@ -14,6 +14,7 @@ extern "C" {
 namespace otk {
 
 class ScreenInfo;
+class RenderColor;
 
 class Surface {
   int _screen;
@@ -26,7 +27,7 @@ protected:
   void destroyObjects();
 
   void setPixmap(XImage *image);
-  void setPixmap(const Color &color);
+  void setPixmap(const RenderColor &color);
   
 public:
   Surface(int screen, const Point &size);
index bd413cff8974eb10492159bf3c2e4196f919083a..dc52bc37bb43a088cb4b6036839a625516b0e961 100644 (file)
@@ -9,6 +9,8 @@
 #include "screeninfo.hh"
 #include "surface.hh"
 
+#include "rendercolor.hh"
+
 extern "C" {
 #ifdef    HAVE_STDLIB_H
 #  include <stdlib.h>
@@ -127,7 +129,8 @@ void TrueRenderControl::drawBackground(Surface& sf,
 
   im->data = (char*) data;
 
-  sf.setPixmap(im);
+//  sf.setPixmap(im);
+  sf.setPixmap(RenderColor(_screen, 0xff, 0xff, 0));
 
   delete [] im->data;
   im->data = NULL;
index 9642cd062f4354efcbf6a5642c32e235157876ca..6436140db239210613b5f299a450b4062754bae9 100644 (file)
@@ -14,6 +14,7 @@
 #include "otk/assassin.hh"
 #include "otk/property.hh"
 #include "otk/util.hh"
+#include "otk/rendercolor.hh"
 
 extern "C" {
 #include <X11/cursorfont.h>
@@ -126,6 +127,7 @@ Openbox::Openbox(int argc, char **argv)
   // anything that died while we were restarting won't give us a SIGCHLD
   while (waitpid(-1, NULL, WNOHANG) > 0);
 
+  otk::RenderColor::initialize();
   otk::Timer::initialize();
   otk::Property::initialize();
   _actions = new Actions();
@@ -208,6 +210,7 @@ Openbox::~Openbox()
   //otk::display->destroy();
 
   otk::Timer::destroy();
+  otk::RenderColor::destroy();
 
   if (_restart) {
     if (!_restart_prog.empty()) {
This page took 0.033045 seconds and 4 git commands to generate.