Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright © 2004 Keith Packard |
michael@0 | 3 | * |
michael@0 | 4 | * Permission to use, copy, modify, distribute, and sell this software and its |
michael@0 | 5 | * documentation for any purpose is hereby granted without fee, provided that |
michael@0 | 6 | * the above copyright notice appear in all copies and that both that |
michael@0 | 7 | * copyright notice and this permission notice appear in supporting |
michael@0 | 8 | * documentation, and that the name of Keith Packard not be used in |
michael@0 | 9 | * advertising or publicity pertaining to distribution of the software without |
michael@0 | 10 | * specific, written prior permission. Keith Packard makes no |
michael@0 | 11 | * representations about the suitability of this software for any purpose. It |
michael@0 | 12 | * is provided "as is" without express or implied warranty. |
michael@0 | 13 | * |
michael@0 | 14 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
michael@0 | 15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
michael@0 | 16 | * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
michael@0 | 17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
michael@0 | 18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
michael@0 | 19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
michael@0 | 20 | * PERFORMANCE OF THIS SOFTWARE. |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | #ifdef HAVE_CONFIG_H |
michael@0 | 24 | #include <config.h> |
michael@0 | 25 | #endif |
michael@0 | 26 | |
michael@0 | 27 | #include <string.h> |
michael@0 | 28 | |
michael@0 | 29 | #include "pixman-private.h" |
michael@0 | 30 | #include "pixman-accessor.h" |
michael@0 | 31 | |
michael@0 | 32 | /* |
michael@0 | 33 | * Step across a small sample grid gap |
michael@0 | 34 | */ |
michael@0 | 35 | #define RENDER_EDGE_STEP_SMALL(edge) \ |
michael@0 | 36 | { \ |
michael@0 | 37 | edge->x += edge->stepx_small; \ |
michael@0 | 38 | edge->e += edge->dx_small; \ |
michael@0 | 39 | if (edge->e > 0) \ |
michael@0 | 40 | { \ |
michael@0 | 41 | edge->e -= edge->dy; \ |
michael@0 | 42 | edge->x += edge->signdx; \ |
michael@0 | 43 | } \ |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | /* |
michael@0 | 47 | * Step across a large sample grid gap |
michael@0 | 48 | */ |
michael@0 | 49 | #define RENDER_EDGE_STEP_BIG(edge) \ |
michael@0 | 50 | { \ |
michael@0 | 51 | edge->x += edge->stepx_big; \ |
michael@0 | 52 | edge->e += edge->dx_big; \ |
michael@0 | 53 | if (edge->e > 0) \ |
michael@0 | 54 | { \ |
michael@0 | 55 | edge->e -= edge->dy; \ |
michael@0 | 56 | edge->x += edge->signdx; \ |
michael@0 | 57 | } \ |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | #ifdef PIXMAN_FB_ACCESSORS |
michael@0 | 61 | #define PIXMAN_RASTERIZE_EDGES pixman_rasterize_edges_accessors |
michael@0 | 62 | #else |
michael@0 | 63 | #define PIXMAN_RASTERIZE_EDGES pixman_rasterize_edges_no_accessors |
michael@0 | 64 | #endif |
michael@0 | 65 | |
michael@0 | 66 | /* |
michael@0 | 67 | * 4 bit alpha |
michael@0 | 68 | */ |
michael@0 | 69 | |
michael@0 | 70 | #define N_BITS 4 |
michael@0 | 71 | #define RASTERIZE_EDGES rasterize_edges_4 |
michael@0 | 72 | |
michael@0 | 73 | #ifndef WORDS_BIGENDIAN |
michael@0 | 74 | #define SHIFT_4(o) ((o) << 2) |
michael@0 | 75 | #else |
michael@0 | 76 | #define SHIFT_4(o) ((1 - (o)) << 2) |
michael@0 | 77 | #endif |
michael@0 | 78 | |
michael@0 | 79 | #define GET_4(x, o) (((x) >> SHIFT_4 (o)) & 0xf) |
michael@0 | 80 | #define PUT_4(x, o, v) \ |
michael@0 | 81 | (((x) & ~(0xf << SHIFT_4 (o))) | (((v) & 0xf) << SHIFT_4 (o))) |
michael@0 | 82 | |
michael@0 | 83 | #define DEFINE_ALPHA(line, x) \ |
michael@0 | 84 | uint8_t *__ap = (uint8_t *) line + ((x) >> 1); \ |
michael@0 | 85 | int __ao = (x) & 1 |
michael@0 | 86 | |
michael@0 | 87 | #define STEP_ALPHA ((__ap += __ao), (__ao ^= 1)) |
michael@0 | 88 | |
michael@0 | 89 | #define ADD_ALPHA(a) \ |
michael@0 | 90 | { \ |
michael@0 | 91 | uint8_t __o = READ (image, __ap); \ |
michael@0 | 92 | uint8_t __a = (a) + GET_4 (__o, __ao); \ |
michael@0 | 93 | WRITE (image, __ap, PUT_4 (__o, __ao, __a | (0 - ((__a) >> 4)))); \ |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | #include "pixman-edge-imp.h" |
michael@0 | 97 | |
michael@0 | 98 | #undef ADD_ALPHA |
michael@0 | 99 | #undef STEP_ALPHA |
michael@0 | 100 | #undef DEFINE_ALPHA |
michael@0 | 101 | #undef RASTERIZE_EDGES |
michael@0 | 102 | #undef N_BITS |
michael@0 | 103 | |
michael@0 | 104 | |
michael@0 | 105 | /* |
michael@0 | 106 | * 1 bit alpha |
michael@0 | 107 | */ |
michael@0 | 108 | |
michael@0 | 109 | #define N_BITS 1 |
michael@0 | 110 | #define RASTERIZE_EDGES rasterize_edges_1 |
michael@0 | 111 | |
michael@0 | 112 | #include "pixman-edge-imp.h" |
michael@0 | 113 | |
michael@0 | 114 | #undef RASTERIZE_EDGES |
michael@0 | 115 | #undef N_BITS |
michael@0 | 116 | |
michael@0 | 117 | /* |
michael@0 | 118 | * 8 bit alpha |
michael@0 | 119 | */ |
michael@0 | 120 | |
michael@0 | 121 | static force_inline uint8_t |
michael@0 | 122 | clip255 (int x) |
michael@0 | 123 | { |
michael@0 | 124 | if (x > 255) |
michael@0 | 125 | return 255; |
michael@0 | 126 | |
michael@0 | 127 | return x; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | #define ADD_SATURATE_8(buf, val, length) \ |
michael@0 | 131 | do \ |
michael@0 | 132 | { \ |
michael@0 | 133 | int i__ = (length); \ |
michael@0 | 134 | uint8_t *buf__ = (buf); \ |
michael@0 | 135 | int val__ = (val); \ |
michael@0 | 136 | \ |
michael@0 | 137 | while (i__--) \ |
michael@0 | 138 | { \ |
michael@0 | 139 | WRITE (image, (buf__), clip255 (READ (image, (buf__)) + (val__))); \ |
michael@0 | 140 | (buf__)++; \ |
michael@0 | 141 | } \ |
michael@0 | 142 | } while (0) |
michael@0 | 143 | |
michael@0 | 144 | /* |
michael@0 | 145 | * We want to detect the case where we add the same value to a long |
michael@0 | 146 | * span of pixels. The triangles on the end are filled in while we |
michael@0 | 147 | * count how many sub-pixel scanlines contribute to the middle section. |
michael@0 | 148 | * |
michael@0 | 149 | * +--------------------------+ |
michael@0 | 150 | * fill_height =| \ / |
michael@0 | 151 | * +------------------+ |
michael@0 | 152 | * |================| |
michael@0 | 153 | * fill_start fill_end |
michael@0 | 154 | */ |
michael@0 | 155 | static void |
michael@0 | 156 | rasterize_edges_8 (pixman_image_t *image, |
michael@0 | 157 | pixman_edge_t * l, |
michael@0 | 158 | pixman_edge_t * r, |
michael@0 | 159 | pixman_fixed_t t, |
michael@0 | 160 | pixman_fixed_t b) |
michael@0 | 161 | { |
michael@0 | 162 | pixman_fixed_t y = t; |
michael@0 | 163 | uint32_t *line; |
michael@0 | 164 | int fill_start = -1, fill_end = -1; |
michael@0 | 165 | int fill_size = 0; |
michael@0 | 166 | uint32_t *buf = (image)->bits.bits; |
michael@0 | 167 | int stride = (image)->bits.rowstride; |
michael@0 | 168 | int width = (image)->bits.width; |
michael@0 | 169 | |
michael@0 | 170 | line = buf + pixman_fixed_to_int (y) * stride; |
michael@0 | 171 | |
michael@0 | 172 | for (;;) |
michael@0 | 173 | { |
michael@0 | 174 | uint8_t *ap = (uint8_t *) line; |
michael@0 | 175 | pixman_fixed_t lx, rx; |
michael@0 | 176 | int lxi, rxi; |
michael@0 | 177 | |
michael@0 | 178 | /* clip X */ |
michael@0 | 179 | lx = l->x; |
michael@0 | 180 | if (lx < 0) |
michael@0 | 181 | lx = 0; |
michael@0 | 182 | |
michael@0 | 183 | rx = r->x; |
michael@0 | 184 | |
michael@0 | 185 | if (pixman_fixed_to_int (rx) >= width) |
michael@0 | 186 | { |
michael@0 | 187 | /* Use the last pixel of the scanline, covered 100%. |
michael@0 | 188 | * We can't use the first pixel following the scanline, |
michael@0 | 189 | * because accessing it could result in a buffer overrun. |
michael@0 | 190 | */ |
michael@0 | 191 | rx = pixman_int_to_fixed (width) - 1; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | /* Skip empty (or backwards) sections */ |
michael@0 | 195 | if (rx > lx) |
michael@0 | 196 | { |
michael@0 | 197 | int lxs, rxs; |
michael@0 | 198 | |
michael@0 | 199 | /* Find pixel bounds for span. */ |
michael@0 | 200 | lxi = pixman_fixed_to_int (lx); |
michael@0 | 201 | rxi = pixman_fixed_to_int (rx); |
michael@0 | 202 | |
michael@0 | 203 | /* Sample coverage for edge pixels */ |
michael@0 | 204 | lxs = RENDER_SAMPLES_X (lx, 8); |
michael@0 | 205 | rxs = RENDER_SAMPLES_X (rx, 8); |
michael@0 | 206 | |
michael@0 | 207 | /* Add coverage across row */ |
michael@0 | 208 | if (lxi == rxi) |
michael@0 | 209 | { |
michael@0 | 210 | WRITE (image, ap + lxi, |
michael@0 | 211 | clip255 (READ (image, ap + lxi) + rxs - lxs)); |
michael@0 | 212 | } |
michael@0 | 213 | else |
michael@0 | 214 | { |
michael@0 | 215 | WRITE (image, ap + lxi, |
michael@0 | 216 | clip255 (READ (image, ap + lxi) + N_X_FRAC (8) - lxs)); |
michael@0 | 217 | |
michael@0 | 218 | /* Move forward so that lxi/rxi is the pixel span */ |
michael@0 | 219 | lxi++; |
michael@0 | 220 | |
michael@0 | 221 | /* Don't bother trying to optimize the fill unless |
michael@0 | 222 | * the span is longer than 4 pixels. */ |
michael@0 | 223 | if (rxi - lxi > 4) |
michael@0 | 224 | { |
michael@0 | 225 | if (fill_start < 0) |
michael@0 | 226 | { |
michael@0 | 227 | fill_start = lxi; |
michael@0 | 228 | fill_end = rxi; |
michael@0 | 229 | fill_size++; |
michael@0 | 230 | } |
michael@0 | 231 | else |
michael@0 | 232 | { |
michael@0 | 233 | if (lxi >= fill_end || rxi < fill_start) |
michael@0 | 234 | { |
michael@0 | 235 | /* We're beyond what we saved, just fill it */ |
michael@0 | 236 | ADD_SATURATE_8 (ap + fill_start, |
michael@0 | 237 | fill_size * N_X_FRAC (8), |
michael@0 | 238 | fill_end - fill_start); |
michael@0 | 239 | fill_start = lxi; |
michael@0 | 240 | fill_end = rxi; |
michael@0 | 241 | fill_size = 1; |
michael@0 | 242 | } |
michael@0 | 243 | else |
michael@0 | 244 | { |
michael@0 | 245 | /* Update fill_start */ |
michael@0 | 246 | if (lxi > fill_start) |
michael@0 | 247 | { |
michael@0 | 248 | ADD_SATURATE_8 (ap + fill_start, |
michael@0 | 249 | fill_size * N_X_FRAC (8), |
michael@0 | 250 | lxi - fill_start); |
michael@0 | 251 | fill_start = lxi; |
michael@0 | 252 | } |
michael@0 | 253 | else if (lxi < fill_start) |
michael@0 | 254 | { |
michael@0 | 255 | ADD_SATURATE_8 (ap + lxi, N_X_FRAC (8), |
michael@0 | 256 | fill_start - lxi); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | /* Update fill_end */ |
michael@0 | 260 | if (rxi < fill_end) |
michael@0 | 261 | { |
michael@0 | 262 | ADD_SATURATE_8 (ap + rxi, |
michael@0 | 263 | fill_size * N_X_FRAC (8), |
michael@0 | 264 | fill_end - rxi); |
michael@0 | 265 | fill_end = rxi; |
michael@0 | 266 | } |
michael@0 | 267 | else if (fill_end < rxi) |
michael@0 | 268 | { |
michael@0 | 269 | ADD_SATURATE_8 (ap + fill_end, |
michael@0 | 270 | N_X_FRAC (8), |
michael@0 | 271 | rxi - fill_end); |
michael@0 | 272 | } |
michael@0 | 273 | fill_size++; |
michael@0 | 274 | } |
michael@0 | 275 | } |
michael@0 | 276 | } |
michael@0 | 277 | else |
michael@0 | 278 | { |
michael@0 | 279 | ADD_SATURATE_8 (ap + lxi, N_X_FRAC (8), rxi - lxi); |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | WRITE (image, ap + rxi, clip255 (READ (image, ap + rxi) + rxs)); |
michael@0 | 283 | } |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | if (y == b) |
michael@0 | 287 | { |
michael@0 | 288 | /* We're done, make sure we clean up any remaining fill. */ |
michael@0 | 289 | if (fill_start != fill_end) |
michael@0 | 290 | { |
michael@0 | 291 | if (fill_size == N_Y_FRAC (8)) |
michael@0 | 292 | { |
michael@0 | 293 | MEMSET_WRAPPED (image, ap + fill_start, |
michael@0 | 294 | 0xff, fill_end - fill_start); |
michael@0 | 295 | } |
michael@0 | 296 | else |
michael@0 | 297 | { |
michael@0 | 298 | ADD_SATURATE_8 (ap + fill_start, fill_size * N_X_FRAC (8), |
michael@0 | 299 | fill_end - fill_start); |
michael@0 | 300 | } |
michael@0 | 301 | } |
michael@0 | 302 | break; |
michael@0 | 303 | } |
michael@0 | 304 | |
michael@0 | 305 | if (pixman_fixed_frac (y) != Y_FRAC_LAST (8)) |
michael@0 | 306 | { |
michael@0 | 307 | RENDER_EDGE_STEP_SMALL (l); |
michael@0 | 308 | RENDER_EDGE_STEP_SMALL (r); |
michael@0 | 309 | y += STEP_Y_SMALL (8); |
michael@0 | 310 | } |
michael@0 | 311 | else |
michael@0 | 312 | { |
michael@0 | 313 | RENDER_EDGE_STEP_BIG (l); |
michael@0 | 314 | RENDER_EDGE_STEP_BIG (r); |
michael@0 | 315 | y += STEP_Y_BIG (8); |
michael@0 | 316 | if (fill_start != fill_end) |
michael@0 | 317 | { |
michael@0 | 318 | if (fill_size == N_Y_FRAC (8)) |
michael@0 | 319 | { |
michael@0 | 320 | MEMSET_WRAPPED (image, ap + fill_start, |
michael@0 | 321 | 0xff, fill_end - fill_start); |
michael@0 | 322 | } |
michael@0 | 323 | else |
michael@0 | 324 | { |
michael@0 | 325 | ADD_SATURATE_8 (ap + fill_start, fill_size * N_X_FRAC (8), |
michael@0 | 326 | fill_end - fill_start); |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | fill_start = fill_end = -1; |
michael@0 | 330 | fill_size = 0; |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | line += stride; |
michael@0 | 334 | } |
michael@0 | 335 | } |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | #ifndef PIXMAN_FB_ACCESSORS |
michael@0 | 339 | static |
michael@0 | 340 | #endif |
michael@0 | 341 | void |
michael@0 | 342 | PIXMAN_RASTERIZE_EDGES (pixman_image_t *image, |
michael@0 | 343 | pixman_edge_t * l, |
michael@0 | 344 | pixman_edge_t * r, |
michael@0 | 345 | pixman_fixed_t t, |
michael@0 | 346 | pixman_fixed_t b) |
michael@0 | 347 | { |
michael@0 | 348 | switch (PIXMAN_FORMAT_BPP (image->bits.format)) |
michael@0 | 349 | { |
michael@0 | 350 | case 1: |
michael@0 | 351 | rasterize_edges_1 (image, l, r, t, b); |
michael@0 | 352 | break; |
michael@0 | 353 | |
michael@0 | 354 | case 4: |
michael@0 | 355 | rasterize_edges_4 (image, l, r, t, b); |
michael@0 | 356 | break; |
michael@0 | 357 | |
michael@0 | 358 | case 8: |
michael@0 | 359 | rasterize_edges_8 (image, l, r, t, b); |
michael@0 | 360 | break; |
michael@0 | 361 | |
michael@0 | 362 | default: |
michael@0 | 363 | break; |
michael@0 | 364 | } |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | #ifndef PIXMAN_FB_ACCESSORS |
michael@0 | 368 | |
michael@0 | 369 | PIXMAN_EXPORT void |
michael@0 | 370 | pixman_rasterize_edges (pixman_image_t *image, |
michael@0 | 371 | pixman_edge_t * l, |
michael@0 | 372 | pixman_edge_t * r, |
michael@0 | 373 | pixman_fixed_t t, |
michael@0 | 374 | pixman_fixed_t b) |
michael@0 | 375 | { |
michael@0 | 376 | return_if_fail (image->type == BITS); |
michael@0 | 377 | return_if_fail (PIXMAN_FORMAT_TYPE (image->bits.format) == PIXMAN_TYPE_A); |
michael@0 | 378 | |
michael@0 | 379 | if (image->bits.read_func || image->bits.write_func) |
michael@0 | 380 | pixman_rasterize_edges_accessors (image, l, r, t, b); |
michael@0 | 381 | else |
michael@0 | 382 | pixman_rasterize_edges_no_accessors (image, l, r, t, b); |
michael@0 | 383 | } |
michael@0 | 384 | |
michael@0 | 385 | #endif |