]> Dogcows Code - chaz/openbox/blob - otk/surface.cc
free the surfaces' pixeldata after rendering it
[chaz/openbox] / otk / surface.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "surface.hh"
6 #include "display.hh"
7 #include "screeninfo.hh"
8 #include "rendercolor.hh"
9
10 extern "C" {
11 #include <X11/Xutil.h>
12 #include <cstring>
13 }
14
15 namespace otk {
16
17 Surface::Surface(int screen, const Size &size)
18 : _screen(screen),
19 _size(size),
20 _pixel_data(new pixel32[size.width()*size.height()]),
21 _pixmap(None),
22 _xftdraw(0)
23 {
24 }
25
26 Surface::~Surface()
27 {
28 destroyObjects();
29 freePixelData();
30 }
31
32 void Surface::freePixelData()
33 {
34 if (_pixel_data) {
35 delete [] _pixel_data;
36 _pixel_data = 0;
37 }
38 }
39
40 void Surface::setPixmap(const RenderColor &color)
41 {
42 assert(_pixel_data);
43 if (_pixmap == None)
44 createObjects();
45
46 XFillRectangle(**display, _pixmap, color.gc(), 0, 0,
47 _size.width(), _size.height());
48
49 pixel32 val = 0; // XXX set this from the color and shift amounts!
50 for (unsigned int i = 0, s = _size.width() * _size.height(); i < s; ++i) {
51 unsigned char *p = (unsigned char*)&_pixel_data[i];
52 *p = (unsigned char) (val >> 24);
53 *++p = (unsigned char) (val >> 16);
54 *++p = (unsigned char) (val >> 8);
55 *++p = (unsigned char) val;
56 }
57 }
58
59 void Surface::setPixmap(XImage *image)
60 {
61 assert(_pixel_data);
62 assert(image->width == _size.width());
63 assert(image->height == _size.height());
64
65 if (_pixmap == None)
66 createObjects();
67
68 XPutImage(**display, _pixmap, DefaultGC(**display, _screen),
69 image, 0, 0, 0, 0, _size.width(), _size.height());
70 }
71
72 void Surface::createObjects()
73 {
74 assert(_pixmap == None); assert(!_xftdraw);
75
76 const ScreenInfo *info = display->screenInfo(_screen);
77
78 _pixmap = XCreatePixmap(**display, info->rootWindow(),
79 _size.width(), _size.height(), info->depth());
80 assert(_pixmap != None);
81
82 _xftdraw = XftDrawCreate(**display, _pixmap,
83 info->visual(), info->colormap());
84 assert(_xftdraw);
85 }
86
87 void Surface::destroyObjects()
88 {
89 if (_xftdraw) {
90 XftDrawDestroy(_xftdraw);
91 _xftdraw = 0;
92 }
93
94 if (_pixmap != None) {
95 XFreePixmap(**display, _pixmap);
96 _pixmap = None;
97 }
98 }
99
100 }
This page took 0.035159 seconds and 4 git commands to generate.