blob: 2d0559ca0d1ad666de8c14e7903e4aaf301841e4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkRegionPriv_DEFINED
9#define SkRegionPriv_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkRegion.h"
12#include "include/private/SkMalloc.h"
13#include "include/private/SkTo.h"
Mike Kleinde2244c2018-12-04 11:16:08 -050014#include <atomic>
Cary Clark69436892018-07-28 10:14:27 -040015#include <functional>
16
17class SkRegionPriv {
18public:
19 static constexpr int kRunTypeSentinel = 0x7FFFFFFF;
20 typedef SkRegion::RunType RunType;
21 typedef SkRegion::RunHead RunHead;
22
23 // Call the function with each span, in Y -> X ascending order.
24 // We pass a rect, but we will still ensure the span Y->X ordering, so often the height
25 // of the rect may be 1. It should never be empty.
26 static void VisitSpans(const SkRegion& rgn, const std::function<void(const SkIRect&)>&);
27
28#ifdef SK_DEBUG
29 static void Validate(const SkRegion& rgn);
30#endif
31};
32
33static constexpr int SkRegion_kRunTypeSentinel = 0x7FFFFFFF;
34
Hal Canary251bf3e2017-02-16 12:42:24 -050035inline bool SkRegionValueIsSentinel(int32_t value) {
Cary Clark69436892018-07-28 10:14:27 -040036 return value == (int32_t)SkRegion_kRunTypeSentinel;
Hal Canary251bf3e2017-02-16 12:42:24 -050037}
38
reed@android.com8a1c16f2008-12-17 15:59:43 +000039#define assert_sentinel(value, isSentinel) \
Hal Canary251bf3e2017-02-16 12:42:24 -050040 SkASSERT(SkRegionValueIsSentinel(value) == isSentinel)
reed@android.com8a1c16f2008-12-17 15:59:43 +000041
reed@google.com9c36a762012-05-02 18:07:33 +000042#ifdef SK_DEBUG
43// Given the first interval (just past the interval-count), compute the
44// interval count, by search for the x-sentinel
45//
Cary Clark69436892018-07-28 10:14:27 -040046static int compute_intervalcount(const SkRegionPriv::RunType runs[]) {
47 const SkRegionPriv::RunType* curr = runs;
48 while (*curr < SkRegion_kRunTypeSentinel) {
reed@google.com9c36a762012-05-02 18:07:33 +000049 SkASSERT(curr[0] < curr[1]);
Cary Clark69436892018-07-28 10:14:27 -040050 SkASSERT(curr[1] < SkRegion_kRunTypeSentinel);
reed@google.com9c36a762012-05-02 18:07:33 +000051 curr += 2;
52 }
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +000053 return SkToInt((curr - runs) >> 1);
reed@google.com9c36a762012-05-02 18:07:33 +000054}
55#endif
56
reed@android.com8a1c16f2008-12-17 15:59:43 +000057struct SkRegion::RunHead {
reed@google.com9c36a762012-05-02 18:07:33 +000058private:
59
60public:
Mike Reedf80989f2018-04-27 12:06:44 -040061 std::atomic<int32_t> fRefCnt;
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 int32_t fRunCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000063
reed@google.comaf7e6942012-05-01 14:43:22 +000064 /**
65 * Number of spans with different Y values. This does not count the initial
66 * Top value, nor does it count the final Y-Sentinel value. In the logical
67 * case of a rectangle, this would return 1, and an empty region would
68 * return 0.
69 */
70 int getYSpanCount() const {
71 return fYSpanCount;
72 }
73
74 /**
75 * Number of intervals in the entire region. This equals the number of
76 * rects that would be returned by the Iterator. In the logical case of
77 * a rect, this would return 1, and an empty region would return 0.
78 */
79 int getIntervalCount() const {
80 return fIntervalCount;
81 }
82
reed@google.coma95ea2c2012-04-30 16:35:25 +000083 static RunHead* Alloc(int count) {
Hal Canary251bf3e2017-02-16 12:42:24 -050084 if (count < SkRegion::kRectRegionRuns) {
85 return nullptr;
86 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000087
djsollen38fd5fe2015-05-18 13:05:11 -070088 const int64_t size = sk_64_mul(count, sizeof(RunType)) + sizeof(RunHead);
Herb Derbyc402b772017-09-20 11:56:00 -040089 if (count < 0 || !SkTFitsIn<int32_t>(size)) { SK_ABORT("Invalid Size"); }
djsollen38fd5fe2015-05-18 13:05:11 -070090
91 RunHead* head = (RunHead*)sk_malloc_throw(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 head->fRefCnt = 1;
93 head->fRunCount = count;
reed@google.comaf7e6942012-05-01 14:43:22 +000094 // these must be filled in later, otherwise we will be invalid
95 head->fYSpanCount = 0;
96 head->fIntervalCount = 0;
97 return head;
98 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000099
reed@google.comaf7e6942012-05-01 14:43:22 +0000100 static RunHead* Alloc(int count, int yspancount, int intervalCount) {
Hal Canary251bf3e2017-02-16 12:42:24 -0500101 if (yspancount <= 0 || intervalCount <= 1) {
102 return nullptr;
103 }
reed@google.comaf7e6942012-05-01 14:43:22 +0000104
105 RunHead* head = Alloc(count);
Hal Canary251bf3e2017-02-16 12:42:24 -0500106 if (!head) {
107 return nullptr;
108 }
reed@google.comaf7e6942012-05-01 14:43:22 +0000109 head->fYSpanCount = yspancount;
110 head->fIntervalCount = intervalCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111 return head;
112 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000113
reed@google.coma95ea2c2012-04-30 16:35:25 +0000114 SkRegion::RunType* writable_runs() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000115 SkASSERT(fRefCnt == 1);
116 return (SkRegion::RunType*)(this + 1);
117 }
reed@google.coma95ea2c2012-04-30 16:35:25 +0000118
119 const SkRegion::RunType* readonly_runs() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 return (const SkRegion::RunType*)(this + 1);
121 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000122
reed@google.coma95ea2c2012-04-30 16:35:25 +0000123 RunHead* ensureWritable() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124 RunHead* writable = this;
reed@google.coma95ea2c2012-04-30 16:35:25 +0000125 if (fRefCnt > 1) {
Mike Kleinde2244c2018-12-04 11:16:08 -0500126 // We need to alloc & copy the current region before decrease
127 // the refcount because it could be freed in the meantime.
reed@google.comaf7e6942012-05-01 14:43:22 +0000128 writable = Alloc(fRunCount, fYSpanCount, fIntervalCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 memcpy(writable->writable_runs(), this->readonly_runs(),
130 fRunCount * sizeof(RunType));
131
132 // fRefCount might have changed since we last checked.
133 // If we own the last reference at this point, we need to
134 // free the memory.
Mike Reedf80989f2018-04-27 12:06:44 -0400135 if (--fRefCnt == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 sk_free(this);
137 }
138 }
139 return writable;
140 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000141
reed@google.com9c36a762012-05-02 18:07:33 +0000142 /**
143 * Given a scanline (including its Bottom value at runs[0]), return the next
144 * scanline. Asserts that there is one (i.e. runs[0] < Sentinel)
145 */
146 static SkRegion::RunType* SkipEntireScanline(const SkRegion::RunType runs[]) {
147 // we are not the Y Sentinel
Cary Clark69436892018-07-28 10:14:27 -0400148 SkASSERT(runs[0] < SkRegion_kRunTypeSentinel);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000149
reed@google.com9c36a762012-05-02 18:07:33 +0000150 const int intervals = runs[1];
Cary Clark69436892018-07-28 10:14:27 -0400151 SkASSERT(runs[2 + intervals * 2] == SkRegion_kRunTypeSentinel);
reed@google.com9c36a762012-05-02 18:07:33 +0000152#ifdef SK_DEBUG
153 {
154 int n = compute_intervalcount(&runs[2]);
155 SkASSERT(n == intervals);
156 }
157#endif
158
159 // skip the entire line [B N [L R] S]
160 runs += 1 + 1 + intervals * 2 + 1;
161 return const_cast<SkRegion::RunType*>(runs);
162 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000163
164
reed@google.com9c36a762012-05-02 18:07:33 +0000165 /**
166 * Return the scanline that contains the Y value. This requires that the Y
167 * value is already known to be contained within the bounds of the region,
halcanary96fcdcc2015-08-27 07:41:13 -0700168 * and so this routine never returns nullptr.
reed@google.com9c36a762012-05-02 18:07:33 +0000169 *
170 * It returns the beginning of the scanline, starting with its Bottom value.
171 */
172 SkRegion::RunType* findScanline(int y) const {
173 const RunType* runs = this->readonly_runs();
174
175 // if the top-check fails, we didn't do a quick check on the bounds
176 SkASSERT(y >= runs[0]);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000177
reed@google.com9c36a762012-05-02 18:07:33 +0000178 runs += 1; // skip top-Y
179 for (;;) {
180 int bottom = runs[0];
181 // If we hit this, we've walked off the region, and our bounds check
182 // failed.
Cary Clark69436892018-07-28 10:14:27 -0400183 SkASSERT(bottom < SkRegion_kRunTypeSentinel);
reed@google.com9c36a762012-05-02 18:07:33 +0000184 if (y < bottom) {
185 break;
186 }
187 runs = SkipEntireScanline(runs);
188 }
189 return const_cast<SkRegion::RunType*>(runs);
190 }
191
192 // Copy src runs into us, computing interval counts and bounds along the way
193 void computeRunBounds(SkIRect* bounds) {
194 RunType* runs = this->writable_runs();
195 bounds->fTop = *runs++;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000196
reed@google.com9c36a762012-05-02 18:07:33 +0000197 int bot;
198 int ySpanCount = 0;
199 int intervalCount = 0;
200 int left = SK_MaxS32;
201 int rite = SK_MinS32;
202
203 do {
204 bot = *runs++;
Cary Clark69436892018-07-28 10:14:27 -0400205 SkASSERT(bot < SkRegion_kRunTypeSentinel);
reed@google.com9c36a762012-05-02 18:07:33 +0000206 ySpanCount += 1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000207
reed@google.com9c36a762012-05-02 18:07:33 +0000208 const int intervals = *runs++;
209 SkASSERT(intervals >= 0);
Cary Clark69436892018-07-28 10:14:27 -0400210 SkASSERT(intervals < SkRegion_kRunTypeSentinel);
reed@google.com9c36a762012-05-02 18:07:33 +0000211
212 if (intervals > 0) {
213#ifdef SK_DEBUG
214 {
215 int n = compute_intervalcount(runs);
216 SkASSERT(n == intervals);
217 }
218#endif
219 RunType L = runs[0];
Cary Clark69436892018-07-28 10:14:27 -0400220 SkASSERT(L < SkRegion_kRunTypeSentinel);
reed@google.com9c36a762012-05-02 18:07:33 +0000221 if (left > L) {
222 left = L;
223 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000224
reed@google.com9c36a762012-05-02 18:07:33 +0000225 runs += intervals * 2;
226 RunType R = runs[-1];
Cary Clark69436892018-07-28 10:14:27 -0400227 SkASSERT(R < SkRegion_kRunTypeSentinel);
reed@google.com9c36a762012-05-02 18:07:33 +0000228 if (rite < R) {
229 rite = R;
230 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000231
reed@google.com9c36a762012-05-02 18:07:33 +0000232 intervalCount += intervals;
233 }
Cary Clark69436892018-07-28 10:14:27 -0400234 SkASSERT(SkRegion_kRunTypeSentinel == *runs);
reed@google.com9c36a762012-05-02 18:07:33 +0000235 runs += 1; // skip x-sentinel
236
237 // test Y-sentinel
Cary Clark69436892018-07-28 10:14:27 -0400238 } while (SkRegion_kRunTypeSentinel > *runs);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000239
reed@google.com9c36a762012-05-02 18:07:33 +0000240#ifdef SK_DEBUG
241 // +1 to skip the last Y-sentinel
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +0000242 int runCount = SkToInt(runs - this->writable_runs() + 1);
reed@google.com9c36a762012-05-02 18:07:33 +0000243 SkASSERT(runCount == fRunCount);
244#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000245
reed@google.com9c36a762012-05-02 18:07:33 +0000246 fYSpanCount = ySpanCount;
247 fIntervalCount = intervalCount;
248
249 bounds->fLeft = left;
250 bounds->fRight = rite;
251 bounds->fBottom = bot;
252 }
reed@google.comaf7e6942012-05-01 14:43:22 +0000253
254private:
255 int32_t fYSpanCount;
256 int32_t fIntervalCount;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000257};
258
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259#endif