summaryrefslogtreecommitdiff
path: root/hw/xgl/xgltrap.c
blob: 26e2bc451eee54a72024477b8a3eeefb841cae56 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright © 2005 Novell, Inc.
 *
 * 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
 * Novell, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Novell, Inc. makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL NOVELL, INC. 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"
#include "gcstruct.h"
#include "damage.h"

#ifdef RENDER

#define XGL_TRAP_FALLBACK_PROLOGUE(pPicture, func) \
    xglSyncDamageBoxBits (pPicture->pDrawable);	   \
    XGL_PICTURE_SCREEN_UNWRAP (func)

#define XGL_TRAP_FALLBACK_EPILOGUE(pPicture, func, xglfunc) \
    XGL_PICTURE_SCREEN_WRAP (func, xglfunc);		    \
    xglAddCurrentSurfaceDamage (pPicture->pDrawable)

/* just a guess */
#define SMOOTH_TRAPS_ESTIMATE_RECTS(nTrap) (30 * nTrap)

#define LINE_FIXED_X(l, _y, v)			 \
    dx = (l)->p2.x - (l)->p1.x;			 \
    ex = (xFixed_32_32) ((_y) - (l)->p1.y) * dx; \
    dy = (l)->p2.y - (l)->p1.y;			 \
    (v) = (l)->p1.x + (xFixed) (ex / dy)

#define LINE_FIXED_X_CEIL(l, _y, v)		      \
    dx = (l)->p2.x - (l)->p1.x;			      \
    ex = (xFixed_32_32) ((_y) - (l)->p1.y) * dx;      \
    dy = (l)->p2.y - (l)->p1.y;			      \
    (v) = (l)->p1.x + (xFixed) ((ex + (dy - 1)) / dy)

static Bool
xglTrapezoidExtents (PicturePtr pDst,
		     int        ntrap,
		     xTrapezoid *traps,
		     BoxPtr     extents)
{
    Bool	 x_overlap, overlap = FALSE;
    xFixed	 dx, dy, top, bottom;
    xFixed_32_32 ex;

    if (!ntrap)
    {
	extents->x1 = MAXSHORT;
	extents->x2 = MINSHORT;
	extents->y1 = MAXSHORT;
	extents->y2 = MINSHORT;

	return FALSE;
    }

    extents->y1 = xFixedToInt (traps->top);
    extents->y2 = xFixedToInt (xFixedCeil (traps->bottom));

    LINE_FIXED_X (&traps->left, traps->top, top);
    LINE_FIXED_X (&traps->left, traps->bottom, bottom);
    extents->x1 = xFixedToInt (MIN (top, bottom));

    LINE_FIXED_X_CEIL (&traps->right, traps->top, top);
    LINE_FIXED_X_CEIL (&traps->right, traps->bottom, bottom);
    extents->x2 = xFixedToInt (xFixedCeil (MAX (top, bottom)));

    ntrap--;
    traps++;

    for (; ntrap; ntrap--, traps++)
    {
	INT16 x1, y1, x2, y2;

	if (!xTrapezoidValid (traps))
	    continue;

	y1 = xFixedToInt (traps->top);
	y2 = xFixedToInt (xFixedCeil (traps->bottom));

	LINE_FIXED_X (&traps->left, traps->top, top);
	LINE_FIXED_X (&traps->left, traps->bottom, bottom);
	x1 = xFixedToInt (MIN (top, bottom));

	LINE_FIXED_X_CEIL (&traps->right, traps->top, top);
	LINE_FIXED_X_CEIL (&traps->right, traps->bottom, bottom);
	x2 = xFixedToInt (xFixedCeil (MAX (top, bottom)));

	x_overlap = FALSE;
	if (x1 >= extents->x2)
	    extents->x2 = x2;
	else if (x2 <= extents->x1)
	    extents->x1 = x1;
	else
	{
	    x_overlap = TRUE;
	    if (x1 < extents->x1)
		extents->x1 = x1;
	    if (x2 > extents->x2)
		extents->x2 = x2;
	}

	if (y1 >= extents->y2)
	    extents->y2 = y2;
	else if (y2 <= extents->y1)
	    extents->y1 = y1;
	else
	{
	    if (y1 < extents->y1)
		extents->y1 = y1;
	    if (y2 > extents->y2)
		extents->y2 = y2;

	    if (x_overlap)
		overlap = TRUE;
	}
    }

    xglPictureClipExtents (pDst, extents);

    return overlap;
}

