Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #include "ClipArea.h" |
| 17 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 18 | #include "utils/LinearAllocator.h" |
| 19 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 20 | #include <SkPath.h> |
| 21 | #include <limits> |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 22 | #include <type_traits> |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 27 | static void handlePoint(Rect& transformedBounds, const Matrix4& transform, float x, float y) { |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 28 | Vertex v = {x, y}; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 29 | transform.mapPoint(v.x, v.y); |
Chris Craik | 15c3f19 | 2015-12-03 12:16:56 -0800 | [diff] [blame] | 30 | transformedBounds.expandToCover(v.x, v.y); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | Rect transformAndCalculateBounds(const Rect& r, const Matrix4& transform) { |
| 34 | const float kMinFloat = std::numeric_limits<float>::lowest(); |
| 35 | const float kMaxFloat = std::numeric_limits<float>::max(); |
| 36 | Rect transformedBounds = { kMaxFloat, kMaxFloat, kMinFloat, kMinFloat }; |
| 37 | handlePoint(transformedBounds, transform, r.left, r.top); |
| 38 | handlePoint(transformedBounds, transform, r.right, r.top); |
| 39 | handlePoint(transformedBounds, transform, r.left, r.bottom); |
| 40 | handlePoint(transformedBounds, transform, r.right, r.bottom); |
| 41 | return transformedBounds; |
| 42 | } |
| 43 | |
Chris Craik | 0280628 | 2016-03-11 19:16:21 -0800 | [diff] [blame] | 44 | void ClipBase::dump() const { |
| 45 | ALOGD("mode %d" RECT_STRING, mode, RECT_ARGS(rect)); |
| 46 | } |
| 47 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 48 | /* |
| 49 | * TransformedRectangle |
| 50 | */ |
| 51 | |
| 52 | TransformedRectangle::TransformedRectangle() { |
| 53 | } |
| 54 | |
| 55 | TransformedRectangle::TransformedRectangle(const Rect& bounds, |
| 56 | const Matrix4& transform) |
| 57 | : mBounds(bounds) |
| 58 | , mTransform(transform) { |
| 59 | } |
| 60 | |
| 61 | bool TransformedRectangle::canSimplyIntersectWith( |
| 62 | const TransformedRectangle& other) const { |
| 63 | |
| 64 | return mTransform == other.mTransform; |
| 65 | } |
| 66 | |
Chris Craik | ac02eb9 | 2015-10-05 12:23:46 -0700 | [diff] [blame] | 67 | void TransformedRectangle::intersectWith(const TransformedRectangle& other) { |
| 68 | mBounds.doIntersect(other.mBounds); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool TransformedRectangle::isEmpty() const { |
| 72 | return mBounds.isEmpty(); |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * RectangleList |
| 77 | */ |
| 78 | |
| 79 | RectangleList::RectangleList() |
| 80 | : mTransformedRectanglesCount(0) { |
| 81 | } |
| 82 | |
| 83 | bool RectangleList::isEmpty() const { |
| 84 | if (mTransformedRectanglesCount < 1) { |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | for (int i = 0; i < mTransformedRectanglesCount; i++) { |
| 89 | if (mTransformedRectangles[i].isEmpty()) { |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | int RectangleList::getTransformedRectanglesCount() const { |
| 97 | return mTransformedRectanglesCount; |
| 98 | } |
| 99 | |
| 100 | const TransformedRectangle& RectangleList::getTransformedRectangle(int i) const { |
| 101 | return mTransformedRectangles[i]; |
| 102 | } |
| 103 | |
| 104 | void RectangleList::setEmpty() { |
| 105 | mTransformedRectanglesCount = 0; |
| 106 | } |
| 107 | |
| 108 | void RectangleList::set(const Rect& bounds, const Matrix4& transform) { |
| 109 | mTransformedRectanglesCount = 1; |
| 110 | mTransformedRectangles[0] = TransformedRectangle(bounds, transform); |
| 111 | } |
| 112 | |
| 113 | bool RectangleList::intersectWith(const Rect& bounds, |
| 114 | const Matrix4& transform) { |
| 115 | TransformedRectangle newRectangle(bounds, transform); |
| 116 | |
| 117 | // Try to find a rectangle with a compatible transformation |
| 118 | int index = 0; |
| 119 | for (; index < mTransformedRectanglesCount; index++) { |
| 120 | TransformedRectangle& tr(mTransformedRectangles[index]); |
| 121 | if (tr.canSimplyIntersectWith(newRectangle)) { |
| 122 | tr.intersectWith(newRectangle); |
| 123 | return true; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Add it to the list if there is room |
| 128 | if (index < kMaxTransformedRectangles) { |
| 129 | mTransformedRectangles[index] = newRectangle; |
| 130 | mTransformedRectanglesCount += 1; |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | // This rectangle list is full |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | Rect RectangleList::calculateBounds() const { |
| 139 | Rect bounds; |
| 140 | for (int index = 0; index < mTransformedRectanglesCount; index++) { |
| 141 | const TransformedRectangle& tr(mTransformedRectangles[index]); |
| 142 | if (index == 0) { |
| 143 | bounds = tr.transformedBounds(); |
| 144 | } else { |
Chris Craik | ac02eb9 | 2015-10-05 12:23:46 -0700 | [diff] [blame] | 145 | bounds.doIntersect(tr.transformedBounds()); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | return bounds; |
| 149 | } |
| 150 | |
| 151 | static SkPath pathFromTransformedRectangle(const Rect& bounds, |
| 152 | const Matrix4& transform) { |
| 153 | SkPath rectPath; |
| 154 | SkPath rectPathTransformed; |
| 155 | rectPath.addRect(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 156 | SkMatrix skTransform; |
| 157 | transform.copyTo(skTransform); |
| 158 | rectPath.transform(skTransform, &rectPathTransformed); |
| 159 | return rectPathTransformed; |
| 160 | } |
| 161 | |
| 162 | SkRegion RectangleList::convertToRegion(const SkRegion& clip) const { |
| 163 | SkRegion rectangleListAsRegion; |
| 164 | for (int index = 0; index < mTransformedRectanglesCount; index++) { |
| 165 | const TransformedRectangle& tr(mTransformedRectangles[index]); |
| 166 | SkPath rectPathTransformed = pathFromTransformedRectangle( |
| 167 | tr.getBounds(), tr.getTransform()); |
| 168 | if (index == 0) { |
| 169 | rectangleListAsRegion.setPath(rectPathTransformed, clip); |
| 170 | } else { |
| 171 | SkRegion rectRegion; |
| 172 | rectRegion.setPath(rectPathTransformed, clip); |
| 173 | rectangleListAsRegion.op(rectRegion, SkRegion::kIntersect_Op); |
| 174 | } |
| 175 | } |
| 176 | return rectangleListAsRegion; |
| 177 | } |
| 178 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 179 | void RectangleList::transform(const Matrix4& transform) { |
| 180 | for (int index = 0; index < mTransformedRectanglesCount; index++) { |
| 181 | mTransformedRectangles[index].transform(transform); |
| 182 | } |
| 183 | } |
| 184 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 185 | /* |
| 186 | * ClipArea |
| 187 | */ |
| 188 | |
| 189 | ClipArea::ClipArea() |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 190 | : mMode(ClipMode::Rectangle) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Interface |
| 195 | */ |
| 196 | |
| 197 | void ClipArea::setViewportDimensions(int width, int height) { |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 198 | mPostViewportClipObserved = false; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 199 | mViewportBounds.set(0, 0, width, height); |
| 200 | mClipRect = mViewportBounds; |
| 201 | } |
| 202 | |
| 203 | void ClipArea::setEmpty() { |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 204 | onClipUpdated(); |
| 205 | mMode = ClipMode::Rectangle; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 206 | mClipRect.setEmpty(); |
| 207 | mClipRegion.setEmpty(); |
| 208 | mRectangleList.setEmpty(); |
| 209 | } |
| 210 | |
| 211 | void ClipArea::setClip(float left, float top, float right, float bottom) { |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 212 | onClipUpdated(); |
| 213 | mMode = ClipMode::Rectangle; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 214 | mClipRect.set(left, top, right, bottom); |
| 215 | mClipRegion.setEmpty(); |
| 216 | } |
| 217 | |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 218 | void ClipArea::clipRectWithTransform(const Rect& r, const mat4* transform, |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 219 | SkRegion::Op op) { |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 220 | if (op == SkRegion::kReplace_Op) mReplaceOpObserved = true; |
Chris Craik | 7fc1b03 | 2016-02-03 19:45:06 -0800 | [diff] [blame] | 221 | if (!mPostViewportClipObserved && op == SkRegion::kIntersect_Op) op = SkRegion::kReplace_Op; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 222 | onClipUpdated(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 223 | switch (mMode) { |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 224 | case ClipMode::Rectangle: |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 225 | rectangleModeClipRectWithTransform(r, transform, op); |
| 226 | break; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 227 | case ClipMode::RectangleList: |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 228 | rectangleListModeClipRectWithTransform(r, transform, op); |
| 229 | break; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 230 | case ClipMode::Region: |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 231 | regionModeClipRectWithTransform(r, transform, op); |
| 232 | break; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 233 | } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 236 | void ClipArea::clipRegion(const SkRegion& region, SkRegion::Op op) { |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 237 | if (op == SkRegion::kReplace_Op) mReplaceOpObserved = true; |
Chris Craik | 7fc1b03 | 2016-02-03 19:45:06 -0800 | [diff] [blame] | 238 | if (!mPostViewportClipObserved && op == SkRegion::kIntersect_Op) op = SkRegion::kReplace_Op; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 239 | onClipUpdated(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 240 | enterRegionMode(); |
| 241 | mClipRegion.op(region, op); |
Tom Hudson | e30b53c | 2015-03-30 15:59:02 -0400 | [diff] [blame] | 242 | onClipRegionUpdated(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 245 | void ClipArea::clipPathWithTransform(const SkPath& path, const mat4* transform, |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 246 | SkRegion::Op op) { |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 247 | if (op == SkRegion::kReplace_Op) mReplaceOpObserved = true; |
Chris Craik | 7fc1b03 | 2016-02-03 19:45:06 -0800 | [diff] [blame] | 248 | if (!mPostViewportClipObserved && op == SkRegion::kIntersect_Op) op = SkRegion::kReplace_Op; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 249 | onClipUpdated(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 250 | SkMatrix skTransform; |
| 251 | transform->copyTo(skTransform); |
| 252 | SkPath transformed; |
| 253 | path.transform(skTransform, &transformed); |
| 254 | SkRegion region; |
| 255 | regionFromPath(transformed, region); |
Chris Craik | bf27b99 | 2016-04-13 15:57:46 -0700 | [diff] [blame] | 256 | enterRegionMode(); |
| 257 | mClipRegion.op(region, op); |
| 258 | onClipRegionUpdated(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Rectangle mode |
| 263 | */ |
| 264 | |
| 265 | void ClipArea::enterRectangleMode() { |
| 266 | // Entering rectangle mode discards any |
| 267 | // existing clipping information from the other modes. |
| 268 | // The only way this occurs is by a clip setting operation. |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 269 | mMode = ClipMode::Rectangle; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 272 | void ClipArea::rectangleModeClipRectWithTransform(const Rect& r, |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 273 | const mat4* transform, SkRegion::Op op) { |
| 274 | |
Chris Craik | 8ce8f3f | 2015-07-16 13:07:45 -0700 | [diff] [blame] | 275 | if (op == SkRegion::kReplace_Op && transform->rectToRect()) { |
| 276 | mClipRect = r; |
| 277 | transform->mapRect(mClipRect); |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 278 | return; |
Chris Craik | 8ce8f3f | 2015-07-16 13:07:45 -0700 | [diff] [blame] | 279 | } else if (op != SkRegion::kIntersect_Op) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 280 | enterRegionMode(); |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 281 | regionModeClipRectWithTransform(r, transform, op); |
| 282 | return; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (transform->rectToRect()) { |
| 286 | Rect transformed(r); |
| 287 | transform->mapRect(transformed); |
Chris Craik | ac02eb9 | 2015-10-05 12:23:46 -0700 | [diff] [blame] | 288 | mClipRect.doIntersect(transformed); |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 289 | return; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | enterRectangleListMode(); |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 293 | rectangleListModeClipRectWithTransform(r, transform, op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 296 | /* |
| 297 | * RectangleList mode implementation |
| 298 | */ |
| 299 | |
| 300 | void ClipArea::enterRectangleListMode() { |
| 301 | // Is is only legal to enter rectangle list mode from |
| 302 | // rectangle mode, since rectangle list mode cannot represent |
| 303 | // all clip areas that can be represented by a region. |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 304 | ALOG_ASSERT(mMode == ClipMode::Rectangle); |
| 305 | mMode = ClipMode::RectangleList; |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 306 | mRectangleList.set(mClipRect, Matrix4::identity()); |
| 307 | } |
| 308 | |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 309 | void ClipArea::rectangleListModeClipRectWithTransform(const Rect& r, |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 310 | const mat4* transform, SkRegion::Op op) { |
| 311 | if (op != SkRegion::kIntersect_Op |
| 312 | || !mRectangleList.intersectWith(r, *transform)) { |
| 313 | enterRegionMode(); |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 314 | regionModeClipRectWithTransform(r, transform, op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 315 | } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 318 | /* |
| 319 | * Region mode implementation |
| 320 | */ |
| 321 | |
| 322 | void ClipArea::enterRegionMode() { |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 323 | ClipMode oldMode = mMode; |
| 324 | mMode = ClipMode::Region; |
| 325 | if (oldMode != ClipMode::Region) { |
| 326 | if (oldMode == ClipMode::Rectangle) { |
| 327 | mClipRegion.setRect(mClipRect.toSkIRect()); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 328 | } else { |
| 329 | mClipRegion = mRectangleList.convertToRegion(createViewportRegion()); |
Tom Hudson | e30b53c | 2015-03-30 15:59:02 -0400 | [diff] [blame] | 330 | onClipRegionUpdated(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 331 | } |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 335 | void ClipArea::regionModeClipRectWithTransform(const Rect& r, |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 336 | const mat4* transform, SkRegion::Op op) { |
| 337 | SkPath transformedRect = pathFromTransformedRectangle(r, *transform); |
| 338 | SkRegion transformedRectRegion; |
| 339 | regionFromPath(transformedRect, transformedRectRegion); |
| 340 | mClipRegion.op(transformedRectRegion, op); |
Tom Hudson | e30b53c | 2015-03-30 15:59:02 -0400 | [diff] [blame] | 341 | onClipRegionUpdated(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Tom Hudson | e30b53c | 2015-03-30 15:59:02 -0400 | [diff] [blame] | 344 | void ClipArea::onClipRegionUpdated() { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 345 | if (!mClipRegion.isEmpty()) { |
| 346 | mClipRect.set(mClipRegion.getBounds()); |
| 347 | |
| 348 | if (mClipRegion.isRect()) { |
| 349 | mClipRegion.setEmpty(); |
Tom Hudson | e30b53c | 2015-03-30 15:59:02 -0400 | [diff] [blame] | 350 | enterRectangleMode(); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 351 | } |
| 352 | } else { |
| 353 | mClipRect.setEmpty(); |
| 354 | } |
| 355 | } |
| 356 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 357 | /** |
| 358 | * Clip serialization |
| 359 | */ |
| 360 | |
| 361 | const ClipBase* ClipArea::serializeClip(LinearAllocator& allocator) { |
| 362 | if (!mPostViewportClipObserved) { |
| 363 | // Only initial clip-to-viewport observed, so no serialization of clip necessary |
| 364 | return nullptr; |
| 365 | } |
| 366 | |
| 367 | static_assert(std::is_trivially_destructible<Rect>::value, |
| 368 | "expect Rect to be trivially destructible"); |
| 369 | static_assert(std::is_trivially_destructible<RectangleList>::value, |
| 370 | "expect RectangleList to be trivially destructible"); |
| 371 | |
| 372 | if (mLastSerialization == nullptr) { |
Chris Craik | 4f4c608 | 2016-02-09 16:32:32 -0800 | [diff] [blame] | 373 | ClipBase* serialization = nullptr; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 374 | switch (mMode) { |
| 375 | case ClipMode::Rectangle: |
Chris Craik | b4f4f3e | 2016-02-08 17:27:04 -0800 | [diff] [blame] | 376 | serialization = allocator.create<ClipRect>(mClipRect); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 377 | break; |
| 378 | case ClipMode::RectangleList: |
Chris Craik | b4f4f3e | 2016-02-08 17:27:04 -0800 | [diff] [blame] | 379 | serialization = allocator.create<ClipRectList>(mRectangleList); |
| 380 | serialization->rect = mRectangleList.calculateBounds(); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 381 | break; |
| 382 | case ClipMode::Region: |
Chris Craik | b4f4f3e | 2016-02-08 17:27:04 -0800 | [diff] [blame] | 383 | serialization = allocator.create<ClipRegion>(mClipRegion); |
| 384 | serialization->rect.set(mClipRegion.getBounds()); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 385 | break; |
| 386 | } |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 387 | serialization->intersectWithRoot = mReplaceOpObserved; |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 388 | // TODO: this is only done for draw time, should eventually avoid for record time |
| 389 | serialization->rect.snapToPixelBoundaries(); |
Chris Craik | b4f4f3e | 2016-02-08 17:27:04 -0800 | [diff] [blame] | 390 | mLastSerialization = serialization; |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 391 | } |
| 392 | return mLastSerialization; |
| 393 | } |
| 394 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 395 | inline static const RectangleList& getRectList(const ClipBase* scb) { |
| 396 | return reinterpret_cast<const ClipRectList*>(scb)->rectList; |
| 397 | } |
| 398 | |
| 399 | inline static const SkRegion& getRegion(const ClipBase* scb) { |
| 400 | return reinterpret_cast<const ClipRegion*>(scb)->region; |
| 401 | } |
| 402 | |
| 403 | // Conservative check for too many rectangles to fit in rectangle list. |
| 404 | // For simplicity, doesn't account for rect merging |
| 405 | static bool cannotFitInRectangleList(const ClipArea& clipArea, const ClipBase* scb) { |
| 406 | int currentRectCount = clipArea.isRectangleList() |
| 407 | ? clipArea.getRectangleList().getTransformedRectanglesCount() |
| 408 | : 1; |
| 409 | int recordedRectCount = (scb->mode == ClipMode::RectangleList) |
| 410 | ? getRectList(scb).getTransformedRectanglesCount() |
| 411 | : 1; |
| 412 | return currentRectCount + recordedRectCount > RectangleList::kMaxTransformedRectangles; |
| 413 | } |
| 414 | |
Chris Craik | 261725f | 2016-02-29 12:52:33 -0800 | [diff] [blame] | 415 | static const ClipRect sEmptyClipRect(Rect(0, 0)); |
| 416 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 417 | const ClipBase* ClipArea::serializeIntersectedClip(LinearAllocator& allocator, |
| 418 | const ClipBase* recordedClip, const Matrix4& recordedClipTransform) { |
Chris Craik | 261725f | 2016-02-29 12:52:33 -0800 | [diff] [blame] | 419 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 420 | // if no recordedClip passed, just serialize current state |
| 421 | if (!recordedClip) return serializeClip(allocator); |
| 422 | |
Chris Craik | 261725f | 2016-02-29 12:52:33 -0800 | [diff] [blame] | 423 | // if either is empty, clip is empty |
| 424 | if (CC_UNLIKELY(recordedClip->rect.isEmpty())|| mClipRect.isEmpty()) return &sEmptyClipRect; |
| 425 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 426 | if (!mLastResolutionResult |
| 427 | || recordedClip != mLastResolutionClip |
| 428 | || recordedClipTransform != mLastResolutionTransform) { |
| 429 | mLastResolutionClip = recordedClip; |
| 430 | mLastResolutionTransform = recordedClipTransform; |
| 431 | |
| 432 | if (CC_LIKELY(mMode == ClipMode::Rectangle |
| 433 | && recordedClip->mode == ClipMode::Rectangle |
| 434 | && recordedClipTransform.rectToRect())) { |
| 435 | // common case - result is a single rectangle |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 436 | auto rectClip = allocator.create<ClipRect>(recordedClip->rect); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 437 | recordedClipTransform.mapRect(rectClip->rect); |
| 438 | rectClip->rect.doIntersect(mClipRect); |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 439 | rectClip->rect.snapToPixelBoundaries(); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 440 | mLastResolutionResult = rectClip; |
| 441 | } else if (CC_UNLIKELY(mMode == ClipMode::Region |
| 442 | || recordedClip->mode == ClipMode::Region |
| 443 | || cannotFitInRectangleList(*this, recordedClip))) { |
| 444 | // region case |
| 445 | SkRegion other; |
| 446 | switch (recordedClip->mode) { |
| 447 | case ClipMode::Rectangle: |
| 448 | if (CC_LIKELY(recordedClipTransform.rectToRect())) { |
| 449 | // simple transform, skip creating SkPath |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 450 | Rect resultClip(recordedClip->rect); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 451 | recordedClipTransform.mapRect(resultClip); |
| 452 | other.setRect(resultClip.toSkIRect()); |
| 453 | } else { |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 454 | SkPath transformedRect = pathFromTransformedRectangle(recordedClip->rect, |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 455 | recordedClipTransform); |
| 456 | other.setPath(transformedRect, createViewportRegion()); |
| 457 | } |
| 458 | break; |
| 459 | case ClipMode::RectangleList: { |
| 460 | RectangleList transformedList(getRectList(recordedClip)); |
| 461 | transformedList.transform(recordedClipTransform); |
| 462 | other = transformedList.convertToRegion(createViewportRegion()); |
| 463 | break; |
| 464 | } |
| 465 | case ClipMode::Region: |
| 466 | other = getRegion(recordedClip); |
Chris Craik | 82457c5 | 2016-06-29 16:22:27 -0700 | [diff] [blame] | 467 | applyTransformToRegion(recordedClipTransform, &other); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | ClipRegion* regionClip = allocator.create<ClipRegion>(); |
| 471 | switch (mMode) { |
| 472 | case ClipMode::Rectangle: |
| 473 | regionClip->region.op(mClipRect.toSkIRect(), other, SkRegion::kIntersect_Op); |
| 474 | break; |
| 475 | case ClipMode::RectangleList: |
| 476 | regionClip->region.op(mRectangleList.convertToRegion(createViewportRegion()), |
| 477 | other, SkRegion::kIntersect_Op); |
| 478 | break; |
| 479 | case ClipMode::Region: |
| 480 | regionClip->region.op(mClipRegion, other, SkRegion::kIntersect_Op); |
| 481 | break; |
| 482 | } |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 483 | // Don't need to snap, since region's in int bounds |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 484 | regionClip->rect.set(regionClip->region.getBounds()); |
| 485 | mLastResolutionResult = regionClip; |
| 486 | } else { |
| 487 | auto rectListClip = allocator.create<ClipRectList>(mRectangleList); |
| 488 | auto&& rectList = rectListClip->rectList; |
| 489 | if (mMode == ClipMode::Rectangle) { |
| 490 | rectList.set(mClipRect, Matrix4::identity()); |
| 491 | } |
| 492 | |
| 493 | if (recordedClip->mode == ClipMode::Rectangle) { |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 494 | rectList.intersectWith(recordedClip->rect, recordedClipTransform); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 495 | } else { |
| 496 | const RectangleList& other = getRectList(recordedClip); |
| 497 | for (int i = 0; i < other.getTransformedRectanglesCount(); i++) { |
| 498 | auto&& tr = other.getTransformedRectangle(i); |
| 499 | Matrix4 totalTransform(recordedClipTransform); |
| 500 | totalTransform.multiply(tr.getTransform()); |
| 501 | rectList.intersectWith(tr.getBounds(), totalTransform); |
| 502 | } |
| 503 | } |
| 504 | rectListClip->rect = rectList.calculateBounds(); |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 505 | rectListClip->rect.snapToPixelBoundaries(); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 506 | mLastResolutionResult = rectListClip; |
| 507 | } |
| 508 | } |
| 509 | return mLastResolutionResult; |
| 510 | } |
| 511 | |
| 512 | void ClipArea::applyClip(const ClipBase* clip, const Matrix4& transform) { |
| 513 | if (!clip) return; // nothing to do |
| 514 | |
| 515 | if (CC_LIKELY(clip->mode == ClipMode::Rectangle)) { |
Chris Craik | 69aeabe | 2016-03-03 12:58:39 -0800 | [diff] [blame] | 516 | clipRectWithTransform(clip->rect, &transform, SkRegion::kIntersect_Op); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 517 | } else if (CC_LIKELY(clip->mode == ClipMode::RectangleList)) { |
| 518 | auto&& rectList = getRectList(clip); |
| 519 | for (int i = 0; i < rectList.getTransformedRectanglesCount(); i++) { |
| 520 | auto&& tr = rectList.getTransformedRectangle(i); |
| 521 | Matrix4 totalTransform(transform); |
| 522 | totalTransform.multiply(tr.getTransform()); |
| 523 | clipRectWithTransform(tr.getBounds(), &totalTransform, SkRegion::kIntersect_Op); |
| 524 | } |
| 525 | } else { |
| 526 | SkRegion region(getRegion(clip)); |
Chris Craik | 82457c5 | 2016-06-29 16:22:27 -0700 | [diff] [blame] | 527 | applyTransformToRegion(transform, ®ion); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 528 | clipRegion(region, SkRegion::kIntersect_Op); |
| 529 | } |
| 530 | } |
| 531 | |
Chris Craik | 82457c5 | 2016-06-29 16:22:27 -0700 | [diff] [blame] | 532 | void ClipArea::applyTransformToRegion(const Matrix4& transform, SkRegion* region) { |
Chris Craik | 0658435 | 2016-07-14 14:24:16 -0700 | [diff] [blame] | 533 | if (transform.rectToRect() && !transform.isPureTranslate()) { |
Chris Craik | 82457c5 | 2016-06-29 16:22:27 -0700 | [diff] [blame] | 534 | // handle matrices with scale manually by mapping each rect |
| 535 | SkRegion other; |
| 536 | SkRegion::Iterator it(*region); |
| 537 | while (!it.done()) { |
| 538 | Rect rect(it.rect()); |
| 539 | transform.mapRect(rect); |
Chris Craik | 0658435 | 2016-07-14 14:24:16 -0700 | [diff] [blame] | 540 | rect.snapGeometryToPixelBoundaries(true); |
Chris Craik | 82457c5 | 2016-06-29 16:22:27 -0700 | [diff] [blame] | 541 | other.op(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kUnion_Op); |
| 542 | it.next(); |
| 543 | } |
| 544 | region->swap(other); |
| 545 | } else { |
| 546 | // TODO: handle non-translate transforms properly! |
| 547 | region->translate(transform.getTranslateX(), transform.getTranslateY()); |
| 548 | } |
| 549 | } |
| 550 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 551 | } /* namespace uirenderer */ |
| 552 | } /* namespace android */ |