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