blob: e5c67a958af2ef2069ff7ec1fff2a75e402a3e39 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkScanPriv.h"
11#include "SkPath.h"
12#include "SkMatrix.h"
13#include "SkBlitter.h"
14#include "SkRegion.h"
15#include "SkAntiRun.h"
16
17#define SHIFT 2
18#define SCALE (1 << SHIFT)
19#define MASK (SCALE - 1)
20
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000021/** @file
reed@google.com2f3567c2011-06-09 15:54:38 +000022 We have two techniques for capturing the output of the supersampler:
23 - SUPERMASK, which records a large mask-bitmap
24 this is often faster for small, complex objects
25 - RLE, which records a rle-encoded scanline
26 this is often faster for large objects with big spans
27
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000028 These blitters use two coordinate systems:
29 - destination coordinates, scale equal to the output - often
30 abbreviated with 'i' or 'I' in variable names
31 - supersampled coordinates, scale equal to the output * SCALE
32
reed@google.com5b7d6032012-05-09 14:07:34 +000033 Enabling SK_USE_LEGACY_AA_COVERAGE keeps the aa coverage calculations as
34 they were before the fix that unified the output of the RLE and MASK
35 supersamplers.
reed@google.com2f3567c2011-06-09 15:54:38 +000036 */
reed@google.com5b7d6032012-05-09 14:07:34 +000037
reed@google.com9f2f0a82011-04-11 19:43:58 +000038//#define FORCE_SUPERMASK
39//#define FORCE_RLE
reed@google.comcd4e9fb2012-05-09 14:33:02 +000040//#define SK_USE_LEGACY_AA_COVERAGE
reed@google.com9f2f0a82011-04-11 19:43:58 +000041
42///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000043
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000044/// Base class for a single-pass supersampled blitter.
reed@android.com8a1c16f2008-12-17 15:59:43 +000045class BaseSuperBlitter : public SkBlitter {
46public:
47 BaseSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
48 const SkRegion& clip);
49
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000050 /// Must be explicitly defined on subclasses.
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 virtual void blitAntiH(int x, int y, const SkAlpha antialias[],
reed@google.com967a35d2011-10-31 19:37:58 +000052 const int16_t runs[]) SK_OVERRIDE {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000053 SkDEBUGFAIL("How did I get here?");
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 }
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000055 /// May not be called on BaseSuperBlitter because it blits out of order.
reed@google.com967a35d2011-10-31 19:37:58 +000056 virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000057 SkDEBUGFAIL("How did I get here?");
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000059
60protected:
61 SkBlitter* fRealBlitter;
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000062 /// Current y coordinate, in destination coordinates.
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 int fCurrIY;
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000064 /// Widest row of region to be blitted, in destination coordinates.
65 int fWidth;
66 /// Leftmost x coordinate in any row, in destination coordinates.
67 int fLeft;
68 /// Leftmost x coordinate in any row, in supersampled coordinates.
69 int fSuperLeft;
reed@android.com8a1c16f2008-12-17 15:59:43 +000070
71 SkDEBUGCODE(int fCurrX;)
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000072 /// Current y coordinate in supersampled coordinates.
reed@google.comfa57ae72011-05-31 19:18:02 +000073 int fCurrY;
tomhudson@google.com05fffdc2011-12-01 20:41:24 +000074 /// Initial y coordinate (top of bounds).
reed@google.com045e62d2011-10-24 12:19:46 +000075 int fTop;
reed@android.com8a1c16f2008-12-17 15:59:43 +000076};
77
78BaseSuperBlitter::BaseSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
79 const SkRegion& clip) {
80 fRealBlitter = realBlitter;
reed@google.com55b6b582011-03-02 15:58:18 +000081
reed@google.comdceecc72012-02-23 19:20:19 +000082 /*
83 * We use the clip bounds instead of the ir, since we may be asked to
84 * draw outside of the rect if we're a inverse filltype
85 */
86 const int left = clip.getBounds().fLeft;
87 const int right = clip.getBounds().fRight;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 fLeft = left;
90 fSuperLeft = left << SHIFT;
91 fWidth = right - left;
reed@google.com045e62d2011-10-24 12:19:46 +000092#if 0
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 fCurrIY = -1;
reed@google.com34e52a02011-06-01 17:32:11 +000094 fCurrY = -1;
reed@google.com045e62d2011-10-24 12:19:46 +000095#else
96 fTop = ir.fTop;
97 fCurrIY = ir.fTop - 1;
98 fCurrY = (ir.fTop << SHIFT) - 1;
99#endif
reed@google.com34e52a02011-06-01 17:32:11 +0000100 SkDEBUGCODE(fCurrX = -1;)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101}
102
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000103/// Run-length-encoded supersampling antialiased blitter.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104class SuperBlitter : public BaseSuperBlitter {
105public:
106 SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
107 const SkRegion& clip);
108
109 virtual ~SuperBlitter() {
110 this->flush();
111 sk_free(fRuns.fRuns);
112 }
113
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000114 /// Once fRuns contains a complete supersampled row, flush() blits
115 /// it out through the wrapped blitter.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 void flush();
117
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000118 /// Blits a row of pixels, with location and width specified
119 /// in supersampled coordinates.
reed@google.com967a35d2011-10-31 19:37:58 +0000120 virtual void blitH(int x, int y, int width) SK_OVERRIDE;
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000121 /// Blits a rectangle of pixels, with location and size specified
122 /// in supersampled coordinates.
reed@google.com967a35d2011-10-31 19:37:58 +0000123 virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
125private:
126 SkAlphaRuns fRuns;
reed@google.comfa57ae72011-05-31 19:18:02 +0000127 int fOffsetX;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128};
129
130SuperBlitter::SuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
131 const SkRegion& clip)
132 : BaseSuperBlitter(realBlitter, ir, clip) {
133 const int width = fWidth;
134
135 // extra one to store the zero at the end
136 fRuns.fRuns = (int16_t*)sk_malloc_throw((width + 1 + (width + 2)/2) * sizeof(int16_t));
137 fRuns.fAlpha = (uint8_t*)(fRuns.fRuns + width + 1);
138 fRuns.reset(width);
reed@google.comfa57ae72011-05-31 19:18:02 +0000139
140 fOffsetX = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141}
142
reed@google.com9f2f0a82011-04-11 19:43:58 +0000143void SuperBlitter::flush() {
reed@google.com045e62d2011-10-24 12:19:46 +0000144 if (fCurrIY >= fTop) {
reed@google.com9f2f0a82011-04-11 19:43:58 +0000145 if (!fRuns.empty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 // SkDEBUGCODE(fRuns.dump();)
147 fRealBlitter->blitAntiH(fLeft, fCurrIY, fRuns.fAlpha, fRuns.fRuns);
148 fRuns.reset(fWidth);
reed@google.comfa57ae72011-05-31 19:18:02 +0000149 fOffsetX = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 }
reed@google.com045e62d2011-10-24 12:19:46 +0000151 fCurrIY = fTop - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 SkDEBUGCODE(fCurrX = -1;)
153 }
154}
155
tomhudson@google.comffc92482012-02-23 19:34:34 +0000156/** coverage_to_partial_alpha() is being used by SkAlphaRuns, which
157 *accumulates* SCALE pixels worth of "alpha" in [0,(256/SCALE)]
158 to produce a final value in [0, 255] and handles clamping 256->255
159 itself, with the same (alpha - (alpha >> 8)) correction as
160 coverage_to_exact_alpha().
161*/
162static inline int coverage_to_partial_alpha(int aa) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000163 aa <<= 8 - 2*SHIFT;
reed@google.com5b7d6032012-05-09 14:07:34 +0000164#ifdef SK_USE_LEGACY_AA_COVERAGE
reed@android.com8a1c16f2008-12-17 15:59:43 +0000165 aa -= aa >> (8 - SHIFT - 1);
tomhudson@google.comffc92482012-02-23 19:34:34 +0000166#endif
reed@google.com5b7d6032012-05-09 14:07:34 +0000167 return aa;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168}
169
tomhudson@google.comffc92482012-02-23 19:34:34 +0000170/** coverage_to_exact_alpha() is being used by our blitter, which wants
171 a final value in [0, 255].
172*/
tomhudson@google.com49eac192011-12-27 13:59:20 +0000173static inline int coverage_to_exact_alpha(int aa) {
tomhudson@google.com31bab392012-01-03 20:12:42 +0000174 int alpha = (256 >> SHIFT) * aa;
175 // clamp 256->255
176 return alpha - (alpha >> 8);
tomhudson@google.com49eac192011-12-27 13:59:20 +0000177}
178
reed@google.com9f2f0a82011-04-11 19:43:58 +0000179void SuperBlitter::blitH(int x, int y, int width) {
reed@google.com967a35d2011-10-31 19:37:58 +0000180 SkASSERT(width > 0);
181
reed@android.com8a1c16f2008-12-17 15:59:43 +0000182 int iy = y >> SHIFT;
183 SkASSERT(iy >= fCurrIY);
184
185 x -= fSuperLeft;
186 // hack, until I figure out why my cubics (I think) go beyond the bounds
reed@google.com9f2f0a82011-04-11 19:43:58 +0000187 if (x < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 width += x;
189 x = 0;
190 }
191
192#ifdef SK_DEBUG
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193 SkASSERT(y != fCurrY || x >= fCurrX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194#endif
reed@google.comfa57ae72011-05-31 19:18:02 +0000195 SkASSERT(y >= fCurrY);
196 if (fCurrY != y) {
197 fOffsetX = 0;
198 fCurrY = y;
199 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000200
reed@google.com9f2f0a82011-04-11 19:43:58 +0000201 if (iy != fCurrIY) { // new scanline
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 this->flush();
203 fCurrIY = iy;
204 }
205
reed@google.com5a1e7952011-05-31 14:36:21 +0000206 int start = x;
207 int stop = x + width;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208
reed@google.com5a1e7952011-05-31 14:36:21 +0000209 SkASSERT(start >= 0 && stop > start);
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000210 // integer-pixel-aligned ends of blit, rounded out
211 int fb = start & MASK;
212 int fe = stop & MASK;
reed@google.com5a1e7952011-05-31 14:36:21 +0000213 int n = (stop >> SHIFT) - (start >> SHIFT) - 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000214
reed@google.com5a1e7952011-05-31 14:36:21 +0000215 if (n < 0) {
216 fb = fe - fb;
217 n = 0;
218 fe = 0;
219 } else {
220 if (fb == 0) {
221 n += 1;
reed@google.com9f2f0a82011-04-11 19:43:58 +0000222 } else {
tomhudson@google.com49eac192011-12-27 13:59:20 +0000223 fb = SCALE - fb;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000224 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000225 }
reed@google.comfa57ae72011-05-31 19:18:02 +0000226
tomhudson@google.comffc92482012-02-23 19:34:34 +0000227 fOffsetX = fRuns.add(x >> SHIFT, coverage_to_partial_alpha(fb),
228 n, coverage_to_partial_alpha(fe),
reed@google.comfa57ae72011-05-31 19:18:02 +0000229 (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT),
230 fOffsetX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231
232#ifdef SK_DEBUG
233 fRuns.assertValid(y & MASK, (1 << (8 - SHIFT)));
234 fCurrX = x + width;
235#endif
236}
237
caryclark@google.com803eceb2012-06-06 12:09:34 +0000238#if 0 // UNUSED
reed@google.coma89c77b2011-12-01 21:47:26 +0000239static void set_left_rite_runs(SkAlphaRuns& runs, int ileft, U8CPU leftA,
240 int n, U8CPU riteA) {
241 SkASSERT(leftA <= 0xFF);
242 SkASSERT(riteA <= 0xFF);
243
244 int16_t* run = runs.fRuns;
245 uint8_t* aa = runs.fAlpha;
246
247 if (ileft > 0) {
248 run[0] = ileft;
249 aa[0] = 0;
250 run += ileft;
251 aa += ileft;
252 }
253
254 SkASSERT(leftA < 0xFF);
255 if (leftA > 0) {
256 *run++ = 1;
257 *aa++ = leftA;
258 }
259
260 if (n > 0) {
261 run[0] = n;
262 aa[0] = 0xFF;
263 run += n;
264 aa += n;
265 }
266
267 SkASSERT(riteA < 0xFF);
268 if (riteA > 0) {
269 *run++ = 1;
270 *aa++ = riteA;
271 }
272 run[0] = 0;
273}
caryclark@google.com803eceb2012-06-06 12:09:34 +0000274#endif
reed@google.coma89c77b2011-12-01 21:47:26 +0000275
reed@google.com967a35d2011-10-31 19:37:58 +0000276void SuperBlitter::blitRect(int x, int y, int width, int height) {
277 SkASSERT(width > 0);
278 SkASSERT(height > 0);
279
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000280 // blit leading rows
281 while ((y & MASK)) {
reed@google.com967a35d2011-10-31 19:37:58 +0000282 this->blitH(x, y++, width);
283 if (--height <= 0) {
284 return;
285 }
286 }
287 SkASSERT(height > 0);
288
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000289 // Since this is a rect, instead of blitting supersampled rows one at a
290 // time and then resolving to the destination canvas, we can blit
291 // directly to the destintion canvas one row per SCALE supersampled rows.
reed@google.com967a35d2011-10-31 19:37:58 +0000292 int start_y = y >> SHIFT;
293 int stop_y = (y + height) >> SHIFT;
294 int count = stop_y - start_y;
295 if (count > 0) {
296 y += count << SHIFT;
297 height -= count << SHIFT;
298
299 // save original X for our tail blitH() loop at the bottom
300 int origX = x;
301
302 x -= fSuperLeft;
303 // hack, until I figure out why my cubics (I think) go beyond the bounds
304 if (x < 0) {
305 width += x;
306 x = 0;
307 }
308
tomhudson@google.com49eac192011-12-27 13:59:20 +0000309 // There is always a left column, a middle, and a right column.
310 // ileft is the destination x of the first pixel of the entire rect.
311 // xleft is (SCALE - # of covered supersampled pixels) in that
312 // destination pixel.
reed@google.com967a35d2011-10-31 19:37:58 +0000313 int ileft = x >> SHIFT;
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000314 int xleft = x & MASK;
tomhudson@google.com49eac192011-12-27 13:59:20 +0000315 // irite is the destination x of the last pixel of the OPAQUE section.
316 // xrite is the number of supersampled pixels extending beyond irite;
317 // xrite/SCALE should give us alpha.
reed@google.com967a35d2011-10-31 19:37:58 +0000318 int irite = (x + width) >> SHIFT;
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000319 int xrite = (x + width) & MASK;
tomhudson@google.com49eac192011-12-27 13:59:20 +0000320 if (!xrite) {
321 xrite = SCALE;
322 irite--;
323 }
324
tomhudson@google.com47143592011-12-28 17:58:07 +0000325 // Need to call flush() to clean up pending draws before we
326 // even consider blitV(), since otherwise it can look nonmonotonic.
327 SkASSERT(start_y > fCurrIY);
328 this->flush();
329
reed@google.com967a35d2011-10-31 19:37:58 +0000330 int n = irite - ileft - 1;
331 if (n < 0) {
tomhudson@google.com47143592011-12-28 17:58:07 +0000332 // If n < 0, we'll only have a single partially-transparent column
333 // of pixels to render.
reed@google.com967a35d2011-10-31 19:37:58 +0000334 xleft = xrite - xleft;
tomhudson@google.com49eac192011-12-27 13:59:20 +0000335 SkASSERT(xleft <= SCALE);
336 SkASSERT(xleft > 0);
reed@google.com967a35d2011-10-31 19:37:58 +0000337 xrite = 0;
tomhudson@google.com49eac192011-12-27 13:59:20 +0000338 fRealBlitter->blitV(ileft + fLeft, start_y, count,
339 coverage_to_exact_alpha(xleft));
reed@google.com967a35d2011-10-31 19:37:58 +0000340 } else {
tomhudson@google.com47143592011-12-28 17:58:07 +0000341 // With n = 0, we have two possibly-transparent columns of pixels
342 // to render; with n > 0, we have opaque columns between them.
343
tomhudson@google.com49eac192011-12-27 13:59:20 +0000344 xleft = SCALE - xleft;
tomhudson@google.com47143592011-12-28 17:58:07 +0000345
346 // Using coverage_to_exact_alpha is not consistent with blitH()
347 const int coverageL = coverage_to_exact_alpha(xleft);
348 const int coverageR = coverage_to_exact_alpha(xrite);
349
350 SkASSERT(coverageL > 0 || n > 0 || coverageR > 0);
351 SkASSERT((coverageL != 0) + n + (coverageR != 0) <= fWidth);
352
353 fRealBlitter->blitAntiRect(ileft + fLeft, start_y, n, count,
354 coverageL, coverageR);
reed@google.com967a35d2011-10-31 19:37:58 +0000355 }
356
reed@google.com967a35d2011-10-31 19:37:58 +0000357 // preamble for our next call to blitH()
358 fCurrIY = stop_y - 1;
359 fOffsetX = 0;
360 fCurrY = y - 1;
361 fRuns.reset(fWidth);
362 x = origX;
363 }
364
tomhudson@google.com49eac192011-12-27 13:59:20 +0000365 // catch any remaining few rows
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000366 SkASSERT(height <= MASK);
reed@google.com967a35d2011-10-31 19:37:58 +0000367 while (--height >= 0) {
368 this->blitH(x, y++, width);
369 }
370}
371
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372///////////////////////////////////////////////////////////////////////////////
373
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000374/// Masked supersampling antialiased blitter.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375class MaskSuperBlitter : public BaseSuperBlitter {
376public:
377 MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
378 const SkRegion& clip);
379 virtual ~MaskSuperBlitter() {
380 fRealBlitter->blitMask(fMask, fClipRect);
381 }
382
reed@google.com967a35d2011-10-31 19:37:58 +0000383 virtual void blitH(int x, int y, int width) SK_OVERRIDE;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000384
reed@google.com9f2f0a82011-04-11 19:43:58 +0000385 static bool CanHandleRect(const SkIRect& bounds) {
386#ifdef FORCE_RLE
387 return false;
388#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000389 int width = bounds.width();
reed@google.comf9f258c2012-03-12 16:09:06 +0000390 int64_t rb = SkAlign4(width);
391 // use 64bits to detect overflow
392 int64_t storage = rb * bounds.height();
reed@google.com55b6b582011-03-02 15:58:18 +0000393
reed@android.com8a1c16f2008-12-17 15:59:43 +0000394 return (width <= MaskSuperBlitter::kMAX_WIDTH) &&
reed@google.comf9f258c2012-03-12 16:09:06 +0000395 (storage <= MaskSuperBlitter::kMAX_STORAGE);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000396 }
reed@google.com55b6b582011-03-02 15:58:18 +0000397
reed@android.com8a1c16f2008-12-17 15:59:43 +0000398private:
399 enum {
reed@google.com9f2f0a82011-04-11 19:43:58 +0000400#ifdef FORCE_SUPERMASK
401 kMAX_WIDTH = 2048,
402 kMAX_STORAGE = 1024 * 1024 * 2
403#else
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404 kMAX_WIDTH = 32, // so we don't try to do very wide things, where the RLE blitter would be faster
405 kMAX_STORAGE = 1024
reed@google.com9f2f0a82011-04-11 19:43:58 +0000406#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000407 };
408
409 SkMask fMask;
410 SkIRect fClipRect;
411 // we add 1 because add_aa_span can write (unchanged) 1 extra byte at the end, rather than
412 // perform a test to see if stopAlpha != 0
413 uint32_t fStorage[(kMAX_STORAGE >> 2) + 1];
414};
415
416MaskSuperBlitter::MaskSuperBlitter(SkBlitter* realBlitter, const SkIRect& ir,
417 const SkRegion& clip)
418 : BaseSuperBlitter(realBlitter, ir, clip) {
419 SkASSERT(CanHandleRect(ir));
420
421 fMask.fImage = (uint8_t*)fStorage;
422 fMask.fBounds = ir;
423 fMask.fRowBytes = ir.width();
424 fMask.fFormat = SkMask::kA8_Format;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000425
reed@android.com8a1c16f2008-12-17 15:59:43 +0000426 fClipRect = ir;
427 fClipRect.intersect(clip.getBounds());
reed@google.com55b6b582011-03-02 15:58:18 +0000428
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 // For valgrind, write 1 extra byte at the end so we don't read
430 // uninitialized memory. See comment in add_aa_span and fStorage[].
431 memset(fStorage, 0, fMask.fBounds.height() * fMask.fRowBytes + 1);
432}
433
reed@google.com9f2f0a82011-04-11 19:43:58 +0000434static void add_aa_span(uint8_t* alpha, U8CPU startAlpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000435 /* I should be able to just add alpha[x] + startAlpha.
436 However, if the trailing edge of the previous span and the leading
437 edge of the current span round to the same super-sampled x value,
438 I might overflow to 256 with this add, hence the funny subtract.
439 */
440 unsigned tmp = *alpha + startAlpha;
441 SkASSERT(tmp <= 256);
442 *alpha = SkToU8(tmp - (tmp >> 8));
443}
444
reed@google.com9f2f0a82011-04-11 19:43:58 +0000445static inline uint32_t quadplicate_byte(U8CPU value) {
446 uint32_t pair = (value << 8) | value;
447 return (pair << 16) | pair;
448}
449
reed@google.com6997e5d2012-05-09 13:48:50 +0000450// Perform this tricky subtract, to avoid overflowing to 256. Our caller should
451// only ever call us with at most enough to hit 256 (never larger), so it is
452// enough to just subtract the high-bit. Actually clamping with a branch would
453// be slower (e.g. if (tmp > 255) tmp = 255;)
454//
455static inline void saturated_add(uint8_t* ptr, U8CPU add) {
456 unsigned tmp = *ptr + add;
457 SkASSERT(tmp <= 256);
458 *ptr = SkToU8(tmp - (tmp >> 8));
459}
460
reed@google.com9f2f0a82011-04-11 19:43:58 +0000461// minimum count before we want to setup an inner loop, adding 4-at-a-time
462#define MIN_COUNT_FOR_QUAD_LOOP 16
463
464static void add_aa_span(uint8_t* alpha, U8CPU startAlpha, int middleCount,
465 U8CPU stopAlpha, U8CPU maxValue) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000466 SkASSERT(middleCount >= 0);
467
reed@google.com6997e5d2012-05-09 13:48:50 +0000468 saturated_add(alpha, startAlpha);
469 alpha += 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000470
reed@google.com9f2f0a82011-04-11 19:43:58 +0000471 if (middleCount >= MIN_COUNT_FOR_QUAD_LOOP) {
472 // loop until we're quad-byte aligned
473 while (SkTCast<intptr_t>(alpha) & 0x3) {
474 alpha[0] = SkToU8(alpha[0] + maxValue);
475 alpha += 1;
476 middleCount -= 1;
477 }
478
479 int bigCount = middleCount >> 2;
480 uint32_t* qptr = reinterpret_cast<uint32_t*>(alpha);
481 uint32_t qval = quadplicate_byte(maxValue);
482 do {
483 *qptr++ += qval;
484 } while (--bigCount > 0);
485
486 middleCount &= 3;
487 alpha = reinterpret_cast<uint8_t*> (qptr);
488 // fall through to the following while-loop
489 }
490
491 while (--middleCount >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000492 alpha[0] = SkToU8(alpha[0] + maxValue);
493 alpha += 1;
494 }
495
496 // potentially this can be off the end of our "legal" alpha values, but that
497 // only happens if stopAlpha is also 0. Rather than test for stopAlpha != 0
498 // every time (slow), we just do it, and ensure that we've allocated extra space
499 // (see the + 1 comment in fStorage[]
reed@google.com6997e5d2012-05-09 13:48:50 +0000500 saturated_add(alpha, stopAlpha);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000501}
502
reed@google.com9f2f0a82011-04-11 19:43:58 +0000503void MaskSuperBlitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504 int iy = (y >> SHIFT);
reed@google.com55b6b582011-03-02 15:58:18 +0000505
reed@android.com8a1c16f2008-12-17 15:59:43 +0000506 SkASSERT(iy >= fMask.fBounds.fTop && iy < fMask.fBounds.fBottom);
507 iy -= fMask.fBounds.fTop; // make it relative to 0
508
senorblanco@chromium.orgc41513c2009-08-21 22:00:12 +0000509 // This should never happen, but it does. Until the true cause is
510 // discovered, let's skip this span instead of crashing.
511 // See http://crbug.com/17569.
512 if (iy < 0) {
513 return;
514 }
515
reed@android.com8a1c16f2008-12-17 15:59:43 +0000516#ifdef SK_DEBUG
517 {
518 int ix = x >> SHIFT;
519 SkASSERT(ix >= fMask.fBounds.fLeft && ix < fMask.fBounds.fRight);
520 }
521#endif
reed@google.com55b6b582011-03-02 15:58:18 +0000522
reed@android.com8a1c16f2008-12-17 15:59:43 +0000523 x -= (fMask.fBounds.fLeft << SHIFT);
524
525 // hack, until I figure out why my cubics (I think) go beyond the bounds
reed@google.com9f2f0a82011-04-11 19:43:58 +0000526 if (x < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000527 width += x;
528 x = 0;
529 }
530
reed@android.com8a1c16f2008-12-17 15:59:43 +0000531 uint8_t* row = fMask.fImage + iy * fMask.fRowBytes + (x >> SHIFT);
532
533 int start = x;
534 int stop = x + width;
535
536 SkASSERT(start >= 0 && stop > start);
tomhudson@google.com05fffdc2011-12-01 20:41:24 +0000537 int fb = start & MASK;
538 int fe = stop & MASK;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539 int n = (stop >> SHIFT) - (start >> SHIFT) - 1;
540
541
reed@google.com9f2f0a82011-04-11 19:43:58 +0000542 if (n < 0) {
senorblanco@chromium.orgfeef49c2009-08-12 20:21:49 +0000543 SkASSERT(row >= fMask.fImage);
544 SkASSERT(row < fMask.fImage + kMAX_STORAGE + 1);
tomhudson@google.comffc92482012-02-23 19:34:34 +0000545 add_aa_span(row, coverage_to_partial_alpha(fe - fb));
reed@google.com9f2f0a82011-04-11 19:43:58 +0000546 } else {
tomhudson@google.com49eac192011-12-27 13:59:20 +0000547 fb = SCALE - fb;
senorblanco@chromium.orgfeef49c2009-08-12 20:21:49 +0000548 SkASSERT(row >= fMask.fImage);
549 SkASSERT(row + n + 1 < fMask.fImage + kMAX_STORAGE + 1);
tomhudson@google.comffc92482012-02-23 19:34:34 +0000550 add_aa_span(row, coverage_to_partial_alpha(fb),
551 n, coverage_to_partial_alpha(fe),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552 (1 << (8 - SHIFT)) - (((y & MASK) + 1) >> SHIFT));
553 }
554
555#ifdef SK_DEBUG
556 fCurrX = x + width;
557#endif
558}
559
560///////////////////////////////////////////////////////////////////////////////
561
reed@google.com07db8612012-02-28 15:32:42 +0000562static bool fitsInsideLimit(const SkRect& r, SkScalar max) {
563 const SkScalar min = -max;
564 return r.fLeft > min && r.fTop > min &&
565 r.fRight < max && r.fBottom < max;
566}
567
tomhudson@google.com17b6ba42012-04-12 14:23:58 +0000568static int overflows_short_shift(int value, int shift) {
569 const int s = 16 + shift;
570 return (value << s >> s) - value;
571}
572
573/**
574 Would any of the coordinates of this rectangle not fit in a short,
575 when left-shifted by shift?
576*/
577static int rect_overflows_short_shift(SkIRect rect, int shift) {
578 SkASSERT(!overflows_short_shift(8191, SHIFT));
579 SkASSERT(overflows_short_shift(8192, SHIFT));
580 SkASSERT(!overflows_short_shift(32767, 0));
581 SkASSERT(overflows_short_shift(32768, 0));
582
583 // Since we expect these to succeed, we bit-or together
584 // for a tiny extra bit of speed.
585 return overflows_short_shift(rect.fLeft, SHIFT) |
586 overflows_short_shift(rect.fRight, SHIFT) |
587 overflows_short_shift(rect.fTop, SHIFT) |
588 overflows_short_shift(rect.fBottom, SHIFT);
589}
590
reed@google.com07db8612012-02-28 15:32:42 +0000591static bool safeRoundOut(const SkRect& src, SkIRect* dst, int32_t maxInt) {
592 const SkScalar maxScalar = SkIntToScalar(maxInt);
reed@google.com8f4d2302013-12-17 16:44:46 +0000593
reed@google.com07db8612012-02-28 15:32:42 +0000594 if (fitsInsideLimit(src, maxScalar)) {
595 src.roundOut(dst);
596 return true;
597 }
598 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599}
600
reed@google.com5546ef22012-01-30 17:09:45 +0000601void SkScan::AntiFillPath(const SkPath& path, const SkRegion& origClip,
reed@google.com2c508f22011-10-03 21:15:46 +0000602 SkBlitter* blitter, bool forceRLE) {
reed@google.com5546ef22012-01-30 17:09:45 +0000603 if (origClip.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000604 return;
605 }
606
reed@android.comd252db02009-04-01 18:31:44 +0000607 SkIRect ir;
reed@google.com07db8612012-02-28 15:32:42 +0000608
609 if (!safeRoundOut(path.getBounds(), &ir, SK_MaxS32 >> SHIFT)) {
610#if 0
611 const SkRect& r = path.getBounds();
612 SkDebugf("--- bounds can't fit in SkIRect\n", r.fLeft, r.fTop, r.fRight, r.fBottom);
613#endif
614 return;
615 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616 if (ir.isEmpty()) {
reed@google.comee068aa2011-12-21 19:36:21 +0000617 if (path.isInverseFillType()) {
reed@google.com5546ef22012-01-30 17:09:45 +0000618 blitter->blitRegion(origClip);
reed@google.comee068aa2011-12-21 19:36:21 +0000619 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000620 return;
621 }
622
tomhudson@google.com17b6ba42012-04-12 14:23:58 +0000623 // If the intersection of the path bounds and the clip bounds
624 // will overflow 32767 when << by SHIFT, we can't supersample,
625 // so draw without antialiasing.
626 SkIRect clippedIR;
627 if (path.isInverseFillType()) {
628 // If the path is an inverse fill, it's going to fill the entire
629 // clip, and we care whether the entire clip exceeds our limits.
630 clippedIR = origClip.getBounds();
631 } else {
632 if (!clippedIR.intersect(ir, origClip.getBounds())) {
633 return;
634 }
635 }
636 if (rect_overflows_short_shift(clippedIR, SHIFT)) {
637 SkScan::FillPath(path, origClip, blitter);
638 return;
639 }
640
reed@google.com5546ef22012-01-30 17:09:45 +0000641 // Our antialiasing can't handle a clip larger than 32767, so we restrict
642 // the clip to that limit here. (the runs[] uses int16_t for its index).
643 //
tomhudson@google.com17b6ba42012-04-12 14:23:58 +0000644 // A more general solution (one that could also eliminate the need to
645 // disable aa based on ir bounds (see overflows_short_shift) would be
646 // to tile the clip/target...
reed@google.com5546ef22012-01-30 17:09:45 +0000647 SkRegion tmpClipStorage;
648 const SkRegion* clipRgn = &origClip;
649 {
650 static const int32_t kMaxClipCoord = 32767;
651 const SkIRect& bounds = origClip.getBounds();
652 if (bounds.fRight > kMaxClipCoord || bounds.fBottom > kMaxClipCoord) {
653 SkIRect limit = { 0, 0, kMaxClipCoord, kMaxClipCoord };
654 tmpClipStorage.op(origClip, limit, SkRegion::kIntersect_Op);
655 clipRgn = &tmpClipStorage;
656 }
657 }
658 // for here down, use clipRgn, not origClip
659
660 SkScanClipper clipper(blitter, clipRgn, ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000661 const SkIRect* clipRect = clipper.getClipRect();
662
663 if (clipper.getBlitter() == NULL) { // clipped out
664 if (path.isInverseFillType()) {
reed@google.com5546ef22012-01-30 17:09:45 +0000665 blitter->blitRegion(*clipRgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000666 }
667 return;
668 }
reed@google.com55b6b582011-03-02 15:58:18 +0000669
reed@android.com8a1c16f2008-12-17 15:59:43 +0000670 // now use the (possibly wrapped) blitter
671 blitter = clipper.getBlitter();
672
673 if (path.isInverseFillType()) {
reed@google.com5546ef22012-01-30 17:09:45 +0000674 sk_blit_above(blitter, ir, *clipRgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675 }
676
677 SkIRect superRect, *superClipRect = NULL;
678
reed@google.com9f2f0a82011-04-11 19:43:58 +0000679 if (clipRect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000680 superRect.set( clipRect->fLeft << SHIFT, clipRect->fTop << SHIFT,
681 clipRect->fRight << SHIFT, clipRect->fBottom << SHIFT);
682 superClipRect = &superRect;
683 }
684
senorblanco@chromium.orgc41513c2009-08-21 22:00:12 +0000685 SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
686
reed@android.com8a1c16f2008-12-17 15:59:43 +0000687 // MaskSuperBlitter can't handle drawing outside of ir, so we can't use it
688 // if we're an inverse filltype
reed@google.com2c508f22011-10-03 21:15:46 +0000689 if (!path.isInverseFillType() && MaskSuperBlitter::CanHandleRect(ir) && !forceRLE) {
reed@google.com5546ef22012-01-30 17:09:45 +0000690 MaskSuperBlitter superBlit(blitter, ir, *clipRgn);
senorblanco@chromium.orgc41513c2009-08-21 22:00:12 +0000691 SkASSERT(SkIntToScalar(ir.fTop) <= path.getBounds().fTop);
reed@google.com5546ef22012-01-30 17:09:45 +0000692 sk_fill_path(path, superClipRect, &superBlit, ir.fTop, ir.fBottom, SHIFT, *clipRgn);
reed@google.com9f2f0a82011-04-11 19:43:58 +0000693 } else {
reed@google.com5546ef22012-01-30 17:09:45 +0000694 SuperBlitter superBlit(blitter, ir, *clipRgn);
695 sk_fill_path(path, superClipRect, &superBlit, ir.fTop, ir.fBottom, SHIFT, *clipRgn);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000696 }
reed@google.com55b6b582011-03-02 15:58:18 +0000697
698 if (path.isInverseFillType()) {
reed@google.com5546ef22012-01-30 17:09:45 +0000699 sk_blit_below(blitter, ir, *clipRgn);
reed@google.com55b6b582011-03-02 15:58:18 +0000700 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000701}
reed@google.com1ba71372011-10-12 20:42:05 +0000702
703///////////////////////////////////////////////////////////////////////////////
704
705#include "SkRasterClip.h"
706
707void SkScan::FillPath(const SkPath& path, const SkRasterClip& clip,
708 SkBlitter* blitter) {
709 if (clip.isEmpty()) {
710 return;
711 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000712
reed@google.com1ba71372011-10-12 20:42:05 +0000713 if (clip.isBW()) {
714 FillPath(path, clip.bwRgn(), blitter);
715 } else {
716 SkRegion tmp;
717 SkAAClipBlitter aaBlitter;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000718
reed@google.com1ba71372011-10-12 20:42:05 +0000719 tmp.setRect(clip.getBounds());
720 aaBlitter.init(blitter, &clip.aaRgn());
721 SkScan::FillPath(path, tmp, &aaBlitter);
722 }
723}
724
725void SkScan::AntiFillPath(const SkPath& path, const SkRasterClip& clip,
726 SkBlitter* blitter) {
727 if (clip.isEmpty()) {
728 return;
729 }
730
731 if (clip.isBW()) {
732 AntiFillPath(path, clip.bwRgn(), blitter);
733 } else {
734 SkRegion tmp;
735 SkAAClipBlitter aaBlitter;
736
737 tmp.setRect(clip.getBounds());
738 aaBlitter.init(blitter, &clip.aaRgn());
739 SkScan::AntiFillPath(path, tmp, &aaBlitter, true);
740 }
741}