summaryrefslogtreecommitdiff
path: root/hw/xgl/xglsync.c
blob: 8856352f6c6a9689b8db96954efc2f7382e8a9ad (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright © 2004 David Reveman
 * 
 * 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, and that the name of
 * David Reveman not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * David Reveman makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * DAVID REVEMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL DAVID REVEMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: David Reveman <davidr@novell.com>
 */

#include "xgl.h"

/*
 * A pixmap may exist both in hardware and in software. Synchronization
 * is handled as follows:
 *
 * Regions modified by software and hardware needs to be tracked.
 * A software operation requires that a rectangle of pixels matching the
 * extents of the operation is synchronized. A hardware operation
 * requires that all pixels are synchronized. If the bounds of a
 * hardware operation intersects the bounds of a currently synchronized
 * software rectangle, the software rectangle will be discarded and the
 * next software operation will require re-synchronization.
 *
 * A software rectangle of pixels is synchronized as follows. If a
 * previously synchronized rectangle exists, then if this previous
 * rectangle subsumes our new rectangle no pixels are fetched from
 * hardware as all pixels that need to be synchronized are already up to
 * date. If a previously synchronized rectangle exists and it intersects
 * with our new rectangle, then both these rectangles are merged into a
 * larger rectangle and pixels not part of the previous rectangle are
 * fetched form hardware. If a previously synchronized rectangle exists
 * and it doesn't intersect with our new rectangle, then the previous
 * rectangle is discarded and pixels are fetched from hardware so that
 * our new rectangle becomes synchronized.
 *
 * If the pixmap exists in hardware and if it can be a target of a
 * drawing operation, then it is kept synchronized all the time, any
 * pixels modified by software will be transfered to hardware right
 * away. If the pixmap exists in hardware but it can only be used as
 * source of a drawing operation, then synchronization is performed
 * only when needed.
 */

#define ALL_BITS(pPixmap, pBox)			\
    ((pBox)->x1 <= 0 && (pBox)->y1 <= 0 &&	\
     (pBox)->x2 >= (pPixmap)->drawable.width && \
     (pBox)->y2 >= (pPixmap)->drawable.height)

Bool
xglSyncBits (DrawablePtr pDrawable,
	     BoxPtr	 pExtents)
{
    RegionRec region;
    BoxPtr    pBitBox;
    BoxRec    box;

    XGL_DRAWABLE_PIXMAP (pDrawable);
    XGL_PIXMAP_PRIV (pPixmap);

    XGL_DECREMENT_PIXMAP_SCORE (pPixmapPriv, 20);
    
    if (pPixmapPriv->allBits)
	return xglMapPixmapBits (pPixmap);

    pBitBox = &pPixmapPriv->bitBox;

    if (pPixmapPriv->target == xglPixmapTargetIn && pExtents)
    {
	box.x1 = MAX (0, pExtents->x1);
	box.y1 = MAX (0, pExtents->y1);
	box.x2 = MAX (0, MIN (pPixmap->drawable.width, pExtents->x2));
	box.y2 = MAX (0, MIN (pPixmap->drawable.height, pExtents->y2));

	if (!BOX_NOTEMPTY (&box))
	    return xglMapPixmapBits (pPixmap);
	
	if (BOX_NOTEMPTY (pBitBox))
	{
	    RegionRec bitRegion;
	    
	    REGION_INIT (pDrawable->pScreen, &bitRegion, pBitBox, 1);
	    
	    switch (RECT_IN_REGION (pDrawable->pScreen, &bitRegion, &box)) {
	    case rgnIN:
		REGION_INIT (pDrawable->pScreen, &region, NullBox, 0);
		break;
	    case rgnOUT:
		REGION_INIT (pDrawable->pScreen, &region, &box, 1);
		*pBitBox = box;
		pPixmapPriv->allBits = ALL_BITS (pPixmap, pBitBox);
		break;
	    case rgnPART:
 		pBitBox->x1 = MIN (pBitBox->x1, box.x1);
		pBitBox->y1 = MIN (pBitBox->y1, box.y1);
		pBitBox->x2 = MAX (pBitBox->x2, box.x2);
		pBitBox->y2 = MAX (pBitBox->y2, box.y2);
		pPixmapPriv->allBits = ALL_BITS (pPixmap, pBitBox);
		
		REGION_INIT (pDrawable->pScreen, &region, pBitBox, 1);
		REGION_SUBTRACT (pDrawable->pScreen, &region, &region,
				 &bitRegion);
		
		break;
	    }
	    REGION_UNINIT (pDrawable->pScreen, &bitRegion);
	}
	else
	{
	    REGION_INIT (pDrawable->pScreen, &region, &box, 1);
	    *pBitBox = box;
	    pPixmapPriv->allBits = ALL_BITS (pPixmap, pBitBox);
	}
    }
    else
    {
	box.x1 = 0;
	box.y1 = 0;
	box.x2 = pPixmap->drawable.width;
	box.y2 = pPixmap->drawable.height;

	REGION_INIT (pDrawable->pScreen, &region, &box, 1);
	
	if (BOX_NOTEMPTY (pBitBox))
	{
	    RegionRec bitRegion;
	    
	    REGION_INIT (pDrawable->pScreen, &bitRegion, pBitBox, 1);
	    REGION_SUBTRACT (pDrawable->pScreen, &region, &region, &bitRegion);
	    REGION_UNINIT (pDrawable->pScreen, &bitRegion);
	}
	
	*pBitBox = box;
	pPixmapPriv->allBits = TRUE;
    }

    if (!pPixmapPriv->buffer)
	if (!xglAllocatePixmapBits (pPixmap, XGL_PIXMAP_USAGE_HINT_DEFAULT))
	    return FALSE;

    if (pPixmapPriv->pDamage)
    {
	RegionPtr pRegion;

	pRegion = DamageRegion (pPixmapPriv->pDamage);
	REGION_SUBTRACT (pDrawable->pScreen, &region, &region, pRegion);
    }
    
    if (REGION_NOTEMPTY (pDrawable->pScreen, &region) && pPixmapPriv->surface)
    {
	glitz_pixel_format_t format;
	BoxPtr		     pBox;
	int		     nBox;
	
	if (!xglSyncSurface (pDrawable))
	    FatalError (XGL_SW_FAILURE_STRING);

	xglUnmapPixmapBits (pPixmap);
	    
	pBox = REGION_RECTS (&region);
	nBox = REGION_NUM_RECTS (&region);

	format.masks = pPixmapPriv->pPixel->masks;
	
	while (nBox--)
	{
	    format.xoffset = pBox->x1;

	    if (pPixmapPriv->stride < 0)
	    {
		format.skip_lines     = pPixmap->drawable.height - pBox->y2;
		format.bytes_per_line = -pPixmapPriv->stride;
		format.scanline_order = GLITZ_PIXEL_SCANLINE_ORDER_BOTTOM_UP;
	    }
	    else
	    {
		format.skip_lines     = pBox->y1;
		format.bytes_per_line = pPixmapPriv->stride;
		format.scanline_order = GLITZ_PIXEL_SCANLINE_ORDER_TOP_DOWN;
	    }

	    glitz_get_pixels (pPixmapPriv->surface,
			      pBox->x1,
			      pBox->y1,
			      pBox->x2 - pBox->x1,
			      pBox->y2 - pBox->y1,
			      &format,
			      pPixmapPriv->buffer);
	    
	    pBox++;
	}
    }

    REGION_UNINIT (pDrawable->pScreen, &region);

    return xglMapPixmapBits (pPixmap);
}

void
xglSyncDamageBoxBits (DrawablePtr pDrawable)
{
    XGL_DRAWABLE_PIXMAP_PRIV (pDrawable);

    if (!xglSyncBits (pDrawable, &pPixmapPriv->damageBox))
	FatalError (XGL_SW_FAILURE_STRING);
}

Bool
xglSyncSurface (DrawablePtr pDrawable)
{
    RegionPtr pRegion;

    XGL_DRAWABLE_PIXMAP (pDrawable);
    XGL_PIXMAP_PRIV (pPixmap);

    if (!pPixmapPriv->surface)
    {
	if (!pPixmapPriv->format)
	    return FALSE;

	if (!xglCreatePixmapSurface (pPixmap))
	    return FALSE;
    }

    pRegion = DamageRegion (pPixmapPriv->pDamage);

    if (REGION_NOTEMPTY (pDrawable->pScreen, pRegion))
    { 
	glitz_pixel_format_t format;
	BoxPtr		     pBox;
	BoxPtr		     pExt;
	int		     nBox;
	
	xglUnmapPixmapBits (pPixmap);

	nBox = REGION_NUM_RECTS (pRegion);
	pBox = REGION_RECTS (pRegion);
	pExt = REGION_EXTENTS (pDrawable->pScreen, pRegion);

	format.masks   = pPixmapPriv->pPixel->masks;
	format.xoffset = pExt->x1;

	if (pPixmapPriv->stride < 0)
	{
	    format.skip_lines	  = pPixmap->drawable.height - pExt->y2;
	    format.bytes_per_line = -pPixmapPriv->stride;
	    format.scanline_order = GLITZ_PIXEL_SCANLINE_ORDER_BOTTOM_UP;
	}
	else
	{
	    format.skip_lines	  = pExt->y1;
	    format.bytes_per_line = pPixmapPriv->stride;
	    format.scanline_order = GLITZ_PIXEL_SCANLINE_ORDER_TOP_DOWN;
	}

	glitz_surface_set_clip_region (pPixmapPriv->surface,
				       0, 0, (glitz_box_t *) pBox, nBox);

	glitz_set_pixels (pPixmapPriv->surface,
			  pExt->x1,
			  pExt->y1,
			  pExt->x2 - pExt->x1,
			  pExt->y2 - pExt->y1,
			  &format,
			  pPixmapPriv->buffer);

	glitz_surface_set_clip_region (pPixmapPriv->surface, 0, 0, NULL, 0);

	REGION_EMPTY (pDrawable->pScreen, pRegion);
    }

    return TRUE;
}

Bool
xglPrepareTarget (DrawablePtr pDrawable)
{
    XGL_DRAWABLE_PIXMAP (pDrawable);
    XGL_PIXMAP_PRIV (pPixmap);

    switch (pPixmapPriv->target) {
    case xglPixmapTargetNo:
	break;
    case xglPixmapTargetIn:
	XGL_INCREMENT_PIXMAP_SCORE (pPixmapPriv, 10);
	
	if (xglSyncSurface (pDrawable))
	    return TRUE;
	break;
    case xglPixmapTargetOut:
	XGL_INCREMENT_PIXMAP_SCORE (pPixmapPriv, 10);

	if (pPixmapPriv->lock)
	    return FALSE;
	
	if (xglFindOffscreenArea (pDrawable->pScreen, pPixmap))
	    return TRUE;
	break;
    }
    
    return FALSE;
}

void
xglAddSurfaceDamage (DrawablePtr pDrawable,
		     RegionPtr   pRegion)
{
    RegionPtr	    pDamageRegion;
    glitz_surface_t *surface;
    int		    xOff, yOff;
    
    XGL_DRAWABLE_PIXMAP_PRIV (pDrawable);

    pPixmapPriv->damageBox = miEmptyBox;
    if (!pPixmapPriv->format)
	return;

    XGL_GET_DRAWABLE (pDrawable, surface, xOff, yOff);

    if (xOff || yOff)
	REGION_TRANSLATE (pDrawable->pScreen, pRegion, xOff, yOff);

    pDamageRegion = DamageRegion (pPixmapPriv->pDamage);

    REGION_UNION (pDrawable->pScreen, pDamageRegion, pDamageRegion, pRegion);
    
    if (xOff || yOff)
	REGION_TRANSLATE (pDrawable->pScreen, pRegion, -xOff, -yOff);
}

void
xglAddCurrentSurfaceDamage (DrawablePtr pDrawable)
{   
    XGL_DRAWABLE_PIXMAP_PRIV (pDrawable);

    if (!pPixmapPriv->format)
    {
	pPixmapPriv->damageBox = miEmptyBox;
	return;
    }

    if (BOX_NOTEMPTY (&pPixmapPriv->damageBox))
    {
	RegionPtr pDamageRegion;
	RegionRec region;

	pDamageRegion = DamageRegion (pPixmapPriv->pDamage);

	REGION_INIT (pDrawable->pScreen, &region, &pPixmapPriv->damageBox, 1);
	REGION_UNION (pDrawable->pScreen,
		      pDamageRegion, pDamageRegion, &region);
	REGION_UNINIT (pDrawable->pScreen, &region);
	
	pPixmapPriv->damageBox = miEmptyBox;
    }
}

void
xglAddCurrentBitDamage (DrawablePtr pDrawable)
{
    XGL_DRAWABLE_PIXMAP_PRIV (pDrawable);

    if (pPixmapPriv->target == xglPixmapTargetIn &&
	pPixmapPriv->damageBox.x1 < pPixmapPriv->bitBox.x2 &&
	pPixmapPriv->damageBox.y1 < pPixmapPriv->bitBox.y2 &&
	pPixmapPriv->damageBox.x2 > pPixmapPriv->bitBox.x1 &&
	pPixmapPriv->damageBox.y2 > pPixmapPriv->bitBox.y1)
    {
	pPixmapPriv->bitBox = miEmptyBox;
	pPixmapPriv->allBits = FALSE;
    }

    pPixmapPriv->damageBox = miEmptyBox;
}