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