blob: dc967e046f11a9f37e776c232ab3118c15cc57b4 [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
Chris Craik5ea17242016-01-11 14:07:59 -08002 * Copyright (C) 2016 The Android Open Source Project
Chris Craikb565df12015-10-05 13:00:52 -07003 *
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
Chris Craikf158b492016-01-12 14:45:08 -080017#include "FrameBuilder.h"
Chris Craikb565df12015-10-05 13:00:52 -070018
Chris Craik0b7e8242015-10-28 16:50:44 -070019#include "LayerUpdateQueue.h"
Chris Craik161f54b2015-11-05 11:08:52 -080020#include "RenderNode.h"
Doris Liu766431a2016-02-04 22:17:11 +000021#include "VectorDrawable.h"
Chris Craik98787e62015-11-13 10:55:30 -080022#include "renderstate/OffscreenBufferPool.h"
sergeyvdccca442016-03-21 15:38:21 -070023#include "hwui/Canvas.h"
Chris Craik161f54b2015-11-05 11:08:52 -080024#include "utils/FatVector.h"
25#include "utils/PaintUtils.h"
Chris Craik8ecf41c2015-11-16 10:27:59 -080026#include "utils/TraceUtils.h"
Chris Craikb565df12015-10-05 13:00:52 -070027
Chris Craikd3daa312015-11-06 10:59:56 -080028#include <SkPathOps.h>
Chris Craik161f54b2015-11-05 11:08:52 -080029#include <utils/TypeHelpers.h>
Chris Craikb565df12015-10-05 13:00:52 -070030
31namespace android {
32namespace uirenderer {
33
Chris Craikf158b492016-01-12 14:45:08 -080034FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip,
Chris Craik0b7e8242015-10-28 16:50:44 -070035 uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craik6e068c012016-01-15 16:15:30 -080036 const std::vector< sp<RenderNode> >& nodes,
37 const LightGeometry& lightGeometry, const Rect &contentDrawBounds, Caches* caches)
38 : mCanvasState(*this)
39 , mCaches(caches)
40 , mLightRadius(lightGeometry.radius) {
Chris Craik818c9fb2015-10-23 14:33:42 -070041 ATRACE_NAME("prepare drawing commands");
Chris Craikb565df12015-10-05 13:00:52 -070042
Chris Craikf158b492016-01-12 14:45:08 -080043 mLayerBuilders.reserve(layers.entries().size());
Chris Craik98787e62015-11-13 10:55:30 -080044 mLayerStack.reserve(layers.entries().size());
45
46 // Prepare to defer Fbo0
Chris Craikf158b492016-01-12 14:45:08 -080047 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
48 mLayerBuilders.push_back(fbo0);
Chris Craik98787e62015-11-13 10:55:30 -080049 mLayerStack.push_back(0);
Chris Craikb565df12015-10-05 13:00:52 -070050 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -070051 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
Chris Craik6e068c012016-01-15 16:15:30 -080052 lightGeometry.center);
Chris Craik0b7e8242015-10-28 16:50:44 -070053
54 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
Chris Craikf158b492016-01-12 14:45:08 -080055 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
Chris Craik0b7e8242015-10-28 16:50:44 -070056 for (int i = layers.entries().size() - 1; i >= 0; i--) {
57 RenderNode* layerNode = layers.entries()[i].renderNode;
Chris Craike9c5fd82016-01-12 18:59:38 -080058 // only schedule repaint if node still on layer - possible it may have been
59 // removed during a dropped frame, but layers may still remain scheduled so
60 // as not to lose info on what portion is damaged
61 if (CC_LIKELY(layerNode->getLayer() != nullptr)) {
62 const Rect& layerDamage = layers.entries()[i].damage;
63 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070064
Chris Craike9c5fd82016-01-12 18:59:38 -080065 // map current light center into RenderNode's coordinate space
66 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
67 layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080068
Chris Craike9c5fd82016-01-12 18:59:38 -080069 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
70 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070071
Chris Craike9c5fd82016-01-12 18:59:38 -080072 if (layerNode->getDisplayList()) {
73 deferNodeOps(*layerNode);
74 }
75 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -070076 }
Chris Craik0b7e8242015-10-28 16:50:44 -070077 }
78
Chong Zhangc3bd5682016-01-25 12:01:12 -080079 // It there are multiple render nodes, they are laid out as follows:
80 // #0 - backdrop (content + caption)
81 // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
82 // #2 - additional overlay nodes
83 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
84 // resizing however it might become partially visible. The following render loop will crop the
85 // backdrop against the content and draw the remaining part of it. It will then draw the content
86 // cropped to the backdrop (since that indicates a shrinking of the window).
87 //
88 // Additional nodes will be drawn on top with no particular clipping semantics.
89
90 // The bounds of the backdrop against which the content should be clipped.
91 Rect backdropBounds = contentDrawBounds;
92 // Usually the contents bounds should be mContentDrawBounds - however - we will
93 // move it towards the fixed edge to give it a more stable appearance (for the moment).
94 // If there is no content bounds we ignore the layering as stated above and start with 2.
95 int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0;
96
Chris Craikb565df12015-10-05 13:00:52 -070097 for (const sp<RenderNode>& node : nodes) {
98 if (node->nothingToDraw()) continue;
Chris Craik8d1f2122015-11-24 16:40:09 -080099 node->computeOrdering();
Florin Malitaeecff562015-12-21 10:43:01 -0500100 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800101
102 if (layer == 0) {
103 const RenderProperties& properties = node->properties();
104 Rect targetBounds(properties.getLeft(), properties.getTop(),
105 properties.getRight(), properties.getBottom());
106 // Move the content bounds towards the fixed corner of the backdrop.
107 const int x = targetBounds.left;
108 const int y = targetBounds.top;
109 // Remember the intersection of the target bounds and the intersection bounds against
110 // which we have to crop the content.
111 backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
112 backdropBounds.doIntersect(targetBounds);
113 } else if (layer == 1) {
114 // We shift and clip the content to match its final location in the window.
115 const float left = contentDrawBounds.left;
116 const float top = contentDrawBounds.top;
117 const float dx = backdropBounds.left - left;
118 const float dy = backdropBounds.top - top;
119 const float width = backdropBounds.getWidth();
120 const float height = backdropBounds.getHeight();
121 mCanvasState.translate(dx, dy);
122 // It gets cropped against the bounds of the backdrop to stay inside.
123 mCanvasState.clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op);
124 }
125
Chris Craik0b7e8242015-10-28 16:50:44 -0700126 deferNodePropsAndOps(*node);
127 mCanvasState.restoreToCount(count);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800128 layer++;
Chris Craikb565df12015-10-05 13:00:52 -0700129 }
130}
131
Chris Craikf158b492016-01-12 14:45:08 -0800132void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700133
Chris Craikf158b492016-01-12 14:45:08 -0800134void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700135
Chris Craikf158b492016-01-12 14:45:08 -0800136void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800137 const RenderProperties& properties = node.properties();
138 const Outline& outline = properties.getOutline();
139 if (properties.getAlpha() <= 0
140 || (outline.getShouldClip() && outline.isEmpty())
141 || properties.getScaleX() == 0
142 || properties.getScaleY() == 0) {
143 return; // rejected
144 }
145
146 if (properties.getLeft() != 0 || properties.getTop() != 0) {
147 mCanvasState.translate(properties.getLeft(), properties.getTop());
148 }
149 if (properties.getStaticMatrix()) {
150 mCanvasState.concatMatrix(*properties.getStaticMatrix());
151 } else if (properties.getAnimationMatrix()) {
152 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
153 }
154 if (properties.hasTransformMatrix()) {
155 if (properties.isTransformTranslateOnly()) {
156 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
157 } else {
158 mCanvasState.concatMatrix(*properties.getTransformMatrix());
159 }
160 }
161
162 const int width = properties.getWidth();
163 const int height = properties.getHeight();
164
165 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
166 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
167 int clipFlags = properties.getClippingFlags();
168 if (properties.getAlpha() < 1) {
169 if (isLayer) {
170 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
171 }
172 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
173 // simply scale rendering content's alpha
174 mCanvasState.scaleAlpha(properties.getAlpha());
175 } else {
176 // schedule saveLayer by initializing saveLayerBounds
177 saveLayerBounds.set(0, 0, width, height);
178 if (clipFlags) {
179 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
180 clipFlags = 0; // all clipping done by savelayer
181 }
182 }
183
184 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
185 // pretend alpha always causes savelayer to warn about
186 // performance problem affecting old versions
187 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
188 }
189 }
190 if (clipFlags) {
191 Rect clipRect;
192 properties.getClippingRectForFlags(clipFlags, &clipRect);
193 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
194 SkRegion::kIntersect_Op);
195 }
196
197 if (properties.getRevealClip().willClip()) {
198 Rect bounds;
199 properties.getRevealClip().getBounds(&bounds);
200 mCanvasState.setClippingRoundRect(mAllocator,
201 bounds, properties.getRevealClip().getRadius());
202 } else if (properties.getOutline().willClip()) {
203 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
204 }
205
Chris Craik8913c892016-01-14 16:15:03 -0800206 bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty()
207 || (properties.getClipToBounds()
208 && mCanvasState.quickRejectConservative(0, 0, width, height));
Chris Craik7fc1b032016-02-03 19:45:06 -0800209 if (!quickRejected) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800210 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700211 if (node.getLayer()) {
212 // HW layer
John Reck7df9ff22016-02-10 16:08:08 -0800213 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700214 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
215 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800216 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700217 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
218 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800219 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
220 // draw DisplayList contents within temporary, since persisted layer could not be used.
221 // (temp layers are clipped to viewport, since they don't persist offscreen content)
222 SkPaint saveLayerPaint;
223 saveLayerPaint.setAlpha(properties.getAlpha());
John Reck7df9ff22016-02-10 16:08:08 -0800224 deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800225 saveLayerBounds,
226 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800227 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800228 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800229 deferNodeOps(node);
John Reck7df9ff22016-02-10 16:08:08 -0800230 deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
Chris Craik0b7e8242015-10-28 16:50:44 -0700231 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800232 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700233 }
234 }
235}
236
Chris Craik161f54b2015-11-05 11:08:52 -0800237typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
238
239template <typename V>
240static void buildZSortedChildList(V* zTranslatedNodes,
241 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
242 if (chunk.beginChildIndex == chunk.endChildIndex) return;
243
244 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
245 RenderNodeOp* childOp = displayList.getChildren()[i];
246 RenderNode* child = childOp->renderNode;
247 float childZ = child->properties().getZ();
248
249 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
250 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
251 childOp->skipInOrderDraw = true;
252 } else if (!child->properties().getProjectBackwards()) {
253 // regular, in order drawing DisplayList
254 childOp->skipInOrderDraw = false;
255 }
256 }
257
258 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
259 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
260}
261
262template <typename V>
263static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
264 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
265 if (zTranslatedNodes[i].key >= 0.0f) return i;
266 }
267 return zTranslatedNodes.size();
268}
269
270template <typename V>
Chris Craikf158b492016-01-12 14:45:08 -0800271void FrameBuilder::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800272 const int size = zTranslatedNodes.size();
273 if (size == 0
274 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
275 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
276 // no 3d children to draw
277 return;
278 }
279
280 /**
281 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
282 * with very similar Z heights to draw together.
283 *
284 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
285 * underneath both, and neither's shadow is drawn on top of the other.
286 */
287 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
288 size_t drawIndex, shadowIndex, endIndex;
289 if (mode == ChildrenSelectMode::Negative) {
290 drawIndex = 0;
291 endIndex = nonNegativeIndex;
292 shadowIndex = endIndex; // draw no shadows
293 } else {
294 drawIndex = nonNegativeIndex;
295 endIndex = size;
296 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
297 }
298
299 float lastCasterZ = 0.0f;
300 while (shadowIndex < endIndex || drawIndex < endIndex) {
301 if (shadowIndex < endIndex) {
302 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
303 const float casterZ = zTranslatedNodes[shadowIndex].key;
304 // attempt to render the shadow if the caster about to be drawn is its caster,
305 // OR if its caster's Z value is similar to the previous potential caster
306 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
307 deferShadow(*casterNodeOp);
308
309 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
310 shadowIndex++;
311 continue;
312 }
313 }
314
315 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800316 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800317 drawIndex++;
318 }
319}
320
Chris Craikf158b492016-01-12 14:45:08 -0800321void FrameBuilder::deferShadow(const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800322 auto& node = *casterNodeOp.renderNode;
323 auto& properties = node.properties();
324
325 if (properties.getAlpha() <= 0.0f
326 || properties.getOutline().getAlpha() <= 0.0f
327 || !properties.getOutline().getPath()
328 || properties.getScaleX() == 0
329 || properties.getScaleY() == 0) {
330 // no shadow to draw
331 return;
332 }
333
334 const SkPath* casterOutlinePath = properties.getOutline().getPath();
335 const SkPath* revealClipPath = properties.getRevealClip().getPath();
336 if (revealClipPath && revealClipPath->isEmpty()) return;
337
338 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
339
340 // holds temporary SkPath to store the result of intersections
341 SkPath* frameAllocatedPath = nullptr;
342 const SkPath* casterPath = casterOutlinePath;
343
344 // intersect the shadow-casting path with the reveal, if present
345 if (revealClipPath) {
346 frameAllocatedPath = createFrameAllocatedPath();
347
348 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
349 casterPath = frameAllocatedPath;
350 }
351
352 // intersect the shadow-casting path with the clipBounds, if present
353 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
354 if (!frameAllocatedPath) {
355 frameAllocatedPath = createFrameAllocatedPath();
356 }
357 Rect clipBounds;
358 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
359 SkPath clipBoundsPath;
360 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
361 clipBounds.right, clipBounds.bottom);
362
363 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
364 casterPath = frameAllocatedPath;
365 }
366
Chris Craik6e068c012016-01-15 16:15:30 -0800367
368 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
369 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
370 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
371 node.applyViewPropertyTransforms(shadowMatrixXY, false);
372 node.applyViewPropertyTransforms(shadowMatrixZ, true);
373
374 LOG_ALWAYS_FATAL_IF(!mCaches, "Caches needed for shadows");
375 sp<TessellationCache::ShadowTask> task = mCaches->tessellationCache.getShadowTask(
376 mCanvasState.currentTransform(),
377 mCanvasState.getLocalClipBounds(),
378 casterAlpha >= 1.0f,
379 casterPath,
380 &shadowMatrixXY, &shadowMatrixZ,
381 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
382 mLightRadius);
383 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
384 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
385 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
386 if (CC_LIKELY(bakedOpState)) {
387 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
388 }
Chris Craikd3daa312015-11-06 10:59:56 -0800389 }
Chris Craik161f54b2015-11-05 11:08:52 -0800390}
Chris Craikd3daa312015-11-06 10:59:56 -0800391
Chris Craikf158b492016-01-12 14:45:08 -0800392void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500393 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800394 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Chris Craik8d1f2122015-11-24 16:40:09 -0800395
Chris Craik678ff812016-03-01 13:27:54 -0800396 SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
397 if (projectionReceiverOutline) {
398 // transform the mask for this projector into render target space
399 // TODO: consider combining both transforms by stashing transform instead of applying
400 SkMatrix skCurrentTransform;
401 mCanvasState.currentTransform()->copyTo(skCurrentTransform);
402 projectionReceiverOutline->transform(
403 skCurrentTransform,
404 &transformedMaskPath);
405 mCanvasState.setProjectionPathMask(mAllocator, &transformedMaskPath);
406 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800407
Chris Craik8d1f2122015-11-24 16:40:09 -0800408 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
409 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
Chris Craika748c082016-03-01 18:48:37 -0800410 RenderNode& childNode = *childOp->renderNode;
Chris Craik678ff812016-03-01 13:27:54 -0800411
Chris Craika748c082016-03-01 18:48:37 -0800412 // Draw child if it has content, but ignore state in childOp - matrix already applied to
413 // transformFromCompositingAncestor, and record-time clip is ignored when projecting
414 if (!childNode.nothingToDraw()) {
415 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800416
Chris Craika748c082016-03-01 18:48:37 -0800417 // Apply transform between ancestor and projected descendant
418 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
419
420 deferNodePropsAndOps(childNode);
421
422 mCanvasState.restoreToCount(restoreTo);
423 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800424 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800425 mCanvasState.restoreToCount(count);
426}
427
Chris Craikb565df12015-10-05 13:00:52 -0700428/**
Chris Craikf158b492016-01-12 14:45:08 -0800429 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700430 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800431 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800432 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700433 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700434#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800435 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
436void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
437 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800438 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800439
440 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
441 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikb36af872015-10-16 14:23:12 -0700442 for (const DisplayList::Chunk& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800443 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
444 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
445
446 defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700447 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700448 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700449 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800450
451 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
452 && displayList.projectionReceiveIndex >= 0
453 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
454 deferProjectedChildren(renderNode);
455 }
Chris Craikb565df12015-10-05 13:00:52 -0700456 }
Chris Craik161f54b2015-11-05 11:08:52 -0800457 defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700458 }
459}
460
Chris Craikf158b492016-01-12 14:45:08 -0800461void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800462 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500463 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700464
Chris Craike4db79d2015-12-22 16:32:23 -0800465 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
466 mCanvasState.writableSnapshot()->mutateClipArea().applyClip(op.localClip,
467 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700468 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700469
Chris Craik0b7e8242015-10-28 16:50:44 -0700470 // then apply state from node properties, and defer ops
471 deferNodePropsAndOps(*op.renderNode);
472
Chris Craik6fe991e52015-10-20 09:39:42 -0700473 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700474}
475
Chris Craikf158b492016-01-12 14:45:08 -0800476void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800477 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800478 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800479 }
480}
481
Chris Craik386aa032015-12-07 17:08:25 -0800482/**
483 * Defers an unmergeable, strokeable op, accounting correctly
484 * for paint's style on the bounds being computed.
485 */
Chris Craikf158b492016-01-12 14:45:08 -0800486void FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800487 BakedOpState::StrokeBehavior strokeBehavior) {
488 // Note: here we account for stroke when baking the op
489 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800490 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik386aa032015-12-07 17:08:25 -0800491 if (!bakedState) return; // quick rejected
492 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
493}
494
495/**
496 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
497 * be used, since they trigger significantly different rendering paths.
498 *
499 * Note: not used for lines/points, since they don't currently support path effects.
500 */
501static batchid_t tessBatchId(const RecordedOp& op) {
502 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700503 return paint.getPathEffect()
504 ? OpBatchType::AlphaMaskTexture
505 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
506}
507
Chris Craikf158b492016-01-12 14:45:08 -0800508void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800509 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800510}
511
Chris Craikb87eadd2016-01-06 09:16:05 -0800512static bool hasMergeableClip(const BakedOpState& state) {
513 return state.computedState.clipState
514 || state.computedState.clipState->mode == ClipMode::Rectangle;
515}
516
Chris Craikf158b492016-01-12 14:45:08 -0800517void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800518 BakedOpState* bakedState = tryBakeOpState(op);
519 if (!bakedState) return; // quick rejected
Chris Craikb565df12015-10-05 13:00:52 -0700520
Chris Craik15c3f192015-12-03 12:16:56 -0800521 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
522 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
523 // MergingDrawBatch::canMergeWith()
524 if (bakedState->computedState.transform.isSimple()
525 && bakedState->computedState.transform.positiveScale()
526 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800527 && op.bitmap->colorType() != kAlpha_8_SkColorType
528 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800529 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800530 // TODO: AssetAtlas in mergeId
531 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
532 } else {
533 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
534 }
Chris Craikb565df12015-10-05 13:00:52 -0700535}
536
Chris Craikf158b492016-01-12 14:45:08 -0800537void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800538 BakedOpState* bakedState = tryBakeOpState(op);
539 if (!bakedState) return; // quick rejected
540 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
541}
542
Chris Craikf158b492016-01-12 14:45:08 -0800543void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800544 BakedOpState* bakedState = tryBakeOpState(op);
545 if (!bakedState) return; // quick rejected
546 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
547}
548
Doris Liu766431a2016-02-04 22:17:11 +0000549void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
550 const SkBitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
551 SkPaint* paint = op.vectorDrawable->getPaint();
John Reck7df9ff22016-02-10 16:08:08 -0800552 const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds,
Doris Liu766431a2016-02-04 22:17:11 +0000553 op.localMatrix,
554 op.localClip,
555 paint,
556 &bitmap,
557 Rect(bitmap.width(), bitmap.height()));
558 deferBitmapRectOp(*resolvedOp);
559}
560
Chris Craikf158b492016-01-12 14:45:08 -0800561void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800562 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
563 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
564 float x = *(op.x);
565 float y = *(op.y);
566 float radius = *(op.radius);
567 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
John Reck7df9ff22016-02-10 16:08:08 -0800568 const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800569 unmappedBounds,
570 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800571 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800572 op.paint);
573 deferOvalOp(*resolvedOp);
574}
575
Chris Craikf158b492016-01-12 14:45:08 -0800576void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700577 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craike29ce6f2015-12-10 16:25:13 -0800578 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800579 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800580}
581
Chris Craikf158b492016-01-12 14:45:08 -0800582void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800583 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800584 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800585}
586
Chris Craikf158b492016-01-12 14:45:08 -0800587void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800588 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800589}
590
Chris Craikf158b492016-01-12 14:45:08 -0800591void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800592 BakedOpState* bakedState = tryBakeOpState(op);
593 if (!bakedState) return; // quick rejected
594
595 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800596 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
597 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800598 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800599 // TODO: AssetAtlas in mergeId
600
601 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
602 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
603 } else {
604 // Use Bitmap batchId since Bitmap+Patch use same shader
605 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
606 }
607}
608
Chris Craikf158b492016-01-12 14:45:08 -0800609void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800610 deferStrokeableOp(op, OpBatchType::Bitmap);
Chris Craik386aa032015-12-07 17:08:25 -0800611}
612
Chris Craikf158b492016-01-12 14:45:08 -0800613void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800614 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800615 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800616}
617
Chris Craikf158b492016-01-12 14:45:08 -0800618void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800619 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800620}
621
Chris Craikf158b492016-01-12 14:45:08 -0800622void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800623 deferStrokeableOp(op, tessBatchId(op));
Chris Craikb565df12015-10-05 13:00:52 -0700624}
625
Chris Craikf158b492016-01-12 14:45:08 -0800626void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800627 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
628 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
John Reck7df9ff22016-02-10 16:08:08 -0800629 const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800630 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
631 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800632 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800633 op.paint, *op.rx, *op.ry);
634 deferRoundRectOp(*resolvedOp);
635}
636
Chris Craikf158b492016-01-12 14:45:08 -0800637void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800638 BakedOpState* bakedState = tryBakeOpState(op);
639 if (!bakedState) return; // quick rejected
640 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700641}
642
Chris Craikd7448e62015-12-15 10:34:36 -0800643static batchid_t textBatchId(const SkPaint& paint) {
644 // TODO: better handling of shader (since we won't care about color then)
645 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
646}
647
Chris Craikf158b492016-01-12 14:45:08 -0800648void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik7c02cab2016-03-16 17:15:12 -0700649 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
650 mAllocator, *mCanvasState.writableSnapshot(), op,
651 BakedOpState::StrokeBehavior::StyleDefined);
Chris Craik15c3f192015-12-03 12:16:56 -0800652 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800653
Chris Craikd7448e62015-12-15 10:34:36 -0800654 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800655 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800656 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
657 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800658 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
659 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
660 } else {
661 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
662 }
Chris Craika1717272015-11-19 13:02:43 -0800663}
664
Chris Craikf158b492016-01-12 14:45:08 -0800665void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700666 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craikd7448e62015-12-15 10:34:36 -0800667 if (!bakedState) return; // quick rejected
668 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
669}
670
Chris Craikf158b492016-01-12 14:45:08 -0800671void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800672 BakedOpState* bakedState = tryBakeOpState(op);
673 if (!bakedState) return; // quick rejected
674 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
675}
676
Chris Craikf158b492016-01-12 14:45:08 -0800677void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800678 float contentTranslateX, float contentTranslateY,
679 const Rect& repaintRect,
680 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700681 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500682 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700683 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700684 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800685 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800686 mCanvasState.writableSnapshot()->transform->loadTranslate(
687 contentTranslateX, contentTranslateY, 0);
688 mCanvasState.writableSnapshot()->setClip(
689 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800690
Chris Craik8ecf41c2015-11-16 10:27:59 -0800691 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800692 mLayerStack.push_back(mLayerBuilders.size());
693 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800694 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800695 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700696}
697
Chris Craikf158b492016-01-12 14:45:08 -0800698void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700699 // restore canvas, and pop finished layer off of the stack
700 mCanvasState.restore();
701 mLayerStack.pop_back();
702}
703
Chris Craikb87eadd2016-01-06 09:16:05 -0800704// TODO: defer time rejection (when bounds become empty) + tests
705// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800706void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800707 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
708 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
709
710 auto previous = mCanvasState.currentSnapshot();
711 Vector3 lightCenter = previous->getRelativeLightCenter();
712
713 // Combine all transforms used to present saveLayer content:
714 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800715 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800716 contentTransform.multiply(op.localMatrix);
717 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
718
719 Matrix4 inverseContentTransform;
720 inverseContentTransform.loadInverse(contentTransform);
721
722 // map the light center into layer-relative space
723 inverseContentTransform.mapPoint3d(lightCenter);
724
725 // Clip bounds of temporary layer to parent's clip rect, so:
726 Rect saveLayerBounds(layerWidth, layerHeight);
727 // 1) transform Rect(width, height) into parent's space
728 // note: left/top offsets put in contentTransform above
729 contentTransform.mapRect(saveLayerBounds);
730 // 2) intersect with parent's clip
731 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
732 // 3) and transform back
733 inverseContentTransform.mapRect(saveLayerBounds);
734 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
735 saveLayerBounds.roundOut();
736
737 // if bounds are reduced, will clip the layer's area by reducing required bounds...
738 layerWidth = saveLayerBounds.getWidth();
739 layerHeight = saveLayerBounds.getHeight();
740 // ...and shifting drawing content to account for left/top side clipping
741 float contentTranslateX = -saveLayerBounds.left;
742 float contentTranslateY = -saveLayerBounds.top;
743
744 saveForLayer(layerWidth, layerHeight,
745 contentTranslateX, contentTranslateY,
746 Rect(layerWidth, layerHeight),
747 lightCenter,
748 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700749}
Chris Craikb565df12015-10-05 13:00:52 -0700750
Chris Craikf158b492016-01-12 14:45:08 -0800751void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700752 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700753 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700754
755 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700756
757 // record the draw operation into the previous layer's list of draw commands
758 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
John Reck7df9ff22016-02-10 16:08:08 -0800759 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
Chris Craik6fe991e52015-10-20 09:39:42 -0700760 beginLayerOp.unmappedBounds,
761 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800762 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700763 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800764 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700765 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
766
767 if (bakedOpState) {
768 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
769 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
770 } else {
771 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800772 // TODO: need to prevent any render work from being done
773 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800774 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700775 return;
Chris Craikb565df12015-10-05 13:00:52 -0700776 }
777}
778
Chris Craikf158b492016-01-12 14:45:08 -0800779void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800780 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
781 boundsTransform.multiply(op.localMatrix);
782
783 Rect dstRect(op.unmappedBounds);
784 boundsTransform.mapRect(dstRect);
785 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
786
Chris Craik4876de12016-02-25 16:54:08 -0800787 if (dstRect.isEmpty()) {
788 // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
789 currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
790 } else {
791 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
792 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800793
Chris Craik4876de12016-02-25 16:54:08 -0800794 /**
795 * First, defer an operation to copy out the content from the rendertarget into a layer.
796 */
797 auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
798 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
799 &(currentLayer().repaintClip), dstRect, *copyToOp);
800 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
Chris Craikb87eadd2016-01-06 09:16:05 -0800801
Chris Craik4876de12016-02-25 16:54:08 -0800802 /**
803 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
804 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
805 */
806 currentLayer().deferLayerClear(dstRect);
Chris Craikb87eadd2016-01-06 09:16:05 -0800807
Chris Craik4876de12016-02-25 16:54:08 -0800808 /**
809 * And stash an operation to copy that layer back under the rendertarget until
810 * a balanced EndUnclippedLayerOp is seen
811 */
812 auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
813 bakedState = BakedOpState::directConstruct(mAllocator,
814 &(currentLayer().repaintClip), dstRect, *copyFromOp);
815 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
816 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800817}
818
Chris Craikf158b492016-01-12 14:45:08 -0800819void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800820 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
821
822 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
Chris Craikb87eadd2016-01-06 09:16:05 -0800823 currentLayer().activeUnclippedSaveLayers.pop_back();
Chris Craik4876de12016-02-25 16:54:08 -0800824 if (copyFromLayerOp) {
825 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
826 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800827}
828
Chris Craikb565df12015-10-05 13:00:52 -0700829} // namespace uirenderer
830} // namespace android