Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 17 | #include "CanvasState.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 18 | #include "hwui/Canvas.h" |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 19 | #include "utils/MathUtils.h" |
| 20 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 24 | CanvasState::CanvasState(CanvasStateClient& renderer) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 25 | : mWidth(-1), mHeight(-1), mSaveCount(1), mCanvas(renderer), mSnapshot(&mFirstSnapshot) {} |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 26 | |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 27 | CanvasState::~CanvasState() { |
| 28 | // First call freeSnapshot on all but mFirstSnapshot |
| 29 | // to invoke all the dtors |
| 30 | freeAllSnapshots(); |
| 31 | |
| 32 | // Now actually release the memory |
| 33 | while (mSnapshotPool) { |
| 34 | void* temp = mSnapshotPool; |
| 35 | mSnapshotPool = mSnapshotPool->previous; |
| 36 | free(temp); |
| 37 | } |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 40 | void CanvasState::initializeRecordingSaveStack(int viewportWidth, int viewportHeight) { |
| 41 | if (mWidth != viewportWidth || mHeight != viewportHeight) { |
| 42 | mWidth = viewportWidth; |
| 43 | mHeight = viewportHeight; |
| 44 | mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight); |
| 45 | mCanvas.onViewportInitialized(); |
| 46 | } |
| 47 | |
| 48 | freeAllSnapshots(); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 49 | mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip); |
Chris Craik | e4db79d | 2015-12-22 16:32:23 -0800 | [diff] [blame] | 50 | mSnapshot->setRelativeLightCenter(Vector3()); |
| 51 | mSaveCount = 1; |
| 52 | } |
| 53 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 54 | void CanvasState::initializeSaveStack(int viewportWidth, int viewportHeight, float clipLeft, |
| 55 | float clipTop, float clipRight, float clipBottom, |
| 56 | const Vector3& lightCenter) { |
Chris Craik | 64e445b | 2015-09-02 14:23:49 -0700 | [diff] [blame] | 57 | if (mWidth != viewportWidth || mHeight != viewportHeight) { |
| 58 | mWidth = viewportWidth; |
| 59 | mHeight = viewportHeight; |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 60 | mFirstSnapshot.initializeViewport(viewportWidth, viewportHeight); |
Chris Craik | 64e445b | 2015-09-02 14:23:49 -0700 | [diff] [blame] | 61 | mCanvas.onViewportInitialized(); |
| 62 | } |
| 63 | |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 64 | freeAllSnapshots(); |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 65 | mSnapshot = allocSnapshot(&mFirstSnapshot, SaveFlags::MatrixClip); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 66 | mSnapshot->setClip(clipLeft, clipTop, clipRight, clipBottom); |
Chris Craik | 6b109c7 | 2015-02-27 10:55:28 -0800 | [diff] [blame] | 67 | mSnapshot->fbo = mCanvas.getTargetFbo(); |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 68 | mSnapshot->setRelativeLightCenter(lightCenter); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 69 | mSaveCount = 1; |
| 70 | } |
| 71 | |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 72 | Snapshot* CanvasState::allocSnapshot(Snapshot* previous, int savecount) { |
| 73 | void* memory; |
| 74 | if (mSnapshotPool) { |
| 75 | memory = mSnapshotPool; |
| 76 | mSnapshotPool = mSnapshotPool->previous; |
| 77 | mSnapshotPoolCount--; |
| 78 | } else { |
| 79 | memory = malloc(sizeof(Snapshot)); |
| 80 | } |
| 81 | return new (memory) Snapshot(previous, savecount); |
| 82 | } |
| 83 | |
| 84 | void CanvasState::freeSnapshot(Snapshot* snapshot) { |
| 85 | snapshot->~Snapshot(); |
| 86 | // Arbitrary number, just don't let this grown unbounded |
| 87 | if (mSnapshotPoolCount > 10) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 88 | free((void*)snapshot); |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 89 | } else { |
| 90 | snapshot->previous = mSnapshotPool; |
| 91 | mSnapshotPool = snapshot; |
| 92 | mSnapshotPoolCount++; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void CanvasState::freeAllSnapshots() { |
| 97 | while (mSnapshot != &mFirstSnapshot) { |
| 98 | Snapshot* temp = mSnapshot; |
| 99 | mSnapshot = mSnapshot->previous; |
| 100 | freeSnapshot(temp); |
| 101 | } |
| 102 | } |
| 103 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | // Save (layer) |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
| 108 | /** |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 109 | * Guaranteed to save without side-effects |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 110 | * |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 111 | * This approach, here and in restoreSnapshot(), allows subclasses to directly manipulate the save |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 112 | * stack, and ensures restoreToCount() doesn't call back into subclass overrides. |
| 113 | */ |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 114 | int CanvasState::saveSnapshot(int flags) { |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 115 | mSnapshot = allocSnapshot(mSnapshot, flags); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 116 | return mSaveCount++; |
| 117 | } |
| 118 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 119 | int CanvasState::save(int flags) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 120 | return saveSnapshot(flags); |
| 121 | } |
| 122 | |
| 123 | /** |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 124 | * Guaranteed to restore without side-effects. |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 125 | */ |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 126 | void CanvasState::restoreSnapshot() { |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 127 | Snapshot* toRemove = mSnapshot; |
| 128 | Snapshot* toRestore = mSnapshot->previous; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 129 | |
| 130 | mSaveCount--; |
| 131 | mSnapshot = toRestore; |
| 132 | |
| 133 | // subclass handles restore implementation |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 134 | mCanvas.onSnapshotRestored(*toRemove, *toRestore); |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 135 | |
| 136 | freeSnapshot(toRemove); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 139 | void CanvasState::restore() { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 140 | if (mSaveCount > 1) { |
| 141 | restoreSnapshot(); |
| 142 | } |
| 143 | } |
| 144 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 145 | void CanvasState::restoreToCount(int saveCount) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 146 | if (saveCount < 1) saveCount = 1; |
| 147 | |
| 148 | while (mSaveCount > saveCount) { |
| 149 | restoreSnapshot(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /////////////////////////////////////////////////////////////////////////////// |
| 154 | // Matrix |
| 155 | /////////////////////////////////////////////////////////////////////////////// |
| 156 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 157 | void CanvasState::getMatrix(SkMatrix* matrix) const { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 158 | mSnapshot->transform->copyTo(*matrix); |
| 159 | } |
| 160 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 161 | void CanvasState::translate(float dx, float dy, float dz) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 162 | mSnapshot->transform->translate(dx, dy, dz); |
| 163 | } |
| 164 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 165 | void CanvasState::rotate(float degrees) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 166 | mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); |
| 167 | } |
| 168 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 169 | void CanvasState::scale(float sx, float sy) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 170 | mSnapshot->transform->scale(sx, sy, 1.0f); |
| 171 | } |
| 172 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 173 | void CanvasState::skew(float sx, float sy) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 174 | mSnapshot->transform->skew(sx, sy); |
| 175 | } |
| 176 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 177 | void CanvasState::setMatrix(const SkMatrix& matrix) { |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 178 | mSnapshot->transform->load(matrix); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 181 | void CanvasState::setMatrix(const Matrix4& matrix) { |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 182 | *(mSnapshot->transform) = matrix; |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 185 | void CanvasState::concatMatrix(const SkMatrix& matrix) { |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 186 | mat4 transform(matrix); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 187 | mSnapshot->transform->multiply(transform); |
| 188 | } |
| 189 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 190 | void CanvasState::concatMatrix(const Matrix4& matrix) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 191 | mSnapshot->transform->multiply(matrix); |
| 192 | } |
| 193 | |
| 194 | /////////////////////////////////////////////////////////////////////////////// |
| 195 | // Clip |
| 196 | /////////////////////////////////////////////////////////////////////////////// |
| 197 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 198 | bool CanvasState::clipRect(float left, float top, float right, float bottom, SkClipOp op) { |
Chris Craik | a2a7072 | 2015-12-17 12:58:24 -0800 | [diff] [blame] | 199 | mSnapshot->clip(Rect(left, top, right, bottom), op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 200 | return !mSnapshot->clipIsEmpty(); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 203 | bool CanvasState::clipPath(const SkPath* path, SkClipOp op) { |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 204 | mSnapshot->clipPath(*path, op); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 205 | return !mSnapshot->clipIsEmpty(); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Tom Hudson | 984162f | 2014-10-10 13:38:16 -0400 | [diff] [blame] | 208 | void CanvasState::setClippingOutline(LinearAllocator& allocator, const Outline* outline) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 209 | Rect bounds; |
| 210 | float radius; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 211 | if (!outline->getAsRoundRect(&bounds, &radius)) return; // only RR supported |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 212 | |
Chris Craik | 79d26c7 | 2014-08-21 12:26:16 -0700 | [diff] [blame] | 213 | bool outlineIsRounded = MathUtils::isPositive(radius); |
| 214 | if (!outlineIsRounded || currentTransform()->isSimple()) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 215 | // TODO: consider storing this rect separately, so that this can't be replaced with clip ops |
Mike Reed | 6c67f1d | 2016-12-14 10:29:54 -0500 | [diff] [blame] | 216 | clipRect(bounds.left, bounds.top, bounds.right, bounds.bottom, SkClipOp::kIntersect); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 217 | } |
Chris Craik | 79d26c7 | 2014-08-21 12:26:16 -0700 | [diff] [blame] | 218 | if (outlineIsRounded) { |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 219 | setClippingRoundRect(allocator, bounds, radius, false); |
Chris Craik | 79d26c7 | 2014-08-21 12:26:16 -0700 | [diff] [blame] | 220 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 223 | /////////////////////////////////////////////////////////////////////////////// |
| 224 | // Quick Rejection |
| 225 | /////////////////////////////////////////////////////////////////////////////// |
| 226 | |
| 227 | /** |
| 228 | * Calculates whether content drawn within the passed bounds would be outside of, or intersect with |
| 229 | * the clipRect. Does not modify the scissor. |
| 230 | * |
| 231 | * @param clipRequired if not null, will be set to true if element intersects clip |
| 232 | * (and wasn't rejected) |
| 233 | * |
| 234 | * @param snapOut if set, the geometry will be treated as having an AA ramp. |
| 235 | * See Rect::snapGeometryToPixelBoundaries() |
| 236 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 237 | bool CanvasState::calculateQuickRejectForScissor(float left, float top, float right, float bottom, |
| 238 | bool* clipRequired, bool* roundRectClipRequired, |
| 239 | bool snapOut) const { |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 240 | if (bottom <= top || right <= left) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
| 244 | Rect r(left, top, right, bottom); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 245 | currentTransform()->mapRect(r); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 246 | r.snapGeometryToPixelBoundaries(snapOut); |
| 247 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 248 | Rect clipRect(currentRenderTargetClip()); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 249 | clipRect.snapToPixelBoundaries(); |
| 250 | |
| 251 | if (!clipRect.intersects(r)) return true; |
| 252 | |
| 253 | // clip is required if geometry intersects clip rect |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 254 | if (clipRequired) { |
| 255 | *clipRequired = !clipRect.contains(r); |
| 256 | } |
| 257 | |
| 258 | // round rect clip is required if RR clip exists, and geometry intersects its corners |
| 259 | if (roundRectClipRequired) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 260 | *roundRectClipRequired = mSnapshot->roundRectClipState != nullptr && |
| 261 | mSnapshot->roundRectClipState->areaRequiresRoundRectClip(r); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 262 | } |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 266 | bool CanvasState::quickRejectConservative(float left, float top, float right, float bottom) const { |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 267 | if (bottom <= top || right <= left) { |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 268 | return true; |
| 269 | } |
| 270 | |
| 271 | Rect r(left, top, right, bottom); |
Chris Craik | d6b65f6 | 2014-01-01 14:45:21 -0800 | [diff] [blame] | 272 | currentTransform()->mapRect(r); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 273 | r.roundOut(); // rounded out to be conservative |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 274 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 275 | Rect clipRect(currentRenderTargetClip()); |
Chris Craik | 14e5130 | 2013-12-30 15:32:54 -0800 | [diff] [blame] | 276 | clipRect.snapToPixelBoundaries(); |
| 277 | |
| 278 | if (!clipRect.intersects(r)) return true; |
| 279 | |
| 280 | return false; |
| 281 | } |
| 282 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 283 | } // namespace uirenderer |
| 284 | } // namespace android |