blob: 1a613f80071c125b9fa5c5e959b24e382d92f143 [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
Mathias Agopian72b0ffe2009-07-06 18:07:26 -070019#include <limits.h>
20
Mathias Agopian20f68782009-05-11 00:03:41 -070021#include <utils/Log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/String8.h>
Mathias Agopian068d47f2012-09-11 18:56:23 -070023#include <utils/CallStack.h>
Mathias Agopian20f68782009-05-11 00:03:41 -070024
25#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <ui/Region.h>
Mathias Agopian20f68782009-05-11 00:03:41 -070027#include <ui/Point.h>
28
29#include <private/ui/RegionHelper.h>
30
31// ----------------------------------------------------------------------------
32#define VALIDATE_REGIONS (false)
33#define VALIDATE_WITH_CORECG (false)
34// ----------------------------------------------------------------------------
35
36#if VALIDATE_WITH_CORECG
37#include <core/SkRegion.h>
38#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40namespace android {
Mathias Agopian20f68782009-05-11 00:03:41 -070041// ----------------------------------------------------------------------------
42
43enum {
44 op_nand = region_operator<Rect>::op_nand,
45 op_and = region_operator<Rect>::op_and,
46 op_or = region_operator<Rect>::op_or,
47 op_xor = region_operator<Rect>::op_xor
48};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Chris Craik3e010f32013-02-25 19:12:47 -080050enum {
51 direction_LTR,
52 direction_RTL
53};
54
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055// ----------------------------------------------------------------------------
56
Mathias Agopian3ab68552012-08-31 14:31:40 -070057Region::Region() {
58 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059}
60
61Region::Region(const Region& rhs)
Mathias Agopian3ab68552012-08-31 14:31:40 -070062 : mStorage(rhs.mStorage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063{
Mathias Agopiand0b55c02011-03-16 23:18:07 -070064#if VALIDATE_REGIONS
65 validate(rhs, "rhs copy-ctor");
66#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067}
68
Mathias Agopian3ab68552012-08-31 14:31:40 -070069Region::Region(const Rect& rhs) {
70 mStorage.add(rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071}
72
73Region::~Region()
74{
75}
76
Chris Craik3e010f32013-02-25 19:12:47 -080077/**
78 * Copy rects from the src vector into the dst vector, resolving vertical T-Junctions along the way
79 *
80 * First pass through, divideSpanRTL will be set because the 'previous span' (indexing into the dst
81 * vector) will be reversed. Each rectangle in the original list, starting from the bottom, will be
82 * compared with the span directly below, and subdivided as needed to resolve T-junctions.
83 *
84 * The resulting temporary vector will be a completely reversed copy of the original, without any
85 * bottom-up T-junctions.
86 *
87 * Second pass through, divideSpanRTL will be false since the previous span will index into the
88 * final, correctly ordered region buffer. Each rectangle will be compared with the span directly
89 * above it, and subdivided to resolve any remaining T-junctions.
90 */
91static void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end,
92 Vector<Rect>& dst, int spanDirection) {
93 dst.clear();
94
95 const Rect* current = end - 1;
96 int lastTop = current->top;
97
98 // add first span immediately
99 do {
100 dst.add(*current);
101 current--;
102 } while (current->top == lastTop && current >= begin);
103
104 unsigned int beginLastSpan = -1;
105 unsigned int endLastSpan = -1;
106 int top = -1;
107 int bottom = -1;
108
109 // for all other spans, split if a t-junction exists in the span directly above
110 while (current >= begin) {
111 if (current->top != (current + 1)->top) {
112 // new span
113 if ((spanDirection == direction_RTL && current->bottom != (current + 1)->top) ||
114 (spanDirection == direction_LTR && current->top != (current + 1)->bottom)) {
115 // previous span not directly adjacent, don't check for T junctions
116 beginLastSpan = INT_MAX;
117 } else {
118 beginLastSpan = endLastSpan + 1;
119 }
120 endLastSpan = dst.size() - 1;
121
122 top = current->top;
123 bottom = current->bottom;
124 }
125 int left = current->left;
126 int right = current->right;
127
128 for (unsigned int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) {
129 const Rect* prev = &dst[prevIndex];
130 if (spanDirection == direction_RTL) {
131 // iterating over previous span RTL, quit if it's too far left
132 if (prev->right <= left) break;
133
134 if (prev->right > left && prev->right < right) {
135 dst.add(Rect(prev->right, top, right, bottom));
136 right = prev->right;
137 }
138
139 if (prev->left > left && prev->left < right) {
140 dst.add(Rect(prev->left, top, right, bottom));
141 right = prev->left;
142 }
143
144 // if an entry in the previous span is too far right, nothing further left in the
145 // current span will need it
146 if (prev->left >= right) {
147 beginLastSpan = prevIndex;
148 }
149 } else {
150 // iterating over previous span LTR, quit if it's too far right
151 if (prev->left >= right) break;
152
153 if (prev->left > left && prev->left < right) {
154 dst.add(Rect(left, top, prev->left, bottom));
155 left = prev->left;
156 }
157
158 if (prev->right > left && prev->right < right) {
159 dst.add(Rect(left, top, prev->right, bottom));
160 left = prev->right;
161 }
162 // if an entry in the previous span is too far left, nothing further right in the
163 // current span will need it
164 if (prev->right <= left) {
165 beginLastSpan = prevIndex;
166 }
167 }
168 }
169
170 if (left < right) {
171 dst.add(Rect(left, top, right, bottom));
172 }
173
174 current--;
175 }
176}
177
178/**
179 * Creates a new region with the same data as the argument, but divides rectangles as necessary to
180 * remove T-Junctions
181 *
182 * Note: the output will not necessarily be a very efficient representation of the region, since it
183 * may be that a triangle-based approach would generate significantly simpler geometry
184 */
185Region Region::createTJunctionFreeRegion(const Region& r) {
186 if (r.isEmpty()) return r;
187 if (r.isRect()) return r;
188
189 Vector<Rect> reversed;
190 reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL);
191
192 Region outputRegion;
193 reverseRectsResolvingJunctions(reversed.begin(), reversed.end(),
194 outputRegion.mStorage, direction_LTR);
195 outputRegion.mStorage.add(r.getBounds()); // to make region valid, mStorage must end with bounds
196
197#if VALIDATE_REGIONS
198 validate(outputRegion, "T-Junction free region");
199#endif
200
201 return outputRegion;
202}
203
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204Region& Region::operator = (const Region& rhs)
205{
Mathias Agopian20f68782009-05-11 00:03:41 -0700206#if VALIDATE_REGIONS
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700207 validate(*this, "this->operator=");
208 validate(rhs, "rhs.operator=");
Mathias Agopian20f68782009-05-11 00:03:41 -0700209#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700210 mStorage = rhs.mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 return *this;
212}
213
Mathias Agopian9f961452009-06-29 18:46:37 -0700214Region& Region::makeBoundsSelf()
215{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700216 if (mStorage.size() >= 2) {
217 const Rect bounds(getBounds());
218 mStorage.clear();
219 mStorage.add(bounds);
220 }
Mathias Agopian9f961452009-06-29 18:46:37 -0700221 return *this;
222}
223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224void Region::clear()
225{
Mathias Agopian20f68782009-05-11 00:03:41 -0700226 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700227 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228}
229
230void Region::set(const Rect& r)
231{
Mathias Agopian20f68782009-05-11 00:03:41 -0700232 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700233 mStorage.add(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234}
235
Mathias Agopian0926f502009-05-04 14:17:04 -0700236void Region::set(uint32_t w, uint32_t h)
237{
Mathias Agopian20f68782009-05-11 00:03:41 -0700238 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700239 mStorage.add(Rect(w,h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700240}
241
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800242// ----------------------------------------------------------------------------
243
Mathias Agopian20f68782009-05-11 00:03:41 -0700244void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700246 Rect rect(l,t,r,b);
247 size_t where = mStorage.size() - 1;
248 mStorage.insertAt(rect, where, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249}
250
Mathias Agopian20f68782009-05-11 00:03:41 -0700251// ----------------------------------------------------------------------------
252
253Region& Region::orSelf(const Rect& r) {
254 return operationSelf(r, op_or);
255}
Romain Guyb8a2e982012-02-07 17:04:34 -0800256Region& Region::xorSelf(const Rect& r) {
257 return operationSelf(r, op_xor);
258}
Mathias Agopian20f68782009-05-11 00:03:41 -0700259Region& Region::andSelf(const Rect& r) {
260 return operationSelf(r, op_and);
261}
262Region& Region::subtractSelf(const Rect& r) {
263 return operationSelf(r, op_nand);
264}
265Region& Region::operationSelf(const Rect& r, int op) {
266 Region lhs(*this);
267 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268 return *this;
269}
270
271// ----------------------------------------------------------------------------
272
273Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700274 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800275}
Romain Guyb8a2e982012-02-07 17:04:34 -0800276Region& Region::xorSelf(const Region& rhs) {
277 return operationSelf(rhs, op_xor);
278}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700280 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700283 return operationSelf(rhs, op_nand);
284}
285Region& Region::operationSelf(const Region& rhs, int op) {
286 Region lhs(*this);
287 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 return *this;
289}
290
291Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700292 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293 return *this;
294}
295
Mathias Agopian20f68782009-05-11 00:03:41 -0700296// ----------------------------------------------------------------------------
297
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700298const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700299 return operation(rhs, op_or);
300}
Romain Guyb8a2e982012-02-07 17:04:34 -0800301const Region Region::mergeExclusive(const Rect& rhs) const {
302 return operation(rhs, op_xor);
303}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700304const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700305 return operation(rhs, op_and);
306}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700307const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700308 return operation(rhs, op_nand);
309}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700310const Region Region::operation(const Rect& rhs, int op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700311 Region result;
312 boolean_operation(op, result, *this, rhs);
313 return result;
314}
315
316// ----------------------------------------------------------------------------
317
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700318const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700319 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800320}
Romain Guyb8a2e982012-02-07 17:04:34 -0800321const Region Region::mergeExclusive(const Region& rhs) const {
322 return operation(rhs, op_xor);
323}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700324const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700325 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700327const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700328 return operation(rhs, op_nand);
329}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700330const Region Region::operation(const Region& rhs, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700332 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333 return result;
334}
335
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700336const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700338 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800339 return result;
340}
341
342// ----------------------------------------------------------------------------
343
344Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700345 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346}
Romain Guyb8a2e982012-02-07 17:04:34 -0800347Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
348 return operationSelf(rhs, dx, dy, op_xor);
349}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700351 return operationSelf(rhs, dx, dy, op_and);
352}
353Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
354 return operationSelf(rhs, dx, dy, op_nand);
355}
356Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) {
357 Region lhs(*this);
358 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 return *this;
360}
361
Mathias Agopian20f68782009-05-11 00:03:41 -0700362// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800363
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700364const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700365 return operation(rhs, dx, dy, 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, int dx, int dy) const {
368 return operation(rhs, dx, dy, op_xor);
369}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700370const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700371 return operation(rhs, dx, dy, 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, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700374 return operation(rhs, dx, dy, op_nand);
375}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700376const Region Region::operation(const Region& rhs, int dx, int dy, int 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, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 return result;
380}
381
382// ----------------------------------------------------------------------------
383
Mathias Agopian20f68782009-05-11 00:03:41 -0700384// This is our region rasterizer, which merges rects and spans together
385// to obtain an optimal region.
386class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800387{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700388 Rect bounds;
Mathias Agopian20f68782009-05-11 00:03:41 -0700389 Vector<Rect>& storage;
390 Rect* head;
391 Rect* tail;
392 Vector<Rect> span;
393 Rect* cur;
394public:
395 rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700396 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700397 storage.clear();
398 }
399
400 ~rasterizer() {
401 if (span.size()) {
402 flushSpan();
403 }
404 if (storage.size()) {
405 bounds.top = storage.itemAt(0).top;
406 bounds.bottom = storage.top().bottom;
407 if (storage.size() == 1) {
408 storage.clear();
409 }
410 } else {
411 bounds.left = 0;
412 bounds.right = 0;
413 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700414 storage.add(bounds);
Mathias Agopian20f68782009-05-11 00:03:41 -0700415 }
416
417 virtual void operator()(const Rect& rect) {
Steve Block9d453682011-12-20 16:23:08 +0000418 //ALOGD(">>> %3d, %3d, %3d, %3d",
Mathias Agopian20f68782009-05-11 00:03:41 -0700419 // rect.left, rect.top, rect.right, rect.bottom);
420 if (span.size()) {
421 if (cur->top != rect.top) {
422 flushSpan();
423 } else if (cur->right == rect.left) {
424 cur->right = rect.right;
425 return;
426 }
427 }
428 span.add(rect);
429 cur = span.editArray() + (span.size() - 1);
430 }
431private:
432 template<typename T>
433 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
434 template<typename T>
435 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
436 void flushSpan() {
437 bool merge = false;
438 if (tail-head == ssize_t(span.size())) {
Romain Guyb8016242010-10-27 18:57:51 -0700439 Rect const* p = span.editArray();
Mathias Agopian20f68782009-05-11 00:03:41 -0700440 Rect const* q = head;
441 if (p->top == q->bottom) {
442 merge = true;
443 while (q != tail) {
444 if ((p->left != q->left) || (p->right != q->right)) {
445 merge = false;
446 break;
447 }
448 p++, q++;
449 }
450 }
451 }
452 if (merge) {
453 const int bottom = span[0].bottom;
454 Rect* r = head;
455 while (r != tail) {
456 r->bottom = bottom;
457 r++;
458 }
459 } else {
460 bounds.left = min(span.itemAt(0).left, bounds.left);
461 bounds.right = max(span.top().right, bounds.right);
462 storage.appendVector(span);
463 tail = storage.editArray() + storage.size();
464 head = tail - span.size();
465 }
466 span.clear();
467 }
468};
469
Mathias Agopian068d47f2012-09-11 18:56:23 -0700470bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700471{
472 bool result = true;
473 const_iterator cur = reg.begin();
474 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700475 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700476 Rect b(*prev);
477 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700478 if (cur->isValid() == false) {
479 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
480 result = false;
481 }
482 if (cur->right > region_operator<Rect>::max_value) {
483 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
484 result = false;
485 }
486 if (cur->bottom > region_operator<Rect>::max_value) {
487 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
488 result = false;
489 }
490 if (prev != cur) {
491 b.left = b.left < cur->left ? b.left : cur->left;
492 b.top = b.top < cur->top ? b.top : cur->top;
493 b.right = b.right > cur->right ? b.right : cur->right;
494 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
495 if ((*prev < *cur) == false) {
496 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700497 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700498 }
499 if (cur->top == prev->top) {
500 if (cur->bottom != prev->bottom) {
501 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
502 result = false;
503 } else if (cur->left < prev->right) {
504 ALOGE_IF(!silent,
505 "%s: spans overlap horizontally prev=%p, cur=%p",
506 name, prev, cur);
507 result = false;
508 }
509 } else if (cur->top < prev->bottom) {
510 ALOGE_IF(!silent,
511 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700512 name, prev, cur);
513 result = false;
514 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700515 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700516 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700517 cur++;
518 }
519 if (b != reg.getBounds()) {
520 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700521 ALOGE_IF(!silent,
522 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700523 b.left, b.top, b.right, b.bottom,
524 reg.getBounds().left, reg.getBounds().top,
525 reg.getBounds().right, reg.getBounds().bottom);
526 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700527 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700528 result = false;
529 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700530 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700531 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700532 reg.dump(name);
Mathias Agopian068d47f2012-09-11 18:56:23 -0700533 CallStack stack;
534 stack.update();
535 stack.dump("");
Mathias Agopian20f68782009-05-11 00:03:41 -0700536 }
537 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538}
539
Mathias Agopian20f68782009-05-11 00:03:41 -0700540void Region::boolean_operation(int op, Region& dst,
541 const Region& lhs,
542 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800543{
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700544#if VALIDATE_REGIONS
545 validate(lhs, "boolean_operation (before): lhs");
546 validate(rhs, "boolean_operation (before): rhs");
547 validate(dst, "boolean_operation (before): dst");
548#endif
549
Mathias Agopian20f68782009-05-11 00:03:41 -0700550 size_t lhs_count;
551 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
552
553 size_t rhs_count;
554 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
555
556 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
557 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
558 region_operator<Rect> operation(op, lhs_region, rhs_region);
559 { // scope for rasterizer (dtor has side effects)
560 rasterizer r(dst);
561 operation(r);
562 }
563
564#if VALIDATE_REGIONS
565 validate(lhs, "boolean_operation: lhs");
566 validate(rhs, "boolean_operation: rhs");
567 validate(dst, "boolean_operation: dst");
568#endif
569
570#if VALIDATE_WITH_CORECG
571 SkRegion sk_lhs;
572 SkRegion sk_rhs;
573 SkRegion sk_dst;
574
575 for (size_t i=0 ; i<lhs_count ; i++)
576 sk_lhs.op(
577 lhs_rects[i].left + dx,
578 lhs_rects[i].top + dy,
579 lhs_rects[i].right + dx,
580 lhs_rects[i].bottom + dy,
581 SkRegion::kUnion_Op);
582
583 for (size_t i=0 ; i<rhs_count ; i++)
584 sk_rhs.op(
585 rhs_rects[i].left + dx,
586 rhs_rects[i].top + dy,
587 rhs_rects[i].right + dx,
588 rhs_rects[i].bottom + dy,
589 SkRegion::kUnion_Op);
590
591 const char* name = "---";
592 SkRegion::Op sk_op;
593 switch (op) {
594 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800595 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700596 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
597 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
598 }
599 sk_dst.op(sk_lhs, sk_rhs, sk_op);
600
601 if (sk_dst.isEmpty() && dst.isEmpty())
602 return;
603
604 bool same = true;
605 Region::const_iterator head = dst.begin();
606 Region::const_iterator const tail = dst.end();
607 SkRegion::Iterator it(sk_dst);
608 while (!it.done()) {
609 if (head != tail) {
610 if (
611 head->left != it.rect().fLeft ||
612 head->top != it.rect().fTop ||
613 head->right != it.rect().fRight ||
614 head->bottom != it.rect().fBottom
615 ) {
616 same = false;
617 break;
618 }
619 } else {
620 same = false;
621 break;
622 }
623 head++;
624 it.next();
625 }
626
627 if (head != tail) {
628 same = false;
629 }
630
631 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000632 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700633 lhs.dump("lhs");
634 rhs.dump("rhs");
635 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000636 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700637 SkRegion::Iterator it(sk_dst);
638 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000639 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700640 it.rect().fLeft,
641 it.rect().fTop,
642 it.rect().fRight,
643 it.rect().fBottom);
644 it.next();
645 }
646 }
647#endif
648}
649
650void Region::boolean_operation(int op, Region& dst,
651 const Region& lhs,
652 const Rect& rhs, int dx, int dy)
653{
Mathias Agopian04504522011-09-19 16:12:08 -0700654 if (!rhs.isValid()) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000655 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700656 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700657 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700658 }
659
Mathias Agopian20f68782009-05-11 00:03:41 -0700660#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS
661 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
662#else
663 size_t lhs_count;
664 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
665
666 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
667 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
668 region_operator<Rect> operation(op, lhs_region, rhs_region);
669 { // scope for rasterizer (dtor has side effects)
670 rasterizer r(dst);
671 operation(r);
672 }
673
674#endif
675}
676
677void Region::boolean_operation(int op, Region& dst,
678 const Region& lhs, const Region& rhs)
679{
680 boolean_operation(op, dst, lhs, rhs, 0, 0);
681}
682
683void Region::boolean_operation(int op, Region& dst,
684 const Region& lhs, const Rect& rhs)
685{
686 boolean_operation(op, dst, lhs, rhs, 0, 0);
687}
688
689void Region::translate(Region& reg, int dx, int dy)
690{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700691 if ((dx || dy) && !reg.isEmpty()) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700692#if VALIDATE_REGIONS
693 validate(reg, "translate (before)");
694#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700695 size_t count = reg.mStorage.size();
696 Rect* rects = reg.mStorage.editArray();
697 while (count) {
698 rects->translate(dx, dy);
699 rects++;
700 count--;
701 }
702#if VALIDATE_REGIONS
703 validate(reg, "translate (after)");
704#endif
705 }
706}
707
708void Region::translate(Region& dst, const Region& reg, int dx, int dy)
709{
710 dst = reg;
711 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712}
713
714// ----------------------------------------------------------------------------
715
Mathias Agopian8683fca2012-08-12 19:37:16 -0700716size_t Region::getSize() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700717 return mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700718}
719
720status_t Region::flatten(void* buffer) const {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700721#if VALIDATE_REGIONS
722 validate(*this, "Region::flatten");
723#endif
Mathias Agopian8683fca2012-08-12 19:37:16 -0700724 Rect* rects = reinterpret_cast<Rect*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700725 memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect));
726 return NO_ERROR;
727}
728
729status_t Region::unflatten(void const* buffer, size_t size) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700730 Region result;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700731 if (size >= sizeof(Rect)) {
732 Rect const* rects = reinterpret_cast<Rect const*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700733 size_t count = size / sizeof(Rect);
734 if (count > 0) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700735 result.mStorage.clear();
736 ssize_t err = result.mStorage.insertAt(0, count);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700737 if (err < 0) {
738 return status_t(err);
739 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700740 memcpy(result.mStorage.editArray(), rects, count*sizeof(Rect));
Mathias Agopianb6121422010-02-17 20:22:26 -0800741 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700742 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700743#if VALIDATE_REGIONS
Mathias Agopian068d47f2012-09-11 18:56:23 -0700744 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700745#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700746
747 if (!result.validate(result, "Region::unflatten", true)) {
748 ALOGE("Region::unflatten() failed, invalid region");
749 return BAD_VALUE;
750 }
751 mStorage = result.mStorage;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700752 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753}
754
Mathias Agopian20f68782009-05-11 00:03:41 -0700755// ----------------------------------------------------------------------------
756
757Region::const_iterator Region::begin() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700758 return mStorage.array();
Mathias Agopian20f68782009-05-11 00:03:41 -0700759}
760
761Region::const_iterator Region::end() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700762 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
763 return mStorage.array() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700764}
765
766Rect const* Region::getArray(size_t* count) const {
767 const_iterator const b(begin());
768 const_iterator const e(end());
769 if (count) *count = e-b;
770 return b;
771}
772
Mathias Agopian2401ead2012-08-31 15:41:24 -0700773SharedBuffer const* Region::getSharedBuffer(size_t* count) const {
774 // We can get to the SharedBuffer of a Vector<Rect> because Rect has
775 // a trivial destructor.
776 SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array());
777 if (count) {
778 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
779 count[0] = numRects;
780 }
781 sb->acquire();
782 return sb;
783}
784
Mathias Agopian20f68782009-05-11 00:03:41 -0700785// ----------------------------------------------------------------------------
786
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800787void Region::dump(String8& out, const char* what, uint32_t flags) const
788{
789 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700790 const_iterator head = begin();
791 const_iterator const tail = end();
792
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793 size_t SIZE = 256;
794 char buffer[SIZE];
Mathias Agopian20f68782009-05-11 00:03:41 -0700795
796 snprintf(buffer, SIZE, " Region %s (this=%p, count=%d)\n",
797 what, this, tail-head);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800798 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700799 while (head != tail) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800800 snprintf(buffer, SIZE, " [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700801 head->left, head->top, head->right, head->bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800802 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700803 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800804 }
805}
806
807void Region::dump(const char* what, uint32_t flags) const
808{
809 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700810 const_iterator head = begin();
811 const_iterator const tail = end();
Steve Block9d453682011-12-20 16:23:08 +0000812 ALOGD(" Region %s (this=%p, count=%d)\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700813 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000814 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700815 head->left, head->top, head->right, head->bottom);
816 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800817 }
818}
819
820// ----------------------------------------------------------------------------
821
822}; // namespace android