blob: 6d58f5649b1aa1e1e4fa6a3c0c994828a5cab033 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056// ----------------------------------------------------------------------------
57
Mathias Agopian3ab68552012-08-31 14:31:40 -070058Region::Region() {
59 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060}
61
62Region::Region(const Region& rhs)
Mathias Agopian3ab68552012-08-31 14:31:40 -070063 : mStorage(rhs.mStorage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064{
Mathias Agopiand0b55c02011-03-16 23:18:07 -070065#if VALIDATE_REGIONS
66 validate(rhs, "rhs copy-ctor");
67#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068}
69
Mathias Agopian3ab68552012-08-31 14:31:40 -070070Region::Region(const Rect& rhs) {
71 mStorage.add(rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072}
73
74Region::~Region()
75{
76}
77
Chris Craik3e010f32013-02-25 19:12:47 -080078/**
79 * Copy rects from the src vector into the dst vector, resolving vertical T-Junctions along the way
80 *
81 * First pass through, divideSpanRTL will be set because the 'previous span' (indexing into the dst
82 * vector) will be reversed. Each rectangle in the original list, starting from the bottom, will be
83 * compared with the span directly below, and subdivided as needed to resolve T-junctions.
84 *
85 * The resulting temporary vector will be a completely reversed copy of the original, without any
86 * bottom-up T-junctions.
87 *
88 * Second pass through, divideSpanRTL will be false since the previous span will index into the
89 * final, correctly ordered region buffer. Each rectangle will be compared with the span directly
90 * above it, and subdivided to resolve any remaining T-junctions.
91 */
92static void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end,
93 Vector<Rect>& dst, int spanDirection) {
94 dst.clear();
95
96 const Rect* current = end - 1;
97 int lastTop = current->top;
98
99 // add first span immediately
100 do {
101 dst.add(*current);
102 current--;
103 } while (current->top == lastTop && current >= begin);
104
105 unsigned int beginLastSpan = -1;
106 unsigned int endLastSpan = -1;
107 int top = -1;
108 int bottom = -1;
109
110 // for all other spans, split if a t-junction exists in the span directly above
111 while (current >= begin) {
112 if (current->top != (current + 1)->top) {
113 // new span
114 if ((spanDirection == direction_RTL && current->bottom != (current + 1)->top) ||
115 (spanDirection == direction_LTR && current->top != (current + 1)->bottom)) {
116 // previous span not directly adjacent, don't check for T junctions
117 beginLastSpan = INT_MAX;
118 } else {
119 beginLastSpan = endLastSpan + 1;
120 }
121 endLastSpan = dst.size() - 1;
122
123 top = current->top;
124 bottom = current->bottom;
125 }
126 int left = current->left;
127 int right = current->right;
128
129 for (unsigned int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) {
130 const Rect* prev = &dst[prevIndex];
131 if (spanDirection == direction_RTL) {
132 // iterating over previous span RTL, quit if it's too far left
133 if (prev->right <= left) break;
134
135 if (prev->right > left && prev->right < right) {
136 dst.add(Rect(prev->right, top, right, bottom));
137 right = prev->right;
138 }
139
140 if (prev->left > left && prev->left < right) {
141 dst.add(Rect(prev->left, top, right, bottom));
142 right = prev->left;
143 }
144
145 // if an entry in the previous span is too far right, nothing further left in the
146 // current span will need it
147 if (prev->left >= right) {
148 beginLastSpan = prevIndex;
149 }
150 } else {
151 // iterating over previous span LTR, quit if it's too far right
152 if (prev->left >= right) break;
153
154 if (prev->left > left && prev->left < right) {
155 dst.add(Rect(left, top, prev->left, bottom));
156 left = prev->left;
157 }
158
159 if (prev->right > left && prev->right < right) {
160 dst.add(Rect(left, top, prev->right, bottom));
161 left = prev->right;
162 }
163 // if an entry in the previous span is too far left, nothing further right in the
164 // current span will need it
165 if (prev->right <= left) {
166 beginLastSpan = prevIndex;
167 }
168 }
169 }
170
171 if (left < right) {
172 dst.add(Rect(left, top, right, bottom));
173 }
174
175 current--;
176 }
177}
178
179/**
180 * Creates a new region with the same data as the argument, but divides rectangles as necessary to
181 * remove T-Junctions
182 *
183 * Note: the output will not necessarily be a very efficient representation of the region, since it
184 * may be that a triangle-based approach would generate significantly simpler geometry
185 */
186Region Region::createTJunctionFreeRegion(const Region& r) {
187 if (r.isEmpty()) return r;
188 if (r.isRect()) return r;
189
190 Vector<Rect> reversed;
191 reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL);
192
193 Region outputRegion;
194 reverseRectsResolvingJunctions(reversed.begin(), reversed.end(),
195 outputRegion.mStorage, direction_LTR);
196 outputRegion.mStorage.add(r.getBounds()); // to make region valid, mStorage must end with bounds
197
198#if VALIDATE_REGIONS
199 validate(outputRegion, "T-Junction free region");
200#endif
201
202 return outputRegion;
203}
204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205Region& Region::operator = (const Region& rhs)
206{
Mathias Agopian20f68782009-05-11 00:03:41 -0700207#if VALIDATE_REGIONS
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700208 validate(*this, "this->operator=");
209 validate(rhs, "rhs.operator=");
Mathias Agopian20f68782009-05-11 00:03:41 -0700210#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700211 mStorage = rhs.mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 return *this;
213}
214
Mathias Agopian9f961452009-06-29 18:46:37 -0700215Region& Region::makeBoundsSelf()
216{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700217 if (mStorage.size() >= 2) {
218 const Rect bounds(getBounds());
219 mStorage.clear();
220 mStorage.add(bounds);
221 }
Mathias Agopian9f961452009-06-29 18:46:37 -0700222 return *this;
223}
224
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225void Region::clear()
226{
Mathias Agopian20f68782009-05-11 00:03:41 -0700227 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700228 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229}
230
231void Region::set(const Rect& r)
232{
Mathias Agopian20f68782009-05-11 00:03:41 -0700233 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700234 mStorage.add(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235}
236
Mathias Agopian0926f502009-05-04 14:17:04 -0700237void Region::set(uint32_t w, uint32_t h)
238{
Mathias Agopian20f68782009-05-11 00:03:41 -0700239 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700240 mStorage.add(Rect(w,h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700241}
242
Mathias Agopian2ca79392013-04-02 18:30:32 -0700243bool Region::isTriviallyEqual(const Region& region) const {
244 return begin() == region.begin();
245}
246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247// ----------------------------------------------------------------------------
248
Mathias Agopian20f68782009-05-11 00:03:41 -0700249void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700251 Rect rect(l,t,r,b);
252 size_t where = mStorage.size() - 1;
253 mStorage.insertAt(rect, where, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800254}
255
Mathias Agopian20f68782009-05-11 00:03:41 -0700256// ----------------------------------------------------------------------------
257
258Region& Region::orSelf(const Rect& r) {
259 return operationSelf(r, op_or);
260}
Romain Guyb8a2e982012-02-07 17:04:34 -0800261Region& Region::xorSelf(const Rect& r) {
262 return operationSelf(r, op_xor);
263}
Mathias Agopian20f68782009-05-11 00:03:41 -0700264Region& Region::andSelf(const Rect& r) {
265 return operationSelf(r, op_and);
266}
267Region& Region::subtractSelf(const Rect& r) {
268 return operationSelf(r, op_nand);
269}
270Region& Region::operationSelf(const Rect& r, int op) {
271 Region lhs(*this);
272 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273 return *this;
274}
275
276// ----------------------------------------------------------------------------
277
278Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700279 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280}
Romain Guyb8a2e982012-02-07 17:04:34 -0800281Region& Region::xorSelf(const Region& rhs) {
282 return operationSelf(rhs, op_xor);
283}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700285 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700288 return operationSelf(rhs, op_nand);
289}
290Region& Region::operationSelf(const Region& rhs, int op) {
291 Region lhs(*this);
292 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 return *this;
294}
295
296Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700297 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298 return *this;
299}
300
Mathias Agopian20f68782009-05-11 00:03:41 -0700301// ----------------------------------------------------------------------------
302
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700303const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700304 return operation(rhs, op_or);
305}
Romain Guyb8a2e982012-02-07 17:04:34 -0800306const Region Region::mergeExclusive(const Rect& rhs) const {
307 return operation(rhs, op_xor);
308}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700309const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700310 return operation(rhs, op_and);
311}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700312const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700313 return operation(rhs, op_nand);
314}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700315const Region Region::operation(const Rect& rhs, int op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700316 Region result;
317 boolean_operation(op, result, *this, rhs);
318 return result;
319}
320
321// ----------------------------------------------------------------------------
322
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700323const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700324 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325}
Romain Guyb8a2e982012-02-07 17:04:34 -0800326const Region Region::mergeExclusive(const Region& rhs) const {
327 return operation(rhs, op_xor);
328}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700329const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700330 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700332const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700333 return operation(rhs, op_nand);
334}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700335const Region Region::operation(const Region& rhs, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800336 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700337 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338 return result;
339}
340
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700341const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800342 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700343 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800344 return result;
345}
346
347// ----------------------------------------------------------------------------
348
349Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700350 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351}
Romain Guyb8a2e982012-02-07 17:04:34 -0800352Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
353 return operationSelf(rhs, dx, dy, op_xor);
354}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700356 return operationSelf(rhs, dx, dy, op_and);
357}
358Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
359 return operationSelf(rhs, dx, dy, op_nand);
360}
361Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) {
362 Region lhs(*this);
363 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800364 return *this;
365}
366
Mathias Agopian20f68782009-05-11 00:03:41 -0700367// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800368
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700369const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700370 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800371}
Romain Guyb8a2e982012-02-07 17:04:34 -0800372const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
373 return operation(rhs, dx, dy, op_xor);
374}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700375const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700376 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800377}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700378const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700379 return operation(rhs, dx, dy, op_nand);
380}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700381const Region Region::operation(const Region& rhs, int dx, int dy, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800382 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700383 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384 return result;
385}
386
387// ----------------------------------------------------------------------------
388
Mathias Agopian20f68782009-05-11 00:03:41 -0700389// This is our region rasterizer, which merges rects and spans together
390// to obtain an optimal region.
391class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700393 Rect bounds;
Mathias Agopian20f68782009-05-11 00:03:41 -0700394 Vector<Rect>& storage;
395 Rect* head;
396 Rect* tail;
397 Vector<Rect> span;
398 Rect* cur;
399public:
400 rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700401 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700402 storage.clear();
403 }
404
405 ~rasterizer() {
406 if (span.size()) {
407 flushSpan();
408 }
409 if (storage.size()) {
410 bounds.top = storage.itemAt(0).top;
411 bounds.bottom = storage.top().bottom;
412 if (storage.size() == 1) {
413 storage.clear();
414 }
415 } else {
416 bounds.left = 0;
417 bounds.right = 0;
418 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700419 storage.add(bounds);
Mathias Agopian20f68782009-05-11 00:03:41 -0700420 }
421
422 virtual void operator()(const Rect& rect) {
Steve Block9d453682011-12-20 16:23:08 +0000423 //ALOGD(">>> %3d, %3d, %3d, %3d",
Mathias Agopian20f68782009-05-11 00:03:41 -0700424 // rect.left, rect.top, rect.right, rect.bottom);
425 if (span.size()) {
426 if (cur->top != rect.top) {
427 flushSpan();
428 } else if (cur->right == rect.left) {
429 cur->right = rect.right;
430 return;
431 }
432 }
433 span.add(rect);
434 cur = span.editArray() + (span.size() - 1);
435 }
436private:
437 template<typename T>
438 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
439 template<typename T>
440 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
441 void flushSpan() {
442 bool merge = false;
443 if (tail-head == ssize_t(span.size())) {
Romain Guyb8016242010-10-27 18:57:51 -0700444 Rect const* p = span.editArray();
Mathias Agopian20f68782009-05-11 00:03:41 -0700445 Rect const* q = head;
446 if (p->top == q->bottom) {
447 merge = true;
448 while (q != tail) {
449 if ((p->left != q->left) || (p->right != q->right)) {
450 merge = false;
451 break;
452 }
453 p++, q++;
454 }
455 }
456 }
457 if (merge) {
458 const int bottom = span[0].bottom;
459 Rect* r = head;
460 while (r != tail) {
461 r->bottom = bottom;
462 r++;
463 }
464 } else {
465 bounds.left = min(span.itemAt(0).left, bounds.left);
466 bounds.right = max(span.top().right, bounds.right);
467 storage.appendVector(span);
468 tail = storage.editArray() + storage.size();
469 head = tail - span.size();
470 }
471 span.clear();
472 }
473};
474
Mathias Agopian068d47f2012-09-11 18:56:23 -0700475bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700476{
477 bool result = true;
478 const_iterator cur = reg.begin();
479 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700480 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700481 Rect b(*prev);
482 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700483 if (cur->isValid() == false) {
484 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
485 result = false;
486 }
487 if (cur->right > region_operator<Rect>::max_value) {
488 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
489 result = false;
490 }
491 if (cur->bottom > region_operator<Rect>::max_value) {
492 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
493 result = false;
494 }
495 if (prev != cur) {
496 b.left = b.left < cur->left ? b.left : cur->left;
497 b.top = b.top < cur->top ? b.top : cur->top;
498 b.right = b.right > cur->right ? b.right : cur->right;
499 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
500 if ((*prev < *cur) == false) {
501 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700502 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700503 }
504 if (cur->top == prev->top) {
505 if (cur->bottom != prev->bottom) {
506 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
507 result = false;
508 } else if (cur->left < prev->right) {
509 ALOGE_IF(!silent,
510 "%s: spans overlap horizontally prev=%p, cur=%p",
511 name, prev, cur);
512 result = false;
513 }
514 } else if (cur->top < prev->bottom) {
515 ALOGE_IF(!silent,
516 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700517 name, prev, cur);
518 result = false;
519 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700520 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700521 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700522 cur++;
523 }
524 if (b != reg.getBounds()) {
525 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700526 ALOGE_IF(!silent,
527 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700528 b.left, b.top, b.right, b.bottom,
529 reg.getBounds().left, reg.getBounds().top,
530 reg.getBounds().right, reg.getBounds().bottom);
531 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700532 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700533 result = false;
534 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700535 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700536 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700537 reg.dump(name);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700538 CallStack stack(LOG_TAG);
Mathias Agopian20f68782009-05-11 00:03:41 -0700539 }
540 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800541}
542
Mathias Agopian20f68782009-05-11 00:03:41 -0700543void Region::boolean_operation(int op, Region& dst,
544 const Region& lhs,
545 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800546{
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700547#if VALIDATE_REGIONS
548 validate(lhs, "boolean_operation (before): lhs");
549 validate(rhs, "boolean_operation (before): rhs");
550 validate(dst, "boolean_operation (before): dst");
551#endif
552
Mathias Agopian20f68782009-05-11 00:03:41 -0700553 size_t lhs_count;
554 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
555
556 size_t rhs_count;
557 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
558
559 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
560 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
561 region_operator<Rect> operation(op, lhs_region, rhs_region);
562 { // scope for rasterizer (dtor has side effects)
563 rasterizer r(dst);
564 operation(r);
565 }
566
567#if VALIDATE_REGIONS
568 validate(lhs, "boolean_operation: lhs");
569 validate(rhs, "boolean_operation: rhs");
570 validate(dst, "boolean_operation: dst");
571#endif
572
573#if VALIDATE_WITH_CORECG
574 SkRegion sk_lhs;
575 SkRegion sk_rhs;
576 SkRegion sk_dst;
577
578 for (size_t i=0 ; i<lhs_count ; i++)
579 sk_lhs.op(
580 lhs_rects[i].left + dx,
581 lhs_rects[i].top + dy,
582 lhs_rects[i].right + dx,
583 lhs_rects[i].bottom + dy,
584 SkRegion::kUnion_Op);
585
586 for (size_t i=0 ; i<rhs_count ; i++)
587 sk_rhs.op(
588 rhs_rects[i].left + dx,
589 rhs_rects[i].top + dy,
590 rhs_rects[i].right + dx,
591 rhs_rects[i].bottom + dy,
592 SkRegion::kUnion_Op);
593
594 const char* name = "---";
595 SkRegion::Op sk_op;
596 switch (op) {
597 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800598 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700599 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
600 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
601 }
602 sk_dst.op(sk_lhs, sk_rhs, sk_op);
603
604 if (sk_dst.isEmpty() && dst.isEmpty())
605 return;
606
607 bool same = true;
608 Region::const_iterator head = dst.begin();
609 Region::const_iterator const tail = dst.end();
610 SkRegion::Iterator it(sk_dst);
611 while (!it.done()) {
612 if (head != tail) {
613 if (
614 head->left != it.rect().fLeft ||
615 head->top != it.rect().fTop ||
616 head->right != it.rect().fRight ||
617 head->bottom != it.rect().fBottom
618 ) {
619 same = false;
620 break;
621 }
622 } else {
623 same = false;
624 break;
625 }
626 head++;
627 it.next();
628 }
629
630 if (head != tail) {
631 same = false;
632 }
633
634 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000635 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700636 lhs.dump("lhs");
637 rhs.dump("rhs");
638 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000639 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700640 SkRegion::Iterator it(sk_dst);
641 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000642 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700643 it.rect().fLeft,
644 it.rect().fTop,
645 it.rect().fRight,
646 it.rect().fBottom);
647 it.next();
648 }
649 }
650#endif
651}
652
653void Region::boolean_operation(int op, Region& dst,
654 const Region& lhs,
655 const Rect& rhs, int dx, int dy)
656{
Mathias Agopian04504522011-09-19 16:12:08 -0700657 if (!rhs.isValid()) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000658 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700659 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700660 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700661 }
662
Mathias Agopian20f68782009-05-11 00:03:41 -0700663#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS
664 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
665#else
666 size_t lhs_count;
667 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
668
669 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
670 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
671 region_operator<Rect> operation(op, lhs_region, rhs_region);
672 { // scope for rasterizer (dtor has side effects)
673 rasterizer r(dst);
674 operation(r);
675 }
676
677#endif
678}
679
680void Region::boolean_operation(int op, Region& dst,
681 const Region& lhs, const Region& rhs)
682{
683 boolean_operation(op, dst, lhs, rhs, 0, 0);
684}
685
686void Region::boolean_operation(int op, Region& dst,
687 const Region& lhs, const Rect& rhs)
688{
689 boolean_operation(op, dst, lhs, rhs, 0, 0);
690}
691
692void Region::translate(Region& reg, int dx, int dy)
693{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700694 if ((dx || dy) && !reg.isEmpty()) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700695#if VALIDATE_REGIONS
696 validate(reg, "translate (before)");
697#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700698 size_t count = reg.mStorage.size();
699 Rect* rects = reg.mStorage.editArray();
700 while (count) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700701 rects->offsetBy(dx, dy);
Mathias Agopian20f68782009-05-11 00:03:41 -0700702 rects++;
703 count--;
704 }
705#if VALIDATE_REGIONS
706 validate(reg, "translate (after)");
707#endif
708 }
709}
710
711void Region::translate(Region& dst, const Region& reg, int dx, int dy)
712{
713 dst = reg;
714 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800715}
716
717// ----------------------------------------------------------------------------
718
Mathias Agopiane1424282013-07-29 21:24:40 -0700719size_t Region::getFlattenedSize() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700720 return mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700721}
722
Mathias Agopiane1424282013-07-29 21:24:40 -0700723status_t Region::flatten(void* buffer, size_t size) const {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700724#if VALIDATE_REGIONS
725 validate(*this, "Region::flatten");
726#endif
Mathias Agopiane1424282013-07-29 21:24:40 -0700727 if (size < mStorage.size() * sizeof(Rect)) {
728 return NO_MEMORY;
729 }
Mathias Agopian8683fca2012-08-12 19:37:16 -0700730 Rect* rects = reinterpret_cast<Rect*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700731 memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect));
732 return NO_ERROR;
733}
734
735status_t Region::unflatten(void const* buffer, size_t size) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700736 Region result;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700737 if (size >= sizeof(Rect)) {
738 Rect const* rects = reinterpret_cast<Rect const*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700739 size_t count = size / sizeof(Rect);
740 if (count > 0) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700741 result.mStorage.clear();
742 ssize_t err = result.mStorage.insertAt(0, count);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700743 if (err < 0) {
744 return status_t(err);
745 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700746 memcpy(result.mStorage.editArray(), rects, count*sizeof(Rect));
Mathias Agopianb6121422010-02-17 20:22:26 -0800747 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700748 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700749#if VALIDATE_REGIONS
Mathias Agopian068d47f2012-09-11 18:56:23 -0700750 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700751#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700752
753 if (!result.validate(result, "Region::unflatten", true)) {
754 ALOGE("Region::unflatten() failed, invalid region");
755 return BAD_VALUE;
756 }
757 mStorage = result.mStorage;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700758 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759}
760
Mathias Agopian20f68782009-05-11 00:03:41 -0700761// ----------------------------------------------------------------------------
762
763Region::const_iterator Region::begin() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700764 return mStorage.array();
Mathias Agopian20f68782009-05-11 00:03:41 -0700765}
766
767Region::const_iterator Region::end() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700768 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
769 return mStorage.array() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700770}
771
772Rect const* Region::getArray(size_t* count) const {
773 const_iterator const b(begin());
774 const_iterator const e(end());
775 if (count) *count = e-b;
776 return b;
777}
778
Mathias Agopian2401ead2012-08-31 15:41:24 -0700779SharedBuffer const* Region::getSharedBuffer(size_t* count) const {
780 // We can get to the SharedBuffer of a Vector<Rect> because Rect has
781 // a trivial destructor.
782 SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array());
783 if (count) {
784 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
785 count[0] = numRects;
786 }
787 sb->acquire();
788 return sb;
789}
790
Mathias Agopian20f68782009-05-11 00:03:41 -0700791// ----------------------------------------------------------------------------
792
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793void Region::dump(String8& out, const char* what, uint32_t flags) const
794{
795 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700796 const_iterator head = begin();
797 const_iterator const tail = end();
798
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800799 size_t SIZE = 256;
800 char buffer[SIZE];
Mathias Agopian20f68782009-05-11 00:03:41 -0700801
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700802 snprintf(buffer, SIZE, " Region %s (this=%p, count=%" PRIdPTR ")\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700803 what, this, tail-head);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700805 while (head != tail) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800806 snprintf(buffer, SIZE, " [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700807 head->left, head->top, head->right, head->bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700809 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800810 }
811}
812
813void Region::dump(const char* what, uint32_t flags) const
814{
815 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700816 const_iterator head = begin();
817 const_iterator const tail = end();
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700818 ALOGD(" Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700819 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000820 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700821 head->left, head->top, head->right, head->bottom);
822 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 }
824}
825
826// ----------------------------------------------------------------------------
827
828}; // namespace android