blob: 778845295f90264e5c15d1565c6fcfb0334f075c [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:
Dan Stoza5065a552015-03-17 16:23:42 -070037 static const Region INVALID_REGION;
38
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039 Region();
40 Region(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041 explicit Region(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042 ~Region();
Chris Craik3e010f32013-02-25 19:12:47 -080043
44 static Region createTJunctionFreeRegion(const Region& r);
45
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080046 Region& operator = (const Region& rhs);
47
Mathias Agopian3ab68552012-08-31 14:31:40 -070048 inline bool isEmpty() const { return getBounds().isEmpty(); }
49 inline bool isRect() const { return mStorage.size() == 1; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Mathias Agopian3ab68552012-08-31 14:31:40 -070051 inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; }
Mathias Agopian17b2ad02009-06-26 19:02:40 -070052 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Michael Wright1c284a92014-02-10 13:00:14 -080054 bool contains(const Point& point) const;
55 bool contains(int x, int y) const;
56
Mathias Agopian9f961452009-06-29 18:46:37 -070057 // the region becomes its bounds
58 Region& makeBoundsSelf();
Dan Stozad3182402014-11-17 12:03:59 -080059
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 void clear();
61 void set(const Rect& r);
Dan Stozad3182402014-11-17 12:03:59 -080062 void set(int32_t w, int32_t h);
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +010063 void set(uint32_t w, uint32_t h);
Dan Stozad3182402014-11-17 12:03:59 -080064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 Region& orSelf(const Rect& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080066 Region& xorSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 Region& andSelf(const Rect& rhs);
Mathias Agopian20f68782009-05-11 00:03:41 -070068 Region& subtractSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
70 // boolean operators, applied on this
71 Region& orSelf(const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080072 Region& xorSelf(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 Region& andSelf(const Region& rhs);
74 Region& subtractSelf(const Region& rhs);
75
Mathias Agopian20f68782009-05-11 00:03:41 -070076 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070077 const Region merge(const Rect& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080078 const Region mergeExclusive(const Rect& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070079 const Region intersect(const Rect& rhs) const;
80 const Region subtract(const Rect& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081
82 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070083 const Region merge(const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080084 const Region mergeExclusive(const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070085 const Region intersect(const Region& rhs) const;
86 const Region subtract(const Region& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087
88 // these translate rhs first
Mathias Agopian20f68782009-05-11 00:03:41 -070089 Region& translateSelf(int dx, int dy);
90 Region& orSelf(const Region& rhs, int dx, int dy);
Romain Guyb8a2e982012-02-07 17:04:34 -080091 Region& xorSelf(const Region& rhs, int dx, int dy);
Mathias Agopian20f68782009-05-11 00:03:41 -070092 Region& andSelf(const Region& rhs, int dx, int dy);
93 Region& subtractSelf(const Region& rhs, int dx, int dy);
94
95 // these translate rhs first
Mathias Agopianbed9dd12009-05-27 17:01:58 -070096 const Region translate(int dx, int dy) const;
97 const Region merge(const Region& rhs, int dx, int dy) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080098 const Region mergeExclusive(const Region& rhs, int dx, int dy) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070099 const Region intersect(const Region& rhs, int dx, int dy) const;
100 const Region subtract(const Region& rhs, int dx, int dy) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 // convenience operators overloads
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700103 inline const Region operator | (const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -0800104 inline const Region operator ^ (const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700105 inline const Region operator & (const Region& rhs) const;
106 inline const Region operator - (const Region& rhs) const;
107 inline const Region operator + (const Point& pt) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108
109 inline Region& operator |= (const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -0800110 inline Region& operator ^= (const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 inline Region& operator &= (const Region& rhs);
112 inline Region& operator -= (const Region& rhs);
113 inline Region& operator += (const Point& pt);
114
Dan Stozad3182402014-11-17 12:03:59 -0800115
Mathias Agopian2ca79392013-04-02 18:30:32 -0700116 // returns true if the regions share the same underlying storage
117 bool isTriviallyEqual(const Region& region) const;
118
119
Mathias Agopian20f68782009-05-11 00:03:41 -0700120 /* various ways to access the rectangle list */
Mathias Agopian2401ead2012-08-31 15:41:24 -0700121
Dan Stozad3182402014-11-17 12:03:59 -0800122
Mathias Agopian2401ead2012-08-31 15:41:24 -0700123 // STL-like iterators
Mathias Agopian20f68782009-05-11 00:03:41 -0700124 typedef Rect const* const_iterator;
Mathias Agopian2401ead2012-08-31 15:41:24 -0700125 const_iterator begin() const;
126 const_iterator end() const;
127
128 // returns an array of rect which has the same life-time has this
129 // Region object.
130 Rect const* getArray(size_t* count) const;
131
Mathias Agopian20f68782009-05-11 00:03:41 -0700132 /* no user serviceable parts here... */
Dan Stozad3182402014-11-17 12:03:59 -0800133
Mathias Agopian20f68782009-05-11 00:03:41 -0700134 // add a rectangle to the internal list. This rectangle must
135 // be sorted in Y and X and must not make the region invalid.
136 void addRectUnchecked(int l, int t, int r, int b);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137
Mathias Agopian8683fca2012-08-12 19:37:16 -0700138 inline bool isFixedSize() const { return false; }
Mathias Agopiane1424282013-07-29 21:24:40 -0700139 size_t getFlattenedSize() const;
140 status_t flatten(void* buffer, size_t size) const;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700141 status_t unflatten(void const* buffer, size_t size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142
143 void dump(String8& out, const char* what, uint32_t flags=0) const;
144 void dump(const char* what, uint32_t flags=0) const;
145
146private:
Mathias Agopian20f68782009-05-11 00:03:41 -0700147 class rasterizer;
148 friend class rasterizer;
Dan Stozad3182402014-11-17 12:03:59 -0800149
Colin Cross8f279962016-09-26 13:08:16 -0700150 Region& operationSelf(const Rect& r, uint32_t op);
151 Region& operationSelf(const Region& r, uint32_t op);
152 Region& operationSelf(const Region& r, int dx, int dy, uint32_t op);
153 const Region operation(const Rect& rhs, uint32_t op) const;
154 const Region operation(const Region& rhs, uint32_t op) const;
155 const Region operation(const Region& rhs, int dx, int dy, uint32_t op) const;
Mathias Agopian20f68782009-05-11 00:03:41 -0700156
Colin Cross8f279962016-09-26 13:08:16 -0700157 static void boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700158 const Region& lhs, const Region& rhs, int dx, int dy);
Colin Cross8f279962016-09-26 13:08:16 -0700159 static void boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700160 const Region& lhs, const Rect& rhs, int dx, int dy);
161
Colin Cross8f279962016-09-26 13:08:16 -0700162 static void boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700163 const Region& lhs, const Region& rhs);
Colin Cross8f279962016-09-26 13:08:16 -0700164 static void boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700165 const Region& lhs, const Rect& rhs);
166
167 static void translate(Region& reg, int dx, int dy);
168 static void translate(Region& dst, const Region& reg, int dx, int dy);
169
Mathias Agopian068d47f2012-09-11 18:56:23 -0700170 static bool validate(const Region& reg,
171 const char* name, bool silent = false);
Dan Stozad3182402014-11-17 12:03:59 -0800172
Mathias Agopian3ab68552012-08-31 14:31:40 -0700173 // mStorage is a (manually) sorted array of Rects describing the region
174 // with an extra Rect as the last element which is set to the
175 // bounds of the region. However, if the region is
176 // a simple Rect then mStorage contains only that rect.
177 Vector<Rect> mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178};
179
180
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700181const Region Region::operator | (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 return merge(rhs);
183}
Romain Guyb8a2e982012-02-07 17:04:34 -0800184const Region Region::operator ^ (const Region& rhs) const {
185 return mergeExclusive(rhs);
186}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700187const Region Region::operator & (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 return intersect(rhs);
189}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700190const Region Region::operator - (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 return subtract(rhs);
192}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700193const Region Region::operator + (const Point& pt) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 return translate(pt.x, pt.y);
195}
196
197
198Region& Region::operator |= (const Region& rhs) {
199 return orSelf(rhs);
200}
Romain Guyb8a2e982012-02-07 17:04:34 -0800201Region& Region::operator ^= (const Region& rhs) {
202 return xorSelf(rhs);
203}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204Region& Region::operator &= (const Region& rhs) {
205 return andSelf(rhs);
206}
207Region& Region::operator -= (const Region& rhs) {
208 return subtractSelf(rhs);
209}
210Region& Region::operator += (const Point& pt) {
211 return translateSelf(pt.x, pt.y);
212}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213// ---------------------------------------------------------------------------
214}; // namespace android
215
216#endif // ANDROID_UI_REGION_H
217