summaryrefslogtreecommitdiff
path: root/hw/xscreen/xs-pixmap.c
blob: e1b83de4022dd0b8f5be62d64478710151a5650a (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* $Xorg: Pixmap.c,v 1.3 2000/08/17 19:53:28 cpqbld Exp $ */
/*

Copyright 1993 by Davor Matic

Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation.  Davor Matic makes no representations about
the suitability of this software for any purpose.  It is provided "as
is" without express or implied warranty.

*/
/* $XFree86: xc/programs/Xserver/hw/xs/Pixmap.c,v 3.7 2003/07/16 01:38:51 dawes Exp $ */

#ifdef HAVE_XS_CONFIG_H
#include <xs-config.h>
#endif

#include <X11/Xmd.h>
#include <xcb/xcb.h>
#include <xcb/xproto.h>
#include <xcb/xcb_image.h> 
#include "regionstr.h"
#include "pixmapstr.h"
#include "scrnintstr.h"
#include "regionstr.h"
#include "gc.h"
#include "servermd.h"
#include "mi.h"

#include "xs-globals.h"
#include "xs-pixmap.h"

#ifdef PIXPRIV
int XS_PIXMAP_PRIVIndex;	    
#endif

static int xsNumFormats;
static xcb_format_t *xsFormats;

/**
 * Initializes the list of available formats, used
 * in calculating the size and other such places.
 **/
void xsInitFormats()
{
    const xcb_setup_t *setup;
        
    setup = xcb_get_setup(xsConnection);
    xsNumFormats = xcb_setup_pixmap_formats_length(setup);
    xsFormats = xcb_setup_pixmap_formats(setup);
}

/**
 * Calculates the size of a pixmap of a given depth,
 * with a given width and height.
 **/
int xsPixmapCalcSize(int depth, int w, int h)
{
    int size;
    int pad;
    int bpp;
    int i;

    for (i=0; i<xsNumFormats; i++) {
        if (xsFormats[i].depth == depth) {
            pad = xsFormats[i].scanline_pad;
            bpp = xsFormats[i].bits_per_pixel;
            break;
        }
    }
    size = (((bpp * w + pad - 1) & -pad) >> 3)*h;
    return size;
}

/**
 * Creates a new pixmap.
 **/
PixmapPtr xsCreatePixmap(ScreenPtr pScreen, int width, int height, int depth)
{
    PixmapPtr pPixmap;

    pPixmap = AllocatePixmap(pScreen, sizeof(XscreenPrivPixmap));
    if (!pPixmap)
        return NullPixmap;
    pPixmap->drawable.type = DRAWABLE_PIXMAP;
    pPixmap->drawable.class = 0;
    pPixmap->drawable.depth = depth;
    pPixmap->drawable.bitsPerPixel = depth;
    pPixmap->drawable.id = 0;
    pPixmap->drawable.x = 0;
    pPixmap->drawable.y = 0;
    pPixmap->drawable.width = width;
    pPixmap->drawable.height = height;
    pPixmap->drawable.pScreen = pScreen;
    pPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
    pPixmap->refcnt = 1;
    pPixmap->devKind = PixmapBytePad(width, depth);
#ifdef PIXPRIV
    pPixmap->devPrivates[XS_PIXMAP_PRIVateIndex].ptr =
        (pointer)((char *)pPixmap + pScreen->totalPixmapSize);
#else
    pPixmap->devPrivate.ptr = (pointer)(pPixmap + 1);
#endif
    if (width && height){
        XS_PIXMAP_PRIV(pPixmap)->pixmap = xcb_generate_id(xsConnection); 
        xcb_create_pixmap(xsConnection,
                depth,
                XS_PIXMAP_PRIV(pPixmap)->pixmap,
                xsBackingRoot,
                width, height);
    } else
        XS_PIXMAP_PRIV(pPixmap)->pixmap = 0;

    return pPixmap;
}

/**
 * Destroys a pixmap*
 **/
Bool xsDestroyPixmap(PixmapPtr pPixmap)
{
    if(--pPixmap->refcnt)
        return TRUE;
    xcb_free_pixmap(xsConnection, XS_PIXMAP_PRIV(pPixmap)->pixmap);
    xfree(pPixmap);
    return TRUE;
}

/**
 * Converts a pixmap to a region
 **/
RegionPtr xsPixmapToRegion(PixmapPtr pPixmap)
{
    xcb_image_t *ximage;
    register RegionPtr pReg, pTmpReg;
    register int x, y;
    unsigned long previousPixel, currentPixel;
    BoxRec Box;
    Bool overlap;

    ximage = xcb_image_get(xsConnection,
                        (xcb_drawable_t)XS_PIXMAP_PRIV(pPixmap)->pixmap,
                        0, 0,
                        pPixmap->drawable.width, pPixmap->drawable.height,
                        1,
                        XYPixmap);

    pReg = REGION_CREATE(pPixmap->drawable.pScreen, NULL, 1);
    pTmpReg = REGION_CREATE(pPixmap->drawable.pScreen, NULL, 1);
    if(!pReg || !pTmpReg) {
        xcb_image_destroy(ximage);
        return NullRegion;
    }

    for (y = 0; y < pPixmap->drawable.height; y++) {
        Box.y1 = y;
        Box.y2 = y + 1;
        previousPixel = 0L;
        for (x = 0; x < pPixmap->drawable.width; x++) {
            currentPixel = xcb_image_get_pixel(ximage, x, y);
            if (previousPixel != currentPixel) {
                if (previousPixel == 0L) { 
                    /* left edge */
                    Box.x1 = x;
                }
                else if (currentPixel == 0L) {
                    /* right edge */
                    Box.x2 = x;
                    REGION_RESET(pPixmap->drawable.pScreen, pTmpReg, &Box);
                    REGION_APPEND(pPixmap->drawable.pScreen, pReg, pTmpReg);
                }
                previousPixel = currentPixel;
            }
        }
        if (previousPixel != 0L) {
            /* right edge because of the end of pixmap */
            Box.x2 = pPixmap->drawable.width;
            REGION_RESET(pPixmap->drawable.pScreen, pTmpReg, &Box);
            REGION_APPEND(pPixmap->drawable.pScreen, pReg, pTmpReg);
        }
    }

    REGION_DESTROY(pPixmap->drawable.pScreen, pTmpReg);
    xcb_image_destroy(ximage);

    REGION_VALIDATE(pPixmap->drawable.pScreen, pReg, &overlap);

    return(pReg);
}