blob: 618c7d62b12f879c4b2dc25320ff622d665f2077 [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
Mathias Agopian20f68782009-05-11 00:03:41 -070022#include <utils/Log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/String8.h>
Mathias Agopian068d47f2012-09-11 18:56:23 -070024#include <utils/CallStack.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// ----------------------------------------------------------------------------
33#define VALIDATE_REGIONS (false)
34#define VALIDATE_WITH_CORECG (false)
35// ----------------------------------------------------------------------------
36
37#if VALIDATE_WITH_CORECG
38#include <core/SkRegion.h>
39#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040
41namespace android {
Mathias Agopian20f68782009-05-11 00:03:41 -070042// ----------------------------------------------------------------------------
43
44enum {
45 op_nand = region_operator<Rect>::op_nand,
46 op_and = region_operator<Rect>::op_and,
47 op_or = region_operator<Rect>::op_or,
48 op_xor = region_operator<Rect>::op_xor
49};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Chris Craik3e010f32013-02-25 19:12:47 -080051enum {
52 direction_LTR,
53 direction_RTL
54};
55
Dan Stoza5065a552015-03-17 16:23:42 -070056const Region Region::INVALID_REGION(Rect::INVALID_RECT);
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058// ----------------------------------------------------------------------------
59
Mathias Agopian3ab68552012-08-31 14:31:40 -070060Region::Region() {
61 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062}
63
64Region::Region(const Region& rhs)
Mathias Agopian3ab68552012-08-31 14:31:40 -070065 : mStorage(rhs.mStorage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066{
Mathias Agopiand0b55c02011-03-16 23:18:07 -070067#if VALIDATE_REGIONS
68 validate(rhs, "rhs copy-ctor");
69#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070}
71
Mathias Agopian3ab68552012-08-31 14:31:40 -070072Region::Region(const Rect& rhs) {
73 mStorage.add(rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074}
75
76Region::~Region()
77{
78}
79
Chris Craik3e010f32013-02-25 19:12:47 -080080/**
81 * Copy rects from the src vector into the dst vector, resolving vertical T-Junctions along the way
82 *
83 * First pass through, divideSpanRTL will be set because the 'previous span' (indexing into the dst
84 * vector) will be reversed. Each rectangle in the original list, starting from the bottom, will be
85 * compared with the span directly below, and subdivided as needed to resolve T-junctions.
86 *
87 * The resulting temporary vector will be a completely reversed copy of the original, without any
88 * bottom-up T-junctions.
89 *
90 * Second pass through, divideSpanRTL will be false since the previous span will index into the
91 * final, correctly ordered region buffer. Each rectangle will be compared with the span directly
92 * above it, and subdivided to resolve any remaining T-junctions.
93 */
94static void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end,
95 Vector<Rect>& dst, int spanDirection) {
96 dst.clear();
97
98 const Rect* current = end - 1;
99 int lastTop = current->top;
100
101 // add first span immediately
102 do {
103 dst.add(*current);
104 current--;
105 } while (current->top == lastTop && current >= begin);
106
Dan Stozad3182402014-11-17 12:03:59 -0800107 int beginLastSpan = -1;
108 int endLastSpan = -1;
Chris Craik3e010f32013-02-25 19:12:47 -0800109 int top = -1;
110 int bottom = -1;
111
112 // for all other spans, split if a t-junction exists in the span directly above
113 while (current >= begin) {
114 if (current->top != (current + 1)->top) {
115 // new span
116 if ((spanDirection == direction_RTL && current->bottom != (current + 1)->top) ||
117 (spanDirection == direction_LTR && current->top != (current + 1)->bottom)) {
118 // previous span not directly adjacent, don't check for T junctions
119 beginLastSpan = INT_MAX;
120 } else {
121 beginLastSpan = endLastSpan + 1;
122 }
Dan Stozad3182402014-11-17 12:03:59 -0800123 endLastSpan = static_cast<int>(dst.size()) - 1;
Chris Craik3e010f32013-02-25 19:12:47 -0800124
125 top = current->top;
126 bottom = current->bottom;
127 }
128 int left = current->left;
129 int right = current->right;
130
Dan Stozad3182402014-11-17 12:03:59 -0800131 for (int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) {
132 // prevIndex can't be -1 here because if endLastSpan is set to a
133 // value greater than -1 (allowing the loop to execute),
134 // beginLastSpan (and therefore prevIndex) will also be increased
ywenaef04452015-03-26 19:51:12 +0800135 const Rect prev = dst[static_cast<size_t>(prevIndex)];
Chris Craik3e010f32013-02-25 19:12:47 -0800136 if (spanDirection == direction_RTL) {
137 // iterating over previous span RTL, quit if it's too far left
ywenaef04452015-03-26 19:51:12 +0800138 if (prev.right <= left) break;
Chris Craik3e010f32013-02-25 19:12:47 -0800139
ywenaef04452015-03-26 19:51:12 +0800140 if (prev.right > left && prev.right < right) {
141 dst.add(Rect(prev.right, top, right, bottom));
142 right = prev.right;
Chris Craik3e010f32013-02-25 19:12:47 -0800143 }
144
ywenaef04452015-03-26 19:51:12 +0800145 if (prev.left > left && prev.left < right) {
146 dst.add(Rect(prev.left, top, right, bottom));
147 right = prev.left;
Chris Craik3e010f32013-02-25 19:12:47 -0800148 }
149
150 // if an entry in the previous span is too far right, nothing further left in the
151 // current span will need it
ywenaef04452015-03-26 19:51:12 +0800152 if (prev.left >= right) {
Chris Craik3e010f32013-02-25 19:12:47 -0800153 beginLastSpan = prevIndex;
154 }
155 } else {
156 // iterating over previous span LTR, quit if it's too far right
ywenaef04452015-03-26 19:51:12 +0800157 if (prev.left >= right) break;
Chris Craik3e010f32013-02-25 19:12:47 -0800158
ywenaef04452015-03-26 19:51:12 +0800159 if (prev.left > left && prev.left < right) {
160 dst.add(Rect(left, top, prev.left, bottom));
161 left = prev.left;
Chris Craik3e010f32013-02-25 19:12:47 -0800162 }
163
ywenaef04452015-03-26 19:51:12 +0800164 if (prev.right > left && prev.right < right) {
165 dst.add(Rect(left, top, prev.right, bottom));
166 left = prev.right;
Chris Craik3e010f32013-02-25 19:12:47 -0800167 }
168 // if an entry in the previous span is too far left, nothing further right in the
169 // current span will need it
ywenaef04452015-03-26 19:51:12 +0800170 if (prev.right <= left) {
Chris Craik3e010f32013-02-25 19:12:47 -0800171 beginLastSpan = prevIndex;
172 }
173 }
174 }
175
176 if (left < right) {
177 dst.add(Rect(left, top, right, bottom));
178 }
179
180 current--;
181 }
182}
183
184/**
185 * Creates a new region with the same data as the argument, but divides rectangles as necessary to
186 * remove T-Junctions
187 *
188 * Note: the output will not necessarily be a very efficient representation of the region, since it
189 * may be that a triangle-based approach would generate significantly simpler geometry
190 */
191Region Region::createTJunctionFreeRegion(const Region& r) {
192 if (r.isEmpty()) return r;
193 if (r.isRect()) return r;
194
195 Vector<Rect> reversed;
196 reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL);
197
198 Region outputRegion;
199 reverseRectsResolvingJunctions(reversed.begin(), reversed.end(),
200 outputRegion.mStorage, direction_LTR);
201 outputRegion.mStorage.add(r.getBounds()); // to make region valid, mStorage must end with bounds
202
203#if VALIDATE_REGIONS
204 validate(outputRegion, "T-Junction free region");
205#endif
206
207 return outputRegion;
208}
209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210Region& Region::operator = (const Region& rhs)
211{
Mathias Agopian20f68782009-05-11 00:03:41 -0700212#if VALIDATE_REGIONS
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700213 validate(*this, "this->operator=");
214 validate(rhs, "rhs.operator=");
Mathias Agopian20f68782009-05-11 00:03:41 -0700215#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700216 mStorage = rhs.mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 return *this;
218}
219
Mathias Agopian9f961452009-06-29 18:46:37 -0700220Region& Region::makeBoundsSelf()
221{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700222 if (mStorage.size() >= 2) {
223 const Rect bounds(getBounds());
224 mStorage.clear();
225 mStorage.add(bounds);
226 }
Mathias Agopian9f961452009-06-29 18:46:37 -0700227 return *this;
228}
229
Michael Wright1c284a92014-02-10 13:00:14 -0800230bool Region::contains(const Point& point) const {
231 return contains(point.x, point.y);
232}
233
234bool Region::contains(int x, int y) const {
235 const_iterator cur = begin();
236 const_iterator const tail = end();
237 while (cur != tail) {
238 if (y >= cur->top && y < cur->bottom && x >= cur->left && x < cur->right) {
239 return true;
240 }
241 cur++;
242 }
243 return false;
244}
245
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800246void Region::clear()
247{
Mathias Agopian20f68782009-05-11 00:03:41 -0700248 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700249 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250}
251
252void Region::set(const Rect& r)
253{
Mathias Agopian20f68782009-05-11 00:03:41 -0700254 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700255 mStorage.add(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800256}
257
Dan Stozad3182402014-11-17 12:03:59 -0800258void Region::set(int32_t w, int32_t h)
Mathias Agopian0926f502009-05-04 14:17:04 -0700259{
Mathias Agopian20f68782009-05-11 00:03:41 -0700260 mStorage.clear();
Dan Stozad3182402014-11-17 12:03:59 -0800261 mStorage.add(Rect(w, h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700262}
263
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +0100264void Region::set(uint32_t w, uint32_t h)
265{
266 mStorage.clear();
267 mStorage.add(Rect(w, h));
268}
269
Mathias Agopian2ca79392013-04-02 18:30:32 -0700270bool Region::isTriviallyEqual(const Region& region) const {
271 return begin() == region.begin();
272}
273
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800274// ----------------------------------------------------------------------------
275
Mathias Agopian20f68782009-05-11 00:03:41 -0700276void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700278 Rect rect(l,t,r,b);
279 size_t where = mStorage.size() - 1;
280 mStorage.insertAt(rect, where, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281}
282
Mathias Agopian20f68782009-05-11 00:03:41 -0700283// ----------------------------------------------------------------------------
284
285Region& Region::orSelf(const Rect& r) {
286 return operationSelf(r, op_or);
287}
Romain Guyb8a2e982012-02-07 17:04:34 -0800288Region& Region::xorSelf(const Rect& r) {
289 return operationSelf(r, op_xor);
290}
Mathias Agopian20f68782009-05-11 00:03:41 -0700291Region& Region::andSelf(const Rect& r) {
292 return operationSelf(r, op_and);
293}
294Region& Region::subtractSelf(const Rect& r) {
295 return operationSelf(r, op_nand);
296}
Colin Cross8f279962016-09-26 13:08:16 -0700297Region& Region::operationSelf(const Rect& r, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700298 Region lhs(*this);
299 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 return *this;
301}
302
303// ----------------------------------------------------------------------------
304
305Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700306 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307}
Romain Guyb8a2e982012-02-07 17:04:34 -0800308Region& Region::xorSelf(const Region& rhs) {
309 return operationSelf(rhs, op_xor);
310}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700312 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700315 return operationSelf(rhs, op_nand);
316}
Colin Cross8f279962016-09-26 13:08:16 -0700317Region& Region::operationSelf(const Region& rhs, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700318 Region lhs(*this);
319 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320 return *this;
321}
322
323Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700324 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 return *this;
326}
327
Riddle Hsu39d4aa52018-11-30 20:46:53 +0800328Region& Region::scaleSelf(float sx, float sy) {
Robert Carre07e1032018-11-26 12:55:53 -0800329 size_t count = mStorage.size();
330 Rect* rects = mStorage.editArray();
331 while (count) {
Riddle Hsu39d4aa52018-11-30 20:46:53 +0800332 rects->left = static_cast<int32_t>(rects->left * sx + 0.5f);
333 rects->right = static_cast<int32_t>(rects->right * sx + 0.5f);
334 rects->top = static_cast<int32_t>(rects->top * sy + 0.5f);
335 rects->bottom = static_cast<int32_t>(rects->bottom * sy + 0.5f);
Robert Carre07e1032018-11-26 12:55:53 -0800336 rects++;
337 count--;
338 }
339 return *this;
340}
341
Mathias Agopian20f68782009-05-11 00:03:41 -0700342// ----------------------------------------------------------------------------
343
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700344const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700345 return operation(rhs, op_or);
346}
Romain Guyb8a2e982012-02-07 17:04:34 -0800347const Region Region::mergeExclusive(const Rect& rhs) const {
348 return operation(rhs, op_xor);
349}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700350const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700351 return operation(rhs, op_and);
352}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700353const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700354 return operation(rhs, op_nand);
355}
Colin Cross8f279962016-09-26 13:08:16 -0700356const Region Region::operation(const Rect& rhs, uint32_t op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700357 Region result;
358 boolean_operation(op, result, *this, rhs);
359 return result;
360}
361
362// ----------------------------------------------------------------------------
363
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700364const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700365 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366}
Romain Guyb8a2e982012-02-07 17:04:34 -0800367const Region Region::mergeExclusive(const Region& rhs) const {
368 return operation(rhs, op_xor);
369}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700370const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700371 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800372}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700373const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700374 return operation(rhs, op_nand);
375}
Colin Cross8f279962016-09-26 13:08:16 -0700376const Region Region::operation(const Region& rhs, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700378 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 return result;
380}
381
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700382const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700384 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800385 return result;
386}
387
388// ----------------------------------------------------------------------------
389
390Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700391 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392}
Romain Guyb8a2e982012-02-07 17:04:34 -0800393Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
394 return operationSelf(rhs, dx, dy, op_xor);
395}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800396Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700397 return operationSelf(rhs, dx, dy, op_and);
398}
399Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
400 return operationSelf(rhs, dx, dy, op_nand);
401}
Colin Cross8f279962016-09-26 13:08:16 -0700402Region& Region::operationSelf(const Region& rhs, int dx, int dy, uint32_t op) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700403 Region lhs(*this);
404 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405 return *this;
406}
407
Mathias Agopian20f68782009-05-11 00:03:41 -0700408// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700410const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700411 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800412}
Romain Guyb8a2e982012-02-07 17:04:34 -0800413const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
414 return operation(rhs, dx, dy, op_xor);
415}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700416const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700417 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800418}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700419const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700420 return operation(rhs, dx, dy, op_nand);
421}
Colin Cross8f279962016-09-26 13:08:16 -0700422const Region Region::operation(const Region& rhs, int dx, int dy, uint32_t op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700424 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800425 return result;
426}
427
428// ----------------------------------------------------------------------------
429
Mathias Agopian20f68782009-05-11 00:03:41 -0700430// This is our region rasterizer, which merges rects and spans together
431// to obtain an optimal region.
Dan Stozad3182402014-11-17 12:03:59 -0800432class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700434 Rect bounds;
Mathias Agopian20f68782009-05-11 00:03:41 -0700435 Vector<Rect>& storage;
436 Rect* head;
437 Rect* tail;
438 Vector<Rect> span;
439 Rect* cur;
440public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700441 explicit rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700442 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700443 storage.clear();
444 }
445
Dan Stozad3182402014-11-17 12:03:59 -0800446 virtual ~rasterizer();
447
448 virtual void operator()(const Rect& rect);
449
Mathias Agopian20f68782009-05-11 00:03:41 -0700450private:
Dan Stozad3182402014-11-17 12:03:59 -0800451 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700452 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800453 template<typename T>
Mathias Agopian20f68782009-05-11 00:03:41 -0700454 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
Dan Stozad3182402014-11-17 12:03:59 -0800455
456 void flushSpan();
Mathias Agopian20f68782009-05-11 00:03:41 -0700457};
458
Dan Stozad3182402014-11-17 12:03:59 -0800459Region::rasterizer::~rasterizer()
460{
461 if (span.size()) {
462 flushSpan();
463 }
464 if (storage.size()) {
465 bounds.top = storage.itemAt(0).top;
466 bounds.bottom = storage.top().bottom;
467 if (storage.size() == 1) {
468 storage.clear();
469 }
470 } else {
471 bounds.left = 0;
472 bounds.right = 0;
473 }
474 storage.add(bounds);
475}
476
477void Region::rasterizer::operator()(const Rect& rect)
478{
479 //ALOGD(">>> %3d, %3d, %3d, %3d",
480 // rect.left, rect.top, rect.right, rect.bottom);
481 if (span.size()) {
482 if (cur->top != rect.top) {
483 flushSpan();
484 } else if (cur->right == rect.left) {
485 cur->right = rect.right;
486 return;
487 }
488 }
489 span.add(rect);
490 cur = span.editArray() + (span.size() - 1);
491}
492
493void Region::rasterizer::flushSpan()
494{
495 bool merge = false;
496 if (tail-head == ssize_t(span.size())) {
497 Rect const* p = span.editArray();
498 Rect const* q = head;
499 if (p->top == q->bottom) {
500 merge = true;
501 while (q != tail) {
502 if ((p->left != q->left) || (p->right != q->right)) {
503 merge = false;
504 break;
505 }
Stephen Hines9c22c3c2016-03-31 22:02:38 -0700506 p++;
507 q++;
Dan Stozad3182402014-11-17 12:03:59 -0800508 }
509 }
510 }
511 if (merge) {
512 const int bottom = span[0].bottom;
513 Rect* r = head;
514 while (r != tail) {
515 r->bottom = bottom;
516 r++;
517 }
518 } else {
519 bounds.left = min(span.itemAt(0).left, bounds.left);
520 bounds.right = max(span.top().right, bounds.right);
521 storage.appendVector(span);
522 tail = storage.editArray() + storage.size();
523 head = tail - span.size();
524 }
525 span.clear();
526}
527
Mathias Agopian068d47f2012-09-11 18:56:23 -0700528bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700529{
Chia-I Wub420b582018-02-07 11:53:41 -0800530 if (reg.mStorage.isEmpty()) {
531 ALOGE_IF(!silent, "%s: mStorage is empty, which is never valid", name);
532 // return immediately as the code below assumes mStorage is non-empty
533 return false;
534 }
535
Mathias Agopian20f68782009-05-11 00:03:41 -0700536 bool result = true;
537 const_iterator cur = reg.begin();
538 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700539 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700540 Rect b(*prev);
541 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700542 if (cur->isValid() == false) {
Dan Stoza5065a552015-03-17 16:23:42 -0700543 // We allow this particular flavor of invalid Rect, since it is used
544 // as a signal value in various parts of the system
545 if (*cur != Rect::INVALID_RECT) {
546 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
547 result = false;
548 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700549 }
550 if (cur->right > region_operator<Rect>::max_value) {
551 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
552 result = false;
553 }
554 if (cur->bottom > region_operator<Rect>::max_value) {
555 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
556 result = false;
557 }
558 if (prev != cur) {
559 b.left = b.left < cur->left ? b.left : cur->left;
560 b.top = b.top < cur->top ? b.top : cur->top;
561 b.right = b.right > cur->right ? b.right : cur->right;
562 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
563 if ((*prev < *cur) == false) {
564 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700565 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700566 }
567 if (cur->top == prev->top) {
568 if (cur->bottom != prev->bottom) {
569 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
570 result = false;
571 } else if (cur->left < prev->right) {
572 ALOGE_IF(!silent,
573 "%s: spans overlap horizontally prev=%p, cur=%p",
574 name, prev, cur);
575 result = false;
576 }
577 } else if (cur->top < prev->bottom) {
578 ALOGE_IF(!silent,
579 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700580 name, prev, cur);
581 result = false;
582 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700583 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700584 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700585 cur++;
586 }
587 if (b != reg.getBounds()) {
588 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700589 ALOGE_IF(!silent,
590 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700591 b.left, b.top, b.right, b.bottom,
592 reg.getBounds().left, reg.getBounds().top,
593 reg.getBounds().right, reg.getBounds().bottom);
594 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700595 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700596 result = false;
597 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700598 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700599 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700600 reg.dump(name);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700601 CallStack stack(LOG_TAG);
Mathias Agopian20f68782009-05-11 00:03:41 -0700602 }
603 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800604}
605
Colin Cross8f279962016-09-26 13:08:16 -0700606void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700607 const Region& lhs,
608 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609{
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700610#if VALIDATE_REGIONS
611 validate(lhs, "boolean_operation (before): lhs");
612 validate(rhs, "boolean_operation (before): rhs");
613 validate(dst, "boolean_operation (before): dst");
614#endif
615
Mathias Agopian20f68782009-05-11 00:03:41 -0700616 size_t lhs_count;
617 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
618
619 size_t rhs_count;
620 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
621
622 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
623 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
624 region_operator<Rect> operation(op, lhs_region, rhs_region);
625 { // scope for rasterizer (dtor has side effects)
626 rasterizer r(dst);
627 operation(r);
628 }
629
630#if VALIDATE_REGIONS
631 validate(lhs, "boolean_operation: lhs");
632 validate(rhs, "boolean_operation: rhs");
633 validate(dst, "boolean_operation: dst");
634#endif
635
636#if VALIDATE_WITH_CORECG
637 SkRegion sk_lhs;
638 SkRegion sk_rhs;
639 SkRegion sk_dst;
640
641 for (size_t i=0 ; i<lhs_count ; i++)
642 sk_lhs.op(
643 lhs_rects[i].left + dx,
644 lhs_rects[i].top + dy,
645 lhs_rects[i].right + dx,
646 lhs_rects[i].bottom + dy,
647 SkRegion::kUnion_Op);
648
649 for (size_t i=0 ; i<rhs_count ; i++)
650 sk_rhs.op(
651 rhs_rects[i].left + dx,
652 rhs_rects[i].top + dy,
653 rhs_rects[i].right + dx,
654 rhs_rects[i].bottom + dy,
655 SkRegion::kUnion_Op);
656
657 const char* name = "---";
658 SkRegion::Op sk_op;
659 switch (op) {
660 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800661 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700662 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
663 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
664 }
665 sk_dst.op(sk_lhs, sk_rhs, sk_op);
666
667 if (sk_dst.isEmpty() && dst.isEmpty())
668 return;
669
670 bool same = true;
671 Region::const_iterator head = dst.begin();
672 Region::const_iterator const tail = dst.end();
673 SkRegion::Iterator it(sk_dst);
674 while (!it.done()) {
675 if (head != tail) {
676 if (
677 head->left != it.rect().fLeft ||
678 head->top != it.rect().fTop ||
679 head->right != it.rect().fRight ||
680 head->bottom != it.rect().fBottom
681 ) {
682 same = false;
683 break;
684 }
685 } else {
686 same = false;
687 break;
688 }
689 head++;
690 it.next();
691 }
692
693 if (head != tail) {
694 same = false;
695 }
696
697 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000698 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700699 lhs.dump("lhs");
700 rhs.dump("rhs");
701 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000702 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700703 SkRegion::Iterator it(sk_dst);
704 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000705 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700706 it.rect().fLeft,
707 it.rect().fTop,
708 it.rect().fRight,
709 it.rect().fBottom);
710 it.next();
711 }
712 }
713#endif
714}
715
Colin Cross8f279962016-09-26 13:08:16 -0700716void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700717 const Region& lhs,
718 const Rect& rhs, int dx, int dy)
719{
Dan Stoza5065a552015-03-17 16:23:42 -0700720 // We allow this particular flavor of invalid Rect, since it is used as a
721 // signal value in various parts of the system
722 if (!rhs.isValid() && rhs != Rect::INVALID_RECT) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000723 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700724 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700725 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700726 }
727
Mathias Agopian20f68782009-05-11 00:03:41 -0700728#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS
729 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
730#else
731 size_t lhs_count;
732 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
733
734 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
735 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
736 region_operator<Rect> operation(op, lhs_region, rhs_region);
737 { // scope for rasterizer (dtor has side effects)
738 rasterizer r(dst);
739 operation(r);
740 }
741
742#endif
743}
744
Colin Cross8f279962016-09-26 13:08:16 -0700745void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700746 const Region& lhs, const Region& rhs)
747{
748 boolean_operation(op, dst, lhs, rhs, 0, 0);
749}
750
Colin Cross8f279962016-09-26 13:08:16 -0700751void Region::boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700752 const Region& lhs, const Rect& rhs)
753{
754 boolean_operation(op, dst, lhs, rhs, 0, 0);
755}
756
757void Region::translate(Region& reg, int dx, int dy)
758{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700759 if ((dx || dy) && !reg.isEmpty()) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700760#if VALIDATE_REGIONS
761 validate(reg, "translate (before)");
762#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700763 size_t count = reg.mStorage.size();
764 Rect* rects = reg.mStorage.editArray();
765 while (count) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700766 rects->offsetBy(dx, dy);
Mathias Agopian20f68782009-05-11 00:03:41 -0700767 rects++;
768 count--;
769 }
770#if VALIDATE_REGIONS
771 validate(reg, "translate (after)");
772#endif
773 }
774}
775
776void Region::translate(Region& dst, const Region& reg, int dx, int dy)
777{
778 dst = reg;
779 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800780}
781
782// ----------------------------------------------------------------------------
783
Mathias Agopiane1424282013-07-29 21:24:40 -0700784size_t Region::getFlattenedSize() const {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700785 return sizeof(uint32_t) + mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700786}
787
Mathias Agopiane1424282013-07-29 21:24:40 -0700788status_t Region::flatten(void* buffer, size_t size) const {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700789#if VALIDATE_REGIONS
790 validate(*this, "Region::flatten");
791#endif
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700792 if (size < getFlattenedSize()) {
Mathias Agopiane1424282013-07-29 21:24:40 -0700793 return NO_MEMORY;
794 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700795 // Cast to uint32_t since the size of a size_t can vary between 32- and
796 // 64-bit processes
797 FlattenableUtils::write(buffer, size, static_cast<uint32_t>(mStorage.size()));
798 for (auto rect : mStorage) {
799 status_t result = rect.flatten(buffer, size);
800 if (result != NO_ERROR) {
801 return result;
802 }
803 FlattenableUtils::advance(buffer, size, sizeof(rect));
804 }
Mathias Agopian8683fca2012-08-12 19:37:16 -0700805 return NO_ERROR;
806}
807
808status_t Region::unflatten(void const* buffer, size_t size) {
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700809 if (size < sizeof(uint32_t)) {
810 return NO_MEMORY;
Mathias Agopian20f68782009-05-11 00:03:41 -0700811 }
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700812
813 uint32_t numRects = 0;
814 FlattenableUtils::read(buffer, size, numRects);
815 if (size < numRects * sizeof(Rect)) {
816 return NO_MEMORY;
817 }
818
Pablo Ceballos1a65fcc2016-07-13 14:11:57 -0700819 if (numRects > (UINT32_MAX / sizeof(Rect))) {
820 android_errorWriteWithInfoLog(0x534e4554, "29983260", -1, NULL, 0);
821 return NO_MEMORY;
822 }
823
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700824 Region result;
825 result.mStorage.clear();
826 for (size_t r = 0; r < numRects; ++r) {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700827 Rect rect(Rect::EMPTY_RECT);
Dan Stoza6fbefbb2015-03-23 13:46:14 -0700828 status_t status = rect.unflatten(buffer, size);
829 if (status != NO_ERROR) {
830 return status;
831 }
832 FlattenableUtils::advance(buffer, size, sizeof(rect));
833 result.mStorage.push_back(rect);
834 }
835
Mathias Agopian3ab68552012-08-31 14:31:40 -0700836#if VALIDATE_REGIONS
Mathias Agopian068d47f2012-09-11 18:56:23 -0700837 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700838#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700839
840 if (!result.validate(result, "Region::unflatten", true)) {
841 ALOGE("Region::unflatten() failed, invalid region");
842 return BAD_VALUE;
843 }
844 mStorage = result.mStorage;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700845 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800846}
847
Mathias Agopian20f68782009-05-11 00:03:41 -0700848// ----------------------------------------------------------------------------
849
850Region::const_iterator Region::begin() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700851 return mStorage.array();
Mathias Agopian20f68782009-05-11 00:03:41 -0700852}
853
854Region::const_iterator Region::end() const {
Dan Stoza2d023062018-04-09 12:14:55 -0700855 // Workaround for b/77643177
856 // mStorage should never be empty, but somehow it is and it's causing
857 // an abort in ubsan
858 if (mStorage.isEmpty()) return mStorage.array();
859
Mathias Agopian3ab68552012-08-31 14:31:40 -0700860 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
861 return mStorage.array() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700862}
863
864Rect const* Region::getArray(size_t* count) const {
Dan Stozad3182402014-11-17 12:03:59 -0800865 if (count) *count = static_cast<size_t>(end() - begin());
866 return begin();
Mathias Agopian20f68782009-05-11 00:03:41 -0700867}
868
Mathias Agopian20f68782009-05-11 00:03:41 -0700869// ----------------------------------------------------------------------------
870
Dan Stozad3182402014-11-17 12:03:59 -0800871void Region::dump(String8& out, const char* what, uint32_t /* flags */) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800872{
Mathias Agopian20f68782009-05-11 00:03:41 -0700873 const_iterator head = begin();
874 const_iterator const tail = end();
875
Dan Stozad3182402014-11-17 12:03:59 -0800876 out.appendFormat(" Region %s (this=%p, count=%" PRIdPTR ")\n",
877 what, this, tail - head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700878 while (head != tail) {
Dan Stozad3182402014-11-17 12:03:59 -0800879 out.appendFormat(" [%3d, %3d, %3d, %3d]\n", head->left, head->top,
880 head->right, head->bottom);
881 ++head;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 }
883}
884
Dan Stozad3182402014-11-17 12:03:59 -0800885void Region::dump(const char* what, uint32_t /* flags */) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886{
Mathias Agopian20f68782009-05-11 00:03:41 -0700887 const_iterator head = begin();
888 const_iterator const tail = end();
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700889 ALOGD(" Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700890 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000891 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700892 head->left, head->top, head->right, head->bottom);
893 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 }
895}
896
897// ----------------------------------------------------------------------------
898
899}; // namespace android