Region.h revision 7c1b96a165f970a09ed239bb4fb3f1b0d8f2a407
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#ifndef ANDROID_UI_REGION_H
18#define ANDROID_UI_REGION_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Vector.h>
24#include <utils/Parcel.h>
25
26#include <ui/Rect.h>
27#include <ui/BlitHardware.h>
28
29#include <corecg/SkRegion.h>
30
31namespace android {
32// ---------------------------------------------------------------------------
33
34class String8;
35
36// ---------------------------------------------------------------------------
37class Region
38{
39public:
40                        Region();
41                        Region(const Region& rhs);
42    explicit            Region(const SkRegion& rhs);
43    explicit            Region(const Rect& rhs);
44    explicit            Region(const Parcel& parcel);
45    explicit            Region(const void* buffer);
46                        ~Region();
47
48        Region& operator = (const Region& rhs);
49
50    inline  bool        isEmpty() const     { return mRegion.isEmpty(); }
51    inline  bool        isRect() const      { return mRegion.isRect(); }
52
53            Rect        bounds() const;
54
55            const SkRegion& toSkRegion() const;
56
57            void        clear();
58            void        set(const Rect& r);
59
60            Region&     orSelf(const Rect& rhs);
61            Region&     andSelf(const Rect& rhs);
62
63            // boolean operators, applied on this
64            Region&     orSelf(const Region& rhs);
65            Region&     andSelf(const Region& rhs);
66            Region&     subtractSelf(const Region& rhs);
67
68            // these translate rhs first
69            Region&     translateSelf(int dx, int dy);
70            Region&     orSelf(const Region& rhs, int dx, int dy);
71            Region&     andSelf(const Region& rhs, int dx, int dy);
72            Region&     subtractSelf(const Region& rhs, int dx, int dy);
73
74            // boolean operators
75            Region      merge(const Region& rhs) const;
76            Region      intersect(const Region& rhs) const;
77            Region      subtract(const Region& rhs) const;
78
79            // these translate rhs first
80            Region      translate(int dx, int dy) const;
81            Region      merge(const Region& rhs, int dx, int dy) const;
82            Region      intersect(const Region& rhs, int dx, int dy) const;
83            Region      subtract(const Region& rhs, int dx, int dy) const;
84
85    // convenience operators overloads
86    inline  Region      operator | (const Region& rhs) const;
87    inline  Region      operator & (const Region& rhs) const;
88    inline  Region      operator - (const Region& rhs) const;
89    inline  Region      operator + (const Point& pt) const;
90
91    inline  Region&     operator |= (const Region& rhs);
92    inline  Region&     operator &= (const Region& rhs);
93    inline  Region&     operator -= (const Region& rhs);
94    inline  Region&     operator += (const Point& pt);
95
96    class iterator {
97        SkRegion::Iterator  mIt;
98    public:
99        iterator(const Region& r);
100        inline operator bool () const { return !done(); }
101        int iterate(Rect* rect);
102    private:
103        inline bool done() const {
104            return const_cast<SkRegion::Iterator&>(mIt).done();
105        }
106    };
107
108            size_t      rects(Vector<Rect>& rectList) const;
109
110            // flatten/unflatten a region to/from a Parcel
111            status_t    write(Parcel& parcel) const;
112            status_t    read(const Parcel& parcel);
113
114            // flatten/unflatten a region to/from a raw buffer
115            ssize_t     write(void* buffer, size_t size) const;
116    static  ssize_t     writeEmpty(void* buffer, size_t size);
117
118            ssize_t     read(const void* buffer);
119    static  bool        isEmpty(void* buffer);
120
121    void        dump(String8& out, const char* what, uint32_t flags=0) const;
122    void        dump(const char* what, uint32_t flags=0) const;
123
124private:
125    SkRegion    mRegion;
126};
127
128
129Region Region::operator | (const Region& rhs) const {
130    return merge(rhs);
131}
132Region Region::operator & (const Region& rhs) const {
133    return intersect(rhs);
134}
135Region Region::operator - (const Region& rhs) const {
136    return subtract(rhs);
137}
138Region Region::operator + (const Point& pt) const {
139    return translate(pt.x, pt.y);
140}
141
142
143Region& Region::operator |= (const Region& rhs) {
144    return orSelf(rhs);
145}
146Region& Region::operator &= (const Region& rhs) {
147    return andSelf(rhs);
148}
149Region& Region::operator -= (const Region& rhs) {
150    return subtractSelf(rhs);
151}
152Region& Region::operator += (const Point& pt) {
153    return translateSelf(pt.x, pt.y);
154}
155
156// ---------------------------------------------------------------------------
157
158struct region_iterator : public copybit_region_t {
159    region_iterator(const Region& region) : i(region) {
160        this->next = iterate;
161    }
162private:
163    static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
164        return static_cast<const region_iterator*>(self)
165        ->i.iterate(reinterpret_cast<Rect*>(rect));
166    }
167    mutable Region::iterator i;
168};
169
170// ---------------------------------------------------------------------------
171}; // namespace android
172
173#endif // ANDROID_UI_REGION_H
174
175