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