xaaFillArc.c revision 706f2543
1/*
2 * Copyright 1996  The XFree86 Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * HARM HANEMAAYER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 * Written by Harm Hanemaayer (H.Hanemaayer@inter.nl.net).
23 */
24
25/*
26 * Filled solid arcs, based on cfbfillarc.c.
27 *
28 * Fill arc using calls to low-level span fill. Because the math for
29 * each span can be done concurrently with the drawing of the span
30 * with a graphics coprocessor operation, this is faster than just
31 * using miPolyFillArc, which first calculates all the spans and then
32 * calls FillSpans.
33 *
34 * Clipped arcs are dispatched to FillSpans.
35 */
36#ifdef HAVE_XORG_CONFIG_H
37#include <xorg-config.h>
38#endif
39
40#include <limits.h>
41
42#include "misc.h"
43#include "xf86.h"
44#include "xf86_OSproc.h"
45
46#include <X11/X.h>
47#include "scrnintstr.h"
48#include "pixmapstr.h"
49#include "xf86str.h"
50#include "xaa.h"
51#include "xaalocal.h"
52#include "mifillarc.h"
53#include "mi.h"
54
55/*
56 * This is based on the integer-math versions from mi. Perhaps on a
57 * Pentium, the floating-point (double)-math version is faster.
58 */
59
60static void
61XAAFillEllipseSolid(DrawablePtr pDraw, GCPtr pGC, xArc *arc)
62{
63    XAAInfoRecPtr infoRec = GET_XAAINFORECPTR_FROM_GC(pGC);
64    register int x, y, e;
65    int yk, xk, ym, xm, dx, dy, xorg, yorg;
66    int slw;
67    miFillArcRec info;
68
69    (*infoRec->SetupForSolidFill)(infoRec->pScrn, pGC->fgPixel, pGC->alu,
70		pGC->planemask);
71
72    miFillArcSetup(arc, &info);
73    MIFILLARCSETUP();
74    if (pGC->miTranslate)
75    {
76	xorg += pDraw->x;
77	yorg += pDraw->y;
78    }
79    while (y > 0)
80    {
81	MIFILLARCSTEP(slw);
82	if (slw > 0) {
83	    (*infoRec->SubsequentSolidFillRect)(infoRec->pScrn, xorg - x,
84		    yorg - y, slw, 1);
85            if (miFillArcLower(slw))
86		(*infoRec->SubsequentSolidFillRect)(infoRec->pScrn,
87			xorg - x, yorg + y + dy, slw, 1);
88	}
89    }
90
91    SET_SYNC_FLAG(infoRec);
92}
93
94
95#define ADDSPAN(l,r) \
96    if (r >= l) \
97	(*infoRec->SubsequentSolidFillRect)( \
98	    infoRec->pScrn, l, ya, r - l + 1, 1);
99
100#define ADDSLICESPANS(flip) \
101    if (!flip) \
102    { \
103	ADDSPAN(xl, xr); \
104    } \
105    else \
106    { \
107	xc = xorg - x; \
108	ADDSPAN(xc, xr); \
109	xc += slw - 1; \
110	ADDSPAN(xl, xc); \
111    }
112
113static void
114XAAFillArcSliceSolid(DrawablePtr pDraw, GCPtr pGC, xArc *arc)
115{
116    XAAInfoRecPtr infoRec = GET_XAAINFORECPTR_FROM_GC(pGC);
117    int yk, xk, ym, xm, dx, dy, xorg, yorg, slw;
118    register int x, y, e;
119    miFillArcRec info;
120    miArcSliceRec slice;
121    int ya, xl, xr, xc;
122
123    (*infoRec->SetupForSolidFill)(infoRec->pScrn, pGC->fgPixel, pGC->alu,
124        pGC->planemask);
125
126    miFillArcSetup(arc, &info);
127    miFillArcSliceSetup(arc, &slice, pGC);
128    MIFILLARCSETUP();
129    slw = arc->height;
130    if (slice.flip_top || slice.flip_bot)
131	slw += (arc->height >> 1) + 1;
132    if (pGC->miTranslate)
133    {
134	xorg += pDraw->x;
135	yorg += pDraw->y;
136	slice.edge1.x += pDraw->x;
137	slice.edge2.x += pDraw->x;
138    }
139    while (y > 0)
140    {
141	MIFILLARCSTEP(slw);
142	MIARCSLICESTEP(slice.edge1);
143	MIARCSLICESTEP(slice.edge2);
144	if (miFillSliceUpper(slice))
145	{
146	    ya = yorg - y;
147	    MIARCSLICEUPPER(xl, xr, slice, slw);
148
149	    ADDSLICESPANS(slice.flip_top);
150	}
151	if (miFillSliceLower(slice))
152	{
153	    ya = yorg + y + dy;
154	    MIARCSLICELOWER(xl, xr, slice, slw);
155	    ADDSLICESPANS(slice.flip_bot);
156	}
157    }
158
159    SET_SYNC_FLAG(infoRec);
160}
161
162
163void
164XAAPolyFillArcSolid(DrawablePtr pDraw, GCPtr pGC, int narcs, xArc *parcs)
165{
166    register xArc *arc;
167    register int i;
168    int x2, y2;
169    BoxRec box;
170    RegionPtr cclip;
171
172    cclip = pGC->pCompositeClip;
173
174    if(!RegionNumRects(cclip))
175	return;
176
177    for (arc = parcs, i = narcs; --i >= 0; arc++)
178    {
179	if (miFillArcEmpty(arc))
180	    continue;
181	if (miCanFillArc(arc))
182	{
183	    box.x1 = arc->x + pDraw->x;
184	    box.y1 = arc->y + pDraw->y;
185 	    /*
186 	     * Because box.x2 and box.y2 get truncated to 16 bits, and the
187 	     * RECT_IN_REGION test treats the resulting number as a signed
188 	     * integer, the RECT_IN_REGION test alone can go the wrong way.
189 	     * This can result in a server crash because the rendering
190 	     * routines in this file deal directly with cpu addresses
191 	     * of pixels to be stored, and do not clip or otherwise check
192 	     * that all such addresses are within their respective pixmaps.
193 	     * So we only allow the RECT_IN_REGION test to be used for
194 	     * values that can be expressed correctly in a signed short.
195 	     */
196 	    x2 = box.x1 + (int)arc->width + 1;
197 	    box.x2 = x2;
198 	    y2 = box.y1 + (int)arc->height + 1;
199 	    box.y2 = y2;
200 	    if ( (x2 <= SHRT_MAX) && (y2 <= SHRT_MAX) &&
201		    (RegionContainsRect(cclip, &box) == rgnIN) )
202	    {
203		if ((arc->angle2 >= FULLCIRCLE) ||
204		    (arc->angle2 <= -FULLCIRCLE))
205		    XAAFillEllipseSolid(pDraw, pGC, arc);
206		else
207		    XAAFillArcSliceSolid(pDraw, pGC, arc);
208		continue;
209	    }
210	}
211	miPolyFillArc(pDraw, pGC, 1, arc);
212    }
213}
214