blob: e9b3a0bad7ef87bf8e1b92991e7ae371e54657fc [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
Mathias Agopian2401ead2012-08-31 15:41:24 -070031class SharedBuffer;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032class String8;
33
34// ---------------------------------------------------------------------------
Mathias Agopian8683fca2012-08-12 19:37:16 -070035class Region : public LightFlattenable<Region>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036{
37public:
Dan Stoza5065a552015-03-17 16:23:42 -070038 static const Region INVALID_REGION;
39
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040 Region();
41 Region(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042 explicit Region(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043 ~Region();
Chris Craik3e010f32013-02-25 19:12:47 -080044
45 static Region createTJunctionFreeRegion(const Region& r);
46
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 Region& operator = (const Region& rhs);
48
Mathias Agopian3ab68552012-08-31 14:31:40 -070049 inline bool isEmpty() const { return getBounds().isEmpty(); }
50 inline bool isRect() const { return mStorage.size() == 1; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051
Mathias Agopian3ab68552012-08-31 14:31:40 -070052 inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; }
Mathias Agopian17b2ad02009-06-26 19:02:40 -070053 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Michael Wright1c284a92014-02-10 13:00:14 -080055 bool contains(const Point& point) const;
56 bool contains(int x, int y) const;
57
Mathias Agopian9f961452009-06-29 18:46:37 -070058 // the region becomes its bounds
59 Region& makeBoundsSelf();
Dan Stozad3182402014-11-17 12:03:59 -080060
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 void clear();
62 void set(const Rect& r);
Dan Stozad3182402014-11-17 12:03:59 -080063 void set(int32_t w, int32_t h);
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +010064 void set(uint32_t w, uint32_t h);
Dan Stozad3182402014-11-17 12:03:59 -080065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 Region& orSelf(const Rect& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080067 Region& xorSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 Region& andSelf(const Rect& rhs);
Mathias Agopian20f68782009-05-11 00:03:41 -070069 Region& subtractSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
71 // boolean operators, applied on this
72 Region& orSelf(const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080073 Region& xorSelf(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 Region& andSelf(const Region& rhs);
75 Region& subtractSelf(const Region& rhs);
76
Mathias Agopian20f68782009-05-11 00:03:41 -070077 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070078 const Region merge(const Rect& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080079 const Region mergeExclusive(const Rect& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070080 const Region intersect(const Rect& rhs) const;
81 const Region subtract(const Rect& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082
83 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070084 const Region merge(const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080085 const Region mergeExclusive(const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070086 const Region intersect(const Region& rhs) const;
87 const Region subtract(const Region& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
89 // these translate rhs first
Mathias Agopian20f68782009-05-11 00:03:41 -070090 Region& translateSelf(int dx, int dy);
91 Region& orSelf(const Region& rhs, int dx, int dy);
Romain Guyb8a2e982012-02-07 17:04:34 -080092 Region& xorSelf(const Region& rhs, int dx, int dy);
Mathias Agopian20f68782009-05-11 00:03:41 -070093 Region& andSelf(const Region& rhs, int dx, int dy);
94 Region& subtractSelf(const Region& rhs, int dx, int dy);
95
96 // these translate rhs first
Mathias Agopianbed9dd12009-05-27 17:01:58 -070097 const Region translate(int dx, int dy) const;
98 const Region merge(const Region& rhs, int dx, int dy) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080099 const Region mergeExclusive(const Region& rhs, int dx, int dy) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700100 const Region intersect(const Region& rhs, int dx, int dy) const;
101 const Region subtract(const Region& rhs, int dx, int dy) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102
103 // convenience operators overloads
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700104 inline const Region operator | (const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -0800105 inline const Region operator ^ (const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700106 inline const Region operator & (const Region& rhs) const;
107 inline const Region operator - (const Region& rhs) const;
108 inline const Region operator + (const Point& pt) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109
110 inline Region& operator |= (const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -0800111 inline Region& operator ^= (const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 inline Region& operator &= (const Region& rhs);
113 inline Region& operator -= (const Region& rhs);
114 inline Region& operator += (const Point& pt);
115
Dan Stozad3182402014-11-17 12:03:59 -0800116
Mathias Agopian2ca79392013-04-02 18:30:32 -0700117 // returns true if the regions share the same underlying storage
118 bool isTriviallyEqual(const Region& region) const;
119
120
Mathias Agopian20f68782009-05-11 00:03:41 -0700121 /* various ways to access the rectangle list */
Mathias Agopian2401ead2012-08-31 15:41:24 -0700122
Dan Stozad3182402014-11-17 12:03:59 -0800123
Mathias Agopian2401ead2012-08-31 15:41:24 -0700124 // STL-like iterators
Mathias Agopian20f68782009-05-11 00:03:41 -0700125 typedef Rect const* const_iterator;
Mathias Agopian2401ead2012-08-31 15:41:24 -0700126 const_iterator begin() const;
127 const_iterator end() const;
128
129 // returns an array of rect which has the same life-time has this
130 // Region object.
131 Rect const* getArray(size_t* count) const;
132
Mathias Agopian20f68782009-05-11 00:03:41 -0700133 /* no user serviceable parts here... */
Dan Stozad3182402014-11-17 12:03:59 -0800134
Mathias Agopian20f68782009-05-11 00:03:41 -0700135 // add a rectangle to the internal list. This rectangle must
136 // be sorted in Y and X and must not make the region invalid.
137 void addRectUnchecked(int l, int t, int r, int b);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138
Mathias Agopian8683fca2012-08-12 19:37:16 -0700139 inline bool isFixedSize() const { return false; }
Mathias Agopiane1424282013-07-29 21:24:40 -0700140 size_t getFlattenedSize() const;
141 status_t flatten(void* buffer, size_t size) const;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700142 status_t unflatten(void const* buffer, size_t size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143
144 void dump(String8& out, const char* what, uint32_t flags=0) const;
145 void dump(const char* what, uint32_t flags=0) const;
146
147private:
Mathias Agopian20f68782009-05-11 00:03:41 -0700148 class rasterizer;
149 friend class rasterizer;
Dan Stozad3182402014-11-17 12:03:59 -0800150
Mathias Agopian20f68782009-05-11 00:03:41 -0700151 Region& operationSelf(const Rect& r, int op);
152 Region& operationSelf(const Region& r, int op);
153 Region& operationSelf(const Region& r, int dx, int dy, int op);
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700154 const Region operation(const Rect& rhs, int op) const;
155 const Region operation(const Region& rhs, int op) const;
156 const Region operation(const Region& rhs, int dx, int dy, int op) const;
Mathias Agopian20f68782009-05-11 00:03:41 -0700157
158 static void boolean_operation(int op, Region& dst,
159 const Region& lhs, const Region& rhs, int dx, int dy);
160 static void boolean_operation(int op, Region& dst,
161 const Region& lhs, const Rect& rhs, int dx, int dy);
162
163 static void boolean_operation(int op, Region& dst,
164 const Region& lhs, const Region& rhs);
165 static void boolean_operation(int op, Region& dst,
166 const Region& lhs, const Rect& rhs);
167
168 static void translate(Region& reg, int dx, int dy);
169 static void translate(Region& dst, const Region& reg, int dx, int dy);
170
Mathias Agopian068d47f2012-09-11 18:56:23 -0700171 static bool validate(const Region& reg,
172 const char* name, bool silent = false);
Dan Stozad3182402014-11-17 12:03:59 -0800173
Mathias Agopian3ab68552012-08-31 14:31:40 -0700174 // mStorage is a (manually) sorted array of Rects describing the region
175 // with an extra Rect as the last element which is set to the
176 // bounds of the region. However, if the region is
177 // a simple Rect then mStorage contains only that rect.
178 Vector<Rect> mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179};
180
181
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700182const Region Region::operator | (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 return merge(rhs);
184}
Romain Guyb8a2e982012-02-07 17:04:34 -0800185const Region Region::operator ^ (const Region& rhs) const {
186 return mergeExclusive(rhs);
187}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700188const Region Region::operator & (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 return intersect(rhs);
190}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700191const Region Region::operator - (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 return subtract(rhs);
193}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700194const Region Region::operator + (const Point& pt) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 return translate(pt.x, pt.y);
196}
197
198
199Region& Region::operator |= (const Region& rhs) {
200 return orSelf(rhs);
201}
Romain Guyb8a2e982012-02-07 17:04:34 -0800202Region& Region::operator ^= (const Region& rhs) {
203 return xorSelf(rhs);
204}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205Region& Region::operator &= (const Region& rhs) {
206 return andSelf(rhs);
207}
208Region& Region::operator -= (const Region& rhs) {
209 return subtractSelf(rhs);
210}
211Region& Region::operator += (const Point& pt) {
212 return translateSelf(pt.x, pt.y);
213}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214// ---------------------------------------------------------------------------
215}; // namespace android
216
217#endif // ANDROID_UI_REGION_H
218