blob: 925fd0620d605b5104e6b9b1dd24bb152ddacf89 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <ui/Rect.h>
26
27#include <hardware/copybit.h>
28
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029namespace android {
30// ---------------------------------------------------------------------------
31
32class String8;
33
34// ---------------------------------------------------------------------------
35class Region
36{
37public:
38 Region();
39 Region(const Region& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 explicit Region(const Rect& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 explicit Region(const void* buffer);
42 ~Region();
43
44 Region& operator = (const Region& rhs);
45
Mathias Agopian6158b1b2009-05-11 00:03:41 -070046 inline bool isEmpty() const { return mBounds.isEmpty(); }
47 inline bool isRect() const { return mStorage.isEmpty(); }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Mathias Agopian5a8d6f42009-06-26 19:02:40 -070049 inline Rect getBounds() const { return mBounds; }
50 inline Rect bounds() const { return getBounds(); }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Mathias Agopian276826b2009-06-29 18:46:37 -070052 // the region becomes its bounds
53 Region& makeBoundsSelf();
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 void clear();
56 void set(const Rect& r);
Mathias Agopiandff8e582009-05-04 14:17:04 -070057 void set(uint32_t w, uint32_t h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
59 Region& orSelf(const Rect& rhs);
60 Region& andSelf(const Rect& rhs);
Mathias Agopian6158b1b2009-05-11 00:03:41 -070061 Region& subtractSelf(const Rect& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
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
Mathias Agopian6158b1b2009-05-11 00:03:41 -070068 // boolean operators
Mathias Agopian12c659a2009-05-27 17:01:58 -070069 const Region merge(const Rect& rhs) const;
70 const Region intersect(const Rect& rhs) const;
71 const Region subtract(const Rect& rhs) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
73 // boolean operators
Mathias Agopian12c659a2009-05-27 17:01:58 -070074 const Region merge(const Region& rhs) const;
75 const Region intersect(const Region& rhs) const;
76 const Region subtract(const Region& rhs) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78 // these translate rhs first
Mathias Agopian6158b1b2009-05-11 00:03:41 -070079 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
Mathias Agopian12c659a2009-05-27 17:01:58 -070085 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90 // convenience operators overloads
Mathias Agopian12c659a2009-05-27 17:01:58 -070091 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
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
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700101
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700109 /* 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 // 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
129private:
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700130 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);
Mathias Agopian12c659a2009-05-27 17:01:58 -0700136 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;
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700139
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157};
158
159
Mathias Agopian12c659a2009-05-27 17:01:58 -0700160const Region Region::operator | (const Region& rhs) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 return merge(rhs);
162}
Mathias Agopian12c659a2009-05-27 17:01:58 -0700163const Region Region::operator & (const Region& rhs) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 return intersect(rhs);
165}
Mathias Agopian12c659a2009-05-27 17:01:58 -0700166const Region Region::operator - (const Region& rhs) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 return subtract(rhs);
168}
Mathias Agopian12c659a2009-05-27 17:01:58 -0700169const Region Region::operator + (const Point& pt) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 return translate(pt.x, pt.y);
171}
172
173
174Region& Region::operator |= (const Region& rhs) {
175 return orSelf(rhs);
176}
177Region& Region::operator &= (const Region& rhs) {
178 return andSelf(rhs);
179}
180Region& Region::operator -= (const Region& rhs) {
181 return subtractSelf(rhs);
182}
183Region& Region::operator += (const Point& pt) {
184 return translateSelf(pt.x, pt.y);
185}
186
187// ---------------------------------------------------------------------------
188
189struct region_iterator : public copybit_region_t {
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700190 region_iterator(const Region& region)
191 : b(region.begin()), e(region.end()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 this->next = iterate;
193 }
194private:
195 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700196 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 }
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700203 mutable Region::const_iterator b;
204 Region::const_iterator const e;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205};
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207// ---------------------------------------------------------------------------
208}; // namespace android
209
210#endif // ANDROID_UI_REGION_H
211