epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkScanPriv.h" |
| 9 | #include "SkBlitter.h" |
| 10 | #include "SkEdge.h" |
mike@reedtribe.org | 948639b | 2011-07-19 01:28:17 +0000 | [diff] [blame] | 11 | #include "SkEdgeBuilder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkGeometry.h" |
| 13 | #include "SkPath.h" |
reed@android.com | c07d23a | 2009-02-06 13:30:58 +0000 | [diff] [blame] | 14 | #include "SkQuadClipper.h" |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 15 | #include "SkRasterClip.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkRegion.h" |
| 17 | #include "SkTemplates.h" |
reed@google.com | 0a71a9c | 2012-05-09 12:07:31 +0000 | [diff] [blame] | 18 | #include "SkTSort.h" |
| 19 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #define kEDGE_HEAD_Y SK_MinS32 |
| 21 | #define kEDGE_TAIL_Y SK_MaxS32 |
| 22 | |
| 23 | #ifdef SK_DEBUG |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 24 | static void validate_sort(const SkEdge* edge) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | int y = kEDGE_HEAD_Y; |
| 26 | |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 27 | while (edge->fFirstY != SK_MaxS32) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | 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.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 39 | static void insert_new_edges(SkEdge* newEdge, int curr_y) { |
caryclark | 2baa84b | 2016-02-01 04:34:57 -0800 | [diff] [blame] | 40 | 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); |
| 64 | nextEdge: |
| 65 | start = newEdge; |
| 66 | newEdge = next; |
| 67 | } while (newEdge->fFirstY == curr_y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | #ifdef SK_DEBUG |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 71 | static void validate_edges_for_y(const SkEdge* edge, int curr_y) { |
| 72 | while (edge->fFirstY <= curr_y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | 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 | |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 86 | #if defined _WIN32 // disable warning : local variable used without having been initialized |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | #pragma warning ( push ) |
| 88 | #pragma warning ( disable : 4701 ) |
| 89 | #endif |
| 90 | |
| 91 | typedef void (*PrePostProc)(SkBlitter* blitter, int y, bool isStartOfScanline); |
| 92 | #define PREPOST_START true |
| 93 | #define PREPOST_END false |
| 94 | |
| 95 | static void walk_edges(SkEdge* prevHead, SkPath::FillType fillType, |
reed@android.com | dca6a56 | 2010-02-22 16:05:48 +0000 | [diff] [blame] | 96 | SkBlitter* blitter, int start_y, int stop_y, |
reed | 01d3319 | 2015-02-07 12:18:41 -0800 | [diff] [blame] | 97 | PrePostProc proc, int rightClip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | validate_sort(prevHead->fNext); |
| 99 | |
reed@android.com | dca6a56 | 2010-02-22 16:05:48 +0000 | [diff] [blame] | 100 | int curr_y = start_y; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | // returns 1 for evenodd, -1 for winding, regardless of inverse-ness |
| 102 | int windingMask = (fillType & 1) ? 1 : -1; |
| 103 | |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 104 | for (;;) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | 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.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | if (proc) { |
| 114 | proc(blitter, curr_y, PREPOST_START); // pre-proc |
| 115 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 116 | |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 117 | while (currE->fFirstY <= curr_y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | SkASSERT(currE->fLastY >= curr_y); |
| 119 | |
reed@google.com | 0a9cc44 | 2012-02-06 19:05:51 +0000 | [diff] [blame] | 120 | int x = SkFixedRoundToInt(currE->fX); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | w += currE->fWinding; |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 122 | if ((w & windingMask) == 0) { // we finished an interval |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 123 | 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.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 129 | } else if (!in_interval) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | left = x; |
| 131 | in_interval = true; |
| 132 | } |
| 133 | |
| 134 | SkEdge* next = currE->fNext; |
| 135 | SkFixed newX; |
| 136 | |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 137 | if (currE->fLastY == curr_y) { // are we done with this edge? |
| 138 | if (currE->fCurveCount < 0) { |
| 139 | if (((SkCubicEdge*)currE)->updateCubic()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | SkASSERT(currE->fFirstY == curr_y + 1); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 141 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | newX = currE->fX; |
| 143 | goto NEXT_X; |
| 144 | } |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 145 | } else if (currE->fCurveCount > 0) { |
| 146 | if (((SkQuadraticEdge*)currE)->updateQuadratic()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | newX = currE->fX; |
| 148 | goto NEXT_X; |
| 149 | } |
| 150 | } |
| 151 | remove_edge(currE); |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 152 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | SkASSERT(currE->fLastY > curr_y); |
| 154 | newX = currE->fX + currE->fDX; |
| 155 | currE->fX = newX; |
| 156 | NEXT_X: |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 157 | if (newX < prevX) { // ripple currE backwards until it is x-sorted |
Yuqian Li | a33b43d | 2017-03-17 11:26:29 -0400 | [diff] [blame] | 158 | backward_insert_edge_based_on_x(currE); |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 159 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 160 | prevX = newX; |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 161 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | } |
| 163 | currE = next; |
| 164 | SkASSERT(currE); |
| 165 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 166 | |
reed | 01d3319 | 2015-02-07 12:18:41 -0800 | [diff] [blame] | 167 | // 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | if (proc) { |
| 176 | proc(blitter, curr_y, PREPOST_END); // post-proc |
| 177 | } |
| 178 | |
| 179 | curr_y += 1; |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 180 | if (curr_y >= stop_y) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | break; |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 182 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | // 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 Li | 7877d32 | 2017-07-18 10:11:05 -0400 | [diff] [blame] | 188 | // return true if we're NOT done with this edge |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 189 | static 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 Li | 7877d32 | 2017-07-18 10:11:05 -0400 | [diff] [blame] | 195 | return true; |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 196 | } |
| 197 | } else if (edge->fCurveCount > 0) { |
| 198 | if (((SkQuadraticEdge*)edge)->updateQuadratic()) { |
| 199 | SkASSERT(edge->fFirstY == last_y + 1); |
Yuqian Li | 7877d32 | 2017-07-18 10:11:05 -0400 | [diff] [blame] | 200 | return true; |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
Yuqian Li | 7877d32 | 2017-07-18 10:11:05 -0400 | [diff] [blame] | 203 | return false; |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 204 | } |
Yuqian Li | 7877d32 | 2017-07-18 10:11:05 -0400 | [diff] [blame] | 205 | return true; |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static void walk_convex_edges(SkEdge* prevHead, SkPath::FillType, |
| 209 | SkBlitter* blitter, int start_y, int stop_y, |
| 210 | PrePostProc proc) { |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 211 | validate_sort(prevHead->fNext); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 212 | |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 213 | SkEdge* leftE = prevHead->fNext; |
| 214 | SkEdge* riteE = leftE->fNext; |
| 215 | SkEdge* currE = riteE->fNext; |
mike@reedtribe.org | fc5da92 | 2011-10-31 02:58:27 +0000 | [diff] [blame] | 216 | |
| 217 | #if 0 |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 218 | int local_top = leftE->fFirstY; |
| 219 | SkASSERT(local_top == riteE->fFirstY); |
mike@reedtribe.org | fc5da92 | 2011-10-31 02:58:27 +0000 | [diff] [blame] | 220 | #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.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 225 | SkASSERT(local_top >= start_y); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 226 | |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 227 | for (;;) { |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 228 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 235 | |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 236 | int local_bot = SkMin32(leftE->fLastY, riteE->fLastY); |
reed@google.com | a6c7c48 | 2011-10-28 18:58:46 +0000 | [diff] [blame] | 237 | local_bot = SkMin32(local_bot, stop_y - 1); |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 238 | SkASSERT(local_top <= local_bot); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 239 | |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 240 | 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.com | 562a2ac | 2011-10-31 14:14:18 +0000 | [diff] [blame] | 246 | if (0 == (dLeft | dRite)) { |
reed@google.com | 0a9cc44 | 2012-02-06 19:05:51 +0000 | [diff] [blame] | 247 | int L = SkFixedRoundToInt(left); |
| 248 | int R = SkFixedRoundToInt(rite); |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 249 | if (L < R) { |
| 250 | count += 1; |
| 251 | blitter->blitRect(L, local_top, R - L, count); |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 252 | } |
mike@reedtribe.org | fc5da92 | 2011-10-31 02:58:27 +0000 | [diff] [blame] | 253 | local_top = local_bot + 1; |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 254 | } else { |
| 255 | do { |
reed@google.com | 0a9cc44 | 2012-02-06 19:05:51 +0000 | [diff] [blame] | 256 | int L = SkFixedRoundToInt(left); |
| 257 | int R = SkFixedRoundToInt(rite); |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 258 | 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 Li | 7877d32 | 2017-07-18 10:11:05 -0400 | [diff] [blame] | 270 | if (!update_edge(leftE, local_bot)) { |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 271 | if (currE->fFirstY >= stop_y) { |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | leftE = currE; |
| 275 | currE = currE->fNext; |
| 276 | } |
Yuqian Li | 7877d32 | 2017-07-18 10:11:05 -0400 | [diff] [blame] | 277 | if (!update_edge(riteE, local_bot)) { |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 278 | if (currE->fFirstY >= stop_y) { |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 279 | break; |
| 280 | } |
| 281 | riteE = currE; |
| 282 | currE = currE->fNext; |
| 283 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 284 | |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 285 | SkASSERT(leftE); |
| 286 | SkASSERT(riteE); |
reed@google.com | a6c7c48 | 2011-10-28 18:58:46 +0000 | [diff] [blame] | 287 | |
| 288 | // check our bottom clip |
| 289 | SkASSERT(local_top == local_bot + 1); |
| 290 | if (local_top >= stop_y) { |
| 291 | break; |
| 292 | } |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | /////////////////////////////////////////////////////////////////////////////// |
| 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 | // |
| 303 | class InverseBlitter : public SkBlitter { |
| 304 | public: |
| 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 |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 322 | void blitH(int x, int y, int width) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 323 | int invWidth = x - fPrevX; |
| 324 | if (invWidth > 0) { |
| 325 | fBlitter->blitH(fPrevX, y, invWidth); |
| 326 | } |
| 327 | fPrevX = x + width; |
| 328 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 329 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 330 | // we do not expect to get called with these entrypoints |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 331 | void blitAntiH(int, int, const SkAlpha[], const int16_t runs[]) override { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 332 | SkDEBUGFAIL("blitAntiH unexpected"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 333 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 334 | void blitV(int x, int y, int height, SkAlpha alpha) override { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 335 | SkDEBUGFAIL("blitV unexpected"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 336 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 337 | void blitRect(int x, int y, int width, int height) override { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 338 | SkDEBUGFAIL("blitRect unexpected"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 339 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 340 | void blitMask(const SkMask&, const SkIRect& clip) override { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 341 | SkDEBUGFAIL("blitMask unexpected"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 343 | const SkPixmap* justAnOpaqueColor(uint32_t* value) override { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 344 | SkDEBUGFAIL("justAnOpaqueColor unexpected"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 345 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 346 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 347 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | private: |
| 349 | SkBlitter* fBlitter; |
| 350 | int fFirstX, fLastX, fPrevX; |
| 351 | }; |
| 352 | |
| 353 | static void PrePostInverseBlitterProc(SkBlitter* blitter, int y, bool isStart) { |
| 354 | ((InverseBlitter*)blitter)->prepost(y, isStart); |
| 355 | } |
| 356 | |
| 357 | /////////////////////////////////////////////////////////////////////////////// |
| 358 | |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 359 | #if defined _WIN32 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | #pragma warning ( pop ) |
| 361 | #endif |
| 362 | |
reed@google.com | 0a71a9c | 2012-05-09 12:07:31 +0000 | [diff] [blame] | 363 | static bool operator<(const SkEdge& a, const SkEdge& b) { |
| 364 | int valuea = a.fFirstY; |
| 365 | int valueb = b.fFirstY; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 366 | |
reed@google.com | 0a71a9c | 2012-05-09 12:07:31 +0000 | [diff] [blame] | 367 | if (valuea == valueb) { |
| 368 | valuea = a.fX; |
| 369 | valueb = b.fX; |
| 370 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 371 | |
reed@google.com | 0a71a9c | 2012-05-09 12:07:31 +0000 | [diff] [blame] | 372 | return valuea < valueb; |
| 373 | } |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 374 | |
| 375 | static SkEdge* sort_edges(SkEdge* list[], int count, SkEdge** last) { |
reed@google.com | 0a71a9c | 2012-05-09 12:07:31 +0000 | [diff] [blame] | 376 | SkTQSort(list, list + count - 1); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 377 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 378 | // 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.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 383 | |
reed@android.com | e72fee5 | 2009-11-16 14:52:01 +0000 | [diff] [blame] | 384 | *last = list[count - 1]; |
| 385 | return list[0]; |
| 386 | } |
| 387 | |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 388 | // clipRect has not been shifted up |
| 389 | void sk_fill_path(const SkPath& path, const SkIRect& clipRect, SkBlitter* blitter, |
| 390 | int start_y, int stop_y, int shiftEdgesUp, bool pathContainedInClip) { |
caryclark | a10742c | 2014-09-18 11:00:40 -0700 | [diff] [blame] | 391 | SkASSERT(blitter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 392 | |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 393 | SkIRect shiftedClip = clipRect; |
| 394 | shiftedClip.fLeft <<= shiftEdgesUp; |
| 395 | shiftedClip.fRight <<= shiftEdgesUp; |
| 396 | shiftedClip.fTop <<= shiftEdgesUp; |
| 397 | shiftedClip.fBottom <<= shiftEdgesUp; |
| 398 | |
Yuqian Li | 1c84088 | 2017-05-26 14:50:35 -0400 | [diff] [blame] | 399 | SkEdgeBuilder builder; |
| 400 | int count = builder.build_edges(path, &shiftedClip, shiftEdgesUp, pathContainedInClip); |
| 401 | SkEdge** list = builder.edgeList(); |
reed@android.com | 909994f | 2009-11-18 16:09:51 +0000 | [diff] [blame] | 402 | |
reed | 31223e0 | 2015-02-09 08:33:07 -0800 | [diff] [blame] | 403 | if (0 == count) { |
agl@chromium.org | 8cfdf01 | 2010-12-06 18:52:40 +0000 | [diff] [blame] | 404 | if (path.isInverseFillType()) { |
reed@google.com | 9d5f76a | 2012-05-01 14:49:28 +0000 | [diff] [blame] | 405 | /* |
| 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 Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 411 | SkIRect rect = clipRect; |
reed@google.com | 9d5f76a | 2012-05-01 14:49:28 +0000 | [diff] [blame] | 412 | 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.org | 8cfdf01 | 2010-12-06 18:52:40 +0000 | [diff] [blame] | 424 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | return; |
| 426 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 427 | |
reed@android.com | 909994f | 2009-11-18 16:09:51 +0000 | [diff] [blame] | 428 | SkEdge headEdge, tailEdge, *last; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 429 | // this returns the first and last edge after they're sorted into a dlink list |
reed@android.com | 909994f | 2009-11-18 16:09:51 +0000 | [diff] [blame] | 430 | SkEdge* edge = sort_edges(list, count, &last); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 431 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 432 | headEdge.fPrev = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 433 | headEdge.fNext = edge; |
| 434 | headEdge.fFirstY = kEDGE_HEAD_Y; |
| 435 | headEdge.fX = SK_MinS32; |
| 436 | edge->fPrev = &headEdge; |
| 437 | |
| 438 | tailEdge.fPrev = last; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 439 | tailEdge.fNext = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | tailEdge.fFirstY = kEDGE_TAIL_Y; |
| 441 | last->fNext = &tailEdge; |
| 442 | |
| 443 | // now edge is the head of the sorted linklist |
| 444 | |
caryclark | 3127c99 | 2015-12-09 12:02:30 -0800 | [diff] [blame] | 445 | start_y = SkLeftShift(start_y, shiftEdgesUp); |
| 446 | stop_y = SkLeftShift(stop_y, shiftEdgesUp); |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 447 | if (!pathContainedInClip && start_y < shiftedClip.fTop) { |
| 448 | start_y = shiftedClip.fTop; |
reed@android.com | dca6a56 | 2010-02-22 16:05:48 +0000 | [diff] [blame] | 449 | } |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 450 | if (!pathContainedInClip && stop_y > shiftedClip.fBottom) { |
| 451 | stop_y = shiftedClip.fBottom; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | InverseBlitter ib; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 455 | PrePostProc proc = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 456 | |
| 457 | if (path.isInverseFillType()) { |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 458 | ib.setBlitter(blitter, clipRect, shiftEdgesUp); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 459 | blitter = &ib; |
| 460 | proc = PrePostInverseBlitterProc; |
| 461 | } |
| 462 | |
Yuqian Li | 1c84088 | 2017-05-26 14:50:35 -0400 | [diff] [blame] | 463 | // count >= 2 is required as the convex walker does not handle missing right edges |
| 464 | if (path.isConvex() && (nullptr == proc) && count >= 2) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 465 | walk_convex_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, nullptr); |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 466 | } else { |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 467 | walk_edges(&headEdge, path.getFillType(), blitter, start_y, stop_y, proc, |
| 468 | shiftedClip.right()); |
reed@google.com | e3a83ec | 2011-10-25 19:27:43 +0000 | [diff] [blame] | 469 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 470 | } |
| 471 | |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 472 | void sk_blit_above(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 473 | const SkIRect& cr = clip.getBounds(); |
| 474 | SkIRect tmp; |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 475 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 476 | tmp.fLeft = cr.fLeft; |
| 477 | tmp.fRight = cr.fRight; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 478 | tmp.fTop = cr.fTop; |
| 479 | tmp.fBottom = ir.fTop; |
| 480 | if (!tmp.isEmpty()) { |
| 481 | blitter->blitRectRegion(tmp, clip); |
| 482 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 483 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 484 | |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 485 | void 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 491 | tmp.fTop = ir.fBottom; |
| 492 | tmp.fBottom = cr.fBottom; |
| 493 | if (!tmp.isEmpty()) { |
| 494 | blitter->blitRectRegion(tmp, clip); |
| 495 | } |
| 496 | } |
| 497 | |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 498 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 499 | |
reed@google.com | 97caebc | 2012-06-08 16:30:12 +0000 | [diff] [blame] | 500 | /** |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 501 | * 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.com | 97caebc | 2012-06-08 16:30:12 +0000 | [diff] [blame] | 504 | */ |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 505 | SkScanClipper::SkScanClipper(SkBlitter* blitter, const SkRegion* clip, |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 506 | const SkIRect& ir, bool skipRejectTest) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 507 | fBlitter = nullptr; // null means blit nothing |
| 508 | fClipRect = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 509 | |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 510 | if (clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 511 | fClipRect = &clip->getBounds(); |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 512 | if (!skipRejectTest && !SkIRect::Intersects(*fClipRect, ir)) { // completely clipped out |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 513 | return; |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 514 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 515 | |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 516 | if (clip->isRect()) { |
| 517 | if (fClipRect->contains(ir)) { |
Mike Reed | 28930b4 | 2016-11-09 15:23:26 -0500 | [diff] [blame] | 518 | #ifdef SK_DEBUG |
Yuqian Li | 99bba9e | 2016-11-21 09:44:59 -0500 | [diff] [blame] | 519 | fRectClipCheckBlitter.init(blitter, *fClipRect); |
| 520 | blitter = &fRectClipCheckBlitter; |
Mike Reed | 28930b4 | 2016-11-09 15:23:26 -0500 | [diff] [blame] | 521 | #endif |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 522 | fClipRect = nullptr; |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 523 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | // only need a wrapper blitter if we're horizontally clipped |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 525 | if (fClipRect->fLeft > ir.fLeft || fClipRect->fRight < ir.fRight) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | fRectBlitter.init(blitter, *fClipRect); |
| 527 | blitter = &fRectBlitter; |
Mike Reed | 28930b4 | 2016-11-09 15:23:26 -0500 | [diff] [blame] | 528 | } else { |
| 529 | #ifdef SK_DEBUG |
Yuqian Li | 99bba9e | 2016-11-21 09:44:59 -0500 | [diff] [blame] | 530 | fRectClipCheckBlitter.init(blitter, *fClipRect); |
| 531 | blitter = &fRectClipCheckBlitter; |
Mike Reed | 28930b4 | 2016-11-09 15:23:26 -0500 | [diff] [blame] | 532 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
mike@reedtribe.org | 99219d3 | 2011-04-09 18:54:23 +0000 | [diff] [blame] | 535 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 536 | fRgnBlitter.init(blitter, clip); |
| 537 | blitter = &fRgnBlitter; |
| 538 | } |
| 539 | } |
| 540 | fBlitter = blitter; |
| 541 | } |
| 542 | |
| 543 | /////////////////////////////////////////////////////////////////////////////// |
| 544 | |
reed@google.com | 1501803 | 2011-07-11 12:21:30 +0000 | [diff] [blame] | 545 | static 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 | |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 557 | /** |
Lee Salzman | b8e7521 | 2017-03-14 14:52:51 -0400 | [diff] [blame] | 558 | * 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 |
| 563 | static const double kRoundBias = 0.5 / SK_FDot6One; |
| 564 | #else |
| 565 | static 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. |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 571 | */ |
| 572 | static inline int round_down_to_int(SkScalar x) { |
| 573 | double xx = x; |
Lee Salzman | b8e7521 | 2017-03-14 14:52:51 -0400 | [diff] [blame] | 574 | xx -= 0.5 + kRoundBias; |
Lee Salzman | b569056 | 2017-01-13 12:03:24 -0500 | [diff] [blame] | 575 | return (int)ceil(xx); |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Lee Salzman | 95acbad | 2017-01-13 13:35:49 -0500 | [diff] [blame] | 578 | /** |
Lee Salzman | b8e7521 | 2017-03-14 14:52:51 -0400 | [diff] [blame] | 579 | * 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 Salzman | 95acbad | 2017-01-13 13:35:49 -0500 | [diff] [blame] | 581 | */ |
Lee Salzman | b8e7521 | 2017-03-14 14:52:51 -0400 | [diff] [blame] | 582 | static inline int round_up_to_int(SkScalar x) { |
Lee Salzman | 95acbad | 2017-01-13 13:35:49 -0500 | [diff] [blame] | 583 | double xx = x; |
Lee Salzman | b8e7521 | 2017-03-14 14:52:51 -0400 | [diff] [blame] | 584 | xx += 0.5 + kRoundBias; |
Lee Salzman | 95acbad | 2017-01-13 13:35:49 -0500 | [diff] [blame] | 585 | return (int)floor(xx); |
| 586 | } |
Lee Salzman | 95acbad | 2017-01-13 13:35:49 -0500 | [diff] [blame] | 587 | |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 588 | /** |
| 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 Salzman | 95acbad | 2017-01-13 13:35:49 -0500 | [diff] [blame] | 605 | * |
| 606 | * |
Lee Salzman | b8e7521 | 2017-03-14 14:52:51 -0400 | [diff] [blame] | 607 | * 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 Salzman | 95acbad | 2017-01-13 13:35:49 -0500 | [diff] [blame] | 611 | * |
| 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 Salzman | b8e7521 | 2017-03-14 14:52:51 -0400 | [diff] [blame] | 616 | * enough. |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 617 | */ |
| 618 | static 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 Salzman | b8e7521 | 2017-03-14 14:52:51 -0400 | [diff] [blame] | 621 | round_up_to_int(src.fRight), round_up_to_int(src.fBottom)); |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 622 | } |
| 623 | |
reed@google.com | 1501803 | 2011-07-11 12:21:30 +0000 | [diff] [blame] | 624 | void SkScan::FillPath(const SkPath& path, const SkRegion& origClip, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 625 | SkBlitter* blitter) { |
reed@google.com | 1501803 | 2011-07-11 12:21:30 +0000 | [diff] [blame] | 626 | if (origClip.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | |
reed@google.com | 1501803 | 2011-07-11 12:21:30 +0000 | [diff] [blame] | 630 | // 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 642 | SkIRect ir; |
caryclark | 6df6115 | 2016-01-04 14:17:47 -0800 | [diff] [blame] | 643 | // 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 648 | if (ir.isEmpty()) { |
| 649 | if (path.isInverseFillType()) { |
reed@google.com | 1501803 | 2011-07-11 12:21:30 +0000 | [diff] [blame] | 650 | blitter->blitRegion(*clipPtr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 651 | } |
| 652 | return; |
| 653 | } |
| 654 | |
reed@google.com | 5ee6491 | 2012-06-11 17:30:33 +0000 | [diff] [blame] | 655 | SkScanClipper clipper(blitter, clipPtr, ir, path.isInverseFillType()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 656 | |
| 657 | blitter = clipper.getBlitter(); |
| 658 | if (blitter) { |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 659 | // 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 661 | if (path.isInverseFillType()) { |
reed@google.com | 1501803 | 2011-07-11 12:21:30 +0000 | [diff] [blame] | 662 | sk_blit_above(blitter, ir, *clipPtr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 663 | } |
Yuqian Li | e4b8b52 | 2016-11-16 10:12:58 -0500 | [diff] [blame] | 664 | 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.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 668 | if (path.isInverseFillType()) { |
reed@google.com | 1501803 | 2011-07-11 12:21:30 +0000 | [diff] [blame] | 669 | sk_blit_below(blitter, ir, *clipPtr); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 670 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 671 | } else { |
| 672 | // what does it mean to not have a blitter if path.isInverseFillType??? |
| 673 | } |
| 674 | } |
| 675 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 676 | void SkScan::FillPath(const SkPath& path, const SkIRect& ir, |
| 677 | SkBlitter* blitter) { |
| 678 | SkRegion rgn(ir); |
| 679 | FillPath(path, rgn, blitter); |
| 680 | } |
| 681 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 682 | /////////////////////////////////////////////////////////////////////////////// |
| 683 | |
| 684 | static int build_tri_edges(SkEdge edge[], const SkPoint pts[], |
| 685 | const SkIRect* clipRect, SkEdge* list[]) { |
| 686 | SkEdge** start = list; |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 687 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 688 | 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.com | 800046e | 2009-10-14 09:36:25 +0000 | [diff] [blame] | 703 | static void sk_fill_triangle(const SkPoint pts[], const SkIRect* clipRect, |
| 704 | SkBlitter* blitter, const SkIRect& ir) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 705 | SkASSERT(pts && blitter); |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 706 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 707 | 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.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 719 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 720 | headEdge.fPrev = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 721 | headEdge.fNext = edge; |
| 722 | headEdge.fFirstY = kEDGE_HEAD_Y; |
| 723 | headEdge.fX = SK_MinS32; |
| 724 | edge->fPrev = &headEdge; |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 725 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 726 | tailEdge.fPrev = last; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 727 | tailEdge.fNext = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 728 | tailEdge.fFirstY = kEDGE_TAIL_Y; |
| 729 | last->fNext = &tailEdge; |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 730 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 731 | // 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.com | dca6a56 | 2010-02-22 16:05:48 +0000 | [diff] [blame] | 736 | int start_y = ir.fTop; |
| 737 | if (clipRect && start_y < clipRect->fTop) { |
| 738 | start_y = clipRect->fTop; |
| 739 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 740 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 742 | } |
| 743 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 744 | void SkScan::FillTriangle(const SkPoint pts[], const SkRasterClip& clip, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 745 | SkBlitter* blitter) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 746 | if (clip.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 747 | return; |
| 748 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 749 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 750 | SkRect r; |
| 751 | SkIRect ir; |
| 752 | r.set(pts, 3); |
| 753 | r.round(&ir); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 754 | if (ir.isEmpty() || !SkIRect::Intersects(ir, clip.getBounds())) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 755 | return; |
| 756 | } |
reed@google.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 757 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 758 | 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.com | 55b6b58 | 2011-03-02 15:58:18 +0000 | [diff] [blame] | 767 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 768 | SkScanClipper clipper(blitter, clipRgn, ir); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 769 | blitter = clipper.getBlitter(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 770 | if (blitter) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 771 | sk_fill_triangle(pts, clipper.getClipRect(), blitter, ir); |
| 772 | } |
| 773 | } |