michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project 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: michael@0: #include "SkRegionPriv.h" michael@0: #include "SkTemplates.h" michael@0: #include "SkThread.h" michael@0: #include "SkUtils.h" michael@0: michael@0: /* Region Layout michael@0: * michael@0: * TOP michael@0: * michael@0: * [ Bottom, X-Intervals, [Left, Right]..., X-Sentinel ] michael@0: * ... michael@0: * michael@0: * Y-Sentinel michael@0: */ michael@0: michael@0: SkDEBUGCODE(int32_t gRgnAllocCounter;) michael@0: michael@0: ///////////////////////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /* Pass in the beginning with the intervals. michael@0: * We back up 1 to read the interval-count. michael@0: * Return the beginning of the next scanline (i.e. the next Y-value) michael@0: */ michael@0: static SkRegion::RunType* skip_intervals(const SkRegion::RunType runs[]) { michael@0: int intervals = runs[-1]; michael@0: #ifdef SK_DEBUG michael@0: if (intervals > 0) { michael@0: SkASSERT(runs[0] < runs[1]); michael@0: SkASSERT(runs[1] < SkRegion::kRunTypeSentinel); michael@0: } else { michael@0: SkASSERT(0 == intervals); michael@0: SkASSERT(SkRegion::kRunTypeSentinel == runs[0]); michael@0: } michael@0: #endif michael@0: runs += intervals * 2 + 1; michael@0: return const_cast(runs); michael@0: } michael@0: michael@0: bool SkRegion::RunsAreARect(const SkRegion::RunType runs[], int count, michael@0: SkIRect* bounds) { michael@0: assert_sentinel(runs[0], false); // top michael@0: SkASSERT(count >= kRectRegionRuns); michael@0: michael@0: if (count == kRectRegionRuns) { michael@0: assert_sentinel(runs[1], false); // bottom michael@0: SkASSERT(1 == runs[2]); michael@0: assert_sentinel(runs[3], false); // left michael@0: assert_sentinel(runs[4], false); // right michael@0: assert_sentinel(runs[5], true); michael@0: assert_sentinel(runs[6], true); michael@0: michael@0: SkASSERT(runs[0] < runs[1]); // valid height michael@0: SkASSERT(runs[3] < runs[4]); // valid width michael@0: michael@0: bounds->set(runs[3], runs[0], runs[4], runs[1]); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkRegion::SkRegion() { michael@0: fBounds.set(0, 0, 0, 0); michael@0: fRunHead = SkRegion_gEmptyRunHeadPtr; michael@0: } michael@0: michael@0: SkRegion::SkRegion(const SkRegion& src) { michael@0: fRunHead = SkRegion_gEmptyRunHeadPtr; // just need a value that won't trigger sk_free(fRunHead) michael@0: this->setRegion(src); michael@0: } michael@0: michael@0: SkRegion::SkRegion(const SkIRect& rect) { michael@0: fRunHead = SkRegion_gEmptyRunHeadPtr; // just need a value that won't trigger sk_free(fRunHead) michael@0: this->setRect(rect); michael@0: } michael@0: michael@0: SkRegion::~SkRegion() { michael@0: this->freeRuns(); michael@0: } michael@0: michael@0: void SkRegion::freeRuns() { michael@0: if (this->isComplex()) { michael@0: SkASSERT(fRunHead->fRefCnt >= 1); michael@0: if (sk_atomic_dec(&fRunHead->fRefCnt) == 1) { michael@0: //SkASSERT(gRgnAllocCounter > 0); michael@0: //SkDEBUGCODE(sk_atomic_dec(&gRgnAllocCounter)); michael@0: //SkDEBUGF(("************** gRgnAllocCounter::free %d\n", gRgnAllocCounter)); michael@0: sk_free(fRunHead); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkRegion::allocateRuns(int count, int ySpanCount, int intervalCount) { michael@0: fRunHead = RunHead::Alloc(count, ySpanCount, intervalCount); michael@0: } michael@0: michael@0: void SkRegion::allocateRuns(int count) { michael@0: fRunHead = RunHead::Alloc(count); michael@0: } michael@0: michael@0: void SkRegion::allocateRuns(const RunHead& head) { michael@0: fRunHead = RunHead::Alloc(head.fRunCount, michael@0: head.getYSpanCount(), michael@0: head.getIntervalCount()); michael@0: } michael@0: michael@0: SkRegion& SkRegion::operator=(const SkRegion& src) { michael@0: (void)this->setRegion(src); michael@0: return *this; michael@0: } michael@0: michael@0: void SkRegion::swap(SkRegion& other) { michael@0: SkTSwap(fBounds, other.fBounds); michael@0: SkTSwap(fRunHead, other.fRunHead); michael@0: } michael@0: michael@0: int SkRegion::computeRegionComplexity() const { michael@0: if (this->isEmpty()) { michael@0: return 0; michael@0: } else if (this->isRect()) { michael@0: return 1; michael@0: } michael@0: return fRunHead->getIntervalCount(); michael@0: } michael@0: michael@0: bool SkRegion::setEmpty() { michael@0: this->freeRuns(); michael@0: fBounds.set(0, 0, 0, 0); michael@0: fRunHead = SkRegion_gEmptyRunHeadPtr; michael@0: return false; michael@0: } michael@0: michael@0: bool SkRegion::setRect(int32_t left, int32_t top, michael@0: int32_t right, int32_t bottom) { michael@0: if (left >= right || top >= bottom) { michael@0: return this->setEmpty(); michael@0: } michael@0: this->freeRuns(); michael@0: fBounds.set(left, top, right, bottom); michael@0: fRunHead = SkRegion_gRectRunHeadPtr; michael@0: return true; michael@0: } michael@0: michael@0: bool SkRegion::setRect(const SkIRect& r) { michael@0: return this->setRect(r.fLeft, r.fTop, r.fRight, r.fBottom); michael@0: } michael@0: michael@0: bool SkRegion::setRegion(const SkRegion& src) { michael@0: if (this != &src) { michael@0: this->freeRuns(); michael@0: michael@0: fBounds = src.fBounds; michael@0: fRunHead = src.fRunHead; michael@0: if (this->isComplex()) { michael@0: sk_atomic_inc(&fRunHead->fRefCnt); michael@0: } michael@0: } michael@0: return fRunHead != SkRegion_gEmptyRunHeadPtr; michael@0: } michael@0: michael@0: bool SkRegion::op(const SkIRect& rect, const SkRegion& rgn, Op op) { michael@0: SkRegion tmp(rect); michael@0: michael@0: return this->op(tmp, rgn, op); michael@0: } michael@0: michael@0: bool SkRegion::op(const SkRegion& rgn, const SkIRect& rect, Op op) { michael@0: SkRegion tmp(rect); michael@0: michael@0: return this->op(rgn, tmp, op); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: #include michael@0: char* SkRegion::toString() { michael@0: Iterator iter(*this); michael@0: int count = 0; michael@0: while (!iter.done()) { michael@0: count++; michael@0: iter.next(); michael@0: } michael@0: // 4 ints, up to 10 digits each plus sign, 3 commas, '(', ')', SkRegion() and '\0' michael@0: const int max = (count*((11*4)+5))+11+1; michael@0: char* result = (char*)malloc(max); michael@0: if (result == NULL) { michael@0: return NULL; michael@0: } michael@0: count = sprintf(result, "SkRegion("); michael@0: iter.reset(*this); michael@0: while (!iter.done()) { michael@0: const SkIRect& r = iter.rect(); michael@0: count += sprintf(result+count, "(%d,%d,%d,%d)", r.fLeft, r.fTop, r.fRight, r.fBottom); michael@0: iter.next(); michael@0: } michael@0: count += sprintf(result+count, ")"); michael@0: return result; michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: int SkRegion::count_runtype_values(int* itop, int* ibot) const { michael@0: if (this == NULL) { michael@0: *itop = SK_MinS32; michael@0: *ibot = SK_MaxS32; michael@0: return 0; michael@0: } michael@0: michael@0: int maxT; michael@0: michael@0: if (this->isRect()) { michael@0: maxT = 2; michael@0: } else { michael@0: SkASSERT(this->isComplex()); michael@0: maxT = fRunHead->getIntervalCount() * 2; michael@0: } michael@0: *itop = fBounds.fTop; michael@0: *ibot = fBounds.fBottom; michael@0: return maxT; michael@0: } michael@0: michael@0: static bool isRunCountEmpty(int count) { michael@0: return count <= 2; michael@0: } michael@0: michael@0: bool SkRegion::setRuns(RunType runs[], int count) { michael@0: SkDEBUGCODE(this->validate();) michael@0: SkASSERT(count > 0); michael@0: michael@0: if (isRunCountEmpty(count)) { michael@0: // SkDEBUGF(("setRuns: empty\n")); michael@0: assert_sentinel(runs[count-1], true); michael@0: return this->setEmpty(); michael@0: } michael@0: michael@0: // trim off any empty spans from the top and bottom michael@0: // weird I should need this, perhaps op() could be smarter... michael@0: if (count > kRectRegionRuns) { michael@0: RunType* stop = runs + count; michael@0: assert_sentinel(runs[0], false); // top michael@0: assert_sentinel(runs[1], false); // bottom michael@0: // runs[2] is uncomputed intervalCount michael@0: michael@0: if (runs[3] == SkRegion::kRunTypeSentinel) { // should be first left... michael@0: runs += 3; // skip empty initial span michael@0: runs[0] = runs[-2]; // set new top to prev bottom michael@0: assert_sentinel(runs[1], false); // bot: a sentinal would mean two in a row michael@0: assert_sentinel(runs[2], false); // intervalcount michael@0: assert_sentinel(runs[3], false); // left michael@0: assert_sentinel(runs[4], false); // right michael@0: } michael@0: michael@0: assert_sentinel(stop[-1], true); michael@0: assert_sentinel(stop[-2], true); michael@0: michael@0: // now check for a trailing empty span michael@0: if (stop[-5] == SkRegion::kRunTypeSentinel) { // eek, stop[-4] was a bottom with no x-runs michael@0: stop[-4] = SkRegion::kRunTypeSentinel; // kill empty last span michael@0: stop -= 3; michael@0: assert_sentinel(stop[-1], true); // last y-sentinel michael@0: assert_sentinel(stop[-2], true); // last x-sentinel michael@0: assert_sentinel(stop[-3], false); // last right michael@0: assert_sentinel(stop[-4], false); // last left michael@0: assert_sentinel(stop[-5], false); // last interval-count michael@0: assert_sentinel(stop[-6], false); // last bottom michael@0: } michael@0: count = (int)(stop - runs); michael@0: } michael@0: michael@0: SkASSERT(count >= kRectRegionRuns); michael@0: michael@0: if (SkRegion::RunsAreARect(runs, count, &fBounds)) { michael@0: return this->setRect(fBounds); michael@0: } michael@0: michael@0: // if we get here, we need to become a complex region michael@0: michael@0: if (!this->isComplex() || fRunHead->fRunCount != count) { michael@0: this->freeRuns(); michael@0: this->allocateRuns(count); michael@0: } michael@0: michael@0: // must call this before we can write directly into runs() michael@0: // in case we are sharing the buffer with another region (copy on write) michael@0: fRunHead = fRunHead->ensureWritable(); michael@0: memcpy(fRunHead->writable_runs(), runs, count * sizeof(RunType)); michael@0: fRunHead->computeRunBounds(&fBounds); michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void SkRegion::BuildRectRuns(const SkIRect& bounds, michael@0: RunType runs[kRectRegionRuns]) { michael@0: runs[0] = bounds.fTop; michael@0: runs[1] = bounds.fBottom; michael@0: runs[2] = 1; // 1 interval for this scanline michael@0: runs[3] = bounds.fLeft; michael@0: runs[4] = bounds.fRight; michael@0: runs[5] = kRunTypeSentinel; michael@0: runs[6] = kRunTypeSentinel; michael@0: } michael@0: michael@0: bool SkRegion::contains(int32_t x, int32_t y) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: if (!fBounds.contains(x, y)) { michael@0: return false; michael@0: } michael@0: if (this->isRect()) { michael@0: return true; michael@0: } michael@0: SkASSERT(this->isComplex()); michael@0: michael@0: const RunType* runs = fRunHead->findScanline(y); michael@0: michael@0: // Skip the Bottom and IntervalCount michael@0: runs += 2; michael@0: michael@0: // Just walk this scanline, checking each interval. The X-sentinel will michael@0: // appear as a left-inteval (runs[0]) and should abort the search. michael@0: // michael@0: // We could do a bsearch, using interval-count (runs[1]), but need to time michael@0: // when that would be worthwhile. michael@0: // michael@0: for (;;) { michael@0: if (x < runs[0]) { michael@0: break; michael@0: } michael@0: if (x < runs[1]) { michael@0: return true; michael@0: } michael@0: runs += 2; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkRegion::RunType scanline_bottom(const SkRegion::RunType runs[]) { michael@0: return runs[0]; michael@0: } michael@0: michael@0: static const SkRegion::RunType* scanline_next(const SkRegion::RunType runs[]) { michael@0: // skip [B N [L R]... S] michael@0: return runs + 2 + runs[1] * 2 + 1; michael@0: } michael@0: michael@0: static bool scanline_contains(const SkRegion::RunType runs[], michael@0: SkRegion::RunType L, SkRegion::RunType R) { michael@0: runs += 2; // skip Bottom and IntervalCount michael@0: for (;;) { michael@0: if (L < runs[0]) { michael@0: break; michael@0: } michael@0: if (R <= runs[1]) { michael@0: return true; michael@0: } michael@0: runs += 2; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool SkRegion::contains(const SkIRect& r) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: if (!fBounds.contains(r)) { michael@0: return false; michael@0: } michael@0: if (this->isRect()) { michael@0: return true; michael@0: } michael@0: SkASSERT(this->isComplex()); michael@0: michael@0: const RunType* scanline = fRunHead->findScanline(r.fTop); michael@0: for (;;) { michael@0: if (!scanline_contains(scanline, r.fLeft, r.fRight)) { michael@0: return false; michael@0: } michael@0: if (r.fBottom <= scanline_bottom(scanline)) { michael@0: break; michael@0: } michael@0: scanline = scanline_next(scanline); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkRegion::contains(const SkRegion& rgn) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: SkDEBUGCODE(rgn.validate();) michael@0: michael@0: if (this->isEmpty() || rgn.isEmpty() || !fBounds.contains(rgn.fBounds)) { michael@0: return false; michael@0: } michael@0: if (this->isRect()) { michael@0: return true; michael@0: } michael@0: if (rgn.isRect()) { michael@0: return this->contains(rgn.getBounds()); michael@0: } michael@0: michael@0: /* michael@0: * A contains B is equivalent to michael@0: * B - A == 0 michael@0: */ michael@0: return !Oper(rgn, *this, kDifference_Op, NULL); michael@0: } michael@0: michael@0: const SkRegion::RunType* SkRegion::getRuns(RunType tmpStorage[], michael@0: int* intervals) const { michael@0: SkASSERT(tmpStorage && intervals); michael@0: const RunType* runs = tmpStorage; michael@0: michael@0: if (this->isEmpty()) { michael@0: tmpStorage[0] = kRunTypeSentinel; michael@0: *intervals = 0; michael@0: } else if (this->isRect()) { michael@0: BuildRectRuns(fBounds, tmpStorage); michael@0: *intervals = 1; michael@0: } else { michael@0: runs = fRunHead->readonly_runs(); michael@0: *intervals = fRunHead->getIntervalCount(); michael@0: } michael@0: return runs; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static bool scanline_intersects(const SkRegion::RunType runs[], michael@0: SkRegion::RunType L, SkRegion::RunType R) { michael@0: runs += 2; // skip Bottom and IntervalCount michael@0: for (;;) { michael@0: if (R <= runs[0]) { michael@0: break; michael@0: } michael@0: if (L < runs[1]) { michael@0: return true; michael@0: } michael@0: runs += 2; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool SkRegion::intersects(const SkIRect& r) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: if (this->isEmpty() || r.isEmpty()) { michael@0: return false; michael@0: } michael@0: michael@0: SkIRect sect; michael@0: if (!sect.intersect(fBounds, r)) { michael@0: return false; michael@0: } michael@0: if (this->isRect()) { michael@0: return true; michael@0: } michael@0: SkASSERT(this->isComplex()); michael@0: michael@0: const RunType* scanline = fRunHead->findScanline(sect.fTop); michael@0: for (;;) { michael@0: if (scanline_intersects(scanline, sect.fLeft, sect.fRight)) { michael@0: return true; michael@0: } michael@0: if (sect.fBottom <= scanline_bottom(scanline)) { michael@0: break; michael@0: } michael@0: scanline = scanline_next(scanline); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool SkRegion::intersects(const SkRegion& rgn) const { michael@0: if (this->isEmpty() || rgn.isEmpty()) { michael@0: return false; michael@0: } michael@0: michael@0: if (!SkIRect::Intersects(fBounds, rgn.fBounds)) { michael@0: return false; michael@0: } michael@0: michael@0: bool weAreARect = this->isRect(); michael@0: bool theyAreARect = rgn.isRect(); michael@0: michael@0: if (weAreARect && theyAreARect) { michael@0: return true; michael@0: } michael@0: if (weAreARect) { michael@0: return rgn.intersects(this->getBounds()); michael@0: } michael@0: if (theyAreARect) { michael@0: return this->intersects(rgn.getBounds()); michael@0: } michael@0: michael@0: // both of us are complex michael@0: return Oper(*this, rgn, kIntersect_Op, NULL); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkRegion::operator==(const SkRegion& b) const { michael@0: SkDEBUGCODE(validate();) michael@0: SkDEBUGCODE(b.validate();) michael@0: michael@0: if (this == &b) { michael@0: return true; michael@0: } michael@0: if (fBounds != b.fBounds) { michael@0: return false; michael@0: } michael@0: michael@0: const SkRegion::RunHead* ah = fRunHead; michael@0: const SkRegion::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: // now we insist that both are complex (but different ptrs) michael@0: if (!this->isComplex() || !b.isComplex()) { michael@0: return false; michael@0: } michael@0: return ah->fRunCount == bh->fRunCount && michael@0: !memcmp(ah->readonly_runs(), bh->readonly_runs(), michael@0: ah->fRunCount * sizeof(SkRegion::RunType)); michael@0: } michael@0: michael@0: void SkRegion::translate(int dx, int dy, SkRegion* dst) const { michael@0: SkDEBUGCODE(this->validate();) michael@0: michael@0: if (NULL == dst) { michael@0: return; michael@0: } michael@0: if (this->isEmpty()) { michael@0: dst->setEmpty(); michael@0: } else if (this->isRect()) { michael@0: dst->setRect(fBounds.fLeft + dx, fBounds.fTop + dy, michael@0: fBounds.fRight + dx, fBounds.fBottom + dy); michael@0: } else { michael@0: if (this == dst) { michael@0: dst->fRunHead = dst->fRunHead->ensureWritable(); michael@0: } else { michael@0: SkRegion tmp; michael@0: tmp.allocateRuns(*fRunHead); michael@0: tmp.fBounds = fBounds; michael@0: dst->swap(tmp); michael@0: } michael@0: michael@0: dst->fBounds.offset(dx, dy); michael@0: michael@0: const RunType* sruns = fRunHead->readonly_runs(); michael@0: RunType* druns = dst->fRunHead->writable_runs(); michael@0: michael@0: *druns++ = (SkRegion::RunType)(*sruns++ + dy); // top michael@0: for (;;) { michael@0: int bottom = *sruns++; michael@0: if (bottom == kRunTypeSentinel) { michael@0: break; michael@0: } michael@0: *druns++ = (SkRegion::RunType)(bottom + dy); // bottom; michael@0: *druns++ = *sruns++; // copy intervalCount; michael@0: for (;;) { michael@0: int x = *sruns++; michael@0: if (x == kRunTypeSentinel) { michael@0: break; michael@0: } michael@0: *druns++ = (SkRegion::RunType)(x + dx); michael@0: *druns++ = (SkRegion::RunType)(*sruns++ + dx); michael@0: } michael@0: *druns++ = kRunTypeSentinel; // x sentinel michael@0: } michael@0: *druns++ = kRunTypeSentinel; // y sentinel michael@0: michael@0: SkASSERT(sruns - fRunHead->readonly_runs() == fRunHead->fRunCount); michael@0: SkASSERT(druns - dst->fRunHead->readonly_runs() == dst->fRunHead->fRunCount); michael@0: } michael@0: michael@0: SkDEBUGCODE(this->validate();) michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: bool SkRegion::setRects(const SkIRect rects[], int count) { michael@0: if (0 == count) { michael@0: this->setEmpty(); michael@0: } else { michael@0: this->setRect(rects[0]); michael@0: for (int i = 1; i < count; i++) { michael@0: this->op(rects[i], kUnion_Op); michael@0: } michael@0: } michael@0: return !this->isEmpty(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #if defined _WIN32 && _MSC_VER >= 1300 // disable warning : local variable used without having been initialized michael@0: #pragma warning ( push ) michael@0: #pragma warning ( disable : 4701 ) michael@0: #endif michael@0: michael@0: #ifdef SK_DEBUG michael@0: static void assert_valid_pair(int left, int rite) michael@0: { michael@0: SkASSERT(left == SkRegion::kRunTypeSentinel || left < rite); michael@0: } michael@0: #else michael@0: #define assert_valid_pair(left, rite) michael@0: #endif michael@0: michael@0: struct spanRec { michael@0: const SkRegion::RunType* fA_runs; michael@0: const SkRegion::RunType* fB_runs; michael@0: int fA_left, fA_rite, fB_left, fB_rite; michael@0: int fLeft, fRite, fInside; michael@0: michael@0: void init(const SkRegion::RunType a_runs[], michael@0: const SkRegion::RunType b_runs[]) { michael@0: fA_left = *a_runs++; michael@0: fA_rite = *a_runs++; michael@0: fB_left = *b_runs++; michael@0: fB_rite = *b_runs++; michael@0: michael@0: fA_runs = a_runs; michael@0: fB_runs = b_runs; michael@0: } michael@0: michael@0: bool done() const { michael@0: SkASSERT(fA_left <= SkRegion::kRunTypeSentinel); michael@0: SkASSERT(fB_left <= SkRegion::kRunTypeSentinel); michael@0: return fA_left == SkRegion::kRunTypeSentinel && michael@0: fB_left == SkRegion::kRunTypeSentinel; michael@0: } michael@0: michael@0: void next() { michael@0: assert_valid_pair(fA_left, fA_rite); michael@0: assert_valid_pair(fB_left, fB_rite); michael@0: michael@0: int inside, left, rite SK_INIT_TO_AVOID_WARNING; michael@0: bool a_flush = false; michael@0: bool b_flush = false; michael@0: michael@0: int a_left = fA_left; michael@0: int a_rite = fA_rite; michael@0: int b_left = fB_left; michael@0: int b_rite = fB_rite; michael@0: michael@0: if (a_left < b_left) { michael@0: inside = 1; michael@0: left = a_left; michael@0: if (a_rite <= b_left) { // [...] <...> michael@0: rite = a_rite; michael@0: a_flush = true; michael@0: } else { // [...<..]...> or [...<...>...] michael@0: rite = a_left = b_left; michael@0: } michael@0: } else if (b_left < a_left) { michael@0: inside = 2; michael@0: left = b_left; michael@0: if (b_rite <= a_left) { // [...] <...> michael@0: rite = b_rite; michael@0: b_flush = true; michael@0: } else { // [...<..]...> or [...<...>...] michael@0: rite = b_left = a_left; michael@0: } michael@0: } else { // a_left == b_left michael@0: inside = 3; michael@0: left = a_left; // or b_left michael@0: if (a_rite <= b_rite) { michael@0: rite = b_left = a_rite; michael@0: a_flush = true; michael@0: } michael@0: if (b_rite <= a_rite) { michael@0: rite = a_left = b_rite; michael@0: b_flush = true; michael@0: } michael@0: } michael@0: michael@0: if (a_flush) { michael@0: a_left = *fA_runs++; michael@0: a_rite = *fA_runs++; michael@0: } michael@0: if (b_flush) { michael@0: b_left = *fB_runs++; michael@0: b_rite = *fB_runs++; michael@0: } michael@0: michael@0: SkASSERT(left <= rite); michael@0: michael@0: // now update our state michael@0: fA_left = a_left; michael@0: fA_rite = a_rite; michael@0: fB_left = b_left; michael@0: fB_rite = b_rite; michael@0: michael@0: fLeft = left; michael@0: fRite = rite; michael@0: fInside = inside; michael@0: } michael@0: }; michael@0: michael@0: static SkRegion::RunType* operate_on_span(const SkRegion::RunType a_runs[], michael@0: const SkRegion::RunType b_runs[], michael@0: SkRegion::RunType dst[], michael@0: int min, int max) { michael@0: spanRec rec; michael@0: bool firstInterval = true; michael@0: michael@0: rec.init(a_runs, b_runs); michael@0: michael@0: while (!rec.done()) { michael@0: rec.next(); michael@0: michael@0: int left = rec.fLeft; michael@0: int rite = rec.fRite; michael@0: michael@0: // add left,rite to our dst buffer (checking for coincidence michael@0: if ((unsigned)(rec.fInside - min) <= (unsigned)(max - min) && michael@0: left < rite) { // skip if equal michael@0: if (firstInterval || dst[-1] < left) { michael@0: *dst++ = (SkRegion::RunType)(left); michael@0: *dst++ = (SkRegion::RunType)(rite); michael@0: firstInterval = false; michael@0: } else { michael@0: // update the right edge michael@0: dst[-1] = (SkRegion::RunType)(rite); michael@0: } michael@0: } michael@0: } michael@0: michael@0: *dst++ = SkRegion::kRunTypeSentinel; michael@0: return dst; michael@0: } michael@0: michael@0: #if defined _WIN32 && _MSC_VER >= 1300 michael@0: #pragma warning ( pop ) michael@0: #endif michael@0: michael@0: static const struct { michael@0: uint8_t fMin; michael@0: uint8_t fMax; michael@0: } gOpMinMax[] = { michael@0: { 1, 1 }, // Difference michael@0: { 3, 3 }, // Intersection michael@0: { 1, 3 }, // Union michael@0: { 1, 2 } // XOR michael@0: }; michael@0: michael@0: class RgnOper { michael@0: public: michael@0: RgnOper(int top, SkRegion::RunType dst[], SkRegion::Op op) { michael@0: // need to ensure that the op enum lines up with our minmax array michael@0: SkASSERT(SkRegion::kDifference_Op == 0); michael@0: SkASSERT(SkRegion::kIntersect_Op == 1); michael@0: SkASSERT(SkRegion::kUnion_Op == 2); michael@0: SkASSERT(SkRegion::kXOR_Op == 3); michael@0: SkASSERT((unsigned)op <= 3); michael@0: michael@0: fStartDst = dst; michael@0: fPrevDst = dst + 1; michael@0: fPrevLen = 0; // will never match a length from operate_on_span michael@0: fTop = (SkRegion::RunType)(top); // just a first guess, we might update this michael@0: michael@0: fMin = gOpMinMax[op].fMin; michael@0: fMax = gOpMinMax[op].fMax; michael@0: } michael@0: michael@0: void addSpan(int bottom, const SkRegion::RunType a_runs[], michael@0: const SkRegion::RunType b_runs[]) { michael@0: // skip X values and slots for the next Y+intervalCount michael@0: SkRegion::RunType* start = fPrevDst + fPrevLen + 2; michael@0: // start points to beginning of dst interval michael@0: SkRegion::RunType* stop = operate_on_span(a_runs, b_runs, start, fMin, fMax); michael@0: size_t len = stop - start; michael@0: SkASSERT(len >= 1 && (len & 1) == 1); michael@0: SkASSERT(SkRegion::kRunTypeSentinel == stop[-1]); michael@0: michael@0: if (fPrevLen == len && michael@0: (1 == len || !memcmp(fPrevDst, start, michael@0: (len - 1) * sizeof(SkRegion::RunType)))) { michael@0: // update Y value michael@0: fPrevDst[-2] = (SkRegion::RunType)(bottom); michael@0: } else { // accept the new span michael@0: if (len == 1 && fPrevLen == 0) { michael@0: fTop = (SkRegion::RunType)(bottom); // just update our bottom michael@0: } else { michael@0: start[-2] = (SkRegion::RunType)(bottom); michael@0: start[-1] = len >> 1; michael@0: fPrevDst = start; michael@0: fPrevLen = len; michael@0: } michael@0: } michael@0: } michael@0: michael@0: int flush() { michael@0: fStartDst[0] = fTop; michael@0: fPrevDst[fPrevLen] = SkRegion::kRunTypeSentinel; michael@0: return (int)(fPrevDst - fStartDst + fPrevLen + 1); michael@0: } michael@0: michael@0: bool isEmpty() const { return 0 == fPrevLen; } michael@0: michael@0: uint8_t fMin, fMax; michael@0: michael@0: private: michael@0: SkRegion::RunType* fStartDst; michael@0: SkRegion::RunType* fPrevDst; michael@0: size_t fPrevLen; michael@0: SkRegion::RunType fTop; michael@0: }; michael@0: michael@0: // want a unique value to signal that we exited due to quickExit michael@0: #define QUICK_EXIT_TRUE_COUNT (-1) michael@0: michael@0: static int operate(const SkRegion::RunType a_runs[], michael@0: const SkRegion::RunType b_runs[], michael@0: SkRegion::RunType dst[], michael@0: SkRegion::Op op, michael@0: bool quickExit) { michael@0: const SkRegion::RunType gEmptyScanline[] = { michael@0: 0, // dummy bottom value michael@0: 0, // zero intervals michael@0: SkRegion::kRunTypeSentinel, michael@0: // just need a 2nd value, since spanRec.init() reads 2 values, even michael@0: // though if the first value is the sentinel, it ignores the 2nd value. michael@0: // w/o the 2nd value here, we might read uninitialized memory. michael@0: // This happens when we are using gSentinel, which is pointing at michael@0: // our sentinel value. michael@0: 0 michael@0: }; michael@0: const SkRegion::RunType* const gSentinel = &gEmptyScanline[2]; michael@0: michael@0: int a_top = *a_runs++; michael@0: int a_bot = *a_runs++; michael@0: int b_top = *b_runs++; michael@0: int b_bot = *b_runs++; michael@0: michael@0: a_runs += 1; // skip the intervalCount; michael@0: b_runs += 1; // skip the intervalCount; michael@0: michael@0: // Now a_runs and b_runs to their intervals (or sentinel) michael@0: michael@0: assert_sentinel(a_top, false); michael@0: assert_sentinel(a_bot, false); michael@0: assert_sentinel(b_top, false); michael@0: assert_sentinel(b_bot, false); michael@0: michael@0: RgnOper oper(SkMin32(a_top, b_top), dst, op); michael@0: michael@0: int prevBot = SkRegion::kRunTypeSentinel; // so we fail the first test michael@0: michael@0: while (a_bot < SkRegion::kRunTypeSentinel || michael@0: b_bot < SkRegion::kRunTypeSentinel) { michael@0: int top, bot SK_INIT_TO_AVOID_WARNING; michael@0: const SkRegion::RunType* run0 = gSentinel; michael@0: const SkRegion::RunType* run1 = gSentinel; michael@0: bool a_flush = false; michael@0: bool b_flush = false; michael@0: michael@0: if (a_top < b_top) { michael@0: top = a_top; michael@0: run0 = a_runs; michael@0: if (a_bot <= b_top) { // [...] <...> michael@0: bot = a_bot; michael@0: a_flush = true; michael@0: } else { // [...<..]...> or [...<...>...] michael@0: bot = a_top = b_top; michael@0: } michael@0: } else if (b_top < a_top) { michael@0: top = b_top; michael@0: run1 = b_runs; michael@0: if (b_bot <= a_top) { // [...] <...> michael@0: bot = b_bot; michael@0: b_flush = true; michael@0: } else { // [...<..]...> or [...<...>...] michael@0: bot = b_top = a_top; michael@0: } michael@0: } else { // a_top == b_top michael@0: top = a_top; // or b_top michael@0: run0 = a_runs; michael@0: run1 = b_runs; michael@0: if (a_bot <= b_bot) { michael@0: bot = b_top = a_bot; michael@0: a_flush = true; michael@0: } michael@0: if (b_bot <= a_bot) { michael@0: bot = a_top = b_bot; michael@0: b_flush = true; michael@0: } michael@0: } michael@0: michael@0: if (top > prevBot) { michael@0: oper.addSpan(top, gSentinel, gSentinel); michael@0: } michael@0: oper.addSpan(bot, run0, run1); michael@0: michael@0: if (quickExit && !oper.isEmpty()) { michael@0: return QUICK_EXIT_TRUE_COUNT; michael@0: } michael@0: michael@0: if (a_flush) { michael@0: a_runs = skip_intervals(a_runs); michael@0: a_top = a_bot; michael@0: a_bot = *a_runs++; michael@0: a_runs += 1; // skip uninitialized intervalCount michael@0: if (a_bot == SkRegion::kRunTypeSentinel) { michael@0: a_top = a_bot; michael@0: } michael@0: } michael@0: if (b_flush) { michael@0: b_runs = skip_intervals(b_runs); michael@0: b_top = b_bot; michael@0: b_bot = *b_runs++; michael@0: b_runs += 1; // skip uninitialized intervalCount michael@0: if (b_bot == SkRegion::kRunTypeSentinel) { michael@0: b_top = b_bot; michael@0: } michael@0: } michael@0: michael@0: prevBot = bot; michael@0: } michael@0: return oper.flush(); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /* Given count RunTypes in a complex region, return the worst case number of michael@0: logical intervals that represents (i.e. number of rects that would be michael@0: returned from the iterator). michael@0: michael@0: We could just return count/2, since there must be at least 2 values per michael@0: interval, but we can first trim off the const overhead of the initial TOP michael@0: value, plus the final BOTTOM + 2 sentinels. michael@0: */ michael@0: #if 0 // UNUSED michael@0: static int count_to_intervals(int count) { michael@0: SkASSERT(count >= 6); // a single rect is 6 values michael@0: return (count - 4) >> 1; michael@0: } michael@0: #endif michael@0: michael@0: /* Given a number of intervals, what is the worst case representation of that michael@0: many intervals? michael@0: michael@0: Worst case (from a storage perspective), is a vertical stack of single michael@0: intervals: TOP + N * (BOTTOM INTERVALCOUNT LEFT RIGHT SENTINEL) + SENTINEL michael@0: */ michael@0: static int intervals_to_count(int intervals) { michael@0: return 1 + intervals * 5 + 1; michael@0: } michael@0: michael@0: /* Given the intervalCounts of RunTypes in two regions, return the worst-case number michael@0: of RunTypes need to store the result after a region-op. michael@0: */ michael@0: static int compute_worst_case_count(int a_intervals, int b_intervals) { michael@0: // Our heuristic worst case is ai * (bi + 1) + bi * (ai + 1) michael@0: int intervals = 2 * a_intervals * b_intervals + a_intervals + b_intervals; michael@0: // convert back to number of RunType values michael@0: return intervals_to_count(intervals); michael@0: } michael@0: michael@0: static bool setEmptyCheck(SkRegion* result) { michael@0: return result ? result->setEmpty() : false; michael@0: } michael@0: michael@0: static bool setRectCheck(SkRegion* result, const SkIRect& rect) { michael@0: return result ? result->setRect(rect) : !rect.isEmpty(); michael@0: } michael@0: michael@0: static bool setRegionCheck(SkRegion* result, const SkRegion& rgn) { michael@0: return result ? result->setRegion(rgn) : !rgn.isEmpty(); michael@0: } michael@0: michael@0: bool SkRegion::Oper(const SkRegion& rgnaOrig, const SkRegion& rgnbOrig, Op op, michael@0: SkRegion* result) { michael@0: SkASSERT((unsigned)op < kOpCount); michael@0: michael@0: if (kReplace_Op == op) { michael@0: return setRegionCheck(result, rgnbOrig); michael@0: } michael@0: michael@0: // swith to using pointers, so we can swap them as needed michael@0: const SkRegion* rgna = &rgnaOrig; michael@0: const SkRegion* rgnb = &rgnbOrig; michael@0: // after this point, do not refer to rgnaOrig or rgnbOrig!!! michael@0: michael@0: // collaps difference and reverse-difference into just difference michael@0: if (kReverseDifference_Op == op) { michael@0: SkTSwap(rgna, rgnb); michael@0: op = kDifference_Op; michael@0: } michael@0: michael@0: SkIRect bounds; michael@0: bool a_empty = rgna->isEmpty(); michael@0: bool b_empty = rgnb->isEmpty(); michael@0: bool a_rect = rgna->isRect(); michael@0: bool b_rect = rgnb->isRect(); michael@0: michael@0: switch (op) { michael@0: case kDifference_Op: michael@0: if (a_empty) { michael@0: return setEmptyCheck(result); michael@0: } michael@0: if (b_empty || !SkIRect::IntersectsNoEmptyCheck(rgna->fBounds, michael@0: rgnb->fBounds)) { michael@0: return setRegionCheck(result, *rgna); michael@0: } michael@0: if (b_rect && rgnb->fBounds.containsNoEmptyCheck(rgna->fBounds)) { michael@0: return setEmptyCheck(result); michael@0: } michael@0: break; michael@0: michael@0: case kIntersect_Op: michael@0: if ((a_empty | b_empty) michael@0: || !bounds.intersect(rgna->fBounds, rgnb->fBounds)) { michael@0: return setEmptyCheck(result); michael@0: } michael@0: if (a_rect & b_rect) { michael@0: return setRectCheck(result, bounds); michael@0: } michael@0: if (a_rect && rgna->fBounds.contains(rgnb->fBounds)) { michael@0: return setRegionCheck(result, *rgnb); michael@0: } michael@0: if (b_rect && rgnb->fBounds.contains(rgna->fBounds)) { michael@0: return setRegionCheck(result, *rgna); michael@0: } michael@0: break; michael@0: michael@0: case kUnion_Op: michael@0: if (a_empty) { michael@0: return setRegionCheck(result, *rgnb); michael@0: } michael@0: if (b_empty) { michael@0: return setRegionCheck(result, *rgna); michael@0: } michael@0: if (a_rect && rgna->fBounds.contains(rgnb->fBounds)) { michael@0: return setRegionCheck(result, *rgna); michael@0: } michael@0: if (b_rect && rgnb->fBounds.contains(rgna->fBounds)) { michael@0: return setRegionCheck(result, *rgnb); michael@0: } michael@0: break; michael@0: michael@0: case kXOR_Op: michael@0: if (a_empty) { michael@0: return setRegionCheck(result, *rgnb); michael@0: } michael@0: if (b_empty) { michael@0: return setRegionCheck(result, *rgna); michael@0: } michael@0: break; michael@0: default: michael@0: SkDEBUGFAIL("unknown region op"); michael@0: return false; michael@0: } michael@0: michael@0: RunType tmpA[kRectRegionRuns]; michael@0: RunType tmpB[kRectRegionRuns]; michael@0: michael@0: int a_intervals, b_intervals; michael@0: const RunType* a_runs = rgna->getRuns(tmpA, &a_intervals); michael@0: const RunType* b_runs = rgnb->getRuns(tmpB, &b_intervals); michael@0: michael@0: int dstCount = compute_worst_case_count(a_intervals, b_intervals); michael@0: SkAutoSTMalloc<256, RunType> array(dstCount); michael@0: michael@0: #ifdef SK_DEBUG michael@0: // Sometimes helpful to seed everything with a known value when debugging michael@0: // sk_memset32((uint32_t*)array.get(), 0x7FFFFFFF, dstCount); michael@0: #endif michael@0: michael@0: int count = operate(a_runs, b_runs, array.get(), op, NULL == result); michael@0: SkASSERT(count <= dstCount); michael@0: michael@0: if (result) { michael@0: SkASSERT(count >= 0); michael@0: return result->setRuns(array.get(), count); michael@0: } else { michael@0: return (QUICK_EXIT_TRUE_COUNT == count) || !isRunCountEmpty(count); michael@0: } michael@0: } michael@0: michael@0: bool SkRegion::op(const SkRegion& rgna, const SkRegion& rgnb, Op op) { michael@0: SkDEBUGCODE(this->validate();) michael@0: return SkRegion::Oper(rgna, rgnb, op, this); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkBuffer.h" michael@0: michael@0: size_t SkRegion::writeToMemory(void* storage) const { michael@0: if (NULL == storage) { michael@0: size_t size = sizeof(int32_t); // -1 (empty), 0 (rect), runCount michael@0: if (!this->isEmpty()) { michael@0: size += sizeof(fBounds); michael@0: if (this->isComplex()) { michael@0: size += 2 * sizeof(int32_t); // ySpanCount + intervalCount michael@0: size += fRunHead->fRunCount * sizeof(RunType); michael@0: } michael@0: } michael@0: return size; michael@0: } michael@0: michael@0: SkWBuffer buffer(storage); michael@0: michael@0: if (this->isEmpty()) { michael@0: buffer.write32(-1); michael@0: } else { michael@0: bool isRect = this->isRect(); michael@0: michael@0: buffer.write32(isRect ? 0 : fRunHead->fRunCount); michael@0: buffer.write(&fBounds, sizeof(fBounds)); michael@0: michael@0: if (!isRect) { michael@0: buffer.write32(fRunHead->getYSpanCount()); michael@0: buffer.write32(fRunHead->getIntervalCount()); michael@0: buffer.write(fRunHead->readonly_runs(), michael@0: fRunHead->fRunCount * sizeof(RunType)); michael@0: } michael@0: } michael@0: return buffer.pos(); michael@0: } michael@0: michael@0: size_t SkRegion::readFromMemory(const void* storage, size_t length) { michael@0: SkRBufferWithSizeCheck buffer(storage, length); michael@0: SkRegion tmp; michael@0: int32_t count; michael@0: michael@0: if (buffer.readS32(&count) && (count >= 0) && buffer.read(&tmp.fBounds, sizeof(tmp.fBounds))) { michael@0: if (count == 0) { michael@0: tmp.fRunHead = SkRegion_gRectRunHeadPtr; michael@0: } else { michael@0: int32_t ySpanCount, intervalCount; michael@0: if (buffer.readS32(&ySpanCount) && buffer.readS32(&intervalCount)) { michael@0: tmp.allocateRuns(count, ySpanCount, intervalCount); michael@0: buffer.read(tmp.fRunHead->writable_runs(), count * sizeof(RunType)); michael@0: } michael@0: } michael@0: } michael@0: size_t sizeRead = 0; michael@0: if (buffer.isValid()) { michael@0: this->swap(tmp); michael@0: sizeRead = buffer.pos(); michael@0: } michael@0: return sizeRead; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: const SkRegion& SkRegion::GetEmptyRegion() { michael@0: static SkRegion gEmpty; michael@0: return gEmpty; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: michael@0: // Starts with first X-interval, and returns a ptr to the X-sentinel michael@0: static const SkRegion::RunType* skip_intervals_slow(const SkRegion::RunType runs[]) { michael@0: // want to track that our intevals are all disjoint, such that michael@0: // prev-right < next-left. We rely on this optimization in places such as michael@0: // contains(). michael@0: // michael@0: SkRegion::RunType prevR = -SkRegion::kRunTypeSentinel; michael@0: michael@0: while (runs[0] < SkRegion::kRunTypeSentinel) { michael@0: SkASSERT(prevR < runs[0]); michael@0: SkASSERT(runs[0] < runs[1]); michael@0: SkASSERT(runs[1] < SkRegion::kRunTypeSentinel); michael@0: prevR = runs[1]; michael@0: runs += 2; michael@0: } michael@0: return runs; michael@0: } michael@0: michael@0: static void compute_bounds(const SkRegion::RunType runs[], michael@0: SkIRect* bounds, int* ySpanCountPtr, michael@0: int* intervalCountPtr) { michael@0: assert_sentinel(runs[0], false); // top michael@0: michael@0: int left = SK_MaxS32; michael@0: int rite = SK_MinS32; michael@0: int bot; michael@0: int ySpanCount = 0; michael@0: int intervalCount = 0; michael@0: michael@0: bounds->fTop = *runs++; michael@0: do { michael@0: bot = *runs++; michael@0: SkASSERT(SkRegion::kRunTypeSentinel > bot); michael@0: michael@0: ySpanCount += 1; michael@0: michael@0: runs += 1; // skip intervalCount for now michael@0: if (*runs < SkRegion::kRunTypeSentinel) { michael@0: if (left > *runs) { michael@0: left = *runs; michael@0: } michael@0: michael@0: const SkRegion::RunType* prev = runs; michael@0: runs = skip_intervals_slow(runs); michael@0: int intervals = (runs - prev) >> 1; michael@0: SkASSERT(prev[-1] == intervals); michael@0: intervalCount += intervals; michael@0: michael@0: if (rite < runs[-1]) { michael@0: rite = runs[-1]; michael@0: } michael@0: } else { michael@0: SkASSERT(0 == runs[-1]); // no intervals michael@0: } michael@0: SkASSERT(SkRegion::kRunTypeSentinel == *runs); michael@0: runs += 1; michael@0: } while (SkRegion::kRunTypeSentinel != *runs); michael@0: michael@0: bounds->fLeft = left; michael@0: bounds->fRight = rite; michael@0: bounds->fBottom = bot; michael@0: *ySpanCountPtr = ySpanCount; michael@0: *intervalCountPtr = intervalCount; michael@0: } michael@0: michael@0: void SkRegion::validate() const { michael@0: if (this->isEmpty()) { michael@0: // check for explicit empty (the zero rect), so we can compare rects to know when michael@0: // two regions are equal (i.e. emptyRectA == emptyRectB) michael@0: // this is stricter than just asserting fBounds.isEmpty() michael@0: SkASSERT(fBounds.fLeft == 0 && fBounds.fTop == 0 && fBounds.fRight == 0 && fBounds.fBottom == 0); michael@0: } else { michael@0: SkASSERT(!fBounds.isEmpty()); michael@0: if (!this->isRect()) { michael@0: SkASSERT(fRunHead->fRefCnt >= 1); michael@0: SkASSERT(fRunHead->fRunCount > kRectRegionRuns); michael@0: michael@0: const RunType* run = fRunHead->readonly_runs(); michael@0: michael@0: // check that our bounds match our runs michael@0: { michael@0: SkIRect bounds; michael@0: int ySpanCount, intervalCount; michael@0: compute_bounds(run, &bounds, &ySpanCount, &intervalCount); michael@0: michael@0: SkASSERT(bounds == fBounds); michael@0: SkASSERT(ySpanCount > 0); michael@0: SkASSERT(fRunHead->getYSpanCount() == ySpanCount); michael@0: // SkASSERT(intervalCount > 1); michael@0: SkASSERT(fRunHead->getIntervalCount() == intervalCount); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkRegion::dump() const { michael@0: if (this->isEmpty()) { michael@0: SkDebugf(" rgn: empty\n"); michael@0: } else { michael@0: SkDebugf(" rgn: [%d %d %d %d]", fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom); michael@0: if (this->isComplex()) { michael@0: const RunType* runs = fRunHead->readonly_runs(); michael@0: for (int i = 0; i < fRunHead->fRunCount; i++) michael@0: SkDebugf(" %d", runs[i]); michael@0: } michael@0: SkDebugf("\n"); michael@0: } michael@0: } michael@0: michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkRegion::Iterator::Iterator(const SkRegion& rgn) { michael@0: this->reset(rgn); michael@0: } michael@0: michael@0: bool SkRegion::Iterator::rewind() { michael@0: if (fRgn) { michael@0: this->reset(*fRgn); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void SkRegion::Iterator::reset(const SkRegion& rgn) { michael@0: fRgn = &rgn; michael@0: if (rgn.isEmpty()) { michael@0: fDone = true; michael@0: } else { michael@0: fDone = false; michael@0: if (rgn.isRect()) { michael@0: fRect = rgn.fBounds; michael@0: fRuns = NULL; michael@0: } else { michael@0: fRuns = rgn.fRunHead->readonly_runs(); michael@0: fRect.set(fRuns[3], fRuns[0], fRuns[4], fRuns[1]); michael@0: fRuns += 5; michael@0: // Now fRuns points to the 2nd interval (or x-sentinel) michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkRegion::Iterator::next() { michael@0: if (fDone) { michael@0: return; michael@0: } michael@0: michael@0: if (fRuns == NULL) { // rect case michael@0: fDone = true; michael@0: return; michael@0: } michael@0: michael@0: const RunType* runs = fRuns; michael@0: michael@0: if (runs[0] < kRunTypeSentinel) { // valid X value michael@0: fRect.fLeft = runs[0]; michael@0: fRect.fRight = runs[1]; michael@0: runs += 2; michael@0: } else { // we're at the end of a line michael@0: runs += 1; michael@0: if (runs[0] < kRunTypeSentinel) { // valid Y value michael@0: int intervals = runs[1]; michael@0: if (0 == intervals) { // empty line michael@0: fRect.fTop = runs[0]; michael@0: runs += 3; michael@0: } else { michael@0: fRect.fTop = fRect.fBottom; michael@0: } michael@0: michael@0: fRect.fBottom = runs[0]; michael@0: assert_sentinel(runs[2], false); michael@0: assert_sentinel(runs[3], false); michael@0: fRect.fLeft = runs[2]; michael@0: fRect.fRight = runs[3]; michael@0: runs += 4; michael@0: } else { // end of rgn michael@0: fDone = true; michael@0: } michael@0: } michael@0: fRuns = runs; michael@0: } michael@0: michael@0: SkRegion::Cliperator::Cliperator(const SkRegion& rgn, const SkIRect& clip) michael@0: : fIter(rgn), fClip(clip), fDone(true) { michael@0: const SkIRect& r = fIter.rect(); michael@0: michael@0: while (!fIter.done()) { michael@0: if (r.fTop >= clip.fBottom) { michael@0: break; michael@0: } michael@0: if (fRect.intersect(clip, r)) { michael@0: fDone = false; michael@0: break; michael@0: } michael@0: fIter.next(); michael@0: } michael@0: } michael@0: michael@0: void SkRegion::Cliperator::next() { michael@0: if (fDone) { michael@0: return; michael@0: } michael@0: michael@0: const SkIRect& r = fIter.rect(); michael@0: michael@0: fDone = true; michael@0: fIter.next(); michael@0: while (!fIter.done()) { michael@0: if (r.fTop >= fClip.fBottom) { michael@0: break; michael@0: } michael@0: if (fRect.intersect(fClip, r)) { michael@0: fDone = false; michael@0: break; michael@0: } michael@0: fIter.next(); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkRegion::Spanerator::Spanerator(const SkRegion& rgn, int y, int left, michael@0: int right) { michael@0: SkDEBUGCODE(rgn.validate();) michael@0: michael@0: const SkIRect& r = rgn.getBounds(); michael@0: michael@0: fDone = true; michael@0: if (!rgn.isEmpty() && y >= r.fTop && y < r.fBottom && michael@0: right > r.fLeft && left < r.fRight) { michael@0: if (rgn.isRect()) { michael@0: if (left < r.fLeft) { michael@0: left = r.fLeft; michael@0: } michael@0: if (right > r.fRight) { michael@0: right = r.fRight; michael@0: } michael@0: fLeft = left; michael@0: fRight = right; michael@0: fRuns = NULL; // means we're a rect, not a rgn michael@0: fDone = false; michael@0: } else { michael@0: const SkRegion::RunType* runs = rgn.fRunHead->findScanline(y); michael@0: runs += 2; // skip Bottom and IntervalCount michael@0: for (;;) { michael@0: // runs[0..1] is to the right of the span, so we're done michael@0: if (runs[0] >= right) { michael@0: break; michael@0: } michael@0: // runs[0..1] is to the left of the span, so continue michael@0: if (runs[1] <= left) { michael@0: runs += 2; michael@0: continue; michael@0: } michael@0: // runs[0..1] intersects the span michael@0: fRuns = runs; michael@0: fLeft = left; michael@0: fRight = right; michael@0: fDone = false; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool SkRegion::Spanerator::next(int* left, int* right) { michael@0: if (fDone) { michael@0: return false; michael@0: } michael@0: michael@0: if (fRuns == NULL) { // we're a rect michael@0: fDone = true; // ok, now we're done michael@0: if (left) { michael@0: *left = fLeft; michael@0: } michael@0: if (right) { michael@0: *right = fRight; michael@0: } michael@0: return true; // this interval is legal michael@0: } michael@0: michael@0: const SkRegion::RunType* runs = fRuns; michael@0: michael@0: if (runs[0] >= fRight) { michael@0: fDone = true; michael@0: return false; michael@0: } michael@0: michael@0: SkASSERT(runs[1] > fLeft); michael@0: michael@0: if (left) { michael@0: *left = SkMax32(fLeft, runs[0]); michael@0: } michael@0: if (right) { michael@0: *right = SkMin32(fRight, runs[1]); michael@0: } michael@0: fRuns = runs + 2; michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifdef SK_DEBUG michael@0: michael@0: bool SkRegion::debugSetRuns(const RunType runs[], int count) { michael@0: // we need to make a copy, since the real method may modify the array, and michael@0: // so it cannot be const. michael@0: michael@0: SkAutoTArray storage(count); michael@0: memcpy(storage.get(), runs, count * sizeof(RunType)); michael@0: return this->setRuns(storage.get(), count); michael@0: } michael@0: michael@0: #endif