3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkBlitter.h"
11 #include "SkColorPriv.h"
17 class AutoAAClipValidate {
19 AutoAAClipValidate(const SkAAClip& clip) : fClip(clip) {
22 ~AutoAAClipValidate() {
26 const SkAAClip& fClip;
30 #define AUTO_AACLIP_VALIDATE(clip) AutoAAClipValidate acv(clip)
32 #define AUTO_AACLIP_VALIDATE(clip)
35 ///////////////////////////////////////////////////////////////////////////////
37 #define kMaxInt32 0x7FFFFFFF
40 static inline bool x_in_rect(int x, const SkIRect& rect) {
41 return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
45 static inline bool y_in_rect(int y, const SkIRect& rect) {
46 return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
50 * Data runs are packed [count, alpha]
53 struct SkAAClip::YOffset {
58 struct SkAAClip::RunHead {
64 return (YOffset*)((char*)this + sizeof(RunHead));
66 const YOffset* yoffsets() const {
67 return (const YOffset*)((const char*)this + sizeof(RunHead));
70 return (uint8_t*)(this->yoffsets() + fRowCount);
72 const uint8_t* data() const {
73 return (const uint8_t*)(this->yoffsets() + fRowCount);
76 static RunHead* Alloc(int rowCount, size_t dataSize) {
77 size_t size = sizeof(RunHead) + rowCount * sizeof(YOffset) + dataSize;
78 RunHead* head = (RunHead*)sk_malloc_throw(size);
80 head->fRowCount = rowCount;
81 head->fDataSize = dataSize;
85 static int ComputeRowSizeForWidth(int width) {
86 // 2 bytes per segment, where each segment can store up to 255 for count
90 int n = SkMin32(width, 255);
93 return segments * 2; // each segment is row[0] + row[1] (n + alpha)
96 static RunHead* AllocRect(const SkIRect& bounds) {
97 SkASSERT(!bounds.isEmpty());
98 int width = bounds.width();
99 size_t rowSize = ComputeRowSizeForWidth(width);
100 RunHead* head = RunHead::Alloc(1, rowSize);
101 YOffset* yoff = head->yoffsets();
102 yoff->fY = bounds.height() - 1;
104 uint8_t* row = head->data();
106 int n = SkMin32(width, 255);
116 class SkAAClip::Iter {
118 Iter(const SkAAClip&);
120 bool done() const { return fDone; }
121 int top() const { return fTop; }
122 int bottom() const { return fBottom; }
123 const uint8_t* data() const { return fData; }
127 const YOffset* fCurrYOff;
128 const YOffset* fStopYOff;
129 const uint8_t* fData;
135 SkAAClip::Iter::Iter(const SkAAClip& clip) {
136 if (clip.isEmpty()) {
138 fTop = fBottom = clip.fBounds.fBottom;
145 const RunHead* head = clip.fRunHead;
146 fCurrYOff = head->yoffsets();
147 fStopYOff = fCurrYOff + head->fRowCount;
148 fData = head->data() + fCurrYOff->fOffset;
151 fTop = clip.fBounds.fTop;
152 fBottom = clip.fBounds.fTop + fCurrYOff->fY + 1;
156 void SkAAClip::Iter::next() {
158 const YOffset* prev = fCurrYOff;
159 const YOffset* curr = prev + 1;
160 SkASSERT(curr <= fStopYOff);
163 if (curr >= fStopYOff) {
168 fBottom += curr->fY - prev->fY;
169 fData += curr->fOffset - prev->fOffset;
176 // assert we're exactly width-wide, and then return the number of bytes used
177 static size_t compute_row_length(const uint8_t row[], int width) {
178 const uint8_t* origRow = row;
182 SkASSERT(n <= width);
186 SkASSERT(0 == width);
187 return row - origRow;
190 void SkAAClip::validate() const {
191 if (NULL == fRunHead) {
192 SkASSERT(fBounds.isEmpty());
196 const RunHead* head = fRunHead;
197 SkASSERT(head->fRefCnt > 0);
198 SkASSERT(head->fRowCount > 0);
200 const YOffset* yoff = head->yoffsets();
201 const YOffset* ystop = yoff + head->fRowCount;
202 const int lastY = fBounds.height() - 1;
204 // Y and offset must be monotonic
206 int32_t prevOffset = -1;
207 while (yoff < ystop) {
208 SkASSERT(prevY < yoff->fY);
209 SkASSERT(yoff->fY <= lastY);
211 SkASSERT(prevOffset < (int32_t)yoff->fOffset);
212 prevOffset = yoff->fOffset;
213 const uint8_t* row = head->data() + yoff->fOffset;
214 size_t rowLength = compute_row_length(row, fBounds.width());
215 SkASSERT(yoff->fOffset + rowLength <= head->fDataSize);
218 // check the last entry;
220 SkASSERT(yoff->fY == lastY);
223 static void dump_one_row(const uint8_t* SK_RESTRICT row,
224 int width, int leading_num) {
226 SkDebugf( "%03d ", leading_num );
234 } else if (val > 0) {
237 for (int i = 0 ; i < n ; i++) {
238 SkDebugf( "%c", out );
246 void SkAAClip::debug(bool compress_y) const {
248 const int width = fBounds.width();
250 int y = fBounds.fTop;
251 while (!iter.done()) {
253 dump_one_row(iter.data(), width, iter.bottom() - iter.top() + 1);
256 dump_one_row(iter.data(), width, 0);
257 } while (++y < iter.bottom());
264 ///////////////////////////////////////////////////////////////////////////////
266 // Count the number of zeros on the left and right edges of the passed in
267 // RLE row. If 'row' is all zeros return 'width' in both variables.
268 static void count_left_right_zeros(const uint8_t* row, int width,
269 int* leftZ, int* riteZ) {
277 SkASSERT(n <= width);
285 // this line is completely empty return 'width' in both variables
306 static void test_count_left_right_zeros() {
313 const uint8_t data0[] = { 0, 0, 10, 0xFF };
314 const uint8_t data1[] = { 0, 0, 5, 0xFF, 2, 0, 3, 0xFF };
315 const uint8_t data2[] = { 7, 0, 5, 0, 2, 0, 3, 0xFF };
316 const uint8_t data3[] = { 0, 5, 5, 0xFF, 2, 0, 3, 0 };
317 const uint8_t data4[] = { 2, 3, 2, 0, 5, 0xFF, 3, 0 };
318 const uint8_t data5[] = { 10, 10, 10, 0 };
319 const uint8_t data6[] = { 2, 2, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 };
321 const uint8_t* array[] = {
322 data0, data1, data2, data3, data4, data5, data6
325 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) {
326 const uint8_t* data = array[i];
327 const int expectedL = *data++;
328 const int expectedR = *data++;
329 int L = 12345, R = 12345;
330 count_left_right_zeros(data, 10, &L, &R);
331 SkASSERT(expectedL == L);
332 SkASSERT(expectedR == R);
337 // modify row in place, trimming off (zeros) from the left and right sides.
338 // return the number of bytes that were completely eliminated from the left
339 static int trim_row_left_right(uint8_t* row, int width, int leftZ, int riteZ) {
342 SkASSERT(0 == row[1]);
345 SkASSERT(n <= width);
354 SkASSERT(leftZ >= 0);
358 // walk row to the end, and then we'll back up to trim riteZ
361 SkASSERT(n <= width);
365 // now skip whole runs of zeros
368 SkASSERT(0 == row[1]);
376 SkASSERT(riteZ >= 0);
384 // assert that this row is exactly this width
385 static void assert_row_width(const uint8_t* row, int width) {
389 SkASSERT(n <= width);
393 SkASSERT(0 == width);
396 static void test_trim_row_left_right() {
403 uint8_t data0[] = { 0, 0, 0, 10, 10, 0xFF };
404 uint8_t data1[] = { 2, 0, 0, 10, 5, 0, 2, 0, 3, 0xFF };
405 uint8_t data2[] = { 5, 0, 2, 10, 5, 0, 2, 0, 3, 0xFF };
406 uint8_t data3[] = { 6, 0, 2, 10, 5, 0, 2, 0, 3, 0xFF };
407 uint8_t data4[] = { 0, 0, 0, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 };
408 uint8_t data5[] = { 1, 0, 0, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 };
409 uint8_t data6[] = { 0, 1, 0, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 };
410 uint8_t data7[] = { 1, 1, 0, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 };
411 uint8_t data8[] = { 2, 2, 2, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 };
412 uint8_t data9[] = { 5, 2, 4, 10, 2, 0, 2, 0, 2, 0, 2, 0xFF, 2, 0 };
413 uint8_t data10[] ={ 74, 0, 4, 150, 9, 0, 65, 0, 76, 0xFF };
416 data0, data1, data2, data3, data4,
417 data5, data6, data7, data8, data9,
421 for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) {
422 uint8_t* data = array[i];
423 const int trimL = *data++;
424 const int trimR = *data++;
425 const int expectedSkip = *data++;
426 const int origWidth = *data++;
427 assert_row_width(data, origWidth);
428 int skip = trim_row_left_right(data, origWidth, trimL, trimR);
429 SkASSERT(expectedSkip == skip);
430 int expectedWidth = origWidth - trimL - trimR;
431 assert_row_width(data + skip, expectedWidth);
436 bool SkAAClip::trimLeftRight() {
437 SkDEBUGCODE(test_trim_row_left_right();)
439 if (this->isEmpty()) {
443 AUTO_AACLIP_VALIDATE(*this);
445 const int width = fBounds.width();
446 RunHead* head = fRunHead;
447 YOffset* yoff = head->yoffsets();
448 YOffset* stop = yoff + head->fRowCount;
449 uint8_t* base = head->data();
451 // After this loop, 'leftZeros' & 'rightZeros' will contain the minimum
452 // number of zeros on the left and right of the clip. This information
453 // can be used to shrink the bounding box.
454 int leftZeros = width;
455 int riteZeros = width;
456 while (yoff < stop) {
458 count_left_right_zeros(base + yoff->fOffset, width, &L, &R);
459 SkASSERT(L + R < width || (L == width && R == width));
466 if (0 == (leftZeros | riteZeros)) {
473 SkASSERT(leftZeros || riteZeros);
474 if (width == leftZeros) {
475 SkASSERT(width == riteZeros);
476 return this->setEmpty();
481 fBounds.fLeft += leftZeros;
482 fBounds.fRight -= riteZeros;
483 SkASSERT(!fBounds.isEmpty());
485 // For now we don't realloc the storage (for time), we just shrink in place
486 // This means we don't have to do any memmoves either, since we can just
487 // play tricks with the yoff->fOffset for each row
488 yoff = head->yoffsets();
489 while (yoff < stop) {
490 uint8_t* row = base + yoff->fOffset;
491 SkDEBUGCODE((void)compute_row_length(row, width);)
492 yoff->fOffset += trim_row_left_right(row, width, leftZeros, riteZeros);
493 SkDEBUGCODE((void)compute_row_length(base + yoff->fOffset, width - leftZeros - riteZeros);)
499 static bool row_is_all_zeros(const uint8_t* row, int width) {
506 SkASSERT(n <= width);
510 SkASSERT(0 == width);
514 bool SkAAClip::trimTopBottom() {
515 if (this->isEmpty()) {
521 const int width = fBounds.width();
522 RunHead* head = fRunHead;
523 YOffset* yoff = head->yoffsets();
524 YOffset* stop = yoff + head->fRowCount;
525 const uint8_t* base = head->data();
527 // Look to trim away empty rows from the top.
530 while (yoff < stop) {
531 const uint8_t* data = base + yoff->fOffset;
532 if (!row_is_all_zeros(data, width)) {
538 SkASSERT(skip <= head->fRowCount);
539 if (skip == head->fRowCount) {
540 return this->setEmpty();
543 // adjust fRowCount and fBounds.fTop, and slide all the data up
544 // as we remove [skip] number of YOffset entries
545 yoff = head->yoffsets();
546 int dy = yoff[skip - 1].fY + 1;
547 for (int i = skip; i < head->fRowCount; ++i) {
548 SkASSERT(yoff[i].fY >= dy);
551 YOffset* dst = head->yoffsets();
552 size_t size = head->fRowCount * sizeof(YOffset) + head->fDataSize;
553 memmove(dst, dst + skip, size - skip * sizeof(YOffset));
556 SkASSERT(!fBounds.isEmpty());
557 head->fRowCount -= skip;
558 SkASSERT(head->fRowCount > 0);
561 // need to reset this after the memmove
565 // Look to trim away empty rows from the bottom.
566 // We know that we have at least one non-zero row, so we can just walk
567 // backwards without checking for running past the start.
569 stop = yoff = head->yoffsets() + head->fRowCount;
572 } while (row_is_all_zeros(base + yoff->fOffset, width));
573 skip = SkToInt(stop - yoff - 1);
574 SkASSERT(skip >= 0 && skip < head->fRowCount);
576 // removing from the bottom is easier than from the top, as we don't
577 // have to adjust any of the Y values, we just have to trim the array
578 memmove(stop - skip, stop, head->fDataSize);
580 fBounds.fBottom = fBounds.fTop + yoff->fY + 1;
581 SkASSERT(!fBounds.isEmpty());
582 head->fRowCount -= skip;
583 SkASSERT(head->fRowCount > 0);
590 // can't validate before we're done, since trimming is part of the process of
591 // making us valid after the Builder. Since we build from top to bottom, its
592 // possible our fBounds.fBottom is bigger than our last scanline of data, so
593 // we trim fBounds.fBottom back up.
595 // TODO: check for duplicates in X and Y to further compress our data
597 bool SkAAClip::trimBounds() {
598 if (this->isEmpty()) {
602 const RunHead* head = fRunHead;
603 const YOffset* yoff = head->yoffsets();
605 SkASSERT(head->fRowCount > 0);
606 const YOffset& lastY = yoff[head->fRowCount - 1];
607 SkASSERT(lastY.fY + 1 <= fBounds.height());
608 fBounds.fBottom = fBounds.fTop + lastY.fY + 1;
609 SkASSERT(lastY.fY + 1 == fBounds.height());
610 SkASSERT(!fBounds.isEmpty());
612 return this->trimTopBottom() && this->trimLeftRight();
615 ///////////////////////////////////////////////////////////////////////////////
617 void SkAAClip::freeRuns() {
619 SkASSERT(fRunHead->fRefCnt >= 1);
620 if (1 == sk_atomic_dec(&fRunHead->fRefCnt)) {
626 SkAAClip::SkAAClip() {
631 SkAAClip::SkAAClip(const SkAAClip& src) {
632 SkDEBUGCODE(fBounds.setEmpty();) // need this for validate
637 SkAAClip::~SkAAClip() {
641 SkAAClip& SkAAClip::operator=(const SkAAClip& src) {
642 AUTO_AACLIP_VALIDATE(*this);
647 fBounds = src.fBounds;
648 fRunHead = src.fRunHead;
650 sk_atomic_inc(&fRunHead->fRefCnt);
656 bool operator==(const SkAAClip& a, const SkAAClip& b) {
663 if (a.fBounds != b.fBounds) {
667 const SkAAClip::RunHead* ah = a.fRunHead;
668 const SkAAClip::RunHead* bh = b.fRunHead;
670 // this catches empties and rects being equal
675 // now we insist that both are complex (but different ptrs)
676 if (!a.fRunHead || !b.fRunHead) {
680 return ah->fRowCount == bh->fRowCount &&
681 ah->fDataSize == bh->fDataSize &&
682 !memcmp(ah->data(), bh->data(), ah->fDataSize);
685 void SkAAClip::swap(SkAAClip& other) {
686 AUTO_AACLIP_VALIDATE(*this);
689 SkTSwap(fBounds, other.fBounds);
690 SkTSwap(fRunHead, other.fRunHead);
693 bool SkAAClip::set(const SkAAClip& src) {
695 return !this->isEmpty();
698 bool SkAAClip::setEmpty() {
705 bool SkAAClip::setRect(const SkIRect& bounds) {
706 if (bounds.isEmpty()) {
707 return this->setEmpty();
710 AUTO_AACLIP_VALIDATE(*this);
717 return this->setPath(path);
721 fRunHead = RunHead::AllocRect(bounds);
722 SkASSERT(!this->isEmpty());
727 bool SkAAClip::isRect() const {
728 if (this->isEmpty()) {
732 const RunHead* head = fRunHead;
733 if (head->fRowCount != 1) {
736 const YOffset* yoff = head->yoffsets();
737 if (yoff->fY != fBounds.fBottom - 1) {
741 const uint8_t* row = head->data() + yoff->fOffset;
742 int width = fBounds.width();
744 if (row[1] != 0xFF) {
748 SkASSERT(n <= width);
755 bool SkAAClip::setRect(const SkRect& r, bool doAA) {
757 return this->setEmpty();
760 AUTO_AACLIP_VALIDATE(*this);
762 // TODO: special case this
766 return this->setPath(path, NULL, doAA);
769 static void append_run(SkTDArray<uint8_t>& array, uint8_t value, int count) {
770 SkASSERT(count >= 0);
776 uint8_t* data = array.append(2);
783 bool SkAAClip::setRegion(const SkRegion& rgn) {
785 return this->setEmpty();
788 return this->setRect(rgn.getBounds());
793 SkRegion::Iterator iter(rgn);
794 for (; !iter.done(); iter.next()) {
795 clip.op(iter.rect(), SkRegion::kUnion_Op);
798 return !this->isEmpty();
800 const SkIRect& bounds = rgn.getBounds();
801 const int offsetX = bounds.fLeft;
802 const int offsetY = bounds.fTop;
804 SkTDArray<YOffset> yArray;
805 SkTDArray<uint8_t> xArray;
807 yArray.setReserve(SkMin32(bounds.height(), 1024));
808 xArray.setReserve(SkMin32(bounds.width() * 128, 64 * 1024));
810 SkRegion::Iterator iter(rgn);
813 YOffset* currY = NULL;
815 for (; !iter.done(); iter.next()) {
816 const SkIRect& r = iter.rect();
817 SkASSERT(bounds.contains(r));
819 int bot = r.fBottom - offsetY;
820 SkASSERT(bot >= prevBot);
824 append_run(xArray, 0, bounds.width() - prevRight);
826 // did we introduce an empty-gap from the prev row?
827 int top = r.fTop - offsetY;
829 currY = yArray.append();
831 currY->fOffset = xArray.count();
832 append_run(xArray, 0, bounds.width());
834 // create a new record for this Y value
835 currY = yArray.append();
837 currY->fOffset = xArray.count();
842 int x = r.fLeft - offsetX;
843 append_run(xArray, 0, x - prevRight);
845 int w = r.fRight - r.fLeft;
846 append_run(xArray, 0xFF, w);
848 SkASSERT(prevRight <= bounds.width());
851 append_run(xArray, 0, bounds.width() - prevRight);
853 // now pack everything into a RunHead
854 RunHead* head = RunHead::Alloc(yArray.count(), xArray.bytes());
855 memcpy(head->yoffsets(), yArray.begin(), yArray.bytes());
856 memcpy(head->data(), xArray.begin(), xArray.bytes());
866 ///////////////////////////////////////////////////////////////////////////////
868 const uint8_t* SkAAClip::findRow(int y, int* lastYForRow) const {
871 if (!y_in_rect(y, fBounds)) {
874 y -= fBounds.y(); // our yoffs values are relative to the top
876 const YOffset* yoff = fRunHead->yoffsets();
877 while (yoff->fY < y) {
879 SkASSERT(yoff - fRunHead->yoffsets() < fRunHead->fRowCount);
883 *lastYForRow = fBounds.y() + yoff->fY;
885 return fRunHead->data() + yoff->fOffset;
888 const uint8_t* SkAAClip::findX(const uint8_t data[], int x, int* initialCount) const {
889 SkASSERT(x_in_rect(x, fBounds));
892 // first skip up to X
897 *initialCount = n - x;
907 bool SkAAClip::quickContains(int left, int top, int right, int bottom) const {
908 if (this->isEmpty()) {
911 if (!fBounds.contains(left, top, right, bottom)) {
915 if (this->isRect()) {
920 int lastY SK_INIT_TO_AVOID_WARNING;
921 const uint8_t* row = this->findRow(top, &lastY);
922 if (lastY < bottom) {
925 // now just need to check in X
927 row = this->findX(row, left, &count);
929 return count >= (right - left) && 0xFF == row[1];
931 int rectWidth = right - left;
932 while (0xFF == row[1]) {
933 if (count >= rectWidth) {
944 ///////////////////////////////////////////////////////////////////////////////
946 class SkAAClip::Builder {
951 SkTDArray<uint8_t>* fData;
953 SkTDArray<Row> fRows;
960 Builder(const SkIRect& bounds) : fBounds(bounds) {
962 fWidth = bounds.width();
968 Row* row = fRows.begin();
969 Row* stop = fRows.end();
976 const SkIRect& getBounds() const { return fBounds; }
978 void addRun(int x, int y, U8CPU alpha, int count) {
980 SkASSERT(fBounds.contains(x, y));
981 SkASSERT(fBounds.contains(x + count - 1, y));
988 SkASSERT(y > fPrevY);
990 row = this->flushRow(true);
993 SkASSERT(row->fData);
994 SkASSERT(0 == row->fData->count());
998 SkASSERT(row->fWidth <= x);
999 SkASSERT(row->fWidth < fBounds.width());
1001 SkTDArray<uint8_t>& data = *row->fData;
1003 int gap = x - row->fWidth;
1005 AppendRun(data, 0, gap);
1007 SkASSERT(row->fWidth < fBounds.width());
1010 AppendRun(data, alpha, count);
1011 row->fWidth += count;
1012 SkASSERT(row->fWidth <= fBounds.width());
1015 void addColumn(int x, int y, U8CPU alpha, int height) {
1016 SkASSERT(fBounds.contains(x, y + height - 1));
1018 this->addRun(x, y, alpha, 1);
1019 this->flushRowH(fCurrRow);
1021 SkASSERT(y == fCurrRow->fY);
1022 fCurrRow->fY = y + height - 1;
1025 void addRectRun(int x, int y, int width, int height) {
1026 SkASSERT(fBounds.contains(x + width - 1, y + height - 1));
1027 this->addRun(x, y, 0xFF, width);
1029 // we assum the rect must be all we'll see for these scanlines
1030 // so we ensure our row goes all the way to our right
1031 this->flushRowH(fCurrRow);
1034 SkASSERT(y == fCurrRow->fY);
1035 fCurrRow->fY = y + height - 1;
1038 void addAntiRectRun(int x, int y, int width, int height,
1039 SkAlpha leftAlpha, SkAlpha rightAlpha) {
1040 SkASSERT(fBounds.contains(x + width - 1 +
1041 (leftAlpha > 0 ? 1 : 0) + (rightAlpha > 0 ? 1 : 0),
1043 SkASSERT(width >= 0);
1045 // Conceptually we're always adding 3 runs, but we should
1046 // merge or omit them if possible.
1047 if (leftAlpha == 0xFF) {
1049 } else if (leftAlpha > 0) {
1050 this->addRun(x++, y, leftAlpha, 1);
1052 if (rightAlpha == 0xFF) {
1056 this->addRun(x, y, 0xFF, width);
1058 if (rightAlpha > 0 && rightAlpha < 255) {
1059 this->addRun(x + width, y, rightAlpha, 1);
1062 // we assume the rect must be all we'll see for these scanlines
1063 // so we ensure our row goes all the way to our right
1064 this->flushRowH(fCurrRow);
1067 SkASSERT(y == fCurrRow->fY);
1068 fCurrRow->fY = y + height - 1;
1071 bool finish(SkAAClip* target) {
1072 this->flushRow(false);
1074 const Row* row = fRows.begin();
1075 const Row* stop = fRows.end();
1077 size_t dataSize = 0;
1078 while (row < stop) {
1079 dataSize += row->fData->count();
1083 if (0 == dataSize) {
1084 return target->setEmpty();
1087 SkASSERT(fMinY >= fBounds.fTop);
1088 SkASSERT(fMinY < fBounds.fBottom);
1089 int adjustY = fMinY - fBounds.fTop;
1090 fBounds.fTop = fMinY;
1092 RunHead* head = RunHead::Alloc(fRows.count(), dataSize);
1093 YOffset* yoffset = head->yoffsets();
1094 uint8_t* data = head->data();
1095 uint8_t* baseData = data;
1097 row = fRows.begin();
1098 SkDEBUGCODE(int prevY = row->fY - 1;)
1099 while (row < stop) {
1100 SkASSERT(prevY < row->fY); // must be monotonic
1101 SkDEBUGCODE(prevY = row->fY);
1103 yoffset->fY = row->fY - adjustY;
1104 yoffset->fOffset = SkToU32(data - baseData);
1107 size_t n = row->fData->count();
1108 memcpy(data, row->fData->begin(), n);
1110 size_t bytesNeeded = compute_row_length(data, fBounds.width());
1111 SkASSERT(bytesNeeded == n);
1119 target->fBounds = fBounds;
1120 target->fRunHead = head;
1121 return target->trimBounds();
1127 for (y = 0; y < fRows.count(); ++y) {
1128 const Row& row = fRows[y];
1129 SkDebugf("Y:%3d W:%3d", row.fY, row.fWidth);
1130 const SkTDArray<uint8_t>& data = *row.fData;
1131 int count = data.count();
1132 SkASSERT(!(count & 1));
1133 const uint8_t* ptr = data.begin();
1134 for (int x = 0; x < count; x += 2) {
1135 SkDebugf(" [%3d:%02X]", ptr[0], ptr[1]);
1144 if (false) { // avoid bit rot, suppress warning
1145 test_count_left_right_zeros();
1148 for (int i = 0; i < fRows.count(); ++i) {
1149 const Row& row = fRows[i];
1150 SkASSERT(prevY < row.fY);
1151 SkASSERT(fWidth == row.fWidth);
1152 int count = row.fData->count();
1153 const uint8_t* ptr = row.fData->begin();
1154 SkASSERT(!(count & 1));
1156 for (int x = 0; x < count; x += 2) {
1160 SkASSERT(w <= fWidth);
1163 SkASSERT(w == fWidth);
1169 // only called by BuilderBlitter
1170 void setMinY(int y) {
1175 void flushRowH(Row* row) {
1176 // flush current row if needed
1177 if (row->fWidth < fWidth) {
1178 AppendRun(*row->fData, 0, fWidth - row->fWidth);
1179 row->fWidth = fWidth;
1183 Row* flushRow(bool readyForAnother) {
1185 int count = fRows.count();
1187 this->flushRowH(&fRows[count - 1]);
1190 // are our last two runs the same?
1191 Row* prev = &fRows[count - 2];
1192 Row* curr = &fRows[count - 1];
1193 SkASSERT(prev->fWidth == fWidth);
1194 SkASSERT(curr->fWidth == fWidth);
1195 if (*prev->fData == *curr->fData) {
1196 prev->fY = curr->fY;
1197 if (readyForAnother) {
1198 curr->fData->rewind();
1202 fRows.removeShuffle(count - 1);
1205 if (readyForAnother) {
1206 next = fRows.append();
1207 next->fData = new SkTDArray<uint8_t>;
1211 if (readyForAnother) {
1212 next = fRows.append();
1213 next->fData = new SkTDArray<uint8_t>;
1219 static void AppendRun(SkTDArray<uint8_t>& data, U8CPU alpha, int count) {
1225 uint8_t* ptr = data.append(2);
1229 } while (count > 0);
1233 class SkAAClip::BuilderBlitter : public SkBlitter {
1237 If we see a gap of 1 or more empty scanlines while building in Y-order,
1238 we inject an explicit empty scanline (alpha==0)
1240 See AAClipTest.cpp : test_path_with_hole()
1242 void checkForYGap(int y) {
1243 SkASSERT(y >= fLastY);
1244 if (fLastY > -SK_MaxS32) {
1245 int gap = y - fLastY;
1247 fBuilder->addRun(fLeft, y - 1, 0, fRight - fLeft);
1255 BuilderBlitter(Builder* builder) {
1257 fLeft = builder->getBounds().fLeft;
1258 fRight = builder->getBounds().fRight;
1260 fLastY = -SK_MaxS32; // sentinel
1264 if (fMinY < SK_MaxS32) {
1265 fBuilder->setMinY(fMinY);
1270 Must evaluate clips in scan-line order, so don't want to allow blitV(),
1271 but an AAClip can be clipped down to a single pixel wide, so we
1272 must support it (given AntiRect semantics: minimum width is 2).
1273 Instead we'll rely on the runtime asserts to guarantee Y monotonicity;
1274 any failure cases that misses may have minor artifacts.
1276 void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE {
1277 this->recordMinY(y);
1278 fBuilder->addColumn(x, y, alpha, height);
1279 fLastY = y + height - 1;
1282 void blitRect(int x, int y, int width, int height) SK_OVERRIDE {
1283 this->recordMinY(y);
1284 this->checkForYGap(y);
1285 fBuilder->addRectRun(x, y, width, height);
1286 fLastY = y + height - 1;
1289 virtual void blitAntiRect(int x, int y, int width, int height,
1290 SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE {
1291 this->recordMinY(y);
1292 this->checkForYGap(y);
1293 fBuilder->addAntiRectRun(x, y, width, height, leftAlpha, rightAlpha);
1294 fLastY = y + height - 1;
1297 void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE
1300 const SkBitmap* justAnOpaqueColor(uint32_t*) SK_OVERRIDE {
1304 void blitH(int x, int y, int width) SK_OVERRIDE {
1305 this->recordMinY(y);
1306 this->checkForYGap(y);
1307 fBuilder->addRun(x, y, 0xFF, width);
1310 virtual void blitAntiH(int x, int y, const SkAlpha alpha[],
1311 const int16_t runs[]) SK_OVERRIDE {
1312 this->recordMinY(y);
1313 this->checkForYGap(y);
1320 // The supersampler's buffer can be the width of the device, so
1321 // we may have to trim the run to our bounds. If so, we assert that
1322 // the extra spans are always alpha==0
1324 int localCount = count;
1326 SkASSERT(0 == *alpha);
1327 int gap = fLeft - x;
1328 SkASSERT(gap <= count);
1332 int right = x + count;
1333 if (right > fRight) {
1334 SkASSERT(0 == *alpha);
1335 localCount -= right - fRight;
1336 SkASSERT(localCount >= 0);
1340 fBuilder->addRun(localX, y, *alpha, localCount);
1351 int fLeft; // cache of builder's bounds' left edge
1356 * We track this, in case the scan converter skipped some number of
1357 * scanlines at the (relative to the bounds it was given). This allows
1358 * the builder, during its finish, to trip its bounds down to the "real"
1361 void recordMinY(int y) {
1368 SkDebugf("---- did not expect to get called here");
1373 bool SkAAClip::setPath(const SkPath& path, const SkRegion* clip, bool doAA) {
1374 AUTO_AACLIP_VALIDATE(*this);
1376 if (clip && clip->isEmpty()) {
1377 return this->setEmpty();
1381 path.getBounds().roundOut(&ibounds);
1385 tmpClip.setRect(ibounds);
1389 if (path.isInverseFillType()) {
1390 ibounds = clip->getBounds();
1392 if (ibounds.isEmpty() || !ibounds.intersect(clip->getBounds())) {
1393 return this->setEmpty();
1397 Builder builder(ibounds);
1398 BuilderBlitter blitter(&builder);
1401 SkScan::AntiFillPath(path, *clip, &blitter, true);
1403 SkScan::FillPath(path, *clip, &blitter);
1407 return builder.finish(this);
1410 ///////////////////////////////////////////////////////////////////////////////
1412 typedef void (*RowProc)(SkAAClip::Builder&, int bottom,
1413 const uint8_t* rowA, const SkIRect& rectA,
1414 const uint8_t* rowB, const SkIRect& rectB);
1416 typedef U8CPU (*AlphaProc)(U8CPU alphaA, U8CPU alphaB);
1418 static U8CPU sectAlphaProc(U8CPU alphaA, U8CPU alphaB) {
1420 return SkMulDiv255Round(alphaA, alphaB);
1423 static U8CPU unionAlphaProc(U8CPU alphaA, U8CPU alphaB) {
1425 return alphaA + alphaB - SkMulDiv255Round(alphaA, alphaB);
1428 static U8CPU diffAlphaProc(U8CPU alphaA, U8CPU alphaB) {
1430 return SkMulDiv255Round(alphaA, 0xFF - alphaB);
1433 static U8CPU xorAlphaProc(U8CPU alphaA, U8CPU alphaB) {
1435 return alphaA + alphaB - 2 * SkMulDiv255Round(alphaA, alphaB);
1438 static AlphaProc find_alpha_proc(SkRegion::Op op) {
1440 case SkRegion::kIntersect_Op:
1441 return sectAlphaProc;
1442 case SkRegion::kDifference_Op:
1443 return diffAlphaProc;
1444 case SkRegion::kUnion_Op:
1445 return unionAlphaProc;
1446 case SkRegion::kXOR_Op:
1447 return xorAlphaProc;
1449 SkDEBUGFAIL("unexpected region op");
1450 return sectAlphaProc;
1456 RowIter(const uint8_t* row, const SkIRect& bounds) {
1458 fLeft = bounds.fLeft;
1459 fBoundsRight = bounds.fRight;
1461 fRight = bounds.fLeft + row[0];
1462 SkASSERT(fRight <= fBoundsRight);
1472 bool done() const { return fDone; }
1473 int left() const { return fLeft; }
1474 int right() const { return fRight; }
1475 U8CPU alpha() const { return fAlpha; }
1479 if (fRight == fBoundsRight) {
1487 SkASSERT(fRight <= fBoundsRight);
1493 const uint8_t* fRow;
1501 static void adjust_row(RowIter& iter, int& leftA, int& riteA, int rite) {
1502 if (rite == riteA) {
1504 leftA = iter.left();
1505 riteA = iter.right();
1510 static bool intersect(int& min, int& max, int boundsMin, int boundsMax) {
1511 SkASSERT(min < max);
1512 SkASSERT(boundsMin < boundsMax);
1513 if (min >= boundsMax || max <= boundsMin) {
1516 if (min < boundsMin) {
1519 if (max > boundsMax) {
1526 static void operatorX(SkAAClip::Builder& builder, int lastY,
1527 RowIter& iterA, RowIter& iterB,
1528 AlphaProc proc, const SkIRect& bounds) {
1529 int leftA = iterA.left();
1530 int riteA = iterA.right();
1531 int leftB = iterB.left();
1532 int riteB = iterB.right();
1534 int prevRite = bounds.fLeft;
1541 if (leftA < leftB) {
1543 alphaA = iterA.alpha();
1544 if (riteA <= leftB) {
1547 rite = leftA = leftB;
1549 } else if (leftB < leftA) {
1551 alphaB = iterB.alpha();
1552 if (riteB <= leftA) {
1555 rite = leftB = leftA;
1558 left = leftA; // or leftB, since leftA == leftB
1559 rite = leftA = leftB = SkMin32(riteA, riteB);
1560 alphaA = iterA.alpha();
1561 alphaB = iterB.alpha();
1564 if (left >= bounds.fRight) {
1567 if (rite > bounds.fRight) {
1568 rite = bounds.fRight;
1571 if (left >= bounds.fLeft) {
1572 SkASSERT(rite > left);
1573 builder.addRun(left, lastY, proc(alphaA, alphaB), rite - left);
1577 adjust_row(iterA, leftA, riteA, rite);
1578 adjust_row(iterB, leftB, riteB, rite);
1579 } while (!iterA.done() || !iterB.done());
1581 if (prevRite < bounds.fRight) {
1582 builder.addRun(prevRite, lastY, 0, bounds.fRight - prevRite);
1586 static void adjust_iter(SkAAClip::Iter& iter, int& topA, int& botA, int bot) {
1590 SkASSERT(botA == iter.top());
1591 botA = iter.bottom();
1595 static void operateY(SkAAClip::Builder& builder, const SkAAClip& A,
1596 const SkAAClip& B, SkRegion::Op op) {
1597 AlphaProc proc = find_alpha_proc(op);
1598 const SkIRect& bounds = builder.getBounds();
1600 SkAAClip::Iter iterA(A);
1601 SkAAClip::Iter iterB(B);
1603 SkASSERT(!iterA.done());
1604 int topA = iterA.top();
1605 int botA = iterA.bottom();
1606 SkASSERT(!iterB.done());
1607 int topB = iterB.top();
1608 int botB = iterB.bottom();
1611 const uint8_t* rowA = NULL;
1612 const uint8_t* rowB = NULL;
1617 rowA = iterA.data();
1624 } else if (topB < topA) {
1626 rowB = iterB.data();
1633 top = topA; // or topB, since topA == topB
1634 bot = topA = topB = SkMin32(botA, botB);
1635 rowA = iterA.data();
1636 rowB = iterB.data();
1639 if (top >= bounds.fBottom) {
1643 if (bot > bounds.fBottom) {
1644 bot = bounds.fBottom;
1646 SkASSERT(top < bot);
1648 if (!rowA && !rowB) {
1649 builder.addRun(bounds.fLeft, bot - 1, 0, bounds.width());
1650 } else if (top >= bounds.fTop) {
1651 SkASSERT(bot <= bounds.fBottom);
1652 RowIter rowIterA(rowA, rowA ? A.getBounds() : bounds);
1653 RowIter rowIterB(rowB, rowB ? B.getBounds() : bounds);
1654 operatorX(builder, bot - 1, rowIterA, rowIterB, proc, bounds);
1657 adjust_iter(iterA, topA, botA, bot);
1658 adjust_iter(iterB, topB, botB, bot);
1659 } while (!iterA.done() || !iterB.done());
1662 bool SkAAClip::op(const SkAAClip& clipAOrig, const SkAAClip& clipBOrig,
1664 AUTO_AACLIP_VALIDATE(*this);
1666 if (SkRegion::kReplace_Op == op) {
1667 return this->set(clipBOrig);
1670 const SkAAClip* clipA = &clipAOrig;
1671 const SkAAClip* clipB = &clipBOrig;
1673 if (SkRegion::kReverseDifference_Op == op) {
1674 SkTSwap(clipA, clipB);
1675 op = SkRegion::kDifference_Op;
1678 bool a_empty = clipA->isEmpty();
1679 bool b_empty = clipB->isEmpty();
1683 case SkRegion::kDifference_Op:
1685 return this->setEmpty();
1687 if (b_empty || !SkIRect::Intersects(clipA->fBounds, clipB->fBounds)) {
1688 return this->set(*clipA);
1690 bounds = clipA->fBounds;
1693 case SkRegion::kIntersect_Op:
1694 if ((a_empty | b_empty) || !bounds.intersect(clipA->fBounds,
1696 return this->setEmpty();
1700 case SkRegion::kUnion_Op:
1701 case SkRegion::kXOR_Op:
1703 return this->set(*clipB);
1706 return this->set(*clipA);
1708 bounds = clipA->fBounds;
1709 bounds.join(clipB->fBounds);
1713 SkDEBUGFAIL("unknown region op");
1714 return !this->isEmpty();
1717 SkASSERT(SkIRect::Intersects(bounds, clipB->fBounds));
1718 SkASSERT(SkIRect::Intersects(bounds, clipB->fBounds));
1720 Builder builder(bounds);
1721 operateY(builder, *clipA, *clipB, op);
1723 return builder.finish(this);
1727 * It can be expensive to build a local aaclip before applying the op, so
1728 * we first see if we can restrict the bounds of new rect to our current
1729 * bounds, or note that the new rect subsumes our current clip.
1732 bool SkAAClip::op(const SkIRect& rOrig, SkRegion::Op op) {
1734 const SkIRect* r = &rOrig;
1737 case SkRegion::kIntersect_Op:
1738 if (!rStorage.intersect(rOrig, fBounds)) {
1739 // no overlap, so we're empty
1740 return this->setEmpty();
1742 if (rStorage == fBounds) {
1743 // we were wholly inside the rect, no change
1744 return !this->isEmpty();
1746 if (this->quickContains(rStorage)) {
1747 // the intersection is wholly inside us, we're a rect
1748 return this->setRect(rStorage);
1750 r = &rStorage; // use the intersected bounds
1752 case SkRegion::kDifference_Op:
1754 case SkRegion::kUnion_Op:
1755 if (rOrig.contains(fBounds)) {
1756 return this->setRect(rOrig);
1765 return this->op(*this, clip, op);
1768 bool SkAAClip::op(const SkRect& rOrig, SkRegion::Op op, bool doAA) {
1769 SkRect rStorage, boundsStorage;
1770 const SkRect* r = &rOrig;
1772 boundsStorage.set(fBounds);
1774 case SkRegion::kIntersect_Op:
1775 case SkRegion::kDifference_Op:
1776 if (!rStorage.intersect(rOrig, boundsStorage)) {
1777 if (SkRegion::kIntersect_Op == op) {
1778 return this->setEmpty();
1779 } else { // kDifference
1780 return !this->isEmpty();
1783 r = &rStorage; // use the intersected bounds
1785 case SkRegion::kUnion_Op:
1786 if (rOrig.contains(boundsStorage)) {
1787 return this->setRect(rOrig);
1795 clip.setRect(*r, doAA);
1796 return this->op(*this, clip, op);
1799 bool SkAAClip::op(const SkAAClip& clip, SkRegion::Op op) {
1800 return this->op(*this, clip, op);
1803 ///////////////////////////////////////////////////////////////////////////////
1805 bool SkAAClip::translate(int dx, int dy, SkAAClip* dst) const {
1807 return !this->isEmpty();
1810 if (this->isEmpty()) {
1811 return dst->setEmpty();
1815 sk_atomic_inc(&fRunHead->fRefCnt);
1817 dst->fRunHead = fRunHead;
1818 dst->fBounds = fBounds;
1820 dst->fBounds.offset(dx, dy);
1824 static void expand_row_to_mask(uint8_t* SK_RESTRICT mask,
1825 const uint8_t* SK_RESTRICT row,
1829 SkASSERT(width >= n);
1830 memset(mask, row[1], n);
1835 SkASSERT(0 == width);
1838 void SkAAClip::copyToMask(SkMask* mask) const {
1839 mask->fFormat = SkMask::kA8_Format;
1840 if (this->isEmpty()) {
1841 mask->fBounds.setEmpty();
1842 mask->fImage = NULL;
1843 mask->fRowBytes = 0;
1847 mask->fBounds = fBounds;
1848 mask->fRowBytes = fBounds.width();
1849 size_t size = mask->computeImageSize();
1850 mask->fImage = SkMask::AllocImage(size);
1853 uint8_t* dst = mask->fImage;
1854 const int width = fBounds.width();
1856 int y = fBounds.fTop;
1857 while (!iter.done()) {
1859 expand_row_to_mask(dst, iter.data(), width);
1860 dst += mask->fRowBytes;
1861 } while (++y < iter.bottom());
1866 ///////////////////////////////////////////////////////////////////////////////
1867 ///////////////////////////////////////////////////////////////////////////////
1869 static void expandToRuns(const uint8_t* SK_RESTRICT data, int initialCount, int width,
1870 int16_t* SK_RESTRICT runs, SkAlpha* SK_RESTRICT aa) {
1871 // we don't read our initial n from data, since the caller may have had to
1872 // clip it, hence the initialCount parameter.
1873 int n = initialCount;
1890 // load the next count
1893 runs[0] = 0; // sentinel
1896 SkAAClipBlitter::~SkAAClipBlitter() {
1897 sk_free(fScanlineScratch);
1900 void SkAAClipBlitter::ensureRunsAndAA() {
1901 if (NULL == fScanlineScratch) {
1902 // add 1 so we can store the terminating run count of 0
1903 int count = fAAClipBounds.width() + 1;
1904 // we use this either for fRuns + fAA, or a scaline of a mask
1905 // which may be as deep as 32bits
1906 fScanlineScratch = sk_malloc_throw(count * sizeof(SkPMColor));
1907 fRuns = (int16_t*)fScanlineScratch;
1908 fAA = (SkAlpha*)(fRuns + count);
1912 void SkAAClipBlitter::blitH(int x, int y, int width) {
1913 SkASSERT(width > 0);
1914 SkASSERT(fAAClipBounds.contains(x, y));
1915 SkASSERT(fAAClipBounds.contains(x + width - 1, y));
1917 const uint8_t* row = fAAClip->findRow(y);
1919 row = fAAClip->findX(row, x, &initialCount);
1921 if (initialCount >= width) {
1922 SkAlpha alpha = row[1];
1926 if (0xFF == alpha) {
1927 fBlitter->blitH(x, y, width);
1932 this->ensureRunsAndAA();
1933 expandToRuns(row, initialCount, width, fRuns, fAA);
1935 fBlitter->blitAntiH(x, y, fAA, fRuns);
1938 static void merge(const uint8_t* SK_RESTRICT row, int rowN,
1939 const SkAlpha* SK_RESTRICT srcAA,
1940 const int16_t* SK_RESTRICT srcRuns,
1941 SkAlpha* SK_RESTRICT dstAA,
1942 int16_t* SK_RESTRICT dstRuns,
1944 SkDEBUGCODE(int accumulated = 0;)
1945 int srcN = srcRuns[0];
1946 // do we need this check?
1955 unsigned newAlpha = SkMulDiv255Round(srcAA[0], row[1]);
1956 int minN = SkMin32(srcN, rowN);
1959 dstAA[0] = newAlpha;
1962 if (0 == (srcN -= minN)) {
1963 srcN = srcRuns[0]; // refresh
1966 srcN = srcRuns[0]; // reload
1971 if (0 == (rowN -= minN)) {
1973 rowN = row[0]; // reload
1976 SkDEBUGCODE(accumulated += minN;)
1977 SkASSERT(accumulated <= width);
1982 void SkAAClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
1983 const int16_t runs[]) {
1985 const uint8_t* row = fAAClip->findRow(y);
1987 row = fAAClip->findX(row, x, &initialCount);
1989 this->ensureRunsAndAA();
1991 merge(row, initialCount, aa, runs, fAA, fRuns, fAAClipBounds.width());
1992 fBlitter->blitAntiH(x, y, fAA, fRuns);
1995 void SkAAClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
1996 if (fAAClip->quickContains(x, y, x + 1, y + height)) {
1997 fBlitter->blitV(x, y, height, alpha);
2002 int lastY SK_INIT_TO_AVOID_WARNING;
2003 const uint8_t* row = fAAClip->findRow(y, &lastY);
2004 int dy = lastY - y + 1;
2010 row = fAAClip->findX(row, x);
2011 SkAlpha newAlpha = SkMulDiv255Round(alpha, row[1]);
2013 fBlitter->blitV(x, y, dy, newAlpha);
2015 SkASSERT(height >= 0);
2023 void SkAAClipBlitter::blitRect(int x, int y, int width, int height) {
2024 if (fAAClip->quickContains(x, y, x + width, y + height)) {
2025 fBlitter->blitRect(x, y, width, height);
2029 while (--height >= 0) {
2030 this->blitH(x, y, width);
2035 typedef void (*MergeAAProc)(const void* src, int width, const uint8_t* row,
2036 int initialRowCount, void* dst);
2038 static void small_memcpy(void* dst, const void* src, size_t n) {
2039 memcpy(dst, src, n);
2042 static void small_bzero(void* dst, size_t n) {
2046 static inline uint8_t mergeOne(uint8_t value, unsigned alpha) {
2047 return SkMulDiv255Round(value, alpha);
2050 static inline uint16_t mergeOne(uint16_t value, unsigned alpha) {
2051 unsigned r = SkGetPackedR16(value);
2052 unsigned g = SkGetPackedG16(value);
2053 unsigned b = SkGetPackedB16(value);
2054 return SkPackRGB16(SkMulDiv255Round(r, alpha),
2055 SkMulDiv255Round(g, alpha),
2056 SkMulDiv255Round(b, alpha));
2059 template <typename T> void mergeT(const T* SK_RESTRICT src, int srcN,
2060 const uint8_t* SK_RESTRICT row, int rowN,
2061 T* SK_RESTRICT dst) {
2066 int n = SkMin32(rowN, srcN);
2067 unsigned rowA = row[1];
2069 small_memcpy(dst, src, n * sizeof(T));
2070 } else if (0 == rowA) {
2071 small_bzero(dst, n * sizeof(T));
2073 for (int i = 0; i < n; ++i) {
2074 dst[i] = mergeOne(src[i], rowA);
2078 if (0 == (srcN -= n)) {
2085 SkASSERT(rowN == n);
2091 static MergeAAProc find_merge_aa_proc(SkMask::Format format) {
2093 case SkMask::kBW_Format:
2094 SkDEBUGFAIL("unsupported");
2096 case SkMask::kA8_Format:
2097 case SkMask::k3D_Format: {
2098 void (*proc8)(const uint8_t*, int, const uint8_t*, int, uint8_t*) = mergeT;
2099 return (MergeAAProc)proc8;
2101 case SkMask::kLCD16_Format: {
2102 void (*proc16)(const uint16_t*, int, const uint8_t*, int, uint16_t*) = mergeT;
2103 return (MergeAAProc)proc16;
2106 SkDEBUGFAIL("unsupported");
2111 static U8CPU bit2byte(int bitInAByte) {
2112 SkASSERT(bitInAByte <= 0xFF);
2113 // negation turns any non-zero into 0xFFFFFF??, so we just shift down
2114 // some value >= 8 to get a full FF value
2115 return -bitInAByte >> 8;
2118 static void upscaleBW2A8(SkMask* dstMask, const SkMask& srcMask) {
2119 SkASSERT(SkMask::kBW_Format == srcMask.fFormat);
2120 SkASSERT(SkMask::kA8_Format == dstMask->fFormat);
2122 const int width = srcMask.fBounds.width();
2123 const int height = srcMask.fBounds.height();
2125 const uint8_t* SK_RESTRICT src = (const uint8_t*)srcMask.fImage;
2126 const size_t srcRB = srcMask.fRowBytes;
2127 uint8_t* SK_RESTRICT dst = (uint8_t*)dstMask->fImage;
2128 const size_t dstRB = dstMask->fRowBytes;
2130 const int wholeBytes = width >> 3;
2131 const int leftOverBits = width & 7;
2133 for (int y = 0; y < height; ++y) {
2134 uint8_t* SK_RESTRICT d = dst;
2135 for (int i = 0; i < wholeBytes; ++i) {
2136 int srcByte = src[i];
2137 d[0] = bit2byte(srcByte & (1 << 7));
2138 d[1] = bit2byte(srcByte & (1 << 6));
2139 d[2] = bit2byte(srcByte & (1 << 5));
2140 d[3] = bit2byte(srcByte & (1 << 4));
2141 d[4] = bit2byte(srcByte & (1 << 3));
2142 d[5] = bit2byte(srcByte & (1 << 2));
2143 d[6] = bit2byte(srcByte & (1 << 1));
2144 d[7] = bit2byte(srcByte & (1 << 0));
2148 int srcByte = src[wholeBytes];
2149 for (int x = 0; x < leftOverBits; ++x) {
2150 *d++ = bit2byte(srcByte & 0x80);
2159 void SkAAClipBlitter::blitMask(const SkMask& origMask, const SkIRect& clip) {
2160 SkASSERT(fAAClip->getBounds().contains(clip));
2162 if (fAAClip->quickContains(clip)) {
2163 fBlitter->blitMask(origMask, clip);
2167 const SkMask* mask = &origMask;
2169 // if we're BW, we need to upscale to A8 (ugh)
2171 grayMask.fImage = NULL;
2172 if (SkMask::kBW_Format == origMask.fFormat) {
2173 grayMask.fFormat = SkMask::kA8_Format;
2174 grayMask.fBounds = origMask.fBounds;
2175 grayMask.fRowBytes = origMask.fBounds.width();
2176 size_t size = grayMask.computeImageSize();
2177 grayMask.fImage = (uint8_t*)fGrayMaskScratch.reset(size,
2178 SkAutoMalloc::kReuse_OnShrink);
2180 upscaleBW2A8(&grayMask, origMask);
2184 this->ensureRunsAndAA();
2186 // HACK -- we are devolving 3D into A8, need to copy the rest of the 3D
2187 // data into a temp block to support it better (ugh)
2189 const void* src = mask->getAddr(clip.fLeft, clip.fTop);
2190 const size_t srcRB = mask->fRowBytes;
2191 const int width = clip.width();
2192 MergeAAProc mergeProc = find_merge_aa_proc(mask->fFormat);
2195 rowMask.fFormat = SkMask::k3D_Format == mask->fFormat ? SkMask::kA8_Format : mask->fFormat;
2196 rowMask.fBounds.fLeft = clip.fLeft;
2197 rowMask.fBounds.fRight = clip.fRight;
2198 rowMask.fRowBytes = mask->fRowBytes; // doesn't matter, since our height==1
2199 rowMask.fImage = (uint8_t*)fScanlineScratch;
2202 const int stopY = y + clip.height();
2205 int localStopY SK_INIT_TO_AVOID_WARNING;
2206 const uint8_t* row = fAAClip->findRow(y, &localStopY);
2207 // findRow returns last Y, not stop, so we add 1
2208 localStopY = SkMin32(localStopY + 1, stopY);
2211 row = fAAClip->findX(row, clip.fLeft, &initialCount);
2213 mergeProc(src, width, row, initialCount, rowMask.fImage);
2214 rowMask.fBounds.fTop = y;
2215 rowMask.fBounds.fBottom = y + 1;
2216 fBlitter->blitMask(rowMask, rowMask.fBounds);
2217 src = (const void*)((const char*)src + srcRB);
2218 } while (++y < localStopY);
2219 } while (y < stopY);
2222 const SkBitmap* SkAAClipBlitter::justAnOpaqueColor(uint32_t* value) {