do_traps.c revision c9e4df9b
1/*****************************************************************************
2Copyright 1988, 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
3
4                        All Rights Reserved
5
6Permission to use, copy, modify, and distribute this software and its
7documentation for any purpose and without fee is hereby granted,
8provided that the above copyright notice appear in all copies and that
9both that copyright notice and this permission notice appear in
10supporting documentation, and that the name of Digital not be
11used in advertising or publicity pertaining to distribution of the
12software without specific, written prior permission.
13
14DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
16DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
19ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20SOFTWARE.
21
22******************************************************************************/
23
24#include "x11perf.h"
25#include "bitmaps.h"
26
27#define NUM_POINTS 4   /* 4 points to a trapezoid */
28static XPoint *points;
29static GC      pgc;
30
31int
32InitTrapezoids(XParms xp, Parms p, int64_t reps)
33{
34    int     numPoints;
35    int     rows;
36    int     x, y;
37    int     size, skew;
38    XPoint  *curPoint;
39
40    pgc = xp->fggc;
41
42    size = p->special;
43    numPoints = (p->objects) * NUM_POINTS;
44    points = malloc(numPoints * sizeof(XPoint));
45    curPoint = points;
46    x = size;
47    y = 0;
48    rows = 0;
49    skew = size;
50
51    for (int i = 0; i != p->objects; i++, curPoint += NUM_POINTS) {
52	curPoint[0].x = x - skew;
53	curPoint[0].y = y;
54	curPoint[1].x = x - skew + size;
55	curPoint[1].y = y;
56	curPoint[2].x = x + skew;
57	curPoint[2].y = y + size;
58	curPoint[3].x = x + skew - size;
59	curPoint[3].y = y + size;
60
61	skew--;
62	if (skew < 0) skew = size;
63
64	y += size;
65	rows++;
66	if (y + size > HEIGHT || rows == MAXROWS) {
67	    rows = 0;
68	    y = 0;
69	    x += 2 * size;
70	    if (x + size > WIDTH) {
71		x = size;
72	    }
73	}
74    }
75
76    SetFillStyle(xp, p);
77    return reps;
78}
79
80void
81DoTrapezoids(XParms xp, Parms p, int64_t reps)
82{
83    for (int i = 0; i != reps; i++) {
84        XPoint  *curPoint = points;
85        for (int j = 0; j != p->objects; j++) {
86            XFillPolygon(xp->d, xp->w, pgc, curPoint, NUM_POINTS, Convex,
87			 CoordModeOrigin);
88            curPoint += NUM_POINTS;
89	}
90        if (pgc == xp->bggc)
91            pgc = xp->fggc;
92        else
93            pgc = xp->bggc;
94	CheckAbort ();
95    }
96}
97
98void
99EndTrapezoids(XParms xp, Parms p)
100{
101    free(points);
102}
103
104#if defined(XRENDER) && defined(XFT)
105#include <X11/extensions/Xrender.h>
106#include <X11/Xft/Xft.h>
107
108static XTrap		    *traps;
109static XTrapezoid	    *trapezoids;
110static XftDraw		    *aadraw;
111static XftColor		    aablack, aawhite;
112static XRenderColor	    transparent = { 0, 0, 0, 0 };
113static XRenderPictFormat    *maskFormat;
114static Pixmap		    maskPixmap;
115static Picture		    mask;
116
117int
118InitFixedTraps(XParms xp, Parms p, int64_t reps)
119{
120    int     numTraps;
121    int     rows;
122    int     x, y;
123    int     size, skew;
124    XTrap	*curTrap;
125    XRenderColor	color;
126    int			major, minor;
127    int		depth;
128    int		width;
129    int		std_fmt;
130
131    XRenderQueryVersion (xp->d, &major, &minor);
132    if (major == 0 && minor < 9)
133	return 0;
134
135    pgc = xp->fggc;
136
137    size = p->special;
138    numTraps = p->objects;
139    traps = malloc(numTraps * sizeof(XTrap));
140    curTrap = traps;
141    x = size;
142    y = 0;
143    rows = 0;
144    skew = size;
145    aadraw = XftDrawCreate (xp->d, xp->w,
146			    xp->vinfo.visual,
147			    xp->cmap);
148
149    depth = 0;
150    width = 0;
151    if (p->font)
152	sscanf (p->font, "%d,%d", &depth, &width);
153
154    switch (depth) {
155    case 8:
156    default:
157	depth = 8;
158	std_fmt = PictStandardA8;
159	break;
160    case 4:
161	std_fmt = PictStandardA4;
162	break;
163    case 1:
164	std_fmt = PictStandardA1;
165	break;
166    }
167    maskFormat = XRenderFindStandardFormat (xp->d, std_fmt);
168    if (!maskFormat)
169	return 0;
170
171    maskPixmap = XCreatePixmap (xp->d, xp->w, WIDTH, HEIGHT, depth);
172
173    mask = XRenderCreatePicture (xp->d, maskPixmap, maskFormat, 0, NULL);
174
175    color.red = 0;
176    color.green = 0;
177    color.blue = 0;
178    color.alpha = 0xffff;
179    if (!XftColorAllocValue (xp->d,
180			     xp->vinfo.visual,
181			     xp->cmap,
182			     &color, &aablack))
183    {
184	XftDrawDestroy (aadraw);
185	aadraw = NULL;
186	return 0;
187    }
188    color.red = 0xffff;
189    color.green = 0xffff;
190    color.blue = 0xffff;
191    color.alpha = 0xffff;
192    if (!XftColorAllocValue (xp->d,
193			     xp->vinfo.visual,
194			     xp->cmap,
195			     &color, &aawhite))
196    {
197	XftDrawDestroy (aadraw);
198	aadraw = NULL;
199	return 0;
200    }
201
202    if (width == 0)
203	width = size;
204    for (int i = 0; i != p->objects; i++, curTrap ++) {
205	curTrap->top.y = XDoubleToFixed (y);
206	curTrap->top.left = XDoubleToFixed (x - skew);
207	curTrap->top.right = XDoubleToFixed (x - skew + width);
208	curTrap->bottom.y = XDoubleToFixed (y + size);
209	curTrap->bottom.left = XDoubleToFixed (x + skew - width);
210	curTrap->bottom.right = XDoubleToFixed (x + skew);
211
212	skew--;
213	if (skew < 0) skew = size;
214
215	y += size;
216	rows++;
217	if (y + size > HEIGHT || rows == MAXROWS) {
218	    rows = 0;
219	    y = 0;
220	    x += 2 * size;
221	    if (x + size > WIDTH) {
222		x = size;
223	    }
224	}
225    }
226
227    SetFillStyle(xp, p);
228    return reps;
229}
230
231void
232DoFixedTraps(XParms xp, Parms p, int64_t reps)
233{
234    Picture	white, black, src, dst;
235
236    white = XftDrawSrcPicture (aadraw, &aawhite);
237    black = XftDrawSrcPicture (aadraw, &aablack);
238    dst = XftDrawPicture (aadraw);
239
240    src = black;
241    for (int i = 0; i != reps; i++) {
242	XRenderFillRectangle (xp->d, PictOpSrc, mask, &transparent,
243			      0, 0, WIDTH, HEIGHT);
244	XRenderAddTraps (xp->d, mask, 0, 0, traps, p->objects);
245	XRenderComposite (xp->d, PictOpOver, src, mask, dst,
246			  0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
247        if (src == black)
248	    src = white;
249        else
250            src = black;
251	CheckAbort ();
252    }
253}
254
255void
256EndFixedTraps (XParms xp, Parms p)
257{
258    free (traps);
259    XftDrawDestroy (aadraw);
260    XRenderFreePicture (xp->d, mask);
261    XFreePixmap (xp->d, maskPixmap);
262}
263
264int
265InitFixedTrapezoids(XParms xp, Parms p, int64_t reps)
266{
267    int     numTraps;
268    int     rows;
269    int     x, y;
270    int     size, skew;
271    XTrapezoid	*curTrap;
272    XRenderColor	color;
273
274    pgc = xp->fggc;
275
276    size = p->special;
277    numTraps = p->objects;
278    trapezoids = malloc(numTraps * sizeof(XTrapezoid));
279    curTrap = trapezoids;
280    x = size;
281    y = 0;
282    rows = 0;
283    skew = size;
284    aadraw = XftDrawCreate (xp->d, xp->w,
285			    xp->vinfo.visual,
286			    xp->cmap);
287    if (p->font && !strcmp (p->font, "add"))
288    {
289	XRenderPictFormat   templ;
290	templ.type = PictTypeDirect;
291	templ.depth = 8;
292	templ.direct.alpha = 0;
293	templ.direct.alphaMask = 0xff;
294	maskFormat = XRenderFindFormat (xp->d,
295					PictFormatType |
296					PictFormatDepth |
297					PictFormatAlpha |
298					PictFormatAlphaMask,
299					&templ,
300					0);
301    }
302    else
303	maskFormat = NULL;
304    color.red = 0;
305    color.green = 0;
306    color.blue = 0;
307    color.alpha = 0xffff;
308    if (!XftColorAllocValue (xp->d,
309			     xp->vinfo.visual,
310			     xp->cmap,
311			     &color, &aablack))
312    {
313	XftDrawDestroy (aadraw);
314	aadraw = NULL;
315	return 0;
316    }
317    color.red = 0xffff;
318    color.green = 0xffff;
319    color.blue = 0xffff;
320    color.alpha = 0xffff;
321    if (!XftColorAllocValue (xp->d,
322			     xp->vinfo.visual,
323			     xp->cmap,
324			     &color, &aawhite))
325    {
326	XftDrawDestroy (aadraw);
327	aadraw = NULL;
328	return 0;
329    }
330
331    for (int i = 0; i != p->objects; i++, curTrap++) {
332	curTrap->top = XDoubleToFixed (y);
333	curTrap->bottom = XDoubleToFixed (y + size);
334	curTrap->left.p1.x = XDoubleToFixed (x - skew);
335	curTrap->left.p1.y = XDoubleToFixed (y);
336	curTrap->left.p2.x = XDoubleToFixed (x + skew - size);
337	curTrap->left.p2.y = XDoubleToFixed (y + size);
338
339	curTrap->right.p1.x = XDoubleToFixed (x - skew + size);
340	curTrap->right.p1.y = XDoubleToFixed (y);
341	curTrap->right.p2.x = XDoubleToFixed (x + skew);
342	curTrap->right.p2.y = XDoubleToFixed (y + size);
343
344	skew--;
345	if (skew < 0) skew = size;
346
347	y += size;
348	rows++;
349	if (y + size > HEIGHT || rows == MAXROWS) {
350	    rows = 0;
351	    y = 0;
352	    x += 2 * size;
353	    if (x + size > WIDTH) {
354		x = size;
355	    }
356	}
357    }
358
359
360    SetFillStyle(xp, p);
361    return reps;
362}
363
364void
365DoFixedTrapezoids(XParms xp, Parms p, int64_t reps)
366{
367    Picture	white, black, src, dst;
368
369    white = XftDrawSrcPicture (aadraw, &aawhite);
370    black = XftDrawSrcPicture (aadraw, &aablack);
371    dst = XftDrawPicture (aadraw);
372
373    src = black;
374    for (int i = 0; i != reps; i++) {
375	XRenderCompositeTrapezoids (xp->d, PictOpOver, src, dst, maskFormat,
376				    0, 0, trapezoids, p->objects);
377        if (src == black)
378	    src = white;
379        else
380            src = black;
381	CheckAbort ();
382    }
383}
384
385void
386EndFixedTrapezoids (XParms xp, Parms p)
387{
388    free (trapezoids);
389    XftDrawDestroy (aadraw);
390}
391
392#endif /* XRENDER */
393