summaryrefslogtreecommitdiffstats
path: root/include/ui/Region.h
blob: 76896737f293df4403fde4a1d4e5d8de0016e271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_UI_REGION_H
#define ANDROID_UI_REGION_H

#include <stdint.h>
#include <sys/types.h>

#include <utils/Vector.h>
#include <utils/Parcel.h>

#include <ui/Rect.h>

#include <hardware/copybit.h>

#include <core/SkRegion.h>

namespace android {
// ---------------------------------------------------------------------------

class String8;

// ---------------------------------------------------------------------------
class Region
{
public:
                        Region();
                        Region(const Region& rhs);
    explicit            Region(const SkRegion& rhs);
    explicit            Region(const Rect& rhs);
    explicit            Region(const Parcel& parcel);
    explicit            Region(const void* buffer);
                        ~Region();
                        
        Region& operator = (const Region& rhs);

    inline  bool        isEmpty() const     { return mRegion.isEmpty(); }
    inline  bool        isRect() const      { return mRegion.isRect(); }

            Rect        bounds() const;

            const SkRegion& toSkRegion() const;

            void        clear();
            void        set(const Rect& r);
        
            Region&     orSelf(const Rect& rhs);
            Region&     andSelf(const Rect& rhs);

            // boolean operators, applied on this
            Region&     orSelf(const Region& rhs);
            Region&     andSelf(const Region& rhs);
            Region&     subtractSelf(const Region& rhs);

            // these translate rhs first
            Region&     translateSelf(int dx, int dy);
            Region&     orSelf(const Region& rhs, int dx, int dy);
            Region&     andSelf(const Region& rhs, int dx, int dy);
            Region&     subtractSelf(const Region& rhs, int dx, int dy);

            // boolean operators
            Region      merge(const Region& rhs) const;
            Region      intersect(const Region& rhs) const;
            Region      subtract(const Region& rhs) const;

            // these translate rhs first
            Region      translate(int dx, int dy) const;
            Region      merge(const Region& rhs, int dx, int dy) const;
            Region      intersect(const Region& rhs, int dx, int dy) const;
            Region      subtract(const Region& rhs, int dx, int dy) const;

    // convenience operators overloads
    inline  Region      operator | (const Region& rhs) const;
    inline  Region      operator & (const Region& rhs) const;
    inline  Region      operator - (const Region& rhs) const;
    inline  Region      operator + (const Point& pt) const;

    inline  Region&     operator |= (const Region& rhs);
    inline  Region&     operator &= (const Region& rhs);
    inline  Region&     operator -= (const Region& rhs);
    inline  Region&     operator += (const Point& pt);

    class iterator {
        SkRegion::Iterator  mIt;
    public:
        iterator(const Region& r);
        inline operator bool () const { return !done(); }
        int iterate(Rect* rect);
    private:
        inline bool done() const {
            return const_cast<SkRegion::Iterator&>(mIt).done();
        }
    };

            size_t      rects(Vector<Rect>& rectList) const;

            // flatten/unflatten a region to/from a Parcel
            status_t    write(Parcel& parcel) const;
            status_t    read(const Parcel& parcel);

            // flatten/unflatten a region to/from a raw buffer
            ssize_t     write(void* buffer, size_t size) const;
    static  ssize_t     writeEmpty(void* buffer, size_t size);

            ssize_t     read(const void* buffer);
    static  bool        isEmpty(void* buffer);

    void        dump(String8& out, const char* what, uint32_t flags=0) const;
    void        dump(const char* what, uint32_t flags=0) const;

private:
    SkRegion    mRegion;
};


Region Region::operator | (const Region& rhs) const {
    return merge(rhs);
}
Region Region::operator & (const Region& rhs) const {
    return intersect(rhs);
}
Region Region::operator - (const Region& rhs) const {
    return subtract(rhs);
}
Region Region::operator + (const Point& pt) const {
    return translate(pt.x, pt.y);
}


Region& Region::operator |= (const Region& rhs) {
    return orSelf(rhs);
}
Region& Region::operator &= (const Region& rhs) {
    return andSelf(rhs);
}
Region& Region::operator -= (const Region& rhs) {
    return subtractSelf(rhs);
}
Region& Region::operator += (const Point& pt) {
    return translateSelf(pt.x, pt.y);
}

// ---------------------------------------------------------------------------

struct region_iterator : public copybit_region_t {
    region_iterator(const Region& region) : i(region) {
        this->next = iterate;
    }
private:
    static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
        return static_cast<const region_iterator*>(self)
        ->i.iterate(reinterpret_cast<Rect*>(rect));
    }
    mutable Region::iterator i;
};
// ---------------------------------------------------------------------------
}; // namespace android

#endif // ANDROID_UI_REGION_H