]> icculus.org git repositories - mikachu/openbox.git/blob - otk/truerendercontrol.cc
add rendercolor class, with a cache of gcs for the colors
[mikachu/openbox.git] / otk / truerendercontrol.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 "truerendercontrol.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10 #include "surface.hh"
11
12 #include "rendercolor.hh"
13
14 extern "C" {
15 #ifdef    HAVE_STDLIB_H
16 #  include <stdlib.h>
17 #endif // HAVE_STDLIB_H
18
19 #include "gettext.h"
20 #define _(str) gettext(str)
21 }
22
23 namespace otk {
24
25 TrueRenderControl::TrueRenderControl(int screen)
26   : RenderControl(screen),
27     _red_offset(0),
28     _green_offset(0),
29     _blue_offset(0)
30 {
31   printf("Initializing TrueColor RenderControl\n");
32
33   Visual *visual = display->screenInfo(_screen)->visual();
34   unsigned long red_mask, green_mask, blue_mask;
35
36   // find the offsets for each color in the visual's masks
37   red_mask = visual->red_mask;
38   green_mask = visual->green_mask;
39   blue_mask = visual->blue_mask;
40
41   while (! (red_mask & 1))   { _red_offset++;   red_mask   >>= 1; }
42   while (! (green_mask & 1)) { _green_offset++; green_mask >>= 1; }
43   while (! (blue_mask & 1))  { _blue_offset++;  blue_mask  >>= 1; }
44
45   _red_shift = _green_shift = _blue_shift = 8;
46   while (red_mask)   { red_mask   >>= 1; _red_shift--;   }
47   while (green_mask) { green_mask >>= 1; _green_shift--; }
48   while (blue_mask)  { blue_mask  >>= 1; _blue_shift--;  }
49 }
50
51 TrueRenderControl::~TrueRenderControl()
52 {
53   printf("Destroying TrueColor RenderControl\n");
54
55
56 }
57
58
59 static inline void renderPixel(XImage *im, unsigned char *dp,
60                                unsigned long pixel)
61 {
62   unsigned int bpp = im->bits_per_pixel + (im->byte_order == MSBFirst ? 1 : 0);
63
64   switch (bpp) {
65   case  8: //  8bpp
66     *dp++ = pixel;
67     break;
68   case 16: // 16bpp LSB
69     *dp++ = pixel;
70     *dp++ = pixel >> 8;
71     break;
72   case 17: // 16bpp MSB
73     *dp++ = pixel >> 8;
74     *dp++ = pixel;
75     break;
76   case 24: // 24bpp LSB
77     *dp++ = pixel;
78     *dp++ = pixel >> 8;
79     *dp++ = pixel >> 16;
80     break;
81   case 25: // 24bpp MSB
82     *dp++ = pixel >> 16;
83     *dp++ = pixel >> 8;
84     *dp++ = pixel;
85     break;
86   case 32: // 32bpp LSB
87     *dp++ = pixel;
88     *dp++ = pixel >> 8;
89     *dp++ = pixel >> 16;
90     *dp++ = pixel >> 24;
91     break;
92   case 33: // 32bpp MSB
93     *dp++ = pixel >> 24;
94     *dp++ = pixel >> 16;
95     *dp++ = pixel >> 8;
96     *dp++ = pixel;
97     break;
98   default:
99     assert(false); // wtf?
100   }
101 }
102
103 void TrueRenderControl::drawBackground(Surface& sf,
104                                        const RenderTexture &texture) const
105 {
106   (void)texture;
107
108   assert(sf._screen == _screen);
109   
110   int w = sf.width(), h = sf.height();
111
112   const ScreenInfo *info = display->screenInfo(_screen);
113   XImage *im = XCreateImage(**display, info->visual(), info->depth(),
114                             ZPixmap, 0, NULL, w, h, 32, 0);
115   
116   unsigned char *data = new unsigned char[im->bytes_per_line * h];
117   unsigned char *dp = data;
118   unsigned int bytes_per_pixel = im->bits_per_pixel/8;
119
120   for (int y = 0; y < h/3; ++y)
121     for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
122       renderPixel(im, dp, (255*x/w) >> _red_shift << _red_offset);
123   for (int y = 0; y < h/3; ++y)
124     for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
125       renderPixel(im, dp, (255*x/w) >> _green_shift << _green_offset);
126   for (int y = 0; y < h/3; ++y)
127     for (int x = 0; x < w; ++x, dp += bytes_per_pixel)
128       renderPixel(im, dp, (255*x/w) >> _blue_shift << _blue_offset);
129
130   im->data = (char*) data;
131
132 //  sf.setPixmap(im);
133   sf.setPixmap(RenderColor(_screen, 0xff, 0xff, 0));
134
135   delete [] im->data;
136   im->data = NULL;
137   XDestroyImage(im);}
138 }