free rgba texture caches
[mikachu/openbox.git] / render / render.c
1 #include <X11/Xlib.h>
2 #include <X11/Xutil.h>
3
4 #include "render.h"
5 #include "gradient.h"
6 #include "font.h"
7 #include "mask.h"
8 #include "color.h"
9 #include "image.h"
10 #include "theme.h"
11
12 #include <glib.h>
13
14 #ifdef HAVE_STDLIB_H
15 #  include <stdlib.h>
16 #endif
17
18 static void pixel_data_to_pixmap(RrAppearance *l,
19                                  gint x, gint y, gint w, gint h);
20
21 void RrPaint(RrAppearance *l, Window win, gint w, gint h)
22 {
23     int i, transferred = 0, sw;
24     RrPixel32 *source, *dest;
25     Pixmap oldp;
26     RrRect tarea; /* area in which to draw textures */
27     gboolean resized;
28
29     if (w <= 0 || h <= 0) return;
30
31     resized = (l->w != w || l->h != h);
32
33     oldp = l->pixmap; /* save to free after changing the visible pixmap */
34     l->pixmap = XCreatePixmap(RrDisplay(l->inst),
35                               RrRootWindow(l->inst),
36                               w, h, RrDepth(l->inst));
37
38     g_assert(l->pixmap != None);
39     l->w = w;
40     l->h = h;
41
42     if (l->xftdraw != NULL)
43         XftDrawDestroy(l->xftdraw);
44     l->xftdraw = XftDrawCreate(RrDisplay(l->inst), l->pixmap,
45                                RrVisual(l->inst), RrColormap(l->inst));
46     g_assert(l->xftdraw != NULL);
47
48     g_free(l->surface.pixel_data);
49     l->surface.pixel_data = g_new(RrPixel32, w * h);
50
51     if (l->surface.grad == RR_SURFACE_PARENTREL) {
52         g_assert (l->surface.parent);
53         g_assert (l->surface.parent->w);
54
55         sw = l->surface.parent->w;
56         source = (l->surface.parent->surface.pixel_data +
57                   l->surface.parentx + sw * l->surface.parenty);
58         dest = l->surface.pixel_data;
59         for (i = 0; i < h; i++, source += sw, dest += w) {
60             memcpy(dest, source, w * sizeof(RrPixel32));
61         }
62     } else
63         RrRender(l, w, h);
64
65     RECT_SET(tarea, 0, 0, w, h);
66     if (l->surface.grad != RR_SURFACE_PARENTREL) {
67         if (l->surface.relief != RR_RELIEF_FLAT) {
68             switch (l->surface.bevel) {
69             case RR_BEVEL_1:
70                 tarea.x += 1; tarea.y += 1;
71                 tarea.width -= 2; tarea.height -= 2;
72                 break;
73             case RR_BEVEL_2:
74                 tarea.x += 2; tarea.y += 2;
75                 tarea.width -= 4; tarea.height -= 4;
76                 break;
77             }
78         } else if (l->surface.border) {
79             tarea.x += 1; tarea.y += 1;
80             tarea.width -= 2; tarea.height -= 2;
81         }
82     }
83
84     for (i = 0; i < l->textures; i++) {
85         switch (l->texture[i].type) {
86         case RR_TEXTURE_NONE:
87             break;
88         case RR_TEXTURE_TEXT:
89             if (!transferred) {
90                 transferred = 1;
91                 if (l->surface.grad != RR_SURFACE_SOLID)
92                     pixel_data_to_pixmap(l, 0, 0, w, h);
93             }
94             if (l->xftdraw == NULL) {
95                 l->xftdraw = XftDrawCreate(RrDisplay(l->inst), l->pixmap, 
96                                            RrVisual(l->inst),
97                                            RrColormap(l->inst));
98             }
99             RrFontDraw(l->xftdraw, &l->texture[i].data.text, &tarea);
100         break;
101         case RR_TEXTURE_MASK:
102             if (!transferred) {
103                 transferred = 1;
104                 if (l->surface.grad != RR_SURFACE_SOLID)
105                     pixel_data_to_pixmap(l, 0, 0, w, h);
106             }
107             if (l->texture[i].data.mask.color->gc == None)
108                 RrColorAllocateGC(l->texture[i].data.mask.color);
109             RrPixmapMaskDraw(l->pixmap, &l->texture[i].data.mask, &tarea);
110         break;
111         case RR_TEXTURE_RGBA:
112             RrImageDraw(l->surface.pixel_data,
113                         &l->texture[i].data.rgba, &tarea);
114         break;
115         }
116     }
117
118     if (!transferred) {
119         transferred = 1;
120         if (l->surface.grad != RR_SURFACE_SOLID)
121             pixel_data_to_pixmap(l, 0, 0, w, h);
122     }
123
124
125     XSetWindowBackgroundPixmap(RrDisplay(l->inst), win, l->pixmap);
126     XClearWindow(RrDisplay(l->inst), win);
127     if (oldp) XFreePixmap(RrDisplay(l->inst), oldp);
128 }
129
130 RrAppearance *RrAppearanceNew(const RrInstance *inst, gint numtex)
131 {
132   RrAppearance *out;
133
134   out = g_new0(RrAppearance, 1);
135   out->inst = inst;
136   out->textures = numtex;
137   if (numtex) out->texture = g_new0(RrTexture, numtex);
138
139   return out;
140 }
141
142 RrAppearance *RrAppearanceCopy(RrAppearance *orig)
143 {
144     RrSurface *spo, *spc;
145     RrAppearance *copy = g_new(RrAppearance, 1);
146     gint i;
147
148     copy->inst = orig->inst;
149
150     spo = &(orig->surface);
151     spc = &(copy->surface);
152     spc->grad = spo->grad;
153     spc->relief = spo->relief;
154     spc->bevel = spo->bevel;
155     if (spo->primary != NULL)
156         spc->primary = RrColorNew(copy->inst,
157                                   spo->primary->r,
158                                   spo->primary->g, 
159                                   spo->primary->b);
160     else spc->primary = NULL;
161
162     if (spo->secondary != NULL)
163         spc->secondary = RrColorNew(copy->inst,
164                                     spo->secondary->r,
165                                     spo->secondary->g,
166                                     spo->secondary->b);
167     else spc->secondary = NULL;
168
169     if (spo->border_color != NULL)
170         spc->border_color = RrColorNew(copy->inst,
171                                        spo->border_color->r,
172                                        spo->border_color->g,
173                                        spo->border_color->b);
174     else spc->border_color = NULL;
175
176     if (spo->bevel_dark != NULL)
177         spc->bevel_dark = RrColorNew(copy->inst,
178                                      spo->bevel_dark->r,
179                                      spo->bevel_dark->g,
180                                      spo->bevel_dark->b);
181     else spc->bevel_dark = NULL;
182
183     if (spo->bevel_light != NULL)
184         spc->bevel_light = RrColorNew(copy->inst,
185                                       spo->bevel_light->r,
186                                       spo->bevel_light->g,
187                                       spo->bevel_light->b);
188     else spc->bevel_light = NULL;
189
190     spc->interlaced = spo->interlaced;
191     spc->border = spo->border;
192     spc->parent = NULL;
193     spc->parentx = spc->parenty = 0;
194     spc->pixel_data = NULL;
195
196     copy->textures = orig->textures;
197     copy->texture = g_memdup(orig->texture,
198                              orig->textures * sizeof(RrTexture));
199     for (i = 0; i < copy->textures; ++i)
200         if (copy->texture[i].type == RR_TEXTURE_RGBA) {
201             g_free(copy->texture[i].data.rgba.cache);
202             copy->texture[i].data.rgba.cache = NULL;
203         }
204     copy->pixmap = None;
205     copy->xftdraw = NULL;
206     copy->w = copy->h = 0;
207     return copy;
208 }
209
210 void RrAppearanceFree(RrAppearance *a)
211 {
212     gint i;
213
214     if (a) {
215         RrSurface *p;
216         if (a->pixmap != None) XFreePixmap(RrDisplay(a->inst), a->pixmap);
217         if (a->xftdraw != NULL) XftDrawDestroy(a->xftdraw);
218         for (i = 0; i < a->textures; ++i)
219             if (a->texture[i].type == RR_TEXTURE_RGBA) {
220                 g_free(a->texture[i].data.rgba.cache);
221                 a->texture[i].data.rgba.cache = NULL;
222             }
223         if (a->textures)
224             g_free(a->texture);
225         p = &a->surface;
226         RrColorFree(p->primary);
227         RrColorFree(p->secondary);
228         RrColorFree(p->border_color);
229         RrColorFree(p->bevel_dark);
230         RrColorFree(p->bevel_light);
231         g_free(p->pixel_data);
232
233         g_free(a);
234     }
235 }
236
237
238 static void pixel_data_to_pixmap(RrAppearance *l,
239                                  gint x, gint y, gint w, gint h)
240 {
241     RrPixel32 *in, *scratch;
242     Pixmap out;
243     XImage *im = NULL;
244     im = XCreateImage(RrDisplay(l->inst), RrVisual(l->inst), RrDepth(l->inst),
245                       ZPixmap, 0, NULL, w, h, 32, 0);
246     g_assert(im != NULL);
247
248     in = l->surface.pixel_data;
249     out = l->pixmap;
250
251     im->byte_order = LSBFirst;
252 /* this malloc is a complete waste of time on normal 32bpp
253    as reduce_depth just sets im->data = data and returns
254 */
255     scratch = g_new(RrPixel32, im->width * im->height);
256     im->data = (char*) scratch;
257     RrReduceDepth(l->inst, in, im);
258     XPutImage(RrDisplay(l->inst), out,
259               DefaultGC(RrDisplay(l->inst), RrScreen(l->inst)),
260               im, 0, 0, x, y, w, h);
261     im->data = NULL;
262     XDestroyImage(im);
263     g_free(scratch);
264 }
265
266 void RrMinsize(RrAppearance *l, gint *w, gint *h)
267 {
268     gint i;
269     gint m;
270     *w = *h = 0;
271
272     for (i = 0; i < l->textures; ++i) {
273         switch (l->texture[i].type) {
274         case RR_TEXTURE_NONE:
275             break;
276         case RR_TEXTURE_MASK:
277             *w = MAX(*w, l->texture[i].data.mask.mask->width);
278             *h = MAX(*h, l->texture[i].data.mask.mask->height);
279             break;
280         case RR_TEXTURE_TEXT:
281             m = RrFontMeasureString(l->texture[i].data.text.font,
282                                     l->texture[i].data.text.string);
283             *w = MAX(*w, m);
284             m = RrFontHeight(l->texture[i].data.text.font);
285             *h += MAX(*h, m);
286             break;
287         case RR_TEXTURE_RGBA:
288             *w += MAX(*w, l->texture[i].data.rgba.width);
289             *h += MAX(*h, l->texture[i].data.rgba.height);
290             break;
291         }
292     }
293
294     if (l->surface.relief != RR_RELIEF_FLAT) {
295         switch (l->surface.bevel) {
296         case RR_BEVEL_1:
297             *w += 2;
298             *h += 2;
299             break;
300         case RR_BEVEL_2:
301             *w += 4;
302             *h += 4;
303             break;
304         }
305     } else if (l->surface.border) {
306         *w += 2;
307         *h += 2;
308     }
309
310     if (*w < 1) *w = 1;
311     if (*h < 1) *h = 1;
312 }
313
314 gboolean RrPixmapToRGBA(const RrInstance *inst,
315                         Pixmap pmap, Pixmap mask,
316                         gint *w, gint *h, RrPixel32 **data)
317 {
318     Window xr;
319     gint xx, xy;
320     guint pw, ph, mw, mh, xb, xd, i, x, y, di;
321     XImage *xi, *xm = NULL;
322
323     if (!XGetGeometry(RrDisplay(inst),
324                       pmap, &xr, &xx, &xy, &pw, &ph, &xb, &xd))
325         return FALSE;
326     if (mask) {
327         if (!XGetGeometry(RrDisplay(inst), mask,
328                           &xr, &xx, &xy, &mw, &mh, &xb, &xd))
329             return FALSE;
330         if (pw != mw || ph != mh || xd != 1)
331             return FALSE;
332     }
333
334     xi = XGetImage(RrDisplay(inst), pmap,
335                    0, 0, pw, ph, 0xffffffff, ZPixmap);
336     if (!xi)
337         return FALSE;
338
339     if (mask) {
340         xm = XGetImage(RrDisplay(inst), mask,
341                        0, 0, mw, mh, 0xffffffff, ZPixmap);
342         if (!xm)
343             return FALSE;
344     }
345
346     *data = g_new(RrPixel32, pw * ph);
347     RrIncreaseDepth(inst, *data, xi);
348
349     if (mask) {
350         /* apply transparency from the mask */
351         di = 0;
352         for (i = 0, y = 0; y < ph; ++y) {
353             for (x = 0; x < pw; ++x, ++i) {
354                 if (!((((unsigned)xm->data[di + x / 8]) >> (x % 8)) & 0x1))
355                     (*data)[i] &= ~(0xff << RrDefaultAlphaOffset);
356             }
357             di += xm->bytes_per_line;
358         }
359     }
360
361     *w = pw;
362     *h = ph;
363
364     return TRUE;
365 }