| 1 |
/* |
| 2 |
* Copyright 2011 Google Inc. |
| 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 "src/core/SkAAClip.h" |
| 9 |
|
| 10 |
#include "include/core/SkClipOp.h" |
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkRegion.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "include/private/base/SkCPUTypes.h" |
| 15 |
#include "include/private/base/SkDebug.h" |
| 16 |
#include "include/private/base/SkMacros.h" |
| 17 |
#include "include/private/base/SkMalloc.h" |
| 18 |
#include "include/private/base/SkMath.h" |
| 19 |
#include "include/private/base/SkTDArray.h" |
| 20 |
#include "include/private/base/SkTo.h" |
| 21 |
#include "src/core/SkBlitter.h" |
| 22 |
#include "src/core/SkColorData.h" |
| 23 |
#include "src/core/SkMask.h" |
| 24 |
#include "src/core/SkPathPriv.h" |
| 25 |
#include "src/core/SkScan.h" |
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <atomic> |
| 29 |
#include <cstring> |
| 30 |
|
| 31 |
namespace { |
| 32 |
|
| 33 |
class AutoAAClipValidate { |
| 34 |
public: |
| 35 |
AutoAAClipValidate(const SkAAClip& clip) : fClip(clip) { |
| 36 |
fClip.validate(); |
| 37 |
} |
| 38 |
~AutoAAClipValidate() { |
| 39 |
fClip.validate(); |
| 40 |
} |
| 41 |
private: |
| 42 |
const SkAAClip& fClip; |
| 43 |
}; |
| 44 |
|
| 45 |
#ifdef SK_DEBUG |
| 46 |
#define AUTO_AACLIP_VALIDATE(clip) AutoAAClipValidate acv(clip) |
| 47 |
#else |
| 48 |
#define AUTO_AACLIP_VALIDATE(clip) |
| 49 |
#endif |
| 50 |
|
| 51 |
/////////////////////////////////////////////////////////////////////////////// |
| 52 |
|
| 53 |
static constexpr int32_t kMaxInt32 = 0x7FFFFFFF; |
| 54 |
|
| 55 |
#ifdef SK_DEBUG |
| 56 |
// assert we're exactly width-wide, and then return the number of bytes used |
| 57 |
static size_t compute_row_length(const uint8_t row[], int width) { |
| 58 |
const uint8_t* origRow = row; |
| 59 |
while (width > 0) { |
| 60 |
int n = row[0]; |
| 61 |
SkASSERT(n > 0); |
| 62 |
SkASSERT(n <= width); |
| 63 |
row += 2; |
| 64 |
width -= n; |
| 65 |
} |
| 66 |
SkASSERT(0 == width); |
| 67 |
return row - origRow; |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
/* |
| 72 |
* Data runs are packed [count, alpha] |
| 73 |
*/ |
| 74 |
struct YOffset { |
| 75 |
int32_t fY; |
| 76 |
uint32_t fOffset; |
| 77 |
}; |
| 78 |
|
| 79 |
class RowIter { |
| 80 |
public: |
| 81 |
RowIter(const uint8_t* row, const SkIRect& bounds) { |
| 82 |
fRow = row; |
| 83 |
fLeft = bounds.fLeft; |
| 84 |
fBoundsRight = bounds.fRight; |
| 85 |
if (row) { |
| 86 |
fRight = bounds.fLeft + row[0]; |
| 87 |
SkASSERT(fRight <= fBoundsRight); |
| 88 |
fAlpha = row[1]; |
| 89 |
fDone = false; |
| 90 |
} else { |
| 91 |
fDone = true; |
| 92 |
fRight = kMaxInt32; |
| 93 |
fAlpha = 0; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
bool done() const { return fDone; } |
| 98 |
int left() const { return fLeft; } |
| 99 |
int right() const { return fRight; } |
| 100 |
U8CPU alpha() const { return fAlpha; } |
| 101 |
void next() { |
| 102 |
if (!fDone) { |
| 103 |
fLeft = fRight; |
| 104 |
if (fRight == fBoundsRight) { |
| 105 |
fDone = true; |
| 106 |
fRight = kMaxInt32; |
| 107 |
fAlpha = 0; |
| 108 |
} else { |
| 109 |
fRow += 2; |
| 110 |
fRight += fRow[0]; |
| 111 |
fAlpha = fRow[1]; |
| 112 |
SkASSERT(fRight <= fBoundsRight); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
private: |
| 118 |
const uint8_t* fRow; |
| 119 |
int fLeft; |
| 120 |
int fRight; |
| 121 |
int fBoundsRight; |
| 122 |
bool fDone; |
| 123 |
uint8_t fAlpha; |
| 124 |
}; |
| 125 |
|
| 126 |
class Iter { |
| 127 |
public: |
| 128 |
Iter() = default; |
| 129 |
|
| 130 |
Iter(int y, const uint8_t* data, const YOffset* start, const YOffset* end) |
| 131 |
: fCurrYOff(start) |
| 132 |
, fStopYOff(end) |
| 133 |
, fData(data + start->fOffset) |
| 134 |
, fTop(y) |
| 135 |
, fBottom(y + start->fY + 1) |
| 136 |
, fDone(false) {} |
| 137 |
|
| 138 |
bool done() const { return fDone; } |
| 139 |
int top() const { return fTop; } |
| 140 |
int bottom() const { return fBottom; } |
| 141 |
const uint8_t* data() const { return fData; } |
| 142 |
|
| 143 |
void next() { |
| 144 |
if (!fDone) { |
| 145 |
const YOffset* prev = fCurrYOff; |
| 146 |
const YOffset* curr = prev + 1; |
| 147 |
SkASSERT(curr <= fStopYOff); |
| 148 |
|
| 149 |
fTop = fBottom; |
| 150 |
if (curr >= fStopYOff) { |
| 151 |
fDone = true; |
| 152 |
fBottom = kMaxInt32; |
| 153 |
fData = nullptr; |
| 154 |
} else { |
| 155 |
fBottom += curr->fY - prev->fY; |
| 156 |
fData += curr->fOffset - prev->fOffset; |
| 157 |
fCurrYOff = curr; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private: |
| 163 |
const YOffset* fCurrYOff = nullptr; |
| 164 |
const YOffset* fStopYOff = nullptr; |
| 165 |
const uint8_t* fData = nullptr; |
| 166 |
|
| 167 |
int fTop = kMaxInt32; |
| 168 |
int fBottom = kMaxInt32; |
| 169 |
bool fDone = true; |
| 170 |
}; |
| 171 |
|
| 172 |
} // namespace |
| 173 |
|
| 174 |
/////////////////////////////////////////////////////////////////////////////// |
| 175 |
|
| 176 |
struct SkAAClip::RunHead { |
| 177 |
std::atomic<int32_t> fRefCnt; |
| 178 |
int32_t fRowCount; |
| 179 |
size_t fDataSize; |
| 180 |
|
| 181 |
YOffset* yoffsets() { |
| 182 |
return (YOffset*)((char*)this + sizeof(RunHead)); |
| 183 |
} |
| 184 |
const YOffset* yoffsets() const { |
| 185 |
return (const YOffset*)((const char*)this + sizeof(RunHead)); |
| 186 |
} |
| 187 |
uint8_t* data() { |
| 188 |
return (uint8_t*)(this->yoffsets() + fRowCount); |
| 189 |
} |
| 190 |
const uint8_t* data() const { |
| 191 |
return (const uint8_t*)(this->yoffsets() + fRowCount); |
| 192 |
} |
| 193 |
|
| 194 |
static RunHead* Alloc(int rowCount, size_t dataSize) { |
| 195 |
size_t size = sizeof(RunHead) + rowCount * sizeof(YOffset) + dataSize; |
| 196 |
RunHead* head = (RunHead*)sk_malloc_throw(size); |
| 197 |
head->fRefCnt.store(1); |
| 198 |
head->fRowCount = rowCount; |
| 199 |
head->fDataSize = dataSize; |
| 200 |
return head; |
| 201 |
} |
| 202 |
|
| 203 |
static int ComputeRowSizeForWidth(int width) { |
| 204 |
// 2 bytes per segment, where each segment can store up to 255 for count |
| 205 |
int segments = 0; |
| 206 |
while (width > 0) { |
| 207 |
segments += 1; |
| 208 |
int n = std::min(width, 255); |
| 209 |
width -= n; |
| 210 |
} |
| 211 |
return segments * 2; // each segment is row[0] + row[1] (n + alpha) |
| 212 |
} |
| 213 |
|
| 214 |
static RunHead* AllocRect(const SkIRect& bounds) { |
| 215 |
SkASSERT(!bounds.isEmpty()); |
| 216 |
int width = bounds.width(); |
| 217 |
size_t rowSize = ComputeRowSizeForWidth(width); |
| 218 |
RunHead* head = RunHead::Alloc(1, rowSize); |
| 219 |
YOffset* yoff = head->yoffsets(); |
| 220 |
yoff->fY = bounds.height() - 1; |
| 221 |
yoff->fOffset = 0; |
| 222 |
uint8_t* row = head->data(); |
| 223 |
while (width > 0) { |
| 224 |
int n = std::min(width, 255); |
| 225 |
row[0] = n; |
| 226 |
row[1] = 0xFF; |
| 227 |
width -= n; |
| 228 |
row += 2; |
| 229 |
} |
| 230 |
return head; |
| 231 |
} |
| 232 |
|
| 233 |
static Iter Iterate(const SkAAClip& clip) { |
| 234 |
const RunHead* head = clip.fRunHead; |
| 235 |
if (!clip.fRunHead) { |
| 236 |
// A null run head is an empty clip, so return aan already finished iterator. |
| 237 |
return Iter(); |
| 238 |
} |
| 239 |
|
| 240 |
return Iter(clip.getBounds().fTop, head->data(), head->yoffsets(), |
| 241 |
head->yoffsets() + head->fRowCount); |
| 242 |
} |
| 243 |
}; |
| 244 |
|
| 245 |
/////////////////////////////////////////////////////////////////////////////// |
| 246 |
|
| 247 |
class SkAAClip::Builder { |
| 248 |
class Blitter; |
| 249 |
|
| 250 |
SkIRect fBounds; |
| 251 |
struct Row { |
| 252 |
int fY; |
| 253 |
int fWidth; |
| 254 |
SkTDArray<uint8_t>* fData; |
| 255 |
}; |
| 256 |
SkTDArray<Row> fRows; |
| 257 |
Row* fCurrRow; |
| 258 |
int fPrevY; |
| 259 |
int fWidth; |
| 260 |
int fMinY; |
| 261 |
|
| 262 |
public: |
| 263 |
Builder(const SkIRect& bounds) : fBounds(bounds) { |
| 264 |
fPrevY = -1; |
| 265 |
fWidth = bounds.width(); |
| 266 |
fCurrRow = nullptr; |
| 267 |
fMinY = bounds.fTop; |
| 268 |
} |
| 269 |
|
| 270 |
~Builder() { |
| 271 |
Row* row = fRows.begin(); |
| 272 |
Row* stop = fRows.end(); |
| 273 |
while (row < stop) { |
| 274 |
delete row->fData; |
| 275 |
row += 1; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
bool applyClipOp(SkAAClip* target, const SkAAClip& other, SkClipOp op); |
| 280 |
bool blitPath(SkAAClip* target, const SkPath& path, bool doAA); |
| 281 |
|
| 282 |
private: |
| 283 |
using AlphaProc = U8CPU (*)(U8CPU alphaA, U8CPU alphaB); |
| 284 |
void operateX(int lastY, RowIter& iterA, RowIter& iterB, AlphaProc proc); |
| 285 |
void operateY(const SkAAClip& A, const SkAAClip& B, SkClipOp op); |
| 286 |
|
| 287 |
void addRun(int x, int y, U8CPU alpha, int count) { |
| 288 |
SkASSERT(count > 0); |
| 289 |
SkASSERT(fBounds.contains(x, y)); |
| 290 |
SkASSERT(fBounds.contains(x + count - 1, y)); |
| 291 |
|
| 292 |
x -= fBounds.left(); |
| 293 |
y -= fBounds.top(); |
| 294 |
|
| 295 |
Row* row = fCurrRow; |
| 296 |
if (y != fPrevY) { |
| 297 |
SkASSERT(y > fPrevY); |
| 298 |
fPrevY = y; |
| 299 |
row = this->flushRow(true); |
| 300 |
row->fY = y; |
| 301 |
row->fWidth = 0; |
| 302 |
SkASSERT(row->fData); |
| 303 |
SkASSERT(row->fData->empty()); |
| 304 |
fCurrRow = row; |
| 305 |
} |
| 306 |
|
| 307 |
SkASSERT(row->fWidth <= x); |
| 308 |
SkASSERT(row->fWidth < fBounds.width()); |
| 309 |
|
| 310 |
SkTDArray<uint8_t>& data = *row->fData; |
| 311 |
|
| 312 |
int gap = x - row->fWidth; |
| 313 |
if (gap) { |
| 314 |
AppendRun(data, 0, gap); |
| 315 |
row->fWidth += gap; |
| 316 |
SkASSERT(row->fWidth < fBounds.width()); |
| 317 |
} |
| 318 |
|
| 319 |
AppendRun(data, alpha, count); |
| 320 |
row->fWidth += count; |
| 321 |
SkASSERT(row->fWidth <= fBounds.width()); |
| 322 |
} |
| 323 |
|
| 324 |
void addColumn(int x, int y, U8CPU alpha, int height) { |
| 325 |
SkASSERT(fBounds.contains(x, y + height - 1)); |
| 326 |
|
| 327 |
this->addRun(x, y, alpha, 1); |
| 328 |
this->flushRowH(fCurrRow); |
| 329 |
y -= fBounds.fTop; |
| 330 |
SkASSERT(y == fCurrRow->fY); |
| 331 |
fCurrRow->fY = y + height - 1; |
| 332 |
} |
| 333 |
|
| 334 |
void addRectRun(int x, int y, int width, int height) { |
| 335 |
SkASSERT(fBounds.contains(x + width - 1, y + height - 1)); |
| 336 |
this->addRun(x, y, 0xFF, width); |
| 337 |
|
| 338 |
// we assum the rect must be all we'll see for these scanlines |
| 339 |
// so we ensure our row goes all the way to our right |
| 340 |
this->flushRowH(fCurrRow); |
| 341 |
|
| 342 |
y -= fBounds.fTop; |
| 343 |
SkASSERT(y == fCurrRow->fY); |
| 344 |
fCurrRow->fY = y + height - 1; |
| 345 |
} |
| 346 |
|
| 347 |
void addAntiRectRun(int x, int y, int width, int height, |
| 348 |
SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 349 |
// According to SkBlitter.cpp, no matter whether leftAlpha is 0 or positive, |
| 350 |
// we should always consider [x, x+1] as the left-most column and [x+1, x+1+width] |
| 351 |
// as the rect with full alpha. |
| 352 |
SkASSERT(fBounds.contains(x + width + (rightAlpha > 0 ? 1 : 0), |
| 353 |
y + height - 1)); |
| 354 |
SkASSERT(width >= 0); |
| 355 |
|
| 356 |
// Conceptually we're always adding 3 runs, but we should |
| 357 |
// merge or omit them if possible. |
| 358 |
if (leftAlpha == 0xFF) { |
| 359 |
width++; |
| 360 |
} else if (leftAlpha > 0) { |
| 361 |
this->addRun(x++, y, leftAlpha, 1); |
| 362 |
} else { |
| 363 |
// leftAlpha is 0, ignore the left column |
| 364 |
x++; |
| 365 |
} |
| 366 |
if (rightAlpha == 0xFF) { |
| 367 |
width++; |
| 368 |
} |
| 369 |
if (width > 0) { |
| 370 |
this->addRun(x, y, 0xFF, width); |
| 371 |
} |
| 372 |
if (rightAlpha > 0 && rightAlpha < 255) { |
| 373 |
this->addRun(x + width, y, rightAlpha, 1); |
| 374 |
} |
| 375 |
|
| 376 |
// if we never called addRun, we might not have a fCurrRow yet |
| 377 |
if (fCurrRow) { |
| 378 |
// we assume the rect must be all we'll see for these scanlines |
| 379 |
// so we ensure our row goes all the way to our right |
| 380 |
this->flushRowH(fCurrRow); |
| 381 |
|
| 382 |
y -= fBounds.fTop; |
| 383 |
SkASSERT(y == fCurrRow->fY); |
| 384 |
fCurrRow->fY = y + height - 1; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
bool finish(SkAAClip* target) { |
| 389 |
this->flushRow(false); |
| 390 |
|
| 391 |
const Row* row = fRows.begin(); |
| 392 |
const Row* stop = fRows.end(); |
| 393 |
|
| 394 |
size_t dataSize = 0; |
| 395 |
while (row < stop) { |
| 396 |
dataSize += row->fData->size(); |
| 397 |
row += 1; |
| 398 |
} |
| 399 |
|
| 400 |
if (0 == dataSize) { |
| 401 |
return target->setEmpty(); |
| 402 |
} |
| 403 |
|
| 404 |
SkASSERT(fMinY >= fBounds.fTop); |
| 405 |
SkASSERT(fMinY < fBounds.fBottom); |
| 406 |
int adjustY = fMinY - fBounds.fTop; |
| 407 |
fBounds.fTop = fMinY; |
| 408 |
|
| 409 |
RunHead* head = RunHead::Alloc(fRows.size(), dataSize); |
| 410 |
YOffset* yoffset = head->yoffsets(); |
| 411 |
uint8_t* data = head->data(); |
| 412 |
uint8_t* baseData = data; |
| 413 |
|
| 414 |
row = fRows.begin(); |
| 415 |
SkDEBUGCODE(int prevY = row->fY - 1;) |
| 416 |
while (row < stop) { |
| 417 |
SkASSERT(prevY < row->fY); // must be monotonic |
| 418 |
SkDEBUGCODE(prevY = row->fY); |
| 419 |
|
| 420 |
yoffset->fY = row->fY - adjustY; |
| 421 |
yoffset->fOffset = SkToU32(data - baseData); |
| 422 |
yoffset += 1; |
| 423 |
|
| 424 |
size_t n = row->fData->size(); |
| 425 |
memcpy(data, row->fData->begin(), n); |
| 426 |
SkASSERT(compute_row_length(data, fBounds.width()) == n); |
| 427 |
data += n; |
| 428 |
|
| 429 |
row += 1; |
| 430 |
} |
| 431 |
|
| 432 |
target->freeRuns(); |
| 433 |
target->fBounds = fBounds; |
| 434 |
target->fRunHead = head; |
| 435 |
return target->trimBounds(); |
| 436 |
} |
| 437 |
|
| 438 |
void dump() { |
| 439 |
this->validate(); |
| 440 |
int y; |
| 441 |
for (y = 0; y < fRows.size(); ++y) { |
| 442 |
const Row& row = fRows[y]; |
| 443 |
SkDebugf("Y:%3d W:%3d", row.fY, row.fWidth); |
| 444 |
const SkTDArray<uint8_t>& data = *row.fData; |
| 445 |
int count = data.size(); |
| 446 |
SkASSERT(!(count & 1)); |
| 447 |
const uint8_t* ptr = data.begin(); |
| 448 |
for (int x = 0; x < count; x += 2) { |
| 449 |
SkDebugf(" [%3d:%02X]", ptr[0], ptr[1]); |
| 450 |
ptr += 2; |
| 451 |
} |
| 452 |
SkDebugf("\n"); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
void validate() { |
| 457 |
#ifdef SK_DEBUG |
| 458 |
int prevY = -1; |
| 459 |
for (int i = 0; i < fRows.size(); ++i) { |
| 460 |
const Row& row = fRows[i]; |
| 461 |
SkASSERT(prevY < row.fY); |
| 462 |
SkASSERT(fWidth == row.fWidth); |
| 463 |
int count = row.fData->size(); |
| 464 |
const uint8_t* ptr = row.fData->begin(); |
| 465 |
SkASSERT(!(count & 1)); |
| 466 |
int w = 0; |
| 467 |
for (int x = 0; x < count; x += 2) { |
| 468 |
int n = ptr[0]; |
| 469 |
SkASSERT(n > 0); |
| 470 |
w += n; |
| 471 |
SkASSERT(w <= fWidth); |
| 472 |
ptr += 2; |
| 473 |
} |
| 474 |
SkASSERT(w == fWidth); |
| 475 |
prevY = row.fY; |
| 476 |
} |
| 477 |
#endif |
| 478 |
} |
| 479 |
|
| 480 |
void flushRowH(Row* row) { |
| 481 |
// flush current row if needed |
| 482 |
if (row->fWidth < fWidth) { |
| 483 |
AppendRun(*row->fData, 0, fWidth - row->fWidth); |
| 484 |
row->fWidth = fWidth; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
Row* flushRow(bool readyForAnother) { |
| 489 |
Row* next = nullptr; |
| 490 |
int count = fRows.size(); |
| 491 |
if (count > 0) { |
| 492 |
this->flushRowH(&fRows[count - 1]); |
| 493 |
} |
| 494 |
if (count > 1) { |
| 495 |
// are our last two runs the same? |
| 496 |
Row* prev = &fRows[count - 2]; |
| 497 |
Row* curr = &fRows[count - 1]; |
| 498 |
SkASSERT(prev->fWidth == fWidth); |
| 499 |
SkASSERT(curr->fWidth == fWidth); |
| 500 |
if (*prev->fData == *curr->fData) { |
| 501 |
prev->fY = curr->fY; |
| 502 |
if (readyForAnother) { |
| 503 |
curr->fData->clear(); |
| 504 |
next = curr; |
| 505 |
} else { |
| 506 |
delete curr->fData; |
| 507 |
fRows.removeShuffle(count - 1); |
| 508 |
} |
| 509 |
} else { |
| 510 |
if (readyForAnother) { |
| 511 |
next = fRows.append(); |
| 512 |
next->fData = new SkTDArray<uint8_t>; |
| 513 |
} |
| 514 |
} |
| 515 |
} else { |
| 516 |
if (readyForAnother) { |
| 517 |
next = fRows.append(); |
| 518 |
next->fData = new SkTDArray<uint8_t>; |
| 519 |
} |
| 520 |
} |
| 521 |
return next; |
| 522 |
} |
| 523 |
|
| 524 |
static void AppendRun(SkTDArray<uint8_t>& data, U8CPU alpha, int count) { |
| 525 |
do { |
| 526 |
int n = count; |
| 527 |
if (n > 255) { |
| 528 |
n = 255; |
| 529 |
} |
| 530 |
uint8_t* ptr = data.append(2); |
| 531 |
ptr[0] = n; |
| 532 |
ptr[1] = alpha; |
| 533 |
count -= n; |
| 534 |
} while (count > 0); |
| 535 |
} |
| 536 |
}; |
| 537 |
|
| 538 |
void SkAAClip::Builder::operateX(int lastY, RowIter& iterA, RowIter& iterB, AlphaProc proc) { |
| 539 |
auto advanceRowIter = [](RowIter& iter, int& iterLeft, int& iterRite, int rite) { |
| 540 |
if (rite == iterRite) { |
| 541 |
iter.next(); |
| 542 |
iterLeft = iter.left(); |
| 543 |
iterRite = iter.right(); |
| 544 |
} |
| 545 |
}; |
| 546 |
|
| 547 |
int leftA = iterA.left(); |
| 548 |
int riteA = iterA.right(); |
| 549 |
int leftB = iterB.left(); |
| 550 |
int riteB = iterB.right(); |
| 551 |
|
| 552 |
int prevRite = fBounds.fLeft; |
| 553 |
|
| 554 |
do { |
| 555 |
U8CPU alphaA = 0; |
| 556 |
U8CPU alphaB = 0; |
| 557 |
int left, rite; |
| 558 |
|
| 559 |
if (leftA < leftB) { |
| 560 |
left = leftA; |
| 561 |
alphaA = iterA.alpha(); |
| 562 |
if (riteA <= leftB) { |
| 563 |
rite = riteA; |
| 564 |
} else { |
| 565 |
rite = leftA = leftB; |
| 566 |
} |
| 567 |
} else if (leftB < leftA) { |
| 568 |
left = leftB; |
| 569 |
alphaB = iterB.alpha(); |
| 570 |
if (riteB <= leftA) { |
| 571 |
rite = riteB; |
| 572 |
} else { |
| 573 |
rite = leftB = leftA; |
| 574 |
} |
| 575 |
} else { |
| 576 |
left = leftA; // or leftB, since leftA == leftB |
| 577 |
rite = leftA = leftB = std::min(riteA, riteB); |
| 578 |
alphaA = iterA.alpha(); |
| 579 |
alphaB = iterB.alpha(); |
| 580 |
} |
| 581 |
|
| 582 |
if (left >= fBounds.fRight) { |
| 583 |
break; |
| 584 |
} |
| 585 |
if (rite > fBounds.fRight) { |
| 586 |
rite = fBounds.fRight; |
| 587 |
} |
| 588 |
|
| 589 |
if (left >= fBounds.fLeft) { |
| 590 |
SkASSERT(rite > left); |
| 591 |
this->addRun(left, lastY, proc(alphaA, alphaB), rite - left); |
| 592 |
prevRite = rite; |
| 593 |
} |
| 594 |
|
| 595 |
advanceRowIter(iterA, leftA, riteA, rite); |
| 596 |
advanceRowIter(iterB, leftB, riteB, rite); |
| 597 |
} while (!iterA.done() || !iterB.done()); |
| 598 |
|
| 599 |
if (prevRite < fBounds.fRight) { |
| 600 |
this->addRun(prevRite, lastY, 0, fBounds.fRight - prevRite); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
void SkAAClip::Builder::operateY(const SkAAClip& A, const SkAAClip& B, SkClipOp op) { |
| 605 |
static const AlphaProc kDiff = [](U8CPU a, U8CPU b) { return SkMulDiv255Round(a, 0xFF - b); }; |
| 606 |
static const AlphaProc kIntersect = [](U8CPU a, U8CPU b) { return SkMulDiv255Round(a, b); }; |
| 607 |
AlphaProc proc = (op == SkClipOp::kDifference) ? kDiff : kIntersect; |
| 608 |
|
| 609 |
Iter iterA = RunHead::Iterate(A); |
| 610 |
Iter iterB = RunHead::Iterate(B); |
| 611 |
|
| 612 |
SkASSERT(!iterA.done()); |
| 613 |
int topA = iterA.top(); |
| 614 |
int botA = iterA.bottom(); |
| 615 |
SkASSERT(!iterB.done()); |
| 616 |
int topB = iterB.top(); |
| 617 |
int botB = iterB.bottom(); |
| 618 |
|
| 619 |
auto advanceIter = [](Iter& iter, int& iterTop, int& iterBot, int bot) { |
| 620 |
if (bot == iterBot) { |
| 621 |
iter.next(); |
| 622 |
iterTop = iterBot; |
| 623 |
SkASSERT(iterBot == iter.top()); |
| 624 |
iterBot = iter.bottom(); |
| 625 |
} |
| 626 |
}; |
| 627 |
|
| 628 |
#if defined(SK_BUILD_FOR_FUZZER) |
| 629 |
if ((botA - topA) > 100000 || (botB - topB) > 100000) { |
| 630 |
return; |
| 631 |
} |
| 632 |
#endif |
| 633 |
|
| 634 |
do { |
| 635 |
const uint8_t* rowA = nullptr; |
| 636 |
const uint8_t* rowB = nullptr; |
| 637 |
int top, bot; |
| 638 |
|
| 639 |
if (topA < topB) { |
| 640 |
top = topA; |
| 641 |
rowA = iterA.data(); |
| 642 |
if (botA <= topB) { |
| 643 |
bot = botA; |
| 644 |
} else { |
| 645 |
bot = topA = topB; |
| 646 |
} |
| 647 |
|
| 648 |
} else if (topB < topA) { |
| 649 |
top = topB; |
| 650 |
rowB = iterB.data(); |
| 651 |
if (botB <= topA) { |
| 652 |
bot = botB; |
| 653 |
} else { |
| 654 |
bot = topB = topA; |
| 655 |
} |
| 656 |
} else { |
| 657 |
top = topA; // or topB, since topA == topB |
| 658 |
bot = topA = topB = std::min(botA, botB); |
| 659 |
rowA = iterA.data(); |
| 660 |
rowB = iterB.data(); |
| 661 |
} |
| 662 |
|
| 663 |
if (top >= fBounds.fBottom) { |
| 664 |
break; |
| 665 |
} |
| 666 |
|
| 667 |
if (bot > fBounds.fBottom) { |
| 668 |
bot = fBounds.fBottom; |
| 669 |
} |
| 670 |
SkASSERT(top < bot); |
| 671 |
|
| 672 |
if (!rowA && !rowB) { |
| 673 |
this->addRun(fBounds.fLeft, bot - 1, 0, fBounds.width()); |
| 674 |
} else if (top >= fBounds.fTop) { |
| 675 |
SkASSERT(bot <= fBounds.fBottom); |
| 676 |
RowIter rowIterA(rowA, rowA ? A.getBounds() : fBounds); |
| 677 |
RowIter rowIterB(rowB, rowB ? B.getBounds() : fBounds); |
| 678 |
this->operateX(bot - 1, rowIterA, rowIterB, proc); |
| 679 |
} |
| 680 |
|
| 681 |
advanceIter(iterA, topA, botA, bot); |
| 682 |
advanceIter(iterB, topB, botB, bot); |
| 683 |
} while (!iterA.done() || !iterB.done()); |
| 684 |
} |
| 685 |
|
| 686 |
class SkAAClip::Builder::Blitter final : public SkBlitter { |
| 687 |
int fLastY; |
| 688 |
|
| 689 |
/* |
| 690 |
If we see a gap of 1 or more empty scanlines while building in Y-order, |
| 691 |
we inject an explicit empty scanline (alpha==0) |
| 692 |
|
| 693 |
See AAClipTest.cpp : test_path_with_hole() |
| 694 |
*/ |
| 695 |
void checkForYGap(int y) { |
| 696 |
SkASSERT(y >= fLastY); |
| 697 |
if (fLastY > -SK_MaxS32) { |
| 698 |
int gap = y - fLastY; |
| 699 |
if (gap > 1) { |
| 700 |
fBuilder->addRun(fLeft, y - 1, 0, fRight - fLeft); |
| 701 |
} |
| 702 |
} |
| 703 |
fLastY = y; |
| 704 |
} |
| 705 |
|
| 706 |
public: |
| 707 |
Blitter(Builder* builder) { |
| 708 |
fBuilder = builder; |
| 709 |
fLeft = builder->fBounds.fLeft; |
| 710 |
fRight = builder->fBounds.fRight; |
| 711 |
fMinY = SK_MaxS32; |
| 712 |
fLastY = -SK_MaxS32; // sentinel |
| 713 |
} |
| 714 |
|
| 715 |
void finish() { |
| 716 |
if (fMinY < SK_MaxS32) { |
| 717 |
fBuilder->fMinY = fMinY; |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
/** |
| 722 |
Must evaluate clips in scan-line order, so don't want to allow blitV(), |
| 723 |
but an AAClip can be clipped down to a single pixel wide, so we |
| 724 |
must support it (given AntiRect semantics: minimum width is 2). |
| 725 |
Instead we'll rely on the runtime asserts to guarantee Y monotonicity; |
| 726 |
any failure cases that misses may have minor artifacts. |
| 727 |
*/ |
| 728 |
void blitV(int x, int y, int height, SkAlpha alpha) override { |
| 729 |
if (height == 1) { |
| 730 |
// We're still in scan-line order if height is 1 |
| 731 |
// This is useful for Analytic AA |
| 732 |
const SkAlpha alphas[2] = {alpha, 0}; |
| 733 |
const int16_t runs[2] = {1, 0}; |
| 734 |
this->blitAntiH(x, y, alphas, runs); |
| 735 |
} else { |
| 736 |
this->recordMinY(y); |
| 737 |
fBuilder->addColumn(x, y, alpha, height); |
| 738 |
fLastY = y + height - 1; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
void blitRect(int x, int y, int width, int height) override { |
| 743 |
this->recordMinY(y); |
| 744 |
this->checkForYGap(y); |
| 745 |
fBuilder->addRectRun(x, y, width, height); |
| 746 |
fLastY = y + height - 1; |
| 747 |
} |
| 748 |
|
| 749 |
void blitAntiRect(int x, int y, int width, int height, |
| 750 |
SkAlpha leftAlpha, SkAlpha rightAlpha) override { |
| 751 |
this->recordMinY(y); |
| 752 |
this->checkForYGap(y); |
| 753 |
fBuilder->addAntiRectRun(x, y, width, height, leftAlpha, rightAlpha); |
| 754 |
fLastY = y + height - 1; |
| 755 |
} |
| 756 |
|
| 757 |
void blitMask(const SkMask&, const SkIRect& clip) override |
| 758 |
{ unexpected(); } |
| 759 |
|
| 760 |
void blitH(int x, int y, int width) override { |
| 761 |
this->recordMinY(y); |
| 762 |
this->checkForYGap(y); |
| 763 |
fBuilder->addRun(x, y, 0xFF, width); |
| 764 |
} |
| 765 |
|
| 766 |
void blitAntiH(int x, int y, const SkAlpha alpha[], const int16_t runs[]) override { |
| 767 |
this->recordMinY(y); |
| 768 |
this->checkForYGap(y); |
| 769 |
for (;;) { |
| 770 |
int count = *runs; |
| 771 |
if (count <= 0) { |
| 772 |
return; |
| 773 |
} |
| 774 |
|
| 775 |
// The supersampler's buffer can be the width of the device, so |
| 776 |
// we may have to trim the run to our bounds. Previously, we assert that |
| 777 |
// the extra spans are always alpha==0. |
| 778 |
// However, the analytic AA is too sensitive to precision errors |
| 779 |
// so it may have extra spans with very tiny alpha because after several |
| 780 |
// arithmatic operations, the edge may bleed the path boundary a little bit. |
| 781 |
// Therefore, instead of always asserting alpha==0, we assert alpha < 0x10. |
| 782 |
int localX = x; |
| 783 |
int localCount = count; |
| 784 |
if (x < fLeft) { |
| 785 |
SkASSERT(0x10 > *alpha); |
| 786 |
int gap = fLeft - x; |
| 787 |
SkASSERT(gap <= count); |
| 788 |
localX += gap; |
| 789 |
localCount -= gap; |
| 790 |
} |
| 791 |
int right = x + count; |
| 792 |
if (right > fRight) { |
| 793 |
SkASSERT(0x10 > *alpha); |
| 794 |
localCount -= right - fRight; |
| 795 |
SkASSERT(localCount >= 0); |
| 796 |
} |
| 797 |
|
| 798 |
if (localCount) { |
| 799 |
fBuilder->addRun(localX, y, *alpha, localCount); |
| 800 |
} |
| 801 |
// Next run |
| 802 |
runs += count; |
| 803 |
alpha += count; |
| 804 |
x += count; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
private: |
| 809 |
Builder* fBuilder; |
| 810 |
int fLeft; // cache of builder's bounds' left edge |
| 811 |
int fRight; |
| 812 |
int fMinY; |
| 813 |
|
| 814 |
/* |
| 815 |
* We track this, in case the scan converter skipped some number of |
| 816 |
* scanlines at the (relative to the bounds it was given). This allows |
| 817 |
* the builder, during its finish, to trip its bounds down to the "real" |
| 818 |
* top. |
| 819 |
*/ |
| 820 |
void recordMinY(int y) { |
| 821 |
if (y < fMinY) { |
| 822 |
fMinY = y; |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
void unexpected() { |
| 827 |
SK_ABORT("---- did not expect to get called here"); |
| 828 |
} |
| 829 |
}; |
| 830 |
|
| 831 |
bool SkAAClip::Builder::applyClipOp(SkAAClip* target, const SkAAClip& other, SkClipOp op) { |
| 832 |
this->operateY(*target, other, op); |
| 833 |
return this->finish(target); |
| 834 |
} |
| 835 |
|
| 836 |
bool SkAAClip::Builder::blitPath(SkAAClip* target, const SkPath& path, bool doAA) { |
| 837 |
Blitter blitter(this); |
| 838 |
SkRegion clip(fBounds); |
| 839 |
|
| 840 |
const auto rc = SkResolveConvexity::kYes; |
| 841 |
SkPathRaw raw = SkPathPriv::Raw(path, rc).value_or(SkPathRaw::Empty(path.getFillType())); |
| 842 |
if (doAA) { |
| 843 |
SkScan::AntiFillPath(raw, clip, &blitter, true); |
| 844 |
} else { |
| 845 |
SkScan::FillPath(raw, clip, &blitter); |
| 846 |
} |
| 847 |
blitter.finish(); |
| 848 |
return this->finish(target); |
| 849 |
} |
| 850 |
|
| 851 |
/////////////////////////////////////////////////////////////////////////////// |
| 852 |
|
| 853 |
void SkAAClip::copyToMask(SkMaskBuilder* mask) const { |
| 854 |
auto expandRowToMask = [](uint8_t* dst, const uint8_t* row, int width) { |
| 855 |
while (width > 0) { |
| 856 |
int n = row[0]; |
| 857 |
SkASSERT(width >= n); |
| 858 |
memset(dst, row[1], n); |
| 859 |
dst += n; |
| 860 |
row += 2; |
| 861 |
width -= n; |
| 862 |
} |
| 863 |
SkASSERT(0 == width); |
| 864 |
}; |
| 865 |
|
| 866 |
mask->format() = SkMask::kA8_Format; |
| 867 |
if (this->isEmpty()) { |
| 868 |
mask->bounds().setEmpty(); |
| 869 |
mask->image() = nullptr; |
| 870 |
mask->rowBytes() = 0; |
| 871 |
return; |
| 872 |
} |
| 873 |
|
| 874 |
mask->bounds() = fBounds; |
| 875 |
mask->rowBytes() = fBounds.width(); |
| 876 |
size_t size = mask->computeImageSize(); |
| 877 |
mask->image() = SkMaskBuilder::AllocImage(size); |
| 878 |
|
| 879 |
Iter iter = RunHead::Iterate(*this); |
| 880 |
uint8_t* dst = mask->image(); |
| 881 |
const int width = fBounds.width(); |
| 882 |
|
| 883 |
int y = fBounds.fTop; |
| 884 |
while (!iter.done()) { |
| 885 |
do { |
| 886 |
expandRowToMask(dst, iter.data(), width); |
| 887 |
dst += mask->fRowBytes; |
| 888 |
} while (++y < iter.bottom()); |
| 889 |
iter.next(); |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
#ifdef SK_DEBUG |
| 894 |
|
| 895 |
void SkAAClip::validate() const { |
| 896 |
if (nullptr == fRunHead) { |
| 897 |
SkASSERT(fBounds.isEmpty()); |
| 898 |
return; |
| 899 |
} |
| 900 |
SkASSERT(!fBounds.isEmpty()); |
| 901 |
|
| 902 |
const RunHead* head = fRunHead; |
| 903 |
SkASSERT(head->fRefCnt.load() > 0); |
| 904 |
SkASSERT(head->fRowCount > 0); |
| 905 |
|
| 906 |
const YOffset* yoff = head->yoffsets(); |
| 907 |
const YOffset* ystop = yoff + head->fRowCount; |
| 908 |
const int lastY = fBounds.height() - 1; |
| 909 |
|
| 910 |
// Y and offset must be monotonic |
| 911 |
int prevY = -1; |
| 912 |
int32_t prevOffset = -1; |
| 913 |
while (yoff < ystop) { |
| 914 |
SkASSERT(prevY < yoff->fY); |
| 915 |
SkASSERT(yoff->fY <= lastY); |
| 916 |
prevY = yoff->fY; |
| 917 |
SkASSERT(prevOffset < (int32_t)yoff->fOffset); |
| 918 |
prevOffset = yoff->fOffset; |
| 919 |
const uint8_t* row = head->data() + yoff->fOffset; |
| 920 |
size_t rowLength = compute_row_length(row, fBounds.width()); |
| 921 |
SkASSERT(yoff->fOffset + rowLength <= head->fDataSize); |
| 922 |
yoff += 1; |
| 923 |
} |
| 924 |
// check the last entry; |
| 925 |
--yoff; |
| 926 |
SkASSERT(yoff->fY == lastY); |
| 927 |
} |
| 928 |
|
| 929 |
static void dump_one_row(const uint8_t* SK_RESTRICT row, |
| 930 |
int width, int leading_num) { |
| 931 |
if (leading_num) { |
| 932 |
SkDebugf( "%03d ", leading_num ); |
| 933 |
} |
| 934 |
while (width > 0) { |
| 935 |
int n = row[0]; |
| 936 |
int val = row[1]; |
| 937 |
char out = '.'; |
| 938 |
if (val == 0xff) { |
| 939 |
out = '*'; |
| 940 |
} else if (val > 0) { |
| 941 |
out = '+'; |
| 942 |
} |
| 943 |
for (int i = 0 ; i < n ; i++) { |
| 944 |
SkDebugf( "%c", out ); |
| 945 |
} |
| 946 |
row += 2; |
| 947 |
width -= n; |
| 948 |
} |
| 949 |
SkDebugf( "\n" ); |
| 950 |
} |
| 951 |
|
| 952 |
void SkAAClip::debug(bool compress_y) const { |
| 953 |
Iter iter = RunHead::Iterate(*this); |
| 954 |
const int width = fBounds.width(); |
| 955 |
|
| 956 |
int y = fBounds.fTop; |
| 957 |
while (!iter.done()) { |
| 958 |
if (compress_y) { |
| 959 |
dump_one_row(iter.data(), width, iter.bottom() - iter.top() + 1); |
| 960 |
} else { |
| 961 |
do { |
| 962 |
dump_one_row(iter.data(), width, 0); |
| 963 |
} while (++y < iter.bottom()); |
| 964 |
} |
| 965 |
iter.next(); |
| 966 |
} |
| 967 |
} |
| 968 |
#endif |
| 969 |
|
| 970 |
/////////////////////////////////////////////////////////////////////////////// |
| 971 |
|
| 972 |
// Count the number of zeros on the left and right edges of the passed in |
| 973 |
// RLE row. If 'row' is all zeros return 'width' in both variables. |
| 974 |
static void count_left_right_zeros(const uint8_t* row, int width, |
| 975 |
int* leftZ, int* riteZ) { |
| 976 |
int zeros = 0; |
| 977 |
do { |
| 978 |
if (row[1]) { |
| 979 |
break; |
| 980 |
} |
| 981 |
int n = row[0]; |
| 982 |
SkASSERT(n > 0); |
| 983 |
SkASSERT(n <= width); |
| 984 |
zeros += n; |
| 985 |
row += 2; |
| 986 |
width -= n; |
| 987 |
} while (width > 0); |
| 988 |
*leftZ = zeros; |
| 989 |
|
| 990 |
if (0 == width) { |
| 991 |
// this line is completely empty return 'width' in both variables |
| 992 |
*riteZ = *leftZ; |
| 993 |
return; |
| 994 |
} |
| 995 |
|
| 996 |
zeros = 0; |
| 997 |
while (width > 0) { |
| 998 |
int n = row[0]; |
| 999 |
SkASSERT(n > 0); |
| 1000 |
if (0 == row[1]) { |
| 1001 |
zeros += n; |
| 1002 |
} else { |
| 1003 |
zeros = 0; |
| 1004 |
} |
| 1005 |
row += 2; |
| 1006 |
width -= n; |
| 1007 |
} |
| 1008 |
*riteZ = zeros; |
| 1009 |
} |
| 1010 |
|
| 1011 |
// modify row in place, trimming off (zeros) from the left and right sides. |
| 1012 |
// return the number of bytes that were completely eliminated from the left |
| 1013 |
static int trim_row_left_right(uint8_t* row, int width, int leftZ, int riteZ) { |
| 1014 |
int trim = 0; |
| 1015 |
while (leftZ > 0) { |
| 1016 |
SkASSERT(0 == row[1]); |
| 1017 |
int n = row[0]; |
| 1018 |
SkASSERT(n > 0); |
| 1019 |
SkASSERT(n <= width); |
| 1020 |
width -= n; |
| 1021 |
row += 2; |
| 1022 |
if (n > leftZ) { |
| 1023 |
row[-2] = n - leftZ; |
| 1024 |
break; |
| 1025 |
} |
| 1026 |
trim += 2; |
| 1027 |
leftZ -= n; |
| 1028 |
SkASSERT(leftZ >= 0); |
| 1029 |
} |
| 1030 |
|
| 1031 |
if (riteZ) { |
| 1032 |
// walk row to the end, and then we'll back up to trim riteZ |
| 1033 |
while (width > 0) { |
| 1034 |
int n = row[0]; |
| 1035 |
SkASSERT(n <= width); |
| 1036 |
width -= n; |
| 1037 |
row += 2; |
| 1038 |
} |
| 1039 |
// now skip whole runs of zeros |
| 1040 |
do { |
| 1041 |
row -= 2; |
| 1042 |
SkASSERT(0 == row[1]); |
| 1043 |
int n = row[0]; |
| 1044 |
SkASSERT(n > 0); |
| 1045 |
if (n > riteZ) { |
| 1046 |
row[0] = n - riteZ; |
| 1047 |
break; |
| 1048 |
} |
| 1049 |
riteZ -= n; |
| 1050 |
SkASSERT(riteZ >= 0); |
| 1051 |
} while (riteZ > 0); |
| 1052 |
} |
| 1053 |
|
| 1054 |
return trim; |
| 1055 |
} |
| 1056 |
|
| 1057 |
bool SkAAClip::trimLeftRight() { |
| 1058 |
if (this->isEmpty()) { |
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
|
| 1062 |
AUTO_AACLIP_VALIDATE(*this); |
| 1063 |
|
| 1064 |
const int width = fBounds.width(); |
| 1065 |
RunHead* head = fRunHead; |
| 1066 |
YOffset* yoff = head->yoffsets(); |
| 1067 |
YOffset* stop = yoff + head->fRowCount; |
| 1068 |
uint8_t* base = head->data(); |
| 1069 |
|
| 1070 |
// After this loop, 'leftZeros' & 'rightZeros' will contain the minimum |
| 1071 |
// number of zeros on the left and right of the clip. This information |
| 1072 |
// can be used to shrink the bounding box. |
| 1073 |
int leftZeros = width; |
| 1074 |
int riteZeros = width; |
| 1075 |
while (yoff < stop) { |
| 1076 |
int L, R; |
| 1077 |
count_left_right_zeros(base + yoff->fOffset, width, &L, &R); |
| 1078 |
SkASSERT(L + R < width || (L == width && R == width)); |
| 1079 |
if (L < leftZeros) { |
| 1080 |
leftZeros = L; |
| 1081 |
} |
| 1082 |
if (R < riteZeros) { |
| 1083 |
riteZeros = R; |
| 1084 |
} |
| 1085 |
if (0 == (leftZeros | riteZeros)) { |
| 1086 |
// no trimming to do |
| 1087 |
return true; |
| 1088 |
} |
| 1089 |
yoff += 1; |
| 1090 |
} |
| 1091 |
|
| 1092 |
SkASSERT(leftZeros || riteZeros); |
| 1093 |
if (width == leftZeros) { |
| 1094 |
SkASSERT(width == riteZeros); |
| 1095 |
return this->setEmpty(); |
| 1096 |
} |
| 1097 |
|
| 1098 |
this->validate(); |
| 1099 |
|
| 1100 |
fBounds.fLeft += leftZeros; |
| 1101 |
fBounds.fRight -= riteZeros; |
| 1102 |
SkASSERT(!fBounds.isEmpty()); |
| 1103 |
|
| 1104 |
// For now we don't realloc the storage (for time), we just shrink in place |
| 1105 |
// This means we don't have to do any memmoves either, since we can just |
| 1106 |
// play tricks with the yoff->fOffset for each row |
| 1107 |
yoff = head->yoffsets(); |
| 1108 |
while (yoff < stop) { |
| 1109 |
uint8_t* row = base + yoff->fOffset; |
| 1110 |
SkDEBUGCODE((void)compute_row_length(row, width);) |
| 1111 |
yoff->fOffset += trim_row_left_right(row, width, leftZeros, riteZeros); |
| 1112 |
SkDEBUGCODE((void)compute_row_length(base + yoff->fOffset, width - leftZeros - riteZeros);) |
| 1113 |
yoff += 1; |
| 1114 |
} |
| 1115 |
return true; |
| 1116 |
} |
| 1117 |
|
| 1118 |
static bool row_is_all_zeros(const uint8_t* row, int width) { |
| 1119 |
SkASSERT(width > 0); |
| 1120 |
do { |
| 1121 |
if (row[1]) { |
| 1122 |
return false; |
| 1123 |
} |
| 1124 |
int n = row[0]; |
| 1125 |
SkASSERT(n <= width); |
| 1126 |
width -= n; |
| 1127 |
row += 2; |
| 1128 |
} while (width > 0); |
| 1129 |
SkASSERT(0 == width); |
| 1130 |
return true; |
| 1131 |
} |
| 1132 |
|
| 1133 |
bool SkAAClip::trimTopBottom() { |
| 1134 |
if (this->isEmpty()) { |
| 1135 |
return false; |
| 1136 |
} |
| 1137 |
|
| 1138 |
this->validate(); |
| 1139 |
|
| 1140 |
const int width = fBounds.width(); |
| 1141 |
RunHead* head = fRunHead; |
| 1142 |
YOffset* yoff = head->yoffsets(); |
| 1143 |
YOffset* stop = yoff + head->fRowCount; |
| 1144 |
const uint8_t* base = head->data(); |
| 1145 |
|
| 1146 |
// Look to trim away empty rows from the top. |
| 1147 |
// |
| 1148 |
int skip = 0; |
| 1149 |
while (yoff < stop) { |
| 1150 |
const uint8_t* data = base + yoff->fOffset; |
| 1151 |
if (!row_is_all_zeros(data, width)) { |
| 1152 |
break; |
| 1153 |
} |
| 1154 |
skip += 1; |
| 1155 |
yoff += 1; |
| 1156 |
} |
| 1157 |
SkASSERT(skip <= head->fRowCount); |
| 1158 |
if (skip == head->fRowCount) { |
| 1159 |
return this->setEmpty(); |
| 1160 |
} |
| 1161 |
if (skip > 0) { |
| 1162 |
// adjust fRowCount and fBounds.fTop, and slide all the data up |
| 1163 |
// as we remove [skip] number of YOffset entries |
| 1164 |
yoff = head->yoffsets(); |
| 1165 |
int dy = yoff[skip - 1].fY + 1; |
| 1166 |
for (int i = skip; i < head->fRowCount; ++i) { |
| 1167 |
SkASSERT(yoff[i].fY >= dy); |
| 1168 |
yoff[i].fY -= dy; |
| 1169 |
} |
| 1170 |
YOffset* dst = head->yoffsets(); |
| 1171 |
size_t size = head->fRowCount * sizeof(YOffset) + head->fDataSize; |
| 1172 |
memmove(dst, dst + skip, size - skip * sizeof(YOffset)); |
| 1173 |
|
| 1174 |
fBounds.fTop += dy; |
| 1175 |
SkASSERT(!fBounds.isEmpty()); |
| 1176 |
head->fRowCount -= skip; |
| 1177 |
SkASSERT(head->fRowCount > 0); |
| 1178 |
|
| 1179 |
this->validate(); |
| 1180 |
// need to reset this after the memmove |
| 1181 |
base = head->data(); |
| 1182 |
} |
| 1183 |
|
| 1184 |
// Look to trim away empty rows from the bottom. |
| 1185 |
// We know that we have at least one non-zero row, so we can just walk |
| 1186 |
// backwards without checking for running past the start. |
| 1187 |
// |
| 1188 |
stop = yoff = head->yoffsets() + head->fRowCount; |
| 1189 |
do { |
| 1190 |
yoff -= 1; |
| 1191 |
} while (row_is_all_zeros(base + yoff->fOffset, width)); |
| 1192 |
skip = SkToInt(stop - yoff - 1); |
| 1193 |
SkASSERT(skip >= 0 && skip < head->fRowCount); |
| 1194 |
if (skip > 0) { |
| 1195 |
// removing from the bottom is easier than from the top, as we don't |
| 1196 |
// have to adjust any of the Y values, we just have to trim the array |
| 1197 |
memmove(stop - skip, stop, head->fDataSize); |
| 1198 |
|
| 1199 |
fBounds.fBottom = fBounds.fTop + yoff->fY + 1; |
| 1200 |
SkASSERT(!fBounds.isEmpty()); |
| 1201 |
head->fRowCount -= skip; |
| 1202 |
SkASSERT(head->fRowCount > 0); |
| 1203 |
} |
| 1204 |
this->validate(); |
| 1205 |
|
| 1206 |
return true; |
| 1207 |
} |
| 1208 |
|
| 1209 |
// can't validate before we're done, since trimming is part of the process of |
| 1210 |
// making us valid after the Builder. Since we build from top to bottom, its |
| 1211 |
// possible our fBounds.fBottom is bigger than our last scanline of data, so |
| 1212 |
// we trim fBounds.fBottom back up. |
| 1213 |
// |
| 1214 |
// TODO: check for duplicates in X and Y to further compress our data |
| 1215 |
// |
| 1216 |
bool SkAAClip::trimBounds() { |
| 1217 |
if (this->isEmpty()) { |
| 1218 |
return false; |
| 1219 |
} |
| 1220 |
|
| 1221 |
const RunHead* head = fRunHead; |
| 1222 |
const YOffset* yoff = head->yoffsets(); |
| 1223 |
|
| 1224 |
SkASSERT(head->fRowCount > 0); |
| 1225 |
const YOffset& lastY = yoff[head->fRowCount - 1]; |
| 1226 |
SkASSERT(lastY.fY + 1 <= fBounds.height()); |
| 1227 |
fBounds.fBottom = fBounds.fTop + lastY.fY + 1; |
| 1228 |
SkASSERT(lastY.fY + 1 == fBounds.height()); |
| 1229 |
SkASSERT(!fBounds.isEmpty()); |
| 1230 |
|
| 1231 |
return this->trimTopBottom() && this->trimLeftRight(); |
| 1232 |
} |
| 1233 |
|
| 1234 |
/////////////////////////////////////////////////////////////////////////////// |
| 1235 |
|
| 1236 |
SkAAClip::SkAAClip() { |
| 1237 |
fBounds.setEmpty(); |
| 1238 |
fRunHead = nullptr; |
| 1239 |
} |
| 1240 |
|
| 1241 |
SkAAClip::SkAAClip(const SkAAClip& src) { |
| 1242 |
SkDEBUGCODE(fBounds.setEmpty();) // need this for validate |
| 1243 |
fRunHead = nullptr; |
| 1244 |
*this = src; |
| 1245 |
} |
| 1246 |
|
| 1247 |
SkAAClip::~SkAAClip() { |
| 1248 |
this->freeRuns(); |
| 1249 |
} |
| 1250 |
|
| 1251 |
SkAAClip& SkAAClip::operator=(const SkAAClip& src) { |
| 1252 |
AUTO_AACLIP_VALIDATE(*this); |
| 1253 |
src.validate(); |
| 1254 |
|
| 1255 |
if (this != &src) { |
| 1256 |
this->freeRuns(); |
| 1257 |
fBounds = src.fBounds; |
| 1258 |
fRunHead = src.fRunHead; |
| 1259 |
if (fRunHead) { |
| 1260 |
fRunHead->fRefCnt++; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
return *this; |
| 1264 |
} |
| 1265 |
|
| 1266 |
bool SkAAClip::setEmpty() { |
| 1267 |
this->freeRuns(); |
| 1268 |
fBounds.setEmpty(); |
| 1269 |
fRunHead = nullptr; |
| 1270 |
return false; |
| 1271 |
} |
| 1272 |
|
| 1273 |
bool SkAAClip::setRect(const SkIRect& bounds) { |
| 1274 |
if (bounds.isEmpty()) { |
| 1275 |
return this->setEmpty(); |
| 1276 |
} |
| 1277 |
|
| 1278 |
AUTO_AACLIP_VALIDATE(*this); |
| 1279 |
|
| 1280 |
this->freeRuns(); |
| 1281 |
fBounds = bounds; |
| 1282 |
fRunHead = RunHead::AllocRect(bounds); |
| 1283 |
SkASSERT(!this->isEmpty()); |
| 1284 |
return true; |
| 1285 |
} |
| 1286 |
|
| 1287 |
bool SkAAClip::isRect() const { |
| 1288 |
if (this->isEmpty()) { |
| 1289 |
return false; |
| 1290 |
} |
| 1291 |
|
| 1292 |
const RunHead* head = fRunHead; |
| 1293 |
if (head->fRowCount != 1) { |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
const YOffset* yoff = head->yoffsets(); |
| 1297 |
if (yoff->fY != fBounds.fBottom - 1) { |
| 1298 |
return false; |
| 1299 |
} |
| 1300 |
|
| 1301 |
const uint8_t* row = head->data() + yoff->fOffset; |
| 1302 |
int width = fBounds.width(); |
| 1303 |
do { |
| 1304 |
if (row[1] != 0xFF) { |
| 1305 |
return false; |
| 1306 |
} |
| 1307 |
int n = row[0]; |
| 1308 |
SkASSERT(n <= width); |
| 1309 |
width -= n; |
| 1310 |
row += 2; |
| 1311 |
} while (width > 0); |
| 1312 |
return true; |
| 1313 |
} |
| 1314 |
|
| 1315 |
bool SkAAClip::setRegion(const SkRegion& rgn) { |
| 1316 |
if (rgn.isEmpty()) { |
| 1317 |
return this->setEmpty(); |
| 1318 |
} |
| 1319 |
if (rgn.isRect()) { |
| 1320 |
return this->setRect(rgn.getBounds()); |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
const SkIRect& bounds = rgn.getBounds(); |
| 1325 |
const int offsetX = bounds.fLeft; |
| 1326 |
const int offsetY = bounds.fTop; |
| 1327 |
|
| 1328 |
SkTDArray<YOffset> yArray; |
| 1329 |
SkTDArray<uint8_t> xArray; |
| 1330 |
|
| 1331 |
yArray.reserve(std::min(bounds.height(), 1024)); |
| 1332 |
xArray.reserve(std::min(bounds.width(), 512) * 128); |
| 1333 |
|
| 1334 |
auto appendXRun = [&xArray](uint8_t value, int count) { |
| 1335 |
SkASSERT(count >= 0); |
| 1336 |
while (count > 0) { |
| 1337 |
int n = count; |
| 1338 |
if (n > 255) { |
| 1339 |
n = 255; |
| 1340 |
} |
| 1341 |
uint8_t* data = xArray.append(2); |
| 1342 |
data[0] = n; |
| 1343 |
data[1] = value; |
| 1344 |
count -= n; |
| 1345 |
} |
| 1346 |
}; |
| 1347 |
|
| 1348 |
SkRegion::Iterator iter(rgn); |
| 1349 |
int prevRight = 0; |
| 1350 |
int prevBot = 0; |
| 1351 |
YOffset* currY = nullptr; |
| 1352 |
|
| 1353 |
for (; !iter.done(); iter.next()) { |
| 1354 |
const SkIRect& r = iter.rect(); |
| 1355 |
SkASSERT(bounds.contains(r)); |
| 1356 |
|
| 1357 |
int bot = r.fBottom - offsetY; |
| 1358 |
SkASSERT(bot >= prevBot); |
| 1359 |
if (bot > prevBot) { |
| 1360 |
if (currY) { |
| 1361 |
// flush current row |
| 1362 |
appendXRun(0, bounds.width() - prevRight); |
| 1363 |
} |
| 1364 |
// did we introduce an empty-gap from the prev row? |
| 1365 |
int top = r.fTop - offsetY; |
| 1366 |
if (top > prevBot) { |
| 1367 |
currY = yArray.append(); |
| 1368 |
currY->fY = top - 1; |
| 1369 |
currY->fOffset = xArray.size(); |
| 1370 |
appendXRun(0, bounds.width()); |
| 1371 |
} |
| 1372 |
// create a new record for this Y value |
| 1373 |
currY = yArray.append(); |
| 1374 |
currY->fY = bot - 1; |
| 1375 |
currY->fOffset = xArray.size(); |
| 1376 |
prevRight = 0; |
| 1377 |
prevBot = bot; |
| 1378 |
} |
| 1379 |
|
| 1380 |
int x = r.fLeft - offsetX; |
| 1381 |
appendXRun(0, x - prevRight); |
| 1382 |
|
| 1383 |
int w = r.fRight - r.fLeft; |
| 1384 |
appendXRun(0xFF, w); |
| 1385 |
prevRight = x + w; |
| 1386 |
SkASSERT(prevRight <= bounds.width()); |
| 1387 |
} |
| 1388 |
// flush last row |
| 1389 |
appendXRun(0, bounds.width() - prevRight); |
| 1390 |
|
| 1391 |
// now pack everything into a RunHead |
| 1392 |
RunHead* head = RunHead::Alloc(yArray.size(), xArray.size_bytes()); |
| 1393 |
memcpy(head->yoffsets(), yArray.begin(), yArray.size_bytes()); |
| 1394 |
memcpy(head->data(), xArray.begin(), xArray.size_bytes()); |
| 1395 |
|
| 1396 |
this->setEmpty(); |
| 1397 |
fBounds = bounds; |
| 1398 |
fRunHead = head; |
| 1399 |
this->validate(); |
| 1400 |
return true; |
| 1401 |
} |
| 1402 |
|
| 1403 |
bool SkAAClip::setPath(const SkPath& path, const SkIRect& clip, bool doAA) { |
| 1404 |
AUTO_AACLIP_VALIDATE(*this); |
| 1405 |
|
| 1406 |
if (clip.isEmpty()) { |
| 1407 |
return this->setEmpty(); |
| 1408 |
} |
| 1409 |
|
| 1410 |
SkIRect ibounds; |
| 1411 |
// Since we assert that the BuilderBlitter will never blit outside the intersection |
| 1412 |
// of clip and ibounds, we create the builder with the snug bounds. |
| 1413 |
if (path.isInverseFillType()) { |
| 1414 |
ibounds = clip; |
| 1415 |
} else { |
| 1416 |
path.getBounds().roundOut(&ibounds); |
| 1417 |
// It's possible the bounds of our path might exceed SK_MaxS32 in width |
| 1418 |
// but since our clip is within that width (otherwise isEmpty() above |
| 1419 |
// would catch it), we can use isEmpty64() safely here. blitPath will |
| 1420 |
// interesect the two bounds before drawing. |
| 1421 |
if (ibounds.isEmpty64() || !ibounds.intersect(clip)) { |
| 1422 |
return this->setEmpty(); |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
Builder builder(ibounds); |
| 1427 |
return builder.blitPath(this, path, doAA); |
| 1428 |
} |
| 1429 |
|
| 1430 |
/////////////////////////////////////////////////////////////////////////////// |
| 1431 |
|
| 1432 |
bool SkAAClip::op(const SkAAClip& other, SkClipOp op) { |
| 1433 |
AUTO_AACLIP_VALIDATE(*this); |
| 1434 |
|
| 1435 |
if (this->isEmpty()) { |
| 1436 |
// Once the clip is empty, it cannot become un-empty. |
| 1437 |
return false; |
| 1438 |
} |
| 1439 |
|
| 1440 |
SkIRect bounds = fBounds; |
| 1441 |
switch(op) { |
| 1442 |
case SkClipOp::kDifference: |
| 1443 |
if (other.isEmpty() || !SkIRect::Intersects(fBounds, other.fBounds)) { |
| 1444 |
// this remains unmodified and isn't empty |
| 1445 |
return true; |
| 1446 |
} |
| 1447 |
break; |
| 1448 |
|
| 1449 |
case SkClipOp::kIntersect: |
| 1450 |
if (other.isEmpty() || !bounds.intersect(other.fBounds)) { |
| 1451 |
// the intersected clip becomes empty |
| 1452 |
return this->setEmpty(); |
| 1453 |
} |
| 1454 |
break; |
| 1455 |
} |
| 1456 |
|
| 1457 |
|
| 1458 |
SkASSERT(SkIRect::Intersects(bounds, fBounds)); |
| 1459 |
SkASSERT(SkIRect::Intersects(bounds, other.fBounds)); |
| 1460 |
|
| 1461 |
Builder builder(bounds); |
| 1462 |
return builder.applyClipOp(this, other, op); |
| 1463 |
} |
| 1464 |
|
| 1465 |
bool SkAAClip::op(const SkIRect& rect, SkClipOp op) { |
| 1466 |
// It can be expensive to build a local aaclip before applying the op, so |
| 1467 |
// we first see if we can restrict the bounds of new rect to our current |
| 1468 |
// bounds, or note that the new rect subsumes our current clip. |
| 1469 |
SkIRect pixelBounds = fBounds; |
| 1470 |
if (!pixelBounds.intersect(rect)) { |
| 1471 |
// No change or clip becomes empty depending on 'op' |
| 1472 |
switch(op) { |
| 1473 |
case SkClipOp::kDifference: return !this->isEmpty(); |
| 1474 |
case SkClipOp::kIntersect: return this->setEmpty(); |
| 1475 |
} |
| 1476 |
SkUNREACHABLE; |
| 1477 |
} else if (pixelBounds == fBounds) { |
| 1478 |
// Wholly inside 'rect', so clip becomes empty or remains unchanged |
| 1479 |
switch(op) { |
| 1480 |
case SkClipOp::kDifference: return this->setEmpty(); |
| 1481 |
case SkClipOp::kIntersect: return !this->isEmpty(); |
| 1482 |
} |
| 1483 |
SkUNREACHABLE; |
| 1484 |
} else if (op == SkClipOp::kIntersect && this->quickContains(pixelBounds)) { |
| 1485 |
// We become just the remaining rectangle |
| 1486 |
return this->setRect(pixelBounds); |
| 1487 |
} else { |
| 1488 |
SkAAClip clip; |
| 1489 |
clip.setRect(rect); |
| 1490 |
return this->op(clip, op); |
| 1491 |
} |
| 1492 |
} |
| 1493 |
|
| 1494 |
bool SkAAClip::op(const SkRect& rect, SkClipOp op, bool doAA) { |
| 1495 |
if (!doAA) { |
| 1496 |
return this->op(rect.round(), op); |
| 1497 |
} else { |
| 1498 |
// Tighten bounds for "path" aaclip of the rect |
| 1499 |
SkIRect pixelBounds = fBounds; |
| 1500 |
if (!pixelBounds.intersect(rect.roundOut())) { |
| 1501 |
// No change or clip becomes empty depending on 'op' |
| 1502 |
switch(op) { |
| 1503 |
case SkClipOp::kDifference: return !this->isEmpty(); |
| 1504 |
case SkClipOp::kIntersect: return this->setEmpty(); |
| 1505 |
} |
| 1506 |
SkUNREACHABLE; |
| 1507 |
} else if (rect.contains(SkRect::Make(fBounds))) { |
| 1508 |
// Wholly inside 'rect', so clip becomes empty or remains unchanged |
| 1509 |
switch(op) { |
| 1510 |
case SkClipOp::kDifference: return this->setEmpty(); |
| 1511 |
case SkClipOp::kIntersect: return !this->isEmpty(); |
| 1512 |
} |
| 1513 |
SkUNREACHABLE; |
| 1514 |
} else if (op == SkClipOp::kIntersect && this->quickContains(pixelBounds)) { |
| 1515 |
// We become just the rect intersected with pixel bounds (preserving fractional coords |
| 1516 |
// for AA edges). |
| 1517 |
return this->setPath(SkPath::Rect(rect), pixelBounds, /*doAA=*/true); |
| 1518 |
} else { |
| 1519 |
SkAAClip rectClip; |
| 1520 |
rectClip.setPath(SkPath::Rect(rect), |
| 1521 |
op == SkClipOp::kDifference ? fBounds : pixelBounds, |
| 1522 |
/*doAA=*/true); |
| 1523 |
return this->op(rectClip, op); |
| 1524 |
} |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
/////////////////////////////////////////////////////////////////////////////// |
| 1529 |
|
| 1530 |
bool SkAAClip::translate(int dx, int dy, SkAAClip* dst) const { |
| 1531 |
if (nullptr == dst) { |
| 1532 |
return !this->isEmpty(); |
| 1533 |
} |
| 1534 |
|
| 1535 |
if (this->isEmpty()) { |
| 1536 |
return dst->setEmpty(); |
| 1537 |
} |
| 1538 |
|
| 1539 |
if (this != dst) { |
| 1540 |
fRunHead->fRefCnt++; |
| 1541 |
dst->freeRuns(); |
| 1542 |
dst->fRunHead = fRunHead; |
| 1543 |
dst->fBounds = fBounds; |
| 1544 |
} |
| 1545 |
dst->fBounds.offset(dx, dy); |
| 1546 |
return true; |
| 1547 |
} |
| 1548 |
|
| 1549 |
void SkAAClip::freeRuns() { |
| 1550 |
if (fRunHead) { |
| 1551 |
SkASSERT(fRunHead->fRefCnt.load() >= 1); |
| 1552 |
if (1 == fRunHead->fRefCnt--) { |
| 1553 |
sk_free(fRunHead); |
| 1554 |
} |
| 1555 |
} |
| 1556 |
} |
| 1557 |
|
| 1558 |
const uint8_t* SkAAClip::findRow(int y, int* lastYForRow) const { |
| 1559 |
SkASSERT(fRunHead); |
| 1560 |
|
| 1561 |
if (y < fBounds.fTop || y >= fBounds.fBottom) { |
| 1562 |
return nullptr; |
| 1563 |
} |
| 1564 |
y -= fBounds.y(); // our yoffs values are relative to the top |
| 1565 |
|
| 1566 |
const YOffset* yoff = fRunHead->yoffsets(); |
| 1567 |
while (yoff->fY < y) { |
| 1568 |
yoff += 1; |
| 1569 |
SkASSERT(yoff - fRunHead->yoffsets() < fRunHead->fRowCount); |
| 1570 |
} |
| 1571 |
|
| 1572 |
if (lastYForRow) { |
| 1573 |
*lastYForRow = fBounds.y() + yoff->fY; |
| 1574 |
} |
| 1575 |
return fRunHead->data() + yoff->fOffset; |
| 1576 |
} |
| 1577 |
|
| 1578 |
const uint8_t* SkAAClip::findX(const uint8_t data[], int x, int* initialCount) const { |
| 1579 |
SkASSERT(x >= fBounds.fLeft && x < fBounds.fRight); |
| 1580 |
x -= fBounds.x(); |
| 1581 |
|
| 1582 |
// first skip up to X |
| 1583 |
for (;;) { |
| 1584 |
int n = data[0]; |
| 1585 |
if (x < n) { |
| 1586 |
if (initialCount) { |
| 1587 |
*initialCount = n - x; |
| 1588 |
} |
| 1589 |
break; |
| 1590 |
} |
| 1591 |
data += 2; |
| 1592 |
x -= n; |
| 1593 |
} |
| 1594 |
return data; |
| 1595 |
} |
| 1596 |
|
| 1597 |
bool SkAAClip::quickContains(int left, int top, int right, int bottom) const { |
| 1598 |
if (this->isEmpty()) { |
| 1599 |
return false; |
| 1600 |
} |
| 1601 |
if (!fBounds.contains(SkIRect{left, top, right, bottom})) { |
| 1602 |
return false; |
| 1603 |
} |
| 1604 |
|
| 1605 |
int lastY SK_INIT_TO_AVOID_WARNING; |
| 1606 |
const uint8_t* row = this->findRow(top, &lastY); |
| 1607 |
if (lastY < bottom) { |
| 1608 |
return false; |
| 1609 |
} |
| 1610 |
// now just need to check in X |
| 1611 |
int count; |
| 1612 |
row = this->findX(row, left, &count); |
| 1613 |
|
| 1614 |
int rectWidth = right - left; |
| 1615 |
while (0xFF == row[1]) { |
| 1616 |
if (count >= rectWidth) { |
| 1617 |
return true; |
| 1618 |
} |
| 1619 |
rectWidth -= count; |
| 1620 |
row += 2; |
| 1621 |
count = row[0]; |
| 1622 |
} |
| 1623 |
return false; |
| 1624 |
} |
| 1625 |
|
| 1626 |
/////////////////////////////////////////////////////////////////////////////// |
| 1627 |
|
| 1628 |
static void expandToRuns(const uint8_t* SK_RESTRICT data, int initialCount, int width, |
| 1629 |
int16_t* SK_RESTRICT runs, SkAlpha* SK_RESTRICT aa) { |
| 1630 |
// we don't read our initial n from data, since the caller may have had to |
| 1631 |
// clip it, hence the initialCount parameter. |
| 1632 |
int n = initialCount; |
| 1633 |
for (;;) { |
| 1634 |
if (n > width) { |
| 1635 |
n = width; |
| 1636 |
} |
| 1637 |
SkASSERT(n > 0); |
| 1638 |
runs[0] = n; |
| 1639 |
runs += n; |
| 1640 |
|
| 1641 |
aa[0] = data[1]; |
| 1642 |
aa += n; |
| 1643 |
|
| 1644 |
data += 2; |
| 1645 |
width -= n; |
| 1646 |
if (0 == width) { |
| 1647 |
break; |
| 1648 |
} |
| 1649 |
// load the next count |
| 1650 |
n = data[0]; |
| 1651 |
} |
| 1652 |
runs[0] = 0; // sentinel |
| 1653 |
} |
| 1654 |
|
| 1655 |
SkAAClipBlitter::~SkAAClipBlitter() { |
| 1656 |
sk_free(fScanlineScratch); |
| 1657 |
} |
| 1658 |
|
| 1659 |
void SkAAClipBlitter::ensureRunsAndAA() { |
| 1660 |
if (nullptr == fScanlineScratch) { |
| 1661 |
// add 1 so we can store the terminating run count of 0 |
| 1662 |
int count = fAAClipBounds.width() + 1; |
| 1663 |
// we use this either for fRuns + fAA, or a scaline of a mask |
| 1664 |
// which may be as deep as 32bits |
| 1665 |
fScanlineScratch = sk_malloc_throw(count * sizeof(SkPMColor)); |
| 1666 |
fRuns = (int16_t*)fScanlineScratch; |
| 1667 |
fAA = (SkAlpha*)(fRuns + count); |
| 1668 |
} |
| 1669 |
} |
| 1670 |
|
| 1671 |
void SkAAClipBlitter::blitH(int x, int y, int width) { |
| 1672 |
SkASSERT(width > 0); |
| 1673 |
SkASSERT(fAAClipBounds.contains(x, y)); |
| 1674 |
SkASSERT(fAAClipBounds.contains(x + width - 1, y)); |
| 1675 |
|
| 1676 |
const uint8_t* row = fAAClip->findRow(y); |
| 1677 |
int initialCount; |
| 1678 |
row = fAAClip->findX(row, x, &initialCount); |
| 1679 |
|
| 1680 |
if (initialCount >= width) { |
| 1681 |
SkAlpha alpha = row[1]; |
| 1682 |
if (0 == alpha) { |
| 1683 |
return; |
| 1684 |
} |
| 1685 |
if (0xFF == alpha) { |
| 1686 |
fBlitter->blitH(x, y, width); |
| 1687 |
return; |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
this->ensureRunsAndAA(); |
| 1692 |
expandToRuns(row, initialCount, width, fRuns, fAA); |
| 1693 |
|
| 1694 |
fBlitter->blitAntiH(x, y, fAA, fRuns); |
| 1695 |
} |
| 1696 |
|
| 1697 |
static void merge(const uint8_t* SK_RESTRICT row, int rowN, |
| 1698 |
const SkAlpha* SK_RESTRICT srcAA, |
| 1699 |
const int16_t* SK_RESTRICT srcRuns, |
| 1700 |
SkAlpha* SK_RESTRICT dstAA, |
| 1701 |
int16_t* SK_RESTRICT dstRuns, |
| 1702 |
int width) { |
| 1703 |
SkDEBUGCODE(int accumulated = 0;) |
| 1704 |
int srcN = srcRuns[0]; |
| 1705 |
// do we need this check? |
| 1706 |
if (0 == srcN) { |
| 1707 |
return; |
| 1708 |
} |
| 1709 |
|
| 1710 |
for (;;) { |
| 1711 |
SkASSERT(rowN > 0); |
| 1712 |
SkASSERT(srcN > 0); |
| 1713 |
|
| 1714 |
unsigned newAlpha = SkMulDiv255Round(srcAA[0], row[1]); |
| 1715 |
int minN = std::min(srcN, rowN); |
| 1716 |
dstRuns[0] = minN; |
| 1717 |
dstRuns += minN; |
| 1718 |
dstAA[0] = newAlpha; |
| 1719 |
dstAA += minN; |
| 1720 |
|
| 1721 |
if (0 == (srcN -= minN)) { |
| 1722 |
srcN = srcRuns[0]; // refresh |
| 1723 |
srcRuns += srcN; |
| 1724 |
srcAA += srcN; |
| 1725 |
srcN = srcRuns[0]; // reload |
| 1726 |
if (0 == srcN) { |
| 1727 |
break; |
| 1728 |
} |
| 1729 |
} |
| 1730 |
if (0 == (rowN -= minN)) { |
| 1731 |
row += 2; |
| 1732 |
rowN = row[0]; // reload |
| 1733 |
} |
| 1734 |
|
| 1735 |
SkDEBUGCODE(accumulated += minN;) |
| 1736 |
SkASSERT(accumulated <= width); |
| 1737 |
} |
| 1738 |
dstRuns[0] = 0; |
| 1739 |
} |
| 1740 |
|
| 1741 |
void SkAAClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[], |
| 1742 |
const int16_t runs[]) { |
| 1743 |
|
| 1744 |
const uint8_t* row = fAAClip->findRow(y); |
| 1745 |
int initialCount; |
| 1746 |
row = fAAClip->findX(row, x, &initialCount); |
| 1747 |
|
| 1748 |
this->ensureRunsAndAA(); |
| 1749 |
|
| 1750 |
merge(row, initialCount, aa, runs, fAA, fRuns, fAAClipBounds.width()); |
| 1751 |
fBlitter->blitAntiH(x, y, fAA, fRuns); |
| 1752 |
} |
| 1753 |
|
| 1754 |
void SkAAClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 1755 |
if (fAAClip->quickContains(x, y, x + 1, y + height)) { |
| 1756 |
fBlitter->blitV(x, y, height, alpha); |
| 1757 |
return; |
| 1758 |
} |
| 1759 |
|
| 1760 |
for (;;) { |
| 1761 |
int lastY SK_INIT_TO_AVOID_WARNING; |
| 1762 |
const uint8_t* row = fAAClip->findRow(y, &lastY); |
| 1763 |
int dy = lastY - y + 1; |
| 1764 |
if (dy > height) { |
| 1765 |
dy = height; |
| 1766 |
} |
| 1767 |
height -= dy; |
| 1768 |
|
| 1769 |
row = fAAClip->findX(row, x); |
| 1770 |
SkAlpha newAlpha = SkMulDiv255Round(alpha, row[1]); |
| 1771 |
if (newAlpha) { |
| 1772 |
fBlitter->blitV(x, y, dy, newAlpha); |
| 1773 |
} |
| 1774 |
SkASSERT(height >= 0); |
| 1775 |
if (height <= 0) { |
| 1776 |
break; |
| 1777 |
} |
| 1778 |
y = lastY + 1; |
| 1779 |
} |
| 1780 |
} |
| 1781 |
|
| 1782 |
void SkAAClipBlitter::blitRect(int x, int y, int width, int height) { |
| 1783 |
if (fAAClip->quickContains(x, y, x + width, y + height)) { |
| 1784 |
fBlitter->blitRect(x, y, width, height); |
| 1785 |
return; |
| 1786 |
} |
| 1787 |
|
| 1788 |
while (--height >= 0) { |
| 1789 |
this->blitH(x, y, width); |
| 1790 |
y += 1; |
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
typedef void (*MergeAAProc)(const void* src, int width, const uint8_t* row, |
| 1795 |
int initialRowCount, void* dst); |
| 1796 |
|
| 1797 |
static void small_memcpy(void* dst, const void* src, size_t n) { |
| 1798 |
memcpy(dst, src, n); |
| 1799 |
} |
| 1800 |
|
| 1801 |
static void small_bzero(void* dst, size_t n) { |
| 1802 |
sk_bzero(dst, n); |
| 1803 |
} |
| 1804 |
|
| 1805 |
static inline uint8_t mergeOne(uint8_t value, unsigned alpha) { |
| 1806 |
return SkMulDiv255Round(value, alpha); |
| 1807 |
} |
| 1808 |
|
| 1809 |
static inline uint16_t mergeOne(uint16_t value, unsigned alpha) { |
| 1810 |
unsigned r = SkGetPackedR16(value); |
| 1811 |
unsigned g = SkGetPackedG16(value); |
| 1812 |
unsigned b = SkGetPackedB16(value); |
| 1813 |
return SkPackRGB16(SkMulDiv255Round(r, alpha), |
| 1814 |
SkMulDiv255Round(g, alpha), |
| 1815 |
SkMulDiv255Round(b, alpha)); |
| 1816 |
} |
| 1817 |
|
| 1818 |
template <typename T> |
| 1819 |
void mergeT(const void* inSrc, int srcN, const uint8_t* SK_RESTRICT row, int rowN, void* inDst) { |
| 1820 |
const T* SK_RESTRICT src = static_cast<const T*>(inSrc); |
| 1821 |
T* SK_RESTRICT dst = static_cast<T*>(inDst); |
| 1822 |
for (;;) { |
| 1823 |
SkASSERT(rowN > 0); |
| 1824 |
SkASSERT(srcN > 0); |
| 1825 |
|
| 1826 |
int n = std::min(rowN, srcN); |
| 1827 |
unsigned rowA = row[1]; |
| 1828 |
if (0xFF == rowA) { |
| 1829 |
small_memcpy(dst, src, n * sizeof(T)); |
| 1830 |
} else if (0 == rowA) { |
| 1831 |
small_bzero(dst, n * sizeof(T)); |
| 1832 |
} else { |
| 1833 |
for (int i = 0; i < n; ++i) { |
| 1834 |
dst[i] = mergeOne(src[i], rowA); |
| 1835 |
} |
| 1836 |
} |
| 1837 |
|
| 1838 |
if (0 == (srcN -= n)) { |
| 1839 |
break; |
| 1840 |
} |
| 1841 |
|
| 1842 |
src += n; |
| 1843 |
dst += n; |
| 1844 |
|
| 1845 |
SkASSERT(rowN == n); |
| 1846 |
row += 2; |
| 1847 |
rowN = row[0]; |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
static MergeAAProc find_merge_aa_proc(SkMask::Format format) { |
| 1852 |
switch (format) { |
| 1853 |
case SkMask::kBW_Format: |
| 1854 |
SkDEBUGFAIL("unsupported"); |
| 1855 |
return nullptr; |
| 1856 |
case SkMask::kA8_Format: |
| 1857 |
case SkMask::k3D_Format: |
| 1858 |
return mergeT<uint8_t> ; |
| 1859 |
case SkMask::kLCD16_Format: |
| 1860 |
return mergeT<uint16_t>; |
| 1861 |
default: |
| 1862 |
SkDEBUGFAIL("unsupported"); |
| 1863 |
return nullptr; |
| 1864 |
} |
| 1865 |
} |
| 1866 |
|
| 1867 |
static U8CPU bit2byte(int bitInAByte) { |
| 1868 |
SkASSERT(bitInAByte <= 0xFF); |
| 1869 |
// negation turns any non-zero into 0xFFFFFF??, so we just shift down |
| 1870 |
// some value >= 8 to get a full FF value |
| 1871 |
return -bitInAByte >> 8; |
| 1872 |
} |
| 1873 |
|
| 1874 |
static void upscaleBW2A8(SkMask* dstMask, const SkMask& srcMask) { |
| 1875 |
SkASSERT(SkMask::kBW_Format == srcMask.fFormat); |
| 1876 |
SkASSERT(SkMask::kA8_Format == dstMask->fFormat); |
| 1877 |
|
| 1878 |
const int width = srcMask.fBounds.width(); |
| 1879 |
const int height = srcMask.fBounds.height(); |
| 1880 |
|
| 1881 |
const uint8_t* SK_RESTRICT src = (const uint8_t*)srcMask.fImage; |
| 1882 |
const size_t srcRB = srcMask.fRowBytes; |
| 1883 |
uint8_t* SK_RESTRICT dst = const_cast<uint8_t*>(dstMask->fImage); |
| 1884 |
const size_t dstRB = dstMask->fRowBytes; |
| 1885 |
|
| 1886 |
const int wholeBytes = width >> 3; |
| 1887 |
const int leftOverBits = width & 7; |
| 1888 |
|
| 1889 |
for (int y = 0; y < height; ++y) { |
| 1890 |
uint8_t* SK_RESTRICT d = dst; |
| 1891 |
for (int i = 0; i < wholeBytes; ++i) { |
| 1892 |
int srcByte = src[i]; |
| 1893 |
d[0] = bit2byte(srcByte & (1 << 7)); |
| 1894 |
d[1] = bit2byte(srcByte & (1 << 6)); |
| 1895 |
d[2] = bit2byte(srcByte & (1 << 5)); |
| 1896 |
d[3] = bit2byte(srcByte & (1 << 4)); |
| 1897 |
d[4] = bit2byte(srcByte & (1 << 3)); |
| 1898 |
d[5] = bit2byte(srcByte & (1 << 2)); |
| 1899 |
d[6] = bit2byte(srcByte & (1 << 1)); |
| 1900 |
d[7] = bit2byte(srcByte & (1 << 0)); |
| 1901 |
d += 8; |
| 1902 |
} |
| 1903 |
if (leftOverBits) { |
| 1904 |
int srcByte = src[wholeBytes]; |
| 1905 |
for (int x = 0; x < leftOverBits; ++x) { |
| 1906 |
*d++ = bit2byte(srcByte & 0x80); |
| 1907 |
srcByte <<= 1; |
| 1908 |
} |
| 1909 |
} |
| 1910 |
src += srcRB; |
| 1911 |
dst += dstRB; |
| 1912 |
} |
| 1913 |
} |
| 1914 |
|
| 1915 |
void SkAAClipBlitter::blitMask(const SkMask& origMask, const SkIRect& clip) { |
| 1916 |
SkASSERT(fAAClip->getBounds().contains(clip)); |
| 1917 |
|
| 1918 |
if (fAAClip->quickContains(clip)) { |
| 1919 |
fBlitter->blitMask(origMask, clip); |
| 1920 |
return; |
| 1921 |
} |
| 1922 |
|
| 1923 |
const SkMask* mask = &origMask; |
| 1924 |
|
| 1925 |
// if we're BW, we need to upscale to A8 (ugh) |
| 1926 |
SkMaskBuilder grayMask; |
| 1927 |
if (SkMask::kBW_Format == origMask.fFormat) { |
| 1928 |
grayMask.format() = SkMask::kA8_Format; |
| 1929 |
grayMask.bounds() = origMask.fBounds; |
| 1930 |
grayMask.rowBytes() = origMask.fBounds.width(); |
| 1931 |
size_t size = grayMask.computeImageSize(); |
| 1932 |
grayMask.image() = reinterpret_cast<uint8_t*>( |
| 1933 |
fGrayMaskScratch.reset(size, SkAutoMalloc::kReuse_OnShrink)); |
| 1934 |
|
| 1935 |
upscaleBW2A8(&grayMask, origMask); |
| 1936 |
mask = &grayMask; |
| 1937 |
} |
| 1938 |
|
| 1939 |
this->ensureRunsAndAA(); |
| 1940 |
|
| 1941 |
// HACK -- we are devolving 3D into A8, need to copy the rest of the 3D |
| 1942 |
// data into a temp block to support it better (ugh) |
| 1943 |
|
| 1944 |
const void* src = mask->getAddr(clip.fLeft, clip.fTop); |
| 1945 |
const size_t srcRB = mask->fRowBytes; |
| 1946 |
const int width = clip.width(); |
| 1947 |
MergeAAProc mergeProc = find_merge_aa_proc(mask->fFormat); |
| 1948 |
|
| 1949 |
SkMaskBuilder rowMask; |
| 1950 |
rowMask.format() = SkMask::k3D_Format == mask->fFormat ? SkMask::kA8_Format : mask->fFormat; |
| 1951 |
rowMask.bounds().fLeft = clip.fLeft; |
| 1952 |
rowMask.bounds().fRight = clip.fRight; |
| 1953 |
rowMask.rowBytes() = mask->fRowBytes; // doesn't matter, since our height==1 |
| 1954 |
rowMask.image() = (uint8_t*)fScanlineScratch; |
| 1955 |
|
| 1956 |
int y = clip.fTop; |
| 1957 |
const int stopY = y + clip.height(); |
| 1958 |
|
| 1959 |
do { |
| 1960 |
int localStopY SK_INIT_TO_AVOID_WARNING; |
| 1961 |
const uint8_t* row = fAAClip->findRow(y, &localStopY); |
| 1962 |
// findRow returns last Y, not stop, so we add 1 |
| 1963 |
localStopY = std::min(localStopY + 1, stopY); |
| 1964 |
|
| 1965 |
int initialCount; |
| 1966 |
row = fAAClip->findX(row, clip.fLeft, &initialCount); |
| 1967 |
do { |
| 1968 |
mergeProc(src, width, row, initialCount, rowMask.image()); |
| 1969 |
rowMask.bounds().fTop = y; |
| 1970 |
rowMask.bounds().fBottom = y + 1; |
| 1971 |
fBlitter->blitMask(rowMask, rowMask.fBounds); |
| 1972 |
src = (const void*)((const char*)src + srcRB); |
| 1973 |
} while (++y < localStopY); |
| 1974 |
} while (y < stopY); |
| 1975 |
} |