15 //#include <GL/glext.h>
16 //#include <GL/glxext.h>
17 #include <GL/glxtokens.h>
25 void (*screen_paint)(d_screen_t *sc);
26 void (*screen_root_pixmap_change)(d_screen_t *sc);
27 void (*window_show)(d_window_t *w);
28 void (*window_zombie_dead)(d_window_t *w);
29 void (*window_resize)(d_window_t *w);
30 void (*window_reshape)(d_window_t *w);
36 uint32_t fbconfig[MAX_DEPTH + 1];
38 xcb_glx_context_t context;
39 xcb_glx_context_tag_t context_tag;
44 xcb_glx_pixmap_t glpixmap;
47 static uint32_t glxrender_visual_info(uint32_t *props, int vis, int numprops,
49 static gboolean glxrender_check_visual(d_screen_t *sc);
50 static gboolean glxrender_find_fb_config(d_screen_t *sc, data_t *d);
52 static void glxrender_paint(d_screen_t *sc);
53 static void glxrender_root_pixmap_change(d_screen_t *sc);
54 static void paint_root(d_screen_t *sc, data_t *d);
55 static void paint_window(d_window_t *window, data_t *d, window_data_t *wd,
56 gboolean opaque, int x, int y, int width,
57 int height, int bwidth);
58 static void paint_shadow(d_window_t *w, data_t *d, window_data_t *wd,
59 int x, int y, int width, int height, int bwidth);
60 static void glxrender_update_window_pixmap(d_window_t *w, data_t *d,
62 static void glxrender_free_window_pixmap(d_window_t *w, data_t *d,
64 static void glxrender_update_root_pixmap(d_screen_t *sc, data_t *d);
66 static void glxrender_window_show(d_window_t *window);
67 static void glxrender_window_zombie_dead(d_window_t *window);
68 static void glxrender_window_resize(d_window_t *window);
69 static void glxrender_window_reshape(d_window_t *window);
72 glxrender_init(d_screen_t *sc, int id)
75 xcb_generic_error_t *err;
76 xcb_glx_make_current_cookie_t curck;
77 xcb_glx_make_current_reply_t *currep;
82 data_t *d = malloc(sizeof(data_t));
83 d->screen_paint = sc->screen_paint;
84 d->screen_root_pixmap_change = sc->screen_root_pixmap_change;
85 d->window_show = sc->window_show;
86 d->window_zombie_dead = sc->window_zombie_dead;
87 d->window_resize = sc->window_resize;
88 d->window_reshape = sc->window_reshape;
89 screen_add_plugin_data(sc, plugin_id, d);
91 sc->screen_paint = glxrender_paint;
92 sc->screen_root_pixmap_change = glxrender_root_pixmap_change;
93 sc->window_show = glxrender_window_show;
94 sc->window_zombie_dead = glxrender_window_zombie_dead;
95 sc->window_resize = glxrender_window_resize;
96 sc->window_reshape = glxrender_window_reshape;
98 d->shadowalpha = 0x3333; /* 20% */
102 if (!(glxrender_find_fb_config(sc, d))) {
103 printf("unable to find FB configs\n");
107 if (!glxrender_check_visual(sc)) {
108 printf("unable to use the overlay window for GLX\n");
112 d->context = xcb_generate_id(sc->dpy->conn);
113 ck = xcb_glx_create_context_checked(sc->dpy->conn, d->context,
115 sc->num, XCB_NONE, GL_TRUE);
116 if ((err = xcb_request_check(sc->dpy->conn, ck))) {
117 printf("context creation failed\n");
118 display_error(sc->dpy, err);
123 curck = xcb_glx_make_current(sc->dpy->conn,
124 sc->overlay, d->context, XCB_NONE);
125 currep = xcb_glx_make_current_reply(sc->dpy->conn, curck, &err);
128 display_error(sc->dpy, err);
131 printf("make current failed\n");
134 d->context_tag = currep->context_tag;
137 glViewport(0, 0, sc->super->width_in_pixels, sc->super->height_in_pixels);
138 glMatrixMode(GL_PROJECTION);
140 glOrtho(0, sc->super->width_in_pixels, sc->super->height_in_pixels,
142 glMatrixMode(GL_MODELVIEW);
144 glClear(GL_COLOR_BUFFER_BIT);
145 glEnable(GL_TEXTURE_2D);
146 xcb_glx_swap_buffers(sc->dpy->conn, d->context_tag, sc->overlay);
147 glClearColor(0.4, 0.4, 0.4, 1.0);
149 //glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
152 glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
158 glxrender_visual_info(uint32_t *props, int vis, int numprops, uint32_t name)
162 assert(numprops > 15);
165 for (s = vis * numprops; s < vis*numprops + numprops; ++s)
166 printf("%x ", props[s]);
172 case GLX_VISUAL_ID: return props[s+0]; /* id number */
173 case GLX_X_VISUAL_TYPE: return props[s+1]; /* XCB_CLASS_TRUE_COLOR etc */
174 case GLX_USE_GL: return props[s+2]; /* boolean */
175 case GLX_RED_SIZE: return props[s+3]; /* number */
176 case GLX_GREEN_SIZE: return props[s+4]; /* number */
177 case GLX_BLUE_SIZE: return props[s+5]; /* number */
178 case GLX_ALPHA_SIZE: return props[s+6]; /* number */
179 case GLX_DOUBLEBUFFER: return props[s+11]; /* boolean */
180 case GLX_DEPTH_SIZE: return props[s+14]; /* number */
181 case GLX_STENCIL_SIZE: return props[s+15]; /* number */
187 glxrender_check_visual(d_screen_t *sc)
189 xcb_glx_get_visual_configs_cookie_t ck;
190 xcb_glx_get_visual_configs_reply_t *rep;
193 ck = xcb_glx_get_visual_configs_unchecked(sc->dpy->conn, sc->num);
194 rep = xcb_glx_get_visual_configs_reply(sc->dpy->conn, ck, NULL);
197 unsigned int i, nprops;
199 props = xcb_glx_get_visual_configs_property_list(rep);
200 nprops = rep->num_properties;
202 for (i = 0; i < rep->num_visuals; ++i) {
203 /* look for the overlay's visual */
204 if (glxrender_visual_info(props, i, nprops, GLX_VISUAL_ID) !=
210 if (!glxrender_visual_info(props, i, nprops, GLX_USE_GL)) {
211 printf("overlay visual does not support GL\n");
215 if (!glxrender_visual_info(props, i, nprops, GLX_DOUBLEBUFFER)) {
216 printf("overlay visual is not double buffered\n");
220 ok = TRUE; /* yippa ! */
229 glxrender_find_fb_config(d_screen_t *sc, data_t *d)
231 static const uint32_t drawable_tfp_attrs[] = {
232 GLX_CONFIG_CAVEAT, GLX_NONE,
233 GLX_DOUBLEBUFFER, FALSE,
239 GLX_RENDER_TYPE, GLX_RGBA_BIT,
240 GLX_BIND_TO_TEXTURE_RGBA_EXT, TRUE, /* For TextureFromPixmap */
243 xcb_depth_iterator_t depth_it;
244 d_glx_fb_config_t *fbcons;
247 fbcons = glxcompat_choose_fb_config(sc->dpy->conn, sc->num,
248 drawable_tfp_attrs, &numfb);
249 if (!fbcons) return FALSE;
251 memset(d->fbconfig, 0, (MAX_DEPTH + 1) * sizeof(d->fbconfig[0]));
253 depth_it = xcb_screen_allowed_depths_iterator(sc->super);
254 for (; depth_it.rem; xcb_depth_next(&depth_it)) {
256 xcb_visualtype_t *visuals;
259 if (depth_it.data->depth > MAX_DEPTH) continue;
261 printf("looking for depth %d\n", depth_it.data->depth);
263 visuals = xcb_depth_visuals(depth_it.data);
264 nvisuals = xcb_depth_visuals_length(depth_it.data);
266 /* look for an fbconfig for this depth */
267 for (j = 0; j < numfb; ++j) {
271 //glxcompat_get_fb_config_attrib(fbcons[j], GLX_FBCONFIG_ID, &v);
272 //printf("trying fbconfig 0x%x\n", v);
274 glxcompat_get_fb_config_attrib(fbcons[j], GLX_VISUAL_ID, &vid);
275 glxcompat_get_fb_config_attrib(fbcons[j], GLX_FBCONFIG_ID, &fbid);
278 for (k = 0; k < nvisuals; ++k)
279 //if (vid == visuals[k].visual_id) {
280 if ((vid == 0x80 && depth_it.data->depth == 32) ||
287 d->fbconfig[depth_it.data->depth] = fbid;
289 printf("found visual 0x%x fbconfig 0x%x for depth %d\n",
290 vid, fbid, depth_it.data->depth);
301 glxrender_free(d_screen_t *sc)
303 data_t *d = screen_find_plugin_data(sc, plugin_id);
305 screen_remove_plugin_data(sc, plugin_id);
309 glxrender_next_timeout(struct d_screen *sc, struct timeval *tv)
317 glxrender_timeout(struct d_screen *sc, const struct timeval *now)
323 glxrender_window_free_data(d_window_t *w, data_t *d, window_data_t *wd)
325 glxrender_free_window_pixmap(w, d, wd);
326 glDeleteTextures(1, &wd->texname);
331 glxrender_window_show(d_window_t *w)
336 d = screen_find_plugin_data(w->sc, plugin_id);
341 wd = window_find_plugin_data(w, plugin_id);
343 glxrender_window_free_data(w, d, wd);
345 wd = malloc(sizeof(window_data_t));
346 glGenTextures(1, &wd->texname);
347 wd->glpixmap = XCB_NONE;
349 window_add_plugin_data(w, plugin_id, wd);
353 glxrender_window_zombie_dead(d_window_t *w)
358 d = screen_find_plugin_data(w->sc, plugin_id);
359 wd = window_find_plugin_data(w, plugin_id);
361 glxrender_window_free_data(w, d, wd);
362 window_remove_plugin_data(w, plugin_id);
366 d->window_zombie_dead(w);
370 glxrender_free_window_pixmap(d_window_t *w, data_t *d, window_data_t *wd)
372 /* this might cause an error, oh well */
374 glBindTexture(GL_TEXTURE_2D, wd->texname);
379 1 CARD8 opcode (X assigned)
380 1 16 GLX opcode (glXVendorPrivate)
382 4 1331 vendor specific opcode
384 4 GLX_DRAWABLE drawable
387 unsigned int len = (2 + 0) * sizeof(uint32_t);
392 xcb_glx_vendor_private(w->sc->dpy->conn,
395 len, (uint8_t*)data);
398 glBindTexture(GL_TEXTURE_2D, 0);
400 xcb_glx_destroy_glx_pixmap(w->sc->dpy->conn, wd->glpixmap);
401 wd->glpixmap = XCB_NONE;
406 glxrender_update_window_pixmap(d_window_t *w, data_t *d, window_data_t *wd)
411 static const uint32_t attrs[] = {
412 GLX_TEXTURE_FORMAT_EXT,
413 GLX_TEXTURE_FORMAT_RGBA_EXT
416 px = window_get_pixmap(w);
417 depth = window_get_depth(w);
418 //printf("%x %d %x\n", px, depth, d->fbconfig[depth]);
419 if (px && !d->fbconfig[depth])
420 printf("no GL visual for depth %d\n", depth);
422 wd->glpixmap = xcb_generate_id(w->sc->dpy->conn);
423 printf("bind config 0x%x screen %d pixmap 0x%x glpixmap 0x%x "
425 d->fbconfig[depth], w->sc->num, px, wd->glpixmap,
426 sizeof(attrs)/sizeof(attrs[0]));
427 xcb_glx_create_pixmap(w->sc->dpy->conn, w->sc->num,
430 sizeof(attrs)/sizeof(attrs[0]),
433 glBindTexture(GL_TEXTURE_2D, wd->texname);
438 1 CARD8 opcode (X assigned)
439 1 16 GLX opcode (glXVendorPrivate)
441 4 1330 vendor specific opcode
443 4 GLX_DRAWABLE drawable
445 4 CARD32 num_attributes
446 4*n LISTofATTRIBUTE_PAIR attribute, value pairs.
448 unsigned int len = (3 + 0) * sizeof(uint32_t);
454 xcb_glx_vendor_private(w->sc->dpy->conn,
457 len, (uint8_t*)data);
460 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
461 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
463 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
464 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
465 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
467 glBindTexture(GL_TEXTURE_2D, 0);
472 glxrender_update_root_pixmap(d_screen_t *sc, data_t *d)
476 px = screen_get_root_pixmap(sc);
482 glxrender_window_resize(d_window_t *w)
487 d = screen_find_plugin_data(w->sc, plugin_id);
488 wd = window_find_plugin_data(w, plugin_id);
494 glxrender_free_window_pixmap(w, d, wd);
498 glxrender_window_reshape(d_window_t *w)
503 d = screen_find_plugin_data(w->sc, plugin_id);
504 wd = window_find_plugin_data(w, plugin_id);
507 d->window_reshape(w);
510 glxrender_free_window_pixmap(w, d, wd);
514 glxrender_root_pixmap_change(d_screen_t *sc)
518 d = screen_find_plugin_data(sc, plugin_id);
519 int a; /* XXX free things here */
520 //if (d->root_picture) {
521 // xcb_render_free_picture(sc->dpy->conn, d->root_picture);
522 // d->root_picture = XCB_NONE;
526 d->screen_root_pixmap_change(sc);
530 glxrender_paint(d_screen_t *sc)
532 data_t *d = screen_find_plugin_data(sc, plugin_id);
535 //printf("painting\n");
539 for (it = list_bottom(sc->stacking); it; it = it->prev) {
540 d_window_t *w = it->data;
542 if (!window_is_input_only(w) &&
543 (window_is_mapped(w) || window_is_zombie(w)))
545 int x, y, width, height, bwidth;
549 window_get_area(w, &x, &y, &width, &height, &bwidth);
551 if (!(x < sc->super->width_in_pixels &&
552 y < sc->super->height_in_pixels &&
553 (x + width > 0 || x + width + d->xshadowoff > 0) &&
554 (y + height > 0 || y + height + d->yshadowoff > 0)))
559 opaque = !window_is_argb(w) && window_get_opacity(w) == 0xffff;
561 wd = window_find_plugin_data(w, plugin_id);
563 //paint_shadow(w, d, wd, x, y, width, height, bwidth);
564 //paint_window(w, d, wd, opaque, x, y, width, height, bwidth);
568 xcb_glx_swap_buffers(sc->dpy->conn, d->context_tag, sc->overlay);
570 /* call the function we replaced in the chain */
575 paint_root(d_screen_t *sc, data_t *d)
577 //if (!d->root_picture)
578 glxrender_update_root_pixmap(sc, d);
580 glClear(GL_COLOR_BUFFER_BIT);
584 paint_window(d_window_t *w, data_t *d, window_data_t *wd, gboolean opaque,
585 int x, int y, int width, int height, int bwidth)
588 glxrender_update_window_pixmap(w, d, wd);
590 glBindTexture(GL_TEXTURE_2D, wd->texname);
592 glColor3f(1.0, 1.0, 1.0);
595 glVertex2i(x + width + bwidth, y);
597 glVertex2i(x + width + bwidth,
598 y + height + bwidth);
600 glVertex2i(x, y + height + bwidth);
606 paint_shadow(d_window_t *w, data_t *d, window_data_t *wd,
607 int x, int y, int width, int height, int bwidth)
610 xcb_render_composite(w->sc->dpy->conn,
611 XCB_RENDER_PICT_OP_OVER,
616 x+d->xshadowoff, y+d->yshadowoff,
617 width + bwidth*2, height + bwidth *2);