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) |
| 33 | , invisible(false) |
| 34 | , empty(false) |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 35 | , alpha(1.0f) |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 36 | , roundRectClipState(nullptr) |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 37 | , projectionPathMask(nullptr) |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 38 | , mClipArea(&mClipAreaRoot) { |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 39 | transform = &mTransformRoot; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 40 | region = nullptr; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Copies the specified snapshot/ The specified snapshot is stored as |
| 45 | * the previous snapshot. |
| 46 | */ |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 47 | Snapshot::Snapshot(Snapshot* s, int saveFlags) |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 48 | : flags(0) |
| 49 | , previous(s) |
| 50 | , layer(s->layer) |
| 51 | , fbo(s->fbo) |
| 52 | , invisible(s->invisible) |
| 53 | , empty(false) |
Chris Craik | a64a2be | 2014-05-14 14:17:01 -0700 | [diff] [blame] | 54 | , alpha(s->alpha) |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 55 | , roundRectClipState(s->roundRectClipState) |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 56 | , projectionPathMask(s->projectionPathMask) |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 57 | , mClipArea(nullptr) |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 58 | , mViewportData(s->mViewportData) |
| 59 | , mRelativeLightCenter(s->mRelativeLightCenter) { |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 60 | if (saveFlags & SaveFlags::Matrix) { |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 61 | mTransformRoot = *s->transform; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 62 | transform = &mTransformRoot; |
| 63 | } else { |
| 64 | transform = s->transform; |
| 65 | } |
| 66 | |
Florin Malita | eecff56 | 2015-12-21 10:43:01 -0500 | [diff] [blame] | 67 | if (saveFlags & SaveFlags::Clip) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 68 | mClipAreaRoot = s->getClipArea(); |
| 69 | mClipArea = &mClipAreaRoot; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 70 | } else { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 71 | mClipArea = s->mClipArea; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (s->flags & Snapshot::kFlagFboTarget) { |
| 75 | flags |= Snapshot::kFlagFboTarget; |
| 76 | region = s->region; |
| 77 | } else { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 78 | region = nullptr; |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | // Clipping |
| 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 86 | void Snapshot::clipRegionTransformed(const SkRegion& region, SkRegion::Op op) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 87 | flags |= Snapshot::kFlagClipSet; |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 88 | mClipArea->clipRegion(region, op); |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Chris Craik | a2a7072 | 2015-12-17 12:58:24 -0800 | [diff] [blame] | 91 | void Snapshot::clip(const Rect& localClip, SkRegion::Op op) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 92 | flags |= Snapshot::kFlagClipSet; |
Chris Craik | a2a7072 | 2015-12-17 12:58:24 -0800 | [diff] [blame] | 93 | mClipArea->clipRectWithTransform(localClip, transform, op); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 96 | void Snapshot::clipPath(const SkPath& path, SkRegion::Op op) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 97 | flags |= Snapshot::kFlagClipSet; |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 98 | mClipArea->clipPathWithTransform(path, transform, op); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void Snapshot::setClip(float left, float top, float right, float bottom) { |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 102 | flags |= Snapshot::kFlagClipSet; |
Chris Craik | 4d3e704 | 2015-08-20 12:54:25 -0700 | [diff] [blame] | 103 | mClipArea->setClip(left, top, right, bottom); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 106 | bool Snapshot::hasPerspectiveTransform() const { |
| 107 | return transform->isPerspective(); |
| 108 | } |
| 109 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 110 | const Rect& Snapshot::getLocalClip() { |
| 111 | mat4 inverse; |
| 112 | inverse.loadInverse(*transform); |
| 113 | |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 114 | mLocalClip.set(mClipArea->getClipRect()); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 115 | inverse.mapRect(mLocalClip); |
| 116 | |
| 117 | return mLocalClip; |
| 118 | } |
| 119 | |
| 120 | void Snapshot::resetClip(float left, float top, float right, float bottom) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 121 | // TODO: This is incorrect, when we start rendering into a new layer, |
| 122 | // we may have to modify the previous snapshot's clip rect and clip |
| 123 | // region if the previous restore() call did not restore the clip |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 124 | mClipArea = &mClipAreaRoot; |
Romain Guy | 967e2bf | 2012-02-07 17:04:34 -0800 | [diff] [blame] | 125 | setClip(left, top, right, bottom); |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /////////////////////////////////////////////////////////////////////////////// |
| 129 | // Transforms |
| 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | void Snapshot::resetTransform(float x, float y, float z) { |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 133 | #if HWUI_NEW_OPS |
| 134 | LOG_ALWAYS_FATAL("not supported - light center managed differently"); |
| 135 | #else |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 136 | // before resetting, map current light pos with inverse of current transform |
| 137 | Vector3 center = mRelativeLightCenter; |
| 138 | mat4 inverse; |
| 139 | inverse.loadInverse(*transform); |
| 140 | inverse.mapPoint3d(center); |
| 141 | mRelativeLightCenter = center; |
| 142 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 143 | transform = &mTransformRoot; |
| 144 | transform->loadTranslate(x, y, z); |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 145 | #endif |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 148 | void Snapshot::buildScreenSpaceTransform(Matrix4* outTransform) const { |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 149 | #if HWUI_NEW_OPS |
| 150 | LOG_ALWAYS_FATAL("not supported - not needed by new ops"); |
| 151 | #else |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 152 | // build (reverse ordered) list of the stack of snapshots, terminated with a NULL |
| 153 | Vector<const Snapshot*> snapshotList; |
| 154 | snapshotList.push(nullptr); |
| 155 | const Snapshot* current = this; |
| 156 | do { |
| 157 | snapshotList.push(current); |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 158 | current = current->previous; |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 159 | } while (current); |
| 160 | |
| 161 | // traverse the list, adding in each transform that contributes to the total transform |
| 162 | outTransform->loadIdentity(); |
| 163 | for (size_t i = snapshotList.size() - 1; i > 0; i--) { |
| 164 | // iterate down the stack |
| 165 | const Snapshot* current = snapshotList[i]; |
| 166 | const Snapshot* next = snapshotList[i - 1]; |
| 167 | if (current->flags & kFlagIsFboLayer) { |
| 168 | // if we've hit a layer, translate by the layer's draw offset |
| 169 | outTransform->translate(current->layer->layer.left, current->layer->layer.top); |
| 170 | } |
| 171 | if (!next || (next->flags & kFlagIsFboLayer)) { |
| 172 | // if this snapshot is last, or if this snapshot is last before an |
| 173 | // FBO layer (which reset the transform), apply it |
| 174 | outTransform->multiply(*(current->transform)); |
| 175 | } |
| 176 | } |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 177 | #endif |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 180 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 181 | // Clipping round rect |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 182 | /////////////////////////////////////////////////////////////////////////////// |
| 183 | |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 184 | void Snapshot::setClippingRoundRect(LinearAllocator& allocator, const Rect& bounds, |
| 185 | float radius, bool highPriority) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 186 | if (bounds.isEmpty()) { |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 187 | mClipArea->setEmpty(); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 188 | return; |
| 189 | } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 190 | |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 191 | if (roundRectClipState && roundRectClipState->highPriority) { |
| 192 | // ignore, don't replace, already have a high priority clip |
| 193 | return; |
| 194 | } |
| 195 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 196 | RoundRectClipState* state = new (allocator) RoundRectClipState; |
| 197 | |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 198 | state->highPriority = highPriority; |
| 199 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 200 | // store the inverse drawing matrix |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 201 | Matrix4 roundRectDrawingMatrix = getOrthoMatrix(); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 202 | roundRectDrawingMatrix.multiply(*transform); |
| 203 | state->matrix.loadInverse(roundRectDrawingMatrix); |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 204 | |
| 205 | // compute area under rounded corners - only draws overlapping these rects need to be clipped |
| 206 | for (int i = 0 ; i < 4; i++) { |
| 207 | state->dangerRects[i] = bounds; |
| 208 | } |
| 209 | state->dangerRects[0].bottom = state->dangerRects[1].bottom = bounds.top + radius; |
| 210 | state->dangerRects[0].right = state->dangerRects[2].right = bounds.left + radius; |
| 211 | state->dangerRects[1].left = state->dangerRects[3].left = bounds.right - radius; |
| 212 | state->dangerRects[2].top = state->dangerRects[3].top = bounds.bottom - radius; |
| 213 | for (int i = 0; i < 4; i++) { |
| 214 | transform->mapRect(state->dangerRects[i]); |
| 215 | |
| 216 | // round danger rects out as though they are AA geometry (since they essentially are) |
| 217 | state->dangerRects[i].snapGeometryToPixelBoundaries(true); |
| 218 | } |
| 219 | |
| 220 | // store RR area |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 221 | state->innerRect = bounds; |
| 222 | state->innerRect.inset(radius); |
| 223 | state->radius = radius; |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 224 | |
| 225 | // store as immutable so, for this frame, pointer uniquely identifies this bundle of shader info |
| 226 | roundRectClipState = state; |
| 227 | } |
| 228 | |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 229 | void Snapshot::setProjectionPathMask(LinearAllocator& allocator, const SkPath* path) { |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 230 | #if HWUI_NEW_OPS |
| 231 | // TODO: remove allocator param for HWUI_NEW_OPS |
| 232 | projectionPathMask = path; |
| 233 | #else |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 234 | if (path) { |
| 235 | ProjectionPathMask* mask = new (allocator) ProjectionPathMask; |
| 236 | mask->projectionMask = path; |
| 237 | buildScreenSpaceTransform(&(mask->projectionMaskTransform)); |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 238 | projectionPathMask = mask; |
| 239 | } else { |
| 240 | projectionPathMask = nullptr; |
| 241 | } |
Chris Craik | 678ff81 | 2016-03-01 13:27:54 -0800 | [diff] [blame] | 242 | #endif |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 245 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 246 | // Queries |
| 247 | /////////////////////////////////////////////////////////////////////////////// |
| 248 | |
| 249 | bool Snapshot::isIgnored() const { |
| 250 | return invisible || empty; |
| 251 | } |
| 252 | |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 253 | void Snapshot::dump() const { |
| 254 | ALOGD("Snapshot %p, flags %x, prev %p, height %d, ignored %d, hasComplexClip %d", |
John Reck | d9ee550 | 2015-10-06 10:06:37 -0700 | [diff] [blame] | 255 | this, flags, previous, getViewportHeight(), isIgnored(), !mClipArea->isSimple()); |
Rob Tsuk | 487a92c | 2015-01-06 13:22:54 -0800 | [diff] [blame] | 256 | const Rect& clipRect(mClipArea->getClipRect()); |
Chris Craik | e9c01a4 | 2015-04-06 10:47:23 -0700 | [diff] [blame] | 257 | ALOGD(" ClipRect %.1f %.1f %.1f %.1f, clip simple %d", |
| 258 | clipRect.left, clipRect.top, clipRect.right, clipRect.bottom, mClipArea->isSimple()); |
| 259 | |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 260 | ALOGD(" Transform (at %p):", transform); |
| 261 | transform->dump(); |
| 262 | } |
| 263 | |
Romain Guy | ada4d53 | 2012-02-02 17:31:16 -0800 | [diff] [blame] | 264 | }; // namespace uirenderer |
| 265 | }; // namespace android |