Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #include "Snapshot.h" |
| 18 | |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 19 | #include "hwui/Canvas.h" |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // Constructors |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 28 | Snapshot::Snapshot() |
| 29 | : flags(0) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 30 | , previous(nullptr) |
| 31 | , layer(nullptr) |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 32 | , fbo(0) |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 33 | , alpha(1.0f) |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 34 | , roundRectClipState(nullptr) |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 35 | , projectionPathMask(nullptr) |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 36 | , mClipArea(&mClipAreaRoot) { |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 37 | transform = &mTransformRoot; |
Keith Mok | de89c2f | 2016-07-13 14:45:16 -0700 | [diff] [blame] | 38 | mRelativeLightCenter.x = mRelativeLightCenter.y = mRelativeLightCenter.z = 0; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Copies the specified snapshot/ The specified snapshot is stored as |
| 43 | * the previous snapshot. |
| 44 | */ |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 45 | Snapshot::Snapshot(Snapshot* s, int saveFlags) |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 46 | : flags(0) |
| 47 | , previous(s) |
| 48 | , layer(s->layer) |
| 49 | , fbo(s->fbo) |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 50 | , alpha(s->alpha) |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 51 | , roundRectClipState(s->roundRectClipState) |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 52 | , projectionPathMask(s->projectionPathMask) |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 53 | , mClipArea(nullptr) |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 54 | , mViewportData(s->mViewportData) |
| 55 | , mRelativeLightCenter(s->mRelativeLightCenter) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 56 | if (saveFlags & SaveFlags::Matrix) { |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 57 | mTransformRoot = *s->transform; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 58 | transform = &mTransformRoot; |
| 59 | } else { |
| 60 | transform = s->transform; |
| 61 | } |
| 62 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 63 | if (saveFlags & SaveFlags::Clip) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 64 | mClipAreaRoot = s->getClipArea(); |
| 65 | mClipArea = &mClipAreaRoot; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 66 | } else { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 67 | mClipArea = s->mClipArea; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 68 | } |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | // Clipping |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 75 | void Snapshot::clip(const Rect& localClip, SkClipOp op) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 76 | flags |= Snapshot::kFlagClipSet; |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 77 | mClipArea->clipRectWithTransform(localClip, transform, static_cast<SkRegion::Op>(op)); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 80 | void Snapshot::clipPath(const SkPath& path, SkClipOp op) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 81 | flags |= Snapshot::kFlagClipSet; |
Mike Reed | 6e49c9f | 2016-12-02 15:36:59 -0500 | [diff] [blame] | 82 | mClipArea->clipPathWithTransform(path, transform, static_cast<SkRegion::Op>(op)); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void Snapshot::setClip(float left, float top, float right, float bottom) { |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 86 | flags |= Snapshot::kFlagClipSet; |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 87 | mClipArea->setClip(left, top, right, bottom); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 90 | bool Snapshot::hasPerspectiveTransform() const { |
| 91 | return transform->isPerspective(); |
| 92 | } |
| 93 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 94 | const Rect& Snapshot::getLocalClip() { |
| 95 | mat4 inverse; |
| 96 | inverse.loadInverse(*transform); |
| 97 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 98 | mLocalClip.set(mClipArea->getClipRect()); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 99 | inverse.mapRect(mLocalClip); |
| 100 | |
| 101 | return mLocalClip; |
| 102 | } |
| 103 | |
| 104 | void Snapshot::resetClip(float left, float top, float right, float bottom) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 105 | // TODO: This is incorrect, when we start rendering into a new layer, |
| 106 | // we may have to modify the previous snapshot's clip rect and clip |
| 107 | // region if the previous restore() call did not restore the clip |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 108 | mClipArea = &mClipAreaRoot; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 109 | setClip(left, top, right, bottom); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 113 | // Clipping round rect |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 116 | void Snapshot::setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds, float radius, |
| 117 | bool highPriority) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 118 | if (bounds.isEmpty()) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 119 | mClipArea->setEmpty(); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 120 | return; |
| 121 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 122 | |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 123 | if (roundRectClipState && roundRectClipState->highPriority) { |
| 124 | // ignore, don't replace, already have a high priority clip |
| 125 | return; |
| 126 | } |
| 127 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 128 | RoundRectClipState* state = new (allocator) RoundRectClipState; |
| 129 | |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 130 | state->highPriority = highPriority; |
| 131 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 132 | // store the inverse drawing matrix |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 133 | Matrix4 roundRectDrawingMatrix = getOrthoMatrix(); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 134 | roundRectDrawingMatrix.multiply(*transform); |
| 135 | state->matrix.loadInverse(roundRectDrawingMatrix); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 136 | |
| 137 | // compute area under rounded corners - only draws overlapping these rects need to be clipped |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 138 | for (int i = 0; i < 4; i++) { |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 139 | state->dangerRects[i] = bounds; |
| 140 | } |
| 141 | state->dangerRects[0].bottom = state->dangerRects[1].bottom = bounds.top + radius; |
| 142 | state->dangerRects[0].right = state->dangerRects[2].right = bounds.left + radius; |
| 143 | state->dangerRects[1].left = state->dangerRects[3].left = bounds.right - radius; |
| 144 | state->dangerRects[2].top = state->dangerRects[3].top = bounds.bottom - radius; |
| 145 | for (int i = 0; i < 4; i++) { |
| 146 | transform->mapRect(state->dangerRects[i]); |
| 147 | |
| 148 | // round danger rects out as though they are AA geometry (since they essentially are) |
| 149 | state->dangerRects[i].snapGeometryToPixelBoundaries(true); |
| 150 | } |
| 151 | |
| 152 | // store RR area |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 153 | state->innerRect = bounds; |
| 154 | state->innerRect.inset(radius); |
| 155 | state->radius = radius; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 156 | |
| 157 | // store as immutable so, for this frame, pointer uniquely identifies this bundle of shader info |
| 158 | roundRectClipState = state; |
| 159 | } |
| 160 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 161 | void Snapshot::setProjectionPathMask(const SkPath* path) { |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 162 | projectionPathMask = path; |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 165 | static Snapshot* getClipRoot(Snapshot* target) { |
| 166 | while (target->previous && target->previous->previous) { |
| 167 | target = target->previous; |
| 168 | } |
| 169 | return target; |
| 170 | } |
| 171 | |
| 172 | const ClipBase* Snapshot::serializeIntersectedClip(LinearAllocator& allocator, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 173 | const ClipBase* recordedClip, |
| 174 | const Matrix4& recordedClipTransform) { |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 175 | auto target = this; |
| 176 | if (CC_UNLIKELY(recordedClip && recordedClip->intersectWithRoot)) { |
| 177 | // Clip must be intersected with root, instead of current clip. |
| 178 | target = getClipRoot(this); |
| 179 | } |
| 180 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 181 | return target->mClipArea->serializeIntersectedClip(allocator, recordedClip, |
| 182 | recordedClipTransform); |
Chris Craik | 04d46eb | 2016-04-07 13:51:07 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void Snapshot::applyClip(const ClipBase* recordedClip, const Matrix4& transform) { |
| 186 | if (CC_UNLIKELY(recordedClip && recordedClip->intersectWithRoot)) { |
| 187 | // current clip is being replaced, but must intersect with clip root |
| 188 | *mClipArea = *(getClipRoot(this)->mClipArea); |
| 189 | } |
| 190 | mClipArea->applyClip(recordedClip, transform); |
| 191 | } |
| 192 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 193 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 194 | // Queries |
| 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 197 | void Snapshot::dump() const { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 198 | ALOGD("Snapshot %p, flags %x, prev %p, height %d, hasComplexClip %d", this, flags, previous, |
| 199 | getViewportHeight(), !mClipArea->isSimple()); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 200 | const Rect& clipRect(mClipArea->getClipRect()); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 201 | ALOGD(" ClipRect %.1f %.1f %.1f %.1f, clip simple %d", clipRect.left, clipRect.top, |
| 202 | clipRect.right, clipRect.bottom, mClipArea->isSimple()); |
Chris Craik | e9c01a4 | 2015-04-06 10:47:23 -0700 | [diff] [blame] | 203 | |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 204 | ALOGD(" Transform (at %p):", transform); |
| 205 | transform->dump(); |
| 206 | } |
| 207 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 208 | }; // namespace uirenderer |
| 209 | }; // namespace android |