blob: 254492ff4384d7c220189672984ff1830bcc316c [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
53 String8 cachesLog;
54 Caches::getInstance().dumpMemoryUsage(cachesLog);
55 fprintf(file, "\nCaches:\n%s", cachesLog.string());
56 fprintf(file, "\n");
57
58 fflush(file);
59}
60
John Reck443a7142014-09-04 17:40:05 -070061void RenderNode::debugDumpLayers(const char* prefix) {
62 if (mLayer) {
63 ALOGD("%sNode %p (%s) has layer %p (fbo = %u, wasBuildLayered = %s)",
64 prefix, this, getName(), mLayer, mLayer->getFbo(),
65 mLayer->wasBuildLayered ? "true" : "false");
66 }
67 if (mDisplayListData) {
68 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
69 mDisplayListData->children()[i]->mRenderNode->debugDumpLayers(prefix);
70 }
71 }
72}
73
John Reck8de65a82014-04-09 15:23:38 -070074RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070075 : mDirtyPropertyFields(0)
John Reck8de65a82014-04-09 15:23:38 -070076 , mNeedsDisplayListDataSync(false)
77 , mDisplayListData(0)
John Recke45b1fd2014-04-15 09:50:16 -070078 , mStagingDisplayListData(0)
John Reck68bfe0a2014-06-24 15:34:58 -070079 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070080 , mLayer(0)
81 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070082}
83
84RenderNode::~RenderNode() {
John Reckdcba6722014-07-08 13:59:49 -070085 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -070086 delete mStagingDisplayListData;
John Reck25fbb3f2014-06-12 13:46:45 -070087 LayerRenderer::destroyLayerDeferred(mLayer);
John Reck113e0822014-03-18 09:22:59 -070088}
89
John Reck8de65a82014-04-09 15:23:38 -070090void RenderNode::setStagingDisplayList(DisplayListData* data) {
91 mNeedsDisplayListDataSync = true;
92 delete mStagingDisplayListData;
93 mStagingDisplayListData = data;
94 if (mStagingDisplayListData) {
John Reck09d5cdd2014-07-24 10:36:08 -070095 Caches::getInstance().registerFunctors(mStagingDisplayListData->functors.size());
John Reck113e0822014-03-18 09:22:59 -070096 }
97}
98
99/**
100 * This function is a simplified version of replay(), where we simply retrieve and log the
101 * display list. This function should remain in sync with the replay() function.
102 */
103void RenderNode::output(uint32_t level) {
104 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
Chris Craik3f0854292014-04-15 16:18:08 -0700105 getName(), isRenderable());
John Reck113e0822014-03-18 09:22:59 -0700106 ALOGD("%*s%s %d", level * 2, "", "Save",
107 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
108
John Reckd0a0b2a2014-03-20 16:28:56 -0700109 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -0700110 int flags = DisplayListOp::kOpLogFlag_Recurse;
John Reckdc0349b2014-08-06 15:28:07 -0700111 if (mDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700112 // TODO: consider printing the chunk boundaries here
John Reckdc0349b2014-08-06 15:28:07 -0700113 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
114 mDisplayListData->displayListOps[i]->output(level, flags);
115 }
John Reck113e0822014-03-18 09:22:59 -0700116 }
117
Chris Craik3f0854292014-04-15 16:18:08 -0700118 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700119}
120
John Reckfe5e7b72014-05-23 17:42:28 -0700121int RenderNode::getDebugSize() {
122 int size = sizeof(RenderNode);
123 if (mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700124 size += mStagingDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700125 }
126 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700127 size += mDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700128 }
129 return size;
130}
131
John Reckf4198b72014-04-09 17:00:04 -0700132void RenderNode::prepareTree(TreeInfo& info) {
133 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700134 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700135
136 prepareTreeImpl(info);
137}
138
John Reck68bfe0a2014-06-24 15:34:58 -0700139void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
140 mAnimatorManager.addAnimator(animator);
141}
142
John Recke4267ea2014-06-03 15:53:15 -0700143void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700144 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700145 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700146 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
147 } else {
148 // Hope this is big enough?
149 // TODO: Get this from the display list ops or something
150 info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
151 }
John Recke4267ea2014-06-03 15:53:15 -0700152 }
153}
154
John Recka7c2ea22014-08-08 13:21:00 -0700155void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
John Reck25fbb3f2014-06-12 13:46:45 -0700156 LayerType layerType = properties().layerProperties().type();
157 if (CC_UNLIKELY(layerType == kLayerTypeRenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700158 // Damage applied so far needs to affect our parent, but does not require
159 // the layer to be updated. So we pop/push here to clear out the current
160 // damage and get a clean state for display list or children updates to
161 // affect, which will require the layer to be updated
162 info.damageAccumulator->popTransform();
163 info.damageAccumulator->pushTransform(this);
164 if (dirtyMask & DISPLAY_LIST) {
165 damageSelf(info);
166 }
John Reck25fbb3f2014-06-12 13:46:45 -0700167 }
168}
169
170void RenderNode::pushLayerUpdate(TreeInfo& info) {
171 LayerType layerType = properties().layerProperties().type();
172 // If we are not a layer OR we cannot be rendered (eg, view was detached)
173 // we need to destroy any Layers we may have had previously
174 if (CC_LIKELY(layerType != kLayerTypeRenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700175 if (CC_UNLIKELY(mLayer)) {
176 LayerRenderer::destroyLayer(mLayer);
177 mLayer = NULL;
178 }
179 return;
180 }
181
Chris Craik69e5adf2014-08-14 13:34:01 -0700182 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700183 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700184 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700185 applyLayerPropertiesToLayer(info);
186 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700187 transformUpdateNeeded = true;
John Reck25fbb3f2014-06-12 13:46:45 -0700188 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700189 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
190 LayerRenderer::destroyLayer(mLayer);
191 mLayer = 0;
192 }
John Reck25fbb3f2014-06-12 13:46:45 -0700193 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700194 transformUpdateNeeded = true;
195 }
196
John Reck25fbb3f2014-06-12 13:46:45 -0700197 SkRect dirty;
198 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700199
John Reckc25e5062014-06-18 14:21:29 -0700200 if (!mLayer) {
201 if (info.errorHandler) {
202 std::string msg = "Unable to create layer for ";
203 msg += getName();
204 info.errorHandler->onError(msg);
205 }
206 return;
207 }
208
Chris Craikc71bfca2014-08-21 10:18:58 -0700209 if (transformUpdateNeeded) {
210 // update the transform in window of the layer to reset its origin wrt light source position
211 Matrix4 windowTransform;
212 info.damageAccumulator->computeCurrentTransform(&windowTransform);
213 mLayer->setWindowTransform(windowTransform);
214 }
John Reckc79eabc2014-08-05 11:03:42 -0700215
216 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
217 dirty.roundOut();
John Reck25fbb3f2014-06-12 13:46:45 -0700218 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
219 }
220 // This is not inside the above if because we may have called
221 // updateDeferred on a previous prepare pass that didn't have a renderer
222 if (info.renderer && mLayer->deferredUpdateScheduled) {
223 info.renderer->pushLayerUpdate(mLayer);
224 }
John Reck998a6d82014-08-28 15:35:53 -0700225
226 if (CC_UNLIKELY(info.canvasContext)) {
227 // If canvasContext is not null that means there are prefetched layers
228 // that need to be accounted for. That might be us, so tell CanvasContext
229 // that this layer is in the tree and should not be destroyed.
230 info.canvasContext->markLayerInUse(this);
231 }
John Reck25fbb3f2014-06-12 13:46:45 -0700232}
233
John Recke4267ea2014-06-03 15:53:15 -0700234void RenderNode::prepareTreeImpl(TreeInfo& info) {
John Recka447d292014-06-11 18:39:44 -0700235 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700236
John Reckdcba6722014-07-08 13:59:49 -0700237 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700238 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700239 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700240 uint32_t animatorDirtyMask = 0;
241 if (CC_LIKELY(info.runAnimations)) {
242 animatorDirtyMask = mAnimatorManager.animate(info);
243 }
John Recka7c2ea22014-08-08 13:21:00 -0700244 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700245 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700246 pushStagingDisplayListChanges(info);
247 }
John Reckf4198b72014-04-09 17:00:04 -0700248 prepareSubTree(info, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700249 pushLayerUpdate(info);
250
John Recka447d292014-06-11 18:39:44 -0700251 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700252}
253
John Reck25fbb3f2014-06-12 13:46:45 -0700254void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700255 // Push the animators first so that setupStartValueIfNecessary() is called
256 // before properties() is trampled by stagingProperties(), as they are
257 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700258 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700259 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700260 }
John Reckff941dc2014-05-14 16:34:14 -0700261 if (mDirtyPropertyFields) {
262 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700263 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700264 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700265 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700266 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700267 // We could try to be clever and only re-damage if the matrix changed.
268 // However, we don't need to worry about that. The cost of over-damaging
269 // here is only going to be a single additional map rect of this node
270 // plus a rect join(). The parent's transform (and up) will only be
271 // performed once.
John Recka447d292014-06-11 18:39:44 -0700272 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700273 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700274 }
John Reck25fbb3f2014-06-12 13:46:45 -0700275}
276
277void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
278 if (CC_LIKELY(!mLayer)) return;
279
280 const LayerProperties& props = properties().layerProperties();
281 mLayer->setAlpha(props.alpha(), props.xferMode());
282 mLayer->setColorFilter(props.colorFilter());
283 mLayer->setBlend(props.needsBlending());
284}
285
286void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700287 if (mNeedsDisplayListDataSync) {
288 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700289 // Make sure we inc first so that we don't fluctuate between 0 and 1,
290 // which would thrash the layer cache
291 if (mStagingDisplayListData) {
292 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
293 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
294 }
295 }
296 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -0700297 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700298 mStagingDisplayListData = NULL;
John Reck09d5cdd2014-07-24 10:36:08 -0700299 if (mDisplayListData) {
300 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
301 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
302 }
303 }
John Recke4267ea2014-06-03 15:53:15 -0700304 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700305 }
John Reck8de65a82014-04-09 15:23:38 -0700306}
307
John Reckdcba6722014-07-08 13:59:49 -0700308void RenderNode::deleteDisplayListData() {
309 if (mDisplayListData) {
310 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
311 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
312 }
313 }
314 delete mDisplayListData;
315 mDisplayListData = NULL;
316}
317
John Reckf4198b72014-04-09 17:00:04 -0700318void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700319 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700320 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700321 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700322 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
323 // and thus falling out of async drawing path.
324 if (subtree->ownedBitmapResources.size()) {
325 info.prepareTextures = false;
326 }
327 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
328 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700329 }
John Reck8de65a82014-04-09 15:23:38 -0700330 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700331 DrawRenderNodeOp* op = subtree->children()[i];
332 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700333 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700334 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700335 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700336 }
John Reck113e0822014-03-18 09:22:59 -0700337 }
338}
339
John Reckdcba6722014-07-08 13:59:49 -0700340void RenderNode::destroyHardwareResources() {
341 if (mLayer) {
342 LayerRenderer::destroyLayer(mLayer);
343 mLayer = NULL;
344 }
345 if (mDisplayListData) {
346 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
347 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
348 }
349 if (mNeedsDisplayListDataSync) {
350 // Next prepare tree we are going to push a new display list, so we can
351 // drop our current one now
352 deleteDisplayListData();
353 }
354 }
355}
356
357void RenderNode::decParentRefCount() {
358 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
359 mParentCount--;
360 if (!mParentCount) {
361 // If a child of ours is being attached to our parent then this will incorrectly
362 // destroy its hardware resources. However, this situation is highly unlikely
363 // and the failure is "just" that the layer is re-created, so this should
364 // be safe enough
365 destroyHardwareResources();
366 }
367}
368
John Reck113e0822014-03-18 09:22:59 -0700369/*
370 * For property operations, we pass a savecount of 0, since the operations aren't part of the
371 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700372 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700373 */
374#define PROPERTY_SAVECOUNT 0
375
376template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700377void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700378#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700379 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700380#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700381 if (properties().getLeft() != 0 || properties().getTop() != 0) {
382 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700383 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700384 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500385 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700386 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500387 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700388 }
John Reckf7483e32014-04-11 08:54:47 -0700389 if (properties().hasTransformMatrix()) {
390 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700391 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700392 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700393 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700394 }
395 }
John Reck25fbb3f2014-06-12 13:46:45 -0700396 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700397 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700398 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700399 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700400 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
401
John Reckd0a0b2a2014-03-20 16:28:56 -0700402 renderer.setOverrideLayerAlpha(properties().getAlpha());
403 } else if (!properties().getHasOverlappingRendering()) {
404 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700405 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700406 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700407 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700408 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700409 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700410 properties().getClippingRectForFlags(clipFlags, &layerBounds);
411 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700412 }
413
414 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700415 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700416 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700417 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700418 }
419 }
Chris Craika753f4c2014-07-24 12:39:17 -0700420 if (clipFlags) {
421 Rect clipRect;
422 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700423 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700424 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
425 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700426 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700427 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700428
Chris Craike83cbd42014-09-03 17:52:24 -0700429 // TODO: support nesting round rect clips
Chris Craikaf4d04c2014-07-29 12:50:14 -0700430 if (mProperties.getRevealClip().willClip()) {
431 Rect bounds;
432 mProperties.getRevealClip().getBounds(&bounds);
433 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
434 } else if (mProperties.getOutline().willClip()) {
435 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700436 }
437}
438
439/**
440 * Apply property-based transformations to input matrix
441 *
442 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
443 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
444 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700445void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700446 if (properties().getLeft() != 0 || properties().getTop() != 0) {
447 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700448 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700449 if (properties().getStaticMatrix()) {
450 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700451 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700452 } else if (properties().getAnimationMatrix()) {
453 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700454 matrix.multiply(anim);
455 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700456
Chris Craikcc39e162014-04-25 18:34:11 -0700457 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700458 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700459 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700460 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700461 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700462 } else {
463 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700464 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700465 } else {
466 mat4 true3dMat;
467 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700468 properties().getPivotX() + properties().getTranslationX(),
469 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700470 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700471 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
472 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
473 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
474 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
475 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700476
477 matrix.multiply(true3dMat);
478 }
479 }
480 }
481}
482
483/**
484 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
485 *
486 * This should be called before a call to defer() or drawDisplayList()
487 *
488 * Each DisplayList that serves as a 3d root builds its list of composited children,
489 * which are flagged to not draw in the standard draw loop.
490 */
491void RenderNode::computeOrdering() {
492 ATRACE_CALL();
493 mProjectedNodes.clear();
494
495 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
496 // transform properties are applied correctly to top level children
497 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700498 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700499 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
500 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700501 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700502 }
503}
504
505void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700506 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700507 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700508 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700509 const mat4* transformFromProjectionSurface) {
510 mProjectedNodes.clear();
511 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
512
513 // TODO: should avoid this calculation in most cases
514 // TODO: just calculate single matrix, down to all leaf composited elements
515 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
516 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
517
John Reckd0a0b2a2014-03-20 16:28:56 -0700518 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700519 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
520 opState->mSkipInOrderDraw = true;
521 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
522 compositedChildrenOfProjectionSurface->add(opState);
523 } else {
524 // standard in order draw
525 opState->mSkipInOrderDraw = false;
526 }
527
John Reck087bc0c2014-04-04 16:20:08 -0700528 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700529 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
530 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700531 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700532 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
533 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700534
Chris Craik3f0854292014-04-15 16:18:08 -0700535 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700536 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700537 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700538 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700539 // if receiving projections, collect projecting descendent
540
541 // Note that if a direct descendent is projecting backwards, we pass it's
542 // grandparent projection collection, since it shouldn't project onto it's
543 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700544 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700545 projectionChildren = &mProjectedNodes;
546 projectionTransform = &mat4::identity();
547 } else {
548 if (!haveAppliedPropertiesToProjection) {
549 applyViewPropertyTransforms(localTransformFromProjectionSurface);
550 haveAppliedPropertiesToProjection = true;
551 }
Chris Craik3f0854292014-04-15 16:18:08 -0700552 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700553 projectionChildren = compositedChildrenOfProjectionSurface;
554 projectionTransform = &localTransformFromProjectionSurface;
555 }
Chris Craik3f0854292014-04-15 16:18:08 -0700556 child->computeOrderingImpl(childOp,
557 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700558 }
559 }
John Reck113e0822014-03-18 09:22:59 -0700560}
561
562class DeferOperationHandler {
563public:
564 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
565 : mDeferStruct(deferStruct), mLevel(level) {}
566 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
567 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
568 }
569 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700570 inline void startMark(const char* name) {} // do nothing
571 inline void endMark() {}
572 inline int level() { return mLevel; }
573 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700574 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700575
576private:
577 DeferStateStruct& mDeferStruct;
578 const int mLevel;
579};
580
Chris Craik80d49022014-06-20 15:03:43 -0700581void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700582 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700583 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700584}
585
586class ReplayOperationHandler {
587public:
588 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
589 : mReplayStruct(replayStruct), mLevel(level) {}
590 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
591#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700592 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700593#endif
594 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
595 }
596 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700597 inline void startMark(const char* name) {
598 mReplayStruct.mRenderer.startMark(name);
599 }
600 inline void endMark() {
601 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700602 }
603 inline int level() { return mLevel; }
604 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700605 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700606
607private:
608 ReplayStateStruct& mReplayStruct;
609 const int mLevel;
610};
611
Chris Craik80d49022014-06-20 15:03:43 -0700612void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700613 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700614 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700615}
616
Chris Craik8afd0f22014-08-21 17:41:57 -0700617void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
618 Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
619 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700620
Chris Craik8afd0f22014-08-21 17:41:57 -0700621 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700622 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
623 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700624 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700625
Chris Craik8afd0f22014-08-21 17:41:57 -0700626 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
Chris Craika7090e02014-06-20 16:01:00 -0700627 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700628 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700629 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700630 // regular, in order drawing DisplayList
631 childOp->mSkipInOrderDraw = false;
632 }
633 }
634
Chris Craik8afd0f22014-08-21 17:41:57 -0700635 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700636 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
637}
638
Chris Craikb265e2c2014-03-27 15:50:09 -0700639template <class T>
640void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700641 if (properties().getAlpha() <= 0.0f
642 || properties().getOutline().getAlpha() <= 0.0f
643 || !properties().getOutline().getPath()) {
644 // no shadow to draw
645 return;
646 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700647
648 mat4 shadowMatrixXY(transformFromParent);
649 applyViewPropertyTransforms(shadowMatrixXY);
650
651 // Z matrix needs actual 3d transformation, so mapped z values will be correct
652 mat4 shadowMatrixZ(transformFromParent);
653 applyViewPropertyTransforms(shadowMatrixZ, true);
654
Chris Craik74669862014-08-07 17:27:30 -0700655 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700656 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700657 if (revealClipPath && revealClipPath->isEmpty()) return;
658
Chris Craik77b5cad2014-07-30 18:23:07 -0700659 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700660
661 const SkPath* outlinePath = casterOutlinePath;
662 if (revealClipPath) {
663 // if we can't simply use the caster's path directly, create a temporary one
664 SkPath* frameAllocatedPath = handler.allocPathForFrame();
665
666 // intersect the outline with the convex reveal clip
667 Op(*casterOutlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
668 outlinePath = frameAllocatedPath;
669 }
670
Chris Craikb265e2c2014-03-27 15:50:09 -0700671 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700672 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700673 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
674}
675
John Reck113e0822014-03-18 09:22:59 -0700676#define SHADOW_DELTA 0.1f
677
678template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700679void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
680 const Matrix4& initialTransform, const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
681 OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700682 const int size = zTranslatedNodes.size();
683 if (size == 0
684 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
685 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
686 // no 3d children to draw
687 return;
688 }
689
Chris Craikc3e75f92014-08-27 15:34:52 -0700690 // Apply the base transform of the parent of the 3d children. This isolates
691 // 3d children of the current chunk from transformations made in previous chunks.
692 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
693 renderer.setMatrix(initialTransform);
694
John Reck113e0822014-03-18 09:22:59 -0700695 /**
696 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
697 * with very similar Z heights to draw together.
698 *
699 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
700 * underneath both, and neither's shadow is drawn on top of the other.
701 */
702 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
703 size_t drawIndex, shadowIndex, endIndex;
704 if (mode == kNegativeZChildren) {
705 drawIndex = 0;
706 endIndex = nonNegativeIndex;
707 shadowIndex = endIndex; // draw no shadows
708 } else {
709 drawIndex = nonNegativeIndex;
710 endIndex = size;
711 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
712 }
Chris Craik3f0854292014-04-15 16:18:08 -0700713
714 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
715 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
716
John Reck113e0822014-03-18 09:22:59 -0700717 float lastCasterZ = 0.0f;
718 while (shadowIndex < endIndex || drawIndex < endIndex) {
719 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700720 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
721 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700722 const float casterZ = zTranslatedNodes[shadowIndex].key;
723 // attempt to render the shadow if the caster about to be drawn is its caster,
724 // OR if its caster's Z value is similar to the previous potential caster
725 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700726 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700727
728 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
729 shadowIndex++;
730 continue;
731 }
732 }
733
734 // only the actual child DL draw needs to be in save/restore,
735 // since it modifies the renderer's matrix
736 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
737
Chris Craika7090e02014-06-20 16:01:00 -0700738 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
739 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700740
741 renderer.concatMatrix(childOp->mTransformFromParent);
742 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700743 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700744 childOp->mSkipInOrderDraw = true;
745
746 renderer.restoreToCount(restoreTo);
747 drawIndex++;
748 }
Chris Craikc3e75f92014-08-27 15:34:52 -0700749 renderer.restoreToCount(rootRestoreTo);
John Reck113e0822014-03-18 09:22:59 -0700750}
751
752template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700753void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700754 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
755 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700756 int restoreTo = renderer.getSaveCount();
757
Chris Craikb3cca872014-08-08 18:42:51 -0700758 LinearAllocator& alloc = handler.allocator();
759 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
760 PROPERTY_SAVECOUNT, properties().getClipToBounds());
761
762 // Transform renderer to match background we're projecting onto
763 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
764 const DisplayListOp* op =
765 (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
766 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
767 const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
768 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
769
Chris Craik3f0854292014-04-15 16:18:08 -0700770 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
771 // Either with clipRect, or special saveLayer masking
Chris Craik3f0854292014-04-15 16:18:08 -0700772 if (projectionReceiverOutline != NULL) {
773 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
774 if (projectionReceiverOutline->isRect(NULL)) {
775 // mask to the rect outline simply with clipRect
Chris Craik3f0854292014-04-15 16:18:08 -0700776 ClipRectOp* clipOp = new (alloc) ClipRectOp(
777 outlineBounds.left(), outlineBounds.top(),
778 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
779 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
780 } else {
781 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
782 SaveLayerOp* op = new (alloc) SaveLayerOp(
783 outlineBounds.left(), outlineBounds.top(),
784 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700785 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700786 op->setMask(projectionReceiverOutline);
787 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
788
789 /* TODO: add optimizations here to take advantage of placement/size of projected
790 * children (which may shrink saveLayer area significantly). This is dependent on
791 * passing actual drawing/dirtying bounds of projected content down to native.
792 */
793 }
794 }
795
796 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700797 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700798 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700799
800 // matrix save, concat, and restore can be done safely without allocating operations
801 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
802 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
803 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700804 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700805 childOp->mSkipInOrderDraw = true;
806 renderer.restoreToCount(restoreTo);
807 }
Chris Craik3f0854292014-04-15 16:18:08 -0700808
809 if (projectionReceiverOutline != NULL) {
810 handler(new (alloc) RestoreToCountOp(restoreTo),
811 PROPERTY_SAVECOUNT, properties().getClipToBounds());
812 }
John Reck113e0822014-03-18 09:22:59 -0700813}
814
815/**
816 * This function serves both defer and replay modes, and will organize the displayList's component
817 * operations for a single frame:
818 *
819 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
820 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
821 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
822 * defer vs replay logic, per operation
823 */
824template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700825void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700826 const int level = handler.level();
827 if (mDisplayListData->isEmpty()) {
828 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
829 return;
830 }
831
John Reck25fbb3f2014-06-12 13:46:45 -0700832 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
833 // If we are updating the contents of mLayer, we don't want to apply any of
834 // the RenderNode's properties to this issueOperations pass. Those will all
835 // be applied when the layer is drawn, aka when this is true.
836 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700837 if (useViewProperties) {
838 const Outline& outline = properties().getOutline();
839 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
840 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
841 return;
842 }
John Reck113e0822014-03-18 09:22:59 -0700843 }
844
Chris Craik3f0854292014-04-15 16:18:08 -0700845 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700846
John Reck113e0822014-03-18 09:22:59 -0700847#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700848 const Rect& clipRect = renderer.getLocalClipBounds();
849 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
850 level * 2, "", this, getName(),
851 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700852#endif
853
854 LinearAllocator& alloc = handler.allocator();
855 int restoreTo = renderer.getSaveCount();
856 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700857 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700858
859 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
860 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
861
John Reck25fbb3f2014-06-12 13:46:45 -0700862 if (useViewProperties) {
863 setViewProperties<T>(renderer, handler);
864 }
John Reck113e0822014-03-18 09:22:59 -0700865
Chris Craik8c271ca2014-03-25 10:33:01 -0700866 bool quickRejected = properties().getClipToBounds()
867 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700868 if (!quickRejected) {
Chris Craikc3e75f92014-08-27 15:34:52 -0700869 Matrix4 initialTransform(*(renderer.currentTransform()));
870
John Reck25fbb3f2014-06-12 13:46:45 -0700871 if (drawLayer) {
872 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
873 renderer.getSaveCount() - 1, properties().getClipToBounds());
874 } else {
Chris Craikc166b6c2014-09-05 19:55:30 -0700875 const int saveCountOffset = renderer.getSaveCount() - 1;
876 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
John Reck25fbb3f2014-06-12 13:46:45 -0700877 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
Chris Craik8afd0f22014-08-21 17:41:57 -0700878 for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
879 const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -0700880
Chris Craik8afd0f22014-08-21 17:41:57 -0700881 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
882 buildZSortedChildList(chunk, zTranslatedNodes);
883
Chris Craikc3e75f92014-08-27 15:34:52 -0700884 issueOperationsOf3dChildren(kNegativeZChildren,
885 initialTransform, zTranslatedNodes, renderer, handler);
886
Chris Craik8afd0f22014-08-21 17:41:57 -0700887
888 for (int opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
889 DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -0700890#if DEBUG_DISPLAY_LIST
Chris Craik8afd0f22014-08-21 17:41:57 -0700891 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700892#endif
Chris Craik8afd0f22014-08-21 17:41:57 -0700893 logBuffer.writeCommand(level, op->name());
894 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700895
Chris Craik8afd0f22014-08-21 17:41:57 -0700896 if (CC_UNLIKELY(!mProjectedNodes.isEmpty() && opIndex == projectionReceiveIndex)) {
897 issueOperationsOfProjectedChildren(renderer, handler);
898 }
John Reck25fbb3f2014-06-12 13:46:45 -0700899 }
John Reck113e0822014-03-18 09:22:59 -0700900
Chris Craikc3e75f92014-08-27 15:34:52 -0700901 issueOperationsOf3dChildren(kPositiveZChildren,
902 initialTransform, zTranslatedNodes, renderer, handler);
Chris Craik8afd0f22014-08-21 17:41:57 -0700903 }
John Reck25fbb3f2014-06-12 13:46:45 -0700904 }
John Reck113e0822014-03-18 09:22:59 -0700905 }
906
907 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
908 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700909 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700910 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700911
Chris Craik3f0854292014-04-15 16:18:08 -0700912 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700913 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700914}
915
916} /* namespace uirenderer */
917} /* namespace android */