blob: 8fb1f644109ef7213c4acedfcf118e3b73808fee [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 Reck8de65a82014-04-09 15:23:38 -070061RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070062 : mDirtyPropertyFields(0)
John Reck8de65a82014-04-09 15:23:38 -070063 , mNeedsDisplayListDataSync(false)
64 , mDisplayListData(0)
John Recke45b1fd2014-04-15 09:50:16 -070065 , mStagingDisplayListData(0)
John Reck68bfe0a2014-06-24 15:34:58 -070066 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070067 , mLayer(0)
68 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070069}
70
71RenderNode::~RenderNode() {
John Reckdcba6722014-07-08 13:59:49 -070072 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -070073 delete mStagingDisplayListData;
John Reck25fbb3f2014-06-12 13:46:45 -070074 LayerRenderer::destroyLayerDeferred(mLayer);
John Reck113e0822014-03-18 09:22:59 -070075}
76
John Reck8de65a82014-04-09 15:23:38 -070077void RenderNode::setStagingDisplayList(DisplayListData* data) {
78 mNeedsDisplayListDataSync = true;
79 delete mStagingDisplayListData;
80 mStagingDisplayListData = data;
81 if (mStagingDisplayListData) {
John Reck09d5cdd2014-07-24 10:36:08 -070082 Caches::getInstance().registerFunctors(mStagingDisplayListData->functors.size());
John Reck113e0822014-03-18 09:22:59 -070083 }
84}
85
86/**
87 * This function is a simplified version of replay(), where we simply retrieve and log the
88 * display list. This function should remain in sync with the replay() function.
89 */
90void RenderNode::output(uint32_t level) {
91 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
Chris Craik3f085422014-04-15 16:18:08 -070092 getName(), isRenderable());
John Reck113e0822014-03-18 09:22:59 -070093 ALOGD("%*s%s %d", level * 2, "", "Save",
94 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
95
John Reckd0a0b2a2014-03-20 16:28:56 -070096 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070097 int flags = DisplayListOp::kOpLogFlag_Recurse;
John Reckdc0349b2014-08-06 15:28:07 -070098 if (mDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -070099 // TODO: consider printing the chunk boundaries here
John Reckdc0349b2014-08-06 15:28:07 -0700100 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
101 mDisplayListData->displayListOps[i]->output(level, flags);
102 }
John Reck113e0822014-03-18 09:22:59 -0700103 }
104
Chris Craik3f085422014-04-15 16:18:08 -0700105 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700106}
107
John Reckfe5e7b72014-05-23 17:42:28 -0700108int RenderNode::getDebugSize() {
109 int size = sizeof(RenderNode);
110 if (mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700111 size += mStagingDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700112 }
113 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700114 size += mDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700115 }
116 return size;
117}
118
John Reckf4198b72014-04-09 17:00:04 -0700119void RenderNode::prepareTree(TreeInfo& info) {
120 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700121 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700122
123 prepareTreeImpl(info);
124}
125
John Reck68bfe0a2014-06-24 15:34:58 -0700126void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
127 mAnimatorManager.addAnimator(animator);
128}
129
John Recke4267ea2014-06-03 15:53:15 -0700130void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700131 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700132 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700133 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
134 } else {
135 // Hope this is big enough?
136 // TODO: Get this from the display list ops or something
137 info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
138 }
John Recke4267ea2014-06-03 15:53:15 -0700139 }
140}
141
John Recka7c2ea22014-08-08 13:21:00 -0700142void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
John Reck25fbb3f2014-06-12 13:46:45 -0700143 LayerType layerType = properties().layerProperties().type();
144 if (CC_UNLIKELY(layerType == kLayerTypeRenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700145 // Damage applied so far needs to affect our parent, but does not require
146 // the layer to be updated. So we pop/push here to clear out the current
147 // damage and get a clean state for display list or children updates to
148 // affect, which will require the layer to be updated
149 info.damageAccumulator->popTransform();
150 info.damageAccumulator->pushTransform(this);
151 if (dirtyMask & DISPLAY_LIST) {
152 damageSelf(info);
153 }
John Reck25fbb3f2014-06-12 13:46:45 -0700154 }
155}
156
157void RenderNode::pushLayerUpdate(TreeInfo& info) {
158 LayerType layerType = properties().layerProperties().type();
159 // If we are not a layer OR we cannot be rendered (eg, view was detached)
160 // we need to destroy any Layers we may have had previously
161 if (CC_LIKELY(layerType != kLayerTypeRenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700162 if (CC_UNLIKELY(mLayer)) {
163 LayerRenderer::destroyLayer(mLayer);
164 mLayer = NULL;
165 }
166 return;
167 }
168
Chris Craik69e5adf2014-08-14 13:34:01 -0700169 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700170 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700171 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700172 applyLayerPropertiesToLayer(info);
173 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700174 transformUpdateNeeded = true;
John Reck25fbb3f2014-06-12 13:46:45 -0700175 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700176 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
177 LayerRenderer::destroyLayer(mLayer);
178 mLayer = 0;
179 }
John Reck25fbb3f2014-06-12 13:46:45 -0700180 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700181 transformUpdateNeeded = true;
182 }
183
John Reck25fbb3f2014-06-12 13:46:45 -0700184 SkRect dirty;
185 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700186
John Reckc25e5062014-06-18 14:21:29 -0700187 if (!mLayer) {
188 if (info.errorHandler) {
189 std::string msg = "Unable to create layer for ";
190 msg += getName();
191 info.errorHandler->onError(msg);
192 }
193 return;
194 }
195
Chris Craikc71bfca2014-08-21 10:18:58 -0700196 if (transformUpdateNeeded) {
197 // update the transform in window of the layer to reset its origin wrt light source position
198 Matrix4 windowTransform;
199 info.damageAccumulator->computeCurrentTransform(&windowTransform);
200 mLayer->setWindowTransform(windowTransform);
201 }
John Reckc79eabc2014-08-05 11:03:42 -0700202
203 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
204 dirty.roundOut();
John Reck25fbb3f2014-06-12 13:46:45 -0700205 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
206 }
207 // This is not inside the above if because we may have called
208 // updateDeferred on a previous prepare pass that didn't have a renderer
209 if (info.renderer && mLayer->deferredUpdateScheduled) {
210 info.renderer->pushLayerUpdate(mLayer);
211 }
John Reck998a6d82014-08-28 15:35:53 -0700212
213 if (CC_UNLIKELY(info.canvasContext)) {
214 // If canvasContext is not null that means there are prefetched layers
215 // that need to be accounted for. That might be us, so tell CanvasContext
216 // that this layer is in the tree and should not be destroyed.
217 info.canvasContext->markLayerInUse(this);
218 }
John Reck25fbb3f2014-06-12 13:46:45 -0700219}
220
John Recke4267ea2014-06-03 15:53:15 -0700221void RenderNode::prepareTreeImpl(TreeInfo& info) {
John Recka447d292014-06-11 18:39:44 -0700222 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700223
John Reckdcba6722014-07-08 13:59:49 -0700224 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700225 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700226 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700227 uint32_t animatorDirtyMask = 0;
228 if (CC_LIKELY(info.runAnimations)) {
229 animatorDirtyMask = mAnimatorManager.animate(info);
230 }
John Recka7c2ea22014-08-08 13:21:00 -0700231 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700232 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700233 pushStagingDisplayListChanges(info);
234 }
John Reckf4198b72014-04-09 17:00:04 -0700235 prepareSubTree(info, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700236 pushLayerUpdate(info);
237
John Recka447d292014-06-11 18:39:44 -0700238 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700239}
240
John Reck25fbb3f2014-06-12 13:46:45 -0700241void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700242 // Push the animators first so that setupStartValueIfNecessary() is called
243 // before properties() is trampled by stagingProperties(), as they are
244 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700245 if (CC_LIKELY(info.runAnimations)) {
John Reck119907c2014-08-14 09:02:01 -0700246 mAnimatorManager.pushStaging();
John Reck9eb9f6f2014-08-21 11:23:05 -0700247 }
John Reckff941dc2014-05-14 16:34:14 -0700248 if (mDirtyPropertyFields) {
249 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700250 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700251 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700252 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700253 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700254 // We could try to be clever and only re-damage if the matrix changed.
255 // However, we don't need to worry about that. The cost of over-damaging
256 // here is only going to be a single additional map rect of this node
257 // plus a rect join(). The parent's transform (and up) will only be
258 // performed once.
John Recka447d292014-06-11 18:39:44 -0700259 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700260 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700261 }
John Reck25fbb3f2014-06-12 13:46:45 -0700262}
263
264void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
265 if (CC_LIKELY(!mLayer)) return;
266
267 const LayerProperties& props = properties().layerProperties();
268 mLayer->setAlpha(props.alpha(), props.xferMode());
269 mLayer->setColorFilter(props.colorFilter());
270 mLayer->setBlend(props.needsBlending());
271}
272
273void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700274 if (mNeedsDisplayListDataSync) {
275 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700276 // Make sure we inc first so that we don't fluctuate between 0 and 1,
277 // which would thrash the layer cache
278 if (mStagingDisplayListData) {
279 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
280 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
281 }
282 }
283 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -0700284 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700285 mStagingDisplayListData = NULL;
John Reck09d5cdd2014-07-24 10:36:08 -0700286 if (mDisplayListData) {
287 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
288 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
289 }
290 }
John Recke4267ea2014-06-03 15:53:15 -0700291 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700292 }
John Reck8de65a82014-04-09 15:23:38 -0700293}
294
John Reckdcba6722014-07-08 13:59:49 -0700295void RenderNode::deleteDisplayListData() {
296 if (mDisplayListData) {
297 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
298 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
299 }
300 }
301 delete mDisplayListData;
302 mDisplayListData = NULL;
303}
304
John Reckf4198b72014-04-09 17:00:04 -0700305void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700306 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700307 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700308 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700309 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
310 // and thus falling out of async drawing path.
311 if (subtree->ownedBitmapResources.size()) {
312 info.prepareTextures = false;
313 }
314 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
315 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700316 }
John Reck8de65a82014-04-09 15:23:38 -0700317 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700318 DrawRenderNodeOp* op = subtree->children()[i];
319 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700320 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700321 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700322 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700323 }
John Reck113e0822014-03-18 09:22:59 -0700324 }
325}
326
John Reckdcba6722014-07-08 13:59:49 -0700327void RenderNode::destroyHardwareResources() {
328 if (mLayer) {
329 LayerRenderer::destroyLayer(mLayer);
330 mLayer = NULL;
331 }
332 if (mDisplayListData) {
333 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
334 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
335 }
336 if (mNeedsDisplayListDataSync) {
337 // Next prepare tree we are going to push a new display list, so we can
338 // drop our current one now
339 deleteDisplayListData();
340 }
341 }
342}
343
344void RenderNode::decParentRefCount() {
345 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
346 mParentCount--;
347 if (!mParentCount) {
348 // If a child of ours is being attached to our parent then this will incorrectly
349 // destroy its hardware resources. However, this situation is highly unlikely
350 // and the failure is "just" that the layer is re-created, so this should
351 // be safe enough
352 destroyHardwareResources();
353 }
354}
355
John Reck113e0822014-03-18 09:22:59 -0700356/*
357 * For property operations, we pass a savecount of 0, since the operations aren't part of the
358 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700359 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700360 */
361#define PROPERTY_SAVECOUNT 0
362
363template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700364void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700365#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700366 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700367#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700368 if (properties().getLeft() != 0 || properties().getTop() != 0) {
369 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700370 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700371 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500372 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700373 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500374 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700375 }
John Reckf7483e32014-04-11 08:54:47 -0700376 if (properties().hasTransformMatrix()) {
377 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700378 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700379 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700380 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700381 }
382 }
John Reck25fbb3f2014-06-12 13:46:45 -0700383 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700384 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700385 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700386 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700387 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
388
John Reckd0a0b2a2014-03-20 16:28:56 -0700389 renderer.setOverrideLayerAlpha(properties().getAlpha());
390 } else if (!properties().getHasOverlappingRendering()) {
391 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700392 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700393 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700394 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700395 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700396 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700397 properties().getClippingRectForFlags(clipFlags, &layerBounds);
398 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700399 }
400
401 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700402 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700403 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700404 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700405 }
406 }
Chris Craika753f4c2014-07-24 12:39:17 -0700407 if (clipFlags) {
408 Rect clipRect;
409 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700410 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700411 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
412 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700413 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700414 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700415
Chris Craike83cbd42014-09-03 17:52:24 -0700416 // TODO: support nesting round rect clips
Chris Craikaf4d04c2014-07-29 12:50:14 -0700417 if (mProperties.getRevealClip().willClip()) {
418 Rect bounds;
419 mProperties.getRevealClip().getBounds(&bounds);
420 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
421 } else if (mProperties.getOutline().willClip()) {
422 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700423 }
424}
425
426/**
427 * Apply property-based transformations to input matrix
428 *
429 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
430 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
431 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700432void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700433 if (properties().getLeft() != 0 || properties().getTop() != 0) {
434 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700435 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700436 if (properties().getStaticMatrix()) {
437 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700438 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700439 } else if (properties().getAnimationMatrix()) {
440 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700441 matrix.multiply(anim);
442 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700443
Chris Craikcc39e162014-04-25 18:34:11 -0700444 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700445 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700446 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700447 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700448 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700449 } else {
450 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700451 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700452 } else {
453 mat4 true3dMat;
454 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700455 properties().getPivotX() + properties().getTranslationX(),
456 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700457 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700458 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
459 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
460 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
461 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
462 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700463
464 matrix.multiply(true3dMat);
465 }
466 }
467 }
468}
469
470/**
471 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
472 *
473 * This should be called before a call to defer() or drawDisplayList()
474 *
475 * Each DisplayList that serves as a 3d root builds its list of composited children,
476 * which are flagged to not draw in the standard draw loop.
477 */
478void RenderNode::computeOrdering() {
479 ATRACE_CALL();
480 mProjectedNodes.clear();
481
482 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
483 // transform properties are applied correctly to top level children
484 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700485 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700486 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
487 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f085422014-04-15 16:18:08 -0700488 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700489 }
490}
491
492void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700493 DrawRenderNodeOp* opState,
Chris Craik3f085422014-04-15 16:18:08 -0700494 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700495 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700496 const mat4* transformFromProjectionSurface) {
497 mProjectedNodes.clear();
498 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
499
500 // TODO: should avoid this calculation in most cases
501 // TODO: just calculate single matrix, down to all leaf composited elements
502 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
503 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
504
John Reckd0a0b2a2014-03-20 16:28:56 -0700505 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700506 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
507 opState->mSkipInOrderDraw = true;
508 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
509 compositedChildrenOfProjectionSurface->add(opState);
510 } else {
511 // standard in order draw
512 opState->mSkipInOrderDraw = false;
513 }
514
John Reck087bc0c2014-04-04 16:20:08 -0700515 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700516 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
517 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700518 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700519 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
520 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700521
Chris Craik3f085422014-04-15 16:18:08 -0700522 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700523 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700524 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700525 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700526 // if receiving projections, collect projecting descendent
527
528 // Note that if a direct descendent is projecting backwards, we pass it's
529 // grandparent projection collection, since it shouldn't project onto it's
530 // parent, where it will already be drawing.
Chris Craik3f085422014-04-15 16:18:08 -0700531 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700532 projectionChildren = &mProjectedNodes;
533 projectionTransform = &mat4::identity();
534 } else {
535 if (!haveAppliedPropertiesToProjection) {
536 applyViewPropertyTransforms(localTransformFromProjectionSurface);
537 haveAppliedPropertiesToProjection = true;
538 }
Chris Craik3f085422014-04-15 16:18:08 -0700539 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700540 projectionChildren = compositedChildrenOfProjectionSurface;
541 projectionTransform = &localTransformFromProjectionSurface;
542 }
Chris Craik3f085422014-04-15 16:18:08 -0700543 child->computeOrderingImpl(childOp,
544 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700545 }
546 }
John Reck113e0822014-03-18 09:22:59 -0700547}
548
549class DeferOperationHandler {
550public:
551 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
552 : mDeferStruct(deferStruct), mLevel(level) {}
553 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
554 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
555 }
556 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700557 inline void startMark(const char* name) {} // do nothing
558 inline void endMark() {}
559 inline int level() { return mLevel; }
560 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700561 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700562
563private:
564 DeferStateStruct& mDeferStruct;
565 const int mLevel;
566};
567
Chris Craik80d49022014-06-20 15:03:43 -0700568void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700569 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700570 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700571}
572
573class ReplayOperationHandler {
574public:
575 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
576 : mReplayStruct(replayStruct), mLevel(level) {}
577 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
578#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f085422014-04-15 16:18:08 -0700579 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700580#endif
581 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
582 }
583 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700584 inline void startMark(const char* name) {
585 mReplayStruct.mRenderer.startMark(name);
586 }
587 inline void endMark() {
588 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700589 }
590 inline int level() { return mLevel; }
591 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700592 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700593
594private:
595 ReplayStateStruct& mReplayStruct;
596 const int mLevel;
597};
598
Chris Craik80d49022014-06-20 15:03:43 -0700599void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700600 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700601 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700602}
603
Chris Craik8afd0f22014-08-21 17:41:57 -0700604void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
605 Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
606 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700607
Chris Craik8afd0f22014-08-21 17:41:57 -0700608 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700609 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
610 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700611 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700612
Chris Craik8afd0f22014-08-21 17:41:57 -0700613 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
Chris Craika7090e02014-06-20 16:01:00 -0700614 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700615 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700616 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700617 // regular, in order drawing DisplayList
618 childOp->mSkipInOrderDraw = false;
619 }
620 }
621
Chris Craik8afd0f22014-08-21 17:41:57 -0700622 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700623 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
624}
625
Chris Craikb265e2c2014-03-27 15:50:09 -0700626template <class T>
627void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700628 if (properties().getAlpha() <= 0.0f
629 || properties().getOutline().getAlpha() <= 0.0f
630 || !properties().getOutline().getPath()) {
631 // no shadow to draw
632 return;
633 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700634
635 mat4 shadowMatrixXY(transformFromParent);
636 applyViewPropertyTransforms(shadowMatrixXY);
637
638 // Z matrix needs actual 3d transformation, so mapped z values will be correct
639 mat4 shadowMatrixZ(transformFromParent);
640 applyViewPropertyTransforms(shadowMatrixZ, true);
641
Chris Craik74669862014-08-07 17:27:30 -0700642 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700643 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700644 if (revealClipPath && revealClipPath->isEmpty()) return;
645
Chris Craik77b5cad2014-07-30 18:23:07 -0700646 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700647
648 const SkPath* outlinePath = casterOutlinePath;
649 if (revealClipPath) {
650 // if we can't simply use the caster's path directly, create a temporary one
651 SkPath* frameAllocatedPath = handler.allocPathForFrame();
652
653 // intersect the outline with the convex reveal clip
654 Op(*casterOutlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
655 outlinePath = frameAllocatedPath;
656 }
657
Chris Craikb265e2c2014-03-27 15:50:09 -0700658 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700659 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700660 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
661}
662
John Reck113e0822014-03-18 09:22:59 -0700663#define SHADOW_DELTA 0.1f
664
665template <class T>
Chris Craikc3e75f92014-08-27 15:34:52 -0700666void RenderNode::issueOperationsOf3dChildren(ChildrenSelectMode mode,
667 const Matrix4& initialTransform, const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
668 OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700669 const int size = zTranslatedNodes.size();
670 if (size == 0
671 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
672 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
673 // no 3d children to draw
674 return;
675 }
676
Chris Craikc3e75f92014-08-27 15:34:52 -0700677 // Apply the base transform of the parent of the 3d children. This isolates
678 // 3d children of the current chunk from transformations made in previous chunks.
679 int rootRestoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
680 renderer.setMatrix(initialTransform);
681
John Reck113e0822014-03-18 09:22:59 -0700682 /**
683 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
684 * with very similar Z heights to draw together.
685 *
686 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
687 * underneath both, and neither's shadow is drawn on top of the other.
688 */
689 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
690 size_t drawIndex, shadowIndex, endIndex;
691 if (mode == kNegativeZChildren) {
692 drawIndex = 0;
693 endIndex = nonNegativeIndex;
694 shadowIndex = endIndex; // draw no shadows
695 } else {
696 drawIndex = nonNegativeIndex;
697 endIndex = size;
698 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
699 }
Chris Craik3f085422014-04-15 16:18:08 -0700700
701 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
702 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
703
John Reck113e0822014-03-18 09:22:59 -0700704 float lastCasterZ = 0.0f;
705 while (shadowIndex < endIndex || drawIndex < endIndex) {
706 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700707 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
708 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700709 const float casterZ = zTranslatedNodes[shadowIndex].key;
710 // attempt to render the shadow if the caster about to be drawn is its caster,
711 // OR if its caster's Z value is similar to the previous potential caster
712 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700713 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700714
715 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
716 shadowIndex++;
717 continue;
718 }
719 }
720
721 // only the actual child DL draw needs to be in save/restore,
722 // since it modifies the renderer's matrix
723 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
724
Chris Craika7090e02014-06-20 16:01:00 -0700725 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
726 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700727
728 renderer.concatMatrix(childOp->mTransformFromParent);
729 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700730 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700731 childOp->mSkipInOrderDraw = true;
732
733 renderer.restoreToCount(restoreTo);
734 drawIndex++;
735 }
Chris Craikc3e75f92014-08-27 15:34:52 -0700736 renderer.restoreToCount(rootRestoreTo);
John Reck113e0822014-03-18 09:22:59 -0700737}
738
739template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700740void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f085422014-04-15 16:18:08 -0700741 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
742 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f085422014-04-15 16:18:08 -0700743 int restoreTo = renderer.getSaveCount();
744
Chris Craikb3cca872014-08-08 18:42:51 -0700745 LinearAllocator& alloc = handler.allocator();
746 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
747 PROPERTY_SAVECOUNT, properties().getClipToBounds());
748
749 // Transform renderer to match background we're projecting onto
750 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
751 const DisplayListOp* op =
752 (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
753 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
754 const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
755 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
756
Chris Craik3f085422014-04-15 16:18:08 -0700757 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
758 // Either with clipRect, or special saveLayer masking
Chris Craik3f085422014-04-15 16:18:08 -0700759 if (projectionReceiverOutline != NULL) {
760 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
761 if (projectionReceiverOutline->isRect(NULL)) {
762 // mask to the rect outline simply with clipRect
Chris Craik3f085422014-04-15 16:18:08 -0700763 ClipRectOp* clipOp = new (alloc) ClipRectOp(
764 outlineBounds.left(), outlineBounds.top(),
765 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
766 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
767 } else {
768 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
769 SaveLayerOp* op = new (alloc) SaveLayerOp(
770 outlineBounds.left(), outlineBounds.top(),
771 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700772 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f085422014-04-15 16:18:08 -0700773 op->setMask(projectionReceiverOutline);
774 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
775
776 /* TODO: add optimizations here to take advantage of placement/size of projected
777 * children (which may shrink saveLayer area significantly). This is dependent on
778 * passing actual drawing/dirtying bounds of projected content down to native.
779 */
780 }
781 }
782
783 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700784 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700785 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700786
787 // matrix save, concat, and restore can be done safely without allocating operations
788 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
789 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
790 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700791 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700792 childOp->mSkipInOrderDraw = true;
793 renderer.restoreToCount(restoreTo);
794 }
Chris Craik3f085422014-04-15 16:18:08 -0700795
796 if (projectionReceiverOutline != NULL) {
797 handler(new (alloc) RestoreToCountOp(restoreTo),
798 PROPERTY_SAVECOUNT, properties().getClipToBounds());
799 }
John Reck113e0822014-03-18 09:22:59 -0700800}
801
802/**
803 * This function serves both defer and replay modes, and will organize the displayList's component
804 * operations for a single frame:
805 *
806 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
807 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
808 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
809 * defer vs replay logic, per operation
810 */
811template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700812void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700813 const int level = handler.level();
814 if (mDisplayListData->isEmpty()) {
815 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
816 return;
817 }
818
John Reck25fbb3f2014-06-12 13:46:45 -0700819 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
820 // If we are updating the contents of mLayer, we don't want to apply any of
821 // the RenderNode's properties to this issueOperations pass. Those will all
822 // be applied when the layer is drawn, aka when this is true.
823 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700824 if (useViewProperties) {
825 const Outline& outline = properties().getOutline();
826 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
827 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
828 return;
829 }
John Reck113e0822014-03-18 09:22:59 -0700830 }
831
Chris Craik3f085422014-04-15 16:18:08 -0700832 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700833
John Reck113e0822014-03-18 09:22:59 -0700834#if DEBUG_DISPLAY_LIST
Chris Craik3f085422014-04-15 16:18:08 -0700835 const Rect& clipRect = renderer.getLocalClipBounds();
836 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
837 level * 2, "", this, getName(),
838 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700839#endif
840
841 LinearAllocator& alloc = handler.allocator();
842 int restoreTo = renderer.getSaveCount();
843 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700844 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700845
846 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
847 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
848
John Reck25fbb3f2014-06-12 13:46:45 -0700849 if (useViewProperties) {
850 setViewProperties<T>(renderer, handler);
851 }
John Reck113e0822014-03-18 09:22:59 -0700852
Chris Craik8c271ca2014-03-25 10:33:01 -0700853 bool quickRejected = properties().getClipToBounds()
854 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700855 if (!quickRejected) {
Chris Craikc3e75f92014-08-27 15:34:52 -0700856 Matrix4 initialTransform(*(renderer.currentTransform()));
857
John Reck25fbb3f2014-06-12 13:46:45 -0700858 if (drawLayer) {
859 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
860 renderer.getSaveCount() - 1, properties().getClipToBounds());
861 } else {
John Reck25fbb3f2014-06-12 13:46:45 -0700862 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
Chris Craik8afd0f22014-08-21 17:41:57 -0700863 for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
864 const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -0700865
Chris Craik8afd0f22014-08-21 17:41:57 -0700866 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
867 buildZSortedChildList(chunk, zTranslatedNodes);
868
Chris Craikc3e75f92014-08-27 15:34:52 -0700869 issueOperationsOf3dChildren(kNegativeZChildren,
870 initialTransform, zTranslatedNodes, renderer, handler);
871
Chris Craik8afd0f22014-08-21 17:41:57 -0700872 const int saveCountOffset = renderer.getSaveCount() - 1;
873 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
874
875 for (int opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
876 DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -0700877#if DEBUG_DISPLAY_LIST
Chris Craik8afd0f22014-08-21 17:41:57 -0700878 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700879#endif
Chris Craik8afd0f22014-08-21 17:41:57 -0700880 logBuffer.writeCommand(level, op->name());
881 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700882
Chris Craik8afd0f22014-08-21 17:41:57 -0700883 if (CC_UNLIKELY(!mProjectedNodes.isEmpty() && opIndex == projectionReceiveIndex)) {
884 issueOperationsOfProjectedChildren(renderer, handler);
885 }
John Reck25fbb3f2014-06-12 13:46:45 -0700886 }
John Reck113e0822014-03-18 09:22:59 -0700887
Chris Craikc3e75f92014-08-27 15:34:52 -0700888 issueOperationsOf3dChildren(kPositiveZChildren,
889 initialTransform, zTranslatedNodes, renderer, handler);
Chris Craik8afd0f22014-08-21 17:41:57 -0700890 }
John Reck25fbb3f2014-06-12 13:46:45 -0700891 }
John Reck113e0822014-03-18 09:22:59 -0700892 }
893
894 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
895 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700896 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700897 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700898
Chris Craik3f085422014-04-15 16:18:08 -0700899 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700900 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700901}
902
903} /* namespace uirenderer */
904} /* namespace android */