super fading
[dana/dcompmgr.git] / render.c
index 1843cc4..113070e 100644 (file)
--- a/render.c
+++ b/render.c
+#include "efence.h"
+
 #include "render.h"
 #include "screen.h"
+#include "window.h"
+#include "display.h"
+#include "list.h"
 #include <stdio.h>
+#include <assert.h>
 #include <stdlib.h>
 #include <xcb/render.h>
 
-#define PLUGIN_NAME "render"
+static int plugin_id;
 
 typedef struct {
     void (*screen_paint)(d_screen_t *sc);
+    void (*screen_root_pixmap_change)(d_screen_t *sc);
+    void (*window_show)(d_window_t *w);
+    void (*window_zombie_dead)(d_window_t *w);
+    void (*window_resize)(d_window_t *w);
+    void (*window_opacity_change)(d_window_t *w);
+
+    xcb_render_pictformat_t root_format;
+    xcb_render_pictformat_t argb32_format;
+    xcb_render_query_pict_formats_reply_t *pict_formats;
+    xcb_render_picture_t overlay_picture;
+    xcb_render_picture_t overlay_buffer;
+    xcb_render_picture_t root_picture;
+    xcb_render_picture_t solid_bg;
+
+    xcb_xfixes_region_t all_region;
+    xcb_xfixes_region_t paint_region;
+    xcb_xfixes_region_t shadow_region;
+
+    uint16_t shadowalpha;
+    int xshadowoff;
+    int yshadowoff;
 } data_t;
 
-static void render_paint_screen(d_screen_t *sc);
+typedef struct {
+    xcb_render_picture_t picture;
+    xcb_render_picture_t shadow_picture;
+    xcb_xfixes_region_t paint_clip;
+} window_data_t;
+
+static void render_paint(d_screen_t *sc);
+static void render_root_pixmap_change(d_screen_t *sc);
+static void paint_root(d_screen_t *sc, data_t *d);
+static void paint_window(d_window_t *window, data_t *d, window_data_t *wd,
+                         gboolean opaque, int x, int y, int width,
+                         int height, int bwidth);
+static void paint_shadow(d_window_t *w, data_t *d, window_data_t *wd,
+                         int x, int y, int width, int height, int bwidth);
+static void render_update_shadow_picture(d_window_t *w, data_t *d,
+                                         window_data_t *wd);
+static void render_update_picture(d_window_t *w, data_t *d, window_data_t *wd);
+static void render_update_root_picture(d_screen_t *sc, data_t *d);
+static void render_free_picture(d_window_t *w, window_data_t *wd);
+static xcb_render_pictformat_t find_visual_format(data_t *d,
+                                                  xcb_visualid_t visual);
+static xcb_render_pictformat_t find_argb32_format(data_t *d);
+static xcb_render_picture_t solid_picture(data_t *d, d_screen_t *sc,
+                                          uint16_t a, uint16_t r,
+                                          uint16_t g, uint16_t b);
+
+static void render_window_show(d_window_t *window);
+static void render_window_zombie_dead(d_window_t *window);
+static void render_window_resize(d_window_t *window);
+static void render_window_opacity_change(d_window_t *w);
 
 void
