blob: 46a36edb9e719ee4456dbff0b7e813938e279534 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
25#include <ui/Rect.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070026#include <utils/Flattenable.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028namespace android {
29// ---------------------------------------------------------------------------
30
31class String8;
32
33// ---------------------------------------------------------------------------
Mathias Agopian8683fca2012-08-12 19:37:16 -070034class Region : public LightFlattenable<Region>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035{
36public:
37 Region();
38 Region(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 explicit Region(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040 ~Region();
41
42 Region& operator = (const Region& rhs);
43
Mathias Agopian3ab68552012-08-31 14:31:40 -070044 inline bool isEmpty() const { return getBounds().isEmpty(); }
45 inline bool isRect() const { return mStorage.size() == 1; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046
Mathias Agopian3ab68552012-08-31 14:31:40 -070047 inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; }
Mathias Agopian17b2ad02009-06-26 19:02:40 -070048 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian9f961452009-06-29 18:46:37 -070050 // the region becomes its bounds
51 Region& makeBoundsSelf();
52
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053 void clear();
54 void set(const Rect& r);
Mathias Agopian0926f502009-05-04 14:17:04 -070055 void set(uint32_t w, uint32_t h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
57 Region& orSelf(const Rect& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080058 Region& xorSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 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);
Romain Guyb8a2e982012-02-07 17:04:34 -080064 Region& xorSelf(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 Region& andSelf(const Region& rhs);
66 Region& subtractSelf(const Region& rhs);
67
Mathias Agopian20f68782009-05-11 00:03:41 -070068 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070069 const Region merge(const Rect& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080070 const Region mergeExclusive(const Rect& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070071 const Region intersect(const Rect& rhs) const;
72 const Region subtract(const Rect& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
74 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070075 const Region merge(const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080076 const Region mergeExclusive(const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070077 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);
Romain Guyb8a2e982012-02-07 17:04:34 -080083 Region& xorSelf(const Region& rhs, int dx, int dy);
Mathias Agopian20f68782009-05-11 00:03:41 -070084 Region& andSelf(const Region& rhs, int dx, int dy);
85 Region& subtractSelf(const Region& rhs, int dx, int dy);
86
87 // these translate rhs first
Mathias Agopianbed9dd12009-05-27 17:01:58 -070088 const Region translate(int dx, int dy) const;
89 const Region merge(const Region& rhs, int dx, int dy) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080090 const Region mergeExclusive(const Region& rhs, int dx, int dy) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070091 const Region intersect(const Region& rhs, int dx, int dy) const;
92 const Region subtract(const Region& rhs, int dx, int dy) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093
94 // convenience operators overloads
Mathias Agopianbed9dd12009-05-27 17:01:58 -070095 inline const Region operator | (const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080096 inline const Region operator ^ (const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070097 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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100
101 inline Region& operator |= (const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -0800102 inline Region& operator ^= (const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 inline Region& operator &= (const Region& rhs);
104 inline Region& operator -= (const Region& rhs);
105 inline Region& operator += (const Point& pt);
106
Mathias Agopian20f68782009-05-11 00:03:41 -0700107
108 /* various ways to access the rectangle list */
109
110 typedef Rect const* const_iterator;
111
112 const_iterator begin() const;
113 const_iterator end() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114
Mathias Agopian20f68782009-05-11 00:03:41 -0700115 /* no user serviceable parts here... */
116
Mathias Agopian20f68782009-05-11 00:03:41 -0700117 Rect const* getArray(size_t* count) const;
118
119
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);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123
Mathias Agopian8683fca2012-08-12 19:37:16 -0700124 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);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128
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
Mathias Agopian3ab68552012-08-31 14:31:40 -0700158 // 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;
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}
Romain Guyb8a2e982012-02-07 17:04:34 -0800169const Region Region::operator ^ (const Region& rhs) const {
170 return mergeExclusive(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 intersect(rhs);
174}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700175const Region Region::operator - (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 return subtract(rhs);
177}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700178const Region Region::operator + (const Point& pt) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 return translate(pt.x, pt.y);
180}
181
182
183Region& Region::operator |= (const Region& rhs) {
184 return orSelf(rhs);
185}
Romain Guyb8a2e982012-02-07 17:04:34 -0800186Region& Region::operator ^= (const Region& rhs) {
187 return xorSelf(rhs);
188}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189Region& Region::operator &= (const Region& rhs) {
190 return andSelf(rhs);
191}
192Region& Region::operator -= (const Region& rhs) {
193 return subtractSelf(rhs);
194}
195Region& Region::operator += (const Point& pt) {
196 return translateSelf(pt.x, pt.y);
197}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198// ---------------------------------------------------------------------------
199}; // namespace android
200
201#endif // ANDROID_UI_REGION_H
202