Region.h revision b8a2e98cd7edbe7513543670c94f6b5efa74462f
1/*
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>
24
25#include <ui/Rect.h>
26
27namespace android {
28// ---------------------------------------------------------------------------
29
30class String8;
31
32// ---------------------------------------------------------------------------
33class Region
34{
35public:
36                        Region();
37                        Region(const Region& rhs);
38    explicit            Region(const Rect& rhs);
39    explicit            Region(const void* buffer);
40                        ~Region();
41
42        Region& operator = (const Region& rhs);
43
44    inline  bool        isEmpty() const     { return mBounds.isEmpty();  }
45    inline  bool        isRect() const      { return mStorage.isEmpty(); }
46
47    inline  Rect        getBounds() const   { return mBounds; }
48    inline  Rect        bounds() const      { return getBounds(); }
49
50            // the region becomes its bounds
51            Region&     makeBoundsSelf();
52
53            void        clear();
54            void        set(const Rect& r);
55            void        set(uint32_t w, uint32_t h);
56
57            Region&     orSelf(const Rect& rhs);
58            Region&     xorSelf(const Rect& rhs);
59            Region&     andSelf(const Rect& rhs);
60            Region&     subtractSelf(const Rect& rhs);
61
62            // boolean operators, applied on this
63            Region&     orSelf(const Region& rhs);
64            Region&     xorSelf(const Region& rhs);
65            Region&     andSelf(const Region& rhs);
66            Region&     subtractSelf(const Region& rhs);
67
68            // boolean operators
69    const   Region      merge(const Rect& rhs) const;
70    const   Region      mergeExclusive(const Rect& rhs) const;
71    const   Region      intersect(const Rect& rhs) const;
72    const   Region      subtract(const Rect& rhs) const;
73
74            // boolean operators
75    const   Region      merge(const Region& rhs) const;
76    const   Region      mergeExclusive(const Region& rhs) const;
77    const   Region      intersect(const Region& rhs) const;
78    const   Region      subtract(const Region& rhs) const;
79
80            // these translate rhs first
81            Region&     translateSelf(int dx, int dy);
82            Region&     orSelf(const Region& rhs, int dx, int dy);
83            Region&     xorSelf(const Region& rhs, int dx, int dy);
84            Region&     andSelf(const Region& rhs, int dx, int dy);
85            Region&     subtractSelf(const Region& rhs, int dx, int dy);
86
87            // these translate rhs first
88    const   Region      translate(int dx, int dy) const;
89    const   Region      merge(const Region& rhs, int dx, int dy) const;
90    const   Region      mergeExclusive(const Region& rhs, int dx, int dy) const;
91    const   Region      intersect(const Region& rhs, int dx, int dy) const;
92    const   Region      subtract(const Region& rhs, int dx, int dy) const;
93
94    // convenience operators overloads
95    inline  const Region      operator | (const Region& rhs) const;
96    inline  const Region      operator ^ (const Region& rhs) const;
97    inline  const Region      operator & (const Region& rhs) const;
98    inline  const Region      operator - (const Region& rhs) const;
99    inline  const Region      operator + (const Point& pt) const;
100
101    inline  Region&     operator |= (const Region& rhs);
102    inline  Region&     operator ^= (const Region& rhs);
103    inline  Region&     operator &= (const Region& rhs);
104    inline  Region&     operator -= (const Region& rhs);
105    inline  Region&     operator += (const Point& pt);
106
107
108    /* various ways to access the rectangle list */
109
110    typedef Rect const* const_iterator;
111
112            const_iterator begin() const;
113            const_iterator end() const;
114
115    /* no user serviceable parts here... */
116
117            size_t      getRects(Vector<Rect>& rectList) const;
118            Rect const* getArray(size_t* count) const;
119
120
121            // add a rectangle to the internal list. This rectangle must
122            // be sorted in Y and X and must not make the region invalid.
123            void        addRectUnchecked(int l, int t, int r, int b);
124
125            // flatten/unflatten a region to/from a raw buffer
126            ssize_t     write(void* buffer, size_t size) const;
127    static  ssize_t     writeEmpty(void* buffer, size_t size);
128
129            ssize_t     read(const void* buffer);
130    static  bool        isEmpty(void* buffer);
131
132    void        dump(String8& out, const char* what, uint32_t flags=0) const;
133    void        dump(const char* what, uint32_t flags=0) const;
134
135private:
136    class rasterizer;
137    friend class rasterizer;
138
139    Region& operationSelf(const Rect& r, int op);
140    Region& operationSelf(const Region& r, int op);
141    Region& operationSelf(const Region& r, int dx, int dy, int op);
142    const Region operation(const Rect& rhs, int op) const;
143    const Region operation(const Region& rhs, int op) const;
144    const Region operation(const Region& rhs, int dx, int dy, int op) const;
145
146    static void boolean_operation(int op, Region& dst,
147            const Region& lhs, const Region& rhs, int dx, int dy);
148    static void boolean_operation(int op, Region& dst,
149            const Region& lhs, const Rect& rhs, int dx, int dy);
150
151    static void boolean_operation(int op, Region& dst,
152            const Region& lhs, const Region& rhs);
153    static void boolean_operation(int op, Region& dst,
154            const Region& lhs, const Rect& rhs);
155
156    static void translate(Region& reg, int dx, int dy);
157    static void translate(Region& dst, const Region& reg, int dx, int dy);
158
159    static bool validate(const Region& reg, const char* name);
160
161    Rect            mBounds;
162    Vector<Rect>    mStorage;
163};
164
165
166const Region Region::operator | (const Region& rhs) const {
167    return merge(rhs);
168}
169const Region Region::operator ^ (const Region& rhs) const {
170    return mergeExclusive(rhs);
171}
172const Region Region::operator & (const Region& rhs) const {
173    return intersect(rhs);
174}
175const Region Region::operator - (const Region& rhs) const {
176    return subtract(rhs);
177}
178const Region Region::operator + (const Point& pt) const {
179    return translate(pt.x, pt.y);
180}
181
182
183Region& Region::operator |= (const Region& rhs) {
184    return orSelf(rhs);
185}
186Region& Region::operator ^= (const Region& rhs) {
187    return xorSelf(rhs);
188}
189Region& Region::operator &= (const Region& rhs) {
190    return andSelf(rhs);
191}
192Region& Region::operator -= (const Region& rhs) {
193    return subtractSelf(rhs);
194}
195Region& Region::operator += (const Point& pt) {
196    return translateSelf(pt.x, pt.y);
197}
198// ---------------------------------------------------------------------------
199}; // namespace android
200
201#endif // ANDROID_UI_REGION_H
202
203