blob: 7de48a460eeaaa1ff7c15987f2459099ccf3bb33 [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
Michael Wright1c284a92014-02-10 13:00:14 -0800224bool Region::contains(const Point& point) const {
225 return contains(point.x, point.y);
226}
227
228bool Region::contains(int x, int y) const {
229 const_iterator cur = begin();
230 const_iterator const tail = end();
231 while (cur != tail) {
232 if (y >= cur->top && y < cur->bottom && x >= cur->left && x < cur->right) {
233 return true;
234 }
235 cur++;
236 }
237 return false;
238}
239
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240void Region::clear()
241{
Mathias Agopian20f68782009-05-11 00:03:41 -0700242 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700243 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
245
246void Region::set(const Rect& r)
247{
Mathias Agopian20f68782009-05-11 00:03:41 -0700248 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700249 mStorage.add(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250}
251
Mathias Agopian0926f502009-05-04 14:17:04 -0700252void Region::set(uint32_t w, uint32_t h)
253{
Mathias Agopian20f68782009-05-11 00:03:41 -0700254 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700255 mStorage.add(Rect(w,h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700256}
257
Mathias Agopian2ca79392013-04-02 18:30:32 -0700258bool Region::isTriviallyEqual(const Region& region) const {
259 return begin() == region.begin();
260}
261
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800262// ----------------------------------------------------------------------------
263
Mathias Agopian20f68782009-05-11 00:03:41 -0700264void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700266 Rect rect(l,t,r,b);
267 size_t where = mStorage.size() - 1;
268 mStorage.insertAt(rect, where, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269}
270
Mathias Agopian20f68782009-05-11 00:03:41 -0700271// ----------------------------------------------------------------------------
272
273Region& Region::orSelf(const Rect& r) {
274 return operationSelf(r, op_or);
275}
Romain Guyb8a2e982012-02-07 17:04:34 -0800276Region& Region::xorSelf(const Rect& r) {
277 return operationSelf(r, op_xor);
278}
Mathias Agopian20f68782009-05-11 00:03:41 -0700279Region& Region::andSelf(const Rect& r) {
280 return operationSelf(r, op_and);
281}
282Region& Region::subtractSelf(const Rect& r) {
283 return operationSelf(r, op_nand);
284}
285Region& Region::operationSelf(const Rect& r, int op) {
286 Region lhs(*this);
287 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288 return *this;
289}
290
291// ----------------------------------------------------------------------------
292
293Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700294 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295}
Romain Guyb8a2e982012-02-07 17:04:34 -0800296Region& Region::xorSelf(const Region& rhs) {
297 return operationSelf(rhs, op_xor);
298}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700300 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800301}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800302Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700303 return operationSelf(rhs, op_nand);
304}
305Region& Region::operationSelf(const Region& rhs, int op) {
306 Region lhs(*this);
307 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800308 return *this;
309}
310
311Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700312 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800313 return *this;
314}
315
Mathias Agopian20f68782009-05-11 00:03:41 -0700316// ----------------------------------------------------------------------------
317
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700318const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700319 return operation(rhs, op_or);
320}
Romain Guyb8a2e982012-02-07 17:04:34 -0800321const Region Region::mergeExclusive(const Rect& rhs) const {
322 return operation(rhs, op_xor);
323}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700324const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700325 return operation(rhs, op_and);
326}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700327const Region Region::subtract(const Rect& 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 Rect& rhs, int op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700331 Region result;
332 boolean_operation(op, result, *this, rhs);
333 return result;
334}
335
336// ----------------------------------------------------------------------------
337
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700338const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700339 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340}
Romain Guyb8a2e982012-02-07 17:04:34 -0800341const Region Region::mergeExclusive(const Region& rhs) const {
342 return operation(rhs, op_xor);
343}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700344const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700345 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800346}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700347const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700348 return operation(rhs, op_nand);
349}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700350const Region Region::operation(const Region& rhs, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700352 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 return result;
354}
355
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700356const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800357 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700358 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800359 return result;
360}
361
362// ----------------------------------------------------------------------------
363
364Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700365 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366}
Romain Guyb8a2e982012-02-07 17:04:34 -0800367Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
368 return operationSelf(rhs, dx, dy, op_xor);
369}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800370Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700371 return operationSelf(rhs, dx, dy, op_and);
372}
373Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
374 return operationSelf(rhs, dx, dy, op_nand);
375}
376Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) {
377 Region lhs(*this);
378 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800379 return *this;
380}
381
Mathias Agopian20f68782009-05-11 00:03:41 -0700382// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800383
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700384const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700385 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800386}
Romain Guyb8a2e982012-02-07 17:04:34 -0800387const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
388 return operation(rhs, dx, dy, op_xor);
389}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700390const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700391 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800392}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700393const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700394 return operation(rhs, dx, dy, op_nand);
395}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700396const Region Region::operation(const Region& rhs, int dx, int dy, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800397 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700398 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800399 return result;
400}
401
402// ----------------------------------------------------------------------------
403
Mathias Agopian20f68782009-05-11 00:03:41 -0700404// This is our region rasterizer, which merges rects and spans together
405// to obtain an optimal region.
406class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800407{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700408 Rect bounds;
Mathias Agopian20f68782009-05-11 00:03:41 -0700409 Vector<Rect>& storage;
410 Rect* head;
411 Rect* tail;
412 Vector<Rect> span;
413 Rect* cur;
414public:
415 rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700416 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700417 storage.clear();
418 }
419
420 ~rasterizer() {
421 if (span.size()) {
422 flushSpan();
423 }
424 if (storage.size()) {
425 bounds.top = storage.itemAt(0).top;
426 bounds.bottom = storage.top().bottom;
427 if (storage.size() == 1) {
428 storage.clear();
429 }
430 } else {
431 bounds.left = 0;
432 bounds.right = 0;
433 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700434 storage.add(bounds);
Mathias Agopian20f68782009-05-11 00:03:41 -0700435 }
436
437 virtual void operator()(const Rect& rect) {
Steve Block9d453682011-12-20 16:23:08 +0000438 //ALOGD(">>> %3d, %3d, %3d, %3d",
Mathias Agopian20f68782009-05-11 00:03:41 -0700439 // rect.left, rect.top, rect.right, rect.bottom);
440 if (span.size()) {
441 if (cur->top != rect.top) {
442 flushSpan();
443 } else if (cur->right == rect.left) {
444 cur->right = rect.right;
445 return;
446 }
447 }
448 span.add(rect);
449 cur = span.editArray() + (span.size() - 1);
450 }
451private:
452 template<typename T>
453 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
454 template<typename T>
455 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
456 void flushSpan() {
457 bool merge = false;
458 if (tail-head == ssize_t(span.size())) {
Romain Guyb8016242010-10-27 18:57:51 -0700459 Rect const* p = span.editArray();
Mathias Agopian20f68782009-05-11 00:03:41 -0700460 Rect const* q = head;
461 if (p->top == q->bottom) {
462 merge = true;
463 while (q != tail) {
464 if ((p->left != q->left) || (p->right != q->right)) {
465 merge = false;
466 break;
467 }
468 p++, q++;
469 }
470 }
471 }
472 if (merge) {
473 const int bottom = span[0].bottom;
474 Rect* r = head;
475 while (r != tail) {
476 r->bottom = bottom;
477 r++;
478 }
479 } else {
480 bounds.left = min(span.itemAt(0).left, bounds.left);
481 bounds.right = max(span.top().right, bounds.right);
482 storage.appendVector(span);
483 tail = storage.editArray() + storage.size();
484 head = tail - span.size();
485 }
486 span.clear();
487 }
488};
489
Mathias Agopian068d47f2012-09-11 18:56:23 -0700490bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700491{
492 bool result = true;
493 const_iterator cur = reg.begin();
494 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700495 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700496 Rect b(*prev);
497 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700498 if (cur->isValid() == false) {
499 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
500 result = false;
501 }
502 if (cur->right > region_operator<Rect>::max_value) {
503 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
504 result = false;
505 }
506 if (cur->bottom > region_operator<Rect>::max_value) {
507 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
508 result = false;
509 }
510 if (prev != cur) {
511 b.left = b.left < cur->left ? b.left : cur->left;
512 b.top = b.top < cur->top ? b.top : cur->top;
513 b.right = b.right > cur->right ? b.right : cur->right;
514 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
515 if ((*prev < *cur) == false) {
516 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700517 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700518 }
519 if (cur->top == prev->top) {
520 if (cur->bottom != prev->bottom) {
521 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
522 result = false;
523 } else if (cur->left < prev->right) {
524 ALOGE_IF(!silent,
525 "%s: spans overlap horizontally prev=%p, cur=%p",
526 name, prev, cur);
527 result = false;
528 }
529 } else if (cur->top < prev->bottom) {
530 ALOGE_IF(!silent,
531 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700532 name, prev, cur);
533 result = false;
534 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700535 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700536 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700537 cur++;
538 }
539 if (b != reg.getBounds()) {
540 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700541 ALOGE_IF(!silent,
542 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700543 b.left, b.top, b.right, b.bottom,
544 reg.getBounds().left, reg.getBounds().top,
545 reg.getBounds().right, reg.getBounds().bottom);
546 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700547 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700548 result = false;
549 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700550 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700551 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700552 reg.dump(name);
Mathias Agopiancab25d62013-03-21 17:12:40 -0700553 CallStack stack(LOG_TAG);
Mathias Agopian20f68782009-05-11 00:03:41 -0700554 }
555 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800556}
557
Mathias Agopian20f68782009-05-11 00:03:41 -0700558void Region::boolean_operation(int op, Region& dst,
559 const Region& lhs,
560 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800561{
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700562#if VALIDATE_REGIONS
563 validate(lhs, "boolean_operation (before): lhs");
564 validate(rhs, "boolean_operation (before): rhs");
565 validate(dst, "boolean_operation (before): dst");
566#endif
567
Mathias Agopian20f68782009-05-11 00:03:41 -0700568 size_t lhs_count;
569 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
570
571 size_t rhs_count;
572 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
573
574 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
575 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
576 region_operator<Rect> operation(op, lhs_region, rhs_region);
577 { // scope for rasterizer (dtor has side effects)
578 rasterizer r(dst);
579 operation(r);
580 }
581
582#if VALIDATE_REGIONS
583 validate(lhs, "boolean_operation: lhs");
584 validate(rhs, "boolean_operation: rhs");
585 validate(dst, "boolean_operation: dst");
586#endif
587
588#if VALIDATE_WITH_CORECG
589 SkRegion sk_lhs;
590 SkRegion sk_rhs;
591 SkRegion sk_dst;
592
593 for (size_t i=0 ; i<lhs_count ; i++)
594 sk_lhs.op(
595 lhs_rects[i].left + dx,
596 lhs_rects[i].top + dy,
597 lhs_rects[i].right + dx,
598 lhs_rects[i].bottom + dy,
599 SkRegion::kUnion_Op);
600
601 for (size_t i=0 ; i<rhs_count ; i++)
602 sk_rhs.op(
603 rhs_rects[i].left + dx,
604 rhs_rects[i].top + dy,
605 rhs_rects[i].right + dx,
606 rhs_rects[i].bottom + dy,
607 SkRegion::kUnion_Op);
608
609 const char* name = "---";
610 SkRegion::Op sk_op;
611 switch (op) {
612 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800613 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700614 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
615 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
616 }
617 sk_dst.op(sk_lhs, sk_rhs, sk_op);
618
619 if (sk_dst.isEmpty() && dst.isEmpty())
620 return;
621
622 bool same = true;
623 Region::const_iterator head = dst.begin();
624 Region::const_iterator const tail = dst.end();
625 SkRegion::Iterator it(sk_dst);
626 while (!it.done()) {
627 if (head != tail) {
628 if (
629 head->left != it.rect().fLeft ||
630 head->top != it.rect().fTop ||
631 head->right != it.rect().fRight ||
632 head->bottom != it.rect().fBottom
633 ) {
634 same = false;
635 break;
636 }
637 } else {
638 same = false;
639 break;
640 }
641 head++;
642 it.next();
643 }
644
645 if (head != tail) {
646 same = false;
647 }
648
649 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000650 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700651 lhs.dump("lhs");
652 rhs.dump("rhs");
653 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000654 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700655 SkRegion::Iterator it(sk_dst);
656 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000657 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700658 it.rect().fLeft,
659 it.rect().fTop,
660 it.rect().fRight,
661 it.rect().fBottom);
662 it.next();
663 }
664 }
665#endif
666}
667
668void Region::boolean_operation(int op, Region& dst,
669 const Region& lhs,
670 const Rect& rhs, int dx, int dy)
671{
Mathias Agopian04504522011-09-19 16:12:08 -0700672 if (!rhs.isValid()) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000673 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700674 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700675 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700676 }
677
Mathias Agopian20f68782009-05-11 00:03:41 -0700678#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS
679 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
680#else
681 size_t lhs_count;
682 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
683
684 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
685 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
686 region_operator<Rect> operation(op, lhs_region, rhs_region);
687 { // scope for rasterizer (dtor has side effects)
688 rasterizer r(dst);
689 operation(r);
690 }
691
692#endif
693}
694
695void Region::boolean_operation(int op, Region& dst,
696 const Region& lhs, const Region& rhs)
697{
698 boolean_operation(op, dst, lhs, rhs, 0, 0);
699}
700
701void Region::boolean_operation(int op, Region& dst,
702 const Region& lhs, const Rect& rhs)
703{
704 boolean_operation(op, dst, lhs, rhs, 0, 0);
705}
706
707void Region::translate(Region& reg, int dx, int dy)
708{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700709 if ((dx || dy) && !reg.isEmpty()) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700710#if VALIDATE_REGIONS
711 validate(reg, "translate (before)");
712#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700713 size_t count = reg.mStorage.size();
714 Rect* rects = reg.mStorage.editArray();
715 while (count) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700716 rects->offsetBy(dx, dy);
Mathias Agopian20f68782009-05-11 00:03:41 -0700717 rects++;
718 count--;
719 }
720#if VALIDATE_REGIONS
721 validate(reg, "translate (after)");
722#endif
723 }
724}
725
726void Region::translate(Region& dst, const Region& reg, int dx, int dy)
727{
728 dst = reg;
729 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730}
731
732// ----------------------------------------------------------------------------
733
Mathias Agopiane1424282013-07-29 21:24:40 -0700734size_t Region::getFlattenedSize() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700735 return mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700736}
737
Mathias Agopiane1424282013-07-29 21:24:40 -0700738status_t Region::flatten(void* buffer, size_t size) const {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700739#if VALIDATE_REGIONS
740 validate(*this, "Region::flatten");
741#endif
Mathias Agopiane1424282013-07-29 21:24:40 -0700742 if (size < mStorage.size() * sizeof(Rect)) {
743 return NO_MEMORY;
744 }
Mathias Agopian8683fca2012-08-12 19:37:16 -0700745 Rect* rects = reinterpret_cast<Rect*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700746 memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect));
747 return NO_ERROR;
748}
749
750status_t Region::unflatten(void const* buffer, size_t size) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700751 Region result;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700752 if (size >= sizeof(Rect)) {
753 Rect const* rects = reinterpret_cast<Rect const*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700754 size_t count = size / sizeof(Rect);
755 if (count > 0) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700756 result.mStorage.clear();
757 ssize_t err = result.mStorage.insertAt(0, count);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700758 if (err < 0) {
759 return status_t(err);
760 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700761 memcpy(result.mStorage.editArray(), rects, count*sizeof(Rect));
Mathias Agopianb6121422010-02-17 20:22:26 -0800762 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700763 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700764#if VALIDATE_REGIONS
Mathias Agopian068d47f2012-09-11 18:56:23 -0700765 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700766#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700767
768 if (!result.validate(result, "Region::unflatten", true)) {
769 ALOGE("Region::unflatten() failed, invalid region");
770 return BAD_VALUE;
771 }
772 mStorage = result.mStorage;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700773 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800774}
775
Mathias Agopian20f68782009-05-11 00:03:41 -0700776// ----------------------------------------------------------------------------
777
778Region::const_iterator Region::begin() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700779 return mStorage.array();
Mathias Agopian20f68782009-05-11 00:03:41 -0700780}
781
782Region::const_iterator Region::end() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700783 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
784 return mStorage.array() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700785}
786
787Rect const* Region::getArray(size_t* count) const {
788 const_iterator const b(begin());
789 const_iterator const e(end());
790 if (count) *count = e-b;
791 return b;
792}
793
Mathias Agopian2401ead2012-08-31 15:41:24 -0700794SharedBuffer const* Region::getSharedBuffer(size_t* count) const {
795 // We can get to the SharedBuffer of a Vector<Rect> because Rect has
796 // a trivial destructor.
797 SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array());
798 if (count) {
799 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
800 count[0] = numRects;
801 }
802 sb->acquire();
803 return sb;
804}
805
Mathias Agopian20f68782009-05-11 00:03:41 -0700806// ----------------------------------------------------------------------------
807
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800808void Region::dump(String8& out, const char* what, uint32_t flags) const
809{
810 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700811 const_iterator head = begin();
812 const_iterator const tail = end();
813
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800814 size_t SIZE = 256;
815 char buffer[SIZE];
Mathias Agopian20f68782009-05-11 00:03:41 -0700816
817 snprintf(buffer, SIZE, " Region %s (this=%p, count=%d)\n",
818 what, this, tail-head);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800819 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700820 while (head != tail) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800821 snprintf(buffer, SIZE, " [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700822 head->left, head->top, head->right, head->bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800823 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700824 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800825 }
826}
827
828void Region::dump(const char* what, uint32_t flags) const
829{
830 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700831 const_iterator head = begin();
832 const_iterator const tail = end();
Steve Block9d453682011-12-20 16:23:08 +0000833 ALOGD(" Region %s (this=%p, count=%d)\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700834 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000835 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700836 head->left, head->top, head->right, head->bottom);
837 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800838 }
839}
840
841// ----------------------------------------------------------------------------
842
843}; // namespace android