blob: 0049fde6ed094ff4e93dc1c65be0e67159d88746 [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();
42
43 Region& operator = (const Region& rhs);
44
Mathias Agopian3ab68552012-08-31 14:31:40 -070045 inline bool isEmpty() const { return getBounds().isEmpty(); }
46 inline bool isRect() const { return mStorage.size() == 1; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047
Mathias Agopian3ab68552012-08-31 14:31:40 -070048 inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; }
Mathias Agopian17b2ad02009-06-26 19:02:40 -070049 inline Rect bounds() const { return getBounds(); }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050
Mathias Agopian9f961452009-06-29 18:46:37 -070051 // the region becomes its bounds
52 Region& makeBoundsSelf();
53
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 void clear();
55 void set(const Rect& r);
Mathias Agopian0926f502009-05-04 14:17:04 -070056 void set(uint32_t w, uint32_t h);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
58 Region& orSelf(const Rect& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080059 Region& xorSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 Region& andSelf(const Rect& rhs);
Mathias Agopian20f68782009-05-11 00:03:41 -070061 Region& subtractSelf(const Rect& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062
63 // boolean operators, applied on this
64 Region& orSelf(const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -080065 Region& xorSelf(const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 Region& andSelf(const Region& rhs);
67 Region& subtractSelf(const Region& rhs);
68
Mathias Agopian20f68782009-05-11 00:03:41 -070069 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070070 const Region merge(const Rect& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080071 const Region mergeExclusive(const Rect& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070072 const Region intersect(const Rect& rhs) const;
73 const Region subtract(const Rect& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
75 // boolean operators
Mathias Agopianbed9dd12009-05-27 17:01:58 -070076 const Region merge(const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080077 const Region mergeExclusive(const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070078 const Region intersect(const Region& rhs) const;
79 const Region subtract(const Region& rhs) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080
81 // these translate rhs first
Mathias Agopian20f68782009-05-11 00:03:41 -070082 Region& translateSelf(int dx, int dy);
83 Region& orSelf(const Region& rhs, int dx, int dy);
Romain Guyb8a2e982012-02-07 17:04:34 -080084 Region& xorSelf(const Region& rhs, int dx, int dy);
Mathias Agopian20f68782009-05-11 00:03:41 -070085 Region& andSelf(const Region& rhs, int dx, int dy);
86 Region& subtractSelf(const Region& rhs, int dx, int dy);
87
88 // these translate rhs first
Mathias Agopianbed9dd12009-05-27 17:01:58 -070089 const Region translate(int dx, int dy) const;
90 const Region merge(const Region& rhs, int dx, int dy) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080091 const Region mergeExclusive(const Region& rhs, int dx, int dy) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070092 const Region intersect(const Region& rhs, int dx, int dy) const;
93 const Region subtract(const Region& rhs, int dx, int dy) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094
95 // convenience operators overloads
Mathias Agopianbed9dd12009-05-27 17:01:58 -070096 inline const Region operator | (const Region& rhs) const;
Romain Guyb8a2e982012-02-07 17:04:34 -080097 inline const Region operator ^ (const Region& rhs) const;
Mathias Agopianbed9dd12009-05-27 17:01:58 -070098 inline const Region operator & (const Region& rhs) const;
99 inline const Region operator - (const Region& rhs) const;
100 inline const Region operator + (const Point& pt) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101
102 inline Region& operator |= (const Region& rhs);
Romain Guyb8a2e982012-02-07 17:04:34 -0800103 inline Region& operator ^= (const Region& rhs);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 inline Region& operator &= (const Region& rhs);
105 inline Region& operator -= (const Region& rhs);
106 inline Region& operator += (const Point& pt);
107
Mathias Agopian20f68782009-05-11 00:03:41 -0700108
109 /* various ways to access the rectangle list */
Mathias Agopian2401ead2012-08-31 15:41:24 -0700110
Mathias Agopian20f68782009-05-11 00:03:41 -0700111
Mathias Agopian2401ead2012-08-31 15:41:24 -0700112 // STL-like iterators
Mathias Agopian20f68782009-05-11 00:03:41 -0700113 typedef Rect const* const_iterator;
Mathias Agopian2401ead2012-08-31 15:41:24 -0700114 const_iterator begin() const;
115 const_iterator end() const;
116
117 // returns an array of rect which has the same life-time has this
118 // Region object.
119 Rect const* getArray(size_t* count) const;
120
121 // returns a SharedBuffer as well as the number of rects.
122 // ownership is transfered to the caller.
123 // the caller must call SharedBuffer::release() to free the memory.
124 SharedBuffer const* getSharedBuffer(size_t* count) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125
Mathias Agopian20f68782009-05-11 00:03:41 -0700126 /* no user serviceable parts here... */
127
Mathias Agopian20f68782009-05-11 00:03:41 -0700128 // add a rectangle to the internal list. This rectangle must
129 // be sorted in Y and X and must not make the region invalid.
130 void addRectUnchecked(int l, int t, int r, int b);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131
Mathias Agopian8683fca2012-08-12 19:37:16 -0700132 inline bool isFixedSize() const { return false; }
133 size_t getSize() const;
134 status_t flatten(void* buffer) const;
135 status_t unflatten(void const* buffer, size_t size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136
137 void dump(String8& out, const char* what, uint32_t flags=0) const;
138 void dump(const char* what, uint32_t flags=0) const;
139
140private:
Mathias Agopian20f68782009-05-11 00:03:41 -0700141 class rasterizer;
142 friend class rasterizer;
143
144 Region& operationSelf(const Rect& r, int op);
145 Region& operationSelf(const Region& r, int op);
146 Region& operationSelf(const Region& r, int dx, int dy, int op);
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700147 const Region operation(const Rect& rhs, int op) const;
148 const Region operation(const Region& rhs, int op) const;
149 const Region operation(const Region& rhs, int dx, int dy, int op) const;
Mathias Agopian20f68782009-05-11 00:03:41 -0700150
151 static void boolean_operation(int op, Region& dst,
152 const Region& lhs, const Region& rhs, int dx, int dy);
153 static void boolean_operation(int op, Region& dst,
154 const Region& lhs, const Rect& rhs, int dx, int dy);
155
156 static void boolean_operation(int op, Region& dst,
157 const Region& lhs, const Region& rhs);
158 static void boolean_operation(int op, Region& dst,
159 const Region& lhs, const Rect& rhs);
160
161 static void translate(Region& reg, int dx, int dy);
162 static void translate(Region& dst, const Region& reg, int dx, int dy);
163
164 static bool validate(const Region& reg, const char* name);
165
Mathias Agopian3ab68552012-08-31 14:31:40 -0700166 // mStorage is a (manually) sorted array of Rects describing the region
167 // with an extra Rect as the last element which is set to the
168 // bounds of the region. However, if the region is
169 // a simple Rect then mStorage contains only that rect.
170 Vector<Rect> mStorage;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171};
172
173
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700174const Region Region::operator | (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 return merge(rhs);
176}
Romain Guyb8a2e982012-02-07 17:04:34 -0800177const Region Region::operator ^ (const Region& rhs) const {
178 return mergeExclusive(rhs);
179}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700180const Region Region::operator & (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 return intersect(rhs);
182}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700183const Region Region::operator - (const Region& rhs) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 return subtract(rhs);
185}
Mathias Agopianbed9dd12009-05-27 17:01:58 -0700186const Region Region::operator + (const Point& pt) const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187 return translate(pt.x, pt.y);
188}
189
190
191Region& Region::operator |= (const Region& rhs) {
192 return orSelf(rhs);
193}
Romain Guyb8a2e982012-02-07 17:04:34 -0800194Region& Region::operator ^= (const Region& rhs) {
195 return xorSelf(rhs);
196}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197Region& Region::operator &= (const Region& rhs) {
198 return andSelf(rhs);
199}
200Region& Region::operator -= (const Region& rhs) {
201 return subtractSelf(rhs);
202}
203Region& Region::operator += (const Point& pt) {
204 return translateSelf(pt.x, pt.y);
205}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206// ---------------------------------------------------------------------------
207}; // namespace android
208
209#endif // ANDROID_UI_REGION_H
210