find the visuals to use, and the fbconfig we want to use for all available depths
[dana/dcompmgr.git] / glxrender.c
1 #include "efence.h"
2
3 #include "render.h"
4 #include "screen.h"
5 #include "window.h"
6 #include "display.h"
7 #include "list.h"
8 #include <stdio.h>
9 #include <assert.h>
10 #include <stdlib.h>
11
12 #include <xcb/glx.h>
13 //#include <GL/glext.h>
14 //#include <GL/glxext.h>
15 #include <GL/glxtokens.h>
16 #include <GL/gl.h>
17
18 #define MAX_DEPTH 32
19
20 static int plugin_id;
21
22 typedef struct {
23     void (*screen_paint)(d_screen_t *sc);
24     void (*screen_root_pixmap_change)(d_screen_t *sc);
25     void (*window_show)(d_window_t *w);
26     void (*window_zombie_dead)(d_window_t *w);
27     void (*window_resize)(d_window_t *w);
28     void (*window_reshape)(d_window_t *w);
29
30     uint16_t shadowalpha;
31     int xshadowoff;
32     int yshadowoff;
33
34     uint32_t fbconfig[MAX_DEPTH + 1];
35
36     xcb_glx_context_t context;
37     xcb_glx_context_tag_t context_tag;
38 } data_t;
39
40 typedef struct {
41     GLuint texname;
42     xcb_glx_pixmap_t glpixmap;
43 } window_data_t;
44
45 static uint32_t glxrender_visual_info(uint32_t *props, int vis, int numprops,
46                                       uint32_t name);
47 static gboolean glxrender_check_visual(d_screen_t *sc);
48 static gboolean glxrender_find_fb_config(d_screen_t *sc, data_t *d);
49
50 static void glxrender_paint(d_screen_t *sc);
51 static void glxrender_root_pixmap_change(d_screen_t *sc);
52 static void paint_root(d_screen_t *sc, data_t *d);
53 static void paint_window(d_window_t *window, data_t *d, window_data_t *wd,
54                          gboolean opaque, int x, int y, int width,
55                          int height, int bwidth);
56 static void paint_shadow(d_window_t *w, data_t *d, window_data_t *wd,
57                          int x, int y, int width, int height, int bwidth);
58 static void glxrender_update_window_pixmap(d_window_t *w, data_t *d,
59                                            window_data_t *wd);
60 static void glxrender_free_window_pixmap(d_window_t *w, window_data_t *wd);
61 static void glxrender_update_root_pixmap(d_screen_t *sc, data_t *d);
62
63 static void glxrender_window_show(d_window_t *window);
64 static void glxrender_window_zombie_dead(d_window_t *window);
65 static void glxrender_window_resize(d_window_t *window);
66 static void glxrender_window_reshape(d_window_t *window);
67
68 void
69 glxrender_init(d_screen_t *sc, int id)
70 {
71     xcb_void_cookie_t ck;
72     xcb_generic_error_t *err;
73     xcb_glx_make_current_cookie_t curck;
74     xcb_glx_make_current_reply_t *currep;
75
76     plugin_id = id;
77
78     data_t *d = malloc(sizeof(data_t));
79     d->screen_paint = sc->screen_paint;
80     d->screen_root_pixmap_change = sc->screen_root_pixmap_change;
81     d->window_show = sc->window_show;
82     d->window_zombie_dead = sc->window_zombie_dead;
83     d->window_resize = sc->window_resize;
84     d->window_reshape = sc->window_reshape;
85     screen_add_plugin_data(sc, plugin_id, d);
86
87     sc->screen_paint = glxrender_paint;
88     sc->screen_root_pixmap_change = glxrender_root_pixmap_change;
89     sc->window_show = glxrender_window_show;
90     sc->window_zombie_dead = glxrender_window_zombie_dead;
91     sc->window_resize = glxrender_window_resize;
92     sc->window_reshape = glxrender_window_reshape;
93
94     d->shadowalpha = 0x3333; /* 20% */
95     d->xshadowoff = 2;
96     d->yshadowoff = 2;
97
98     if (!glxrender_check_visual(sc)) {
99         printf("unable to use the overlay window for GLX\n");
100         exit(1);
101     }
102
103     if (!(glxrender_find_fb_config(sc, d))) {
104         printf("unable to find FB configs\n");
105         exit(1);
106     }
107
108     d->context = xcb_generate_id(sc->dpy->conn);
109     ck = xcb_glx_create_context_checked(sc->dpy->conn, d->context,
110                                         sc->overlay_visual, sc->num,
111                                         XCB_NONE, TRUE);
112     if ((err = xcb_request_check(sc->dpy->conn, ck))) {
113         printf("context creation failed\n");
114         display_error(sc->dpy, err);
115         free(err);
116         exit(1);
117     }
118
119     curck = xcb_glx_make_current(sc->dpy->conn,
120                                  sc->overlay, d->context, XCB_NONE);
121     currep = xcb_glx_make_current_reply(sc->dpy->conn, curck, &err);
122     if (!currep) {
123         if (err) {
124             display_error(sc->dpy, err);
125             free(err);
126         }
127         printf("make current failed\n");
128         exit(1);
129     }
130     d->context_tag = currep->context_tag;
131     free(currep);
132
133     glViewport(0, 0, sc->super.width_in_pixels, sc->super.height_in_pixels);
134     glMatrixMode(GL_PROJECTION);
135     glLoadIdentity();
136     glOrtho(0, sc->super.width_in_pixels, sc->super.height_in_pixels,
137             0.0, -1.0, 100.0);
138     glMatrixMode(GL_MODELVIEW);
139     glLoadIdentity();
140     glClear(GL_COLOR_BUFFER_BIT);
141     glEnable(GL_TEXTURE_2D);
142     xcb_glx_swap_buffers(sc->dpy->conn, d->context_tag, sc->overlay);
143     glClearColor(0.4, 0.4, 0.4, 1.0);
144
145 /*
146   glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);                          
147   glEnable(GL_BLEND);
148 */
149 }
150
151 static uint32_t
152 glxrender_visual_info(uint32_t *props, int vis, int numprops, uint32_t name)
153 {
154     int s;
155
156     assert(numprops > 14);
157
158     /*
159     for (s = vis * numprops; s < vis*numprops + numprops; ++s)
160         printf("%x ", props[s]);
161     printf("\n");
162     */
163
164     s = vis * numprops;
165     switch (name) {
166     case GLX_VISUAL_ID:     return props[s+0];  /* id number */
167     case GLX_X_VISUAL_TYPE: return props[s+1];  /* XCB_CLASS_TRUE_COLOR etc */
168     case GLX_USE_GL:        return props[s+2];  /* boolean */
169     case GLX_RED_SIZE:      return props[s+3];  /* number */
170     case GLX_GREEN_SIZE:    return props[s+4];  /* number */
171     case GLX_BLUE_SIZE:     return props[s+5];  /* number */
172     case GLX_ALPHA_SIZE:    return props[s+6];  /* number */
173     case GLX_DOUBLEBUFFER:  return props[s+11]; /* boolean */
174     case GLX_DEPTH_SIZE:    return props[s+14]; /* number */
175     case GLX_STENCIL_SIZE:  return props[s+15]; /* number */
176     default: assert(0);
177     }
178 }
179
180 static gboolean
181 glxrender_check_visual(d_screen_t *sc)
182 {
183     xcb_glx_get_visual_configs_cookie_t ck;
184     xcb_glx_get_visual_configs_reply_t *rep;
185     gboolean ok = FALSE;
186
187     ck = xcb_glx_get_visual_configs_unchecked(sc->dpy->conn, sc->num);
188     rep = xcb_glx_get_visual_configs_reply(sc->dpy->conn, ck, NULL);
189     if (rep) {
190         uint32_t *props;
191         unsigned int i, nprops;
192
193 /*
194         static int config[] = {
195             GLX_DEPTH_SIZE, 1,
196             GLX_DOUBLEBUFFER,
197             GLX_RGBA,
198             XCB_NONE
199         };
200 */
201
202         props = xcb_glx_get_visual_configs_property_list(rep);
203         nprops = rep->num_properties;
204
205         for (i = 0; i < rep->num_visuals; ++i) {
206             /* look for the overlay's visual */
207             if (glxrender_visual_info(props, i, nprops, GLX_VISUAL_ID) !=
208                 sc->overlay_visual)
209             {
210                 continue;
211             }
212
213             if (!glxrender_visual_info(props, i, nprops, GLX_USE_GL)) {
214                 printf("overlay visual does not support GL\n");
215                 break;
216             }
217
218             if (!glxrender_visual_info(props, i, nprops, GLX_DOUBLEBUFFER)) {
219                 printf("overlay visual is not double buffered\n");
220                 break;
221             }
222
223             ok = TRUE; /* yippa ! */
224         }
225
226         free(rep);
227     }
228     return ok;
229 }
230
231 static uint32_t
232 glxrender_fbconfig_info(uint32_t *props, int con, int numprops, uint32_t name)
233 {
234     int i;
235
236     for (i = 0; i < numprops; ++i) {
237         if (props[i*2 + con*numprops*2] == name)
238             return props[i*2 + con*numprops*2 + 1];
239     }
240     return 0;
241 }
242
243 static gboolean
244 glxrender_find_fb_config(d_screen_t *sc, data_t *d)
245 {
246     xcb_glx_get_visual_configs_cookie_t vck;
247     xcb_glx_get_visual_configs_reply_t *vrep;
248     xcb_glx_get_fb_configs_cookie_t fbck;
249     xcb_glx_get_fb_configs_reply_t *fbrep;
250     xcb_depth_iterator_t depth_it;
251     uint32_t fbcon = FALSE;
252     unsigned int i, nvprops, nfbprops;
253     uint32_t *vprops, *fbprops;
254     uint32_t db, stencil, depthsize;
255
256     vck = xcb_glx_get_visual_configs_unchecked(sc->dpy->conn, sc->num);
257     vrep = xcb_glx_get_visual_configs_reply(sc->dpy->conn, vck, NULL);
258     if (!vrep) return FALSE;
259
260     fbck = xcb_glx_get_fb_configs(sc->dpy->conn, sc->num);
261     fbrep = xcb_glx_get_fb_configs_reply(sc->dpy->conn, fbck, NULL);
262     if (!fbrep) return FALSE;
263
264     vprops = xcb_glx_get_visual_configs_property_list(vrep);
265     nvprops = vrep->num_properties;
266     fbprops = xcb_glx_get_fb_configs_property_list(fbrep);
267     nfbprops = fbrep->num_properties;
268
269     for (i = 0; i <= MAX_DEPTH; ++i)
270         d->fbconfig[i] = 0;
271
272     db = 32767;
273     stencil = 32767;
274     depthsize = 32767;
275     depth_it = xcb_screen_allowed_depths_iterator(&sc->super);
276     for (; depth_it.rem; xcb_depth_next(&depth_it)) {
277         uint32_t vid;
278         int j;
279         unsigned int k;
280         xcb_visualtype_t *visuals;
281         int nvisuals;
282
283         vid = 0;
284         if (depth_it.data->depth > MAX_DEPTH) continue;
285
286         visuals = xcb_depth_visuals(depth_it.data);
287         nvisuals = xcb_depth_visuals_length(depth_it.data);
288
289         /* pick the nicest visual for the depth */
290         for (j = 0; j < nvisuals; ++j) {
291             uint32_t val;
292
293             /* find the visual's properties */
294             for (k = 0; k < vrep->num_visuals; ++k)
295                 if (glxrender_visual_info(vprops, k, nvprops, GLX_VISUAL_ID)
296                     == visuals[j].visual_id)
297                 {
298                     break;
299                 }
300             if (k == vrep->num_visuals) continue;
301
302             val = glxrender_visual_info(vprops, k, nvprops, GLX_USE_GL);
303             if (!val) continue;
304
305             val = glxrender_visual_info(vprops, k, nvprops, GLX_DOUBLEBUFFER);
306             if (!val > db) continue;
307             db = val;
308
309             val = glxrender_visual_info(vprops, k, nvprops, GLX_STENCIL_SIZE);
310             if (!val > stencil) continue;
311             stencil = val;
312
313             val = glxrender_visual_info(vprops, k, nvprops, GLX_DEPTH_SIZE);
314             if (!val > depthsize) continue;
315             depthsize = val;
316
317             /* use this visual */
318             vid = visuals[j].visual_id;
319         }
320
321         if (!vid) continue;
322
323         printf("found visual for depth %d\n", i);
324
325         for (k = 0; k < fbrep->num_FB_configs; ++k) {
326             uint32_t val;
327
328             printf("root visual 0x%x\n", sc->super.root_visual);
329             printf("overlay visual 0x%x\n", sc->overlay_visual);
330
331             val = glxrender_fbconfig_info(fbprops, k, nfbprops,
332                                           GLX_VISUAL_ID);
333             printf("x visual 0x%x\n", val);
334             if (val != vid) continue;
335
336             val = glxrender_fbconfig_info(fbprops, k, nfbprops,
337                                           GLX_DOUBLEBUFFER);
338             printf("dbl buffer %s\n", val ? "yes" : "no");
339             if (db) continue;
340
341             val = glxrender_fbconfig_info(fbprops, k, nfbprops,
342                                           GLX_DEPTH_SIZE);
343             printf("depth size %d\n", val);
344
345             val = glxrender_fbconfig_info(fbprops, k, nfbprops,
346                                           GLX_RED_SIZE);
347             printf("red size %d\n", val);
348             if (!val) continue;
349
350             val = glxrender_fbconfig_info(fbprops, k, nfbprops,
351                                           GLX_GREEN_SIZE);
352             printf("green size %d\n", val);
353             if (!val) continue;
354
355             val = glxrender_fbconfig_info(fbprops, k, nfbprops,
356                                           GLX_BLUE_SIZE);
357             printf("blue size %d\n", val);
358             if (!val) continue;
359
360             val = glxrender_fbconfig_info(fbprops, k, nfbprops,
361                                           GLX_ALPHA_SIZE);
362             printf("alpha size %d\n", val);
363             if (!val) continue;
364
365             val = glxrender_fbconfig_info(fbprops, j, nfbprops,
366                                           GLX_RENDER_TYPE);
367             printf("rgba bit %s\n", val & GLX_RGBA_BIT ? "yes" : "no");
368             if (!(val & GLX_RGBA_BIT)) continue;
369
370             val = glxrender_fbconfig_info(fbprops, j, nfbprops,
371                                           GLX_CONFIG_CAVEAT);
372             printf("caveat 0x%x\n", val);
373
374             val = glxrender_fbconfig_info(fbprops, j, nfbprops,
375                                           GLX_BIND_TO_TEXTURE_RGBA_EXT);
376             printf("bind ext %s\n", val ? "yes" : "no");
377             if (!val) continue;
378
379             d->fbconfig[depth_it.data->depth] =
380                 glxrender_fbconfig_info(fbprops, i, nfbprops, GLX_FBCONFIG_ID);
381             break;
382         }
383     }
384
385     free(vrep);
386     free(fbrep);
387     return TRUE;
388 }
389
390 void
391 glxrender_free(d_screen_t *sc)
392 {
393     data_t *d = screen_find_plugin_data(sc, plugin_id);
394     free(d);
395     screen_remove_plugin_data(sc, plugin_id);
396 }
397
398 int
399 glxrender_next_timeout(struct d_screen *sc, struct timeval *tv)
400 {
401     (void)sc;
402     (void)tv;
403     return FALSE;
404 }
405
406 void
407 glxrender_timeout(struct d_screen *sc, const struct timeval *now)
408 {
409     (void)sc; (void)now;
410 }
411
412 void
413 glxrender_window_free_data(d_window_t *w, window_data_t *wd)
414 {
415     glxrender_free_window_pixmap(w, wd);
416     glDeleteTextures(1, &wd->texname);
417     free(wd);
418 }
419
420 static void
421 glxrender_window_show(d_window_t *w)
422 {
423     data_t *d;
424     window_data_t *wd;
425
426     d = screen_find_plugin_data(w->sc, plugin_id);
427
428     /* pass it on */
429     d->window_show(w);
430
431     wd = window_find_plugin_data(w, plugin_id);
432     if (wd)
433         glxrender_window_free_data(w, wd);
434    
435     wd = malloc(sizeof(window_data_t));
436     glGenTextures(1, &wd->texname);
437
438     window_add_plugin_data(w, plugin_id, wd);
439 }
440
441 static void
442 glxrender_window_zombie_dead(d_window_t *w)
443 {
444     data_t *d;
445     window_data_t *wd;
446
447     d = screen_find_plugin_data(w->sc, plugin_id);
448     wd = window_find_plugin_data(w, plugin_id);
449     if (wd) {
450         glxrender_window_free_data(w, wd);
451         window_remove_plugin_data(w, plugin_id);
452     }
453
454     /* pass it on */
455     d->window_zombie_dead(w);
456 }
457
458 static void
459 glxrender_free_window_pixmap(d_window_t *w, window_data_t *wd)
460 {
461     /* this might cause an error, oh well */
462     //if (wd->picture) {
463     //    xcb_render_free_picture(w->sc->dpy->conn, wd->picture);
464     //    wd->picture = XCB_NONE;
465     //}
466 }
467
468 static void
469 glxrender_update_window_pixmap(d_window_t *w, data_t *d, window_data_t *wd)
470 {
471     xcb_pixmap_t px;
472
473     px = window_get_pixmap(w);
474 }
475
476 static void
477 glxrender_update_root_pixmap(d_screen_t *sc, data_t *d)
478 {
479     xcb_pixmap_t px;
480
481     px = screen_get_root_pixmap(sc);
482     if (px) {
483     }
484 }
485
486 static void
487 glxrender_window_resize(d_window_t *w)
488 {
489     data_t *d;
490     window_data_t *wd;
491
492     d = screen_find_plugin_data(w->sc, plugin_id);
493     wd = window_find_plugin_data(w, plugin_id);
494
495     /* pass it on */
496     d->window_resize(w);
497
498     assert(wd != NULL);
499     glxrender_free_window_pixmap(w, wd);
500 }
501
502 static void
503 glxrender_window_reshape(d_window_t *w)
504 {
505     data_t *d;
506     window_data_t *wd;
507
508     d = screen_find_plugin_data(w->sc, plugin_id);
509     wd = window_find_plugin_data(w, plugin_id);
510
511     /* pass it on */
512     d->window_reshape(w);
513
514     assert(wd != NULL);
515     glxrender_free_window_pixmap(w, wd);
516 }
517
518 static void
519 glxrender_root_pixmap_change(d_screen_t *sc)
520 {
521     data_t *d;
522
523     d = screen_find_plugin_data(sc, plugin_id);
524     int a; /* XXX free things here */
525     //if (d->root_picture) {
526     //    xcb_render_free_picture(sc->dpy->conn, d->root_picture);
527     //    d->root_picture = XCB_NONE;
528     //}
529
530     /* pass it on */
531     d->screen_root_pixmap_change(sc);
532 }
533
534 static void
535 glxrender_paint(d_screen_t *sc)
536 {
537     data_t *d = screen_find_plugin_data(sc, plugin_id);
538     d_list_it_t *it;
539
540     paint_root(sc, d);
541
542     for (it = list_bottom(sc->stacking); it; it = it->prev) {
543         d_window_t *w = it->data;
544
545         if (!window_is_input_only(w) &&
546             (window_is_mapped(w) || window_is_zombie(w)))
547         {
548             int x, y, width, height, bwidth;
549             gboolean opaque;
550             window_data_t *wd;
551
552             window_get_area(w, &x, &y, &width, &height, &bwidth);
553
554             if (!(x < sc->super.width_in_pixels &&
555                   y < sc->super.height_in_pixels &&
556                   (x + width > 0 || x + width + d->xshadowoff > 0) &&
557                   (y + height > 0 || y + height + d->yshadowoff > 0)))
558             {
559                 continue;
560             }
561
562             opaque = !window_is_argb(w) && window_get_opacity(w) == 0xffff;
563
564             wd = window_find_plugin_data(w, plugin_id);
565
566             paint_shadow(w, d, wd, x, y, width, height, bwidth);
567             paint_window(w, d, wd, opaque, x, y, width, height, bwidth);
568         }
569     }
570
571     xcb_glx_swap_buffers(sc->dpy->conn, d->context_tag, sc->overlay);
572
573     /* call the function we replaced in the chain */
574     d->screen_paint(sc);
575 }
576
577 static void
578 paint_root(d_screen_t *sc, data_t *d)
579 {
580     //if (!d->root_picture)
581     glxrender_update_root_pixmap(sc, d);
582 }
583
584 static void
585 paint_window(d_window_t *w, data_t *d, window_data_t *wd, gboolean opaque,
586              int x, int y, int width, int height, int bwidth)
587 {
588     //if (!wd->picture)
589     glxrender_update_window_pixmap(w, d, wd);
590
591     //printf("-- paint window 0x%x picture 0x%x --\n", w->id, wd->picture);
592 /*
593     if (wd->picture) {
594         xcb_render_picture_t alphamap;
595         int op;
596
597         if (opaque) {
598             op = XCB_RENDER_PICT_OP_SRC;
599             alphamap = XCB_NONE;
600         }
601         else {
602             op = XCB_RENDER_PICT_OP_OVER;
603             alphamap = wd->alpha_picture;
604         }
605
606         xcb_render_composite(w->sc->dpy->conn,
607                              op,
608                              wd->picture,
609                              alphamap,
610                              d->overlay_buffer,
611                              0, 0, 0, 0,
612                              x, y, width + bwidth*2, height + bwidth *2);
613     }
614 */
615 }
616
617 static void
618 paint_shadow(d_window_t *w, data_t *d, window_data_t *wd,
619              int x, int y, int width, int height, int bwidth)
620 {
621 /*
622     xcb_render_composite(w->sc->dpy->conn,
623                          XCB_RENDER_PICT_OP_OVER,
624                          wd->shadow_picture,
625                          wd->picture,
626                          d->overlay_buffer,
627                          0, 0, 0, 0,
628                          x+d->xshadowoff, y+d->yshadowoff,
629                          width + bwidth*2, height + bwidth *2);
630 */
631 }