void
xglTrapezoids (CARD8	     op,
	       PicturePtr    pSrc,
	       PicturePtr    pDst,
	       PictFormatPtr maskFormat,
	       INT16	     xSrc,
	       INT16	     ySrc,
	       int	     nTrap,
	       xTrapezoid    *traps)
{
    ScreenPtr	    pScreen = pDst->pDrawable->pScreen;
    PicturePtr	    pMask = NULL, pSrcPicture, pDstPicture;
    PicturePtr	    pMaskPicture = NULL;
    xglGeometryPtr  pGeometry = NULL;
    unsigned int    polyEdge = pDst->polyEdge;
    INT16	    xDst, yDst;
    INT16	    xOff, yOff;
    BoxRec	    extents;
    Bool	    overlap;
    Bool	    target;

    XGL_SCREEN_PRIV (pScreen);

    xDst = traps[0].left.p1.x >> 16;
    yDst = traps[0].left.p1.y >> 16;

    overlap = xglTrapezoidExtents (pDst, nTrap, traps, &extents);
    if (extents.y1 >= extents.y2 || extents.x1 >= extents.x2)
	return;

    target = xglPrepareTarget (pDst->pDrawable);

    if (nTrap > 1 && op != PictOpAdd && maskFormat &&
	(!target || overlap || op != PictOpOver))
    {
	PixmapPtr  pPixmap;
	GCPtr	   pGC;
	xRectangle rect;
	int	   error;
	int	   area;

	if (!pScreenPriv->pSolidAlpha)
	{
	    xglCreateSolidAlphaPicture (pScreen);
	    if (!pScreenPriv->pSolidAlpha)
		return;
	}

	rect.x = 0;
	rect.y = 0;
	rect.width = extents.x2 - extents.x1;
	rect.height = extents.y2 - extents.y1;

	pPixmap = (*pScreen->CreatePixmap) (pScreen,
					    rect.width, rect.height,
					    maskFormat->depth);
	if (!pPixmap)
	    return;

	pMask = CreatePicture (0, &pPixmap->drawable, maskFormat,
			       0, 0, serverClient, &error);
	if (!pMask)
	{
	    (*pScreen->DestroyPixmap) (pPixmap);
	    return;
	}

	if (!target)
	{
	    /* make sure we don't do accelerated drawing to mask */
	    xglSetPixmapVisual (pPixmap, NULL);
	}

	area = rect.width * rect.height;
	if ((SMOOTH_TRAPS_ESTIMATE_RECTS (nTrap) * 4) > area)
	    XGL_GET_PIXMAP_PRIV (pPixmap)->target = xglPixmapTargetNo;

	ValidatePicture (pMask);
	pGC = GetScratchGC (pPixmap->drawable.depth, pScreen);
	ValidateGC (&pPixmap->drawable, pGC);
	(*pGC->ops->PolyFillRect) (&pPixmap->drawable, pGC, 1, &rect);
	FreeScratchGC (pGC);

	(*pScreen->DestroyPixmap) (pPixmap);

	target = xglPrepareTarget (pMask->pDrawable);

	xOff = -extents.x1;
	yOff = -extents.y1;
	pSrcPicture = pScreenPriv->pSolidAlpha;
	pDstPicture = pMask;
    }
    else
    {
	if (maskFormat)
	{
	    if (maskFormat->depth == 1)
		polyEdge = PolyEdgeSharp;
	    else
		polyEdge = PolyEdgeSmooth;
	}

	xOff = 0;
	yOff = 0;
	pSrcPicture = pSrc;
	pDstPicture = pDst;
    }

    if (target)
    {
	if (maskFormat || polyEdge == PolyEdgeSmooth)
	{
	    glitz_vertex_format_t *format;
	    glitz_surface_t	  *mask;
	    xTrapezoid		  *pTrap = traps;
	    int			  nAddedTrap, n = nTrap;
	    int			  offset = 0;
	    int			  size = SMOOTH_TRAPS_ESTIMATE_RECTS (n);

	    pMaskPicture = pScreenPriv->trapInfo.pMask;
	    format = &pScreenPriv->trapInfo.format.vertex;
	    mask = pMaskPicture->pSourcePict->source.devPrivate.ptr;

	    size *= format->bytes_per_vertex;
	    pGeometry = xglGetScratchGeometryWithSize (pScreen, size);

	    while (n)
	    {
		if (pGeometry->size < size)
		    GEOMETRY_RESIZE (pScreen, pGeometry, size);

		if (!pGeometry->buffer)
		{
		    if (pMask)
			FreePicture (pMask, 0);

		    return;
		}

		offset +=
		    glitz_add_trapezoids (pGeometry->buffer,
					  offset, size - offset, format->type,
					  mask, (glitz_trapezoid_t *) pTrap, n,
					  &nAddedTrap);

		n     -= nAddedTrap;
		pTrap += nAddedTrap;
		size  *= 2;
	    }

	    pGeometry->f     = pScreenPriv->trapInfo.format;
	    pGeometry->count = offset / format->bytes_per_vertex;
	}
	else
	{
	    pGeometry =
		xglGetScratchVertexGeometryWithType (pScreen,
						     GEOMETRY_DATA_TYPE_FLOAT,
						     4 * nTrap);
	    if (!pGeometry->buffer)
	    {
		if (pMask)
		    FreePicture (pMask, 0);

		return;
	    }

	    GEOMETRY_ADD_TRAPEZOID (pScreen, pGeometry, traps, nTrap);
	}

	GEOMETRY_TRANSLATE (pGeometry,
			    pDstPicture->pDrawable->x + xOff,
			    pDstPicture->pDrawable->y + yOff);
    }

    if (pGeometry &&
	xglCompositeGeneral (pMask ? PictOpAdd : op,
			     pSrcPicture,
			     pMaskPicture,
			     pDstPicture,
			     pGeometry,
			     extents.x1 + xOff + xSrc - xDst,
			     extents.y1 + yOff + ySrc - yDst,
			     0, 0,
			     pDstPicture->pDrawable->x + extents.x1 + xOff,
			     pDstPicture->pDrawable->y + extents.y1 + yOff,
			     extents.x2 - extents.x1,
			     extents.y2 - extents.y1))
    {
	/* no intermediate mask? we need to register damage from here as
	   CompositePicture will never be called. */
	if (!pMask)
	{
	    RegionRec region;

	    REGION_INIT (pScreen, &region, &extents, 1);
	    REGION_TRANSLATE (pScreen, &region,
			      pDst->pDrawable->x, pDst->pDrawable->y);

	    DamageDamageRegion (pDst->pDrawable, &region);

	    REGION_UNINIT (pScreen, &region);
	}

	xglAddCurrentBitDamage (pDstPicture->pDrawable);
    }
    else
    {
	XGL_DRAWABLE_PIXMAP_PRIV (pDstPicture->pDrawable);

	pPixmapPriv->damageBox.x1 = extents.x1 + xOff;
	pPixmapPriv->damageBox.y1 = extents.y1 + yOff;
	pPixmapPriv->damageBox.x2 = extents.x2 + xOff;
	pPixmapPriv->damageBox.y2 = extents.y2 + yOff;

	xglSyncDamageBoxBits (pDstPicture->pDrawable);

	if (pMask || (polyEdge == PolyEdgeSmooth &&
		      op == PictOpAdd && miIsSolidAlpha (pSrc)))
	{
	    PictureScreenPtr ps = GetPictureScreen (pScreen);

	    for (; nTrap; nTrap--, traps++)
		(*ps->RasterizeTrapezoid) (pDstPicture, traps, xOff, yOff);

	    xglAddCurrentSurfaceDamage (pDstPicture->pDrawable);
	}
	else
	    miTrapezoids (op, pSrc, pDstPicture, maskFormat,
			  xSrc, ySrc, nTrap, traps);
    }

    if (pMask)
    {
	CompositePicture (op, pSrc, pMask, pDst,
			  extents.x1 + xSrc - xDst,
			  extents.y1 + ySrc - yDst,
			  0, 0,
			  extents.x1, extents.y1,
			  extents.x2 - extents.x1,
			  extents.y2 - extents.y1);

	FreePicture (pMask, 0);
    }
}

