11 #include <xcb/render.h>
15 /* if defined, it will not use double buffering */
16 #undef MONITOR_REPAINT
19 void (*screen_paint)(d_screen_t *sc);
20 void (*screen_root_pixmap_change)(d_screen_t *sc);
21 void (*window_show)(d_window_t *w);
22 void (*window_zombie_dead)(d_window_t *w);
23 void (*window_move)(d_window_t *w);
24 void (*window_resize)(d_window_t *w);
25 void (*window_reshape)(d_window_t *w);
26 void (*window_restack)(d_window_t *w, d_window_t *above);
27 void (*window_opacity_change)(d_window_t *w);
28 void (*window_damage)(d_window_t *w);
30 xcb_render_pictformat_t root_format;
31 xcb_render_pictformat_t argb32_format;
32 xcb_render_query_pict_formats_reply_t *pict_formats;
33 xcb_render_picture_t overlay_picture;
34 xcb_render_picture_t overlay_buffer;
35 xcb_render_picture_t root_picture;
36 xcb_render_picture_t solid_bg;
38 xcb_xfixes_region_t all_region;
39 xcb_xfixes_region_t damaged_region;
40 xcb_xfixes_region_t paint_region;
41 xcb_xfixes_region_t shadow_paint_region;
49 xcb_render_picture_t picture;
50 xcb_render_picture_t alpha_picture;
51 xcb_render_picture_t shadow_picture;
52 xcb_xfixes_region_t paint_clip;
53 xcb_xfixes_region_t shadow_region;
54 gboolean shadow_valid;
57 static void render_paint(d_screen_t *sc);
58 static void render_root_pixmap_change(d_screen_t *sc);
59 static void paint_root(d_screen_t *sc, data_t *d);
60 static void paint_window(d_window_t *window, data_t *d, window_data_t *wd,
61 gboolean opaque, int x, int y, int width,
62 int height, int bwidth);
63 static void paint_shadow(d_window_t *w, data_t *d, window_data_t *wd,
64 int x, int y, int width, int height, int bwidth);
65 static void render_update_picture(d_window_t *w, data_t *d, window_data_t *wd);
66 static void render_update_root_picture(d_screen_t *sc, data_t *d);
67 static void render_free_picture(d_window_t *w, window_data_t *wd);
68 static xcb_render_pictformat_t find_visual_format(data_t *d,
69 xcb_visualid_t visual);
70 static xcb_render_pictformat_t find_argb32_format(data_t *d);
71 static xcb_render_picture_t solid_picture(data_t *d, d_screen_t *sc,
72 uint16_t a, uint16_t r,
73 uint16_t g, uint16_t b);
74 static void render_update_opacity(d_window_t *w, data_t *d, window_data_t *wd);
75 static void render_update_shadow(d_window_t *w, data_t *d, window_data_t *wd);
77 static void render_window_show(d_window_t *window);
78 static void render_window_zombie_dead(d_window_t *window);
79 static void render_window_move(d_window_t *window);
80 static void render_window_resize(d_window_t *window);
81 static void render_window_reshape(d_window_t *window);
82 static void render_window_restack(d_window_t *window, d_window_t *above);
83 static void render_window_opacity_change(d_window_t *w);
84 static void render_window_damage(d_window_t *w);
87 render_init(d_screen_t *sc, int id)
89 xcb_render_query_pict_formats_cookie_t ck;
95 data_t *d = malloc(sizeof(data_t));
96 d->screen_paint = sc->screen_paint;
97 d->screen_root_pixmap_change = sc->screen_root_pixmap_change;
98 d->window_show = sc->window_show;
99 d->window_zombie_dead = sc->window_zombie_dead;
100 d->window_move = sc->window_move;
101 d->window_resize = sc->window_resize;
102 d->window_reshape = sc->window_reshape;
103 d->window_restack = sc->window_restack;
104 d->window_opacity_change = sc->window_opacity_change;
105 d->window_damage = sc->window_damage;
106 screen_add_plugin_data(sc, plugin_id, d);
108 sc->screen_paint = render_paint;
109 sc->screen_root_pixmap_change = render_root_pixmap_change;
110 sc->window_show = render_window_show;
111 sc->window_zombie_dead = render_window_zombie_dead;
112 sc->window_move = render_window_move;
113 sc->window_resize = render_window_resize;
114 sc->window_reshape = render_window_reshape;
115 sc->window_restack = render_window_restack;
116 sc->window_opacity_change = render_window_opacity_change;
117 sc->window_damage = render_window_damage;
119 d->solid_bg = solid_picture(d, sc, 0xffff, 0x6060, 02020, 0x3030);
121 d->shadowalpha = 0x3333; /* 20% */
125 ck = xcb_render_query_pict_formats_unchecked(sc->dpy->conn);
126 d->pict_formats = xcb_render_query_pict_formats_reply(sc->dpy->conn, ck,
129 d->root_format = find_visual_format(d, sc->super->root_visual);
130 d->argb32_format = find_argb32_format(d);
131 d->root_picture = XCB_NONE;
133 d->overlay_picture = xcb_generate_id(sc->dpy->conn);
134 xcb_render_create_picture(sc->dpy->conn,
135 d->overlay_picture, sc->overlay, d->root_format,
138 /* make the double buffer */
139 px = xcb_generate_id(sc->dpy->conn);
140 xcb_create_pixmap(sc->dpy->conn, sc->super->root_depth, px,
141 sc->super->root, sc->super->width_in_pixels,
142 sc->super->height_in_pixels);
143 d->overlay_buffer = xcb_generate_id(sc->dpy->conn);
144 #ifdef MONITOR_REPAINT
145 xcb_render_create_picture(sc->dpy->conn,
146 d->overlay_buffer, sc->overlay, d->root_format,
149 xcb_render_create_picture(sc->dpy->conn, d->overlay_buffer, px,
150 d->root_format, 0, 0);
152 xcb_free_pixmap(sc->dpy->conn, px);
154 d->all_region = xcb_generate_id(sc->dpy->conn);
155 d->damaged_region = xcb_generate_id(sc->dpy->conn);
156 d->paint_region = xcb_generate_id(sc->dpy->conn);
157 d->shadow_paint_region = xcb_generate_id(sc->dpy->conn);
159 rect.width = sc->super->width_in_pixels;
160 rect.height = sc->super->height_in_pixels;
161 xcb_xfixes_create_region(sc->dpy->conn, d->all_region, 1, &rect);
162 xcb_xfixes_create_region(sc->dpy->conn, d->damaged_region, 1, &rect);
163 xcb_xfixes_create_region(sc->dpy->conn, d->paint_region, 1, &rect);
164 xcb_xfixes_create_region(sc->dpy->conn, d->shadow_paint_region, 1, &rect);
168 render_free(d_screen_t *sc)
170 data_t *d = screen_find_plugin_data(sc, plugin_id);
172 free(d->pict_formats);
173 xcb_render_free_picture(sc->dpy->conn, d->solid_bg);
175 xcb_render_free_picture(sc->dpy->conn, d->root_picture);
176 xcb_render_free_picture(sc->dpy->conn, d->overlay_picture);
177 xcb_render_free_picture(sc->dpy->conn, d->overlay_buffer);
178 xcb_xfixes_destroy_region(sc->dpy->conn, d->all_region);
179 xcb_xfixes_destroy_region(sc->dpy->conn, d->damaged_region);
180 xcb_xfixes_destroy_region(sc->dpy->conn, d->paint_region);
181 xcb_xfixes_destroy_region(sc->dpy->conn, d->shadow_paint_region);
184 screen_remove_plugin_data(sc, plugin_id);
188 render_next_timeout(struct d_screen *sc, struct timeval *tv)
196 render_timeout(struct d_screen *sc, const struct timeval *now)
202 render_window_free(d_window_t *w, window_data_t *wd)
204 render_free_picture(w, wd);
205 xcb_render_free_picture(w->sc->dpy->conn, wd->alpha_picture);
206 xcb_render_free_picture(w->sc->dpy->conn, wd->shadow_picture);
207 xcb_xfixes_destroy_region(w->sc->dpy->conn, wd->paint_clip);
208 xcb_xfixes_destroy_region(w->sc->dpy->conn, wd->shadow_region);
213 render_window_show(d_window_t *w)
217 xcb_rectangle_t rect;
219 d = screen_find_plugin_data(w->sc, plugin_id);
224 wd = window_find_plugin_data(w, plugin_id);
226 render_window_free(w, wd);
228 wd = malloc(sizeof(window_data_t));
229 wd->picture = XCB_NONE;
231 wd->paint_clip = xcb_generate_id(w->sc->dpy->conn);
232 wd->shadow_region = xcb_generate_id(w->sc->dpy->conn);
233 rect.x = rect.y = -1;
234 rect.width = rect.height = 1;
235 xcb_xfixes_create_region(w->sc->dpy->conn, wd->paint_clip, 1, &rect);
236 xcb_xfixes_create_region(w->sc->dpy->conn, wd->shadow_region, 1, &rect);
238 wd->shadow_valid = FALSE;
240 wd->alpha_picture = solid_picture(d, w->sc, 0, 0, 0, 0);
241 wd->shadow_picture = solid_picture(d, w->sc, 0, 0, 0, 0);
243 render_update_opacity(w, d, wd);
245 window_add_plugin_data(w, plugin_id, wd);
249 render_update_opacity(d_window_t *w, data_t *d, window_data_t *wd)
251 static xcb_render_color_t c = { 0, 0, 0, 0 };
252 static xcb_rectangle_t rect = {0, 0, 1, 1};
255 /* make the window's shadow and alpha-map match its current opacity */
256 c.alpha = window_get_opacity(w);
257 xcb_render_fill_rectangles(w->sc->dpy->conn,
258 XCB_RENDER_PICT_OP_SRC,
259 wd->alpha_picture, c, 1, &rect);
261 a = a * window_get_opacity(w) / 0xffff;
263 xcb_render_fill_rectangles(w->sc->dpy->conn,
264 XCB_RENDER_PICT_OP_SRC,
265 wd->shadow_picture, c, 1, &rect);
269 render_window_zombie_dead(d_window_t *w)
274 d = screen_find_plugin_data(w->sc, plugin_id);
275 wd = window_find_plugin_data(w, plugin_id);
277 /* make sure the shadow region is right */
278 render_update_shadow(w, d, wd);
280 /* redraw the window's area, and its shadow's as well */
281 xcb_xfixes_union_region(w->sc->dpy->conn, d->damaged_region,
282 window_get_region(w), d->damaged_region);
283 xcb_xfixes_union_region(w->sc->dpy->conn, d->damaged_region,
284 wd->shadow_region, d->damaged_region);
286 render_window_free(w, wd);
287 window_remove_plugin_data(w, plugin_id);
291 d->window_zombie_dead(w);
294 static xcb_render_picture_t
295 solid_picture(data_t *d, d_screen_t *sc, uint16_t a, uint16_t r,
296 uint16_t g, uint16_t b)
299 xcb_render_picture_t picture;
300 xcb_render_color_t c;
301 const uint32_t vals = XCB_RENDER_REPEAT_NORMAL;
302 const xcb_rectangle_t rect = { 0, 0, 1, 1 };
304 pixmap = xcb_generate_id(sc->dpy->conn);
305 picture = xcb_generate_id(sc->dpy->conn);
307 xcb_create_pixmap(sc->dpy->conn, 32, pixmap, sc->super->root, 1, 1);
308 xcb_render_create_picture(sc->dpy->conn, picture, pixmap, d->argb32_format,
309 XCB_RENDER_CP_REPEAT, &vals);
316 xcb_render_fill_rectangles(sc->dpy->conn, XCB_RENDER_PICT_OP_SRC,
317 picture, c, 1, &rect);
318 xcb_free_pixmap(sc->dpy->conn, pixmap);
323 static xcb_render_pictformat_t
324 find_argb32_format(data_t *d)
326 xcb_render_pictforminfo_iterator_t it;
328 for (it = xcb_render_query_pict_formats_formats_iterator(d->pict_formats);
329 it.rem; xcb_render_pictforminfo_next(&it))
331 xcb_render_pictforminfo_t *format = it.data;
332 if (format->type == XCB_RENDER_PICT_TYPE_DIRECT) {
333 if (format->depth == 32 &&
334 format->direct.alpha_mask == 0xff &&
335 format->direct.red_mask == 0xff &&
336 format->direct.green_mask == 0xff &&
337 format->direct.blue_mask == 0xff &&
338 format->direct.alpha_shift == 24 &&
339 format->direct.red_shift == 16 &&
340 format->direct.green_shift == 8 &&
341 format->direct.blue_shift == 0)
348 static xcb_render_pictformat_t
349 find_visual_format(data_t *d, xcb_visualid_t visual)
351 xcb_render_pictscreen_iterator_t si;
352 xcb_render_pictdepth_iterator_t di;
353 xcb_render_pictvisual_iterator_t vi;
355 if (!visual) return XCB_NONE;
357 /* go through all the screens */
358 si = xcb_render_query_pict_formats_screens_iterator(d->pict_formats);
359 for (; si.rem; xcb_render_pictscreen_next(&si)) {
360 di = xcb_render_pictscreen_depths_iterator(si.data);
361 for (; di.rem; xcb_render_pictdepth_next(&di)) {
362 vi = xcb_render_pictdepth_visuals_iterator(di.data);
363 for (; vi.rem; xcb_render_pictvisual_next(&vi)) {
364 if (vi.data->visual == visual)
365 return vi.data->format;
373 render_free_picture(d_window_t *w, window_data_t *wd)
375 /* this might cause an error, oh well */
377 xcb_render_free_picture(w->sc->dpy->conn, wd->picture);
378 wd->picture = XCB_NONE;
383 render_update_root_picture(d_screen_t *sc, data_t *d)
387 px = screen_get_root_pixmap(sc);
389 d->root_picture = xcb_generate_id(sc->dpy->conn);
390 xcb_render_create_picture(sc->dpy->conn,
392 d->root_format, 0, NULL);
397 render_update_picture(d_window_t *w, data_t *d, window_data_t *wd)
401 px = window_get_pixmap(w);
402 //printf("got pixmap 0x%x\n", px);
404 xcb_render_pictformat_t format;
405 const uint32_t vals = XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS;
407 render_free_picture(w, wd);
409 wd->picture = xcb_generate_id(w->sc->dpy->conn);
410 format = find_visual_format(d, window_get_visual(w));
411 /* we don't need to check this. if it fails, we'll just be drawing
412 an invalid picture and creating some X errors but that's no big
414 xcb_render_create_picture(w->sc->dpy->conn,
415 wd->picture, px, format,
416 XCB_RENDER_CP_SUBWINDOW_MODE,
422 render_window_move(d_window_t *w)
427 d = screen_find_plugin_data(w->sc, plugin_id);
428 wd = window_find_plugin_data(w, plugin_id);
433 /* redraw the screen */
434 xcb_xfixes_copy_region(w->sc->dpy->conn, d->all_region, d->damaged_region);
435 /* reget the shadow's shape */
436 wd->shadow_valid = FALSE;
440 render_window_resize(d_window_t *w)
445 d = screen_find_plugin_data(w->sc, plugin_id);
446 wd = window_find_plugin_data(w, plugin_id);
452 render_free_picture(w, wd);
454 /* redraw the screen */
455 xcb_xfixes_copy_region(w->sc->dpy->conn, d->all_region, d->damaged_region);
456 /* reget the shadow's shape */
457 wd->shadow_valid = FALSE;
461 render_window_reshape(d_window_t *w)
466 d = screen_find_plugin_data(w->sc, plugin_id);
467 wd = window_find_plugin_data(w, plugin_id);
470 d->window_reshape(w);
473 render_free_picture(w, wd);
475 /* redraw the screen */
476 xcb_xfixes_copy_region(w->sc->dpy->conn, d->all_region, d->damaged_region);
477 /* reget the shadow's shape */
478 wd->shadow_valid = FALSE;
482 render_window_restack(d_window_t *w, d_window_t *above)
487 d = screen_find_plugin_data(w->sc, plugin_id);
488 wd = window_find_plugin_data(w, plugin_id);
491 d->window_restack(w, above);
493 /* the window might not be visible */
494 if (wd == NULL) return;
496 /* make sure the shadow region is right */
497 render_update_shadow(w, d, wd);
499 /* redraw the window's contents, and its shadow as well */
500 xcb_xfixes_union_region(w->sc->dpy->conn, d->damaged_region,
501 window_get_region(w), d->damaged_region);
502 xcb_xfixes_union_region(w->sc->dpy->conn, d->damaged_region,
503 wd->shadow_region, d->damaged_region);
507 render_update_shadow(d_window_t *w, data_t *d, window_data_t *wd)
509 if (!wd->shadow_valid) {
510 xcb_xfixes_copy_region(w->sc->dpy->conn, window_get_region(w),
512 xcb_xfixes_translate_region(w->sc->dpy->conn, wd->shadow_region,
513 d->xshadowoff, d->yshadowoff);
514 wd->shadow_valid = TRUE;
520 render_window_opacity_change(d_window_t *w)
525 d = screen_find_plugin_data(w->sc, plugin_id);
526 wd = window_find_plugin_data(w, plugin_id);
529 d->window_opacity_change(w);
533 /* update the window and its shadows opacities */
534 render_update_opacity(w, d, wd);
536 /* make sure the shadow region is right */
537 render_update_shadow(w, d, wd);
539 /* redraw the window's contents, and its shadow as well */
540 xcb_xfixes_union_region(w->sc->dpy->conn, d->damaged_region,
541 window_get_region(w), d->damaged_region);
542 xcb_xfixes_union_region(w->sc->dpy->conn, d->damaged_region,
543 wd->shadow_region, d->damaged_region);
545 //printf("opacity change 0x%x\n", w->id);
549 render_window_damage(d_window_t *w)
553 d = screen_find_plugin_data(w->sc, plugin_id);
558 /* redraw the window's contents */
559 xcb_xfixes_union_region(w->sc->dpy->conn, d->damaged_region,
560 window_get_region(w), d->damaged_region);
564 render_root_pixmap_change(d_screen_t *sc)
568 d = screen_find_plugin_data(sc, plugin_id);
569 if (d->root_picture) {
570 xcb_render_free_picture(sc->dpy->conn, d->root_picture);
571 d->root_picture = XCB_NONE;
575 d->screen_root_pixmap_change(sc);
577 /* redraw the screen */
578 xcb_xfixes_copy_region(sc->dpy->conn, d->all_region, d->damaged_region);
582 render_paint(d_screen_t *sc)
584 data_t *d = screen_find_plugin_data(sc, plugin_id);
587 xcb_xfixes_copy_region(sc->dpy->conn, d->damaged_region, d->paint_region);
589 //printf("-- painting --\n");
590 for (it = list_top(sc->stacking); it; it = it->next) {
591 d_window_t *w = it->data;
593 if (!window_is_input_only(w) &&
594 (window_is_mapped(w) || window_is_zombie(w)))
596 int x, y, width, height, bwidth;
600 window_get_area(w, &x, &y, &width, &height, &bwidth);
602 if (!(x < sc->super->width_in_pixels &&
603 y < sc->super->height_in_pixels &&
604 x + width > 0 && y + height > 0))
609 opaque = !window_is_argb(w) && window_get_opacity(w) == 0xffff;
611 wd = window_find_plugin_data(w, plugin_id);
614 xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
618 paint_window(w, d, wd, opaque,
619 x, y, width, height, bwidth);
621 /* remove this window from the paint region, as nothing is
622 above it, so nothing should draw to this space again */
623 xcb_xfixes_subtract_region(sc->dpy->conn, d->paint_region,
624 window_get_region(w),
628 /* save the clip region, when drawing windows (and shadows)
629 below this window, they should use this clip region */
630 xcb_xfixes_copy_region(sc->dpy->conn, d->paint_region,
635 xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
641 for (it = list_bottom(sc->stacking); it; it = it->prev) {
642 d_window_t *w = it->data;
644 if (!window_is_input_only(w) &&
645 (window_is_mapped(w) || window_is_zombie(w)))
647 int x, y, width, height, bwidth;
651 window_get_area(w, &x, &y, &width, &height, &bwidth);
653 if (!(x < sc->super->width_in_pixels &&
654 y < sc->super->height_in_pixels &&
655 (x + width > 0 || x + width + d->xshadowoff > 0) &&
656 (y + height > 0 || y + height + d->yshadowoff > 0)))
661 opaque = !window_is_argb(w) && window_get_opacity(w) == 0xffff;
663 wd = window_find_plugin_data(w, plugin_id);
665 if (!wd->shadow_valid)
666 render_update_shadow(w, d, wd);
668 /* shape the shadow to the window */
669 xcb_xfixes_intersect_region(sc->dpy->conn,
670 wd->paint_clip, wd->shadow_region,
671 d->shadow_paint_region);
672 xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
674 d->shadow_paint_region,
676 paint_shadow(w, d, wd, x, y, width, height, bwidth);
679 /* use the clip region of the highest opaque window seen so
680 far, as nothing should be able to draw on top of that region
682 xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
686 paint_window(w, d, wd, opaque, x, y, width, height, bwidth);
691 xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
696 /* copy the double buffer to the overlay window */
697 #ifndef MONITOR_REPAINT
698 xcb_render_composite(sc->dpy->conn,
699 XCB_RENDER_PICT_OP_SRC,
705 sc->super->width_in_pixels,
706 sc->super->height_in_pixels);
709 /* empty the damaged region */
710 xcb_xfixes_subtract_region(sc->dpy->conn, d->all_region,
711 d->all_region, d->damaged_region);
713 /* call the function we replaced in the chain */
718 paint_root(d_screen_t *sc, data_t *d)
720 xcb_render_picture_t src;
723 if (!d->root_picture)
724 render_update_root_picture(sc, d);
726 if (d->root_picture) {
727 src = d->root_picture;
728 op = XCB_RENDER_PICT_OP_SRC;
732 op = XCB_RENDER_PICT_OP_CLEAR;
735 xcb_render_composite(sc->dpy->conn,
742 sc->super->width_in_pixels,
743 sc->super->height_in_pixels);
747 paint_window(d_window_t *w, data_t *d, window_data_t *wd, gboolean opaque,
748 int x, int y, int width, int height, int bwidth)
751 render_update_picture(w, d, wd);
753 //printf("-- paint window 0x%x picture 0x%x --\n", w->id, wd->picture);
755 xcb_render_picture_t alphamap;
759 op = XCB_RENDER_PICT_OP_SRC;
763 op = XCB_RENDER_PICT_OP_OVER;
764 alphamap = wd->alpha_picture;
767 xcb_render_composite(w->sc->dpy->conn,
773 x, y, width + bwidth*2, height + bwidth *2);
778 paint_shadow(d_window_t *w, data_t *d, window_data_t *wd,
779 int x, int y, int width, int height, int bwidth)
781 xcb_render_composite(w->sc->dpy->conn,
782 XCB_RENDER_PICT_OP_OVER,
787 x+d->xshadowoff, y+d->yshadowoff,
788 width + bwidth*2, height + bwidth *2);