blob: 5b92ff92fb529d184dbc5a1a2635b559feafc912 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkScanPriv.h"
9#include "SkBlitter.h"
10#include "SkEdge.h"
mike@reedtribe.org948639b2011-07-19 01:28:17 +000011#include "SkEdgeBuilder.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkGeometry.h"
13#include "SkPath.h"
reed@android.comc07d23a2009-02-06 13:30:58 +000014#include "SkQuadClipper.h"
reed@google.com045e62d2011-10-24 12:19:46 +000015#include "SkRasterClip.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#include "SkRegion.h"
17#include "SkTemplates.h"
18
19#define kEDGE_HEAD_Y SK_MinS32
20#define kEDGE_TAIL_Y SK_MaxS32
21
22#ifdef SK_DEBUG
mike@reedtribe.org99219d32011-04-09 18:54:23 +000023 static void validate_sort(const SkEdge* edge) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 int y = kEDGE_HEAD_Y;
25
mike@reedtribe.org99219d32011-04-09 18:54:23 +000026 while (edge->fFirstY != SK_MaxS32) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 edge->validate();
28 SkASSERT(y <= edge->fFirstY);
29
30 y = edge->fFirstY;
31 edge = edge->fNext;
32 }
33 }
34#else
35 #define validate_sort(edge)
36#endif
37
mike@reedtribe.org99219d32011-04-09 18:54:23 +000038static inline void remove_edge(SkEdge* edge) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 edge->fPrev->fNext = edge->fNext;
40 edge->fNext->fPrev = edge->fPrev;
41}
42
mike@reedtribe.org99219d32011-04-09 18:54:23 +000043static inline void swap_edges(SkEdge* prev, SkEdge* next) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 SkASSERT(prev->fNext == next && next->fPrev == prev);
45
46 // remove prev from the list
47 prev->fPrev->fNext = next;
48 next->fPrev = prev->fPrev;
49
50 // insert prev after next
51 prev->fNext = next->fNext;
52 next->fNext->fPrev = prev;
53 next->fNext = prev;
54 prev->fPrev = next;
55}
56
mike@reedtribe.org99219d32011-04-09 18:54:23 +000057static void backward_insert_edge_based_on_x(SkEdge* edge SkDECLAREPARAM(int, curr_y)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SkFixed x = edge->fX;
59
mike@reedtribe.org99219d32011-04-09 18:54:23 +000060 for (;;) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 SkEdge* prev = edge->fPrev;
reed@google.com55b6b582011-03-02 15:58:18 +000062
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 // add 1 to curr_y since we may have added new edges (built from curves)
64 // that start on the next scanline
65 SkASSERT(prev && prev->fFirstY <= curr_y + 1);
66
mike@reedtribe.org99219d32011-04-09 18:54:23 +000067 if (prev->fX <= x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 break;
mike@reedtribe.org99219d32011-04-09 18:54:23 +000069 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 swap_edges(prev, edge);
71 }
72}
73
mike@reedtribe.org99219d32011-04-09 18:54:23 +000074static void insert_new_edges(SkEdge* newEdge, int curr_y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkASSERT(newEdge->fFirstY >= curr_y);
76
mike@reedtribe.org99219d32011-04-09 18:54:23 +000077 while (newEdge->fFirstY == curr_y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 SkEdge* next = newEdge->fNext;
79 backward_insert_edge_based_on_x(newEdge SkPARAM(curr_y));
80 newEdge = next;
81 }
82}
83
84#ifdef SK_DEBUG
mike@reedtribe.org99219d32011-04-09 18:54:23 +000085static void validate_edges_for_y(const SkEdge* edge, int curr_y) {
86 while (edge->fFirstY <= curr_y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000087 SkASSERT(edge->fPrev && edge->fNext);
88 SkASSERT(edge->fPrev->fNext == edge);
89 SkASSERT(edge->fNext->fPrev == edge);
90 SkASSERT(edge->fFirstY <= edge->fLastY);
91
92 SkASSERT(edge->fPrev->fX <= edge->fX);
93 edge = edge->fNext;
94 }
95}
96#else
97 #define validate_edges_for_y(edge, curr_y)
98#endif
99
100#if defined _WIN32 && _MSC_VER >= 1300 // disable warning : local variable used without having been initialized
101#pragma warning ( push )
102#pragma warning ( disable : 4701 )
103#endif
104
105typedef void (*PrePostProc)(SkBlitter* blitter, int y, bool isStartOfScanline);
106#define PREPOST_START true
107#define PREPOST_END false
108
109static void walk_edges(SkEdge* prevHead, SkPath::FillType fillType,
reed@android.comdca6a562010-02-22 16:05:48 +0000110 SkBlitter* blitter, int start_y, int stop_y,
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000111 PrePostProc proc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 validate_sort(prevHead->fNext);
113
reed@android.comdca6a562010-02-22 16:05:48 +0000114 int curr_y = start_y;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 // returns 1 for evenodd, -1 for winding, regardless of inverse-ness
116 int windingMask = (fillType & 1) ? 1 : -1;
117
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000118 for (;;) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000119 int w = 0;
120 int left SK_INIT_TO_AVOID_WARNING;
121 bool in_interval = false;
122 SkEdge* currE = prevHead->fNext;
123 SkFixed prevX = prevHead->fX;
124
125 validate_edges_for_y(currE, curr_y);
reed@google.com55b6b582011-03-02 15:58:18 +0000126
reed@android.com8a1c16f2008-12-17 15:59:43 +0000127 if (proc) {
128 proc(blitter, curr_y, PREPOST_START); // pre-proc
129 }
reed@google.com55b6b582011-03-02 15:58:18 +0000130
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000131 while (currE->fFirstY <= curr_y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 SkASSERT(currE->fLastY >= curr_y);
133
reed@google.com0a9cc442012-02-06 19:05:51 +0000134 int x = SkFixedRoundToInt(currE->fX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 w += currE->fWinding;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000136 if ((w & windingMask) == 0) { // we finished an interval
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 SkASSERT(in_interval);
138 int width = x - left;
139 SkASSERT(width >= 0);
140 if (width)
141 blitter->blitH(left, curr_y, width);
142 in_interval = false;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000143 } else if (!in_interval) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 left = x;
145 in_interval = true;
146 }
147
148 SkEdge* next = currE->fNext;
149 SkFixed newX;
150
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000151 if (currE->fLastY == curr_y) { // are we done with this edge?
152 if (currE->fCurveCount < 0) {
153 if (((SkCubicEdge*)currE)->updateCubic()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154 SkASSERT(currE->fFirstY == curr_y + 1);
reed@google.com55b6b582011-03-02 15:58:18 +0000155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 newX = currE->fX;
157 goto NEXT_X;
158 }
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000159 } else if (currE->fCurveCount > 0) {
160 if (((SkQuadraticEdge*)currE)->updateQuadratic()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161 newX = currE->fX;
162 goto NEXT_X;
163 }
164 }
165 remove_edge(currE);
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000166 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 SkASSERT(currE->fLastY > curr_y);
168 newX = currE->fX + currE->fDX;
169 currE->fX = newX;
170 NEXT_X:
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000171 if (newX < prevX) { // ripple currE backwards until it is x-sorted
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172 backward_insert_edge_based_on_x(currE SkPARAM(curr_y));
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000173 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 prevX = newX;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000175 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000176 }
177 currE = next;
178 SkASSERT(currE);
179 }
reed@google.com55b6b582011-03-02 15:58:18 +0000180
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 if (proc) {
182 proc(blitter, curr_y, PREPOST_END); // post-proc
183 }
184
185 curr_y += 1;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000186 if (curr_y >= stop_y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 break;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000188 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000189 // now currE points to the first edge with a Yint larger than curr_y
190 insert_new_edges(currE, curr_y);
191 }
192}
193
reed@google.come3a83ec2011-10-25 19:27:43 +0000194// return true if we're done with this edge
195static bool update_edge(SkEdge* edge, int last_y) {
196 SkASSERT(edge->fLastY >= last_y);
197 if (last_y == edge->fLastY) {
198 if (edge->fCurveCount < 0) {
199 if (((SkCubicEdge*)edge)->updateCubic()) {
200 SkASSERT(edge->fFirstY == last_y + 1);
201 return false;
202 }
203 } else if (edge->fCurveCount > 0) {
204 if (((SkQuadraticEdge*)edge)->updateQuadratic()) {
205 SkASSERT(edge->fFirstY == last_y + 1);
206 return false;
207 }
208 }
209 return true;
210 }
211 return false;
212}
213
214static void walk_convex_edges(SkEdge* prevHead, SkPath::FillType,
215 SkBlitter* blitter, int start_y, int stop_y,
216 PrePostProc proc) {
217 static int gCalls;
218 gCalls++;
219
220 validate_sort(prevHead->fNext);
221
222 SkEdge* leftE = prevHead->fNext;
223 SkEdge* riteE = leftE->fNext;
224 SkEdge* currE = riteE->fNext;
mike@reedtribe.orgfc5da922011-10-31 02:58:27 +0000225
226#if 0
reed@google.come3a83ec2011-10-25 19:27:43 +0000227 int local_top = leftE->fFirstY;
228 SkASSERT(local_top == riteE->fFirstY);
mike@reedtribe.orgfc5da922011-10-31 02:58:27 +0000229#else
230 // our edge choppers for curves can result in the initial edges
231 // not lining up, so we take the max.
232 int local_top = SkMax32(leftE->fFirstY, riteE->fFirstY);
233#endif
reed@google.come3a83ec2011-10-25 19:27:43 +0000234 SkASSERT(local_top >= start_y);
235
236 int gLoops = 0;
237 for (;;) {
238 gLoops++;
239
240 SkASSERT(leftE->fFirstY <= stop_y);
241 SkASSERT(riteE->fFirstY <= stop_y);
242
243 if (leftE->fX > riteE->fX || (leftE->fX == riteE->fX &&
244 leftE->fDX > riteE->fDX)) {
245 SkTSwap(leftE, riteE);
246 }
247
248 int local_bot = SkMin32(leftE->fLastY, riteE->fLastY);
reed@google.coma6c7c482011-10-28 18:58:46 +0000249 local_bot = SkMin32(local_bot, stop_y - 1);
reed@google.come3a83ec2011-10-25 19:27:43 +0000250 SkASSERT(local_top <= local_bot);
reed@google.come3a83ec2011-10-25 19:27:43 +0000251
252 SkFixed left = leftE->fX;
253 SkFixed dLeft = leftE->fDX;
254 SkFixed rite = riteE->fX;
255 SkFixed dRite = riteE->fDX;
256 int count = local_bot - local_top;
257 SkASSERT(count >= 0);
reed@google.com562a2ac2011-10-31 14:14:18 +0000258 if (0 == (dLeft | dRite)) {
reed@google.com0a9cc442012-02-06 19:05:51 +0000259 int L = SkFixedRoundToInt(left);
260 int R = SkFixedRoundToInt(rite);
reed@google.come3a83ec2011-10-25 19:27:43 +0000261 if (L < R) {
262 count += 1;
263 blitter->blitRect(L, local_top, R - L, count);
reed@google.come3a83ec2011-10-25 19:27:43 +0000264 left += count * dLeft;
265 rite += count * dRite;
266 }
mike@reedtribe.orgfc5da922011-10-31 02:58:27 +0000267 local_top = local_bot + 1;
reed@google.come3a83ec2011-10-25 19:27:43 +0000268 } else {
269 do {
reed@google.com0a9cc442012-02-06 19:05:51 +0000270 int L = SkFixedRoundToInt(left);
271 int R = SkFixedRoundToInt(rite);
reed@google.come3a83ec2011-10-25 19:27:43 +0000272 if (L < R) {
273 blitter->blitH(L, local_top, R - L);
274 }
275 left += dLeft;
276 rite += dRite;
277 local_top += 1;
278 } while (--count >= 0);
279 }
280
281 leftE->fX = left;
282 riteE->fX = rite;
283
284 if (update_edge(leftE, local_bot)) {
285 if (currE->fFirstY >= stop_y) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000286 break;
287 }
288 leftE = currE;
289 currE = currE->fNext;
290 }
291 if (update_edge(riteE, local_bot)) {
292 if (currE->fFirstY >= stop_y) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000293 break;
294 }
295 riteE = currE;
296 currE = currE->fNext;
297 }
298
299 SkASSERT(leftE);
300 SkASSERT(riteE);
reed@google.coma6c7c482011-10-28 18:58:46 +0000301
302 // check our bottom clip
303 SkASSERT(local_top == local_bot + 1);
304 if (local_top >= stop_y) {
305 break;
306 }
reed@google.come3a83ec2011-10-25 19:27:43 +0000307 }
308}
309
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310///////////////////////////////////////////////////////////////////////////////
311
312// this guy overrides blitH, and will call its proxy blitter with the inverse
313// of the spans it is given (clipped to the left/right of the cliprect)
314//
315// used to implement inverse filltypes on paths
316//
317class InverseBlitter : public SkBlitter {
318public:
319 void setBlitter(SkBlitter* blitter, const SkIRect& clip, int shift) {
320 fBlitter = blitter;
321 fFirstX = clip.fLeft << shift;
322 fLastX = clip.fRight << shift;
323 }
324 void prepost(int y, bool isStart) {
325 if (isStart) {
326 fPrevX = fFirstX;
327 } else {
328 int invWidth = fLastX - fPrevX;
329 if (invWidth > 0) {
330 fBlitter->blitH(fPrevX, y, invWidth);
331 }
332 }
333 }
334
335 // overrides
336 virtual void blitH(int x, int y, int width) {
337 int invWidth = x - fPrevX;
338 if (invWidth > 0) {
339 fBlitter->blitH(fPrevX, y, invWidth);
340 }
341 fPrevX = x + width;
342 }
reed@google.com55b6b582011-03-02 15:58:18 +0000343
reed@android.com8a1c16f2008-12-17 15:59:43 +0000344 // we do not expect to get called with these entrypoints
345 virtual void blitAntiH(int, int, const SkAlpha[], const int16_t runs[]) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000346 SkDEBUGFAIL("blitAntiH unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 }
348 virtual void blitV(int x, int y, int height, SkAlpha alpha) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000349 SkDEBUGFAIL("blitV unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 }
351 virtual void blitRect(int x, int y, int width, int height) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000352 SkDEBUGFAIL("blitRect unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000353 }
354 virtual void blitMask(const SkMask&, const SkIRect& clip) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000355 SkDEBUGFAIL("blitMask unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000356 }
357 virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000358 SkDEBUGFAIL("justAnOpaqueColor unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000359 return NULL;
360 }
reed@google.com55b6b582011-03-02 15:58:18 +0000361
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362private:
363 SkBlitter* fBlitter;
364 int fFirstX, fLastX, fPrevX;
365};
366
367static void PrePostInverseBlitterProc(SkBlitter* blitter, int y, bool isStart) {
368 ((InverseBlitter*)blitter)->prepost(y, isStart);
369}
370
371///////////////////////////////////////////////////////////////////////////////
372
373#if defined _WIN32 && _MSC_VER >= 1300
374#pragma warning ( pop )
375#endif
376
reed@android.come72fee52009-11-16 14:52:01 +0000377extern "C" {
378 static int edge_compare(const void* a, const void* b) {
379 const SkEdge* edgea = *(const SkEdge**)a;
380 const SkEdge* edgeb = *(const SkEdge**)b;
reed@google.com55b6b582011-03-02 15:58:18 +0000381
reed@android.come72fee52009-11-16 14:52:01 +0000382 int valuea = edgea->fFirstY;
383 int valueb = edgeb->fFirstY;
reed@google.com55b6b582011-03-02 15:58:18 +0000384
reed@android.come72fee52009-11-16 14:52:01 +0000385 if (valuea == valueb) {
386 valuea = edgea->fX;
387 valueb = edgeb->fX;
388 }
reed@google.com55b6b582011-03-02 15:58:18 +0000389
reed@android.come72fee52009-11-16 14:52:01 +0000390 // this overflows if valuea >>> valueb or vice-versa
391 // return valuea - valueb;
392 // do perform the slower but safe compares
393 return (valuea < valueb) ? -1 : (valuea > valueb);
394 }
395}
396
397static SkEdge* sort_edges(SkEdge* list[], int count, SkEdge** last) {
398 qsort(list, count, sizeof(SkEdge*), edge_compare);
reed@google.com55b6b582011-03-02 15:58:18 +0000399
reed@android.come72fee52009-11-16 14:52:01 +0000400 // now make the edges linked in sorted order
401 for (int i = 1; i < count; i++) {
402 list[i - 1]->fNext = list[i];
403 list[i]->fPrev = list[i - 1];
404 }
reed@google.com55b6b582011-03-02 15:58:18 +0000405
reed@android.come72fee52009-11-16 14:52:01 +0000406 *last = list[count - 1];
407 return list[0];
408}
409
reed@android.com8a1c16f2008-12-17 15:59:43 +0000410// clipRect may be null, even though we always have a clip. This indicates that
411// the path is contained in the clip, and so we can ignore it during the blit
412//
413// clipRect (if no null) has already been shifted up
414//
415void sk_fill_path(const SkPath& path, const SkIRect* clipRect, SkBlitter* blitter,
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000416 int start_y, int stop_y, int shiftEdgesUp,
417 const SkRegion& clipRgn) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000418 SkASSERT(&path && blitter);
419
reed@android.com909994f2009-11-18 16:09:51 +0000420 SkEdgeBuilder builder;
reed@google.com55b6b582011-03-02 15:58:18 +0000421
reed@android.com909994f2009-11-18 16:09:51 +0000422 int count = builder.build(path, clipRect, shiftEdgesUp);
423 SkEdge** list = builder.edgeList();
reed@android.com909994f2009-11-18 16:09:51 +0000424
reed@android.com9da1ae32009-07-22 17:06:15 +0000425 if (count < 2) {
agl@chromium.org8cfdf012010-12-06 18:52:40 +0000426 if (path.isInverseFillType()) {
427 const SkIRect& clipRect = clipRgn.getBounds();
428 blitter->blitRect(clipRect.fLeft << shiftEdgesUp,
429 clipRect.fTop << shiftEdgesUp,
430 clipRect.width() << shiftEdgesUp,
431 clipRect.height() << shiftEdgesUp);
432 }
433
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 return;
435 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436
reed@android.com909994f2009-11-18 16:09:51 +0000437 SkEdge headEdge, tailEdge, *last;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000438 // this returns the first and last edge after they're sorted into a dlink list
reed@android.com909994f2009-11-18 16:09:51 +0000439 SkEdge* edge = sort_edges(list, count, &last);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440
441 headEdge.fPrev = NULL;
442 headEdge.fNext = edge;
443 headEdge.fFirstY = kEDGE_HEAD_Y;
444 headEdge.fX = SK_MinS32;
445 edge->fPrev = &headEdge;
446
447 tailEdge.fPrev = last;
448 tailEdge.fNext = NULL;
449 tailEdge.fFirstY = kEDGE_TAIL_Y;
450 last->fNext = &tailEdge;
451
452 // now edge is the head of the sorted linklist
453
reed@android.comdca6a562010-02-22 16:05:48 +0000454 start_y <<= shiftEdgesUp;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000455 stop_y <<= shiftEdgesUp;
reed@android.comdca6a562010-02-22 16:05:48 +0000456 if (clipRect && start_y < clipRect->fTop) {
457 start_y = clipRect->fTop;
458 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459 if (clipRect && stop_y > clipRect->fBottom) {
460 stop_y = clipRect->fBottom;
461 }
462
463 InverseBlitter ib;
464 PrePostProc proc = NULL;
465
466 if (path.isInverseFillType()) {
467 ib.setBlitter(blitter, clipRgn.getBounds(), shiftEdgesUp);
468 blitter = &ib;
469 proc = PrePostInverseBlitterProc;
470 }
471
reed@google.coma6c7c482011-10-28 18:58:46 +0000472 if (path.isConvex() && (NULL == proc)) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000473 walk_convex_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, NULL);
474 } else {
475 walk_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, proc);
476 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000477}
478
reed@google.com55b6b582011-03-02 15:58:18 +0000479void sk_blit_above(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000480 const SkIRect& cr = clip.getBounds();
481 SkIRect tmp;
reed@google.com55b6b582011-03-02 15:58:18 +0000482
reed@android.com8a1c16f2008-12-17 15:59:43 +0000483 tmp.fLeft = cr.fLeft;
484 tmp.fRight = cr.fRight;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000485 tmp.fTop = cr.fTop;
486 tmp.fBottom = ir.fTop;
487 if (!tmp.isEmpty()) {
488 blitter->blitRectRegion(tmp, clip);
489 }
reed@google.com55b6b582011-03-02 15:58:18 +0000490}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000491
reed@google.com55b6b582011-03-02 15:58:18 +0000492void sk_blit_below(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) {
493 const SkIRect& cr = clip.getBounds();
494 SkIRect tmp;
495
496 tmp.fLeft = cr.fLeft;
497 tmp.fRight = cr.fRight;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000498 tmp.fTop = ir.fBottom;
499 tmp.fBottom = cr.fBottom;
500 if (!tmp.isEmpty()) {
501 blitter->blitRectRegion(tmp, clip);
502 }
503}
504
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000505///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000506
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000507SkScanClipper::SkScanClipper(SkBlitter* blitter, const SkRegion* clip,
508 const SkIRect& ir) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509 fBlitter = NULL; // null means blit nothing
510 fClipRect = NULL;
511
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000512 if (clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 fClipRect = &clip->getBounds();
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000514 if (!SkIRect::Intersects(*fClipRect, ir)) { // completely clipped out
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515 return;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000516 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000517
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000518 if (clip->isRect()) {
519 if (fClipRect->contains(ir)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000520 fClipRect = NULL;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000521 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000522 // only need a wrapper blitter if we're horizontally clipped
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000523 if (fClipRect->fLeft > ir.fLeft || fClipRect->fRight < ir.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524 fRectBlitter.init(blitter, *fClipRect);
525 blitter = &fRectBlitter;
526 }
527 }
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000528 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 fRgnBlitter.init(blitter, clip);
530 blitter = &fRgnBlitter;
531 }
532 }
533 fBlitter = blitter;
534}
535
536///////////////////////////////////////////////////////////////////////////////
537
reed@google.com15018032011-07-11 12:21:30 +0000538static bool clip_to_limit(const SkRegion& orig, SkRegion* reduced) {
539 const int32_t limit = 32767;
540
541 SkIRect limitR;
542 limitR.set(-limit, -limit, limit, limit);
543 if (limitR.contains(orig.getBounds())) {
544 return false;
545 }
546 reduced->op(orig, limitR, SkRegion::kIntersect_Op);
547 return true;
548}
549
550void SkScan::FillPath(const SkPath& path, const SkRegion& origClip,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551 SkBlitter* blitter) {
reed@google.com15018032011-07-11 12:21:30 +0000552 if (origClip.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000553 return;
554 }
555
reed@google.com15018032011-07-11 12:21:30 +0000556 // Our edges are fixed-point, and don't like the bounds of the clip to
557 // exceed that. Here we trim the clip just so we don't overflow later on
558 const SkRegion* clipPtr = &origClip;
559 SkRegion finiteClip;
560 if (clip_to_limit(origClip, &finiteClip)) {
561 if (finiteClip.isEmpty()) {
562 return;
563 }
564 clipPtr = &finiteClip;
565 }
566 // don't reference "origClip" any more, just use clipPtr
567
reed@android.com8a1c16f2008-12-17 15:59:43 +0000568 SkIRect ir;
reed@android.comd252db02009-04-01 18:31:44 +0000569 path.getBounds().round(&ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000570 if (ir.isEmpty()) {
571 if (path.isInverseFillType()) {
reed@google.com15018032011-07-11 12:21:30 +0000572 blitter->blitRegion(*clipPtr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 }
574 return;
575 }
576
reed@google.com15018032011-07-11 12:21:30 +0000577 SkScanClipper clipper(blitter, clipPtr, ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000578
579 blitter = clipper.getBlitter();
580 if (blitter) {
reed@google.com55b6b582011-03-02 15:58:18 +0000581 // we have to keep our calls to blitter in sorted order, so we
582 // must blit the above section first, then the middle, then the bottom.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000583 if (path.isInverseFillType()) {
reed@google.com15018032011-07-11 12:21:30 +0000584 sk_blit_above(blitter, ir, *clipPtr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000585 }
reed@google.com15018032011-07-11 12:21:30 +0000586 sk_fill_path(path, clipper.getClipRect(), blitter, ir.fTop, ir.fBottom,
587 0, *clipPtr);
reed@google.com55b6b582011-03-02 15:58:18 +0000588 if (path.isInverseFillType()) {
reed@google.com15018032011-07-11 12:21:30 +0000589 sk_blit_below(blitter, ir, *clipPtr);
reed@google.com55b6b582011-03-02 15:58:18 +0000590 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000591 } else {
592 // what does it mean to not have a blitter if path.isInverseFillType???
593 }
594}
595
reed@google.com045e62d2011-10-24 12:19:46 +0000596void SkScan::FillPath(const SkPath& path, const SkIRect& ir,
597 SkBlitter* blitter) {
598 SkRegion rgn(ir);
599 FillPath(path, rgn, blitter);
600}
601
reed@android.com8a1c16f2008-12-17 15:59:43 +0000602///////////////////////////////////////////////////////////////////////////////
603
604static int build_tri_edges(SkEdge edge[], const SkPoint pts[],
605 const SkIRect* clipRect, SkEdge* list[]) {
606 SkEdge** start = list;
reed@google.com55b6b582011-03-02 15:58:18 +0000607
reed@android.com8a1c16f2008-12-17 15:59:43 +0000608 if (edge->setLine(pts[0], pts[1], clipRect, 0)) {
609 *list++ = edge;
610 edge = (SkEdge*)((char*)edge + sizeof(SkEdge));
611 }
612 if (edge->setLine(pts[1], pts[2], clipRect, 0)) {
613 *list++ = edge;
614 edge = (SkEdge*)((char*)edge + sizeof(SkEdge));
615 }
616 if (edge->setLine(pts[2], pts[0], clipRect, 0)) {
617 *list++ = edge;
618 }
619 return (int)(list - start);
620}
621
622
reed@android.com800046e2009-10-14 09:36:25 +0000623static void sk_fill_triangle(const SkPoint pts[], const SkIRect* clipRect,
624 SkBlitter* blitter, const SkIRect& ir) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 SkASSERT(pts && blitter);
reed@google.com55b6b582011-03-02 15:58:18 +0000626
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 SkEdge edgeStorage[3];
628 SkEdge* list[3];
629
630 int count = build_tri_edges(edgeStorage, pts, clipRect, list);
631 if (count < 2) {
632 return;
633 }
634
635 SkEdge headEdge, tailEdge, *last;
636
637 // this returns the first and last edge after they're sorted into a dlink list
638 SkEdge* edge = sort_edges(list, count, &last);
reed@google.com55b6b582011-03-02 15:58:18 +0000639
reed@android.com8a1c16f2008-12-17 15:59:43 +0000640 headEdge.fPrev = NULL;
641 headEdge.fNext = edge;
642 headEdge.fFirstY = kEDGE_HEAD_Y;
643 headEdge.fX = SK_MinS32;
644 edge->fPrev = &headEdge;
reed@google.com55b6b582011-03-02 15:58:18 +0000645
reed@android.com8a1c16f2008-12-17 15:59:43 +0000646 tailEdge.fPrev = last;
647 tailEdge.fNext = NULL;
648 tailEdge.fFirstY = kEDGE_TAIL_Y;
649 last->fNext = &tailEdge;
reed@google.com55b6b582011-03-02 15:58:18 +0000650
reed@android.com8a1c16f2008-12-17 15:59:43 +0000651 // now edge is the head of the sorted linklist
652 int stop_y = ir.fBottom;
653 if (clipRect && stop_y > clipRect->fBottom) {
654 stop_y = clipRect->fBottom;
655 }
reed@android.comdca6a562010-02-22 16:05:48 +0000656 int start_y = ir.fTop;
657 if (clipRect && start_y < clipRect->fTop) {
658 start_y = clipRect->fTop;
659 }
reed@google.coma6c7c482011-10-28 18:58:46 +0000660 walk_convex_edges(&headEdge, SkPath::kEvenOdd_FillType, blitter, start_y, stop_y, NULL);
661// walk_edges(&headEdge, SkPath::kEvenOdd_FillType, blitter, start_y, stop_y, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000662}
663
reed@google.com045e62d2011-10-24 12:19:46 +0000664void SkScan::FillTriangle(const SkPoint pts[], const SkRasterClip& clip,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000665 SkBlitter* blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +0000666 if (clip.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000667 return;
668 }
reed@google.com55b6b582011-03-02 15:58:18 +0000669
reed@android.com8a1c16f2008-12-17 15:59:43 +0000670 SkRect r;
671 SkIRect ir;
672 r.set(pts, 3);
673 r.round(&ir);
reed@google.com045e62d2011-10-24 12:19:46 +0000674 if (ir.isEmpty() || !SkIRect::Intersects(ir, clip.getBounds())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000675 return;
676 }
reed@google.com55b6b582011-03-02 15:58:18 +0000677
reed@google.com045e62d2011-10-24 12:19:46 +0000678 SkAAClipBlitterWrapper wrap;
679 const SkRegion* clipRgn;
680 if (clip.isBW()) {
681 clipRgn = &clip.bwRgn();
682 } else {
683 wrap.init(clip, blitter);
684 clipRgn = &wrap.getRgn();
685 blitter = wrap.getBlitter();
686 }
reed@google.com55b6b582011-03-02 15:58:18 +0000687
reed@google.com045e62d2011-10-24 12:19:46 +0000688 SkScanClipper clipper(blitter, clipRgn, ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000689 blitter = clipper.getBlitter();
690 if (NULL != blitter) {
691 sk_fill_triangle(pts, clipper.getClipRect(), blitter, ir);
692 }
693}
694