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