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