Region.cpp revision cab25d680e644d962041d05a319e485b96136a5d
1/*
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
19#include <limits.h>
20
21#include <utils/Log.h>
22#include <utils/String8.h>
23#include <utils/CallStack.h>
24
25#include <ui/Rect.h>
26#include <ui/Region.h>
27#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
39
40namespace android {
41// ----------------------------------------------------------------------------
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};
49
50enum {
51    direction_LTR,
52    direction_RTL
53};
54
55// ----------------------------------------------------------------------------
56
57Region::Region() {
58    mStorage.add(Rect(0,0));
59}
60
61Region::Region(const Region& rhs)
62    : mStorage(rhs.mStorage)
63{
64#if VALIDATE_REGIONS
65    validate(rhs, "rhs copy-ctor");
66#endif
67}
68
69Region::Region(const Rect& rhs) {
70    mStorage.add(rhs);
71}
72
73Region::~Region()
74{
75}
76
77/**
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
204Region& Region::operator = (const Region& rhs)
205{
206#if VALIDATE_REGIONS
207    validate(*this, "this->operator=");
208    validate(rhs, "rhs.operator=");
209#endif
210    mStorage = rhs.mStorage;
211    return *this;
212}
213
214Region& Region::makeBoundsSelf()
215{
216    if (mStorage.size() >= 2) {
217        const Rect bounds(getBounds());
218        mStorage.clear();
219        mStorage.add(bounds);
220    }
221    return *this;
222}
223
224void Region::clear()
225{
226    mStorage.clear();
227    mStorage.add(Rect(0,0));
228}
229
230void Region::set(const Rect& r)
231{
232    mStorage.clear();
233    mStorage.add(r);
234}
235
236void Region::set(uint32_t w, uint32_t h)
237{
238    mStorage.clear();
239    mStorage.add(Rect(w,h));
240}
241
242// ----------------------------------------------------------------------------
243
244void Region::addRectUnchecked(int l, int t, int r, int b)
245{
246    Rect rect(l,t,r,b);
247    size_t where = mStorage.size() - 1;
248    mStorage.insertAt(rect, where, 1);
249}
250
251// ----------------------------------------------------------------------------
252
253Region& Region::orSelf(const Rect& r) {
254    return operationSelf(r, op_or);
255}
256Region& Region::xorSelf(const Rect& r) {
257    return operationSelf(r, op_xor);
258}
259Region& Region::andSelf(const Rect& r) {
260    return operationSelf(r, op_and);
261}
262Region& Region::subtractSelf(const Rect& r) {
263    return operationSelf(r, op_nand);
264}
265Region& Region::operationSelf(const Rect& r, int op) {
266    Region lhs(*this);
267    boolean_operation(op, *this, lhs, r);
268    return *this;
269}
270
271// ----------------------------------------------------------------------------
272
273Region& Region::orSelf(const Region& rhs) {
274    return operationSelf(rhs, op_or);
275}
276Region& Region::xorSelf(const Region& rhs) {
277    return operationSelf(rhs, op_xor);
278}
279Region& Region::andSelf(const Region& rhs) {
280    return operationSelf(rhs, op_and);
281}
282Region& Region::subtractSelf(const Region& rhs) {
283    return operationSelf(rhs, op_nand);
284}
285Region& Region::operationSelf(const Region& rhs, int op) {
286    Region lhs(*this);
287    boolean_operation(op, *this, lhs, rhs);
288    return *this;
289}
290
291Region& Region::translateSelf(int x, int y) {
292    if (x|y) translate(*this, x, y);
293    return *this;
294}
295
296// ----------------------------------------------------------------------------
297
298const Region Region::merge(const Rect& rhs) const {
299    return operation(rhs, op_or);
300}
301const Region Region::mergeExclusive(const Rect& rhs) const {
302    return operation(rhs, op_xor);
303}
304const Region Region::intersect(const Rect& rhs) const {
305    return operation(rhs, op_and);
306}
307const Region Region::subtract(const Rect& rhs) const {
308    return operation(rhs, op_nand);
309}
310const Region Region::operation(const Rect& rhs, int op) const {
311    Region result;
312    boolean_operation(op, result, *this, rhs);
313    return result;
314}
315
316// ----------------------------------------------------------------------------
317
318const Region Region::merge(const Region& rhs) const {
319    return operation(rhs, op_or);
320}
321const Region Region::mergeExclusive(const Region& rhs) const {
322    return operation(rhs, op_xor);
323}
324const Region Region::intersect(const Region& rhs) const {
325    return operation(rhs, op_and);
326}
327const Region Region::subtract(const Region& rhs) const {
328    return operation(rhs, op_nand);
329}
330const Region Region::operation(const Region& rhs, int op) const {
331    Region result;
332    boolean_operation(op, result, *this, rhs);
333    return result;
334}
335
336const Region Region::translate(int x, int y) const {
337    Region result;
338    translate(result, *this, x, y);
339    return result;
340}
341
342// ----------------------------------------------------------------------------
343
344Region& Region::orSelf(const Region& rhs, int dx, int dy) {
345    return operationSelf(rhs, dx, dy, op_or);
346}
347Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
348    return operationSelf(rhs, dx, dy, op_xor);
349}
350Region& Region::andSelf(const Region& rhs, int dx, int dy) {
351    return operationSelf(rhs, dx, dy, op_and);
352}
353Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
354    return operationSelf(rhs, dx, dy, op_nand);
355}
356Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) {
357    Region lhs(*this);
358    boolean_operation(op, *this, lhs, rhs, dx, dy);
359    return *this;
360}
361
362// ----------------------------------------------------------------------------
363
364const Region Region::merge(const Region& rhs, int dx, int dy) const {
365    return operation(rhs, dx, dy, op_or);
366}
367const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
368    return operation(rhs, dx, dy, op_xor);
369}
370const Region Region::intersect(const Region& rhs, int dx, int dy) const {
371    return operation(rhs, dx, dy, op_and);
372}
373const Region Region::subtract(const Region& rhs, int dx, int dy) const {
374    return operation(rhs, dx, dy, op_nand);
375}
376const Region Region::operation(const Region& rhs, int dx, int dy, int op) const {
377    Region result;
378    boolean_operation(op, result, *this, rhs, dx, dy);
379    return result;
380}
381
382// ----------------------------------------------------------------------------
383
384// This is our region rasterizer, which merges rects and spans together
385// to obtain an optimal region.
386class Region::rasterizer : public region_operator<Rect>::region_rasterizer
387{
388    Rect bounds;
389    Vector<Rect>& storage;
390    Rect* head;
391    Rect* tail;
392    Vector<Rect> span;
393    Rect* cur;
394public:
395    rasterizer(Region& reg)
396        : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
397        storage.clear();
398    }
399
400    ~rasterizer() {
401        if (span.size()) {
402            flushSpan();
403        }
404        if (storage.size()) {
405            bounds.top = storage.itemAt(0).top;
406            bounds.bottom = storage.top().bottom;
407            if (storage.size() == 1) {
408                storage.clear();
409            }
410        } else {
411            bounds.left  = 0;
412            bounds.right = 0;
413        }
414        storage.add(bounds);
415    }
416
417    virtual void operator()(const Rect& rect) {
418        //ALOGD(">>> %3d, %3d, %3d, %3d",
419        //        rect.left, rect.top, rect.right, rect.bottom);
420        if (span.size()) {
421            if (cur->top != rect.top) {
422                flushSpan();
423            } else if (cur->right == rect.left) {
424                cur->right = rect.right;
425                return;
426            }
427        }
428        span.add(rect);
429        cur = span.editArray() + (span.size() - 1);
430    }
431private:
432    template<typename T>
433    static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
434    template<typename T>
435    static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
436    void flushSpan() {
437        bool merge = false;
438        if (tail-head == ssize_t(span.size())) {
439            Rect const* p = span.editArray();
440            Rect const* q = head;
441            if (p->top == q->bottom) {
442                merge = true;
443                while (q != tail) {
444                    if ((p->left != q->left) || (p->right != q->right)) {
445                        merge = false;
446                        break;
447                    }
448                    p++, q++;
449                }
450            }
451        }
452        if (merge) {
453            const int bottom = span[0].bottom;
454            Rect* r = head;
455            while (r != tail) {
456                r->bottom = bottom;
457                r++;
458            }
459        } else {
460            bounds.left = min(span.itemAt(0).left, bounds.left);
461            bounds.right = max(span.top().right, bounds.right);
462            storage.appendVector(span);
463            tail = storage.editArray() + storage.size();
464            head = tail - span.size();
465        }
466        span.clear();
467    }
468};
469
470bool Region::validate(const Region& reg, const char* name, bool silent)
471{
472    bool result = true;
473    const_iterator cur = reg.begin();
474    const_iterator const tail = reg.end();
475    const_iterator prev = cur;
476    Rect b(*prev);
477    while (cur != tail) {
478        if (cur->isValid() == false) {
479            ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
480            result = false;
481        }
482        if (cur->right > region_operator<Rect>::max_value) {
483            ALOGE_IF(!silent, "%s: rect->right > max_value", name);
484            result = false;
485        }
486        if (cur->bottom > region_operator<Rect>::max_value) {
487            ALOGE_IF(!silent, "%s: rect->right > max_value", name);
488            result = false;
489        }
490        if (prev != cur) {
491            b.left   = b.left   < cur->left   ? b.left   : cur->left;
492            b.top    = b.top    < cur->top    ? b.top    : cur->top;
493            b.right  = b.right  > cur->right  ? b.right  : cur->right;
494            b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
495            if ((*prev < *cur) == false) {
496                ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
497                result = false;
498            }
499            if (cur->top == prev->top) {
500                if (cur->bottom != prev->bottom) {
501                    ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
502                    result = false;
503                } else if (cur->left < prev->right) {
504                    ALOGE_IF(!silent,
505                            "%s: spans overlap horizontally prev=%p, cur=%p",
506                            name, prev, cur);
507                    result = false;
508                }
509            } else if (cur->top < prev->bottom) {
510                ALOGE_IF(!silent,
511                        "%s: spans overlap vertically prev=%p, cur=%p",
512                        name, prev, cur);
513                result = false;
514            }
515            prev = cur;
516        }
517        cur++;
518    }
519    if (b != reg.getBounds()) {
520        result = false;
521        ALOGE_IF(!silent,
522                "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
523                b.left, b.top, b.right, b.bottom,
524                reg.getBounds().left, reg.getBounds().top,
525                reg.getBounds().right, reg.getBounds().bottom);
526    }
527    if (reg.mStorage.size() == 2) {
528        result = false;
529        ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
530    }
531    if (result == false && !silent) {
532        reg.dump(name);
533        CallStack stack(LOG_TAG);
534    }
535    return result;
536}
537
538void Region::boolean_operation(int op, Region& dst,
539        const Region& lhs,
540        const Region& rhs, int dx, int dy)
541{
542#if VALIDATE_REGIONS
543    validate(lhs, "boolean_operation (before): lhs");
544    validate(rhs, "boolean_operation (before): rhs");
545    validate(dst, "boolean_operation (before): dst");
546#endif
547
548    size_t lhs_count;
549    Rect const * const lhs_rects = lhs.getArray(&lhs_count);
550
551    size_t rhs_count;
552    Rect const * const rhs_rects = rhs.getArray(&rhs_count);
553
554    region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
555    region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
556    region_operator<Rect> operation(op, lhs_region, rhs_region);
557    { // scope for rasterizer (dtor has side effects)
558        rasterizer r(dst);
559        operation(r);
560    }
561
562#if VALIDATE_REGIONS
563    validate(lhs, "boolean_operation: lhs");
564    validate(rhs, "boolean_operation: rhs");
565    validate(dst, "boolean_operation: dst");
566#endif
567
568#if VALIDATE_WITH_CORECG
569    SkRegion sk_lhs;
570    SkRegion sk_rhs;
571    SkRegion sk_dst;
572
573    for (size_t i=0 ; i<lhs_count ; i++)
574        sk_lhs.op(
575                lhs_rects[i].left   + dx,
576                lhs_rects[i].top    + dy,
577                lhs_rects[i].right  + dx,
578                lhs_rects[i].bottom + dy,
579                SkRegion::kUnion_Op);
580
581    for (size_t i=0 ; i<rhs_count ; i++)
582        sk_rhs.op(
583                rhs_rects[i].left   + dx,
584                rhs_rects[i].top    + dy,
585                rhs_rects[i].right  + dx,
586                rhs_rects[i].bottom + dy,
587                SkRegion::kUnion_Op);
588
589    const char* name = "---";
590    SkRegion::Op sk_op;
591    switch (op) {
592        case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
593        case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
594        case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
595        case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
596    }
597    sk_dst.op(sk_lhs, sk_rhs, sk_op);
598
599    if (sk_dst.isEmpty() && dst.isEmpty())
600        return;
601
602    bool same = true;
603    Region::const_iterator head = dst.begin();
604    Region::const_iterator const tail = dst.end();
605    SkRegion::Iterator it(sk_dst);
606    while (!it.done()) {
607        if (head != tail) {
608            if (
609                    head->left != it.rect().fLeft ||
610                    head->top != it.rect().fTop ||
611                    head->right != it.rect().fRight ||
612                    head->bottom != it.rect().fBottom
613            ) {
614                same = false;
615                break;
616            }
617        } else {
618            same = false;
619            break;
620        }
621        head++;
622        it.next();
623    }
624
625    if (head != tail) {
626        same = false;
627    }
628
629    if(!same) {
630        ALOGD("---\nregion boolean %s failed", name);
631        lhs.dump("lhs");
632        rhs.dump("rhs");
633        dst.dump("dst");
634        ALOGD("should be");
635        SkRegion::Iterator it(sk_dst);
636        while (!it.done()) {
637            ALOGD("    [%3d, %3d, %3d, %3d]",
638                it.rect().fLeft,
639                it.rect().fTop,
640                it.rect().fRight,
641                it.rect().fBottom);
642            it.next();
643        }
644    }
645#endif
646}
647
648void Region::boolean_operation(int op, Region& dst,
649        const Region& lhs,
650        const Rect& rhs, int dx, int dy)
651{
652    if (!rhs.isValid()) {
653        ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
654                op, rhs.left, rhs.top, rhs.right, rhs.bottom);
655        return;
656    }
657
658#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS
659    boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
660#else
661    size_t lhs_count;
662    Rect const * const lhs_rects = lhs.getArray(&lhs_count);
663
664    region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
665    region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
666    region_operator<Rect> operation(op, lhs_region, rhs_region);
667    { // scope for rasterizer (dtor has side effects)
668        rasterizer r(dst);
669        operation(r);
670    }
671
672#endif
673}
674
675void Region::boolean_operation(int op, Region& dst,
676        const Region& lhs, const Region& rhs)
677{
678    boolean_operation(op, dst, lhs, rhs, 0, 0);
679}
680
681void Region::boolean_operation(int op, Region& dst,
682        const Region& lhs, const Rect& rhs)
683{
684    boolean_operation(op, dst, lhs, rhs, 0, 0);
685}
686
687void Region::translate(Region& reg, int dx, int dy)
688{
689    if ((dx || dy) && !reg.isEmpty()) {
690#if VALIDATE_REGIONS
691        validate(reg, "translate (before)");
692#endif
693        size_t count = reg.mStorage.size();
694        Rect* rects = reg.mStorage.editArray();
695        while (count) {
696            rects->translate(dx, dy);
697            rects++;
698            count--;
699        }
700#if VALIDATE_REGIONS
701        validate(reg, "translate (after)");
702#endif
703    }
704}
705
706void Region::translate(Region& dst, const Region& reg, int dx, int dy)
707{
708    dst = reg;
709    translate(dst, dx, dy);
710}
711
712// ----------------------------------------------------------------------------
713
714size_t Region::getSize() const {
715    return mStorage.size() * sizeof(Rect);
716}
717
718status_t Region::flatten(void* buffer) const {
719#if VALIDATE_REGIONS
720    validate(*this, "Region::flatten");
721#endif
722    Rect* rects = reinterpret_cast<Rect*>(buffer);
723    memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect));
724    return NO_ERROR;
725}
726
727status_t Region::unflatten(void const* buffer, size_t size) {
728    Region result;
729    if (size >= sizeof(Rect)) {
730        Rect const* rects = reinterpret_cast<Rect const*>(buffer);
731        size_t count = size / sizeof(Rect);
732        if (count > 0) {
733            result.mStorage.clear();
734            ssize_t err = result.mStorage.insertAt(0, count);
735            if (err < 0) {
736                return status_t(err);
737            }
738            memcpy(result.mStorage.editArray(), rects, count*sizeof(Rect));
739        }
740    }
741#if VALIDATE_REGIONS
742    validate(result, "Region::unflatten");
743#endif
744
745    if (!result.validate(result, "Region::unflatten", true)) {
746        ALOGE("Region::unflatten() failed, invalid region");
747        return BAD_VALUE;
748    }
749    mStorage = result.mStorage;
750    return NO_ERROR;
751}
752
753// ----------------------------------------------------------------------------
754
755Region::const_iterator Region::begin() const {
756    return mStorage.array();
757}
758
759Region::const_iterator Region::end() const {
760    size_t numRects = isRect() ? 1 : mStorage.size() - 1;
761    return mStorage.array() + numRects;
762}
763
764Rect const* Region::getArray(size_t* count) const {
765    const_iterator const b(begin());
766    const_iterator const e(end());
767    if (count) *count = e-b;
768    return b;
769}
770
771SharedBuffer const* Region::getSharedBuffer(size_t* count) const {
772    // We can get to the SharedBuffer of a Vector<Rect> because Rect has
773    // a trivial destructor.
774    SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array());
775    if (count) {
776        size_t numRects = isRect() ? 1 : mStorage.size() - 1;
777        count[0] = numRects;
778    }
779    sb->acquire();
780    return sb;
781}
782
783// ----------------------------------------------------------------------------
784
785void Region::dump(String8& out, const char* what, uint32_t flags) const
786{
787    (void)flags;
788    const_iterator head = begin();
789    const_iterator const tail = end();
790
791    size_t SIZE = 256;
792    char buffer[SIZE];
793
794    snprintf(buffer, SIZE, "  Region %s (this=%p, count=%d)\n",
795            what, this, tail-head);
796    out.append(buffer);
797    while (head != tail) {
798        snprintf(buffer, SIZE, "    [%3d, %3d, %3d, %3d]\n",
799                head->left, head->top, head->right, head->bottom);
800        out.append(buffer);
801        head++;
802    }
803}
804
805void Region::dump(const char* what, uint32_t flags) const
806{
807    (void)flags;
808    const_iterator head = begin();
809    const_iterator const tail = end();
810    ALOGD("  Region %s (this=%p, count=%d)\n", what, this, tail-head);
811    while (head != tail) {
812        ALOGD("    [%3d, %3d, %3d, %3d]\n",
813                head->left, head->top, head->right, head->bottom);
814        head++;
815    }
816}
817
818// ----------------------------------------------------------------------------
819
820}; // namespace android
821