blob: a457212495422188dc4d27c8f30d343b20724d54 [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 Craik98787e62015-11-13 10:55:30 -080035 const std::vector< sp<RenderNode> >& nodes, const Vector3& lightCenter)
Chong Zhangc3bd5682016-01-25 12:01:12 -080036 : FrameBuilder(layers, clip, viewportWidth, viewportHeight, nodes, lightCenter,
37 Rect(0, 0, 0, 0)) {
38}
39
40
41FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip,
42 uint32_t viewportWidth, uint32_t viewportHeight,
43 const std::vector< sp<RenderNode> >& nodes, const Vector3& lightCenter,
44 const Rect &contentDrawBounds)
Chris Craik6fe991e52015-10-20 09:39:42 -070045 : mCanvasState(*this) {
Chris Craik818c9fb2015-10-23 14:33:42 -070046 ATRACE_NAME("prepare drawing commands");
Chris Craikb565df12015-10-05 13:00:52 -070047
Chris Craikf158b492016-01-12 14:45:08 -080048 mLayerBuilders.reserve(layers.entries().size());
Chris Craik98787e62015-11-13 10:55:30 -080049 mLayerStack.reserve(layers.entries().size());
50
51 // Prepare to defer Fbo0
Chris Craikf158b492016-01-12 14:45:08 -080052 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
53 mLayerBuilders.push_back(fbo0);
Chris Craik98787e62015-11-13 10:55:30 -080054 mLayerStack.push_back(0);
Chris Craikb565df12015-10-05 13:00:52 -070055 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -070056 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
Chris Craik98787e62015-11-13 10:55:30 -080057 lightCenter);
Chris Craik0b7e8242015-10-28 16:50:44 -070058
59 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
Chris Craikf158b492016-01-12 14:45:08 -080060 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
Chris Craik0b7e8242015-10-28 16:50:44 -070061 for (int i = layers.entries().size() - 1; i >= 0; i--) {
62 RenderNode* layerNode = layers.entries()[i].renderNode;
Chris Craike9c5fd82016-01-12 18:59:38 -080063 // only schedule repaint if node still on layer - possible it may have been
64 // removed during a dropped frame, but layers may still remain scheduled so
65 // as not to lose info on what portion is damaged
66 if (CC_LIKELY(layerNode->getLayer() != nullptr)) {
67 const Rect& layerDamage = layers.entries()[i].damage;
68 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070069
Chris Craike9c5fd82016-01-12 18:59:38 -080070 // map current light center into RenderNode's coordinate space
71 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
72 layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080073
Chris Craike9c5fd82016-01-12 18:59:38 -080074 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
75 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070076
Chris Craike9c5fd82016-01-12 18:59:38 -080077 if (layerNode->getDisplayList()) {
78 deferNodeOps(*layerNode);
79 }
80 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -070081 }
Chris Craik0b7e8242015-10-28 16:50:44 -070082 }
83
Chong Zhangc3bd5682016-01-25 12:01:12 -080084 // It there are multiple render nodes, they are laid out as follows:
85 // #0 - backdrop (content + caption)
86 // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
87 // #2 - additional overlay nodes
88 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
89 // resizing however it might become partially visible. The following render loop will crop the
90 // backdrop against the content and draw the remaining part of it. It will then draw the content
91 // cropped to the backdrop (since that indicates a shrinking of the window).
92 //
93 // Additional nodes will be drawn on top with no particular clipping semantics.
94
95 // The bounds of the backdrop against which the content should be clipped.
96 Rect backdropBounds = contentDrawBounds;
97 // Usually the contents bounds should be mContentDrawBounds - however - we will
98 // move it towards the fixed edge to give it a more stable appearance (for the moment).
99 // If there is no content bounds we ignore the layering as stated above and start with 2.
100 int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0;
101
Chris Craikb565df12015-10-05 13:00:52 -0700102 for (const sp<RenderNode>& node : nodes) {
103 if (node->nothingToDraw()) continue;
Chris Craik8d1f2122015-11-24 16:40:09 -0800104 node->computeOrdering();
Florin Malitaeecff562015-12-21 10:43:01 -0500105 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800106
107 if (layer == 0) {
108 const RenderProperties& properties = node->properties();
109 Rect targetBounds(properties.getLeft(), properties.getTop(),
110 properties.getRight(), properties.getBottom());
111 // Move the content bounds towards the fixed corner of the backdrop.
112 const int x = targetBounds.left;
113 const int y = targetBounds.top;
114 // Remember the intersection of the target bounds and the intersection bounds against
115 // which we have to crop the content.
116 backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
117 backdropBounds.doIntersect(targetBounds);
118 } else if (layer == 1) {
119 // We shift and clip the content to match its final location in the window.
120 const float left = contentDrawBounds.left;
121 const float top = contentDrawBounds.top;
122 const float dx = backdropBounds.left - left;
123 const float dy = backdropBounds.top - top;
124 const float width = backdropBounds.getWidth();
125 const float height = backdropBounds.getHeight();
126 mCanvasState.translate(dx, dy);
127 // It gets cropped against the bounds of the backdrop to stay inside.
128 mCanvasState.clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op);
129 }
130
Chris Craik0b7e8242015-10-28 16:50:44 -0700131 deferNodePropsAndOps(*node);
132 mCanvasState.restoreToCount(count);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800133 layer++;
Chris Craikb565df12015-10-05 13:00:52 -0700134 }
135}
136
Chris Craikf158b492016-01-12 14:45:08 -0800137void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700138
Chris Craikf158b492016-01-12 14:45:08 -0800139void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700140
Chris Craikf158b492016-01-12 14:45:08 -0800141void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800142 const RenderProperties& properties = node.properties();
143 const Outline& outline = properties.getOutline();
144 if (properties.getAlpha() <= 0
145 || (outline.getShouldClip() && outline.isEmpty())
146 || properties.getScaleX() == 0
147 || properties.getScaleY() == 0) {
148 return; // rejected
149 }
150
151 if (properties.getLeft() != 0 || properties.getTop() != 0) {
152 mCanvasState.translate(properties.getLeft(), properties.getTop());
153 }
154 if (properties.getStaticMatrix()) {
155 mCanvasState.concatMatrix(*properties.getStaticMatrix());
156 } else if (properties.getAnimationMatrix()) {
157 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
158 }
159 if (properties.hasTransformMatrix()) {
160 if (properties.isTransformTranslateOnly()) {
161 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
162 } else {
163 mCanvasState.concatMatrix(*properties.getTransformMatrix());
164 }
165 }
166
167 const int width = properties.getWidth();
168 const int height = properties.getHeight();
169
170 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
171 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
172 int clipFlags = properties.getClippingFlags();
173 if (properties.getAlpha() < 1) {
174 if (isLayer) {
175 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
176 }
177 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
178 // simply scale rendering content's alpha
179 mCanvasState.scaleAlpha(properties.getAlpha());
180 } else {
181 // schedule saveLayer by initializing saveLayerBounds
182 saveLayerBounds.set(0, 0, width, height);
183 if (clipFlags) {
184 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
185 clipFlags = 0; // all clipping done by savelayer
186 }
187 }
188
189 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
190 // pretend alpha always causes savelayer to warn about
191 // performance problem affecting old versions
192 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
193 }
194 }
195 if (clipFlags) {
196 Rect clipRect;
197 properties.getClippingRectForFlags(clipFlags, &clipRect);
198 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
199 SkRegion::kIntersect_Op);
200 }
201
202 if (properties.getRevealClip().willClip()) {
203 Rect bounds;
204 properties.getRevealClip().getBounds(&bounds);
205 mCanvasState.setClippingRoundRect(mAllocator,
206 bounds, properties.getRevealClip().getRadius());
207 } else if (properties.getOutline().willClip()) {
208 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
209 }
210
211 if (!mCanvasState.quickRejectConservative(0, 0, width, height)) {
212 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700213 if (node.getLayer()) {
214 // HW layer
215 LayerOp* drawLayerOp = new (mAllocator) LayerOp(node);
216 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
217 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800218 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700219 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
220 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800221 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
222 // draw DisplayList contents within temporary, since persisted layer could not be used.
223 // (temp layers are clipped to viewport, since they don't persist offscreen content)
224 SkPaint saveLayerPaint;
225 saveLayerPaint.setAlpha(properties.getAlpha());
Chris Craik268a9c02015-12-09 18:05:12 -0800226 deferBeginLayerOp(*new (mAllocator) BeginLayerOp(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800227 saveLayerBounds,
228 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800229 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800230 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800231 deferNodeOps(node);
Chris Craik268a9c02015-12-09 18:05:12 -0800232 deferEndLayerOp(*new (mAllocator) EndLayerOp());
Chris Craik0b7e8242015-10-28 16:50:44 -0700233 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800234 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700235 }
236 }
237}
238
Chris Craik161f54b2015-11-05 11:08:52 -0800239typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
240
241template <typename V>
242static void buildZSortedChildList(V* zTranslatedNodes,
243 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
244 if (chunk.beginChildIndex == chunk.endChildIndex) return;
245
246 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
247 RenderNodeOp* childOp = displayList.getChildren()[i];
248 RenderNode* child = childOp->renderNode;
249 float childZ = child->properties().getZ();
250
251 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
252 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
253 childOp->skipInOrderDraw = true;
254 } else if (!child->properties().getProjectBackwards()) {
255 // regular, in order drawing DisplayList
256 childOp->skipInOrderDraw = false;
257 }
258 }
259
260 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
261 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
262}
263
264template <typename V>
265static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
266 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
267 if (zTranslatedNodes[i].key >= 0.0f) return i;
268 }
269 return zTranslatedNodes.size();
270}
271
272template <typename V>
Chris Craikf158b492016-01-12 14:45:08 -0800273void FrameBuilder::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800274 const int size = zTranslatedNodes.size();
275 if (size == 0
276 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
277 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
278 // no 3d children to draw
279 return;
280 }
281
282 /**
283 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
284 * with very similar Z heights to draw together.
285 *
286 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
287 * underneath both, and neither's shadow is drawn on top of the other.
288 */
289 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
290 size_t drawIndex, shadowIndex, endIndex;
291 if (mode == ChildrenSelectMode::Negative) {
292 drawIndex = 0;
293 endIndex = nonNegativeIndex;
294 shadowIndex = endIndex; // draw no shadows
295 } else {
296 drawIndex = nonNegativeIndex;
297 endIndex = size;
298 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
299 }
300
301 float lastCasterZ = 0.0f;
302 while (shadowIndex < endIndex || drawIndex < endIndex) {
303 if (shadowIndex < endIndex) {
304 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
305 const float casterZ = zTranslatedNodes[shadowIndex].key;
306 // attempt to render the shadow if the caster about to be drawn is its caster,
307 // OR if its caster's Z value is similar to the previous potential caster
308 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
309 deferShadow(*casterNodeOp);
310
311 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
312 shadowIndex++;
313 continue;
314 }
315 }
316
317 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800318 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800319 drawIndex++;
320 }
321}
322
Chris Craikf158b492016-01-12 14:45:08 -0800323void FrameBuilder::deferShadow(const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800324 auto& node = *casterNodeOp.renderNode;
325 auto& properties = node.properties();
326
327 if (properties.getAlpha() <= 0.0f
328 || properties.getOutline().getAlpha() <= 0.0f
329 || !properties.getOutline().getPath()
330 || properties.getScaleX() == 0
331 || properties.getScaleY() == 0) {
332 // no shadow to draw
333 return;
334 }
335
336 const SkPath* casterOutlinePath = properties.getOutline().getPath();
337 const SkPath* revealClipPath = properties.getRevealClip().getPath();
338 if (revealClipPath && revealClipPath->isEmpty()) return;
339
340 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
341
342 // holds temporary SkPath to store the result of intersections
343 SkPath* frameAllocatedPath = nullptr;
344 const SkPath* casterPath = casterOutlinePath;
345
346 // intersect the shadow-casting path with the reveal, if present
347 if (revealClipPath) {
348 frameAllocatedPath = createFrameAllocatedPath();
349
350 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
351 casterPath = frameAllocatedPath;
352 }
353
354 // intersect the shadow-casting path with the clipBounds, if present
355 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
356 if (!frameAllocatedPath) {
357 frameAllocatedPath = createFrameAllocatedPath();
358 }
359 Rect clipBounds;
360 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
361 SkPath clipBoundsPath;
362 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
363 clipBounds.right, clipBounds.bottom);
364
365 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
366 casterPath = frameAllocatedPath;
367 }
368
369 ShadowOp* shadowOp = new (mAllocator) ShadowOp(casterNodeOp, casterAlpha, casterPath,
Chris Craik98787e62015-11-13 10:55:30 -0800370 mCanvasState.getLocalClipBounds(),
371 mCanvasState.currentSnapshot()->getRelativeLightCenter());
Chris Craikd3daa312015-11-06 10:59:56 -0800372 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800373 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
Chris Craikd3daa312015-11-06 10:59:56 -0800374 if (CC_LIKELY(bakedOpState)) {
375 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
376 }
Chris Craik161f54b2015-11-05 11:08:52 -0800377}
Chris Craikd3daa312015-11-06 10:59:56 -0800378
Chris Craikf158b492016-01-12 14:45:08 -0800379void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800380 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Florin Malitaeecff562015-12-21 10:43:01 -0500381 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik8d1f2122015-11-24 16:40:09 -0800382
383 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
384 const DisplayList& displayList = *(renderNode.getDisplayList());
385
386 const RecordedOp* op = (displayList.getOps()[displayList.projectionReceiveIndex]);
387 const RenderNodeOp* backgroundOp = static_cast<const RenderNodeOp*>(op);
388 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
389
390 // Transform renderer to match background we're projecting onto
391 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
392 mCanvasState.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
393
394 // If the projection receiver has an outline, we mask projected content to it
395 // (which we know, apriori, are all tessellated paths)
396 mCanvasState.setProjectionPathMask(mAllocator, projectionReceiverOutline);
397
398 // draw projected nodes
399 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
400 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
401
Florin Malitaeecff562015-12-21 10:43:01 -0500402 int restoreTo = mCanvasState.save(SaveFlags::Matrix);
Chris Craik8d1f2122015-11-24 16:40:09 -0800403 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
Chris Craik268a9c02015-12-09 18:05:12 -0800404 deferRenderNodeOpImpl(*childOp);
Chris Craik8d1f2122015-11-24 16:40:09 -0800405 mCanvasState.restoreToCount(restoreTo);
406 }
407
408 mCanvasState.restoreToCount(count);
409}
410
Chris Craikb565df12015-10-05 13:00:52 -0700411/**
Chris Craikf158b492016-01-12 14:45:08 -0800412 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700413 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800414 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800415 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700416 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700417#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800418 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
419void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
420 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800421 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800422
423 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
424 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikb36af872015-10-16 14:23:12 -0700425 for (const DisplayList::Chunk& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800426 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
427 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
428
429 defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700430 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700431 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700432 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800433
434 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
435 && displayList.projectionReceiveIndex >= 0
436 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
437 deferProjectedChildren(renderNode);
438 }
Chris Craikb565df12015-10-05 13:00:52 -0700439 }
Chris Craik161f54b2015-11-05 11:08:52 -0800440 defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700441 }
442}
443
Chris Craikf158b492016-01-12 14:45:08 -0800444void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800445 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500446 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700447
Chris Craike4db79d2015-12-22 16:32:23 -0800448 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
449 mCanvasState.writableSnapshot()->mutateClipArea().applyClip(op.localClip,
450 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700451 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700452
Chris Craik0b7e8242015-10-28 16:50:44 -0700453 // then apply state from node properties, and defer ops
454 deferNodePropsAndOps(*op.renderNode);
455
Chris Craik6fe991e52015-10-20 09:39:42 -0700456 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700457}
458
Chris Craikf158b492016-01-12 14:45:08 -0800459void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800460 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800461 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800462 }
463}
464
Chris Craik386aa032015-12-07 17:08:25 -0800465/**
466 * Defers an unmergeable, strokeable op, accounting correctly
467 * for paint's style on the bounds being computed.
468 */
Chris Craikf158b492016-01-12 14:45:08 -0800469void FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800470 BakedOpState::StrokeBehavior strokeBehavior) {
471 // Note: here we account for stroke when baking the op
472 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800473 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik386aa032015-12-07 17:08:25 -0800474 if (!bakedState) return; // quick rejected
475 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
476}
477
478/**
479 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
480 * be used, since they trigger significantly different rendering paths.
481 *
482 * Note: not used for lines/points, since they don't currently support path effects.
483 */
484static batchid_t tessBatchId(const RecordedOp& op) {
485 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700486 return paint.getPathEffect()
487 ? OpBatchType::AlphaMaskTexture
488 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
489}
490
Chris Craikf158b492016-01-12 14:45:08 -0800491void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800492 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800493}
494
Chris Craikb87eadd2016-01-06 09:16:05 -0800495static bool hasMergeableClip(const BakedOpState& state) {
496 return state.computedState.clipState
497 || state.computedState.clipState->mode == ClipMode::Rectangle;
498}
499
Chris Craikf158b492016-01-12 14:45:08 -0800500void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800501 BakedOpState* bakedState = tryBakeOpState(op);
502 if (!bakedState) return; // quick rejected
Chris Craikb565df12015-10-05 13:00:52 -0700503
John Reck14de0412016-01-26 09:01:30 -0800504 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
505
506 // TODO: Fix this ( b/26569206 )
507/*
Chris Craik15c3f192015-12-03 12:16:56 -0800508 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
509 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
510 // MergingDrawBatch::canMergeWith()
511 if (bakedState->computedState.transform.isSimple()
512 && bakedState->computedState.transform.positiveScale()
513 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800514 && op.bitmap->colorType() != kAlpha_8_SkColorType
515 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800516 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800517 // TODO: AssetAtlas in mergeId
518 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
519 } else {
520 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
521 }
John Reck14de0412016-01-26 09:01:30 -0800522*/
Chris Craikb565df12015-10-05 13:00:52 -0700523}
524
Chris Craikf158b492016-01-12 14:45:08 -0800525void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800526 BakedOpState* bakedState = tryBakeOpState(op);
527 if (!bakedState) return; // quick rejected
528 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
529}
530
Chris Craikf158b492016-01-12 14:45:08 -0800531void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800532 BakedOpState* bakedState = tryBakeOpState(op);
533 if (!bakedState) return; // quick rejected
534 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
535}
536
Chris Craikf158b492016-01-12 14:45:08 -0800537void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800538 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
539 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
540 float x = *(op.x);
541 float y = *(op.y);
542 float radius = *(op.radius);
543 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
544 const OvalOp* resolvedOp = new (mAllocator) OvalOp(
545 unmappedBounds,
546 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800547 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800548 op.paint);
549 deferOvalOp(*resolvedOp);
550}
551
Chris Craikf158b492016-01-12 14:45:08 -0800552void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craike29ce6f2015-12-10 16:25:13 -0800553 BakedOpState* bakedState = tryBakeOpState(op);
554 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800555 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800556}
557
Chris Craikf158b492016-01-12 14:45:08 -0800558void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800559 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800560 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800561}
562
Chris Craikf158b492016-01-12 14:45:08 -0800563void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800564 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800565}
566
Chris Craikf158b492016-01-12 14:45:08 -0800567void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800568 BakedOpState* bakedState = tryBakeOpState(op);
569 if (!bakedState) return; // quick rejected
570
571 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800572 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
573 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800574 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800575 // TODO: AssetAtlas in mergeId
576
577 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
578 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
579 } else {
580 // Use Bitmap batchId since Bitmap+Patch use same shader
581 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
582 }
583}
584
Chris Craikf158b492016-01-12 14:45:08 -0800585void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800586 deferStrokeableOp(op, OpBatchType::Bitmap);
Chris Craik386aa032015-12-07 17:08:25 -0800587}
588
Chris Craikf158b492016-01-12 14:45:08 -0800589void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800590 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800591 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800592}
593
Chris Craikf158b492016-01-12 14:45:08 -0800594void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800595 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800596}
597
Chris Craikf158b492016-01-12 14:45:08 -0800598void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800599 deferStrokeableOp(op, tessBatchId(op));
Chris Craikb565df12015-10-05 13:00:52 -0700600}
601
Chris Craikf158b492016-01-12 14:45:08 -0800602void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800603 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
604 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
605 const RoundRectOp* resolvedOp = new (mAllocator) RoundRectOp(
606 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
607 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800608 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800609 op.paint, *op.rx, *op.ry);
610 deferRoundRectOp(*resolvedOp);
611}
612
Chris Craikf158b492016-01-12 14:45:08 -0800613void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800614 BakedOpState* bakedState = tryBakeOpState(op);
615 if (!bakedState) return; // quick rejected
616 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700617}
618
Chris Craikd7448e62015-12-15 10:34:36 -0800619static batchid_t textBatchId(const SkPaint& paint) {
620 // TODO: better handling of shader (since we won't care about color then)
621 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
622}
623
Chris Craikf158b492016-01-12 14:45:08 -0800624void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800625 BakedOpState* bakedState = tryBakeOpState(op);
626 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800627
Chris Craikd7448e62015-12-15 10:34:36 -0800628 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800629 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800630 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
631 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800632 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
633 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
634 } else {
635 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
636 }
Chris Craika1717272015-11-19 13:02:43 -0800637}
638
Chris Craikf158b492016-01-12 14:45:08 -0800639void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craikd7448e62015-12-15 10:34:36 -0800640 BakedOpState* bakedState = tryBakeOpState(op);
641 if (!bakedState) return; // quick rejected
642 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
643}
644
Chris Craikf158b492016-01-12 14:45:08 -0800645void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800646 BakedOpState* bakedState = tryBakeOpState(op);
647 if (!bakedState) return; // quick rejected
648 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
649}
650
Chris Craikf158b492016-01-12 14:45:08 -0800651void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800652 float contentTranslateX, float contentTranslateY,
653 const Rect& repaintRect,
654 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700655 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500656 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700657 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700658 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800659 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800660 mCanvasState.writableSnapshot()->transform->loadTranslate(
661 contentTranslateX, contentTranslateY, 0);
662 mCanvasState.writableSnapshot()->setClip(
663 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800664
Chris Craik8ecf41c2015-11-16 10:27:59 -0800665 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800666 mLayerStack.push_back(mLayerBuilders.size());
667 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800668 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800669 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700670}
671
Chris Craikf158b492016-01-12 14:45:08 -0800672void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700673 // restore canvas, and pop finished layer off of the stack
674 mCanvasState.restore();
675 mLayerStack.pop_back();
676}
677
Chris Craikb87eadd2016-01-06 09:16:05 -0800678// TODO: defer time rejection (when bounds become empty) + tests
679// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800680void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800681 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
682 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
683
684 auto previous = mCanvasState.currentSnapshot();
685 Vector3 lightCenter = previous->getRelativeLightCenter();
686
687 // Combine all transforms used to present saveLayer content:
688 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800689 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800690 contentTransform.multiply(op.localMatrix);
691 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
692
693 Matrix4 inverseContentTransform;
694 inverseContentTransform.loadInverse(contentTransform);
695
696 // map the light center into layer-relative space
697 inverseContentTransform.mapPoint3d(lightCenter);
698
699 // Clip bounds of temporary layer to parent's clip rect, so:
700 Rect saveLayerBounds(layerWidth, layerHeight);
701 // 1) transform Rect(width, height) into parent's space
702 // note: left/top offsets put in contentTransform above
703 contentTransform.mapRect(saveLayerBounds);
704 // 2) intersect with parent's clip
705 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
706 // 3) and transform back
707 inverseContentTransform.mapRect(saveLayerBounds);
708 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
709 saveLayerBounds.roundOut();
710
711 // if bounds are reduced, will clip the layer's area by reducing required bounds...
712 layerWidth = saveLayerBounds.getWidth();
713 layerHeight = saveLayerBounds.getHeight();
714 // ...and shifting drawing content to account for left/top side clipping
715 float contentTranslateX = -saveLayerBounds.left;
716 float contentTranslateY = -saveLayerBounds.top;
717
718 saveForLayer(layerWidth, layerHeight,
719 contentTranslateX, contentTranslateY,
720 Rect(layerWidth, layerHeight),
721 lightCenter,
722 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700723}
Chris Craikb565df12015-10-05 13:00:52 -0700724
Chris Craikf158b492016-01-12 14:45:08 -0800725void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700726 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700727 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700728
729 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700730
731 // record the draw operation into the previous layer's list of draw commands
732 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
733 LayerOp* drawLayerOp = new (mAllocator) LayerOp(
734 beginLayerOp.unmappedBounds,
735 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800736 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700737 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800738 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700739 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
740
741 if (bakedOpState) {
742 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
743 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
744 } else {
745 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800746 // TODO: need to prevent any render work from being done
747 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800748 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700749 return;
Chris Craikb565df12015-10-05 13:00:52 -0700750 }
751}
752
Chris Craikf158b492016-01-12 14:45:08 -0800753void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800754 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
755 boundsTransform.multiply(op.localMatrix);
756
757 Rect dstRect(op.unmappedBounds);
758 boundsTransform.mapRect(dstRect);
759 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
760
761 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
Chris Craik7435eb12016-01-07 17:41:40 -0800762 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800763
764 /**
765 * First, defer an operation to copy out the content from the rendertarget into a layer.
766 */
767 auto copyToOp = new (mAllocator) CopyToLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800768 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
769 &(currentLayer().viewportClip), dstRect, *copyToOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800770 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
771
772 /**
773 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
774 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
775 */
776 currentLayer().deferLayerClear(dstRect);
777
778 /**
779 * And stash an operation to copy that layer back under the rendertarget until
780 * a balanced EndUnclippedLayerOp is seen
781 */
782 auto copyFromOp = new (mAllocator) CopyFromLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800783 bakedState = BakedOpState::directConstruct(mAllocator,
784 &(currentLayer().viewportClip), dstRect, *copyFromOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800785 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
786}
787
Chris Craikf158b492016-01-12 14:45:08 -0800788void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800789 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
790
791 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
792 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
793 currentLayer().activeUnclippedSaveLayers.pop_back();
794}
795
Chris Craikb565df12015-10-05 13:00:52 -0700796} // namespace uirenderer
797} // namespace android