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