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> |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 26 | #include <utils/Flattenable.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | class String8; |
| 32 | |
| 33 | // --------------------------------------------------------------------------- |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 34 | class Region : public LightFlattenable<Region> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | { |
| 36 | public: |
Dan Stoza | 5065a55 | 2015-03-17 16:23:42 -0700 | [diff] [blame] | 37 | static const Region INVALID_REGION; |
| 38 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | Region(); |
| 40 | Region(const Region& rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | explicit Region(const Rect& rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | ~Region(); |
Chris Craik | 3e010f3 | 2013-02-25 19:12:47 -0800 | [diff] [blame] | 43 | |
| 44 | static Region createTJunctionFreeRegion(const Region& r); |
| 45 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | Region& operator = (const Region& rhs); |
| 47 | |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 48 | inline bool isEmpty() const { return getBounds().isEmpty(); } |
| 49 | inline bool isRect() const { return mStorage.size() == 1; } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 51 | inline Rect getBounds() const { return mStorage[mStorage.size() - 1]; } |
Mathias Agopian | 17b2ad0 | 2009-06-26 19:02:40 -0700 | [diff] [blame] | 52 | inline Rect bounds() const { return getBounds(); } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | |
Michael Wright | 1c284a9 | 2014-02-10 13:00:14 -0800 | [diff] [blame] | 54 | bool contains(const Point& point) const; |
| 55 | bool contains(int x, int y) const; |
| 56 | |
Mathias Agopian | 9f96145 | 2009-06-29 18:46:37 -0700 | [diff] [blame] | 57 | // the region becomes its bounds |
| 58 | Region& makeBoundsSelf(); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 59 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | void clear(); |
| 61 | void set(const Rect& r); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 62 | void set(int32_t w, int32_t h); |
Bernhard Rosenkraenzer | fe4966d | 2014-12-22 21:15:08 +0100 | [diff] [blame] | 63 | void set(uint32_t w, uint32_t h); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 64 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | Region& orSelf(const Rect& rhs); |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 66 | Region& xorSelf(const Rect& rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | Region& andSelf(const Rect& rhs); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 68 | Region& subtractSelf(const Rect& rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| 70 | // boolean operators, applied on this |
| 71 | Region& orSelf(const Region& rhs); |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 72 | Region& xorSelf(const Region& rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | Region& andSelf(const Region& rhs); |
| 74 | Region& subtractSelf(const Region& rhs); |
| 75 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 76 | // boolean operators |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 77 | const Region merge(const Rect& rhs) const; |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 78 | const Region mergeExclusive(const Rect& rhs) const; |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 79 | const Region intersect(const Rect& rhs) const; |
| 80 | const Region subtract(const Rect& rhs) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
| 82 | // boolean operators |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 83 | const Region merge(const Region& rhs) const; |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 84 | const Region mergeExclusive(const Region& rhs) const; |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 85 | const Region intersect(const Region& rhs) const; |
| 86 | const Region subtract(const Region& rhs) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
| 88 | // these translate rhs first |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 89 | Region& translateSelf(int dx, int dy); |
| 90 | Region& orSelf(const Region& rhs, int dx, int dy); |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 91 | Region& xorSelf(const Region& rhs, int dx, int dy); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 92 | Region& andSelf(const Region& rhs, int dx, int dy); |
| 93 | Region& subtractSelf(const Region& rhs, int dx, int dy); |
| 94 | |
| 95 | // these translate rhs first |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 96 | const Region translate(int dx, int dy) const; |
| 97 | const Region merge(const Region& rhs, int dx, int dy) const; |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 98 | const Region mergeExclusive(const Region& rhs, int dx, int dy) const; |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 99 | const Region intersect(const Region& rhs, int dx, int dy) const; |
| 100 | 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] | 101 | |
| 102 | // convenience operators overloads |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 103 | inline const Region operator | (const Region& rhs) const; |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 104 | inline const Region operator ^ (const Region& rhs) const; |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 105 | inline const Region operator & (const Region& rhs) const; |
| 106 | inline const Region operator - (const Region& rhs) const; |
| 107 | inline const Region operator + (const Point& pt) const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
| 109 | inline Region& operator |= (const Region& rhs); |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 110 | inline Region& operator ^= (const Region& rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | inline Region& operator &= (const Region& rhs); |
| 112 | inline Region& operator -= (const Region& rhs); |
| 113 | inline Region& operator += (const Point& pt); |
| 114 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 115 | |
Mathias Agopian | 2ca7939 | 2013-04-02 18:30:32 -0700 | [diff] [blame] | 116 | // returns true if the regions share the same underlying storage |
| 117 | bool isTriviallyEqual(const Region& region) const; |
| 118 | |
| 119 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 120 | /* various ways to access the rectangle list */ |
Mathias Agopian | 2401ead | 2012-08-31 15:41:24 -0700 | [diff] [blame] | 121 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 122 | |
Mathias Agopian | 2401ead | 2012-08-31 15:41:24 -0700 | [diff] [blame] | 123 | // STL-like iterators |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 124 | typedef Rect const* const_iterator; |
Mathias Agopian | 2401ead | 2012-08-31 15:41:24 -0700 | [diff] [blame] | 125 | const_iterator begin() const; |
| 126 | const_iterator end() const; |
| 127 | |
| 128 | // returns an array of rect which has the same life-time has this |
| 129 | // Region object. |
| 130 | Rect const* getArray(size_t* count) const; |
| 131 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 132 | /* no user serviceable parts here... */ |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 133 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 134 | // add a rectangle to the internal list. This rectangle must |
| 135 | // be sorted in Y and X and must not make the region invalid. |
| 136 | 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] | 137 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 138 | inline bool isFixedSize() const { return false; } |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 139 | size_t getFlattenedSize() const; |
| 140 | status_t flatten(void* buffer, size_t size) const; |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 141 | status_t unflatten(void const* buffer, size_t size); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
| 143 | void dump(String8& out, const char* what, uint32_t flags=0) const; |
| 144 | void dump(const char* what, uint32_t flags=0) const; |
| 145 | |
| 146 | private: |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 147 | class rasterizer; |
| 148 | friend class rasterizer; |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 149 | |
Colin Cross | 8f27996 | 2016-09-26 13:08:16 -0700 | [diff] [blame] | 150 | Region& operationSelf(const Rect& r, uint32_t op); |
| 151 | Region& operationSelf(const Region& r, uint32_t op); |
| 152 | Region& operationSelf(const Region& r, int dx, int dy, uint32_t op); |
| 153 | const Region operation(const Rect& rhs, uint32_t op) const; |
| 154 | const Region operation(const Region& rhs, uint32_t op) const; |
| 155 | const Region operation(const Region& rhs, int dx, int dy, uint32_t op) const; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 156 | |
Colin Cross | 8f27996 | 2016-09-26 13:08:16 -0700 | [diff] [blame] | 157 | static void boolean_operation(uint32_t op, Region& dst, |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 158 | const Region& lhs, const Region& rhs, int dx, int dy); |
Colin Cross | 8f27996 | 2016-09-26 13:08:16 -0700 | [diff] [blame] | 159 | static void boolean_operation(uint32_t op, Region& dst, |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 160 | const Region& lhs, const Rect& rhs, int dx, int dy); |
| 161 | |
Colin Cross | 8f27996 | 2016-09-26 13:08:16 -0700 | [diff] [blame] | 162 | static void boolean_operation(uint32_t op, Region& dst, |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 163 | const Region& lhs, const Region& rhs); |
Colin Cross | 8f27996 | 2016-09-26 13:08:16 -0700 | [diff] [blame] | 164 | static void boolean_operation(uint32_t op, Region& dst, |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 165 | const Region& lhs, const Rect& rhs); |
| 166 | |
| 167 | static void translate(Region& reg, int dx, int dy); |
| 168 | static void translate(Region& dst, const Region& reg, int dx, int dy); |
| 169 | |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 170 | static bool validate(const Region& reg, |
| 171 | const char* name, bool silent = false); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 172 | |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 173 | // mStorage is a (manually) sorted array of Rects describing the region |
| 174 | // with an extra Rect as the last element which is set to the |
| 175 | // bounds of the region. However, if the region is |
| 176 | // a simple Rect then mStorage contains only that rect. |
| 177 | Vector<Rect> mStorage; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 181 | const Region Region::operator | (const Region& rhs) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | return merge(rhs); |
| 183 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 184 | const Region Region::operator ^ (const Region& rhs) const { |
| 185 | return mergeExclusive(rhs); |
| 186 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 187 | const Region Region::operator & (const Region& rhs) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | return intersect(rhs); |
| 189 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 190 | const Region Region::operator - (const Region& rhs) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | return subtract(rhs); |
| 192 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 193 | const Region Region::operator + (const Point& pt) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | return translate(pt.x, pt.y); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | Region& Region::operator |= (const Region& rhs) { |
| 199 | return orSelf(rhs); |
| 200 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 201 | Region& Region::operator ^= (const Region& rhs) { |
| 202 | return xorSelf(rhs); |
| 203 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | Region& Region::operator &= (const Region& rhs) { |
| 205 | return andSelf(rhs); |
| 206 | } |
| 207 | Region& Region::operator -= (const Region& rhs) { |
| 208 | return subtractSelf(rhs); |
| 209 | } |
| 210 | Region& Region::operator += (const Point& pt) { |
| 211 | return translateSelf(pt.x, pt.y); |
| 212 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | // --------------------------------------------------------------------------- |
| 214 | }; // namespace android |
| 215 | |
| 216 | #endif // ANDROID_UI_REGION_H |
| 217 | |