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 27 namespace android { 28 // --------------------------------------------------------------------------- 29 30 class String8; 31 32 // --------------------------------------------------------------------------- 33 class Region 34 { 35 public: 36 Region(); 37 Region(const Region& rhs); 38 explicit Region(const Rect& rhs); 39 explicit Region(const void* buffer); 40 ~Region(); 41 42 Region& operator = (const Region& rhs); 43 isEmpty()44 inline bool isEmpty() const { return mBounds.isEmpty(); } isRect()45 inline bool isRect() const { return mStorage.isEmpty(); } 46 getBounds()47 inline Rect getBounds() const { return mBounds; } bounds()48 inline Rect bounds() const { return getBounds(); } 49 50 // the region becomes its bounds 51 Region& makeBoundsSelf(); 52 53 void clear(); 54 void set(const Rect& r); 55 void set(uint32_t w, uint32_t h); 56 57 Region& orSelf(const Rect& rhs); 58 Region& andSelf(const Rect& rhs); 59 Region& subtractSelf(const Rect& rhs); 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 66 // boolean operators 67 const Region merge(const Rect& rhs) const; 68 const Region intersect(const Rect& rhs) const; 69 const Region subtract(const Rect& rhs) const; 70 71 // boolean operators 72 const Region merge(const Region& rhs) const; 73 const Region intersect(const Region& rhs) const; 74 const Region subtract(const Region& rhs) const; 75 76 // these translate rhs first 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 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; 87 88 // convenience operators overloads 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; 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 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; 106 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); 116 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: 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); 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; 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; 155 }; 156 157 158 const Region Region::operator | (const Region& rhs) const { 159 return merge(rhs); 160 } 161 const Region Region::operator & (const Region& rhs) const { 162 return intersect(rhs); 163 } 164 const Region Region::operator - (const Region& rhs) const { 165 return subtract(rhs); 166 } 167 const Region Region::operator + (const Point& pt) const { 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 } 184 // --------------------------------------------------------------------------- 185 }; // namespace android 186 187 #endif // ANDROID_UI_REGION_H 188 189