| 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 |
|
| 8 |
#include "include/core/SkColor.h" |
| 9 |
#include "include/core/SkPath.h" |
| 10 |
#include "include/core/SkPathTypes.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkRegion.h" |
| 13 |
#include "include/core/SkScalar.h" |
| 14 |
#include "include/private/base/SkAssert.h" |
| 15 |
#include "include/private/base/SkDebug.h" |
| 16 |
#include "include/private/base/SkFixed.h" |
| 17 |
#include "include/private/base/SkFloatingPoint.h" |
| 18 |
#include "include/private/base/SkMacros.h" |
| 19 |
#include "include/private/base/SkMath.h" |
| 20 |
#include "include/private/base/SkPoint_impl.h" |
| 21 |
#include "include/private/base/SkSafe32.h" |
| 22 |
#include "src/base/SkTSort.h" |
| 23 |
#include "src/core/SkBlitter.h" |
| 24 |
#include "src/core/SkEdge.h" |
| 25 |
#include "src/core/SkEdgeBuilder.h" |
| 26 |
#include "src/core/SkFDot6.h" |
| 27 |
#include "src/core/SkPathPriv.h" |
| 28 |
#include "src/core/SkPathRawShapes.h" |
| 29 |
#include "src/core/SkRasterClip.h" |
| 30 |
#include "src/core/SkRectPriv.h" |
| 31 |
#include "src/core/SkScan.h" |
| 32 |
#include "src/core/SkScanPriv.h" |
| 33 |
|
| 34 |
#include <algorithm> |
| 35 |
#include <cmath> |
| 36 |
#include <cstdint> |
| 37 |
|
| 38 |
struct SkMask; |
| 39 |
|
| 40 |
#define kEDGE_HEAD_Y SK_MinS32 |
| 41 |
#define kEDGE_TAIL_Y SK_MaxS32 |
| 42 |
|
| 43 |
#ifdef SK_DEBUG |
| 44 |
static void validate_sort(const SkEdge* edge) { |
| 45 |
int y = kEDGE_HEAD_Y; |
| 46 |
|
| 47 |
while (edge->fFirstY != SK_MaxS32) { |
| 48 |
edge->validate(); |
| 49 |
SkASSERT(y <= edge->fFirstY); |
| 50 |
|
| 51 |
y = edge->fFirstY; |
| 52 |
edge = edge->fNext; |
| 53 |
} |
| 54 |
} |
| 55 |
#else |
| 56 |
#define validate_sort(edge) |
| 57 |
#endif |
| 58 |
|
| 59 |
static void insert_new_edges(SkEdge* newEdge, int curr_y) { |
| 60 |
if (newEdge->fFirstY != curr_y) { |
| 61 |
return; |
| 62 |
} |
| 63 |
SkEdge* prev = newEdge->fPrev; |
| 64 |
if (prev->fX <= newEdge->fX) { |
| 65 |
return; |
| 66 |
} |
| 67 |
// find first x pos to insert |
| 68 |
SkEdge* start = backward_insert_start(prev, newEdge->fX); |
| 69 |
// insert the lot, fixing up the links as we go |
| 70 |
do { |
| 71 |
SkEdge* next = newEdge->fNext; |
| 72 |
do { |
| 73 |
if (start->fNext == newEdge) { |
| 74 |
goto nextEdge; |
| 75 |
} |
| 76 |
SkEdge* after = start->fNext; |
| 77 |
if (after->fX >= newEdge->fX) { |
| 78 |
break; |
| 79 |
} |
| 80 |
start = after; |
| 81 |
} while (true); |
| 82 |
remove_edge(newEdge); |
| 83 |
insert_edge_after(newEdge, start); |
| 84 |
nextEdge: |
| 85 |
start = newEdge; |
| 86 |
newEdge = next; |
| 87 |
} while (newEdge->fFirstY == curr_y); |
| 88 |
} |
| 89 |
|
| 90 |
#ifdef SK_DEBUG |
| 91 |
static void validate_edges_for_y(const SkEdge* edge, int curr_y) { |
| 92 |
while (edge->fFirstY <= curr_y) { |
| 93 |
SkASSERT(edge->fPrev && edge->fNext); |
| 94 |
SkASSERT(edge->fPrev->fNext == edge); |
| 95 |
SkASSERT(edge->fNext->fPrev == edge); |
| 96 |
SkASSERT(edge->fFirstY <= edge->fLastY); |
| 97 |
|
| 98 |
SkASSERT(edge->fPrev->fX <= edge->fX); |
| 99 |
edge = edge->fNext; |
| 100 |
} |
| 101 |
} |
| 102 |
#else |
| 103 |
#define validate_edges_for_y(edge, curr_y) |
| 104 |
#endif |
| 105 |
|
| 106 |
#if defined _WIN32 // disable warning : local variable used without having been initialized |
| 107 |
#pragma warning ( push ) |
| 108 |
#pragma warning ( disable : 4701 ) |
| 109 |
#endif |
| 110 |
|
| 111 |
typedef void (*PrePostProc)(SkBlitter* blitter, int y, bool isStartOfScanline); |
| 112 |
#define PREPOST_START true |
| 113 |
#define PREPOST_END false |
| 114 |
|
| 115 |
static void walk_edges(SkEdge* prevHead, SkPathFillType fillType, |
| 116 |
SkBlitter* blitter, int start_y, int stop_y, |
| 117 |
PrePostProc proc, int rightClip) { |
| 118 |
validate_sort(prevHead->fNext); |
| 119 |
|
| 120 |
int curr_y = start_y; |
| 121 |
int windingMask = SkPathFillType_IsEvenOdd(fillType) ? 1 : -1; |
| 122 |
|
| 123 |
for (;;) { |
| 124 |
int w = 0; |
| 125 |
int left SK_INIT_TO_AVOID_WARNING; |
| 126 |
SkEdge* currE = prevHead->fNext; |
| 127 |
SkFixed prevX = prevHead->fX; |
| 128 |
|
| 129 |
validate_edges_for_y(currE, curr_y); |
| 130 |
|
| 131 |
if (proc) { |
| 132 |
proc(blitter, curr_y, PREPOST_START); // pre-proc |
| 133 |
} |
| 134 |
|
| 135 |
while (currE->fFirstY <= curr_y) { |
| 136 |
SkASSERT(currE->fLastY >= curr_y); |
| 137 |
|
| 138 |
int x = SkFixedRoundToInt(currE->fX); |
| 139 |
|
| 140 |
if ((w & windingMask) == 0) { // we're starting interval |
| 141 |
left = x; |
| 142 |
} |
| 143 |
|
| 144 |
w += static_cast<int>(currE->fWinding); |
| 145 |
|
| 146 |
if ((w & windingMask) == 0) { // we finished an interval |
| 147 |
int width = x - left; |
| 148 |
SkASSERT(width >= 0); |
| 149 |
if (width > 0) { |
| 150 |
blitter->blitH(left, curr_y, width); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
SkEdge* next = currE->fNext; |
| 155 |
SkFixed newX; |
| 156 |
|
| 157 |
if (currE->fLastY == curr_y) { // are we done with this edge? |
| 158 |
if (currE->hasNextSegment()) { |
| 159 |
if (currE->nextSegment()) { |
| 160 |
SkASSERT(currE->fFirstY == curr_y + 1); |
| 161 |
|
| 162 |
newX = currE->fX; |
| 163 |
goto NEXT_X; |
| 164 |
} |
| 165 |
} |
| 166 |
remove_edge(currE); |
| 167 |
} else { |
| 168 |
SkASSERT(currE->fLastY > curr_y); |
| 169 |
newX = currE->fX + currE->fDxDy; |
| 170 |
currE->fX = newX; |
| 171 |
NEXT_X: |
| 172 |
if (newX < prevX) { // ripple currE backwards until it is x-sorted |
| 173 |
backward_insert_edge_based_on_x(currE); |
| 174 |
} else { |
| 175 |
prevX = newX; |
| 176 |
} |
| 177 |
} |
| 178 |
currE = next; |
| 179 |
SkASSERT(currE); |
| 180 |
} |
| 181 |
|
| 182 |
if ((w & windingMask) != 0) { // was our right-edge culled away? |
| 183 |
int width = rightClip - left; |
| 184 |
if (width > 0) { |
| 185 |
blitter->blitH(left, curr_y, width); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
if (proc) { |
| 190 |
proc(blitter, curr_y, PREPOST_END); // post-proc |
| 191 |
} |
| 192 |
|
| 193 |
curr_y += 1; |
| 194 |
if (curr_y >= stop_y) { |
| 195 |
break; |
| 196 |
} |
| 197 |
// now currE points to the first edge with a Yint larger than curr_y |
| 198 |
insert_new_edges(currE, curr_y); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// return true if we're NOT done with this edge |
| 203 |
static bool update_edge(SkEdge* edge, int last_y) { |
| 204 |
SkASSERT(edge->fLastY >= last_y); |
| 205 |
if (last_y != edge->fLastY) { |
| 206 |
return true; |
| 207 |
} |
| 208 |
if (!edge->hasNextSegment()) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
if (edge->nextSegment()) { |
| 212 |
SkASSERT(edge->fFirstY == last_y + 1); |
| 213 |
return true; |
| 214 |
} |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
// Unexpected conditions for which we need to return |
| 219 |
#define ASSERT_RETURN(cond) \ |
| 220 |
do { \ |
| 221 |
if (!(cond)) { \ |
| 222 |
SkDEBUGFAILF("assert(%s)", #cond); \ |
| 223 |
return; \ |
| 224 |
} \ |
| 225 |
} while (0) |
| 226 |
|
| 227 |
// Needs Y to only change once (looser than convex in X) |
| 228 |
static void walk_simple_edges(SkEdge* prevHead, SkBlitter* blitter, int start_y, int stop_y) { |
| 229 |
validate_sort(prevHead->fNext); |
| 230 |
|
| 231 |
SkEdge* leftE = prevHead->fNext; |
| 232 |
SkEdge* riteE = leftE->fNext; |
| 233 |
SkEdge* currE = riteE->fNext; |
| 234 |
|
| 235 |
// our edge choppers for curves can result in the initial edges |
| 236 |
// not lining up, so we take the max. |
| 237 |
int local_top = std::max(leftE->fFirstY, riteE->fFirstY); |
| 238 |
ASSERT_RETURN(local_top >= start_y); |
| 239 |
|
| 240 |
while (local_top < stop_y) { |
| 241 |
SkASSERT(leftE->fFirstY <= stop_y); |
| 242 |
SkASSERT(riteE->fFirstY <= stop_y); |
| 243 |
|
| 244 |
int local_bot = std::min(leftE->fLastY, riteE->fLastY); |
| 245 |
local_bot = std::min(local_bot, stop_y - 1); |
| 246 |
ASSERT_RETURN(local_top <= local_bot); |
| 247 |
|
| 248 |
SkFixed left = leftE->fX; |
| 249 |
SkFixed dLeft = leftE->fDxDy; |
| 250 |
SkFixed rite = riteE->fX; |
| 251 |
SkFixed dRite = riteE->fDxDy; |
| 252 |
int count = local_bot - local_top; |
| 253 |
ASSERT_RETURN(count >= 0); |
| 254 |
|
| 255 |
if (dLeft == 0 && dRite == 0) { |
| 256 |
int L = SkFixedRoundToInt(left); |
| 257 |
int R = SkFixedRoundToInt(rite); |
| 258 |
if (L > R) { |
| 259 |
std::swap(L, R); |
| 260 |
} |
| 261 |
if (L < R) { |
| 262 |
count += 1; |
| 263 |
blitter->blitRect(L, local_top, R - L, count); |
| 264 |
} |
| 265 |
local_top = local_bot + 1; |
| 266 |
} else { |
| 267 |
do { |
| 268 |
int L = SkFixedRoundToInt(left); |
| 269 |
int R = SkFixedRoundToInt(rite); |
| 270 |
if (L > R) { |
| 271 |
std::swap(L, R); |
| 272 |
} |
| 273 |
if (L < R) { |
| 274 |
blitter->blitH(L, local_top, R - L); |
| 275 |
} |
| 276 |
// Either/both of these might overflow, since we perform this step even if |
| 277 |
// (later) we determine that we are done with the edge, and so the computed |
| 278 |
// left or rite edge will not be used (see update_edge). Use this helper to |
| 279 |
// silence UBSAN when we perform the add. |
| 280 |
left = Sk32_can_overflow_add(left, dLeft); |
| 281 |
rite = Sk32_can_overflow_add(rite, dRite); |
| 282 |
local_top += 1; |
| 283 |
} while (--count >= 0); |
| 284 |
} |
| 285 |
|
| 286 |
leftE->fX = left; |
| 287 |
riteE->fX = rite; |
| 288 |
|
| 289 |
if (!update_edge(leftE, local_bot)) { |
| 290 |
if (currE->fFirstY >= stop_y) { |
| 291 |
return; // we're done |
| 292 |
} |
| 293 |
leftE = currE; |
| 294 |
currE = currE->fNext; |
| 295 |
ASSERT_RETURN(leftE->fFirstY == local_top); |
| 296 |
} |
| 297 |
if (!update_edge(riteE, local_bot)) { |
| 298 |
if (currE->fFirstY >= stop_y) { |
| 299 |
return; // we're done |
| 300 |
} |
| 301 |
riteE = currE; |
| 302 |
currE = currE->fNext; |
| 303 |
ASSERT_RETURN(riteE->fFirstY == local_top); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/////////////////////////////////////////////////////////////////////////////// |
| 309 |
|
| 310 |
// this overrides blitH, and will call its proxy blitter with the inverse |
| 311 |
// of the spans it is given (clipped to the left/right of the cliprect) |
| 312 |
// |
| 313 |
// used to implement inverse filltypes on paths |
| 314 |
// |
| 315 |
class InverseBlitter : public SkBlitter { |
| 316 |
public: |
| 317 |
void setBlitter(SkBlitter* blitter, const SkIRect& clip) { |
| 318 |
fBlitter = blitter; |
| 319 |
fFirstX = clip.fLeft; |
| 320 |
fLastX = clip.fRight; |
| 321 |
} |
| 322 |
void prepost(int y, bool isStart) { |
| 323 |
if (isStart) { |
| 324 |
fPrevX = fFirstX; |
| 325 |
} else { |
| 326 |
int invWidth = fLastX - fPrevX; |
| 327 |
if (invWidth > 0) { |
| 328 |
fBlitter->blitH(fPrevX, y, invWidth); |
| 329 |
} |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// overrides |
| 334 |
void blitH(int x, int y, int width) override { |
| 335 |
int invWidth = x - fPrevX; |
| 336 |
if (invWidth > 0) { |
| 337 |
fBlitter->blitH(fPrevX, y, invWidth); |
| 338 |
} |
| 339 |
fPrevX = x + width; |
| 340 |
} |
| 341 |
|
| 342 |
// we do not expect to get called with these entrypoints |
| 343 |
void blitAntiH(int, int, const SkAlpha[], const int16_t runs[]) override { |
| 344 |
SkDEBUGFAIL("blitAntiH unexpected"); |
| 345 |
} |
| 346 |
void blitV(int x, int y, int height, SkAlpha alpha) override { |
| 347 |
SkDEBUGFAIL("blitV unexpected"); |
| 348 |
} |
| 349 |
void blitRect(int x, int y, int width, int height) override { |
| 350 |
SkDEBUGFAIL("blitRect unexpected"); |
| 351 |
} |
| 352 |
void blitMask(const SkMask&, const SkIRect& clip) override { |
| 353 |
SkDEBUGFAIL("blitMask unexpected"); |
| 354 |
} |
| 355 |
|
| 356 |
private: |
| 357 |
SkBlitter* fBlitter; |
| 358 |
int fFirstX, fLastX, fPrevX; |
| 359 |
}; |
| 360 |
|
| 361 |
static void PrePostInverseBlitterProc(SkBlitter* blitter, int y, bool isStart) { |
| 362 |
((InverseBlitter*)blitter)->prepost(y, isStart); |
| 363 |
} |
| 364 |
|
| 365 |
/////////////////////////////////////////////////////////////////////////////// |
| 366 |
|
| 367 |
#if defined _WIN32 |
| 368 |
#pragma warning ( pop ) |
| 369 |
#endif |
| 370 |
|
| 371 |
static bool compare_edges(const SkEdge* a, const SkEdge* b) { |
| 372 |
if (a->fFirstY != b->fFirstY) { |
| 373 |
return a->fFirstY < b->fFirstY; |
| 374 |
} |
| 375 |
|
| 376 |
return a->fX < b->fX; |
| 377 |
} |
| 378 |
|
| 379 |
static SkEdge* sort_edges(SkEdge* list[], int count, SkEdge** last) { |
| 380 |
SkTQSort(list, list + count, compare_edges); |
| 381 |
|
| 382 |
// now make the edges linked in sorted order |
| 383 |
for (int i = 1; i < count; i++) { |
| 384 |
list[i - 1]->fNext = list[i]; |
| 385 |
list[i]->fPrev = list[i - 1]; |
| 386 |
} |
| 387 |
|
| 388 |
*last = list[count - 1]; |
| 389 |
return list[0]; |
| 390 |
} |
| 391 |
|
| 392 |
static void sk_fill_path(const SkPathRaw& raw, const SkIRect& clipRect, SkBlitter* blitter, |
| 393 |
int start_y, int stop_y, bool pathContainedInClip) { |
| 394 |
SkASSERT(blitter); |
| 395 |
|
| 396 |
SkBasicEdgeBuilder builder; |
| 397 |
int count = builder.buildEdges(raw, pathContainedInClip ? nullptr : &clipRect); |
| 398 |
SkEdge** list = builder.edgeList(); |
| 399 |
|
| 400 |
if (0 == count) { |
| 401 |
if (raw.isInverseFillType()) { |
| 402 |
/* |
| 403 |
* Since we are in inverse-fill, our caller has already drawn above |
| 404 |
* our top (start_y) and will draw below our bottom (stop_y). Thus |
| 405 |
* we need to restrict our drawing to the intersection of the clip |
| 406 |
* and those two limits. |
| 407 |
*/ |
| 408 |
SkIRect rect = clipRect; |
| 409 |
if (rect.fTop < start_y) { |
| 410 |
rect.fTop = start_y; |
| 411 |
} |
| 412 |
if (rect.fBottom > stop_y) { |
| 413 |
rect.fBottom = stop_y; |
| 414 |
} |
| 415 |
if (!rect.isEmpty()) { |
| 416 |
blitter->blitRect(rect.fLeft, |
| 417 |
rect.fTop, |
| 418 |
rect.width(), |
| 419 |
rect.height()); |
| 420 |
} |
| 421 |
} |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
SkEdge headEdge, tailEdge, *last; |
| 426 |
// this returns the first and last edge after they're sorted into a dlink list |
| 427 |
SkEdge* edge = sort_edges(list, count, &last); |
| 428 |
|
| 429 |
headEdge.fPrev = nullptr; |
| 430 |
headEdge.fNext = edge; |
| 431 |
headEdge.fFirstY = kEDGE_HEAD_Y; |
| 432 |
headEdge.fX = SK_MinS32; |
| 433 |
edge->fPrev = &headEdge; |
| 434 |
|
| 435 |
tailEdge.fPrev = last; |
| 436 |
tailEdge.fNext = nullptr; |
| 437 |
tailEdge.fFirstY = kEDGE_TAIL_Y; |
| 438 |
last->fNext = &tailEdge; |
| 439 |
|
| 440 |
// now edge is the head of the sorted linklist |
| 441 |
if (!pathContainedInClip && start_y < clipRect.fTop) { |
| 442 |
start_y = clipRect.fTop; |
| 443 |
} |
| 444 |
if (!pathContainedInClip && stop_y > clipRect.fBottom) { |
| 445 |
stop_y = clipRect.fBottom; |
| 446 |
} |
| 447 |
|
| 448 |
InverseBlitter ib; |
| 449 |
PrePostProc proc = nullptr; |
| 450 |
|
| 451 |
if (raw.isInverseFillType()) { |
| 452 |
ib.setBlitter(blitter, clipRect); |
| 453 |
blitter = &ib; |
| 454 |
proc = PrePostInverseBlitterProc; |
| 455 |
} |
| 456 |
|
| 457 |
// count >= 2 is required as the convex walker does not handle missing right edges |
| 458 |
if (raw.isKnownToBeConvex() && (nullptr == proc) && count >= 2) { |
| 459 |
walk_simple_edges(&headEdge, blitter, start_y, stop_y); |
| 460 |
} else { |
| 461 |
walk_edges(&headEdge, raw.fillType(), blitter, start_y, stop_y, proc, |
| 462 |
clipRect.right()); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
void sk_blit_above(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) { |
| 467 |
const SkIRect& cr = clip.getBounds(); |
| 468 |
SkIRect tmp; |
| 469 |
|
| 470 |
tmp.fLeft = cr.fLeft; |
| 471 |
tmp.fRight = cr.fRight; |
| 472 |
tmp.fTop = cr.fTop; |
| 473 |
tmp.fBottom = ir.fTop; |
| 474 |
if (!tmp.isEmpty()) { |
| 475 |
blitter->blitRectRegion(tmp, clip); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
void sk_blit_below(SkBlitter* blitter, const SkIRect& ir, const SkRegion& clip) { |
| 480 |
const SkIRect& cr = clip.getBounds(); |
| 481 |
SkIRect tmp; |
| 482 |
|
| 483 |
tmp.fLeft = cr.fLeft; |
| 484 |
tmp.fRight = cr.fRight; |
| 485 |
tmp.fTop = ir.fBottom; |
| 486 |
tmp.fBottom = cr.fBottom; |
| 487 |
if (!tmp.isEmpty()) { |
| 488 |
blitter->blitRectRegion(tmp, clip); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
/////////////////////////////////////////////////////////////////////////////// |
| 493 |
|
| 494 |
/** |
| 495 |
* If the caller is drawing an inverse-fill path, then it pass true for |
| 496 |
* skipRejectTest, so we don't abort drawing just because the src bounds (ir) |
| 497 |
* is outside of the clip. |
| 498 |
*/ |
| 499 |
SkScanClipper::SkScanClipper(SkBlitter* blitter, const SkRegion* clip, |
| 500 |
const SkIRect& ir, bool skipRejectTest, bool irPreClipped) { |
| 501 |
fBlitter = nullptr; // null means blit nothing |
| 502 |
fClipRect = nullptr; |
| 503 |
|
| 504 |
if (clip) { |
| 505 |
fClipRect = &clip->getBounds(); |
| 506 |
if (!skipRejectTest && !SkIRect::Intersects(*fClipRect, ir)) { // completely clipped out |
| 507 |
return; |
| 508 |
} |
| 509 |
|
| 510 |
if (clip->isRect()) { |
| 511 |
if (!irPreClipped && fClipRect->contains(ir)) { |
| 512 |
#ifdef SK_DEBUG |
| 513 |
fRectClipCheckBlitter.init(blitter, *fClipRect); |
| 514 |
blitter = &fRectClipCheckBlitter; |
| 515 |
#endif |
| 516 |
fClipRect = nullptr; |
| 517 |
} else { |
| 518 |
// only need a wrapper blitter if we're horizontally clipped |
| 519 |
if (irPreClipped || |
| 520 |
fClipRect->fLeft > ir.fLeft || fClipRect->fRight < ir.fRight) { |
| 521 |
fRectBlitter.init(blitter, *fClipRect); |
| 522 |
blitter = &fRectBlitter; |
| 523 |
} else { |
| 524 |
#ifdef SK_DEBUG |
| 525 |
fRectClipCheckBlitter.init(blitter, *fClipRect); |
| 526 |
blitter = &fRectClipCheckBlitter; |
| 527 |
#endif |
| 528 |
} |
| 529 |
} |
| 530 |
} else { |
| 531 |
fRgnBlitter.init(blitter, clip); |
| 532 |
blitter = &fRgnBlitter; |
| 533 |
} |
| 534 |
} |
| 535 |
fBlitter = blitter; |
| 536 |
} |
| 537 |
|
| 538 |
/////////////////////////////////////////////////////////////////////////////// |
| 539 |
|
| 540 |
static bool clip_to_limit(const SkRegion& orig, SkRegion* reduced) { |
| 541 |
// need to limit coordinates such that the width/height of our rect can be represented |
| 542 |
// in SkFixed (16.16). See skbug.com/40039252 |
| 543 |
const int32_t limit = 32767 >> 1; |
| 544 |
|
| 545 |
SkIRect limitR; |
| 546 |
limitR.setLTRB(-limit, -limit, limit, limit); |
| 547 |
if (limitR.contains(orig.getBounds())) { |
| 548 |
return false; |
| 549 |
} |
| 550 |
reduced->op(orig, limitR, SkRegion::kIntersect_Op); |
| 551 |
return true; |
| 552 |
} |
| 553 |
|
| 554 |
// Bias used for conservative rounding of float rects to int rects, to nudge the irects a little |
| 555 |
// larger, so we don't "think" a path's bounds are inside a clip, when (due to numeric drift in |
| 556 |
// the scan-converter) we might walk beyond the predicted limits. |
| 557 |
// |
| 558 |
// This value has been determined trial and error: pick the smallest value (after the 0.5) that |
| 559 |
// fixes any problematic cases (e.g. crbug.com/844457) |
| 560 |
// NOTE: cubics appear to be the main reason for needing this slop. If we could (perhaps) have a |
| 561 |
// more accurate walker for cubics, we may be able to reduce this fudge factor. |
| 562 |
static const double kConservativeRoundBias = 0.5 + 1.5 / SK_FDot6One; |
| 563 |
|
| 564 |
/** |
| 565 |
* Round the value down. This is used to round the top and left of a rectangle, |
| 566 |
* and corresponds to the way the scan converter treats the top and left edges. |
| 567 |
* It has a slight bias to make the "rounded" int smaller than a normal round, to create a more |
| 568 |
* conservative int-bounds (larger) from a float rect. |
| 569 |
*/ |
| 570 |
static inline int round_down_to_int(SkScalar x) { |
| 571 |
double xx = x; |
| 572 |
xx -= kConservativeRoundBias; |
| 573 |
return sk_double_saturate2int(ceil(xx)); |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* Round the value up. This is used to round the right and bottom of a rectangle. |
| 578 |
* It has a slight bias to make the "rounded" int smaller than a normal round, to create a more |
| 579 |
* conservative int-bounds (larger) from a float rect. |
| 580 |
*/ |
| 581 |
static inline int round_up_to_int(SkScalar x) { |
| 582 |
double xx = x; |
| 583 |
xx += kConservativeRoundBias; |
| 584 |
return sk_double_saturate2int(floor(xx)); |
| 585 |
} |
| 586 |
|
| 587 |
/* |
| 588 |
* Conservative rounding function, which effectively nudges the int-rect to be slightly larger |
| 589 |
* than SkRect::round() might have produced. This is a safety-net for the scan-converter, which |
| 590 |
* inspects the returned int-rect, and may disable clipping (for speed) if it thinks all of the |
| 591 |
* edges will fit inside the clip's bounds. The scan-converter introduces slight numeric errors |
| 592 |
* due to accumulated += of the slope, so this function is used to return a conservatively large |
| 593 |
* int-bounds, and thus we will only disable clipping if we're sure the edges will stay in-bounds. |
| 594 |
*/ |
| 595 |
static SkIRect conservative_round_to_int(const SkRect& src) { |
| 596 |
return { |
| 597 |
round_down_to_int(src.fLeft), |
| 598 |
round_down_to_int(src.fTop), |
| 599 |
round_up_to_int(src.fRight), |
| 600 |
round_up_to_int(src.fBottom), |
| 601 |
}; |
| 602 |
} |
| 603 |
|
| 604 |
void SkScan::FillPath(const SkPathRaw& raw, const SkRegion& origClip, SkBlitter* blitter) { |
| 605 |
if (origClip.isEmpty()) { |
| 606 |
return; |
| 607 |
} |
| 608 |
|
| 609 |
// Our edges are fixed-point, and don't like the bounds of the clip to |
| 610 |
// exceed that. Here we trim the clip just so we don't overflow later on |
| 611 |
const SkRegion* clipPtr = &origClip; |
| 612 |
SkRegion finiteClip; |
| 613 |
if (clip_to_limit(origClip, &finiteClip)) { |
| 614 |
if (finiteClip.isEmpty()) { |
| 615 |
return; |
| 616 |
} |
| 617 |
clipPtr = &finiteClip; |
| 618 |
} |
| 619 |
// don't reference "origClip" any more, just use clipPtr |
| 620 |
|
| 621 |
|
| 622 |
SkRect bounds = raw.bounds(); |
| 623 |
bool irPreClipped = false; |
| 624 |
if (!SkRectPriv::MakeLargeS32().contains(bounds)) { |
| 625 |
if (!bounds.intersect(SkRectPriv::MakeLargeS32())) { |
| 626 |
bounds.setEmpty(); |
| 627 |
} |
| 628 |
irPreClipped = true; |
| 629 |
} |
| 630 |
|
| 631 |
SkIRect ir = conservative_round_to_int(bounds); |
| 632 |
if (ir.isEmpty()) { |
| 633 |
if (raw.isInverseFillType()) { |
| 634 |
blitter->blitRegion(*clipPtr); |
| 635 |
} |
| 636 |
return; |
| 637 |
} |
| 638 |
|
| 639 |
SkScanClipper clipper(blitter, clipPtr, ir, raw.isInverseFillType(), irPreClipped); |
| 640 |
|
| 641 |
blitter = clipper.getBlitter(); |
| 642 |
if (blitter) { |
| 643 |
// we have to keep our calls to blitter in sorted order, so we |
| 644 |
// must blit the above section first, then the middle, then the bottom. |
| 645 |
if (raw.isInverseFillType()) { |
| 646 |
sk_blit_above(blitter, ir, *clipPtr); |
| 647 |
} |
| 648 |
SkASSERT(clipper.getClipRect() == nullptr || |
| 649 |
*clipper.getClipRect() == clipPtr->getBounds()); |
| 650 |
sk_fill_path(raw, clipPtr->getBounds(), blitter, ir.fTop, ir.fBottom, |
| 651 |
clipper.getClipRect() == nullptr); |
| 652 |
if (raw.isInverseFillType()) { |
| 653 |
sk_blit_below(blitter, ir, *clipPtr); |
| 654 |
} |
| 655 |
} else { |
| 656 |
// what does it mean to not have a blitter if path.isInverseFillType??? |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
bool SkScan::PathRequiresTiling(const SkIRect& bounds) { |
| 661 |
SkRegion out; // ignored |
| 662 |
return clip_to_limit(SkRegion(bounds), &out); |
| 663 |
} |
| 664 |
|
| 665 |
/////////////////////////////////////////////////////////////////////////////// |
| 666 |
|
| 667 |
static int build_tri_edges(SkEdge edge[], const SkPoint pts[], |
| 668 |
const SkIRect* clipRect, SkEdge* list[]) { |
| 669 |
SkEdge** start = list; |
| 670 |
|
| 671 |
if (edge->setLine(pts[0], pts[1], clipRect)) { |
| 672 |
*list++ = edge; |
| 673 |
edge++; |
| 674 |
} |
| 675 |
if (edge->setLine(pts[1], pts[2], clipRect)) { |
| 676 |
*list++ = edge; |
| 677 |
edge++; |
| 678 |
} |
| 679 |
if (edge->setLine(pts[2], pts[0], clipRect)) { |
| 680 |
*list++ = edge; |
| 681 |
} |
| 682 |
return (int)(list - start); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
static void sk_fill_triangle(const SkPoint pts[], const SkIRect* clipRect, |
| 687 |
SkBlitter* blitter, const SkIRect& ir) { |
| 688 |
SkASSERT(pts && blitter); |
| 689 |
|
| 690 |
SkEdge edgeStorage[3]; |
| 691 |
SkEdge* list[3]; |
| 692 |
|
| 693 |
int count = build_tri_edges(edgeStorage, pts, clipRect, list); |
| 694 |
if (count < 2) { |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
SkEdge headEdge, tailEdge, *last; |
| 699 |
|
| 700 |
// this returns the first and last edge after they're sorted into a dlink list |
| 701 |
SkEdge* edge = sort_edges(list, count, &last); |
| 702 |
|
| 703 |
headEdge.fPrev = nullptr; |
| 704 |
headEdge.fNext = edge; |
| 705 |
headEdge.fFirstY = kEDGE_HEAD_Y; |
| 706 |
headEdge.fX = SK_MinS32; |
| 707 |
edge->fPrev = &headEdge; |
| 708 |
|
| 709 |
tailEdge.fPrev = last; |
| 710 |
tailEdge.fNext = nullptr; |
| 711 |
tailEdge.fFirstY = kEDGE_TAIL_Y; |
| 712 |
last->fNext = &tailEdge; |
| 713 |
|
| 714 |
// now edge is the head of the sorted linklist |
| 715 |
int stop_y = ir.fBottom; |
| 716 |
if (clipRect && stop_y > clipRect->fBottom) { |
| 717 |
stop_y = clipRect->fBottom; |
| 718 |
} |
| 719 |
int start_y = ir.fTop; |
| 720 |
if (clipRect && start_y < clipRect->fTop) { |
| 721 |
start_y = clipRect->fTop; |
| 722 |
} |
| 723 |
walk_simple_edges(&headEdge, blitter, start_y, stop_y); |
| 724 |
} |
| 725 |
|
| 726 |
void SkScan::FillTriangle(const SkPoint pts[], const SkRasterClip& clip, |
| 727 |
SkBlitter* blitter) { |
| 728 |
if (clip.isEmpty()) { |
| 729 |
return; |
| 730 |
} |
| 731 |
|
| 732 |
const auto r = SkRect::Bounds({pts, 3}); |
| 733 |
if (!r) { |
| 734 |
return; |
| 735 |
} |
| 736 |
|
| 737 |
// If r is too large (larger than can easily fit in SkFixed) then we need perform geometric |
| 738 |
// clipping. This is a bit of work, so we just call the general FillPath() to handle it. |
| 739 |
// Use FixedMax/2 as the limit so we can subtract two edges and still store that in Fixed. |
| 740 |
const SkScalar limit = SK_MaxS16 >> 1; |
| 741 |
if (!SkRect::MakeLTRB(-limit, -limit, limit, limit).contains(*r)) { |
| 742 |
SkPathRawShapes::Triangle tri({pts, 3}, *r); |
| 743 |
FillPath(tri, clip, blitter); |
| 744 |
return; |
| 745 |
} |
| 746 |
|
| 747 |
SkIRect ir = conservative_round_to_int(*r); |
| 748 |
if (ir.isEmpty() || !SkIRect::Intersects(ir, clip.getBounds())) { |
| 749 |
return; |
| 750 |
} |
| 751 |
|
| 752 |
SkAAClipBlitterWrapper wrap; |
| 753 |
const SkRegion* clipRgn; |
| 754 |
if (clip.isBW()) { |
| 755 |
clipRgn = &clip.bwRgn(); |
| 756 |
} else { |
| 757 |
wrap.init(clip, blitter); |
| 758 |
clipRgn = &wrap.getRgn(); |
| 759 |
blitter = wrap.getBlitter(); |
| 760 |
} |
| 761 |
|
| 762 |
SkScanClipper clipper(blitter, clipRgn, ir); |
| 763 |
blitter = clipper.getBlitter(); |
| 764 |
if (blitter) { |
| 765 |
sk_fill_triangle(pts, clipper.getClipRect(), blitter, ir); |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
void SkScan::FillPath(const SkPathRaw& raw, const SkRasterClip& clip, SkBlitter* blitter) { |
| 770 |
if (clip.isEmpty()) { |
| 771 |
return; |
| 772 |
} |
| 773 |
|
| 774 |
if (clip.isBW()) { |
| 775 |
SkScan::FillPath(raw, clip.bwRgn(), blitter); |
| 776 |
} else { |
| 777 |
SkRegion tmp; |
| 778 |
SkAAClipBlitter aaBlitter; |
| 779 |
|
| 780 |
tmp.setRect(clip.getBounds()); |
| 781 |
aaBlitter.init(blitter, &clip.aaRgn()); |
| 782 |
SkScan::FillPath(raw, tmp, &aaBlitter); |
| 783 |
} |
| 784 |
} |
| 785 |
|