blob: 94fb1d5520a1f0818c56bbef07b934cde133d1f2 [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 Agopian20f68782009-05-11 00:03:41 -070023
24#include <ui/Rect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <ui/Region.h>
Mathias Agopian20f68782009-05-11 00:03:41 -070026#include <ui/Point.h>
27
28#include <private/ui/RegionHelper.h>
29
30// ----------------------------------------------------------------------------
31#define VALIDATE_REGIONS (false)
32#define VALIDATE_WITH_CORECG (false)
33// ----------------------------------------------------------------------------
34
35#if VALIDATE_WITH_CORECG
36#include <core/SkRegion.h>
37#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
39namespace android {
Mathias Agopian20f68782009-05-11 00:03:41 -070040// ----------------------------------------------------------------------------
41
42enum {
43 op_nand = region_operator<Rect>::op_nand,
44 op_and = region_operator<Rect>::op_and,
45 op_or = region_operator<Rect>::op_or,
46 op_xor = region_operator<Rect>::op_xor
47};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048
49// ----------------------------------------------------------------------------
50
Mathias Agopian3ab68552012-08-31 14:31:40 -070051Region::Region() {
52 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053}
54
55Region::Region(const Region& rhs)
Mathias Agopian3ab68552012-08-31 14:31:40 -070056 : mStorage(rhs.mStorage)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057{
Mathias Agopiand0b55c02011-03-16 23:18:07 -070058#if VALIDATE_REGIONS
59 validate(rhs, "rhs copy-ctor");
60#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061}
62
Mathias Agopian3ab68552012-08-31 14:31:40 -070063Region::Region(const Rect& rhs) {
64 mStorage.add(rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065}
66
67Region::~Region()
68{
69}
70
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071Region& Region::operator = (const Region& rhs)
72{
Mathias Agopian20f68782009-05-11 00:03:41 -070073#if VALIDATE_REGIONS
Mathias Agopiand0b55c02011-03-16 23:18:07 -070074 validate(*this, "this->operator=");
75 validate(rhs, "rhs.operator=");
Mathias Agopian20f68782009-05-11 00:03:41 -070076#endif
Mathias Agopian20f68782009-05-11 00:03:41 -070077 mStorage = rhs.mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 return *this;
79}
80
Mathias Agopian9f961452009-06-29 18:46:37 -070081Region& Region::makeBoundsSelf()
82{
Mathias Agopian3ab68552012-08-31 14:31:40 -070083 if (mStorage.size() >= 2) {
84 const Rect bounds(getBounds());
85 mStorage.clear();
86 mStorage.add(bounds);
87 }
Mathias Agopian9f961452009-06-29 18:46:37 -070088 return *this;
89}
90
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091void Region::clear()
92{
Mathias Agopian20f68782009-05-11 00:03:41 -070093 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -070094 mStorage.add(Rect(0,0));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095}
96
97void Region::set(const Rect& r)
98{
Mathias Agopian20f68782009-05-11 00:03:41 -070099 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700100 mStorage.add(r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101}
102
Mathias Agopian0926f502009-05-04 14:17:04 -0700103void Region::set(uint32_t w, uint32_t h)
104{
Mathias Agopian20f68782009-05-11 00:03:41 -0700105 mStorage.clear();
Mathias Agopian3ab68552012-08-31 14:31:40 -0700106 mStorage.add(Rect(w,h));
Mathias Agopian0926f502009-05-04 14:17:04 -0700107}
108
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109// ----------------------------------------------------------------------------
110
Mathias Agopian20f68782009-05-11 00:03:41 -0700111void Region::addRectUnchecked(int l, int t, int r, int b)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700113 Rect rect(l,t,r,b);
114 size_t where = mStorage.size() - 1;
115 mStorage.insertAt(rect, where, 1);
116
Mathias Agopian20f68782009-05-11 00:03:41 -0700117#if VALIDATE_REGIONS
118 validate(*this, "addRectUnchecked");
119#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120}
121
Mathias Agopian20f68782009-05-11 00:03:41 -0700122// ----------------------------------------------------------------------------
123
124Region& Region::orSelf(const Rect& r) {
125 return operationSelf(r, op_or);
126}
Romain Guyb8a2e982012-02-07 17:04:34 -0800127Region& Region::xorSelf(const Rect& r) {
128 return operationSelf(r, op_xor);
129}
Mathias Agopian20f68782009-05-11 00:03:41 -0700130Region& Region::andSelf(const Rect& r) {
131 return operationSelf(r, op_and);
132}
133Region& Region::subtractSelf(const Rect& r) {
134 return operationSelf(r, op_nand);
135}
136Region& Region::operationSelf(const Rect& r, int op) {
137 Region lhs(*this);
138 boolean_operation(op, *this, lhs, r);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 return *this;
140}
141
142// ----------------------------------------------------------------------------
143
144Region& Region::orSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700145 return operationSelf(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146}
Romain Guyb8a2e982012-02-07 17:04:34 -0800147Region& Region::xorSelf(const Region& rhs) {
148 return operationSelf(rhs, op_xor);
149}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150Region& Region::andSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700151 return operationSelf(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153Region& Region::subtractSelf(const Region& rhs) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700154 return operationSelf(rhs, op_nand);
155}
156Region& Region::operationSelf(const Region& rhs, int op) {
157 Region lhs(*this);
158 boolean_operation(op, *this, lhs, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 return *this;
160}
161
162Region& Region::translateSelf(int x, int y) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700163 if (x|y) translate(*this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 return *this;
165}
166
Mathias Agopian20f68782009-05-11 00:03:41 -0700167// ----------------------------------------------------------------------------
168
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700169const Region Region::merge(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700170 return operation(rhs, op_or);
171}
Romain Guyb8a2e982012-02-07 17:04:34 -0800172const Region Region::mergeExclusive(const Rect& rhs) const {
173 return operation(rhs, op_xor);
174}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700175const Region Region::intersect(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700176 return operation(rhs, op_and);
177}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700178const Region Region::subtract(const Rect& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700179 return operation(rhs, op_nand);
180}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700181const Region Region::operation(const Rect& rhs, int op) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700182 Region result;
183 boolean_operation(op, result, *this, rhs);
184 return result;
185}
186
187// ----------------------------------------------------------------------------
188
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700189const Region Region::merge(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700190 return operation(rhs, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
Romain Guyb8a2e982012-02-07 17:04:34 -0800192const Region Region::mergeExclusive(const Region& rhs) const {
193 return operation(rhs, op_xor);
194}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700195const Region Region::intersect(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700196 return operation(rhs, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700198const Region Region::subtract(const Region& rhs) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700199 return operation(rhs, op_nand);
200}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700201const Region Region::operation(const Region& rhs, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700203 boolean_operation(op, result, *this, rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 return result;
205}
206
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700207const Region Region::translate(int x, int y) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700209 translate(result, *this, x, y);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 return result;
211}
212
213// ----------------------------------------------------------------------------
214
215Region& Region::orSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700216 return operationSelf(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217}
Romain Guyb8a2e982012-02-07 17:04:34 -0800218Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
219 return operationSelf(rhs, dx, dy, op_xor);
220}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221Region& Region::andSelf(const Region& rhs, int dx, int dy) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700222 return operationSelf(rhs, dx, dy, op_and);
223}
224Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
225 return operationSelf(rhs, dx, dy, op_nand);
226}
227Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) {
228 Region lhs(*this);
229 boolean_operation(op, *this, lhs, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 return *this;
231}
232
Mathias Agopian20f68782009-05-11 00:03:41 -0700233// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700235const Region Region::merge(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700236 return operation(rhs, dx, dy, op_or);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237}
Romain Guyb8a2e982012-02-07 17:04:34 -0800238const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
239 return operation(rhs, dx, dy, op_xor);
240}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700241const Region Region::intersect(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700242 return operation(rhs, dx, dy, op_and);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700244const Region Region::subtract(const Region& rhs, int dx, int dy) const {
Mathias Agopian20f68782009-05-11 00:03:41 -0700245 return operation(rhs, dx, dy, op_nand);
246}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700247const Region Region::operation(const Region& rhs, int dx, int dy, int op) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 Region result;
Mathias Agopian20f68782009-05-11 00:03:41 -0700249 boolean_operation(op, result, *this, rhs, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250 return result;
251}
252
253// ----------------------------------------------------------------------------
254
Mathias Agopian20f68782009-05-11 00:03:41 -0700255// This is our region rasterizer, which merges rects and spans together
256// to obtain an optimal region.
257class Region::rasterizer : public region_operator<Rect>::region_rasterizer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258{
Mathias Agopian3ab68552012-08-31 14:31:40 -0700259 Rect bounds;
Mathias Agopian20f68782009-05-11 00:03:41 -0700260 Vector<Rect>& storage;
261 Rect* head;
262 Rect* tail;
263 Vector<Rect> span;
264 Rect* cur;
265public:
266 rasterizer(Region& reg)
Mathias Agopian3ab68552012-08-31 14:31:40 -0700267 : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
Mathias Agopian20f68782009-05-11 00:03:41 -0700268 storage.clear();
269 }
270
271 ~rasterizer() {
272 if (span.size()) {
273 flushSpan();
274 }
275 if (storage.size()) {
276 bounds.top = storage.itemAt(0).top;
277 bounds.bottom = storage.top().bottom;
278 if (storage.size() == 1) {
279 storage.clear();
280 }
281 } else {
282 bounds.left = 0;
283 bounds.right = 0;
284 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700285 storage.add(bounds);
Mathias Agopian20f68782009-05-11 00:03:41 -0700286 }
287
288 virtual void operator()(const Rect& rect) {
Steve Block9d453682011-12-20 16:23:08 +0000289 //ALOGD(">>> %3d, %3d, %3d, %3d",
Mathias Agopian20f68782009-05-11 00:03:41 -0700290 // rect.left, rect.top, rect.right, rect.bottom);
291 if (span.size()) {
292 if (cur->top != rect.top) {
293 flushSpan();
294 } else if (cur->right == rect.left) {
295 cur->right = rect.right;
296 return;
297 }
298 }
299 span.add(rect);
300 cur = span.editArray() + (span.size() - 1);
301 }
302private:
303 template<typename T>
304 static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
305 template<typename T>
306 static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
307 void flushSpan() {
308 bool merge = false;
309 if (tail-head == ssize_t(span.size())) {
Romain Guyb8016242010-10-27 18:57:51 -0700310 Rect const* p = span.editArray();
Mathias Agopian20f68782009-05-11 00:03:41 -0700311 Rect const* q = head;
312 if (p->top == q->bottom) {
313 merge = true;
314 while (q != tail) {
315 if ((p->left != q->left) || (p->right != q->right)) {
316 merge = false;
317 break;
318 }
319 p++, q++;
320 }
321 }
322 }
323 if (merge) {
324 const int bottom = span[0].bottom;
325 Rect* r = head;
326 while (r != tail) {
327 r->bottom = bottom;
328 r++;
329 }
330 } else {
331 bounds.left = min(span.itemAt(0).left, bounds.left);
332 bounds.right = max(span.top().right, bounds.right);
333 storage.appendVector(span);
334 tail = storage.editArray() + storage.size();
335 head = tail - span.size();
336 }
337 span.clear();
338 }
339};
340
341bool Region::validate(const Region& reg, const char* name)
342{
343 bool result = true;
344 const_iterator cur = reg.begin();
345 const_iterator const tail = reg.end();
346 const_iterator prev = cur++;
347 Rect b(*prev);
348 while (cur != tail) {
349 b.left = b.left < cur->left ? b.left : cur->left;
350 b.top = b.top < cur->top ? b.top : cur->top;
351 b.right = b.right > cur->right ? b.right : cur->right;
352 b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
353 if (cur->top == prev->top) {
354 if (cur->bottom != prev->bottom) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000355 ALOGE("%s: invalid span %p", name, cur);
Mathias Agopian20f68782009-05-11 00:03:41 -0700356 result = false;
357 } else if (cur->left < prev->right) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000358 ALOGE("%s: spans overlap horizontally prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700359 name, prev, cur);
360 result = false;
361 }
362 } else if (cur->top < prev->bottom) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000363 ALOGE("%s: spans overlap vertically prev=%p, cur=%p",
Mathias Agopian20f68782009-05-11 00:03:41 -0700364 name, prev, cur);
365 result = false;
366 }
367 prev = cur;
368 cur++;
369 }
370 if (b != reg.getBounds()) {
371 result = false;
Steve Blocke6f43dd2012-01-06 19:20:56 +0000372 ALOGE("%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
Mathias Agopian20f68782009-05-11 00:03:41 -0700373 b.left, b.top, b.right, b.bottom,
374 reg.getBounds().left, reg.getBounds().top,
375 reg.getBounds().right, reg.getBounds().bottom);
376 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700377 if (reg.mStorage.size() == 2) {
378 ALOGE("mStorage size is 2, which is never valid");
379 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700380 if (result == false) {
381 reg.dump(name);
382 }
383 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800384}
385
Mathias Agopian20f68782009-05-11 00:03:41 -0700386void Region::boolean_operation(int op, Region& dst,
387 const Region& lhs,
388 const Region& rhs, int dx, int dy)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389{
Mathias Agopiand0b55c02011-03-16 23:18:07 -0700390#if VALIDATE_REGIONS
391 validate(lhs, "boolean_operation (before): lhs");
392 validate(rhs, "boolean_operation (before): rhs");
393 validate(dst, "boolean_operation (before): dst");
394#endif
395
Mathias Agopian20f68782009-05-11 00:03:41 -0700396 size_t lhs_count;
397 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
398
399 size_t rhs_count;
400 Rect const * const rhs_rects = rhs.getArray(&rhs_count);
401
402 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
403 region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
404 region_operator<Rect> operation(op, lhs_region, rhs_region);
405 { // scope for rasterizer (dtor has side effects)
406 rasterizer r(dst);
407 operation(r);
408 }
409
410#if VALIDATE_REGIONS
411 validate(lhs, "boolean_operation: lhs");
412 validate(rhs, "boolean_operation: rhs");
413 validate(dst, "boolean_operation: dst");
414#endif
415
416#if VALIDATE_WITH_CORECG
417 SkRegion sk_lhs;
418 SkRegion sk_rhs;
419 SkRegion sk_dst;
420
421 for (size_t i=0 ; i<lhs_count ; i++)
422 sk_lhs.op(
423 lhs_rects[i].left + dx,
424 lhs_rects[i].top + dy,
425 lhs_rects[i].right + dx,
426 lhs_rects[i].bottom + dy,
427 SkRegion::kUnion_Op);
428
429 for (size_t i=0 ; i<rhs_count ; i++)
430 sk_rhs.op(
431 rhs_rects[i].left + dx,
432 rhs_rects[i].top + dy,
433 rhs_rects[i].right + dx,
434 rhs_rects[i].bottom + dy,
435 SkRegion::kUnion_Op);
436
437 const char* name = "---";
438 SkRegion::Op sk_op;
439 switch (op) {
440 case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
Romain Guyb8a2e982012-02-07 17:04:34 -0800441 case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
Mathias Agopian20f68782009-05-11 00:03:41 -0700442 case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
443 case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
444 }
445 sk_dst.op(sk_lhs, sk_rhs, sk_op);
446
447 if (sk_dst.isEmpty() && dst.isEmpty())
448 return;
449
450 bool same = true;
451 Region::const_iterator head = dst.begin();
452 Region::const_iterator const tail = dst.end();
453 SkRegion::Iterator it(sk_dst);
454 while (!it.done()) {
455 if (head != tail) {
456 if (
457 head->left != it.rect().fLeft ||
458 head->top != it.rect().fTop ||
459 head->right != it.rect().fRight ||
460 head->bottom != it.rect().fBottom
461 ) {
462 same = false;
463 break;
464 }
465 } else {
466 same = false;
467 break;
468 }
469 head++;
470 it.next();
471 }
472
473 if (head != tail) {
474 same = false;
475 }
476
477 if(!same) {
Steve Block9d453682011-12-20 16:23:08 +0000478 ALOGD("---\nregion boolean %s failed", name);
Mathias Agopian20f68782009-05-11 00:03:41 -0700479 lhs.dump("lhs");
480 rhs.dump("rhs");
481 dst.dump("dst");
Steve Block9d453682011-12-20 16:23:08 +0000482 ALOGD("should be");
Mathias Agopian20f68782009-05-11 00:03:41 -0700483 SkRegion::Iterator it(sk_dst);
484 while (!it.done()) {
Steve Block9d453682011-12-20 16:23:08 +0000485 ALOGD(" [%3d, %3d, %3d, %3d]",
Mathias Agopian20f68782009-05-11 00:03:41 -0700486 it.rect().fLeft,
487 it.rect().fTop,
488 it.rect().fRight,
489 it.rect().fBottom);
490 it.next();
491 }
492 }
493#endif
494}
495
496void Region::boolean_operation(int op, Region& dst,
497 const Region& lhs,
498 const Rect& rhs, int dx, int dy)
499{
Mathias Agopian04504522011-09-19 16:12:08 -0700500 if (!rhs.isValid()) {
Steve Blocke6f43dd2012-01-06 19:20:56 +0000501 ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
Mathias Agopian04504522011-09-19 16:12:08 -0700502 op, rhs.left, rhs.top, rhs.right, rhs.bottom);
Mathias Agopian0857c8f2011-09-26 15:58:20 -0700503 return;
Mathias Agopian04504522011-09-19 16:12:08 -0700504 }
505
Mathias Agopian20f68782009-05-11 00:03:41 -0700506#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS
507 boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
508#else
509 size_t lhs_count;
510 Rect const * const lhs_rects = lhs.getArray(&lhs_count);
511
512 region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
513 region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
514 region_operator<Rect> operation(op, lhs_region, rhs_region);
515 { // scope for rasterizer (dtor has side effects)
516 rasterizer r(dst);
517 operation(r);
518 }
519
520#endif
521}
522
523void Region::boolean_operation(int op, Region& dst,
524 const Region& lhs, const Region& rhs)
525{
526 boolean_operation(op, dst, lhs, rhs, 0, 0);
527}
528
529void Region::boolean_operation(int op, Region& dst,
530 const Region& lhs, const Rect& rhs)
531{
532 boolean_operation(op, dst, lhs, rhs, 0, 0);
533}
534
535void Region::translate(Region& reg, int dx, int dy)
536{
Mathias Agopian4c0a1702012-08-31 12:45:33 -0700537 if ((dx || dy) && !reg.isEmpty()) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700538#if VALIDATE_REGIONS
539 validate(reg, "translate (before)");
540#endif
Mathias Agopian20f68782009-05-11 00:03:41 -0700541 size_t count = reg.mStorage.size();
542 Rect* rects = reg.mStorage.editArray();
543 while (count) {
544 rects->translate(dx, dy);
545 rects++;
546 count--;
547 }
548#if VALIDATE_REGIONS
549 validate(reg, "translate (after)");
550#endif
551 }
552}
553
554void Region::translate(Region& dst, const Region& reg, int dx, int dy)
555{
556 dst = reg;
557 translate(dst, dx, dy);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800558}
559
560// ----------------------------------------------------------------------------
561
Mathias Agopian8683fca2012-08-12 19:37:16 -0700562size_t Region::getSize() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700563 return mStorage.size() * sizeof(Rect);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700564}
565
566status_t Region::flatten(void* buffer) const {
567 Rect* rects = reinterpret_cast<Rect*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700568 memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect));
569 return NO_ERROR;
570}
571
572status_t Region::unflatten(void const* buffer, size_t size) {
573 mStorage.clear();
574 if (size >= sizeof(Rect)) {
575 Rect const* rects = reinterpret_cast<Rect const*>(buffer);
Mathias Agopian8683fca2012-08-12 19:37:16 -0700576 size_t count = size / sizeof(Rect);
577 if (count > 0) {
578 ssize_t err = mStorage.insertAt(0, count);
579 if (err < 0) {
580 return status_t(err);
581 }
582 memcpy(mStorage.editArray(), rects, count*sizeof(Rect));
Mathias Agopianb6121422010-02-17 20:22:26 -0800583 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700584 }
Mathias Agopian3ab68552012-08-31 14:31:40 -0700585#if VALIDATE_REGIONS
586 validate(*this, "Region::unflatten");
587#endif
Mathias Agopian8683fca2012-08-12 19:37:16 -0700588 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800589}
590
Mathias Agopian20f68782009-05-11 00:03:41 -0700591// ----------------------------------------------------------------------------
592
593Region::const_iterator Region::begin() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700594 return mStorage.array();
Mathias Agopian20f68782009-05-11 00:03:41 -0700595}
596
597Region::const_iterator Region::end() const {
Mathias Agopian3ab68552012-08-31 14:31:40 -0700598 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
599 return mStorage.array() + numRects;
Mathias Agopian20f68782009-05-11 00:03:41 -0700600}
601
602Rect const* Region::getArray(size_t* count) const {
603 const_iterator const b(begin());
604 const_iterator const e(end());
605 if (count) *count = e-b;
606 return b;
607}
608
Mathias Agopian2401ead2012-08-31 15:41:24 -0700609SharedBuffer const* Region::getSharedBuffer(size_t* count) const {
610 // We can get to the SharedBuffer of a Vector<Rect> because Rect has
611 // a trivial destructor.
612 SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array());
613 if (count) {
614 size_t numRects = isRect() ? 1 : mStorage.size() - 1;
615 count[0] = numRects;
616 }
617 sb->acquire();
618 return sb;
619}
620
Mathias Agopian20f68782009-05-11 00:03:41 -0700621// ----------------------------------------------------------------------------
622
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800623void Region::dump(String8& out, const char* what, uint32_t flags) const
624{
625 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700626 const_iterator head = begin();
627 const_iterator const tail = end();
628
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629 size_t SIZE = 256;
630 char buffer[SIZE];
Mathias Agopian20f68782009-05-11 00:03:41 -0700631
632 snprintf(buffer, SIZE, " Region %s (this=%p, count=%d)\n",
633 what, this, tail-head);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800634 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700635 while (head != tail) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800636 snprintf(buffer, SIZE, " [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700637 head->left, head->top, head->right, head->bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800638 out.append(buffer);
Mathias Agopian20f68782009-05-11 00:03:41 -0700639 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800640 }
641}
642
643void Region::dump(const char* what, uint32_t flags) const
644{
645 (void)flags;
Mathias Agopian20f68782009-05-11 00:03:41 -0700646 const_iterator head = begin();
647 const_iterator const tail = end();
Steve Block9d453682011-12-20 16:23:08 +0000648 ALOGD(" Region %s (this=%p, count=%d)\n", what, this, tail-head);
Mathias Agopian20f68782009-05-11 00:03:41 -0700649 while (head != tail) {
Steve Block9d453682011-12-20 16:23:08 +0000650 ALOGD(" [%3d, %3d, %3d, %3d]\n",
Mathias Agopian20f68782009-05-11 00:03:41 -0700651 head->left, head->top, head->right, head->bottom);
652 head++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 }
654}
655
656// ----------------------------------------------------------------------------
657
658}; // namespace android