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 | |
Mathias Agopian | 72b0ffe | 2009-07-06 18:07:26 -0700 | [diff] [blame] | 19 | #include <limits.h> |
| 20 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <utils/String8.h> |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 23 | |
| 24 | #include <ui/Rect.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <ui/Region.h> |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 26 | #include <ui/Point.h> |
| 27 | |
| 28 | #include <private/ui/RegionHelper.h> |
| 29 | |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | #define VALIDATE_REGIONS (false) |
| 32 | #define VALIDATE_WITH_CORECG (false) |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | #if VALIDATE_WITH_CORECG |
| 36 | #include <core/SkRegion.h> |
| 37 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | namespace android { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | enum { |
| 43 | op_nand = region_operator<Rect>::op_nand, |
| 44 | op_and = region_operator<Rect>::op_and, |
| 45 | op_or = region_operator<Rect>::op_or, |
| 46 | op_xor = region_operator<Rect>::op_xor |
| 47 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | // ---------------------------------------------------------------------------- |
| 50 | |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 51 | Region::Region() { |
| 52 | mStorage.add(Rect(0,0)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | Region::Region(const Region& rhs) |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 56 | : mStorage(rhs.mStorage) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | { |
Mathias Agopian | d0b55c0 | 2011-03-16 23:18:07 -0700 | [diff] [blame] | 58 | #if VALIDATE_REGIONS |
| 59 | validate(rhs, "rhs copy-ctor"); |
| 60 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 63 | Region::Region(const Rect& rhs) { |
| 64 | mStorage.add(rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Region::~Region() |
| 68 | { |
| 69 | } |
| 70 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | Region& Region::operator = (const Region& rhs) |
| 72 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 73 | #if VALIDATE_REGIONS |
Mathias Agopian | d0b55c0 | 2011-03-16 23:18:07 -0700 | [diff] [blame] | 74 | validate(*this, "this->operator="); |
| 75 | validate(rhs, "rhs.operator="); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 76 | #endif |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 77 | mStorage = rhs.mStorage; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | return *this; |
| 79 | } |
| 80 | |
Mathias Agopian | 9f96145 | 2009-06-29 18:46:37 -0700 | [diff] [blame] | 81 | Region& Region::makeBoundsSelf() |
| 82 | { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 83 | if (mStorage.size() >= 2) { |
| 84 | const Rect bounds(getBounds()); |
| 85 | mStorage.clear(); |
| 86 | mStorage.add(bounds); |
| 87 | } |
Mathias Agopian | 9f96145 | 2009-06-29 18:46:37 -0700 | [diff] [blame] | 88 | return *this; |
| 89 | } |
| 90 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | void Region::clear() |
| 92 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 93 | mStorage.clear(); |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 94 | mStorage.add(Rect(0,0)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void Region::set(const Rect& r) |
| 98 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 99 | mStorage.clear(); |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 100 | mStorage.add(r); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 103 | void Region::set(uint32_t w, uint32_t h) |
| 104 | { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 105 | mStorage.clear(); |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 106 | mStorage.add(Rect(w,h)); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 107 | } |
| 108 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | // ---------------------------------------------------------------------------- |
| 110 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 111 | 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] | 112 | { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 113 | Rect rect(l,t,r,b); |
| 114 | size_t where = mStorage.size() - 1; |
| 115 | mStorage.insertAt(rect, where, 1); |
| 116 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 117 | #if VALIDATE_REGIONS |
| 118 | validate(*this, "addRectUnchecked"); |
| 119 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 122 | // ---------------------------------------------------------------------------- |
| 123 | |
| 124 | Region& Region::orSelf(const Rect& r) { |
| 125 | return operationSelf(r, op_or); |
| 126 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 127 | Region& Region::xorSelf(const Rect& r) { |
| 128 | return operationSelf(r, op_xor); |
| 129 | } |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 130 | Region& Region::andSelf(const Rect& r) { |
| 131 | return operationSelf(r, op_and); |
| 132 | } |
| 133 | Region& Region::subtractSelf(const Rect& r) { |
| 134 | return operationSelf(r, op_nand); |
| 135 | } |
| 136 | Region& Region::operationSelf(const Rect& r, int op) { |
| 137 | Region lhs(*this); |
| 138 | boolean_operation(op, *this, lhs, r); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | // ---------------------------------------------------------------------------- |
| 143 | |
| 144 | Region& Region::orSelf(const Region& rhs) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 145 | return operationSelf(rhs, op_or); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 147 | Region& Region::xorSelf(const Region& rhs) { |
| 148 | return operationSelf(rhs, op_xor); |
| 149 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | Region& Region::andSelf(const Region& rhs) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 151 | return operationSelf(rhs, op_and); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | Region& Region::subtractSelf(const Region& rhs) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 154 | return operationSelf(rhs, op_nand); |
| 155 | } |
| 156 | Region& Region::operationSelf(const Region& rhs, int op) { |
| 157 | Region lhs(*this); |
| 158 | boolean_operation(op, *this, lhs, rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | return *this; |
| 160 | } |
| 161 | |
| 162 | Region& Region::translateSelf(int x, int y) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 163 | if (x|y) translate(*this, x, y); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | return *this; |
| 165 | } |
| 166 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 167 | // ---------------------------------------------------------------------------- |
| 168 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 169 | const Region Region::merge(const Rect& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 170 | return operation(rhs, op_or); |
| 171 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 172 | const Region Region::mergeExclusive(const Rect& rhs) const { |
| 173 | return operation(rhs, op_xor); |
| 174 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 175 | const Region Region::intersect(const Rect& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 176 | return operation(rhs, op_and); |
| 177 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 178 | const Region Region::subtract(const Rect& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 179 | return operation(rhs, op_nand); |
| 180 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 181 | const Region Region::operation(const Rect& rhs, int op) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 182 | Region result; |
| 183 | boolean_operation(op, result, *this, rhs); |
| 184 | return result; |
| 185 | } |
| 186 | |
| 187 | // ---------------------------------------------------------------------------- |
| 188 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 189 | const Region Region::merge(const Region& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 190 | return operation(rhs, op_or); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 192 | const Region Region::mergeExclusive(const Region& rhs) const { |
| 193 | return operation(rhs, op_xor); |
| 194 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 195 | const Region Region::intersect(const Region& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 196 | return operation(rhs, op_and); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 198 | const Region Region::subtract(const Region& rhs) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 199 | return operation(rhs, op_nand); |
| 200 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 201 | 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] | 202 | Region result; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 203 | boolean_operation(op, result, *this, rhs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | return result; |
| 205 | } |
| 206 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 207 | const Region Region::translate(int x, int y) const { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | Region result; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 209 | translate(result, *this, x, y); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | return result; |
| 211 | } |
| 212 | |
| 213 | // ---------------------------------------------------------------------------- |
| 214 | |
| 215 | Region& Region::orSelf(const Region& rhs, int dx, int dy) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 216 | return operationSelf(rhs, dx, dy, op_or); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 218 | Region& Region::xorSelf(const Region& rhs, int dx, int dy) { |
| 219 | return operationSelf(rhs, dx, dy, op_xor); |
| 220 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | Region& Region::andSelf(const Region& rhs, int dx, int dy) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 222 | return operationSelf(rhs, dx, dy, op_and); |
| 223 | } |
| 224 | Region& Region::subtractSelf(const Region& rhs, int dx, int dy) { |
| 225 | return operationSelf(rhs, dx, dy, op_nand); |
| 226 | } |
| 227 | Region& Region::operationSelf(const Region& rhs, int dx, int dy, int op) { |
| 228 | Region lhs(*this); |
| 229 | boolean_operation(op, *this, lhs, rhs, dx, dy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | return *this; |
| 231 | } |
| 232 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 233 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 235 | const Region Region::merge(const Region& rhs, int dx, int dy) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 236 | return operation(rhs, dx, dy, op_or); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 238 | const Region Region::mergeExclusive(const Region& rhs, int dx, int dy) const { |
| 239 | return operation(rhs, dx, dy, op_xor); |
| 240 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 241 | const Region Region::intersect(const Region& rhs, int dx, int dy) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 242 | return operation(rhs, dx, dy, op_and); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 244 | const Region Region::subtract(const Region& rhs, int dx, int dy) const { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 245 | return operation(rhs, dx, dy, op_nand); |
| 246 | } |
Mathias Agopian | bed9dd1 | 2009-05-27 17:01:58 -0700 | [diff] [blame] | 247 | 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] | 248 | Region result; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 249 | boolean_operation(op, result, *this, rhs, dx, dy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | return result; |
| 251 | } |
| 252 | |
| 253 | // ---------------------------------------------------------------------------- |
| 254 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 255 | // This is our region rasterizer, which merges rects and spans together |
| 256 | // to obtain an optimal region. |
| 257 | class Region::rasterizer : public region_operator<Rect>::region_rasterizer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 259 | Rect bounds; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 260 | Vector<Rect>& storage; |
| 261 | Rect* head; |
| 262 | Rect* tail; |
| 263 | Vector<Rect> span; |
| 264 | Rect* cur; |
| 265 | public: |
| 266 | rasterizer(Region& reg) |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 267 | : 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] | 268 | storage.clear(); |
| 269 | } |
| 270 | |
| 271 | ~rasterizer() { |
| 272 | if (span.size()) { |
| 273 | flushSpan(); |
| 274 | } |
| 275 | if (storage.size()) { |
| 276 | bounds.top = storage.itemAt(0).top; |
| 277 | bounds.bottom = storage.top().bottom; |
| 278 | if (storage.size() == 1) { |
| 279 | storage.clear(); |
| 280 | } |
| 281 | } else { |
| 282 | bounds.left = 0; |
| 283 | bounds.right = 0; |
| 284 | } |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 285 | storage.add(bounds); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | virtual void operator()(const Rect& rect) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 289 | //ALOGD(">>> %3d, %3d, %3d, %3d", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 290 | // rect.left, rect.top, rect.right, rect.bottom); |
| 291 | if (span.size()) { |
| 292 | if (cur->top != rect.top) { |
| 293 | flushSpan(); |
| 294 | } else if (cur->right == rect.left) { |
| 295 | cur->right = rect.right; |
| 296 | return; |
| 297 | } |
| 298 | } |
| 299 | span.add(rect); |
| 300 | cur = span.editArray() + (span.size() - 1); |
| 301 | } |
| 302 | private: |
| 303 | template<typename T> |
| 304 | static inline T min(T rhs, T lhs) { return rhs < lhs ? rhs : lhs; } |
| 305 | template<typename T> |
| 306 | static inline T max(T rhs, T lhs) { return rhs > lhs ? rhs : lhs; } |
| 307 | void flushSpan() { |
| 308 | bool merge = false; |
| 309 | if (tail-head == ssize_t(span.size())) { |
Romain Guy | b801624 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 310 | Rect const* p = span.editArray(); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 311 | Rect const* q = head; |
| 312 | if (p->top == q->bottom) { |
| 313 | merge = true; |
| 314 | while (q != tail) { |
| 315 | if ((p->left != q->left) || (p->right != q->right)) { |
| 316 | merge = false; |
| 317 | break; |
| 318 | } |
| 319 | p++, q++; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | if (merge) { |
| 324 | const int bottom = span[0].bottom; |
| 325 | Rect* r = head; |
| 326 | while (r != tail) { |
| 327 | r->bottom = bottom; |
| 328 | r++; |
| 329 | } |
| 330 | } else { |
| 331 | bounds.left = min(span.itemAt(0).left, bounds.left); |
| 332 | bounds.right = max(span.top().right, bounds.right); |
| 333 | storage.appendVector(span); |
| 334 | tail = storage.editArray() + storage.size(); |
| 335 | head = tail - span.size(); |
| 336 | } |
| 337 | span.clear(); |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | bool Region::validate(const Region& reg, const char* name) |
| 342 | { |
| 343 | bool result = true; |
| 344 | const_iterator cur = reg.begin(); |
| 345 | const_iterator const tail = reg.end(); |
| 346 | const_iterator prev = cur++; |
| 347 | Rect b(*prev); |
| 348 | while (cur != tail) { |
| 349 | b.left = b.left < cur->left ? b.left : cur->left; |
| 350 | b.top = b.top < cur->top ? b.top : cur->top; |
| 351 | b.right = b.right > cur->right ? b.right : cur->right; |
| 352 | b.bottom = b.bottom > cur->bottom ? b.bottom : cur->bottom; |
| 353 | if (cur->top == prev->top) { |
| 354 | if (cur->bottom != prev->bottom) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 355 | ALOGE("%s: invalid span %p", name, cur); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 356 | result = false; |
| 357 | } else if (cur->left < prev->right) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 358 | ALOGE("%s: spans overlap horizontally prev=%p, cur=%p", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 359 | name, prev, cur); |
| 360 | result = false; |
| 361 | } |
| 362 | } else if (cur->top < prev->bottom) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 363 | ALOGE("%s: spans overlap vertically prev=%p, cur=%p", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 364 | name, prev, cur); |
| 365 | result = false; |
| 366 | } |
| 367 | prev = cur; |
| 368 | cur++; |
| 369 | } |
| 370 | if (b != reg.getBounds()) { |
| 371 | result = false; |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 372 | ALOGE("%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] | 373 | b.left, b.top, b.right, b.bottom, |
| 374 | reg.getBounds().left, reg.getBounds().top, |
| 375 | reg.getBounds().right, reg.getBounds().bottom); |
| 376 | } |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 377 | if (reg.mStorage.size() == 2) { |
| 378 | ALOGE("mStorage size is 2, which is never valid"); |
| 379 | } |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 380 | if (result == false) { |
| 381 | reg.dump(name); |
| 382 | } |
| 383 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 386 | void Region::boolean_operation(int op, Region& dst, |
| 387 | const Region& lhs, |
| 388 | const Region& rhs, int dx, int dy) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | { |
Mathias Agopian | d0b55c0 | 2011-03-16 23:18:07 -0700 | [diff] [blame] | 390 | #if VALIDATE_REGIONS |
| 391 | validate(lhs, "boolean_operation (before): lhs"); |
| 392 | validate(rhs, "boolean_operation (before): rhs"); |
| 393 | validate(dst, "boolean_operation (before): dst"); |
| 394 | #endif |
| 395 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 396 | size_t lhs_count; |
| 397 | Rect const * const lhs_rects = lhs.getArray(&lhs_count); |
| 398 | |
| 399 | size_t rhs_count; |
| 400 | Rect const * const rhs_rects = rhs.getArray(&rhs_count); |
| 401 | |
| 402 | region_operator<Rect>::region lhs_region(lhs_rects, lhs_count); |
| 403 | region_operator<Rect>::region rhs_region(rhs_rects, rhs_count, dx, dy); |
| 404 | region_operator<Rect> operation(op, lhs_region, rhs_region); |
| 405 | { // scope for rasterizer (dtor has side effects) |
| 406 | rasterizer r(dst); |
| 407 | operation(r); |
| 408 | } |
| 409 | |
| 410 | #if VALIDATE_REGIONS |
| 411 | validate(lhs, "boolean_operation: lhs"); |
| 412 | validate(rhs, "boolean_operation: rhs"); |
| 413 | validate(dst, "boolean_operation: dst"); |
| 414 | #endif |
| 415 | |
| 416 | #if VALIDATE_WITH_CORECG |
| 417 | SkRegion sk_lhs; |
| 418 | SkRegion sk_rhs; |
| 419 | SkRegion sk_dst; |
| 420 | |
| 421 | for (size_t i=0 ; i<lhs_count ; i++) |
| 422 | sk_lhs.op( |
| 423 | lhs_rects[i].left + dx, |
| 424 | lhs_rects[i].top + dy, |
| 425 | lhs_rects[i].right + dx, |
| 426 | lhs_rects[i].bottom + dy, |
| 427 | SkRegion::kUnion_Op); |
| 428 | |
| 429 | for (size_t i=0 ; i<rhs_count ; i++) |
| 430 | sk_rhs.op( |
| 431 | rhs_rects[i].left + dx, |
| 432 | rhs_rects[i].top + dy, |
| 433 | rhs_rects[i].right + dx, |
| 434 | rhs_rects[i].bottom + dy, |
| 435 | SkRegion::kUnion_Op); |
| 436 | |
| 437 | const char* name = "---"; |
| 438 | SkRegion::Op sk_op; |
| 439 | switch (op) { |
| 440 | case op_or: sk_op = SkRegion::kUnion_Op; name="OR"; break; |
Romain Guy | b8a2e98 | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 441 | case op_xor: sk_op = SkRegion::kUnion_XOR; name="XOR"; break; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 442 | case op_and: sk_op = SkRegion::kIntersect_Op; name="AND"; break; |
| 443 | case op_nand: sk_op = SkRegion::kDifference_Op; name="NAND"; break; |
| 444 | } |
| 445 | sk_dst.op(sk_lhs, sk_rhs, sk_op); |
| 446 | |
| 447 | if (sk_dst.isEmpty() && dst.isEmpty()) |
| 448 | return; |
| 449 | |
| 450 | bool same = true; |
| 451 | Region::const_iterator head = dst.begin(); |
| 452 | Region::const_iterator const tail = dst.end(); |
| 453 | SkRegion::Iterator it(sk_dst); |
| 454 | while (!it.done()) { |
| 455 | if (head != tail) { |
| 456 | if ( |
| 457 | head->left != it.rect().fLeft || |
| 458 | head->top != it.rect().fTop || |
| 459 | head->right != it.rect().fRight || |
| 460 | head->bottom != it.rect().fBottom |
| 461 | ) { |
| 462 | same = false; |
| 463 | break; |
| 464 | } |
| 465 | } else { |
| 466 | same = false; |
| 467 | break; |
| 468 | } |
| 469 | head++; |
| 470 | it.next(); |
| 471 | } |
| 472 | |
| 473 | if (head != tail) { |
| 474 | same = false; |
| 475 | } |
| 476 | |
| 477 | if(!same) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 478 | ALOGD("---\nregion boolean %s failed", name); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 479 | lhs.dump("lhs"); |
| 480 | rhs.dump("rhs"); |
| 481 | dst.dump("dst"); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 482 | ALOGD("should be"); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 483 | SkRegion::Iterator it(sk_dst); |
| 484 | while (!it.done()) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 485 | ALOGD(" [%3d, %3d, %3d, %3d]", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 486 | it.rect().fLeft, |
| 487 | it.rect().fTop, |
| 488 | it.rect().fRight, |
| 489 | it.rect().fBottom); |
| 490 | it.next(); |
| 491 | } |
| 492 | } |
| 493 | #endif |
| 494 | } |
| 495 | |
| 496 | void Region::boolean_operation(int op, Region& dst, |
| 497 | const Region& lhs, |
| 498 | const Rect& rhs, int dx, int dy) |
| 499 | { |
Mathias Agopian | 0450452 | 2011-09-19 16:12:08 -0700 | [diff] [blame] | 500 | if (!rhs.isValid()) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 501 | ALOGE("Region::boolean_operation(op=%d) invalid Rect={%d,%d,%d,%d}", |
Mathias Agopian | 0450452 | 2011-09-19 16:12:08 -0700 | [diff] [blame] | 502 | op, rhs.left, rhs.top, rhs.right, rhs.bottom); |
Mathias Agopian | 0857c8f | 2011-09-26 15:58:20 -0700 | [diff] [blame] | 503 | return; |
Mathias Agopian | 0450452 | 2011-09-19 16:12:08 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 506 | #if VALIDATE_WITH_CORECG || VALIDATE_REGIONS |
| 507 | boolean_operation(op, dst, lhs, Region(rhs), dx, dy); |
| 508 | #else |
| 509 | size_t lhs_count; |
| 510 | Rect const * const lhs_rects = lhs.getArray(&lhs_count); |
| 511 | |
| 512 | region_operator<Rect>::region lhs_region(lhs_rects, lhs_count); |
| 513 | region_operator<Rect>::region rhs_region(&rhs, 1, dx, dy); |
| 514 | region_operator<Rect> operation(op, lhs_region, rhs_region); |
| 515 | { // scope for rasterizer (dtor has side effects) |
| 516 | rasterizer r(dst); |
| 517 | operation(r); |
| 518 | } |
| 519 | |
| 520 | #endif |
| 521 | } |
| 522 | |
| 523 | void Region::boolean_operation(int op, Region& dst, |
| 524 | const Region& lhs, const Region& rhs) |
| 525 | { |
| 526 | boolean_operation(op, dst, lhs, rhs, 0, 0); |
| 527 | } |
| 528 | |
| 529 | void Region::boolean_operation(int op, Region& dst, |
| 530 | const Region& lhs, const Rect& rhs) |
| 531 | { |
| 532 | boolean_operation(op, dst, lhs, rhs, 0, 0); |
| 533 | } |
| 534 | |
| 535 | void Region::translate(Region& reg, int dx, int dy) |
| 536 | { |
Mathias Agopian | 4c0a170 | 2012-08-31 12:45:33 -0700 | [diff] [blame] | 537 | if ((dx || dy) && !reg.isEmpty()) { |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 538 | #if VALIDATE_REGIONS |
| 539 | validate(reg, "translate (before)"); |
| 540 | #endif |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 541 | size_t count = reg.mStorage.size(); |
| 542 | Rect* rects = reg.mStorage.editArray(); |
| 543 | while (count) { |
| 544 | rects->translate(dx, dy); |
| 545 | rects++; |
| 546 | count--; |
| 547 | } |
| 548 | #if VALIDATE_REGIONS |
| 549 | validate(reg, "translate (after)"); |
| 550 | #endif |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void Region::translate(Region& dst, const Region& reg, int dx, int dy) |
| 555 | { |
| 556 | dst = reg; |
| 557 | translate(dst, dx, dy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | // ---------------------------------------------------------------------------- |
| 561 | |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 562 | size_t Region::getSize() const { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 563 | return mStorage.size() * sizeof(Rect); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | status_t Region::flatten(void* buffer) const { |
| 567 | Rect* rects = reinterpret_cast<Rect*>(buffer); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 568 | memcpy(rects, mStorage.array(), mStorage.size() * sizeof(Rect)); |
| 569 | return NO_ERROR; |
| 570 | } |
| 571 | |
| 572 | status_t Region::unflatten(void const* buffer, size_t size) { |
| 573 | mStorage.clear(); |
| 574 | if (size >= sizeof(Rect)) { |
| 575 | Rect const* rects = reinterpret_cast<Rect const*>(buffer); |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 576 | size_t count = size / sizeof(Rect); |
| 577 | if (count > 0) { |
| 578 | ssize_t err = mStorage.insertAt(0, count); |
| 579 | if (err < 0) { |
| 580 | return status_t(err); |
| 581 | } |
| 582 | memcpy(mStorage.editArray(), rects, count*sizeof(Rect)); |
Mathias Agopian | b612142 | 2010-02-17 20:22:26 -0800 | [diff] [blame] | 583 | } |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 584 | } |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 585 | #if VALIDATE_REGIONS |
| 586 | validate(*this, "Region::unflatten"); |
| 587 | #endif |
Mathias Agopian | 8683fca | 2012-08-12 19:37:16 -0700 | [diff] [blame] | 588 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 591 | // ---------------------------------------------------------------------------- |
| 592 | |
| 593 | Region::const_iterator Region::begin() const { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 594 | return mStorage.array(); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | Region::const_iterator Region::end() const { |
Mathias Agopian | 3ab6855 | 2012-08-31 14:31:40 -0700 | [diff] [blame] | 598 | size_t numRects = isRect() ? 1 : mStorage.size() - 1; |
| 599 | return mStorage.array() + numRects; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | Rect const* Region::getArray(size_t* count) const { |
| 603 | const_iterator const b(begin()); |
| 604 | const_iterator const e(end()); |
| 605 | if (count) *count = e-b; |
| 606 | return b; |
| 607 | } |
| 608 | |
Mathias Agopian | 2401ead | 2012-08-31 15:41:24 -0700 | [diff] [blame] | 609 | SharedBuffer const* Region::getSharedBuffer(size_t* count) const { |
| 610 | // We can get to the SharedBuffer of a Vector<Rect> because Rect has |
| 611 | // a trivial destructor. |
| 612 | SharedBuffer const* sb = SharedBuffer::bufferFromData(mStorage.array()); |
| 613 | if (count) { |
| 614 | size_t numRects = isRect() ? 1 : mStorage.size() - 1; |
| 615 | count[0] = numRects; |
| 616 | } |
| 617 | sb->acquire(); |
| 618 | return sb; |
| 619 | } |
| 620 | |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 621 | // ---------------------------------------------------------------------------- |
| 622 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | void Region::dump(String8& out, const char* what, uint32_t flags) const |
| 624 | { |
| 625 | (void)flags; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 626 | const_iterator head = begin(); |
| 627 | const_iterator const tail = end(); |
| 628 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | size_t SIZE = 256; |
| 630 | char buffer[SIZE]; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 631 | |
| 632 | snprintf(buffer, SIZE, " Region %s (this=%p, count=%d)\n", |
| 633 | what, this, tail-head); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 634 | out.append(buffer); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 635 | while (head != tail) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | snprintf(buffer, SIZE, " [%3d, %3d, %3d, %3d]\n", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 637 | head->left, head->top, head->right, head->bottom); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | out.append(buffer); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 639 | head++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | void Region::dump(const char* what, uint32_t flags) const |
| 644 | { |
| 645 | (void)flags; |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 646 | const_iterator head = begin(); |
| 647 | const_iterator const tail = end(); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 648 | ALOGD(" Region %s (this=%p, count=%d)\n", what, this, tail-head); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 649 | while (head != tail) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 650 | ALOGD(" [%3d, %3d, %3d, %3d]\n", |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 651 | head->left, head->top, head->right, head->bottom); |
| 652 | head++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | // ---------------------------------------------------------------------------- |
| 657 | |
| 658 | }; // namespace android |