John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [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 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 17 | #include "DamageAccumulator.h" |
| 18 | |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 19 | #include <log/log.h> |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 20 | |
| 21 | #include "RenderNode.h" |
| 22 | #include "utils/MathUtils.h" |
| 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 27 | enum TransformType { |
John Reck | 2dc223d | 2014-06-17 10:46:09 -0700 | [diff] [blame] | 28 | TransformInvalid = 0, |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 29 | TransformRenderNode, |
| 30 | TransformMatrix4, |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 31 | TransformNone, |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 34 | struct DirtyStack { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 35 | TransformType type; |
| 36 | union { |
| 37 | const RenderNode* renderNode; |
| 38 | const Matrix4* matrix4; |
| 39 | }; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 40 | // When this frame is pop'd, this rect is mapped through the above transform |
| 41 | // and applied to the previous (aka parent) frame |
| 42 | SkRect pendingDirty; |
| 43 | DirtyStack* prev; |
| 44 | DirtyStack* next; |
| 45 | }; |
| 46 | |
| 47 | DamageAccumulator::DamageAccumulator() { |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 48 | mHead = mAllocator.create_trivial<DirtyStack>(); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 49 | memset(mHead, 0, sizeof(DirtyStack)); |
| 50 | // Create a root that we will not pop off |
| 51 | mHead->prev = mHead; |
John Reck | 2dc223d | 2014-06-17 10:46:09 -0700 | [diff] [blame] | 52 | mHead->type = TransformNone; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 55 | static void computeTransformImpl(const DirtyStack* currentFrame, Matrix4* outMatrix) { |
| 56 | if (currentFrame->prev != currentFrame) { |
| 57 | computeTransformImpl(currentFrame->prev, outMatrix); |
| 58 | } |
| 59 | switch (currentFrame->type) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | case TransformRenderNode: |
| 61 | currentFrame->renderNode->applyViewPropertyTransforms(*outMatrix); |
| 62 | break; |
| 63 | case TransformMatrix4: |
| 64 | outMatrix->multiply(*currentFrame->matrix4); |
| 65 | break; |
| 66 | case TransformNone: |
| 67 | // nothing to be done |
| 68 | break; |
| 69 | default: |
| 70 | LOG_ALWAYS_FATAL("Tried to compute transform with an invalid type: %d", |
| 71 | currentFrame->type); |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | void DamageAccumulator::computeCurrentTransform(Matrix4* outMatrix) const { |
| 76 | outMatrix->loadIdentity(); |
| 77 | computeTransformImpl(mHead, outMatrix); |
| 78 | } |
| 79 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 80 | void DamageAccumulator::pushCommon() { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 81 | if (!mHead->next) { |
John Reck | 7df9ff2 | 2016-02-10 16:08:08 -0800 | [diff] [blame] | 82 | DirtyStack* nextFrame = mAllocator.create_trivial<DirtyStack>(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 83 | nextFrame->next = nullptr; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 84 | nextFrame->prev = mHead; |
| 85 | mHead->next = nextFrame; |
| 86 | } |
| 87 | mHead = mHead->next; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 88 | mHead->pendingDirty.setEmpty(); |
| 89 | } |
| 90 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 91 | void DamageAccumulator::pushTransform(const RenderNode* transform) { |
| 92 | pushCommon(); |
| 93 | mHead->type = TransformRenderNode; |
| 94 | mHead->renderNode = transform; |
| 95 | } |
| 96 | |
| 97 | void DamageAccumulator::pushTransform(const Matrix4* transform) { |
| 98 | pushCommon(); |
| 99 | mHead->type = TransformMatrix4; |
| 100 | mHead->matrix4 = transform; |
| 101 | } |
| 102 | |
| 103 | void DamageAccumulator::popTransform() { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 104 | LOG_ALWAYS_FATAL_IF(mHead->prev == mHead, "Cannot pop the root frame!"); |
| 105 | DirtyStack* dirtyFrame = mHead; |
| 106 | mHead = mHead->prev; |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 107 | switch (dirtyFrame->type) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 108 | case TransformRenderNode: |
| 109 | applyRenderNodeTransform(dirtyFrame); |
| 110 | break; |
| 111 | case TransformMatrix4: |
| 112 | applyMatrix4Transform(dirtyFrame); |
| 113 | break; |
| 114 | case TransformNone: |
| 115 | mHead->pendingDirty.join(dirtyFrame->pendingDirty); |
| 116 | break; |
| 117 | default: |
| 118 | LOG_ALWAYS_FATAL("Tried to pop an invalid type: %d", dirtyFrame->type); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | static inline void mapRect(const Matrix4* matrix, const SkRect& in, SkRect* out) { |
| 123 | if (in.isEmpty()) return; |
| 124 | Rect temp(in); |
John Reck | c128823 | 2015-08-12 13:39:11 -0700 | [diff] [blame] | 125 | if (CC_LIKELY(!matrix->isPerspective())) { |
| 126 | matrix->mapRect(temp); |
| 127 | } else { |
| 128 | // Don't attempt to calculate damage for a perspective transform |
| 129 | // as the numbers this works with can break the perspective |
| 130 | // calculations. Just give up and expand to DIRTY_MIN/DIRTY_MAX |
| 131 | temp.set(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX); |
| 132 | } |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 133 | out->join({RECT_ARGS(temp)}); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void DamageAccumulator::applyMatrix4Transform(DirtyStack* frame) { |
| 137 | mapRect(frame->matrix4, frame->pendingDirty, &mHead->pendingDirty); |
| 138 | } |
| 139 | |
John Reck | e275052 | 2019-06-26 13:46:09 -0700 | [diff] [blame] | 140 | static inline void applyMatrix(const SkMatrix* transform, SkRect* rect) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 141 | if (transform && !transform->isIdentity()) { |
John Reck | c128823 | 2015-08-12 13:39:11 -0700 | [diff] [blame] | 142 | if (CC_LIKELY(!transform->hasPerspective())) { |
John Reck | e275052 | 2019-06-26 13:46:09 -0700 | [diff] [blame] | 143 | transform->mapRect(rect); |
John Reck | c128823 | 2015-08-12 13:39:11 -0700 | [diff] [blame] | 144 | } else { |
| 145 | // Don't attempt to calculate damage for a perspective transform |
| 146 | // as the numbers this works with can break the perspective |
| 147 | // calculations. Just give up and expand to DIRTY_MIN/DIRTY_MAX |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 148 | rect->setLTRB(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX); |
John Reck | c128823 | 2015-08-12 13:39:11 -0700 | [diff] [blame] | 149 | } |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 150 | } |
John Reck | e275052 | 2019-06-26 13:46:09 -0700 | [diff] [blame] | 151 | } |
| 152 | |
John Reck | 8ed00dc | 2021-05-10 13:09:27 -0400 | [diff] [blame] | 153 | static inline void applyMatrix(const SkMatrix& transform, SkRect* rect) { |
| 154 | return applyMatrix(&transform, rect); |
| 155 | } |
| 156 | |
John Reck | e275052 | 2019-06-26 13:46:09 -0700 | [diff] [blame] | 157 | static inline void mapRect(const RenderProperties& props, const SkRect& in, SkRect* out) { |
| 158 | if (in.isEmpty()) return; |
| 159 | SkRect temp(in); |
Nader Jawad | 93d6e24 | 2021-05-17 18:12:29 -0700 | [diff] [blame] | 160 | if (Properties::getStretchEffectBehavior() == StretchEffectBehavior::UniformScale) { |
John Reck | 8ed00dc | 2021-05-10 13:09:27 -0400 | [diff] [blame] | 161 | const StretchEffect& stretch = props.layerProperties().getStretchEffect(); |
| 162 | if (!stretch.isEmpty()) { |
| 163 | applyMatrix(stretch.makeLinearStretch(props.getWidth(), props.getHeight()), &temp); |
| 164 | } |
| 165 | } |
John Reck | e275052 | 2019-06-26 13:46:09 -0700 | [diff] [blame] | 166 | applyMatrix(props.getTransformMatrix(), &temp); |
| 167 | if (props.getStaticMatrix()) { |
| 168 | applyMatrix(props.getStaticMatrix(), &temp); |
| 169 | } else if (props.getAnimationMatrix()) { |
| 170 | applyMatrix(props.getAnimationMatrix(), &temp); |
| 171 | } |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 172 | temp.offset(props.getLeft(), props.getTop()); |
| 173 | out->join(temp); |
| 174 | } |
| 175 | |
| 176 | static DirtyStack* findParentRenderNode(DirtyStack* frame) { |
| 177 | while (frame->prev != frame) { |
| 178 | frame = frame->prev; |
| 179 | if (frame->type == TransformRenderNode) { |
| 180 | return frame; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 181 | } |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 182 | } |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 183 | return nullptr; |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static DirtyStack* findProjectionReceiver(DirtyStack* frame) { |
| 187 | if (frame) { |
| 188 | while (frame->prev != frame) { |
| 189 | frame = frame->prev; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 190 | if (frame->type == TransformRenderNode && frame->renderNode->hasProjectionReceiver()) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 191 | return frame; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 192 | } |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 193 | } |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 194 | } |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 195 | return nullptr; |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void applyTransforms(DirtyStack* frame, DirtyStack* end) { |
| 199 | SkRect* rect = &frame->pendingDirty; |
| 200 | while (frame != end) { |
| 201 | if (frame->type == TransformRenderNode) { |
| 202 | mapRect(frame->renderNode->properties(), *rect, rect); |
| 203 | } else { |
| 204 | mapRect(frame->matrix4, *rect, rect); |
| 205 | } |
| 206 | frame = frame->prev; |
| 207 | } |
| 208 | } |
| 209 | |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 210 | static void computeTransformImpl(const DirtyStack* frame, const DirtyStack* end, |
| 211 | Matrix4* outMatrix) { |
| 212 | while (frame != end) { |
| 213 | switch (frame->type) { |
| 214 | case TransformRenderNode: |
| 215 | frame->renderNode->applyViewPropertyTransforms(*outMatrix); |
| 216 | break; |
| 217 | case TransformMatrix4: |
| 218 | outMatrix->multiply(*frame->matrix4); |
| 219 | break; |
| 220 | case TransformNone: |
| 221 | // nothing to be done |
| 222 | break; |
| 223 | default: |
| 224 | LOG_ALWAYS_FATAL("Tried to compute transform with an invalid type: %d", |
| 225 | frame->type); |
| 226 | } |
| 227 | frame = frame->prev; |
| 228 | } |
| 229 | } |
| 230 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 231 | void DamageAccumulator::applyRenderNodeTransform(DirtyStack* frame) { |
| 232 | if (frame->pendingDirty.isEmpty()) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | const RenderProperties& props = frame->renderNode->properties(); |
John Reck | ce9f308 | 2014-06-17 16:18:09 -0700 | [diff] [blame] | 237 | if (props.getAlpha() <= 0) { |
| 238 | return; |
| 239 | } |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 240 | |
| 241 | // Perform clipping |
John Reck | 293e868 | 2014-06-17 10:34:02 -0700 | [diff] [blame] | 242 | if (props.getClipDamageToBounds() && !frame->pendingDirty.isEmpty()) { |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 243 | if (!frame->pendingDirty.intersect(SkRect::MakeIWH(props.getWidth(), props.getHeight()))) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 244 | frame->pendingDirty.setEmpty(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // apply all transforms |
| 249 | mapRect(props, frame->pendingDirty, &mHead->pendingDirty); |
| 250 | |
| 251 | // project backwards if necessary |
| 252 | if (props.getProjectBackwards() && !frame->pendingDirty.isEmpty()) { |
| 253 | // First, find our parent RenderNode: |
| 254 | DirtyStack* parentNode = findParentRenderNode(frame); |
| 255 | // Find our parent's projection receiver, which is what we project onto |
| 256 | DirtyStack* projectionReceiver = findProjectionReceiver(parentNode); |
| 257 | if (projectionReceiver) { |
| 258 | applyTransforms(frame, projectionReceiver); |
| 259 | projectionReceiver->pendingDirty.join(frame->pendingDirty); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | frame->pendingDirty.setEmpty(); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | void DamageAccumulator::dirty(float left, float top, float right, float bottom) { |
Mike Reed | 39adc88 | 2019-08-22 11:53:05 -0400 | [diff] [blame] | 267 | mHead->pendingDirty.join({left, top, right, bottom}); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Chris Craik | c71bfca | 2014-08-21 10:18:58 -0700 | [diff] [blame] | 270 | void DamageAccumulator::peekAtDirty(SkRect* dest) const { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 271 | *dest = mHead->pendingDirty; |
| 272 | } |
| 273 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 274 | void DamageAccumulator::finish(SkRect* totalDirty) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 275 | LOG_ALWAYS_FATAL_IF(mHead->prev != mHead, "Cannot finish, mismatched push/pop calls! %p vs. %p", |
| 276 | mHead->prev, mHead); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 277 | // Root node never has a transform, so this is the fully mapped dirty rect |
| 278 | *totalDirty = mHead->pendingDirty; |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 279 | totalDirty->roundOut(totalDirty); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 280 | mHead->pendingDirty.setEmpty(); |
| 281 | } |
| 282 | |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 283 | DamageAccumulator::StretchResult DamageAccumulator::findNearestStretchEffect() const { |
John Reck | 5d53a75 | 2021-02-08 19:22:59 -0500 | [diff] [blame] | 284 | DirtyStack* frame = mHead; |
Nader Jawad | bc34186 | 2021-05-06 10:48:18 -0700 | [diff] [blame] | 285 | const auto& headProperties = mHead->renderNode->properties(); |
| 286 | float startWidth = headProperties.getWidth(); |
| 287 | float startHeight = headProperties.getHeight(); |
John Reck | 5d53a75 | 2021-02-08 19:22:59 -0500 | [diff] [blame] | 288 | while (frame->prev != frame) { |
John Reck | 5d53a75 | 2021-02-08 19:22:59 -0500 | [diff] [blame] | 289 | if (frame->type == TransformRenderNode) { |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 290 | const auto& renderNode = frame->renderNode; |
| 291 | const auto& frameRenderNodeProperties = renderNode->properties(); |
John Reck | 5d53a75 | 2021-02-08 19:22:59 -0500 | [diff] [blame] | 292 | const auto& effect = |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 293 | frameRenderNodeProperties.layerProperties().getStretchEffect(); |
Nader Jawad | bc34186 | 2021-05-06 10:48:18 -0700 | [diff] [blame] | 294 | const float width = (float) frameRenderNodeProperties.getWidth(); |
| 295 | const float height = (float) frameRenderNodeProperties.getHeight(); |
John Reck | 5d53a75 | 2021-02-08 19:22:59 -0500 | [diff] [blame] | 296 | if (!effect.isEmpty()) { |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 297 | Matrix4 stretchMatrix; |
| 298 | computeTransformImpl(mHead, frame, &stretchMatrix); |
Nader Jawad | bc34186 | 2021-05-06 10:48:18 -0700 | [diff] [blame] | 299 | Rect stretchRect = Rect(0.f, 0.f, startWidth, startHeight); |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 300 | stretchMatrix.mapRect(stretchRect); |
| 301 | |
| 302 | return StretchResult{ |
| 303 | .stretchEffect = &effect, |
| 304 | .childRelativeBounds = SkRect::MakeLTRB( |
| 305 | stretchRect.left, |
| 306 | stretchRect.top, |
| 307 | stretchRect.right, |
| 308 | stretchRect.bottom |
| 309 | ), |
| 310 | .width = width, |
| 311 | .height = height |
| 312 | }; |
John Reck | 5d53a75 | 2021-02-08 19:22:59 -0500 | [diff] [blame] | 313 | } |
| 314 | } |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 315 | frame = frame->prev; |
John Reck | 5d53a75 | 2021-02-08 19:22:59 -0500 | [diff] [blame] | 316 | } |
Nader Jawad | 197743f | 2021-04-19 19:45:13 -0700 | [diff] [blame] | 317 | return StretchResult{}; |
John Reck | 5d53a75 | 2021-02-08 19:22:59 -0500 | [diff] [blame] | 318 | } |
| 319 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 320 | } /* namespace uirenderer */ |
| 321 | } /* namespace android */ |