Round horizontal sampling points towards northwest.
[profile/ivi/pixman.git] / pixman / pixman-edge-imp.h
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 #ifndef rasterize_span
24 #endif
25
26 static void
27 RASTERIZE_EDGES (pixman_image_t  *image,
28                 pixman_edge_t   *l,
29                 pixman_edge_t   *r,
30                 pixman_fixed_t          t,
31                 pixman_fixed_t          b)
32 {
33     pixman_fixed_t  y = t;
34     uint32_t  *line;
35     uint32_t *buf = (image)->bits.bits;
36     int stride = (image)->bits.rowstride;
37     int width = (image)->bits.width;
38
39     line = buf + pixman_fixed_to_int (y) * stride;
40
41     for (;;)
42     {
43         pixman_fixed_t  lx;
44         pixman_fixed_t      rx;
45         int     lxi;
46         int rxi;
47
48         lx = l->x;
49         rx = r->x;
50 #if N_BITS == 1
51         /* For the non-antialiased case, round the coordinates up, in effect
52          * sampling just slightly to the left of the pixel. This is so that
53          * when the sample point lies exactly on the line, we round towards
54          * north-west.
55          *
56          * (The AA case does a similar  adjustment in RENDER_SAMPLES_X)
57          */
58         lx += X_FRAC_FIRST(1) - pixman_fixed_e;
59         rx += X_FRAC_FIRST(1) - pixman_fixed_e;
60 #endif
61         /* clip X */
62         if (lx < 0)
63             lx = 0;
64         if (pixman_fixed_to_int (rx) >= width)
65 #if N_BITS == 1
66             rx = pixman_int_to_fixed (width);
67 #else
68             /* Use the last pixel of the scanline, covered 100%.
69              * We can't use the first pixel following the scanline,
70              * because accessing it could result in a buffer overrun.
71              */
72             rx = pixman_int_to_fixed (width) - 1;
73 #endif
74
75         /* Skip empty (or backwards) sections */
76         if (rx > lx)
77         {
78
79             /* Find pixel bounds for span */
80             lxi = pixman_fixed_to_int (lx);
81             rxi = pixman_fixed_to_int (rx);
82
83 #if N_BITS == 1
84             {
85
86 #ifdef WORDS_BIGENDIAN
87 #   define SCREEN_SHIFT_LEFT(x,n)       ((x) << (n))
88 #   define SCREEN_SHIFT_RIGHT(x,n)      ((x) >> (n))
89 #else
90 #   define SCREEN_SHIFT_LEFT(x,n)       ((x) >> (n))
91 #   define SCREEN_SHIFT_RIGHT(x,n)      ((x) << (n))
92 #endif
93
94 #define LEFT_MASK(x)                                                    \
95                 (((x) & 0x1f) ?                                         \
96                  SCREEN_SHIFT_RIGHT (0xffffffff, (x) & 0x1f) : 0)
97 #define RIGHT_MASK(x)                                                   \
98                 (((32 - (x)) & 0x1f) ?                                  \
99                  SCREEN_SHIFT_LEFT (0xffffffff, (32 - (x)) & 0x1f) : 0)
100                 
101 #define MASK_BITS(x,w,l,n,r) {                                          \
102                     n = (w);                                            \
103                     r = RIGHT_MASK ((x) + n);                           \
104                     l = LEFT_MASK (x);                                  \
105                     if (l) {                                            \
106                         n -= 32 - ((x) & 0x1f);                         \
107                         if (n < 0) {                                    \
108                             n = 0;                                      \
109                             l &= r;                                     \
110                             r = 0;                                      \
111                         }                                               \
112                     }                                                   \
113                     n >>= 5;                                            \
114                 }
115                 
116                 uint32_t  *a = line;
117                 uint32_t  startmask;
118                 uint32_t  endmask;
119                 int         nmiddle;
120                 int         width = rxi - lxi;
121                 int         x = lxi;
122                 
123                 a += x >> 5;
124                 x &= 0x1f;
125                 
126                 MASK_BITS (x, width, startmask, nmiddle, endmask);
127
128                 if (startmask) {
129                     WRITE(image, a, READ(image, a) | startmask);
130                     a++;
131                 }
132                 while (nmiddle--)
133                     WRITE(image, a++, 0xffffffff);
134                 if (endmask)
135                     WRITE(image, a, READ(image, a) | endmask);
136             }
137 #else
138             {
139                 DEFINE_ALPHA(line,lxi);
140                 int         lxs;
141                 int     rxs;
142
143                 /* Sample coverage for edge pixels */
144                 lxs = RENDER_SAMPLES_X (lx, N_BITS);
145                 rxs = RENDER_SAMPLES_X (rx, N_BITS);
146
147                 /* Add coverage across row */
148                 if (lxi == rxi)
149                 {
150                     ADD_ALPHA (rxs - lxs);
151                 }
152                 else
153                 {
154                     int xi;
155
156                     ADD_ALPHA (N_X_FRAC(N_BITS) - lxs);
157                     STEP_ALPHA;
158                     for (xi = lxi + 1; xi < rxi; xi++)
159                     {
160                         ADD_ALPHA (N_X_FRAC(N_BITS));
161                         STEP_ALPHA;
162                     }
163                     ADD_ALPHA (rxs);
164                 }
165             }
166 #endif
167         }
168
169         if (y == b)
170             break;
171
172 #if N_BITS > 1
173         if (pixman_fixed_frac (y) != Y_FRAC_LAST(N_BITS))
174         {
175             RENDER_EDGE_STEP_SMALL (l);
176             RENDER_EDGE_STEP_SMALL (r);
177             y += STEP_Y_SMALL(N_BITS);
178         }
179         else
180 #endif
181         {
182             RENDER_EDGE_STEP_BIG (l);
183             RENDER_EDGE_STEP_BIG (r);
184             y += STEP_Y_BIG(N_BITS);
185             line += stride;
186         }
187     }
188 }
189
190 #undef rasterize_span