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