blob: db69e16d8fbdc09d1204905477a98fd0dcc46983 [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
50// ----------------------------------------------------------------------------
51
Mathias Agopian3ab68552012-08-31 14:31:40 -070052Region::Region() {
53 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054}
55
56Region::Region(const Region& rhs)
Mathias Agopian3ab68552012-08-31 14:31:40 -070057 : mStorage(rhs.mStorage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058{
Mathias Agopiand0b55c02011-03-16 23:18:07 -070059#if VALIDATE_REGIONS
60 validate(rhs, "rhs copy-ctor");
61#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062}
63
Mathias Agopian3ab68552012-08-31 14:31:40 -070064Region::Region(const Rect& rhs) {
65 mStorage.add(rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066}
67
68Region::~Region()
69{
70}
71
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072Region& Region::operator = (const Region& rhs)
73{
Mathias Agopian20f68782009-05-11 00:03:41 -070074#if VALIDATE_REGIONS
Mathias Agopiand0b55c02011-03-16 23:18:07 -070075 validate(*this, "this->operator=");
76 validate(rhs, "rhs.operator=");
Mathias Agopian20f68782009-05-11 00:03:41 -070077#endif
Mathias Agopian20f68782009-05-11 00:03:41 -070078 mStorage = rhs.mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079 return *this;
80}
81
Mathias Agopian9f961452009-06-29 18:46:37 -070082Region& Region::makeBoundsSelf()
83{
Mathias Agopian3ab68552012-08-31 14:31:40 -070084 if (mStorage.size() >= 2) {
85 const Rect bounds(getBounds());
86 mStorage.clear();
87 mStorage.add(bounds);
88 }
Mathias Agopian9f961452009-06-29 18:46:37 -070089 return *this;
90}
91
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092void Region::clear()
93{
Mathias Agopian20f68782009-05-11 00:03:41 -070094 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -070095 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096}
97
98void Region::set(const Rect& r)
99{
Mathias Agopian20f68782009-05-11 00:03:41 -0700100 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700101 mStorage.add(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102}
103
Mathias Agopian0926f502009-05-04 14:17:04 -0700104void Region::set(uint32_t w, uint32_t h)
105{
Mathias Agopian20f68782009-05-11 00:03:41 -0700106 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700107 mStorage.add(Rect(w,h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700108}
109
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110// ----------------------------------------------------------------------------
111
Mathias Agopian20f68782009-05-11 00:03:41 -0700112void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700114 Rect rect(l,t,r,b);
115 size_t where = mStorage.size() - 1;
116 mStorage.insertAt(rect, where, 1);
117
Mathias Agopian20f68782009-05-11 00:03:41 -0700118#if VALIDATE_REGIONS
119 validate(*this, "addRectUnchecked");
120#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121}
122
Mathias Agopian20f68782009-05-11 00:03:41 -0700123// ----------------------------------------------------------------------------
124
125Region& Region::orSelf(const Rect& r) {
126 return operationSelf(r, op_or);
127}
Romain Guyb8a2e982012-02-07 17:04:34 -0800128Region& Region::xorSelf(const Rect& r) {
129 return operationSelf(r, op_xor);
130}
Mathias Agopian20f68782009-05-11 00:03:41 -0700131Region& Region::andSelf(const Rect& r) {
132 return operationSelf(r, op_and);
133}
134Region& Region::subtractSelf(const Rect& r) {
135 return operationSelf(r, op_nand);
136}
137Region& Region::operationSelf(const Rect& r, int op) {
138 Region lhs(*this);
139 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 return *this;
141}
142
143// ----------------------------------------------------------------------------
144
145Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700146 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147}
Romain Guyb8a2e982012-02-07 17:04:34 -0800148Region& Region::xorSelf(const Region& rhs) {
149 return operationSelf(rhs, op_xor);
150}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700152 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700155 return operationSelf(rhs, op_nand);
156}
157Region& Region::operationSelf(const Region& rhs, int op) {
158 Region lhs(*this);
159 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 return *this;
161}
162
163Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700164 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 return *this;
166}
167
Mathias Agopian20f68782009-05-11 00:03:41 -0700168// ----------------------------------------------------------------------------
169
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700170const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700171 return operation(rhs, op_or);
172}
Romain Guyb8a2e982012-02-07 17:04:34 -0800173const Region Region::mergeExclusive(const Rect& rhs) const {
174 return operation(rhs, op_xor);
175}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700176const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700177 return operation(rhs, op_and);
178}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700179const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700180 return operation(rhs, op_nand);
181}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700182const Region Region::operation(const Rect& rhs, int op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700183 Region result;
184 boolean_operation(op, result, *this, rhs);
185 return result;
186}
187
188// ----------------------------------------------------------------------------
189
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700190const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700191 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
Romain Guyb8a2e982012-02-07 17:04:34 -0800193const Region Region::mergeExclusive(const Region& rhs) const {
194 return operation(rhs, op_xor);
195}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700196const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700197 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700199const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700200 return operation(rhs, op_nand);
201}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700202const Region Region::operation(const Region& rhs, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700204 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 return result;
206}
207
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700208const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700210 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 return result;
212}
213
214// ----------------------------------------------------------------------------
215
216Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700217 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218}
Romain Guyb8a2e982012-02-07 17:04:34 -0800219Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
220 return operationSelf(rhs, dx, dy, op_xor);
221}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700223 return operationSelf(rhs, dx, dy, op_and);
224}
225Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
226 return operationSelf(rhs, dx, dy, op_nand);
227}
228Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) {
229 Region lhs(*this);
230 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 return *this;
232}
233
Mathias Agopian20f68782009-05-11 00:03:41 -0700234// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700236const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700237 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238}
Romain Guyb8a2e982012-02-07 17:04:34 -0800239const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
240 return operation(rhs, dx, dy, op_xor);
241}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700242const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700243 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800244}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700245const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700246 return operation(rhs, dx, dy, op_nand);
247}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700248const Region Region::operation(const Region& rhs, int dx, int dy, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700250 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251 return result;
252}
253
254// ----------------------------------------------------------------------------
255
Mathias Agopian20f68782009-05-11 00:03:41 -0700256// This is our region rasterizer, which merges rects and spans together
257// to obtain an optimal region.
258class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800259{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700260 Rect bounds;
Mathias Agopian20f68782009-05-11 00:03:41 -0700261 Vector<Rect>& storage;
262 Rect* head;
263 Rect* tail;
264 Vector<Rect> span;
265 Rect* cur;
266public:
267 rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700268 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700269 storage.clear();
270 }
271
272 ~rasterizer() {
273 if (span.size()) {
274 flushSpan();
275 }
276 if (storage.size()) {
277 bounds.top = storage.itemAt(0).top;
278 bounds.bottom = storage.top().bottom;
279 if (storage.size() == 1) {
280 storage.clear();
281 }
282 } else {
283 bounds.left = 0;
284 bounds.right = 0;
285 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700286 storage.add(bounds);
Mathias Agopian20f68782009-05-11 00:03:41 -0700287 }
288
289 virtual void operator()(const Rect& rect) {
Steve Block9d453682011-12-20 16:23:08 +0000290 //ALOGD(">>> %3d, %3d, %3d, %3d",
Mathias Agopian20f68782009-05-11 00:03:41 -0700291 // rect.left, rect.top, rect.right, rect.bottom);
292 if (span.size()) {
293 if (cur->top != rect.top) {
294 flushSpan();
295 } else if (cur->right == rect.left) {
296 cur->right = rect.right;
297 return;
298 }
299 }
300 span.add(rect);
301 cur = span.editArray() + (span.size() - 1);
302 }
303private:
304 template<typename T>
305 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
306 template<typename T>
307 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
308 void flushSpan() {
309 bool merge = false;
310 if (tail-head == ssize_t(span.size())) {
Romain Guyb8016242010-10-27 18:57:51 -0700311 Rect const* p = span.editArray();
Mathias Agopian20f68782009-05-11 00:03:41 -0700312 Rect const* q = head;
313 if (p->top == q->bottom) {
314 merge = true;
315 while (q != tail) {
316 if ((p->left != q->left) || (p->right != q->right)) {
317 merge = false;
318 break;
319 }
320 p++, q++;
321 }
322 }
323 }
324 if (merge) {
325 const int bottom = span[0].bottom;
326 Rect* r = head;
327 while (r != tail) {
328 r->bottom = bottom;
329 r++;
330 }
331 } else {
332 bounds.left = min(span.itemAt(0).left, bounds.left);
333 bounds.right = max(span.top().right, bounds.right);
334 storage.appendVector(span);
335 tail = storage.editArray() + storage.size();
336 head = tail - span.size();
337 }
338 span.clear();
339 }
340};
341
Mathias Agopian068d47f2012-09-11 18:56:23 -0700342bool Region::validate(const Region& reg, const char* name, bool silent)
Mathias Agopian20f68782009-05-11 00:03:41 -0700343{
344 bool result = true;
345 const_iterator cur = reg.begin();
346 const_iterator const tail = reg.end();
Mathias Agopian068d47f2012-09-11 18:56:23 -0700347 const_iterator prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700348 Rect b(*prev);
349 while (cur != tail) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700350 if (cur->isValid() == false) {
351 ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
352 result = false;
353 }
354 if (cur->right > region_operator<Rect>::max_value) {
355 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
356 result = false;
357 }
358 if (cur->bottom > region_operator<Rect>::max_value) {
359 ALOGE_IF(!silent, "%s: rect->right > max_value", name);
360 result = false;
361 }
362 if (prev != cur) {
363 b.left = b.left < cur->left ? b.left : cur->left;
364 b.top = b.top < cur->top ? b.top : cur->top;
365 b.right = b.right > cur->right ? b.right : cur->right;
366 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
367 if ((*prev < *cur) == false) {
368 ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700369 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700370 }
371 if (cur->top == prev->top) {
372 if (cur->bottom != prev->bottom) {
373 ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
374 result = false;
375 } else if (cur->left < prev->right) {
376 ALOGE_IF(!silent,
377 "%s: spans overlap horizontally prev=%p, cur=%p",
378 name, prev, cur);
379 result = false;
380 }
381 } else if (cur->top < prev->bottom) {
382 ALOGE_IF(!silent,
383 "%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700384 name, prev, cur);
385 result = false;
386 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700387 prev = cur;
Mathias Agopian20f68782009-05-11 00:03:41 -0700388 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700389 cur++;
390 }
391 if (b != reg.getBounds()) {
392 result = false;
Mathias Agopian068d47f2012-09-11 18:56:23 -0700393 ALOGE_IF(!silent,
394 "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700395 b.left, b.top, b.right, b.bottom,
396 reg.getBounds().left, reg.getBounds().top,
397 reg.getBounds().right, reg.getBounds().bottom);
398 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700399 if (reg.mStorage.size() == 2) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700400 result = false;
401 ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
Mathias Agopian3ab68552012-08-31 14:31:40 -0700402 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700403 if (result == false && !silent) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700404 reg.dump(name);
Mathias Agopian068d47f2012-09-11 18:56:23 -0700405 CallStack stack;
406 stack.update();
407 stack.dump("");
Mathias Agopian20f68782009-05-11 00:03:41 -0700408 }
409 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410}
411
Mathias Agopian20f68782009-05-11 00:03:41 -0700412void Region::boolean_operation(int op, Region& dst,
413 const Region& lhs,
414 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800415{
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700416#if VALIDATE_REGIONS
417 validate(lhs, "boolean_operation (before): lhs");
418 validate(rhs, "boolean_operation (before): rhs");
419 validate(dst, "boolean_operation (before): dst");
420#endif
421
Mathias Agopian20f68782009-05-11 00:03:41 -0700422 size_t lhs_count;
423 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
424
425 size_t rhs_count;
426 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
427
428 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
429 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
430 region_operator<Rect> operation(op, lhs_region, rhs_region);
431 { // scope for rasterizer (dtor has side effects)
432 rasterizer r(dst);
433 operation(r);
434 }
435
436#if VALIDATE_REGIONS
437 validate(lhs, "boolean_operation: lhs");
438 validate(rhs, "boolean_operation: rhs");
439 validate(dst, "boolean_operation: dst");
440#endif
441
442#if VALIDATE_WITH_CORECG
443 SkRegion sk_lhs;
444 SkRegion sk_rhs;
445 SkRegion sk_dst;
446
447 for (size_t i=0 ; i<lhs_count ; i++)
448 sk_lhs.op(
449 lhs_rects[i].left + dx,
450 lhs_rects[i].top + dy,
451 lhs_rects[i].right + dx,
452 lhs_rects[i].bottom + dy,
453 SkRegion::kUnion_Op);
454
455 for (size_t i=0 ; i<rhs_count ; i++)
456 sk_rhs.op(
457 rhs_rects[i].left + dx,
458 rhs_rects[i].top + dy,
459 rhs_rects[i].right + dx,
460 rhs_rects[i].bottom + dy,
461 SkRegion::kUnion_Op);
462
463 const char* name = "---";
464 SkRegion::Op sk_op;
465 switch (op) {
466 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800467 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700468 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
469 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
470 }
471 sk_dst.op(sk_lhs, sk_rhs, sk_op);
472
473 if (sk_dst.isEmpty() && dst.isEmpty())
474 return;
475
476 bool same = true;
477 Region::const_iterator head = dst.begin();
478 Region::const_iterator const tail = dst.end();
479 SkRegion::Iterator it(sk_dst);
480 while (!it.done()) {
481 if (head != tail) {
482 if (
483 head->left != it.rect().fLeft ||
484 head->top != it.rect().fTop ||
485 head->right != it.rect().fRight ||
486 head->bottom != it.rect().fBottom
487 ) {
488 same = false;
489 break;
490 }
491 } else {
492 same = false;
493 break;
494 }
495 head++;
496 it.next();
497 }
498
499 if (head != tail) {
500 same = false;
501 }
502
503 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000504 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700505 lhs.dump("lhs");
506 rhs.dump("rhs");
507 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000508 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700509 SkRegion::Iterator it(sk_dst);
510 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000511 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700512 it.rect().fLeft,
513 it.rect().fTop,
514 it.rect().fRight,
515 it.rect().fBottom);
516 it.next();
517 }
518 }
519#endif
520}
521
522void Region::boolean_operation(int op, Region& dst,
523 const Region& lhs,
524 const Rect& rhs, int dx, int dy)
525{
Mathias Agopian04504522011-09-19 16:12:08 -0700526 if (!rhs.isValid()) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000527 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700528 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700529 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700530 }
531
Mathias Agopian20f68782009-05-11 00:03:41 -0700532#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS
533 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
534#else
535 size_t lhs_count;
536 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
537
538 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
539 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
540 region_operator<Rect> operation(op, lhs_region, rhs_region);
541 { // scope for rasterizer (dtor has side effects)
542 rasterizer r(dst);
543 operation(r);
544 }
545
546#endif
547}
548
549void Region::boolean_operation(int op, Region& dst,
550 const Region& lhs, const Region& rhs)
551{
552 boolean_operation(op, dst, lhs, rhs, 0, 0);
553}
554
555void Region::boolean_operation(int op, Region& dst,
556 const Region& lhs, const Rect& rhs)
557{
558 boolean_operation(op, dst, lhs, rhs, 0, 0);
559}
560
561void Region::translate(Region& reg, int dx, int dy)
562{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700563 if ((dx || dy) && !reg.isEmpty()) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700564#if VALIDATE_REGIONS
565 validate(reg, "translate (before)");
566#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700567 size_t count = reg.mStorage.size();
568 Rect* rects = reg.mStorage.editArray();
569 while (count) {
570 rects->translate(dx, dy);
571 rects++;
572 count--;
573 }
574#if VALIDATE_REGIONS
575 validate(reg, "translate (after)");
576#endif
577 }
578}
579
580void Region::translate(Region& dst, const Region& reg, int dx, int dy)
581{
582 dst = reg;
583 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800584}
585
586// ----------------------------------------------------------------------------
587
Mathias Agopian8683fca2012-08-12 19:37:16 -0700588size_t Region::getSize() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700589 return mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700590}
591
592status_t Region::flatten(void* buffer) const {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700593#if VALIDATE_REGIONS
594 validate(*this, "Region::flatten");
595#endif
Mathias Agopian8683fca2012-08-12 19:37:16 -0700596 Rect* rects = reinterpret_cast<Rect*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700597 memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect));
598 return NO_ERROR;
599}
600
601status_t Region::unflatten(void const* buffer, size_t size) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700602 Region result;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700603 if (size >= sizeof(Rect)) {
604 Rect const* rects = reinterpret_cast<Rect const*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700605 size_t count = size / sizeof(Rect);
606 if (count > 0) {
Mathias Agopian068d47f2012-09-11 18:56:23 -0700607 result.mStorage.clear();
608 ssize_t err = result.mStorage.insertAt(0, count);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700609 if (err < 0) {
610 return status_t(err);
611 }
Mathias Agopian068d47f2012-09-11 18:56:23 -0700612 memcpy(result.mStorage.editArray(), rects, count*sizeof(Rect));
Mathias Agopianb6121422010-02-17 20:22:26 -0800613 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700614 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700615#if VALIDATE_REGIONS
Mathias Agopian068d47f2012-09-11 18:56:23 -0700616 validate(result, "Region::unflatten");
Mathias Agopian3ab68552012-08-31 14:31:40 -0700617#endif
Mathias Agopian068d47f2012-09-11 18:56:23 -0700618
619 if (!result.validate(result, "Region::unflatten", true)) {
620 ALOGE("Region::unflatten() failed, invalid region");
621 return BAD_VALUE;
622 }
623 mStorage = result.mStorage;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700624 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800625}
626
Mathias Agopian20f68782009-05-11 00:03:41 -0700627// ----------------------------------------------------------------------------
628
629Region::const_iterator Region::begin() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700630 return mStorage.array();
Mathias Agopian20f68782009-05-11 00:03:41 -0700631}
632
633Region::const_iterator Region::end() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700634 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
635 return mStorage.array() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700636}
637
638Rect const* Region::getArray(size_t* count) const {
639 const_iterator const b(begin());
640 const_iterator const e(end());
641 if (count) *count = e-b;
642 return b;
643}
644
Mathias Agopian2401ead2012-08-31 15:41:24 -0700645SharedBuffer const* Region::getSharedBuffer(size_t* count) const {
646 // We can get to the SharedBuffer of a Vector<Rect> because Rect has
647 // a trivial destructor.
648 SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array());
649 if (count) {
650 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
651 count[0] = numRects;
652 }
653 sb->acquire();
654 return sb;
655}
656
Mathias Agopian20f68782009-05-11 00:03:41 -0700657// ----------------------------------------------------------------------------
658
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800659void Region::dump(String8& out, const char* what, uint32_t flags) const
660{
661 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700662 const_iterator head = begin();
663 const_iterator const tail = end();
664
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800665 size_t SIZE = 256;
666 char buffer[SIZE];
Mathias Agopian20f68782009-05-11 00:03:41 -0700667
668 snprintf(buffer, SIZE, " Region %s (this=%p, count=%d)\n",
669 what, this, tail-head);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800670 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700671 while (head != tail) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800672 snprintf(buffer, SIZE, " [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700673 head->left, head->top, head->right, head->bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800674 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700675 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676 }
677}
678
679void Region::dump(const char* what, uint32_t flags) const
680{
681 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700682 const_iterator head = begin();
683 const_iterator const tail = end();
Steve Block9d453682011-12-20 16:23:08 +0000684 ALOGD(" Region %s (this=%p, count=%d)\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700685 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000686 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700687 head->left, head->top, head->right, head->bottom);
688 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800689 }
690}
691
692// ----------------------------------------------------------------------------
693
694}; // namespace android