fbtrap.c revision 6747b715
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 "renderedge.h" 32#include "fbpict.h" 33 34void 35fbAddTraps (PicturePtr pPicture, 36 INT16 x_off, 37 INT16 y_off, 38 int ntrap, 39 xTrap *traps) 40{ 41 int image_xoff, image_yoff; 42 pixman_image_t *image = image_from_pict (pPicture, FALSE, &image_xoff, &image_yoff); 43 44 if (!image) 45 return; 46 47 pixman_add_traps (image, x_off, y_off, ntrap, (pixman_trap_t *)traps); 48 49 free_pixman_pict (pPicture, image); 50} 51 52void 53fbRasterizeTrapezoid (PicturePtr pPicture, 54 xTrapezoid *trap, 55 int x_off, 56 int y_off) 57{ 58 int mask_xoff, mask_yoff; 59 pixman_image_t *image = image_from_pict (pPicture, FALSE, &mask_xoff, &mask_yoff); 60 61 if (!image) 62 return; 63 64 pixman_rasterize_trapezoid (image, (pixman_trapezoid_t *)trap, x_off, y_off); 65 66 free_pixman_pict (pPicture, image); 67} 68 69static int 70_GreaterY (xPointFixed *a, xPointFixed *b) 71{ 72 if (a->y == b->y) 73 return a->x > b->x; 74 return a->y > b->y; 75} 76 77/* 78 * Note that the definition of this function is a bit odd because 79 * of the X coordinate space (y increasing downwards). 80 */ 81static int 82_Clockwise (xPointFixed *ref, xPointFixed *a, xPointFixed *b) 83{ 84 xPointFixed ad, bd; 85 86 ad.x = a->x - ref->x; 87 ad.y = a->y - ref->y; 88 bd.x = b->x - ref->x; 89 bd.y = b->y - ref->y; 90 91 return ((xFixed_32_32) bd.y * ad.x - (xFixed_32_32) ad.y * bd.x) < 0; 92} 93 94/* FIXME -- this could be made more efficient */ 95void 96fbAddTriangles (PicturePtr pPicture, 97 INT16 x_off, 98 INT16 y_off, 99 int ntri, 100 xTriangle *tris) 101{ 102 xPointFixed *top, *left, *right, *tmp; 103 xTrapezoid trap; 104 105 for (; ntri; ntri--, tris++) 106 { 107 top = &tris->p1; 108 left = &tris->p2; 109 right = &tris->p3; 110 if (_GreaterY (top, left)) { 111 tmp = left; left = top; top = tmp; 112 } 113 if (_GreaterY (top, right)) { 114 tmp = right; right = top; top = tmp; 115 } 116 if (_Clockwise (top, right, left)) { 117 tmp = right; right = left; left = tmp; 118 } 119 120 /* 121 * Two cases: 122 * 123 * + + 124 * / \ / \ 125 * / \ / \ 126 * / + + \ 127 * / -- -- \ 128 * / -- -- \ 129 * / --- --- \ 130 * +-- --+ 131 */ 132 133 trap.top = top->y; 134 trap.left.p1 = *top; 135 trap.left.p2 = *left; 136 trap.right.p1 = *top; 137 trap.right.p2 = *right; 138 if (right->y < left->y) 139 trap.bottom = right->y; 140 else 141 trap.bottom = left->y; 142 fbRasterizeTrapezoid (pPicture, &trap, x_off, y_off); 143 if (right->y < left->y) 144 { 145 trap.top = right->y; 146 trap.bottom = left->y; 147 trap.right.p1 = *right; 148 trap.right.p2 = *left; 149 } 150 else 151 { 152 trap.top = left->y; 153 trap.bottom = right->y; 154 trap.left.p1 = *left; 155 trap.left.p2 = *right; 156 } 157 fbRasterizeTrapezoid (pPicture, &trap, x_off, y_off); 158 } 159} 160 161