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 | #define LOG_TAG "Region" |
| 18 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Mathias Agopian | 72b0ffe | 2009-07-06 18:07:26 -0700 | [diff] [blame] | 20 | #include <limits.h> |
| 21 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 22 | #include <utils/Log.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <utils/String8.h> |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 24 | #include <utils/CallStack.h> |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 25 | |
| 26 | #include <ui/Rect.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <ui/Region.h> |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 28 | #include <ui/Point.h> |
| 29 | |
| 30 | #include <private/ui/RegionHelper.h> |
| 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | #define VALIDATE_REGIONS (false) |
| 34 | #define VALIDATE_WITH_CORECG (false) |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | |
| 37 | #if VALIDATE_WITH_CORECG |
| 38 | #include <core/SkRegion.h> |
| 39 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
| 41 | namespace android { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | enum { |
| 45 | op_nand = region_operator<Rect>::op_nand, |
| 46 | op_and = region_operator<Rect>::op_and, |
| 47 | op_or = region_operator<Rect>::op_or, |
| 48 | op_xor = region_operator<Rect>::op_xor |
| 49 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
Chris Craik | 3e010f3 | 2013-02-25 19:12:47 -0800 | [diff] [blame] | 51 | enum { |
| 52 | direction_LTR, |
| 53 | direction_RTL |
| 54 | }; |
| 55 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | // ---------------------------------------------------------------------------- |
| 57 | |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 58 | Region::Region() { |
| 59 | mStorage.add(Rect(0,0)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Region::Region(const Region& rhs) |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 63 | : mStorage(rhs.mStorage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | { |
Mathias Agopian | d0b55c0 | 2011-03-16 23:18:07 -0700 | [diff] [blame] | 65 | #if VALIDATE_REGIONS |
| 66 | validate(rhs, "rhs copy-ctor"); |
| 67 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 70 | Region::Region(const Rect& rhs) { |
| 71 | mStorage.add(rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | Region::~Region() |
| 75 | { |
| 76 | } |
| 77 | |
Chris Craik | 3e010f3 | 2013-02-25 19:12:47 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Copy rects from the src vector into the dst vector, resolving vertical T-Junctions along the way |
| 80 | * |
| 81 | * First pass through, divideSpanRTL will be set because the 'previous span' (indexing into the dst |
| 82 | * vector) will be reversed. Each rectangle in the original list, starting from the bottom, will be |
| 83 | * compared with the span directly below, and subdivided as needed to resolve T-junctions. |
| 84 | * |
| 85 | * The resulting temporary vector will be a completely reversed copy of the original, without any |
| 86 | * bottom-up T-junctions. |
| 87 | * |
| 88 | * Second pass through, divideSpanRTL will be false since the previous span will index into the |
| 89 | * final, correctly ordered region buffer. Each rectangle will be compared with the span directly |
| 90 | * above it, and subdivided to resolve any remaining T-junctions. |
| 91 | */ |
| 92 | static void reverseRectsResolvingJunctions(const Rect* begin, const Rect* end, |
| 93 | Vector<Rect>& dst, int spanDirection) { |
| 94 | dst.clear(); |
| 95 | |
| 96 | const Rect* current = end - 1; |
| 97 | int lastTop = current->top; |
| 98 | |
| 99 | // add first span immediately |
| 100 | do { |
| 101 | dst.add(*current); |
| 102 | current--; |
| 103 | } while (current->top == lastTop && current >= begin); |
| 104 | |
| 105 | unsigned int beginLastSpan = -1; |
| 106 | unsigned int endLastSpan = -1; |
| 107 | int top = -1; |
| 108 | int bottom = -1; |
| 109 | |
| 110 | // for all other spans, split if a t-junction exists in the span directly above |
| 111 | while (current >= begin) { |
| 112 | if (current->top != (current + 1)->top) { |
| 113 | // new span |
| 114 | if ((spanDirection == direction_RTL && current->bottom != (current + 1)->top) || |
| 115 | (spanDirection == direction_LTR && current->top != (current + 1)->bottom)) { |
| 116 | // previous span not directly adjacent, don't check for T junctions |
| 117 | beginLastSpan = INT_MAX; |
| 118 | } else { |
| 119 | beginLastSpan = endLastSpan + 1; |
| 120 | } |
| 121 | endLastSpan = dst.size() - 1; |
| 122 | |
| 123 | top = current->top; |
| 124 | bottom = current->bottom; |
| 125 | } |
| 126 | int left = current->left; |
| 127 | int right = current->right; |
| 128 | |
| 129 | for (unsigned int prevIndex = beginLastSpan; prevIndex <= endLastSpan; prevIndex++) { |
| 130 | const Rect* prev = &dst[prevIndex]; |
| 131 | if (spanDirection == direction_RTL) { |
| 132 | // iterating over previous span RTL, quit if it's too far left |
| 133 | if (prev->right <= left) break; |
| 134 | |
| 135 | if (prev->right > left && prev->right < right) { |
| 136 | dst.add(Rect(prev->right, top, right, bottom)); |
| 137 | right = prev->right; |
| 138 | } |
| 139 | |
| 140 | if (prev->left > left && prev->left < right) { |
| 141 | dst.add(Rect(prev->left, top, right, bottom)); |
| 142 | right = prev->left; |
| 143 | } |
| 144 | |
| 145 | // if an entry in the previous span is too far right, nothing further left in the |
| 146 | // current span will need it |
| 147 | if (prev->left >= right) { |
| 148 | beginLastSpan = prevIndex; |
| 149 | } |
| 150 | } else { |
| 151 | // iterating over previous span LTR, quit if it's too far right |
| 152 | if (prev->left >= right) break; |
| 153 | |
| 154 | if (prev->left > left && prev->left < right) { |
| 155 | dst.add(Rect(left, top, prev->left, bottom)); |
| 156 | left = prev->left; |
| 157 | } |
| 158 | |
| 159 | if (prev->right > left && prev->right < right) { |
| 160 | dst.add(Rect(left, top, prev->right, bottom)); |
| 161 | left = prev->right; |
| 162 | } |
| 163 | // if an entry in the previous span is too far left, nothing further right in the |
| 164 | // current span will need it |
| 165 | if (prev->right <= left) { |
| 166 | beginLastSpan = prevIndex; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (left < right) { |
| 172 | dst.add(Rect(left, top, right, bottom)); |
| 173 | } |
| 174 | |
| 175 | current--; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Creates a new region with the same data as the argument, but divides rectangles as necessary to |
| 181 | * remove T-Junctions |
| 182 | * |
| 183 | * Note: the output will not necessarily be a very efficient representation of the region, since it |
| 184 | * may be that a triangle-based approach would generate significantly simpler geometry |
| 185 | */ |
| 186 | Region Region::createTJunctionFreeRegion(const Region& r) { |
| 187 | if (r.isEmpty()) return r; |
| 188 | if (r.isRect()) return r; |
| 189 | |
| 190 | Vector<Rect> reversed; |
| 191 | reverseRectsResolvingJunctions(r.begin(), r.end(), reversed, direction_RTL); |
| 192 | |
| 193 | Region outputRegion; |
| 194 | reverseRectsResolvingJunctions(reversed.begin(), reversed.end(), |
| 195 | outputRegion.mStorage, direction_LTR); |
| 196 | outputRegion.mStorage.add(r.getBounds()); // to make region valid, mStorage must end with bounds |
| 197 | |
| 198 | #if VALIDATE_REGIONS |
| 199 | validate(outputRegion, "T-Junction free region"); |
| 200 | #endif |
| 201 | |
| 202 | return outputRegion; |
| 203 | } |
| 204 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | Region& Region::operator = (const Region& rhs) |
| 206 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 207 | #if VALIDATE_REGIONS |
Mathias Agopian | d0b55c0 | 2011-03-16 23:18:07 -0700 | [diff] [blame] | 208 | validate(*this, "this->operator="); |
| 209 | validate(rhs, "rhs.operator="); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 210 | #endif |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 211 | mStorage = rhs.mStorage; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | return *this; |
| 213 | } |
| 214 | |
Mathias Agopian | 9f96145 | 2009-06-29 18:46:37 -0700 | [diff] [blame] | 215 | Region& Region::makeBoundsSelf() |
| 216 | { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 217 | if (mStorage.size() >= 2) { |
| 218 | const Rect bounds(getBounds()); |
| 219 | mStorage.clear(); |
| 220 | mStorage.add(bounds); |
| 221 | } |
Mathias Agopian | 9f96145 | 2009-06-29 18:46:37 -0700 | [diff] [blame] | 222 | return *this; |
| 223 | } |
| 224 | |
Michael Wright | 1c284a9 | 2014-02-10 13:00:14 -0800 | [diff] [blame] | 225 | bool Region::contains(const Point& point) const { |
| 226 | return contains(point.x, point.y); |
| 227 | } |
| 228 | |
| 229 | bool Region::contains(int x, int y) const { |
| 230 | const_iterator cur = begin(); |
| 231 | const_iterator const tail = end(); |
| 232 | while (cur != tail) { |
| 233 | if (y >= cur->top && y < cur->bottom && x >= cur->left && x < cur->right) { |
| 234 | return true; |
| 235 | } |
| 236 | cur++; |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | void Region::clear() |
| 242 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 243 | mStorage.clear(); |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 244 | mStorage.add(Rect(0,0)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void Region::set(const Rect& r) |
| 248 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 249 | mStorage.clear(); |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 250 | mStorage.add(r); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 253 | void Region::set(uint32_t w, uint32_t h) |
| 254 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 255 | mStorage.clear(); |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 256 | mStorage.add(Rect(w,h)); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Mathias Agopian | 2ca7939 | 2013-04-02 18:30:32 -0700 | [diff] [blame] | 259 | bool Region::isTriviallyEqual(const Region& region) const { |
| 260 | return begin() == region.begin(); |
| 261 | } |
| 262 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | // ---------------------------------------------------------------------------- |
| 264 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 265 | void Region::addRectUnchecked(int l, int t, int r, int b) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 267 | Rect rect(l,t,r,b); |
| 268 | size_t where = mStorage.size() - 1; |
| 269 | mStorage.insertAt(rect, where, 1); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 272 | // ---------------------------------------------------------------------------- |
| 273 | |
| 274 | Region& Region::orSelf(const Rect& r) { |
| 275 | return operationSelf(r, op_or); |
| 276 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 277 | Region& Region::xorSelf(const Rect& r) { |
| 278 | return operationSelf(r, op_xor); |
| 279 | } |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 280 | Region& Region::andSelf(const Rect& r) { |
| 281 | return operationSelf(r, op_and); |
| 282 | } |
| 283 | Region& Region::subtractSelf(const Rect& r) { |
| 284 | return operationSelf(r, op_nand); |
| 285 | } |
| 286 | Region& Region::operationSelf(const Rect& r, int op) { |
| 287 | Region lhs(*this); |
| 288 | boolean_operation(op, *this, lhs, r); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | return *this; |
| 290 | } |
| 291 | |
| 292 | // ---------------------------------------------------------------------------- |
| 293 | |
| 294 | Region& Region::orSelf(const Region& rhs) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 295 | return operationSelf(rhs, op_or); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 297 | Region& Region::xorSelf(const Region& rhs) { |
| 298 | return operationSelf(rhs, op_xor); |
| 299 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | Region& Region::andSelf(const Region& rhs) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 301 | return operationSelf(rhs, op_and); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | Region& Region::subtractSelf(const Region& rhs) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 304 | return operationSelf(rhs, op_nand); |
| 305 | } |
| 306 | Region& Region::operationSelf(const Region& rhs, int op) { |
| 307 | Region lhs(*this); |
| 308 | boolean_operation(op, *this, lhs, rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | return *this; |
| 310 | } |
| 311 | |
| 312 | Region& Region::translateSelf(int x, int y) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 313 | if (x|y) translate(*this, x, y); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | return *this; |
| 315 | } |
| 316 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 317 | // ---------------------------------------------------------------------------- |
| 318 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 319 | const Region Region::merge(const Rect& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 320 | return operation(rhs, op_or); |
| 321 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 322 | const Region Region::mergeExclusive(const Rect& rhs) const { |
| 323 | return operation(rhs, op_xor); |
| 324 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 325 | const Region Region::intersect(const Rect& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 326 | return operation(rhs, op_and); |
| 327 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 328 | const Region Region::subtract(const Rect& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 329 | return operation(rhs, op_nand); |
| 330 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 331 | const Region Region::operation(const Rect& rhs, int op) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 332 | Region result; |
| 333 | boolean_operation(op, result, *this, rhs); |
| 334 | return result; |
| 335 | } |
| 336 | |
| 337 | // ---------------------------------------------------------------------------- |
| 338 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 339 | const Region Region::merge(const Region& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 340 | return operation(rhs, op_or); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 342 | const Region Region::mergeExclusive(const Region& rhs) const { |
| 343 | return operation(rhs, op_xor); |
| 344 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 345 | const Region Region::intersect(const Region& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 346 | return operation(rhs, op_and); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 348 | const Region Region::subtract(const Region& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 349 | return operation(rhs, op_nand); |
| 350 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 351 | const Region Region::operation(const Region& rhs, int op) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | Region result; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 353 | boolean_operation(op, result, *this, rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | return result; |
| 355 | } |
| 356 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 357 | const Region Region::translate(int x, int y) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | Region result; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 359 | translate(result, *this, x, y); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | return result; |
| 361 | } |
| 362 | |
| 363 | // ---------------------------------------------------------------------------- |
| 364 | |
| 365 | Region& Region::orSelf(const Region& rhs, int dx, int dy) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 366 | return operationSelf(rhs, dx, dy, op_or); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 368 | Region& Region::xorSelf(const Region& rhs, int dx, int dy) { |
| 369 | return operationSelf(rhs, dx, dy, op_xor); |
| 370 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | Region& Region::andSelf(const Region& rhs, int dx, int dy) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 372 | return operationSelf(rhs, dx, dy, op_and); |
| 373 | } |
| 374 | Region& Region::subtractSelf(const Region& rhs, int dx, int dy) { |
| 375 | return operationSelf(rhs, dx, dy, op_nand); |
| 376 | } |
| 377 | Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) { |
| 378 | Region lhs(*this); |
| 379 | boolean_operation(op, *this, lhs, rhs, dx, dy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | return *this; |
| 381 | } |
| 382 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 383 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 385 | const Region Region::merge(const Region& rhs, int dx, int dy) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 386 | return operation(rhs, dx, dy, op_or); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 388 | const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const { |
| 389 | return operation(rhs, dx, dy, op_xor); |
| 390 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 391 | const Region Region::intersect(const Region& rhs, int dx, int dy) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 392 | return operation(rhs, dx, dy, op_and); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 394 | const Region Region::subtract(const Region& rhs, int dx, int dy) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 395 | return operation(rhs, dx, dy, op_nand); |
| 396 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 397 | const Region Region::operation(const Region& rhs, int dx, int dy, int op) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | Region result; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 399 | boolean_operation(op, result, *this, rhs, dx, dy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | return result; |
| 401 | } |
| 402 | |
| 403 | // ---------------------------------------------------------------------------- |
| 404 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 405 | // This is our region rasterizer, which merges rects and spans together |
| 406 | // to obtain an optimal region. |
| 407 | class Region::rasterizer : public region_operator<Rect>::region_rasterizer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 409 | Rect bounds; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 410 | Vector<Rect>& storage; |
| 411 | Rect* head; |
| 412 | Rect* tail; |
| 413 | Vector<Rect> span; |
| 414 | Rect* cur; |
| 415 | public: |
| 416 | rasterizer(Region& reg) |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 417 | : bounds(INT_MAX, 0, INT_MIN, 0), storage(reg.mStorage), head(), tail(), cur() { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 418 | storage.clear(); |
| 419 | } |
| 420 | |
| 421 | ~rasterizer() { |
| 422 | if (span.size()) { |
| 423 | flushSpan(); |
| 424 | } |
| 425 | if (storage.size()) { |
| 426 | bounds.top = storage.itemAt(0).top; |
| 427 | bounds.bottom = storage.top().bottom; |
| 428 | if (storage.size() == 1) { |
| 429 | storage.clear(); |
| 430 | } |
| 431 | } else { |
| 432 | bounds.left = 0; |
| 433 | bounds.right = 0; |
| 434 | } |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 435 | storage.add(bounds); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | virtual void operator()(const Rect& rect) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 439 | //ALOGD(">>> %3d, %3d, %3d, %3d", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 440 | // rect.left, rect.top, rect.right, rect.bottom); |
| 441 | if (span.size()) { |
| 442 | if (cur->top != rect.top) { |
| 443 | flushSpan(); |
| 444 | } else if (cur->right == rect.left) { |
| 445 | cur->right = rect.right; |
| 446 | return; |
| 447 | } |
| 448 | } |
| 449 | span.add(rect); |
| 450 | cur = span.editArray() + (span.size() - 1); |
| 451 | } |
| 452 | private: |
| 453 | template<typename T> |
| 454 | static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; } |
| 455 | template<typename T> |
| 456 | static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; } |
| 457 | void flushSpan() { |
| 458 | bool merge = false; |
| 459 | if (tail-head == ssize_t(span.size())) { |
Romain Guy | b801624 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 460 | Rect const* p = span.editArray(); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 461 | Rect const* q = head; |
| 462 | if (p->top == q->bottom) { |
| 463 | merge = true; |
| 464 | while (q != tail) { |
| 465 | if ((p->left != q->left) || (p->right != q->right)) { |
| 466 | merge = false; |
| 467 | break; |
| 468 | } |
| 469 | p++, q++; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | if (merge) { |
| 474 | const int bottom = span[0].bottom; |
| 475 | Rect* r = head; |
| 476 | while (r != tail) { |
| 477 | r->bottom = bottom; |
| 478 | r++; |
| 479 | } |
| 480 | } else { |
| 481 | bounds.left = min(span.itemAt(0).left, bounds.left); |
| 482 | bounds.right = max(span.top().right, bounds.right); |
| 483 | storage.appendVector(span); |
| 484 | tail = storage.editArray() + storage.size(); |
| 485 | head = tail - span.size(); |
| 486 | } |
| 487 | span.clear(); |
| 488 | } |
| 489 | }; |
| 490 | |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 491 | bool Region::validate(const Region& reg, const char* name, bool silent) |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 492 | { |
| 493 | bool result = true; |
| 494 | const_iterator cur = reg.begin(); |
| 495 | const_iterator const tail = reg.end(); |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 496 | const_iterator prev = cur; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 497 | Rect b(*prev); |
| 498 | while (cur != tail) { |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 499 | if (cur->isValid() == false) { |
| 500 | ALOGE_IF(!silent, "%s: region contains an invalid Rect", name); |
| 501 | result = false; |
| 502 | } |
| 503 | if (cur->right > region_operator<Rect>::max_value) { |
| 504 | ALOGE_IF(!silent, "%s: rect->right > max_value", name); |
| 505 | result = false; |
| 506 | } |
| 507 | if (cur->bottom > region_operator<Rect>::max_value) { |
| 508 | ALOGE_IF(!silent, "%s: rect->right > max_value", name); |
| 509 | result = false; |
| 510 | } |
| 511 | if (prev != cur) { |
| 512 | b.left = b.left < cur->left ? b.left : cur->left; |
| 513 | b.top = b.top < cur->top ? b.top : cur->top; |
| 514 | b.right = b.right > cur->right ? b.right : cur->right; |
| 515 | b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom; |
| 516 | if ((*prev < *cur) == false) { |
| 517 | ALOGE_IF(!silent, "%s: region's Rects not sorted", name); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 518 | result = false; |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 519 | } |
| 520 | if (cur->top == prev->top) { |
| 521 | if (cur->bottom != prev->bottom) { |
| 522 | ALOGE_IF(!silent, "%s: invalid span %p", name, cur); |
| 523 | result = false; |
| 524 | } else if (cur->left < prev->right) { |
| 525 | ALOGE_IF(!silent, |
| 526 | "%s: spans overlap horizontally prev=%p, cur=%p", |
| 527 | name, prev, cur); |
| 528 | result = false; |
| 529 | } |
| 530 | } else if (cur->top < prev->bottom) { |
| 531 | ALOGE_IF(!silent, |
| 532 | "%s: spans overlap vertically prev=%p, cur=%p", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 533 | name, prev, cur); |
| 534 | result = false; |
| 535 | } |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 536 | prev = cur; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 537 | } |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 538 | cur++; |
| 539 | } |
| 540 | if (b != reg.getBounds()) { |
| 541 | result = false; |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 542 | ALOGE_IF(!silent, |
| 543 | "%s: invalid bounds [%d,%d,%d,%d] vs. [%d,%d,%d,%d]", name, |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 544 | b.left, b.top, b.right, b.bottom, |
| 545 | reg.getBounds().left, reg.getBounds().top, |
| 546 | reg.getBounds().right, reg.getBounds().bottom); |
| 547 | } |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 548 | if (reg.mStorage.size() == 2) { |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 549 | result = false; |
| 550 | ALOGE_IF(!silent, "%s: mStorage size is 2, which is never valid", name); |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 551 | } |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 552 | if (result == false && !silent) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 553 | reg.dump(name); |
Mathias Agopian | cab25d6 | 2013-03-21 17:12:40 -0700 | [diff] [blame] | 554 | CallStack stack(LOG_TAG); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 555 | } |
| 556 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 559 | void Region::boolean_operation(int op, Region& dst, |
| 560 | const Region& lhs, |
| 561 | const Region& rhs, int dx, int dy) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | { |
Mathias Agopian | d0b55c0 | 2011-03-16 23:18:07 -0700 | [diff] [blame] | 563 | #if VALIDATE_REGIONS |
| 564 | validate(lhs, "boolean_operation (before): lhs"); |
| 565 | validate(rhs, "boolean_operation (before): rhs"); |
| 566 | validate(dst, "boolean_operation (before): dst"); |
| 567 | #endif |
| 568 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 569 | size_t lhs_count; |
| 570 | Rect const * const lhs_rects = lhs.getArray(&lhs_count); |
| 571 | |
| 572 | size_t rhs_count; |
| 573 | Rect const * const rhs_rects = rhs.getArray(&rhs_count); |
| 574 | |
| 575 | region_operator<Rect>::region lhs_region(lhs_rects, lhs_count); |
| 576 | region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy); |
| 577 | region_operator<Rect> operation(op, lhs_region, rhs_region); |
| 578 | { // scope for rasterizer (dtor has side effects) |
| 579 | rasterizer r(dst); |
| 580 | operation(r); |
| 581 | } |
| 582 | |
| 583 | #if VALIDATE_REGIONS |
| 584 | validate(lhs, "boolean_operation: lhs"); |
| 585 | validate(rhs, "boolean_operation: rhs"); |
| 586 | validate(dst, "boolean_operation: dst"); |
| 587 | #endif |
| 588 | |
| 589 | #if VALIDATE_WITH_CORECG |
| 590 | SkRegion sk_lhs; |
| 591 | SkRegion sk_rhs; |
| 592 | SkRegion sk_dst; |
| 593 | |
| 594 | for (size_t i=0 ; i<lhs_count ; i++) |
| 595 | sk_lhs.op( |
| 596 | lhs_rects[i].left + dx, |
| 597 | lhs_rects[i].top + dy, |
| 598 | lhs_rects[i].right + dx, |
| 599 | lhs_rects[i].bottom + dy, |
| 600 | SkRegion::kUnion_Op); |
| 601 | |
| 602 | for (size_t i=0 ; i<rhs_count ; i++) |
| 603 | sk_rhs.op( |
| 604 | rhs_rects[i].left + dx, |
| 605 | rhs_rects[i].top + dy, |
| 606 | rhs_rects[i].right + dx, |
| 607 | rhs_rects[i].bottom + dy, |
| 608 | SkRegion::kUnion_Op); |
| 609 | |
| 610 | const char* name = "---"; |
| 611 | SkRegion::Op sk_op; |
| 612 | switch (op) { |
| 613 | case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break; |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 614 | case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 615 | case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break; |
| 616 | case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break; |
| 617 | } |
| 618 | sk_dst.op(sk_lhs, sk_rhs, sk_op); |
| 619 | |
| 620 | if (sk_dst.isEmpty() && dst.isEmpty()) |
| 621 | return; |
| 622 | |
| 623 | bool same = true; |
| 624 | Region::const_iterator head = dst.begin(); |
| 625 | Region::const_iterator const tail = dst.end(); |
| 626 | SkRegion::Iterator it(sk_dst); |
| 627 | while (!it.done()) { |
| 628 | if (head != tail) { |
| 629 | if ( |
| 630 | head->left != it.rect().fLeft || |
| 631 | head->top != it.rect().fTop || |
| 632 | head->right != it.rect().fRight || |
| 633 | head->bottom != it.rect().fBottom |
| 634 | ) { |
| 635 | same = false; |
| 636 | break; |
| 637 | } |
| 638 | } else { |
| 639 | same = false; |
| 640 | break; |
| 641 | } |
| 642 | head++; |
| 643 | it.next(); |
| 644 | } |
| 645 | |
| 646 | if (head != tail) { |
| 647 | same = false; |
| 648 | } |
| 649 | |
| 650 | if(!same) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 651 | ALOGD("---\nregion boolean %s failed", name); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 652 | lhs.dump("lhs"); |
| 653 | rhs.dump("rhs"); |
| 654 | dst.dump("dst"); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 655 | ALOGD("should be"); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 656 | SkRegion::Iterator it(sk_dst); |
| 657 | while (!it.done()) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 658 | ALOGD(" [%3d, %3d, %3d, %3d]", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 659 | it.rect().fLeft, |
| 660 | it.rect().fTop, |
| 661 | it.rect().fRight, |
| 662 | it.rect().fBottom); |
| 663 | it.next(); |
| 664 | } |
| 665 | } |
| 666 | #endif |
| 667 | } |
| 668 | |
| 669 | void Region::boolean_operation(int op, Region& dst, |
| 670 | const Region& lhs, |
| 671 | const Rect& rhs, int dx, int dy) |
| 672 | { |
Mathias Agopian | 0450452 | 2011-09-19 16:12:08 -0700 | [diff] [blame] | 673 | if (!rhs.isValid()) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 674 | ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}", |
Mathias Agopian | 0450452 | 2011-09-19 16:12:08 -0700 | [diff] [blame] | 675 | op, rhs.left, rhs.top, rhs.right, rhs.bottom); |
Mathias Agopian | 0857c8f | 2011-09-26 15:58:20 -0700 | [diff] [blame] | 676 | return; |
Mathias Agopian | 0450452 | 2011-09-19 16:12:08 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 679 | #if VALIDATE_WITH_CORECG || VALIDATE_REGIONS |
| 680 | boolean_operation(op, dst, lhs, Region(rhs), dx, dy); |
| 681 | #else |
| 682 | size_t lhs_count; |
| 683 | Rect const * const lhs_rects = lhs.getArray(&lhs_count); |
| 684 | |
| 685 | region_operator<Rect>::region lhs_region(lhs_rects, lhs_count); |
| 686 | region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy); |
| 687 | region_operator<Rect> operation(op, lhs_region, rhs_region); |
| 688 | { // scope for rasterizer (dtor has side effects) |
| 689 | rasterizer r(dst); |
| 690 | operation(r); |
| 691 | } |
| 692 | |
| 693 | #endif |
| 694 | } |
| 695 | |
| 696 | void Region::boolean_operation(int op, Region& dst, |
| 697 | const Region& lhs, const Region& rhs) |
| 698 | { |
| 699 | boolean_operation(op, dst, lhs, rhs, 0, 0); |
| 700 | } |
| 701 | |
| 702 | void Region::boolean_operation(int op, Region& dst, |
| 703 | const Region& lhs, const Rect& rhs) |
| 704 | { |
| 705 | boolean_operation(op, dst, lhs, rhs, 0, 0); |
| 706 | } |
| 707 | |
| 708 | void Region::translate(Region& reg, int dx, int dy) |
| 709 | { |
Mathias Agopian | 4c0a170 | 2012-08-31 12:45:33 -0700 | [diff] [blame] | 710 | if ((dx || dy) && !reg.isEmpty()) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 711 | #if VALIDATE_REGIONS |
| 712 | validate(reg, "translate (before)"); |
| 713 | #endif |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 714 | size_t count = reg.mStorage.size(); |
| 715 | Rect* rects = reg.mStorage.editArray(); |
| 716 | while (count) { |
Mathias Agopian | 6c7f25a | 2013-05-09 20:37:10 -0700 | [diff] [blame] | 717 | rects->offsetBy(dx, dy); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 718 | rects++; |
| 719 | count--; |
| 720 | } |
| 721 | #if VALIDATE_REGIONS |
| 722 | validate(reg, "translate (after)"); |
| 723 | #endif |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | void Region::translate(Region& dst, const Region& reg, int dx, int dy) |
| 728 | { |
| 729 | dst = reg; |
| 730 | translate(dst, dx, dy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | // ---------------------------------------------------------------------------- |
| 734 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 735 | size_t Region::getFlattenedSize() const { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 736 | return mStorage.size() * sizeof(Rect); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 739 | status_t Region::flatten(void* buffer, size_t size) const { |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 740 | #if VALIDATE_REGIONS |
| 741 | validate(*this, "Region::flatten"); |
| 742 | #endif |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 743 | if (size < mStorage.size() * sizeof(Rect)) { |
| 744 | return NO_MEMORY; |
| 745 | } |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 746 | Rect* rects = reinterpret_cast<Rect*>(buffer); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 747 | memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect)); |
| 748 | return NO_ERROR; |
| 749 | } |
| 750 | |
| 751 | status_t Region::unflatten(void const* buffer, size_t size) { |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 752 | Region result; |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 753 | if (size >= sizeof(Rect)) { |
| 754 | Rect const* rects = reinterpret_cast<Rect const*>(buffer); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 755 | size_t count = size / sizeof(Rect); |
| 756 | if (count > 0) { |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 757 | result.mStorage.clear(); |
| 758 | ssize_t err = result.mStorage.insertAt(0, count); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 759 | if (err < 0) { |
| 760 | return status_t(err); |
| 761 | } |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 762 | memcpy(result.mStorage.editArray(), rects, count*sizeof(Rect)); |
Mathias Agopian | b612142 | 2010-02-17 20:22:26 -0800 | [diff] [blame] | 763 | } |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 764 | } |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 765 | #if VALIDATE_REGIONS |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 766 | validate(result, "Region::unflatten"); |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 767 | #endif |
Mathias Agopian | 068d47f | 2012-09-11 18:56:23 -0700 | [diff] [blame] | 768 | |
| 769 | if (!result.validate(result, "Region::unflatten", true)) { |
| 770 | ALOGE("Region::unflatten() failed, invalid region"); |
| 771 | return BAD_VALUE; |
| 772 | } |
| 773 | mStorage = result.mStorage; |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 774 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 777 | // ---------------------------------------------------------------------------- |
| 778 | |
| 779 | Region::const_iterator Region::begin() const { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 780 | return mStorage.array(); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | Region::const_iterator Region::end() const { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 784 | size_t numRects = isRect() ? 1 : mStorage.size() - 1; |
| 785 | return mStorage.array() + numRects; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | Rect const* Region::getArray(size_t* count) const { |
| 789 | const_iterator const b(begin()); |
| 790 | const_iterator const e(end()); |
| 791 | if (count) *count = e-b; |
| 792 | return b; |
| 793 | } |
| 794 | |
Mathias Agopian | 2401ead | 2012-08-31 15:41:24 -0700 | [diff] [blame] | 795 | SharedBuffer const* Region::getSharedBuffer(size_t* count) const { |
| 796 | // We can get to the SharedBuffer of a Vector<Rect> because Rect has |
| 797 | // a trivial destructor. |
| 798 | SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array()); |
| 799 | if (count) { |
| 800 | size_t numRects = isRect() ? 1 : mStorage.size() - 1; |
| 801 | count[0] = numRects; |
| 802 | } |
Praveena Pachipulusu | e0c1a8b | 2014-06-19 12:10:56 +0530 | [diff] [blame] | 803 | if (sb != NULL) { |
| 804 | sb->acquire(); |
| 805 | } |
Mathias Agopian | 2401ead | 2012-08-31 15:41:24 -0700 | [diff] [blame] | 806 | return sb; |
| 807 | } |
| 808 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 809 | // ---------------------------------------------------------------------------- |
| 810 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | void Region::dump(String8& out, const char* what, uint32_t flags) const |
| 812 | { |
| 813 | (void)flags; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 814 | const_iterator head = begin(); |
| 815 | const_iterator const tail = end(); |
| 816 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | size_t SIZE = 256; |
| 818 | char buffer[SIZE]; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 819 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 820 | snprintf(buffer, SIZE, " Region %s (this=%p, count=%" PRIdPTR ")\n", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 821 | what, this, tail-head); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | out.append(buffer); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 823 | while (head != tail) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | snprintf(buffer, SIZE, " [%3d, %3d, %3d, %3d]\n", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 825 | head->left, head->top, head->right, head->bottom); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | out.append(buffer); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 827 | head++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
| 831 | void Region::dump(const char* what, uint32_t flags) const |
| 832 | { |
| 833 | (void)flags; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 834 | const_iterator head = begin(); |
| 835 | const_iterator const tail = end(); |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 836 | ALOGD(" Region %s (this=%p, count=%" PRIdPTR ")\n", what, this, tail-head); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 837 | while (head != tail) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 838 | ALOGD(" [%3d, %3d, %3d, %3d]\n", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 839 | head->left, head->top, head->right, head->bottom); |
| 840 | head++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
| 844 | // ---------------------------------------------------------------------------- |
| 845 | |
| 846 | }; // namespace android |