blob: 83ebecaa6779496a45d87039433cc09a15d31c0e [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Region"
18
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070019#include <inttypes.h>
Mathias Agopian72b0ffe2009-07-06 18:07:26 -070020#include <limits.h>
21
Yiwei Zhang5434a782018-12-05 18:06:32 -080022#include <android-base/stringprintf.h>
23
Mathias Agopian20f68782009-05-11 00:03:41 -070024#include <utils/Log.h>
Mathias Agopian20f68782009-05-11 00:03:41 -070025
Marissa Wall15375f92019-12-03 15:10:46 -080026#include <ui/Point.h>
Mathias Agopian20f68782009-05-11 00:03:41 -070027#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <ui/Region.h>
Marissa Wall15375f92019-12-03 15:10:46 -080029#include <ui/RegionHelper.h>
Mathias Agopian20f68782009-05-11 00:03:41 -070030
31// ----------------------------------------------------------------------------
Chong Zhang639a1e12019-04-22 14:01:20 -070032
33// ### VALIDATE_REGIONS ###
34// To enable VALIDATE_REGIONS traces, use the "libui-validate-regions-defaults"
35// in Android.bp. Do not #define VALIDATE_REGIONS here as it requires extra libs.
36
Mathias Agopian20f68782009-05-11 00:03:41 -070037#define VALIDATE_WITH_CORECG (false)
38// ----------------------------------------------------------------------------
39
Chong Zhang639a1e12019-04-22 14:01:20 -070040#if defined(VALIDATE_REGIONS)
41#include <utils/CallStack.h>
42#endif
43
Mathias Agopian20f68782009-05-11 00:03:41 -070044#if VALIDATE_WITH_CORECG
45#include <core/SkRegion.h>
46#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
48namespace android {
Mathias Agopian20f68782009-05-11 00:03:41 -070049// ----------------------------------------------------------------------------
50
Yiwei Zhang5434a782018-12-05 18:06:32 -080051using base::StringAppendF;
52
Mathias Agopian20f68782009-05-11 00:03:41 -070053enum {
54 op_nand = region_operator<Rect>::op_nand,
55 op_and = region_operator<Rect>::op_and,
56 op_or = region_operator<Rect>::op_or,
57 op_xor = region_operator<Rect>::op_xor
58};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Chris Craik3e010f32013-02-25 19:12:47 -080060enum {
61 direction_LTR,
62 direction_RTL
63};
64
Dan Stoza5065a552015-03-17 16:23:42 -070065const Region Region::INVALID_REGION(Rect::INVALID_RECT);
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067// ----------------------------------------------------------------------------
68
Mathias Agopian3ab68552012-08-31 14:31:40 -070069Region::Region() {
70 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071}
72
73Region::Region(const Region& rhs)
Mathias Agopian3ab68552012-08-31 14:31:40 -070074 : mStorage(rhs.mStorage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075{
Chong Zhang639a1e12019-04-22 14:01:20 -070076#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -070077 validate(rhs, "rhs copy-ctor");
78#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079}
80
Mathias Agopian3ab68552012-08-31 14:31:40 -070081Region::Region(const Rect& rhs) {
82 mStorage.add(rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083}
84
85Region::~Region()
86{
87}
88
Chris Craik3e010f32013-02-25 19:12:47 -080089/**
90 * Copy rects from the src vector into the dst vector, resolving vertical T-Junctions along the way
91 *
92 * First pass through, divideSpanRTL will be set because the 'previous span' (indexing into the dst
93 * vector) will be reversed. Each rectangle in the original list, starting from the bottom, will be
94 * compared with the span directly below, and subdivided as needed to resolve T-junctions.
95 *
96 * The resulting temporary vector will be a completely reversed copy of the original, without any
97 * bottom-up T-junctions.
98 *
99 * Second pass through, divideSpanRTL will be false since the previous span will index into the
100 * final, correctly ordered region buffer. Each rectangle will be compared with the span directly
101 * above it, and subdivided to resolve any remaining T-junctions.
102 */
103static void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end,
104 Vector<Rect>& dst, int spanDirection) {
105 dst.clear();
106
107 const Rect* current = end - 1;
108 int lastTop = current->top;
109
110 // add first span immediately
111 do {
112 dst.add(*current);
113 current--;
114 } while (current->top == lastTop && current >= begin);
115
Dan Stozad3182402014-11-17 12:03:59 -0800116 int beginLastSpan = -1;
117 int endLastSpan = -1;
Chris Craik3e010f32013-02-25 19:12:47 -0800118 int top = -1;
119 int bottom = -1;
120
121 // for all other spans, split if a t-junction exists in the span directly above
122 while (current >= begin) {
123 if (current->top != (current + 1)->top) {
124 // new span
125 if ((spanDirection == direction_RTL && current->bottom != (current + 1)->top) ||
126 (spanDirection == direction_LTR && current->top != (current + 1)->bottom)) {
127 // previous span not directly adjacent, don't check for T junctions
128 beginLastSpan = INT_MAX;
129 } else {
130 beginLastSpan = endLastSpan + 1;
131 }
Dan Stozad3182402014-11-17 12:03:59 -0800132 endLastSpan = static_cast<int>(dst.size()) - 1;
Chris Craik3e010f32013-02-25 19:12:47 -0800133
134 top = current->top;
135 bottom = current->bottom;
136 }
137 int left = current->left;
138 int right = current->right;
139
Dan Stozad3182402014-11-17 12:03:59 -0800140 for (int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) {
141 // prevIndex can't be -1 here because if endLastSpan is set to a
142 // value greater than -1 (allowing the loop to execute),
143 // beginLastSpan (and therefore prevIndex) will also be increased
ywenaef04452015-03-26 19:51:12 +0800144 const Rect prev = dst[static_cast<size_t>(prevIndex)];
Chris Craik3e010f32013-02-25 19:12:47 -0800145 if (spanDirection == direction_RTL) {
146 // iterating over previous span RTL, quit if it's too far left
ywenaef04452015-03-26 19:51:12 +0800147 if (prev.right <= left) break;
Chris Craik3e010f32013-02-25 19:12:47 -0800148
ywenaef04452015-03-26 19:51:12 +0800149 if (prev.right > left && prev.right < right) {
150 dst.add(Rect(prev.right, top, right, bottom));
151 right = prev.right;
Chris Craik3e010f32013-02-25 19:12:47 -0800152 }
153
ywenaef04452015-03-26 19:51:12 +0800154 if (prev.left > left && prev.left < right) {
155 dst.add(Rect(prev.left, top, right, bottom));
156 right = prev.left;
Chris Craik3e010f32013-02-25 19:12:47 -0800157 }
158
159 // if an entry in the previous span is too far right, nothing further left in the
160 // current span will need it
ywenaef04452015-03-26 19:51:12 +0800161 if (prev.left >= right) {
Chris Craik3e010f32013-02-25 19:12:47 -0800162 beginLastSpan = prevIndex;
163 }
164 } else {
165 // iterating over previous span LTR, quit if it's too far right
ywenaef04452015-03-26 19:51:12 +0800166 if (prev.left >= right) break;
Chris Craik3e010f32013-02-25 19:12:47 -0800167
ywenaef04452015-03-26 19:51:12 +0800168 if (prev.left > left && prev.left < right) {
169 dst.add(Rect(left, top, prev.left, bottom));
170 left = prev.left;
Chris Craik3e010f32013-02-25 19:12:47 -0800171 }
172
ywenaef04452015-03-26 19:51:12 +0800173 if (prev.right > left && prev.right < right) {
174 dst.add(Rect(left, top, prev.right, bottom));
175 left = prev.right;
Chris Craik3e010f32013-02-25 19:12:47 -0800176 }
177 // if an entry in the previous span is too far left, nothing further right in the
178 // current span will need it
ywenaef04452015-03-26 19:51:12 +0800179 if (prev.right <= left) {
Chris Craik3e010f32013-02-25 19:12:47 -0800180 beginLastSpan = prevIndex;
181 }
182 }
183 }
184
185 if (left < right) {
186 dst.add(Rect(left, top, right, bottom));
187 }
188
189 current--;
190 }
191}
192
193/**
194 * Creates a new region with the same data as the argument, but divides rectangles as necessary to
195 * remove T-Junctions
196 *
197 * Note: the output will not necessarily be a very efficient representation of the region, since it
198 * may be that a triangle-based approach would generate significantly simpler geometry
199 */
200Region Region::createTJunctionFreeRegion(const Region& r) {
201 if (r.isEmpty()) return r;
202 if (r.isRect()) return r;
203
204 Vector<Rect> reversed;
205 reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL);
206
207 Region outputRegion;
208 reverseRectsResolvingJunctions(reversed.begin(), reversed.end(),
209 outputRegion.mStorage, direction_LTR);
210 outputRegion.mStorage.add(r.getBounds()); // to make region valid, mStorage must end with bounds
211
Chong Zhang639a1e12019-04-22 14:01:20 -0700212#if defined(VALIDATE_REGIONS)
Chris Craik3e010f32013-02-25 19:12:47 -0800213 validate(outputRegion, "T-Junction free region");
214#endif
215
216 return outputRegion;
217}
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219Region& Region::operator = (const Region& rhs)
220{
Chong Zhang639a1e12019-04-22 14:01:20 -0700221#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700222 validate(*this, "this->operator=");
223 validate(rhs, "rhs.operator=");
Mathias Agopian20f68782009-05-11 00:03:41 -0700224#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700225 mStorage = rhs.mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 return *this;
227}
228
Mathias Agopian9f961452009-06-29 18:46:37 -0700229Region& Region::makeBoundsSelf()
230{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700231 if (mStorage.size() >= 2) {
232 const Rect bounds(getBounds());
233 mStorage.clear();
234 mStorage.add(bounds);
235 }
Mathias Agopian9f961452009-06-29 18:46:37 -0700236 return *this;
237}
238
Michael Wright1c284a92014-02-10 13:00:14 -0800239bool Region::contains(const Point& point) const {
240 return contains(point.x, point.y);
241}
242
243bool Region::contains(int x, int y) const {
244 const_iterator cur = begin();
245 const_iterator const tail = end();
246 while (cur != tail) {
247 if (y >= cur->top && y < cur->bottom && x >= cur->left && x < cur->right) {
248 return true;
249 }
250 cur++;
251 }
252 return false;
253}
254
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255void Region::clear()
256{
Mathias Agopian20f68782009-05-11 00:03:41 -0700257 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700258 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259}
260
261void Region::set(const Rect& r)
262{
Mathias Agopian20f68782009-05-11 00:03:41 -0700263 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700264 mStorage.add(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265}
266
Dan Stozad3182402014-11-17 12:03:59 -0800267void Region::set(int32_t w, int32_t h)
Mathias Agopian0926f502009-05-04 14:17:04 -0700268{
Mathias Agopian20f68782009-05-11 00:03:41 -0700269 mStorage.clear();
Dan Stozad3182402014-11-17 12:03:59 -0800270 mStorage.add(Rect(w, h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700271}
272
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +0100273void Region::set(uint32_t w, uint32_t h)
274{
275 mStorage.clear();
276 mStorage.add(Rect(w, h));
277}
278
Mathias Agopian2ca79392013-04-02 18:30:32 -0700279bool Region::isTriviallyEqual(const Region& region) const {
280 return begin() == region.begin();
281}
282
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283// ----------------------------------------------------------------------------
284
Mathias Agopian20f68782009-05-11 00:03:41 -0700285void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700287 Rect rect(l,t,r,b);
288 size_t where = mStorage.size() - 1;
289 mStorage.insertAt(rect, where, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800290}
291
Mathias Agopian20f68782009-05-11 00:03:41 -0700292// ----------------------------------------------------------------------------
293
294Region& Region::orSelf(const Rect& r) {
295 return operationSelf(r, op_or);
296}
Romain Guyb8a2e982012-02-07 17:04:34 -0800297Region& Region::xorSelf(const Rect& r) {
298 return operationSelf(r, op_xor);
299}
Mathias Agopian20f68782009-05-11 00:03:41 -0700300Region& Region::andSelf(const Rect& r) {
301 return operationSelf(r, op_and);
302}
303Region& Region::subtractSelf(const Rect& r) {
304 return operationSelf(r, op_nand);
305}
Colin Cross8f279962016-09-26 13:08:16 -0700306Region& Region::operationSelf(const Rect& r, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700307 Region lhs(*this);
308 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309 return *this;
310}
311
312// ----------------------------------------------------------------------------
313
314Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700315 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316}
Romain Guyb8a2e982012-02-07 17:04:34 -0800317Region& Region::xorSelf(const Region& rhs) {
318 return operationSelf(rhs, op_xor);
319}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700321 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800322}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700324 return operationSelf(rhs, op_nand);
325}
Colin Cross8f279962016-09-26 13:08:16 -0700326Region& Region::operationSelf(const Region& rhs, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700327 Region lhs(*this);
328 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 return *this;
330}
331
332Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700333 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 return *this;
335}
336
Riddle Hsu39d4aa52018-11-30 20:46:53 +0800337Region& Region::scaleSelf(float sx, float sy) {
Robert Carre07e1032018-11-26 12:55:53 -0800338 size_t count = mStorage.size();
339 Rect* rects = mStorage.editArray();
340 while (count) {
Nick Desaulniersea6c7132019-10-15 19:14:39 -0700341 rects->left = static_cast<int32_t>(static_cast<float>(rects->left) * sx + 0.5f);
342 rects->right = static_cast<int32_t>(static_cast<float>(rects->right) * sx + 0.5f);
343 rects->top = static_cast<int32_t>(static_cast<float>(rects->top) * sy + 0.5f);
344 rects->bottom = static_cast<int32_t>(static_cast<float>(rects->bottom) * sy + 0.5f);
Robert Carre07e1032018-11-26 12:55:53 -0800345 rects++;
346 count--;
347 }
348 return *this;
349}
350
Mathias Agopian20f68782009-05-11 00:03:41 -0700351// ----------------------------------------------------------------------------
352
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700353const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700354 return operation(rhs, op_or);
355}
Romain Guyb8a2e982012-02-07 17:04:34 -0800356const Region Region::mergeExclusive(const Rect& rhs) const {
357 return operation(rhs, op_xor);
358}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700359const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700360 return operation(rhs, op_and);
361}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700362const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700363 return operation(rhs, op_nand);
364}
Colin Cross8f279962016-09-26 13:08:16 -0700365const Region Region::operation(const Rect& rhs, uint32_t op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700366 Region result;
367 boolean_operation(op, result, *this, rhs);
368 return result;
369}
370
371// ----------------------------------------------------------------------------
372
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700373const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700374 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800375}
Romain Guyb8a2e982012-02-07 17:04:34 -0800376const Region Region::mergeExclusive(const Region& rhs) const {
377 return operation(rhs, op_xor);
378}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700379const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700380 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800381}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700382const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700383 return operation(rhs, op_nand);
384}
Colin Cross8f279962016-09-26 13:08:16 -0700385const Region Region::operation(const Region& rhs, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700387 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800388 return result;
389}
390
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700391const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700393 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800394 return result;
395}
396
397// ----------------------------------------------------------------------------
398
399Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700400 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800401}
Romain Guyb8a2e982012-02-07 17:04:34 -0800402Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
403 return operationSelf(rhs, dx, dy, op_xor);
404}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700406 return operationSelf(rhs, dx, dy, op_and);
407}
408Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
409 return operationSelf(rhs, dx, dy, op_nand);
410}
Colin Cross8f279962016-09-26 13:08:16 -0700411Region& Region::operationSelf(const Region& rhs, int dx, int dy, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700412 Region lhs(*this);
413 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800414 return *this;
415}
416
Mathias Agopian20f68782009-05-11 00:03:41 -0700417// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700419const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700420 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421}
Romain Guyb8a2e982012-02-07 17:04:34 -0800422const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
423 return operation(rhs, dx, dy, op_xor);
424}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700425const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700426 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800427}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700428const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700429 return operation(rhs, dx, dy, op_nand);
430}
Colin Cross8f279962016-09-26 13:08:16 -0700431const Region Region::operation(const Region& rhs, int dx, int dy, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800432 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700433 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434 return result;
435}
436
437// ----------------------------------------------------------------------------
438
Mathias Agopian20f68782009-05-11 00:03:41 -0700439// This is our region rasterizer, which merges rects and spans together
440// to obtain an optimal region.
Dan Stozad3182402014-11-17 12:03:59 -0800441class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700443 Rect bounds;
Mathias Agopian20f68782009-05-11 00:03:41 -0700444 Vector<Rect>& storage;
445 Rect* head;
446 Rect* tail;
447 Vector<Rect> span;
448 Rect* cur;
449public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700450 explicit rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700451 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700452 storage.clear();
453 }
454
Dan Stozad3182402014-11-17 12:03:59 -0800455 virtual ~rasterizer();
456
457 virtual void operator()(const Rect& rect);
458
Mathias Agopian20f68782009-05-11 00:03:41 -0700459private:
Dan Stozad3182402014-11-17 12:03:59 -0800460 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700461 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800462 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700463 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800464
465 void flushSpan();
Mathias Agopian20f68782009-05-11 00:03:41 -0700466};
467
Dan Stozad3182402014-11-17 12:03:59 -0800468Region::rasterizer::~rasterizer()
469{
470 if (span.size()) {
471 flushSpan();
472 }
473 if (storage.size()) {
474 bounds.top = storage.itemAt(0).top;
475 bounds.bottom = storage.top().bottom;
476 if (storage.size() == 1) {
477 storage.clear();
478 }
479 } else {
480 bounds.left = 0;
481 bounds.right = 0;
482 }
483 storage.add(bounds);
484}
485
486void Region::rasterizer::operator()(const Rect& rect)
487{
488 //ALOGD(">>> %3d, %3d, %3d, %3d",
489 // rect.left, rect.top, rect.right, rect.bottom);
490 if (span.size()) {
491 if (cur->top != rect.top) {
492 flushSpan();
493 } else if (cur->right == rect.left) {
494 cur->right = rect.right;
495 return;
496 }
497 }
498 span.add(rect);
499 cur = span.editArray() + (span.size() - 1);
500}
501
502void Region::rasterizer::flushSpan()
503{
504 bool merge = false;
505 if (tail-head == ssize_t(span.size())) {
506 Rect const* p = span.editArray();
507 Rect const* q = head;
508 if (p->top == q->bottom) {
509 merge = true;
510 while (q != tail) {
511 if ((p->left != q->left) || (p->right != q->right)) {
512 merge = false;
513 break;
514 }
Stephen Hines9c22c3c2016-03-31 22:02:38 -0700515 p++;
516 q++;
Dan Stozad3182402014-11-17 12:03:59 -0800517 }
518 }
519 }
520 if (merge) {
521 const int bottom = span[0].bottom;
522 Rect* r = head;
523 while (r != tail) {
524 r->bottom = bottom;
525 r++;
526 }
527 } else {
528 bounds.left = min(span.itemAt(0).left, bounds.left);
529 bounds.right = max(span.top().right, bounds.right);
530 storage.appendVector(span);
531 tail = storage.editArray() + storage.size();
532 head = tail - span.size();
533 }
534 span.clear();
535}
536
Mathias Agopian068d47f2012-09-11 18:56:23 -0700537bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700538{
Chia-I Wub420b582018-02-07 11:53:41 -0800539 if (reg.mStorage.isEmpty()) {
540 ALOGE_IF(!silent, "%s: mStorage is empty, which is never valid", name);
541 // return immediately as the code below assumes mStorage is non-empty
542 return false;
543 }
544
Mathias Agopian20f68782009-05-11 00:03:41 -0700545 bool result = true;
546 const_iterator cur = reg.begin();
547 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700548 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700549 Rect b(*prev);
550 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700551 if (cur->isValid() == false) {
Dan Stoza5065a552015-03-17 16:23:42 -0700552 // We allow this particular flavor of invalid Rect, since it is used
553 // as a signal value in various parts of the system
554 if (*cur != Rect::INVALID_RECT) {
555 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
556 result = false;
557 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700558 }
559 if (cur->right > region_operator<Rect>::max_value) {
560 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
561 result = false;
562 }
563 if (cur->bottom > region_operator<Rect>::max_value) {
564 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
565 result = false;
566 }
567 if (prev != cur) {
568 b.left = b.left < cur->left ? b.left : cur->left;
569 b.top = b.top < cur->top ? b.top : cur->top;
570 b.right = b.right > cur->right ? b.right : cur->right;
571 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
572 if ((*prev < *cur) == false) {
573 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700574 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700575 }
576 if (cur->top == prev->top) {
577 if (cur->bottom != prev->bottom) {
578 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
579 result = false;
580 } else if (cur->left < prev->right) {
581 ALOGE_IF(!silent,
582 "%s: spans overlap horizontally prev=%p, cur=%p",
583 name, prev, cur);
584 result = false;
585 }
586 } else if (cur->top < prev->bottom) {
587 ALOGE_IF(!silent,
588 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700589 name, prev, cur);
590 result = false;
591 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700592 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700593 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700594 cur++;
595 }
596 if (b != reg.getBounds()) {
597 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700598 ALOGE_IF(!silent,
599 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700600 b.left, b.top, b.right, b.bottom,
601 reg.getBounds().left, reg.getBounds().top,
602 reg.getBounds().right, reg.getBounds().bottom);
603 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700604 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700605 result = false;
606 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700607 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700608#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700609 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700610 reg.dump(name);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700611 CallStack stack(LOG_TAG);
Mathias Agopian20f68782009-05-11 00:03:41 -0700612 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700613#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700614 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615}
616
Colin Cross8f279962016-09-26 13:08:16 -0700617void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700618 const Region& lhs,
619 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800620{
Chong Zhang639a1e12019-04-22 14:01:20 -0700621#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700622 validate(lhs, "boolean_operation (before): lhs");
623 validate(rhs, "boolean_operation (before): rhs");
624 validate(dst, "boolean_operation (before): dst");
625#endif
626
Mathias Agopian20f68782009-05-11 00:03:41 -0700627 size_t lhs_count;
628 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
629
630 size_t rhs_count;
631 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
632
633 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
634 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
635 region_operator<Rect> operation(op, lhs_region, rhs_region);
636 { // scope for rasterizer (dtor has side effects)
637 rasterizer r(dst);
638 operation(r);
639 }
640
Chong Zhang639a1e12019-04-22 14:01:20 -0700641#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700642 validate(lhs, "boolean_operation: lhs");
643 validate(rhs, "boolean_operation: rhs");
644 validate(dst, "boolean_operation: dst");
645#endif
646
647#if VALIDATE_WITH_CORECG
648 SkRegion sk_lhs;
649 SkRegion sk_rhs;
650 SkRegion sk_dst;
651
652 for (size_t i=0 ; i<lhs_count ; i++)
653 sk_lhs.op(
654 lhs_rects[i].left + dx,
655 lhs_rects[i].top + dy,
656 lhs_rects[i].right + dx,
657 lhs_rects[i].bottom + dy,
658 SkRegion::kUnion_Op);
659
660 for (size_t i=0 ; i<rhs_count ; i++)
661 sk_rhs.op(
662 rhs_rects[i].left + dx,
663 rhs_rects[i].top + dy,
664 rhs_rects[i].right + dx,
665 rhs_rects[i].bottom + dy,
666 SkRegion::kUnion_Op);
667
668 const char* name = "---";
669 SkRegion::Op sk_op;
670 switch (op) {
671 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800672 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700673 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
674 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
675 }
676 sk_dst.op(sk_lhs, sk_rhs, sk_op);
677
678 if (sk_dst.isEmpty() && dst.isEmpty())
679 return;
680
681 bool same = true;
682 Region::const_iterator head = dst.begin();
683 Region::const_iterator const tail = dst.end();
684 SkRegion::Iterator it(sk_dst);
685 while (!it.done()) {
686 if (head != tail) {
687 if (
688 head->left != it.rect().fLeft ||
689 head->top != it.rect().fTop ||
690 head->right != it.rect().fRight ||
691 head->bottom != it.rect().fBottom
692 ) {
693 same = false;
694 break;
695 }
696 } else {
697 same = false;
698 break;
699 }
700 head++;
701 it.next();
702 }
703
704 if (head != tail) {
705 same = false;
706 }
707
708 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000709 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700710 lhs.dump("lhs");
711 rhs.dump("rhs");
712 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000713 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700714 SkRegion::Iterator it(sk_dst);
715 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000716 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700717 it.rect().fLeft,
718 it.rect().fTop,
719 it.rect().fRight,
720 it.rect().fBottom);
721 it.next();
722 }
723 }
724#endif
725}
726
Colin Cross8f279962016-09-26 13:08:16 -0700727void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700728 const Region& lhs,
729 const Rect& rhs, int dx, int dy)
730{
Dan Stoza5065a552015-03-17 16:23:42 -0700731 // We allow this particular flavor of invalid Rect, since it is used as a
732 // signal value in various parts of the system
733 if (!rhs.isValid() && rhs != Rect::INVALID_RECT) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000734 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700735 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700736 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700737 }
738
Chong Zhang639a1e12019-04-22 14:01:20 -0700739#if VALIDATE_WITH_CORECG || defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700740 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
741#else
742 size_t lhs_count;
743 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
744
745 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
746 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
747 region_operator<Rect> operation(op, lhs_region, rhs_region);
748 { // scope for rasterizer (dtor has side effects)
749 rasterizer r(dst);
750 operation(r);
751 }
752
753#endif
754}
755
Colin Cross8f279962016-09-26 13:08:16 -0700756void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700757 const Region& lhs, const Region& rhs)
758{
759 boolean_operation(op, dst, lhs, rhs, 0, 0);
760}
761
Colin Cross8f279962016-09-26 13:08:16 -0700762void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700763 const Region& lhs, const Rect& rhs)
764{
765 boolean_operation(op, dst, lhs, rhs, 0, 0);
766}
767
768void Region::translate(Region& reg, int dx, int dy)
769{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700770 if ((dx || dy) && !reg.isEmpty()) {
Chong Zhang639a1e12019-04-22 14:01:20 -0700771#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700772 validate(reg, "translate (before)");
773#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700774 size_t count = reg.mStorage.size();
775 Rect* rects = reg.mStorage.editArray();
776 while (count) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700777 rects->offsetBy(dx, dy);
Mathias Agopian20f68782009-05-11 00:03:41 -0700778 rects++;
779 count--;
780 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700781#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700782 validate(reg, "translate (after)");
783#endif
784 }
785}
786
787void Region::translate(Region& dst, const Region& reg, int dx, int dy)
788{
789 dst = reg;
790 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791}
792
793// ----------------------------------------------------------------------------
794
Mathias Agopiane1424282013-07-29 21:24:40 -0700795size_t Region::getFlattenedSize() const {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700796 return sizeof(uint32_t) + mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700797}
798
Mathias Agopiane1424282013-07-29 21:24:40 -0700799status_t Region::flatten(void* buffer, size_t size) const {
Chong Zhang639a1e12019-04-22 14:01:20 -0700800#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700801 validate(*this, "Region::flatten");
802#endif
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700803 if (size < getFlattenedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700804 return NO_MEMORY;
805 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700806 // Cast to uint32_t since the size of a size_t can vary between 32- and
807 // 64-bit processes
808 FlattenableUtils::write(buffer, size, static_cast<uint32_t>(mStorage.size()));
809 for (auto rect : mStorage) {
810 status_t result = rect.flatten(buffer, size);
811 if (result != NO_ERROR) {
812 return result;
813 }
814 FlattenableUtils::advance(buffer, size, sizeof(rect));
815 }
Mathias Agopian8683fca2012-08-12 19:37:16 -0700816 return NO_ERROR;
817}
818
819status_t Region::unflatten(void const* buffer, size_t size) {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700820 if (size < sizeof(uint32_t)) {
821 return NO_MEMORY;
Mathias Agopian20f68782009-05-11 00:03:41 -0700822 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700823
824 uint32_t numRects = 0;
825 FlattenableUtils::read(buffer, size, numRects);
826 if (size < numRects * sizeof(Rect)) {
827 return NO_MEMORY;
828 }
829
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700830 if (numRects > (UINT32_MAX / sizeof(Rect))) {
Yi Kong48d76082019-03-24 02:01:06 -0700831 android_errorWriteWithInfoLog(0x534e4554, "29983260", -1, nullptr, 0);
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700832 return NO_MEMORY;
833 }
834
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700835 Region result;
836 result.mStorage.clear();
837 for (size_t r = 0; r < numRects; ++r) {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700838 Rect rect(Rect::EMPTY_RECT);
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700839 status_t status = rect.unflatten(buffer, size);
840 if (status != NO_ERROR) {
841 return status;
842 }
843 FlattenableUtils::advance(buffer, size, sizeof(rect));
844 result.mStorage.push_back(rect);
845 }
846
Chong Zhang639a1e12019-04-22 14:01:20 -0700847#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700848 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700849#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700850
851 if (!result.validate(result, "Region::unflatten", true)) {
852 ALOGE("Region::unflatten() failed, invalid region");
853 return BAD_VALUE;
854 }
855 mStorage = result.mStorage;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700856 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800857}
858
Mathias Agopian20f68782009-05-11 00:03:41 -0700859// ----------------------------------------------------------------------------
860
861Region::const_iterator Region::begin() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700862 return mStorage.array();
Mathias Agopian20f68782009-05-11 00:03:41 -0700863}
864
865Region::const_iterator Region::end() const {
Dan Stoza2d023062018-04-09 12:14:55 -0700866 // Workaround for b/77643177
867 // mStorage should never be empty, but somehow it is and it's causing
868 // an abort in ubsan
869 if (mStorage.isEmpty()) return mStorage.array();
870
Mathias Agopian3ab68552012-08-31 14:31:40 -0700871 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
872 return mStorage.array() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700873}
874
875Rect const* Region::getArray(size_t* count) const {
Dan Stozad3182402014-11-17 12:03:59 -0800876 if (count) *count = static_cast<size_t>(end() - begin());
877 return begin();
Mathias Agopian20f68782009-05-11 00:03:41 -0700878}
879
Mathias Agopian20f68782009-05-11 00:03:41 -0700880// ----------------------------------------------------------------------------
881
Yiwei Zhang5434a782018-12-05 18:06:32 -0800882void Region::dump(std::string& out, const char* what, uint32_t /* flags */) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700883 const_iterator head = begin();
884 const_iterator const tail = end();
885
Yiwei Zhang5434a782018-12-05 18:06:32 -0800886 StringAppendF(&out, " Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail - head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700887 while (head != tail) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800888 StringAppendF(&out, " [%3d, %3d, %3d, %3d]\n", head->left, head->top, head->right,
889 head->bottom);
Dan Stozad3182402014-11-17 12:03:59 -0800890 ++head;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800891 }
892}
893
Dan Stozad3182402014-11-17 12:03:59 -0800894void Region::dump(const char* what, uint32_t /* flags */) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800895{
Mathias Agopian20f68782009-05-11 00:03:41 -0700896 const_iterator head = begin();
897 const_iterator const tail = end();
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700898 ALOGD(" Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700899 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000900 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700901 head->left, head->top, head->right, head->bottom);
902 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800903 }
904}
905
906// ----------------------------------------------------------------------------
907
908}; // namespace android