2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#ifndef SkRegionPriv_DEFINED
|
|
|
|
#define SkRegionPriv_DEFINED
|
|
|
|
|
|
|
|
#include "SkRegion.h"
|
2015-09-29 16:37:59 +00:00
|
|
|
#include "SkAtomics.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#define assert_sentinel(value, isSentinel) \
|
|
|
|
SkASSERT(((value) == SkRegion::kRunTypeSentinel) == isSentinel)
|
|
|
|
|
|
|
|
//SkDEBUGCODE(extern int32_t gRgnAllocCounter;)
|
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
// Given the first interval (just past the interval-count), compute the
|
|
|
|
// interval count, by search for the x-sentinel
|
|
|
|
//
|
|
|
|
static int compute_intervalcount(const SkRegion::RunType runs[]) {
|
|
|
|
const SkRegion::RunType* curr = runs;
|
|
|
|
while (*curr < SkRegion::kRunTypeSentinel) {
|
|
|
|
SkASSERT(curr[0] < curr[1]);
|
|
|
|
SkASSERT(curr[1] < SkRegion::kRunTypeSentinel);
|
|
|
|
curr += 2;
|
|
|
|
}
|
2014-04-23 19:19:44 +00:00
|
|
|
return SkToInt((curr - runs) >> 1);
|
2012-05-02 18:07:33 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
struct SkRegion::RunHead {
|
2012-05-02 18:07:33 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
2008-12-17 15:59:43 +00:00
|
|
|
int32_t fRefCnt;
|
|
|
|
int32_t fRunCount;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-01 14:43:22 +00:00
|
|
|
/**
|
|
|
|
* Number of spans with different Y values. This does not count the initial
|
|
|
|
* Top value, nor does it count the final Y-Sentinel value. In the logical
|
|
|
|
* case of a rectangle, this would return 1, and an empty region would
|
|
|
|
* return 0.
|
|
|
|
*/
|
|
|
|
int getYSpanCount() const {
|
|
|
|
return fYSpanCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of intervals in the entire region. This equals the number of
|
|
|
|
* rects that would be returned by the Iterator. In the logical case of
|
|
|
|
* a rect, this would return 1, and an empty region would return 0.
|
|
|
|
*/
|
|
|
|
int getIntervalCount() const {
|
|
|
|
return fIntervalCount;
|
|
|
|
}
|
|
|
|
|
2012-04-30 16:35:25 +00:00
|
|
|
static RunHead* Alloc(int count) {
|
2008-12-17 15:59:43 +00:00
|
|
|
//SkDEBUGCODE(sk_atomic_inc(&gRgnAllocCounter);)
|
|
|
|
//SkDEBUGF(("************** gRgnAllocCounter::alloc %d\n", gRgnAllocCounter));
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(count >= SkRegion::kRectRegionRuns);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2015-05-18 20:05:11 +00:00
|
|
|
const int64_t size = sk_64_mul(count, sizeof(RunType)) + sizeof(RunHead);
|
2016-01-29 16:51:04 +00:00
|
|
|
if (count < 0 || !sk_64_isS32(size)) { SK_ABORT("Invalid Size"); }
|
2015-05-18 20:05:11 +00:00
|
|
|
|
|
|
|
RunHead* head = (RunHead*)sk_malloc_throw(size);
|
2008-12-17 15:59:43 +00:00
|
|
|
head->fRefCnt = 1;
|
|
|
|
head->fRunCount = count;
|
2012-05-01 14:43:22 +00:00
|
|
|
// these must be filled in later, otherwise we will be invalid
|
|
|
|
head->fYSpanCount = 0;
|
|
|
|
head->fIntervalCount = 0;
|
|
|
|
return head;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-01 14:43:22 +00:00
|
|
|
static RunHead* Alloc(int count, int yspancount, int intervalCount) {
|
|
|
|
SkASSERT(yspancount > 0);
|
|
|
|
SkASSERT(intervalCount > 1);
|
|
|
|
|
|
|
|
RunHead* head = Alloc(count);
|
|
|
|
head->fYSpanCount = yspancount;
|
|
|
|
head->fIntervalCount = intervalCount;
|
2008-12-17 15:59:43 +00:00
|
|
|
return head;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-04-30 16:35:25 +00:00
|
|
|
SkRegion::RunType* writable_runs() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(fRefCnt == 1);
|
|
|
|
return (SkRegion::RunType*)(this + 1);
|
|
|
|
}
|
2012-04-30 16:35:25 +00:00
|
|
|
|
|
|
|
const SkRegion::RunType* readonly_runs() const {
|
2008-12-17 15:59:43 +00:00
|
|
|
return (const SkRegion::RunType*)(this + 1);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-04-30 16:35:25 +00:00
|
|
|
RunHead* ensureWritable() {
|
2008-12-17 15:59:43 +00:00
|
|
|
RunHead* writable = this;
|
2012-04-30 16:35:25 +00:00
|
|
|
if (fRefCnt > 1) {
|
2008-12-17 15:59:43 +00:00
|
|
|
// We need to alloc & copy the current region before we call
|
|
|
|
// sk_atomic_dec because it could be freed in the meantime,
|
2012-08-23 18:09:54 +00:00
|
|
|
// otherwise.
|
2012-05-01 14:43:22 +00:00
|
|
|
writable = Alloc(fRunCount, fYSpanCount, fIntervalCount);
|
2008-12-17 15:59:43 +00:00
|
|
|
memcpy(writable->writable_runs(), this->readonly_runs(),
|
|
|
|
fRunCount * sizeof(RunType));
|
|
|
|
|
|
|
|
// fRefCount might have changed since we last checked.
|
|
|
|
// If we own the last reference at this point, we need to
|
|
|
|
// free the memory.
|
2012-04-30 16:35:25 +00:00
|
|
|
if (sk_atomic_dec(&fRefCnt) == 1) {
|
2008-12-17 15:59:43 +00:00
|
|
|
sk_free(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return writable;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
/**
|
|
|
|
* Given a scanline (including its Bottom value at runs[0]), return the next
|
|
|
|
* scanline. Asserts that there is one (i.e. runs[0] < Sentinel)
|
|
|
|
*/
|
|
|
|
static SkRegion::RunType* SkipEntireScanline(const SkRegion::RunType runs[]) {
|
|
|
|
// we are not the Y Sentinel
|
|
|
|
SkASSERT(runs[0] < SkRegion::kRunTypeSentinel);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
const int intervals = runs[1];
|
|
|
|
SkASSERT(runs[2 + intervals * 2] == SkRegion::kRunTypeSentinel);
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
{
|
|
|
|
int n = compute_intervalcount(&runs[2]);
|
|
|
|
SkASSERT(n == intervals);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// skip the entire line [B N [L R] S]
|
|
|
|
runs += 1 + 1 + intervals * 2 + 1;
|
|
|
|
return const_cast<SkRegion::RunType*>(runs);
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
/**
|
|
|
|
* Return the scanline that contains the Y value. This requires that the Y
|
|
|
|
* value is already known to be contained within the bounds of the region,
|
2015-08-27 14:41:13 +00:00
|
|
|
* and so this routine never returns nullptr.
|
2012-05-02 18:07:33 +00:00
|
|
|
*
|
|
|
|
* It returns the beginning of the scanline, starting with its Bottom value.
|
|
|
|
*/
|
|
|
|
SkRegion::RunType* findScanline(int y) const {
|
|
|
|
const RunType* runs = this->readonly_runs();
|
|
|
|
|
|
|
|
// if the top-check fails, we didn't do a quick check on the bounds
|
|
|
|
SkASSERT(y >= runs[0]);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
runs += 1; // skip top-Y
|
|
|
|
for (;;) {
|
|
|
|
int bottom = runs[0];
|
|
|
|
// If we hit this, we've walked off the region, and our bounds check
|
|
|
|
// failed.
|
|
|
|
SkASSERT(bottom < SkRegion::kRunTypeSentinel);
|
|
|
|
if (y < bottom) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
runs = SkipEntireScanline(runs);
|
|
|
|
}
|
|
|
|
return const_cast<SkRegion::RunType*>(runs);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy src runs into us, computing interval counts and bounds along the way
|
|
|
|
void computeRunBounds(SkIRect* bounds) {
|
|
|
|
RunType* runs = this->writable_runs();
|
|
|
|
bounds->fTop = *runs++;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
int bot;
|
|
|
|
int ySpanCount = 0;
|
|
|
|
int intervalCount = 0;
|
|
|
|
int left = SK_MaxS32;
|
|
|
|
int rite = SK_MinS32;
|
|
|
|
|
|
|
|
do {
|
|
|
|
bot = *runs++;
|
|
|
|
SkASSERT(bot < SkRegion::kRunTypeSentinel);
|
|
|
|
ySpanCount += 1;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
const int intervals = *runs++;
|
|
|
|
SkASSERT(intervals >= 0);
|
|
|
|
SkASSERT(intervals < SkRegion::kRunTypeSentinel);
|
|
|
|
|
|
|
|
if (intervals > 0) {
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
{
|
|
|
|
int n = compute_intervalcount(runs);
|
|
|
|
SkASSERT(n == intervals);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
RunType L = runs[0];
|
|
|
|
SkASSERT(L < SkRegion::kRunTypeSentinel);
|
|
|
|
if (left > L) {
|
|
|
|
left = L;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
runs += intervals * 2;
|
|
|
|
RunType R = runs[-1];
|
|
|
|
SkASSERT(R < SkRegion::kRunTypeSentinel);
|
|
|
|
if (rite < R) {
|
|
|
|
rite = R;
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
intervalCount += intervals;
|
|
|
|
}
|
|
|
|
SkASSERT(SkRegion::kRunTypeSentinel == *runs);
|
|
|
|
runs += 1; // skip x-sentinel
|
|
|
|
|
|
|
|
// test Y-sentinel
|
|
|
|
} while (SkRegion::kRunTypeSentinel > *runs);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
// +1 to skip the last Y-sentinel
|
2014-04-23 19:19:44 +00:00
|
|
|
int runCount = SkToInt(runs - this->writable_runs() + 1);
|
2012-05-02 18:07:33 +00:00
|
|
|
SkASSERT(runCount == fRunCount);
|
|
|
|
#endif
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-05-02 18:07:33 +00:00
|
|
|
fYSpanCount = ySpanCount;
|
|
|
|
fIntervalCount = intervalCount;
|
|
|
|
|
|
|
|
bounds->fLeft = left;
|
|
|
|
bounds->fRight = rite;
|
|
|
|
bounds->fBottom = bot;
|
|
|
|
}
|
2012-05-01 14:43:22 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t fYSpanCount;
|
|
|
|
int32_t fIntervalCount;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|