blob: 6fc74a58565929bed63256e8bcbebe41e755166f [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
Chris Craik0b7e8242015-10-28 16:50:44 -070019#include "LayerUpdateQueue.h"
Chris Craik161f54b2015-11-05 11:08:52 -080020#include "RenderNode.h"
Doris Liu766431a2016-02-04 22:17:11 +000021#include "VectorDrawable.h"
Chris Craik98787e62015-11-13 10:55:30 -080022#include "renderstate/OffscreenBufferPool.h"
sergeyvdccca442016-03-21 15:38:21 -070023#include "hwui/Canvas.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,
Chris Craik3a5811b2016-03-22 15:03:08 -070037 const LightGeometry& lightGeometry, const Rect &contentDrawBounds, Caches& caches)
Chris Craik6e068c012016-01-15 16:15:30 -080038 : mCanvasState(*this)
39 , mCaches(caches)
Chris Craik6246d2782016-03-29 15:01:41 -070040 , mLightRadius(lightGeometry.radius)
41 , mDrawFbo0(!nodes.empty()) {
Chris Craik818c9fb2015-10-23 14:33:42 -070042 ATRACE_NAME("prepare drawing commands");
Chris Craikb565df12015-10-05 13:00:52 -070043
Chris Craikf158b492016-01-12 14:45:08 -080044 mLayerBuilders.reserve(layers.entries().size());
Chris Craik98787e62015-11-13 10:55:30 -080045 mLayerStack.reserve(layers.entries().size());
46
47 // Prepare to defer Fbo0
Chris Craikf158b492016-01-12 14:45:08 -080048 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
49 mLayerBuilders.push_back(fbo0);
Chris Craik98787e62015-11-13 10:55:30 -080050 mLayerStack.push_back(0);
Chris Craikb565df12015-10-05 13:00:52 -070051 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -070052 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
Chris Craik6e068c012016-01-15 16:15:30 -080053 lightGeometry.center);
Chris Craik0b7e8242015-10-28 16:50:44 -070054
55 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
Chris Craikf158b492016-01-12 14:45:08 -080056 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
Chris Craik0b7e8242015-10-28 16:50:44 -070057 for (int i = layers.entries().size() - 1; i >= 0; i--) {
58 RenderNode* layerNode = layers.entries()[i].renderNode;
Chris Craike9c5fd82016-01-12 18:59:38 -080059 // only schedule repaint if node still on layer - possible it may have been
60 // removed during a dropped frame, but layers may still remain scheduled so
61 // as not to lose info on what portion is damaged
62 if (CC_LIKELY(layerNode->getLayer() != nullptr)) {
63 const Rect& layerDamage = layers.entries()[i].damage;
64 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070065
Chris Craike9c5fd82016-01-12 18:59:38 -080066 // map current light center into RenderNode's coordinate space
67 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
68 layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080069
Chris Craike9c5fd82016-01-12 18:59:38 -080070 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
71 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070072
Chris Craike9c5fd82016-01-12 18:59:38 -080073 if (layerNode->getDisplayList()) {
74 deferNodeOps(*layerNode);
75 }
76 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -070077 }
Chris Craik0b7e8242015-10-28 16:50:44 -070078 }
79
Chong Zhangc3bd5682016-01-25 12:01:12 -080080 // It there are multiple render nodes, they are laid out as follows:
81 // #0 - backdrop (content + caption)
82 // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
83 // #2 - additional overlay nodes
84 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
85 // resizing however it might become partially visible. The following render loop will crop the
86 // backdrop against the content and draw the remaining part of it. It will then draw the content
87 // cropped to the backdrop (since that indicates a shrinking of the window).
88 //
89 // Additional nodes will be drawn on top with no particular clipping semantics.
90
91 // The bounds of the backdrop against which the content should be clipped.
92 Rect backdropBounds = contentDrawBounds;
93 // Usually the contents bounds should be mContentDrawBounds - however - we will
94 // move it towards the fixed edge to give it a more stable appearance (for the moment).
95 // If there is no content bounds we ignore the layering as stated above and start with 2.
96 int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0;
97
Chris Craikb565df12015-10-05 13:00:52 -070098 for (const sp<RenderNode>& node : nodes) {
99 if (node->nothingToDraw()) continue;
Chris Craik8d1f2122015-11-24 16:40:09 -0800100 node->computeOrdering();
Florin Malitaeecff562015-12-21 10:43:01 -0500101 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800102
103 if (layer == 0) {
104 const RenderProperties& properties = node->properties();
105 Rect targetBounds(properties.getLeft(), properties.getTop(),
106 properties.getRight(), properties.getBottom());
107 // Move the content bounds towards the fixed corner of the backdrop.
108 const int x = targetBounds.left;
109 const int y = targetBounds.top;
110 // Remember the intersection of the target bounds and the intersection bounds against
111 // which we have to crop the content.
112 backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
113 backdropBounds.doIntersect(targetBounds);
114 } else if (layer == 1) {
115 // We shift and clip the content to match its final location in the window.
116 const float left = contentDrawBounds.left;
117 const float top = contentDrawBounds.top;
118 const float dx = backdropBounds.left - left;
119 const float dy = backdropBounds.top - top;
120 const float width = backdropBounds.getWidth();
121 const float height = backdropBounds.getHeight();
122 mCanvasState.translate(dx, dy);
123 // It gets cropped against the bounds of the backdrop to stay inside.
124 mCanvasState.clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op);
125 }
126
Chris Craik0b7e8242015-10-28 16:50:44 -0700127 deferNodePropsAndOps(*node);
128 mCanvasState.restoreToCount(count);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800129 layer++;
Chris Craikb565df12015-10-05 13:00:52 -0700130 }
131}
132
Chris Craikf158b492016-01-12 14:45:08 -0800133void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700134
Chris Craikf158b492016-01-12 14:45:08 -0800135void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700136
Chris Craikf158b492016-01-12 14:45:08 -0800137void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800138 const RenderProperties& properties = node.properties();
139 const Outline& outline = properties.getOutline();
140 if (properties.getAlpha() <= 0
141 || (outline.getShouldClip() && outline.isEmpty())
142 || properties.getScaleX() == 0
143 || properties.getScaleY() == 0) {
144 return; // rejected
145 }
146
147 if (properties.getLeft() != 0 || properties.getTop() != 0) {
148 mCanvasState.translate(properties.getLeft(), properties.getTop());
149 }
150 if (properties.getStaticMatrix()) {
151 mCanvasState.concatMatrix(*properties.getStaticMatrix());
152 } else if (properties.getAnimationMatrix()) {
153 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
154 }
155 if (properties.hasTransformMatrix()) {
156 if (properties.isTransformTranslateOnly()) {
157 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
158 } else {
159 mCanvasState.concatMatrix(*properties.getTransformMatrix());
160 }
161 }
162
163 const int width = properties.getWidth();
164 const int height = properties.getHeight();
165
166 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
167 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
168 int clipFlags = properties.getClippingFlags();
169 if (properties.getAlpha() < 1) {
170 if (isLayer) {
171 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
172 }
173 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
174 // simply scale rendering content's alpha
175 mCanvasState.scaleAlpha(properties.getAlpha());
176 } else {
177 // schedule saveLayer by initializing saveLayerBounds
178 saveLayerBounds.set(0, 0, width, height);
179 if (clipFlags) {
180 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
181 clipFlags = 0; // all clipping done by savelayer
182 }
183 }
184
185 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
186 // pretend alpha always causes savelayer to warn about
187 // performance problem affecting old versions
188 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
189 }
190 }
191 if (clipFlags) {
192 Rect clipRect;
193 properties.getClippingRectForFlags(clipFlags, &clipRect);
194 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
195 SkRegion::kIntersect_Op);
196 }
197
198 if (properties.getRevealClip().willClip()) {
199 Rect bounds;
200 properties.getRevealClip().getBounds(&bounds);
201 mCanvasState.setClippingRoundRect(mAllocator,
202 bounds, properties.getRevealClip().getRadius());
203 } else if (properties.getOutline().willClip()) {
204 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
205 }
206
Chris Craik8913c892016-01-14 16:15:03 -0800207 bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty()
208 || (properties.getClipToBounds()
209 && mCanvasState.quickRejectConservative(0, 0, width, height));
Chris Craik7fc1b032016-02-03 19:45:06 -0800210 if (!quickRejected) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800211 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700212 if (node.getLayer()) {
213 // HW layer
John Reck7df9ff22016-02-10 16:08:08 -0800214 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700215 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
216 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800217 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700218 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
219 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800220 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
221 // draw DisplayList contents within temporary, since persisted layer could not be used.
222 // (temp layers are clipped to viewport, since they don't persist offscreen content)
223 SkPaint saveLayerPaint;
224 saveLayerPaint.setAlpha(properties.getAlpha());
John Reck7df9ff22016-02-10 16:08:08 -0800225 deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800226 saveLayerBounds,
227 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800228 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800229 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800230 deferNodeOps(node);
John Reck7df9ff22016-02-10 16:08:08 -0800231 deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
Chris Craik0b7e8242015-10-28 16:50:44 -0700232 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800233 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700234 }
235 }
236}
237
Chris Craik161f54b2015-11-05 11:08:52 -0800238typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
239
240template <typename V>
241static void buildZSortedChildList(V* zTranslatedNodes,
242 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
243 if (chunk.beginChildIndex == chunk.endChildIndex) return;
244
245 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
246 RenderNodeOp* childOp = displayList.getChildren()[i];
247 RenderNode* child = childOp->renderNode;
248 float childZ = child->properties().getZ();
249
250 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
251 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
252 childOp->skipInOrderDraw = true;
253 } else if (!child->properties().getProjectBackwards()) {
254 // regular, in order drawing DisplayList
255 childOp->skipInOrderDraw = false;
256 }
257 }
258
259 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
260 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
261}
262
263template <typename V>
264static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
265 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
266 if (zTranslatedNodes[i].key >= 0.0f) return i;
267 }
268 return zTranslatedNodes.size();
269}
270
271template <typename V>
Chris Craikd6456402016-04-11 12:24:23 -0700272void FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode,
273 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) {
Chris Craikd6456402016-04-11 12:24:23 -0700309 deferShadow(reorderClip, *casterNodeOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800310
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 Craikd6456402016-04-11 12:24:23 -0700323void FrameBuilder::deferShadow(const ClipBase* reorderClip, 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
Chris Craikd6456402016-04-11 12:24:23 -0700369 // apply reorder clip to shadow, so it respects clip at beginning of reorderable chunk
370 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
371 mCanvasState.writableSnapshot()->applyClip(reorderClip,
372 *mCanvasState.currentSnapshot()->transform);
Chris Craik6e068c012016-01-15 16:15:30 -0800373 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
374 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
375 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
376 node.applyViewPropertyTransforms(shadowMatrixXY, false);
377 node.applyViewPropertyTransforms(shadowMatrixZ, true);
378
Chris Craik3a5811b2016-03-22 15:03:08 -0700379 sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask(
Chris Craik6e068c012016-01-15 16:15:30 -0800380 mCanvasState.currentTransform(),
381 mCanvasState.getLocalClipBounds(),
382 casterAlpha >= 1.0f,
383 casterPath,
384 &shadowMatrixXY, &shadowMatrixZ,
385 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
386 mLightRadius);
387 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
388 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
389 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
390 if (CC_LIKELY(bakedOpState)) {
391 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
392 }
Chris Craikd3daa312015-11-06 10:59:56 -0800393 }
Chris Craikd6456402016-04-11 12:24:23 -0700394 mCanvasState.restoreToCount(restoreTo);
Chris Craik161f54b2015-11-05 11:08:52 -0800395}
Chris Craikd3daa312015-11-06 10:59:56 -0800396
Chris Craikf158b492016-01-12 14:45:08 -0800397void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500398 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800399 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Chris Craik8d1f2122015-11-24 16:40:09 -0800400
Chris Craik678ff812016-03-01 13:27:54 -0800401 SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
402 if (projectionReceiverOutline) {
403 // transform the mask for this projector into render target space
404 // TODO: consider combining both transforms by stashing transform instead of applying
405 SkMatrix skCurrentTransform;
406 mCanvasState.currentTransform()->copyTo(skCurrentTransform);
407 projectionReceiverOutline->transform(
408 skCurrentTransform,
409 &transformedMaskPath);
410 mCanvasState.setProjectionPathMask(mAllocator, &transformedMaskPath);
411 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800412
Chris Craik8d1f2122015-11-24 16:40:09 -0800413 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
414 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
Chris Craika748c082016-03-01 18:48:37 -0800415 RenderNode& childNode = *childOp->renderNode;
Chris Craik678ff812016-03-01 13:27:54 -0800416
Chris Craika748c082016-03-01 18:48:37 -0800417 // Draw child if it has content, but ignore state in childOp - matrix already applied to
418 // transformFromCompositingAncestor, and record-time clip is ignored when projecting
419 if (!childNode.nothingToDraw()) {
420 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800421
Chris Craika748c082016-03-01 18:48:37 -0800422 // Apply transform between ancestor and projected descendant
423 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
424
425 deferNodePropsAndOps(childNode);
426
427 mCanvasState.restoreToCount(restoreTo);
428 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800429 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800430 mCanvasState.restoreToCount(count);
431}
432
Chris Craikb565df12015-10-05 13:00:52 -0700433/**
Chris Craikf158b492016-01-12 14:45:08 -0800434 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700435 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800436 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800437 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700438 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700439#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800440 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
441void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
442 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800443 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800444
445 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
446 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikd6456402016-04-11 12:24:23 -0700447 for (auto& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800448 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
449 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
450
Chris Craikd6456402016-04-11 12:24:23 -0700451 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700452 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700453 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700454 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800455
456 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
457 && displayList.projectionReceiveIndex >= 0
458 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
459 deferProjectedChildren(renderNode);
460 }
Chris Craikb565df12015-10-05 13:00:52 -0700461 }
Chris Craikd6456402016-04-11 12:24:23 -0700462 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700463 }
464}
465
Chris Craikf158b492016-01-12 14:45:08 -0800466void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800467 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500468 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700469
Chris Craike4db79d2015-12-22 16:32:23 -0800470 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
Chris Craik04d46eb2016-04-07 13:51:07 -0700471 mCanvasState.writableSnapshot()->applyClip(op.localClip,
Chris Craike4db79d2015-12-22 16:32:23 -0800472 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700473 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700474
Chris Craik0b7e8242015-10-28 16:50:44 -0700475 // then apply state from node properties, and defer ops
476 deferNodePropsAndOps(*op.renderNode);
477
Chris Craik6fe991e52015-10-20 09:39:42 -0700478 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700479}
480
Chris Craikf158b492016-01-12 14:45:08 -0800481void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800482 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800483 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800484 }
485}
486
Chris Craik386aa032015-12-07 17:08:25 -0800487/**
488 * Defers an unmergeable, strokeable op, accounting correctly
489 * for paint's style on the bounds being computed.
490 */
Chris Craik80d2ade2016-03-28 12:54:07 -0700491BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800492 BakedOpState::StrokeBehavior strokeBehavior) {
493 // Note: here we account for stroke when baking the op
494 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800495 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik3a5811b2016-03-22 15:03:08 -0700496 if (!bakedState) return nullptr; // quick rejected
Chris Craik80d2ade2016-03-28 12:54:07 -0700497
498 if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) {
499 bakedState->setupOpacity(op.paint);
500 }
501
Chris Craik386aa032015-12-07 17:08:25 -0800502 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
Chris Craik3a5811b2016-03-22 15:03:08 -0700503 return bakedState;
Chris Craik386aa032015-12-07 17:08:25 -0800504}
505
506/**
507 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
508 * be used, since they trigger significantly different rendering paths.
509 *
510 * Note: not used for lines/points, since they don't currently support path effects.
511 */
512static batchid_t tessBatchId(const RecordedOp& op) {
513 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700514 return paint.getPathEffect()
515 ? OpBatchType::AlphaMaskTexture
516 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
517}
518
Chris Craikf158b492016-01-12 14:45:08 -0800519void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800520 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800521}
522
Chris Craikb87eadd2016-01-06 09:16:05 -0800523static bool hasMergeableClip(const BakedOpState& state) {
524 return state.computedState.clipState
525 || state.computedState.clipState->mode == ClipMode::Rectangle;
526}
527
Chris Craikf158b492016-01-12 14:45:08 -0800528void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800529 BakedOpState* bakedState = tryBakeOpState(op);
530 if (!bakedState) return; // quick rejected
sergeyva82ffc52016-04-04 17:12:04 -0700531
532 if (op.bitmap->isOpaque()) {
533 bakedState->setupOpacity(op.paint);
534 }
Chris Craikb565df12015-10-05 13:00:52 -0700535
Chris Craik15c3f192015-12-03 12:16:56 -0800536 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
537 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
538 // MergingDrawBatch::canMergeWith()
539 if (bakedState->computedState.transform.isSimple()
540 && bakedState->computedState.transform.positiveScale()
541 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800542 && op.bitmap->colorType() != kAlpha_8_SkColorType
543 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800544 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800545 // TODO: AssetAtlas in mergeId
546 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
547 } else {
548 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
549 }
Chris Craikb565df12015-10-05 13:00:52 -0700550}
551
Chris Craikf158b492016-01-12 14:45:08 -0800552void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800553 BakedOpState* bakedState = tryBakeOpState(op);
554 if (!bakedState) return; // quick rejected
555 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
556}
557
Chris Craikf158b492016-01-12 14:45:08 -0800558void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800559 BakedOpState* bakedState = tryBakeOpState(op);
560 if (!bakedState) return; // quick rejected
561 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
562}
563
Doris Liu766431a2016-02-04 22:17:11 +0000564void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
565 const SkBitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
566 SkPaint* paint = op.vectorDrawable->getPaint();
John Reck7df9ff22016-02-10 16:08:08 -0800567 const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds,
Doris Liu766431a2016-02-04 22:17:11 +0000568 op.localMatrix,
569 op.localClip,
570 paint,
571 &bitmap,
572 Rect(bitmap.width(), bitmap.height()));
573 deferBitmapRectOp(*resolvedOp);
574}
575
Chris Craikf158b492016-01-12 14:45:08 -0800576void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800577 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
578 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
579 float x = *(op.x);
580 float y = *(op.y);
581 float radius = *(op.radius);
582 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
John Reck7df9ff22016-02-10 16:08:08 -0800583 const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800584 unmappedBounds,
585 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800586 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800587 op.paint);
588 deferOvalOp(*resolvedOp);
589}
590
Chris Craika2048482016-03-25 14:17:49 -0700591void FrameBuilder::deferColorOp(const ColorOp& op) {
592 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
593 if (!bakedState) return; // quick rejected
594 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
595}
596
Chris Craikf158b492016-01-12 14:45:08 -0800597void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700598 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craike29ce6f2015-12-10 16:25:13 -0800599 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800600 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800601}
602
Chris Craikf158b492016-01-12 14:45:08 -0800603void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800604 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800605 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800606}
607
Chris Craikf158b492016-01-12 14:45:08 -0800608void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800609 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800610}
611
Chris Craikf158b492016-01-12 14:45:08 -0800612void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800613 BakedOpState* bakedState = tryBakeOpState(op);
614 if (!bakedState) return; // quick rejected
615
616 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800617 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
618 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800619 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800620 // TODO: AssetAtlas in mergeId
621
622 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
623 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
624 } else {
625 // Use Bitmap batchId since Bitmap+Patch use same shader
626 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
627 }
628}
629
Chris Craikf158b492016-01-12 14:45:08 -0800630void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700631 auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture);
632 if (CC_LIKELY(state)) {
633 mCaches.pathCache.precache(op.path, op.paint);
634 }
Chris Craik386aa032015-12-07 17:08:25 -0800635}
636
Chris Craikf158b492016-01-12 14:45:08 -0800637void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800638 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800639 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800640}
641
Chris Craikf158b492016-01-12 14:45:08 -0800642void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800643 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800644}
645
Chris Craikf158b492016-01-12 14:45:08 -0800646void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700647 auto state = deferStrokeableOp(op, tessBatchId(op));
648 if (CC_LIKELY(state && !op.paint->getPathEffect())) {
649 // TODO: consider storing tessellation task in BakedOpState
650 mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint),
651 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
652 }
Chris Craikb565df12015-10-05 13:00:52 -0700653}
654
Chris Craikf158b492016-01-12 14:45:08 -0800655void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800656 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
657 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
John Reck7df9ff22016-02-10 16:08:08 -0800658 const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800659 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
660 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800661 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800662 op.paint, *op.rx, *op.ry);
663 deferRoundRectOp(*resolvedOp);
664}
665
Chris Craikf158b492016-01-12 14:45:08 -0800666void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800667 BakedOpState* bakedState = tryBakeOpState(op);
668 if (!bakedState) return; // quick rejected
669 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700670}
671
Chris Craikd7448e62015-12-15 10:34:36 -0800672static batchid_t textBatchId(const SkPaint& paint) {
673 // TODO: better handling of shader (since we won't care about color then)
674 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
675}
676
Chris Craikf158b492016-01-12 14:45:08 -0800677void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik7c02cab2016-03-16 17:15:12 -0700678 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
679 mAllocator, *mCanvasState.writableSnapshot(), op,
680 BakedOpState::StrokeBehavior::StyleDefined);
Chris Craik15c3f192015-12-03 12:16:56 -0800681 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800682
Chris Craikd7448e62015-12-15 10:34:36 -0800683 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800684 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800685 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
686 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800687 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
688 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
689 } else {
690 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
691 }
Chris Craik3a5811b2016-03-22 15:03:08 -0700692
693 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
694 auto& totalTransform = bakedState->computedState.transform;
695 if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) {
696 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
697 } else {
698 // Partial transform case, see BakedOpDispatcher::renderTextOp
699 float sx, sy;
700 totalTransform.decomposeScale(sx, sy);
701 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::MakeScale(
702 roundf(std::max(1.0f, sx)),
703 roundf(std::max(1.0f, sy))));
704 }
Chris Craika1717272015-11-19 13:02:43 -0800705}
706
Chris Craikf158b492016-01-12 14:45:08 -0800707void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700708 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craikd7448e62015-12-15 10:34:36 -0800709 if (!bakedState) return; // quick rejected
710 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
Chris Craik3a5811b2016-03-22 15:03:08 -0700711
712 mCaches.fontRenderer.getFontRenderer().precache(
713 op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
Chris Craikd7448e62015-12-15 10:34:36 -0800714}
715
Chris Craikf158b492016-01-12 14:45:08 -0800716void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
John Reck417ed6d2016-03-22 16:01:08 -0700717 if (CC_UNLIKELY(!op.layer->isRenderable())) return;
Chris Craikaafb01d2016-03-25 18:34:11 -0700718
719 const TextureLayerOp* textureLayerOp = &op;
720 // Now safe to access transform (which was potentially unready at record time)
721 if (!op.layer->getTransform().isIdentity()) {
722 // non-identity transform present, so 'inject it' into op by copying + replacing matrix
723 Matrix4 combinedMatrix(op.localMatrix);
724 combinedMatrix.multiply(op.layer->getTransform());
725 textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix);
726 }
727 BakedOpState* bakedState = tryBakeOpState(*textureLayerOp);
728
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800729 if (!bakedState) return; // quick rejected
730 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
731}
732
Chris Craikf158b492016-01-12 14:45:08 -0800733void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800734 float contentTranslateX, float contentTranslateY,
735 const Rect& repaintRect,
736 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700737 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500738 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700739 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700740 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800741 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800742 mCanvasState.writableSnapshot()->transform->loadTranslate(
743 contentTranslateX, contentTranslateY, 0);
744 mCanvasState.writableSnapshot()->setClip(
745 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800746
Chris Craik8ecf41c2015-11-16 10:27:59 -0800747 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800748 mLayerStack.push_back(mLayerBuilders.size());
749 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800750 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800751 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700752}
753
Chris Craikf158b492016-01-12 14:45:08 -0800754void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700755 // restore canvas, and pop finished layer off of the stack
756 mCanvasState.restore();
757 mLayerStack.pop_back();
758}
759
Chris Craikb87eadd2016-01-06 09:16:05 -0800760// TODO: defer time rejection (when bounds become empty) + tests
761// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800762void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800763 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
764 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
765
766 auto previous = mCanvasState.currentSnapshot();
767 Vector3 lightCenter = previous->getRelativeLightCenter();
768
769 // Combine all transforms used to present saveLayer content:
770 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800771 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800772 contentTransform.multiply(op.localMatrix);
773 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
774
775 Matrix4 inverseContentTransform;
776 inverseContentTransform.loadInverse(contentTransform);
777
778 // map the light center into layer-relative space
779 inverseContentTransform.mapPoint3d(lightCenter);
780
781 // Clip bounds of temporary layer to parent's clip rect, so:
782 Rect saveLayerBounds(layerWidth, layerHeight);
783 // 1) transform Rect(width, height) into parent's space
784 // note: left/top offsets put in contentTransform above
785 contentTransform.mapRect(saveLayerBounds);
786 // 2) intersect with parent's clip
787 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
788 // 3) and transform back
789 inverseContentTransform.mapRect(saveLayerBounds);
790 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
791 saveLayerBounds.roundOut();
792
793 // if bounds are reduced, will clip the layer's area by reducing required bounds...
794 layerWidth = saveLayerBounds.getWidth();
795 layerHeight = saveLayerBounds.getHeight();
796 // ...and shifting drawing content to account for left/top side clipping
797 float contentTranslateX = -saveLayerBounds.left;
798 float contentTranslateY = -saveLayerBounds.top;
799
800 saveForLayer(layerWidth, layerHeight,
801 contentTranslateX, contentTranslateY,
802 Rect(layerWidth, layerHeight),
803 lightCenter,
804 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700805}
Chris Craikb565df12015-10-05 13:00:52 -0700806
Chris Craikf158b492016-01-12 14:45:08 -0800807void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700808 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700809 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700810
811 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700812
813 // record the draw operation into the previous layer's list of draw commands
814 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
John Reck7df9ff22016-02-10 16:08:08 -0800815 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
Chris Craik6fe991e52015-10-20 09:39:42 -0700816 beginLayerOp.unmappedBounds,
817 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800818 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700819 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800820 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700821 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
822
823 if (bakedOpState) {
824 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
825 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
826 } else {
827 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800828 // TODO: need to prevent any render work from being done
829 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800830 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700831 return;
Chris Craikb565df12015-10-05 13:00:52 -0700832 }
833}
834
Chris Craikf158b492016-01-12 14:45:08 -0800835void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800836 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
837 boundsTransform.multiply(op.localMatrix);
838
839 Rect dstRect(op.unmappedBounds);
840 boundsTransform.mapRect(dstRect);
841 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
842
Chris Craik4876de12016-02-25 16:54:08 -0800843 if (dstRect.isEmpty()) {
844 // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
845 currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
846 } else {
847 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
848 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800849
Chris Craik4876de12016-02-25 16:54:08 -0800850 /**
851 * First, defer an operation to copy out the content from the rendertarget into a layer.
852 */
853 auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
854 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
855 &(currentLayer().repaintClip), dstRect, *copyToOp);
856 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
Chris Craikb87eadd2016-01-06 09:16:05 -0800857
Chris Craik4876de12016-02-25 16:54:08 -0800858 /**
859 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
860 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
861 */
862 currentLayer().deferLayerClear(dstRect);
Chris Craikb87eadd2016-01-06 09:16:05 -0800863
Chris Craik4876de12016-02-25 16:54:08 -0800864 /**
865 * And stash an operation to copy that layer back under the rendertarget until
866 * a balanced EndUnclippedLayerOp is seen
867 */
868 auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
869 bakedState = BakedOpState::directConstruct(mAllocator,
870 &(currentLayer().repaintClip), dstRect, *copyFromOp);
871 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
872 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800873}
874
Chris Craikf158b492016-01-12 14:45:08 -0800875void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800876 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
877
878 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
Chris Craikb87eadd2016-01-06 09:16:05 -0800879 currentLayer().activeUnclippedSaveLayers.pop_back();
Chris Craik4876de12016-02-25 16:54:08 -0800880 if (copyFromLayerOp) {
881 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
882 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800883}
884
Chris Craik3a5811b2016-03-22 15:03:08 -0700885void FrameBuilder::finishDefer() {
886 mCaches.fontRenderer.endPrecaching();
887}
888
Chris Craikb565df12015-10-05 13:00:52 -0700889} // namespace uirenderer
890} // namespace android