blob: 658265d8335ea021aae45772936820ac24f84a88 [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 Reck113e0822014-03-18 09:22:59 -070037
38namespace android {
39namespace uirenderer {
40
41void RenderNode::outputLogBuffer(int fd) {
42 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
43 if (logBuffer.isEmpty()) {
44 return;
45 }
46
47 FILE *file = fdopen(fd, "a");
48
49 fprintf(file, "\nRecent DisplayList operations\n");
50 logBuffer.outputCommands(file);
51
52 String8 cachesLog;
53 Caches::getInstance().dumpMemoryUsage(cachesLog);
54 fprintf(file, "\nCaches:\n%s", cachesLog.string());
55 fprintf(file, "\n");
56
57 fflush(file);
58}
59
John Reck8de65a82014-04-09 15:23:38 -070060RenderNode::RenderNode()
John Reckff941dc2014-05-14 16:34:14 -070061 : mDirtyPropertyFields(0)
John Reck8de65a82014-04-09 15:23:38 -070062 , mNeedsDisplayListDataSync(false)
63 , mDisplayListData(0)
John Recke45b1fd2014-04-15 09:50:16 -070064 , mStagingDisplayListData(0)
John Reck68bfe0a2014-06-24 15:34:58 -070065 , mAnimatorManager(*this)
John Reckdcba6722014-07-08 13:59:49 -070066 , mLayer(0)
67 , mParentCount(0) {
John Reck113e0822014-03-18 09:22:59 -070068}
69
70RenderNode::~RenderNode() {
John Reckdcba6722014-07-08 13:59:49 -070071 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -070072 delete mStagingDisplayListData;
John Reck25fbb3f2014-06-12 13:46:45 -070073 LayerRenderer::destroyLayerDeferred(mLayer);
John Reck113e0822014-03-18 09:22:59 -070074}
75
John Reck8de65a82014-04-09 15:23:38 -070076void RenderNode::setStagingDisplayList(DisplayListData* data) {
77 mNeedsDisplayListDataSync = true;
78 delete mStagingDisplayListData;
79 mStagingDisplayListData = data;
80 if (mStagingDisplayListData) {
John Reck09d5cdd2014-07-24 10:36:08 -070081 Caches::getInstance().registerFunctors(mStagingDisplayListData->functors.size());
John Reck113e0822014-03-18 09:22:59 -070082 }
83}
84
85/**
86 * This function is a simplified version of replay(), where we simply retrieve and log the
87 * display list. This function should remain in sync with the replay() function.
88 */
89void RenderNode::output(uint32_t level) {
90 ALOGD("%*sStart display list (%p, %s, render=%d)", (level - 1) * 2, "", this,
Chris Craik3f0854292014-04-15 16:18:08 -070091 getName(), isRenderable());
John Reck113e0822014-03-18 09:22:59 -070092 ALOGD("%*s%s %d", level * 2, "", "Save",
93 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
94
John Reckd0a0b2a2014-03-20 16:28:56 -070095 properties().debugOutputProperties(level);
John Reck113e0822014-03-18 09:22:59 -070096 int flags = DisplayListOp::kOpLogFlag_Recurse;
John Reckdc0349b2014-08-06 15:28:07 -070097 if (mDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -070098 // TODO: consider printing the chunk boundaries here
John Reckdc0349b2014-08-06 15:28:07 -070099 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
100 mDisplayListData->displayListOps[i]->output(level, flags);
101 }
John Reck113e0822014-03-18 09:22:59 -0700102 }
103
Chris Craik3f0854292014-04-15 16:18:08 -0700104 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700105}
106
John Reckfe5e7b72014-05-23 17:42:28 -0700107int RenderNode::getDebugSize() {
108 int size = sizeof(RenderNode);
109 if (mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700110 size += mStagingDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700111 }
112 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
Chris Craik8afd0f22014-08-21 17:41:57 -0700113 size += mDisplayListData->getUsedSize();
John Reckfe5e7b72014-05-23 17:42:28 -0700114 }
115 return size;
116}
117
John Reckf4198b72014-04-09 17:00:04 -0700118void RenderNode::prepareTree(TreeInfo& info) {
119 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700120 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700121
122 prepareTreeImpl(info);
123}
124
John Reck68bfe0a2014-06-24 15:34:58 -0700125void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
126 mAnimatorManager.addAnimator(animator);
127}
128
John Recke4267ea2014-06-03 15:53:15 -0700129void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700130 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700131 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700132 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
133 } else {
134 // Hope this is big enough?
135 // TODO: Get this from the display list ops or something
136 info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
137 }
John Recke4267ea2014-06-03 15:53:15 -0700138 }
139}
140
John Recka7c2ea22014-08-08 13:21:00 -0700141void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
John Reck25fbb3f2014-06-12 13:46:45 -0700142 LayerType layerType = properties().layerProperties().type();
143 if (CC_UNLIKELY(layerType == kLayerTypeRenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700144 // Damage applied so far needs to affect our parent, but does not require
145 // the layer to be updated. So we pop/push here to clear out the current
146 // damage and get a clean state for display list or children updates to
147 // affect, which will require the layer to be updated
148 info.damageAccumulator->popTransform();
149 info.damageAccumulator->pushTransform(this);
150 if (dirtyMask & DISPLAY_LIST) {
151 damageSelf(info);
152 }
John Reck25fbb3f2014-06-12 13:46:45 -0700153 }
154}
155
156void RenderNode::pushLayerUpdate(TreeInfo& info) {
157 LayerType layerType = properties().layerProperties().type();
158 // If we are not a layer OR we cannot be rendered (eg, view was detached)
159 // we need to destroy any Layers we may have had previously
160 if (CC_LIKELY(layerType != kLayerTypeRenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700161 if (CC_UNLIKELY(mLayer)) {
162 LayerRenderer::destroyLayer(mLayer);
163 mLayer = NULL;
164 }
165 return;
166 }
167
Chris Craik69e5adf2014-08-14 13:34:01 -0700168 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700169 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700170 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700171 applyLayerPropertiesToLayer(info);
172 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700173 transformUpdateNeeded = true;
John Reck25fbb3f2014-06-12 13:46:45 -0700174 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700175 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
176 LayerRenderer::destroyLayer(mLayer);
177 mLayer = 0;
178 }
John Reck25fbb3f2014-06-12 13:46:45 -0700179 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700180 transformUpdateNeeded = true;
181 }
182
John Reck25fbb3f2014-06-12 13:46:45 -0700183 SkRect dirty;
184 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700185
John Reckc25e5062014-06-18 14:21:29 -0700186 if (!mLayer) {
187 if (info.errorHandler) {
188 std::string msg = "Unable to create layer for ";
189 msg += getName();
190 info.errorHandler->onError(msg);
191 }
192 return;
193 }
194
Chris Craikc71bfca2014-08-21 10:18:58 -0700195 if (transformUpdateNeeded) {
196 // update the transform in window of the layer to reset its origin wrt light source position
197 Matrix4 windowTransform;
198 info.damageAccumulator->computeCurrentTransform(&windowTransform);
199 mLayer->setWindowTransform(windowTransform);
200 }
John Reckc79eabc2014-08-05 11:03:42 -0700201
202 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
203 dirty.roundOut();
John Reck25fbb3f2014-06-12 13:46:45 -0700204 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
205 }
206 // This is not inside the above if because we may have called
207 // updateDeferred on a previous prepare pass that didn't have a renderer
208 if (info.renderer && mLayer->deferredUpdateScheduled) {
209 info.renderer->pushLayerUpdate(mLayer);
210 }
211}
212
John Recke4267ea2014-06-03 15:53:15 -0700213void RenderNode::prepareTreeImpl(TreeInfo& info) {
John Recka447d292014-06-11 18:39:44 -0700214 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700215
John Reckdcba6722014-07-08 13:59:49 -0700216 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700217 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700218 }
John Reck9eb9f6f2014-08-21 11:23:05 -0700219 uint32_t animatorDirtyMask = 0;
220 if (CC_LIKELY(info.runAnimations)) {
221 animatorDirtyMask = mAnimatorManager.animate(info);
222 }
John Recka7c2ea22014-08-08 13:21:00 -0700223 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700224 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700225 pushStagingDisplayListChanges(info);
226 }
John Reckf4198b72014-04-09 17:00:04 -0700227 prepareSubTree(info, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700228 pushLayerUpdate(info);
229
John Recka447d292014-06-11 18:39:44 -0700230 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700231}
232
John Reck25fbb3f2014-06-12 13:46:45 -0700233void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700234 // Push the animators first so that setupStartValueIfNecessary() is called
235 // before properties() is trampled by stagingProperties(), as they are
236 // required by some animators.
John Reck9eb9f6f2014-08-21 11:23:05 -0700237 if (CC_LIKELY(info.runAnimations)) {
238 mAnimatorManager.pushStaging(info);
239 }
John Reckff941dc2014-05-14 16:34:14 -0700240 if (mDirtyPropertyFields) {
241 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700242 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700243 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700244 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700245 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700246 // We could try to be clever and only re-damage if the matrix changed.
247 // However, we don't need to worry about that. The cost of over-damaging
248 // here is only going to be a single additional map rect of this node
249 // plus a rect join(). The parent's transform (and up) will only be
250 // performed once.
John Recka447d292014-06-11 18:39:44 -0700251 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700252 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700253 }
John Reck25fbb3f2014-06-12 13:46:45 -0700254}
255
256void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
257 if (CC_LIKELY(!mLayer)) return;
258
259 const LayerProperties& props = properties().layerProperties();
260 mLayer->setAlpha(props.alpha(), props.xferMode());
261 mLayer->setColorFilter(props.colorFilter());
262 mLayer->setBlend(props.needsBlending());
263}
264
265void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700266 if (mNeedsDisplayListDataSync) {
267 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700268 // Make sure we inc first so that we don't fluctuate between 0 and 1,
269 // which would thrash the layer cache
270 if (mStagingDisplayListData) {
271 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
272 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
273 }
274 }
275 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -0700276 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700277 mStagingDisplayListData = NULL;
John Reck09d5cdd2014-07-24 10:36:08 -0700278 if (mDisplayListData) {
279 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
280 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
281 }
282 }
John Recke4267ea2014-06-03 15:53:15 -0700283 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700284 }
John Reck8de65a82014-04-09 15:23:38 -0700285}
286
John Reckdcba6722014-07-08 13:59:49 -0700287void RenderNode::deleteDisplayListData() {
288 if (mDisplayListData) {
289 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
290 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
291 }
292 }
293 delete mDisplayListData;
294 mDisplayListData = NULL;
295}
296
John Reckf4198b72014-04-09 17:00:04 -0700297void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700298 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700299 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700300 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700301 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
302 // and thus falling out of async drawing path.
303 if (subtree->ownedBitmapResources.size()) {
304 info.prepareTextures = false;
305 }
306 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
307 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700308 }
John Reck8de65a82014-04-09 15:23:38 -0700309 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700310 DrawRenderNodeOp* op = subtree->children()[i];
311 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700312 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700313 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700314 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700315 }
John Reck113e0822014-03-18 09:22:59 -0700316 }
317}
318
John Reckdcba6722014-07-08 13:59:49 -0700319void RenderNode::destroyHardwareResources() {
320 if (mLayer) {
321 LayerRenderer::destroyLayer(mLayer);
322 mLayer = NULL;
323 }
324 if (mDisplayListData) {
325 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
326 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
327 }
328 if (mNeedsDisplayListDataSync) {
329 // Next prepare tree we are going to push a new display list, so we can
330 // drop our current one now
331 deleteDisplayListData();
332 }
333 }
334}
335
336void RenderNode::decParentRefCount() {
337 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
338 mParentCount--;
339 if (!mParentCount) {
340 // If a child of ours is being attached to our parent then this will incorrectly
341 // destroy its hardware resources. However, this situation is highly unlikely
342 // and the failure is "just" that the layer is re-created, so this should
343 // be safe enough
344 destroyHardwareResources();
345 }
346}
347
John Reck113e0822014-03-18 09:22:59 -0700348/*
349 * For property operations, we pass a savecount of 0, since the operations aren't part of the
350 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700351 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700352 */
353#define PROPERTY_SAVECOUNT 0
354
355template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700356void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700357#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700358 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700359#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700360 if (properties().getLeft() != 0 || properties().getTop() != 0) {
361 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700362 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700363 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500364 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700365 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500366 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700367 }
John Reckf7483e32014-04-11 08:54:47 -0700368 if (properties().hasTransformMatrix()) {
369 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700370 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700371 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700372 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700373 }
374 }
John Reck25fbb3f2014-06-12 13:46:45 -0700375 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700376 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700377 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700378 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700379 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
380
John Reckd0a0b2a2014-03-20 16:28:56 -0700381 renderer.setOverrideLayerAlpha(properties().getAlpha());
382 } else if (!properties().getHasOverlappingRendering()) {
383 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700384 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700385 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700386 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700387 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700388 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700389 properties().getClippingRectForFlags(clipFlags, &layerBounds);
390 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700391 }
392
393 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700394 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700395 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700396 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700397 }
398 }
Chris Craika753f4c2014-07-24 12:39:17 -0700399 if (clipFlags) {
400 Rect clipRect;
401 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700402 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700403 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
404 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700405 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700406 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700407
Chris Craikaf4d04c2014-07-29 12:50:14 -0700408 // TODO: support both reveal clip and outline clip simultaneously
409 if (mProperties.getRevealClip().willClip()) {
410 Rect bounds;
411 mProperties.getRevealClip().getBounds(&bounds);
412 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
413 } else if (mProperties.getOutline().willClip()) {
414 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700415 }
Chris Craikaf4d04c2014-07-29 12:50:14 -0700416
John Reck113e0822014-03-18 09:22:59 -0700417}
418
419/**
420 * Apply property-based transformations to input matrix
421 *
422 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
423 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
424 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700425void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700426 if (properties().getLeft() != 0 || properties().getTop() != 0) {
427 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700428 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700429 if (properties().getStaticMatrix()) {
430 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700431 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700432 } else if (properties().getAnimationMatrix()) {
433 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700434 matrix.multiply(anim);
435 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700436
Chris Craikcc39e162014-04-25 18:34:11 -0700437 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700438 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700439 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700440 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700441 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700442 } else {
443 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700444 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700445 } else {
446 mat4 true3dMat;
447 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700448 properties().getPivotX() + properties().getTranslationX(),
449 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700450 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700451 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
452 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
453 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
454 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
455 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700456
457 matrix.multiply(true3dMat);
458 }
459 }
460 }
461}
462
463/**
464 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
465 *
466 * This should be called before a call to defer() or drawDisplayList()
467 *
468 * Each DisplayList that serves as a 3d root builds its list of composited children,
469 * which are flagged to not draw in the standard draw loop.
470 */
471void RenderNode::computeOrdering() {
472 ATRACE_CALL();
473 mProjectedNodes.clear();
474
475 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
476 // transform properties are applied correctly to top level children
477 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700478 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700479 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
480 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700481 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700482 }
483}
484
485void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700486 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700487 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700488 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700489 const mat4* transformFromProjectionSurface) {
490 mProjectedNodes.clear();
491 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
492
493 // TODO: should avoid this calculation in most cases
494 // TODO: just calculate single matrix, down to all leaf composited elements
495 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
496 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
497
John Reckd0a0b2a2014-03-20 16:28:56 -0700498 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700499 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
500 opState->mSkipInOrderDraw = true;
501 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
502 compositedChildrenOfProjectionSurface->add(opState);
503 } else {
504 // standard in order draw
505 opState->mSkipInOrderDraw = false;
506 }
507
John Reck087bc0c2014-04-04 16:20:08 -0700508 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700509 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
510 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700511 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700512 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
513 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700514
Chris Craik3f0854292014-04-15 16:18:08 -0700515 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700516 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700517 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700518 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700519 // if receiving projections, collect projecting descendent
520
521 // Note that if a direct descendent is projecting backwards, we pass it's
522 // grandparent projection collection, since it shouldn't project onto it's
523 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700524 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700525 projectionChildren = &mProjectedNodes;
526 projectionTransform = &mat4::identity();
527 } else {
528 if (!haveAppliedPropertiesToProjection) {
529 applyViewPropertyTransforms(localTransformFromProjectionSurface);
530 haveAppliedPropertiesToProjection = true;
531 }
Chris Craik3f0854292014-04-15 16:18:08 -0700532 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700533 projectionChildren = compositedChildrenOfProjectionSurface;
534 projectionTransform = &localTransformFromProjectionSurface;
535 }
Chris Craik3f0854292014-04-15 16:18:08 -0700536 child->computeOrderingImpl(childOp,
537 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700538 }
539 }
John Reck113e0822014-03-18 09:22:59 -0700540}
541
542class DeferOperationHandler {
543public:
544 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
545 : mDeferStruct(deferStruct), mLevel(level) {}
546 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
547 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
548 }
549 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700550 inline void startMark(const char* name) {} // do nothing
551 inline void endMark() {}
552 inline int level() { return mLevel; }
553 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700554 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700555
556private:
557 DeferStateStruct& mDeferStruct;
558 const int mLevel;
559};
560
Chris Craik80d49022014-06-20 15:03:43 -0700561void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700562 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700563 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700564}
565
566class ReplayOperationHandler {
567public:
568 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
569 : mReplayStruct(replayStruct), mLevel(level) {}
570 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
571#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700572 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700573#endif
574 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
575 }
576 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700577 inline void startMark(const char* name) {
578 mReplayStruct.mRenderer.startMark(name);
579 }
580 inline void endMark() {
581 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700582 }
583 inline int level() { return mLevel; }
584 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700585 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700586
587private:
588 ReplayStateStruct& mReplayStruct;
589 const int mLevel;
590};
591
Chris Craik80d49022014-06-20 15:03:43 -0700592void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700593 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700594 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700595}
596
Chris Craik8afd0f22014-08-21 17:41:57 -0700597void RenderNode::buildZSortedChildList(const DisplayListData::Chunk& chunk,
598 Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
599 if (chunk.beginChildIndex == chunk.endChildIndex) return;
John Reck113e0822014-03-18 09:22:59 -0700600
Chris Craik8afd0f22014-08-21 17:41:57 -0700601 for (unsigned int i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700602 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
603 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700604 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700605
Chris Craik8afd0f22014-08-21 17:41:57 -0700606 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
Chris Craika7090e02014-06-20 16:01:00 -0700607 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700608 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700609 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700610 // regular, in order drawing DisplayList
611 childOp->mSkipInOrderDraw = false;
612 }
613 }
614
Chris Craik8afd0f22014-08-21 17:41:57 -0700615 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
John Reck113e0822014-03-18 09:22:59 -0700616 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
617}
618
Chris Craikb265e2c2014-03-27 15:50:09 -0700619template <class T>
620void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700621 if (properties().getAlpha() <= 0.0f
622 || properties().getOutline().getAlpha() <= 0.0f
623 || !properties().getOutline().getPath()) {
624 // no shadow to draw
625 return;
626 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700627
628 mat4 shadowMatrixXY(transformFromParent);
629 applyViewPropertyTransforms(shadowMatrixXY);
630
631 // Z matrix needs actual 3d transformation, so mapped z values will be correct
632 mat4 shadowMatrixZ(transformFromParent);
633 applyViewPropertyTransforms(shadowMatrixZ, true);
634
Chris Craik74669862014-08-07 17:27:30 -0700635 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700636 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700637 if (revealClipPath && revealClipPath->isEmpty()) return;
638
Chris Craik77b5cad2014-07-30 18:23:07 -0700639 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700640
641 const SkPath* outlinePath = casterOutlinePath;
642 if (revealClipPath) {
643 // if we can't simply use the caster's path directly, create a temporary one
644 SkPath* frameAllocatedPath = handler.allocPathForFrame();
645
646 // intersect the outline with the convex reveal clip
647 Op(*casterOutlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
648 outlinePath = frameAllocatedPath;
649 }
650
Chris Craikb265e2c2014-03-27 15:50:09 -0700651 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700652 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700653 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
654}
655
Chris Craik80d49022014-06-20 15:03:43 -0700656template <class T>
657int RenderNode::issueOperationsOfNegZChildren(
Chris Craika7090e02014-06-20 16:01:00 -0700658 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700659 OpenGLRenderer& renderer, T& handler) {
660 if (zTranslatedNodes.isEmpty()) return -1;
661
662 // create a save around the body of the ViewGroup's draw method, so that
663 // matrix/clip methods don't affect composited children
664 int shadowSaveCount = renderer.getSaveCount();
665 handler(new (handler.allocator()) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
666 PROPERTY_SAVECOUNT, properties().getClipToBounds());
667
668 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
669 return shadowSaveCount;
670}
671
672template <class T>
673void RenderNode::issueOperationsOfPosZChildren(int shadowRestoreTo,
Chris Craika7090e02014-06-20 16:01:00 -0700674 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700675 OpenGLRenderer& renderer, T& handler) {
676 if (zTranslatedNodes.isEmpty()) return;
677
678 LOG_ALWAYS_FATAL_IF(shadowRestoreTo < 0, "invalid save to restore to");
679 handler(new (handler.allocator()) RestoreToCountOp(shadowRestoreTo),
680 PROPERTY_SAVECOUNT, properties().getClipToBounds());
681 renderer.setOverrideLayerAlpha(1.0f);
682
683 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
684}
685
John Reck113e0822014-03-18 09:22:59 -0700686#define SHADOW_DELTA 0.1f
687
688template <class T>
Chris Craika7090e02014-06-20 16:01:00 -0700689void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700690 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
691 const int size = zTranslatedNodes.size();
692 if (size == 0
693 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
694 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
695 // no 3d children to draw
696 return;
697 }
698
John Reck113e0822014-03-18 09:22:59 -0700699 /**
700 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
701 * with very similar Z heights to draw together.
702 *
703 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
704 * underneath both, and neither's shadow is drawn on top of the other.
705 */
706 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
707 size_t drawIndex, shadowIndex, endIndex;
708 if (mode == kNegativeZChildren) {
709 drawIndex = 0;
710 endIndex = nonNegativeIndex;
711 shadowIndex = endIndex; // draw no shadows
712 } else {
713 drawIndex = nonNegativeIndex;
714 endIndex = size;
715 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
716 }
Chris Craik3f0854292014-04-15 16:18:08 -0700717
718 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
719 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
720
John Reck113e0822014-03-18 09:22:59 -0700721 float lastCasterZ = 0.0f;
722 while (shadowIndex < endIndex || drawIndex < endIndex) {
723 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700724 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
725 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700726 const float casterZ = zTranslatedNodes[shadowIndex].key;
727 // attempt to render the shadow if the caster about to be drawn is its caster,
728 // OR if its caster's Z value is similar to the previous potential caster
729 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700730 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700731
732 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
733 shadowIndex++;
734 continue;
735 }
736 }
737
738 // only the actual child DL draw needs to be in save/restore,
739 // since it modifies the renderer's matrix
740 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
741
Chris Craika7090e02014-06-20 16:01:00 -0700742 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
743 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700744
745 renderer.concatMatrix(childOp->mTransformFromParent);
746 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700747 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700748 childOp->mSkipInOrderDraw = true;
749
750 renderer.restoreToCount(restoreTo);
751 drawIndex++;
752 }
John Reck113e0822014-03-18 09:22:59 -0700753}
754
755template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700756void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700757 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
758 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700759 int restoreTo = renderer.getSaveCount();
760
Chris Craikb3cca872014-08-08 18:42:51 -0700761 LinearAllocator& alloc = handler.allocator();
762 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
763 PROPERTY_SAVECOUNT, properties().getClipToBounds());
764
765 // Transform renderer to match background we're projecting onto
766 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
767 const DisplayListOp* op =
768 (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
769 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
770 const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
771 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
772
Chris Craik3f0854292014-04-15 16:18:08 -0700773 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
774 // Either with clipRect, or special saveLayer masking
Chris Craik3f0854292014-04-15 16:18:08 -0700775 if (projectionReceiverOutline != NULL) {
776 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
777 if (projectionReceiverOutline->isRect(NULL)) {
778 // mask to the rect outline simply with clipRect
Chris Craik3f0854292014-04-15 16:18:08 -0700779 ClipRectOp* clipOp = new (alloc) ClipRectOp(
780 outlineBounds.left(), outlineBounds.top(),
781 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
782 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
783 } else {
784 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
785 SaveLayerOp* op = new (alloc) SaveLayerOp(
786 outlineBounds.left(), outlineBounds.top(),
787 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700788 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700789 op->setMask(projectionReceiverOutline);
790 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
791
792 /* TODO: add optimizations here to take advantage of placement/size of projected
793 * children (which may shrink saveLayer area significantly). This is dependent on
794 * passing actual drawing/dirtying bounds of projected content down to native.
795 */
796 }
797 }
798
799 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700800 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700801 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700802
803 // matrix save, concat, and restore can be done safely without allocating operations
804 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
805 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
806 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700807 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700808 childOp->mSkipInOrderDraw = true;
809 renderer.restoreToCount(restoreTo);
810 }
Chris Craik3f0854292014-04-15 16:18:08 -0700811
812 if (projectionReceiverOutline != NULL) {
813 handler(new (alloc) RestoreToCountOp(restoreTo),
814 PROPERTY_SAVECOUNT, properties().getClipToBounds());
815 }
John Reck113e0822014-03-18 09:22:59 -0700816}
817
818/**
819 * This function serves both defer and replay modes, and will organize the displayList's component
820 * operations for a single frame:
821 *
822 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
823 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
824 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
825 * defer vs replay logic, per operation
826 */
827template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700828void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700829 const int level = handler.level();
830 if (mDisplayListData->isEmpty()) {
831 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
832 return;
833 }
834
John Reck25fbb3f2014-06-12 13:46:45 -0700835 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
836 // If we are updating the contents of mLayer, we don't want to apply any of
837 // the RenderNode's properties to this issueOperations pass. Those will all
838 // be applied when the layer is drawn, aka when this is true.
839 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700840 if (useViewProperties) {
841 const Outline& outline = properties().getOutline();
842 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
843 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
844 return;
845 }
John Reck113e0822014-03-18 09:22:59 -0700846 }
847
Chris Craik3f0854292014-04-15 16:18:08 -0700848 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700849
John Reck113e0822014-03-18 09:22:59 -0700850#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700851 const Rect& clipRect = renderer.getLocalClipBounds();
852 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
853 level * 2, "", this, getName(),
854 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700855#endif
856
857 LinearAllocator& alloc = handler.allocator();
858 int restoreTo = renderer.getSaveCount();
859 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700860 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700861
862 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
863 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
864
John Reck25fbb3f2014-06-12 13:46:45 -0700865 if (useViewProperties) {
866 setViewProperties<T>(renderer, handler);
867 }
John Reck113e0822014-03-18 09:22:59 -0700868
Chris Craik8c271ca2014-03-25 10:33:01 -0700869 bool quickRejected = properties().getClipToBounds()
870 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700871 if (!quickRejected) {
John Reck25fbb3f2014-06-12 13:46:45 -0700872 if (drawLayer) {
873 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
874 renderer.getSaveCount() - 1, properties().getClipToBounds());
875 } else {
John Reck25fbb3f2014-06-12 13:46:45 -0700876 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
Chris Craik8afd0f22014-08-21 17:41:57 -0700877 for (size_t chunkIndex = 0; chunkIndex < mDisplayListData->getChunks().size(); chunkIndex++) {
878 const DisplayListData::Chunk& chunk = mDisplayListData->getChunks()[chunkIndex];
John Reck113e0822014-03-18 09:22:59 -0700879
Chris Craik8afd0f22014-08-21 17:41:57 -0700880 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
881 buildZSortedChildList(chunk, zTranslatedNodes);
882
883 // for 3d root, draw children with negative z values
884 int shadowRestoreTo = issueOperationsOfNegZChildren(zTranslatedNodes,
885 renderer, handler);
886 const int saveCountOffset = renderer.getSaveCount() - 1;
887 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
888
889 for (int opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
890 DisplayListOp *op = mDisplayListData->displayListOps[opIndex];
Chris Craik80d49022014-06-20 15:03:43 -0700891#if DEBUG_DISPLAY_LIST
Chris Craik8afd0f22014-08-21 17:41:57 -0700892 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700893#endif
Chris Craik8afd0f22014-08-21 17:41:57 -0700894 logBuffer.writeCommand(level, op->name());
895 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700896
Chris Craik8afd0f22014-08-21 17:41:57 -0700897 if (CC_UNLIKELY(!mProjectedNodes.isEmpty() && opIndex == projectionReceiveIndex)) {
898 issueOperationsOfProjectedChildren(renderer, handler);
899 }
John Reck25fbb3f2014-06-12 13:46:45 -0700900 }
John Reck113e0822014-03-18 09:22:59 -0700901
Chris Craik8afd0f22014-08-21 17:41:57 -0700902 // for 3d root, draw children with positive z values
903 issueOperationsOfPosZChildren(shadowRestoreTo, zTranslatedNodes, renderer, handler);
904 }
John Reck25fbb3f2014-06-12 13:46:45 -0700905 }
John Reck113e0822014-03-18 09:22:59 -0700906 }
907
908 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
909 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700910 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700911 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700912
Chris Craik3f0854292014-04-15 16:18:08 -0700913 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700914 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700915}
916
917} /* namespace uirenderer */
918} /* namespace android */