John Reck | 113e082 | 2014-03-18 09:22:59 -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 | |
| 17 | #define ATRACE_TAG ATRACE_TAG_VIEW |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 18 | #define LOG_TAG "RenderNode" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include "RenderNode.h" |
| 21 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 24 | #include <SkCanvas.h> |
| 25 | #include <algorithm> |
| 26 | |
| 27 | #include <utils/Trace.h> |
| 28 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 29 | #include "DamageAccumulator.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 30 | #include "Debug.h" |
| 31 | #include "DisplayListOp.h" |
| 32 | #include "DisplayListLogBuffer.h" |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 33 | #include "LayerRenderer.h" |
| 34 | #include "OpenGLRenderer.h" |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 35 | #include "utils/MathUtils.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | namespace uirenderer { |
| 39 | |
| 40 | void RenderNode::outputLogBuffer(int fd) { |
| 41 | DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); |
| 42 | if (logBuffer.isEmpty()) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | FILE *file = fdopen(fd, "a"); |
| 47 | |
| 48 | fprintf(file, "\nRecent DisplayList operations\n"); |
| 49 | logBuffer.outputCommands(file); |
| 50 | |
| 51 | String8 cachesLog; |
| 52 | Caches::getInstance().dumpMemoryUsage(cachesLog); |
| 53 | fprintf(file, "\nCaches:\n%s", cachesLog.string()); |
| 54 | fprintf(file, "\n"); |
| 55 | |
| 56 | fflush(file); |
| 57 | } |
| 58 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 59 | RenderNode::RenderNode() |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 60 | : mDirtyPropertyFields(0) |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 61 | , mNeedsDisplayListDataSync(false) |
| 62 | , mDisplayListData(0) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 63 | , mStagingDisplayListData(0) |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 64 | , mNeedsAnimatorsSync(false) |
| 65 | , mLayer(0) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | RenderNode::~RenderNode() { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 69 | delete mDisplayListData; |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 70 | delete mStagingDisplayListData; |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 71 | LayerRenderer::destroyLayerDeferred(mLayer); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 72 | } |
| 73 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 74 | void RenderNode::setStagingDisplayList(DisplayListData* data) { |
| 75 | mNeedsDisplayListDataSync = true; |
| 76 | delete mStagingDisplayListData; |
| 77 | mStagingDisplayListData = data; |
| 78 | if (mStagingDisplayListData) { |
| 79 | Caches::getInstance().registerFunctors(mStagingDisplayListData->functorCount); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * This function is a simplified version of replay(), where we simply retrieve and log the |
| 85 | * display list. This function should remain in sync with the replay() function. |
| 86 | */ |
| 87 | void RenderNode::output(uint32_t level) { |
| 88 | ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 89 | getName(), isRenderable()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 90 | ALOGD("%*s%s %d", level * 2, "", "Save", |
| 91 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
| 92 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 93 | properties().debugOutputProperties(level); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 94 | int flags = DisplayListOp::kOpLogFlag_Recurse; |
| 95 | for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { |
| 96 | mDisplayListData->displayListOps[i]->output(level, flags); |
| 97 | } |
| 98 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 99 | ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 100 | } |
| 101 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 102 | int RenderNode::getDebugSize() { |
| 103 | int size = sizeof(RenderNode); |
| 104 | if (mStagingDisplayListData) { |
| 105 | size += mStagingDisplayListData->allocator.usedSize(); |
| 106 | } |
| 107 | if (mDisplayListData && mDisplayListData != mStagingDisplayListData) { |
| 108 | size += mDisplayListData->allocator.usedSize(); |
| 109 | } |
| 110 | return size; |
| 111 | } |
| 112 | |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 113 | void RenderNode::prepareTree(TreeInfo& info) { |
| 114 | ATRACE_CALL(); |
| 115 | |
| 116 | prepareTreeImpl(info); |
| 117 | } |
| 118 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 119 | void RenderNode::damageSelf(TreeInfo& info) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 120 | if (isRenderable() && properties().getAlpha() > 0) { |
| 121 | if (properties().getClipToBounds()) { |
| 122 | info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight()); |
| 123 | } else { |
| 124 | // Hope this is big enough? |
| 125 | // TODO: Get this from the display list ops or something |
| 126 | info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX); |
| 127 | } |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 131 | void RenderNode::prepareLayer(TreeInfo& info) { |
| 132 | LayerType layerType = properties().layerProperties().type(); |
| 133 | if (CC_UNLIKELY(layerType == kLayerTypeRenderLayer)) { |
| 134 | // We push a null transform here as we don't care what the existing dirty |
| 135 | // area is, only what our display list dirty is as well as our children's |
| 136 | // dirty area |
| 137 | info.damageAccumulator->pushNullTransform(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void RenderNode::pushLayerUpdate(TreeInfo& info) { |
| 142 | LayerType layerType = properties().layerProperties().type(); |
| 143 | // If we are not a layer OR we cannot be rendered (eg, view was detached) |
| 144 | // we need to destroy any Layers we may have had previously |
| 145 | if (CC_LIKELY(layerType != kLayerTypeRenderLayer) || CC_UNLIKELY(!isRenderable())) { |
| 146 | if (layerType == kLayerTypeRenderLayer) { |
| 147 | info.damageAccumulator->popTransform(); |
| 148 | } |
| 149 | if (CC_UNLIKELY(mLayer)) { |
| 150 | LayerRenderer::destroyLayer(mLayer); |
| 151 | mLayer = NULL; |
| 152 | } |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (!mLayer) { |
| 157 | mLayer = LayerRenderer::createRenderLayer(getWidth(), getHeight()); |
| 158 | applyLayerPropertiesToLayer(info); |
| 159 | damageSelf(info); |
| 160 | } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) { |
| 161 | LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight()); |
| 162 | damageSelf(info); |
| 163 | } |
| 164 | |
| 165 | SkRect dirty; |
| 166 | info.damageAccumulator->peekAtDirty(&dirty); |
| 167 | info.damageAccumulator->popTransform(); |
| 168 | |
| 169 | if (!dirty.isEmpty()) { |
| 170 | mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom); |
| 171 | } |
| 172 | // This is not inside the above if because we may have called |
| 173 | // updateDeferred on a previous prepare pass that didn't have a renderer |
| 174 | if (info.renderer && mLayer->deferredUpdateScheduled) { |
| 175 | info.renderer->pushLayerUpdate(mLayer); |
| 176 | } |
| 177 | } |
| 178 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 179 | void RenderNode::prepareTreeImpl(TreeInfo& info) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 180 | info.damageAccumulator->pushTransform(this); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 181 | if (info.mode == TreeInfo::MODE_FULL) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 182 | pushStagingPropertiesChanges(info); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 183 | evaluateAnimations(info); |
| 184 | } else if (info.mode == TreeInfo::MODE_MAYBE_DETACHING) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 185 | pushStagingPropertiesChanges(info); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 186 | } else if (info.mode == TreeInfo::MODE_RT_ONLY) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 187 | evaluateAnimations(info); |
| 188 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 189 | |
| 190 | prepareLayer(info); |
| 191 | if (info.mode == TreeInfo::MODE_FULL) { |
| 192 | pushStagingDisplayListChanges(info); |
| 193 | } |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 194 | prepareSubTree(info, mDisplayListData); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 195 | pushLayerUpdate(info); |
| 196 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 197 | info.damageAccumulator->popTransform(); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 198 | } |
| 199 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 200 | class PushAnimatorsFunctor { |
| 201 | public: |
| 202 | PushAnimatorsFunctor(RenderNode* target, TreeInfo& info) |
| 203 | : mTarget(target), mInfo(info) {} |
| 204 | |
| 205 | bool operator() (const sp<BaseRenderNodeAnimator>& animator) { |
| 206 | animator->setupStartValueIfNecessary(mTarget, mInfo); |
| 207 | return animator->isFinished(); |
| 208 | } |
| 209 | private: |
| 210 | RenderNode* mTarget; |
| 211 | TreeInfo& mInfo; |
| 212 | }; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 213 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 214 | void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 215 | // Push the animators first so that setupStartValueIfNecessary() is called |
| 216 | // before properties() is trampled by stagingProperties(), as they are |
| 217 | // required by some animators. |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 218 | if (mNeedsAnimatorsSync) { |
John Reck | 5262266 | 2014-04-30 14:19:56 -0700 | [diff] [blame] | 219 | mAnimators.resize(mStagingAnimators.size()); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 220 | std::vector< sp<BaseRenderNodeAnimator> >::iterator it; |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 221 | PushAnimatorsFunctor functor(this, info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 222 | // hint: this means copy_if_not() |
| 223 | it = std::remove_copy_if(mStagingAnimators.begin(), mStagingAnimators.end(), |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 224 | mAnimators.begin(), functor); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 225 | mAnimators.resize(std::distance(mAnimators.begin(), it)); |
| 226 | } |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 227 | if (mDirtyPropertyFields) { |
| 228 | mDirtyPropertyFields = 0; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 229 | damageSelf(info); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 230 | info.damageAccumulator->popTransform(); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 231 | mProperties = mStagingProperties; |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 232 | applyLayerPropertiesToLayer(info); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 233 | // We could try to be clever and only re-damage if the matrix changed. |
| 234 | // However, we don't need to worry about that. The cost of over-damaging |
| 235 | // here is only going to be a single additional map rect of this node |
| 236 | // plus a rect join(). The parent's transform (and up) will only be |
| 237 | // performed once. |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 238 | info.damageAccumulator->pushTransform(this); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 239 | damageSelf(info); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 240 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 241 | } |
| 242 | |
| 243 | void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) { |
| 244 | if (CC_LIKELY(!mLayer)) return; |
| 245 | |
| 246 | const LayerProperties& props = properties().layerProperties(); |
| 247 | mLayer->setAlpha(props.alpha(), props.xferMode()); |
| 248 | mLayer->setColorFilter(props.colorFilter()); |
| 249 | mLayer->setBlend(props.needsBlending()); |
| 250 | } |
| 251 | |
| 252 | void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) { |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 253 | if (mNeedsDisplayListDataSync) { |
| 254 | mNeedsDisplayListDataSync = false; |
| 255 | // Do a push pass on the old tree to handle freeing DisplayListData |
| 256 | // that are no longer used |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 257 | TreeInfo oldTreeInfo(TreeInfo::MODE_MAYBE_DETACHING); |
| 258 | oldTreeInfo.damageAccumulator = info.damageAccumulator; |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 259 | prepareSubTree(oldTreeInfo, mDisplayListData); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 260 | delete mDisplayListData; |
| 261 | mDisplayListData = mStagingDisplayListData; |
| 262 | mStagingDisplayListData = 0; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 263 | damageSelf(info); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 264 | } |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 265 | } |
| 266 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 267 | class AnimateFunctor { |
| 268 | public: |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 269 | AnimateFunctor(RenderNode* target, TreeInfo& info) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 270 | : mTarget(target), mInfo(info) {} |
| 271 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 272 | bool operator() (const sp<BaseRenderNodeAnimator>& animator) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 273 | return animator->animate(mTarget, mInfo); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 274 | } |
| 275 | private: |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 276 | RenderNode* mTarget; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 277 | TreeInfo& mInfo; |
| 278 | }; |
| 279 | |
| 280 | void RenderNode::evaluateAnimations(TreeInfo& info) { |
| 281 | if (!mAnimators.size()) return; |
| 282 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 283 | // TODO: Can we target this better? For now treat it like any other staging |
| 284 | // property push and just damage self before and after animators are run |
| 285 | |
| 286 | damageSelf(info); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 287 | info.damageAccumulator->popTransform(); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 288 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 289 | AnimateFunctor functor(this, info); |
| 290 | std::vector< sp<BaseRenderNodeAnimator> >::iterator newEnd; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 291 | newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor); |
| 292 | mAnimators.erase(newEnd, mAnimators.end()); |
| 293 | mProperties.updateMatrix(); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 294 | info.out.hasAnimations |= mAnimators.size(); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 295 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 296 | info.damageAccumulator->pushTransform(this); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 297 | damageSelf(info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 298 | } |
| 299 | |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 300 | void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) { |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 301 | if (subtree) { |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 302 | TextureCache& cache = Caches::getInstance().textureCache; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 303 | info.out.hasFunctors |= subtree->functorCount; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 304 | // TODO: Fix ownedBitmapResources to not require disabling prepareTextures |
| 305 | // and thus falling out of async drawing path. |
| 306 | if (subtree->ownedBitmapResources.size()) { |
| 307 | info.prepareTextures = false; |
| 308 | } |
| 309 | for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) { |
| 310 | info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 311 | } |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 312 | for (size_t i = 0; i < subtree->children().size(); i++) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 313 | DrawDisplayListOp* op = subtree->children()[i]; |
| 314 | RenderNode* childNode = op->mDisplayList; |
| 315 | info.damageAccumulator->pushTransform(&op->mTransformFromParent); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 316 | childNode->prepareTreeImpl(info); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 317 | info.damageAccumulator->popTransform(); |
John Reck | 5bf11bb | 2014-03-25 10:22:09 -0700 | [diff] [blame] | 318 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * For property operations, we pass a savecount of 0, since the operations aren't part of the |
| 324 | * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 325 | * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount()) |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 326 | */ |
| 327 | #define PROPERTY_SAVECOUNT 0 |
| 328 | |
| 329 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 330 | void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 331 | #if DEBUG_DISPLAY_LIST |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 332 | properties().debugOutputProperties(handler.level() + 1); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 333 | #endif |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 334 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 335 | renderer.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 336 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 337 | if (properties().getStaticMatrix()) { |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 338 | renderer.concatMatrix(*properties().getStaticMatrix()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 339 | } else if (properties().getAnimationMatrix()) { |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 340 | renderer.concatMatrix(*properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 341 | } |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 342 | if (properties().hasTransformMatrix()) { |
| 343 | if (properties().isTransformTranslateOnly()) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 344 | renderer.translate(properties().getTranslationX(), properties().getTranslationY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 345 | } else { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 346 | renderer.concatMatrix(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 349 | const bool isLayer = properties().layerProperties().type() != kLayerTypeNone; |
| 350 | bool clipToBoundsNeeded = isLayer ? false : properties().getClipToBounds(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 351 | if (properties().getAlpha() < 1) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 352 | if (isLayer) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 353 | renderer.setOverrideLayerAlpha(properties().getAlpha()); |
| 354 | } else if (!properties().getHasOverlappingRendering()) { |
| 355 | renderer.scaleAlpha(properties().getAlpha()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 356 | } else { |
| 357 | // TODO: should be able to store the size of a DL at record time and not |
| 358 | // have to pass it into this call. In fact, this information might be in the |
| 359 | // location/size info that we store with the new native transform data. |
| 360 | int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag; |
| 361 | if (clipToBoundsNeeded) { |
| 362 | saveFlags |= SkCanvas::kClipToLayer_SaveFlag; |
| 363 | clipToBoundsNeeded = false; // clipping done by saveLayer |
| 364 | } |
| 365 | |
| 366 | SaveLayerOp* op = new (handler.allocator()) SaveLayerOp( |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 367 | 0, 0, properties().getWidth(), properties().getHeight(), |
| 368 | properties().getAlpha() * 255, saveFlags); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 369 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | if (clipToBoundsNeeded) { |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 373 | ClipRectOp* op = new (handler.allocator()) ClipRectOp( |
| 374 | 0, 0, properties().getWidth(), properties().getHeight(), SkRegion::kIntersect_Op); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 375 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 376 | } |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 377 | |
| 378 | if (CC_UNLIKELY(properties().hasClippingPath())) { |
Chris Craik | 2bcad17 | 2014-05-14 18:11:23 -0700 | [diff] [blame] | 379 | ClipPathOp* op = new (handler.allocator()) ClipPathOp( |
| 380 | properties().getClippingPath(), properties().getClippingPathOp()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 381 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Apply property-based transformations to input matrix |
| 387 | * |
| 388 | * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4 |
| 389 | * matrix computation instead of the Skia 3x3 matrix + camera hackery. |
| 390 | */ |
| 391 | void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 392 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 393 | matrix.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 394 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 395 | if (properties().getStaticMatrix()) { |
| 396 | mat4 stat(*properties().getStaticMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 397 | matrix.multiply(stat); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 398 | } else if (properties().getAnimationMatrix()) { |
| 399 | mat4 anim(*properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 400 | matrix.multiply(anim); |
| 401 | } |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 402 | |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 403 | bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ()); |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 404 | if (properties().hasTransformMatrix() || applyTranslationZ) { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 405 | if (properties().isTransformTranslateOnly()) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 406 | matrix.translate(properties().getTranslationX(), properties().getTranslationY(), |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 407 | true3dTransform ? properties().getZ() : 0.0f); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 408 | } else { |
| 409 | if (!true3dTransform) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 410 | matrix.multiply(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 411 | } else { |
| 412 | mat4 true3dMat; |
| 413 | true3dMat.loadTranslate( |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 414 | properties().getPivotX() + properties().getTranslationX(), |
| 415 | properties().getPivotY() + properties().getTranslationY(), |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 416 | properties().getZ()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 417 | true3dMat.rotate(properties().getRotationX(), 1, 0, 0); |
| 418 | true3dMat.rotate(properties().getRotationY(), 0, 1, 0); |
| 419 | true3dMat.rotate(properties().getRotation(), 0, 0, 1); |
| 420 | true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1); |
| 421 | true3dMat.translate(-properties().getPivotX(), -properties().getPivotY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 422 | |
| 423 | matrix.multiply(true3dMat); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Organizes the DisplayList hierarchy to prepare for background projection reordering. |
| 431 | * |
| 432 | * This should be called before a call to defer() or drawDisplayList() |
| 433 | * |
| 434 | * Each DisplayList that serves as a 3d root builds its list of composited children, |
| 435 | * which are flagged to not draw in the standard draw loop. |
| 436 | */ |
| 437 | void RenderNode::computeOrdering() { |
| 438 | ATRACE_CALL(); |
| 439 | mProjectedNodes.clear(); |
| 440 | |
| 441 | // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that |
| 442 | // transform properties are applied correctly to top level children |
| 443 | if (mDisplayListData == NULL) return; |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 444 | for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { |
| 445 | DrawDisplayListOp* childOp = mDisplayListData->children()[i]; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 446 | childOp->mDisplayList->computeOrderingImpl(childOp, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 447 | properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | void RenderNode::computeOrderingImpl( |
| 452 | DrawDisplayListOp* opState, |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 453 | const SkPath* outlineOfProjectionSurface, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 454 | Vector<DrawDisplayListOp*>* compositedChildrenOfProjectionSurface, |
| 455 | const mat4* transformFromProjectionSurface) { |
| 456 | mProjectedNodes.clear(); |
| 457 | if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return; |
| 458 | |
| 459 | // TODO: should avoid this calculation in most cases |
| 460 | // TODO: just calculate single matrix, down to all leaf composited elements |
| 461 | Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface); |
| 462 | localTransformFromProjectionSurface.multiply(opState->mTransformFromParent); |
| 463 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 464 | if (properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 465 | // composited projectee, flag for out of order draw, save matrix, and store in proj surface |
| 466 | opState->mSkipInOrderDraw = true; |
| 467 | opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface); |
| 468 | compositedChildrenOfProjectionSurface->add(opState); |
| 469 | } else { |
| 470 | // standard in order draw |
| 471 | opState->mSkipInOrderDraw = false; |
| 472 | } |
| 473 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 474 | if (mDisplayListData->children().size() > 0) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 475 | const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0; |
| 476 | bool haveAppliedPropertiesToProjection = false; |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 477 | for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { |
| 478 | DrawDisplayListOp* childOp = mDisplayListData->children()[i]; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 479 | RenderNode* child = childOp->mDisplayList; |
| 480 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 481 | const SkPath* projectionOutline = NULL; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 482 | Vector<DrawDisplayListOp*>* projectionChildren = NULL; |
| 483 | const mat4* projectionTransform = NULL; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 484 | if (isProjectionReceiver && !child->properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 485 | // if receiving projections, collect projecting descendent |
| 486 | |
| 487 | // Note that if a direct descendent is projecting backwards, we pass it's |
| 488 | // grandparent projection collection, since it shouldn't project onto it's |
| 489 | // parent, where it will already be drawing. |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 490 | projectionOutline = properties().getOutline().getPath(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 491 | projectionChildren = &mProjectedNodes; |
| 492 | projectionTransform = &mat4::identity(); |
| 493 | } else { |
| 494 | if (!haveAppliedPropertiesToProjection) { |
| 495 | applyViewPropertyTransforms(localTransformFromProjectionSurface); |
| 496 | haveAppliedPropertiesToProjection = true; |
| 497 | } |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 498 | projectionOutline = outlineOfProjectionSurface; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 499 | projectionChildren = compositedChildrenOfProjectionSurface; |
| 500 | projectionTransform = &localTransformFromProjectionSurface; |
| 501 | } |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 502 | child->computeOrderingImpl(childOp, |
| 503 | projectionOutline, projectionChildren, projectionTransform); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | class DeferOperationHandler { |
| 509 | public: |
| 510 | DeferOperationHandler(DeferStateStruct& deferStruct, int level) |
| 511 | : mDeferStruct(deferStruct), mLevel(level) {} |
| 512 | inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { |
| 513 | operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds); |
| 514 | } |
| 515 | inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); } |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 516 | inline void startMark(const char* name) {} // do nothing |
| 517 | inline void endMark() {} |
| 518 | inline int level() { return mLevel; } |
| 519 | inline int replayFlags() { return mDeferStruct.mReplayFlags; } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 520 | |
| 521 | private: |
| 522 | DeferStateStruct& mDeferStruct; |
| 523 | const int mLevel; |
| 524 | }; |
| 525 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 526 | void RenderNode::deferNodeTree(DeferStateStruct& deferStruct) { |
| 527 | DeferOperationHandler handler(deferStruct, 0); |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 528 | if (MathUtils::isPositive(properties().getZ())) { |
| 529 | issueDrawShadowOperation(Matrix4::identity(), handler); |
| 530 | } |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 531 | issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); |
| 532 | } |
| 533 | |
| 534 | void RenderNode::deferNodeInParent(DeferStateStruct& deferStruct, const int level) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 535 | DeferOperationHandler handler(deferStruct, level); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 536 | issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | class ReplayOperationHandler { |
| 540 | public: |
| 541 | ReplayOperationHandler(ReplayStateStruct& replayStruct, int level) |
| 542 | : mReplayStruct(replayStruct), mLevel(level) {} |
| 543 | inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { |
| 544 | #if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 545 | mReplayStruct.mRenderer.eventMark(operation->name()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 546 | #endif |
| 547 | operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds); |
| 548 | } |
| 549 | inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); } |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 550 | inline void startMark(const char* name) { |
| 551 | mReplayStruct.mRenderer.startMark(name); |
| 552 | } |
| 553 | inline void endMark() { |
| 554 | mReplayStruct.mRenderer.endMark(); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 555 | } |
| 556 | inline int level() { return mLevel; } |
| 557 | inline int replayFlags() { return mReplayStruct.mReplayFlags; } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 558 | |
| 559 | private: |
| 560 | ReplayStateStruct& mReplayStruct; |
| 561 | const int mLevel; |
| 562 | }; |
| 563 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 564 | void RenderNode::replayNodeTree(ReplayStateStruct& replayStruct) { |
| 565 | ReplayOperationHandler handler(replayStruct, 0); |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 566 | if (MathUtils::isPositive(properties().getZ())) { |
| 567 | issueDrawShadowOperation(Matrix4::identity(), handler); |
| 568 | } |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 569 | issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); |
| 570 | } |
| 571 | |
| 572 | void RenderNode::replayNodeInParent(ReplayStateStruct& replayStruct, const int level) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 573 | ReplayOperationHandler handler(replayStruct, level); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 574 | issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | void RenderNode::buildZSortedChildList(Vector<ZDrawDisplayListOpPair>& zTranslatedNodes) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 578 | if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 579 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 580 | for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { |
| 581 | DrawDisplayListOp* childOp = mDisplayListData->children()[i]; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 582 | RenderNode* child = childOp->mDisplayList; |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 583 | float childZ = child->properties().getZ(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 584 | |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 585 | if (!MathUtils::isZero(childZ)) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 586 | zTranslatedNodes.add(ZDrawDisplayListOpPair(childZ, childOp)); |
| 587 | childOp->mSkipInOrderDraw = true; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 588 | } else if (!child->properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 589 | // regular, in order drawing DisplayList |
| 590 | childOp->mSkipInOrderDraw = false; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order) |
| 595 | std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end()); |
| 596 | } |
| 597 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 598 | template <class T> |
| 599 | void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) { |
Chris Craik | 6131732 | 2014-05-21 13:03:52 -0700 | [diff] [blame] | 600 | if (properties().getAlpha() <= 0.0f || properties().getOutline().isEmpty()) return; |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 601 | |
| 602 | mat4 shadowMatrixXY(transformFromParent); |
| 603 | applyViewPropertyTransforms(shadowMatrixXY); |
| 604 | |
| 605 | // Z matrix needs actual 3d transformation, so mapped z values will be correct |
| 606 | mat4 shadowMatrixZ(transformFromParent); |
| 607 | applyViewPropertyTransforms(shadowMatrixZ, true); |
| 608 | |
| 609 | const SkPath* outlinePath = properties().getOutline().getPath(); |
| 610 | const RevealClip& revealClip = properties().getRevealClip(); |
| 611 | const SkPath* revealClipPath = revealClip.hasConvexClip() |
| 612 | ? revealClip.getPath() : NULL; // only pass the reveal clip's path if it's convex |
| 613 | |
Chris Craik | 6131732 | 2014-05-21 13:03:52 -0700 | [diff] [blame] | 614 | if (revealClipPath && revealClipPath->isEmpty()) return; |
| 615 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 616 | /** |
| 617 | * The drawing area of the caster is always the same as the its perimeter (which |
| 618 | * the shadow system uses) *except* in the inverse clip case. Inform the shadow |
| 619 | * system that the caster's drawing area (as opposed to its perimeter) has been |
| 620 | * clipped, so that it knows the caster can't be opaque. |
| 621 | */ |
| 622 | bool casterUnclipped = !revealClip.willClip() || revealClip.hasConvexClip(); |
| 623 | |
| 624 | DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp( |
| 625 | shadowMatrixXY, shadowMatrixZ, |
| 626 | properties().getAlpha(), casterUnclipped, |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 627 | outlinePath, revealClipPath); |
| 628 | handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 629 | } |
| 630 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 631 | #define SHADOW_DELTA 0.1f |
| 632 | |
| 633 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 634 | void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawDisplayListOpPair>& zTranslatedNodes, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 635 | ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) { |
| 636 | const int size = zTranslatedNodes.size(); |
| 637 | if (size == 0 |
| 638 | || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f) |
| 639 | || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) { |
| 640 | // no 3d children to draw |
| 641 | return; |
| 642 | } |
| 643 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 644 | /** |
| 645 | * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters |
| 646 | * with very similar Z heights to draw together. |
| 647 | * |
| 648 | * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are |
| 649 | * underneath both, and neither's shadow is drawn on top of the other. |
| 650 | */ |
| 651 | const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); |
| 652 | size_t drawIndex, shadowIndex, endIndex; |
| 653 | if (mode == kNegativeZChildren) { |
| 654 | drawIndex = 0; |
| 655 | endIndex = nonNegativeIndex; |
| 656 | shadowIndex = endIndex; // draw no shadows |
| 657 | } else { |
| 658 | drawIndex = nonNegativeIndex; |
| 659 | endIndex = size; |
| 660 | shadowIndex = drawIndex; // potentially draw shadow for each pos Z child |
| 661 | } |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 662 | |
| 663 | DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "", |
| 664 | endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive"); |
| 665 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 666 | float lastCasterZ = 0.0f; |
| 667 | while (shadowIndex < endIndex || drawIndex < endIndex) { |
| 668 | if (shadowIndex < endIndex) { |
| 669 | DrawDisplayListOp* casterOp = zTranslatedNodes[shadowIndex].value; |
| 670 | RenderNode* caster = casterOp->mDisplayList; |
| 671 | const float casterZ = zTranslatedNodes[shadowIndex].key; |
| 672 | // attempt to render the shadow if the caster about to be drawn is its caster, |
| 673 | // OR if its caster's Z value is similar to the previous potential caster |
| 674 | if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) { |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 675 | caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 676 | |
| 677 | lastCasterZ = casterZ; // must do this even if current caster not casting a shadow |
| 678 | shadowIndex++; |
| 679 | continue; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | // only the actual child DL draw needs to be in save/restore, |
| 684 | // since it modifies the renderer's matrix |
| 685 | int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
| 686 | |
| 687 | DrawDisplayListOp* childOp = zTranslatedNodes[drawIndex].value; |
| 688 | RenderNode* child = childOp->mDisplayList; |
| 689 | |
| 690 | renderer.concatMatrix(childOp->mTransformFromParent); |
| 691 | childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 692 | handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 693 | childOp->mSkipInOrderDraw = true; |
| 694 | |
| 695 | renderer.restoreToCount(restoreTo); |
| 696 | drawIndex++; |
| 697 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 701 | void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) { |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 702 | DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size()); |
| 703 | const SkPath* projectionReceiverOutline = properties().getOutline().getPath(); |
| 704 | bool maskProjecteesWithPath = projectionReceiverOutline != NULL |
| 705 | && !projectionReceiverOutline->isRect(NULL); |
| 706 | int restoreTo = renderer.getSaveCount(); |
| 707 | |
| 708 | // If the projection reciever has an outline, we mask each of the projected rendernodes to it |
| 709 | // Either with clipRect, or special saveLayer masking |
| 710 | LinearAllocator& alloc = handler.allocator(); |
| 711 | if (projectionReceiverOutline != NULL) { |
| 712 | const SkRect& outlineBounds = projectionReceiverOutline->getBounds(); |
| 713 | if (projectionReceiverOutline->isRect(NULL)) { |
| 714 | // mask to the rect outline simply with clipRect |
| 715 | handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), |
| 716 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 717 | ClipRectOp* clipOp = new (alloc) ClipRectOp( |
| 718 | outlineBounds.left(), outlineBounds.top(), |
| 719 | outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op); |
| 720 | handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 721 | } else { |
| 722 | // wrap the projected RenderNodes with a SaveLayer that will mask to the outline |
| 723 | SaveLayerOp* op = new (alloc) SaveLayerOp( |
| 724 | outlineBounds.left(), outlineBounds.top(), |
| 725 | outlineBounds.right(), outlineBounds.bottom(), |
| 726 | 255, SkCanvas::kARGB_ClipLayer_SaveFlag); |
| 727 | op->setMask(projectionReceiverOutline); |
| 728 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 729 | |
| 730 | /* TODO: add optimizations here to take advantage of placement/size of projected |
| 731 | * children (which may shrink saveLayer area significantly). This is dependent on |
| 732 | * passing actual drawing/dirtying bounds of projected content down to native. |
| 733 | */ |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | // draw projected nodes |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 738 | for (size_t i = 0; i < mProjectedNodes.size(); i++) { |
| 739 | DrawDisplayListOp* childOp = mProjectedNodes[i]; |
| 740 | |
| 741 | // matrix save, concat, and restore can be done safely without allocating operations |
| 742 | int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
| 743 | renderer.concatMatrix(childOp->mTransformFromCompositingAncestor); |
| 744 | childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 745 | handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 746 | childOp->mSkipInOrderDraw = true; |
| 747 | renderer.restoreToCount(restoreTo); |
| 748 | } |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 749 | |
| 750 | if (projectionReceiverOutline != NULL) { |
| 751 | handler(new (alloc) RestoreToCountOp(restoreTo), |
| 752 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 753 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /** |
| 757 | * This function serves both defer and replay modes, and will organize the displayList's component |
| 758 | * operations for a single frame: |
| 759 | * |
| 760 | * Every 'simple' state operation that affects just the matrix and alpha (or other factors of |
| 761 | * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom |
| 762 | * defer logic) and operations in displayListOps are issued through the 'handler' which handles the |
| 763 | * defer vs replay logic, per operation |
| 764 | */ |
| 765 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 766 | void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 767 | const bool drawLayer = (mLayer && (&renderer != mLayer->renderer)); |
| 768 | // If we are updating the contents of mLayer, we don't want to apply any of |
| 769 | // the RenderNode's properties to this issueOperations pass. Those will all |
| 770 | // be applied when the layer is drawn, aka when this is true. |
| 771 | const bool useViewProperties = (!mLayer || drawLayer); |
| 772 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 773 | const int level = handler.level(); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 774 | if (mDisplayListData->isEmpty() || (useViewProperties && properties().getAlpha() <= 0)) { |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 775 | DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 779 | handler.startMark(getName()); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 780 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 781 | #if DEBUG_DISPLAY_LIST |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 782 | const Rect& clipRect = renderer.getLocalClipBounds(); |
| 783 | DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f", |
| 784 | level * 2, "", this, getName(), |
| 785 | clipRect.left, clipRect.top, clipRect.right, clipRect.bottom); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 786 | #endif |
| 787 | |
| 788 | LinearAllocator& alloc = handler.allocator(); |
| 789 | int restoreTo = renderer.getSaveCount(); |
| 790 | handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 791 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 792 | |
| 793 | DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "", |
| 794 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo); |
| 795 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 796 | if (useViewProperties) { |
| 797 | setViewProperties<T>(renderer, handler); |
| 798 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 799 | |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 800 | bool quickRejected = properties().getClipToBounds() |
| 801 | && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 802 | if (!quickRejected) { |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 803 | if (mProperties.getOutline().willClip()) { |
| 804 | renderer.setClippingOutline(alloc, &(mProperties.getOutline())); |
| 805 | } |
| 806 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 807 | if (drawLayer) { |
| 808 | handler(new (alloc) DrawLayerOp(mLayer, 0, 0), |
| 809 | renderer.getSaveCount() - 1, properties().getClipToBounds()); |
| 810 | } else { |
| 811 | Vector<ZDrawDisplayListOpPair> zTranslatedNodes; |
| 812 | buildZSortedChildList(zTranslatedNodes); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 813 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 814 | // for 3d root, draw children with negative z values |
| 815 | issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 816 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 817 | DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance(); |
| 818 | const int saveCountOffset = renderer.getSaveCount() - 1; |
| 819 | const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex; |
| 820 | for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { |
| 821 | DisplayListOp *op = mDisplayListData->displayListOps[i]; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 822 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 823 | #if DEBUG_DISPLAY_LIST |
| 824 | op->output(level + 1); |
| 825 | #endif |
| 826 | logBuffer.writeCommand(level, op->name()); |
| 827 | handler(op, saveCountOffset, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 828 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 829 | if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) { |
| 830 | issueOperationsOfProjectedChildren(renderer, handler); |
| 831 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 832 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 833 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame^] | 834 | // for 3d root, draw children with positive z values |
| 835 | issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler); |
| 836 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo); |
| 840 | handler(new (alloc) RestoreToCountOp(restoreTo), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 841 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 842 | renderer.setOverrideLayerAlpha(1.0f); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 843 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 844 | DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName()); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 845 | handler.endMark(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | } /* namespace uirenderer */ |
| 849 | } /* namespace android */ |