blob: 2db3b10f137cc5104ee9d59285c85366fa147bf3 [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>
Lloyd Piqueea629282019-12-03 15:57:10 -080022#include <ostream>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023
24#include <utils/Vector.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025
26#include <ui/Rect.h>
Mathias Agopian8683fca2012-08-12 19:37:16 -070027#include <utils/Flattenable.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
Robert Carr6aae18a2018-08-15 11:52:09 -070029#include <android-base/macros.h>
30
Yiwei Zhang5434a782018-12-05 18:06:32 -080031#include <string>
32
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033namespace android {
34// ---------------------------------------------------------------------------
35
Mathias Agopian8683fca2012-08-12 19:37:16 -070036class Region : public LightFlattenable<Region>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037{
38public:
Dan Stoza5065a552015-03-17 16:23:42 -070039 static const Region INVALID_REGION;
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041 Region();
42 Region(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043 explicit Region(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044 ~Region();
Chris Craik3e010f32013-02-25 19:12:47 -080045
46 static Region createTJunctionFreeRegion(const Region& r);
47
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048 Region& operator = (const Region& rhs);
49
Mathias Agopian3ab68552012-08-31 14:31:40 -070050 inline bool isEmpty() const { return getBounds().isEmpty(); }
51 inline bool isRect() const { return mStorage.size() == 1; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052
Mathias Agopian3ab68552012-08-31 14:31:40 -070053 inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; }
Mathias Agopian17b2ad02009-06-26 19:02:40 -070054 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Michael Wright1c284a92014-02-10 13:00:14 -080056 bool contains(const Point& point) const;
57 bool contains(int x, int y) const;
58
Mathias Agopian9f961452009-06-29 18:46:37 -070059 // the region becomes its bounds
60 Region& makeBoundsSelf();
Dan Stozad3182402014-11-17 12:03:59 -080061
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062 void clear();
63 void set(const Rect& r);
Dan Stozad3182402014-11-17 12:03:59 -080064 void set(int32_t w, int32_t h);
Bernhard Rosenkraenzerfe4966d2014-12-22 21:15:08 +010065 void set(uint32_t w, uint32_t h);
Dan Stozad3182402014-11-17 12:03:59 -080066
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 Region& orSelf(const Rect& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080068 Region& xorSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 Region& andSelf(const Rect& rhs);
Mathias Agopian20f68782009-05-11 00:03:41 -070070 Region& subtractSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
72 // boolean operators, applied on this
73 Region& orSelf(const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080074 Region& xorSelf(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 Region& andSelf(const Region& rhs);
76 Region& subtractSelf(const Region& rhs);
77
Mathias Agopian20f68782009-05-11 00:03:41 -070078 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070079 const Region merge(const Rect& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080080 const Region mergeExclusive(const Rect& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070081 const Region intersect(const Rect& rhs) const;
82 const Region subtract(const Rect& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083
84 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070085 const Region merge(const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080086 const Region mergeExclusive(const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070087 const Region intersect(const Region& rhs) const;
88 const Region subtract(const Region& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089
90 // these translate rhs first
Mathias Agopian20f68782009-05-11 00:03:41 -070091 Region& translateSelf(int dx, int dy);
Riddle Hsu39d4aa52018-11-30 20:46:53 +080092 Region& scaleSelf(float sx, float sy);
Mathias Agopian20f68782009-05-11 00:03:41 -070093 Region& orSelf(const Region& rhs, int dx, int dy);
Romain Guyb8a2e982012-02-07 17:04:34 -080094 Region& xorSelf(const Region& rhs, int dx, int dy);
Mathias Agopian20f68782009-05-11 00:03:41 -070095 Region& andSelf(const Region& rhs, int dx, int dy);
96 Region& subtractSelf(const Region& rhs, int dx, int dy);
97
Robert Carre07e1032018-11-26 12:55:53 -080098
Mathias Agopian20f68782009-05-11 00:03:41 -070099 // these translate rhs first
Robert Carr6aae18a2018-08-15 11:52:09 -0700100 const Region translate(int dx, int dy) const WARN_UNUSED;
101 const Region merge(const Region& rhs, int dx, int dy) const WARN_UNUSED;
102 const Region mergeExclusive(const Region& rhs, int dx, int dy) const WARN_UNUSED;
103 const Region intersect(const Region& rhs, int dx, int dy) const WARN_UNUSED;
104 const Region subtract(const Region& rhs, int dx, int dy) const WARN_UNUSED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105
106 // convenience operators overloads
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700107 inline const Region operator | (const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -0800108 inline const Region operator ^ (const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700109 inline const Region operator & (const Region& rhs) const;
110 inline const Region operator - (const Region& rhs) const;
111 inline const Region operator + (const Point& pt) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112
113 inline Region& operator |= (const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -0800114 inline Region& operator ^= (const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 inline Region& operator &= (const Region& rhs);
116 inline Region& operator -= (const Region& rhs);
117 inline Region& operator += (const Point& pt);
118
Dan Stozad3182402014-11-17 12:03:59 -0800119
Mathias Agopian2ca79392013-04-02 18:30:32 -0700120 // returns true if the regions share the same underlying storage
121 bool isTriviallyEqual(const Region& region) const;
122
Lloyd Piqueea629282019-12-03 15:57:10 -0800123 // returns true if the regions consist of the same rectangle sequence
124 bool hasSameRects(const Region& region) const;
Mathias Agopian2ca79392013-04-02 18:30:32 -0700125
Mathias Agopian20f68782009-05-11 00:03:41 -0700126 /* various ways to access the rectangle list */
Mathias Agopian2401ead2012-08-31 15:41:24 -0700127
Dan Stozad3182402014-11-17 12:03:59 -0800128
Mathias Agopian2401ead2012-08-31 15:41:24 -0700129 // STL-like iterators
Mathias Agopian20f68782009-05-11 00:03:41 -0700130 typedef Rect const* const_iterator;
Mathias Agopian2401ead2012-08-31 15:41:24 -0700131 const_iterator begin() const;
132 const_iterator end() const;
133
134 // returns an array of rect which has the same life-time has this
135 // Region object.
136 Rect const* getArray(size_t* count) const;
137
Mathias Agopian20f68782009-05-11 00:03:41 -0700138 /* no user serviceable parts here... */
Dan Stozad3182402014-11-17 12:03:59 -0800139
Mathias Agopian20f68782009-05-11 00:03:41 -0700140 // add a rectangle to the internal list. This rectangle must
141 // be sorted in Y and X and must not make the region invalid.
142 void addRectUnchecked(int l, int t, int r, int b);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143
Mathias Agopian8683fca2012-08-12 19:37:16 -0700144 inline bool isFixedSize() const { return false; }
Mathias Agopiane1424282013-07-29 21:24:40 -0700145 size_t getFlattenedSize() const;
146 status_t flatten(void* buffer, size_t size) const;
Mathias Agopian8683fca2012-08-12 19:37:16 -0700147 status_t unflatten(void const* buffer, size_t size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148
Yiwei Zhang5434a782018-12-05 18:06:32 -0800149 void dump(std::string& out, const char* what, uint32_t flags=0) const;
150 void dump(const char* what, uint32_t flags=0) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151
152private:
Mathias Agopian20f68782009-05-11 00:03:41 -0700153 class rasterizer;
154 friend class rasterizer;
Dan Stozad3182402014-11-17 12:03:59 -0800155
Colin Cross8f279962016-09-26 13:08:16 -0700156 Region& operationSelf(const Rect& r, uint32_t op);
157 Region& operationSelf(const Region& r, uint32_t op);
158 Region& operationSelf(const Region& r, int dx, int dy, uint32_t op);
159 const Region operation(const Rect& rhs, uint32_t op) const;
160 const Region operation(const Region& rhs, uint32_t op) const;
161 const Region operation(const Region& rhs, int dx, int dy, uint32_t op) const;
Mathias Agopian20f68782009-05-11 00:03:41 -0700162
Colin Cross8f279962016-09-26 13:08:16 -0700163 static void boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700164 const Region& lhs, const Region& rhs, int dx, int dy);
Colin Cross8f279962016-09-26 13:08:16 -0700165 static void boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700166 const Region& lhs, const Rect& rhs, int dx, int dy);
167
Colin Cross8f279962016-09-26 13:08:16 -0700168 static void boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700169 const Region& lhs, const Region& rhs);
Colin Cross8f279962016-09-26 13:08:16 -0700170 static void boolean_operation(uint32_t op, Region& dst,
Mathias Agopian20f68782009-05-11 00:03:41 -0700171 const Region& lhs, const Rect& rhs);
172
173 static void translate(Region& reg, int dx, int dy);
174 static void translate(Region& dst, const Region& reg, int dx, int dy);
175
Mathias Agopian068d47f2012-09-11 18:56:23 -0700176 static bool validate(const Region& reg,
177 const char* name, bool silent = false);
Dan Stozad3182402014-11-17 12:03:59 -0800178
Mathias Agopian3ab68552012-08-31 14:31:40 -0700179 // mStorage is a (manually) sorted array of Rects describing the region
180 // with an extra Rect as the last element which is set to the
181 // bounds of the region. However, if the region is
182 // a simple Rect then mStorage contains only that rect.
183 Vector<Rect> mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184};
185
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 merge(rhs);
189}
Romain Guyb8a2e982012-02-07 17:04:34 -0800190const Region Region::operator ^ (const Region& rhs) const {
191 return mergeExclusive(rhs);
192}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700193const Region Region::operator & (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 return intersect(rhs);
195}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700196const Region Region::operator - (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 return subtract(rhs);
198}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700199const Region Region::operator + (const Point& pt) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 return translate(pt.x, pt.y);
201}
202
203
204Region& Region::operator |= (const Region& rhs) {
205 return orSelf(rhs);
206}
Romain Guyb8a2e982012-02-07 17:04:34 -0800207Region& Region::operator ^= (const Region& rhs) {
208 return xorSelf(rhs);
209}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210Region& Region::operator &= (const Region& rhs) {
211 return andSelf(rhs);
212}
213Region& Region::operator -= (const Region& rhs) {
214 return subtractSelf(rhs);
215}
216Region& Region::operator += (const Point& pt) {
217 return translateSelf(pt.x, pt.y);
218}
Lloyd Piqueea629282019-12-03 15:57:10 -0800219
220// Defining PrintTo helps with Google Tests.
221static inline void PrintTo(const Region& region, ::std::ostream* os) {
222 Region::const_iterator head = region.begin();
223 Region::const_iterator const tail = region.end();
224 bool first = true;
225 while (head != tail) {
226 *os << (first ? "Region(" : ", ");
227 PrintTo(*head, os);
228 head++;
229 first = false;
230 }
231 *os << ")";
232}
233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234// ---------------------------------------------------------------------------
235}; // namespace android
236
237#endif // ANDROID_UI_REGION_H
238