Region.h revision c5b2c0bf8007562536b822eb060fc54a01f8e08b
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#include <binder/Parcel.h>
25
26#include <ui/Rect.h>
27
28#include <hardware/copybit.h>
29
30#include <core/SkRegion.h>
31
32namespace android {
33// ---------------------------------------------------------------------------
34
35class String8;
36
37// ---------------------------------------------------------------------------
38class Region
39{
40public:
41                        Region();
42                        Region(const Region& rhs);
43    explicit            Region(const SkRegion& rhs);
44    explicit            Region(const Rect& rhs);
45    explicit            Region(const Parcel& parcel);
46    explicit            Region(const void* buffer);
47                        ~Region();
48
49        Region& operator = (const Region& rhs);
50
51    inline  bool        isEmpty() const     { return mRegion.isEmpty(); }
52    inline  bool        isRect() const      { return mRegion.isRect(); }
53
54            Rect        bounds() const;
55
56            const SkRegion& toSkRegion() const;
57
58            void        clear();
59            void        set(const Rect& r);
60
61            Region&     orSelf(const Rect& rhs);
62            Region&     andSelf(const Rect& rhs);
63
64            // boolean operators, applied on this
65            Region&     orSelf(const Region& rhs);
66            Region&     andSelf(const Region& rhs);
67            Region&     subtractSelf(const Region& rhs);
68
69            // these translate rhs first
70            Region&     translateSelf(int dx, int dy);
71            Region&     orSelf(const Region& rhs, int dx, int dy);
72            Region&     andSelf(const Region& rhs, int dx, int dy);
73            Region&     subtractSelf(const Region& rhs, int dx, int dy);
74
75            // boolean operators
76            Region      merge(const Region& rhs) const;
77            Region      intersect(const Region& rhs) const;
78            Region      subtract(const Region& rhs) const;
79
80            // these translate rhs first
81            Region      translate(int dx, int dy) const;
82            Region      merge(const Region& rhs, int dx, int dy) const;
83            Region      intersect(const Region& rhs, int dx, int dy) const;
84            Region      subtract(const Region& rhs, int dx, int dy) const;
85
86    // convenience operators overloads
87    inline  Region      operator | (const Region& rhs) const;
88    inline  Region      operator & (const Region& rhs) const;
89    inline  Region      operator - (const Region& rhs) const;
90    inline  Region      operator + (const Point& pt) const;
91
92    inline  Region&     operator |= (const Region& rhs);
93    inline  Region&     operator &= (const Region& rhs);
94    inline  Region&     operator -= (const Region& rhs);
95    inline  Region&     operator += (const Point& pt);
96
97    class iterator {
98        SkRegion::Iterator  mIt;
99    public:
100        iterator(const Region& r);
101        inline operator bool () const { return !done(); }
102        int iterate(Rect* rect);
103    private:
104        inline bool done() const {
105            return const_cast<SkRegion::Iterator&>(mIt).done();
106        }
107    };
108
109            size_t      rects(Vector<Rect>& rectList) const;
110
111            // flatten/unflatten a region to/from a Parcel
112            status_t    write(Parcel& parcel) const;
113            status_t    read(const Parcel& parcel);
114
115            // flatten/unflatten a region to/from a raw buffer
116            ssize_t     write(void* buffer, size_t size) const;
117    static  ssize_t     writeEmpty(void* buffer, size_t size);
118
119            ssize_t     read(const void* buffer);
120    static  bool        isEmpty(void* buffer);
121
122    void        dump(String8& out, const char* what, uint32_t flags=0) const;
123    void        dump(const char* what, uint32_t flags=0) const;
124
125private:
126    SkRegion    mRegion;
127};
128
129
130Region Region::operator | (const Region& rhs) const {
131    return merge(rhs);
132}
133Region Region::operator & (const Region& rhs) const {
134    return intersect(rhs);
135}
136Region Region::operator - (const Region& rhs) const {
137    return subtract(rhs);
138}
139Region Region::operator + (const Point& pt) const {
140    return translate(pt.x, pt.y);
141}
142
143
144Region& Region::operator |= (const Region& rhs) {
145    return orSelf(rhs);
146}
147Region& Region::operator &= (const Region& rhs) {
148    return andSelf(rhs);
149}
150Region& Region::operator -= (const Region& rhs) {
151    return subtractSelf(rhs);
152}
153Region& Region::operator += (const Point& pt) {
154    return translateSelf(pt.x, pt.y);
155}
156
157// ---------------------------------------------------------------------------
158
159struct region_iterator : public copybit_region_t {
160    region_iterator(const Region& region) : i(region) {
161        this->next = iterate;
162    }
163private:
164    static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
165        return static_cast<const region_iterator*>(self)
166        ->i.iterate(reinterpret_cast<Rect*>(rect));
167    }
168    mutable Region::iterator i;
169};
170// ---------------------------------------------------------------------------
171}; // namespace android
172
173#endif // ANDROID_UI_REGION_H
174
175