-render_init(d_screen_t *sc)
+render_init(d_screen_t *sc, int id)
 {
+    xcb_render_query_pict_formats_cookie_t ck;
+    xcb_pixmap_t px;
+    xcb_rectangle_t rect;
+
+    plugin_id = id;
+
     data_t *d = malloc(sizeof(data_t));
     d->screen_paint = sc->screen_paint;
-    screen_add_plugin_data(sc, PLUGIN_NAME, d);
+    d->screen_root_pixmap_change = sc->screen_root_pixmap_change;
+    d->window_show = sc->window_show;
+    d->window_zombie_dead = sc->window_zombie_dead;
+    d->window_resize = sc->window_resize;
+    d->window_opacity_change = sc->window_opacity_change;
+    screen_add_plugin_data(sc, plugin_id, d);
 
-    sc->screen_paint = render_paint_screen;
+    sc->screen_paint = render_paint;
+    sc->screen_root_pixmap_change = render_root_pixmap_change;
+    sc->window_show = render_window_show;
+    sc->window_zombie_dead = render_window_zombie_dead;
+    sc->window_resize = render_window_resize;
+    sc->window_opacity_change = render_window_opacity_change;
+
+    ck = xcb_render_query_pict_formats_unchecked(sc->dpy->conn);
+    d->pict_formats = xcb_render_query_pict_formats_reply(sc->dpy->conn, ck,
+                                                          NULL);
+
+    d->root_format = find_visual_format(d, sc->super.root_visual);
+    d->argb32_format = find_argb32_format(d);
+    d->root_picture = XCB_NONE;
+
+    d->overlay_picture = xcb_generate_id(sc->dpy->conn);
+    xcb_render_create_picture(sc->dpy->conn,
+                              d->overlay_picture, sc->overlay, d->root_format,
+                              0, NULL);
+
+    /* make the double buffer */
+    px = xcb_generate_id(sc->dpy->conn);
+    xcb_create_pixmap(sc->dpy->conn, sc->super.root_depth, px,
+                      sc->super.root, sc->super.width_in_pixels,
+                      sc->super.height_in_pixels);
+    d->overlay_buffer = xcb_generate_id(sc->dpy->conn);
+    xcb_render_create_picture(sc->dpy->conn, d->overlay_buffer, px,
+                              d->root_format, 0, 0);
+    xcb_free_pixmap(sc->dpy->conn, px);
+
+    d->solid_bg = solid_picture(d, sc, 0xffff, 0x6060, 02020, 0x3030);
+
+    d->all_region = xcb_generate_id(sc->dpy->conn);
+    d->paint_region = xcb_generate_id(sc->dpy->conn);
+    d->shadow_region = xcb_generate_id(sc->dpy->conn);
+    rect.x = rect.y = 0;
+    rect.width = sc->super.width_in_pixels;
+    rect.height = sc->super.height_in_pixels;
+    xcb_xfixes_create_region(sc->dpy->conn, d->all_region, 1, &rect);
+    xcb_xfixes_create_region(sc->dpy->conn, d->paint_region, 1, &rect);
+    xcb_xfixes_create_region(sc->dpy->conn, d->shadow_region, 1, &rect);
+
+    d->shadowalpha = 0x3333; /* 20% */
+    d->xshadowoff = 2;
+    d->yshadowoff = 2;
 }
 
 void
 render_free(d_screen_t *sc)
 {
-    data_t *d = screen_find_plugin_data(sc, PLUGIN_NAME);
+    data_t *d = screen_find_plugin_data(sc, plugin_id);
+    free(d->pict_formats);
+    xcb_render_free_picture(sc->dpy->conn, d->solid_bg);
+    if (d->root_picture)
+        xcb_render_free_picture(sc->dpy->conn, d->root_picture);
+    xcb_render_free_picture(sc->dpy->conn, d->overlay_picture);
+    xcb_render_free_picture(sc->dpy->conn, d->overlay_buffer);
+    xcb_xfixes_destroy_region(sc->dpy->conn, d->all_region);
+    xcb_xfixes_destroy_region(sc->dpy->conn, d->paint_region);
+    xcb_xfixes_destroy_region(sc->dpy->conn, d->shadow_region);
     free(d);
+    screen_remove_plugin_data(sc, plugin_id);
+}
+
+int
+render_next_timeout(struct d_screen *sc, struct timeval *tv)
+{
+    (void)sc;
+    (void)tv;
+    return FALSE;
+}
+
+void
+render_timeout(struct d_screen *sc, const struct timeval *now)
+{
+    (void)sc; (void)now;
+}
+
+void
+render_window_free(d_window_t *w, window_data_t *wd)
+{
+    render_free_picture(w, wd);
+    if (wd->shadow_picture) {
+        xcb_render_free_picture(w->sc->dpy->conn, wd->shadow_picture);
+        wd->shadow_picture = XCB_NONE;
+    }
+    free(wd);
+}
+
+static void
+render_window_show(d_window_t *w)
+{
+    data_t *d;
+    window_data_t *wd;
+    xcb_rectangle_t rect;
+
+    d = screen_find_plugin_data(w->sc, plugin_id);
+
+    /* pass it on */
+    d->window_show(w);
+
+    wd = window_find_plugin_data(w, plugin_id);
+    if (wd)
+        render_window_free(w, wd);
+   
+    wd = malloc(sizeof(window_data_t));
+    wd->picture = XCB_NONE;
+
+    wd->paint_clip = xcb_generate_id(w->sc->dpy->conn);
+    rect.x = rect.y = 0;
+    rect.width = rect.height = 1;
+    xcb_xfixes_create_region(w->sc->dpy->conn, wd->paint_clip, 1, &rect);
+
+    render_update_shadow_picture(w, d, wd);
+
+    window_add_plugin_data(w, plugin_id, wd);
+}
+
+static void
+render_window_zombie_dead(d_window_t *w)
+{
+    data_t *d;
+    window_data_t *wd;
+
+    d = screen_find_plugin_data(w->sc, plugin_id);
+    wd = window_find_plugin_data(w, plugin_id);
+    if (wd) {
+        render_window_free(w, wd);
+        xcb_xfixes_destroy_region(w->sc->dpy->conn, wd->paint_clip);
+        window_remove_plugin_data(w, plugin_id);
+    }
+
+    /* pass it on */
+    d->window_zombie_dead(w);
+}
+
+static xcb_render_picture_t
+solid_picture(data_t *d, d_screen_t *sc, uint16_t a, uint16_t r,
+              uint16_t g, uint16_t b)
+{
+    xcb_pixmap_t pixmap;
+    xcb_render_picture_t picture;
+    xcb_render_color_t   c;
+    const uint32_t vals = XCB_RENDER_REPEAT_NORMAL;
+    const xcb_rectangle_t rect = { 0, 0, 1, 1 };
+
+    pixmap = xcb_generate_id(sc->dpy->conn);
+    picture = xcb_generate_id(sc->dpy->conn);
+
+    xcb_create_pixmap(sc->dpy->conn, 32, pixmap, sc->super.root, 1, 1);
+    xcb_render_create_picture(sc->dpy->conn, picture, pixmap, d->argb32_format,
+                              XCB_RENDER_CP_REPEAT, &vals);
+
+    c.alpha = a;
+    c.red   = r;
+    c.green = g;
+    c.blue  = b;
+
+    xcb_render_fill_rectangles(sc->dpy->conn, XCB_RENDER_PICT_OP_SRC,
+                               picture, c, 1, &rect);
+    xcb_free_pixmap(sc->dpy->conn, pixmap);
+
+    return picture;
+}
+
+static xcb_render_pictformat_t
+find_argb32_format(data_t *d)
+{
+    xcb_render_pictforminfo_iterator_t it;
+
+    for (it = xcb_render_query_pict_formats_formats_iterator(d->pict_formats);
+         it.rem; xcb_render_pictforminfo_next(&it))
+    {
+        xcb_render_pictforminfo_t *format = it.data;
+        if (format->type == XCB_RENDER_PICT_TYPE_DIRECT) {
+            if (format->depth              == 32   &&
+                format->direct.alpha_mask  == 0xff &&
+                format->direct.red_mask    == 0xff &&
+                format->direct.green_mask  == 0xff &&
+                format->direct.blue_mask   == 0xff &&
+                format->direct.alpha_shift == 24   &&
+                format->direct.red_shift   == 16   &&
+                format->direct.green_shift == 8    &&
+                format->direct.blue_shift  == 0)
+                return format->id;
+        }
+    }
+    return XCB_NONE;
+}
+
+static xcb_render_pictformat_t
+find_visual_format(data_t *d, xcb_visualid_t visual)
+{
+    xcb_render_pictscreen_iterator_t si;
+    xcb_render_pictdepth_iterator_t di;
+    xcb_render_pictvisual_iterator_t vi;
+
+    if (!visual) return XCB_NONE;
+
+    /* go through all the screens */
+    si = xcb_render_query_pict_formats_screens_iterator(d->pict_formats);
+    for (; si.rem; xcb_render_pictscreen_next(&si)) {
+        di = xcb_render_pictscreen_depths_iterator(si.data);
+        for (; di.rem; xcb_render_pictdepth_next(&di)) {
+            vi = xcb_render_pictdepth_visuals_iterator(di.data);
+            for (; vi.rem; xcb_render_pictvisual_next(&vi)) {
+                if (vi.data->visual == visual)
+                    return vi.data->format;
+            }
+        }
+    }
+    return XCB_NONE;
+}
+
+static void
+render_free_picture(d_window_t *w, window_data_t *wd)
+{
+    /* this might cause an error, oh well */
+    if (wd->picture) {
+        xcb_render_free_picture(w->sc->dpy->conn, wd->picture);
+        wd->picture = XCB_NONE;
+    }
+}
+
+static void
+render_update_shadow_picture(d_window_t *w, data_t *d, window_data_t *wd)
+{
+    if (wd->shadow_picture)
+        xcb_render_free_picture(w->sc->dpy->conn, wd->shadow_picture);
+    wd->shadow_picture = solid_picture(d, w->sc,
+                                       d->shadowalpha *
+                                       window_get_opacity(w) / 0xffff,
+                                       0, 0, 0);
+}
+
+static void
+render_update_root_picture(d_screen_t *sc, data_t *d)
+{
+    xcb_pixmap_t px;
+
+    px = screen_get_root_pixmap(sc);
+    if (px) {
+        d->root_picture = xcb_generate_id(sc->dpy->conn);
+        xcb_render_create_picture(sc->dpy->conn,
+                                  d->root_picture, px,
+                                  d->root_format, 0, NULL);
+    }
+}
+
+static void
+render_update_picture(d_window_t *w, data_t *d, window_data_t *wd)
+{
+    xcb_pixmap_t px;
+
+    px = window_get_pixmap(w);
+    //printf("got pixmap 0x%x\n", px);
+    if (px) {
+        xcb_render_pictformat_t format;
+        const uint32_t vals = XCB_SUBWINDOW_MODE_INCLUDE_INFERIORS;
+
+        render_free_picture(w, wd);
+
+        wd->picture = xcb_generate_id(w->sc->dpy->conn);
+        format = find_visual_format(d, window_get_visual(w));
+        /* we don't need to check this.  if it fails, we'll just be drawing
+           an invalid picture and creating some X errors but that's no big
+           deal really */
+        xcb_render_create_picture(w->sc->dpy->conn,
+                                  wd->picture, px, format,
+                                  XCB_RENDER_CP_SUBWINDOW_MODE,
+                                  &vals);
+    }
+}
+
+static void
+render_window_resize(d_window_t *w)
+{
+    data_t *d;
+    window_data_t *wd;
+
+    d = screen_find_plugin_data(w->sc, plugin_id);
+    wd = window_find_plugin_data(w, plugin_id);
+
+    /* pass it on */
+    d->window_resize(w);
+
+    assert(wd != NULL);
+    render_free_picture(w, wd);
 }
 
 static void
