michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkAAClip.h" michael@0: #include "SkBlitter.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkPath.h" michael@0: #include "SkScan.h" michael@0: #include "SkThread.h" michael@0: #include "SkUtils.h" michael@0: michael@0: class AutoAAClipValidate { michael@0: public: michael@0: AutoAAClipValidate(const SkAAClip& clip) : fClip(clip) { michael@0: fClip.validate(); michael@0: } michael@0: ~AutoAAClipValidate() { michael@0: fClip.validate(); michael@0: } michael@0: private: michael@0: const SkAAClip& fClip; michael@0: }; michael@0: michael@0: #ifdef SK_DEBUG michael@0: #define AUTO_AACLIP_VALIDATE(clip) AutoAAClipValidate acv(clip) michael@0: #else michael@0: #define AUTO_AACLIP_VALIDATE(clip) michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #define kMaxInt32 0x7FFFFFFF michael@0: michael@0: #ifdef SK_DEBUG michael@0: static inline bool x_in_rect(int x, const SkIRect& rect) { michael@0: return (unsigned)(x - rect.fLeft) < (unsigned)rect.width(); michael@0: } michael@0: #endif michael@0: michael@0: static inline bool y_in_rect(int y, const SkIRect& rect) { michael@0: return (unsigned)(y - rect.fTop) < (unsigned)rect.height(); michael@0: } michael@0: michael@0: /* michael@0: * Data runs are packed [count, alpha] michael@0: */ michael@0: michael@0: struct SkAAClip::YOffset { michael@0: int32_t fY; michael@0: uint32_t fOffset; michael@0: }; michael@0: michael@0: struct SkAAClip::RunHead { michael@0: int32_t fRefCnt; michael@0: int32_t fRowCount; michael@0: size_t fDataSize; michael@0: michael@0: YOffset* yoffsets() { michael@0: return (YOffset*)((char*)this + sizeof(RunHead)); michael@0: } michael@0: const YOffset* yoffsets() const { michael@0: return (const YOffset*)((const char*)this + sizeof(RunHead)); michael@0: } michael@0: uint8_t* data() { michael@0: return (uint8_t*)(this->yoffsets() + fRowCount); michael@0: } michael@0: const uint8_t* data() const { michael@0: return (const uint8_t*)(this->yoffsets() + fRowCount); michael@0: } michael@0: michael@0: static RunHead* Alloc(int rowCount, size_t dataSize) { michael@0: size_t size = sizeof(RunHead) + rowCount * sizeof(YOffset) + dataSize; michael@0: RunHead* head = (RunHead*)sk_malloc_throw(size); michael@0: head->fRefCnt = 1; michael@0: head->fRowCount = rowCount; michael@0: head->fDataSize = dataSize; michael@0: return head; michael@0: } michael@0: michael@0: static int ComputeRowSizeForWidth(int width) { michael@0: // 2 bytes per segment, where each segment can store up to 255 for count michael@0: int segments = 0; michael@0: while (width > 0) { michael@0: segments += 1; michael@0: int n = SkMin32(width, 255); michael@0: width -= n; michael@0: } michael@0: return segments * 2; // each segment is row[0] + row[1] (n + alpha) michael@0: } michael@0: michael@0: static RunHead* AllocRect(const SkIRect& bounds) { michael@0: SkASSERT(!bounds.isEmpty()); michael@0: int width = bounds.width(); michael@0: size_t rowSize = ComputeRowSizeForWidth(width); michael@0: RunHead* head = RunHead::Alloc(1, rowSize); michael@0: YOffset* yoff = head->yoffsets(); michael@0: yoff->fY = bounds.height() - 1; michael@0: yoff->fOffset = 0; michael@0: uint8_t* row = head->data(); michael@0: while (width > 0) { michael@0: int n = SkMin32(width, 255); michael@0: row[0] = n; michael@0: row[1] = 0xFF; michael@0: width -= n; michael@0: row += 2; michael@0: } michael@0: return head; michael@0: } michael@0: }; michael@0: michael@0: class SkAAClip::Iter { michael@0: public: michael@0: Iter(const SkAAClip&); michael@0: michael@0: bool done() const { return fDone; } michael@0: int top() const { return fTop; } michael@0: int bottom() const { return fBottom; } michael@0: const uint8_t* data() const { return fData; } michael@0: void next(); michael@0: michael@0: private: michael@0: const YOffset* fCurrYOff; michael@0: const YOffset* fStopYOff; michael@0: const uint8_t* fData; michael@0: michael@0: int fTop, fBottom; michael@0: bool fDone; michael@0: }; michael@0: michael@0: SkAAClip::Iter::Iter(const SkAAClip& clip) { michael@0: if (clip.isEmpty()) { michael@0: fDone = true; michael@0: fTop = fBottom = clip.fBounds.fBottom; michael@0: fData = NULL; michael@0: fCurrYOff = NULL; michael@0: fStopYOff = NULL; michael@0: return; michael@0: } michael@0: michael@0: const RunHead* head = clip.fRunHead; michael@0: fCurrYOff = head->yoffsets(); michael@0: fStopYOff = fCurrYOff + head->fRowCount; michael@0: fData = head->data() + fCurrYOff->fOffset; michael@0: michael@0: // setup first value michael@0: fTop = clip.fBounds.fTop; michael@0: fBottom = clip.fBounds.fTop + fCurrYOff->fY + 1; michael@0: fDone = false; michael@0: } michael@0: michael@0: void SkAAClip::Iter::next() { michael@0: if (!fDone) { michael@0: const YOffset* prev = fCurrYOff; michael@0: const YOffset* curr = prev + 1; michael@0: SkASSERT(curr <= fStopYOff); michael@0: michael@0: fTop = fBottom; michael@0: if (curr >= fStopYOff) { michael@0: fDone = true; michael@0: fBottom = kMaxInt32; michael@0: fData = NULL; michael@0: } else { michael@0: fBottom += curr->fY - prev->fY; michael@0: fData += curr->fOffset - prev->fOffset; michael@0: fCurrYOff = curr; michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: // assert we're exactly width-wide, and then return the number of bytes used michael@0: static size_t compute_row_length(const uint8_t row[], int width) { michael@0: const uint8_t* origRow = row; michael@0: while (width > 0) { michael@0: int n = row[0]; michael@0: SkASSERT(n > 0); michael@0: SkASSERT(n <= width); michael@0: row += 2; michael@0: width -= n; michael@0: } michael@0: SkASSERT(0 == width); michael@0: return row - origRow; michael@0: } michael@0: michael@0: void SkAAClip::validate() const { michael@0: if (NULL == fRunHead) { michael@0: SkASSERT(fBounds.isEmpty()); michael@0: return; michael@0: } michael@0: michael@0: const RunHead* head = fRunHead; michael@0: SkASSERT(head->fRefCnt > 0); michael@0: SkASSERT(head->fRowCount > 0); michael@0: michael@0: const YOffset* yoff = head->yoffsets(); michael@0: const YOffset* ystop = yoff + head->fRowCount; michael@0: const int lastY = fBounds.height() - 1; michael@0: michael@0: // Y and offset must be monotonic michael@0: int prevY = -1; michael@0: int32_t prevOffset = -1; michael@0: while (yoff < ystop) { michael@0: SkASSERT(prevY < yoff->fY); michael@0: SkASSERT(yoff->fY <= lastY); michael@0: prevY = yoff->fY; michael@0: SkASSERT(prevOffset < (int32_t)yoff->fOffset); michael@0: prevOffset = yoff->fOffset; michael@0: const uint8_t* row = head->data() + yoff->fOffset; michael@0: size_t rowLength = compute_row_length(row, fBounds.width()); michael@0: SkASSERT(yoff->fOffset + rowLength <= head->fDataSize); michael@0: yoff += 1; michael@0: } michael@0: // check the last entry; michael@0: --yoff; michael@0: SkASSERT(yoff->fY == lastY); michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // Count the number of zeros on the left and right edges of the passed in michael@0: // RLE row. If 'row' is all zeros return 'width' in both variables. michael@0: static void count_left_right_zeros(const uint8_t* row, int width, michael@0: int* leftZ, int* riteZ) { michael@0: int zeros = 0; michael@0: do { michael@0: if (row[1]) { michael@0: break; michael@0: } michael@0: int n = row[0]; michael@0: SkASSERT(n > 0); michael@0: SkASSERT(n <= width); michael@0: zeros += n; michael@0: row += 2; michael@0: width -= n; michael@0: } while (width > 0); michael@0: *leftZ = zeros; michael@0: michael@0: if (0 == width) { michael@0: // this line is completely empty return 'width' in both variables michael@0: *riteZ = *leftZ; michael@0: return; michael@0: } michael@0: michael@0: zeros = 0; michael@0: while (width > 0) { michael@0: int n = row[0]; michael@0: SkASSERT(n > 0); michael@0: if (0 == row[1]) { michael@0: zeros += n; michael@0: } else { michael@0: zeros = 0; michael@0: } michael@0: row += 2; michael@0: width -= n; michael@0: } michael@0: *riteZ = zeros; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: static void test_count_left_right_zeros() { michael@0: static bool gOnce; michael@0: if (gOnce) { michael@0: return; michael@0: } michael@0: gOnce = true; michael@0: michael@0: const uint8_t data0[] = { 0, 0, 10, 0xFF }; michael@0: const uint8_t data1[] = { 0, 0, 5, 0xFF, 2, 0, 3, 0xFF }; michael@0: const uint8_t data2[] = { 7, 0, 5, 0, 2, 0, 3, 0xFF }; michael@0: const uint8_t data3[] = { 0, 5, 5, 0xFF, 2, 0, 3, 0 }; michael@0: const uint8_t data4[] = { 2, 3, 2, 0, 5, 0xFF, 3, 0 }; michael@0: const uint8_t data5[] = { 10, 10, 10, 0 }; michael@0: const uint8_t data6[] = { 2, 2, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 }; michael@0: michael@0: const uint8_t* array[] = { michael@0: data0, data1, data2, data3, data4, data5, data6 michael@0: }; michael@0: michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { michael@0: const uint8_t* data = array[i]; michael@0: const int expectedL = *data++; michael@0: const int expectedR = *data++; michael@0: int L = 12345, R = 12345; michael@0: count_left_right_zeros(data, 10, &L, &R); michael@0: SkASSERT(expectedL == L); michael@0: SkASSERT(expectedR == R); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: // modify row in place, trimming off (zeros) from the left and right sides. michael@0: // return the number of bytes that were completely eliminated from the left michael@0: static int trim_row_left_right(uint8_t* row, int width, int leftZ, int riteZ) { michael@0: int trim = 0; michael@0: while (leftZ > 0) { michael@0: SkASSERT(0 == row[1]); michael@0: int n = row[0]; michael@0: SkASSERT(n > 0); michael@0: SkASSERT(n <= width); michael@0: width -= n; michael@0: row += 2; michael@0: if (n > leftZ) { michael@0: row[-2] = n - leftZ; michael@0: break; michael@0: } michael@0: trim += 2; michael@0: leftZ -= n; michael@0: SkASSERT(leftZ >= 0); michael@0: } michael@0: michael@0: if (riteZ) { michael@0: // walk row to the end, and then we'll back up to trim riteZ michael@0: while (width > 0) { michael@0: int n = row[0]; michael@0: SkASSERT(n <= width); michael@0: width -= n; michael@0: row += 2; michael@0: } michael@0: // now skip whole runs of zeros michael@0: do { michael@0: row -= 2; michael@0: SkASSERT(0 == row[1]); michael@0: int n = row[0]; michael@0: SkASSERT(n > 0); michael@0: if (n > riteZ) { michael@0: row[0] = n - riteZ; michael@0: break; michael@0: } michael@0: riteZ -= n; michael@0: SkASSERT(riteZ >= 0); michael@0: } while (riteZ > 0); michael@0: } michael@0: michael@0: return trim; michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: // assert that this row is exactly this width michael@0: static void assert_row_width(const uint8_t* row, int width) { michael@0: while (width > 0) { michael@0: int n = row[0]; michael@0: SkASSERT(n > 0); michael@0: SkASSERT(n <= width); michael@0: width -= n; michael@0: row += 2; michael@0: } michael@0: SkASSERT(0 == width); michael@0: } michael@0: michael@0: static void test_trim_row_left_right() { michael@0: static bool gOnce; michael@0: if (gOnce) { michael@0: return; michael@0: } michael@0: gOnce = true; michael@0: michael@0: uint8_t data0[] = { 0, 0, 0, 10, 10, 0xFF }; michael@0: uint8_t data1[] = { 2, 0, 0, 10, 5, 0, 2, 0, 3, 0xFF }; michael@0: uint8_t data2[] = { 5, 0, 2, 10, 5, 0, 2, 0, 3, 0xFF }; michael@0: uint8_t data3[] = { 6, 0, 2, 10, 5, 0, 2, 0, 3, 0xFF }; michael@0: uint8_t data4[] = { 0, 0, 0, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 }; michael@0: uint8_t data5[] = { 1, 0, 0, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 }; michael@0: uint8_t data6[] = { 0, 1, 0, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 }; michael@0: uint8_t data7[] = { 1, 1, 0, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 }; michael@0: uint8_t data8[] = { 2, 2, 2, 10, 2, 0, 2, 0xFF, 2, 0, 2, 0xFF, 2, 0 }; michael@0: uint8_t data9[] = { 5, 2, 4, 10, 2, 0, 2, 0, 2, 0, 2, 0xFF, 2, 0 }; michael@0: uint8_t data10[] ={ 74, 0, 4, 150, 9, 0, 65, 0, 76, 0xFF }; michael@0: michael@0: uint8_t* array[] = { michael@0: data0, data1, data2, data3, data4, michael@0: data5, data6, data7, data8, data9, michael@0: data10 michael@0: }; michael@0: michael@0: for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) { michael@0: uint8_t* data = array[i]; michael@0: const int trimL = *data++; michael@0: const int trimR = *data++; michael@0: const int expectedSkip = *data++; michael@0: const int origWidth = *data++; michael@0: assert_row_width(data, origWidth); michael@0: int skip = trim_row_left_right(data, origWidth, trimL, trimR); michael@0: SkASSERT(expectedSkip == skip); michael@0: int expectedWidth = origWidth - trimL - trimR; michael@0: assert_row_width(data + skip, expectedWidth); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: bool SkAAClip::trimLeftRight() { michael@0: SkDEBUGCODE(test_trim_row_left_right();) michael@0: michael@0: if (this->isEmpty()) { michael@0: return false; michael@0: } michael@0: michael@0: AUTO_AACLIP_VALIDATE(*this); michael@0: michael@0: const int width = fBounds.width(); michael@0: RunHead* head = fRunHead; michael@0: YOffset* yoff = head->yoffsets(); michael@0: YOffset* stop = yoff + head->fRowCount; michael@0: uint8_t* base = head->data(); michael@0: michael@0: // After this loop, 'leftZeros' & 'rightZeros' will contain the minimum michael@0: // number of zeros on the left and right of the clip. This information michael@0: // can be used to shrink the bounding box. michael@0: int leftZeros = width; michael@0: int riteZeros = width; michael@0: while (yoff < stop) { michael@0: int L, R; michael@0: count_left_right_zeros(base + yoff->fOffset, width, &L, &R); michael@0: SkASSERT(L + R < width || (L == width && R == width)); michael@0: if (L < leftZeros) { michael@0: leftZeros = L; michael@0: } michael@0: if (R < riteZeros) { michael@0: riteZeros = R; michael@0: } michael@0: if (0 == (leftZeros | riteZeros)) { michael@0: // no trimming to do michael@0: return true; michael@0: } michael@0: yoff += 1; michael@0: } michael@0: michael@0: SkASSERT(leftZeros || riteZeros); michael@0: if (width == leftZeros) { michael@0: SkASSERT(width == riteZeros); michael@0: return this->setEmpty(); michael@0: } michael@0: michael@0: this->validate(); michael@0: michael@0: fBounds.fLeft += leftZeros; michael@0: fBounds.fRight -= riteZeros; michael@0: SkASSERT(!fBounds.isEmpty()); michael@0: michael@0: // For now we don't realloc the storage (for time), we just shrink in place michael@0: // This means we don't have to do any memmoves either, since we can just michael@0: // play tricks with the yoff->fOffset for each row michael@0: yoff = head->yoffsets(); michael@0: while (yoff < stop) { michael@0: uint8_t* row = base + yoff->fOffset; michael@0: SkDEBUGCODE((void)compute_row_length(row, width);) michael@0: yoff->fOffset += trim_row_left_right(row, width, leftZeros, riteZeros); michael@0: SkDEBUGCODE((void)compute_row_length(base + yoff->fOffset, width - leftZeros - riteZeros);) michael@0: yoff += 1; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: static bool row_is_all_zeros(const uint8_t* row, int width) { michael@0: SkASSERT(width > 0); michael@0: do { michael@0: if (row[1]) { michael@0: return false; michael@0: } michael@0: int n = row[0]; michael@0: SkASSERT(n <= width); michael@0: width -= n; michael@0: row += 2; michael@0: } while (width > 0); michael@0: SkASSERT(0 == width); michael@0: return true; michael@0: } michael@0: michael@0: bool SkAAClip::trimTopBottom() { michael@0: if (this->isEmpty()) { michael@0: return false; michael@0: } michael@0: michael@0: this->validate(); michael@0: michael@0: const int width = fBounds.width(); michael@0: RunHead* head = fRunHead; michael@0: YOffset* yoff = head->yoffsets(); michael@0: YOffset* stop = yoff + head->fRowCount; michael@0: const uint8_t* base = head->data(); michael@0: michael@0: // Look to trim away empty rows from the top. michael@0: // michael@0: int skip = 0; michael@0: while (yoff < stop) { michael@0: const uint8_t* data = base + yoff->fOffset; michael@0: if (!row_is_all_zeros(data, width)) { michael@0: break; michael@0: } michael@0: skip += 1; michael@0: yoff += 1; michael@0: } michael@0: SkASSERT(skip <= head->fRowCount); michael@0: if (skip == head->fRowCount) { michael@0: return this->setEmpty(); michael@0: } michael@0: if (skip > 0) { michael@0: // adjust fRowCount and fBounds.fTop, and slide all the data up michael@0: // as we remove [skip] number of YOffset entries michael@0: yoff = head->yoffsets(); michael@0: int dy = yoff[skip - 1].fY + 1; michael@0: for (int i = skip; i < head->fRowCount; ++i) { michael@0: SkASSERT(yoff[i].fY >= dy); michael@0: yoff[i].fY -= dy; michael@0: } michael@0: YOffset* dst = head->yoffsets(); michael@0: size_t size = head->fRowCount * sizeof(YOffset) + head->fDataSize; michael@0: memmove(dst, dst + skip, size - skip * sizeof(YOffset)); michael@0: michael@0: fBounds.fTop += dy; michael@0: SkASSERT(!fBounds.isEmpty()); michael@0: head->fRowCount -= skip; michael@0: SkASSERT(head->fRowCount > 0); michael@0: michael@0: this->validate(); michael@0: // need to reset this after the memmove michael@0: base = head->data(); michael@0: } michael@0: michael@0: // Look to trim away empty rows from the bottom. michael@0: // We know that we have at least one non-zero row, so we can just walk michael@0: // backwards without checking for running past the start. michael@0: // michael@0: stop = yoff = head->yoffsets() + head->fRowCount; michael@0: do { michael@0: yoff -= 1; michael@0: } while (row_is_all_zeros(base + yoff->fOffset, width)); michael@0: skip = SkToInt(stop - yoff - 1); michael@0: SkASSERT(skip >= 0 && skip < head->fRowCount); michael@0: if (skip > 0) { michael@0: // removing from the bottom is easier than from the top, as we don't michael@0: // have to adjust any of the Y values, we just have to trim the array michael@0: memmove(stop - skip, stop, head->fDataSize); michael@0: michael@0: fBounds.fBottom = fBounds.fTop + yoff->fY + 1; michael@0: SkASSERT(!fBounds.isEmpty()); michael@0: head->fRowCount -= skip; michael@0: SkASSERT(head->fRowCount > 0); michael@0: } michael@0: this->validate(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // can't validate before we're done, since trimming is part of the process of michael@0: // making us valid after the Builder. Since we build from top to bottom, its michael@0: // possible our fBounds.fBottom is bigger than our last scanline of data, so michael@0: // we trim fBounds.fBottom back up. michael@0: // michael@0: // TODO: check for duplicates in X and Y to further compress our data michael@0: // michael@0: bool SkAAClip::trimBounds() { michael@0: if (this->isEmpty()) { michael@0: return false; michael@0: } michael@0: michael@0: const RunHead* head = fRunHead; michael@0: const YOffset* yoff = head->yoffsets(); michael@0: michael@0: SkASSERT(head->fRowCount > 0); michael@0: const YOffset& lastY = yoff[head->fRowCount - 1]; michael@0: SkASSERT(lastY.fY + 1 <= fBounds.height()); michael@0: fBounds.fBottom = fBounds.fTop + lastY.fY + 1; michael@0: SkASSERT(lastY.fY + 1 == fBounds.height()); michael@0: SkASSERT(!fBounds.isEmpty()); michael@0: michael@0: return this->trimTopBottom() && this->trimLeftRight(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkAAClip::freeRuns() { michael@0: if (fRunHead) { michael@0: SkASSERT(fRunHead->fRefCnt >= 1); michael@0: if (1 == sk_atomic_dec(&fRunHead->fRefCnt)) { michael@0: sk_free(fRunHead); michael@0: } michael@0: } michael@0: } michael@0: michael@0: SkAAClip::SkAAClip() { michael@0: fBounds.setEmpty(); michael@0: fRunHead = NULL; michael@0: } michael@0: michael@0: SkAAClip::SkAAClip(const SkAAClip& src) { michael@0: SkDEBUGCODE(fBounds.setEmpty();) // need this for validate michael@0: fRunHead = NULL; michael@0: *this = src; michael@0: } michael@0: michael@0: SkAAClip::~SkAAClip() { michael@0: this->freeRuns(); michael@0: } michael@0: michael@0: SkAAClip& SkAAClip::operator=(const SkAAClip& src) { michael@0: AUTO_AACLIP_VALIDATE(*this); michael@0: src.validate(); michael@0: michael@0: if (this != &src) { michael@0: this->freeRuns(); michael@0: fBounds = src.fBounds; michael@0: fRunHead = src.fRunHead; michael@0: if (fRunHead) { michael@0: sk_atomic_inc(&fRunHead->fRefCnt); michael@0: } michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: bool operator==(const SkAAClip& a, const SkAAClip& b) { michael@0: a.validate(); michael@0: b.validate(); michael@0: michael@0: if (&a == &b) { michael@0: return true; michael@0: } michael@0: if (a.fBounds != b.fBounds) { michael@0: return false; michael@0: } michael@0: michael@0: const SkAAClip::RunHead* ah = a.fRunHead; michael@0: const SkAAClip::RunHead* bh = b.fRunHead; michael@0: michael@0: // this catches empties and rects being equal michael@0: if (ah == bh) { michael@0: return true; michael@0: } michael@0: michael@0: // now we insist that both are complex (but different ptrs) michael@0: if (!a.fRunHead || !b.fRunHead) { michael@0: return false; michael@0: } michael@0: michael@0: return ah->fRowCount == bh->fRowCount && michael@0: ah->fDataSize == bh->fDataSize && michael@0: !memcmp(ah->data(), bh->data(), ah->fDataSize); michael@0: } michael@0: michael@0: void SkAAClip::swap(SkAAClip& other) { michael@0: AUTO_AACLIP_VALIDATE(*this); michael@0: other.validate(); michael@0: michael@0: SkTSwap(fBounds, other.fBounds); michael@0: SkTSwap(fRunHead, other.fRunHead); michael@0: } michael@0: michael@0: bool SkAAClip::set(const SkAAClip& src) { michael@0: *this = src; michael@0: return !this->isEmpty(); michael@0: } michael@0: michael@0: bool SkAAClip::setEmpty() { michael@0: this->freeRuns(); michael@0: fBounds.setEmpty(); michael@0: fRunHead = NULL; michael@0: return false; michael@0: } michael@0: michael@0: bool SkAAClip::setRect(const SkIRect& bounds) { michael@0: if (bounds.isEmpty()) { michael@0: return this->setEmpty(); michael@0: } michael@0: michael@0: AUTO_AACLIP_VALIDATE(*this); michael@0: michael@0: #if 0 michael@0: SkRect r; michael@0: r.set(bounds); michael@0: SkPath path; michael@0: path.addRect(r); michael@0: return this->setPath(path); michael@0: #else michael@0: this->freeRuns(); michael@0: fBounds = bounds; michael@0: fRunHead = RunHead::AllocRect(bounds); michael@0: SkASSERT(!this->isEmpty()); michael@0: return true; michael@0: #endif michael@0: } michael@0: michael@0: bool SkAAClip::setRect(const SkRect& r, bool doAA) { michael@0: if (r.isEmpty()) { michael@0: return this->setEmpty(); michael@0: } michael@0: michael@0: AUTO_AACLIP_VALIDATE(*this); michael@0: michael@0: // TODO: special case this michael@0: michael@0: SkPath path; michael@0: path.addRect(r); michael@0: return this->setPath(path, NULL, doAA); michael@0: } michael@0: michael@0: static void append_run(SkTDArray& array, uint8_t value, int count) { michael@0: SkASSERT(count >= 0); michael@0: while (count > 0) { michael@0: int n = count; michael@0: if (n > 255) { michael@0: n = 255; michael@0: } michael@0: uint8_t* data = array.append(2); michael@0: data[0] = n; michael@0: data[1] = value; michael@0: count -= n; michael@0: } michael@0: } michael@0: michael@0: bool SkAAClip::setRegion(const SkRegion& rgn) { michael@0: if (rgn.isEmpty()) { michael@0: return this->setEmpty(); michael@0: } michael@0: if (rgn.isRect()) { michael@0: return this->setRect(rgn.getBounds()); michael@0: } michael@0: michael@0: #if 0 michael@0: SkAAClip clip; michael@0: SkRegion::Iterator iter(rgn); michael@0: for (; !iter.done(); iter.next()) { michael@0: clip.op(iter.rect(), SkRegion::kUnion_Op); michael@0: } michael@0: this->swap(clip); michael@0: return !this->isEmpty(); michael@0: #else michael@0: const SkIRect& bounds = rgn.getBounds(); michael@0: const int offsetX = bounds.fLeft; michael@0: const int offsetY = bounds.fTop; michael@0: michael@0: SkTDArray yArray; michael@0: SkTDArray xArray; michael@0: michael@0: yArray.setReserve(SkMin32(bounds.height(), 1024)); michael@0: xArray.setReserve(SkMin32(bounds.width() * 128, 64 * 1024)); michael@0: michael@0: SkRegion::Iterator iter(rgn); michael@0: int prevRight = 0; michael@0: int prevBot = 0; michael@0: YOffset* currY = NULL; michael@0: michael@0: for (; !iter.done(); iter.next()) { michael@0: const SkIRect& r = iter.rect(); michael@0: SkASSERT(bounds.contains(r)); michael@0: michael@0: int bot = r.fBottom - offsetY; michael@0: SkASSERT(bot >= prevBot); michael@0: if (bot > prevBot) { michael@0: if (currY) { michael@0: // flush current row michael@0: append_run(xArray, 0, bounds.width() - prevRight); michael@0: } michael@0: // did we introduce an empty-gap from the prev row? michael@0: int top = r.fTop - offsetY; michael@0: if (top > prevBot) { michael@0: currY = yArray.append(); michael@0: currY->fY = top - 1; michael@0: currY->fOffset = xArray.count(); michael@0: append_run(xArray, 0, bounds.width()); michael@0: } michael@0: // create a new record for this Y value michael@0: currY = yArray.append(); michael@0: currY->fY = bot - 1; michael@0: currY->fOffset = xArray.count(); michael@0: prevRight = 0; michael@0: prevBot = bot; michael@0: } michael@0: michael@0: int x = r.fLeft - offsetX; michael@0: append_run(xArray, 0, x - prevRight); michael@0: michael@0: int w = r.fRight - r.fLeft; michael@0: append_run(xArray, 0xFF, w); michael@0: prevRight = x + w; michael@0: SkASSERT(prevRight <= bounds.width()); michael@0: } michael@0: // flush last row michael@0: append_run(xArray, 0, bounds.width() - prevRight); michael@0: michael@0: // now pack everything into a RunHead michael@0: RunHead* head = RunHead::Alloc(yArray.count(), xArray.bytes()); michael@0: memcpy(head->yoffsets(), yArray.begin(), yArray.bytes()); michael@0: memcpy(head->data(), xArray.begin(), xArray.bytes()); michael@0: michael@0: this->setEmpty(); michael@0: fBounds = bounds; michael@0: fRunHead = head; michael@0: this->validate(); michael@0: return true; michael@0: #endif michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: const uint8_t* SkAAClip::findRow(int y, int* lastYForRow) const { michael@0: SkASSERT(fRunHead); michael@0: michael@0: if (!y_in_rect(y, fBounds)) { michael@0: return NULL; michael@0: } michael@0: y -= fBounds.y(); // our yoffs values are relative to the top michael@0: michael@0: const YOffset* yoff = fRunHead->yoffsets(); michael@0: while (yoff->fY < y) { michael@0: yoff += 1; michael@0: SkASSERT(yoff - fRunHead->yoffsets() < fRunHead->fRowCount); michael@0: } michael@0: michael@0: if (lastYForRow) { michael@0: *lastYForRow = fBounds.y() + yoff->fY; michael@0: } michael@0: return fRunHead->data() + yoff->fOffset; michael@0: } michael@0: michael@0: const uint8_t* SkAAClip::findX(const uint8_t data[], int x, int* initialCount) const { michael@0: SkASSERT(x_in_rect(x, fBounds)); michael@0: x -= fBounds.x(); michael@0: michael@0: // first skip up to X michael@0: for (;;) { michael@0: int n = data[0]; michael@0: if (x < n) { michael@0: if (initialCount) { michael@0: *initialCount = n - x; michael@0: } michael@0: break; michael@0: } michael@0: data += 2; michael@0: x -= n; michael@0: } michael@0: return data; michael@0: } michael@0: michael@0: bool SkAAClip::quickContains(int left, int top, int right, int bottom) const { michael@0: if (this->isEmpty()) { michael@0: return false; michael@0: } michael@0: if (!fBounds.contains(left, top, right, bottom)) { michael@0: return false; michael@0: } michael@0: #if 0 michael@0: if (this->isRect()) { michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: int lastY SK_INIT_TO_AVOID_WARNING; michael@0: const uint8_t* row = this->findRow(top, &lastY); michael@0: if (lastY < bottom) { michael@0: return false; michael@0: } michael@0: // now just need to check in X michael@0: int count; michael@0: row = this->findX(row, left, &count); michael@0: #if 0 michael@0: return count >= (right - left) && 0xFF == row[1]; michael@0: #else michael@0: int rectWidth = right - left; michael@0: while (0xFF == row[1]) { michael@0: if (count >= rectWidth) { michael@0: return true; michael@0: } michael@0: rectWidth -= count; michael@0: row += 2; michael@0: count = row[0]; michael@0: } michael@0: return false; michael@0: #endif michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class SkAAClip::Builder { michael@0: SkIRect fBounds; michael@0: struct Row { michael@0: int fY; michael@0: int fWidth; michael@0: SkTDArray* fData; michael@0: }; michael@0: SkTDArray fRows; michael@0: Row* fCurrRow; michael@0: int fPrevY; michael@0: int fWidth; michael@0: int fMinY; michael@0: michael@0: public: michael@0: Builder(const SkIRect& bounds) : fBounds(bounds) { michael@0: fPrevY = -1; michael@0: fWidth = bounds.width(); michael@0: fCurrRow = NULL; michael@0: fMinY = bounds.fTop; michael@0: } michael@0: michael@0: ~Builder() { michael@0: Row* row = fRows.begin(); michael@0: Row* stop = fRows.end(); michael@0: while (row < stop) { michael@0: delete row->fData; michael@0: row += 1; michael@0: } michael@0: } michael@0: michael@0: const SkIRect& getBounds() const { return fBounds; } michael@0: michael@0: void addRun(int x, int y, U8CPU alpha, int count) { michael@0: SkASSERT(count > 0); michael@0: SkASSERT(fBounds.contains(x, y)); michael@0: SkASSERT(fBounds.contains(x + count - 1, y)); michael@0: michael@0: x -= fBounds.left(); michael@0: y -= fBounds.top(); michael@0: michael@0: Row* row = fCurrRow; michael@0: if (y != fPrevY) { michael@0: SkASSERT(y > fPrevY); michael@0: fPrevY = y; michael@0: row = this->flushRow(true); michael@0: row->fY = y; michael@0: row->fWidth = 0; michael@0: SkASSERT(row->fData); michael@0: SkASSERT(0 == row->fData->count()); michael@0: fCurrRow = row; michael@0: } michael@0: michael@0: SkASSERT(row->fWidth <= x); michael@0: SkASSERT(row->fWidth < fBounds.width()); michael@0: michael@0: SkTDArray& data = *row->fData; michael@0: michael@0: int gap = x - row->fWidth; michael@0: if (gap) { michael@0: AppendRun(data, 0, gap); michael@0: row->fWidth += gap; michael@0: SkASSERT(row->fWidth < fBounds.width()); michael@0: } michael@0: michael@0: AppendRun(data, alpha, count); michael@0: row->fWidth += count; michael@0: SkASSERT(row->fWidth <= fBounds.width()); michael@0: } michael@0: michael@0: void addColumn(int x, int y, U8CPU alpha, int height) { michael@0: SkASSERT(fBounds.contains(x, y + height - 1)); michael@0: michael@0: this->addRun(x, y, alpha, 1); michael@0: this->flushRowH(fCurrRow); michael@0: y -= fBounds.fTop; michael@0: SkASSERT(y == fCurrRow->fY); michael@0: fCurrRow->fY = y + height - 1; michael@0: } michael@0: michael@0: void addRectRun(int x, int y, int width, int height) { michael@0: SkASSERT(fBounds.contains(x + width - 1, y + height - 1)); michael@0: this->addRun(x, y, 0xFF, width); michael@0: michael@0: // we assum the rect must be all we'll see for these scanlines michael@0: // so we ensure our row goes all the way to our right michael@0: this->flushRowH(fCurrRow); michael@0: michael@0: y -= fBounds.fTop; michael@0: SkASSERT(y == fCurrRow->fY); michael@0: fCurrRow->fY = y + height - 1; michael@0: } michael@0: michael@0: void addAntiRectRun(int x, int y, int width, int height, michael@0: SkAlpha leftAlpha, SkAlpha rightAlpha) { michael@0: SkASSERT(fBounds.contains(x + width - 1 + michael@0: (leftAlpha > 0 ? 1 : 0) + (rightAlpha > 0 ? 1 : 0), michael@0: y + height - 1)); michael@0: SkASSERT(width >= 0); michael@0: michael@0: // Conceptually we're always adding 3 runs, but we should michael@0: // merge or omit them if possible. michael@0: if (leftAlpha == 0xFF) { michael@0: width++; michael@0: } else if (leftAlpha > 0) { michael@0: this->addRun(x++, y, leftAlpha, 1); michael@0: } michael@0: if (rightAlpha == 0xFF) { michael@0: width++; michael@0: } michael@0: if (width > 0) { michael@0: this->addRun(x, y, 0xFF, width); michael@0: } michael@0: if (rightAlpha > 0 && rightAlpha < 255) { michael@0: this->addRun(x + width, y, rightAlpha, 1); michael@0: } michael@0: michael@0: // we assume the rect must be all we'll see for these scanlines michael@0: // so we ensure our row goes all the way to our right michael@0: this->flushRowH(fCurrRow); michael@0: michael@0: y -= fBounds.fTop; michael@0: SkASSERT(y == fCurrRow->fY); michael@0: fCurrRow->fY = y + height - 1; michael@0: } michael@0: michael@0: bool finish(SkAAClip* target) { michael@0: this->flushRow(false); michael@0: michael@0: const Row* row = fRows.begin(); michael@0: const Row* stop = fRows.end(); michael@0: michael@0: size_t dataSize = 0; michael@0: while (row < stop) { michael@0: dataSize += row->fData->count(); michael@0: row += 1; michael@0: } michael@0: michael@0: if (0 == dataSize) { michael@0: return target->setEmpty(); michael@0: } michael@0: michael@0: SkASSERT(fMinY >= fBounds.fTop); michael@0: SkASSERT(fMinY < fBounds.fBottom); michael@0: int adjustY = fMinY - fBounds.fTop; michael@0: fBounds.fTop = fMinY; michael@0: michael@0: RunHead* head = RunHead::Alloc(fRows.count(), dataSize); michael@0: YOffset* yoffset = head->yoffsets(); michael@0: uint8_t* data = head->data(); michael@0: uint8_t* baseData = data; michael@0: michael@0: row = fRows.begin(); michael@0: SkDEBUGCODE(int prevY = row->fY - 1;) michael@0: while (row < stop) { michael@0: SkASSERT(prevY < row->fY); // must be monotonic michael@0: SkDEBUGCODE(prevY = row->fY); michael@0: michael@0: yoffset->fY = row->fY - adjustY; michael@0: yoffset->fOffset = SkToU32(data - baseData); michael@0: yoffset += 1; michael@0: michael@0: size_t n = row->fData->count(); michael@0: memcpy(data, row->fData->begin(), n); michael@0: #ifdef SK_DEBUG michael@0: size_t bytesNeeded = compute_row_length(data, fBounds.width()); michael@0: SkASSERT(bytesNeeded == n); michael@0: #endif michael@0: data += n; michael@0: michael@0: row += 1; michael@0: } michael@0: michael@0: target->freeRuns(); michael@0: target->fBounds = fBounds; michael@0: target->fRunHead = head; michael@0: return target->trimBounds(); michael@0: } michael@0: michael@0: void dump() { michael@0: this->validate(); michael@0: int y; michael@0: for (y = 0; y < fRows.count(); ++y) { michael@0: const Row& row = fRows[y]; michael@0: SkDebugf("Y:%3d W:%3d", row.fY, row.fWidth); michael@0: const SkTDArray& data = *row.fData; michael@0: int count = data.count(); michael@0: SkASSERT(!(count & 1)); michael@0: const uint8_t* ptr = data.begin(); michael@0: for (int x = 0; x < count; x += 2) { michael@0: SkDebugf(" [%3d:%02X]", ptr[0], ptr[1]); michael@0: ptr += 2; michael@0: } michael@0: SkDebugf("\n"); michael@0: } michael@0: } michael@0: michael@0: void validate() { michael@0: #ifdef SK_DEBUG michael@0: if (false) { // avoid bit rot, suppress warning michael@0: test_count_left_right_zeros(); michael@0: } michael@0: int prevY = -1; michael@0: for (int i = 0; i < fRows.count(); ++i) { michael@0: const Row& row = fRows[i]; michael@0: SkASSERT(prevY < row.fY); michael@0: SkASSERT(fWidth == row.fWidth); michael@0: int count = row.fData->count(); michael@0: const uint8_t* ptr = row.fData->begin(); michael@0: SkASSERT(!(count & 1)); michael@0: int w = 0; michael@0: for (int x = 0; x < count; x += 2) { michael@0: int n = ptr[0]; michael@0: SkASSERT(n > 0); michael@0: w += n; michael@0: SkASSERT(w <= fWidth); michael@0: ptr += 2; michael@0: } michael@0: SkASSERT(w == fWidth); michael@0: prevY = row.fY; michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: // only called by BuilderBlitter michael@0: void setMinY(int y) { michael@0: fMinY = y; michael@0: } michael@0: michael@0: private: michael@0: void flushRowH(Row* row) { michael@0: // flush current row if needed michael@0: if (row->fWidth < fWidth) { michael@0: AppendRun(*row->fData, 0, fWidth - row->fWidth); michael@0: row->fWidth = fWidth; michael@0: } michael@0: } michael@0: michael@0: Row* flushRow(bool readyForAnother) { michael@0: Row* next = NULL; michael@0: int count = fRows.count(); michael@0: if (count > 0) { michael@0: this->flushRowH(&fRows[count - 1]); michael@0: } michael@0: if (count > 1) { michael@0: // are our last two runs the same? michael@0: Row* prev = &fRows[count - 2]; michael@0: Row* curr = &fRows[count - 1]; michael@0: SkASSERT(prev->fWidth == fWidth); michael@0: SkASSERT(curr->fWidth == fWidth); michael@0: if (*prev->fData == *curr->fData) { michael@0: prev->fY = curr->fY; michael@0: if (readyForAnother) { michael@0: curr->fData->rewind(); michael@0: next = curr; michael@0: } else { michael@0: delete curr->fData; michael@0: fRows.removeShuffle(count - 1); michael@0: } michael@0: } else { michael@0: if (readyForAnother) { michael@0: next = fRows.append(); michael@0: next->fData = new SkTDArray; michael@0: } michael@0: } michael@0: } else { michael@0: if (readyForAnother) { michael@0: next = fRows.append(); michael@0: next->fData = new SkTDArray; michael@0: } michael@0: } michael@0: return next; michael@0: } michael@0: michael@0: static void AppendRun(SkTDArray& data, U8CPU alpha, int count) { michael@0: do { michael@0: int n = count; michael@0: if (n > 255) { michael@0: n = 255; michael@0: } michael@0: uint8_t* ptr = data.append(2); michael@0: ptr[0] = n; michael@0: ptr[1] = alpha; michael@0: count -= n; michael@0: } while (count > 0); michael@0: } michael@0: }; michael@0: michael@0: class SkAAClip::BuilderBlitter : public SkBlitter { michael@0: int fLastY; michael@0: michael@0: /* michael@0: If we see a gap of 1 or more empty scanlines while building in Y-order, michael@0: we inject an explicit empty scanline (alpha==0) michael@0: michael@0: See AAClipTest.cpp : test_path_with_hole() michael@0: */ michael@0: void checkForYGap(int y) { michael@0: SkASSERT(y >= fLastY); michael@0: if (fLastY > -SK_MaxS32) { michael@0: int gap = y - fLastY; michael@0: if (gap > 1) { michael@0: fBuilder->addRun(fLeft, y - 1, 0, fRight - fLeft); michael@0: } michael@0: } michael@0: fLastY = y; michael@0: } michael@0: michael@0: public: michael@0: michael@0: BuilderBlitter(Builder* builder) { michael@0: fBuilder = builder; michael@0: fLeft = builder->getBounds().fLeft; michael@0: fRight = builder->getBounds().fRight; michael@0: fMinY = SK_MaxS32; michael@0: fLastY = -SK_MaxS32; // sentinel michael@0: } michael@0: michael@0: void finish() { michael@0: if (fMinY < SK_MaxS32) { michael@0: fBuilder->setMinY(fMinY); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: Must evaluate clips in scan-line order, so don't want to allow blitV(), michael@0: but an AAClip can be clipped down to a single pixel wide, so we michael@0: must support it (given AntiRect semantics: minimum width is 2). michael@0: Instead we'll rely on the runtime asserts to guarantee Y monotonicity; michael@0: any failure cases that misses may have minor artifacts. michael@0: */ michael@0: virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE { michael@0: this->recordMinY(y); michael@0: fBuilder->addColumn(x, y, alpha, height); michael@0: fLastY = y + height - 1; michael@0: } michael@0: michael@0: virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE { michael@0: this->recordMinY(y); michael@0: this->checkForYGap(y); michael@0: fBuilder->addRectRun(x, y, width, height); michael@0: fLastY = y + height - 1; michael@0: } michael@0: michael@0: virtual void blitAntiRect(int x, int y, int width, int height, michael@0: SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVERRIDE { michael@0: this->recordMinY(y); michael@0: this->checkForYGap(y); michael@0: fBuilder->addAntiRectRun(x, y, width, height, leftAlpha, rightAlpha); michael@0: fLastY = y + height - 1; michael@0: } michael@0: michael@0: virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE michael@0: { unexpected(); } michael@0: michael@0: virtual const SkBitmap* justAnOpaqueColor(uint32_t*) SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: michael@0: virtual void blitH(int x, int y, int width) SK_OVERRIDE { michael@0: this->recordMinY(y); michael@0: this->checkForYGap(y); michael@0: fBuilder->addRun(x, y, 0xFF, width); michael@0: } michael@0: michael@0: virtual void blitAntiH(int x, int y, const SkAlpha alpha[], michael@0: const int16_t runs[]) SK_OVERRIDE { michael@0: this->recordMinY(y); michael@0: this->checkForYGap(y); michael@0: for (;;) { michael@0: int count = *runs; michael@0: if (count <= 0) { michael@0: return; michael@0: } michael@0: michael@0: // The supersampler's buffer can be the width of the device, so michael@0: // we may have to trim the run to our bounds. If so, we assert that michael@0: // the extra spans are always alpha==0 michael@0: int localX = x; michael@0: int localCount = count; michael@0: if (x < fLeft) { michael@0: SkASSERT(0 == *alpha); michael@0: int gap = fLeft - x; michael@0: SkASSERT(gap <= count); michael@0: localX += gap; michael@0: localCount -= gap; michael@0: } michael@0: int right = x + count; michael@0: if (right > fRight) { michael@0: SkASSERT(0 == *alpha); michael@0: localCount -= right - fRight; michael@0: SkASSERT(localCount >= 0); michael@0: } michael@0: michael@0: if (localCount) { michael@0: fBuilder->addRun(localX, y, *alpha, localCount); michael@0: } michael@0: // Next run michael@0: runs += count; michael@0: alpha += count; michael@0: x += count; michael@0: } michael@0: } michael@0: michael@0: private: michael@0: Builder* fBuilder; michael@0: int fLeft; // cache of builder's bounds' left edge michael@0: int fRight; michael@0: int fMinY; michael@0: michael@0: /* michael@0: * We track this, in case the scan converter skipped some number of michael@0: * scanlines at the (relative to the bounds it was given). This allows michael@0: * the builder, during its finish, to trip its bounds down to the "real" michael@0: * top. michael@0: */ michael@0: void recordMinY(int y) { michael@0: if (y < fMinY) { michael@0: fMinY = y; michael@0: } michael@0: } michael@0: michael@0: void unexpected() { michael@0: SkDebugf("---- did not expect to get called here"); michael@0: sk_throw(); michael@0: } michael@0: }; michael@0: michael@0: bool SkAAClip::setPath(const SkPath& path, const SkRegion* clip, bool doAA) { michael@0: AUTO_AACLIP_VALIDATE(*this); michael@0: michael@0: if (clip && clip->isEmpty()) { michael@0: return this->setEmpty(); michael@0: } michael@0: michael@0: SkIRect ibounds; michael@0: path.getBounds().roundOut(&ibounds); michael@0: michael@0: SkRegion tmpClip; michael@0: if (NULL == clip) { michael@0: tmpClip.setRect(ibounds); michael@0: clip = &tmpClip; michael@0: } michael@0: michael@0: if (path.isInverseFillType()) { michael@0: ibounds = clip->getBounds(); michael@0: } else { michael@0: if (ibounds.isEmpty() || !ibounds.intersect(clip->getBounds())) { michael@0: return this->setEmpty(); michael@0: } michael@0: } michael@0: michael@0: Builder builder(ibounds); michael@0: BuilderBlitter blitter(&builder); michael@0: michael@0: if (doAA) { michael@0: SkScan::AntiFillPath(path, *clip, &blitter, true); michael@0: } else { michael@0: SkScan::FillPath(path, *clip, &blitter); michael@0: } michael@0: michael@0: blitter.finish(); michael@0: return builder.finish(this); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: typedef void (*RowProc)(SkAAClip::Builder&, int bottom, michael@0: const uint8_t* rowA, const SkIRect& rectA, michael@0: const uint8_t* rowB, const SkIRect& rectB); michael@0: michael@0: typedef U8CPU (*AlphaProc)(U8CPU alphaA, U8CPU alphaB); michael@0: michael@0: static U8CPU sectAlphaProc(U8CPU alphaA, U8CPU alphaB) { michael@0: // Multiply michael@0: return SkMulDiv255Round(alphaA, alphaB); michael@0: } michael@0: michael@0: static U8CPU unionAlphaProc(U8CPU alphaA, U8CPU alphaB) { michael@0: // SrcOver michael@0: return alphaA + alphaB - SkMulDiv255Round(alphaA, alphaB); michael@0: } michael@0: michael@0: static U8CPU diffAlphaProc(U8CPU alphaA, U8CPU alphaB) { michael@0: // SrcOut michael@0: return SkMulDiv255Round(alphaA, 0xFF - alphaB); michael@0: } michael@0: michael@0: static U8CPU xorAlphaProc(U8CPU alphaA, U8CPU alphaB) { michael@0: // XOR michael@0: return alphaA + alphaB - 2 * SkMulDiv255Round(alphaA, alphaB); michael@0: } michael@0: michael@0: static AlphaProc find_alpha_proc(SkRegion::Op op) { michael@0: switch (op) { michael@0: case SkRegion::kIntersect_Op: michael@0: return sectAlphaProc; michael@0: case SkRegion::kDifference_Op: michael@0: return diffAlphaProc; michael@0: case SkRegion::kUnion_Op: michael@0: return unionAlphaProc; michael@0: case SkRegion::kXOR_Op: michael@0: return xorAlphaProc; michael@0: default: michael@0: SkDEBUGFAIL("unexpected region op"); michael@0: return sectAlphaProc; michael@0: } michael@0: } michael@0: michael@0: class RowIter { michael@0: public: michael@0: RowIter(const uint8_t* row, const SkIRect& bounds) { michael@0: fRow = row; michael@0: fLeft = bounds.fLeft; michael@0: fBoundsRight = bounds.fRight; michael@0: if (row) { michael@0: fRight = bounds.fLeft + row[0]; michael@0: SkASSERT(fRight <= fBoundsRight); michael@0: fAlpha = row[1]; michael@0: fDone = false; michael@0: } else { michael@0: fDone = true; michael@0: fRight = kMaxInt32; michael@0: fAlpha = 0; michael@0: } michael@0: } michael@0: michael@0: bool done() const { return fDone; } michael@0: int left() const { return fLeft; } michael@0: int right() const { return fRight; } michael@0: U8CPU alpha() const { return fAlpha; } michael@0: void next() { michael@0: if (!fDone) { michael@0: fLeft = fRight; michael@0: if (fRight == fBoundsRight) { michael@0: fDone = true; michael@0: fRight = kMaxInt32; michael@0: fAlpha = 0; michael@0: } else { michael@0: fRow += 2; michael@0: fRight += fRow[0]; michael@0: fAlpha = fRow[1]; michael@0: SkASSERT(fRight <= fBoundsRight); michael@0: } michael@0: } michael@0: } michael@0: michael@0: private: michael@0: const uint8_t* fRow; michael@0: int fLeft; michael@0: int fRight; michael@0: int fBoundsRight; michael@0: bool fDone; michael@0: uint8_t fAlpha; michael@0: }; michael@0: michael@0: static void adjust_row(RowIter& iter, int& leftA, int& riteA, int rite) { michael@0: if (rite == riteA) { michael@0: iter.next(); michael@0: leftA = iter.left(); michael@0: riteA = iter.right(); michael@0: } michael@0: } michael@0: michael@0: #if 0 // UNUSED michael@0: static bool intersect(int& min, int& max, int boundsMin, int boundsMax) { michael@0: SkASSERT(min < max); michael@0: SkASSERT(boundsMin < boundsMax); michael@0: if (min >= boundsMax || max <= boundsMin) { michael@0: return false; michael@0: } michael@0: if (min < boundsMin) { michael@0: min = boundsMin; michael@0: } michael@0: if (max > boundsMax) { michael@0: max = boundsMax; michael@0: } michael@0: return true; michael@0: } michael@0: #endif michael@0: michael@0: static void operatorX(SkAAClip::Builder& builder, int lastY, michael@0: RowIter& iterA, RowIter& iterB, michael@0: AlphaProc proc, const SkIRect& bounds) { michael@0: int leftA = iterA.left(); michael@0: int riteA = iterA.right(); michael@0: int leftB = iterB.left(); michael@0: int riteB = iterB.right(); michael@0: michael@0: int prevRite = bounds.fLeft; michael@0: michael@0: do { michael@0: U8CPU alphaA = 0; michael@0: U8CPU alphaB = 0; michael@0: int left, rite; michael@0: michael@0: if (leftA < leftB) { michael@0: left = leftA; michael@0: alphaA = iterA.alpha(); michael@0: if (riteA <= leftB) { michael@0: rite = riteA; michael@0: } else { michael@0: rite = leftA = leftB; michael@0: } michael@0: } else if (leftB < leftA) { michael@0: left = leftB; michael@0: alphaB = iterB.alpha(); michael@0: if (riteB <= leftA) { michael@0: rite = riteB; michael@0: } else { michael@0: rite = leftB = leftA; michael@0: } michael@0: } else { michael@0: left = leftA; // or leftB, since leftA == leftB michael@0: rite = leftA = leftB = SkMin32(riteA, riteB); michael@0: alphaA = iterA.alpha(); michael@0: alphaB = iterB.alpha(); michael@0: } michael@0: michael@0: if (left >= bounds.fRight) { michael@0: break; michael@0: } michael@0: if (rite > bounds.fRight) { michael@0: rite = bounds.fRight; michael@0: } michael@0: michael@0: if (left >= bounds.fLeft) { michael@0: SkASSERT(rite > left); michael@0: builder.addRun(left, lastY, proc(alphaA, alphaB), rite - left); michael@0: prevRite = rite; michael@0: } michael@0: michael@0: adjust_row(iterA, leftA, riteA, rite); michael@0: adjust_row(iterB, leftB, riteB, rite); michael@0: } while (!iterA.done() || !iterB.done()); michael@0: michael@0: if (prevRite < bounds.fRight) { michael@0: builder.addRun(prevRite, lastY, 0, bounds.fRight - prevRite); michael@0: } michael@0: } michael@0: michael@0: static void adjust_iter(SkAAClip::Iter& iter, int& topA, int& botA, int bot) { michael@0: if (bot == botA) { michael@0: iter.next(); michael@0: topA = botA; michael@0: SkASSERT(botA == iter.top()); michael@0: botA = iter.bottom(); michael@0: } michael@0: } michael@0: michael@0: static void operateY(SkAAClip::Builder& builder, const SkAAClip& A, michael@0: const SkAAClip& B, SkRegion::Op op) { michael@0: AlphaProc proc = find_alpha_proc(op); michael@0: const SkIRect& bounds = builder.getBounds(); michael@0: michael@0: SkAAClip::Iter iterA(A); michael@0: SkAAClip::Iter iterB(B); michael@0: michael@0: SkASSERT(!iterA.done()); michael@0: int topA = iterA.top(); michael@0: int botA = iterA.bottom(); michael@0: SkASSERT(!iterB.done()); michael@0: int topB = iterB.top(); michael@0: int botB = iterB.bottom(); michael@0: michael@0: do { michael@0: const uint8_t* rowA = NULL; michael@0: const uint8_t* rowB = NULL; michael@0: int top, bot; michael@0: michael@0: if (topA < topB) { michael@0: top = topA; michael@0: rowA = iterA.data(); michael@0: if (botA <= topB) { michael@0: bot = botA; michael@0: } else { michael@0: bot = topA = topB; michael@0: } michael@0: michael@0: } else if (topB < topA) { michael@0: top = topB; michael@0: rowB = iterB.data(); michael@0: if (botB <= topA) { michael@0: bot = botB; michael@0: } else { michael@0: bot = topB = topA; michael@0: } michael@0: } else { michael@0: top = topA; // or topB, since topA == topB michael@0: bot = topA = topB = SkMin32(botA, botB); michael@0: rowA = iterA.data(); michael@0: rowB = iterB.data(); michael@0: } michael@0: michael@0: if (top >= bounds.fBottom) { michael@0: break; michael@0: } michael@0: michael@0: if (bot > bounds.fBottom) { michael@0: bot = bounds.fBottom; michael@0: } michael@0: SkASSERT(top < bot); michael@0: michael@0: if (!rowA && !rowB) { michael@0: builder.addRun(bounds.fLeft, bot - 1, 0, bounds.width()); michael@0: } else if (top >= bounds.fTop) { michael@0: SkASSERT(bot <= bounds.fBottom); michael@0: RowIter rowIterA(rowA, rowA ? A.getBounds() : bounds); michael@0: RowIter rowIterB(rowB, rowB ? B.getBounds() : bounds); michael@0: operatorX(builder, bot - 1, rowIterA, rowIterB, proc, bounds); michael@0: } michael@0: michael@0: adjust_iter(iterA, topA, botA, bot); michael@0: adjust_iter(iterB, topB, botB, bot); michael@0: } while (!iterA.done() || !iterB.done()); michael@0: } michael@0: michael@0: bool SkAAClip::op(const SkAAClip& clipAOrig, const SkAAClip& clipBOrig, michael@0: SkRegion::Op op) { michael@0: AUTO_AACLIP_VALIDATE(*this); michael@0: michael@0: if (SkRegion::kReplace_Op == op) { michael@0: return this->set(clipBOrig); michael@0: } michael@0: michael@0: const SkAAClip* clipA = &clipAOrig; michael@0: const SkAAClip* clipB = &clipBOrig; michael@0: michael@0: if (SkRegion::kReverseDifference_Op == op) { michael@0: SkTSwap(clipA, clipB); michael@0: op = SkRegion::kDifference_Op; michael@0: } michael@0: michael@0: bool a_empty = clipA->isEmpty(); michael@0: bool b_empty = clipB->isEmpty(); michael@0: michael@0: SkIRect bounds; michael@0: switch (op) { michael@0: case SkRegion::kDifference_Op: michael@0: if (a_empty) { michael@0: return this->setEmpty(); michael@0: } michael@0: if (b_empty || !SkIRect::Intersects(clipA->fBounds, clipB->fBounds)) { michael@0: return this->set(*clipA); michael@0: } michael@0: bounds = clipA->fBounds; michael@0: break; michael@0: michael@0: case SkRegion::kIntersect_Op: michael@0: if ((a_empty | b_empty) || !bounds.intersect(clipA->fBounds, michael@0: clipB->fBounds)) { michael@0: return this->setEmpty(); michael@0: } michael@0: break; michael@0: michael@0: case SkRegion::kUnion_Op: michael@0: case SkRegion::kXOR_Op: michael@0: if (a_empty) { michael@0: return this->set(*clipB); michael@0: } michael@0: if (b_empty) { michael@0: return this->set(*clipA); michael@0: } michael@0: bounds = clipA->fBounds; michael@0: bounds.join(clipB->fBounds); michael@0: break; michael@0: michael@0: default: michael@0: SkDEBUGFAIL("unknown region op"); michael@0: return !this->isEmpty(); michael@0: } michael@0: michael@0: SkASSERT(SkIRect::Intersects(bounds, clipB->fBounds)); michael@0: SkASSERT(SkIRect::Intersects(bounds, clipB->fBounds)); michael@0: michael@0: Builder builder(bounds); michael@0: operateY(builder, *clipA, *clipB, op); michael@0: michael@0: return builder.finish(this); michael@0: } michael@0: michael@0: /* michael@0: * It can be expensive to build a local aaclip before applying the op, so michael@0: * we first see if we can restrict the bounds of new rect to our current michael@0: * bounds, or note that the new rect subsumes our current clip. michael@0: */ michael@0: michael@0: bool SkAAClip::op(const SkIRect& rOrig, SkRegion::Op op) { michael@0: SkIRect rStorage; michael@0: const SkIRect* r = &rOrig; michael@0: michael@0: switch (op) { michael@0: case SkRegion::kIntersect_Op: michael@0: if (!rStorage.intersect(rOrig, fBounds)) { michael@0: // no overlap, so we're empty michael@0: return this->setEmpty(); michael@0: } michael@0: if (rStorage == fBounds) { michael@0: // we were wholly inside the rect, no change michael@0: return !this->isEmpty(); michael@0: } michael@0: if (this->quickContains(rStorage)) { michael@0: // the intersection is wholly inside us, we're a rect michael@0: return this->setRect(rStorage); michael@0: } michael@0: r = &rStorage; // use the intersected bounds michael@0: break; michael@0: case SkRegion::kDifference_Op: michael@0: break; michael@0: case SkRegion::kUnion_Op: michael@0: if (rOrig.contains(fBounds)) { michael@0: return this->setRect(rOrig); michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: SkAAClip clip; michael@0: clip.setRect(*r); michael@0: return this->op(*this, clip, op); michael@0: } michael@0: michael@0: bool SkAAClip::op(const SkRect& rOrig, SkRegion::Op op, bool doAA) { michael@0: SkRect rStorage, boundsStorage; michael@0: const SkRect* r = &rOrig; michael@0: michael@0: boundsStorage.set(fBounds); michael@0: switch (op) { michael@0: case SkRegion::kIntersect_Op: michael@0: case SkRegion::kDifference_Op: michael@0: if (!rStorage.intersect(rOrig, boundsStorage)) { michael@0: if (SkRegion::kIntersect_Op == op) { michael@0: return this->setEmpty(); michael@0: } else { // kDifference michael@0: return !this->isEmpty(); michael@0: } michael@0: } michael@0: r = &rStorage; // use the intersected bounds michael@0: break; michael@0: case SkRegion::kUnion_Op: michael@0: if (rOrig.contains(boundsStorage)) { michael@0: return this->setRect(rOrig); michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: SkAAClip clip; michael@0: clip.setRect(*r, doAA); michael@0: return this->op(*this, clip, op); michael@0: } michael@0: michael@0: bool SkAAClip::op(const SkAAClip& clip, SkRegion::Op op) { michael@0: return this->op(*this, clip, op); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkAAClip::translate(int dx, int dy, SkAAClip* dst) const { michael@0: if (NULL == dst) { michael@0: return !this->isEmpty(); michael@0: } michael@0: michael@0: if (this->isEmpty()) { michael@0: return dst->setEmpty(); michael@0: } michael@0: michael@0: if (this != dst) { michael@0: sk_atomic_inc(&fRunHead->fRefCnt); michael@0: dst->freeRuns(); michael@0: dst->fRunHead = fRunHead; michael@0: dst->fBounds = fBounds; michael@0: } michael@0: dst->fBounds.offset(dx, dy); michael@0: return true; michael@0: } michael@0: michael@0: static void expand_row_to_mask(uint8_t* SK_RESTRICT mask, michael@0: const uint8_t* SK_RESTRICT row, michael@0: int width) { michael@0: while (width > 0) { michael@0: int n = row[0]; michael@0: SkASSERT(width >= n); michael@0: memset(mask, row[1], n); michael@0: mask += n; michael@0: row += 2; michael@0: width -= n; michael@0: } michael@0: SkASSERT(0 == width); michael@0: } michael@0: michael@0: void SkAAClip::copyToMask(SkMask* mask) const { michael@0: mask->fFormat = SkMask::kA8_Format; michael@0: if (this->isEmpty()) { michael@0: mask->fBounds.setEmpty(); michael@0: mask->fImage = NULL; michael@0: mask->fRowBytes = 0; michael@0: return; michael@0: } michael@0: michael@0: mask->fBounds = fBounds; michael@0: mask->fRowBytes = fBounds.width(); michael@0: size_t size = mask->computeImageSize(); michael@0: mask->fImage = SkMask::AllocImage(size); michael@0: michael@0: Iter iter(*this); michael@0: uint8_t* dst = mask->fImage; michael@0: const int width = fBounds.width(); michael@0: michael@0: int y = fBounds.fTop; michael@0: while (!iter.done()) { michael@0: do { michael@0: expand_row_to_mask(dst, iter.data(), width); michael@0: dst += mask->fRowBytes; michael@0: } while (++y < iter.bottom()); michael@0: iter.next(); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static void expandToRuns(const uint8_t* SK_RESTRICT data, int initialCount, int width, michael@0: int16_t* SK_RESTRICT runs, SkAlpha* SK_RESTRICT aa) { michael@0: // we don't read our initial n from data, since the caller may have had to michael@0: // clip it, hence the initialCount parameter. michael@0: int n = initialCount; michael@0: for (;;) { michael@0: if (n > width) { michael@0: n = width; michael@0: } michael@0: SkASSERT(n > 0); michael@0: runs[0] = n; michael@0: runs += n; michael@0: michael@0: aa[0] = data[1]; michael@0: aa += n; michael@0: michael@0: data += 2; michael@0: width -= n; michael@0: if (0 == width) { michael@0: break; michael@0: } michael@0: // load the next count michael@0: n = data[0]; michael@0: } michael@0: runs[0] = 0; // sentinel michael@0: } michael@0: michael@0: SkAAClipBlitter::~SkAAClipBlitter() { michael@0: sk_free(fScanlineScratch); michael@0: } michael@0: michael@0: void SkAAClipBlitter::ensureRunsAndAA() { michael@0: if (NULL == fScanlineScratch) { michael@0: // add 1 so we can store the terminating run count of 0 michael@0: int count = fAAClipBounds.width() + 1; michael@0: // we use this either for fRuns + fAA, or a scaline of a mask michael@0: // which may be as deep as 32bits michael@0: fScanlineScratch = sk_malloc_throw(count * sizeof(SkPMColor)); michael@0: fRuns = (int16_t*)fScanlineScratch; michael@0: fAA = (SkAlpha*)(fRuns + count); michael@0: } michael@0: } michael@0: michael@0: void SkAAClipBlitter::blitH(int x, int y, int width) { michael@0: SkASSERT(width > 0); michael@0: SkASSERT(fAAClipBounds.contains(x, y)); michael@0: SkASSERT(fAAClipBounds.contains(x + width - 1, y)); michael@0: michael@0: const uint8_t* row = fAAClip->findRow(y); michael@0: int initialCount; michael@0: row = fAAClip->findX(row, x, &initialCount); michael@0: michael@0: if (initialCount >= width) { michael@0: SkAlpha alpha = row[1]; michael@0: if (0 == alpha) { michael@0: return; michael@0: } michael@0: if (0xFF == alpha) { michael@0: fBlitter->blitH(x, y, width); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: this->ensureRunsAndAA(); michael@0: expandToRuns(row, initialCount, width, fRuns, fAA); michael@0: michael@0: fBlitter->blitAntiH(x, y, fAA, fRuns); michael@0: } michael@0: michael@0: static void merge(const uint8_t* SK_RESTRICT row, int rowN, michael@0: const SkAlpha* SK_RESTRICT srcAA, michael@0: const int16_t* SK_RESTRICT srcRuns, michael@0: SkAlpha* SK_RESTRICT dstAA, michael@0: int16_t* SK_RESTRICT dstRuns, michael@0: int width) { michael@0: SkDEBUGCODE(int accumulated = 0;) michael@0: int srcN = srcRuns[0]; michael@0: // do we need this check? michael@0: if (0 == srcN) { michael@0: return; michael@0: } michael@0: michael@0: for (;;) { michael@0: SkASSERT(rowN > 0); michael@0: SkASSERT(srcN > 0); michael@0: michael@0: unsigned newAlpha = SkMulDiv255Round(srcAA[0], row[1]); michael@0: int minN = SkMin32(srcN, rowN); michael@0: dstRuns[0] = minN; michael@0: dstRuns += minN; michael@0: dstAA[0] = newAlpha; michael@0: dstAA += minN; michael@0: michael@0: if (0 == (srcN -= minN)) { michael@0: srcN = srcRuns[0]; // refresh michael@0: srcRuns += srcN; michael@0: srcAA += srcN; michael@0: srcN = srcRuns[0]; // reload michael@0: if (0 == srcN) { michael@0: break; michael@0: } michael@0: } michael@0: if (0 == (rowN -= minN)) { michael@0: row += 2; michael@0: rowN = row[0]; // reload michael@0: } michael@0: michael@0: SkDEBUGCODE(accumulated += minN;) michael@0: SkASSERT(accumulated <= width); michael@0: } michael@0: dstRuns[0] = 0; michael@0: } michael@0: michael@0: void SkAAClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[], michael@0: const int16_t runs[]) { michael@0: michael@0: const uint8_t* row = fAAClip->findRow(y); michael@0: int initialCount; michael@0: row = fAAClip->findX(row, x, &initialCount); michael@0: michael@0: this->ensureRunsAndAA(); michael@0: michael@0: merge(row, initialCount, aa, runs, fAA, fRuns, fAAClipBounds.width()); michael@0: fBlitter->blitAntiH(x, y, fAA, fRuns); michael@0: } michael@0: michael@0: void SkAAClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { michael@0: if (fAAClip->quickContains(x, y, x + 1, y + height)) { michael@0: fBlitter->blitV(x, y, height, alpha); michael@0: return; michael@0: } michael@0: michael@0: for (;;) { michael@0: int lastY SK_INIT_TO_AVOID_WARNING; michael@0: const uint8_t* row = fAAClip->findRow(y, &lastY); michael@0: int dy = lastY - y + 1; michael@0: if (dy > height) { michael@0: dy = height; michael@0: } michael@0: height -= dy; michael@0: michael@0: row = fAAClip->findX(row, x); michael@0: SkAlpha newAlpha = SkMulDiv255Round(alpha, row[1]); michael@0: if (newAlpha) { michael@0: fBlitter->blitV(x, y, dy, newAlpha); michael@0: } michael@0: SkASSERT(height >= 0); michael@0: if (height <= 0) { michael@0: break; michael@0: } michael@0: y = lastY + 1; michael@0: } michael@0: } michael@0: michael@0: void SkAAClipBlitter::blitRect(int x, int y, int width, int height) { michael@0: if (fAAClip->quickContains(x, y, x + width, y + height)) { michael@0: fBlitter->blitRect(x, y, width, height); michael@0: return; michael@0: } michael@0: michael@0: while (--height >= 0) { michael@0: this->blitH(x, y, width); michael@0: y += 1; michael@0: } michael@0: } michael@0: michael@0: typedef void (*MergeAAProc)(const void* src, int width, const uint8_t* row, michael@0: int initialRowCount, void* dst); michael@0: michael@0: static void small_memcpy(void* dst, const void* src, size_t n) { michael@0: memcpy(dst, src, n); michael@0: } michael@0: michael@0: static void small_bzero(void* dst, size_t n) { michael@0: sk_bzero(dst, n); michael@0: } michael@0: michael@0: static inline uint8_t mergeOne(uint8_t value, unsigned alpha) { michael@0: return SkMulDiv255Round(value, alpha); michael@0: } michael@0: static inline uint16_t mergeOne(uint16_t value, unsigned alpha) { michael@0: unsigned r = SkGetPackedR16(value); michael@0: unsigned g = SkGetPackedG16(value); michael@0: unsigned b = SkGetPackedB16(value); michael@0: return SkPackRGB16(SkMulDiv255Round(r, alpha), michael@0: SkMulDiv255Round(g, alpha), michael@0: SkMulDiv255Round(b, alpha)); michael@0: } michael@0: static inline SkPMColor mergeOne(SkPMColor value, unsigned alpha) { michael@0: unsigned a = SkGetPackedA32(value); michael@0: unsigned r = SkGetPackedR32(value); michael@0: unsigned g = SkGetPackedG32(value); michael@0: unsigned b = SkGetPackedB32(value); michael@0: return SkPackARGB32(SkMulDiv255Round(a, alpha), michael@0: SkMulDiv255Round(r, alpha), michael@0: SkMulDiv255Round(g, alpha), michael@0: SkMulDiv255Round(b, alpha)); michael@0: } michael@0: michael@0: template void mergeT(const T* SK_RESTRICT src, int srcN, michael@0: const uint8_t* SK_RESTRICT row, int rowN, michael@0: T* SK_RESTRICT dst) { michael@0: for (;;) { michael@0: SkASSERT(rowN > 0); michael@0: SkASSERT(srcN > 0); michael@0: michael@0: int n = SkMin32(rowN, srcN); michael@0: unsigned rowA = row[1]; michael@0: if (0xFF == rowA) { michael@0: small_memcpy(dst, src, n * sizeof(T)); michael@0: } else if (0 == rowA) { michael@0: small_bzero(dst, n * sizeof(T)); michael@0: } else { michael@0: for (int i = 0; i < n; ++i) { michael@0: dst[i] = mergeOne(src[i], rowA); michael@0: } michael@0: } michael@0: michael@0: if (0 == (srcN -= n)) { michael@0: break; michael@0: } michael@0: michael@0: src += n; michael@0: dst += n; michael@0: michael@0: SkASSERT(rowN == n); michael@0: row += 2; michael@0: rowN = row[0]; michael@0: } michael@0: } michael@0: michael@0: static MergeAAProc find_merge_aa_proc(SkMask::Format format) { michael@0: switch (format) { michael@0: case SkMask::kBW_Format: michael@0: SkDEBUGFAIL("unsupported"); michael@0: return NULL; michael@0: case SkMask::kA8_Format: michael@0: case SkMask::k3D_Format: { michael@0: void (*proc8)(const uint8_t*, int, const uint8_t*, int, uint8_t*) = mergeT; michael@0: return (MergeAAProc)proc8; michael@0: } michael@0: case SkMask::kLCD16_Format: { michael@0: void (*proc16)(const uint16_t*, int, const uint8_t*, int, uint16_t*) = mergeT; michael@0: return (MergeAAProc)proc16; michael@0: } michael@0: case SkMask::kLCD32_Format: { michael@0: void (*proc32)(const SkPMColor*, int, const uint8_t*, int, SkPMColor*) = mergeT; michael@0: return (MergeAAProc)proc32; michael@0: } michael@0: default: michael@0: SkDEBUGFAIL("unsupported"); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: static U8CPU bit2byte(int bitInAByte) { michael@0: SkASSERT(bitInAByte <= 0xFF); michael@0: // negation turns any non-zero into 0xFFFFFF??, so we just shift down michael@0: // some value >= 8 to get a full FF value michael@0: return -bitInAByte >> 8; michael@0: } michael@0: michael@0: static void upscaleBW2A8(SkMask* dstMask, const SkMask& srcMask) { michael@0: SkASSERT(SkMask::kBW_Format == srcMask.fFormat); michael@0: SkASSERT(SkMask::kA8_Format == dstMask->fFormat); michael@0: michael@0: const int width = srcMask.fBounds.width(); michael@0: const int height = srcMask.fBounds.height(); michael@0: michael@0: const uint8_t* SK_RESTRICT src = (const uint8_t*)srcMask.fImage; michael@0: const size_t srcRB = srcMask.fRowBytes; michael@0: uint8_t* SK_RESTRICT dst = (uint8_t*)dstMask->fImage; michael@0: const size_t dstRB = dstMask->fRowBytes; michael@0: michael@0: const int wholeBytes = width >> 3; michael@0: const int leftOverBits = width & 7; michael@0: michael@0: for (int y = 0; y < height; ++y) { michael@0: uint8_t* SK_RESTRICT d = dst; michael@0: for (int i = 0; i < wholeBytes; ++i) { michael@0: int srcByte = src[i]; michael@0: d[0] = bit2byte(srcByte & (1 << 7)); michael@0: d[1] = bit2byte(srcByte & (1 << 6)); michael@0: d[2] = bit2byte(srcByte & (1 << 5)); michael@0: d[3] = bit2byte(srcByte & (1 << 4)); michael@0: d[4] = bit2byte(srcByte & (1 << 3)); michael@0: d[5] = bit2byte(srcByte & (1 << 2)); michael@0: d[6] = bit2byte(srcByte & (1 << 1)); michael@0: d[7] = bit2byte(srcByte & (1 << 0)); michael@0: d += 8; michael@0: } michael@0: if (leftOverBits) { michael@0: int srcByte = src[wholeBytes]; michael@0: for (int x = 0; x < leftOverBits; ++x) { michael@0: *d++ = bit2byte(srcByte & 0x80); michael@0: srcByte <<= 1; michael@0: } michael@0: } michael@0: src += srcRB; michael@0: dst += dstRB; michael@0: } michael@0: } michael@0: michael@0: void SkAAClipBlitter::blitMask(const SkMask& origMask, const SkIRect& clip) { michael@0: SkASSERT(fAAClip->getBounds().contains(clip)); michael@0: michael@0: if (fAAClip->quickContains(clip)) { michael@0: fBlitter->blitMask(origMask, clip); michael@0: return; michael@0: } michael@0: michael@0: const SkMask* mask = &origMask; michael@0: michael@0: // if we're BW, we need to upscale to A8 (ugh) michael@0: SkMask grayMask; michael@0: grayMask.fImage = NULL; michael@0: if (SkMask::kBW_Format == origMask.fFormat) { michael@0: grayMask.fFormat = SkMask::kA8_Format; michael@0: grayMask.fBounds = origMask.fBounds; michael@0: grayMask.fRowBytes = origMask.fBounds.width(); michael@0: size_t size = grayMask.computeImageSize(); michael@0: grayMask.fImage = (uint8_t*)fGrayMaskScratch.reset(size, michael@0: SkAutoMalloc::kReuse_OnShrink); michael@0: michael@0: upscaleBW2A8(&grayMask, origMask); michael@0: mask = &grayMask; michael@0: } michael@0: michael@0: this->ensureRunsAndAA(); michael@0: michael@0: // HACK -- we are devolving 3D into A8, need to copy the rest of the 3D michael@0: // data into a temp block to support it better (ugh) michael@0: michael@0: const void* src = mask->getAddr(clip.fLeft, clip.fTop); michael@0: const size_t srcRB = mask->fRowBytes; michael@0: const int width = clip.width(); michael@0: MergeAAProc mergeProc = find_merge_aa_proc(mask->fFormat); michael@0: michael@0: SkMask rowMask; michael@0: rowMask.fFormat = SkMask::k3D_Format == mask->fFormat ? SkMask::kA8_Format : mask->fFormat; michael@0: rowMask.fBounds.fLeft = clip.fLeft; michael@0: rowMask.fBounds.fRight = clip.fRight; michael@0: rowMask.fRowBytes = mask->fRowBytes; // doesn't matter, since our height==1 michael@0: rowMask.fImage = (uint8_t*)fScanlineScratch; michael@0: michael@0: int y = clip.fTop; michael@0: const int stopY = y + clip.height(); michael@0: michael@0: do { michael@0: int localStopY SK_INIT_TO_AVOID_WARNING; michael@0: const uint8_t* row = fAAClip->findRow(y, &localStopY); michael@0: // findRow returns last Y, not stop, so we add 1 michael@0: localStopY = SkMin32(localStopY + 1, stopY); michael@0: michael@0: int initialCount; michael@0: row = fAAClip->findX(row, clip.fLeft, &initialCount); michael@0: do { michael@0: mergeProc(src, width, row, initialCount, rowMask.fImage); michael@0: rowMask.fBounds.fTop = y; michael@0: rowMask.fBounds.fBottom = y + 1; michael@0: fBlitter->blitMask(rowMask, rowMask.fBounds); michael@0: src = (const void*)((const char*)src + srcRB); michael@0: } while (++y < localStopY); michael@0: } while (y < stopY); michael@0: } michael@0: michael@0: const SkBitmap* SkAAClipBlitter::justAnOpaqueColor(uint32_t* value) { michael@0: return NULL; michael@0: }