blob: c8910cbdd33774a301272ff603cf1b68ccdb82d6 [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
Florin Malitaeecff562015-12-21 10:43:01 -050019#include "Canvas.h"
Chris Craik0b7e8242015-10-28 16:50:44 -070020#include "LayerUpdateQueue.h"
Chris Craik161f54b2015-11-05 11:08:52 -080021#include "RenderNode.h"
Chris Craik98787e62015-11-13 10:55:30 -080022#include "renderstate/OffscreenBufferPool.h"
Chris Craik161f54b2015-11-05 11:08:52 -080023#include "utils/FatVector.h"
24#include "utils/PaintUtils.h"
Chris Craik8ecf41c2015-11-16 10:27:59 -080025#include "utils/TraceUtils.h"
Chris Craikb565df12015-10-05 13:00:52 -070026
Chris Craikd3daa312015-11-06 10:59:56 -080027#include <SkPathOps.h>
Chris Craik161f54b2015-11-05 11:08:52 -080028#include <utils/TypeHelpers.h>
Chris Craikb565df12015-10-05 13:00:52 -070029
30namespace android {
31namespace uirenderer {
32
Chris Craikf158b492016-01-12 14:45:08 -080033FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip,
Chris Craik0b7e8242015-10-28 16:50:44 -070034 uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craik6e068c012016-01-15 16:15:30 -080035 const std::vector< sp<RenderNode> >& nodes,
36 const LightGeometry& lightGeometry, const Rect &contentDrawBounds, Caches* caches)
37 : mCanvasState(*this)
38 , mCaches(caches)
39 , mLightRadius(lightGeometry.radius) {
Chris Craik818c9fb2015-10-23 14:33:42 -070040 ATRACE_NAME("prepare drawing commands");
Chris Craikb565df12015-10-05 13:00:52 -070041
Chris Craikf158b492016-01-12 14:45:08 -080042 mLayerBuilders.reserve(layers.entries().size());
Chris Craik98787e62015-11-13 10:55:30 -080043 mLayerStack.reserve(layers.entries().size());
44
45 // Prepare to defer Fbo0
Chris Craikf158b492016-01-12 14:45:08 -080046 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
47 mLayerBuilders.push_back(fbo0);
Chris Craik98787e62015-11-13 10:55:30 -080048 mLayerStack.push_back(0);
Chris Craikb565df12015-10-05 13:00:52 -070049 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -070050 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
Chris Craik6e068c012016-01-15 16:15:30 -080051 lightGeometry.center);
Chris Craik0b7e8242015-10-28 16:50:44 -070052
53 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
Chris Craikf158b492016-01-12 14:45:08 -080054 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
Chris Craik0b7e8242015-10-28 16:50:44 -070055 for (int i = layers.entries().size() - 1; i >= 0; i--) {
56 RenderNode* layerNode = layers.entries()[i].renderNode;
Chris Craike9c5fd82016-01-12 18:59:38 -080057 // only schedule repaint if node still on layer - possible it may have been
58 // removed during a dropped frame, but layers may still remain scheduled so
59 // as not to lose info on what portion is damaged
60 if (CC_LIKELY(layerNode->getLayer() != nullptr)) {
61 const Rect& layerDamage = layers.entries()[i].damage;
62 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070063
Chris Craike9c5fd82016-01-12 18:59:38 -080064 // map current light center into RenderNode's coordinate space
65 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
66 layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080067
Chris Craike9c5fd82016-01-12 18:59:38 -080068 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
69 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070070
Chris Craike9c5fd82016-01-12 18:59:38 -080071 if (layerNode->getDisplayList()) {
72 deferNodeOps(*layerNode);
73 }
74 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -070075 }
Chris Craik0b7e8242015-10-28 16:50:44 -070076 }
77
Chong Zhangc3bd5682016-01-25 12:01:12 -080078 // It there are multiple render nodes, they are laid out as follows:
79 // #0 - backdrop (content + caption)
80 // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
81 // #2 - additional overlay nodes
82 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
83 // resizing however it might become partially visible. The following render loop will crop the
84 // backdrop against the content and draw the remaining part of it. It will then draw the content
85 // cropped to the backdrop (since that indicates a shrinking of the window).
86 //
87 // Additional nodes will be drawn on top with no particular clipping semantics.
88
89 // The bounds of the backdrop against which the content should be clipped.
90 Rect backdropBounds = contentDrawBounds;
91 // Usually the contents bounds should be mContentDrawBounds - however - we will
92 // move it towards the fixed edge to give it a more stable appearance (for the moment).
93 // If there is no content bounds we ignore the layering as stated above and start with 2.
94 int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0;
95
Chris Craikb565df12015-10-05 13:00:52 -070096 for (const sp<RenderNode>& node : nodes) {
97 if (node->nothingToDraw()) continue;
Chris Craik8d1f2122015-11-24 16:40:09 -080098 node->computeOrdering();
Florin Malitaeecff562015-12-21 10:43:01 -050099 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800100
101 if (layer == 0) {
102 const RenderProperties& properties = node->properties();
103 Rect targetBounds(properties.getLeft(), properties.getTop(),
104 properties.getRight(), properties.getBottom());
105 // Move the content bounds towards the fixed corner of the backdrop.
106 const int x = targetBounds.left;
107 const int y = targetBounds.top;
108 // Remember the intersection of the target bounds and the intersection bounds against
109 // which we have to crop the content.
110 backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
111 backdropBounds.doIntersect(targetBounds);
112 } else if (layer == 1) {
113 // We shift and clip the content to match its final location in the window.
114 const float left = contentDrawBounds.left;
115 const float top = contentDrawBounds.top;
116 const float dx = backdropBounds.left - left;
117 const float dy = backdropBounds.top - top;
118 const float width = backdropBounds.getWidth();
119 const float height = backdropBounds.getHeight();
120 mCanvasState.translate(dx, dy);
121 // It gets cropped against the bounds of the backdrop to stay inside.
122 mCanvasState.clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op);
123 }
124
Chris Craik0b7e8242015-10-28 16:50:44 -0700125 deferNodePropsAndOps(*node);
126 mCanvasState.restoreToCount(count);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800127 layer++;
Chris Craikb565df12015-10-05 13:00:52 -0700128 }
129}
130
Chris Craikf158b492016-01-12 14:45:08 -0800131void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700132
Chris Craikf158b492016-01-12 14:45:08 -0800133void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700134
Chris Craikf158b492016-01-12 14:45:08 -0800135void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800136 const RenderProperties& properties = node.properties();
137 const Outline& outline = properties.getOutline();
138 if (properties.getAlpha() <= 0
139 || (outline.getShouldClip() && outline.isEmpty())
140 || properties.getScaleX() == 0
141 || properties.getScaleY() == 0) {
142 return; // rejected
143 }
144
145 if (properties.getLeft() != 0 || properties.getTop() != 0) {
146 mCanvasState.translate(properties.getLeft(), properties.getTop());
147 }
148 if (properties.getStaticMatrix()) {
149 mCanvasState.concatMatrix(*properties.getStaticMatrix());
150 } else if (properties.getAnimationMatrix()) {
151 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
152 }
153 if (properties.hasTransformMatrix()) {
154 if (properties.isTransformTranslateOnly()) {
155 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
156 } else {
157 mCanvasState.concatMatrix(*properties.getTransformMatrix());
158 }
159 }
160
161 const int width = properties.getWidth();
162 const int height = properties.getHeight();
163
164 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
165 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
166 int clipFlags = properties.getClippingFlags();
167 if (properties.getAlpha() < 1) {
168 if (isLayer) {
169 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
170 }
171 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
172 // simply scale rendering content's alpha
173 mCanvasState.scaleAlpha(properties.getAlpha());
174 } else {
175 // schedule saveLayer by initializing saveLayerBounds
176 saveLayerBounds.set(0, 0, width, height);
177 if (clipFlags) {
178 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
179 clipFlags = 0; // all clipping done by savelayer
180 }
181 }
182
183 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
184 // pretend alpha always causes savelayer to warn about
185 // performance problem affecting old versions
186 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
187 }
188 }
189 if (clipFlags) {
190 Rect clipRect;
191 properties.getClippingRectForFlags(clipFlags, &clipRect);
192 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
193 SkRegion::kIntersect_Op);
194 }
195
196 if (properties.getRevealClip().willClip()) {
197 Rect bounds;
198 properties.getRevealClip().getBounds(&bounds);
199 mCanvasState.setClippingRoundRect(mAllocator,
200 bounds, properties.getRevealClip().getRadius());
201 } else if (properties.getOutline().willClip()) {
202 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
203 }
204
Chris Craik7fc1b032016-02-03 19:45:06 -0800205 bool quickRejected = properties.getClipToBounds()
206 && mCanvasState.quickRejectConservative(0, 0, width, height);
207 if (!quickRejected) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800208 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700209 if (node.getLayer()) {
210 // HW layer
211 LayerOp* drawLayerOp = new (mAllocator) LayerOp(node);
212 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
213 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800214 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700215 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
216 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800217 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
218 // draw DisplayList contents within temporary, since persisted layer could not be used.
219 // (temp layers are clipped to viewport, since they don't persist offscreen content)
220 SkPaint saveLayerPaint;
221 saveLayerPaint.setAlpha(properties.getAlpha());
Chris Craik268a9c02015-12-09 18:05:12 -0800222 deferBeginLayerOp(*new (mAllocator) BeginLayerOp(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800223 saveLayerBounds,
224 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800225 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800226 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800227 deferNodeOps(node);
Chris Craik268a9c02015-12-09 18:05:12 -0800228 deferEndLayerOp(*new (mAllocator) EndLayerOp());
Chris Craik0b7e8242015-10-28 16:50:44 -0700229 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800230 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700231 }
232 }
233}
234
Chris Craik161f54b2015-11-05 11:08:52 -0800235typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
236
237template <typename V>
238static void buildZSortedChildList(V* zTranslatedNodes,
239 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
240 if (chunk.beginChildIndex == chunk.endChildIndex) return;
241
242 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
243 RenderNodeOp* childOp = displayList.getChildren()[i];
244 RenderNode* child = childOp->renderNode;
245 float childZ = child->properties().getZ();
246
247 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
248 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
249 childOp->skipInOrderDraw = true;
250 } else if (!child->properties().getProjectBackwards()) {
251 // regular, in order drawing DisplayList
252 childOp->skipInOrderDraw = false;
253 }
254 }
255
256 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
257 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
258}
259
260template <typename V>
261static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
262 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
263 if (zTranslatedNodes[i].key >= 0.0f) return i;
264 }
265 return zTranslatedNodes.size();
266}
267
268template <typename V>
Chris Craikf158b492016-01-12 14:45:08 -0800269void FrameBuilder::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800270 const int size = zTranslatedNodes.size();
271 if (size == 0
272 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
273 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
274 // no 3d children to draw
275 return;
276 }
277
278 /**
279 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
280 * with very similar Z heights to draw together.
281 *
282 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
283 * underneath both, and neither's shadow is drawn on top of the other.
284 */
285 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
286 size_t drawIndex, shadowIndex, endIndex;
287 if (mode == ChildrenSelectMode::Negative) {
288 drawIndex = 0;
289 endIndex = nonNegativeIndex;
290 shadowIndex = endIndex; // draw no shadows
291 } else {
292 drawIndex = nonNegativeIndex;
293 endIndex = size;
294 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
295 }
296
297 float lastCasterZ = 0.0f;
298 while (shadowIndex < endIndex || drawIndex < endIndex) {
299 if (shadowIndex < endIndex) {
300 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
301 const float casterZ = zTranslatedNodes[shadowIndex].key;
302 // attempt to render the shadow if the caster about to be drawn is its caster,
303 // OR if its caster's Z value is similar to the previous potential caster
304 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
305 deferShadow(*casterNodeOp);
306
307 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
308 shadowIndex++;
309 continue;
310 }
311 }
312
313 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800314 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800315 drawIndex++;
316 }
317}
318
Chris Craikf158b492016-01-12 14:45:08 -0800319void FrameBuilder::deferShadow(const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800320 auto& node = *casterNodeOp.renderNode;
321 auto& properties = node.properties();
322
323 if (properties.getAlpha() <= 0.0f
324 || properties.getOutline().getAlpha() <= 0.0f
325 || !properties.getOutline().getPath()
326 || properties.getScaleX() == 0
327 || properties.getScaleY() == 0) {
328 // no shadow to draw
329 return;
330 }
331
332 const SkPath* casterOutlinePath = properties.getOutline().getPath();
333 const SkPath* revealClipPath = properties.getRevealClip().getPath();
334 if (revealClipPath && revealClipPath->isEmpty()) return;
335
336 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
337
338 // holds temporary SkPath to store the result of intersections
339 SkPath* frameAllocatedPath = nullptr;
340 const SkPath* casterPath = casterOutlinePath;
341
342 // intersect the shadow-casting path with the reveal, if present
343 if (revealClipPath) {
344 frameAllocatedPath = createFrameAllocatedPath();
345
346 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
347 casterPath = frameAllocatedPath;
348 }
349
350 // intersect the shadow-casting path with the clipBounds, if present
351 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
352 if (!frameAllocatedPath) {
353 frameAllocatedPath = createFrameAllocatedPath();
354 }
355 Rect clipBounds;
356 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
357 SkPath clipBoundsPath;
358 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
359 clipBounds.right, clipBounds.bottom);
360
361 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
362 casterPath = frameAllocatedPath;
363 }
364
Chris Craik6e068c012016-01-15 16:15:30 -0800365
366 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
367 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
368 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
369 node.applyViewPropertyTransforms(shadowMatrixXY, false);
370 node.applyViewPropertyTransforms(shadowMatrixZ, true);
371
372 LOG_ALWAYS_FATAL_IF(!mCaches, "Caches needed for shadows");
373 sp<TessellationCache::ShadowTask> task = mCaches->tessellationCache.getShadowTask(
374 mCanvasState.currentTransform(),
375 mCanvasState.getLocalClipBounds(),
376 casterAlpha >= 1.0f,
377 casterPath,
378 &shadowMatrixXY, &shadowMatrixZ,
379 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
380 mLightRadius);
381 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
382 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
383 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
384 if (CC_LIKELY(bakedOpState)) {
385 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
386 }
Chris Craikd3daa312015-11-06 10:59:56 -0800387 }
Chris Craik161f54b2015-11-05 11:08:52 -0800388}
Chris Craikd3daa312015-11-06 10:59:56 -0800389
Chris Craikf158b492016-01-12 14:45:08 -0800390void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800391 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Florin Malitaeecff562015-12-21 10:43:01 -0500392 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik8d1f2122015-11-24 16:40:09 -0800393
394 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
395 const DisplayList& displayList = *(renderNode.getDisplayList());
396
397 const RecordedOp* op = (displayList.getOps()[displayList.projectionReceiveIndex]);
398 const RenderNodeOp* backgroundOp = static_cast<const RenderNodeOp*>(op);
399 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
400
401 // Transform renderer to match background we're projecting onto
402 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
403 mCanvasState.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
404
405 // If the projection receiver has an outline, we mask projected content to it
406 // (which we know, apriori, are all tessellated paths)
407 mCanvasState.setProjectionPathMask(mAllocator, projectionReceiverOutline);
408
409 // draw projected nodes
410 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
411 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
412
Florin Malitaeecff562015-12-21 10:43:01 -0500413 int restoreTo = mCanvasState.save(SaveFlags::Matrix);
Chris Craik8d1f2122015-11-24 16:40:09 -0800414 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
Chris Craik268a9c02015-12-09 18:05:12 -0800415 deferRenderNodeOpImpl(*childOp);
Chris Craik8d1f2122015-11-24 16:40:09 -0800416 mCanvasState.restoreToCount(restoreTo);
417 }
418
419 mCanvasState.restoreToCount(count);
420}
421
Chris Craikb565df12015-10-05 13:00:52 -0700422/**
Chris Craikf158b492016-01-12 14:45:08 -0800423 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700424 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800425 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800426 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700427 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700428#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800429 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
430void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
431 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800432 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800433
434 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
435 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikb36af872015-10-16 14:23:12 -0700436 for (const DisplayList::Chunk& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800437 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
438 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
439
440 defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700441 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700442 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700443 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800444
445 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
446 && displayList.projectionReceiveIndex >= 0
447 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
448 deferProjectedChildren(renderNode);
449 }
Chris Craikb565df12015-10-05 13:00:52 -0700450 }
Chris Craik161f54b2015-11-05 11:08:52 -0800451 defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700452 }
453}
454
Chris Craikf158b492016-01-12 14:45:08 -0800455void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800456 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500457 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700458
Chris Craike4db79d2015-12-22 16:32:23 -0800459 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
460 mCanvasState.writableSnapshot()->mutateClipArea().applyClip(op.localClip,
461 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700462 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700463
Chris Craik0b7e8242015-10-28 16:50:44 -0700464 // then apply state from node properties, and defer ops
465 deferNodePropsAndOps(*op.renderNode);
466
Chris Craik6fe991e52015-10-20 09:39:42 -0700467 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700468}
469
Chris Craikf158b492016-01-12 14:45:08 -0800470void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800471 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800472 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800473 }
474}
475
Chris Craik386aa032015-12-07 17:08:25 -0800476/**
477 * Defers an unmergeable, strokeable op, accounting correctly
478 * for paint's style on the bounds being computed.
479 */
Chris Craikf158b492016-01-12 14:45:08 -0800480void FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800481 BakedOpState::StrokeBehavior strokeBehavior) {
482 // Note: here we account for stroke when baking the op
483 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800484 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik386aa032015-12-07 17:08:25 -0800485 if (!bakedState) return; // quick rejected
486 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
487}
488
489/**
490 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
491 * be used, since they trigger significantly different rendering paths.
492 *
493 * Note: not used for lines/points, since they don't currently support path effects.
494 */
495static batchid_t tessBatchId(const RecordedOp& op) {
496 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700497 return paint.getPathEffect()
498 ? OpBatchType::AlphaMaskTexture
499 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
500}
501
Chris Craikf158b492016-01-12 14:45:08 -0800502void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800503 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800504}
505
Chris Craikb87eadd2016-01-06 09:16:05 -0800506static bool hasMergeableClip(const BakedOpState& state) {
507 return state.computedState.clipState
508 || state.computedState.clipState->mode == ClipMode::Rectangle;
509}
510
Chris Craikf158b492016-01-12 14:45:08 -0800511void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800512 BakedOpState* bakedState = tryBakeOpState(op);
513 if (!bakedState) return; // quick rejected
Chris Craikb565df12015-10-05 13:00:52 -0700514
John Reck14de0412016-01-26 09:01:30 -0800515 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
516
517 // TODO: Fix this ( b/26569206 )
518/*
Chris Craik15c3f192015-12-03 12:16:56 -0800519 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
520 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
521 // MergingDrawBatch::canMergeWith()
522 if (bakedState->computedState.transform.isSimple()
523 && bakedState->computedState.transform.positiveScale()
524 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800525 && op.bitmap->colorType() != kAlpha_8_SkColorType
526 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800527 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800528 // TODO: AssetAtlas in mergeId
529 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
530 } else {
531 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
532 }
John Reck14de0412016-01-26 09:01:30 -0800533*/
Chris Craikb565df12015-10-05 13:00:52 -0700534}
535
Chris Craikf158b492016-01-12 14:45:08 -0800536void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800537 BakedOpState* bakedState = tryBakeOpState(op);
538 if (!bakedState) return; // quick rejected
539 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
540}
541
Chris Craikf158b492016-01-12 14:45:08 -0800542void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800543 BakedOpState* bakedState = tryBakeOpState(op);
544 if (!bakedState) return; // quick rejected
545 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
546}
547
Chris Craikf158b492016-01-12 14:45:08 -0800548void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800549 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
550 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
551 float x = *(op.x);
552 float y = *(op.y);
553 float radius = *(op.radius);
554 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
555 const OvalOp* resolvedOp = new (mAllocator) OvalOp(
556 unmappedBounds,
557 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800558 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800559 op.paint);
560 deferOvalOp(*resolvedOp);
561}
562
Chris Craikf158b492016-01-12 14:45:08 -0800563void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craike29ce6f2015-12-10 16:25:13 -0800564 BakedOpState* bakedState = tryBakeOpState(op);
565 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800566 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800567}
568
Chris Craikf158b492016-01-12 14:45:08 -0800569void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800570 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800571 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800572}
573
Chris Craikf158b492016-01-12 14:45:08 -0800574void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800575 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800576}
577
Chris Craikf158b492016-01-12 14:45:08 -0800578void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800579 BakedOpState* bakedState = tryBakeOpState(op);
580 if (!bakedState) return; // quick rejected
581
582 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800583 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
584 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800585 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800586 // TODO: AssetAtlas in mergeId
587
588 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
589 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
590 } else {
591 // Use Bitmap batchId since Bitmap+Patch use same shader
592 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
593 }
594}
595
Chris Craikf158b492016-01-12 14:45:08 -0800596void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800597 deferStrokeableOp(op, OpBatchType::Bitmap);
Chris Craik386aa032015-12-07 17:08:25 -0800598}
599
Chris Craikf158b492016-01-12 14:45:08 -0800600void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800601 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800602 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800603}
604
Chris Craikf158b492016-01-12 14:45:08 -0800605void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800606 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800607}
608
Chris Craikf158b492016-01-12 14:45:08 -0800609void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800610 deferStrokeableOp(op, tessBatchId(op));
Chris Craikb565df12015-10-05 13:00:52 -0700611}
612
Chris Craikf158b492016-01-12 14:45:08 -0800613void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800614 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
615 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
616 const RoundRectOp* resolvedOp = new (mAllocator) RoundRectOp(
617 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
618 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800619 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800620 op.paint, *op.rx, *op.ry);
621 deferRoundRectOp(*resolvedOp);
622}
623
Chris Craikf158b492016-01-12 14:45:08 -0800624void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800625 BakedOpState* bakedState = tryBakeOpState(op);
626 if (!bakedState) return; // quick rejected
627 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700628}
629
Chris Craikd7448e62015-12-15 10:34:36 -0800630static batchid_t textBatchId(const SkPaint& paint) {
631 // TODO: better handling of shader (since we won't care about color then)
632 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
633}
634
Chris Craikf158b492016-01-12 14:45:08 -0800635void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800636 BakedOpState* bakedState = tryBakeOpState(op);
637 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800638
Chris Craikd7448e62015-12-15 10:34:36 -0800639 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800640 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800641 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
642 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800643 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
644 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
645 } else {
646 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
647 }
Chris Craika1717272015-11-19 13:02:43 -0800648}
649
Chris Craikf158b492016-01-12 14:45:08 -0800650void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craikd7448e62015-12-15 10:34:36 -0800651 BakedOpState* bakedState = tryBakeOpState(op);
652 if (!bakedState) return; // quick rejected
653 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
654}
655
Chris Craikf158b492016-01-12 14:45:08 -0800656void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800657 BakedOpState* bakedState = tryBakeOpState(op);
658 if (!bakedState) return; // quick rejected
659 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
660}
661
Chris Craikf158b492016-01-12 14:45:08 -0800662void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800663 float contentTranslateX, float contentTranslateY,
664 const Rect& repaintRect,
665 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700666 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500667 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700668 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700669 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800670 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800671 mCanvasState.writableSnapshot()->transform->loadTranslate(
672 contentTranslateX, contentTranslateY, 0);
673 mCanvasState.writableSnapshot()->setClip(
674 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800675
Chris Craik8ecf41c2015-11-16 10:27:59 -0800676 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800677 mLayerStack.push_back(mLayerBuilders.size());
678 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800679 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800680 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700681}
682
Chris Craikf158b492016-01-12 14:45:08 -0800683void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700684 // restore canvas, and pop finished layer off of the stack
685 mCanvasState.restore();
686 mLayerStack.pop_back();
687}
688
Chris Craikb87eadd2016-01-06 09:16:05 -0800689// TODO: defer time rejection (when bounds become empty) + tests
690// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800691void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800692 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
693 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
694
695 auto previous = mCanvasState.currentSnapshot();
696 Vector3 lightCenter = previous->getRelativeLightCenter();
697
698 // Combine all transforms used to present saveLayer content:
699 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800700 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800701 contentTransform.multiply(op.localMatrix);
702 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
703
704 Matrix4 inverseContentTransform;
705 inverseContentTransform.loadInverse(contentTransform);
706
707 // map the light center into layer-relative space
708 inverseContentTransform.mapPoint3d(lightCenter);
709
710 // Clip bounds of temporary layer to parent's clip rect, so:
711 Rect saveLayerBounds(layerWidth, layerHeight);
712 // 1) transform Rect(width, height) into parent's space
713 // note: left/top offsets put in contentTransform above
714 contentTransform.mapRect(saveLayerBounds);
715 // 2) intersect with parent's clip
716 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
717 // 3) and transform back
718 inverseContentTransform.mapRect(saveLayerBounds);
719 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
720 saveLayerBounds.roundOut();
721
722 // if bounds are reduced, will clip the layer's area by reducing required bounds...
723 layerWidth = saveLayerBounds.getWidth();
724 layerHeight = saveLayerBounds.getHeight();
725 // ...and shifting drawing content to account for left/top side clipping
726 float contentTranslateX = -saveLayerBounds.left;
727 float contentTranslateY = -saveLayerBounds.top;
728
729 saveForLayer(layerWidth, layerHeight,
730 contentTranslateX, contentTranslateY,
731 Rect(layerWidth, layerHeight),
732 lightCenter,
733 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700734}
Chris Craikb565df12015-10-05 13:00:52 -0700735
Chris Craikf158b492016-01-12 14:45:08 -0800736void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700737 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700738 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700739
740 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700741
742 // record the draw operation into the previous layer's list of draw commands
743 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
744 LayerOp* drawLayerOp = new (mAllocator) LayerOp(
745 beginLayerOp.unmappedBounds,
746 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800747 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700748 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800749 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700750 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
751
752 if (bakedOpState) {
753 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
754 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
755 } else {
756 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800757 // TODO: need to prevent any render work from being done
758 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800759 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700760 return;
Chris Craikb565df12015-10-05 13:00:52 -0700761 }
762}
763
Chris Craikf158b492016-01-12 14:45:08 -0800764void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800765 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
766 boundsTransform.multiply(op.localMatrix);
767
768 Rect dstRect(op.unmappedBounds);
769 boundsTransform.mapRect(dstRect);
770 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
771
772 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
Chris Craik7435eb12016-01-07 17:41:40 -0800773 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800774
775 /**
776 * First, defer an operation to copy out the content from the rendertarget into a layer.
777 */
778 auto copyToOp = new (mAllocator) CopyToLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800779 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
780 &(currentLayer().viewportClip), dstRect, *copyToOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800781 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
782
783 /**
784 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
785 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
786 */
787 currentLayer().deferLayerClear(dstRect);
788
789 /**
790 * And stash an operation to copy that layer back under the rendertarget until
791 * a balanced EndUnclippedLayerOp is seen
792 */
793 auto copyFromOp = new (mAllocator) CopyFromLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800794 bakedState = BakedOpState::directConstruct(mAllocator,
795 &(currentLayer().viewportClip), dstRect, *copyFromOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800796 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
797}
798
Chris Craikf158b492016-01-12 14:45:08 -0800799void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800800 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
801
802 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
803 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
804 currentLayer().activeUnclippedSaveLayers.pop_back();
805}
806
Chris Craikb565df12015-10-05 13:00:52 -0700807} // namespace uirenderer
808} // namespace android