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 25 #include <ui/Rect.h> 26 #include <utils/Flattenable.h> 27 28 #include <android-base/macros.h> 29 30 #include <string> 31 32 namespace android { 33 // --------------------------------------------------------------------------- 34 35 class Region : public LightFlattenable<Region> 36 { 37 public: 38 static const Region INVALID_REGION; 39 40 Region(); 41 Region(const Region& rhs); 42 explicit Region(const Rect& rhs); 43 ~Region(); 44 45 static Region createTJunctionFreeRegion(const Region& r); 46 47 Region& operator = (const Region& rhs); 48 49 inline bool isEmpty() const { return getBounds().isEmpty(); } 50 inline bool isRect() const { return mStorage.size() == 1; } 51 52 inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; } 53 inline Rect bounds() const { return getBounds(); } 54 55 bool contains(const Point& point) const; 56 bool contains(int x, int y) const; 57 58 // the region becomes its bounds 59 Region& makeBoundsSelf(); 60 61 void clear(); 62 void set(const Rect& r); 63 void set(int32_t w, int32_t h); 64 void set(uint32_t w, uint32_t h); 65 66 Region& orSelf(const Rect& rhs); 67 Region& xorSelf(const Rect& rhs); 68 Region& andSelf(const Rect& rhs); 69 Region& subtractSelf(const Rect& rhs); 70 71 // boolean operators, applied on this 72 Region& orSelf(const Region& rhs); 73 Region& xorSelf(const Region& rhs); 74 Region& andSelf(const Region& rhs); 75 Region& subtractSelf(const Region& rhs); 76 77 // boolean operators 78 const Region merge(const Rect& rhs) const; 79 const Region mergeExclusive(const Rect& rhs) const; 80 const Region intersect(const Rect& rhs) const; 81 const Region subtract(const Rect& rhs) const; 82 83 // boolean operators 84 const Region merge(const Region& rhs) const; 85 const Region mergeExclusive(const Region& rhs) const; 86 const Region intersect(const Region& rhs) const; 87 const Region subtract(const Region& rhs) const; 88 89 // these translate rhs first 90 Region& translateSelf(int dx, int dy); 91 Region& scaleSelf(float sx, float sy); 92 Region& orSelf(const Region& rhs, int dx, int dy); 93 Region& xorSelf(const Region& rhs, int dx, int dy); 94 Region& andSelf(const Region& rhs, int dx, int dy); 95 Region& subtractSelf(const Region& rhs, int dx, int dy); 96 97 98 // these translate rhs first 99 const Region translate(int dx, int dy) const WARN_UNUSED; 100 const Region merge(const Region& rhs, int dx, int dy) const WARN_UNUSED; 101 const Region mergeExclusive(const Region& rhs, int dx, int dy) const WARN_UNUSED; 102 const Region intersect(const Region& rhs, int dx, int dy) const WARN_UNUSED; 103 const Region subtract(const Region& rhs, int dx, int dy) const WARN_UNUSED; 104 105 // convenience operators overloads 106 inline const Region operator | (const Region& rhs) const; 107 inline const Region operator ^ (const Region& rhs) const; 108 inline const Region operator & (const Region& rhs) const; 109 inline const Region operator - (const Region& rhs) const; 110 inline const Region operator + (const Point& pt) const; 111 112 inline Region& operator |= (const Region& rhs); 113 inline Region& operator ^= (const Region& rhs); 114 inline Region& operator &= (const Region& rhs); 115 inline Region& operator -= (const Region& rhs); 116 inline Region& operator += (const Point& pt); 117 118 119 // returns true if the regions share the same underlying storage 120 bool isTriviallyEqual(const Region& region) const; 121 122 123 /* various ways to access the rectangle list */ 124 125 126 // STL-like iterators 127 typedef Rect const* const_iterator; 128 const_iterator begin() const; 129 const_iterator end() const; 130 131 // returns an array of rect which has the same life-time has this 132 // Region object. 133 Rect const* getArray(size_t* count) const; 134 135 /* no user serviceable parts here... */ 136 137 // add a rectangle to the internal list. This rectangle must 138 // be sorted in Y and X and must not make the region invalid. 139 void addRectUnchecked(int l, int t, int r, int b); 140 141 inline bool isFixedSize() const { return false; } 142 size_t getFlattenedSize() const; 143 status_t flatten(void* buffer, size_t size) const; 144 status_t unflatten(void const* buffer, size_t size); 145 146 void dump(std::string& out, const char* what, uint32_t flags=0) const; 147 void dump(const char* what, uint32_t flags=0) const; 148 149 private: 150 class rasterizer; 151 friend class rasterizer; 152 153 Region& operationSelf(const Rect& r, uint32_t op); 154 Region& operationSelf(const Region& r, uint32_t op); 155 Region& operationSelf(const Region& r, int dx, int dy, uint32_t op); 156 const Region operation(const Rect& rhs, uint32_t op) const; 157 const Region operation(const Region& rhs, uint32_t op) const; 158 const Region operation(const Region& rhs, int dx, int dy, uint32_t op) const; 159 160 static void boolean_operation(uint32_t op, Region& dst, 161 const Region& lhs, const Region& rhs, int dx, int dy); 162 static void boolean_operation(uint32_t op, Region& dst, 163 const Region& lhs, const Rect& rhs, int dx, int dy); 164 165 static void boolean_operation(uint32_t op, Region& dst, 166 const Region& lhs, const Region& rhs); 167 static void boolean_operation(uint32_t op, Region& dst, 168 const Region& lhs, const Rect& rhs); 169 170 static void translate(Region& reg, int dx, int dy); 171 static void translate(Region& dst, const Region& reg, int dx, int dy); 172 173 static bool validate(const Region& reg, 174 const char* name, bool silent = false); 175 176 // mStorage is a (manually) sorted array of Rects describing the region 177 // with an extra Rect as the last element which is set to the 178 // bounds of the region. However, if the region is 179 // a simple Rect then mStorage contains only that rect. 180 Vector<Rect> mStorage; 181 }; 182 183 184 const Region Region::operator | (const Region& rhs) const { 185 return merge(rhs); 186 } 187 const Region Region::operator ^ (const Region& rhs) const { 188 return mergeExclusive(rhs); 189 } 190 const Region Region::operator & (const Region& rhs) const { 191 return intersect(rhs); 192 } 193 const Region Region::operator - (const Region& rhs) const { 194 return subtract(rhs); 195 } 196 const Region Region::operator + (const Point& pt) const { 197 return translate(pt.x, pt.y); 198 } 199 200 201 Region& Region::operator |= (const Region& rhs) { 202 return orSelf(rhs); 203 } 204 Region& Region::operator ^= (const Region& rhs) { 205 return xorSelf(rhs); 206 } 207 Region& Region::operator &= (const Region& rhs) { 208 return andSelf(rhs); 209 } 210 Region& Region::operator -= (const Region& rhs) { 211 return subtractSelf(rhs); 212 } 213 Region& Region::operator += (const Point& pt) { 214 return translateSelf(pt.x, pt.y); 215 } 216 // --------------------------------------------------------------------------- 217 }; // namespace android 218 219 #endif // ANDROID_UI_REGION_H 220 221