blob: 2bcad5b9ea4803da124eb4c8847c1019c04546a7 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070024#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025
26#include <ui/Rect.h>
27
28#include <hardware/copybit.h>
29
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030namespace android {
31// ---------------------------------------------------------------------------
32
33class String8;
34
35// ---------------------------------------------------------------------------
36class Region
37{
38public:
39 Region();
40 Region(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041 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
Mathias Agopian20f68782009-05-11 00:03:41 -070048 inline bool isEmpty() const { return mBounds.isEmpty(); }
49 inline bool isRect() const { return mStorage.isEmpty(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Mathias Agopian17b2ad02009-06-26 19:02:40 -070051 inline Rect getBounds() const { return mBounds; }
52 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian9f961452009-06-29 18:46:37 -070054 // the region becomes its bounds
55 Region& makeBoundsSelf();
56
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057 void clear();
58 void set(const Rect& r);
Mathias Agopian0926f502009-05-04 14:17:04 -070059 void set(uint32_t w, uint32_t h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
61 Region& orSelf(const Rect& rhs);
62 Region& andSelf(const Rect& rhs);
Mathias Agopian20f68782009-05-11 00:03:41 -070063 Region& subtractSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064
65 // boolean operators, applied on this
66 Region& orSelf(const Region& rhs);
67 Region& andSelf(const Region& rhs);
68 Region& subtractSelf(const Region& rhs);
69
Mathias Agopian20f68782009-05-11 00:03:41 -070070 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070071 const Region merge(const Rect& rhs) const;
72 const Region intersect(const Rect& rhs) const;
73 const Region subtract(const Rect& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
75 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070076 const Region merge(const Region& rhs) const;
77 const Region intersect(const Region& rhs) const;
78 const Region subtract(const Region& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079
80 // these translate rhs first
Mathias Agopian20f68782009-05-11 00:03:41 -070081 Region& translateSelf(int dx, int dy);
82 Region& orSelf(const Region& rhs, int dx, int dy);
83 Region& andSelf(const Region& rhs, int dx, int dy);
84 Region& subtractSelf(const Region& rhs, int dx, int dy);
85
86 // these translate rhs first
Mathias Agopianbed9dd12009-05-27 17:01:58 -070087 const Region translate(int dx, int dy) const;
88 const Region merge(const Region& rhs, int dx, int dy) const;
89 const Region intersect(const Region& rhs, int dx, int dy) const;
90 const Region subtract(const Region& rhs, int dx, int dy) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091
92 // convenience operators overloads
Mathias Agopianbed9dd12009-05-27 17:01:58 -070093 inline const Region operator | (const Region& rhs) const;
94 inline const Region operator & (const Region& rhs) const;
95 inline const Region operator - (const Region& rhs) const;
96 inline const Region operator + (const Point& pt) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097
98 inline Region& operator |= (const Region& rhs);
99 inline Region& operator &= (const Region& rhs);
100 inline Region& operator -= (const Region& rhs);
101 inline Region& operator += (const Point& pt);
102
Mathias Agopian20f68782009-05-11 00:03:41 -0700103
104 /* various ways to access the rectangle list */
105
106 typedef Rect const* const_iterator;
107
108 const_iterator begin() const;
109 const_iterator end() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
Mathias Agopian20f68782009-05-11 00:03:41 -0700111 /* no user serviceable parts here... */
112
113 size_t getRects(Vector<Rect>& rectList) const;
114 Rect const* getArray(size_t* count) const;
115
116
117 // add a rectangle to the internal list. This rectangle must
118 // be sorted in Y and X and must not make the region invalid.
119 void addRectUnchecked(int l, int t, int r, int b);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
121 // flatten/unflatten a region to/from a Parcel
122 status_t write(Parcel& parcel) const;
123 status_t read(const Parcel& parcel);
124
125 // flatten/unflatten a region to/from a raw buffer
126 ssize_t write(void* buffer, size_t size) const;
127 static ssize_t writeEmpty(void* buffer, size_t size);
128
129 ssize_t read(const void* buffer);
130 static bool isEmpty(void* buffer);
131
132 void dump(String8& out, const char* what, uint32_t flags=0) const;
133 void dump(const char* what, uint32_t flags=0) const;
134
135private:
Mathias Agopian20f68782009-05-11 00:03:41 -0700136 class rasterizer;
137 friend class rasterizer;
138
139 Region& operationSelf(const Rect& r, int op);
140 Region& operationSelf(const Region& r, int op);
141 Region& operationSelf(const Region& r, int dx, int dy, int op);
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700142 const Region operation(const Rect& rhs, int op) const;
143 const Region operation(const Region& rhs, int op) const;
144 const Region operation(const Region& rhs, int dx, int dy, int op) const;
Mathias Agopian20f68782009-05-11 00:03:41 -0700145
146 static void boolean_operation(int op, Region& dst,
147 const Region& lhs, const Region& rhs, int dx, int dy);
148 static void boolean_operation(int op, Region& dst,
149 const Region& lhs, const Rect& rhs, int dx, int dy);
150
151 static void boolean_operation(int op, Region& dst,
152 const Region& lhs, const Region& rhs);
153 static void boolean_operation(int op, Region& dst,
154 const Region& lhs, const Rect& rhs);
155
156 static void translate(Region& reg, int dx, int dy);
157 static void translate(Region& dst, const Region& reg, int dx, int dy);
158
159 static bool validate(const Region& reg, const char* name);
160
161 Rect mBounds;
162 Vector<Rect> mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163};
164
165
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700166const Region Region::operator | (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 return merge(rhs);
168}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700169const Region Region::operator & (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 return intersect(rhs);
171}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700172const Region Region::operator - (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 return subtract(rhs);
174}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700175const Region Region::operator + (const Point& pt) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 return translate(pt.x, pt.y);
177}
178
179
180Region& Region::operator |= (const Region& rhs) {
181 return orSelf(rhs);
182}
183Region& Region::operator &= (const Region& rhs) {
184 return andSelf(rhs);
185}
186Region& Region::operator -= (const Region& rhs) {
187 return subtractSelf(rhs);
188}
189Region& Region::operator += (const Point& pt) {
190 return translateSelf(pt.x, pt.y);
191}
192
193// ---------------------------------------------------------------------------
194
195struct region_iterator : public copybit_region_t {
Mathias Agopian20f68782009-05-11 00:03:41 -0700196 region_iterator(const Region& region)
197 : b(region.begin()), e(region.end()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 this->next = iterate;
199 }
200private:
201 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700202 region_iterator const* me = static_cast<region_iterator const*>(self);
203 if (me->b != me->e) {
204 *reinterpret_cast<Rect*>(rect) = *me->b++;
205 return 1;
206 }
207 return 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700209 mutable Region::const_iterator b;
210 Region::const_iterator const e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211};
Mathias Agopian20f68782009-05-11 00:03:41 -0700212
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213// ---------------------------------------------------------------------------
214}; // namespace android
215
216#endif // ANDROID_UI_REGION_H
217