blob: 1222cd6fad7a6fa831ee46d76d12b2fe93a695d7 [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
26#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <ui/Region.h>
Mathias Agopian20f68782009-05-11 00:03:41 -070028#include <ui/Point.h>
29
30#include <private/ui/RegionHelper.h>
31
32// ----------------------------------------------------------------------------
Chong Zhang639a1e12019-04-22 14:01:20 -070033
34// ### VALIDATE_REGIONS ###
35// To enable VALIDATE_REGIONS traces, use the "libui-validate-regions-defaults"
36// in Android.bp. Do not #define VALIDATE_REGIONS here as it requires extra libs.
37
Mathias Agopian20f68782009-05-11 00:03:41 -070038#define VALIDATE_WITH_CORECG (false)
39// ----------------------------------------------------------------------------
40
Chong Zhang639a1e12019-04-22 14:01:20 -070041#if defined(VALIDATE_REGIONS)
42#include <utils/CallStack.h>
43#endif
44
Mathias Agopian20f68782009-05-11 00:03:41 -070045#if VALIDATE_WITH_CORECG
46#include <core/SkRegion.h>
47#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
49namespace android {
Mathias Agopian20f68782009-05-11 00:03:41 -070050// ----------------------------------------------------------------------------
51
Yiwei Zhang5434a782018-12-05 18:06:32 -080052using base::StringAppendF;
53
Mathias Agopian20f68782009-05-11 00:03:41 -070054enum {
55 op_nand = region_operator<Rect>::op_nand,
56 op_and = region_operator<Rect>::op_and,
57 op_or = region_operator<Rect>::op_or,
58 op_xor = region_operator<Rect>::op_xor
59};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Chris Craik3e010f32013-02-25 19:12:47 -080061enum {
62 direction_LTR,
63 direction_RTL
64};
65
Dan Stoza5065a552015-03-17 16:23:42 -070066const Region Region::INVALID_REGION(Rect::INVALID_RECT);
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068// ----------------------------------------------------------------------------
69
Mathias Agopian3ab68552012-08-31 14:31:40 -070070Region::Region() {
71 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072}
73
74Region::Region(const Region& rhs)
Mathias Agopian3ab68552012-08-31 14:31:40 -070075 : mStorage(rhs.mStorage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076{
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) {
83 mStorage.add(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 */
104static void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end,
105 Vector<Rect>& dst, int spanDirection) {
106 dst.clear();
107
108 const Rect* current = end - 1;
109 int lastTop = current->top;
110
111 // add first span immediately
112 do {
113 dst.add(*current);
114 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) {
151 dst.add(Rect(prev.right, top, right, bottom));
152 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) {
156 dst.add(Rect(prev.left, top, right, bottom));
157 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) {
170 dst.add(Rect(left, top, prev.left, bottom));
171 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) {
175 dst.add(Rect(left, top, prev.right, bottom));
176 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) {
187 dst.add(Rect(left, top, right, bottom));
188 }
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
205 Vector<Rect> reversed;
206 reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL);
207
208 Region outputRegion;
209 reverseRectsResolvingJunctions(reversed.begin(), reversed.end(),
210 outputRegion.mStorage, direction_LTR);
211 outputRegion.mStorage.add(r.getBounds()); // to make region valid, mStorage must end with bounds
212
Chong Zhang639a1e12019-04-22 14:01:20 -0700213#if defined(VALIDATE_REGIONS)
Chris Craik3e010f32013-02-25 19:12:47 -0800214 validate(outputRegion, "T-Junction free region");
215#endif
216
217 return outputRegion;
218}
219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220Region& Region::operator = (const Region& rhs)
221{
Chong Zhang639a1e12019-04-22 14:01:20 -0700222#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700223 validate(*this, "this->operator=");
224 validate(rhs, "rhs.operator=");
Mathias Agopian20f68782009-05-11 00:03:41 -0700225#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700226 mStorage = rhs.mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 return *this;
228}
229
Mathias Agopian9f961452009-06-29 18:46:37 -0700230Region& Region::makeBoundsSelf()
231{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700232 if (mStorage.size() >= 2) {
233 const Rect bounds(getBounds());
234 mStorage.clear();
235 mStorage.add(bounds);
236 }
Mathias Agopian9f961452009-06-29 18:46:37 -0700237 return *this;
238}
239
Michael Wright1c284a92014-02-10 13:00:14 -0800240bool Region::contains(const Point& point) const {
241 return contains(point.x, point.y);
242}
243
244bool Region::contains(int x, int y) const {
245 const_iterator cur = begin();
246 const_iterator const tail = end();
247 while (cur != tail) {
248 if (y >= cur->top && y < cur->bottom && x >= cur->left && x < cur->right) {
249 return true;
250 }
251 cur++;
252 }
253 return false;
254}
255
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256void Region::clear()
257{
Mathias Agopian20f68782009-05-11 00:03:41 -0700258 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700259 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260}
261
262void Region::set(const Rect& r)
263{
Mathias Agopian20f68782009-05-11 00:03:41 -0700264 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700265 mStorage.add(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266}
267
Dan Stozad3182402014-11-17 12:03:59 -0800268void Region::set(int32_t w, int32_t h)
Mathias Agopian0926f502009-05-04 14:17:04 -0700269{
Mathias Agopian20f68782009-05-11 00:03:41 -0700270 mStorage.clear();
Dan Stozad3182402014-11-17 12:03:59 -0800271 mStorage.add(Rect(w, h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700272}
273
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +0100274void Region::set(uint32_t w, uint32_t h)
275{
276 mStorage.clear();
277 mStorage.add(Rect(w, h));
278}
279
Mathias Agopian2ca79392013-04-02 18:30:32 -0700280bool Region::isTriviallyEqual(const Region& region) const {
281 return begin() == region.begin();
282}
283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284// ----------------------------------------------------------------------------
285
Mathias Agopian20f68782009-05-11 00:03:41 -0700286void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700288 Rect rect(l,t,r,b);
289 size_t where = mStorage.size() - 1;
290 mStorage.insertAt(rect, where, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800291}
292
Mathias Agopian20f68782009-05-11 00:03:41 -0700293// ----------------------------------------------------------------------------
294
295Region& Region::orSelf(const Rect& r) {
296 return operationSelf(r, op_or);
297}
Romain Guyb8a2e982012-02-07 17:04:34 -0800298Region& Region::xorSelf(const Rect& r) {
299 return operationSelf(r, op_xor);
300}
Mathias Agopian20f68782009-05-11 00:03:41 -0700301Region& Region::andSelf(const Rect& r) {
302 return operationSelf(r, op_and);
303}
304Region& Region::subtractSelf(const Rect& r) {
305 return operationSelf(r, op_nand);
306}
Colin Cross8f279962016-09-26 13:08:16 -0700307Region& Region::operationSelf(const Rect& r, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700308 Region lhs(*this);
309 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310 return *this;
311}
312
313// ----------------------------------------------------------------------------
314
315Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700316 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317}
Romain Guyb8a2e982012-02-07 17:04:34 -0800318Region& Region::xorSelf(const Region& rhs) {
319 return operationSelf(rhs, op_xor);
320}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700322 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800324Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700325 return operationSelf(rhs, op_nand);
326}
Colin Cross8f279962016-09-26 13:08:16 -0700327Region& Region::operationSelf(const Region& rhs, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700328 Region lhs(*this);
329 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800330 return *this;
331}
332
333Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700334 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 return *this;
336}
337
Riddle Hsu39d4aa52018-11-30 20:46:53 +0800338Region& Region::scaleSelf(float sx, float sy) {
Robert Carre07e1032018-11-26 12:55:53 -0800339 size_t count = mStorage.size();
340 Rect* rects = mStorage.editArray();
341 while (count) {
Nick Desaulniersea6c7132019-10-15 19:14:39 -0700342 rects->left = static_cast<int32_t>(static_cast<float>(rects->left) * sx + 0.5f);
343 rects->right = static_cast<int32_t>(static_cast<float>(rects->right) * sx + 0.5f);
344 rects->top = static_cast<int32_t>(static_cast<float>(rects->top) * sy + 0.5f);
345 rects->bottom = static_cast<int32_t>(static_cast<float>(rects->bottom) * sy + 0.5f);
Robert Carre07e1032018-11-26 12:55:53 -0800346 rects++;
347 count--;
348 }
349 return *this;
350}
351
Mathias Agopian20f68782009-05-11 00:03:41 -0700352// ----------------------------------------------------------------------------
353
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700354const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700355 return operation(rhs, op_or);
356}
Romain Guyb8a2e982012-02-07 17:04:34 -0800357const Region Region::mergeExclusive(const Rect& rhs) const {
358 return operation(rhs, op_xor);
359}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700360const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700361 return operation(rhs, op_and);
362}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700363const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700364 return operation(rhs, op_nand);
365}
Colin Cross8f279962016-09-26 13:08:16 -0700366const Region Region::operation(const Rect& rhs, uint32_t op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700367 Region result;
368 boolean_operation(op, result, *this, rhs);
369 return result;
370}
371
372// ----------------------------------------------------------------------------
373
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700374const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700375 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376}
Romain Guyb8a2e982012-02-07 17:04:34 -0800377const Region Region::mergeExclusive(const Region& rhs) const {
378 return operation(rhs, op_xor);
379}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700380const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700381 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700383const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700384 return operation(rhs, op_nand);
385}
Colin Cross8f279962016-09-26 13:08:16 -0700386const Region Region::operation(const Region& rhs, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700388 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 return result;
390}
391
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700392const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800393 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700394 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800395 return result;
396}
397
398// ----------------------------------------------------------------------------
399
400Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700401 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800402}
Romain Guyb8a2e982012-02-07 17:04:34 -0800403Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
404 return operationSelf(rhs, dx, dy, op_xor);
405}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800406Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700407 return operationSelf(rhs, dx, dy, op_and);
408}
409Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
410 return operationSelf(rhs, dx, dy, op_nand);
411}
Colin Cross8f279962016-09-26 13:08:16 -0700412Region& Region::operationSelf(const Region& rhs, int dx, int dy, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700413 Region lhs(*this);
414 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415 return *this;
416}
417
Mathias Agopian20f68782009-05-11 00:03:41 -0700418// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800419
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700420const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700421 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800422}
Romain Guyb8a2e982012-02-07 17:04:34 -0800423const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
424 return operation(rhs, dx, dy, op_xor);
425}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700426const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700427 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800428}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700429const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700430 return operation(rhs, dx, dy, op_nand);
431}
Colin Cross8f279962016-09-26 13:08:16 -0700432const Region Region::operation(const Region& rhs, int dx, int dy, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700434 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800435 return result;
436}
437
438// ----------------------------------------------------------------------------
439
Mathias Agopian20f68782009-05-11 00:03:41 -0700440// This is our region rasterizer, which merges rects and spans together
441// to obtain an optimal region.
Dan Stozad3182402014-11-17 12:03:59 -0800442class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700444 Rect bounds;
Mathias Agopian20f68782009-05-11 00:03:41 -0700445 Vector<Rect>& storage;
446 Rect* head;
447 Rect* tail;
448 Vector<Rect> span;
449 Rect* cur;
450public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700451 explicit rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700452 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700453 storage.clear();
454 }
455
Dan Stozad3182402014-11-17 12:03:59 -0800456 virtual ~rasterizer();
457
458 virtual void operator()(const Rect& rect);
459
Mathias Agopian20f68782009-05-11 00:03:41 -0700460private:
Dan Stozad3182402014-11-17 12:03:59 -0800461 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700462 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800463 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700464 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800465
466 void flushSpan();
Mathias Agopian20f68782009-05-11 00:03:41 -0700467};
468
Dan Stozad3182402014-11-17 12:03:59 -0800469Region::rasterizer::~rasterizer()
470{
471 if (span.size()) {
472 flushSpan();
473 }
474 if (storage.size()) {
475 bounds.top = storage.itemAt(0).top;
476 bounds.bottom = storage.top().bottom;
477 if (storage.size() == 1) {
478 storage.clear();
479 }
480 } else {
481 bounds.left = 0;
482 bounds.right = 0;
483 }
484 storage.add(bounds);
485}
486
487void Region::rasterizer::operator()(const Rect& rect)
488{
489 //ALOGD(">>> %3d, %3d, %3d, %3d",
490 // rect.left, rect.top, rect.right, rect.bottom);
491 if (span.size()) {
492 if (cur->top != rect.top) {
493 flushSpan();
494 } else if (cur->right == rect.left) {
495 cur->right = rect.right;
496 return;
497 }
498 }
499 span.add(rect);
500 cur = span.editArray() + (span.size() - 1);
501}
502
503void Region::rasterizer::flushSpan()
504{
505 bool merge = false;
506 if (tail-head == ssize_t(span.size())) {
507 Rect const* p = span.editArray();
508 Rect const* q = head;
509 if (p->top == q->bottom) {
510 merge = true;
511 while (q != tail) {
512 if ((p->left != q->left) || (p->right != q->right)) {
513 merge = false;
514 break;
515 }
Stephen Hines9c22c3c2016-03-31 22:02:38 -0700516 p++;
517 q++;
Dan Stozad3182402014-11-17 12:03:59 -0800518 }
519 }
520 }
521 if (merge) {
522 const int bottom = span[0].bottom;
523 Rect* r = head;
524 while (r != tail) {
525 r->bottom = bottom;
526 r++;
527 }
528 } else {
529 bounds.left = min(span.itemAt(0).left, bounds.left);
530 bounds.right = max(span.top().right, bounds.right);
531 storage.appendVector(span);
532 tail = storage.editArray() + storage.size();
533 head = tail - span.size();
534 }
535 span.clear();
536}
537
Mathias Agopian068d47f2012-09-11 18:56:23 -0700538bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700539{
Chia-I Wub420b582018-02-07 11:53:41 -0800540 if (reg.mStorage.isEmpty()) {
541 ALOGE_IF(!silent, "%s: mStorage is empty, which is never valid", name);
542 // return immediately as the code below assumes mStorage is non-empty
543 return false;
544 }
545
Mathias Agopian20f68782009-05-11 00:03:41 -0700546 bool result = true;
547 const_iterator cur = reg.begin();
548 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700549 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700550 Rect b(*prev);
551 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700552 if (cur->isValid() == false) {
Dan Stoza5065a552015-03-17 16:23:42 -0700553 // We allow this particular flavor of invalid Rect, since it is used
554 // as a signal value in various parts of the system
555 if (*cur != Rect::INVALID_RECT) {
556 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
557 result = false;
558 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700559 }
560 if (cur->right > region_operator<Rect>::max_value) {
561 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
562 result = false;
563 }
564 if (cur->bottom > region_operator<Rect>::max_value) {
565 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
566 result = false;
567 }
568 if (prev != cur) {
569 b.left = b.left < cur->left ? b.left : cur->left;
570 b.top = b.top < cur->top ? b.top : cur->top;
571 b.right = b.right > cur->right ? b.right : cur->right;
572 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
573 if ((*prev < *cur) == false) {
574 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700575 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700576 }
577 if (cur->top == prev->top) {
578 if (cur->bottom != prev->bottom) {
579 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
580 result = false;
581 } else if (cur->left < prev->right) {
582 ALOGE_IF(!silent,
583 "%s: spans overlap horizontally prev=%p, cur=%p",
584 name, prev, cur);
585 result = false;
586 }
587 } else if (cur->top < prev->bottom) {
588 ALOGE_IF(!silent,
589 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700590 name, prev, cur);
591 result = false;
592 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700593 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700594 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700595 cur++;
596 }
597 if (b != reg.getBounds()) {
598 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700599 ALOGE_IF(!silent,
600 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700601 b.left, b.top, b.right, b.bottom,
602 reg.getBounds().left, reg.getBounds().top,
603 reg.getBounds().right, reg.getBounds().bottom);
604 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700605 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700606 result = false;
607 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700608 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700609#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700610 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700611 reg.dump(name);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700612 CallStack stack(LOG_TAG);
Mathias Agopian20f68782009-05-11 00:03:41 -0700613 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700614#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700615 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800616}
617
Colin Cross8f279962016-09-26 13:08:16 -0700618void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700619 const Region& lhs,
620 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621{
Chong Zhang639a1e12019-04-22 14:01:20 -0700622#if defined(VALIDATE_REGIONS)
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700623 validate(lhs, "boolean_operation (before): lhs");
624 validate(rhs, "boolean_operation (before): rhs");
625 validate(dst, "boolean_operation (before): dst");
626#endif
627
Mathias Agopian20f68782009-05-11 00:03:41 -0700628 size_t lhs_count;
629 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
630
631 size_t rhs_count;
632 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
633
634 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
635 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
636 region_operator<Rect> operation(op, lhs_region, rhs_region);
637 { // scope for rasterizer (dtor has side effects)
638 rasterizer r(dst);
639 operation(r);
640 }
641
Chong Zhang639a1e12019-04-22 14:01:20 -0700642#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700643 validate(lhs, "boolean_operation: lhs");
644 validate(rhs, "boolean_operation: rhs");
645 validate(dst, "boolean_operation: dst");
646#endif
647
648#if VALIDATE_WITH_CORECG
649 SkRegion sk_lhs;
650 SkRegion sk_rhs;
651 SkRegion sk_dst;
652
653 for (size_t i=0 ; i<lhs_count ; i++)
654 sk_lhs.op(
655 lhs_rects[i].left + dx,
656 lhs_rects[i].top + dy,
657 lhs_rects[i].right + dx,
658 lhs_rects[i].bottom + dy,
659 SkRegion::kUnion_Op);
660
661 for (size_t i=0 ; i<rhs_count ; i++)
662 sk_rhs.op(
663 rhs_rects[i].left + dx,
664 rhs_rects[i].top + dy,
665 rhs_rects[i].right + dx,
666 rhs_rects[i].bottom + dy,
667 SkRegion::kUnion_Op);
668
669 const char* name = "---";
670 SkRegion::Op sk_op;
671 switch (op) {
672 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800673 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700674 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
675 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
676 }
677 sk_dst.op(sk_lhs, sk_rhs, sk_op);
678
679 if (sk_dst.isEmpty() && dst.isEmpty())
680 return;
681
682 bool same = true;
683 Region::const_iterator head = dst.begin();
684 Region::const_iterator const tail = dst.end();
685 SkRegion::Iterator it(sk_dst);
686 while (!it.done()) {
687 if (head != tail) {
688 if (
689 head->left != it.rect().fLeft ||
690 head->top != it.rect().fTop ||
691 head->right != it.rect().fRight ||
692 head->bottom != it.rect().fBottom
693 ) {
694 same = false;
695 break;
696 }
697 } else {
698 same = false;
699 break;
700 }
701 head++;
702 it.next();
703 }
704
705 if (head != tail) {
706 same = false;
707 }
708
709 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000710 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700711 lhs.dump("lhs");
712 rhs.dump("rhs");
713 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000714 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700715 SkRegion::Iterator it(sk_dst);
716 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000717 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700718 it.rect().fLeft,
719 it.rect().fTop,
720 it.rect().fRight,
721 it.rect().fBottom);
722 it.next();
723 }
724 }
725#endif
726}
727
Colin Cross8f279962016-09-26 13:08:16 -0700728void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700729 const Region& lhs,
730 const Rect& rhs, int dx, int dy)
731{
Dan Stoza5065a552015-03-17 16:23:42 -0700732 // We allow this particular flavor of invalid Rect, since it is used as a
733 // signal value in various parts of the system
734 if (!rhs.isValid() && rhs != Rect::INVALID_RECT) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000735 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700736 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700737 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700738 }
739
Chong Zhang639a1e12019-04-22 14:01:20 -0700740#if VALIDATE_WITH_CORECG || defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700741 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
742#else
743 size_t lhs_count;
744 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
745
746 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
747 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
748 region_operator<Rect> operation(op, lhs_region, rhs_region);
749 { // scope for rasterizer (dtor has side effects)
750 rasterizer r(dst);
751 operation(r);
752 }
753
754#endif
755}
756
Colin Cross8f279962016-09-26 13:08:16 -0700757void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700758 const Region& lhs, const Region& rhs)
759{
760 boolean_operation(op, dst, lhs, rhs, 0, 0);
761}
762
Colin Cross8f279962016-09-26 13:08:16 -0700763void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700764 const Region& lhs, const Rect& rhs)
765{
766 boolean_operation(op, dst, lhs, rhs, 0, 0);
767}
768
769void Region::translate(Region& reg, int dx, int dy)
770{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700771 if ((dx || dy) && !reg.isEmpty()) {
Chong Zhang639a1e12019-04-22 14:01:20 -0700772#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700773 validate(reg, "translate (before)");
774#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700775 size_t count = reg.mStorage.size();
776 Rect* rects = reg.mStorage.editArray();
777 while (count) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700778 rects->offsetBy(dx, dy);
Mathias Agopian20f68782009-05-11 00:03:41 -0700779 rects++;
780 count--;
781 }
Chong Zhang639a1e12019-04-22 14:01:20 -0700782#if defined(VALIDATE_REGIONS)
Mathias Agopian20f68782009-05-11 00:03:41 -0700783 validate(reg, "translate (after)");
784#endif
785 }
786}
787
788void Region::translate(Region& dst, const Region& reg, int dx, int dy)
789{
790 dst = reg;
791 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800792}
793
794// ----------------------------------------------------------------------------
795
Mathias Agopiane1424282013-07-29 21:24:40 -0700796size_t Region::getFlattenedSize() const {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700797 return sizeof(uint32_t) + mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700798}
799
Mathias Agopiane1424282013-07-29 21:24:40 -0700800status_t Region::flatten(void* buffer, size_t size) const {
Chong Zhang639a1e12019-04-22 14:01:20 -0700801#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700802 validate(*this, "Region::flatten");
803#endif
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700804 if (size < getFlattenedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700805 return NO_MEMORY;
806 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700807 // Cast to uint32_t since the size of a size_t can vary between 32- and
808 // 64-bit processes
809 FlattenableUtils::write(buffer, size, static_cast<uint32_t>(mStorage.size()));
810 for (auto rect : mStorage) {
811 status_t result = rect.flatten(buffer, size);
812 if (result != NO_ERROR) {
813 return result;
814 }
815 FlattenableUtils::advance(buffer, size, sizeof(rect));
816 }
Mathias Agopian8683fca2012-08-12 19:37:16 -0700817 return NO_ERROR;
818}
819
820status_t Region::unflatten(void const* buffer, size_t size) {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700821 if (size < sizeof(uint32_t)) {
822 return NO_MEMORY;
Mathias Agopian20f68782009-05-11 00:03:41 -0700823 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700824
825 uint32_t numRects = 0;
826 FlattenableUtils::read(buffer, size, numRects);
827 if (size < numRects * sizeof(Rect)) {
828 return NO_MEMORY;
829 }
830
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700831 if (numRects > (UINT32_MAX / sizeof(Rect))) {
Yi Kong48d76082019-03-24 02:01:06 -0700832 android_errorWriteWithInfoLog(0x534e4554, "29983260", -1, nullptr, 0);
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700833 return NO_MEMORY;
834 }
835
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700836 Region result;
837 result.mStorage.clear();
838 for (size_t r = 0; r < numRects; ++r) {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700839 Rect rect(Rect::EMPTY_RECT);
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700840 status_t status = rect.unflatten(buffer, size);
841 if (status != NO_ERROR) {
842 return status;
843 }
844 FlattenableUtils::advance(buffer, size, sizeof(rect));
845 result.mStorage.push_back(rect);
846 }
847
Chong Zhang639a1e12019-04-22 14:01:20 -0700848#if defined(VALIDATE_REGIONS)
Mathias Agopian068d47f2012-09-11 18:56:23 -0700849 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700850#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700851
852 if (!result.validate(result, "Region::unflatten", true)) {
853 ALOGE("Region::unflatten() failed, invalid region");
854 return BAD_VALUE;
855 }
856 mStorage = result.mStorage;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700857 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800858}
859
Mathias Agopian20f68782009-05-11 00:03:41 -0700860// ----------------------------------------------------------------------------
861
862Region::const_iterator Region::begin() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700863 return mStorage.array();
Mathias Agopian20f68782009-05-11 00:03:41 -0700864}
865
866Region::const_iterator Region::end() const {
Dan Stoza2d023062018-04-09 12:14:55 -0700867 // Workaround for b/77643177
868 // mStorage should never be empty, but somehow it is and it's causing
869 // an abort in ubsan
870 if (mStorage.isEmpty()) return mStorage.array();
871
Mathias Agopian3ab68552012-08-31 14:31:40 -0700872 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
873 return mStorage.array() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700874}
875
876Rect const* Region::getArray(size_t* count) const {
Dan Stozad3182402014-11-17 12:03:59 -0800877 if (count) *count = static_cast<size_t>(end() - begin());
878 return begin();
Mathias Agopian20f68782009-05-11 00:03:41 -0700879}
880
Mathias Agopian20f68782009-05-11 00:03:41 -0700881// ----------------------------------------------------------------------------
882
Yiwei Zhang5434a782018-12-05 18:06:32 -0800883void Region::dump(std::string& out, const char* what, uint32_t /* flags */) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700884 const_iterator head = begin();
885 const_iterator const tail = end();
886
Yiwei Zhang5434a782018-12-05 18:06:32 -0800887 StringAppendF(&out, " Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail - head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700888 while (head != tail) {
Yiwei Zhang5434a782018-12-05 18:06:32 -0800889 StringAppendF(&out, " [%3d, %3d, %3d, %3d]\n", head->left, head->top, head->right,
890 head->bottom);
Dan Stozad3182402014-11-17 12:03:59 -0800891 ++head;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892 }
893}
894
Dan Stozad3182402014-11-17 12:03:59 -0800895void Region::dump(const char* what, uint32_t /* flags */) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896{
Mathias Agopian20f68782009-05-11 00:03:41 -0700897 const_iterator head = begin();
898 const_iterator const tail = end();
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700899 ALOGD(" Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700900 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000901 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700902 head->left, head->top, head->right, head->bottom);
903 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800904 }
905}
906
907// ----------------------------------------------------------------------------
908
909}; // namespace android