blob: 9528587ed860f49dd680f13db3aed6848f35ea68 [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
205 if (!mCanvasState.quickRejectConservative(0, 0, width, height)) {
206 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700207 if (node.getLayer()) {
208 // HW layer
209 LayerOp* drawLayerOp = new (mAllocator) LayerOp(node);
210 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
211 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800212 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700213 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
214 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800215 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
216 // draw DisplayList contents within temporary, since persisted layer could not be used.
217 // (temp layers are clipped to viewport, since they don't persist offscreen content)
218 SkPaint saveLayerPaint;
219 saveLayerPaint.setAlpha(properties.getAlpha());
Chris Craik268a9c02015-12-09 18:05:12 -0800220 deferBeginLayerOp(*new (mAllocator) BeginLayerOp(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800221 saveLayerBounds,
222 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800223 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800224 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800225 deferNodeOps(node);
Chris Craik268a9c02015-12-09 18:05:12 -0800226 deferEndLayerOp(*new (mAllocator) EndLayerOp());
Chris Craik0b7e8242015-10-28 16:50:44 -0700227 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800228 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700229 }
230 }
231}
232
Chris Craik161f54b2015-11-05 11:08:52 -0800233typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
234
235template <typename V>
236static void buildZSortedChildList(V* zTranslatedNodes,
237 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
238 if (chunk.beginChildIndex == chunk.endChildIndex) return;
239
240 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
241 RenderNodeOp* childOp = displayList.getChildren()[i];
242 RenderNode* child = childOp->renderNode;
243 float childZ = child->properties().getZ();
244
245 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
246 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
247 childOp->skipInOrderDraw = true;
248 } else if (!child->properties().getProjectBackwards()) {
249 // regular, in order drawing DisplayList
250 childOp->skipInOrderDraw = false;
251 }
252 }
253
254 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
255 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
256}
257
258template <typename V>
259static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
260 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
261 if (zTranslatedNodes[i].key >= 0.0f) return i;
262 }
263 return zTranslatedNodes.size();
264}
265
266template <typename V>
Chris Craikf158b492016-01-12 14:45:08 -0800267void FrameBuilder::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800268 const int size = zTranslatedNodes.size();
269 if (size == 0
270 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
271 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
272 // no 3d children to draw
273 return;
274 }
275
276 /**
277 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
278 * with very similar Z heights to draw together.
279 *
280 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
281 * underneath both, and neither's shadow is drawn on top of the other.
282 */
283 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
284 size_t drawIndex, shadowIndex, endIndex;
285 if (mode == ChildrenSelectMode::Negative) {
286 drawIndex = 0;
287 endIndex = nonNegativeIndex;
288 shadowIndex = endIndex; // draw no shadows
289 } else {
290 drawIndex = nonNegativeIndex;
291 endIndex = size;
292 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
293 }
294
295 float lastCasterZ = 0.0f;
296 while (shadowIndex < endIndex || drawIndex < endIndex) {
297 if (shadowIndex < endIndex) {
298 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
299 const float casterZ = zTranslatedNodes[shadowIndex].key;
300 // attempt to render the shadow if the caster about to be drawn is its caster,
301 // OR if its caster's Z value is similar to the previous potential caster
302 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
303 deferShadow(*casterNodeOp);
304
305 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
306 shadowIndex++;
307 continue;
308 }
309 }
310
311 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800312 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800313 drawIndex++;
314 }
315}
316
Chris Craikf158b492016-01-12 14:45:08 -0800317void FrameBuilder::deferShadow(const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800318 auto& node = *casterNodeOp.renderNode;
319 auto& properties = node.properties();
320
321 if (properties.getAlpha() <= 0.0f
322 || properties.getOutline().getAlpha() <= 0.0f
323 || !properties.getOutline().getPath()
324 || properties.getScaleX() == 0
325 || properties.getScaleY() == 0) {
326 // no shadow to draw
327 return;
328 }
329
330 const SkPath* casterOutlinePath = properties.getOutline().getPath();
331 const SkPath* revealClipPath = properties.getRevealClip().getPath();
332 if (revealClipPath && revealClipPath->isEmpty()) return;
333
334 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
335
336 // holds temporary SkPath to store the result of intersections
337 SkPath* frameAllocatedPath = nullptr;
338 const SkPath* casterPath = casterOutlinePath;
339
340 // intersect the shadow-casting path with the reveal, if present
341 if (revealClipPath) {
342 frameAllocatedPath = createFrameAllocatedPath();
343
344 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
345 casterPath = frameAllocatedPath;
346 }
347
348 // intersect the shadow-casting path with the clipBounds, if present
349 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
350 if (!frameAllocatedPath) {
351 frameAllocatedPath = createFrameAllocatedPath();
352 }
353 Rect clipBounds;
354 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
355 SkPath clipBoundsPath;
356 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
357 clipBounds.right, clipBounds.bottom);
358
359 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
360 casterPath = frameAllocatedPath;
361 }
362
Chris Craik6e068c012016-01-15 16:15:30 -0800363
364 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
365 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
366 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
367 node.applyViewPropertyTransforms(shadowMatrixXY, false);
368 node.applyViewPropertyTransforms(shadowMatrixZ, true);
369
370 LOG_ALWAYS_FATAL_IF(!mCaches, "Caches needed for shadows");
371 sp<TessellationCache::ShadowTask> task = mCaches->tessellationCache.getShadowTask(
372 mCanvasState.currentTransform(),
373 mCanvasState.getLocalClipBounds(),
374 casterAlpha >= 1.0f,
375 casterPath,
376 &shadowMatrixXY, &shadowMatrixZ,
377 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
378 mLightRadius);
379 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
380 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
381 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
382 if (CC_LIKELY(bakedOpState)) {
383 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
384 }
Chris Craikd3daa312015-11-06 10:59:56 -0800385 }
Chris Craik161f54b2015-11-05 11:08:52 -0800386}
Chris Craikd3daa312015-11-06 10:59:56 -0800387
Chris Craikf158b492016-01-12 14:45:08 -0800388void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800389 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Florin Malitaeecff562015-12-21 10:43:01 -0500390 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik8d1f2122015-11-24 16:40:09 -0800391
392 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
393 const DisplayList& displayList = *(renderNode.getDisplayList());
394
395 const RecordedOp* op = (displayList.getOps()[displayList.projectionReceiveIndex]);
396 const RenderNodeOp* backgroundOp = static_cast<const RenderNodeOp*>(op);
397 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
398
399 // Transform renderer to match background we're projecting onto
400 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
401 mCanvasState.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
402
403 // If the projection receiver has an outline, we mask projected content to it
404 // (which we know, apriori, are all tessellated paths)
405 mCanvasState.setProjectionPathMask(mAllocator, projectionReceiverOutline);
406
407 // draw projected nodes
408 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
409 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
410
Florin Malitaeecff562015-12-21 10:43:01 -0500411 int restoreTo = mCanvasState.save(SaveFlags::Matrix);
Chris Craik8d1f2122015-11-24 16:40:09 -0800412 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
Chris Craik268a9c02015-12-09 18:05:12 -0800413 deferRenderNodeOpImpl(*childOp);
Chris Craik8d1f2122015-11-24 16:40:09 -0800414 mCanvasState.restoreToCount(restoreTo);
415 }
416
417 mCanvasState.restoreToCount(count);
418}
419
Chris Craikb565df12015-10-05 13:00:52 -0700420/**
Chris Craikf158b492016-01-12 14:45:08 -0800421 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700422 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800423 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800424 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700425 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700426#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800427 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
428void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
429 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800430 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800431
432 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
433 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikb36af872015-10-16 14:23:12 -0700434 for (const DisplayList::Chunk& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800435 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
436 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
437
438 defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700439 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700440 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700441 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800442
443 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
444 && displayList.projectionReceiveIndex >= 0
445 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
446 deferProjectedChildren(renderNode);
447 }
Chris Craikb565df12015-10-05 13:00:52 -0700448 }
Chris Craik161f54b2015-11-05 11:08:52 -0800449 defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700450 }
451}
452
Chris Craikf158b492016-01-12 14:45:08 -0800453void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800454 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500455 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700456
Chris Craike4db79d2015-12-22 16:32:23 -0800457 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
458 mCanvasState.writableSnapshot()->mutateClipArea().applyClip(op.localClip,
459 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700460 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700461
Chris Craik0b7e8242015-10-28 16:50:44 -0700462 // then apply state from node properties, and defer ops
463 deferNodePropsAndOps(*op.renderNode);
464
Chris Craik6fe991e52015-10-20 09:39:42 -0700465 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700466}
467
Chris Craikf158b492016-01-12 14:45:08 -0800468void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800469 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800470 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800471 }
472}
473
Chris Craik386aa032015-12-07 17:08:25 -0800474/**
475 * Defers an unmergeable, strokeable op, accounting correctly
476 * for paint's style on the bounds being computed.
477 */
Chris Craikf158b492016-01-12 14:45:08 -0800478void FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800479 BakedOpState::StrokeBehavior strokeBehavior) {
480 // Note: here we account for stroke when baking the op
481 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800482 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik386aa032015-12-07 17:08:25 -0800483 if (!bakedState) return; // quick rejected
484 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
485}
486
487/**
488 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
489 * be used, since they trigger significantly different rendering paths.
490 *
491 * Note: not used for lines/points, since they don't currently support path effects.
492 */
493static batchid_t tessBatchId(const RecordedOp& op) {
494 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700495 return paint.getPathEffect()
496 ? OpBatchType::AlphaMaskTexture
497 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
498}
499
Chris Craikf158b492016-01-12 14:45:08 -0800500void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800501 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800502}
503
Chris Craikb87eadd2016-01-06 09:16:05 -0800504static bool hasMergeableClip(const BakedOpState& state) {
505 return state.computedState.clipState
506 || state.computedState.clipState->mode == ClipMode::Rectangle;
507}
508
Chris Craikf158b492016-01-12 14:45:08 -0800509void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800510 BakedOpState* bakedState = tryBakeOpState(op);
511 if (!bakedState) return; // quick rejected
Chris Craikb565df12015-10-05 13:00:52 -0700512
John Reck14de0412016-01-26 09:01:30 -0800513 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
514
515 // TODO: Fix this ( b/26569206 )
516/*
Chris Craik15c3f192015-12-03 12:16:56 -0800517 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
518 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
519 // MergingDrawBatch::canMergeWith()
520 if (bakedState->computedState.transform.isSimple()
521 && bakedState->computedState.transform.positiveScale()
522 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800523 && op.bitmap->colorType() != kAlpha_8_SkColorType
524 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800525 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800526 // TODO: AssetAtlas in mergeId
527 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
528 } else {
529 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
530 }
John Reck14de0412016-01-26 09:01:30 -0800531*/
Chris Craikb565df12015-10-05 13:00:52 -0700532}
533
Chris Craikf158b492016-01-12 14:45:08 -0800534void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800535 BakedOpState* bakedState = tryBakeOpState(op);
536 if (!bakedState) return; // quick rejected
537 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
538}
539
Chris Craikf158b492016-01-12 14:45:08 -0800540void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800541 BakedOpState* bakedState = tryBakeOpState(op);
542 if (!bakedState) return; // quick rejected
543 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
544}
545
Chris Craikf158b492016-01-12 14:45:08 -0800546void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800547 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
548 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
549 float x = *(op.x);
550 float y = *(op.y);
551 float radius = *(op.radius);
552 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
553 const OvalOp* resolvedOp = new (mAllocator) OvalOp(
554 unmappedBounds,
555 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800556 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800557 op.paint);
558 deferOvalOp(*resolvedOp);
559}
560
Chris Craikf158b492016-01-12 14:45:08 -0800561void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craike29ce6f2015-12-10 16:25:13 -0800562 BakedOpState* bakedState = tryBakeOpState(op);
563 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800564 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800565}
566
Chris Craikf158b492016-01-12 14:45:08 -0800567void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800568 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800569 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800570}
571
Chris Craikf158b492016-01-12 14:45:08 -0800572void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800573 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800574}
575
Chris Craikf158b492016-01-12 14:45:08 -0800576void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800577 BakedOpState* bakedState = tryBakeOpState(op);
578 if (!bakedState) return; // quick rejected
579
580 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800581 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
582 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800583 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800584 // TODO: AssetAtlas in mergeId
585
586 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
587 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
588 } else {
589 // Use Bitmap batchId since Bitmap+Patch use same shader
590 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
591 }
592}
593
Chris Craikf158b492016-01-12 14:45:08 -0800594void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800595 deferStrokeableOp(op, OpBatchType::Bitmap);
Chris Craik386aa032015-12-07 17:08:25 -0800596}
597
Chris Craikf158b492016-01-12 14:45:08 -0800598void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800599 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800600 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800601}
602
Chris Craikf158b492016-01-12 14:45:08 -0800603void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800604 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800605}
606
Chris Craikf158b492016-01-12 14:45:08 -0800607void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800608 deferStrokeableOp(op, tessBatchId(op));
Chris Craikb565df12015-10-05 13:00:52 -0700609}
610
Chris Craikf158b492016-01-12 14:45:08 -0800611void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800612 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
613 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
614 const RoundRectOp* resolvedOp = new (mAllocator) RoundRectOp(
615 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
616 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800617 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800618 op.paint, *op.rx, *op.ry);
619 deferRoundRectOp(*resolvedOp);
620}
621
Chris Craikf158b492016-01-12 14:45:08 -0800622void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800623 BakedOpState* bakedState = tryBakeOpState(op);
624 if (!bakedState) return; // quick rejected
625 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700626}
627
Chris Craikd7448e62015-12-15 10:34:36 -0800628static batchid_t textBatchId(const SkPaint& paint) {
629 // TODO: better handling of shader (since we won't care about color then)
630 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
631}
632
Chris Craikf158b492016-01-12 14:45:08 -0800633void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800634 BakedOpState* bakedState = tryBakeOpState(op);
635 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800636
Chris Craikd7448e62015-12-15 10:34:36 -0800637 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800638 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800639 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
640 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800641 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
642 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
643 } else {
644 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
645 }
Chris Craika1717272015-11-19 13:02:43 -0800646}
647
Chris Craikf158b492016-01-12 14:45:08 -0800648void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craikd7448e62015-12-15 10:34:36 -0800649 BakedOpState* bakedState = tryBakeOpState(op);
650 if (!bakedState) return; // quick rejected
651 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
652}
653
Chris Craikf158b492016-01-12 14:45:08 -0800654void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800655 BakedOpState* bakedState = tryBakeOpState(op);
656 if (!bakedState) return; // quick rejected
657 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
658}
659
Chris Craikf158b492016-01-12 14:45:08 -0800660void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800661 float contentTranslateX, float contentTranslateY,
662 const Rect& repaintRect,
663 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700664 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500665 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700666 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700667 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800668 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800669 mCanvasState.writableSnapshot()->transform->loadTranslate(
670 contentTranslateX, contentTranslateY, 0);
671 mCanvasState.writableSnapshot()->setClip(
672 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800673
Chris Craik8ecf41c2015-11-16 10:27:59 -0800674 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800675 mLayerStack.push_back(mLayerBuilders.size());
676 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800677 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800678 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700679}
680
Chris Craikf158b492016-01-12 14:45:08 -0800681void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700682 // restore canvas, and pop finished layer off of the stack
683 mCanvasState.restore();
684 mLayerStack.pop_back();
685}
686
Chris Craikb87eadd2016-01-06 09:16:05 -0800687// TODO: defer time rejection (when bounds become empty) + tests
688// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800689void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800690 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
691 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
692
693 auto previous = mCanvasState.currentSnapshot();
694 Vector3 lightCenter = previous->getRelativeLightCenter();
695
696 // Combine all transforms used to present saveLayer content:
697 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800698 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800699 contentTransform.multiply(op.localMatrix);
700 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
701
702 Matrix4 inverseContentTransform;
703 inverseContentTransform.loadInverse(contentTransform);
704
705 // map the light center into layer-relative space
706 inverseContentTransform.mapPoint3d(lightCenter);
707
708 // Clip bounds of temporary layer to parent's clip rect, so:
709 Rect saveLayerBounds(layerWidth, layerHeight);
710 // 1) transform Rect(width, height) into parent's space
711 // note: left/top offsets put in contentTransform above
712 contentTransform.mapRect(saveLayerBounds);
713 // 2) intersect with parent's clip
714 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
715 // 3) and transform back
716 inverseContentTransform.mapRect(saveLayerBounds);
717 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
718 saveLayerBounds.roundOut();
719
720 // if bounds are reduced, will clip the layer's area by reducing required bounds...
721 layerWidth = saveLayerBounds.getWidth();
722 layerHeight = saveLayerBounds.getHeight();
723 // ...and shifting drawing content to account for left/top side clipping
724 float contentTranslateX = -saveLayerBounds.left;
725 float contentTranslateY = -saveLayerBounds.top;
726
727 saveForLayer(layerWidth, layerHeight,
728 contentTranslateX, contentTranslateY,
729 Rect(layerWidth, layerHeight),
730 lightCenter,
731 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700732}
Chris Craikb565df12015-10-05 13:00:52 -0700733
Chris Craikf158b492016-01-12 14:45:08 -0800734void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700735 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700736 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700737
738 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700739
740 // record the draw operation into the previous layer's list of draw commands
741 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
742 LayerOp* drawLayerOp = new (mAllocator) LayerOp(
743 beginLayerOp.unmappedBounds,
744 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800745 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700746 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800747 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700748 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
749
750 if (bakedOpState) {
751 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
752 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
753 } else {
754 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800755 // TODO: need to prevent any render work from being done
756 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800757 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700758 return;
Chris Craikb565df12015-10-05 13:00:52 -0700759 }
760}
761
Chris Craikf158b492016-01-12 14:45:08 -0800762void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800763 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
764 boundsTransform.multiply(op.localMatrix);
765
766 Rect dstRect(op.unmappedBounds);
767 boundsTransform.mapRect(dstRect);
768 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
769
770 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
Chris Craik7435eb12016-01-07 17:41:40 -0800771 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800772
773 /**
774 * First, defer an operation to copy out the content from the rendertarget into a layer.
775 */
776 auto copyToOp = new (mAllocator) CopyToLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800777 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
778 &(currentLayer().viewportClip), dstRect, *copyToOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800779 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
780
781 /**
782 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
783 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
784 */
785 currentLayer().deferLayerClear(dstRect);
786
787 /**
788 * And stash an operation to copy that layer back under the rendertarget until
789 * a balanced EndUnclippedLayerOp is seen
790 */
791 auto copyFromOp = new (mAllocator) CopyFromLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800792 bakedState = BakedOpState::directConstruct(mAllocator,
793 &(currentLayer().viewportClip), dstRect, *copyFromOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800794 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
795}
796
Chris Craikf158b492016-01-12 14:45:08 -0800797void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800798 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
799
800 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
801 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
802 currentLayer().activeUnclippedSaveLayers.pop_back();
803}
804
Chris Craikb565df12015-10-05 13:00:52 -0700805} // namespace uirenderer
806} // namespace android