blob: dca7520b78e829ce77be8fdc6321294efbd67c70 [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) {
98 for (unsigned int i = 0; i < mDisplayListData->displayListOps.size(); i++) {
99 mDisplayListData->displayListOps[i]->output(level, flags);
100 }
John Reck113e0822014-03-18 09:22:59 -0700101 }
102
Chris Craik3f0854292014-04-15 16:18:08 -0700103 ALOGD("%*sDone (%p, %s)", (level - 1) * 2, "", this, getName());
John Reck113e0822014-03-18 09:22:59 -0700104}
105
John Reckfe5e7b72014-05-23 17:42:28 -0700106int RenderNode::getDebugSize() {
107 int size = sizeof(RenderNode);
108 if (mStagingDisplayListData) {
109 size += mStagingDisplayListData->allocator.usedSize();
110 }
111 if (mDisplayListData && mDisplayListData != mStagingDisplayListData) {
112 size += mDisplayListData->allocator.usedSize();
113 }
114 return size;
115}
116
John Reckf4198b72014-04-09 17:00:04 -0700117void RenderNode::prepareTree(TreeInfo& info) {
118 ATRACE_CALL();
Chris Craik69e5adf2014-08-14 13:34:01 -0700119 LOG_ALWAYS_FATAL_IF(!info.damageAccumulator, "DamageAccumulator missing");
John Reckf4198b72014-04-09 17:00:04 -0700120
121 prepareTreeImpl(info);
122}
123
John Reck68bfe0a2014-06-24 15:34:58 -0700124void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
125 mAnimatorManager.addAnimator(animator);
126}
127
John Recke4267ea2014-06-03 15:53:15 -0700128void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700129 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700130 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700131 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
132 } else {
133 // Hope this is big enough?
134 // TODO: Get this from the display list ops or something
135 info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
136 }
John Recke4267ea2014-06-03 15:53:15 -0700137 }
138}
139
John Recka7c2ea22014-08-08 13:21:00 -0700140void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
John Reck25fbb3f2014-06-12 13:46:45 -0700141 LayerType layerType = properties().layerProperties().type();
142 if (CC_UNLIKELY(layerType == kLayerTypeRenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700143 // Damage applied so far needs to affect our parent, but does not require
144 // the layer to be updated. So we pop/push here to clear out the current
145 // damage and get a clean state for display list or children updates to
146 // affect, which will require the layer to be updated
147 info.damageAccumulator->popTransform();
148 info.damageAccumulator->pushTransform(this);
149 if (dirtyMask & DISPLAY_LIST) {
150 damageSelf(info);
151 }
John Reck25fbb3f2014-06-12 13:46:45 -0700152 }
153}
154
155void RenderNode::pushLayerUpdate(TreeInfo& info) {
156 LayerType layerType = properties().layerProperties().type();
157 // If we are not a layer OR we cannot be rendered (eg, view was detached)
158 // we need to destroy any Layers we may have had previously
159 if (CC_LIKELY(layerType != kLayerTypeRenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700160 if (CC_UNLIKELY(mLayer)) {
161 LayerRenderer::destroyLayer(mLayer);
162 mLayer = NULL;
163 }
164 return;
165 }
166
Chris Craik69e5adf2014-08-14 13:34:01 -0700167 bool transformUpdateNeeded = false;
John Reck25fbb3f2014-06-12 13:46:45 -0700168 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700169 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700170 applyLayerPropertiesToLayer(info);
171 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700172 transformUpdateNeeded = true;
John Reck25fbb3f2014-06-12 13:46:45 -0700173 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700174 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
175 LayerRenderer::destroyLayer(mLayer);
176 mLayer = 0;
177 }
John Reck25fbb3f2014-06-12 13:46:45 -0700178 damageSelf(info);
Chris Craik69e5adf2014-08-14 13:34:01 -0700179 transformUpdateNeeded = true;
180 }
181
John Reck25fbb3f2014-06-12 13:46:45 -0700182 SkRect dirty;
183 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700184
John Reckc25e5062014-06-18 14:21:29 -0700185 if (!mLayer) {
186 if (info.errorHandler) {
187 std::string msg = "Unable to create layer for ";
188 msg += getName();
189 info.errorHandler->onError(msg);
190 }
191 return;
192 }
193
Chris Craikc71bfca2014-08-21 10:18:58 -0700194 if (transformUpdateNeeded) {
195 // update the transform in window of the layer to reset its origin wrt light source position
196 Matrix4 windowTransform;
197 info.damageAccumulator->computeCurrentTransform(&windowTransform);
198 mLayer->setWindowTransform(windowTransform);
199 }
John Reckc79eabc2014-08-05 11:03:42 -0700200
201 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
202 dirty.roundOut();
John Reck25fbb3f2014-06-12 13:46:45 -0700203 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
204 }
205 // This is not inside the above if because we may have called
206 // updateDeferred on a previous prepare pass that didn't have a renderer
207 if (info.renderer && mLayer->deferredUpdateScheduled) {
208 info.renderer->pushLayerUpdate(mLayer);
209 }
210}
211
John Recke4267ea2014-06-03 15:53:15 -0700212void RenderNode::prepareTreeImpl(TreeInfo& info) {
John Recka447d292014-06-11 18:39:44 -0700213 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700214
John Reckdcba6722014-07-08 13:59:49 -0700215 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700216 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700217 }
John Recka7c2ea22014-08-08 13:21:00 -0700218 uint32_t animatorDirtyMask = mAnimatorManager.animate(info);
219 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700220 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700221 pushStagingDisplayListChanges(info);
222 }
John Reckf4198b72014-04-09 17:00:04 -0700223 prepareSubTree(info, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700224 pushLayerUpdate(info);
225
John Recka447d292014-06-11 18:39:44 -0700226 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700227}
228
John Reck25fbb3f2014-06-12 13:46:45 -0700229void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700230 // Push the animators first so that setupStartValueIfNecessary() is called
231 // before properties() is trampled by stagingProperties(), as they are
232 // required by some animators.
John Reck68bfe0a2014-06-24 15:34:58 -0700233 mAnimatorManager.pushStaging(info);
John Reckff941dc2014-05-14 16:34:14 -0700234 if (mDirtyPropertyFields) {
235 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700236 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700237 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700238 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700239 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700240 // We could try to be clever and only re-damage if the matrix changed.
241 // However, we don't need to worry about that. The cost of over-damaging
242 // here is only going to be a single additional map rect of this node
243 // plus a rect join(). The parent's transform (and up) will only be
244 // performed once.
John Recka447d292014-06-11 18:39:44 -0700245 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700246 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700247 }
John Reck25fbb3f2014-06-12 13:46:45 -0700248}
249
250void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
251 if (CC_LIKELY(!mLayer)) return;
252
253 const LayerProperties& props = properties().layerProperties();
254 mLayer->setAlpha(props.alpha(), props.xferMode());
255 mLayer->setColorFilter(props.colorFilter());
256 mLayer->setBlend(props.needsBlending());
257}
258
259void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700260 if (mNeedsDisplayListDataSync) {
261 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700262 // Make sure we inc first so that we don't fluctuate between 0 and 1,
263 // which would thrash the layer cache
264 if (mStagingDisplayListData) {
265 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
266 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
267 }
268 }
269 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -0700270 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700271 mStagingDisplayListData = NULL;
John Reck09d5cdd2014-07-24 10:36:08 -0700272 if (mDisplayListData) {
273 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
274 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
275 }
276 }
John Recke4267ea2014-06-03 15:53:15 -0700277 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700278 }
John Reck8de65a82014-04-09 15:23:38 -0700279}
280
John Reckdcba6722014-07-08 13:59:49 -0700281void RenderNode::deleteDisplayListData() {
282 if (mDisplayListData) {
283 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
284 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
285 }
286 }
287 delete mDisplayListData;
288 mDisplayListData = NULL;
289}
290
John Reckf4198b72014-04-09 17:00:04 -0700291void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700292 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700293 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700294 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700295 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
296 // and thus falling out of async drawing path.
297 if (subtree->ownedBitmapResources.size()) {
298 info.prepareTextures = false;
299 }
300 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
301 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700302 }
John Reck8de65a82014-04-09 15:23:38 -0700303 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700304 DrawRenderNodeOp* op = subtree->children()[i];
305 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700306 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700307 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700308 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700309 }
John Reck113e0822014-03-18 09:22:59 -0700310 }
311}
312
John Reckdcba6722014-07-08 13:59:49 -0700313void RenderNode::destroyHardwareResources() {
314 if (mLayer) {
315 LayerRenderer::destroyLayer(mLayer);
316 mLayer = NULL;
317 }
318 if (mDisplayListData) {
319 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
320 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
321 }
322 if (mNeedsDisplayListDataSync) {
323 // Next prepare tree we are going to push a new display list, so we can
324 // drop our current one now
325 deleteDisplayListData();
326 }
327 }
328}
329
330void RenderNode::decParentRefCount() {
331 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
332 mParentCount--;
333 if (!mParentCount) {
334 // If a child of ours is being attached to our parent then this will incorrectly
335 // destroy its hardware resources. However, this situation is highly unlikely
336 // and the failure is "just" that the layer is re-created, so this should
337 // be safe enough
338 destroyHardwareResources();
339 }
340}
341
John Reck113e0822014-03-18 09:22:59 -0700342/*
343 * For property operations, we pass a savecount of 0, since the operations aren't part of the
344 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700345 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700346 */
347#define PROPERTY_SAVECOUNT 0
348
349template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700350void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700351#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700352 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700353#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700354 if (properties().getLeft() != 0 || properties().getTop() != 0) {
355 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700356 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700357 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500358 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700359 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500360 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700361 }
John Reckf7483e32014-04-11 08:54:47 -0700362 if (properties().hasTransformMatrix()) {
363 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700364 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700365 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700366 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700367 }
368 }
John Reck25fbb3f2014-06-12 13:46:45 -0700369 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700370 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700371 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700372 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700373 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
374
John Reckd0a0b2a2014-03-20 16:28:56 -0700375 renderer.setOverrideLayerAlpha(properties().getAlpha());
376 } else if (!properties().getHasOverlappingRendering()) {
377 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700378 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700379 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700380 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700381 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700382 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700383 properties().getClippingRectForFlags(clipFlags, &layerBounds);
384 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700385 }
386
387 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700388 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700389 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700390 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700391 }
392 }
Chris Craika753f4c2014-07-24 12:39:17 -0700393 if (clipFlags) {
394 Rect clipRect;
395 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700396 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700397 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
398 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700399 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700400 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700401
Chris Craikaf4d04c2014-07-29 12:50:14 -0700402 // TODO: support both reveal clip and outline clip simultaneously
403 if (mProperties.getRevealClip().willClip()) {
404 Rect bounds;
405 mProperties.getRevealClip().getBounds(&bounds);
406 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
407 } else if (mProperties.getOutline().willClip()) {
408 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700409 }
Chris Craikaf4d04c2014-07-29 12:50:14 -0700410
John Reck113e0822014-03-18 09:22:59 -0700411}
412
413/**
414 * Apply property-based transformations to input matrix
415 *
416 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
417 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
418 */
Chris Craik69e5adf2014-08-14 13:34:01 -0700419void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) const {
John Reckd0a0b2a2014-03-20 16:28:56 -0700420 if (properties().getLeft() != 0 || properties().getTop() != 0) {
421 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700422 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700423 if (properties().getStaticMatrix()) {
424 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700425 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700426 } else if (properties().getAnimationMatrix()) {
427 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700428 matrix.multiply(anim);
429 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700430
Chris Craikcc39e162014-04-25 18:34:11 -0700431 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700432 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700433 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700434 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700435 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700436 } else {
437 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700438 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700439 } else {
440 mat4 true3dMat;
441 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700442 properties().getPivotX() + properties().getTranslationX(),
443 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700444 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700445 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
446 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
447 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
448 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
449 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700450
451 matrix.multiply(true3dMat);
452 }
453 }
454 }
455}
456
457/**
458 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
459 *
460 * This should be called before a call to defer() or drawDisplayList()
461 *
462 * Each DisplayList that serves as a 3d root builds its list of composited children,
463 * which are flagged to not draw in the standard draw loop.
464 */
465void RenderNode::computeOrdering() {
466 ATRACE_CALL();
467 mProjectedNodes.clear();
468
469 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
470 // transform properties are applied correctly to top level children
471 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700472 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700473 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
474 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700475 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700476 }
477}
478
479void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700480 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700481 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700482 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700483 const mat4* transformFromProjectionSurface) {
484 mProjectedNodes.clear();
485 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
486
487 // TODO: should avoid this calculation in most cases
488 // TODO: just calculate single matrix, down to all leaf composited elements
489 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
490 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
491
John Reckd0a0b2a2014-03-20 16:28:56 -0700492 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700493 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
494 opState->mSkipInOrderDraw = true;
495 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
496 compositedChildrenOfProjectionSurface->add(opState);
497 } else {
498 // standard in order draw
499 opState->mSkipInOrderDraw = false;
500 }
501
John Reck087bc0c2014-04-04 16:20:08 -0700502 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700503 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
504 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700505 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700506 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
507 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700508
Chris Craik3f0854292014-04-15 16:18:08 -0700509 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700510 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700511 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700512 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700513 // if receiving projections, collect projecting descendent
514
515 // Note that if a direct descendent is projecting backwards, we pass it's
516 // grandparent projection collection, since it shouldn't project onto it's
517 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700518 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700519 projectionChildren = &mProjectedNodes;
520 projectionTransform = &mat4::identity();
521 } else {
522 if (!haveAppliedPropertiesToProjection) {
523 applyViewPropertyTransforms(localTransformFromProjectionSurface);
524 haveAppliedPropertiesToProjection = true;
525 }
Chris Craik3f0854292014-04-15 16:18:08 -0700526 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700527 projectionChildren = compositedChildrenOfProjectionSurface;
528 projectionTransform = &localTransformFromProjectionSurface;
529 }
Chris Craik3f0854292014-04-15 16:18:08 -0700530 child->computeOrderingImpl(childOp,
531 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700532 }
533 }
John Reck113e0822014-03-18 09:22:59 -0700534}
535
536class DeferOperationHandler {
537public:
538 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
539 : mDeferStruct(deferStruct), mLevel(level) {}
540 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
541 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
542 }
543 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700544 inline void startMark(const char* name) {} // do nothing
545 inline void endMark() {}
546 inline int level() { return mLevel; }
547 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700548 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700549
550private:
551 DeferStateStruct& mDeferStruct;
552 const int mLevel;
553};
554
Chris Craik80d49022014-06-20 15:03:43 -0700555void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700556 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700557 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700558}
559
560class ReplayOperationHandler {
561public:
562 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
563 : mReplayStruct(replayStruct), mLevel(level) {}
564 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
565#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700566 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700567#endif
568 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
569 }
570 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700571 inline void startMark(const char* name) {
572 mReplayStruct.mRenderer.startMark(name);
573 }
574 inline void endMark() {
575 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700576 }
577 inline int level() { return mLevel; }
578 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700579 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700580
581private:
582 ReplayStateStruct& mReplayStruct;
583 const int mLevel;
584};
585
Chris Craik80d49022014-06-20 15:03:43 -0700586void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700587 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700588 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700589}
590
Chris Craika7090e02014-06-20 16:01:00 -0700591void RenderNode::buildZSortedChildList(Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700592 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700593
John Reck087bc0c2014-04-04 16:20:08 -0700594 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700595 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
596 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700597 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700598
Chris Craike0bb87d2014-04-22 17:55:41 -0700599 if (!MathUtils::isZero(childZ)) {
Chris Craika7090e02014-06-20 16:01:00 -0700600 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700601 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700602 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700603 // regular, in order drawing DisplayList
604 childOp->mSkipInOrderDraw = false;
605 }
606 }
607
608 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
609 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
610}
611
Chris Craikb265e2c2014-03-27 15:50:09 -0700612template <class T>
613void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700614 if (properties().getAlpha() <= 0.0f
615 || properties().getOutline().getAlpha() <= 0.0f
616 || !properties().getOutline().getPath()) {
617 // no shadow to draw
618 return;
619 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700620
621 mat4 shadowMatrixXY(transformFromParent);
622 applyViewPropertyTransforms(shadowMatrixXY);
623
624 // Z matrix needs actual 3d transformation, so mapped z values will be correct
625 mat4 shadowMatrixZ(transformFromParent);
626 applyViewPropertyTransforms(shadowMatrixZ, true);
627
Chris Craik74669862014-08-07 17:27:30 -0700628 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700629 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700630 if (revealClipPath && revealClipPath->isEmpty()) return;
631
Chris Craik77b5cad2014-07-30 18:23:07 -0700632 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700633
634 const SkPath* outlinePath = casterOutlinePath;
635 if (revealClipPath) {
636 // if we can't simply use the caster's path directly, create a temporary one
637 SkPath* frameAllocatedPath = handler.allocPathForFrame();
638
639 // intersect the outline with the convex reveal clip
640 Op(*casterOutlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
641 outlinePath = frameAllocatedPath;
642 }
643
Chris Craikb265e2c2014-03-27 15:50:09 -0700644 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700645 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700646 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
647}
648
Chris Craik80d49022014-06-20 15:03:43 -0700649template <class T>
650int RenderNode::issueOperationsOfNegZChildren(
Chris Craika7090e02014-06-20 16:01:00 -0700651 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700652 OpenGLRenderer& renderer, T& handler) {
653 if (zTranslatedNodes.isEmpty()) return -1;
654
655 // create a save around the body of the ViewGroup's draw method, so that
656 // matrix/clip methods don't affect composited children
657 int shadowSaveCount = renderer.getSaveCount();
658 handler(new (handler.allocator()) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
659 PROPERTY_SAVECOUNT, properties().getClipToBounds());
660
661 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
662 return shadowSaveCount;
663}
664
665template <class T>
666void RenderNode::issueOperationsOfPosZChildren(int shadowRestoreTo,
Chris Craika7090e02014-06-20 16:01:00 -0700667 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700668 OpenGLRenderer& renderer, T& handler) {
669 if (zTranslatedNodes.isEmpty()) return;
670
671 LOG_ALWAYS_FATAL_IF(shadowRestoreTo < 0, "invalid save to restore to");
672 handler(new (handler.allocator()) RestoreToCountOp(shadowRestoreTo),
673 PROPERTY_SAVECOUNT, properties().getClipToBounds());
674 renderer.setOverrideLayerAlpha(1.0f);
675
676 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
677}
678
John Reck113e0822014-03-18 09:22:59 -0700679#define SHADOW_DELTA 0.1f
680
681template <class T>
Chris Craika7090e02014-06-20 16:01:00 -0700682void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700683 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
684 const int size = zTranslatedNodes.size();
685 if (size == 0
686 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
687 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
688 // no 3d children to draw
689 return;
690 }
691
John Reck113e0822014-03-18 09:22:59 -0700692 /**
693 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
694 * with very similar Z heights to draw together.
695 *
696 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
697 * underneath both, and neither's shadow is drawn on top of the other.
698 */
699 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
700 size_t drawIndex, shadowIndex, endIndex;
701 if (mode == kNegativeZChildren) {
702 drawIndex = 0;
703 endIndex = nonNegativeIndex;
704 shadowIndex = endIndex; // draw no shadows
705 } else {
706 drawIndex = nonNegativeIndex;
707 endIndex = size;
708 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
709 }
Chris Craik3f0854292014-04-15 16:18:08 -0700710
711 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
712 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
713
John Reck113e0822014-03-18 09:22:59 -0700714 float lastCasterZ = 0.0f;
715 while (shadowIndex < endIndex || drawIndex < endIndex) {
716 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700717 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
718 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700719 const float casterZ = zTranslatedNodes[shadowIndex].key;
720 // attempt to render the shadow if the caster about to be drawn is its caster,
721 // OR if its caster's Z value is similar to the previous potential caster
722 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700723 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700724
725 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
726 shadowIndex++;
727 continue;
728 }
729 }
730
731 // only the actual child DL draw needs to be in save/restore,
732 // since it modifies the renderer's matrix
733 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
734
Chris Craika7090e02014-06-20 16:01:00 -0700735 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
736 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700737
738 renderer.concatMatrix(childOp->mTransformFromParent);
739 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700740 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700741 childOp->mSkipInOrderDraw = true;
742
743 renderer.restoreToCount(restoreTo);
744 drawIndex++;
745 }
John Reck113e0822014-03-18 09:22:59 -0700746}
747
748template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700749void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700750 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
751 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700752 int restoreTo = renderer.getSaveCount();
753
Chris Craikb3cca872014-08-08 18:42:51 -0700754 LinearAllocator& alloc = handler.allocator();
755 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
756 PROPERTY_SAVECOUNT, properties().getClipToBounds());
757
758 // Transform renderer to match background we're projecting onto
759 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
760 const DisplayListOp* op =
761 (mDisplayListData->displayListOps[mDisplayListData->projectionReceiveIndex]);
762 const DrawRenderNodeOp* backgroundOp = reinterpret_cast<const DrawRenderNodeOp*>(op);
763 const RenderProperties& backgroundProps = backgroundOp->mRenderNode->properties();
764 renderer.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
765
Chris Craik3f0854292014-04-15 16:18:08 -0700766 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
767 // Either with clipRect, or special saveLayer masking
Chris Craik3f0854292014-04-15 16:18:08 -0700768 if (projectionReceiverOutline != NULL) {
769 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
770 if (projectionReceiverOutline->isRect(NULL)) {
771 // mask to the rect outline simply with clipRect
Chris Craik3f0854292014-04-15 16:18:08 -0700772 ClipRectOp* clipOp = new (alloc) ClipRectOp(
773 outlineBounds.left(), outlineBounds.top(),
774 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
775 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
776 } else {
777 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
778 SaveLayerOp* op = new (alloc) SaveLayerOp(
779 outlineBounds.left(), outlineBounds.top(),
780 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700781 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700782 op->setMask(projectionReceiverOutline);
783 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
784
785 /* TODO: add optimizations here to take advantage of placement/size of projected
786 * children (which may shrink saveLayer area significantly). This is dependent on
787 * passing actual drawing/dirtying bounds of projected content down to native.
788 */
789 }
790 }
791
792 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700793 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700794 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700795
796 // matrix save, concat, and restore can be done safely without allocating operations
797 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
798 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
799 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700800 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700801 childOp->mSkipInOrderDraw = true;
802 renderer.restoreToCount(restoreTo);
803 }
Chris Craik3f0854292014-04-15 16:18:08 -0700804
805 if (projectionReceiverOutline != NULL) {
806 handler(new (alloc) RestoreToCountOp(restoreTo),
807 PROPERTY_SAVECOUNT, properties().getClipToBounds());
808 }
John Reck113e0822014-03-18 09:22:59 -0700809}
810
811/**
812 * This function serves both defer and replay modes, and will organize the displayList's component
813 * operations for a single frame:
814 *
815 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
816 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
817 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
818 * defer vs replay logic, per operation
819 */
820template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700821void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700822 const int level = handler.level();
823 if (mDisplayListData->isEmpty()) {
824 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
825 return;
826 }
827
John Reck25fbb3f2014-06-12 13:46:45 -0700828 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
829 // If we are updating the contents of mLayer, we don't want to apply any of
830 // the RenderNode's properties to this issueOperations pass. Those will all
831 // be applied when the layer is drawn, aka when this is true.
832 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700833 if (useViewProperties) {
834 const Outline& outline = properties().getOutline();
835 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
836 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
837 return;
838 }
John Reck113e0822014-03-18 09:22:59 -0700839 }
840
Chris Craik3f0854292014-04-15 16:18:08 -0700841 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700842
John Reck113e0822014-03-18 09:22:59 -0700843#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700844 const Rect& clipRect = renderer.getLocalClipBounds();
845 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
846 level * 2, "", this, getName(),
847 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700848#endif
849
850 LinearAllocator& alloc = handler.allocator();
851 int restoreTo = renderer.getSaveCount();
852 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700853 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700854
855 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
856 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
857
John Reck25fbb3f2014-06-12 13:46:45 -0700858 if (useViewProperties) {
859 setViewProperties<T>(renderer, handler);
860 }
John Reck113e0822014-03-18 09:22:59 -0700861
Chris Craik8c271ca2014-03-25 10:33:01 -0700862 bool quickRejected = properties().getClipToBounds()
863 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700864 if (!quickRejected) {
John Reck25fbb3f2014-06-12 13:46:45 -0700865 if (drawLayer) {
866 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
867 renderer.getSaveCount() - 1, properties().getClipToBounds());
868 } else {
Chris Craika7090e02014-06-20 16:01:00 -0700869 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
John Reck25fbb3f2014-06-12 13:46:45 -0700870 buildZSortedChildList(zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700871
John Reck25fbb3f2014-06-12 13:46:45 -0700872 // for 3d root, draw children with negative z values
Chris Craik80d49022014-06-20 15:03:43 -0700873 int shadowRestoreTo = issueOperationsOfNegZChildren(zTranslatedNodes, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700874
John Reck25fbb3f2014-06-12 13:46:45 -0700875 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
876 const int saveCountOffset = renderer.getSaveCount() - 1;
877 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
John Reck1aa5d2d2014-07-24 13:38:28 -0700878 const int size = static_cast<int>(mDisplayListData->displayListOps.size());
879 for (int i = 0; i < size; i++) {
John Reck25fbb3f2014-06-12 13:46:45 -0700880 DisplayListOp *op = mDisplayListData->displayListOps[i];
John Reck113e0822014-03-18 09:22:59 -0700881
Chris Craik80d49022014-06-20 15:03:43 -0700882#if DEBUG_DISPLAY_LIST
John Reck25fbb3f2014-06-12 13:46:45 -0700883 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700884#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700885 logBuffer.writeCommand(level, op->name());
886 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700887
John Reck25fbb3f2014-06-12 13:46:45 -0700888 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
889 issueOperationsOfProjectedChildren(renderer, handler);
890 }
John Reck113e0822014-03-18 09:22:59 -0700891 }
John Reck113e0822014-03-18 09:22:59 -0700892
John Reck25fbb3f2014-06-12 13:46:45 -0700893 // for 3d root, draw children with positive z values
Chris Craik80d49022014-06-20 15:03:43 -0700894 issueOperationsOfPosZChildren(shadowRestoreTo, zTranslatedNodes, renderer, handler);
John Reck25fbb3f2014-06-12 13:46:45 -0700895 }
John Reck113e0822014-03-18 09:22:59 -0700896 }
897
898 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
899 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700900 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700901 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700902
Chris Craik3f0854292014-04-15 16:18:08 -0700903 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700904 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700905}
906
907} /* namespace uirenderer */
908} /* namespace android */