blob: 8af0ea6b86a1eeb2787200bf8ec843b7aa17b03c [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"
reed@google.com0a71a9c2012-05-09 12:07:31 +000018#include "SkTSort.h"
19
reed@android.com8a1c16f2008-12-17 15:59:43 +000020#define kEDGE_HEAD_Y SK_MinS32
21#define kEDGE_TAIL_Y SK_MaxS32
22
23#ifdef SK_DEBUG
mike@reedtribe.org99219d32011-04-09 18:54:23 +000024 static void validate_sort(const SkEdge* edge) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 int y = kEDGE_HEAD_Y;
26
mike@reedtribe.org99219d32011-04-09 18:54:23 +000027 while (edge->fFirstY != SK_MaxS32) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000028 edge->validate();
29 SkASSERT(y <= edge->fFirstY);
30
31 y = edge->fFirstY;
32 edge = edge->fNext;
33 }
34 }
35#else
36 #define validate_sort(edge)
37#endif
38
mike@reedtribe.org99219d32011-04-09 18:54:23 +000039static void insert_new_edges(SkEdge* newEdge, int curr_y) {
caryclark2baa84b2016-02-01 04:34:57 -080040 if (newEdge->fFirstY != curr_y) {
41 return;
42 }
43 SkEdge* prev = newEdge->fPrev;
44 if (prev->fX <= newEdge->fX) {
45 return;
46 }
47 // find first x pos to insert
48 SkEdge* start = backward_insert_start(prev, newEdge->fX);
49 // insert the lot, fixing up the links as we go
50 do {
51 SkEdge* next = newEdge->fNext;
52 do {
53 if (start->fNext == newEdge) {
54 goto nextEdge;
55 }
56 SkEdge* after = start->fNext;
57 if (after->fX >= newEdge->fX) {
58 break;
59 }
60 start = after;
61 } while (true);
62 remove_edge(newEdge);
63 insert_edge_after(newEdge, start);
64nextEdge:
65 start = newEdge;
66 newEdge = next;
67 } while (newEdge->fFirstY == curr_y);
reed@android.com8a1c16f2008-12-17 15:59:43 +000068}
69
70#ifdef SK_DEBUG
mike@reedtribe.org99219d32011-04-09 18:54:23 +000071static void validate_edges_for_y(const SkEdge* edge, int curr_y) {
72 while (edge->fFirstY <= curr_y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 SkASSERT(edge->fPrev && edge->fNext);
74 SkASSERT(edge->fPrev->fNext == edge);
75 SkASSERT(edge->fNext->fPrev == edge);
76 SkASSERT(edge->fFirstY <= edge->fLastY);
77
78 SkASSERT(edge->fPrev->fX <= edge->fX);
79 edge = edge->fNext;
80 }
81}
82#else
83 #define validate_edges_for_y(edge, curr_y)
84#endif
85
bungemand7dc76f2016-03-10 11:14:40 -080086#if defined _WIN32 // disable warning : local variable used without having been initialized
reed@android.com8a1c16f2008-12-17 15:59:43 +000087#pragma warning ( push )
88#pragma warning ( disable : 4701 )
89#endif
90
91typedef void (*PrePostProc)(SkBlitter* blitter, int y, bool isStartOfScanline);
92#define PREPOST_START true
93#define PREPOST_END false
94
95static void walk_edges(SkEdge* prevHead, SkPath::FillType fillType,
reed@android.comdca6a562010-02-22 16:05:48 +000096 SkBlitter* blitter, int start_y, int stop_y,
reed01d33192015-02-07 12:18:41 -080097 PrePostProc proc, int rightClip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 validate_sort(prevHead->fNext);
99
reed@android.comdca6a562010-02-22 16:05:48 +0000100 int curr_y = start_y;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 // returns 1 for evenodd, -1 for winding, regardless of inverse-ness
102 int windingMask = (fillType & 1) ? 1 : -1;
103
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000104 for (;;) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 int w = 0;
106 int left SK_INIT_TO_AVOID_WARNING;
107 bool in_interval = false;
108 SkEdge* currE = prevHead->fNext;
109 SkFixed prevX = prevHead->fX;
110
111 validate_edges_for_y(currE, curr_y);
reed@google.com55b6b582011-03-02 15:58:18 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 if (proc) {
114 proc(blitter, curr_y, PREPOST_START); // pre-proc
115 }
reed@google.com55b6b582011-03-02 15:58:18 +0000116
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000117 while (currE->fFirstY <= curr_y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 SkASSERT(currE->fLastY >= curr_y);
119
reed@google.com0a9cc442012-02-06 19:05:51 +0000120 int x = SkFixedRoundToInt(currE->fX);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121 w += currE->fWinding;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000122 if ((w & windingMask) == 0) { // we finished an interval
reed@android.com8a1c16f2008-12-17 15:59:43 +0000123 SkASSERT(in_interval);
124 int width = x - left;
125 SkASSERT(width >= 0);
126 if (width)
127 blitter->blitH(left, curr_y, width);
128 in_interval = false;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000129 } else if (!in_interval) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 left = x;
131 in_interval = true;
132 }
133
134 SkEdge* next = currE->fNext;
135 SkFixed newX;
136
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000137 if (currE->fLastY == curr_y) { // are we done with this edge?
138 if (currE->fCurveCount < 0) {
139 if (((SkCubicEdge*)currE)->updateCubic()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000140 SkASSERT(currE->fFirstY == curr_y + 1);
reed@google.com55b6b582011-03-02 15:58:18 +0000141
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 newX = currE->fX;
143 goto NEXT_X;
144 }
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000145 } else if (currE->fCurveCount > 0) {
146 if (((SkQuadraticEdge*)currE)->updateQuadratic()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 newX = currE->fX;
148 goto NEXT_X;
149 }
150 }
151 remove_edge(currE);
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000152 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 SkASSERT(currE->fLastY > curr_y);
154 newX = currE->fX + currE->fDX;
155 currE->fX = newX;
156 NEXT_X:
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000157 if (newX < prevX) { // ripple currE backwards until it is x-sorted
Yuqian Lia33b43d2017-03-17 11:26:29 -0400158 backward_insert_edge_based_on_x(currE);
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000159 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000160 prevX = newX;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000161 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162 }
163 currE = next;
164 SkASSERT(currE);
165 }
reed@google.com55b6b582011-03-02 15:58:18 +0000166
reed01d33192015-02-07 12:18:41 -0800167 // was our right-edge culled away?
168 if (in_interval) {
169 int width = rightClip - left;
170 if (width > 0) {
171 blitter->blitH(left, curr_y, width);
172 }
173 }
174
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 if (proc) {
176 proc(blitter, curr_y, PREPOST_END); // post-proc
177 }
178
179 curr_y += 1;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000180 if (curr_y >= stop_y) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000181 break;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000182 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 // now currE points to the first edge with a Yint larger than curr_y
184 insert_new_edges(currE, curr_y);
185 }
186}
187
Yuqian Li7877d322017-07-18 10:11:05 -0400188// return true if we're NOT done with this edge
reed@google.come3a83ec2011-10-25 19:27:43 +0000189static bool update_edge(SkEdge* edge, int last_y) {
190 SkASSERT(edge->fLastY >= last_y);
191 if (last_y == edge->fLastY) {
192 if (edge->fCurveCount < 0) {
193 if (((SkCubicEdge*)edge)->updateCubic()) {
194 SkASSERT(edge->fFirstY == last_y + 1);
Yuqian Li7877d322017-07-18 10:11:05 -0400195 return true;
reed@google.come3a83ec2011-10-25 19:27:43 +0000196 }
197 } else if (edge->fCurveCount > 0) {
198 if (((SkQuadraticEdge*)edge)->updateQuadratic()) {
199 SkASSERT(edge->fFirstY == last_y + 1);
Yuqian Li7877d322017-07-18 10:11:05 -0400200 return true;
reed@google.come3a83ec2011-10-25 19:27:43 +0000201 }
202 }
Yuqian Li7877d322017-07-18 10:11:05 -0400203 return false;
reed@google.come3a83ec2011-10-25 19:27:43 +0000204 }
Yuqian Li7877d322017-07-18 10:11:05 -0400205 return true;
reed@google.come3a83ec2011-10-25 19:27:43 +0000206}
207
208static void walk_convex_edges(SkEdge* prevHead, SkPath::FillType,
209 SkBlitter* blitter, int start_y, int stop_y,
210 PrePostProc proc) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000211 validate_sort(prevHead->fNext);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000212
reed@google.come3a83ec2011-10-25 19:27:43 +0000213 SkEdge* leftE = prevHead->fNext;
214 SkEdge* riteE = leftE->fNext;
215 SkEdge* currE = riteE->fNext;
mike@reedtribe.orgfc5da922011-10-31 02:58:27 +0000216
217#if 0
reed@google.come3a83ec2011-10-25 19:27:43 +0000218 int local_top = leftE->fFirstY;
219 SkASSERT(local_top == riteE->fFirstY);
mike@reedtribe.orgfc5da922011-10-31 02:58:27 +0000220#else
221 // our edge choppers for curves can result in the initial edges
222 // not lining up, so we take the max.
223 int local_top = SkMax32(leftE->fFirstY, riteE->fFirstY);
224#endif
reed@google.come3a83ec2011-10-25 19:27:43 +0000225 SkASSERT(local_top >= start_y);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000226
reed@google.come3a83ec2011-10-25 19:27:43 +0000227 for (;;) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000228 SkASSERT(leftE->fFirstY <= stop_y);
229 SkASSERT(riteE->fFirstY <= stop_y);
230
231 if (leftE->fX > riteE->fX || (leftE->fX == riteE->fX &&
232 leftE->fDX > riteE->fDX)) {
233 SkTSwap(leftE, riteE);
234 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000235
reed@google.come3a83ec2011-10-25 19:27:43 +0000236 int local_bot = SkMin32(leftE->fLastY, riteE->fLastY);
reed@google.coma6c7c482011-10-28 18:58:46 +0000237 local_bot = SkMin32(local_bot, stop_y - 1);
reed@google.come3a83ec2011-10-25 19:27:43 +0000238 SkASSERT(local_top <= local_bot);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000239
reed@google.come3a83ec2011-10-25 19:27:43 +0000240 SkFixed left = leftE->fX;
241 SkFixed dLeft = leftE->fDX;
242 SkFixed rite = riteE->fX;
243 SkFixed dRite = riteE->fDX;
244 int count = local_bot - local_top;
245 SkASSERT(count >= 0);
reed@google.com562a2ac2011-10-31 14:14:18 +0000246 if (0 == (dLeft | dRite)) {
reed@google.com0a9cc442012-02-06 19:05:51 +0000247 int L = SkFixedRoundToInt(left);
248 int R = SkFixedRoundToInt(rite);
reed@google.come3a83ec2011-10-25 19:27:43 +0000249 if (L < R) {
250 count += 1;
251 blitter->blitRect(L, local_top, R - L, count);
reed@google.come3a83ec2011-10-25 19:27:43 +0000252 }
mike@reedtribe.orgfc5da922011-10-31 02:58:27 +0000253 local_top = local_bot + 1;
reed@google.come3a83ec2011-10-25 19:27:43 +0000254 } else {
255 do {
reed@google.com0a9cc442012-02-06 19:05:51 +0000256 int L = SkFixedRoundToInt(left);
257 int R = SkFixedRoundToInt(rite);
reed@google.come3a83ec2011-10-25 19:27:43 +0000258 if (L < R) {
259 blitter->blitH(L, local_top, R - L);
260 }
261 left += dLeft;
262 rite += dRite;
263 local_top += 1;
264 } while (--count >= 0);
265 }
266
267 leftE->fX = left;
268 riteE->fX = rite;
269
Yuqian Li7877d322017-07-18 10:11:05 -0400270 if (!update_edge(leftE, local_bot)) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000271 if (currE->fFirstY >= stop_y) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000272 break;
273 }
274 leftE = currE;
275 currE = currE->fNext;
276 }
Yuqian Li7877d322017-07-18 10:11:05 -0400277 if (!update_edge(riteE, local_bot)) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000278 if (currE->fFirstY >= stop_y) {
reed@google.come3a83ec2011-10-25 19:27:43 +0000279 break;
280 }
281 riteE = currE;
282 currE = currE->fNext;
283 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000284
reed@google.come3a83ec2011-10-25 19:27:43 +0000285 SkASSERT(leftE);
286 SkASSERT(riteE);
reed@google.coma6c7c482011-10-28 18:58:46 +0000287
288 // check our bottom clip
289 SkASSERT(local_top == local_bot + 1);
290 if (local_top >= stop_y) {
291 break;
292 }
reed@google.come3a83ec2011-10-25 19:27:43 +0000293 }
294}
295
reed@android.com8a1c16f2008-12-17 15:59:43 +0000296///////////////////////////////////////////////////////////////////////////////
297
298// this guy overrides blitH, and will call its proxy blitter with the inverse
299// of the spans it is given (clipped to the left/right of the cliprect)
300//
301// used to implement inverse filltypes on paths
302//
303class InverseBlitter : public SkBlitter {
304public:
305 void setBlitter(SkBlitter* blitter, const SkIRect& clip, int shift) {
306 fBlitter = blitter;
307 fFirstX = clip.fLeft << shift;
308 fLastX = clip.fRight << shift;
309 }
310 void prepost(int y, bool isStart) {
311 if (isStart) {
312 fPrevX = fFirstX;
313 } else {
314 int invWidth = fLastX - fPrevX;
315 if (invWidth > 0) {
316 fBlitter->blitH(fPrevX, y, invWidth);
317 }
318 }
319 }
320
321 // overrides
mtklein36352bf2015-03-25 18:17:31 -0700322 void blitH(int x, int y, int width) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323 int invWidth = x - fPrevX;
324 if (invWidth > 0) {
325 fBlitter->blitH(fPrevX, y, invWidth);
326 }
327 fPrevX = x + width;
328 }
reed@google.com55b6b582011-03-02 15:58:18 +0000329
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330 // we do not expect to get called with these entrypoints
mtklein36352bf2015-03-25 18:17:31 -0700331 void blitAntiH(int, int, const SkAlpha[], const int16_t runs[]) override {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000332 SkDEBUGFAIL("blitAntiH unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000333 }
mtklein36352bf2015-03-25 18:17:31 -0700334 void blitV(int x, int y, int height, SkAlpha alpha) override {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000335 SkDEBUGFAIL("blitV unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000336 }
mtklein36352bf2015-03-25 18:17:31 -0700337 void blitRect(int x, int y, int width, int height) override {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000338 SkDEBUGFAIL("blitRect unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000339 }
mtklein36352bf2015-03-25 18:17:31 -0700340 void blitMask(const SkMask&, const SkIRect& clip) override {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000341 SkDEBUGFAIL("blitMask unexpected");
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 }
reed41e010c2015-06-09 12:16:53 -0700343 const SkPixmap* justAnOpaqueColor(uint32_t* value) override {
tomhudson@google.com0c00f212011-12-28 14:59:50 +0000344 SkDEBUGFAIL("justAnOpaqueColor unexpected");
halcanary96fcdcc2015-08-27 07:41:13 -0700345 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000346 }
reed@google.com55b6b582011-03-02 15:58:18 +0000347
reed@android.com8a1c16f2008-12-17 15:59:43 +0000348private:
349 SkBlitter* fBlitter;
350 int fFirstX, fLastX, fPrevX;
351};
352
353static void PrePostInverseBlitterProc(SkBlitter* blitter, int y, bool isStart) {
354 ((InverseBlitter*)blitter)->prepost(y, isStart);
355}
356
357///////////////////////////////////////////////////////////////////////////////
358
bungemand7dc76f2016-03-10 11:14:40 -0800359#if defined _WIN32
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360#pragma warning ( pop )
361#endif
362
reed@google.com0a71a9c2012-05-09 12:07:31 +0000363static bool operator<(const SkEdge& a, const SkEdge& b) {
364 int valuea = a.fFirstY;
365 int valueb = b.fFirstY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000366
reed@google.com0a71a9c2012-05-09 12:07:31 +0000367 if (valuea == valueb) {
368 valuea = a.fX;
369 valueb = b.fX;
370 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000371
reed@google.com0a71a9c2012-05-09 12:07:31 +0000372 return valuea < valueb;
373}
reed@android.come72fee52009-11-16 14:52:01 +0000374
375static SkEdge* sort_edges(SkEdge* list[], int count, SkEdge** last) {
reed@google.com0a71a9c2012-05-09 12:07:31 +0000376 SkTQSort(list, list + count - 1);
reed@google.com55b6b582011-03-02 15:58:18 +0000377
reed@android.come72fee52009-11-16 14:52:01 +0000378 // now make the edges linked in sorted order
379 for (int i = 1; i < count; i++) {
380 list[i - 1]->fNext = list[i];
381 list[i]->fPrev = list[i - 1];
382 }
reed@google.com55b6b582011-03-02 15:58:18 +0000383
reed@android.come72fee52009-11-16 14:52:01 +0000384 *last = list[count - 1];
385 return list[0];
386}
387
Yuqian Lie4b8b522016-11-16 10:12:58 -0500388// clipRect has not been shifted up
389void sk_fill_path(const SkPath& path, const SkIRect& clipRect, SkBlitter* blitter,
390 int start_y, int stop_y, int shiftEdgesUp, bool pathContainedInClip) {
caryclarka10742c2014-09-18 11:00:40 -0700391 SkASSERT(blitter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000392
Yuqian Lie4b8b522016-11-16 10:12:58 -0500393 SkIRect shiftedClip = clipRect;
394 shiftedClip.fLeft <<= shiftEdgesUp;
395 shiftedClip.fRight <<= shiftEdgesUp;
396 shiftedClip.fTop <<= shiftEdgesUp;
397 shiftedClip.fBottom <<= shiftEdgesUp;
398
Yuqian Li1c840882017-05-26 14:50:35 -0400399 SkEdgeBuilder builder;
400 int count = builder.build_edges(path, &shiftedClip, shiftEdgesUp, pathContainedInClip);
401 SkEdge** list = builder.edgeList();
reed@android.com909994f2009-11-18 16:09:51 +0000402
reed31223e02015-02-09 08:33:07 -0800403 if (0 == count) {
agl@chromium.org8cfdf012010-12-06 18:52:40 +0000404 if (path.isInverseFillType()) {
reed@google.com9d5f76a2012-05-01 14:49:28 +0000405 /*
406 * Since we are in inverse-fill, our caller has already drawn above
407 * our top (start_y) and will draw below our bottom (stop_y). Thus
408 * we need to restrict our drawing to the intersection of the clip
409 * and those two limits.
410 */
Yuqian Lie4b8b522016-11-16 10:12:58 -0500411 SkIRect rect = clipRect;
reed@google.com9d5f76a2012-05-01 14:49:28 +0000412 if (rect.fTop < start_y) {
413 rect.fTop = start_y;
414 }
415 if (rect.fBottom > stop_y) {
416 rect.fBottom = stop_y;
417 }
418 if (!rect.isEmpty()) {
419 blitter->blitRect(rect.fLeft << shiftEdgesUp,
420 rect.fTop << shiftEdgesUp,
421 rect.width() << shiftEdgesUp,
422 rect.height() << shiftEdgesUp);
423 }
agl@chromium.org8cfdf012010-12-06 18:52:40 +0000424 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 return;
426 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000427
reed@android.com909994f2009-11-18 16:09:51 +0000428 SkEdge headEdge, tailEdge, *last;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 // this returns the first and last edge after they're sorted into a dlink list
reed@android.com909994f2009-11-18 16:09:51 +0000430 SkEdge* edge = sort_edges(list, count, &last);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000431
halcanary96fcdcc2015-08-27 07:41:13 -0700432 headEdge.fPrev = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000433 headEdge.fNext = edge;
434 headEdge.fFirstY = kEDGE_HEAD_Y;
435 headEdge.fX = SK_MinS32;
436 edge->fPrev = &headEdge;
437
438 tailEdge.fPrev = last;
halcanary96fcdcc2015-08-27 07:41:13 -0700439 tailEdge.fNext = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000440 tailEdge.fFirstY = kEDGE_TAIL_Y;
441 last->fNext = &tailEdge;
442
443 // now edge is the head of the sorted linklist
444
caryclark3127c992015-12-09 12:02:30 -0800445 start_y = SkLeftShift(start_y, shiftEdgesUp);
446 stop_y = SkLeftShift(stop_y, shiftEdgesUp);
Yuqian Lie4b8b522016-11-16 10:12:58 -0500447 if (!pathContainedInClip && start_y < shiftedClip.fTop) {
448 start_y = shiftedClip.fTop;
reed@android.comdca6a562010-02-22 16:05:48 +0000449 }
Yuqian Lie4b8b522016-11-16 10:12:58 -0500450 if (!pathContainedInClip && stop_y > shiftedClip.fBottom) {
451 stop_y = shiftedClip.fBottom;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452 }
453
454 InverseBlitter ib;
halcanary96fcdcc2015-08-27 07:41:13 -0700455 PrePostProc proc = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456
457 if (path.isInverseFillType()) {
Yuqian Lie4b8b522016-11-16 10:12:58 -0500458 ib.setBlitter(blitter, clipRect, shiftEdgesUp);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000459 blitter = &ib;
460 proc = PrePostInverseBlitterProc;
461 }
462
Yuqian Li1c840882017-05-26 14:50:35 -0400463 // count >= 2 is required as the convex walker does not handle missing right edges
464 if (path.isConvex() && (nullptr == proc) && count >= 2) {
halcanary96fcdcc2015-08-27 07:41:13 -0700465 walk_convex_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, nullptr);
reed@google.come3a83ec2011-10-25 19:27:43 +0000466 } else {
Yuqian Lie4b8b522016-11-16 10:12:58 -0500467 walk_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, proc,
468 shiftedClip.right());
reed@google.come3a83ec2011-10-25 19:27:43 +0000469 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000470}
471
reed@google.com55b6b582011-03-02 15:58:18 +0000472void sk_blit_above(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000473 const SkIRect& cr = clip.getBounds();
474 SkIRect tmp;
reed@google.com55b6b582011-03-02 15:58:18 +0000475
reed@android.com8a1c16f2008-12-17 15:59:43 +0000476 tmp.fLeft = cr.fLeft;
477 tmp.fRight = cr.fRight;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 tmp.fTop = cr.fTop;
479 tmp.fBottom = ir.fTop;
480 if (!tmp.isEmpty()) {
481 blitter->blitRectRegion(tmp, clip);
482 }
reed@google.com55b6b582011-03-02 15:58:18 +0000483}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000484
reed@google.com55b6b582011-03-02 15:58:18 +0000485void sk_blit_below(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) {
486 const SkIRect& cr = clip.getBounds();
487 SkIRect tmp;
488
489 tmp.fLeft = cr.fLeft;
490 tmp.fRight = cr.fRight;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000491 tmp.fTop = ir.fBottom;
492 tmp.fBottom = cr.fBottom;
493 if (!tmp.isEmpty()) {
494 blitter->blitRectRegion(tmp, clip);
495 }
496}
497
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000498///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499
reed@google.com97caebc2012-06-08 16:30:12 +0000500/**
reed@google.com5ee64912012-06-11 17:30:33 +0000501 * If the caller is drawing an inverse-fill path, then it pass true for
502 * skipRejectTest, so we don't abort drawing just because the src bounds (ir)
503 * is outside of the clip.
reed@google.com97caebc2012-06-08 16:30:12 +0000504 */
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000505SkScanClipper::SkScanClipper(SkBlitter* blitter, const SkRegion* clip,
reed@google.com5ee64912012-06-11 17:30:33 +0000506 const SkIRect& ir, bool skipRejectTest) {
halcanary96fcdcc2015-08-27 07:41:13 -0700507 fBlitter = nullptr; // null means blit nothing
508 fClipRect = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000509
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000510 if (clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000511 fClipRect = &clip->getBounds();
reed@google.com5ee64912012-06-11 17:30:33 +0000512 if (!skipRejectTest && !SkIRect::Intersects(*fClipRect, ir)) { // completely clipped out
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 return;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000514 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000515
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000516 if (clip->isRect()) {
517 if (fClipRect->contains(ir)) {
Mike Reed28930b42016-11-09 15:23:26 -0500518#ifdef SK_DEBUG
Yuqian Li99bba9e2016-11-21 09:44:59 -0500519 fRectClipCheckBlitter.init(blitter, *fClipRect);
520 blitter = &fRectClipCheckBlitter;
Mike Reed28930b42016-11-09 15:23:26 -0500521#endif
halcanary96fcdcc2015-08-27 07:41:13 -0700522 fClipRect = nullptr;
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000523 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524 // only need a wrapper blitter if we're horizontally clipped
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000525 if (fClipRect->fLeft > ir.fLeft || fClipRect->fRight < ir.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000526 fRectBlitter.init(blitter, *fClipRect);
527 blitter = &fRectBlitter;
Mike Reed28930b42016-11-09 15:23:26 -0500528 } else {
529#ifdef SK_DEBUG
Yuqian Li99bba9e2016-11-21 09:44:59 -0500530 fRectClipCheckBlitter.init(blitter, *fClipRect);
531 blitter = &fRectClipCheckBlitter;
Mike Reed28930b42016-11-09 15:23:26 -0500532#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000533 }
534 }
mike@reedtribe.org99219d32011-04-09 18:54:23 +0000535 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 fRgnBlitter.init(blitter, clip);
537 blitter = &fRgnBlitter;
538 }
539 }
540 fBlitter = blitter;
541}
542
543///////////////////////////////////////////////////////////////////////////////
544
reed@google.com15018032011-07-11 12:21:30 +0000545static bool clip_to_limit(const SkRegion& orig, SkRegion* reduced) {
546 const int32_t limit = 32767;
547
548 SkIRect limitR;
549 limitR.set(-limit, -limit, limit, limit);
550 if (limitR.contains(orig.getBounds())) {
551 return false;
552 }
553 reduced->op(orig, limitR, SkRegion::kIntersect_Op);
554 return true;
555}
556
caryclark6df61152016-01-04 14:17:47 -0800557/**
Lee Salzmanb8e75212017-03-14 14:52:51 -0400558 * Variants of SkScalarRoundToInt, identical to SkDScalarRoundToInt except when the input fraction
559 * is 0.5. When SK_RASTERIZE_EVEN_ROUNDING is enabled, we must bias the result before rounding to
560 * account for potential FDot6 rounding edge-cases.
561 */
562#ifdef SK_RASTERIZE_EVEN_ROUNDING
563static const double kRoundBias = 0.5 / SK_FDot6One;
564#else
565static const double kRoundBias = 0.0;
566#endif
567
568/**
569 * Round the value down. This is used to round the top and left of a rectangle,
570 * and corresponds to the way the scan converter treats the top and left edges.
caryclark6df61152016-01-04 14:17:47 -0800571 */
572static inline int round_down_to_int(SkScalar x) {
573 double xx = x;
Lee Salzmanb8e75212017-03-14 14:52:51 -0400574 xx -= 0.5 + kRoundBias;
Lee Salzmanb5690562017-01-13 12:03:24 -0500575 return (int)ceil(xx);
caryclark6df61152016-01-04 14:17:47 -0800576}
577
Lee Salzman95acbad2017-01-13 13:35:49 -0500578/**
Lee Salzmanb8e75212017-03-14 14:52:51 -0400579 * Round the value up. This is used to round the bottom and right of a rectangle,
580 * and corresponds to the way the scan converter treats the bottom and right edges.
Lee Salzman95acbad2017-01-13 13:35:49 -0500581 */
Lee Salzmanb8e75212017-03-14 14:52:51 -0400582static inline int round_up_to_int(SkScalar x) {
Lee Salzman95acbad2017-01-13 13:35:49 -0500583 double xx = x;
Lee Salzmanb8e75212017-03-14 14:52:51 -0400584 xx += 0.5 + kRoundBias;
Lee Salzman95acbad2017-01-13 13:35:49 -0500585 return (int)floor(xx);
586}
Lee Salzman95acbad2017-01-13 13:35:49 -0500587
caryclark6df61152016-01-04 14:17:47 -0800588/**
589 * Variant of SkRect::round() that explicitly performs the rounding step (i.e. floor(x + 0.5))
590 * using double instead of SkScalar (float). It does this by calling SkDScalarRoundToInt(),
591 * which may be slower than calling SkScalarRountToInt(), but gives slightly more accurate
592 * results. Also rounds top and left using double, flooring when the fraction is exactly 0.5f.
593 *
594 * e.g.
595 * SkScalar left = 0.5f;
596 * int ileft = SkScalarRoundToInt(left);
597 * SkASSERT(0 == ileft); // <--- fails
598 * int ileft = round_down_to_int(left);
599 * SkASSERT(0 == ileft); // <--- succeeds
600 * SkScalar right = 0.49999997f;
601 * int iright = SkScalarRoundToInt(right);
602 * SkASSERT(0 == iright); // <--- fails
603 * iright = SkDScalarRoundToInt(right);
604 * SkASSERT(0 == iright); // <--- succeeds
Lee Salzman95acbad2017-01-13 13:35:49 -0500605 *
606 *
Lee Salzmanb8e75212017-03-14 14:52:51 -0400607 * If using SK_RASTERIZE_EVEN_ROUNDING, we need to ensure we account for edges bounded by this
608 * rect being rounded to FDot6 format before being later rounded to an integer. For example, a
609 * value like 0.499 can be below 0.5, but round to 0.5 as FDot6, which would finally round to
610 * the integer 1, instead of just rounding to 0.
Lee Salzman95acbad2017-01-13 13:35:49 -0500611 *
612 * To handle this, a small bias of half an FDot6 increment is added before actually rounding to
613 * an integer value. This simulates the rounding of SkScalarRoundToFDot6 without incurring the
614 * range loss of converting to FDot6 format first, preserving the integer range for the SkIRect.
615 * Thus, bottom and right are rounded in this manner (biased up), ensuring the rect is large
Lee Salzmanb8e75212017-03-14 14:52:51 -0400616 * enough.
caryclark6df61152016-01-04 14:17:47 -0800617 */
618static void round_asymmetric_to_int(const SkRect& src, SkIRect* dst) {
619 SkASSERT(dst);
620 dst->set(round_down_to_int(src.fLeft), round_down_to_int(src.fTop),
Lee Salzmanb8e75212017-03-14 14:52:51 -0400621 round_up_to_int(src.fRight), round_up_to_int(src.fBottom));
caryclark6df61152016-01-04 14:17:47 -0800622}
623
reed@google.com15018032011-07-11 12:21:30 +0000624void SkScan::FillPath(const SkPath& path, const SkRegion& origClip,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000625 SkBlitter* blitter) {
reed@google.com15018032011-07-11 12:21:30 +0000626 if (origClip.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000627 return;
628 }
629
reed@google.com15018032011-07-11 12:21:30 +0000630 // Our edges are fixed-point, and don't like the bounds of the clip to
631 // exceed that. Here we trim the clip just so we don't overflow later on
632 const SkRegion* clipPtr = &origClip;
633 SkRegion finiteClip;
634 if (clip_to_limit(origClip, &finiteClip)) {
635 if (finiteClip.isEmpty()) {
636 return;
637 }
638 clipPtr = &finiteClip;
639 }
640 // don't reference "origClip" any more, just use clipPtr
641
reed@android.com8a1c16f2008-12-17 15:59:43 +0000642 SkIRect ir;
caryclark6df61152016-01-04 14:17:47 -0800643 // We deliberately call round_asymmetric_to_int() instead of round(), since we can't afford
644 // to generate a bounds that is tighter than the corresponding SkEdges. The edge code basically
645 // converts the floats to fixed, and then "rounds". If we called round() instead of
646 // round_asymmetric_to_int() here, we could generate the wrong ir for values like 0.4999997.
647 round_asymmetric_to_int(path.getBounds(), &ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000648 if (ir.isEmpty()) {
649 if (path.isInverseFillType()) {
reed@google.com15018032011-07-11 12:21:30 +0000650 blitter->blitRegion(*clipPtr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000651 }
652 return;
653 }
654
reed@google.com5ee64912012-06-11 17:30:33 +0000655 SkScanClipper clipper(blitter, clipPtr, ir, path.isInverseFillType());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000656
657 blitter = clipper.getBlitter();
658 if (blitter) {
reed@google.com55b6b582011-03-02 15:58:18 +0000659 // we have to keep our calls to blitter in sorted order, so we
660 // must blit the above section first, then the middle, then the bottom.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000661 if (path.isInverseFillType()) {
reed@google.com15018032011-07-11 12:21:30 +0000662 sk_blit_above(blitter, ir, *clipPtr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663 }
Yuqian Lie4b8b522016-11-16 10:12:58 -0500664 SkASSERT(clipper.getClipRect() == nullptr ||
665 *clipper.getClipRect() == clipPtr->getBounds());
666 sk_fill_path(path, clipPtr->getBounds(), blitter, ir.fTop, ir.fBottom,
667 0, clipper.getClipRect() == nullptr);
reed@google.com55b6b582011-03-02 15:58:18 +0000668 if (path.isInverseFillType()) {
reed@google.com15018032011-07-11 12:21:30 +0000669 sk_blit_below(blitter, ir, *clipPtr);
reed@google.com55b6b582011-03-02 15:58:18 +0000670 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000671 } else {
672 // what does it mean to not have a blitter if path.isInverseFillType???
673 }
674}
675
reed@google.com045e62d2011-10-24 12:19:46 +0000676void SkScan::FillPath(const SkPath& path, const SkIRect& ir,
677 SkBlitter* blitter) {
678 SkRegion rgn(ir);
679 FillPath(path, rgn, blitter);
680}
681
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682///////////////////////////////////////////////////////////////////////////////
683
684static int build_tri_edges(SkEdge edge[], const SkPoint pts[],
685 const SkIRect* clipRect, SkEdge* list[]) {
686 SkEdge** start = list;
reed@google.com55b6b582011-03-02 15:58:18 +0000687
reed@android.com8a1c16f2008-12-17 15:59:43 +0000688 if (edge->setLine(pts[0], pts[1], clipRect, 0)) {
689 *list++ = edge;
690 edge = (SkEdge*)((char*)edge + sizeof(SkEdge));
691 }
692 if (edge->setLine(pts[1], pts[2], clipRect, 0)) {
693 *list++ = edge;
694 edge = (SkEdge*)((char*)edge + sizeof(SkEdge));
695 }
696 if (edge->setLine(pts[2], pts[0], clipRect, 0)) {
697 *list++ = edge;
698 }
699 return (int)(list - start);
700}
701
702
reed@android.com800046e2009-10-14 09:36:25 +0000703static void sk_fill_triangle(const SkPoint pts[], const SkIRect* clipRect,
704 SkBlitter* blitter, const SkIRect& ir) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000705 SkASSERT(pts && blitter);
reed@google.com55b6b582011-03-02 15:58:18 +0000706
reed@android.com8a1c16f2008-12-17 15:59:43 +0000707 SkEdge edgeStorage[3];
708 SkEdge* list[3];
709
710 int count = build_tri_edges(edgeStorage, pts, clipRect, list);
711 if (count < 2) {
712 return;
713 }
714
715 SkEdge headEdge, tailEdge, *last;
716
717 // this returns the first and last edge after they're sorted into a dlink list
718 SkEdge* edge = sort_edges(list, count, &last);
reed@google.com55b6b582011-03-02 15:58:18 +0000719
halcanary96fcdcc2015-08-27 07:41:13 -0700720 headEdge.fPrev = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000721 headEdge.fNext = edge;
722 headEdge.fFirstY = kEDGE_HEAD_Y;
723 headEdge.fX = SK_MinS32;
724 edge->fPrev = &headEdge;
reed@google.com55b6b582011-03-02 15:58:18 +0000725
reed@android.com8a1c16f2008-12-17 15:59:43 +0000726 tailEdge.fPrev = last;
halcanary96fcdcc2015-08-27 07:41:13 -0700727 tailEdge.fNext = nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000728 tailEdge.fFirstY = kEDGE_TAIL_Y;
729 last->fNext = &tailEdge;
reed@google.com55b6b582011-03-02 15:58:18 +0000730
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731 // now edge is the head of the sorted linklist
732 int stop_y = ir.fBottom;
733 if (clipRect && stop_y > clipRect->fBottom) {
734 stop_y = clipRect->fBottom;
735 }
reed@android.comdca6a562010-02-22 16:05:48 +0000736 int start_y = ir.fTop;
737 if (clipRect && start_y < clipRect->fTop) {
738 start_y = clipRect->fTop;
739 }
halcanary96fcdcc2015-08-27 07:41:13 -0700740 walk_convex_edges(&headEdge, SkPath::kEvenOdd_FillType, blitter, start_y, stop_y, nullptr);
741// walk_edges(&headEdge, SkPath::kEvenOdd_FillType, blitter, start_y, stop_y, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000742}
743
reed@google.com045e62d2011-10-24 12:19:46 +0000744void SkScan::FillTriangle(const SkPoint pts[], const SkRasterClip& clip,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000745 SkBlitter* blitter) {
reed@google.com045e62d2011-10-24 12:19:46 +0000746 if (clip.isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747 return;
748 }
reed@google.com55b6b582011-03-02 15:58:18 +0000749
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750 SkRect r;
751 SkIRect ir;
752 r.set(pts, 3);
753 r.round(&ir);
reed@google.com045e62d2011-10-24 12:19:46 +0000754 if (ir.isEmpty() || !SkIRect::Intersects(ir, clip.getBounds())) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000755 return;
756 }
reed@google.com55b6b582011-03-02 15:58:18 +0000757
reed@google.com045e62d2011-10-24 12:19:46 +0000758 SkAAClipBlitterWrapper wrap;
759 const SkRegion* clipRgn;
760 if (clip.isBW()) {
761 clipRgn = &clip.bwRgn();
762 } else {
763 wrap.init(clip, blitter);
764 clipRgn = &wrap.getRgn();
765 blitter = wrap.getBlitter();
766 }
reed@google.com55b6b582011-03-02 15:58:18 +0000767
reed@google.com045e62d2011-10-24 12:19:46 +0000768 SkScanClipper clipper(blitter, clipRgn, ir);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000769 blitter = clipper.getBlitter();
bsalomon49f085d2014-09-05 13:34:00 -0700770 if (blitter) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000771 sk_fill_triangle(pts, clipper.getClipRect(), blitter, ir);
772 }
773}