-render_paint_screen(d_screen_t *sc)
+render_window_opacity_change(d_window_t *w)
 {
-    data_t *d = screen_find_plugin_data(sc, PLUGIN_NAME);
+    data_t *d;
+    window_data_t *wd;
+
+    d = screen_find_plugin_data(w->sc, plugin_id);
+    wd = window_find_plugin_data(w, plugin_id);
+
+    /* pass it on */
+    d->window_opacity_change(w);
+
+    assert(wd != NULL);
+    render_update_shadow_picture(w, d, wd);
+}
+
+static void
+render_root_pixmap_change(d_screen_t *sc)
+{
+    data_t *d;
+
+    d = screen_find_plugin_data(sc, plugin_id);
+    if (d->root_picture) {
+        xcb_render_free_picture(sc->dpy->conn, d->root_picture);
+        d->root_picture = XCB_NONE;
+    }
+
+    /* pass it on */
+    d->screen_root_pixmap_change(sc);
+}
+
+static void
+render_paint(d_screen_t *sc)
+{
+    data_t *d = screen_find_plugin_data(sc, plugin_id);
+    d_list_it_t *it;
+
+    xcb_xfixes_copy_region(sc->dpy->conn, d->all_region, d->paint_region);
+
+    //printf("-- painting --\n");
+    for (it = list_top(sc->stacking); it; it = it->next) {
+        d_window_t *w = it->data;
+        int x, y, width, height, bwidth;
+
+        window_get_area(w, &x, &y, &width, &height, &bwidth);
+
+        if (!window_is_input_only(w) &&
+            (window_is_mapped(w) || window_is_zombie(w)) &&
+            x < sc->super.width_in_pixels &&
+            y < sc->super.height_in_pixels &&
+            x + width > 0 && y + height > 0)
+        {
+            gboolean opaque = !(window_is_argb(w) ||
+                                window_get_opacity(w) < 0xffff);
+            window_data_t *wd;
 
-    printf("-- painting --\n");
+            wd = window_find_plugin_data(w, plugin_id);
+
+            if (opaque) {
+                paint_window(w, d, wd, opaque, x, y, width, height, bwidth);
+
+                /* remove this window from the paint region, as nothing is
+                   above it, so nothing should draw to this space again */
+                xcb_xfixes_subtract_region(sc->dpy->conn, d->paint_region,
+                                           window_get_region(w),
+                                           d->paint_region);
+                xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
+                                                   d->overlay_buffer,
+                                                   d->paint_region,
+                                                   0, 0);
+            }
+
+            /* save the clip region, when drawing windows (and shadows)
+               below this window, they should use this clip region */
+            xcb_xfixes_copy_region(sc->dpy->conn, d->paint_region,
+                                   wd->paint_clip);
+        }
+    }
+
+    paint_root(sc, d);
+
+    xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
+                                       d->overlay_buffer,
+                                       d->paint_region,
+                                       0, 0);
+
+    for (it = list_bottom(sc->stacking); it; it = it->prev) {
+        d_window_t *w = it->data;
+        int x, y, width, height, bwidth;
+
+        window_get_area(w, &x, &y, &width, &height, &bwidth);
+
+        if (!window_is_input_only(w) &&
+            (window_is_mapped(w) || window_is_zombie(w)) &&
+            x < sc->super.width_in_pixels &&
+            y < sc->super.height_in_pixels &&
+            (x + width > 0 || x + width + d->xshadowoff > 0) &&
+            (y + height > 0 || y + height + d->yshadowoff > 0))
+        {
+            window_data_t *wd;
+            gboolean opaque = !(window_is_argb(w) ||
+                                window_get_opacity(w) < 0xffff);
+
+            wd = window_find_plugin_data(w, plugin_id);
+
+            /* shape the shadow to the window */
+            xcb_xfixes_copy_region(sc->dpy->conn, window_get_region(w),
+                                   d->shadow_region);
+            xcb_xfixes_translate_region(sc->dpy->conn, d->shadow_region,
+                                        d->xshadowoff, d->yshadowoff);
+            xcb_xfixes_intersect_region(sc->dpy->conn,
+                                        wd->paint_clip, d->shadow_region,
+                                        d->shadow_region);
+            xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
+                                               d->overlay_buffer,
+                                               d->shadow_region,
+                                               0, 0);
+            paint_shadow(w, d, wd, x, y, width, height, bwidth);
+
+            if (!opaque) {
+                /* use the clip region of the highest opaque window seen so
+                   far, as nothing should be able to draw on top of that region
+                */
+                xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
+                                                   d->overlay_buffer,
+                                                   wd->paint_clip,
+                                                   0, 0);
+                paint_window(w, d, wd, opaque, x, y, width, height, bwidth);
+            }
+        }
+    }
+
+    xcb_xfixes_set_picture_clip_region(sc->dpy->conn,
+                                       d->overlay_buffer,
+                                       d->all_region,
+                                       0, 0);
+
+    /* copy the double buffer to the overlay window */
+    xcb_render_composite(sc->dpy->conn,
+                         XCB_RENDER_PICT_OP_SRC,
+                         d->overlay_buffer,
+                         XCB_NONE,
+                         d->overlay_picture,
+                         0, 0, 0, 0,
+                         0, 0,
+                         sc->super.width_in_pixels,
+                         sc->super.height_in_pixels);
 
     /* call the function we replaced in the chain */
     d->screen_paint(sc);
 }
