1/*
2 * Copyright © 2004 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  Keith Packard makes no
11 * representations about the suitability of this software for any purpose.  It
12 * is provided "as is" without express or implied warranty.
13 *
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#ifdef HAVE_DIX_CONFIG_H
24#include <dix-config.h>
25#endif
26
27#include "fb.h"
28
29#include "picturestr.h"
30#include "mipict.h"
31#include "fbpict.h"
32
33void
34fbAddTraps (PicturePtr	pPicture,
35	    INT16	x_off,
36	    INT16	y_off,
37	    int		ntrap,
38	    xTrap	*traps)
39{
40    int image_xoff, image_yoff;
41    pixman_image_t *image = image_from_pict (pPicture, FALSE, &image_xoff, &image_yoff);
42
43    if (!image)
44	return;
45
46    pixman_add_traps (image, x_off, y_off, ntrap, (pixman_trap_t *)traps);
47
48    free_pixman_pict (pPicture, image);
49}
50
51void
52fbRasterizeTrapezoid (PicturePtr    pPicture,
53		      xTrapezoid  *trap,
54		      int	    x_off,
55		      int	    y_off)
56{
57    int	mask_xoff, mask_yoff;
58    pixman_image_t *image = image_from_pict (pPicture, FALSE, &mask_xoff, &mask_yoff);
59
60    if (!image)
61	return;
62
63    pixman_rasterize_trapezoid (image, (pixman_trapezoid_t *)trap, x_off, y_off);
64
65    free_pixman_pict (pPicture, image);
66}
67
68static int
69_GreaterY (xPointFixed *a, xPointFixed *b)
70{
71    if (a->y == b->y)
72	return a->x > b->x;
73    return a->y > b->y;
74}
75
76/*
77 * Note that the definition of this function is a bit odd because
78 * of the X coordinate space (y increasing downwards).
79 */
80static int
81_Clockwise (xPointFixed *ref, xPointFixed *a, xPointFixed *b)
82{
83    xPointFixed	ad, bd;
84
85    ad.x = a->x - ref->x;
86    ad.y = a->y - ref->y;
87    bd.x = b->x - ref->x;
88    bd.y = b->y - ref->y;
89
90    return ((xFixed_32_32) bd.y * ad.x - (xFixed_32_32) ad.y * bd.x) < 0;
91}
92
93/* FIXME -- this could be made more efficient */
94void
95fbAddTriangles (PicturePtr  pPicture,
96		INT16	    x_off,
97		INT16	    y_off,
98		int	    ntri,
99		xTriangle *tris)
100{
101    xPointFixed	  *top, *left, *right, *tmp;
102    xTrapezoid	    trap;
103
104    for (; ntri; ntri--, tris++)
105    {
106	top = &tris->p1;
107	left = &tris->p2;
108	right = &tris->p3;
109	if (_GreaterY (top, left)) {
110	    tmp = left; left = top; top = tmp;
111	}
112	if (_GreaterY (top, right)) {
113	    tmp = right; right = top; top = tmp;
114	}
115	if (_Clockwise (top, right, left)) {
116	    tmp = right; right = left; left = tmp;
117	}
118
119	/*
120	 * Two cases:
121	 *
122	 *		+		+
123	 *	       / \             / \
124	 *	      /   \           /   \
125	 *	     /     +         +     \
126	 *      /    --           --    \
127	 *     /   --               --   \
128	 *    / ---                   --- \
129	 *	 +--                         --+
130	 */
131
132	trap.top = top->y;
133	trap.left.p1 = *top;
134	trap.left.p2 = *left;
135	trap.right.p1 = *top;
136	trap.right.p2 = *right;
137	if (right->y < left->y)
138	    trap.bottom = right->y;
139	else
140	    trap.bottom = left->y;
141	fbRasterizeTrapezoid (pPicture, &trap, x_off, y_off);
142	if (right->y < left->y)
143	{
144	    trap.top = right->y;
145	    trap.bottom = left->y;
146	    trap.right.p1 = *right;
147	    trap.right.p2 = *left;
148	}
149	else
150	{
151	    trap.top = left->y;
152	    trap.bottom = right->y;
153	    trap.left.p1 = *left;
154	    trap.left.p2 = *right;
155	}
156	fbRasterizeTrapezoid (pPicture, &trap, x_off, y_off);
157    }
158}
159
160