2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_UI_REGION_H
18 #define ANDROID_UI_REGION_H
21 #include <sys/types.h>
23 #include <utils/Vector.h>
26 #include <utils/Flattenable.h>
29 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
34 class Region : public LightFlattenable<Region>
38 Region(const Region& rhs);
39 explicit Region(const Rect& rhs);
42 Region& operator = (const Region& rhs);
44 inline bool isEmpty() const { return getBounds().isEmpty(); }
45 inline bool isRect() const { return mStorage.size() == 1; }
47 inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; }
48 inline Rect bounds() const { return getBounds(); }
50 // the region becomes its bounds
51 Region& makeBoundsSelf();
54 void set(const Rect& r);
55 void set(uint32_t w, uint32_t h);
57 Region& orSelf(const Rect& rhs);
58 Region& xorSelf(const Rect& rhs);
59 Region& andSelf(const Rect& rhs);
60 Region& subtractSelf(const Rect& rhs);
62 // boolean operators, applied on this
63 Region& orSelf(const Region& rhs);
64 Region& xorSelf(const Region& rhs);
65 Region& andSelf(const Region& rhs);
66 Region& subtractSelf(const Region& rhs);
69 const Region merge(const Rect& rhs) const;
70 const Region mergeExclusive(const Rect& rhs) const;
71 const Region intersect(const Rect& rhs) const;
72 const Region subtract(const Rect& rhs) const;
75 const Region merge(const Region& rhs) const;
76 const Region mergeExclusive(const Region& rhs) const;
77 const Region intersect(const Region& rhs) const;
78 const Region subtract(const Region& rhs) const;
80 // these translate rhs first
81 Region& translateSelf(int dx, int dy);
82 Region& orSelf(const Region& rhs, int dx, int dy);
83 Region& xorSelf(const Region& rhs, int dx, int dy);
84 Region& andSelf(const Region& rhs, int dx, int dy);
85 Region& subtractSelf(const Region& rhs, int dx, int dy);
87 // these translate rhs first
88 const Region translate(int dx, int dy) const;
89 const Region merge(const Region& rhs, int dx, int dy) const;
90 const Region mergeExclusive(const Region& rhs, int dx, int dy) const;
91 const Region intersect(const Region& rhs, int dx, int dy) const;
92 const Region subtract(const Region& rhs, int dx, int dy) const;
94 // convenience operators overloads
95 inline const Region operator | (const Region& rhs) const;
96 inline const Region operator ^ (const Region& rhs) const;
97 inline const Region operator & (const Region& rhs) const;
98 inline const Region operator - (const Region& rhs) const;
99 inline const Region operator + (const Point& pt) const;
101 inline Region& operator |= (const Region& rhs);
102 inline Region& operator ^= (const Region& rhs);
103 inline Region& operator &= (const Region& rhs);
104 inline Region& operator -= (const Region& rhs);
105 inline Region& operator += (const Point& pt);
108 /* various ways to access the rectangle list */
110 typedef Rect const* const_iterator;
112 const_iterator begin() const;
113 const_iterator end() const;
115 /* no user serviceable parts here... */
117 Rect const* getArray(size_t* count) const;
120 // add a rectangle to the internal list. This rectangle must
121 // be sorted in Y and X and must not make the region invalid.
122 void addRectUnchecked(int l, int t, int r, int b);
124 inline bool isFixedSize() const { return false; }
125 size_t getSize() const;
126 status_t flatten(void* buffer) const;
127 status_t unflatten(void const* buffer, size_t size);
129 void dump(String8& out, const char* what, uint32_t flags=0) const;
130 void dump(const char* what, uint32_t flags=0) const;
134 friend class rasterizer;
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 const Region operation(const Rect& rhs, int op) const;
140 const Region operation(const Region& rhs, int op) const;
141 const Region operation(const Region& rhs, int dx, int dy, int op) const;
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);
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);
153 static void translate(Region& reg, int dx, int dy);
154 static void translate(Region& dst, const Region& reg, int dx, int dy);
156 static bool validate(const Region& reg, const char* name);
158 // mStorage is a (manually) sorted array of Rects describing the region
159 // with an extra Rect as the last element which is set to the
160 // bounds of the region. However, if the region is
161 // a simple Rect then mStorage contains only that rect.
162 Vector<Rect> mStorage;
166 const Region Region::operator | (const Region& rhs) const {
169 const Region Region::operator ^ (const Region& rhs) const {
170 return mergeExclusive(rhs);
172 const Region Region::operator & (const Region& rhs) const {
173 return intersect(rhs);
175 const Region Region::operator - (const Region& rhs) const {
176 return subtract(rhs);
178 const Region Region::operator + (const Point& pt) const {
179 return translate(pt.x, pt.y);
183 Region& Region::operator |= (const Region& rhs) {
186 Region& Region::operator ^= (const Region& rhs) {
189 Region& Region::operator &= (const Region& rhs) {
192 Region& Region::operator -= (const Region& rhs) {
193 return subtractSelf(rhs);
195 Region& Region::operator += (const Point& pt) {
196 return translateSelf(pt.x, pt.y);
198 // ---------------------------------------------------------------------------
199 }; // namespace android
201 #endif // ANDROID_UI_REGION_H