blob: 82ce757d5a4a7b8c415a9c32d73f978557caecc6 [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() {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +000070 mStorage.push_back(Rect(0, 0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071}
72
73Region::Region(const Region& rhs)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074{
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +000075 mStorage.clear();
76 mStorage.insert(mStorage.begin(), rhs.mStorage.begin(), rhs.mStorage.end());
Chong Zhang639a1e12019-04-22 14:01:20 -070077#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -070078 validate(rhs, "rhs copy-ctor");
79#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080}
81
Mathias Agopian3ab68552012-08-31 14:31:40 -070082Region::Region(const Rect& rhs) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +000083 mStorage.push_back(rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084}
85
86Region::~Region()
87{
88}
89
Chris Craik3e010f32013-02-25 19:12:47 -080090/**
91 * Copy rects from the src vector into the dst vector, resolving vertical T-Junctions along the way
92 *
93 * First pass through, divideSpanRTL will be set because the 'previous span' (indexing into the dst
94 * vector) will be reversed. Each rectangle in the original list, starting from the bottom, will be
95 * compared with the span directly below, and subdivided as needed to resolve T-junctions.
96 *
97 * The resulting temporary vector will be a completely reversed copy of the original, without any
98 * bottom-up T-junctions.
99 *
100 * Second pass through, divideSpanRTL will be false since the previous span will index into the
101 * final, correctly ordered region buffer. Each rectangle will be compared with the span directly
102 * above it, and subdivided to resolve any remaining T-junctions.
103 */
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000104static void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end, FatVector<Rect>& dst,
105 int spanDirection) {
Chris Craik3e010f32013-02-25 19:12:47 -0800106 dst.clear();
107
108 const Rect* current = end - 1;
109 int lastTop = current->top;
110
111 // add first span immediately
112 do {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000113 dst.push_back(*current);
Chris Craik3e010f32013-02-25 19:12:47 -0800114 current--;
115 } while (current->top == lastTop && current >= begin);
116
Dan Stozad3182402014-11-17 12:03:59 -0800117 int beginLastSpan = -1;
118 int endLastSpan = -1;
Chris Craik3e010f32013-02-25 19:12:47 -0800119 int top = -1;
120 int bottom = -1;
121
122 // for all other spans, split if a t-junction exists in the span directly above
123 while (current >= begin) {
124 if (current->top != (current + 1)->top) {
125 // new span
126 if ((spanDirection == direction_RTL && current->bottom != (current + 1)->top) ||
127 (spanDirection == direction_LTR && current->top != (current + 1)->bottom)) {
128 // previous span not directly adjacent, don't check for T junctions
129 beginLastSpan = INT_MAX;
130 } else {
131 beginLastSpan = endLastSpan + 1;
132 }
Dan Stozad3182402014-11-17 12:03:59 -0800133 endLastSpan = static_cast<int>(dst.size()) - 1;
Chris Craik3e010f32013-02-25 19:12:47 -0800134
135 top = current->top;
136 bottom = current->bottom;
137 }
138 int left = current->left;
139 int right = current->right;
140
Dan Stozad3182402014-11-17 12:03:59 -0800141 for (int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) {
142 // prevIndex can't be -1 here because if endLastSpan is set to a
143 // value greater than -1 (allowing the loop to execute),
144 // beginLastSpan (and therefore prevIndex) will also be increased
ywenaef04452015-03-26 19:51:12 +0800145 const Rect prev = dst[static_cast<size_t>(prevIndex)];
Chris Craik3e010f32013-02-25 19:12:47 -0800146 if (spanDirection == direction_RTL) {
147 // iterating over previous span RTL, quit if it's too far left
ywenaef04452015-03-26 19:51:12 +0800148 if (prev.right <= left) break;
Chris Craik3e010f32013-02-25 19:12:47 -0800149
ywenaef04452015-03-26 19:51:12 +0800150 if (prev.right > left && prev.right < right) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000151 dst.push_back(Rect(prev.right, top, right, bottom));
ywenaef04452015-03-26 19:51:12 +0800152 right = prev.right;
Chris Craik3e010f32013-02-25 19:12:47 -0800153 }
154
ywenaef04452015-03-26 19:51:12 +0800155 if (prev.left > left && prev.left < right) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000156 dst.push_back(Rect(prev.left, top, right, bottom));
ywenaef04452015-03-26 19:51:12 +0800157 right = prev.left;
Chris Craik3e010f32013-02-25 19:12:47 -0800158 }
159
160 // if an entry in the previous span is too far right, nothing further left in the
161 // current span will need it
ywenaef04452015-03-26 19:51:12 +0800162 if (prev.left >= right) {
Chris Craik3e010f32013-02-25 19:12:47 -0800163 beginLastSpan = prevIndex;
164 }
165 } else {
166 // iterating over previous span LTR, quit if it's too far right
ywenaef04452015-03-26 19:51:12 +0800167 if (prev.left >= right) break;
Chris Craik3e010f32013-02-25 19:12:47 -0800168
ywenaef04452015-03-26 19:51:12 +0800169 if (prev.left > left && prev.left < right) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000170 dst.push_back(Rect(left, top, prev.left, bottom));
ywenaef04452015-03-26 19:51:12 +0800171 left = prev.left;
Chris Craik3e010f32013-02-25 19:12:47 -0800172 }
173
ywenaef04452015-03-26 19:51:12 +0800174 if (prev.right > left && prev.right < right) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000175 dst.push_back(Rect(left, top, prev.right, bottom));
ywenaef04452015-03-26 19:51:12 +0800176 left = prev.right;
Chris Craik3e010f32013-02-25 19:12:47 -0800177 }
178 // if an entry in the previous span is too far left, nothing further right in the
179 // current span will need it
ywenaef04452015-03-26 19:51:12 +0800180 if (prev.right <= left) {
Chris Craik3e010f32013-02-25 19:12:47 -0800181 beginLastSpan = prevIndex;
182 }
183 }
184 }
185
186 if (left < right) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000187 dst.push_back(Rect(left, top, right, bottom));
Chris Craik3e010f32013-02-25 19:12:47 -0800188 }
189
190 current--;
191 }
192}
193
194/**
195 * Creates a new region with the same data as the argument, but divides rectangles as necessary to
196 * remove T-Junctions
197 *
198 * Note: the output will not necessarily be a very efficient representation of the region, since it
199 * may be that a triangle-based approach would generate significantly simpler geometry
200 */
201Region Region::createTJunctionFreeRegion(const Region& r) {
202 if (r.isEmpty()) return r;
203 if (r.isRect()) return r;
204
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000205 FatVector<Rect> reversed;
Chris Craik3e010f32013-02-25 19:12:47 -0800206 reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL);
207
208 Region outputRegion;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000209 reverseRectsResolvingJunctions(reversed.data(), reversed.data() + reversed.size(),
210 outputRegion.mStorage, direction_LTR);
211 outputRegion.mStorage.push_back(
212 r.getBounds()); // to make region valid, mStorage must end with bounds
Chris Craik3e010f32013-02-25 19:12:47 -0800213
Chong Zhang639a1e12019-04-22 14:01:20 -0700214#if defined(VALIDATE_REGIONS)
Chris Craik3e010f32013-02-25 19:12:47 -0800215 validate(outputRegion, "T-Junction free region");
216#endif
217
218 return outputRegion;
219}
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221Region& Region::operator = (const Region& rhs)
222{
Chong Zhang639a1e12019-04-22 14:01:20 -0700223#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700224 validate(*this, "this->operator=");
225 validate(rhs, "rhs.operator=");
Mathias Agopian20f68782009-05-11 00:03:41 -0700226#endif
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000227 mStorage.clear();
228 mStorage.insert(mStorage.begin(), rhs.mStorage.begin(), rhs.mStorage.end());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 return *this;
230}
231
Mathias Agopian9f961452009-06-29 18:46:37 -0700232Region& Region::makeBoundsSelf()
233{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700234 if (mStorage.size() >= 2) {
235 const Rect bounds(getBounds());
236 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000237 mStorage.push_back(bounds);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700238 }
Mathias Agopian9f961452009-06-29 18:46:37 -0700239 return *this;
240}
241
Michael Wright1c284a92014-02-10 13:00:14 -0800242bool Region::contains(const Point& point) const {
243 return contains(point.x, point.y);
244}
245
246bool Region::contains(int x, int y) const {
247 const_iterator cur = begin();
248 const_iterator const tail = end();
249 while (cur != tail) {
250 if (y >= cur->top && y < cur->bottom && x >= cur->left && x < cur->right) {
251 return true;
252 }
253 cur++;
254 }
255 return false;
256}
257
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258void Region::clear()
259{
Mathias Agopian20f68782009-05-11 00:03:41 -0700260 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000261 mStorage.push_back(Rect(0, 0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262}
263
264void Region::set(const Rect& r)
265{
Mathias Agopian20f68782009-05-11 00:03:41 -0700266 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000267 mStorage.push_back(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268}
269
Dan Stozad3182402014-11-17 12:03:59 -0800270void Region::set(int32_t w, int32_t h)
Mathias Agopian0926f502009-05-04 14:17:04 -0700271{
Mathias Agopian20f68782009-05-11 00:03:41 -0700272 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000273 mStorage.push_back(Rect(w, h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700274}
275
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +0100276void Region::set(uint32_t w, uint32_t h)
277{
278 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000279 mStorage.push_back(Rect(w, h));
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +0100280}
281
Mathias Agopian2ca79392013-04-02 18:30:32 -0700282bool Region::isTriviallyEqual(const Region& region) const {
283 return begin() == region.begin();
284}
285
Lloyd Piqueea629282019-12-03 15:57:10 -0800286bool Region::hasSameRects(const Region& other) const {
287 size_t thisRectCount = 0;
288 android::Rect const* thisRects = getArray(&thisRectCount);
289 size_t otherRectCount = 0;
290 android::Rect const* otherRects = other.getArray(&otherRectCount);
291
292 if (thisRectCount != otherRectCount) return false;
293
294 for (size_t i = 0; i < thisRectCount; i++) {
295 if (thisRects[i] != otherRects[i]) return false;
296 }
297 return true;
298}
299
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300// ----------------------------------------------------------------------------
301
Mathias Agopian20f68782009-05-11 00:03:41 -0700302void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700304 Rect rect(l,t,r,b);
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000305 mStorage.insert(mStorage.end() - 1, rect);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306}
307
Mathias Agopian20f68782009-05-11 00:03:41 -0700308// ----------------------------------------------------------------------------
309
310Region& Region::orSelf(const Rect& r) {
Dan Stoza547808b2020-04-02 09:31:08 -0700311 if (isEmpty()) {
312 set(r);
313 return *this;
314 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700315 return operationSelf(r, op_or);
316}
Romain Guyb8a2e982012-02-07 17:04:34 -0800317Region& Region::xorSelf(const Rect& r) {
318 return operationSelf(r, op_xor);
319}
Mathias Agopian20f68782009-05-11 00:03:41 -0700320Region& Region::andSelf(const Rect& r) {
321 return operationSelf(r, op_and);
322}
323Region& Region::subtractSelf(const Rect& r) {
324 return operationSelf(r, op_nand);
325}
Colin Cross8f279962016-09-26 13:08:16 -0700326Region& Region::operationSelf(const Rect& r, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700327 Region lhs(*this);
328 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800329 return *this;
330}
331
332// ----------------------------------------------------------------------------
333
334Region& Region::orSelf(const Region& rhs) {
Dan Stoza547808b2020-04-02 09:31:08 -0700335 if (isEmpty()) {
336 *this = rhs;
337 return *this;
338 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700339 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340}
Romain Guyb8a2e982012-02-07 17:04:34 -0800341Region& Region::xorSelf(const Region& rhs) {
342 return operationSelf(rhs, op_xor);
343}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700345 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800347Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700348 return operationSelf(rhs, op_nand);
349}
Colin Cross8f279962016-09-26 13:08:16 -0700350Region& Region::operationSelf(const Region& rhs, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700351 Region lhs(*this);
352 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 return *this;
354}
355
356Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700357 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 return *this;
359}
360
Riddle Hsu39d4aa52018-11-30 20:46:53 +0800361Region& Region::scaleSelf(float sx, float sy) {
Robert Carre07e1032018-11-26 12:55:53 -0800362 size_t count = mStorage.size();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000363 Rect* rects = mStorage.data();
Robert Carre07e1032018-11-26 12:55:53 -0800364 while (count) {
Nick Desaulniersea6c7132019-10-15 19:14:39 -0700365 rects->left = static_cast<int32_t>(static_cast<float>(rects->left) * sx + 0.5f);
366 rects->right = static_cast<int32_t>(static_cast<float>(rects->right) * sx + 0.5f);
367 rects->top = static_cast<int32_t>(static_cast<float>(rects->top) * sy + 0.5f);
368 rects->bottom = static_cast<int32_t>(static_cast<float>(rects->bottom) * sy + 0.5f);
Robert Carre07e1032018-11-26 12:55:53 -0800369 rects++;
370 count--;
371 }
372 return *this;
373}
374
Mathias Agopian20f68782009-05-11 00:03:41 -0700375// ----------------------------------------------------------------------------
376
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700377const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700378 return operation(rhs, op_or);
379}
Romain Guyb8a2e982012-02-07 17:04:34 -0800380const Region Region::mergeExclusive(const Rect& rhs) const {
381 return operation(rhs, op_xor);
382}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700383const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700384 return operation(rhs, op_and);
385}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700386const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700387 return operation(rhs, op_nand);
388}
Colin Cross8f279962016-09-26 13:08:16 -0700389const Region Region::operation(const Rect& rhs, uint32_t op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700390 Region result;
391 boolean_operation(op, result, *this, rhs);
392 return result;
393}
394
395// ----------------------------------------------------------------------------
396
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700397const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700398 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399}
Romain Guyb8a2e982012-02-07 17:04:34 -0800400const Region Region::mergeExclusive(const Region& rhs) const {
401 return operation(rhs, op_xor);
402}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700403const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700404 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700406const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700407 return operation(rhs, op_nand);
408}
Colin Cross8f279962016-09-26 13:08:16 -0700409const Region Region::operation(const Region& rhs, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700411 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412 return result;
413}
414
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700415const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700417 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418 return result;
419}
420
421// ----------------------------------------------------------------------------
422
423Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700424 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425}
Romain Guyb8a2e982012-02-07 17:04:34 -0800426Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
427 return operationSelf(rhs, dx, dy, op_xor);
428}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700430 return operationSelf(rhs, dx, dy, op_and);
431}
432Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
433 return operationSelf(rhs, dx, dy, op_nand);
434}
Colin Cross8f279962016-09-26 13:08:16 -0700435Region& Region::operationSelf(const Region& rhs, int dx, int dy, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700436 Region lhs(*this);
437 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438 return *this;
439}
440
Mathias Agopian20f68782009-05-11 00:03:41 -0700441// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800442
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700443const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700444 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800445}
Romain Guyb8a2e982012-02-07 17:04:34 -0800446const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
447 return operation(rhs, dx, dy, op_xor);
448}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700449const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700450 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700452const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700453 return operation(rhs, dx, dy, op_nand);
454}
Colin Cross8f279962016-09-26 13:08:16 -0700455const Region Region::operation(const Region& rhs, int dx, int dy, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700457 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458 return result;
459}
460
461// ----------------------------------------------------------------------------
462
Mathias Agopian20f68782009-05-11 00:03:41 -0700463// This is our region rasterizer, which merges rects and spans together
464// to obtain an optimal region.
Dan Stozad3182402014-11-17 12:03:59 -0800465class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800466{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700467 Rect bounds;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000468 FatVector<Rect>& storage;
Mathias Agopian20f68782009-05-11 00:03:41 -0700469 Rect* head;
470 Rect* tail;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000471 FatVector<Rect> span;
Mathias Agopian20f68782009-05-11 00:03:41 -0700472 Rect* cur;
473public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700474 explicit rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700475 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700476 storage.clear();
477 }
478
Dan Stozad3182402014-11-17 12:03:59 -0800479 virtual ~rasterizer();
480
481 virtual void operator()(const Rect& rect);
482
Mathias Agopian20f68782009-05-11 00:03:41 -0700483private:
Dan Stozad3182402014-11-17 12:03:59 -0800484 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700485 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800486 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700487 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800488
489 void flushSpan();
Mathias Agopian20f68782009-05-11 00:03:41 -0700490};
491
Dan Stozad3182402014-11-17 12:03:59 -0800492Region::rasterizer::~rasterizer()
493{
494 if (span.size()) {
495 flushSpan();
496 }
497 if (storage.size()) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000498 bounds.top = storage.front().top;
499 bounds.bottom = storage.back().bottom;
Dan Stozad3182402014-11-17 12:03:59 -0800500 if (storage.size() == 1) {
501 storage.clear();
502 }
503 } else {
504 bounds.left = 0;
505 bounds.right = 0;
506 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000507 storage.push_back(bounds);
Dan Stozad3182402014-11-17 12:03:59 -0800508}
509
510void Region::rasterizer::operator()(const Rect& rect)
511{
512 //ALOGD(">>> %3d, %3d, %3d, %3d",
513 // rect.left, rect.top, rect.right, rect.bottom);
514 if (span.size()) {
515 if (cur->top != rect.top) {
516 flushSpan();
517 } else if (cur->right == rect.left) {
518 cur->right = rect.right;
519 return;
520 }
521 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000522 span.push_back(rect);
523 cur = span.data() + (span.size() - 1);
Dan Stozad3182402014-11-17 12:03:59 -0800524}
525
526void Region::rasterizer::flushSpan()
527{
528 bool merge = false;
529 if (tail-head == ssize_t(span.size())) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000530 Rect const* p = span.data();
Dan Stozad3182402014-11-17 12:03:59 -0800531 Rect const* q = head;
532 if (p->top == q->bottom) {
533 merge = true;
534 while (q != tail) {
535 if ((p->left != q->left) || (p->right != q->right)) {
536 merge = false;
537 break;
538 }
Stephen Hines9c22c3c2016-03-31 22:02:38 -0700539 p++;
540 q++;
Dan Stozad3182402014-11-17 12:03:59 -0800541 }
542 }
543 }
544 if (merge) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000545 const int bottom = span.front().bottom;
Dan Stozad3182402014-11-17 12:03:59 -0800546 Rect* r = head;
547 while (r != tail) {
548 r->bottom = bottom;
549 r++;
550 }
551 } else {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000552 bounds.left = min(span.front().left, bounds.left);
553 bounds.right = max(span.back().right, bounds.right);
554 storage.insert(storage.end(), span.begin(), span.end());
555 tail = storage.data() + storage.size();
Dan Stozad3182402014-11-17 12:03:59 -0800556 head = tail - span.size();
557 }
558 span.clear();
559}
560
Mathias Agopian068d47f2012-09-11 18:56:23 -0700561bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700562{
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000563 if (reg.mStorage.empty()) {
Chia-I Wub420b582018-02-07 11:53:41 -0800564 ALOGE_IF(!silent, "%s: mStorage is empty, which is never valid", name);
565 // return immediately as the code below assumes mStorage is non-empty
566 return false;
567 }
568
Mathias Agopian20f68782009-05-11 00:03:41 -0700569 bool result = true;
570 const_iterator cur = reg.begin();
571 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700572 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700573 Rect b(*prev);
574 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700575 if (cur->isValid() == false) {
Dan Stoza5065a552015-03-17 16:23:42 -0700576 // We allow this particular flavor of invalid Rect, since it is used
577 // as a signal value in various parts of the system
578 if (*cur != Rect::INVALID_RECT) {
579 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
580 result = false;
581 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700582 }
583 if (cur->right > region_operator<Rect>::max_value) {
584 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
585 result = false;
586 }
587 if (cur->bottom > region_operator<Rect>::max_value) {
588 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
589 result = false;
590 }
591 if (prev != cur) {
592 b.left = b.left < cur->left ? b.left : cur->left;
593 b.top = b.top < cur->top ? b.top : cur->top;
594 b.right = b.right > cur->right ? b.right : cur->right;
595 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
596 if ((*prev < *cur) == false) {
597 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700598 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700599 }
600 if (cur->top == prev->top) {
601 if (cur->bottom != prev->bottom) {
602 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
603 result = false;
604 } else if (cur->left < prev->right) {
605 ALOGE_IF(!silent,
606 "%s: spans overlap horizontally prev=%p, cur=%p",
607 name, prev, cur);
608 result = false;
609 }
610 } else if (cur->top < prev->bottom) {
611 ALOGE_IF(!silent,
612 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700613 name, prev, cur);
614 result = false;
615 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700616 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700617 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700618 cur++;
619 }
620 if (b != reg.getBounds()) {
621 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700622 ALOGE_IF(!silent,
623 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700624 b.left, b.top, b.right, b.bottom,
625 reg.getBounds().left, reg.getBounds().top,
626 reg.getBounds().right, reg.getBounds().bottom);
627 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700628 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700629 result = false;
630 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700631 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700632#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700633 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700634 reg.dump(name);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700635 CallStack stack(LOG_TAG);
Mathias Agopian20f68782009-05-11 00:03:41 -0700636 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700637#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700638 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800639}
640
Colin Cross8f279962016-09-26 13:08:16 -0700641void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700642 const Region& lhs,
643 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644{
Chong Zhang639a1e12019-04-22 14:01:20 -0700645#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700646 validate(lhs, "boolean_operation (before): lhs");
647 validate(rhs, "boolean_operation (before): rhs");
648 validate(dst, "boolean_operation (before): dst");
649#endif
650
Mathias Agopian20f68782009-05-11 00:03:41 -0700651 size_t lhs_count;
652 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
653
654 size_t rhs_count;
655 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
656
657 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
658 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
659 region_operator<Rect> operation(op, lhs_region, rhs_region);
660 { // scope for rasterizer (dtor has side effects)
661 rasterizer r(dst);
662 operation(r);
663 }
664
Chong Zhang639a1e12019-04-22 14:01:20 -0700665#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700666 validate(lhs, "boolean_operation: lhs");
667 validate(rhs, "boolean_operation: rhs");
668 validate(dst, "boolean_operation: dst");
669#endif
670
671#if VALIDATE_WITH_CORECG
672 SkRegion sk_lhs;
673 SkRegion sk_rhs;
674 SkRegion sk_dst;
675
676 for (size_t i=0 ; i<lhs_count ; i++)
677 sk_lhs.op(
678 lhs_rects[i].left + dx,
679 lhs_rects[i].top + dy,
680 lhs_rects[i].right + dx,
681 lhs_rects[i].bottom + dy,
682 SkRegion::kUnion_Op);
683
684 for (size_t i=0 ; i<rhs_count ; i++)
685 sk_rhs.op(
686 rhs_rects[i].left + dx,
687 rhs_rects[i].top + dy,
688 rhs_rects[i].right + dx,
689 rhs_rects[i].bottom + dy,
690 SkRegion::kUnion_Op);
691
692 const char* name = "---";
693 SkRegion::Op sk_op;
694 switch (op) {
695 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800696 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700697 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
698 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
699 }
700 sk_dst.op(sk_lhs, sk_rhs, sk_op);
701
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000702 if (sk_dst.empty() && dst.empty()) return;
703
Mathias Agopian20f68782009-05-11 00:03:41 -0700704 bool same = true;
705 Region::const_iterator head = dst.begin();
706 Region::const_iterator const tail = dst.end();
707 SkRegion::Iterator it(sk_dst);
708 while (!it.done()) {
709 if (head != tail) {
710 if (
711 head->left != it.rect().fLeft ||
712 head->top != it.rect().fTop ||
713 head->right != it.rect().fRight ||
714 head->bottom != it.rect().fBottom
715 ) {
716 same = false;
717 break;
718 }
719 } else {
720 same = false;
721 break;
722 }
723 head++;
724 it.next();
725 }
726
727 if (head != tail) {
728 same = false;
729 }
730
731 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000732 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700733 lhs.dump("lhs");
734 rhs.dump("rhs");
735 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000736 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700737 SkRegion::Iterator it(sk_dst);
738 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000739 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700740 it.rect().fLeft,
741 it.rect().fTop,
742 it.rect().fRight,
743 it.rect().fBottom);
744 it.next();
745 }
746 }
747#endif
748}
749
Colin Cross8f279962016-09-26 13:08:16 -0700750void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700751 const Region& lhs,
752 const Rect& rhs, int dx, int dy)
753{
Dan Stoza5065a552015-03-17 16:23:42 -0700754 // We allow this particular flavor of invalid Rect, since it is used as a
755 // signal value in various parts of the system
756 if (!rhs.isValid() && rhs != Rect::INVALID_RECT) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000757 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700758 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700759 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700760 }
761
Chong Zhang639a1e12019-04-22 14:01:20 -0700762#if VALIDATE_WITH_CORECG || defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700763 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
764#else
765 size_t lhs_count;
766 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
767
768 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
769 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
770 region_operator<Rect> operation(op, lhs_region, rhs_region);
771 { // scope for rasterizer (dtor has side effects)
772 rasterizer r(dst);
773 operation(r);
774 }
775
776#endif
777}
778
Colin Cross8f279962016-09-26 13:08:16 -0700779void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700780 const Region& lhs, const Region& rhs)
781{
782 boolean_operation(op, dst, lhs, rhs, 0, 0);
783}
784
Colin Cross8f279962016-09-26 13:08:16 -0700785void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700786 const Region& lhs, const Rect& rhs)
787{
788 boolean_operation(op, dst, lhs, rhs, 0, 0);
789}
790
791void Region::translate(Region& reg, int dx, int dy)
792{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700793 if ((dx || dy) && !reg.isEmpty()) {
Chong Zhang639a1e12019-04-22 14:01:20 -0700794#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700795 validate(reg, "translate (before)");
796#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700797 size_t count = reg.mStorage.size();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000798 Rect* rects = reg.mStorage.data();
Mathias Agopian20f68782009-05-11 00:03:41 -0700799 while (count) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700800 rects->offsetBy(dx, dy);
Mathias Agopian20f68782009-05-11 00:03:41 -0700801 rects++;
802 count--;
803 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700804#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700805 validate(reg, "translate (after)");
806#endif
807 }
808}
809
810void Region::translate(Region& dst, const Region& reg, int dx, int dy)
811{
812 dst = reg;
813 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814}
815
816// ----------------------------------------------------------------------------
817
Mathias Agopiane1424282013-07-29 21:24:40 -0700818size_t Region::getFlattenedSize() const {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700819 return sizeof(uint32_t) + mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700820}
821
Mathias Agopiane1424282013-07-29 21:24:40 -0700822status_t Region::flatten(void* buffer, size_t size) const {
Chong Zhang639a1e12019-04-22 14:01:20 -0700823#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700824 validate(*this, "Region::flatten");
825#endif
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700826 if (size < getFlattenedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700827 return NO_MEMORY;
828 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700829 // Cast to uint32_t since the size of a size_t can vary between 32- and
830 // 64-bit processes
831 FlattenableUtils::write(buffer, size, static_cast<uint32_t>(mStorage.size()));
832 for (auto rect : mStorage) {
833 status_t result = rect.flatten(buffer, size);
834 if (result != NO_ERROR) {
835 return result;
836 }
837 FlattenableUtils::advance(buffer, size, sizeof(rect));
838 }
Mathias Agopian8683fca2012-08-12 19:37:16 -0700839 return NO_ERROR;
840}
841
842status_t Region::unflatten(void const* buffer, size_t size) {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700843 if (size < sizeof(uint32_t)) {
844 return NO_MEMORY;
Mathias Agopian20f68782009-05-11 00:03:41 -0700845 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700846
847 uint32_t numRects = 0;
848 FlattenableUtils::read(buffer, size, numRects);
849 if (size < numRects * sizeof(Rect)) {
850 return NO_MEMORY;
851 }
852
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700853 if (numRects > (UINT32_MAX / sizeof(Rect))) {
Yi Kong48d76082019-03-24 02:01:06 -0700854 android_errorWriteWithInfoLog(0x534e4554, "29983260", -1, nullptr, 0);
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700855 return NO_MEMORY;
856 }
857
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700858 Region result;
859 result.mStorage.clear();
860 for (size_t r = 0; r < numRects; ++r) {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700861 Rect rect(Rect::EMPTY_RECT);
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700862 status_t status = rect.unflatten(buffer, size);
863 if (status != NO_ERROR) {
864 return status;
865 }
866 FlattenableUtils::advance(buffer, size, sizeof(rect));
867 result.mStorage.push_back(rect);
868 }
869
Chong Zhang639a1e12019-04-22 14:01:20 -0700870#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700871 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700872#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700873
874 if (!result.validate(result, "Region::unflatten", true)) {
875 ALOGE("Region::unflatten() failed, invalid region");
876 return BAD_VALUE;
877 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000878 mStorage.clear();
879 mStorage.insert(mStorage.begin(), result.mStorage.begin(), result.mStorage.end());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700880 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881}
882
Mathias Agopian20f68782009-05-11 00:03:41 -0700883// ----------------------------------------------------------------------------
884
885Region::const_iterator Region::begin() const {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000886 return mStorage.data();
Mathias Agopian20f68782009-05-11 00:03:41 -0700887}
888
889Region::const_iterator Region::end() const {
Dan Stoza2d023062018-04-09 12:14:55 -0700890 // Workaround for b/77643177
891 // mStorage should never be empty, but somehow it is and it's causing
892 // an abort in ubsan
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000893 if (mStorage.empty()) return mStorage.data();
Dan Stoza2d023062018-04-09 12:14:55 -0700894
Mathias Agopian3ab68552012-08-31 14:31:40 -0700895 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000896 return mStorage.data() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700897}
898
899Rect const* Region::getArray(size_t* count) const {
Dan Stozad3182402014-11-17 12:03:59 -0800900 if (count) *count = static_cast<size_t>(end() - begin());
901 return begin();
Mathias Agopian20f68782009-05-11 00:03:41 -0700902}
903
Mathias Agopian20f68782009-05-11 00:03:41 -0700904// ----------------------------------------------------------------------------
905
Yiwei Zhang5434a782018-12-05 18:06:32 -0800906void Region::dump(std::string& out, const char* what, uint32_t /* flags */) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700907 const_iterator head = begin();
908 const_iterator const tail = end();
909
Yiwei Zhang5434a782018-12-05 18:06:32 -0800910 StringAppendF(&out, " Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail - head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700911 while (head != tail) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800912 StringAppendF(&out, " [%3d, %3d, %3d, %3d]\n", head->left, head->top, head->right,
913 head->bottom);
Dan Stozad3182402014-11-17 12:03:59 -0800914 ++head;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800915 }
916}
917
Dan Stozad3182402014-11-17 12:03:59 -0800918void Region::dump(const char* what, uint32_t /* flags */) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919{
Mathias Agopian20f68782009-05-11 00:03:41 -0700920 const_iterator head = begin();
921 const_iterator const tail = end();
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700922 ALOGD(" Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700923 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000924 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700925 head->left, head->top, head->right, head->bottom);
926 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800927 }
928}
929
930// ----------------------------------------------------------------------------
931
932}; // namespace android