blob: 4740e1f13bdac940faf3e33954f375ac47053c3b [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"
Doris Liuf276acd2016-01-07 13:49:26 -080022#include "VectorDrawable.h"
Chris Craik98787e62015-11-13 10:55:30 -080023#include "renderstate/OffscreenBufferPool.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 Craik98787e62015-11-13 10:55:30 -080036 const std::vector< sp<RenderNode> >& nodes, const Vector3& lightCenter)
Chong Zhangc3bd5682016-01-25 12:01:12 -080037 : FrameBuilder(layers, clip, viewportWidth, viewportHeight, nodes, lightCenter,
38 Rect(0, 0, 0, 0)) {
39}
40
41
42FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip,
43 uint32_t viewportWidth, uint32_t viewportHeight,
44 const std::vector< sp<RenderNode> >& nodes, const Vector3& lightCenter,
45 const Rect &contentDrawBounds)
Chris Craik6fe991e52015-10-20 09:39:42 -070046 : mCanvasState(*this) {
Chris Craik818c9fb2015-10-23 14:33:42 -070047 ATRACE_NAME("prepare drawing commands");
Chris Craikb565df12015-10-05 13:00:52 -070048
Chris Craikf158b492016-01-12 14:45:08 -080049 mLayerBuilders.reserve(layers.entries().size());
Chris Craik98787e62015-11-13 10:55:30 -080050 mLayerStack.reserve(layers.entries().size());
51
52 // Prepare to defer Fbo0
Chris Craikf158b492016-01-12 14:45:08 -080053 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
54 mLayerBuilders.push_back(fbo0);
Chris Craik98787e62015-11-13 10:55:30 -080055 mLayerStack.push_back(0);
Chris Craikb565df12015-10-05 13:00:52 -070056 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -070057 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
Chris Craik98787e62015-11-13 10:55:30 -080058 lightCenter);
Chris Craik0b7e8242015-10-28 16:50:44 -070059
60 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
Chris Craikf158b492016-01-12 14:45:08 -080061 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
Chris Craik0b7e8242015-10-28 16:50:44 -070062 for (int i = layers.entries().size() - 1; i >= 0; i--) {
63 RenderNode* layerNode = layers.entries()[i].renderNode;
Chris Craike9c5fd82016-01-12 18:59:38 -080064 // only schedule repaint if node still on layer - possible it may have been
65 // removed during a dropped frame, but layers may still remain scheduled so
66 // as not to lose info on what portion is damaged
67 if (CC_LIKELY(layerNode->getLayer() != nullptr)) {
68 const Rect& layerDamage = layers.entries()[i].damage;
69 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070070
Chris Craike9c5fd82016-01-12 18:59:38 -080071 // map current light center into RenderNode's coordinate space
72 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
73 layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080074
Chris Craike9c5fd82016-01-12 18:59:38 -080075 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
76 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070077
Chris Craike9c5fd82016-01-12 18:59:38 -080078 if (layerNode->getDisplayList()) {
79 deferNodeOps(*layerNode);
80 }
81 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -070082 }
Chris Craik0b7e8242015-10-28 16:50:44 -070083 }
84
Chong Zhangc3bd5682016-01-25 12:01:12 -080085 // It there are multiple render nodes, they are laid out as follows:
86 // #0 - backdrop (content + caption)
87 // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
88 // #2 - additional overlay nodes
89 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
90 // resizing however it might become partially visible. The following render loop will crop the
91 // backdrop against the content and draw the remaining part of it. It will then draw the content
92 // cropped to the backdrop (since that indicates a shrinking of the window).
93 //
94 // Additional nodes will be drawn on top with no particular clipping semantics.
95
96 // The bounds of the backdrop against which the content should be clipped.
97 Rect backdropBounds = contentDrawBounds;
98 // Usually the contents bounds should be mContentDrawBounds - however - we will
99 // move it towards the fixed edge to give it a more stable appearance (for the moment).
100 // If there is no content bounds we ignore the layering as stated above and start with 2.
101 int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0;
102
Chris Craikb565df12015-10-05 13:00:52 -0700103 for (const sp<RenderNode>& node : nodes) {
104 if (node->nothingToDraw()) continue;
Chris Craik8d1f2122015-11-24 16:40:09 -0800105 node->computeOrdering();
Florin Malitaeecff562015-12-21 10:43:01 -0500106 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800107
108 if (layer == 0) {
109 const RenderProperties& properties = node->properties();
110 Rect targetBounds(properties.getLeft(), properties.getTop(),
111 properties.getRight(), properties.getBottom());
112 // Move the content bounds towards the fixed corner of the backdrop.
113 const int x = targetBounds.left;
114 const int y = targetBounds.top;
115 // Remember the intersection of the target bounds and the intersection bounds against
116 // which we have to crop the content.
117 backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
118 backdropBounds.doIntersect(targetBounds);
119 } else if (layer == 1) {
120 // We shift and clip the content to match its final location in the window.
121 const float left = contentDrawBounds.left;
122 const float top = contentDrawBounds.top;
123 const float dx = backdropBounds.left - left;
124 const float dy = backdropBounds.top - top;
125 const float width = backdropBounds.getWidth();
126 const float height = backdropBounds.getHeight();
127 mCanvasState.translate(dx, dy);
128 // It gets cropped against the bounds of the backdrop to stay inside.
129 mCanvasState.clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op);
130 }
131
Chris Craik0b7e8242015-10-28 16:50:44 -0700132 deferNodePropsAndOps(*node);
133 mCanvasState.restoreToCount(count);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800134 layer++;
Chris Craikb565df12015-10-05 13:00:52 -0700135 }
136}
137
Chris Craikf158b492016-01-12 14:45:08 -0800138void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700139
Chris Craikf158b492016-01-12 14:45:08 -0800140void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700141
Chris Craikf158b492016-01-12 14:45:08 -0800142void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800143 const RenderProperties& properties = node.properties();
144 const Outline& outline = properties.getOutline();
145 if (properties.getAlpha() <= 0
146 || (outline.getShouldClip() && outline.isEmpty())
147 || properties.getScaleX() == 0
148 || properties.getScaleY() == 0) {
149 return; // rejected
150 }
151
152 if (properties.getLeft() != 0 || properties.getTop() != 0) {
153 mCanvasState.translate(properties.getLeft(), properties.getTop());
154 }
155 if (properties.getStaticMatrix()) {
156 mCanvasState.concatMatrix(*properties.getStaticMatrix());
157 } else if (properties.getAnimationMatrix()) {
158 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
159 }
160 if (properties.hasTransformMatrix()) {
161 if (properties.isTransformTranslateOnly()) {
162 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
163 } else {
164 mCanvasState.concatMatrix(*properties.getTransformMatrix());
165 }
166 }
167
168 const int width = properties.getWidth();
169 const int height = properties.getHeight();
170
171 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
172 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
173 int clipFlags = properties.getClippingFlags();
174 if (properties.getAlpha() < 1) {
175 if (isLayer) {
176 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
177 }
178 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
179 // simply scale rendering content's alpha
180 mCanvasState.scaleAlpha(properties.getAlpha());
181 } else {
182 // schedule saveLayer by initializing saveLayerBounds
183 saveLayerBounds.set(0, 0, width, height);
184 if (clipFlags) {
185 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
186 clipFlags = 0; // all clipping done by savelayer
187 }
188 }
189
190 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
191 // pretend alpha always causes savelayer to warn about
192 // performance problem affecting old versions
193 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
194 }
195 }
196 if (clipFlags) {
197 Rect clipRect;
198 properties.getClippingRectForFlags(clipFlags, &clipRect);
199 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
200 SkRegion::kIntersect_Op);
201 }
202
203 if (properties.getRevealClip().willClip()) {
204 Rect bounds;
205 properties.getRevealClip().getBounds(&bounds);
206 mCanvasState.setClippingRoundRect(mAllocator,
207 bounds, properties.getRevealClip().getRadius());
208 } else if (properties.getOutline().willClip()) {
209 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
210 }
211
212 if (!mCanvasState.quickRejectConservative(0, 0, width, height)) {
213 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700214 if (node.getLayer()) {
215 // HW layer
216 LayerOp* drawLayerOp = new (mAllocator) LayerOp(node);
217 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
218 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800219 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700220 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
221 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800222 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
223 // draw DisplayList contents within temporary, since persisted layer could not be used.
224 // (temp layers are clipped to viewport, since they don't persist offscreen content)
225 SkPaint saveLayerPaint;
226 saveLayerPaint.setAlpha(properties.getAlpha());
Chris Craik268a9c02015-12-09 18:05:12 -0800227 deferBeginLayerOp(*new (mAllocator) BeginLayerOp(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800228 saveLayerBounds,
229 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800230 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800231 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800232 deferNodeOps(node);
Chris Craik268a9c02015-12-09 18:05:12 -0800233 deferEndLayerOp(*new (mAllocator) EndLayerOp());
Chris Craik0b7e8242015-10-28 16:50:44 -0700234 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800235 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700236 }
237 }
238}
239
Chris Craik161f54b2015-11-05 11:08:52 -0800240typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
241
242template <typename V>
243static void buildZSortedChildList(V* zTranslatedNodes,
244 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
245 if (chunk.beginChildIndex == chunk.endChildIndex) return;
246
247 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
248 RenderNodeOp* childOp = displayList.getChildren()[i];
249 RenderNode* child = childOp->renderNode;
250 float childZ = child->properties().getZ();
251
252 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
253 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
254 childOp->skipInOrderDraw = true;
255 } else if (!child->properties().getProjectBackwards()) {
256 // regular, in order drawing DisplayList
257 childOp->skipInOrderDraw = false;
258 }
259 }
260
261 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
262 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
263}
264
265template <typename V>
266static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
267 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
268 if (zTranslatedNodes[i].key >= 0.0f) return i;
269 }
270 return zTranslatedNodes.size();
271}
272
273template <typename V>
Chris Craikf158b492016-01-12 14:45:08 -0800274void FrameBuilder::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800275 const int size = zTranslatedNodes.size();
276 if (size == 0
277 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
278 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
279 // no 3d children to draw
280 return;
281 }
282
283 /**
284 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
285 * with very similar Z heights to draw together.
286 *
287 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
288 * underneath both, and neither's shadow is drawn on top of the other.
289 */
290 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
291 size_t drawIndex, shadowIndex, endIndex;
292 if (mode == ChildrenSelectMode::Negative) {
293 drawIndex = 0;
294 endIndex = nonNegativeIndex;
295 shadowIndex = endIndex; // draw no shadows
296 } else {
297 drawIndex = nonNegativeIndex;
298 endIndex = size;
299 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
300 }
301
302 float lastCasterZ = 0.0f;
303 while (shadowIndex < endIndex || drawIndex < endIndex) {
304 if (shadowIndex < endIndex) {
305 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
306 const float casterZ = zTranslatedNodes[shadowIndex].key;
307 // attempt to render the shadow if the caster about to be drawn is its caster,
308 // OR if its caster's Z value is similar to the previous potential caster
309 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
310 deferShadow(*casterNodeOp);
311
312 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
313 shadowIndex++;
314 continue;
315 }
316 }
317
318 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800319 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800320 drawIndex++;
321 }
322}
323
Chris Craikf158b492016-01-12 14:45:08 -0800324void FrameBuilder::deferShadow(const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800325 auto& node = *casterNodeOp.renderNode;
326 auto& properties = node.properties();
327
328 if (properties.getAlpha() <= 0.0f
329 || properties.getOutline().getAlpha() <= 0.0f
330 || !properties.getOutline().getPath()
331 || properties.getScaleX() == 0
332 || properties.getScaleY() == 0) {
333 // no shadow to draw
334 return;
335 }
336
337 const SkPath* casterOutlinePath = properties.getOutline().getPath();
338 const SkPath* revealClipPath = properties.getRevealClip().getPath();
339 if (revealClipPath && revealClipPath->isEmpty()) return;
340
341 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
342
343 // holds temporary SkPath to store the result of intersections
344 SkPath* frameAllocatedPath = nullptr;
345 const SkPath* casterPath = casterOutlinePath;
346
347 // intersect the shadow-casting path with the reveal, if present
348 if (revealClipPath) {
349 frameAllocatedPath = createFrameAllocatedPath();
350
351 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
352 casterPath = frameAllocatedPath;
353 }
354
355 // intersect the shadow-casting path with the clipBounds, if present
356 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
357 if (!frameAllocatedPath) {
358 frameAllocatedPath = createFrameAllocatedPath();
359 }
360 Rect clipBounds;
361 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
362 SkPath clipBoundsPath;
363 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
364 clipBounds.right, clipBounds.bottom);
365
366 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
367 casterPath = frameAllocatedPath;
368 }
369
370 ShadowOp* shadowOp = new (mAllocator) ShadowOp(casterNodeOp, casterAlpha, casterPath,
Chris Craik98787e62015-11-13 10:55:30 -0800371 mCanvasState.getLocalClipBounds(),
372 mCanvasState.currentSnapshot()->getRelativeLightCenter());
Chris Craikd3daa312015-11-06 10:59:56 -0800373 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800374 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
Chris Craikd3daa312015-11-06 10:59:56 -0800375 if (CC_LIKELY(bakedOpState)) {
376 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
377 }
Chris Craik161f54b2015-11-05 11:08:52 -0800378}
Chris Craikd3daa312015-11-06 10:59:56 -0800379
Chris Craikf158b492016-01-12 14:45:08 -0800380void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800381 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Florin Malitaeecff562015-12-21 10:43:01 -0500382 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik8d1f2122015-11-24 16:40:09 -0800383
384 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
385 const DisplayList& displayList = *(renderNode.getDisplayList());
386
387 const RecordedOp* op = (displayList.getOps()[displayList.projectionReceiveIndex]);
388 const RenderNodeOp* backgroundOp = static_cast<const RenderNodeOp*>(op);
389 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
390
391 // Transform renderer to match background we're projecting onto
392 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
393 mCanvasState.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
394
395 // If the projection receiver has an outline, we mask projected content to it
396 // (which we know, apriori, are all tessellated paths)
397 mCanvasState.setProjectionPathMask(mAllocator, projectionReceiverOutline);
398
399 // draw projected nodes
400 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
401 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
402
Florin Malitaeecff562015-12-21 10:43:01 -0500403 int restoreTo = mCanvasState.save(SaveFlags::Matrix);
Chris Craik8d1f2122015-11-24 16:40:09 -0800404 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
Chris Craik268a9c02015-12-09 18:05:12 -0800405 deferRenderNodeOpImpl(*childOp);
Chris Craik8d1f2122015-11-24 16:40:09 -0800406 mCanvasState.restoreToCount(restoreTo);
407 }
408
409 mCanvasState.restoreToCount(count);
410}
411
Chris Craikb565df12015-10-05 13:00:52 -0700412/**
Chris Craikf158b492016-01-12 14:45:08 -0800413 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700414 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800415 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800416 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700417 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700418#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800419 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
420void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
421 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800422 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800423
424 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
425 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikb36af872015-10-16 14:23:12 -0700426 for (const DisplayList::Chunk& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800427 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
428 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
429
430 defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700431 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700432 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700433 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800434
435 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
436 && displayList.projectionReceiveIndex >= 0
437 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
438 deferProjectedChildren(renderNode);
439 }
Chris Craikb565df12015-10-05 13:00:52 -0700440 }
Chris Craik161f54b2015-11-05 11:08:52 -0800441 defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700442 }
443}
444
Chris Craikf158b492016-01-12 14:45:08 -0800445void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800446 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500447 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700448
Chris Craike4db79d2015-12-22 16:32:23 -0800449 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
450 mCanvasState.writableSnapshot()->mutateClipArea().applyClip(op.localClip,
451 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700452 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700453
Chris Craik0b7e8242015-10-28 16:50:44 -0700454 // then apply state from node properties, and defer ops
455 deferNodePropsAndOps(*op.renderNode);
456
Chris Craik6fe991e52015-10-20 09:39:42 -0700457 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700458}
459
Chris Craikf158b492016-01-12 14:45:08 -0800460void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800461 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800462 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800463 }
464}
465
Chris Craik386aa032015-12-07 17:08:25 -0800466/**
467 * Defers an unmergeable, strokeable op, accounting correctly
468 * for paint's style on the bounds being computed.
469 */
Chris Craikf158b492016-01-12 14:45:08 -0800470void FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800471 BakedOpState::StrokeBehavior strokeBehavior) {
472 // Note: here we account for stroke when baking the op
473 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800474 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik386aa032015-12-07 17:08:25 -0800475 if (!bakedState) return; // quick rejected
476 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
477}
478
479/**
480 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
481 * be used, since they trigger significantly different rendering paths.
482 *
483 * Note: not used for lines/points, since they don't currently support path effects.
484 */
485static batchid_t tessBatchId(const RecordedOp& op) {
486 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700487 return paint.getPathEffect()
488 ? OpBatchType::AlphaMaskTexture
489 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
490}
491
Chris Craikf158b492016-01-12 14:45:08 -0800492void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800493 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800494}
495
Chris Craikb87eadd2016-01-06 09:16:05 -0800496static bool hasMergeableClip(const BakedOpState& state) {
497 return state.computedState.clipState
498 || state.computedState.clipState->mode == ClipMode::Rectangle;
499}
500
Chris Craikf158b492016-01-12 14:45:08 -0800501void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800502 BakedOpState* bakedState = tryBakeOpState(op);
503 if (!bakedState) return; // quick rejected
Chris Craikb565df12015-10-05 13:00:52 -0700504
John Reck14de0412016-01-26 09:01:30 -0800505 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
506
507 // TODO: Fix this ( b/26569206 )
508/*
Chris Craik15c3f192015-12-03 12:16:56 -0800509 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
510 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
511 // MergingDrawBatch::canMergeWith()
512 if (bakedState->computedState.transform.isSimple()
513 && bakedState->computedState.transform.positiveScale()
514 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800515 && op.bitmap->colorType() != kAlpha_8_SkColorType
516 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800517 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800518 // TODO: AssetAtlas in mergeId
519 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
520 } else {
521 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
522 }
John Reck14de0412016-01-26 09:01:30 -0800523*/
Chris Craikb565df12015-10-05 13:00:52 -0700524}
525
Chris Craikf158b492016-01-12 14:45:08 -0800526void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800527 BakedOpState* bakedState = tryBakeOpState(op);
528 if (!bakedState) return; // quick rejected
529 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
530}
531
Chris Craikf158b492016-01-12 14:45:08 -0800532void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800533 BakedOpState* bakedState = tryBakeOpState(op);
534 if (!bakedState) return; // quick rejected
535 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
536}
537
Doris Liuf276acd2016-01-07 13:49:26 -0800538void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
539 const SkBitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
540 SkPaint* paint = op.vectorDrawable->getPaint();
541 const BitmapRectOp* resolvedOp = new (mAllocator) BitmapRectOp(op.unmappedBounds,
542 op.localMatrix,
543 op.localClip,
544 paint,
545 &bitmap,
546 Rect(bitmap.width(), bitmap.height()));
547 deferBitmapRectOp(*resolvedOp);
548}
549
Chris Craikf158b492016-01-12 14:45:08 -0800550void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800551 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
552 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
553 float x = *(op.x);
554 float y = *(op.y);
555 float radius = *(op.radius);
556 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
557 const OvalOp* resolvedOp = new (mAllocator) OvalOp(
558 unmappedBounds,
559 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800560 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800561 op.paint);
562 deferOvalOp(*resolvedOp);
563}
564
Chris Craikf158b492016-01-12 14:45:08 -0800565void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craike29ce6f2015-12-10 16:25:13 -0800566 BakedOpState* bakedState = tryBakeOpState(op);
567 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800568 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800569}
570
Chris Craikf158b492016-01-12 14:45:08 -0800571void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800572 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800573 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800574}
575
Chris Craikf158b492016-01-12 14:45:08 -0800576void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800577 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800578}
579
Chris Craikf158b492016-01-12 14:45:08 -0800580void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800581 BakedOpState* bakedState = tryBakeOpState(op);
582 if (!bakedState) return; // quick rejected
583
584 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800585 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
586 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800587 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800588 // TODO: AssetAtlas in mergeId
589
590 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
591 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
592 } else {
593 // Use Bitmap batchId since Bitmap+Patch use same shader
594 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
595 }
596}
597
Chris Craikf158b492016-01-12 14:45:08 -0800598void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800599 deferStrokeableOp(op, OpBatchType::Bitmap);
Chris Craik386aa032015-12-07 17:08:25 -0800600}
601
Chris Craikf158b492016-01-12 14:45:08 -0800602void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800603 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800604 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800605}
606
Chris Craikf158b492016-01-12 14:45:08 -0800607void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800608 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800609}
610
Chris Craikf158b492016-01-12 14:45:08 -0800611void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800612 deferStrokeableOp(op, tessBatchId(op));
Chris Craikb565df12015-10-05 13:00:52 -0700613}
614
Chris Craikf158b492016-01-12 14:45:08 -0800615void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800616 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
617 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
618 const RoundRectOp* resolvedOp = new (mAllocator) RoundRectOp(
619 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
620 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800621 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800622 op.paint, *op.rx, *op.ry);
623 deferRoundRectOp(*resolvedOp);
624}
625
Chris Craikf158b492016-01-12 14:45:08 -0800626void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800627 BakedOpState* bakedState = tryBakeOpState(op);
628 if (!bakedState) return; // quick rejected
629 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700630}
631
Chris Craikd7448e62015-12-15 10:34:36 -0800632static batchid_t textBatchId(const SkPaint& paint) {
633 // TODO: better handling of shader (since we won't care about color then)
634 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
635}
636
Chris Craikf158b492016-01-12 14:45:08 -0800637void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800638 BakedOpState* bakedState = tryBakeOpState(op);
639 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800640
Chris Craikd7448e62015-12-15 10:34:36 -0800641 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800642 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800643 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
644 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800645 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
646 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
647 } else {
648 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
649 }
Chris Craika1717272015-11-19 13:02:43 -0800650}
651
Chris Craikf158b492016-01-12 14:45:08 -0800652void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craikd7448e62015-12-15 10:34:36 -0800653 BakedOpState* bakedState = tryBakeOpState(op);
654 if (!bakedState) return; // quick rejected
655 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
656}
657
Chris Craikf158b492016-01-12 14:45:08 -0800658void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800659 BakedOpState* bakedState = tryBakeOpState(op);
660 if (!bakedState) return; // quick rejected
661 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
662}
663
Chris Craikf158b492016-01-12 14:45:08 -0800664void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800665 float contentTranslateX, float contentTranslateY,
666 const Rect& repaintRect,
667 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700668 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500669 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700670 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700671 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800672 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800673 mCanvasState.writableSnapshot()->transform->loadTranslate(
674 contentTranslateX, contentTranslateY, 0);
675 mCanvasState.writableSnapshot()->setClip(
676 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800677
Chris Craik8ecf41c2015-11-16 10:27:59 -0800678 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800679 mLayerStack.push_back(mLayerBuilders.size());
680 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800681 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800682 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700683}
684
Chris Craikf158b492016-01-12 14:45:08 -0800685void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700686 // restore canvas, and pop finished layer off of the stack
687 mCanvasState.restore();
688 mLayerStack.pop_back();
689}
690
Chris Craikb87eadd2016-01-06 09:16:05 -0800691// TODO: defer time rejection (when bounds become empty) + tests
692// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800693void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800694 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
695 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
696
697 auto previous = mCanvasState.currentSnapshot();
698 Vector3 lightCenter = previous->getRelativeLightCenter();
699
700 // Combine all transforms used to present saveLayer content:
701 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800702 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800703 contentTransform.multiply(op.localMatrix);
704 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
705
706 Matrix4 inverseContentTransform;
707 inverseContentTransform.loadInverse(contentTransform);
708
709 // map the light center into layer-relative space
710 inverseContentTransform.mapPoint3d(lightCenter);
711
712 // Clip bounds of temporary layer to parent's clip rect, so:
713 Rect saveLayerBounds(layerWidth, layerHeight);
714 // 1) transform Rect(width, height) into parent's space
715 // note: left/top offsets put in contentTransform above
716 contentTransform.mapRect(saveLayerBounds);
717 // 2) intersect with parent's clip
718 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
719 // 3) and transform back
720 inverseContentTransform.mapRect(saveLayerBounds);
721 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
722 saveLayerBounds.roundOut();
723
724 // if bounds are reduced, will clip the layer's area by reducing required bounds...
725 layerWidth = saveLayerBounds.getWidth();
726 layerHeight = saveLayerBounds.getHeight();
727 // ...and shifting drawing content to account for left/top side clipping
728 float contentTranslateX = -saveLayerBounds.left;
729 float contentTranslateY = -saveLayerBounds.top;
730
731 saveForLayer(layerWidth, layerHeight,
732 contentTranslateX, contentTranslateY,
733 Rect(layerWidth, layerHeight),
734 lightCenter,
735 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700736}
Chris Craikb565df12015-10-05 13:00:52 -0700737
Chris Craikf158b492016-01-12 14:45:08 -0800738void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700739 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700740 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700741
742 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700743
744 // record the draw operation into the previous layer's list of draw commands
745 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
746 LayerOp* drawLayerOp = new (mAllocator) LayerOp(
747 beginLayerOp.unmappedBounds,
748 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800749 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700750 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800751 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700752 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
753
754 if (bakedOpState) {
755 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
756 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
757 } else {
758 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800759 // TODO: need to prevent any render work from being done
760 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800761 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700762 return;
Chris Craikb565df12015-10-05 13:00:52 -0700763 }
764}
765
Chris Craikf158b492016-01-12 14:45:08 -0800766void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800767 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
768 boundsTransform.multiply(op.localMatrix);
769
770 Rect dstRect(op.unmappedBounds);
771 boundsTransform.mapRect(dstRect);
772 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
773
774 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
Chris Craik7435eb12016-01-07 17:41:40 -0800775 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800776
777 /**
778 * First, defer an operation to copy out the content from the rendertarget into a layer.
779 */
780 auto copyToOp = new (mAllocator) CopyToLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800781 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
782 &(currentLayer().viewportClip), dstRect, *copyToOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800783 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
784
785 /**
786 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
787 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
788 */
789 currentLayer().deferLayerClear(dstRect);
790
791 /**
792 * And stash an operation to copy that layer back under the rendertarget until
793 * a balanced EndUnclippedLayerOp is seen
794 */
795 auto copyFromOp = new (mAllocator) CopyFromLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800796 bakedState = BakedOpState::directConstruct(mAllocator,
797 &(currentLayer().viewportClip), dstRect, *copyFromOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800798 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
799}
800
Chris Craikf158b492016-01-12 14:45:08 -0800801void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800802 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
803
804 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
805 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
806 currentLayer().activeUnclippedSaveLayers.pop_back();
807}
808
Chris Craikb565df12015-10-05 13:00:52 -0700809} // namespace uirenderer
810} // namespace android