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