blob: df455a96b16d6dcfb99e34b200d900f9f55f6674 [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
54 void clear();
55 void set(const Rect& r);
Mathias Agopian0926f502009-05-04 14:17:04 -070056 void set(uint32_t w, uint32_t h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
58 Region& orSelf(const Rect& rhs);
59 Region& andSelf(const Rect& rhs);
Mathias Agopian20f68782009-05-11 00:03:41 -070060 Region& subtractSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
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
Mathias Agopian20f68782009-05-11 00:03:41 -070067 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070068 const Region merge(const Rect& rhs) const;
69 const Region intersect(const Rect& rhs) const;
70 const Region subtract(const Rect& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
72 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070073 const Region merge(const Region& rhs) const;
74 const Region intersect(const Region& rhs) const;
75 const Region subtract(const Region& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
77 // these translate rhs first
Mathias Agopian20f68782009-05-11 00:03:41 -070078 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
Mathias Agopianbed9dd12009-05-27 17:01:58 -070084 const Region translate(int dx, int dy) const;
85 const Region merge(const Region& rhs, int dx, int dy) const;
86 const Region intersect(const Region& rhs, int dx, int dy) const;
87 const Region subtract(const Region& rhs, int dx, int dy) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
89 // convenience operators overloads
Mathias Agopianbed9dd12009-05-27 17:01:58 -070090 inline const Region operator | (const Region& rhs) const;
91 inline const Region operator & (const Region& rhs) const;
92 inline const Region operator - (const Region& rhs) const;
93 inline const Region operator + (const Point& pt) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094
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
Mathias Agopian20f68782009-05-11 00:03:41 -0700100
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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
Mathias Agopian20f68782009-05-11 00:03:41 -0700108 /* 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);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
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:
Mathias Agopian20f68782009-05-11 00:03:41 -0700133 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);
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700139 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;
Mathias Agopian20f68782009-05-11 00:03:41 -0700142
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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160};
161
162
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700163const Region Region::operator | (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164 return merge(rhs);
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 intersect(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 subtract(rhs);
171}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700172const Region Region::operator + (const Point& pt) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 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 {
Mathias Agopian20f68782009-05-11 00:03:41 -0700193 region_iterator(const Region& region)
194 : b(region.begin()), e(region.end()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 this->next = iterate;
196 }
197private:
198 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
Mathias Agopian20f68782009-05-11 00:03:41 -0700199 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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 }
Mathias Agopian20f68782009-05-11 00:03:41 -0700206 mutable Region::const_iterator b;
207 Region::const_iterator const e;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208};
Mathias Agopian20f68782009-05-11 00:03:41 -0700209
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210// ---------------------------------------------------------------------------
211}; // namespace android
212
213#endif // ANDROID_UI_REGION_H
214