summaryrefslogtreecommitdiff
path: root/surface.cpp
blob: a38a42cae39dcb7c86945875cb599a88d506dc9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <QImage>

#include "surface.h"
//test

#include <stdlib.h>
#include <stdio.h>

XRenderSurf *
xrender_surf_new(Display *disp, Drawable draw, Visual *vis, int w, int h, int alpha)
{
    XRenderSurf *rs;
    XRenderPictFormat *fmt;
    XRenderPictureAttributes att;

    rs = (XRenderSurf*)calloc(1, sizeof(XRenderSurf));
    if (alpha)
        fmt = XRenderFindStandardFormat(disp, PictStandardARGB32);
    else
        fmt = XRenderFindStandardFormat(disp, PictStandardRGB24);
    rs->w = w;
    rs->h = h;
    rs->depth = fmt->depth;
    rs->vis = vis;
    rs->draw = XCreatePixmap(disp, draw, w, h, fmt->depth);
    att.dither = 1;
    att.component_alpha = 1;
    att.repeat = 0;
    rs->pic = XRenderCreatePicture(disp, rs->draw, fmt,
                                   CPRepeat | CPDither | CPComponentAlpha, &att);
    rs->allocated = 1;
    return rs;
}

XRenderSurf *
xrender_surf_adopt(Display *disp, Drawable draw, Visual *vis, int w, int h)
{
    XRenderSurf *rs;
    XRenderPictFormat *fmt;
    XRenderPictureAttributes att;

    rs = (XRenderSurf*)calloc(1, sizeof(XRenderSurf));
    fmt = XRenderFindVisualFormat(disp, vis);
    rs->w = w;
    rs->h = h;
    rs->depth = fmt->depth;
    rs->vis = vis;
    rs->draw = draw;
    att.dither = 1;
    att.component_alpha = 1;
    att.repeat = 0;
    rs->pic = XRenderCreatePicture(disp, rs->draw, fmt, CPRepeat | CPDither | CPComponentAlpha, &att);
    rs->allocated = 0;
    return rs;
}

void
xrender_surf_free(Display *disp, XRenderSurf *rs)
{
    if (rs->allocated) XFreePixmap(disp, rs->draw);
    XRenderFreePicture(disp, rs->pic);
    free(rs);
}

void
xrender_surf_populate(Display *disp, XRenderSurf *rs, int w, int h, const QImage &img_data)
{
    GC gc;
    XGCValues gcv;
    XImage *xim;
    int x, y;

    /* yes this isn't optimal - i know.. i just want some data for now */
    gc = XCreateGC(disp, rs->draw, 0, &gcv);
    xim = XCreateImage(disp, rs->vis, rs->depth, ZPixmap, 0, NULL, w, h, 32, 0);
    xim->data = (char*)malloc(xim->bytes_per_line * xim->height);
    for (y = 0; y < h; y++)
    {
	for (x = 0; x < w; x++)
        {
            QRgb pixel;
            int a, r, g, b;

            pixel = img_data.pixel(x, y);
            a = (pixel >> 24) & 0xff;
            r = (pixel >> 16) & 0xff;
            g = (pixel >> 8 ) & 0xff;
            b = (pixel      ) & 0xff;
            r = (r * (a + 1)) / 256;
            g = (g * (a + 1)) / 256;
            b = (b * (a + 1)) / 256;
            XPutPixel(xim, x, y, (a << 24) | (r << 16) | (g << 8) | b);
        }
    }
    XPutImage(disp, rs->draw, gc, xim, 0, 0, 0, 0, w, h);
    free(xim->data);
    xim->data = NULL;
    XDestroyImage(xim);
    XFreeGC(disp, gc);
}

void
xrender_surf_prepare(Display *disp, XRenderSurf *src, int w, int h,
                     int transformations, SurfaceFilter filter)
{
    if (transformations) {
        XTransform xf;

        xf.matrix[0][0] = (65536 * src->w) / (w*2); xf.matrix[0][1] = 0; xf.matrix[0][2] = 0;
        xf.matrix[1][0] = 0; xf.matrix[1][1] = (65536 * src->h) / h; xf.matrix[1][2] = 0;
        xf.matrix[2][0] = 0; xf.matrix[2][1] = 0; xf.matrix[2][2] = 65536;
        XRenderSetPictureTransform(disp, src->pic, &xf);
    }

    switch(filter) {
    case SurfaceBilinear:
        XRenderSetPictureFilter(disp, src->pic, "bilinear", NULL, 0);
        break;
    case SurfaceNearest:
        XRenderSetPictureFilter(disp, src->pic, "nearest", NULL, 0);
        break;
    default:
        break;
    }
}

void
xrender_surf_blend(Display *disp, int op, XRenderSurf *src, XRenderSurf *dst,
                   int x, int y, int w, int h)
{
    XRenderComposite(disp, op, src->pic, None, dst->pic, 0, 0, 0, 0, x, y, w, h);
}