Region.h revision 310f8da0c3122ed5419db8f12a6e98d3188325b2
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 <binder/Parcel.h>
25
26#include <ui/Rect.h>
27
28#include <hardware/copybit.h>
29
30namespace android {
31// ---------------------------------------------------------------------------
32
33class String8;
34
35// ---------------------------------------------------------------------------
36class Region
37{
38public:
39                        Region();
40                        Region(const Region& rhs);
41    explicit            Region(const Rect& rhs);
42    explicit            Region(const Parcel& parcel);
43    explicit            Region(const void* buffer);
44                        ~Region();
45
46        Region& operator = (const Region& rhs);
47
48    inline  bool        isEmpty() const     { return mBounds.isEmpty();  }
49    inline  bool        isRect() const      { return mStorage.isEmpty(); }
50
51    inline  const Rect& getBounds() const   { return mBounds; }
52    inline  const Rect& bounds() const      { return getBounds(); }
53
54            void        clear();
55            void        set(const Rect& r);
56            void        set(uint32_t w, uint32_t h);
57
58            Region&     orSelf(const Rect& rhs);
59            Region&     andSelf(const Rect& rhs);
60            Region&     subtractSelf(const Rect& rhs);
61
62            // boolean operators, applied on this
63            Region&     orSelf(const Region& rhs);
64            Region&     andSelf(const Region& rhs);
65            Region&     subtractSelf(const Region& rhs);
66
67            // boolean operators
68            Region      merge(const Rect& rhs) const;
69            Region      intersect(const Rect& rhs) const;
70            Region      subtract(const Rect& rhs) const;
71
72            // boolean operators
73            Region      merge(const Region& rhs) const;
74            Region      intersect(const Region& rhs) const;
75            Region      subtract(const Region& rhs) const;
76
77            // these translate rhs first
78            Region&     translateSelf(int dx, int dy);
79            Region&     orSelf(const Region& rhs, int dx, int dy);
80            Region&     andSelf(const Region& rhs, int dx, int dy);
81            Region&     subtractSelf(const Region& rhs, int dx, int dy);
82
83            // these translate rhs first
84            Region      translate(int dx, int dy) const;
85            Region      merge(const Region& rhs, int dx, int dy) const;
86            Region      intersect(const Region& rhs, int dx, int dy) const;
87            Region      subtract(const Region& rhs, int dx, int dy) const;
88
89    // convenience operators overloads
90    inline  Region      operator | (const Region& rhs) const;
91    inline  Region      operator & (const Region& rhs) const;
92    inline  Region      operator - (const Region& rhs) const;
93    inline  Region      operator + (const Point& pt) const;
94
95    inline  Region&     operator |= (const Region& rhs);
96    inline  Region&     operator &= (const Region& rhs);
97    inline  Region&     operator -= (const Region& rhs);
98    inline  Region&     operator += (const Point& pt);
99
100
101    /* various ways to access the rectangle list */
102
103    typedef Rect const* const_iterator;
104
105            const_iterator begin() const;
106            const_iterator end() const;
107
108    /* no user serviceable parts here... */
109
110            size_t      getRects(Vector<Rect>& rectList) const;
111            Rect const* getArray(size_t* count) const;
112
113
114            // add a rectangle to the internal list. This rectangle must
115            // be sorted in Y and X and must not make the region invalid.
116            void        addRectUnchecked(int l, int t, int r, int b);
117
118            // flatten/unflatten a region to/from a Parcel
119            status_t    write(Parcel& parcel) const;
120            status_t    read(const Parcel& parcel);
121
122            // flatten/unflatten a region to/from a raw buffer
123            ssize_t     write(void* buffer, size_t size) const;
124    static  ssize_t     writeEmpty(void* buffer, size_t size);
125
126            ssize_t     read(const void* buffer);
127    static  bool        isEmpty(void* buffer);
128
129    void        dump(String8& out, const char* what, uint32_t flags=0) const;
130    void        dump(const char* what, uint32_t flags=0) const;
131
132private:
133    class rasterizer;
134    friend class rasterizer;
135
136    Region& operationSelf(const Rect& r, int op);
137    Region& operationSelf(const Region& r, int op);
138    Region& operationSelf(const Region& r, int dx, int dy, int op);
139    Region operation(const Rect& rhs, int op) const;
140    Region operation(const Region& rhs, int op) const;
141    Region operation(const Region& rhs, int dx, int dy, int op) const;
142
143    static void boolean_operation(int op, Region& dst,
144            const Region& lhs, const Region& rhs, int dx, int dy);
145    static void boolean_operation(int op, Region& dst,
146            const Region& lhs, const Rect& rhs, int dx, int dy);
147
148    static void boolean_operation(int op, Region& dst,
149            const Region& lhs, const Region& rhs);
150    static void boolean_operation(int op, Region& dst,
151            const Region& lhs, const Rect& rhs);
152
153    static void translate(Region& reg, int dx, int dy);
154    static void translate(Region& dst, const Region& reg, int dx, int dy);
155
156    static bool validate(const Region& reg, const char* name);
157
158    Rect            mBounds;
159    Vector<Rect>    mStorage;
160};
161
162
163Region Region::operator | (const Region& rhs) const {
164    return merge(rhs);
165}
166Region Region::operator & (const Region& rhs) const {
167    return intersect(rhs);
168}
169Region Region::operator - (const Region& rhs) const {
170    return subtract(rhs);
171}
172Region Region::operator + (const Point& pt) const {
173    return translate(pt.x, pt.y);
174}
175
176
177Region& Region::operator |= (const Region& rhs) {
178    return orSelf(rhs);
179}
180Region& Region::operator &= (const Region& rhs) {
181    return andSelf(rhs);
182}
183Region& Region::operator -= (const Region& rhs) {
184    return subtractSelf(rhs);
185}
186Region& Region::operator += (const Point& pt) {
187    return translateSelf(pt.x, pt.y);
188}
189
190// ---------------------------------------------------------------------------
191
192struct region_iterator : public copybit_region_t {
193    region_iterator(const Region& region)
194        : b(region.begin()), e(region.end()) {
195        this->next = iterate;
196    }
197private:
198    static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
199        region_iterator const* me = static_cast<region_iterator const*>(self);
200        if (me->b != me->e) {
201            *reinterpret_cast<Rect*>(rect) = *me->b++;
202            return 1;
203        }
204        return 0;
205    }
206    mutable Region::const_iterator b;
207    Region::const_iterator const e;
208};
209
210// ---------------------------------------------------------------------------
211}; // namespace android
212
213#endif // ANDROID_UI_REGION_H
214
215