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