summaryrefslogtreecommitdiff
path: root/hw/dmx/dmxpixmap.c
blob: 81b71151a48b4107bcfa20c31095a1c108b6b17a (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation on the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * Authors:
 *   Kevin E. Martin <kem@redhat.com>
 *
 */

/** \file
 * Provides pixmap support. */

#ifdef HAVE_DMX_CONFIG_H
#include <dmx-config.h>
#endif

#include "dmx.h"
#include "dmxsync.h"
#include "dmxpixmap.h"

#include "pixmapstr.h"
#include "servermd.h"
#include "privates.h"

/** Initialize a private area in \a pScreen for pixmap information. */
Bool dmxInitPixmap(ScreenPtr pScreen)
{
    if (!dixRegisterPrivateKey(&dmxPixPrivateKeyRec, PRIVATE_PIXMAP, sizeof(dmxPixPrivRec)))
	return FALSE;

    return TRUE;
}

/** Create a pixmap on the back-end server. */
void dmxBECreatePixmap(PixmapPtr pPixmap)
{
    ScreenPtr      pScreen   = pPixmap->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxPixPrivPtr  pPixPriv  = DMX_GET_PIXMAP_PRIV(pPixmap);

    /* Make sure we haven't already created this pixmap.  This can
     * happen when the pixmap is used elsewhere (e.g., as a background
     * or border for a window) and the refcnt > 1.
     */
    if (pPixPriv->pixmap)
	return;

    if (pPixmap->drawable.width && pPixmap->drawable.height) {
	pPixPriv->pixmap = XCreatePixmap(dmxScreen->beDisplay,
					 dmxScreen->scrnWin,
					 pPixmap->drawable.width,
					 pPixmap->drawable.height,
					 pPixmap->drawable.depth);
	dmxSync(dmxScreen, FALSE);
    }
}

/** Create a pixmap for \a pScreen with the specified \a width, \a
 *  height, and \a depth. */
PixmapPtr dmxCreatePixmap(ScreenPtr pScreen, int width, int height, int depth,
			  unsigned usage_hint)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    PixmapPtr      pPixmap;
    int            bpp;
    dmxPixPrivPtr  pPixPriv;

#if 0
    DMX_UNWRAP(CreatePixmap, dmxScreen, pScreen);
    if (pScreen->CreatePixmap)
	ret = pScreen->CreatePixmap(pPixmap);
#endif

    /* Create pixmap on back-end server */
    if (depth == 24) bpp = 32;
    else             bpp = depth;

    pPixmap = AllocatePixmap(pScreen, 0);
    if (!pPixmap)
	return NullPixmap;

    pPixmap->drawable.type = DRAWABLE_PIXMAP;
    pPixmap->drawable.class = 0;
    pPixmap->drawable.pScreen = pScreen;
    pPixmap->drawable.depth = depth;
    pPixmap->drawable.bitsPerPixel = bpp;
    pPixmap->drawable.id = 0;
    pPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
    pPixmap->drawable.x = 0;
    pPixmap->drawable.y = 0;
    pPixmap->drawable.width = width;
    pPixmap->drawable.height = height;
    pPixmap->devKind = PixmapBytePad(width, bpp);
    pPixmap->refcnt = 1;
    pPixmap->usage_hint = usage_hint;

    pPixPriv = DMX_GET_PIXMAP_PRIV(pPixmap);
    pPixPriv->pixmap = (Pixmap)0;
    pPixPriv->detachedImage = NULL;

    /* Create the pixmap on the back-end server */
    if (dmxScreen->beDisplay) {
	dmxBECreatePixmap(pPixmap);
    }

#if 0
    DMX_WRAP(CreatePixmap, dmxCreatePixmap, dmxScreen, pScreen);
#endif

    return pPixmap;
}

/** Destroy the pixmap on the back-end server. */
Bool dmxBEFreePixmap(PixmapPtr pPixmap)
{
    ScreenPtr      pScreen = pPixmap->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    dmxPixPrivPtr  pPixPriv = DMX_GET_PIXMAP_PRIV(pPixmap);

    if (pPixPriv->pixmap) {
	XFreePixmap(dmxScreen->beDisplay, pPixPriv->pixmap);
	pPixPriv->pixmap = (Pixmap)0;
	return TRUE;
    }

    return FALSE;
}

/** Destroy the pixmap pointed to by \a pPixmap. */
Bool dmxDestroyPixmap(PixmapPtr pPixmap)
{
    ScreenPtr      pScreen = pPixmap->drawable.pScreen;
    DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
    Bool           ret = TRUE;

#if 0
    DMX_UNWRAP(DestroyPixmap, dmxScreen, pScreen);
#endif

    if (--pPixmap->refcnt)
	return TRUE;

    /* Destroy pixmap on back-end server */
    if (dmxScreen->beDisplay) {
	if (dmxBEFreePixmap(pPixmap)) {
	    /* Also make sure that we destroy any detached image */
	    dmxPixPrivPtr  pPixPriv = DMX_GET_PIXMAP_PRIV(pPixmap);
	    if (pPixPriv->detachedImage)
		XDestroyImage(pPixPriv->detachedImage);
	    dmxSync(dmxScreen, FALSE);
	}
    }
    FreePixmap(pPixmap);

#if 0
    if (pScreen->DestroyPixmap)
	ret = pScreen->DestroyPixmap(pPixmap);
    DMX_WRAP(DestroyPixmap, dmxDestroyPixmap, dmxScreen, pScreen);
#endif

    return ret;
}

/** Create and return a region based on the pixmap pointed to by \a
 *  pPixmap. */
RegionPtr dmxBitmapToRegion(PixmapPtr pPixmap)
{
    DMXScreenInfo *dmxScreen = &dmxScreens[pPixmap->drawable.pScreen->myNum];
    dmxPixPrivPtr  pPixPriv = DMX_GET_PIXMAP_PRIV(pPixmap);
    XImage        *ximage;
    RegionPtr      pReg, pTmpReg;
    int            x, y;
    unsigned long  previousPixel, currentPixel;
    BoxRec         Box;
    Bool           overlap;
  
    if (!dmxScreen->beDisplay) {
	pReg = RegionCreate(NullBox, 1);
	return pReg;
    }

    ximage = XGetImage(dmxScreen->beDisplay, pPixPriv->pixmap, 0, 0,
		       pPixmap->drawable.width, pPixmap->drawable.height,
		       1, XYPixmap);

    pReg = RegionCreate(NullBox, 1);
    pTmpReg = RegionCreate(NullBox, 1);
    if(!pReg || !pTmpReg) {
	XDestroyImage(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 = XGetPixel(ximage, x, y);
	    if (previousPixel != currentPixel) {
		if (previousPixel == 0L) { 
		    /* left edge */
		    Box.x1 = x;
		} else if (currentPixel == 0L) {
		    /* right edge */
		    Box.x2 = x;
		    RegionReset(pTmpReg, &Box);
		    RegionAppend(pReg, pTmpReg);
		}
		previousPixel = currentPixel;
	    }
	}
	if (previousPixel != 0L) {
	    /* right edge because of the end of pixmap */
	    Box.x2 = pPixmap->drawable.width;
	    RegionReset(pTmpReg, &Box);
	    RegionAppend(pReg, pTmpReg);
	}
    }
  
    RegionDestroy(pTmpReg);
    XDestroyImage(ximage);

    RegionValidate(pReg, &overlap);

    dmxSync(dmxScreen, FALSE);
    return pReg;
}