blob: c59b010818c32da795880ce3b98e4283772c6a7d [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();
119
120 prepareTreeImpl(info);
121}
122
John Reck68bfe0a2014-06-24 15:34:58 -0700123void RenderNode::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
124 mAnimatorManager.addAnimator(animator);
125}
126
John Recke4267ea2014-06-03 15:53:15 -0700127void RenderNode::damageSelf(TreeInfo& info) {
John Reckce9f3082014-06-17 16:18:09 -0700128 if (isRenderable()) {
John Reck293e8682014-06-17 10:34:02 -0700129 if (properties().getClipDamageToBounds()) {
John Recka447d292014-06-11 18:39:44 -0700130 info.damageAccumulator->dirty(0, 0, properties().getWidth(), properties().getHeight());
131 } else {
132 // Hope this is big enough?
133 // TODO: Get this from the display list ops or something
134 info.damageAccumulator->dirty(INT_MIN, INT_MIN, INT_MAX, INT_MAX);
135 }
John Recke4267ea2014-06-03 15:53:15 -0700136 }
137}
138
John Recka7c2ea22014-08-08 13:21:00 -0700139void RenderNode::prepareLayer(TreeInfo& info, uint32_t dirtyMask) {
John Reck25fbb3f2014-06-12 13:46:45 -0700140 LayerType layerType = properties().layerProperties().type();
141 if (CC_UNLIKELY(layerType == kLayerTypeRenderLayer)) {
John Recka7c2ea22014-08-08 13:21:00 -0700142 // Damage applied so far needs to affect our parent, but does not require
143 // the layer to be updated. So we pop/push here to clear out the current
144 // damage and get a clean state for display list or children updates to
145 // affect, which will require the layer to be updated
146 info.damageAccumulator->popTransform();
147 info.damageAccumulator->pushTransform(this);
148 if (dirtyMask & DISPLAY_LIST) {
149 damageSelf(info);
150 }
John Reck25fbb3f2014-06-12 13:46:45 -0700151 }
152}
153
154void RenderNode::pushLayerUpdate(TreeInfo& info) {
155 LayerType layerType = properties().layerProperties().type();
156 // If we are not a layer OR we cannot be rendered (eg, view was detached)
157 // we need to destroy any Layers we may have had previously
158 if (CC_LIKELY(layerType != kLayerTypeRenderLayer) || CC_UNLIKELY(!isRenderable())) {
John Reck25fbb3f2014-06-12 13:46:45 -0700159 if (CC_UNLIKELY(mLayer)) {
160 LayerRenderer::destroyLayer(mLayer);
161 mLayer = NULL;
162 }
163 return;
164 }
165
166 if (!mLayer) {
John Reck3b202512014-06-23 13:13:08 -0700167 mLayer = LayerRenderer::createRenderLayer(info.renderState, getWidth(), getHeight());
John Reck25fbb3f2014-06-12 13:46:45 -0700168 applyLayerPropertiesToLayer(info);
169 damageSelf(info);
170 } else if (mLayer->layer.getWidth() != getWidth() || mLayer->layer.getHeight() != getHeight()) {
John Reckc25e5062014-06-18 14:21:29 -0700171 if (!LayerRenderer::resizeLayer(mLayer, getWidth(), getHeight())) {
172 LayerRenderer::destroyLayer(mLayer);
173 mLayer = 0;
174 }
John Reck25fbb3f2014-06-12 13:46:45 -0700175 damageSelf(info);
176 }
177
178 SkRect dirty;
179 info.damageAccumulator->peekAtDirty(&dirty);
John Reck25fbb3f2014-06-12 13:46:45 -0700180
John Reckc25e5062014-06-18 14:21:29 -0700181 if (!mLayer) {
182 if (info.errorHandler) {
183 std::string msg = "Unable to create layer for ";
184 msg += getName();
185 info.errorHandler->onError(msg);
186 }
187 return;
188 }
189
John Reckc79eabc2014-08-05 11:03:42 -0700190
191 if (dirty.intersect(0, 0, getWidth(), getHeight())) {
192 dirty.roundOut();
John Reck25fbb3f2014-06-12 13:46:45 -0700193 mLayer->updateDeferred(this, dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom);
194 }
195 // This is not inside the above if because we may have called
196 // updateDeferred on a previous prepare pass that didn't have a renderer
197 if (info.renderer && mLayer->deferredUpdateScheduled) {
198 info.renderer->pushLayerUpdate(mLayer);
199 }
200}
201
John Recke4267ea2014-06-03 15:53:15 -0700202void RenderNode::prepareTreeImpl(TreeInfo& info) {
John Recka447d292014-06-11 18:39:44 -0700203 info.damageAccumulator->pushTransform(this);
John Reckf47a5942014-06-30 16:20:04 -0700204
John Reckdcba6722014-07-08 13:59:49 -0700205 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700206 pushStagingPropertiesChanges(info);
John Recke45b1fd2014-04-15 09:50:16 -0700207 }
John Recka7c2ea22014-08-08 13:21:00 -0700208 uint32_t animatorDirtyMask = mAnimatorManager.animate(info);
209 prepareLayer(info, animatorDirtyMask);
John Reckdcba6722014-07-08 13:59:49 -0700210 if (info.mode == TreeInfo::MODE_FULL) {
John Reck25fbb3f2014-06-12 13:46:45 -0700211 pushStagingDisplayListChanges(info);
212 }
John Reckf4198b72014-04-09 17:00:04 -0700213 prepareSubTree(info, mDisplayListData);
John Reck25fbb3f2014-06-12 13:46:45 -0700214 pushLayerUpdate(info);
215
John Recka447d292014-06-11 18:39:44 -0700216 info.damageAccumulator->popTransform();
John Reckf4198b72014-04-09 17:00:04 -0700217}
218
John Reck25fbb3f2014-06-12 13:46:45 -0700219void RenderNode::pushStagingPropertiesChanges(TreeInfo& info) {
John Reckff941dc2014-05-14 16:34:14 -0700220 // Push the animators first so that setupStartValueIfNecessary() is called
221 // before properties() is trampled by stagingProperties(), as they are
222 // required by some animators.
John Reck68bfe0a2014-06-24 15:34:58 -0700223 mAnimatorManager.pushStaging(info);
John Reckff941dc2014-05-14 16:34:14 -0700224 if (mDirtyPropertyFields) {
225 mDirtyPropertyFields = 0;
John Recke4267ea2014-06-03 15:53:15 -0700226 damageSelf(info);
John Recka447d292014-06-11 18:39:44 -0700227 info.damageAccumulator->popTransform();
John Reckff941dc2014-05-14 16:34:14 -0700228 mProperties = mStagingProperties;
John Reck25fbb3f2014-06-12 13:46:45 -0700229 applyLayerPropertiesToLayer(info);
John Recke4267ea2014-06-03 15:53:15 -0700230 // We could try to be clever and only re-damage if the matrix changed.
231 // However, we don't need to worry about that. The cost of over-damaging
232 // here is only going to be a single additional map rect of this node
233 // plus a rect join(). The parent's transform (and up) will only be
234 // performed once.
John Recka447d292014-06-11 18:39:44 -0700235 info.damageAccumulator->pushTransform(this);
John Recke4267ea2014-06-03 15:53:15 -0700236 damageSelf(info);
John Reckff941dc2014-05-14 16:34:14 -0700237 }
John Reck25fbb3f2014-06-12 13:46:45 -0700238}
239
240void RenderNode::applyLayerPropertiesToLayer(TreeInfo& info) {
241 if (CC_LIKELY(!mLayer)) return;
242
243 const LayerProperties& props = properties().layerProperties();
244 mLayer->setAlpha(props.alpha(), props.xferMode());
245 mLayer->setColorFilter(props.colorFilter());
246 mLayer->setBlend(props.needsBlending());
247}
248
249void RenderNode::pushStagingDisplayListChanges(TreeInfo& info) {
John Reck8de65a82014-04-09 15:23:38 -0700250 if (mNeedsDisplayListDataSync) {
251 mNeedsDisplayListDataSync = false;
John Reckdcba6722014-07-08 13:59:49 -0700252 // Make sure we inc first so that we don't fluctuate between 0 and 1,
253 // which would thrash the layer cache
254 if (mStagingDisplayListData) {
255 for (size_t i = 0; i < mStagingDisplayListData->children().size(); i++) {
256 mStagingDisplayListData->children()[i]->mRenderNode->incParentRefCount();
257 }
258 }
259 deleteDisplayListData();
John Reck8de65a82014-04-09 15:23:38 -0700260 mDisplayListData = mStagingDisplayListData;
John Reckdcba6722014-07-08 13:59:49 -0700261 mStagingDisplayListData = NULL;
John Reck09d5cdd2014-07-24 10:36:08 -0700262 if (mDisplayListData) {
263 for (size_t i = 0; i < mDisplayListData->functors.size(); i++) {
264 (*mDisplayListData->functors[i])(DrawGlInfo::kModeSync, NULL);
265 }
266 }
John Recke4267ea2014-06-03 15:53:15 -0700267 damageSelf(info);
John Reck8de65a82014-04-09 15:23:38 -0700268 }
John Reck8de65a82014-04-09 15:23:38 -0700269}
270
John Reckdcba6722014-07-08 13:59:49 -0700271void RenderNode::deleteDisplayListData() {
272 if (mDisplayListData) {
273 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
274 mDisplayListData->children()[i]->mRenderNode->decParentRefCount();
275 }
276 }
277 delete mDisplayListData;
278 mDisplayListData = NULL;
279}
280
John Reckf4198b72014-04-09 17:00:04 -0700281void RenderNode::prepareSubTree(TreeInfo& info, DisplayListData* subtree) {
John Reck8de65a82014-04-09 15:23:38 -0700282 if (subtree) {
John Reck860d1552014-04-11 19:15:05 -0700283 TextureCache& cache = Caches::getInstance().textureCache;
John Reck09d5cdd2014-07-24 10:36:08 -0700284 info.out.hasFunctors |= subtree->functors.size();
John Reck860d1552014-04-11 19:15:05 -0700285 // TODO: Fix ownedBitmapResources to not require disabling prepareTextures
286 // and thus falling out of async drawing path.
287 if (subtree->ownedBitmapResources.size()) {
288 info.prepareTextures = false;
289 }
290 for (size_t i = 0; info.prepareTextures && i < subtree->bitmapResources.size(); i++) {
291 info.prepareTextures = cache.prefetchAndMarkInUse(subtree->bitmapResources[i]);
John Reckf4198b72014-04-09 17:00:04 -0700292 }
John Reck8de65a82014-04-09 15:23:38 -0700293 for (size_t i = 0; i < subtree->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700294 DrawRenderNodeOp* op = subtree->children()[i];
295 RenderNode* childNode = op->mRenderNode;
John Recka447d292014-06-11 18:39:44 -0700296 info.damageAccumulator->pushTransform(&op->mTransformFromParent);
John Reckf4198b72014-04-09 17:00:04 -0700297 childNode->prepareTreeImpl(info);
John Recka447d292014-06-11 18:39:44 -0700298 info.damageAccumulator->popTransform();
John Reck5bf11bb2014-03-25 10:22:09 -0700299 }
John Reck113e0822014-03-18 09:22:59 -0700300 }
301}
302
John Reckdcba6722014-07-08 13:59:49 -0700303void RenderNode::destroyHardwareResources() {
304 if (mLayer) {
305 LayerRenderer::destroyLayer(mLayer);
306 mLayer = NULL;
307 }
308 if (mDisplayListData) {
309 for (size_t i = 0; i < mDisplayListData->children().size(); i++) {
310 mDisplayListData->children()[i]->mRenderNode->destroyHardwareResources();
311 }
312 if (mNeedsDisplayListDataSync) {
313 // Next prepare tree we are going to push a new display list, so we can
314 // drop our current one now
315 deleteDisplayListData();
316 }
317 }
318}
319
320void RenderNode::decParentRefCount() {
321 LOG_ALWAYS_FATAL_IF(!mParentCount, "already 0!");
322 mParentCount--;
323 if (!mParentCount) {
324 // If a child of ours is being attached to our parent then this will incorrectly
325 // destroy its hardware resources. However, this situation is highly unlikely
326 // and the failure is "just" that the layer is re-created, so this should
327 // be safe enough
328 destroyHardwareResources();
329 }
330}
331
John Reck113e0822014-03-18 09:22:59 -0700332/*
333 * For property operations, we pass a savecount of 0, since the operations aren't part of the
334 * displaylist, and thus don't have to compensate for the record-time/playback-time discrepancy in
John Reckd0a0b2a2014-03-20 16:28:56 -0700335 * base saveCount (i.e., how RestoreToCount uses saveCount + properties().getCount())
John Reck113e0822014-03-18 09:22:59 -0700336 */
337#define PROPERTY_SAVECOUNT 0
338
339template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700340void RenderNode::setViewProperties(OpenGLRenderer& renderer, T& handler) {
John Reck113e0822014-03-18 09:22:59 -0700341#if DEBUG_DISPLAY_LIST
Chris Craikb265e2c2014-03-27 15:50:09 -0700342 properties().debugOutputProperties(handler.level() + 1);
John Reck113e0822014-03-18 09:22:59 -0700343#endif
John Reckd0a0b2a2014-03-20 16:28:56 -0700344 if (properties().getLeft() != 0 || properties().getTop() != 0) {
345 renderer.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700346 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700347 if (properties().getStaticMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500348 renderer.concatMatrix(*properties().getStaticMatrix());
John Reckd0a0b2a2014-03-20 16:28:56 -0700349 } else if (properties().getAnimationMatrix()) {
Derek Sollenberger13908822013-12-10 12:28:58 -0500350 renderer.concatMatrix(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700351 }
John Reckf7483e32014-04-11 08:54:47 -0700352 if (properties().hasTransformMatrix()) {
353 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700354 renderer.translate(properties().getTranslationX(), properties().getTranslationY());
John Reck113e0822014-03-18 09:22:59 -0700355 } else {
John Reckd0a0b2a2014-03-20 16:28:56 -0700356 renderer.concatMatrix(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700357 }
358 }
John Reck25fbb3f2014-06-12 13:46:45 -0700359 const bool isLayer = properties().layerProperties().type() != kLayerTypeNone;
Chris Craika753f4c2014-07-24 12:39:17 -0700360 int clipFlags = properties().getClippingFlags();
John Reckd0a0b2a2014-03-20 16:28:56 -0700361 if (properties().getAlpha() < 1) {
John Reck25fbb3f2014-06-12 13:46:45 -0700362 if (isLayer) {
Chris Craika753f4c2014-07-24 12:39:17 -0700363 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
364
John Reckd0a0b2a2014-03-20 16:28:56 -0700365 renderer.setOverrideLayerAlpha(properties().getAlpha());
366 } else if (!properties().getHasOverlappingRendering()) {
367 renderer.scaleAlpha(properties().getAlpha());
John Reck113e0822014-03-18 09:22:59 -0700368 } else {
Chris Craika753f4c2014-07-24 12:39:17 -0700369 Rect layerBounds(0, 0, getWidth(), getHeight());
John Reck113e0822014-03-18 09:22:59 -0700370 int saveFlags = SkCanvas::kHasAlphaLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700371 if (clipFlags) {
John Reck113e0822014-03-18 09:22:59 -0700372 saveFlags |= SkCanvas::kClipToLayer_SaveFlag;
Chris Craika753f4c2014-07-24 12:39:17 -0700373 properties().getClippingRectForFlags(clipFlags, &layerBounds);
374 clipFlags = 0; // all clipping done by saveLayer
John Reck113e0822014-03-18 09:22:59 -0700375 }
376
377 SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700378 layerBounds.left, layerBounds.top, layerBounds.right, layerBounds.bottom,
Chris Craik8c271ca2014-03-25 10:33:01 -0700379 properties().getAlpha() * 255, saveFlags);
John Reckd0a0b2a2014-03-20 16:28:56 -0700380 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700381 }
382 }
Chris Craika753f4c2014-07-24 12:39:17 -0700383 if (clipFlags) {
384 Rect clipRect;
385 properties().getClippingRectForFlags(clipFlags, &clipRect);
Chris Craik8c271ca2014-03-25 10:33:01 -0700386 ClipRectOp* op = new (handler.allocator()) ClipRectOp(
Chris Craika753f4c2014-07-24 12:39:17 -0700387 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
388 SkRegion::kIntersect_Op);
John Reckd0a0b2a2014-03-20 16:28:56 -0700389 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700390 }
Chris Craik8c271ca2014-03-25 10:33:01 -0700391
Chris Craikaf4d04c2014-07-29 12:50:14 -0700392 // TODO: support both reveal clip and outline clip simultaneously
393 if (mProperties.getRevealClip().willClip()) {
394 Rect bounds;
395 mProperties.getRevealClip().getBounds(&bounds);
396 renderer.setClippingRoundRect(handler.allocator(), bounds, mProperties.getRevealClip().getRadius());
397 } else if (mProperties.getOutline().willClip()) {
398 renderer.setClippingOutline(handler.allocator(), &(mProperties.getOutline()));
John Reck113e0822014-03-18 09:22:59 -0700399 }
Chris Craikaf4d04c2014-07-29 12:50:14 -0700400
John Reck113e0822014-03-18 09:22:59 -0700401}
402
403/**
404 * Apply property-based transformations to input matrix
405 *
406 * If true3dTransform is set to true, the transform applied to the input matrix will use true 4x4
407 * matrix computation instead of the Skia 3x3 matrix + camera hackery.
408 */
409void RenderNode::applyViewPropertyTransforms(mat4& matrix, bool true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700410 if (properties().getLeft() != 0 || properties().getTop() != 0) {
411 matrix.translate(properties().getLeft(), properties().getTop());
John Reck113e0822014-03-18 09:22:59 -0700412 }
John Reckd0a0b2a2014-03-20 16:28:56 -0700413 if (properties().getStaticMatrix()) {
414 mat4 stat(*properties().getStaticMatrix());
John Reck113e0822014-03-18 09:22:59 -0700415 matrix.multiply(stat);
John Reckd0a0b2a2014-03-20 16:28:56 -0700416 } else if (properties().getAnimationMatrix()) {
417 mat4 anim(*properties().getAnimationMatrix());
John Reck113e0822014-03-18 09:22:59 -0700418 matrix.multiply(anim);
419 }
Chris Craike0bb87d2014-04-22 17:55:41 -0700420
Chris Craikcc39e162014-04-25 18:34:11 -0700421 bool applyTranslationZ = true3dTransform && !MathUtils::isZero(properties().getZ());
Chris Craike0bb87d2014-04-22 17:55:41 -0700422 if (properties().hasTransformMatrix() || applyTranslationZ) {
John Reckf7483e32014-04-11 08:54:47 -0700423 if (properties().isTransformTranslateOnly()) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700424 matrix.translate(properties().getTranslationX(), properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700425 true3dTransform ? properties().getZ() : 0.0f);
John Reck113e0822014-03-18 09:22:59 -0700426 } else {
427 if (!true3dTransform) {
John Reckd0a0b2a2014-03-20 16:28:56 -0700428 matrix.multiply(*properties().getTransformMatrix());
John Reck113e0822014-03-18 09:22:59 -0700429 } else {
430 mat4 true3dMat;
431 true3dMat.loadTranslate(
John Reckd0a0b2a2014-03-20 16:28:56 -0700432 properties().getPivotX() + properties().getTranslationX(),
433 properties().getPivotY() + properties().getTranslationY(),
Chris Craikcc39e162014-04-25 18:34:11 -0700434 properties().getZ());
John Reckd0a0b2a2014-03-20 16:28:56 -0700435 true3dMat.rotate(properties().getRotationX(), 1, 0, 0);
436 true3dMat.rotate(properties().getRotationY(), 0, 1, 0);
437 true3dMat.rotate(properties().getRotation(), 0, 0, 1);
438 true3dMat.scale(properties().getScaleX(), properties().getScaleY(), 1);
439 true3dMat.translate(-properties().getPivotX(), -properties().getPivotY());
John Reck113e0822014-03-18 09:22:59 -0700440
441 matrix.multiply(true3dMat);
442 }
443 }
444 }
445}
446
447/**
448 * Organizes the DisplayList hierarchy to prepare for background projection reordering.
449 *
450 * This should be called before a call to defer() or drawDisplayList()
451 *
452 * Each DisplayList that serves as a 3d root builds its list of composited children,
453 * which are flagged to not draw in the standard draw loop.
454 */
455void RenderNode::computeOrdering() {
456 ATRACE_CALL();
457 mProjectedNodes.clear();
458
459 // TODO: create temporary DDLOp and call computeOrderingImpl on top DisplayList so that
460 // transform properties are applied correctly to top level children
461 if (mDisplayListData == NULL) return;
John Reck087bc0c2014-04-04 16:20:08 -0700462 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700463 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
464 childOp->mRenderNode->computeOrderingImpl(childOp,
Chris Craik3f0854292014-04-15 16:18:08 -0700465 properties().getOutline().getPath(), &mProjectedNodes, &mat4::identity());
John Reck113e0822014-03-18 09:22:59 -0700466 }
467}
468
469void RenderNode::computeOrderingImpl(
Chris Craika7090e02014-06-20 16:01:00 -0700470 DrawRenderNodeOp* opState,
Chris Craik3f0854292014-04-15 16:18:08 -0700471 const SkPath* outlineOfProjectionSurface,
Chris Craika7090e02014-06-20 16:01:00 -0700472 Vector<DrawRenderNodeOp*>* compositedChildrenOfProjectionSurface,
John Reck113e0822014-03-18 09:22:59 -0700473 const mat4* transformFromProjectionSurface) {
474 mProjectedNodes.clear();
475 if (mDisplayListData == NULL || mDisplayListData->isEmpty()) return;
476
477 // TODO: should avoid this calculation in most cases
478 // TODO: just calculate single matrix, down to all leaf composited elements
479 Matrix4 localTransformFromProjectionSurface(*transformFromProjectionSurface);
480 localTransformFromProjectionSurface.multiply(opState->mTransformFromParent);
481
John Reckd0a0b2a2014-03-20 16:28:56 -0700482 if (properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700483 // composited projectee, flag for out of order draw, save matrix, and store in proj surface
484 opState->mSkipInOrderDraw = true;
485 opState->mTransformFromCompositingAncestor.load(localTransformFromProjectionSurface);
486 compositedChildrenOfProjectionSurface->add(opState);
487 } else {
488 // standard in order draw
489 opState->mSkipInOrderDraw = false;
490 }
491
John Reck087bc0c2014-04-04 16:20:08 -0700492 if (mDisplayListData->children().size() > 0) {
John Reck113e0822014-03-18 09:22:59 -0700493 const bool isProjectionReceiver = mDisplayListData->projectionReceiveIndex >= 0;
494 bool haveAppliedPropertiesToProjection = false;
John Reck087bc0c2014-04-04 16:20:08 -0700495 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700496 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
497 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700498
Chris Craik3f0854292014-04-15 16:18:08 -0700499 const SkPath* projectionOutline = NULL;
Chris Craika7090e02014-06-20 16:01:00 -0700500 Vector<DrawRenderNodeOp*>* projectionChildren = NULL;
John Reck113e0822014-03-18 09:22:59 -0700501 const mat4* projectionTransform = NULL;
John Reckd0a0b2a2014-03-20 16:28:56 -0700502 if (isProjectionReceiver && !child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700503 // if receiving projections, collect projecting descendent
504
505 // Note that if a direct descendent is projecting backwards, we pass it's
506 // grandparent projection collection, since it shouldn't project onto it's
507 // parent, where it will already be drawing.
Chris Craik3f0854292014-04-15 16:18:08 -0700508 projectionOutline = properties().getOutline().getPath();
John Reck113e0822014-03-18 09:22:59 -0700509 projectionChildren = &mProjectedNodes;
510 projectionTransform = &mat4::identity();
511 } else {
512 if (!haveAppliedPropertiesToProjection) {
513 applyViewPropertyTransforms(localTransformFromProjectionSurface);
514 haveAppliedPropertiesToProjection = true;
515 }
Chris Craik3f0854292014-04-15 16:18:08 -0700516 projectionOutline = outlineOfProjectionSurface;
John Reck113e0822014-03-18 09:22:59 -0700517 projectionChildren = compositedChildrenOfProjectionSurface;
518 projectionTransform = &localTransformFromProjectionSurface;
519 }
Chris Craik3f0854292014-04-15 16:18:08 -0700520 child->computeOrderingImpl(childOp,
521 projectionOutline, projectionChildren, projectionTransform);
John Reck113e0822014-03-18 09:22:59 -0700522 }
523 }
John Reck113e0822014-03-18 09:22:59 -0700524}
525
526class DeferOperationHandler {
527public:
528 DeferOperationHandler(DeferStateStruct& deferStruct, int level)
529 : mDeferStruct(deferStruct), mLevel(level) {}
530 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
531 operation->defer(mDeferStruct, saveCount, mLevel, clipToBounds);
532 }
533 inline LinearAllocator& allocator() { return *(mDeferStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700534 inline void startMark(const char* name) {} // do nothing
535 inline void endMark() {}
536 inline int level() { return mLevel; }
537 inline int replayFlags() { return mDeferStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700538 inline SkPath* allocPathForFrame() { return mDeferStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700539
540private:
541 DeferStateStruct& mDeferStruct;
542 const int mLevel;
543};
544
Chris Craik80d49022014-06-20 15:03:43 -0700545void RenderNode::defer(DeferStateStruct& deferStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700546 DeferOperationHandler handler(deferStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700547 issueOperations<DeferOperationHandler>(deferStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700548}
549
550class ReplayOperationHandler {
551public:
552 ReplayOperationHandler(ReplayStateStruct& replayStruct, int level)
553 : mReplayStruct(replayStruct), mLevel(level) {}
554 inline void operator()(DisplayListOp* operation, int saveCount, bool clipToBounds) {
555#if DEBUG_DISPLAY_LIST_OPS_AS_EVENTS
Chris Craik3f0854292014-04-15 16:18:08 -0700556 mReplayStruct.mRenderer.eventMark(operation->name());
John Reck113e0822014-03-18 09:22:59 -0700557#endif
558 operation->replay(mReplayStruct, saveCount, mLevel, clipToBounds);
559 }
560 inline LinearAllocator& allocator() { return *(mReplayStruct.mAllocator); }
Chris Craikb265e2c2014-03-27 15:50:09 -0700561 inline void startMark(const char* name) {
562 mReplayStruct.mRenderer.startMark(name);
563 }
564 inline void endMark() {
565 mReplayStruct.mRenderer.endMark();
Chris Craikb265e2c2014-03-27 15:50:09 -0700566 }
567 inline int level() { return mLevel; }
568 inline int replayFlags() { return mReplayStruct.mReplayFlags; }
Chris Craik74669862014-08-07 17:27:30 -0700569 inline SkPath* allocPathForFrame() { return mReplayStruct.allocPathForFrame(); }
John Reck113e0822014-03-18 09:22:59 -0700570
571private:
572 ReplayStateStruct& mReplayStruct;
573 const int mLevel;
574};
575
Chris Craik80d49022014-06-20 15:03:43 -0700576void RenderNode::replay(ReplayStateStruct& replayStruct, const int level) {
John Reck113e0822014-03-18 09:22:59 -0700577 ReplayOperationHandler handler(replayStruct, level);
Chris Craikb265e2c2014-03-27 15:50:09 -0700578 issueOperations<ReplayOperationHandler>(replayStruct.mRenderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700579}
580
Chris Craika7090e02014-06-20 16:01:00 -0700581void RenderNode::buildZSortedChildList(Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes) {
John Reck087bc0c2014-04-04 16:20:08 -0700582 if (mDisplayListData == NULL || mDisplayListData->children().size() == 0) return;
John Reck113e0822014-03-18 09:22:59 -0700583
John Reck087bc0c2014-04-04 16:20:08 -0700584 for (unsigned int i = 0; i < mDisplayListData->children().size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700585 DrawRenderNodeOp* childOp = mDisplayListData->children()[i];
586 RenderNode* child = childOp->mRenderNode;
Chris Craikcc39e162014-04-25 18:34:11 -0700587 float childZ = child->properties().getZ();
John Reck113e0822014-03-18 09:22:59 -0700588
Chris Craike0bb87d2014-04-22 17:55:41 -0700589 if (!MathUtils::isZero(childZ)) {
Chris Craika7090e02014-06-20 16:01:00 -0700590 zTranslatedNodes.add(ZDrawRenderNodeOpPair(childZ, childOp));
John Reck113e0822014-03-18 09:22:59 -0700591 childOp->mSkipInOrderDraw = true;
John Reckd0a0b2a2014-03-20 16:28:56 -0700592 } else if (!child->properties().getProjectBackwards()) {
John Reck113e0822014-03-18 09:22:59 -0700593 // regular, in order drawing DisplayList
594 childOp->mSkipInOrderDraw = false;
595 }
596 }
597
598 // Z sort 3d children (stable-ness makes z compare fall back to standard drawing order)
599 std::stable_sort(zTranslatedNodes.begin(), zTranslatedNodes.end());
600}
601
Chris Craikb265e2c2014-03-27 15:50:09 -0700602template <class T>
603void RenderNode::issueDrawShadowOperation(const Matrix4& transformFromParent, T& handler) {
Chris Craik77b5cad2014-07-30 18:23:07 -0700604 if (properties().getAlpha() <= 0.0f
605 || properties().getOutline().getAlpha() <= 0.0f
606 || !properties().getOutline().getPath()) {
607 // no shadow to draw
608 return;
609 }
Chris Craikb265e2c2014-03-27 15:50:09 -0700610
611 mat4 shadowMatrixXY(transformFromParent);
612 applyViewPropertyTransforms(shadowMatrixXY);
613
614 // Z matrix needs actual 3d transformation, so mapped z values will be correct
615 mat4 shadowMatrixZ(transformFromParent);
616 applyViewPropertyTransforms(shadowMatrixZ, true);
617
Chris Craik74669862014-08-07 17:27:30 -0700618 const SkPath* casterOutlinePath = properties().getOutline().getPath();
Chris Craikaf4d04c2014-07-29 12:50:14 -0700619 const SkPath* revealClipPath = properties().getRevealClip().getPath();
Chris Craik61317322014-05-21 13:03:52 -0700620 if (revealClipPath && revealClipPath->isEmpty()) return;
621
Chris Craik77b5cad2014-07-30 18:23:07 -0700622 float casterAlpha = properties().getAlpha() * properties().getOutline().getAlpha();
Chris Craik74669862014-08-07 17:27:30 -0700623
624 const SkPath* outlinePath = casterOutlinePath;
625 if (revealClipPath) {
626 // if we can't simply use the caster's path directly, create a temporary one
627 SkPath* frameAllocatedPath = handler.allocPathForFrame();
628
629 // intersect the outline with the convex reveal clip
630 Op(*casterOutlinePath, *revealClipPath, kIntersect_PathOp, frameAllocatedPath);
631 outlinePath = frameAllocatedPath;
632 }
633
Chris Craikb265e2c2014-03-27 15:50:09 -0700634 DisplayListOp* shadowOp = new (handler.allocator()) DrawShadowOp(
Chris Craik74669862014-08-07 17:27:30 -0700635 shadowMatrixXY, shadowMatrixZ, casterAlpha, outlinePath);
Chris Craikb265e2c2014-03-27 15:50:09 -0700636 handler(shadowOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
637}
638
Chris Craik80d49022014-06-20 15:03:43 -0700639template <class T>
640int RenderNode::issueOperationsOfNegZChildren(
Chris Craika7090e02014-06-20 16:01:00 -0700641 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700642 OpenGLRenderer& renderer, T& handler) {
643 if (zTranslatedNodes.isEmpty()) return -1;
644
645 // create a save around the body of the ViewGroup's draw method, so that
646 // matrix/clip methods don't affect composited children
647 int shadowSaveCount = renderer.getSaveCount();
648 handler(new (handler.allocator()) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
649 PROPERTY_SAVECOUNT, properties().getClipToBounds());
650
651 issueOperationsOf3dChildren(zTranslatedNodes, kNegativeZChildren, renderer, handler);
652 return shadowSaveCount;
653}
654
655template <class T>
656void RenderNode::issueOperationsOfPosZChildren(int shadowRestoreTo,
Chris Craika7090e02014-06-20 16:01:00 -0700657 const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
Chris Craik80d49022014-06-20 15:03:43 -0700658 OpenGLRenderer& renderer, T& handler) {
659 if (zTranslatedNodes.isEmpty()) return;
660
661 LOG_ALWAYS_FATAL_IF(shadowRestoreTo < 0, "invalid save to restore to");
662 handler(new (handler.allocator()) RestoreToCountOp(shadowRestoreTo),
663 PROPERTY_SAVECOUNT, properties().getClipToBounds());
664 renderer.setOverrideLayerAlpha(1.0f);
665
666 issueOperationsOf3dChildren(zTranslatedNodes, kPositiveZChildren, renderer, handler);
667}
668
John Reck113e0822014-03-18 09:22:59 -0700669#define SHADOW_DELTA 0.1f
670
671template <class T>
Chris Craika7090e02014-06-20 16:01:00 -0700672void RenderNode::issueOperationsOf3dChildren(const Vector<ZDrawRenderNodeOpPair>& zTranslatedNodes,
John Reck113e0822014-03-18 09:22:59 -0700673 ChildrenSelectMode mode, OpenGLRenderer& renderer, T& handler) {
674 const int size = zTranslatedNodes.size();
675 if (size == 0
676 || (mode == kNegativeZChildren && zTranslatedNodes[0].key > 0.0f)
677 || (mode == kPositiveZChildren && zTranslatedNodes[size - 1].key < 0.0f)) {
678 // no 3d children to draw
679 return;
680 }
681
John Reck113e0822014-03-18 09:22:59 -0700682 /**
683 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
684 * with very similar Z heights to draw together.
685 *
686 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
687 * underneath both, and neither's shadow is drawn on top of the other.
688 */
689 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
690 size_t drawIndex, shadowIndex, endIndex;
691 if (mode == kNegativeZChildren) {
692 drawIndex = 0;
693 endIndex = nonNegativeIndex;
694 shadowIndex = endIndex; // draw no shadows
695 } else {
696 drawIndex = nonNegativeIndex;
697 endIndex = size;
698 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
699 }
Chris Craik3f0854292014-04-15 16:18:08 -0700700
701 DISPLAY_LIST_LOGD("%*s%d %s 3d children:", (handler.level() + 1) * 2, "",
702 endIndex - drawIndex, mode == kNegativeZChildren ? "negative" : "positive");
703
John Reck113e0822014-03-18 09:22:59 -0700704 float lastCasterZ = 0.0f;
705 while (shadowIndex < endIndex || drawIndex < endIndex) {
706 if (shadowIndex < endIndex) {
Chris Craika7090e02014-06-20 16:01:00 -0700707 DrawRenderNodeOp* casterOp = zTranslatedNodes[shadowIndex].value;
708 RenderNode* caster = casterOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700709 const float casterZ = zTranslatedNodes[shadowIndex].key;
710 // attempt to render the shadow if the caster about to be drawn is its caster,
711 // OR if its caster's Z value is similar to the previous potential caster
712 if (shadowIndex == drawIndex || casterZ - lastCasterZ < SHADOW_DELTA) {
Chris Craikb265e2c2014-03-27 15:50:09 -0700713 caster->issueDrawShadowOperation(casterOp->mTransformFromParent, handler);
John Reck113e0822014-03-18 09:22:59 -0700714
715 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
716 shadowIndex++;
717 continue;
718 }
719 }
720
721 // only the actual child DL draw needs to be in save/restore,
722 // since it modifies the renderer's matrix
723 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
724
Chris Craika7090e02014-06-20 16:01:00 -0700725 DrawRenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
726 RenderNode* child = childOp->mRenderNode;
John Reck113e0822014-03-18 09:22:59 -0700727
728 renderer.concatMatrix(childOp->mTransformFromParent);
729 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700730 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700731 childOp->mSkipInOrderDraw = true;
732
733 renderer.restoreToCount(restoreTo);
734 drawIndex++;
735 }
John Reck113e0822014-03-18 09:22:59 -0700736}
737
738template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700739void RenderNode::issueOperationsOfProjectedChildren(OpenGLRenderer& renderer, T& handler) {
Chris Craik3f0854292014-04-15 16:18:08 -0700740 DISPLAY_LIST_LOGD("%*s%d projected children:", (handler.level() + 1) * 2, "", mProjectedNodes.size());
741 const SkPath* projectionReceiverOutline = properties().getOutline().getPath();
Chris Craik3f0854292014-04-15 16:18:08 -0700742 int restoreTo = renderer.getSaveCount();
743
744 // If the projection reciever has an outline, we mask each of the projected rendernodes to it
745 // Either with clipRect, or special saveLayer masking
746 LinearAllocator& alloc = handler.allocator();
747 if (projectionReceiverOutline != NULL) {
748 const SkRect& outlineBounds = projectionReceiverOutline->getBounds();
749 if (projectionReceiverOutline->isRect(NULL)) {
750 // mask to the rect outline simply with clipRect
751 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
752 PROPERTY_SAVECOUNT, properties().getClipToBounds());
753 ClipRectOp* clipOp = new (alloc) ClipRectOp(
754 outlineBounds.left(), outlineBounds.top(),
755 outlineBounds.right(), outlineBounds.bottom(), SkRegion::kIntersect_Op);
756 handler(clipOp, PROPERTY_SAVECOUNT, properties().getClipToBounds());
757 } else {
758 // wrap the projected RenderNodes with a SaveLayer that will mask to the outline
759 SaveLayerOp* op = new (alloc) SaveLayerOp(
760 outlineBounds.left(), outlineBounds.top(),
761 outlineBounds.right(), outlineBounds.bottom(),
Chris Craik80d49022014-06-20 15:03:43 -0700762 255, SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag | SkCanvas::kARGB_ClipLayer_SaveFlag);
Chris Craik3f0854292014-04-15 16:18:08 -0700763 op->setMask(projectionReceiverOutline);
764 handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
765
766 /* TODO: add optimizations here to take advantage of placement/size of projected
767 * children (which may shrink saveLayer area significantly). This is dependent on
768 * passing actual drawing/dirtying bounds of projected content down to native.
769 */
770 }
771 }
772
773 // draw projected nodes
John Reck113e0822014-03-18 09:22:59 -0700774 for (size_t i = 0; i < mProjectedNodes.size(); i++) {
Chris Craika7090e02014-06-20 16:01:00 -0700775 DrawRenderNodeOp* childOp = mProjectedNodes[i];
John Reck113e0822014-03-18 09:22:59 -0700776
777 // matrix save, concat, and restore can be done safely without allocating operations
778 int restoreTo = renderer.save(SkCanvas::kMatrix_SaveFlag);
779 renderer.concatMatrix(childOp->mTransformFromCompositingAncestor);
780 childOp->mSkipInOrderDraw = false; // this is horrible, I'm so sorry everyone
John Reckd0a0b2a2014-03-20 16:28:56 -0700781 handler(childOp, renderer.getSaveCount() - 1, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700782 childOp->mSkipInOrderDraw = true;
783 renderer.restoreToCount(restoreTo);
784 }
Chris Craik3f0854292014-04-15 16:18:08 -0700785
786 if (projectionReceiverOutline != NULL) {
787 handler(new (alloc) RestoreToCountOp(restoreTo),
788 PROPERTY_SAVECOUNT, properties().getClipToBounds());
789 }
John Reck113e0822014-03-18 09:22:59 -0700790}
791
792/**
793 * This function serves both defer and replay modes, and will organize the displayList's component
794 * operations for a single frame:
795 *
796 * Every 'simple' state operation that affects just the matrix and alpha (or other factors of
797 * DeferredDisplayState) may be issued directly to the renderer, but complex operations (with custom
798 * defer logic) and operations in displayListOps are issued through the 'handler' which handles the
799 * defer vs replay logic, per operation
800 */
801template <class T>
Chris Craikb265e2c2014-03-27 15:50:09 -0700802void RenderNode::issueOperations(OpenGLRenderer& renderer, T& handler) {
Chris Craik06451282014-07-21 10:25:54 -0700803 const int level = handler.level();
804 if (mDisplayListData->isEmpty()) {
805 DISPLAY_LIST_LOGD("%*sEmpty display list (%p, %s)", level * 2, "", this, getName());
806 return;
807 }
808
John Reck25fbb3f2014-06-12 13:46:45 -0700809 const bool drawLayer = (mLayer && (&renderer != mLayer->renderer));
810 // If we are updating the contents of mLayer, we don't want to apply any of
811 // the RenderNode's properties to this issueOperations pass. Those will all
812 // be applied when the layer is drawn, aka when this is true.
813 const bool useViewProperties = (!mLayer || drawLayer);
Chris Craik06451282014-07-21 10:25:54 -0700814 if (useViewProperties) {
815 const Outline& outline = properties().getOutline();
816 if (properties().getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty())) {
817 DISPLAY_LIST_LOGD("%*sRejected display list (%p, %s)", level * 2, "", this, getName());
818 return;
819 }
John Reck113e0822014-03-18 09:22:59 -0700820 }
821
Chris Craik3f0854292014-04-15 16:18:08 -0700822 handler.startMark(getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700823
John Reck113e0822014-03-18 09:22:59 -0700824#if DEBUG_DISPLAY_LIST
Chris Craik3f0854292014-04-15 16:18:08 -0700825 const Rect& clipRect = renderer.getLocalClipBounds();
826 DISPLAY_LIST_LOGD("%*sStart display list (%p, %s), localClipBounds: %.0f, %.0f, %.0f, %.0f",
827 level * 2, "", this, getName(),
828 clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
John Reck113e0822014-03-18 09:22:59 -0700829#endif
830
831 LinearAllocator& alloc = handler.allocator();
832 int restoreTo = renderer.getSaveCount();
833 handler(new (alloc) SaveOp(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag),
John Reckd0a0b2a2014-03-20 16:28:56 -0700834 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700835
836 DISPLAY_LIST_LOGD("%*sSave %d %d", (level + 1) * 2, "",
837 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag, restoreTo);
838
John Reck25fbb3f2014-06-12 13:46:45 -0700839 if (useViewProperties) {
840 setViewProperties<T>(renderer, handler);
841 }
John Reck113e0822014-03-18 09:22:59 -0700842
Chris Craik8c271ca2014-03-25 10:33:01 -0700843 bool quickRejected = properties().getClipToBounds()
844 && renderer.quickRejectConservative(0, 0, properties().getWidth(), properties().getHeight());
John Reck113e0822014-03-18 09:22:59 -0700845 if (!quickRejected) {
John Reck25fbb3f2014-06-12 13:46:45 -0700846 if (drawLayer) {
847 handler(new (alloc) DrawLayerOp(mLayer, 0, 0),
848 renderer.getSaveCount() - 1, properties().getClipToBounds());
849 } else {
Chris Craika7090e02014-06-20 16:01:00 -0700850 Vector<ZDrawRenderNodeOpPair> zTranslatedNodes;
John Reck25fbb3f2014-06-12 13:46:45 -0700851 buildZSortedChildList(zTranslatedNodes);
John Reck113e0822014-03-18 09:22:59 -0700852
John Reck25fbb3f2014-06-12 13:46:45 -0700853 // for 3d root, draw children with negative z values
Chris Craik80d49022014-06-20 15:03:43 -0700854 int shadowRestoreTo = issueOperationsOfNegZChildren(zTranslatedNodes, renderer, handler);
John Reck113e0822014-03-18 09:22:59 -0700855
John Reck25fbb3f2014-06-12 13:46:45 -0700856 DisplayListLogBuffer& logBuffer = DisplayListLogBuffer::getInstance();
857 const int saveCountOffset = renderer.getSaveCount() - 1;
858 const int projectionReceiveIndex = mDisplayListData->projectionReceiveIndex;
John Reck1aa5d2d2014-07-24 13:38:28 -0700859 const int size = static_cast<int>(mDisplayListData->displayListOps.size());
860 for (int i = 0; i < size; i++) {
John Reck25fbb3f2014-06-12 13:46:45 -0700861 DisplayListOp *op = mDisplayListData->displayListOps[i];
John Reck113e0822014-03-18 09:22:59 -0700862
Chris Craik80d49022014-06-20 15:03:43 -0700863#if DEBUG_DISPLAY_LIST
John Reck25fbb3f2014-06-12 13:46:45 -0700864 op->output(level + 1);
Chris Craik80d49022014-06-20 15:03:43 -0700865#endif
John Reck25fbb3f2014-06-12 13:46:45 -0700866 logBuffer.writeCommand(level, op->name());
867 handler(op, saveCountOffset, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700868
John Reck25fbb3f2014-06-12 13:46:45 -0700869 if (CC_UNLIKELY(i == projectionReceiveIndex && mProjectedNodes.size() > 0)) {
870 issueOperationsOfProjectedChildren(renderer, handler);
871 }
John Reck113e0822014-03-18 09:22:59 -0700872 }
John Reck113e0822014-03-18 09:22:59 -0700873
John Reck25fbb3f2014-06-12 13:46:45 -0700874 // for 3d root, draw children with positive z values
Chris Craik80d49022014-06-20 15:03:43 -0700875 issueOperationsOfPosZChildren(shadowRestoreTo, zTranslatedNodes, renderer, handler);
John Reck25fbb3f2014-06-12 13:46:45 -0700876 }
John Reck113e0822014-03-18 09:22:59 -0700877 }
878
879 DISPLAY_LIST_LOGD("%*sRestoreToCount %d", (level + 1) * 2, "", restoreTo);
880 handler(new (alloc) RestoreToCountOp(restoreTo),
John Reckd0a0b2a2014-03-20 16:28:56 -0700881 PROPERTY_SAVECOUNT, properties().getClipToBounds());
John Reck113e0822014-03-18 09:22:59 -0700882 renderer.setOverrideLayerAlpha(1.0f);
Chris Craikb265e2c2014-03-27 15:50:09 -0700883
Chris Craik3f0854292014-04-15 16:18:08 -0700884 DISPLAY_LIST_LOGD("%*sDone (%p, %s)", level * 2, "", this, getName());
Chris Craikb265e2c2014-03-27 15:50:09 -0700885 handler.endMark();
John Reck113e0822014-03-18 09:22:59 -0700886}
887
888} /* namespace uirenderer */
889} /* namespace android */