blob: e01309b6797df3a85ae9694127baa7077bd8820e [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
Siarhei Vishniakoufb9fcda2020-05-04 14:59:19 -0700227 if (this == &rhs) {
228 // Already equal to itself
229 return *this;
230 }
231
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000232 mStorage.clear();
233 mStorage.insert(mStorage.begin(), rhs.mStorage.begin(), rhs.mStorage.end());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234 return *this;
235}
236
Mathias Agopian9f961452009-06-29 18:46:37 -0700237Region& Region::makeBoundsSelf()
238{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700239 if (mStorage.size() >= 2) {
240 const Rect bounds(getBounds());
241 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000242 mStorage.push_back(bounds);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700243 }
Mathias Agopian9f961452009-06-29 18:46:37 -0700244 return *this;
245}
246
Michael Wright1c284a92014-02-10 13:00:14 -0800247bool Region::contains(const Point& point) const {
248 return contains(point.x, point.y);
249}
250
251bool Region::contains(int x, int y) const {
252 const_iterator cur = begin();
253 const_iterator const tail = end();
254 while (cur != tail) {
255 if (y >= cur->top && y < cur->bottom && x >= cur->left && x < cur->right) {
256 return true;
257 }
258 cur++;
259 }
260 return false;
261}
262
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800263void Region::clear()
264{
Mathias Agopian20f68782009-05-11 00:03:41 -0700265 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000266 mStorage.push_back(Rect(0, 0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800267}
268
269void Region::set(const Rect& r)
270{
Mathias Agopian20f68782009-05-11 00:03:41 -0700271 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000272 mStorage.push_back(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273}
274
Dan Stozad3182402014-11-17 12:03:59 -0800275void Region::set(int32_t w, int32_t h)
Mathias Agopian0926f502009-05-04 14:17:04 -0700276{
Mathias Agopian20f68782009-05-11 00:03:41 -0700277 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000278 mStorage.push_back(Rect(w, h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700279}
280
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +0100281void Region::set(uint32_t w, uint32_t h)
282{
283 mStorage.clear();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000284 mStorage.push_back(Rect(w, h));
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +0100285}
286
Mathias Agopian2ca79392013-04-02 18:30:32 -0700287bool Region::isTriviallyEqual(const Region& region) const {
288 return begin() == region.begin();
289}
290
Lloyd Piqueea629282019-12-03 15:57:10 -0800291bool Region::hasSameRects(const Region& other) const {
292 size_t thisRectCount = 0;
293 android::Rect const* thisRects = getArray(&thisRectCount);
294 size_t otherRectCount = 0;
295 android::Rect const* otherRects = other.getArray(&otherRectCount);
296
297 if (thisRectCount != otherRectCount) return false;
298
299 for (size_t i = 0; i < thisRectCount; i++) {
300 if (thisRects[i] != otherRects[i]) return false;
301 }
302 return true;
303}
304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305// ----------------------------------------------------------------------------
306
Mathias Agopian20f68782009-05-11 00:03:41 -0700307void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700309 Rect rect(l,t,r,b);
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000310 mStorage.insert(mStorage.end() - 1, rect);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311}
312
Mathias Agopian20f68782009-05-11 00:03:41 -0700313// ----------------------------------------------------------------------------
314
315Region& Region::orSelf(const Rect& r) {
Dan Stoza547808b2020-04-02 09:31:08 -0700316 if (isEmpty()) {
317 set(r);
318 return *this;
319 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700320 return operationSelf(r, op_or);
321}
Romain Guyb8a2e982012-02-07 17:04:34 -0800322Region& Region::xorSelf(const Rect& r) {
323 return operationSelf(r, op_xor);
324}
Mathias Agopian20f68782009-05-11 00:03:41 -0700325Region& Region::andSelf(const Rect& r) {
326 return operationSelf(r, op_and);
327}
328Region& Region::subtractSelf(const Rect& r) {
329 return operationSelf(r, op_nand);
330}
Colin Cross8f279962016-09-26 13:08:16 -0700331Region& Region::operationSelf(const Rect& r, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700332 Region lhs(*this);
333 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800334 return *this;
335}
336
337// ----------------------------------------------------------------------------
338
339Region& Region::orSelf(const Region& rhs) {
Dan Stoza547808b2020-04-02 09:31:08 -0700340 if (isEmpty()) {
341 *this = rhs;
342 return *this;
343 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700344 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800345}
Romain Guyb8a2e982012-02-07 17:04:34 -0800346Region& Region::xorSelf(const Region& rhs) {
347 return operationSelf(rhs, op_xor);
348}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700350 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800352Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700353 return operationSelf(rhs, op_nand);
354}
Colin Cross8f279962016-09-26 13:08:16 -0700355Region& Region::operationSelf(const Region& rhs, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700356 Region lhs(*this);
357 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800358 return *this;
359}
360
361Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700362 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363 return *this;
364}
365
Riddle Hsu39d4aa52018-11-30 20:46:53 +0800366Region& Region::scaleSelf(float sx, float sy) {
Robert Carre07e1032018-11-26 12:55:53 -0800367 size_t count = mStorage.size();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000368 Rect* rects = mStorage.data();
Robert Carre07e1032018-11-26 12:55:53 -0800369 while (count) {
Nick Desaulniersea6c7132019-10-15 19:14:39 -0700370 rects->left = static_cast<int32_t>(static_cast<float>(rects->left) * sx + 0.5f);
371 rects->right = static_cast<int32_t>(static_cast<float>(rects->right) * sx + 0.5f);
372 rects->top = static_cast<int32_t>(static_cast<float>(rects->top) * sy + 0.5f);
373 rects->bottom = static_cast<int32_t>(static_cast<float>(rects->bottom) * sy + 0.5f);
Robert Carre07e1032018-11-26 12:55:53 -0800374 rects++;
375 count--;
376 }
377 return *this;
378}
379
Mathias Agopian20f68782009-05-11 00:03:41 -0700380// ----------------------------------------------------------------------------
381
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700382const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700383 return operation(rhs, op_or);
384}
Romain Guyb8a2e982012-02-07 17:04:34 -0800385const Region Region::mergeExclusive(const Rect& rhs) const {
386 return operation(rhs, op_xor);
387}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700388const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700389 return operation(rhs, op_and);
390}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700391const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700392 return operation(rhs, op_nand);
393}
Colin Cross8f279962016-09-26 13:08:16 -0700394const Region Region::operation(const Rect& rhs, uint32_t op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700395 Region result;
396 boolean_operation(op, result, *this, rhs);
397 return result;
398}
399
400// ----------------------------------------------------------------------------
401
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700402const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700403 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404}
Romain Guyb8a2e982012-02-07 17:04:34 -0800405const Region Region::mergeExclusive(const Region& rhs) const {
406 return operation(rhs, op_xor);
407}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700408const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700409 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700411const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700412 return operation(rhs, op_nand);
413}
Colin Cross8f279962016-09-26 13:08:16 -0700414const Region Region::operation(const Region& rhs, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700416 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800417 return result;
418}
419
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700420const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800421 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700422 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 return result;
424}
425
426// ----------------------------------------------------------------------------
427
428Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700429 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800430}
Romain Guyb8a2e982012-02-07 17:04:34 -0800431Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
432 return operationSelf(rhs, dx, dy, op_xor);
433}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800434Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700435 return operationSelf(rhs, dx, dy, op_and);
436}
437Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
438 return operationSelf(rhs, dx, dy, op_nand);
439}
Colin Cross8f279962016-09-26 13:08:16 -0700440Region& Region::operationSelf(const Region& rhs, int dx, int dy, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700441 Region lhs(*this);
442 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 return *this;
444}
445
Mathias Agopian20f68782009-05-11 00:03:41 -0700446// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800447
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700448const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700449 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800450}
Romain Guyb8a2e982012-02-07 17:04:34 -0800451const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
452 return operation(rhs, dx, dy, op_xor);
453}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700454const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700455 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800456}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700457const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700458 return operation(rhs, dx, dy, op_nand);
459}
Colin Cross8f279962016-09-26 13:08:16 -0700460const Region Region::operation(const Region& rhs, int dx, int dy, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800461 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700462 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800463 return result;
464}
465
466// ----------------------------------------------------------------------------
467
Mathias Agopian20f68782009-05-11 00:03:41 -0700468// This is our region rasterizer, which merges rects and spans together
469// to obtain an optimal region.
Dan Stozad3182402014-11-17 12:03:59 -0800470class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700472 Rect bounds;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000473 FatVector<Rect>& storage;
Mathias Agopian20f68782009-05-11 00:03:41 -0700474 Rect* head;
475 Rect* tail;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000476 FatVector<Rect> span;
Mathias Agopian20f68782009-05-11 00:03:41 -0700477 Rect* cur;
478public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700479 explicit rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700480 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700481 storage.clear();
482 }
483
Dan Stozad3182402014-11-17 12:03:59 -0800484 virtual ~rasterizer();
485
486 virtual void operator()(const Rect& rect);
487
Mathias Agopian20f68782009-05-11 00:03:41 -0700488private:
Dan Stozad3182402014-11-17 12:03:59 -0800489 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700490 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800491 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700492 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800493
494 void flushSpan();
Mathias Agopian20f68782009-05-11 00:03:41 -0700495};
496
Dan Stozad3182402014-11-17 12:03:59 -0800497Region::rasterizer::~rasterizer()
498{
499 if (span.size()) {
500 flushSpan();
501 }
502 if (storage.size()) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000503 bounds.top = storage.front().top;
504 bounds.bottom = storage.back().bottom;
Dan Stozad3182402014-11-17 12:03:59 -0800505 if (storage.size() == 1) {
506 storage.clear();
507 }
508 } else {
509 bounds.left = 0;
510 bounds.right = 0;
511 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000512 storage.push_back(bounds);
Dan Stozad3182402014-11-17 12:03:59 -0800513}
514
515void Region::rasterizer::operator()(const Rect& rect)
516{
517 //ALOGD(">>> %3d, %3d, %3d, %3d",
518 // rect.left, rect.top, rect.right, rect.bottom);
519 if (span.size()) {
520 if (cur->top != rect.top) {
521 flushSpan();
522 } else if (cur->right == rect.left) {
523 cur->right = rect.right;
524 return;
525 }
526 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000527 span.push_back(rect);
528 cur = span.data() + (span.size() - 1);
Dan Stozad3182402014-11-17 12:03:59 -0800529}
530
531void Region::rasterizer::flushSpan()
532{
533 bool merge = false;
534 if (tail-head == ssize_t(span.size())) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000535 Rect const* p = span.data();
Dan Stozad3182402014-11-17 12:03:59 -0800536 Rect const* q = head;
537 if (p->top == q->bottom) {
538 merge = true;
539 while (q != tail) {
540 if ((p->left != q->left) || (p->right != q->right)) {
541 merge = false;
542 break;
543 }
Stephen Hines9c22c3c2016-03-31 22:02:38 -0700544 p++;
545 q++;
Dan Stozad3182402014-11-17 12:03:59 -0800546 }
547 }
548 }
549 if (merge) {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000550 const int bottom = span.front().bottom;
Dan Stozad3182402014-11-17 12:03:59 -0800551 Rect* r = head;
552 while (r != tail) {
553 r->bottom = bottom;
554 r++;
555 }
556 } else {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000557 bounds.left = min(span.front().left, bounds.left);
558 bounds.right = max(span.back().right, bounds.right);
559 storage.insert(storage.end(), span.begin(), span.end());
560 tail = storage.data() + storage.size();
Dan Stozad3182402014-11-17 12:03:59 -0800561 head = tail - span.size();
562 }
563 span.clear();
564}
565
Mathias Agopian068d47f2012-09-11 18:56:23 -0700566bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700567{
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000568 if (reg.mStorage.empty()) {
Chia-I Wub420b582018-02-07 11:53:41 -0800569 ALOGE_IF(!silent, "%s: mStorage is empty, which is never valid", name);
570 // return immediately as the code below assumes mStorage is non-empty
571 return false;
572 }
573
Mathias Agopian20f68782009-05-11 00:03:41 -0700574 bool result = true;
575 const_iterator cur = reg.begin();
576 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700577 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700578 Rect b(*prev);
579 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700580 if (cur->isValid() == false) {
Dan Stoza5065a552015-03-17 16:23:42 -0700581 // We allow this particular flavor of invalid Rect, since it is used
582 // as a signal value in various parts of the system
583 if (*cur != Rect::INVALID_RECT) {
584 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
585 result = false;
586 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700587 }
588 if (cur->right > region_operator<Rect>::max_value) {
589 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
590 result = false;
591 }
592 if (cur->bottom > region_operator<Rect>::max_value) {
593 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
594 result = false;
595 }
596 if (prev != cur) {
597 b.left = b.left < cur->left ? b.left : cur->left;
598 b.top = b.top < cur->top ? b.top : cur->top;
599 b.right = b.right > cur->right ? b.right : cur->right;
600 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
601 if ((*prev < *cur) == false) {
602 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700603 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700604 }
605 if (cur->top == prev->top) {
606 if (cur->bottom != prev->bottom) {
607 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
608 result = false;
609 } else if (cur->left < prev->right) {
610 ALOGE_IF(!silent,
611 "%s: spans overlap horizontally prev=%p, cur=%p",
612 name, prev, cur);
613 result = false;
614 }
615 } else if (cur->top < prev->bottom) {
616 ALOGE_IF(!silent,
617 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700618 name, prev, cur);
619 result = false;
620 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700621 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700622 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700623 cur++;
624 }
625 if (b != reg.getBounds()) {
626 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700627 ALOGE_IF(!silent,
628 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700629 b.left, b.top, b.right, b.bottom,
630 reg.getBounds().left, reg.getBounds().top,
631 reg.getBounds().right, reg.getBounds().bottom);
632 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700633 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700634 result = false;
635 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700636 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700637#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700638 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700639 reg.dump(name);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700640 CallStack stack(LOG_TAG);
Mathias Agopian20f68782009-05-11 00:03:41 -0700641 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700642#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700643 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800644}
645
Colin Cross8f279962016-09-26 13:08:16 -0700646void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700647 const Region& lhs,
648 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649{
Chong Zhang639a1e12019-04-22 14:01:20 -0700650#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700651 validate(lhs, "boolean_operation (before): lhs");
652 validate(rhs, "boolean_operation (before): rhs");
653 validate(dst, "boolean_operation (before): dst");
654#endif
655
Mathias Agopian20f68782009-05-11 00:03:41 -0700656 size_t lhs_count;
657 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
658
659 size_t rhs_count;
660 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
661
662 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
663 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
664 region_operator<Rect> operation(op, lhs_region, rhs_region);
665 { // scope for rasterizer (dtor has side effects)
666 rasterizer r(dst);
667 operation(r);
668 }
669
Chong Zhang639a1e12019-04-22 14:01:20 -0700670#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700671 validate(lhs, "boolean_operation: lhs");
672 validate(rhs, "boolean_operation: rhs");
673 validate(dst, "boolean_operation: dst");
674#endif
675
676#if VALIDATE_WITH_CORECG
677 SkRegion sk_lhs;
678 SkRegion sk_rhs;
679 SkRegion sk_dst;
680
681 for (size_t i=0 ; i<lhs_count ; i++)
682 sk_lhs.op(
683 lhs_rects[i].left + dx,
684 lhs_rects[i].top + dy,
685 lhs_rects[i].right + dx,
686 lhs_rects[i].bottom + dy,
687 SkRegion::kUnion_Op);
688
689 for (size_t i=0 ; i<rhs_count ; i++)
690 sk_rhs.op(
691 rhs_rects[i].left + dx,
692 rhs_rects[i].top + dy,
693 rhs_rects[i].right + dx,
694 rhs_rects[i].bottom + dy,
695 SkRegion::kUnion_Op);
696
697 const char* name = "---";
698 SkRegion::Op sk_op;
699 switch (op) {
700 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800701 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700702 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
703 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
704 }
705 sk_dst.op(sk_lhs, sk_rhs, sk_op);
706
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000707 if (sk_dst.empty() && dst.empty()) return;
708
Mathias Agopian20f68782009-05-11 00:03:41 -0700709 bool same = true;
710 Region::const_iterator head = dst.begin();
711 Region::const_iterator const tail = dst.end();
712 SkRegion::Iterator it(sk_dst);
713 while (!it.done()) {
714 if (head != tail) {
715 if (
716 head->left != it.rect().fLeft ||
717 head->top != it.rect().fTop ||
718 head->right != it.rect().fRight ||
719 head->bottom != it.rect().fBottom
720 ) {
721 same = false;
722 break;
723 }
724 } else {
725 same = false;
726 break;
727 }
728 head++;
729 it.next();
730 }
731
732 if (head != tail) {
733 same = false;
734 }
735
736 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000737 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700738 lhs.dump("lhs");
739 rhs.dump("rhs");
740 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000741 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700742 SkRegion::Iterator it(sk_dst);
743 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000744 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700745 it.rect().fLeft,
746 it.rect().fTop,
747 it.rect().fRight,
748 it.rect().fBottom);
749 it.next();
750 }
751 }
752#endif
753}
754
Colin Cross8f279962016-09-26 13:08:16 -0700755void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700756 const Region& lhs,
757 const Rect& rhs, int dx, int dy)
758{
Dan Stoza5065a552015-03-17 16:23:42 -0700759 // We allow this particular flavor of invalid Rect, since it is used as a
760 // signal value in various parts of the system
761 if (!rhs.isValid() && rhs != Rect::INVALID_RECT) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000762 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700763 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700764 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700765 }
766
Chong Zhang639a1e12019-04-22 14:01:20 -0700767#if VALIDATE_WITH_CORECG || defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700768 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
769#else
770 size_t lhs_count;
771 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
772
773 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
774 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
775 region_operator<Rect> operation(op, lhs_region, rhs_region);
776 { // scope for rasterizer (dtor has side effects)
777 rasterizer r(dst);
778 operation(r);
779 }
780
781#endif
782}
783
Colin Cross8f279962016-09-26 13:08:16 -0700784void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700785 const Region& lhs, const Region& rhs)
786{
787 boolean_operation(op, dst, lhs, rhs, 0, 0);
788}
789
Colin Cross8f279962016-09-26 13:08:16 -0700790void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700791 const Region& lhs, const Rect& rhs)
792{
793 boolean_operation(op, dst, lhs, rhs, 0, 0);
794}
795
796void Region::translate(Region& reg, int dx, int dy)
797{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700798 if ((dx || dy) && !reg.isEmpty()) {
Chong Zhang639a1e12019-04-22 14:01:20 -0700799#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700800 validate(reg, "translate (before)");
801#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700802 size_t count = reg.mStorage.size();
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000803 Rect* rects = reg.mStorage.data();
Mathias Agopian20f68782009-05-11 00:03:41 -0700804 while (count) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700805 rects->offsetBy(dx, dy);
Mathias Agopian20f68782009-05-11 00:03:41 -0700806 rects++;
807 count--;
808 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700809#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700810 validate(reg, "translate (after)");
811#endif
812 }
813}
814
815void Region::translate(Region& dst, const Region& reg, int dx, int dy)
816{
817 dst = reg;
818 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819}
820
821// ----------------------------------------------------------------------------
822
Mathias Agopiane1424282013-07-29 21:24:40 -0700823size_t Region::getFlattenedSize() const {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700824 return sizeof(uint32_t) + mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700825}
826
Mathias Agopiane1424282013-07-29 21:24:40 -0700827status_t Region::flatten(void* buffer, size_t size) const {
Chong Zhang639a1e12019-04-22 14:01:20 -0700828#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700829 validate(*this, "Region::flatten");
830#endif
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700831 if (size < getFlattenedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700832 return NO_MEMORY;
833 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700834 // Cast to uint32_t since the size of a size_t can vary between 32- and
835 // 64-bit processes
836 FlattenableUtils::write(buffer, size, static_cast<uint32_t>(mStorage.size()));
837 for (auto rect : mStorage) {
838 status_t result = rect.flatten(buffer, size);
839 if (result != NO_ERROR) {
840 return result;
841 }
842 FlattenableUtils::advance(buffer, size, sizeof(rect));
843 }
Mathias Agopian8683fca2012-08-12 19:37:16 -0700844 return NO_ERROR;
845}
846
847status_t Region::unflatten(void const* buffer, size_t size) {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700848 if (size < sizeof(uint32_t)) {
849 return NO_MEMORY;
Mathias Agopian20f68782009-05-11 00:03:41 -0700850 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700851
852 uint32_t numRects = 0;
853 FlattenableUtils::read(buffer, size, numRects);
854 if (size < numRects * sizeof(Rect)) {
855 return NO_MEMORY;
856 }
857
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700858 if (numRects > (UINT32_MAX / sizeof(Rect))) {
Yi Kong48d76082019-03-24 02:01:06 -0700859 android_errorWriteWithInfoLog(0x534e4554, "29983260", -1, nullptr, 0);
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700860 return NO_MEMORY;
861 }
862
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700863 Region result;
864 result.mStorage.clear();
865 for (size_t r = 0; r < numRects; ++r) {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700866 Rect rect(Rect::EMPTY_RECT);
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700867 status_t status = rect.unflatten(buffer, size);
868 if (status != NO_ERROR) {
869 return status;
870 }
871 FlattenableUtils::advance(buffer, size, sizeof(rect));
872 result.mStorage.push_back(rect);
873 }
874
Chong Zhang639a1e12019-04-22 14:01:20 -0700875#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700876 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700877#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700878
879 if (!result.validate(result, "Region::unflatten", true)) {
880 ALOGE("Region::unflatten() failed, invalid region");
881 return BAD_VALUE;
882 }
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000883 mStorage.clear();
884 mStorage.insert(mStorage.begin(), result.mStorage.begin(), result.mStorage.end());
Mathias Agopian8683fca2012-08-12 19:37:16 -0700885 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886}
887
Mathias Agopian20f68782009-05-11 00:03:41 -0700888// ----------------------------------------------------------------------------
889
890Region::const_iterator Region::begin() const {
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000891 return mStorage.data();
Mathias Agopian20f68782009-05-11 00:03:41 -0700892}
893
894Region::const_iterator Region::end() const {
Dan Stoza2d023062018-04-09 12:14:55 -0700895 // Workaround for b/77643177
896 // mStorage should never be empty, but somehow it is and it's causing
897 // an abort in ubsan
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000898 if (mStorage.empty()) return mStorage.data();
Dan Stoza2d023062018-04-09 12:14:55 -0700899
Mathias Agopian3ab68552012-08-31 14:31:40 -0700900 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
Jagadeesh Pakaravoora9a6b462020-05-01 00:01:40 +0000901 return mStorage.data() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700902}
903
904Rect const* Region::getArray(size_t* count) const {
Dan Stozad3182402014-11-17 12:03:59 -0800905 if (count) *count = static_cast<size_t>(end() - begin());
906 return begin();
Mathias Agopian20f68782009-05-11 00:03:41 -0700907}
908
Mathias Agopian20f68782009-05-11 00:03:41 -0700909// ----------------------------------------------------------------------------
910
Yiwei Zhang5434a782018-12-05 18:06:32 -0800911void Region::dump(std::string& out, const char* what, uint32_t /* flags */) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700912 const_iterator head = begin();
913 const_iterator const tail = end();
914
Yiwei Zhang5434a782018-12-05 18:06:32 -0800915 StringAppendF(&out, " Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail - head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700916 while (head != tail) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800917 StringAppendF(&out, " [%3d, %3d, %3d, %3d]\n", head->left, head->top, head->right,
918 head->bottom);
Dan Stozad3182402014-11-17 12:03:59 -0800919 ++head;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800920 }
921}
922
Dan Stozad3182402014-11-17 12:03:59 -0800923void Region::dump(const char* what, uint32_t /* flags */) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924{
Mathias Agopian20f68782009-05-11 00:03:41 -0700925 const_iterator head = begin();
926 const_iterator const tail = end();
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700927 ALOGD(" Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700928 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000929 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700930 head->left, head->top, head->right, head->bottom);
931 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800932 }
933}
934
935// ----------------------------------------------------------------------------
936
937}; // namespace android