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 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 17 | #include "RenderNode.h" |
| 18 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 19 | #include <algorithm> |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 20 | #include <string> |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 21 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 22 | #include <SkCanvas.h> |
| 23 | #include <algorithm> |
| 24 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 25 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 26 | #include "DamageAccumulator.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 27 | #include "Debug.h" |
| 28 | #include "DisplayListOp.h" |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 29 | #include "LayerRenderer.h" |
| 30 | #include "OpenGLRenderer.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 31 | #include "TreeInfo.h" |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 32 | #include "utils/MathUtils.h" |
Chris Craik | 70850ea | 2014-11-18 10:49:23 -0800 | [diff] [blame] | 33 | #include "utils/TraceUtils.h" |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 34 | #include "renderthread/CanvasContext.h" |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 35 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 36 | #include "protos/hwui.pb.h" |
| 37 | #include "protos/ProtoHelpers.h" |
| 38 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 42 | void RenderNode::debugDumpLayers(const char* prefix) { |
| 43 | if (mLayer) { |
| 44 | ALOGD("%sNode %p (%s) has layer %p (fbo = %u, wasBuildLayered = %s)", |
| 45 | prefix, this, getName(), mLayer, mLayer->getFbo(), |
| 46 | mLayer->wasBuildLayered ? "true" : "false"); |
| 47 | } |
| 48 | if (mDisplayListData) { |
| 49 | for (size_t i = 0; i < mDisplayListData->children().size(); i++) { |
| 50 | mDisplayListData->children()[i]->mRenderNode->debugDumpLayers(prefix); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 55 | RenderNode::RenderNode() |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 56 | : mDirtyPropertyFields(0) |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 57 | , mNeedsDisplayListDataSync(false) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 58 | , mDisplayListData(nullptr) |
| 59 | , mStagingDisplayListData(nullptr) |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 60 | , mAnimatorManager(*this) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 61 | , mLayer(nullptr) |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 62 | , mParentCount(0) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | RenderNode::~RenderNode() { |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 66 | deleteDisplayListData(); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 67 | delete mStagingDisplayListData; |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 68 | if (mLayer) { |
| 69 | ALOGW("Memory Warning: Layer %p missed its detachment, held on to for far too long!", mLayer); |
| 70 | mLayer->postDecStrong(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 71 | mLayer = nullptr; |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 72 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 73 | } |
| 74 | |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 75 | void RenderNode::setStagingDisplayList(DisplayListData* data) { |
| 76 | mNeedsDisplayListDataSync = true; |
| 77 | delete mStagingDisplayListData; |
| 78 | mStagingDisplayListData = data; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * This function is a simplified version of replay(), where we simply retrieve and log the |
| 83 | * display list. This function should remain in sync with the replay() function. |
| 84 | */ |
| 85 | void RenderNode::output(uint32_t level) { |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 86 | ALOGD("%*sStart display list (%p, %s%s%s%s%s%s)", (level - 1) * 2, "", this, |
Chris Craik | b5a5435 | 2014-11-21 14:54:35 -0800 | [diff] [blame] | 87 | getName(), |
Chris Craik | 43a1d31 | 2015-05-27 11:28:14 -0700 | [diff] [blame] | 88 | (MathUtils::isZero(properties().getAlpha()) ? ", zero alpha" : ""), |
Chris Craik | b5a5435 | 2014-11-21 14:54:35 -0800 | [diff] [blame] | 89 | (properties().hasShadow() ? ", casting shadow" : ""), |
| 90 | (isRenderable() ? "" : ", empty"), |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 91 | (properties().getProjectBackwards() ? ", projected" : ""), |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 92 | (mLayer != nullptr ? ", on HW Layer" : "")); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 93 | ALOGD("%*s%s %d", level * 2, "", "Save", |
| 94 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
| 95 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 96 | properties().debugOutputProperties(level); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 97 | int flags = DisplayListOp::kOpLogFlag_Recurse; |
John Reck | dc0349b | 2014-08-06 15:28:07 -0700 | [diff] [blame] | 98 | if (mDisplayListData) { |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 99 | // TODO: consider printing the chunk boundaries here |
John Reck | dc0349b | 2014-08-06 15:28:07 -0700 | [diff] [blame] | 100 | for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) { |
| 101 | mDisplayListData->displayListOps[i]->output(level, flags); |
| 102 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 105 | ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 106 | } |
| 107 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 108 | void RenderNode::copyTo(proto::RenderNode *pnode) { |
| 109 | pnode->set_id(static_cast<uint64_t>( |
| 110 | reinterpret_cast<uintptr_t>(this))); |
| 111 | pnode->set_name(mName.string(), mName.length()); |
| 112 | |
| 113 | proto::RenderProperties* pprops = pnode->mutable_properties(); |
| 114 | pprops->set_left(properties().getLeft()); |
| 115 | pprops->set_top(properties().getTop()); |
| 116 | pprops->set_right(properties().getRight()); |
| 117 | pprops->set_bottom(properties().getBottom()); |
| 118 | pprops->set_clip_flags(properties().getClippingFlags()); |
| 119 | pprops->set_alpha(properties().getAlpha()); |
| 120 | pprops->set_translation_x(properties().getTranslationX()); |
| 121 | pprops->set_translation_y(properties().getTranslationY()); |
| 122 | pprops->set_translation_z(properties().getTranslationZ()); |
| 123 | pprops->set_elevation(properties().getElevation()); |
| 124 | pprops->set_rotation(properties().getRotation()); |
| 125 | pprops->set_rotation_x(properties().getRotationX()); |
| 126 | pprops->set_rotation_y(properties().getRotationY()); |
| 127 | pprops->set_scale_x(properties().getScaleX()); |
| 128 | pprops->set_scale_y(properties().getScaleY()); |
| 129 | pprops->set_pivot_x(properties().getPivotX()); |
| 130 | pprops->set_pivot_y(properties().getPivotY()); |
| 131 | pprops->set_has_overlapping_rendering(properties().getHasOverlappingRendering()); |
| 132 | pprops->set_pivot_explicitly_set(properties().isPivotExplicitlySet()); |
| 133 | pprops->set_project_backwards(properties().getProjectBackwards()); |
| 134 | pprops->set_projection_receiver(properties().isProjectionReceiver()); |
| 135 | set(pprops->mutable_clip_bounds(), properties().getClipBounds()); |
| 136 | |
| 137 | const Outline& outline = properties().getOutline(); |
| 138 | if (outline.getType() != Outline::Type::None) { |
| 139 | proto::Outline* poutline = pprops->mutable_outline(); |
| 140 | poutline->clear_path(); |
| 141 | if (outline.getType() == Outline::Type::Empty) { |
| 142 | poutline->set_type(proto::Outline_Type_Empty); |
| 143 | } else if (outline.getType() == Outline::Type::ConvexPath) { |
| 144 | poutline->set_type(proto::Outline_Type_ConvexPath); |
| 145 | if (const SkPath* path = outline.getPath()) { |
| 146 | set(poutline->mutable_path(), *path); |
| 147 | } |
| 148 | } else if (outline.getType() == Outline::Type::RoundRect) { |
| 149 | poutline->set_type(proto::Outline_Type_RoundRect); |
| 150 | } else { |
| 151 | ALOGW("Uknown outline type! %d", static_cast<int>(outline.getType())); |
| 152 | poutline->set_type(proto::Outline_Type_None); |
| 153 | } |
| 154 | poutline->set_should_clip(outline.getShouldClip()); |
| 155 | poutline->set_alpha(outline.getAlpha()); |
| 156 | poutline->set_radius(outline.getRadius()); |
| 157 | set(poutline->mutable_bounds(), outline.getBounds()); |
| 158 | } else { |
| 159 | pprops->clear_outline(); |
| 160 | } |
| 161 | |
| 162 | const RevealClip& revealClip = properties().getRevealClip(); |
| 163 | if (revealClip.willClip()) { |
| 164 | proto::RevealClip* prevealClip = pprops->mutable_reveal_clip(); |
| 165 | prevealClip->set_x(revealClip.getX()); |
| 166 | prevealClip->set_y(revealClip.getY()); |
| 167 | prevealClip->set_radius(revealClip.getRadius()); |
| 168 | } else { |
| 169 | pprops->clear_reveal_clip(); |
| 170 | } |
| 171 | |
| 172 | pnode->clear_children(); |
| 173 | if (mDisplayListData) { |
| 174 | for (auto&& child : mDisplayListData->children()) { |
| 175 | child->mRenderNode->copyTo(pnode->add_children()); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 180 | int RenderNode::getDebugSize() { |
| 181 | int size = sizeof(RenderNode); |
| 182 | if (mStagingDisplayListData) { |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 183 | size += mStagingDisplayListData->getUsedSize(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 184 | } |
| 185 | if (mDisplayListData && mDisplayListData != mStagingDisplayListData) { |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 186 | size += mDisplayListData->getUsedSize(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 187 | } |
| 188 | return size; |
| 189 | } |
| 190 | |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 191 | void RenderNode::prepareTree(TreeInfo& info) { |
| 192 | ATRACE_CALL(); |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 193 | LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing"); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 194 | |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 195 | // Functors don't correctly handle stencil usage of overdraw debugging - shove 'em in a layer. |
| 196 | bool functorsNeedLayer = Properties::debugOverdraw; |
| 197 | |
| 198 | prepareTreeImpl(info, functorsNeedLayer); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 199 | } |
| 200 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 201 | void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) { |
| 202 | mAnimatorManager.addAnimator(animator); |
| 203 | } |
| 204 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 205 | void RenderNode::damageSelf(TreeInfo& info) { |
John Reck | ce9f308 | 2014-06-17 16:18:09 -0700 | [diff] [blame] | 206 | if (isRenderable()) { |
John Reck | 293e868 | 2014-06-17 10:34:02 -0700 | [diff] [blame] | 207 | if (properties().getClipDamageToBounds()) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 208 | info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight()); |
| 209 | } else { |
| 210 | // Hope this is big enough? |
| 211 | // TODO: Get this from the display list ops or something |
John Reck | c128823 | 2015-08-12 13:39:11 -0700 | [diff] [blame] | 212 | info.damageAccumulator->dirty(DIRTY_MIN, DIRTY_MIN, DIRTY_MAX, DIRTY_MAX); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 213 | } |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 217 | void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) { |
Chris Craik | 856f0cc | 2015-04-21 15:13:29 -0700 | [diff] [blame] | 218 | LayerType layerType = properties().effectiveLayerType(); |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 219 | if (CC_UNLIKELY(layerType == LayerType::RenderLayer)) { |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 220 | // Damage applied so far needs to affect our parent, but does not require |
| 221 | // the layer to be updated. So we pop/push here to clear out the current |
| 222 | // damage and get a clean state for display list or children updates to |
| 223 | // affect, which will require the layer to be updated |
| 224 | info.damageAccumulator->popTransform(); |
| 225 | info.damageAccumulator->pushTransform(this); |
| 226 | if (dirtyMask & DISPLAY_LIST) { |
| 227 | damageSelf(info); |
| 228 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | void RenderNode::pushLayerUpdate(TreeInfo& info) { |
Chris Craik | 856f0cc | 2015-04-21 15:13:29 -0700 | [diff] [blame] | 233 | LayerType layerType = properties().effectiveLayerType(); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 234 | // If we are not a layer OR we cannot be rendered (eg, view was detached) |
| 235 | // we need to destroy any Layers we may have had previously |
Chris Craik | 182952f | 2015-03-09 14:17:29 -0700 | [diff] [blame] | 236 | if (CC_LIKELY(layerType != LayerType::RenderLayer) || CC_UNLIKELY(!isRenderable())) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 237 | if (CC_UNLIKELY(mLayer)) { |
| 238 | LayerRenderer::destroyLayer(mLayer); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 239 | mLayer = nullptr; |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 240 | } |
| 241 | return; |
| 242 | } |
| 243 | |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 244 | bool transformUpdateNeeded = false; |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 245 | if (!mLayer) { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 246 | mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight()); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 247 | applyLayerPropertiesToLayer(info); |
| 248 | damageSelf(info); |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 249 | transformUpdateNeeded = true; |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 250 | } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) { |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 251 | if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) { |
| 252 | LayerRenderer::destroyLayer(mLayer); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 253 | mLayer = nullptr; |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 254 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 255 | damageSelf(info); |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 256 | transformUpdateNeeded = true; |
| 257 | } |
| 258 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 259 | SkRect dirty; |
| 260 | info.damageAccumulator->peekAtDirty(&dirty); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 261 | |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 262 | if (!mLayer) { |
John Reck | 0e89e2b | 2014-10-31 14:49:06 -0700 | [diff] [blame] | 263 | Caches::getInstance().dumpMemoryUsage(); |
John Reck | c25e506 | 2014-06-18 14:21:29 -0700 | [diff] [blame] | 264 | if (info.errorHandler) { |
| 265 | std::string msg = "Unable to create layer for "; |
| 266 | msg += getName(); |
| 267 | info.errorHandler->onError(msg); |
| 268 | } |
| 269 | return; |
| 270 | } |
| 271 | |
Chris Craik | c71bfca | 2014-08-21 10:18:58 -0700 | [diff] [blame] | 272 | if (transformUpdateNeeded) { |
| 273 | // update the transform in window of the layer to reset its origin wrt light source position |
| 274 | Matrix4 windowTransform; |
| 275 | info.damageAccumulator->computeCurrentTransform(&windowTransform); |
| 276 | mLayer->setWindowTransform(windowTransform); |
| 277 | } |
John Reck | c79eabc | 2014-08-05 11:03:42 -0700 | [diff] [blame] | 278 | |
| 279 | if (dirty.intersect(0, 0, getWidth(), getHeight())) { |
Mike Reed | 71487eb | 2014-11-19 16:13:20 -0500 | [diff] [blame] | 280 | dirty.roundOut(&dirty); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 281 | mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom); |
| 282 | } |
| 283 | // This is not inside the above if because we may have called |
| 284 | // updateDeferred on a previous prepare pass that didn't have a renderer |
| 285 | if (info.renderer && mLayer->deferredUpdateScheduled) { |
| 286 | info.renderer->pushLayerUpdate(mLayer); |
| 287 | } |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 288 | |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 289 | if (info.canvasContext) { |
| 290 | // There might be prefetched layers that need to be accounted for. |
| 291 | // That might be us, so tell CanvasContext that this layer is in the |
| 292 | // tree and should not be destroyed. |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 293 | info.canvasContext->markLayerInUse(this); |
| 294 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Traverse down the the draw tree to prepare for a frame. |
| 299 | * |
| 300 | * MODE_FULL = UI Thread-driven (thus properties must be synced), otherwise RT driven |
| 301 | * |
| 302 | * While traversing down the tree, functorsNeedLayer flag is set to true if anything that uses the |
| 303 | * stencil buffer may be needed. Views that use a functor to draw will be forced onto a layer. |
| 304 | */ |
| 305 | void RenderNode::prepareTreeImpl(TreeInfo& info, bool functorsNeedLayer) { |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 306 | info.damageAccumulator->pushTransform(this); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 307 | |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 308 | if (info.mode == TreeInfo::MODE_FULL) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 309 | pushStagingPropertiesChanges(info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 310 | } |
John Reck | 9eb9f6f | 2014-08-21 11:23:05 -0700 | [diff] [blame] | 311 | uint32_t animatorDirtyMask = 0; |
| 312 | if (CC_LIKELY(info.runAnimations)) { |
| 313 | animatorDirtyMask = mAnimatorManager.animate(info); |
| 314 | } |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 315 | |
John Reck | 3f725f0 | 2015-06-16 10:29:31 -0700 | [diff] [blame] | 316 | bool willHaveFunctor = false; |
| 317 | if (info.mode == TreeInfo::MODE_FULL && mStagingDisplayListData) { |
| 318 | willHaveFunctor = !mStagingDisplayListData->functors.isEmpty(); |
| 319 | } else if (mDisplayListData) { |
| 320 | willHaveFunctor = !mDisplayListData->functors.isEmpty(); |
| 321 | } |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 322 | bool childFunctorsNeedLayer = mProperties.prepareForFunctorPresence( |
| 323 | willHaveFunctor, functorsNeedLayer); |
| 324 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 325 | prepareLayer(info, animatorDirtyMask); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 326 | if (info.mode == TreeInfo::MODE_FULL) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 327 | pushStagingDisplayListChanges(info); |
| 328 | } |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 329 | prepareSubTree(info, childFunctorsNeedLayer, mDisplayListData); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 330 | pushLayerUpdate(info); |
| 331 | |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 332 | info.damageAccumulator->popTransform(); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 333 | } |
| 334 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 335 | void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 336 | // Push the animators first so that setupStartValueIfNecessary() is called |
| 337 | // before properties() is trampled by stagingProperties(), as they are |
| 338 | // required by some animators. |
John Reck | 9eb9f6f | 2014-08-21 11:23:05 -0700 | [diff] [blame] | 339 | if (CC_LIKELY(info.runAnimations)) { |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 340 | mAnimatorManager.pushStaging(); |
John Reck | 9eb9f6f | 2014-08-21 11:23:05 -0700 | [diff] [blame] | 341 | } |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 342 | if (mDirtyPropertyFields) { |
| 343 | mDirtyPropertyFields = 0; |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 344 | damageSelf(info); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 345 | info.damageAccumulator->popTransform(); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 346 | mProperties = mStagingProperties; |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 347 | applyLayerPropertiesToLayer(info); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 348 | // We could try to be clever and only re-damage if the matrix changed. |
| 349 | // However, we don't need to worry about that. The cost of over-damaging |
| 350 | // here is only going to be a single additional map rect of this node |
| 351 | // plus a rect join(). The parent's transform (and up) will only be |
| 352 | // performed once. |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 353 | info.damageAccumulator->pushTransform(this); |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 354 | damageSelf(info); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 355 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 358 | void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 359 | if (CC_LIKELY(!mLayer)) return; |
| 360 | |
| 361 | const LayerProperties& props = properties().layerProperties(); |
| 362 | mLayer->setAlpha(props.alpha(), props.xferMode()); |
| 363 | mLayer->setColorFilter(props.colorFilter()); |
| 364 | mLayer->setBlend(props.needsBlending()); |
| 365 | } |
| 366 | |
| 367 | void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) { |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 368 | if (mNeedsDisplayListDataSync) { |
| 369 | mNeedsDisplayListDataSync = false; |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 370 | // Make sure we inc first so that we don't fluctuate between 0 and 1, |
| 371 | // which would thrash the layer cache |
| 372 | if (mStagingDisplayListData) { |
| 373 | for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) { |
| 374 | mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount(); |
| 375 | } |
| 376 | } |
John Reck | 5c9d717 | 2014-10-22 11:32:27 -0700 | [diff] [blame] | 377 | // Damage with the old display list first then the new one to catch any |
| 378 | // changes in isRenderable or, in the future, bounds |
| 379 | damageSelf(info); |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 380 | deleteDisplayListData(); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 381 | mDisplayListData = mStagingDisplayListData; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 382 | mStagingDisplayListData = nullptr; |
John Reck | 09d5cdd | 2014-07-24 10:36:08 -0700 | [diff] [blame] | 383 | if (mDisplayListData) { |
| 384 | for (size_t i = 0; i < mDisplayListData->functors.size(); i++) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 385 | (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, nullptr); |
John Reck | 09d5cdd | 2014-07-24 10:36:08 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 388 | damageSelf(info); |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 389 | } |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 390 | } |
| 391 | |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 392 | void RenderNode::deleteDisplayListData() { |
| 393 | if (mDisplayListData) { |
| 394 | for (size_t i = 0; i < mDisplayListData->children().size(); i++) { |
| 395 | mDisplayListData->children()[i]->mRenderNode->decParentRefCount(); |
| 396 | } |
| 397 | } |
| 398 | delete mDisplayListData; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 399 | mDisplayListData = nullptr; |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 402 | void RenderNode::prepareSubTree(TreeInfo& info, bool functorsNeedLayer, DisplayListData* subtree) { |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 403 | if (subtree) { |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 404 | TextureCache& cache = Caches::getInstance().textureCache; |
John Reck | 09d5cdd | 2014-07-24 10:36:08 -0700 | [diff] [blame] | 405 | info.out.hasFunctors |= subtree->functors.size(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 406 | for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) { |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 407 | info.prepareTextures = cache.prefetchAndMarkInUse( |
| 408 | info.canvasContext, subtree->bitmapResources[i]); |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 409 | } |
John Reck | 8de65a8 | 2014-04-09 15:23:38 -0700 | [diff] [blame] | 410 | for (size_t i = 0; i < subtree->children().size(); i++) { |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 411 | DrawRenderNodeOp* op = subtree->children()[i]; |
| 412 | RenderNode* childNode = op->mRenderNode; |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 413 | info.damageAccumulator->pushTransform(&op->mTransformFromParent); |
Chris Craik | a766cb2 | 2015-06-08 16:49:43 -0700 | [diff] [blame] | 414 | bool childFunctorsNeedLayer = functorsNeedLayer |
| 415 | // Recorded with non-rect clip, or canvas-rotated by parent |
| 416 | || op->mRecordedWithPotentialStencilClip; |
| 417 | childNode->prepareTreeImpl(info, childFunctorsNeedLayer); |
John Reck | a447d29 | 2014-06-11 18:39:44 -0700 | [diff] [blame] | 418 | info.damageAccumulator->popTransform(); |
John Reck | 5bf11bb | 2014-03-25 10:22:09 -0700 | [diff] [blame] | 419 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 423 | void RenderNode::destroyHardwareResources() { |
| 424 | if (mLayer) { |
| 425 | LayerRenderer::destroyLayer(mLayer); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 426 | mLayer = nullptr; |
John Reck | dcba672 | 2014-07-08 13:59:49 -0700 | [diff] [blame] | 427 | } |
| 428 | if (mDisplayListData) { |
| 429 | for (size_t i = 0; i < mDisplayListData->children().size(); i++) { |
| 430 | mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources(); |
| 431 | } |
| 432 | if (mNeedsDisplayListDataSync) { |
| 433 | // Next prepare tree we are going to push a new display list, so we can |
| 434 | // drop our current one now |
| 435 | deleteDisplayListData(); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void RenderNode::decParentRefCount() { |
| 441 | LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!"); |
| 442 | mParentCount--; |
| 443 | if (!mParentCount) { |
| 444 | // If a child of ours is being attached to our parent then this will incorrectly |
| 445 | // destroy its hardware resources. However, this situation is highly unlikely |
| 446 | // and the failure is "just" that the layer is re-created, so this should |
| 447 | // be safe enough |
| 448 | destroyHardwareResources(); |
| 449 | } |
| 450 | } |
| 451 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 452 | /* |
| 453 | * For property operations, we pass a savecount of 0, since the operations aren't part of the |
| 454 | * 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] | 455 | * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount()) |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 456 | */ |
| 457 | #define PROPERTY_SAVECOUNT 0 |
| 458 | |
| 459 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 460 | void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 461 | #if DEBUG_DISPLAY_LIST |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 462 | properties().debugOutputProperties(handler.level() + 1); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 463 | #endif |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 464 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 465 | renderer.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 466 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 467 | if (properties().getStaticMatrix()) { |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 468 | renderer.concatMatrix(*properties().getStaticMatrix()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 469 | } else if (properties().getAnimationMatrix()) { |
Derek Sollenberger | 1390882 | 2013-12-10 12:28:58 -0500 | [diff] [blame] | 470 | renderer.concatMatrix(*properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 471 | } |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 472 | if (properties().hasTransformMatrix()) { |
| 473 | if (properties().isTransformTranslateOnly()) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 474 | renderer.translate(properties().getTranslationX(), properties().getTranslationY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 475 | } else { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 476 | renderer.concatMatrix(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
Chris Craik | 856f0cc | 2015-04-21 15:13:29 -0700 | [diff] [blame] | 479 | const bool isLayer = properties().effectiveLayerType() != LayerType::None; |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 480 | int clipFlags = properties().getClippingFlags(); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 481 | if (properties().getAlpha() < 1) { |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 482 | if (isLayer) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 483 | clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 484 | } |
Chris Craik | 4e9d9b2 | 2015-06-12 11:07:23 -0700 | [diff] [blame] | 485 | if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) { |
| 486 | // simply scale rendering content's alpha |
| 487 | renderer.scaleAlpha(properties().getAlpha()); |
| 488 | } else { |
| 489 | // savelayer needed to create an offscreen buffer |
| 490 | Rect layerBounds(0, 0, getWidth(), getHeight()); |
| 491 | if (clipFlags) { |
| 492 | properties().getClippingRectForFlags(clipFlags, &layerBounds); |
| 493 | clipFlags = 0; // all clipping done by savelayer |
| 494 | } |
| 495 | SaveLayerOp* op = new (handler.allocator()) SaveLayerOp( |
| 496 | layerBounds.left, layerBounds.top, |
| 497 | layerBounds.right, layerBounds.bottom, |
| 498 | (int) (properties().getAlpha() * 255), |
| 499 | SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag); |
| 500 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 501 | } |
Chris Craik | 1a0808e | 2015-05-13 16:33:04 -0700 | [diff] [blame] | 502 | |
| 503 | if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) { |
Chris Craik | 4e9d9b2 | 2015-06-12 11:07:23 -0700 | [diff] [blame] | 504 | // pretend alpha always causes savelayer to warn about |
| 505 | // performance problem affecting old versions |
Chris Craik | 1a0808e | 2015-05-13 16:33:04 -0700 | [diff] [blame] | 506 | ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(), |
| 507 | static_cast<int>(getWidth()), |
| 508 | static_cast<int>(getHeight())); |
| 509 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 510 | } |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 511 | if (clipFlags) { |
| 512 | Rect clipRect; |
| 513 | properties().getClippingRectForFlags(clipFlags, &clipRect); |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 514 | ClipRectOp* op = new (handler.allocator()) ClipRectOp( |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 515 | clipRect.left, clipRect.top, clipRect.right, clipRect.bottom, |
| 516 | SkRegion::kIntersect_Op); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 517 | handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 518 | } |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 519 | |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 520 | // TODO: support nesting round rect clips |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 521 | if (mProperties.getRevealClip().willClip()) { |
| 522 | Rect bounds; |
| 523 | mProperties.getRevealClip().getBounds(&bounds); |
| 524 | renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius()); |
| 525 | } else if (mProperties.getOutline().willClip()) { |
| 526 | renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline())); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Apply property-based transformations to input matrix |
| 532 | * |
| 533 | * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4 |
| 534 | * matrix computation instead of the Skia 3x3 matrix + camera hackery. |
| 535 | */ |
Chris Craik | 69e5adf | 2014-08-14 13:34:01 -0700 | [diff] [blame] | 536 | void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 537 | if (properties().getLeft() != 0 || properties().getTop() != 0) { |
| 538 | matrix.translate(properties().getLeft(), properties().getTop()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 539 | } |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 540 | if (properties().getStaticMatrix()) { |
| 541 | mat4 stat(*properties().getStaticMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 542 | matrix.multiply(stat); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 543 | } else if (properties().getAnimationMatrix()) { |
| 544 | mat4 anim(*properties().getAnimationMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 545 | matrix.multiply(anim); |
| 546 | } |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 547 | |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 548 | bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ()); |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 549 | if (properties().hasTransformMatrix() || applyTranslationZ) { |
John Reck | f7483e3 | 2014-04-11 08:54:47 -0700 | [diff] [blame] | 550 | if (properties().isTransformTranslateOnly()) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 551 | matrix.translate(properties().getTranslationX(), properties().getTranslationY(), |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 552 | true3dTransform ? properties().getZ() : 0.0f); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 553 | } else { |
| 554 | if (!true3dTransform) { |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 555 | matrix.multiply(*properties().getTransformMatrix()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 556 | } else { |
| 557 | mat4 true3dMat; |
| 558 | true3dMat.loadTranslate( |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 559 | properties().getPivotX() + properties().getTranslationX(), |
| 560 | properties().getPivotY() + properties().getTranslationY(), |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 561 | properties().getZ()); |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 562 | true3dMat.rotate(properties().getRotationX(), 1, 0, 0); |
| 563 | true3dMat.rotate(properties().getRotationY(), 0, 1, 0); |
| 564 | true3dMat.rotate(properties().getRotation(), 0, 0, 1); |
| 565 | true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1); |
| 566 | true3dMat.translate(-properties().getPivotX(), -properties().getPivotY()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 567 | |
| 568 | matrix.multiply(true3dMat); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Organizes the DisplayList hierarchy to prepare for background projection reordering. |
| 576 | * |
| 577 | * This should be called before a call to defer() or drawDisplayList() |
| 578 | * |
| 579 | * Each DisplayList that serves as a 3d root builds its list of composited children, |
| 580 | * which are flagged to not draw in the standard draw loop. |
| 581 | */ |
| 582 | void RenderNode::computeOrdering() { |
| 583 | ATRACE_CALL(); |
| 584 | mProjectedNodes.clear(); |
| 585 | |
| 586 | // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that |
| 587 | // transform properties are applied correctly to top level children |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 588 | if (mDisplayListData == nullptr) return; |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 589 | for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 590 | DrawRenderNodeOp* childOp = mDisplayListData->children()[i]; |
Derek Sollenberger | f293259 | 2015-08-13 14:59:33 -0400 | [diff] [blame] | 591 | childOp->mRenderNode->computeOrderingImpl(childOp, &mProjectedNodes, &mat4::identity()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
| 595 | void RenderNode::computeOrderingImpl( |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 596 | DrawRenderNodeOp* opState, |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 597 | std::vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface, |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 598 | const mat4* transformFromProjectionSurface) { |
| 599 | mProjectedNodes.clear(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 600 | if (mDisplayListData == nullptr || mDisplayListData->isEmpty()) return; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 601 | |
| 602 | // TODO: should avoid this calculation in most cases |
| 603 | // TODO: just calculate single matrix, down to all leaf composited elements |
| 604 | Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface); |
| 605 | localTransformFromProjectionSurface.multiply(opState->mTransformFromParent); |
| 606 | |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 607 | if (properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 608 | // composited projectee, flag for out of order draw, save matrix, and store in proj surface |
| 609 | opState->mSkipInOrderDraw = true; |
Chris Craik | 7c85c54 | 2015-08-19 15:10:24 -0700 | [diff] [blame] | 610 | opState->mTransformFromCompositingAncestor = localTransformFromProjectionSurface; |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 611 | compositedChildrenOfProjectionSurface->push_back(opState); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 612 | } else { |
| 613 | // standard in order draw |
| 614 | opState->mSkipInOrderDraw = false; |
| 615 | } |
| 616 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 617 | if (mDisplayListData->children().size() > 0) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 618 | const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0; |
| 619 | bool haveAppliedPropertiesToProjection = false; |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 620 | for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) { |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 621 | DrawRenderNodeOp* childOp = mDisplayListData->children()[i]; |
| 622 | RenderNode* child = childOp->mRenderNode; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 623 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 624 | std::vector<DrawRenderNodeOp*>* projectionChildren = nullptr; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 625 | const mat4* projectionTransform = nullptr; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 626 | if (isProjectionReceiver && !child->properties().getProjectBackwards()) { |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 627 | // if receiving projections, collect projecting descendant |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 628 | |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 629 | // Note that if a direct descendant is projecting backwards, we pass its |
| 630 | // grandparent projection collection, since it shouldn't project onto its |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 631 | // parent, where it will already be drawing. |
| 632 | projectionChildren = &mProjectedNodes; |
| 633 | projectionTransform = &mat4::identity(); |
| 634 | } else { |
| 635 | if (!haveAppliedPropertiesToProjection) { |
| 636 | applyViewPropertyTransforms(localTransformFromProjectionSurface); |
| 637 | haveAppliedPropertiesToProjection = true; |
| 638 | } |
| 639 | projectionChildren = compositedChildrenOfProjectionSurface; |
| 640 | projectionTransform = &localTransformFromProjectionSurface; |
| 641 | } |
Derek Sollenberger | f293259 | 2015-08-13 14:59:33 -0400 | [diff] [blame] | 642 | child->computeOrderingImpl(childOp, projectionChildren, projectionTransform); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 643 | } |
| 644 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | class DeferOperationHandler { |
| 648 | public: |
| 649 | DeferOperationHandler(DeferStateStruct& deferStruct, int level) |
| 650 | : mDeferStruct(deferStruct), mLevel(level) {} |
| 651 | inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { |
| 652 | operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds); |
| 653 | } |
| 654 | inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); } |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 655 | inline void startMark(const char* name) {} // do nothing |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 656 | inline void endMark() {} |
| 657 | inline int level() { return mLevel; } |
| 658 | inline int replayFlags() { return mDeferStruct.mReplayFlags; } |
Chris Craik | 7466986 | 2014-08-07 17:27:30 -0700 | [diff] [blame] | 659 | inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 660 | |
| 661 | private: |
| 662 | DeferStateStruct& mDeferStruct; |
| 663 | const int mLevel; |
| 664 | }; |
| 665 | |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 666 | void RenderNode::defer(DeferStateStruct& deferStruct, const int level) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 667 | DeferOperationHandler handler(deferStruct, level); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 668 | issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | class ReplayOperationHandler { |
| 672 | public: |
| 673 | ReplayOperationHandler(ReplayStateStruct& replayStruct, int level) |
| 674 | : mReplayStruct(replayStruct), mLevel(level) {} |
| 675 | inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) { |
| 676 | #if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 677 | mReplayStruct.mRenderer.eventMark(operation->name()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 678 | #endif |
| 679 | operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds); |
| 680 | } |
| 681 | inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); } |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 682 | inline void startMark(const char* name) { |
| 683 | mReplayStruct.mRenderer.startMark(name); |
| 684 | } |
| 685 | inline void endMark() { |
| 686 | mReplayStruct.mRenderer.endMark(); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 687 | } |
| 688 | inline int level() { return mLevel; } |
| 689 | inline int replayFlags() { return mReplayStruct.mReplayFlags; } |
Chris Craik | 7466986 | 2014-08-07 17:27:30 -0700 | [diff] [blame] | 690 | inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 691 | |
| 692 | private: |
| 693 | ReplayStateStruct& mReplayStruct; |
| 694 | const int mLevel; |
| 695 | }; |
| 696 | |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 697 | void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 698 | ReplayOperationHandler handler(replayStruct, level); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 699 | issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 702 | void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk, |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 703 | std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) { |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 704 | if (chunk.beginChildIndex == chunk.endChildIndex) return; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 705 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 706 | for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) { |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 707 | DrawRenderNodeOp* childOp = mDisplayListData->children()[i]; |
| 708 | RenderNode* child = childOp->mRenderNode; |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 709 | float childZ = child->properties().getZ(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 710 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 711 | if (!MathUtils::isZero(childZ) && chunk.reorderChildren) { |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 712 | zTranslatedNodes.push_back(ZDrawRenderNodeOpPair(childZ, childOp)); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 713 | childOp->mSkipInOrderDraw = true; |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 714 | } else if (!child->properties().getProjectBackwards()) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 715 | // regular, in order drawing DisplayList |
| 716 | childOp->mSkipInOrderDraw = false; |
| 717 | } |
| 718 | } |
| 719 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 720 | // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order) |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 721 | std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end()); |
| 722 | } |
| 723 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 724 | template <class T> |
| 725 | void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) { |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 726 | if (properties().getAlpha() <= 0.0f |
| 727 | || properties().getOutline().getAlpha() <= 0.0f |
| 728 | || !properties().getOutline().getPath()) { |
| 729 | // no shadow to draw |
| 730 | return; |
| 731 | } |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 732 | |
| 733 | mat4 shadowMatrixXY(transformFromParent); |
| 734 | applyViewPropertyTransforms(shadowMatrixXY); |
| 735 | |
| 736 | // Z matrix needs actual 3d transformation, so mapped z values will be correct |
| 737 | mat4 shadowMatrixZ(transformFromParent); |
| 738 | applyViewPropertyTransforms(shadowMatrixZ, true); |
| 739 | |
Chris Craik | 7466986 | 2014-08-07 17:27:30 -0700 | [diff] [blame] | 740 | const SkPath* casterOutlinePath = properties().getOutline().getPath(); |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 741 | const SkPath* revealClipPath = properties().getRevealClip().getPath(); |
Chris Craik | 6131732 | 2014-05-21 13:03:52 -0700 | [diff] [blame] | 742 | if (revealClipPath && revealClipPath->isEmpty()) return; |
| 743 | |
Chris Craik | 77b5cad | 2014-07-30 18:23:07 -0700 | [diff] [blame] | 744 | float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha(); |
Chris Craik | 7466986 | 2014-08-07 17:27:30 -0700 | [diff] [blame] | 745 | |
Chris Craik | 7466986 | 2014-08-07 17:27:30 -0700 | [diff] [blame] | 746 | |
Chris Craik | faa79ff | 2014-12-01 13:44:21 -0800 | [diff] [blame] | 747 | // holds temporary SkPath to store the result of intersections |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 748 | SkPath* frameAllocatedPath = nullptr; |
Chris Craik | faa79ff | 2014-12-01 13:44:21 -0800 | [diff] [blame] | 749 | const SkPath* outlinePath = casterOutlinePath; |
| 750 | |
| 751 | // intersect the outline with the reveal clip, if present |
| 752 | if (revealClipPath) { |
| 753 | frameAllocatedPath = handler.allocPathForFrame(); |
| 754 | |
Tom Hudson | 02a2630 | 2015-06-24 11:32:42 -0400 | [diff] [blame] | 755 | Op(*outlinePath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath); |
Chris Craik | faa79ff | 2014-12-01 13:44:21 -0800 | [diff] [blame] | 756 | outlinePath = frameAllocatedPath; |
| 757 | } |
| 758 | |
| 759 | // intersect the outline with the clipBounds, if present |
| 760 | if (properties().getClippingFlags() & CLIP_TO_CLIP_BOUNDS) { |
| 761 | if (!frameAllocatedPath) { |
| 762 | frameAllocatedPath = handler.allocPathForFrame(); |
| 763 | } |
| 764 | |
| 765 | Rect clipBounds; |
| 766 | properties().getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds); |
| 767 | SkPath clipBoundsPath; |
| 768 | clipBoundsPath.addRect(clipBounds.left, clipBounds.top, |
| 769 | clipBounds.right, clipBounds.bottom); |
| 770 | |
Tom Hudson | 02a2630 | 2015-06-24 11:32:42 -0400 | [diff] [blame] | 771 | Op(*outlinePath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath); |
Chris Craik | 7466986 | 2014-08-07 17:27:30 -0700 | [diff] [blame] | 772 | outlinePath = frameAllocatedPath; |
| 773 | } |
| 774 | |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 775 | DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp( |
Chris Craik | 7466986 | 2014-08-07 17:27:30 -0700 | [diff] [blame] | 776 | shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 777 | handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 778 | } |
| 779 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 780 | #define SHADOW_DELTA 0.1f |
| 781 | |
| 782 | template <class T> |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 783 | void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode, |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 784 | const Matrix4& initialTransform, const std::vector<ZDrawRenderNodeOpPair>& zTranslatedNodes, |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 785 | OpenGLRenderer& renderer, T& handler) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 786 | const int size = zTranslatedNodes.size(); |
| 787 | if (size == 0 |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 788 | || (mode == ChildrenSelectMode::NegativeZChildren && zTranslatedNodes[0].key > 0.0f) |
| 789 | || (mode == ChildrenSelectMode::PositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 790 | // no 3d children to draw |
| 791 | return; |
| 792 | } |
| 793 | |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 794 | // Apply the base transform of the parent of the 3d children. This isolates |
| 795 | // 3d children of the current chunk from transformations made in previous chunks. |
| 796 | int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
Chris Craik | 6daa13c | 2015-08-19 13:32:12 -0700 | [diff] [blame] | 797 | renderer.setGlobalMatrix(initialTransform); |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 798 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 799 | /** |
| 800 | * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters |
| 801 | * with very similar Z heights to draw together. |
| 802 | * |
| 803 | * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are |
| 804 | * underneath both, and neither's shadow is drawn on top of the other. |
| 805 | */ |
| 806 | const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes); |
| 807 | size_t drawIndex, shadowIndex, endIndex; |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 808 | if (mode == ChildrenSelectMode::NegativeZChildren) { |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 809 | drawIndex = 0; |
| 810 | endIndex = nonNegativeIndex; |
| 811 | shadowIndex = endIndex; // draw no shadows |
| 812 | } else { |
| 813 | drawIndex = nonNegativeIndex; |
| 814 | endIndex = size; |
| 815 | shadowIndex = drawIndex; // potentially draw shadow for each pos Z child |
| 816 | } |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 817 | |
| 818 | DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "", |
| 819 | endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive"); |
| 820 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 821 | float lastCasterZ = 0.0f; |
| 822 | while (shadowIndex < endIndex || drawIndex < endIndex) { |
| 823 | if (shadowIndex < endIndex) { |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 824 | DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value; |
| 825 | RenderNode* caster = casterOp->mRenderNode; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 826 | const float casterZ = zTranslatedNodes[shadowIndex].key; |
| 827 | // attempt to render the shadow if the caster about to be drawn is its caster, |
| 828 | // OR if its caster's Z value is similar to the previous potential caster |
| 829 | if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) { |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 830 | caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 831 | |
| 832 | lastCasterZ = casterZ; // must do this even if current caster not casting a shadow |
| 833 | shadowIndex++; |
| 834 | continue; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | // only the actual child DL draw needs to be in save/restore, |
| 839 | // since it modifies the renderer's matrix |
| 840 | int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
| 841 | |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 842 | DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 843 | |
| 844 | renderer.concatMatrix(childOp->mTransformFromParent); |
| 845 | childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 846 | handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 847 | childOp->mSkipInOrderDraw = true; |
| 848 | |
| 849 | renderer.restoreToCount(restoreTo); |
| 850 | drawIndex++; |
| 851 | } |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 852 | renderer.restoreToCount(rootRestoreTo); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 856 | void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) { |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 857 | DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size()); |
| 858 | const SkPath* projectionReceiverOutline = properties().getOutline().getPath(); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 859 | int restoreTo = renderer.getSaveCount(); |
| 860 | |
Chris Craik | b3cca87 | 2014-08-08 18:42:51 -0700 | [diff] [blame] | 861 | LinearAllocator& alloc = handler.allocator(); |
| 862 | handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), |
| 863 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
| 864 | |
| 865 | // Transform renderer to match background we're projecting onto |
| 866 | // (by offsetting canvas by translationX/Y of background rendernode, since only those are set) |
| 867 | const DisplayListOp* op = |
| 868 | (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]); |
| 869 | const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op); |
| 870 | const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties(); |
| 871 | renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY()); |
| 872 | |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 873 | // If the projection reciever has an outline, we mask projected content to it |
| 874 | // (which we know, apriori, are all tessellated paths) |
| 875 | renderer.setProjectionPathMask(alloc, projectionReceiverOutline); |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 876 | |
| 877 | // draw projected nodes |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 878 | for (size_t i = 0; i < mProjectedNodes.size(); i++) { |
Chris Craik | a7090e0 | 2014-06-20 16:01:00 -0700 | [diff] [blame] | 879 | DrawRenderNodeOp* childOp = mProjectedNodes[i]; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 880 | |
| 881 | // matrix save, concat, and restore can be done safely without allocating operations |
| 882 | int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag); |
| 883 | renderer.concatMatrix(childOp->mTransformFromCompositingAncestor); |
| 884 | childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 885 | handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 886 | childOp->mSkipInOrderDraw = true; |
| 887 | renderer.restoreToCount(restoreTo); |
| 888 | } |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 889 | |
Chris Craik | fca52b75 | 2015-04-28 11:45:59 -0700 | [diff] [blame] | 890 | handler(new (alloc) RestoreToCountOp(restoreTo), |
| 891 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /** |
| 895 | * This function serves both defer and replay modes, and will organize the displayList's component |
| 896 | * operations for a single frame: |
| 897 | * |
| 898 | * Every 'simple' state operation that affects just the matrix and alpha (or other factors of |
| 899 | * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom |
| 900 | * defer logic) and operations in displayListOps are issued through the 'handler' which handles the |
| 901 | * defer vs replay logic, per operation |
| 902 | */ |
| 903 | template <class T> |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 904 | void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) { |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 905 | if (mDisplayListData->isEmpty()) { |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 906 | DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", handler.level() * 2, "", |
| 907 | this, getName()); |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 908 | return; |
| 909 | } |
| 910 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 911 | const bool drawLayer = (mLayer && (&renderer != mLayer->renderer.get())); |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 912 | // If we are updating the contents of mLayer, we don't want to apply any of |
| 913 | // the RenderNode's properties to this issueOperations pass. Those will all |
| 914 | // be applied when the layer is drawn, aka when this is true. |
| 915 | const bool useViewProperties = (!mLayer || drawLayer); |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 916 | if (useViewProperties) { |
| 917 | const Outline& outline = properties().getOutline(); |
| 918 | if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) { |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 919 | DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", handler.level() * 2, "", |
| 920 | this, getName()); |
Chris Craik | 0645128 | 2014-07-21 10:25:54 -0700 | [diff] [blame] | 921 | return; |
| 922 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 925 | handler.startMark(getName()); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 926 | |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 927 | #if DEBUG_DISPLAY_LIST |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 928 | const Rect& clipRect = renderer.getLocalClipBounds(); |
| 929 | DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f", |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 930 | handler.level() * 2, "", this, getName(), |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame] | 931 | clipRect.left, clipRect.top, clipRect.right, clipRect.bottom); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 932 | #endif |
| 933 | |
| 934 | LinearAllocator& alloc = handler.allocator(); |
| 935 | int restoreTo = renderer.getSaveCount(); |
| 936 | handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 937 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 938 | |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 939 | DISPLAY_LIST_LOGD("%*sSave %d %d", (handler.level() + 1) * 2, "", |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 940 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo); |
| 941 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 942 | if (useViewProperties) { |
| 943 | setViewProperties<T>(renderer, handler); |
| 944 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 945 | |
Chris Craik | 8c271ca | 2014-03-25 10:33:01 -0700 | [diff] [blame] | 946 | bool quickRejected = properties().getClipToBounds() |
| 947 | && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 948 | if (!quickRejected) { |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 949 | Matrix4 initialTransform(*(renderer.currentTransform())); |
Tom Hudson | ac7b6d3 | 2015-06-30 11:26:13 -0400 | [diff] [blame] | 950 | renderer.setBaseTransform(initialTransform); |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 951 | |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 952 | if (drawLayer) { |
Chris Craik | 3aadd60 | 2015-08-20 12:41:40 -0700 | [diff] [blame] | 953 | handler(new (alloc) DrawLayerOp(mLayer), |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 954 | renderer.getSaveCount() - 1, properties().getClipToBounds()); |
| 955 | } else { |
Chris Craik | c166b6c | 2014-09-05 19:55:30 -0700 | [diff] [blame] | 956 | const int saveCountOffset = renderer.getSaveCount() - 1; |
| 957 | const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex; |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 958 | for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) { |
| 959 | const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex]; |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 960 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 961 | std::vector<ZDrawRenderNodeOpPair> zTranslatedNodes; |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 962 | buildZSortedChildList(chunk, zTranslatedNodes); |
| 963 | |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 964 | issueOperationsOf3dChildren(ChildrenSelectMode::NegativeZChildren, |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 965 | initialTransform, zTranslatedNodes, renderer, handler); |
| 966 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 967 | |
Andreas Gampe | edaecc1 | 2014-11-10 20:54:07 -0800 | [diff] [blame] | 968 | for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) { |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 969 | DisplayListOp *op = mDisplayListData->displayListOps[opIndex]; |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 970 | #if DEBUG_DISPLAY_LIST |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 971 | op->output(handler.level() + 1); |
Chris Craik | 80d4902 | 2014-06-20 15:03:43 -0700 | [diff] [blame] | 972 | #endif |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 973 | handler(op, saveCountOffset, properties().getClipToBounds()); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 974 | |
John Reck | 272a685 | 2015-07-29 16:48:58 -0700 | [diff] [blame] | 975 | if (CC_UNLIKELY(!mProjectedNodes.empty() && projectionReceiveIndex >= 0 && |
Andreas Gampe | edaecc1 | 2014-11-10 20:54:07 -0800 | [diff] [blame] | 976 | opIndex == static_cast<size_t>(projectionReceiveIndex))) { |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 977 | issueOperationsOfProjectedChildren(renderer, handler); |
| 978 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 979 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 980 | |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 981 | issueOperationsOf3dChildren(ChildrenSelectMode::PositiveZChildren, |
Chris Craik | c3e75f9 | 2014-08-27 15:34:52 -0700 | [diff] [blame] | 982 | initialTransform, zTranslatedNodes, renderer, handler); |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 983 | } |
John Reck | 25fbb3f | 2014-06-12 13:46:45 -0700 | [diff] [blame] | 984 | } |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 987 | DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (handler.level() + 1) * 2, "", restoreTo); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 988 | handler(new (alloc) RestoreToCountOp(restoreTo), |
John Reck | d0a0b2a | 2014-03-20 16:28:56 -0700 | [diff] [blame] | 989 | PROPERTY_SAVECOUNT, properties().getClipToBounds()); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 990 | |
Chris Craik | bf72eb8 | 2015-06-08 11:30:44 -0700 | [diff] [blame] | 991 | DISPLAY_LIST_LOGD("%*sDone (%p, %s)", handler.level() * 2, "", this, getName()); |
Chris Craik | b265e2c | 2014-03-27 15:50:09 -0700 | [diff] [blame] | 992 | handler.endMark(); |
John Reck | 113e082 | 2014-03-18 09:22:59 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | } /* namespace uirenderer */ |
| 996 | } /* namespace android */ |