+
+static void
+paint_root(d_screen_t *sc, data_t *d)
+{
+    xcb_render_picture_t src;
+    int op;
+
+    if (!d->root_picture)
+        render_update_root_picture(sc, d);
+
+    if (d->root_picture) {
+        src = d->root_picture;
+        op = XCB_RENDER_PICT_OP_SRC;
+    }
+    else {
+        src = d->solid_bg;
+        op = XCB_RENDER_PICT_OP_CLEAR;
+    }
+
+    xcb_render_composite(sc->dpy->conn,
+                         op,
+                         src,
+                         XCB_NONE,
+                         d->overlay_buffer,
+                         0, 0, 0, 0,
+                         0, 0,
+                         sc->super.width_in_pixels,
+                         sc->super.height_in_pixels);
+}
+
+static void
+paint_window(d_window_t *w, data_t *d, window_data_t *wd, gboolean opaque,
+             int x, int y, int width, int height, int bwidth)
+{
+    xcb_render_picture_t alphamap;
+
+    if (!wd->picture)
+        render_update_picture(w, d, wd);
+
+    //printf("-- paint window 0x%x picture 0x%x --\n", w->id, wd->picture);
+    if (wd->picture) {
+        int op;
+
+        op = !opaque ?
+            XCB_RENDER_PICT_OP_OVER : XCB_RENDER_PICT_OP_SRC;
+
+        if (opaque)
+            alphamap = XCB_NONE;
+        else
+            alphamap = solid_picture(d, w->sc, window_get_opacity(w), 0, 0, 0);
+
+        xcb_render_composite(w->sc->dpy->conn,
+                             op,
+                             wd->picture,
+                             alphamap,
+                             d->overlay_buffer,
+                             0, 0, 0, 0,
+                             x, y, width + bwidth*2, height + bwidth *2);
+    }
+}
+
+static void
+paint_shadow(d_window_t *w, data_t *d, window_data_t *wd,
+             int x, int y, int width, int height, int bwidth)
+{
+    xcb_render_composite(w->sc->dpy->conn,
+                         XCB_RENDER_PICT_OP_OVER,
+                         wd->shadow_picture,
+                         wd->picture,
+                         d->overlay_buffer,
+                         0, 0, 0, 0,
+                         x+d->xshadowoff, y+d->yshadowoff,
+                         width + bwidth*2, height + bwidth *2);
+
+}