blob: 6c9a6203e793cf9a6aadf9452d2eb7f3dfb6f298 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080024
25#include <ui/Rect.h>
26
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027namespace android {
28// ---------------------------------------------------------------------------
29
30class String8;
31
32// ---------------------------------------------------------------------------
33class Region
34{
35public:
36 Region();
37 Region(const Region& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 explicit Region(const Rect& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 explicit Region(const void* buffer);
40 ~Region();
41
42 Region& operator = (const Region& rhs);
43
Mathias Agopian6158b1b2009-05-11 00:03:41 -070044 inline bool isEmpty() const { return mBounds.isEmpty(); }
45 inline bool isRect() const { return mStorage.isEmpty(); }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Mathias Agopian5a8d6f42009-06-26 19:02:40 -070047 inline Rect getBounds() const { return mBounds; }
48 inline Rect bounds() const { return getBounds(); }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Mathias Agopian276826b2009-06-29 18:46:37 -070050 // the region becomes its bounds
51 Region& makeBoundsSelf();
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 void clear();
54 void set(const Rect& r);
Mathias Agopiandff8e582009-05-04 14:17:04 -070055 void set(uint32_t w, uint32_t h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57 Region& orSelf(const Rect& rhs);
58 Region& andSelf(const Rect& rhs);
Mathias Agopian6158b1b2009-05-11 00:03:41 -070059 Region& subtractSelf(const Rect& rhs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 // boolean operators, applied on this
62 Region& orSelf(const Region& rhs);
63 Region& andSelf(const Region& rhs);
64 Region& subtractSelf(const Region& rhs);
65
Mathias Agopian6158b1b2009-05-11 00:03:41 -070066 // boolean operators
Mathias Agopian12c659a2009-05-27 17:01:58 -070067 const Region merge(const Rect& rhs) const;
68 const Region intersect(const Rect& rhs) const;
69 const Region subtract(const Rect& rhs) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
71 // boolean operators
Mathias Agopian12c659a2009-05-27 17:01:58 -070072 const Region merge(const Region& rhs) const;
73 const Region intersect(const Region& rhs) const;
74 const Region subtract(const Region& rhs) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075
76 // these translate rhs first
Mathias Agopian6158b1b2009-05-11 00:03:41 -070077 Region& translateSelf(int dx, int dy);
78 Region& orSelf(const Region& rhs, int dx, int dy);
79 Region& andSelf(const Region& rhs, int dx, int dy);
80 Region& subtractSelf(const Region& rhs, int dx, int dy);
81
82 // these translate rhs first
Mathias Agopian12c659a2009-05-27 17:01:58 -070083 const Region translate(int dx, int dy) const;
84 const Region merge(const Region& rhs, int dx, int dy) const;
85 const Region intersect(const Region& rhs, int dx, int dy) const;
86 const Region subtract(const Region& rhs, int dx, int dy) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88 // convenience operators overloads
Mathias Agopian12c659a2009-05-27 17:01:58 -070089 inline const Region operator | (const Region& rhs) const;
90 inline const Region operator & (const Region& rhs) const;
91 inline const Region operator - (const Region& rhs) const;
92 inline const Region operator + (const Point& pt) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 inline Region& operator |= (const Region& rhs);
95 inline Region& operator &= (const Region& rhs);
96 inline Region& operator -= (const Region& rhs);
97 inline Region& operator += (const Point& pt);
98
Mathias Agopian6158b1b2009-05-11 00:03:41 -070099
100 /* various ways to access the rectangle list */
101
102 typedef Rect const* const_iterator;
103
104 const_iterator begin() const;
105 const_iterator end() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700107 /* no user serviceable parts here... */
108
109 size_t getRects(Vector<Rect>& rectList) const;
110 Rect const* getArray(size_t* count) const;
111
112
113 // add a rectangle to the internal list. This rectangle must
114 // be sorted in Y and X and must not make the region invalid.
115 void addRectUnchecked(int l, int t, int r, int b);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 // flatten/unflatten a region to/from a raw buffer
118 ssize_t write(void* buffer, size_t size) const;
119 static ssize_t writeEmpty(void* buffer, size_t size);
120
121 ssize_t read(const void* buffer);
122 static bool isEmpty(void* buffer);
123
124 void dump(String8& out, const char* what, uint32_t flags=0) const;
125 void dump(const char* what, uint32_t flags=0) const;
126
127private:
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700128 class rasterizer;
129 friend class rasterizer;
130
131 Region& operationSelf(const Rect& r, int op);
132 Region& operationSelf(const Region& r, int op);
133 Region& operationSelf(const Region& r, int dx, int dy, int op);
Mathias Agopian12c659a2009-05-27 17:01:58 -0700134 const Region operation(const Rect& rhs, int op) const;
135 const Region operation(const Region& rhs, int op) const;
136 const Region operation(const Region& rhs, int dx, int dy, int op) const;
Mathias Agopian6158b1b2009-05-11 00:03:41 -0700137
138 static void boolean_operation(int op, Region& dst,
139 const Region& lhs, const Region& rhs, int dx, int dy);
140 static void boolean_operation(int op, Region& dst,
141 const Region& lhs, const Rect& rhs, int dx, int dy);
142
143 static void boolean_operation(int op, Region& dst,
144 const Region& lhs, const Region& rhs);
145 static void boolean_operation(int op, Region& dst,
146 const Region& lhs, const Rect& rhs);
147
148 static void translate(Region& reg, int dx, int dy);
149 static void translate(Region& dst, const Region& reg, int dx, int dy);
150
151 static bool validate(const Region& reg, const char* name);
152
153 Rect mBounds;
154 Vector<Rect> mStorage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155};
156
157
Mathias Agopian12c659a2009-05-27 17:01:58 -0700158const Region Region::operator | (const Region& rhs) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 return merge(rhs);
160}
Mathias Agopian12c659a2009-05-27 17:01:58 -0700161const Region Region::operator & (const Region& rhs) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 return intersect(rhs);
163}
Mathias Agopian12c659a2009-05-27 17:01:58 -0700164const Region Region::operator - (const Region& rhs) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 return subtract(rhs);
166}
Mathias Agopian12c659a2009-05-27 17:01:58 -0700167const Region Region::operator + (const Point& pt) const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 return translate(pt.x, pt.y);
169}
170
171
172Region& Region::operator |= (const Region& rhs) {
173 return orSelf(rhs);
174}
175Region& Region::operator &= (const Region& rhs) {
176 return andSelf(rhs);
177}
178Region& Region::operator -= (const Region& rhs) {
179 return subtractSelf(rhs);
180}
181Region& Region::operator += (const Point& pt) {
182 return translateSelf(pt.x, pt.y);
183}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184// ---------------------------------------------------------------------------
185}; // namespace android
186
187#endif // ANDROID_UI_REGION_H
188