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