Region.h revision 2ca79399b933935eb1b6c0ec1f746f8c4475369c
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#include <utils/Flattenable.h>
27
28namespace android {
29// ---------------------------------------------------------------------------
30
31class SharedBuffer;
32class String8;
33
34// ---------------------------------------------------------------------------
35class Region : public LightFlattenable<Region>
36{
37public:
38                        Region();
39                        Region(const Region& rhs);
40    explicit            Region(const Rect& rhs);
41                        ~Region();
42
43    static  Region      createTJunctionFreeRegion(const Region& r);
44
45        Region& operator = (const Region& rhs);
46
47    inline  bool        isEmpty() const     { return getBounds().isEmpty(); }
48    inline  bool        isRect() const      { return mStorage.size() == 1; }
49
50    inline  Rect        getBounds() const   { return mStorage[mStorage.size() - 1]; }
51    inline  Rect        bounds() const      { return getBounds(); }
52
53            // the region becomes its bounds
54            Region&     makeBoundsSelf();
55
56            void        clear();
57            void        set(const Rect& r);
58            void        set(uint32_t w, uint32_t h);
59
60            Region&     orSelf(const Rect& rhs);
61            Region&     xorSelf(const Rect& rhs);
62            Region&     andSelf(const Rect& rhs);
63            Region&     subtractSelf(const Rect& rhs);
64
65            // boolean operators, applied on this
66            Region&     orSelf(const Region& rhs);
67            Region&     xorSelf(const Region& rhs);
68            Region&     andSelf(const Region& rhs);
69            Region&     subtractSelf(const Region& rhs);
70
71            // boolean operators
72    const   Region      merge(const Rect& rhs) const;
73    const   Region      mergeExclusive(const Rect& rhs) const;
74    const   Region      intersect(const Rect& rhs) const;
75    const   Region      subtract(const Rect& rhs) const;
76
77            // boolean operators
78    const   Region      merge(const Region& rhs) const;
79    const   Region      mergeExclusive(const Region& rhs) const;
80    const   Region      intersect(const Region& rhs) const;
81    const   Region      subtract(const Region& rhs) const;
82
83            // these translate rhs first
84            Region&     translateSelf(int dx, int dy);
85            Region&     orSelf(const Region& rhs, int dx, int dy);
86            Region&     xorSelf(const Region& rhs, int dx, int dy);
87            Region&     andSelf(const Region& rhs, int dx, int dy);
88            Region&     subtractSelf(const Region& rhs, int dx, int dy);
89
90            // these translate rhs first
91    const   Region      translate(int dx, int dy) const;
92    const   Region      merge(const Region& rhs, int dx, int dy) const;
93    const   Region      mergeExclusive(const Region& rhs, int dx, int dy) const;
94    const   Region      intersect(const Region& rhs, int dx, int dy) const;
95    const   Region      subtract(const Region& rhs, int dx, int dy) const;
96
97    // convenience operators overloads
98    inline  const Region      operator | (const Region& rhs) const;
99    inline  const Region      operator ^ (const Region& rhs) const;
100    inline  const Region      operator & (const Region& rhs) const;
101    inline  const Region      operator - (const Region& rhs) const;
102    inline  const Region      operator + (const Point& pt) const;
103
104    inline  Region&     operator |= (const Region& rhs);
105    inline  Region&     operator ^= (const Region& rhs);
106    inline  Region&     operator &= (const Region& rhs);
107    inline  Region&     operator -= (const Region& rhs);
108    inline  Region&     operator += (const Point& pt);
109
110
111    // returns true if the regions share the same underlying storage
112    bool isTriviallyEqual(const Region& region) const;
113
114
115    /* various ways to access the rectangle list */
116
117
118    // STL-like iterators
119    typedef Rect const* const_iterator;
120    const_iterator begin() const;
121    const_iterator end() const;
122
123    // returns an array of rect which has the same life-time has this
124    // Region object.
125    Rect const* getArray(size_t* count) const;
126
127    // returns a SharedBuffer as well as the number of rects.
128    // ownership is transfered to the caller.
129    // the caller must call SharedBuffer::release() to free the memory.
130    SharedBuffer const* getSharedBuffer(size_t* count) const;
131
132    /* no user serviceable parts here... */
133
134            // 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);
137
138    inline  bool        isFixedSize() const { return false; }
139            size_t      getSize() const;
140            status_t    flatten(void* buffer) const;
141            status_t    unflatten(void const* buffer, size_t size);
142
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:
147    class rasterizer;
148    friend class rasterizer;
149
150    Region& operationSelf(const Rect& r, int op);
151    Region& operationSelf(const Region& r, int op);
152    Region& operationSelf(const Region& r, int dx, int dy, int op);
153    const Region operation(const Rect& rhs, int op) const;
154    const Region operation(const Region& rhs, int op) const;
155    const Region operation(const Region& rhs, int dx, int dy, int op) const;
156
157    static void boolean_operation(int op, Region& dst,
158            const Region& lhs, const Region& rhs, int dx, int dy);
159    static void boolean_operation(int op, Region& dst,
160            const Region& lhs, const Rect& rhs, int dx, int dy);
161
162    static void boolean_operation(int op, Region& dst,
163            const Region& lhs, const Region& rhs);
164    static void boolean_operation(int op, Region& dst,
165            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
170    static bool validate(const Region& reg,
171            const char* name, bool silent = false);
172
173    // 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;
178};
179
180
181const Region Region::operator | (const Region& rhs) const {
182    return merge(rhs);
183}
184const Region Region::operator ^ (const Region& rhs) const {
185    return mergeExclusive(rhs);
186}
187const Region Region::operator & (const Region& rhs) const {
188    return intersect(rhs);
189}
190const Region Region::operator - (const Region& rhs) const {
191    return subtract(rhs);
192}
193const Region Region::operator + (const Point& pt) const {
194    return translate(pt.x, pt.y);
195}
196
197
198Region& Region::operator |= (const Region& rhs) {
199    return orSelf(rhs);
200}
201Region& Region::operator ^= (const Region& rhs) {
202    return xorSelf(rhs);
203}
204Region& 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}
213// ---------------------------------------------------------------------------
214}; // namespace android
215
216#endif // ANDROID_UI_REGION_H
217
218