blob: cd2a448c3ed55b2f42026c644e407f51cdbe732e [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) {
311 return operationSelf(r, op_or);
312}
Romain Guyb8a2e982012-02-07 17:04:34 -0800313Region& Region::xorSelf(const Rect& r) {
314 return operationSelf(r, op_xor);
315}
Mathias Agopian20f68782009-05-11 00:03:41 -0700316Region& Region::andSelf(const Rect& r) {
317 return operationSelf(r, op_and);
318}
319Region& Region::subtractSelf(const Rect& r) {
320 return operationSelf(r, op_nand);
321}
Colin Cross8f279962016-09-26 13:08:16 -0700322Region& Region::operationSelf(const Rect& r, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700323 Region lhs(*this);
324 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 return *this;
326}
327
328// ----------------------------------------------------------------------------
329
330Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700331 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800332}
Romain Guyb8a2e982012-02-07 17:04:34 -0800333Region& Region::xorSelf(const Region& rhs) {
334 return operationSelf(rhs, op_xor);
335}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700337 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700340 return operationSelf(rhs, op_nand);
341}
Colin Cross8f279962016-09-26 13:08:16 -0700342Region& Region::operationSelf(const Region& rhs, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700343 Region lhs(*this);
344 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345 return *this;
346}
347
348Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700349 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350 return *this;
351}
352
Riddle Hsu39d4aa52018-11-30 20:46:53 +0800353Region& Region::scaleSelf(float sx, float sy) {
Robert Carre07e1032018-11-26 12:55:53 -0800354 size_t count = mStorage.size();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000355 Rect* rects = mStorage.data();
Robert Carre07e1032018-11-26 12:55:53 -0800356 while (count) {
Nick Desaulniersea6c7132019-10-15 19:14:39 -0700357 rects->left = static_cast<int32_t>(static_cast<float>(rects->left) * sx + 0.5f);
358 rects->right = static_cast<int32_t>(static_cast<float>(rects->right) * sx + 0.5f);
359 rects->top = static_cast<int32_t>(static_cast<float>(rects->top) * sy + 0.5f);
360 rects->bottom = static_cast<int32_t>(static_cast<float>(rects->bottom) * sy + 0.5f);
Robert Carre07e1032018-11-26 12:55:53 -0800361 rects++;
362 count--;
363 }
364 return *this;
365}
366
Mathias Agopian20f68782009-05-11 00:03:41 -0700367// ----------------------------------------------------------------------------
368
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700369const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700370 return operation(rhs, op_or);
371}
Romain Guyb8a2e982012-02-07 17:04:34 -0800372const Region Region::mergeExclusive(const Rect& rhs) const {
373 return operation(rhs, op_xor);
374}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700375const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700376 return operation(rhs, op_and);
377}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700378const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700379 return operation(rhs, op_nand);
380}
Colin Cross8f279962016-09-26 13:08:16 -0700381const Region Region::operation(const Rect& rhs, uint32_t op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700382 Region result;
383 boolean_operation(op, result, *this, rhs);
384 return result;
385}
386
387// ----------------------------------------------------------------------------
388
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700389const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700390 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800391}
Romain Guyb8a2e982012-02-07 17:04:34 -0800392const Region Region::mergeExclusive(const Region& rhs) const {
393 return operation(rhs, op_xor);
394}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700395const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700396 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700398const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700399 return operation(rhs, op_nand);
400}
Colin Cross8f279962016-09-26 13:08:16 -0700401const Region Region::operation(const Region& rhs, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700403 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404 return result;
405}
406
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700407const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700409 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410 return result;
411}
412
413// ----------------------------------------------------------------------------
414
415Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700416 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417}
Romain Guyb8a2e982012-02-07 17:04:34 -0800418Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
419 return operationSelf(rhs, dx, dy, op_xor);
420}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700422 return operationSelf(rhs, dx, dy, op_and);
423}
424Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
425 return operationSelf(rhs, dx, dy, op_nand);
426}
Colin Cross8f279962016-09-26 13:08:16 -0700427Region& Region::operationSelf(const Region& rhs, int dx, int dy, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700428 Region lhs(*this);
429 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430 return *this;
431}
432
Mathias Agopian20f68782009-05-11 00:03:41 -0700433// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700435const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700436 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437}
Romain Guyb8a2e982012-02-07 17:04:34 -0800438const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
439 return operation(rhs, dx, dy, op_xor);
440}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700441const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700442 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700444const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700445 return operation(rhs, dx, dy, op_nand);
446}
Colin Cross8f279962016-09-26 13:08:16 -0700447const Region Region::operation(const Region& rhs, int dx, int dy, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800448 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700449 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450 return result;
451}
452
453// ----------------------------------------------------------------------------
454
Mathias Agopian20f68782009-05-11 00:03:41 -0700455// This is our region rasterizer, which merges rects and spans together
456// to obtain an optimal region.
Dan Stozad3182402014-11-17 12:03:59 -0800457class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800458{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700459 Rect bounds;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000460 FatVector<Rect>& storage;
Mathias Agopian20f68782009-05-11 00:03:41 -0700461 Rect* head;
462 Rect* tail;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000463 FatVector<Rect> span;
Mathias Agopian20f68782009-05-11 00:03:41 -0700464 Rect* cur;
465public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700466 explicit rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700467 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700468 storage.clear();
469 }
470
Dan Stozad3182402014-11-17 12:03:59 -0800471 virtual ~rasterizer();
472
473 virtual void operator()(const Rect& rect);
474
Mathias Agopian20f68782009-05-11 00:03:41 -0700475private:
Dan Stozad3182402014-11-17 12:03:59 -0800476 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700477 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800478 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700479 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800480
481 void flushSpan();
Mathias Agopian20f68782009-05-11 00:03:41 -0700482};
483
Dan Stozad3182402014-11-17 12:03:59 -0800484Region::rasterizer::~rasterizer()
485{
486 if (span.size()) {
487 flushSpan();
488 }
489 if (storage.size()) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000490 bounds.top = storage.front().top;
491 bounds.bottom = storage.back().bottom;
Dan Stozad3182402014-11-17 12:03:59 -0800492 if (storage.size() == 1) {
493 storage.clear();
494 }
495 } else {
496 bounds.left = 0;
497 bounds.right = 0;
498 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000499 storage.push_back(bounds);
Dan Stozad3182402014-11-17 12:03:59 -0800500}
501
502void Region::rasterizer::operator()(const Rect& rect)
503{
504 //ALOGD(">>> %3d, %3d, %3d, %3d",
505 // rect.left, rect.top, rect.right, rect.bottom);
506 if (span.size()) {
507 if (cur->top != rect.top) {
508 flushSpan();
509 } else if (cur->right == rect.left) {
510 cur->right = rect.right;
511 return;
512 }
513 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000514 span.push_back(rect);
515 cur = span.data() + (span.size() - 1);
Dan Stozad3182402014-11-17 12:03:59 -0800516}
517
518void Region::rasterizer::flushSpan()
519{
520 bool merge = false;
521 if (tail-head == ssize_t(span.size())) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000522 Rect const* p = span.data();
Dan Stozad3182402014-11-17 12:03:59 -0800523 Rect const* q = head;
524 if (p->top == q->bottom) {
525 merge = true;
526 while (q != tail) {
527 if ((p->left != q->left) || (p->right != q->right)) {
528 merge = false;
529 break;
530 }
Stephen Hines9c22c3c2016-03-31 22:02:38 -0700531 p++;
532 q++;
Dan Stozad3182402014-11-17 12:03:59 -0800533 }
534 }
535 }
536 if (merge) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000537 const int bottom = span.front().bottom;
Dan Stozad3182402014-11-17 12:03:59 -0800538 Rect* r = head;
539 while (r != tail) {
540 r->bottom = bottom;
541 r++;
542 }
543 } else {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000544 bounds.left = min(span.front().left, bounds.left);
545 bounds.right = max(span.back().right, bounds.right);
546 storage.insert(storage.end(), span.begin(), span.end());
547 tail = storage.data() + storage.size();
Dan Stozad3182402014-11-17 12:03:59 -0800548 head = tail - span.size();
549 }
550 span.clear();
551}
552
Mathias Agopian068d47f2012-09-11 18:56:23 -0700553bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700554{
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000555 if (reg.mStorage.empty()) {
Chia-I Wub420b582018-02-07 11:53:41 -0800556 ALOGE_IF(!silent, "%s: mStorage is empty, which is never valid", name);
557 // return immediately as the code below assumes mStorage is non-empty
558 return false;
559 }
560
Mathias Agopian20f68782009-05-11 00:03:41 -0700561 bool result = true;
562 const_iterator cur = reg.begin();
563 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700564 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700565 Rect b(*prev);
566 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700567 if (cur->isValid() == false) {
Dan Stoza5065a552015-03-17 16:23:42 -0700568 // We allow this particular flavor of invalid Rect, since it is used
569 // as a signal value in various parts of the system
570 if (*cur != Rect::INVALID_RECT) {
571 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
572 result = false;
573 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700574 }
575 if (cur->right > region_operator<Rect>::max_value) {
576 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
577 result = false;
578 }
579 if (cur->bottom > region_operator<Rect>::max_value) {
580 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
581 result = false;
582 }
583 if (prev != cur) {
584 b.left = b.left < cur->left ? b.left : cur->left;
585 b.top = b.top < cur->top ? b.top : cur->top;
586 b.right = b.right > cur->right ? b.right : cur->right;
587 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
588 if ((*prev < *cur) == false) {
589 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700590 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700591 }
592 if (cur->top == prev->top) {
593 if (cur->bottom != prev->bottom) {
594 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
595 result = false;
596 } else if (cur->left < prev->right) {
597 ALOGE_IF(!silent,
598 "%s: spans overlap horizontally prev=%p, cur=%p",
599 name, prev, cur);
600 result = false;
601 }
602 } else if (cur->top < prev->bottom) {
603 ALOGE_IF(!silent,
604 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700605 name, prev, cur);
606 result = false;
607 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700608 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700609 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700610 cur++;
611 }
612 if (b != reg.getBounds()) {
613 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700614 ALOGE_IF(!silent,
615 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700616 b.left, b.top, b.right, b.bottom,
617 reg.getBounds().left, reg.getBounds().top,
618 reg.getBounds().right, reg.getBounds().bottom);
619 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700620 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700621 result = false;
622 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700623 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700624#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700625 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700626 reg.dump(name);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700627 CallStack stack(LOG_TAG);
Mathias Agopian20f68782009-05-11 00:03:41 -0700628 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700629#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700630 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631}
632
Colin Cross8f279962016-09-26 13:08:16 -0700633void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700634 const Region& lhs,
635 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636{
Chong Zhang639a1e12019-04-22 14:01:20 -0700637#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700638 validate(lhs, "boolean_operation (before): lhs");
639 validate(rhs, "boolean_operation (before): rhs");
640 validate(dst, "boolean_operation (before): dst");
641#endif
642
Mathias Agopian20f68782009-05-11 00:03:41 -0700643 size_t lhs_count;
644 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
645
646 size_t rhs_count;
647 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
648
649 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
650 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
651 region_operator<Rect> operation(op, lhs_region, rhs_region);
652 { // scope for rasterizer (dtor has side effects)
653 rasterizer r(dst);
654 operation(r);
655 }
656
Chong Zhang639a1e12019-04-22 14:01:20 -0700657#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700658 validate(lhs, "boolean_operation: lhs");
659 validate(rhs, "boolean_operation: rhs");
660 validate(dst, "boolean_operation: dst");
661#endif
662
663#if VALIDATE_WITH_CORECG
664 SkRegion sk_lhs;
665 SkRegion sk_rhs;
666 SkRegion sk_dst;
667
668 for (size_t i=0 ; i<lhs_count ; i++)
669 sk_lhs.op(
670 lhs_rects[i].left + dx,
671 lhs_rects[i].top + dy,
672 lhs_rects[i].right + dx,
673 lhs_rects[i].bottom + dy,
674 SkRegion::kUnion_Op);
675
676 for (size_t i=0 ; i<rhs_count ; i++)
677 sk_rhs.op(
678 rhs_rects[i].left + dx,
679 rhs_rects[i].top + dy,
680 rhs_rects[i].right + dx,
681 rhs_rects[i].bottom + dy,
682 SkRegion::kUnion_Op);
683
684 const char* name = "---";
685 SkRegion::Op sk_op;
686 switch (op) {
687 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800688 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700689 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
690 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
691 }
692 sk_dst.op(sk_lhs, sk_rhs, sk_op);
693
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000694 if (sk_dst.empty() && dst.empty()) return;
695
Mathias Agopian20f68782009-05-11 00:03:41 -0700696 bool same = true;
697 Region::const_iterator head = dst.begin();
698 Region::const_iterator const tail = dst.end();
699 SkRegion::Iterator it(sk_dst);
700 while (!it.done()) {
701 if (head != tail) {
702 if (
703 head->left != it.rect().fLeft ||
704 head->top != it.rect().fTop ||
705 head->right != it.rect().fRight ||
706 head->bottom != it.rect().fBottom
707 ) {
708 same = false;
709 break;
710 }
711 } else {
712 same = false;
713 break;
714 }
715 head++;
716 it.next();
717 }
718
719 if (head != tail) {
720 same = false;
721 }
722
723 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000724 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700725 lhs.dump("lhs");
726 rhs.dump("rhs");
727 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000728 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700729 SkRegion::Iterator it(sk_dst);
730 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000731 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700732 it.rect().fLeft,
733 it.rect().fTop,
734 it.rect().fRight,
735 it.rect().fBottom);
736 it.next();
737 }
738 }
739#endif
740}
741
Colin Cross8f279962016-09-26 13:08:16 -0700742void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700743 const Region& lhs,
744 const Rect& rhs, int dx, int dy)
745{
Dan Stoza5065a552015-03-17 16:23:42 -0700746 // We allow this particular flavor of invalid Rect, since it is used as a
747 // signal value in various parts of the system
748 if (!rhs.isValid() && rhs != Rect::INVALID_RECT) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000749 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700750 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700751 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700752 }
753
Chong Zhang639a1e12019-04-22 14:01:20 -0700754#if VALIDATE_WITH_CORECG || defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700755 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
756#else
757 size_t lhs_count;
758 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
759
760 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
761 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
762 region_operator<Rect> operation(op, lhs_region, rhs_region);
763 { // scope for rasterizer (dtor has side effects)
764 rasterizer r(dst);
765 operation(r);
766 }
767
768#endif
769}
770
Colin Cross8f279962016-09-26 13:08:16 -0700771void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700772 const Region& lhs, const Region& rhs)
773{
774 boolean_operation(op, dst, lhs, rhs, 0, 0);
775}
776
Colin Cross8f279962016-09-26 13:08:16 -0700777void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700778 const Region& lhs, const Rect& rhs)
779{
780 boolean_operation(op, dst, lhs, rhs, 0, 0);
781}
782
783void Region::translate(Region& reg, int dx, int dy)
784{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700785 if ((dx || dy) && !reg.isEmpty()) {
Chong Zhang639a1e12019-04-22 14:01:20 -0700786#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700787 validate(reg, "translate (before)");
788#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700789 size_t count = reg.mStorage.size();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000790 Rect* rects = reg.mStorage.data();
Mathias Agopian20f68782009-05-11 00:03:41 -0700791 while (count) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700792 rects->offsetBy(dx, dy);
Mathias Agopian20f68782009-05-11 00:03:41 -0700793 rects++;
794 count--;
795 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700796#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700797 validate(reg, "translate (after)");
798#endif
799 }
800}
801
802void Region::translate(Region& dst, const Region& reg, int dx, int dy)
803{
804 dst = reg;
805 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806}
807
808// ----------------------------------------------------------------------------
809
Mathias Agopiane1424282013-07-29 21:24:40 -0700810size_t Region::getFlattenedSize() const {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700811 return sizeof(uint32_t) + mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700812}
813
Mathias Agopiane1424282013-07-29 21:24:40 -0700814status_t Region::flatten(void* buffer, size_t size) const {
Chong Zhang639a1e12019-04-22 14:01:20 -0700815#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700816 validate(*this, "Region::flatten");
817#endif
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700818 if (size < getFlattenedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700819 return NO_MEMORY;
820 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700821 // Cast to uint32_t since the size of a size_t can vary between 32- and
822 // 64-bit processes
823 FlattenableUtils::write(buffer, size, static_cast<uint32_t>(mStorage.size()));
824 for (auto rect : mStorage) {
825 status_t result = rect.flatten(buffer, size);
826 if (result != NO_ERROR) {
827 return result;
828 }
829 FlattenableUtils::advance(buffer, size, sizeof(rect));
830 }
Mathias Agopian8683fca2012-08-12 19:37:16 -0700831 return NO_ERROR;
832}
833
834status_t Region::unflatten(void const* buffer, size_t size) {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700835 if (size < sizeof(uint32_t)) {
836 return NO_MEMORY;
Mathias Agopian20f68782009-05-11 00:03:41 -0700837 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700838
839 uint32_t numRects = 0;
840 FlattenableUtils::read(buffer, size, numRects);
841 if (size < numRects * sizeof(Rect)) {
842 return NO_MEMORY;
843 }
844
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700845 if (numRects > (UINT32_MAX / sizeof(Rect))) {
Yi Kong48d76082019-03-24 02:01:06 -0700846 android_errorWriteWithInfoLog(0x534e4554, "29983260", -1, nullptr, 0);
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700847 return NO_MEMORY;
848 }
849
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700850 Region result;
851 result.mStorage.clear();
852 for (size_t r = 0; r < numRects; ++r) {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700853 Rect rect(Rect::EMPTY_RECT);
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700854 status_t status = rect.unflatten(buffer, size);
855 if (status != NO_ERROR) {
856 return status;
857 }
858 FlattenableUtils::advance(buffer, size, sizeof(rect));
859 result.mStorage.push_back(rect);
860 }
861
Chong Zhang639a1e12019-04-22 14:01:20 -0700862#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700863 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700864#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700865
866 if (!result.validate(result, "Region::unflatten", true)) {
867 ALOGE("Region::unflatten() failed, invalid region");
868 return BAD_VALUE;
869 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000870 mStorage.clear();
871 mStorage.insert(mStorage.begin(), result.mStorage.begin(), result.mStorage.end());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700872 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800873}
874
Mathias Agopian20f68782009-05-11 00:03:41 -0700875// ----------------------------------------------------------------------------
876
877Region::const_iterator Region::begin() const {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000878 return mStorage.data();
Mathias Agopian20f68782009-05-11 00:03:41 -0700879}
880
881Region::const_iterator Region::end() const {
Dan Stoza2d023062018-04-09 12:14:55 -0700882 // Workaround for b/77643177
883 // mStorage should never be empty, but somehow it is and it's causing
884 // an abort in ubsan
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000885 if (mStorage.empty()) return mStorage.data();
Dan Stoza2d023062018-04-09 12:14:55 -0700886
Mathias Agopian3ab68552012-08-31 14:31:40 -0700887 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000888 return mStorage.data() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700889}
890
891Rect const* Region::getArray(size_t* count) const {
Dan Stozad3182402014-11-17 12:03:59 -0800892 if (count) *count = static_cast<size_t>(end() - begin());
893 return begin();
Mathias Agopian20f68782009-05-11 00:03:41 -0700894}
895
Mathias Agopian20f68782009-05-11 00:03:41 -0700896// ----------------------------------------------------------------------------
897
Yiwei Zhang5434a782018-12-05 18:06:32 -0800898void Region::dump(std::string& out, const char* what, uint32_t /* flags */) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700899 const_iterator head = begin();
900 const_iterator const tail = end();
901
Yiwei Zhang5434a782018-12-05 18:06:32 -0800902 StringAppendF(&out, " Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail - head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700903 while (head != tail) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800904 StringAppendF(&out, " [%3d, %3d, %3d, %3d]\n", head->left, head->top, head->right,
905 head->bottom);
Dan Stozad3182402014-11-17 12:03:59 -0800906 ++head;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800907 }
908}
909
Dan Stozad3182402014-11-17 12:03:59 -0800910void Region::dump(const char* what, uint32_t /* flags */) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800911{
Mathias Agopian20f68782009-05-11 00:03:41 -0700912 const_iterator head = begin();
913 const_iterator const tail = end();
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700914 ALOGD(" Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700915 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000916 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700917 head->left, head->top, head->right, head->bottom);
918 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 }
920}
921
922// ----------------------------------------------------------------------------
923
924}; // namespace android