blob: 57e5b9d8735a83b636b374968a4ea212cf8aa367 [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 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
206 if (!mCanvasState.quickRejectConservative(0, 0, width, height)) {
207 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700208 if (node.getLayer()) {
209 // HW layer
210 LayerOp* drawLayerOp = new (mAllocator) LayerOp(node);
211 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
212 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800213 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700214 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
215 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800216 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
217 // draw DisplayList contents within temporary, since persisted layer could not be used.
218 // (temp layers are clipped to viewport, since they don't persist offscreen content)
219 SkPaint saveLayerPaint;
220 saveLayerPaint.setAlpha(properties.getAlpha());
Chris Craik268a9c02015-12-09 18:05:12 -0800221 deferBeginLayerOp(*new (mAllocator) BeginLayerOp(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800222 saveLayerBounds,
223 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800224 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800225 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800226 deferNodeOps(node);
Chris Craik268a9c02015-12-09 18:05:12 -0800227 deferEndLayerOp(*new (mAllocator) EndLayerOp());
Chris Craik0b7e8242015-10-28 16:50:44 -0700228 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800229 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700230 }
231 }
232}
233
Chris Craik161f54b2015-11-05 11:08:52 -0800234typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
235
236template <typename V>
237static void buildZSortedChildList(V* zTranslatedNodes,
238 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
239 if (chunk.beginChildIndex == chunk.endChildIndex) return;
240
241 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
242 RenderNodeOp* childOp = displayList.getChildren()[i];
243 RenderNode* child = childOp->renderNode;
244 float childZ = child->properties().getZ();
245
246 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
247 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
248 childOp->skipInOrderDraw = true;
249 } else if (!child->properties().getProjectBackwards()) {
250 // regular, in order drawing DisplayList
251 childOp->skipInOrderDraw = false;
252 }
253 }
254
255 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
256 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
257}
258
259template <typename V>
260static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
261 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
262 if (zTranslatedNodes[i].key >= 0.0f) return i;
263 }
264 return zTranslatedNodes.size();
265}
266
267template <typename V>
Chris Craikf158b492016-01-12 14:45:08 -0800268void FrameBuilder::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800269 const int size = zTranslatedNodes.size();
270 if (size == 0
271 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
272 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
273 // no 3d children to draw
274 return;
275 }
276
277 /**
278 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
279 * with very similar Z heights to draw together.
280 *
281 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
282 * underneath both, and neither's shadow is drawn on top of the other.
283 */
284 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
285 size_t drawIndex, shadowIndex, endIndex;
286 if (mode == ChildrenSelectMode::Negative) {
287 drawIndex = 0;
288 endIndex = nonNegativeIndex;
289 shadowIndex = endIndex; // draw no shadows
290 } else {
291 drawIndex = nonNegativeIndex;
292 endIndex = size;
293 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
294 }
295
296 float lastCasterZ = 0.0f;
297 while (shadowIndex < endIndex || drawIndex < endIndex) {
298 if (shadowIndex < endIndex) {
299 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
300 const float casterZ = zTranslatedNodes[shadowIndex].key;
301 // attempt to render the shadow if the caster about to be drawn is its caster,
302 // OR if its caster's Z value is similar to the previous potential caster
303 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
304 deferShadow(*casterNodeOp);
305
306 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
307 shadowIndex++;
308 continue;
309 }
310 }
311
312 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800313 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800314 drawIndex++;
315 }
316}
317
Chris Craikf158b492016-01-12 14:45:08 -0800318void FrameBuilder::deferShadow(const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800319 auto& node = *casterNodeOp.renderNode;
320 auto& properties = node.properties();
321
322 if (properties.getAlpha() <= 0.0f
323 || properties.getOutline().getAlpha() <= 0.0f
324 || !properties.getOutline().getPath()
325 || properties.getScaleX() == 0
326 || properties.getScaleY() == 0) {
327 // no shadow to draw
328 return;
329 }
330
331 const SkPath* casterOutlinePath = properties.getOutline().getPath();
332 const SkPath* revealClipPath = properties.getRevealClip().getPath();
333 if (revealClipPath && revealClipPath->isEmpty()) return;
334
335 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
336
337 // holds temporary SkPath to store the result of intersections
338 SkPath* frameAllocatedPath = nullptr;
339 const SkPath* casterPath = casterOutlinePath;
340
341 // intersect the shadow-casting path with the reveal, if present
342 if (revealClipPath) {
343 frameAllocatedPath = createFrameAllocatedPath();
344
345 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
346 casterPath = frameAllocatedPath;
347 }
348
349 // intersect the shadow-casting path with the clipBounds, if present
350 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
351 if (!frameAllocatedPath) {
352 frameAllocatedPath = createFrameAllocatedPath();
353 }
354 Rect clipBounds;
355 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
356 SkPath clipBoundsPath;
357 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
358 clipBounds.right, clipBounds.bottom);
359
360 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
361 casterPath = frameAllocatedPath;
362 }
363
Chris Craik6e068c012016-01-15 16:15:30 -0800364
365 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
366 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
367 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
368 node.applyViewPropertyTransforms(shadowMatrixXY, false);
369 node.applyViewPropertyTransforms(shadowMatrixZ, true);
370
371 LOG_ALWAYS_FATAL_IF(!mCaches, "Caches needed for shadows");
372 sp<TessellationCache::ShadowTask> task = mCaches->tessellationCache.getShadowTask(
373 mCanvasState.currentTransform(),
374 mCanvasState.getLocalClipBounds(),
375 casterAlpha >= 1.0f,
376 casterPath,
377 &shadowMatrixXY, &shadowMatrixZ,
378 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
379 mLightRadius);
380 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
381 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
382 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
383 if (CC_LIKELY(bakedOpState)) {
384 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
385 }
Chris Craikd3daa312015-11-06 10:59:56 -0800386 }
Chris Craik161f54b2015-11-05 11:08:52 -0800387}
Chris Craikd3daa312015-11-06 10:59:56 -0800388
Chris Craikf158b492016-01-12 14:45:08 -0800389void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800390 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Florin Malitaeecff562015-12-21 10:43:01 -0500391 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik8d1f2122015-11-24 16:40:09 -0800392
393 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
394 const DisplayList& displayList = *(renderNode.getDisplayList());
395
396 const RecordedOp* op = (displayList.getOps()[displayList.projectionReceiveIndex]);
397 const RenderNodeOp* backgroundOp = static_cast<const RenderNodeOp*>(op);
398 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
399
400 // Transform renderer to match background we're projecting onto
401 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
402 mCanvasState.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
403
404 // If the projection receiver has an outline, we mask projected content to it
405 // (which we know, apriori, are all tessellated paths)
406 mCanvasState.setProjectionPathMask(mAllocator, projectionReceiverOutline);
407
408 // draw projected nodes
409 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
410 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
411
Florin Malitaeecff562015-12-21 10:43:01 -0500412 int restoreTo = mCanvasState.save(SaveFlags::Matrix);
Chris Craik8d1f2122015-11-24 16:40:09 -0800413 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
Chris Craik268a9c02015-12-09 18:05:12 -0800414 deferRenderNodeOpImpl(*childOp);
Chris Craik8d1f2122015-11-24 16:40:09 -0800415 mCanvasState.restoreToCount(restoreTo);
416 }
417
418 mCanvasState.restoreToCount(count);
419}
420
Chris Craikb565df12015-10-05 13:00:52 -0700421/**
Chris Craikf158b492016-01-12 14:45:08 -0800422 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700423 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800424 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800425 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700426 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700427#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800428 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
429void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
430 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800431 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800432
433 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
434 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikb36af872015-10-16 14:23:12 -0700435 for (const DisplayList::Chunk& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800436 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
437 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
438
439 defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700440 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700441 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700442 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800443
444 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
445 && displayList.projectionReceiveIndex >= 0
446 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
447 deferProjectedChildren(renderNode);
448 }
Chris Craikb565df12015-10-05 13:00:52 -0700449 }
Chris Craik161f54b2015-11-05 11:08:52 -0800450 defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700451 }
452}
453
Chris Craikf158b492016-01-12 14:45:08 -0800454void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800455 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500456 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700457
Chris Craike4db79d2015-12-22 16:32:23 -0800458 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
459 mCanvasState.writableSnapshot()->mutateClipArea().applyClip(op.localClip,
460 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700461 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700462
Chris Craik0b7e8242015-10-28 16:50:44 -0700463 // then apply state from node properties, and defer ops
464 deferNodePropsAndOps(*op.renderNode);
465
Chris Craik6fe991e52015-10-20 09:39:42 -0700466 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700467}
468
Chris Craikf158b492016-01-12 14:45:08 -0800469void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800470 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800471 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800472 }
473}
474
Chris Craik386aa032015-12-07 17:08:25 -0800475/**
476 * Defers an unmergeable, strokeable op, accounting correctly
477 * for paint's style on the bounds being computed.
478 */
Chris Craikf158b492016-01-12 14:45:08 -0800479void FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800480 BakedOpState::StrokeBehavior strokeBehavior) {
481 // Note: here we account for stroke when baking the op
482 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800483 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik386aa032015-12-07 17:08:25 -0800484 if (!bakedState) return; // quick rejected
485 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
486}
487
488/**
489 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
490 * be used, since they trigger significantly different rendering paths.
491 *
492 * Note: not used for lines/points, since they don't currently support path effects.
493 */
494static batchid_t tessBatchId(const RecordedOp& op) {
495 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700496 return paint.getPathEffect()
497 ? OpBatchType::AlphaMaskTexture
498 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
499}
500
Chris Craikf158b492016-01-12 14:45:08 -0800501void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800502 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800503}
504
Chris Craikb87eadd2016-01-06 09:16:05 -0800505static bool hasMergeableClip(const BakedOpState& state) {
506 return state.computedState.clipState
507 || state.computedState.clipState->mode == ClipMode::Rectangle;
508}
509
Chris Craikf158b492016-01-12 14:45:08 -0800510void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800511 BakedOpState* bakedState = tryBakeOpState(op);
512 if (!bakedState) return; // quick rejected
Chris Craikb565df12015-10-05 13:00:52 -0700513
John Reck14de0412016-01-26 09:01:30 -0800514 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
515
516 // TODO: Fix this ( b/26569206 )
517/*
Chris Craik15c3f192015-12-03 12:16:56 -0800518 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
519 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
520 // MergingDrawBatch::canMergeWith()
521 if (bakedState->computedState.transform.isSimple()
522 && bakedState->computedState.transform.positiveScale()
523 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800524 && op.bitmap->colorType() != kAlpha_8_SkColorType
525 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800526 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800527 // TODO: AssetAtlas in mergeId
528 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
529 } else {
530 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
531 }
John Reck14de0412016-01-26 09:01:30 -0800532*/
Chris Craikb565df12015-10-05 13:00:52 -0700533}
534
Chris Craikf158b492016-01-12 14:45:08 -0800535void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800536 BakedOpState* bakedState = tryBakeOpState(op);
537 if (!bakedState) return; // quick rejected
538 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
539}
540
Chris Craikf158b492016-01-12 14:45:08 -0800541void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800542 BakedOpState* bakedState = tryBakeOpState(op);
543 if (!bakedState) return; // quick rejected
544 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
545}
546
Doris Liuf276acd2016-01-07 13:49:26 -0800547void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
548 const SkBitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
549 SkPaint* paint = op.vectorDrawable->getPaint();
550 const BitmapRectOp* resolvedOp = new (mAllocator) BitmapRectOp(op.unmappedBounds,
551 op.localMatrix,
552 op.localClip,
553 paint,
554 &bitmap,
555 Rect(bitmap.width(), bitmap.height()));
556 deferBitmapRectOp(*resolvedOp);
557}
558
Chris Craikf158b492016-01-12 14:45:08 -0800559void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800560 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
561 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
562 float x = *(op.x);
563 float y = *(op.y);
564 float radius = *(op.radius);
565 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
566 const OvalOp* resolvedOp = new (mAllocator) OvalOp(
567 unmappedBounds,
568 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800569 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800570 op.paint);
571 deferOvalOp(*resolvedOp);
572}
573
Chris Craikf158b492016-01-12 14:45:08 -0800574void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craike29ce6f2015-12-10 16:25:13 -0800575 BakedOpState* bakedState = tryBakeOpState(op);
576 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800577 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800578}
579
Chris Craikf158b492016-01-12 14:45:08 -0800580void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800581 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800582 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800583}
584
Chris Craikf158b492016-01-12 14:45:08 -0800585void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800586 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800587}
588
Chris Craikf158b492016-01-12 14:45:08 -0800589void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800590 BakedOpState* bakedState = tryBakeOpState(op);
591 if (!bakedState) return; // quick rejected
592
593 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800594 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
595 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800596 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800597 // TODO: AssetAtlas in mergeId
598
599 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
600 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
601 } else {
602 // Use Bitmap batchId since Bitmap+Patch use same shader
603 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
604 }
605}
606
Chris Craikf158b492016-01-12 14:45:08 -0800607void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800608 deferStrokeableOp(op, OpBatchType::Bitmap);
Chris Craik386aa032015-12-07 17:08:25 -0800609}
610
Chris Craikf158b492016-01-12 14:45:08 -0800611void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800612 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800613 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800614}
615
Chris Craikf158b492016-01-12 14:45:08 -0800616void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800617 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800618}
619
Chris Craikf158b492016-01-12 14:45:08 -0800620void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800621 deferStrokeableOp(op, tessBatchId(op));
Chris Craikb565df12015-10-05 13:00:52 -0700622}
623
Chris Craikf158b492016-01-12 14:45:08 -0800624void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800625 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
626 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
627 const RoundRectOp* resolvedOp = new (mAllocator) RoundRectOp(
628 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
629 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800630 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800631 op.paint, *op.rx, *op.ry);
632 deferRoundRectOp(*resolvedOp);
633}
634
Chris Craikf158b492016-01-12 14:45:08 -0800635void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800636 BakedOpState* bakedState = tryBakeOpState(op);
637 if (!bakedState) return; // quick rejected
638 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700639}
640
Chris Craikd7448e62015-12-15 10:34:36 -0800641static batchid_t textBatchId(const SkPaint& paint) {
642 // TODO: better handling of shader (since we won't care about color then)
643 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
644}
645
Chris Craikf158b492016-01-12 14:45:08 -0800646void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800647 BakedOpState* bakedState = tryBakeOpState(op);
648 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800649
Chris Craikd7448e62015-12-15 10:34:36 -0800650 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800651 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800652 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
653 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800654 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
655 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
656 } else {
657 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
658 }
Chris Craika1717272015-11-19 13:02:43 -0800659}
660
Chris Craikf158b492016-01-12 14:45:08 -0800661void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craikd7448e62015-12-15 10:34:36 -0800662 BakedOpState* bakedState = tryBakeOpState(op);
663 if (!bakedState) return; // quick rejected
664 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
665}
666
Chris Craikf158b492016-01-12 14:45:08 -0800667void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800668 BakedOpState* bakedState = tryBakeOpState(op);
669 if (!bakedState) return; // quick rejected
670 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
671}
672
Chris Craikf158b492016-01-12 14:45:08 -0800673void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800674 float contentTranslateX, float contentTranslateY,
675 const Rect& repaintRect,
676 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700677 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500678 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700679 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700680 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800681 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800682 mCanvasState.writableSnapshot()->transform->loadTranslate(
683 contentTranslateX, contentTranslateY, 0);
684 mCanvasState.writableSnapshot()->setClip(
685 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800686
Chris Craik8ecf41c2015-11-16 10:27:59 -0800687 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800688 mLayerStack.push_back(mLayerBuilders.size());
689 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800690 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800691 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700692}
693
Chris Craikf158b492016-01-12 14:45:08 -0800694void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700695 // restore canvas, and pop finished layer off of the stack
696 mCanvasState.restore();
697 mLayerStack.pop_back();
698}
699
Chris Craikb87eadd2016-01-06 09:16:05 -0800700// TODO: defer time rejection (when bounds become empty) + tests
701// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800702void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800703 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
704 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
705
706 auto previous = mCanvasState.currentSnapshot();
707 Vector3 lightCenter = previous->getRelativeLightCenter();
708
709 // Combine all transforms used to present saveLayer content:
710 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800711 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800712 contentTransform.multiply(op.localMatrix);
713 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
714
715 Matrix4 inverseContentTransform;
716 inverseContentTransform.loadInverse(contentTransform);
717
718 // map the light center into layer-relative space
719 inverseContentTransform.mapPoint3d(lightCenter);
720
721 // Clip bounds of temporary layer to parent's clip rect, so:
722 Rect saveLayerBounds(layerWidth, layerHeight);
723 // 1) transform Rect(width, height) into parent's space
724 // note: left/top offsets put in contentTransform above
725 contentTransform.mapRect(saveLayerBounds);
726 // 2) intersect with parent's clip
727 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
728 // 3) and transform back
729 inverseContentTransform.mapRect(saveLayerBounds);
730 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
731 saveLayerBounds.roundOut();
732
733 // if bounds are reduced, will clip the layer's area by reducing required bounds...
734 layerWidth = saveLayerBounds.getWidth();
735 layerHeight = saveLayerBounds.getHeight();
736 // ...and shifting drawing content to account for left/top side clipping
737 float contentTranslateX = -saveLayerBounds.left;
738 float contentTranslateY = -saveLayerBounds.top;
739
740 saveForLayer(layerWidth, layerHeight,
741 contentTranslateX, contentTranslateY,
742 Rect(layerWidth, layerHeight),
743 lightCenter,
744 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700745}
Chris Craikb565df12015-10-05 13:00:52 -0700746
Chris Craikf158b492016-01-12 14:45:08 -0800747void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700748 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700749 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700750
751 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700752
753 // record the draw operation into the previous layer's list of draw commands
754 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
755 LayerOp* drawLayerOp = new (mAllocator) LayerOp(
756 beginLayerOp.unmappedBounds,
757 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800758 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700759 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800760 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700761 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
762
763 if (bakedOpState) {
764 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
765 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
766 } else {
767 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800768 // TODO: need to prevent any render work from being done
769 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800770 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700771 return;
Chris Craikb565df12015-10-05 13:00:52 -0700772 }
773}
774
Chris Craikf158b492016-01-12 14:45:08 -0800775void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800776 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
777 boundsTransform.multiply(op.localMatrix);
778
779 Rect dstRect(op.unmappedBounds);
780 boundsTransform.mapRect(dstRect);
781 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
782
783 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
Chris Craik7435eb12016-01-07 17:41:40 -0800784 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800785
786 /**
787 * First, defer an operation to copy out the content from the rendertarget into a layer.
788 */
789 auto copyToOp = new (mAllocator) CopyToLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800790 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
791 &(currentLayer().viewportClip), dstRect, *copyToOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800792 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
793
794 /**
795 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
796 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
797 */
798 currentLayer().deferLayerClear(dstRect);
799
800 /**
801 * And stash an operation to copy that layer back under the rendertarget until
802 * a balanced EndUnclippedLayerOp is seen
803 */
804 auto copyFromOp = new (mAllocator) CopyFromLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800805 bakedState = BakedOpState::directConstruct(mAllocator,
806 &(currentLayer().viewportClip), dstRect, *copyFromOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800807 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
808}
809
Chris Craikf158b492016-01-12 14:45:08 -0800810void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800811 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
812
813 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
814 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
815 currentLayer().activeUnclippedSaveLayers.pop_back();
816}
817
Chris Craikb565df12015-10-05 13:00:52 -0700818} // namespace uirenderer
819} // namespace android