blob: 49740f7ed7f9c4a14d3cf98cccb2692f3de9c584 [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:
38 Region();
39 Region(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040 explicit Region(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041 ~Region();
Chris Craik3e010f32013-02-25 19:12:47 -080042
43 static Region createTJunctionFreeRegion(const Region& r);
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045 Region& operator = (const Region& rhs);
46
Mathias Agopian3ab68552012-08-31 14:31:40 -070047 inline bool isEmpty() const { return getBounds().isEmpty(); }
48 inline bool isRect() const { return mStorage.size() == 1; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049
Mathias Agopian3ab68552012-08-31 14:31:40 -070050 inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; }
Mathias Agopian17b2ad02009-06-26 19:02:40 -070051 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
Michael Wright1c284a92014-02-10 13:00:14 -080053 bool contains(const Point& point) const;
54 bool contains(int x, int y) const;
55
Mathias Agopian9f961452009-06-29 18:46:37 -070056 // the region becomes its bounds
57 Region& makeBoundsSelf();
Dan Stozad3182402014-11-17 12:03:59 -080058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 void clear();
60 void set(const Rect& r);
Dan Stozad3182402014-11-17 12:03:59 -080061 void set(int32_t w, int32_t h);
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +010062 void set(uint32_t w, uint32_t h);
Dan Stozad3182402014-11-17 12:03:59 -080063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 Region& orSelf(const Rect& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080065 Region& xorSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 Region& andSelf(const Rect& rhs);
Mathias Agopian20f68782009-05-11 00:03:41 -070067 Region& subtractSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
69 // boolean operators, applied on this
70 Region& orSelf(const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080071 Region& xorSelf(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 Region& andSelf(const Region& rhs);
73 Region& subtractSelf(const Region& rhs);
74
Mathias Agopian20f68782009-05-11 00:03:41 -070075 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070076 const Region merge(const Rect& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080077 const Region mergeExclusive(const Rect& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070078 const Region intersect(const Rect& rhs) const;
79 const Region subtract(const Rect& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
81 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070082 const Region merge(const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080083 const Region mergeExclusive(const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070084 const Region intersect(const Region& rhs) const;
85 const Region subtract(const Region& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086
87 // these translate rhs first
Mathias Agopian20f68782009-05-11 00:03:41 -070088 Region& translateSelf(int dx, int dy);
89 Region& orSelf(const Region& rhs, int dx, int dy);
Romain Guyb8a2e982012-02-07 17:04:34 -080090 Region& xorSelf(const Region& rhs, int dx, int dy);
Mathias Agopian20f68782009-05-11 00:03:41 -070091 Region& andSelf(const Region& rhs, int dx, int dy);
92 Region& subtractSelf(const Region& rhs, int dx, int dy);
93
94 // these translate rhs first
Mathias Agopianbed9dd12009-05-27 17:01:58 -070095 const Region translate(int dx, int dy) const;
96 const Region merge(const Region& rhs, int dx, int dy) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080097 const Region mergeExclusive(const Region& rhs, int dx, int dy) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070098 const Region intersect(const Region& rhs, int dx, int dy) const;
99 const Region subtract(const Region& rhs, int dx, int dy) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100
101 // convenience operators overloads
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700102 inline const Region operator | (const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -0800103 inline const Region operator ^ (const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700104 inline const Region operator & (const Region& rhs) const;
105 inline const Region operator - (const Region& rhs) const;
106 inline const Region operator + (const Point& pt) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107
108 inline Region& operator |= (const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -0800109 inline Region& operator ^= (const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 inline Region& operator &= (const Region& rhs);
111 inline Region& operator -= (const Region& rhs);
112 inline Region& operator += (const Point& pt);
113
Dan Stozad3182402014-11-17 12:03:59 -0800114
Mathias Agopian2ca79392013-04-02 18:30:32 -0700115 // returns true if the regions share the same underlying storage
116 bool isTriviallyEqual(const Region& region) const;
117
118
Mathias Agopian20f68782009-05-11 00:03:41 -0700119 /* various ways to access the rectangle list */
Mathias Agopian2401ead2012-08-31 15:41:24 -0700120
Dan Stozad3182402014-11-17 12:03:59 -0800121
Mathias Agopian2401ead2012-08-31 15:41:24 -0700122 // STL-like iterators
Mathias Agopian20f68782009-05-11 00:03:41 -0700123 typedef Rect const* const_iterator;
Mathias Agopian2401ead2012-08-31 15:41:24 -0700124 const_iterator begin() const;
125 const_iterator end() const;
126
127 // returns an array of rect which has the same life-time has this
128 // Region object.
129 Rect const* getArray(size_t* count) const;
130
131 // returns a SharedBuffer as well as the number of rects.
132 // ownership is transfered to the caller.
133 // the caller must call SharedBuffer::release() to free the memory.
134 SharedBuffer const* getSharedBuffer(size_t* count) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135
Mathias Agopian20f68782009-05-11 00:03:41 -0700136 /* no user serviceable parts here... */
Dan Stozad3182402014-11-17 12:03:59 -0800137
Mathias Agopian20f68782009-05-11 00:03:41 -0700138 // add a rectangle to the internal list. This rectangle must
139 // be sorted in Y and X and must not make the region invalid.
140 void addRectUnchecked(int l, int t, int r, int b);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141
Mathias Agopian8683fca2012-08-12 19:37:16 -0700142 inline bool isFixedSize() const { return false; }
Mathias Agopiane1424282013-07-29 21:24:40 -0700143 size_t getFlattenedSize() const;
144 status_t flatten(void* buffer, size_t size) const;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700145 status_t unflatten(void const* buffer, size_t size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146
147 void dump(String8& out, const char* what, uint32_t flags=0) const;
148 void dump(const char* what, uint32_t flags=0) const;
149
150private:
Mathias Agopian20f68782009-05-11 00:03:41 -0700151 class rasterizer;
152 friend class rasterizer;
Dan Stozad3182402014-11-17 12:03:59 -0800153
Mathias Agopian20f68782009-05-11 00:03:41 -0700154 Region& operationSelf(const Rect& r, int op);
155 Region& operationSelf(const Region& r, int op);
156 Region& operationSelf(const Region& r, int dx, int dy, int op);
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700157 const Region operation(const Rect& rhs, int op) const;
158 const Region operation(const Region& rhs, int op) const;
159 const Region operation(const Region& rhs, int dx, int dy, int op) const;
Mathias Agopian20f68782009-05-11 00:03:41 -0700160
161 static void boolean_operation(int op, Region& dst,
162 const Region& lhs, const Region& rhs, int dx, int dy);
163 static void boolean_operation(int op, Region& dst,
164 const Region& lhs, const Rect& rhs, int dx, int dy);
165
166 static void boolean_operation(int op, Region& dst,
167 const Region& lhs, const Region& rhs);
168 static void boolean_operation(int op, Region& dst,
169 const Region& lhs, const Rect& rhs);
170
171 static void translate(Region& reg, int dx, int dy);
172 static void translate(Region& dst, const Region& reg, int dx, int dy);
173
Mathias Agopian068d47f2012-09-11 18:56:23 -0700174 static bool validate(const Region& reg,
175 const char* name, bool silent = false);
Dan Stozad3182402014-11-17 12:03:59 -0800176
Mathias Agopian3ab68552012-08-31 14:31:40 -0700177 // mStorage is a (manually) sorted array of Rects describing the region
178 // with an extra Rect as the last element which is set to the
179 // bounds of the region. However, if the region is
180 // a simple Rect then mStorage contains only that rect.
181 Vector<Rect> mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182};
183
184
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700185const Region Region::operator | (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 return merge(rhs);
187}
Romain Guyb8a2e982012-02-07 17:04:34 -0800188const Region Region::operator ^ (const Region& rhs) const {
189 return mergeExclusive(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 intersect(rhs);
193}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700194const Region Region::operator - (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 return subtract(rhs);
196}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700197const Region Region::operator + (const Point& pt) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198 return translate(pt.x, pt.y);
199}
200
201
202Region& Region::operator |= (const Region& rhs) {
203 return orSelf(rhs);
204}
Romain Guyb8a2e982012-02-07 17:04:34 -0800205Region& Region::operator ^= (const Region& rhs) {
206 return xorSelf(rhs);
207}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208Region& Region::operator &= (const Region& rhs) {
209 return andSelf(rhs);
210}
211Region& Region::operator -= (const Region& rhs) {
212 return subtractSelf(rhs);
213}
214Region& Region::operator += (const Point& pt) {
215 return translateSelf(pt.x, pt.y);
216}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217// ---------------------------------------------------------------------------
218}; // namespace android
219
220#endif // ANDROID_UI_REGION_H
221