Region.cpp revision 92dc3fc52cf097bd105460cf377779bdcf146d62
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 <inttypes.h>
20#include <limits.h>
21
22#include <utils/Log.h>
23#include <utils/String8.h>
24#include <utils/CallStack.h>
25
26#include <ui/Rect.h>
27#include <ui/Region.h>
28#include <ui/Point.h>
29
30#include <private/ui/RegionHelper.h>
31
32// ----------------------------------------------------------------------------
33#define VALIDATE_REGIONS        (false)
34#define VALIDATE_WITH_CORECG    (false)
35// ----------------------------------------------------------------------------
36
37#if VALIDATE_WITH_CORECG
38#include <core/SkRegion.h>
39#endif
40
41namespace android {
42// ----------------------------------------------------------------------------
43
44enum {
45    op_nand = region_operator<Rect>::op_nand,
46    op_and  = region_operator<Rect>::op_and,
47    op_or   = region_operator<Rect>::op_or,
48    op_xor  = region_operator<Rect>::op_xor
49};
50
51enum {
52    direction_LTR,
53    direction_RTL
54};
55
56// ----------------------------------------------------------------------------
57
58Region::Region() {
59    mStorage.add(Rect(0,0));
60}
61
62Region::Region(const Region& rhs)
63    : mStorage(rhs.mStorage)
64{
65#if VALIDATE_REGIONS
66    validate(rhs, "rhs copy-ctor");
67#endif
68}
69
70Region::Region(const Rect& rhs) {
71    mStorage.add(rhs);
72}
73
74Region::~Region()
75{
76}
77
78/**
79 * Copy rects from the src vector into the dst vector, resolving vertical T-Junctions along the way
80 *
81 * First pass through, divideSpanRTL will be set because the 'previous span' (indexing into the dst
82 * vector) will be reversed. Each rectangle in the original list, starting from the bottom, will be
83 * compared with the span directly below, and subdivided as needed to resolve T-junctions.
84 *
85 * The resulting temporary vector will be a completely reversed copy of the original, without any
86 * bottom-up T-junctions.
87 *
88 * Second pass through, divideSpanRTL will be false since the previous span will index into the
89 * final, correctly ordered region buffer. Each rectangle will be compared with the span directly
90 * above it, and subdivided to resolve any remaining T-junctions.
91 */
92static void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end,
93        Vector<Rect>& dst, int spanDirection) {
94    dst.clear();
95
96    const Rect* current = end - 1;
97    int lastTop = current->top;
98
99    // add first span immediately
100    do {
101        dst.add(*current);
102        current--;
103    } while (current->top == lastTop && current >= begin);
104
105    unsigned int beginLastSpan = -1;
106    unsigned int endLastSpan = -1;
107    int top = -1;
108    int bottom = -1;
109
110    // for all other spans, split if a t-junction exists in the span directly above
111    while (current >= begin) {
112        if (current->top != (current + 1)->top) {
113            // new span
114            if ((spanDirection == direction_RTL && current->bottom != (current + 1)->top) ||
115                    (spanDirection == direction_LTR && current->top != (current + 1)->bottom)) {
116                // previous span not directly adjacent, don't check for T junctions
117                beginLastSpan = INT_MAX;
118            } else {
119                beginLastSpan = endLastSpan + 1;
120            }
121            endLastSpan = dst.size() - 1;
122
123            top = current->top;
124            bottom = current->bottom;
125        }
126        int left = current->left;
127        int right = current->right;
128
129        for (unsigned int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) {
130            const Rect* prev = &dst[prevIndex];
131            if (spanDirection == direction_RTL) {
132                // iterating over previous span RTL, quit if it's too far left
133                if (prev->right <= left) break;
134
135                if (prev->right > left && prev->right < right) {
136                    dst.add(Rect(prev->right, top, right, bottom));
137                    right = prev->right;
138                }
139
140                if (prev->left > left && prev->left < right) {
141                    dst.add(Rect(prev->left, top, right, bottom));
142                    right = prev->left;
143                }
144
145                // if an entry in the previous span is too far right, nothing further left in the
146                // current span will need it
147                if (prev->left >= right) {
148                    beginLastSpan = prevIndex;
149                }
150            } else {
151                // iterating over previous span LTR, quit if it's too far right
152                if (prev->left >= right) break;
153
154                if (prev->left > left && prev->left < right) {
155                    dst.add(Rect(left, top, prev->left, bottom));
156                    left = prev->left;
157                }
158
159                if (prev->right > left && prev->right < right) {
160                    dst.add(Rect(left, top, prev->right, bottom));
161                    left = prev->right;
162                }
163                // if an entry in the previous span is too far left, nothing further right in the
164                // current span will need it
165                if (prev->right <= left) {
166                    beginLastSpan = prevIndex;
167                }
168            }
169        }
170
171        if (left < right) {
172            dst.add(Rect(left, top, right, bottom));
173        }
174
175        current--;
176    }
177}
178
179/**
180 * Creates a new region with the same data as the argument, but divides rectangles as necessary to
181 * remove T-Junctions
182 *
183 * Note: the output will not necessarily be a very efficient representation of the region, since it
184 * may be that a triangle-based approach would generate significantly simpler geometry
185 */
186Region Region::createTJunctionFreeRegion(const Region& r) {
187    if (r.isEmpty()) return r;
188    if (r.isRect()) return r;
189
190    Vector<Rect> reversed;
191    reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL);
192
193    Region outputRegion;
194    reverseRectsResolvingJunctions(reversed.begin(), reversed.end(),
195            outputRegion.mStorage, direction_LTR);
196    outputRegion.mStorage.add(r.getBounds()); // to make region valid, mStorage must end with bounds
197
198#if VALIDATE_REGIONS
199    validate(outputRegion, "T-Junction free region");
200#endif
201
202    return outputRegion;
203}
204
205Region& Region::operator = (const Region& rhs)
206{
207#if VALIDATE_REGIONS
208    validate(*this, "this->operator=");
209    validate(rhs, "rhs.operator=");
210#endif
211    mStorage = rhs.mStorage;
212    return *this;
213}
214
215Region& Region::makeBoundsSelf()
216{
217    if (mStorage.size() >= 2) {
218        const Rect bounds(getBounds());
219        mStorage.clear();
220        mStorage.add(bounds);
221    }
222    return *this;
223}
224
225void Region::clear()
226{
227    mStorage.clear();
228    mStorage.add(Rect(0,0));
229}
230
231void Region::set(const Rect& r)
232{
233    mStorage.clear();
234    mStorage.add(r);
235}
236
237void Region::set(uint32_t w, uint32_t h)
238{
239    mStorage.clear();
240    mStorage.add(Rect(w,h));
241}
242
243bool Region::isTriviallyEqual(const Region& region) const {
244    return begin() == region.begin();
245}
246
247// ----------------------------------------------------------------------------
248
249void Region::addRectUnchecked(int l, int t, int r, int b)
250{
251    Rect rect(l,t,r,b);
252    size_t where = mStorage.size() - 1;
253    mStorage.insertAt(rect, where, 1);
254}
255
256// ----------------------------------------------------------------------------
257
258Region& Region::orSelf(const Rect& r) {
259    return operationSelf(r, op_or);
260}
261Region& Region::xorSelf(const Rect& r) {
262    return operationSelf(r, op_xor);
263}
264Region& Region::andSelf(const Rect& r) {
265    return operationSelf(r, op_and);
266}
267Region& Region::subtractSelf(const Rect& r) {
268    return operationSelf(r, op_nand);
269}
270Region& Region::operationSelf(const Rect& r, int op) {
271    Region lhs(*this);
272    boolean_operation(op, *this, lhs, r);
273    return *this;
274}
275
276// ----------------------------------------------------------------------------
277
278Region& Region::orSelf(const Region& rhs) {
279    return operationSelf(rhs, op_or);
280}
281Region& Region::xorSelf(const Region& rhs) {
282    return operationSelf(rhs, op_xor);
283}
284Region& Region::andSelf(const Region& rhs) {
285    return operationSelf(rhs, op_and);
286}
287Region& Region::subtractSelf(const Region& rhs) {
288    return operationSelf(rhs, op_nand);
289}
290Region& Region::operationSelf(const Region& rhs, int op) {
291    Region lhs(*this);
292    boolean_operation(op, *this, lhs, rhs);
293    return *this;
294}
295
296Region& Region::translateSelf(int x, int y) {
297    if (x|y) translate(*this, x, y);
298    return *this;
299}
300
301// ----------------------------------------------------------------------------
302
303const Region Region::merge(const Rect& rhs) const {
304    return operation(rhs, op_or);
305}
306const Region Region::mergeExclusive(const Rect& rhs) const {
307    return operation(rhs, op_xor);
308}
309const Region Region::intersect(const Rect& rhs) const {
310    return operation(rhs, op_and);
311}
312const Region Region::subtract(const Rect& rhs) const {
313    return operation(rhs, op_nand);
314}
315const Region Region::operation(const Rect& rhs, int op) const {
316    Region result;
317    boolean_operation(op, result, *this, rhs);
318    return result;
319}
320
321// ----------------------------------------------------------------------------
322
323const Region Region::merge(const Region& rhs) const {
324    return operation(rhs, op_or);
325}
326const Region Region::mergeExclusive(const Region& rhs) const {
327    return operation(rhs, op_xor);
328}
329const Region Region::intersect(const Region& rhs) const {
330    return operation(rhs, op_and);
331}
332const Region Region::subtract(const Region& rhs) const {
333    return operation(rhs, op_nand);
334}
335const Region Region::operation(const Region& rhs, int op) const {
336    Region result;
337    boolean_operation(op, result, *this, rhs);
338    return result;
339}
340
341const Region Region::translate(int x, int y) const {
342    Region result;
343    translate(result, *this, x, y);
344    return result;
345}
346
347// ----------------------------------------------------------------------------
348
349Region& Region::orSelf(const Region& rhs, int dx, int dy) {
350    return operationSelf(rhs, dx, dy, op_or);
351}
352Region& Region::xorSelf(const Region& rhs, int dx, int dy) {
353    return operationSelf(rhs, dx, dy, op_xor);
354}
355Region& Region::andSelf(const Region& rhs, int dx, int dy) {
356    return operationSelf(rhs, dx, dy, op_and);
357}
358Region& Region::subtractSelf(const Region& rhs, int dx, int dy) {
359    return operationSelf(rhs, dx, dy, op_nand);
360}
361Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) {
362    Region lhs(*this);
363    boolean_operation(op, *this, lhs, rhs, dx, dy);
364    return *this;
365}
366
367// ----------------------------------------------------------------------------
368
369const Region Region::merge(const Region& rhs, int dx, int dy) const {
370    return operation(rhs, dx, dy, op_or);
371}
372const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const {
373    return operation(rhs, dx, dy, op_xor);
374}
375const Region Region::intersect(const Region& rhs, int dx, int dy) const {
376    return operation(rhs, dx, dy, op_and);
377}
378const Region Region::subtract(const Region& rhs, int dx, int dy) const {
379    return operation(rhs, dx, dy, op_nand);
380}
381const Region Region::operation(const Region& rhs, int dx, int dy, int op) const {
382    Region result;
383    boolean_operation(op, result, *this, rhs, dx, dy);
384    return result;
385}
386
387// ----------------------------------------------------------------------------
388
389// This is our region rasterizer, which merges rects and spans together
390// to obtain an optimal region.
391class Region::rasterizer : public region_operator<Rect>::region_rasterizer
392{
393    Rect bounds;
394    Vector<Rect>& storage;
395    Rect* head;
396    Rect* tail;
397    Vector<Rect> span;
398    Rect* cur;
399public:
400    rasterizer(Region& reg)
401        : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() {
402        storage.clear();
403    }
404
405    ~rasterizer() {
406        if (span.size()) {
407            flushSpan();
408        }
409        if (storage.size()) {
410            bounds.top = storage.itemAt(0).top;
411            bounds.bottom = storage.top().bottom;
412            if (storage.size() == 1) {
413                storage.clear();
414            }
415        } else {
416            bounds.left  = 0;
417            bounds.right = 0;
418        }
419        storage.add(bounds);
420    }
421
422    virtual void operator()(const Rect& rect) {
423        //ALOGD(">>> %3d, %3d, %3d, %3d",
424        //        rect.left, rect.top, rect.right, rect.bottom);
425        if (span.size()) {
426            if (cur->top != rect.top) {
427                flushSpan();
428            } else if (cur->right == rect.left) {
429                cur->right = rect.right;
430                return;
431            }
432        }
433        span.add(rect);
434        cur = span.editArray() + (span.size() - 1);
435    }
436private:
437    template<typename T>
438    static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; }
439    template<typename T>
440    static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; }
441    void flushSpan() {
442        bool merge = false;
443        if (tail-head == ssize_t(span.size())) {
444            Rect const* p = span.editArray();
445            Rect const* q = head;
446            if (p->top == q->bottom) {
447                merge = true;
448                while (q != tail) {
449                    if ((p->left != q->left) || (p->right != q->right)) {
450                        merge = false;
451                        break;
452                    }
453                    p++, q++;
454                }
455            }
456        }
457        if (merge) {
458            const int bottom = span[0].bottom;
459            Rect* r = head;
460            while (r != tail) {
461                r->bottom = bottom;
462                r++;
463            }
464        } else {
465            bounds.left = min(span.itemAt(0).left, bounds.left);
466            bounds.right = max(span.top().right, bounds.right);
467            storage.appendVector(span);
468            tail = storage.editArray() + storage.size();
469            head = tail - span.size();
470        }
471        span.clear();
472    }
473};
474
475bool Region::validate(const Region& reg, const char* name, bool silent)
476{
477    bool result = true;
478    const_iterator cur = reg.begin();
479    const_iterator const tail = reg.end();
480    const_iterator prev = cur;
481    Rect b(*prev);
482    while (cur != tail) {
483        if (cur->isValid() == false) {
484            ALOGE_IF(!silent, "%s: region contains an invalid Rect", name);
485            result = false;
486        }
487        if (cur->right > region_operator<Rect>::max_value) {
488            ALOGE_IF(!silent, "%s: rect->right > max_value", name);
489            result = false;
490        }
491        if (cur->bottom > region_operator<Rect>::max_value) {
492            ALOGE_IF(!silent, "%s: rect->right > max_value", name);
493            result = false;
494        }
495        if (prev != cur) {
496            b.left   = b.left   < cur->left   ? b.left   : cur->left;
497            b.top    = b.top    < cur->top    ? b.top    : cur->top;
498            b.right  = b.right  > cur->right  ? b.right  : cur->right;
499            b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom;
500            if ((*prev < *cur) == false) {
501                ALOGE_IF(!silent, "%s: region's Rects not sorted", name);
502                result = false;
503            }
504            if (cur->top == prev->top) {
505                if (cur->bottom != prev->bottom) {
506                    ALOGE_IF(!silent, "%s: invalid span %p", name, cur);
507                    result = false;
508                } else if (cur->left < prev->right) {
509                    ALOGE_IF(!silent,
510                            "%s: spans overlap horizontally prev=%p, cur=%p",
511                            name, prev, cur);
512                    result = false;
513                }
514            } else if (cur->top < prev->bottom) {
515                ALOGE_IF(!silent,
516                        "%s: spans overlap vertically prev=%p, cur=%p",
517                        name, prev, cur);
518                result = false;
519            }
520            prev = cur;
521        }
522        cur++;
523    }
524    if (b != reg.getBounds()) {
525        result = false;
526        ALOGE_IF(!silent,
527                "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name,
528                b.left, b.top, b.right, b.bottom,
529                reg.getBounds().left, reg.getBounds().top,
530                reg.getBounds().right, reg.getBounds().bottom);
531    }
532    if (reg.mStorage.size() == 2) {
533        result = false;
534        ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name);
535    }
536    if (result == false && !silent) {
537        reg.dump(name);
538        CallStack stack(LOG_TAG);
539    }
540    return result;
541}
542
543void Region::boolean_operation(int op, Region& dst,
544        const Region& lhs,
545        const Region& rhs, int dx, int dy)
546{
547#if VALIDATE_REGIONS
548    validate(lhs, "boolean_operation (before): lhs");
549    validate(rhs, "boolean_operation (before): rhs");
550    validate(dst, "boolean_operation (before): dst");
551#endif
552
553    size_t lhs_count;
554    Rect const * const lhs_rects = lhs.getArray(&lhs_count);
555
556    size_t rhs_count;
557    Rect const * const rhs_rects = rhs.getArray(&rhs_count);
558
559    region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
560    region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy);
561    region_operator<Rect> operation(op, lhs_region, rhs_region);
562    { // scope for rasterizer (dtor has side effects)
563        rasterizer r(dst);
564        operation(r);
565    }
566
567#if VALIDATE_REGIONS
568    validate(lhs, "boolean_operation: lhs");
569    validate(rhs, "boolean_operation: rhs");
570    validate(dst, "boolean_operation: dst");
571#endif
572
573#if VALIDATE_WITH_CORECG
574    SkRegion sk_lhs;
575    SkRegion sk_rhs;
576    SkRegion sk_dst;
577
578    for (size_t i=0 ; i<lhs_count ; i++)
579        sk_lhs.op(
580                lhs_rects[i].left   + dx,
581                lhs_rects[i].top    + dy,
582                lhs_rects[i].right  + dx,
583                lhs_rects[i].bottom + dy,
584                SkRegion::kUnion_Op);
585
586    for (size_t i=0 ; i<rhs_count ; i++)
587        sk_rhs.op(
588                rhs_rects[i].left   + dx,
589                rhs_rects[i].top    + dy,
590                rhs_rects[i].right  + dx,
591                rhs_rects[i].bottom + dy,
592                SkRegion::kUnion_Op);
593
594    const char* name = "---";
595    SkRegion::Op sk_op;
596    switch (op) {
597        case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break;
598        case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break;
599        case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break;
600        case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break;
601    }
602    sk_dst.op(sk_lhs, sk_rhs, sk_op);
603
604    if (sk_dst.isEmpty() && dst.isEmpty())
605        return;
606
607    bool same = true;
608    Region::const_iterator head = dst.begin();
609    Region::const_iterator const tail = dst.end();
610    SkRegion::Iterator it(sk_dst);
611    while (!it.done()) {
612        if (head != tail) {
613            if (
614                    head->left != it.rect().fLeft ||
615                    head->top != it.rect().fTop ||
616                    head->right != it.rect().fRight ||
617                    head->bottom != it.rect().fBottom
618            ) {
619                same = false;
620                break;
621            }
622        } else {
623            same = false;
624            break;
625        }
626        head++;
627        it.next();
628    }
629
630    if (head != tail) {
631        same = false;
632    }
633
634    if(!same) {
635        ALOGD("---\nregion boolean %s failed", name);
636        lhs.dump("lhs");
637        rhs.dump("rhs");
638        dst.dump("dst");
639        ALOGD("should be");
640        SkRegion::Iterator it(sk_dst);
641        while (!it.done()) {
642            ALOGD("    [%3d, %3d, %3d, %3d]",
643                it.rect().fLeft,
644                it.rect().fTop,
645                it.rect().fRight,
646                it.rect().fBottom);
647            it.next();
648        }
649    }
650#endif
651}
652
653void Region::boolean_operation(int op, Region& dst,
654        const Region& lhs,
655        const Rect& rhs, int dx, int dy)
656{
657    if (!rhs.isValid()) {
658        ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}",
659                op, rhs.left, rhs.top, rhs.right, rhs.bottom);
660        return;
661    }
662
663#if VALIDATE_WITH_CORECG || VALIDATE_REGIONS
664    boolean_operation(op, dst, lhs, Region(rhs), dx, dy);
665#else
666    size_t lhs_count;
667    Rect const * const lhs_rects = lhs.getArray(&lhs_count);
668
669    region_operator<Rect>::region lhs_region(lhs_rects, lhs_count);
670    region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy);
671    region_operator<Rect> operation(op, lhs_region, rhs_region);
672    { // scope for rasterizer (dtor has side effects)
673        rasterizer r(dst);
674        operation(r);
675    }
676
677#endif
678}
679
680void Region::boolean_operation(int op, Region& dst,
681        const Region& lhs, const Region& rhs)
682{
683    boolean_operation(op, dst, lhs, rhs, 0, 0);
684}
685
686void Region::boolean_operation(int op, Region& dst,
687        const Region& lhs, const Rect& rhs)
688{
689    boolean_operation(op, dst, lhs, rhs, 0, 0);
690}
691
692void Region::translate(Region& reg, int dx, int dy)
693{
694    if ((dx || dy) && !reg.isEmpty()) {
695#if VALIDATE_REGIONS
696        validate(reg, "translate (before)");
697#endif
698        size_t count = reg.mStorage.size();
699        Rect* rects = reg.mStorage.editArray();
700        while (count) {
701            rects->offsetBy(dx, dy);
702            rects++;
703            count--;
704        }
705#if VALIDATE_REGIONS
706        validate(reg, "translate (after)");
707#endif
708    }
709}
710
711void Region::translate(Region& dst, const Region& reg, int dx, int dy)
712{
713    dst = reg;
714    translate(dst, dx, dy);
715}
716
717// ----------------------------------------------------------------------------
718
719size_t Region::getFlattenedSize() const {
720    return mStorage.size() * sizeof(Rect);
721}
722
723status_t Region::flatten(void* buffer, size_t size) const {
724#if VALIDATE_REGIONS
725    validate(*this, "Region::flatten");
726#endif
727    if (size < mStorage.size() * sizeof(Rect)) {
728        return NO_MEMORY;
729    }
730    Rect* rects = reinterpret_cast<Rect*>(buffer);
731    memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect));
732    return NO_ERROR;
733}
734
735status_t Region::unflatten(void const* buffer, size_t size) {
736    Region result;
737    if (size >= sizeof(Rect)) {
738        Rect const* rects = reinterpret_cast<Rect const*>(buffer);
739        size_t count = size / sizeof(Rect);
740        if (count > 0) {
741            result.mStorage.clear();
742            ssize_t err = result.mStorage.insertAt(0, count);
743            if (err < 0) {
744                return status_t(err);
745            }
746            memcpy(result.mStorage.editArray(), rects, count*sizeof(Rect));
747        }
748    }
749#if VALIDATE_REGIONS
750    validate(result, "Region::unflatten");
751#endif
752
753    if (!result.validate(result, "Region::unflatten", true)) {
754        ALOGE("Region::unflatten() failed, invalid region");
755        return BAD_VALUE;
756    }
757    mStorage = result.mStorage;
758    return NO_ERROR;
759}
760
761// ----------------------------------------------------------------------------
762
763Region::const_iterator Region::begin() const {
764    return mStorage.array();
765}
766
767Region::const_iterator Region::end() const {
768    size_t numRects = isRect() ? 1 : mStorage.size() - 1;
769    return mStorage.array() + numRects;
770}
771
772Rect const* Region::getArray(size_t* count) const {
773    const_iterator const b(begin());
774    const_iterator const e(end());
775    if (count) *count = e-b;
776    return b;
777}
778
779SharedBuffer const* Region::getSharedBuffer(size_t* count) const {
780    // We can get to the SharedBuffer of a Vector<Rect> because Rect has
781    // a trivial destructor.
782    SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array());
783    if (count) {
784        size_t numRects = isRect() ? 1 : mStorage.size() - 1;
785        count[0] = numRects;
786    }
787    sb->acquire();
788    return sb;
789}
790
791// ----------------------------------------------------------------------------
792
793void Region::dump(String8& out, const char* what, uint32_t flags) const
794{
795    (void)flags;
796    const_iterator head = begin();
797    const_iterator const tail = end();
798
799    size_t SIZE = 256;
800    char buffer[SIZE];
801
802    snprintf(buffer, SIZE, "  Region %s (this=%p, count=%" PRIdPTR ")\n",
803            what, this, tail-head);
804    out.append(buffer);
805    while (head != tail) {
806        snprintf(buffer, SIZE, "    [%3d, %3d, %3d, %3d]\n",
807                head->left, head->top, head->right, head->bottom);
808        out.append(buffer);
809        head++;
810    }
811}
812
813void Region::dump(const char* what, uint32_t flags) const
814{
815    (void)flags;
816    const_iterator head = begin();
817    const_iterator const tail = end();
818    ALOGD("  Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head);
819    while (head != tail) {
820        ALOGD("    [%3d, %3d, %3d, %3d]\n",
821                head->left, head->top, head->right, head->bottom);
822        head++;
823    }
824}
825
826// ----------------------------------------------------------------------------
827
828}; // namespace android
829