blob: c9ed9a7d780b64efa364c880342fedd04885a5a7 [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;
101 if (mStagingDisplayListData) {
John Reck09d5cdd2014-07-24 10:36:08 -0700102 Caches::getInstance().registerFunctors(mStagingDisplayListData->functors.size());
John Reck113e0822014-03-18 09:22:59 -0700103 }
104}
105
106/**
107 * This function is a simplified version of replay(), where we simply retrieve and log the
108 * display list. This function should remain in sync with the replay() function.
109 */
110void RenderNode::output(uint32_t level) {
111 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
Chris Craik3f0854292014-04-15 16:18:08 -0700112 getName(), isRenderable());
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 Reck8de65a82014-04-09 15:23:38 -0700308 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700309 mStagingDisplayListData = NULL;
John Reck09d5cdd2014-07-24 10:36:08 -0700310 if (mDisplayListData) {
311 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
312 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
313 }
314 }
John Recke4267ea2014-06-03 15:53:15 -0700315 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700316 }
John Reck8de65a82014-04-09 15:23:38 -0700317}
318
John Reckdcba6722014-07-08 13:59:49 -0700319void RenderNode::deleteDisplayListData() {
320 if (mDisplayListData) {
321 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
322 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
323 }
324 }
325 delete mDisplayListData;
326 mDisplayListData = NULL;
327}
328
John Reckf4198b72014-04-09 17:00:04 -0700329void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700330 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700331 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700332 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700333 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
334 // and thus falling out of async drawing path.
335 if (subtree->ownedBitmapResources.size()) {
336 info.prepareTextures = false;
337 }
338 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
339 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700340 }
John Reck8de65a82014-04-09 15:23:38 -0700341 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700342 DrawRenderNodeOp* op = subtree->children()[i];
343 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700344 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700345 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700346 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700347 }
John Reck113e0822014-03-18 09:22:59 -0700348 }
349}
350
John Reckdcba6722014-07-08 13:59:49 -0700351void RenderNode::destroyHardwareResources() {
352 if (mLayer) {
353 LayerRenderer::destroyLayer(mLayer);
354 mLayer = NULL;
355 }
356 if (mDisplayListData) {
357 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
358 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
359 }
360 if (mNeedsDisplayListDataSync) {
361 // Next prepare tree we are going to push a new display list, so we can
362 // drop our current one now
363 deleteDisplayListData();
364 }
365 }
366}
367
368void RenderNode::decParentRefCount() {
369 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
370 mParentCount--;
371 if (!mParentCount) {
372 // If a child of ours is being attached to our parent then this will incorrectly
373 // destroy its hardware resources. However, this situation is highly unlikely
374 // and the failure is "just" that the layer is re-created, so this should
375 // be safe enough
376 destroyHardwareResources();
377 }
378}
379
John Reck113e0822014-03-18 09:22:59 -0700380/*
381 * For property operations, we pass a savecount of 0, since the operations aren't part of the
382 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700383 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700384 */
385#define PROPERTY_SAVECOUNT 0
386
387template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700388void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700389#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700390 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700391#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700392 if (properties().getLeft() != 0 || properties().getTop() != 0) {
393 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700394 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700395 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500396 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700397 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500398 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700399 }
John Reckf7483e32014-04-11 08:54:47 -0700400 if (properties().hasTransformMatrix()) {
401 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700402 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700403 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700404 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700405 }
406 }
John Reck25fbb3f2014-06-12 13:46:45 -0700407 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700408 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700409 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700410 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700411 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
412
John Reckd0a0b2a2014-03-20 16:28:56 -0700413 renderer.setOverrideLayerAlpha(properties().getAlpha());
414 } else if (!properties().getHasOverlappingRendering()) {
415 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700416 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700417 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700418 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700419 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700420 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700421 properties().getClippingRectForFlags(clipFlags, &layerBounds);
422 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700423 }
424
425 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700426 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700427 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700428 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700429 }
430 }
Chris Craika753f4c2014-07-24 12:39:17 -0700431 if (clipFlags) {
432 Rect clipRect;
433 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700434 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700435 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
436 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700437 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700438 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700439
Chris Craike83cbd42014-09-03 17:52:24 -0700440 // TODO: support nesting round rect clips
Chris Craikaf4d04c2014-07-29 12:50:14 -0700441 if (mProperties.getRevealClip().willClip()) {
442 Rect bounds;
443 mProperties.getRevealClip().getBounds(&bounds);
444 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
445 } else if (mProperties.getOutline().willClip()) {
446 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700447 }
448}
449
450/**
451 * Apply property-based transformations to input matrix
452 *
453 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
454 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
455 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700456void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700457 if (properties().getLeft() != 0 || properties().getTop() != 0) {
458 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700459 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700460 if (properties().getStaticMatrix()) {
461 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700462 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700463 } else if (properties().getAnimationMatrix()) {
464 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700465 matrix.multiply(anim);
466 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700467
Chris Craikcc39e162014-04-25 18:34:11 -0700468 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700469 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700470 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700471 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700472 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700473 } else {
474 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700475 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700476 } else {
477 mat4 true3dMat;
478 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700479 properties().getPivotX() + properties().getTranslationX(),
480 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700481 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700482 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
483 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
484 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
485 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
486 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700487
488 matrix.multiply(true3dMat);
489 }
490 }
491 }
492}
493
494/**
495 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
496 *
497 * This should be called before a call to defer() or drawDisplayList()
498 *
499 * Each DisplayList that serves as a 3d root builds its list of composited children,
500 * which are flagged to not draw in the standard draw loop.
501 */
502void RenderNode::computeOrdering() {
503 ATRACE_CALL();
504 mProjectedNodes.clear();
505
506 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
507 // transform properties are applied correctly to top level children
508 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700509 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700510 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
511 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700512 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700513 }
514}
515
516void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700517 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700518 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700519 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700520 const mat4* transformFromProjectionSurface) {
521 mProjectedNodes.clear();
522 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
523
524 // TODO: should avoid this calculation in most cases
525 // TODO: just calculate single matrix, down to all leaf composited elements
526 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
527 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
528
John Reckd0a0b2a2014-03-20 16:28:56 -0700529 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700530 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
531 opState->mSkipInOrderDraw = true;
532 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
533 compositedChildrenOfProjectionSurface->add(opState);
534 } else {
535 // standard in order draw
536 opState->mSkipInOrderDraw = false;
537 }
538
John Reck087bc0c2014-04-04 16:20:08 -0700539 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700540 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
541 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700542 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700543 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
544 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700545
Chris Craik3f0854292014-04-15 16:18:08 -0700546 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700547 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700548 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700549 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700550 // if receiving projections, collect projecting descendent
551
552 // Note that if a direct descendent is projecting backwards, we pass it's
553 // grandparent projection collection, since it shouldn't project onto it's
554 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700555 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700556 projectionChildren = &mProjectedNodes;
557 projectionTransform = &mat4::identity();
558 } else {
559 if (!haveAppliedPropertiesToProjection) {
560 applyViewPropertyTransforms(localTransformFromProjectionSurface);
561 haveAppliedPropertiesToProjection = true;
562 }
Chris Craik3f0854292014-04-15 16:18:08 -0700563 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700564 projectionChildren = compositedChildrenOfProjectionSurface;
565 projectionTransform = &localTransformFromProjectionSurface;
566 }
Chris Craik3f0854292014-04-15 16:18:08 -0700567 child->computeOrderingImpl(childOp,
568 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700569 }
570 }
John Reck113e0822014-03-18 09:22:59 -0700571}
572
573class DeferOperationHandler {
574public:
575 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
576 : mDeferStruct(deferStruct), mLevel(level) {}
577 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
578 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
579 }
580 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700581 inline void startMark(const char* name) {} // do nothing
582 inline void endMark() {}
583 inline int level() { return mLevel; }
584 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700585 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700586
587private:
588 DeferStateStruct& mDeferStruct;
589 const int mLevel;
590};
591
Chris Craik80d49022014-06-20 15:03:43 -0700592void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700593 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700594 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700595}
596
597class ReplayOperationHandler {
598public:
599 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
600 : mReplayStruct(replayStruct), mLevel(level) {}
601 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
602#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700603 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700604#endif
605 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
606 }
607 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700608 inline void startMark(const char* name) {
609 mReplayStruct.mRenderer.startMark(name);
610 }
611 inline void endMark() {
612 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700613 }
614 inline int level() { return mLevel; }
615 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700616 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700617
618private:
619 ReplayStateStruct& mReplayStruct;
620 const int mLevel;
621};
622
Chris Craik80d49022014-06-20 15:03:43 -0700623void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700624 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700625 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700626}
627
Chris Craik8afd0f22014-08-21 17:41:57 -0700628void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
629 Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
630 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700631
Chris Craik8afd0f22014-08-21 17:41:57 -0700632 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700633 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
634 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700635 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700636
Chris Craik8afd0f22014-08-21 17:41:57 -0700637 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
Chris Craika7090e02014-06-20 16:01:00 -0700638 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700639 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700640 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700641 // regular, in order drawing DisplayList
642 childOp->mSkipInOrderDraw = false;
643 }
644 }
645
Chris Craik8afd0f22014-08-21 17:41:57 -0700646 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700647 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
648}
649
Chris Craikb265e2c2014-03-27 15:50:09 -0700650template <class T>
651void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700652 if (properties().getAlpha() <= 0.0f
653 || properties().getOutline().getAlpha() <= 0.0f
654 || !properties().getOutline().getPath()) {
655 // no shadow to draw
656 return;
657 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700658
659 mat4 shadowMatrixXY(transformFromParent);
660 applyViewPropertyTransforms(shadowMatrixXY);
661
662 // Z matrix needs actual 3d transformation, so mapped z values will be correct
663 mat4 shadowMatrixZ(transformFromParent);
664 applyViewPropertyTransforms(shadowMatrixZ, true);
665
Chris Craik74669862014-08-07 17:27:30 -0700666 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700667 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700668 if (revealClipPath && revealClipPath->isEmpty()) return;
669
Chris Craik77b5cad2014-07-30 18:23:07 -0700670 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700671
672 const SkPath* outlinePath = casterOutlinePath;
673 if (revealClipPath) {
674 // if we can't simply use the caster's path directly, create a temporary one
675 SkPath* frameAllocatedPath = handler.allocPathForFrame();
676
677 // intersect the outline with the convex reveal clip
678 Op(*casterOutlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
679 outlinePath = frameAllocatedPath;
680 }
681
Chris Craikb265e2c2014-03-27 15:50:09 -0700682 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700683 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700684 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
685}
686
John Reck113e0822014-03-18 09:22:59 -0700687#define SHADOW_DELTA 0.1f
688
689template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700690void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
691 const Matrix4& initialTransform, const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
692 OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700693 const int size = zTranslatedNodes.size();
694 if (size == 0
695 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
696 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
697 // no 3d children to draw
698 return;
699 }
700
Chris Craikc3e75f92014-08-27 15:34:52 -0700701 // Apply the base transform of the parent of the 3d children. This isolates
702 // 3d children of the current chunk from transformations made in previous chunks.
703 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
704 renderer.setMatrix(initialTransform);
705
John Reck113e0822014-03-18 09:22:59 -0700706 /**
707 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
708 * with very similar Z heights to draw together.
709 *
710 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
711 * underneath both, and neither's shadow is drawn on top of the other.
712 */
713 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
714 size_t drawIndex, shadowIndex, endIndex;
715 if (mode == kNegativeZChildren) {
716 drawIndex = 0;
717 endIndex = nonNegativeIndex;
718 shadowIndex = endIndex; // draw no shadows
719 } else {
720 drawIndex = nonNegativeIndex;
721 endIndex = size;
722 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
723 }
Chris Craik3f0854292014-04-15 16:18:08 -0700724
725 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
726 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
727
John Reck113e0822014-03-18 09:22:59 -0700728 float lastCasterZ = 0.0f;
729 while (shadowIndex < endIndex || drawIndex < endIndex) {
730 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700731 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
732 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700733 const float casterZ = zTranslatedNodes[shadowIndex].key;
734 // attempt to render the shadow if the caster about to be drawn is its caster,
735 // OR if its caster's Z value is similar to the previous potential caster
736 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700737 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700738
739 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
740 shadowIndex++;
741 continue;
742 }
743 }
744
745 // only the actual child DL draw needs to be in save/restore,
746 // since it modifies the renderer's matrix
747 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
748
Chris Craika7090e02014-06-20 16:01:00 -0700749 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
750 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700751
752 renderer.concatMatrix(childOp->mTransformFromParent);
753 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700754 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700755 childOp->mSkipInOrderDraw = true;
756
757 renderer.restoreToCount(restoreTo);
758 drawIndex++;
759 }
Chris Craikc3e75f92014-08-27 15:34:52 -0700760 renderer.restoreToCount(rootRestoreTo);
John Reck113e0822014-03-18 09:22:59 -0700761}
762
763template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700764void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700765 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
766 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700767 int restoreTo = renderer.getSaveCount();
768
Chris Craikb3cca872014-08-08 18:42:51 -0700769 LinearAllocator& alloc = handler.allocator();
770 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
771 PROPERTY_SAVECOUNT, properties().getClipToBounds());
772
773 // Transform renderer to match background we're projecting onto
774 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
775 const DisplayListOp* op =
776 (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
777 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
778 const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
779 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
780
Chris Craik3f0854292014-04-15 16:18:08 -0700781 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
782 // Either with clipRect, or special saveLayer masking
Chris Craik3f0854292014-04-15 16:18:08 -0700783 if (projectionReceiverOutline != NULL) {
784 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
785 if (projectionReceiverOutline->isRect(NULL)) {
786 // mask to the rect outline simply with clipRect
Chris Craik3f0854292014-04-15 16:18:08 -0700787 ClipRectOp* clipOp = new (alloc) ClipRectOp(
788 outlineBounds.left(), outlineBounds.top(),
789 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
790 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
791 } else {
792 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
793 SaveLayerOp* op = new (alloc) SaveLayerOp(
794 outlineBounds.left(), outlineBounds.top(),
795 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700796 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700797 op->setMask(projectionReceiverOutline);
798 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
799
800 /* TODO: add optimizations here to take advantage of placement/size of projected
801 * children (which may shrink saveLayer area significantly). This is dependent on
802 * passing actual drawing/dirtying bounds of projected content down to native.
803 */
804 }
805 }
806
807 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700808 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700809 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700810
811 // matrix save, concat, and restore can be done safely without allocating operations
812 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
813 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
814 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700815 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700816 childOp->mSkipInOrderDraw = true;
817 renderer.restoreToCount(restoreTo);
818 }
Chris Craik3f0854292014-04-15 16:18:08 -0700819
820 if (projectionReceiverOutline != NULL) {
821 handler(new (alloc) RestoreToCountOp(restoreTo),
822 PROPERTY_SAVECOUNT, properties().getClipToBounds());
823 }
John Reck113e0822014-03-18 09:22:59 -0700824}
825
826/**
827 * This function serves both defer and replay modes, and will organize the displayList's component
828 * operations for a single frame:
829 *
830 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
831 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
832 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
833 * defer vs replay logic, per operation
834 */
835template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700836void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700837 const int level = handler.level();
838 if (mDisplayListData->isEmpty()) {
839 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
840 return;
841 }
842
John Reck25fbb3f2014-06-12 13:46:45 -0700843 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
844 // If we are updating the contents of mLayer, we don't want to apply any of
845 // the RenderNode's properties to this issueOperations pass. Those will all
846 // be applied when the layer is drawn, aka when this is true.
847 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700848 if (useViewProperties) {
849 const Outline& outline = properties().getOutline();
850 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
851 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
852 return;
853 }
John Reck113e0822014-03-18 09:22:59 -0700854 }
855
Chris Craik3f0854292014-04-15 16:18:08 -0700856 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700857
John Reck113e0822014-03-18 09:22:59 -0700858#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700859 const Rect& clipRect = renderer.getLocalClipBounds();
860 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
861 level * 2, "", this, getName(),
862 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700863#endif
864
865 LinearAllocator& alloc = handler.allocator();
866 int restoreTo = renderer.getSaveCount();
867 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700868 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700869
870 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
871 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
872
John Reck25fbb3f2014-06-12 13:46:45 -0700873 if (useViewProperties) {
874 setViewProperties<T>(renderer, handler);
875 }
John Reck113e0822014-03-18 09:22:59 -0700876
Chris Craik8c271ca2014-03-25 10:33:01 -0700877 bool quickRejected = properties().getClipToBounds()
878 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700879 if (!quickRejected) {
Chris Craikc3e75f92014-08-27 15:34:52 -0700880 Matrix4 initialTransform(*(renderer.currentTransform()));
881
John Reck25fbb3f2014-06-12 13:46:45 -0700882 if (drawLayer) {
883 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
884 renderer.getSaveCount() - 1, properties().getClipToBounds());
885 } else {
Chris Craikc166b6c2014-09-05 19:55:30 -0700886 const int saveCountOffset = renderer.getSaveCount() - 1;
887 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
John Reck25fbb3f2014-06-12 13:46:45 -0700888 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
Chris Craik8afd0f22014-08-21 17:41:57 -0700889 for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
890 const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -0700891
Chris Craik8afd0f22014-08-21 17:41:57 -0700892 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
893 buildZSortedChildList(chunk, zTranslatedNodes);
894
Chris Craikc3e75f92014-08-27 15:34:52 -0700895 issueOperationsOf3dChildren(kNegativeZChildren,
896 initialTransform, zTranslatedNodes, renderer, handler);
897
Chris Craik8afd0f22014-08-21 17:41:57 -0700898
899 for (int opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
900 DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -0700901#if DEBUG_DISPLAY_LIST
Chris Craik8afd0f22014-08-21 17:41:57 -0700902 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700903#endif
Chris Craik8afd0f22014-08-21 17:41:57 -0700904 logBuffer.writeCommand(level, op->name());
905 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700906
Chris Craik8afd0f22014-08-21 17:41:57 -0700907 if (CC_UNLIKELY(!mProjectedNodes.isEmpty() && opIndex == projectionReceiveIndex)) {
908 issueOperationsOfProjectedChildren(renderer, handler);
909 }
John Reck25fbb3f2014-06-12 13:46:45 -0700910 }
John Reck113e0822014-03-18 09:22:59 -0700911
Chris Craikc3e75f92014-08-27 15:34:52 -0700912 issueOperationsOf3dChildren(kPositiveZChildren,
913 initialTransform, zTranslatedNodes, renderer, handler);
Chris Craik8afd0f22014-08-21 17:41:57 -0700914 }
John Reck25fbb3f2014-06-12 13:46:45 -0700915 }
John Reck113e0822014-03-18 09:22:59 -0700916 }
917
918 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
919 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700920 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700921 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700922
Chris Craik3f0854292014-04-15 16:18:08 -0700923 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700924 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700925}
926
927} /* namespace uirenderer */
928} /* namespace android */