blob: 5d5f152c95ce9d2892e2b1977aa891e84183f9b3 [file] [log] [blame]
John Reck113e0822014-03-18 09:22:59 -07001/*
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
Chris Craik80d49022014-06-20 15:03:43 -070018#define LOG_TAG "OpenGLRenderer"
John Reck113e0822014-03-18 09:22:59 -070019
20#include "RenderNode.h"
21
John Recke45b1fd2014-04-15 09:50:16 -070022#include <algorithm>
John Reckc25e5062014-06-18 14:21:29 -070023#include <string>
John Recke45b1fd2014-04-15 09:50:16 -070024
John Reck113e0822014-03-18 09:22:59 -070025#include <SkCanvas.h>
26#include <algorithm>
27
28#include <utils/Trace.h>
29
John Recke4267ea2014-06-03 15:53:15 -070030#include "DamageAccumulator.h"
John Reck113e0822014-03-18 09:22:59 -070031#include "Debug.h"
32#include "DisplayListOp.h"
33#include "DisplayListLogBuffer.h"
John Reck25fbb3f2014-06-12 13:46:45 -070034#include "LayerRenderer.h"
35#include "OpenGLRenderer.h"
Chris Craike0bb87d2014-04-22 17:55:41 -070036#include "utils/MathUtils.h"
John Reck998a6d82014-08-28 15:35:53 -070037#include "renderthread/CanvasContext.h"
John Reck113e0822014-03-18 09:22:59 -070038
39namespace android {
40namespace uirenderer {
41
42void RenderNode::outputLogBuffer(int fd) {
43 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
44 if (logBuffer.isEmpty()) {
45 return;
46 }
47
48 FILE *file = fdopen(fd, "a");
49
50 fprintf(file, "\nRecent DisplayList operations\n");
51 logBuffer.outputCommands(file);
52
Chris Craik059476a2014-09-29 17:09:53 -070053 if (Caches::hasInstance()) {
54 String8 cachesLog;
55 Caches::getInstance().dumpMemoryUsage(cachesLog);
56 fprintf(file, "\nCaches:\n%s\n", cachesLog.string());
57 } else {
58 fprintf(file, "\nNo caches instance.\n");
59 }
John Reck113e0822014-03-18 09:22:59 -070060
61 fflush(file);
62}
63
John Reck443a7142014-09-04 17:40:05 -070064void RenderNode::debugDumpLayers(const char* prefix) {
65 if (mLayer) {
66 ALOGD("%sNode %p (%s) has layer %p (fbo = %u, wasBuildLayered = %s)",
67 prefix, this, getName(), mLayer, mLayer->getFbo(),
68 mLayer->wasBuildLayered ? "true" : "false");
69 }
70 if (mDisplayListData) {
71 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
72 mDisplayListData->children()[i]->mRenderNode->debugDumpLayers(prefix);
73 }
74 }
75}
76
John Reck8de65a82014-04-09 15:23:38 -070077RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070078 : mDirtyPropertyFields(0)
John Reck8de65a82014-04-09 15:23:38 -070079 , mNeedsDisplayListDataSync(false)
80 , mDisplayListData(0)
John Recke45b1fd2014-04-15 09:50:16 -070081 , mStagingDisplayListData(0)
John Reck68bfe0a2014-06-24 15:34:58 -070082 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070083 , mLayer(0)
84 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070085}
86
87RenderNode::~RenderNode() {
John Reckdcba6722014-07-08 13:59:49 -070088 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -070089 delete mStagingDisplayListData;
John Reck0e89e2b2014-10-31 14:49:06 -070090 if (mLayer) {
91 ALOGW("Memory Warning: Layer %p missed its detachment, held on to for far too long!", mLayer);
92 mLayer->postDecStrong();
93 mLayer = 0;
94 }
John Reck113e0822014-03-18 09:22:59 -070095}
96
John Reck8de65a82014-04-09 15:23:38 -070097void RenderNode::setStagingDisplayList(DisplayListData* data) {
98 mNeedsDisplayListDataSync = true;
99 delete mStagingDisplayListData;
100 mStagingDisplayListData = data;
John Reck113e0822014-03-18 09:22:59 -0700101}
102
103/**
104 * This function is a simplified version of replay(), where we simply retrieve and log the
105 * display list. This function should remain in sync with the replay() function.
106 */
107void RenderNode::output(uint32_t level) {
Chris Craikb5a54352014-11-21 14:54:35 -0800108 ALOGD("%*sStart display list (%p, %s%s%s%s)", (level - 1) * 2, "", this,
109 getName(),
110 (properties().hasShadow() ? ", casting shadow" : ""),
111 (isRenderable() ? "" : ", empty"),
112 (mLayer != NULL ? ", on HW Layer" : ""));
John Reck113e0822014-03-18 09:22:59 -0700113 ALOGD("%*s%s %d", level * 2, "", "Save",
114 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
115
John Reckd0a0b2a2014-03-20 16:28:56 -0700116 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -0700117 int flags = DisplayListOp::kOpLogFlag_Recurse;
John Reckdc0349b2014-08-06 15:28:07 -0700118 if (mDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700119 // TODO: consider printing the chunk boundaries here
John Reckdc0349b2014-08-06 15:28:07 -0700120 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
121 mDisplayListData->displayListOps[i]->output(level, flags);
122 }
John Reck113e0822014-03-18 09:22:59 -0700123 }
124
Chris Craik3f0854292014-04-15 16:18:08 -0700125 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700126}
127
John Reckfe5e7b72014-05-23 17:42:28 -0700128int RenderNode::getDebugSize() {
129 int size = sizeof(RenderNode);
130 if (mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700131 size += mStagingDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700132 }
133 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700134 size += mDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700135 }
136 return size;
137}
138
John Reckf4198b72014-04-09 17:00:04 -0700139void RenderNode::prepareTree(TreeInfo& info) {
140 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700141 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700142
143 prepareTreeImpl(info);
144}
145
John Reck68bfe0a2014-06-24 15:34:58 -0700146void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
147 mAnimatorManager.addAnimator(animator);
148}
149
John Recke4267ea2014-06-03 15:53:15 -0700150void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700151 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700152 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700153 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
154 } else {
155 // Hope this is big enough?
156 // TODO: Get this from the display list ops or something
157 info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
158 }
John Recke4267ea2014-06-03 15:53:15 -0700159 }
160}
161
John Recka7c2ea22014-08-08 13:21:00 -0700162void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
John Reck25fbb3f2014-06-12 13:46:45 -0700163 LayerType layerType = properties().layerProperties().type();
164 if (CC_UNLIKELY(layerType == kLayerTypeRenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700165 // Damage applied so far needs to affect our parent, but does not require
166 // the layer to be updated. So we pop/push here to clear out the current
167 // damage and get a clean state for display list or children updates to
168 // affect, which will require the layer to be updated
169 info.damageAccumulator->popTransform();
170 info.damageAccumulator->pushTransform(this);
171 if (dirtyMask & DISPLAY_LIST) {
172 damageSelf(info);
173 }
John Reck25fbb3f2014-06-12 13:46:45 -0700174 }
175}
176
177void RenderNode::pushLayerUpdate(TreeInfo& info) {
178 LayerType layerType = properties().layerProperties().type();
179 // If we are not a layer OR we cannot be rendered (eg, view was detached)
180 // we need to destroy any Layers we may have had previously
181 if (CC_LIKELY(layerType != kLayerTypeRenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700182 if (CC_UNLIKELY(mLayer)) {
183 LayerRenderer::destroyLayer(mLayer);
184 mLayer = NULL;
185 }
186 return;
187 }
188
Chris Craik69e5adf2014-08-14 13:34:01 -0700189 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700190 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700191 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700192 applyLayerPropertiesToLayer(info);
193 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700194 transformUpdateNeeded = true;
John Reck25fbb3f2014-06-12 13:46:45 -0700195 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700196 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
197 LayerRenderer::destroyLayer(mLayer);
198 mLayer = 0;
199 }
John Reck25fbb3f2014-06-12 13:46:45 -0700200 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700201 transformUpdateNeeded = true;
202 }
203
John Reck25fbb3f2014-06-12 13:46:45 -0700204 SkRect dirty;
205 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700206
John Reckc25e5062014-06-18 14:21:29 -0700207 if (!mLayer) {
John Reck0e89e2b2014-10-31 14:49:06 -0700208 Caches::getInstance().dumpMemoryUsage();
John Reckc25e5062014-06-18 14:21:29 -0700209 if (info.errorHandler) {
210 std::string msg = "Unable to create layer for ";
211 msg += getName();
212 info.errorHandler->onError(msg);
213 }
214 return;
215 }
216
Chris Craikc71bfca2014-08-21 10:18:58 -0700217 if (transformUpdateNeeded) {
218 // update the transform in window of the layer to reset its origin wrt light source position
219 Matrix4 windowTransform;
220 info.damageAccumulator->computeCurrentTransform(&windowTransform);
221 mLayer->setWindowTransform(windowTransform);
222 }
John Reckc79eabc2014-08-05 11:03:42 -0700223
224 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
225 dirty.roundOut();
John Reck25fbb3f2014-06-12 13:46:45 -0700226 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
227 }
228 // This is not inside the above if because we may have called
229 // updateDeferred on a previous prepare pass that didn't have a renderer
230 if (info.renderer && mLayer->deferredUpdateScheduled) {
231 info.renderer->pushLayerUpdate(mLayer);
232 }
John Reck998a6d82014-08-28 15:35:53 -0700233
234 if (CC_UNLIKELY(info.canvasContext)) {
235 // If canvasContext is not null that means there are prefetched layers
236 // that need to be accounted for. That might be us, so tell CanvasContext
237 // that this layer is in the tree and should not be destroyed.
238 info.canvasContext->markLayerInUse(this);
239 }
John Reck25fbb3f2014-06-12 13:46:45 -0700240}
241
John Recke4267ea2014-06-03 15:53:15 -0700242void RenderNode::prepareTreeImpl(TreeInfo& info) {
John Recka447d292014-06-11 18:39:44 -0700243 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700244
John Reckdcba6722014-07-08 13:59:49 -0700245 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700246 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700247 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700248 uint32_t animatorDirtyMask = 0;
249 if (CC_LIKELY(info.runAnimations)) {
250 animatorDirtyMask = mAnimatorManager.animate(info);
251 }
John Recka7c2ea22014-08-08 13:21:00 -0700252 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700253 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700254 pushStagingDisplayListChanges(info);
255 }
John Reckf4198b72014-04-09 17:00:04 -0700256 prepareSubTree(info, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700257 pushLayerUpdate(info);
258
John Recka447d292014-06-11 18:39:44 -0700259 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700260}
261
John Reck25fbb3f2014-06-12 13:46:45 -0700262void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700263 // Push the animators first so that setupStartValueIfNecessary() is called
264 // before properties() is trampled by stagingProperties(), as they are
265 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700266 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700267 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700268 }
John Reckff941dc2014-05-14 16:34:14 -0700269 if (mDirtyPropertyFields) {
270 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700271 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700272 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700273 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700274 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700275 // We could try to be clever and only re-damage if the matrix changed.
276 // However, we don't need to worry about that. The cost of over-damaging
277 // here is only going to be a single additional map rect of this node
278 // plus a rect join(). The parent's transform (and up) will only be
279 // performed once.
John Recka447d292014-06-11 18:39:44 -0700280 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700281 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700282 }
John Reck25fbb3f2014-06-12 13:46:45 -0700283}
284
285void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
286 if (CC_LIKELY(!mLayer)) return;
287
288 const LayerProperties& props = properties().layerProperties();
289 mLayer->setAlpha(props.alpha(), props.xferMode());
290 mLayer->setColorFilter(props.colorFilter());
291 mLayer->setBlend(props.needsBlending());
292}
293
294void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700295 if (mNeedsDisplayListDataSync) {
296 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700297 // Make sure we inc first so that we don't fluctuate between 0 and 1,
298 // which would thrash the layer cache
299 if (mStagingDisplayListData) {
300 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
301 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
302 }
303 }
John Reck5c9d7172014-10-22 11:32:27 -0700304 // Damage with the old display list first then the new one to catch any
305 // changes in isRenderable or, in the future, bounds
306 damageSelf(info);
John Reckdcba6722014-07-08 13:59:49 -0700307 deleteDisplayListData();
John Recka35778c72014-11-06 09:45:10 -0800308 // TODO: Remove this caches stuff
309 if (mStagingDisplayListData && mStagingDisplayListData->functors.size()) {
310 Caches::getInstance().registerFunctors(mStagingDisplayListData->functors.size());
311 }
John Reck8de65a82014-04-09 15:23:38 -0700312 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700313 mStagingDisplayListData = NULL;
John Reck09d5cdd2014-07-24 10:36:08 -0700314 if (mDisplayListData) {
315 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
316 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
317 }
318 }
John Recke4267ea2014-06-03 15:53:15 -0700319 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700320 }
John Reck8de65a82014-04-09 15:23:38 -0700321}
322
John Reckdcba6722014-07-08 13:59:49 -0700323void RenderNode::deleteDisplayListData() {
324 if (mDisplayListData) {
325 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
326 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
327 }
John Recka35778c72014-11-06 09:45:10 -0800328 if (mDisplayListData->functors.size()) {
329 Caches::getInstance().unregisterFunctors(mDisplayListData->functors.size());
330 }
John Reckdcba6722014-07-08 13:59:49 -0700331 }
332 delete mDisplayListData;
333 mDisplayListData = NULL;
334}
335
John Reckf4198b72014-04-09 17:00:04 -0700336void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700337 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700338 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700339 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700340 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
341 // and thus falling out of async drawing path.
342 if (subtree->ownedBitmapResources.size()) {
343 info.prepareTextures = false;
344 }
345 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
346 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700347 }
John Reck8de65a82014-04-09 15:23:38 -0700348 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700349 DrawRenderNodeOp* op = subtree->children()[i];
350 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700351 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700352 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700353 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700354 }
John Reck113e0822014-03-18 09:22:59 -0700355 }
356}
357
John Reckdcba6722014-07-08 13:59:49 -0700358void RenderNode::destroyHardwareResources() {
359 if (mLayer) {
360 LayerRenderer::destroyLayer(mLayer);
361 mLayer = NULL;
362 }
363 if (mDisplayListData) {
364 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
365 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
366 }
367 if (mNeedsDisplayListDataSync) {
368 // Next prepare tree we are going to push a new display list, so we can
369 // drop our current one now
370 deleteDisplayListData();
371 }
372 }
373}
374
375void RenderNode::decParentRefCount() {
376 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
377 mParentCount--;
378 if (!mParentCount) {
379 // If a child of ours is being attached to our parent then this will incorrectly
380 // destroy its hardware resources. However, this situation is highly unlikely
381 // and the failure is "just" that the layer is re-created, so this should
382 // be safe enough
383 destroyHardwareResources();
384 }
385}
386
John Reck113e0822014-03-18 09:22:59 -0700387/*
388 * For property operations, we pass a savecount of 0, since the operations aren't part of the
389 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700390 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700391 */
392#define PROPERTY_SAVECOUNT 0
393
394template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700395void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700396#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700397 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700398#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700399 if (properties().getLeft() != 0 || properties().getTop() != 0) {
400 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700401 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700402 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500403 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700404 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500405 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700406 }
John Reckf7483e32014-04-11 08:54:47 -0700407 if (properties().hasTransformMatrix()) {
408 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700409 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700410 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700411 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700412 }
413 }
John Reck25fbb3f2014-06-12 13:46:45 -0700414 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700415 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700416 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700417 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700418 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
419
John Reckd0a0b2a2014-03-20 16:28:56 -0700420 renderer.setOverrideLayerAlpha(properties().getAlpha());
421 } else if (!properties().getHasOverlappingRendering()) {
422 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700423 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700424 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700425 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700426 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700427 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700428 properties().getClippingRectForFlags(clipFlags, &layerBounds);
429 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700430 }
431
432 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700433 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700434 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700435 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700436 }
437 }
Chris Craika753f4c2014-07-24 12:39:17 -0700438 if (clipFlags) {
439 Rect clipRect;
440 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700441 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700442 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
443 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700444 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700445 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700446
Chris Craike83cbd42014-09-03 17:52:24 -0700447 // TODO: support nesting round rect clips
Chris Craikaf4d04c2014-07-29 12:50:14 -0700448 if (mProperties.getRevealClip().willClip()) {
449 Rect bounds;
450 mProperties.getRevealClip().getBounds(&bounds);
451 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
452 } else if (mProperties.getOutline().willClip()) {
453 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700454 }
455}
456
457/**
458 * Apply property-based transformations to input matrix
459 *
460 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
461 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
462 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700463void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700464 if (properties().getLeft() != 0 || properties().getTop() != 0) {
465 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700466 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700467 if (properties().getStaticMatrix()) {
468 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700469 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700470 } else if (properties().getAnimationMatrix()) {
471 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700472 matrix.multiply(anim);
473 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700474
Chris Craikcc39e162014-04-25 18:34:11 -0700475 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700476 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700477 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700478 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700479 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700480 } else {
481 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700482 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700483 } else {
484 mat4 true3dMat;
485 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700486 properties().getPivotX() + properties().getTranslationX(),
487 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700488 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700489 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
490 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
491 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
492 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
493 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700494
495 matrix.multiply(true3dMat);
496 }
497 }
498 }
499}
500
501/**
502 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
503 *
504 * This should be called before a call to defer() or drawDisplayList()
505 *
506 * Each DisplayList that serves as a 3d root builds its list of composited children,
507 * which are flagged to not draw in the standard draw loop.
508 */
509void RenderNode::computeOrdering() {
510 ATRACE_CALL();
511 mProjectedNodes.clear();
512
513 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
514 // transform properties are applied correctly to top level children
515 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700516 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700517 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
518 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700519 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700520 }
521}
522
523void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700524 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700525 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700526 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700527 const mat4* transformFromProjectionSurface) {
528 mProjectedNodes.clear();
529 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
530
531 // TODO: should avoid this calculation in most cases
532 // TODO: just calculate single matrix, down to all leaf composited elements
533 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
534 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
535
John Reckd0a0b2a2014-03-20 16:28:56 -0700536 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700537 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
538 opState->mSkipInOrderDraw = true;
539 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
540 compositedChildrenOfProjectionSurface->add(opState);
541 } else {
542 // standard in order draw
543 opState->mSkipInOrderDraw = false;
544 }
545
John Reck087bc0c2014-04-04 16:20:08 -0700546 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700547 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
548 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700549 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700550 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
551 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700552
Chris Craik3f0854292014-04-15 16:18:08 -0700553 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700554 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700555 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700556 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700557 // if receiving projections, collect projecting descendent
558
559 // Note that if a direct descendent is projecting backwards, we pass it's
560 // grandparent projection collection, since it shouldn't project onto it's
561 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700562 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700563 projectionChildren = &mProjectedNodes;
564 projectionTransform = &mat4::identity();
565 } else {
566 if (!haveAppliedPropertiesToProjection) {
567 applyViewPropertyTransforms(localTransformFromProjectionSurface);
568 haveAppliedPropertiesToProjection = true;
569 }
Chris Craik3f0854292014-04-15 16:18:08 -0700570 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700571 projectionChildren = compositedChildrenOfProjectionSurface;
572 projectionTransform = &localTransformFromProjectionSurface;
573 }
Chris Craik3f0854292014-04-15 16:18:08 -0700574 child->computeOrderingImpl(childOp,
575 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700576 }
577 }
John Reck113e0822014-03-18 09:22:59 -0700578}
579
580class DeferOperationHandler {
581public:
582 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
583 : mDeferStruct(deferStruct), mLevel(level) {}
584 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
585 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
586 }
587 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700588 inline void startMark(const char* name) {} // do nothing
589 inline void endMark() {}
590 inline int level() { return mLevel; }
591 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700592 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700593
594private:
595 DeferStateStruct& mDeferStruct;
596 const int mLevel;
597};
598
Chris Craik80d49022014-06-20 15:03:43 -0700599void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700600 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700601 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700602}
603
604class ReplayOperationHandler {
605public:
606 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
607 : mReplayStruct(replayStruct), mLevel(level) {}
608 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
609#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700610 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700611#endif
612 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
613 }
614 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700615 inline void startMark(const char* name) {
616 mReplayStruct.mRenderer.startMark(name);
617 }
618 inline void endMark() {
619 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700620 }
621 inline int level() { return mLevel; }
622 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700623 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700624
625private:
626 ReplayStateStruct& mReplayStruct;
627 const int mLevel;
628};
629
Chris Craik80d49022014-06-20 15:03:43 -0700630void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700631 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700632 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700633}
634
Chris Craik8afd0f22014-08-21 17:41:57 -0700635void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
636 Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
637 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700638
Chris Craik8afd0f22014-08-21 17:41:57 -0700639 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700640 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
641 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700642 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700643
Chris Craik8afd0f22014-08-21 17:41:57 -0700644 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
Chris Craika7090e02014-06-20 16:01:00 -0700645 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700646 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700647 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700648 // regular, in order drawing DisplayList
649 childOp->mSkipInOrderDraw = false;
650 }
651 }
652
Chris Craik8afd0f22014-08-21 17:41:57 -0700653 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700654 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
655}
656
Chris Craikb265e2c2014-03-27 15:50:09 -0700657template <class T>
658void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700659 if (properties().getAlpha() <= 0.0f
660 || properties().getOutline().getAlpha() <= 0.0f
661 || !properties().getOutline().getPath()) {
662 // no shadow to draw
663 return;
664 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700665
666 mat4 shadowMatrixXY(transformFromParent);
667 applyViewPropertyTransforms(shadowMatrixXY);
668
669 // Z matrix needs actual 3d transformation, so mapped z values will be correct
670 mat4 shadowMatrixZ(transformFromParent);
671 applyViewPropertyTransforms(shadowMatrixZ, true);
672
Chris Craik74669862014-08-07 17:27:30 -0700673 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700674 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700675 if (revealClipPath && revealClipPath->isEmpty()) return;
676
Chris Craik77b5cad2014-07-30 18:23:07 -0700677 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700678
679 const SkPath* outlinePath = casterOutlinePath;
680 if (revealClipPath) {
681 // if we can't simply use the caster's path directly, create a temporary one
682 SkPath* frameAllocatedPath = handler.allocPathForFrame();
683
684 // intersect the outline with the convex reveal clip
685 Op(*casterOutlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
686 outlinePath = frameAllocatedPath;
687 }
688
Chris Craikb265e2c2014-03-27 15:50:09 -0700689 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700690 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700691 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
692}
693
John Reck113e0822014-03-18 09:22:59 -0700694#define SHADOW_DELTA 0.1f
695
696template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700697void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
698 const Matrix4& initialTransform, const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
699 OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700700 const int size = zTranslatedNodes.size();
701 if (size == 0
702 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
703 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
704 // no 3d children to draw
705 return;
706 }
707
Chris Craikc3e75f92014-08-27 15:34:52 -0700708 // Apply the base transform of the parent of the 3d children. This isolates
709 // 3d children of the current chunk from transformations made in previous chunks.
710 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
711 renderer.setMatrix(initialTransform);
712
John Reck113e0822014-03-18 09:22:59 -0700713 /**
714 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
715 * with very similar Z heights to draw together.
716 *
717 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
718 * underneath both, and neither's shadow is drawn on top of the other.
719 */
720 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
721 size_t drawIndex, shadowIndex, endIndex;
722 if (mode == kNegativeZChildren) {
723 drawIndex = 0;
724 endIndex = nonNegativeIndex;
725 shadowIndex = endIndex; // draw no shadows
726 } else {
727 drawIndex = nonNegativeIndex;
728 endIndex = size;
729 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
730 }
Chris Craik3f0854292014-04-15 16:18:08 -0700731
732 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
733 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
734
John Reck113e0822014-03-18 09:22:59 -0700735 float lastCasterZ = 0.0f;
736 while (shadowIndex < endIndex || drawIndex < endIndex) {
737 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700738 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
739 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700740 const float casterZ = zTranslatedNodes[shadowIndex].key;
741 // attempt to render the shadow if the caster about to be drawn is its caster,
742 // OR if its caster's Z value is similar to the previous potential caster
743 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700744 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700745
746 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
747 shadowIndex++;
748 continue;
749 }
750 }
751
752 // only the actual child DL draw needs to be in save/restore,
753 // since it modifies the renderer's matrix
754 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
755
Chris Craika7090e02014-06-20 16:01:00 -0700756 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
757 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700758
759 renderer.concatMatrix(childOp->mTransformFromParent);
760 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700761 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700762 childOp->mSkipInOrderDraw = true;
763
764 renderer.restoreToCount(restoreTo);
765 drawIndex++;
766 }
Chris Craikc3e75f92014-08-27 15:34:52 -0700767 renderer.restoreToCount(rootRestoreTo);
John Reck113e0822014-03-18 09:22:59 -0700768}
769
770template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700771void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700772 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
773 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700774 int restoreTo = renderer.getSaveCount();
775
Chris Craikb3cca872014-08-08 18:42:51 -0700776 LinearAllocator& alloc = handler.allocator();
777 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
778 PROPERTY_SAVECOUNT, properties().getClipToBounds());
779
780 // Transform renderer to match background we're projecting onto
781 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
782 const DisplayListOp* op =
783 (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
784 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
785 const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
786 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
787
Chris Craik3f0854292014-04-15 16:18:08 -0700788 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
789 // Either with clipRect, or special saveLayer masking
Chris Craik3f0854292014-04-15 16:18:08 -0700790 if (projectionReceiverOutline != NULL) {
791 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
792 if (projectionReceiverOutline->isRect(NULL)) {
793 // mask to the rect outline simply with clipRect
Chris Craik3f0854292014-04-15 16:18:08 -0700794 ClipRectOp* clipOp = new (alloc) ClipRectOp(
795 outlineBounds.left(), outlineBounds.top(),
796 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
797 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
798 } else {
799 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
800 SaveLayerOp* op = new (alloc) SaveLayerOp(
801 outlineBounds.left(), outlineBounds.top(),
802 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700803 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700804 op->setMask(projectionReceiverOutline);
805 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
806
807 /* TODO: add optimizations here to take advantage of placement/size of projected
808 * children (which may shrink saveLayer area significantly). This is dependent on
809 * passing actual drawing/dirtying bounds of projected content down to native.
810 */
811 }
812 }
813
814 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700815 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700816 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700817
818 // matrix save, concat, and restore can be done safely without allocating operations
819 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
820 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
821 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700822 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700823 childOp->mSkipInOrderDraw = true;
824 renderer.restoreToCount(restoreTo);
825 }
Chris Craik3f0854292014-04-15 16:18:08 -0700826
827 if (projectionReceiverOutline != NULL) {
828 handler(new (alloc) RestoreToCountOp(restoreTo),
829 PROPERTY_SAVECOUNT, properties().getClipToBounds());
830 }
John Reck113e0822014-03-18 09:22:59 -0700831}
832
833/**
834 * This function serves both defer and replay modes, and will organize the displayList's component
835 * operations for a single frame:
836 *
837 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
838 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
839 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
840 * defer vs replay logic, per operation
841 */
842template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700843void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700844 const int level = handler.level();
845 if (mDisplayListData->isEmpty()) {
846 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
847 return;
848 }
849
John Reck25fbb3f2014-06-12 13:46:45 -0700850 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
851 // If we are updating the contents of mLayer, we don't want to apply any of
852 // the RenderNode's properties to this issueOperations pass. Those will all
853 // be applied when the layer is drawn, aka when this is true.
854 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700855 if (useViewProperties) {
856 const Outline& outline = properties().getOutline();
857 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
858 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
859 return;
860 }
John Reck113e0822014-03-18 09:22:59 -0700861 }
862
Chris Craik3f0854292014-04-15 16:18:08 -0700863 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700864
John Reck113e0822014-03-18 09:22:59 -0700865#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700866 const Rect& clipRect = renderer.getLocalClipBounds();
867 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
868 level * 2, "", this, getName(),
869 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700870#endif
871
872 LinearAllocator& alloc = handler.allocator();
873 int restoreTo = renderer.getSaveCount();
874 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700875 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700876
877 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
878 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
879
John Reck25fbb3f2014-06-12 13:46:45 -0700880 if (useViewProperties) {
881 setViewProperties<T>(renderer, handler);
882 }
John Reck113e0822014-03-18 09:22:59 -0700883
Chris Craik8c271ca2014-03-25 10:33:01 -0700884 bool quickRejected = properties().getClipToBounds()
885 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700886 if (!quickRejected) {
Chris Craikc3e75f92014-08-27 15:34:52 -0700887 Matrix4 initialTransform(*(renderer.currentTransform()));
888
John Reck25fbb3f2014-06-12 13:46:45 -0700889 if (drawLayer) {
890 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
891 renderer.getSaveCount() - 1, properties().getClipToBounds());
892 } else {
Chris Craikc166b6c2014-09-05 19:55:30 -0700893 const int saveCountOffset = renderer.getSaveCount() - 1;
894 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
John Reck25fbb3f2014-06-12 13:46:45 -0700895 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
Chris Craik8afd0f22014-08-21 17:41:57 -0700896 for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
897 const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -0700898
Chris Craik8afd0f22014-08-21 17:41:57 -0700899 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
900 buildZSortedChildList(chunk, zTranslatedNodes);
901
Chris Craikc3e75f92014-08-27 15:34:52 -0700902 issueOperationsOf3dChildren(kNegativeZChildren,
903 initialTransform, zTranslatedNodes, renderer, handler);
904
Chris Craik8afd0f22014-08-21 17:41:57 -0700905
906 for (int opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
907 DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -0700908#if DEBUG_DISPLAY_LIST
Chris Craik8afd0f22014-08-21 17:41:57 -0700909 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700910#endif
Chris Craik8afd0f22014-08-21 17:41:57 -0700911 logBuffer.writeCommand(level, op->name());
912 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700913
Chris Craik8afd0f22014-08-21 17:41:57 -0700914 if (CC_UNLIKELY(!mProjectedNodes.isEmpty() && opIndex == projectionReceiveIndex)) {
915 issueOperationsOfProjectedChildren(renderer, handler);
916 }
John Reck25fbb3f2014-06-12 13:46:45 -0700917 }
John Reck113e0822014-03-18 09:22:59 -0700918
Chris Craikc3e75f92014-08-27 15:34:52 -0700919 issueOperationsOf3dChildren(kPositiveZChildren,
920 initialTransform, zTranslatedNodes, renderer, handler);
Chris Craik8afd0f22014-08-21 17:41:57 -0700921 }
John Reck25fbb3f2014-06-12 13:46:45 -0700922 }
John Reck113e0822014-03-18 09:22:59 -0700923 }
924
925 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
926 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700927 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700928 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700929
Chris Craik3f0854292014-04-15 16:18:08 -0700930 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700931 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700932}
933
934} /* namespace uirenderer */
935} /* namespace android */