void
xglAddTraps (PicturePtr pDst,
	     INT16	xOff,
	     INT16	yOff,
	     int	nTrap,
	     xTrap	*traps)
{
    PictureScreenPtr pPictureScreen;
    ScreenPtr	     pScreen = pDst->pDrawable->pScreen;

    XGL_SCREEN_PRIV (pScreen);
    XGL_DRAWABLE_PIXMAP_PRIV (pDst->pDrawable);

    if (!pScreenPriv->pSolidAlpha)
    {
	xglCreateSolidAlphaPicture (pScreen);
	if (!pScreenPriv->pSolidAlpha)
	    return;
    }

    pPixmapPriv->damageBox.x1 = 0;
    pPixmapPriv->damageBox.y1 = 0;
    pPixmapPriv->damageBox.x2 = pDst->pDrawable->width;
    pPixmapPriv->damageBox.y2 = pDst->pDrawable->height;

    if (xglPrepareTarget (pDst->pDrawable))
    {
	PicturePtr	      pMask;
	glitz_vertex_format_t *format;
	glitz_surface_t	      *mask;
	xglGeometryPtr	      pGeometry;
	xTrap		      *pTrap = traps;
	int		      nAddedTrap, n = nTrap;
	int		      offset = 0;
	int		      size = SMOOTH_TRAPS_ESTIMATE_RECTS (n);

	pMask = pScreenPriv->trapInfo.pMask;
	format = &pScreenPriv->trapInfo.format.vertex;
	mask = pMask->pSourcePict->source.devPrivate.ptr;

	size *= format->bytes_per_vertex;
	pGeometry = xglGetScratchGeometryWithSize (pScreen, size);

	while (n)
	{
	    if (pGeometry->size < size)
		GEOMETRY_RESIZE (pScreen, pGeometry, size);

	    if (!pGeometry->buffer)
		return;

	    offset +=
		glitz_add_traps (pGeometry->buffer,
				 offset, size - offset, format->type, mask,
				 (glitz_trap_t *) pTrap, n,
				 &nAddedTrap);

	    n     -= nAddedTrap;
	    pTrap += nAddedTrap;
	    size  *= 2;
	}

	pGeometry->f     = pScreenPriv->trapInfo.format;
	pGeometry->count = offset / format->bytes_per_vertex;

	GEOMETRY_TRANSLATE (pGeometry,
			    pDst->pDrawable->x + xOff,
			    pDst->pDrawable->y + yOff);

	if (xglCompositeGeneral (PictOpAdd,
				 pScreenPriv->pSolidAlpha,
				 pMask,
				 pDst,
				 pGeometry,
				 0, 0,
				 0, 0,
				 pDst->pDrawable->x, pDst->pDrawable->y,
				 pDst->pDrawable->width,
				 pDst->pDrawable->height))
	{
	    xglAddCurrentBitDamage (pDst->pDrawable);
	    return;
	}
    }

    pPictureScreen = GetPictureScreen (pScreen);

    XGL_TRAP_FALLBACK_PROLOGUE (pDst, AddTraps);
    (*pPictureScreen->AddTraps) (pDst, xOff, yOff, nTrap, traps);
    XGL_TRAP_FALLBACK_EPILOGUE (pDst, AddTraps, xglAddTraps);
}

#endif