blob: 86f9a5d73fd1e45abb5c0e9a0964d1f69ad65b8f [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
sergeyv3e9999b2017-01-19 15:37:02 -080019#include "DeferredLayerUpdater.h"
Chris Craik0b7e8242015-10-28 16:50:44 -070020#include "LayerUpdateQueue.h"
Chris Craik161f54b2015-11-05 11:08:52 -080021#include "RenderNode.h"
Doris Liu766431a2016-02-04 22:17:11 +000022#include "VectorDrawable.h"
Chris Craik98787e62015-11-13 10:55:30 -080023#include "renderstate/OffscreenBufferPool.h"
sergeyvdccca442016-03-21 15:38:21 -070024#include "hwui/Canvas.h"
Chris Craik161f54b2015-11-05 11:08:52 -080025#include "utils/FatVector.h"
26#include "utils/PaintUtils.h"
Chris Craik8ecf41c2015-11-16 10:27:59 -080027#include "utils/TraceUtils.h"
Chris Craikb565df12015-10-05 13:00:52 -070028
Chris Craikd3daa312015-11-06 10:59:56 -080029#include <SkPathOps.h>
Chris Craik161f54b2015-11-05 11:08:52 -080030#include <utils/TypeHelpers.h>
Chris Craikb565df12015-10-05 13:00:52 -070031
32namespace android {
33namespace uirenderer {
34
Chris Craik9cd1bbe2016-04-14 16:08:25 -070035FrameBuilder::FrameBuilder(const SkRect& clip,
Chris Craik0b7e8242015-10-28 16:50:44 -070036 uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craik9cd1bbe2016-04-14 16:08:25 -070037 const LightGeometry& lightGeometry, Caches& caches)
38 : mStdAllocator(mAllocator)
39 , mLayerBuilders(mStdAllocator)
40 , mLayerStack(mStdAllocator)
41 , mCanvasState(*this)
Chris Craik6e068c012016-01-15 16:15:30 -080042 , mCaches(caches)
Chris Craik6246d2782016-03-29 15:01:41 -070043 , mLightRadius(lightGeometry.radius)
Chris Craik9cd1bbe2016-04-14 16:08:25 -070044 , mDrawFbo0(true) {
Chris Craik98787e62015-11-13 10:55:30 -080045
46 // Prepare to defer Fbo0
Chris Craikf158b492016-01-12 14:45:08 -080047 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
48 mLayerBuilders.push_back(fbo0);
Chris Craik98787e62015-11-13 10:55:30 -080049 mLayerStack.push_back(0);
Chris Craikb565df12015-10-05 13:00:52 -070050 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -070051 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
Chris Craik6e068c012016-01-15 16:15:30 -080052 lightGeometry.center);
Chris Craik9cd1bbe2016-04-14 16:08:25 -070053}
Chris Craik0b7e8242015-10-28 16:50:44 -070054
Chris Craik9cd1bbe2016-04-14 16:08:25 -070055FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers,
56 const LightGeometry& lightGeometry, Caches& caches)
57 : mStdAllocator(mAllocator)
58 , mLayerBuilders(mStdAllocator)
59 , mLayerStack(mStdAllocator)
60 , mCanvasState(*this)
61 , mCaches(caches)
62 , mLightRadius(lightGeometry.radius)
63 , mDrawFbo0(false) {
64 // TODO: remove, with each layer on its own save stack
65
66 // Prepare to defer Fbo0 (which will be empty)
67 auto fbo0 = mAllocator.create<LayerBuilder>(1, 1, Rect(1, 1));
68 mLayerBuilders.push_back(fbo0);
69 mLayerStack.push_back(0);
70 mCanvasState.initializeSaveStack(1, 1,
71 0, 0, 1, 1,
72 lightGeometry.center);
73
74 deferLayers(layers);
75}
76
77void FrameBuilder::deferLayers(const LayerUpdateQueue& layers) {
Chris Craik0b7e8242015-10-28 16:50:44 -070078 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
Chris Craikf158b492016-01-12 14:45:08 -080079 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
Chris Craik0b7e8242015-10-28 16:50:44 -070080 for (int i = layers.entries().size() - 1; i >= 0; i--) {
John Reckfc29f7acd2017-03-02 13:23:16 -080081 RenderNode* layerNode = layers.entries()[i].renderNode.get();
Chris Craike9c5fd82016-01-12 18:59:38 -080082 // only schedule repaint if node still on layer - possible it may have been
83 // removed during a dropped frame, but layers may still remain scheduled so
84 // as not to lose info on what portion is damaged
Chris Craik37413282016-05-12 17:48:51 -070085 OffscreenBuffer* layer = layerNode->getLayer();
86 if (CC_LIKELY(layer)) {
Chris Craikaff230f2016-05-04 16:27:28 -070087 ATRACE_FORMAT("Optimize HW Layer DisplayList %s %ux%u",
88 layerNode->getName(), layerNode->getWidth(), layerNode->getHeight());
89
Chris Craikd4fe4d32016-06-09 16:57:11 -070090 Rect layerDamage = layers.entries()[i].damage;
91 // TODO: ensure layer damage can't be larger than layer
92 layerDamage.doIntersect(0, 0, layer->viewportWidth, layer->viewportHeight);
Chris Craike9c5fd82016-01-12 18:59:38 -080093 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070094
Chris Craike9c5fd82016-01-12 18:59:38 -080095 // map current light center into RenderNode's coordinate space
96 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
Chris Craik37413282016-05-12 17:48:51 -070097 layer->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080098
Chris Craike9c5fd82016-01-12 18:59:38 -080099 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
100 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -0700101
Chris Craike9c5fd82016-01-12 18:59:38 -0800102 if (layerNode->getDisplayList()) {
103 deferNodeOps(*layerNode);
104 }
105 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -0700106 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700107 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700108}
Chris Craik0b7e8242015-10-28 16:50:44 -0700109
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700110void FrameBuilder::deferRenderNode(RenderNode& renderNode) {
111 renderNode.computeOrdering();
112
113 mCanvasState.save(SaveFlags::MatrixClip);
114 deferNodePropsAndOps(renderNode);
115 mCanvasState.restore();
116}
117
118void FrameBuilder::deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode) {
119 renderNode.computeOrdering();
120
121 mCanvasState.save(SaveFlags::MatrixClip);
122 mCanvasState.translate(tx, ty);
123 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
Mike Reed6c67f1d2016-12-14 10:29:54 -0500124 SkClipOp::kIntersect);
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700125 deferNodePropsAndOps(renderNode);
126 mCanvasState.restore();
127}
128
129static Rect nodeBounds(RenderNode& node) {
130 auto& props = node.properties();
131 return Rect(props.getLeft(), props.getTop(),
132 props.getRight(), props.getBottom());
133}
134
135void FrameBuilder::deferRenderNodeScene(const std::vector< sp<RenderNode> >& nodes,
136 const Rect& contentDrawBounds) {
137 if (nodes.size() < 1) return;
138 if (nodes.size() == 1) {
139 if (!nodes[0]->nothingToDraw()) {
140 deferRenderNode(*nodes[0]);
141 }
142 return;
143 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800144 // It there are multiple render nodes, they are laid out as follows:
145 // #0 - backdrop (content + caption)
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700146 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
Chong Zhangc3bd5682016-01-25 12:01:12 -0800147 // #2 - additional overlay nodes
148 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
149 // resizing however it might become partially visible. The following render loop will crop the
150 // backdrop against the content and draw the remaining part of it. It will then draw the content
151 // cropped to the backdrop (since that indicates a shrinking of the window).
152 //
153 // Additional nodes will be drawn on top with no particular clipping semantics.
154
Chong Zhangc3bd5682016-01-25 12:01:12 -0800155 // Usually the contents bounds should be mContentDrawBounds - however - we will
156 // move it towards the fixed edge to give it a more stable appearance (for the moment).
157 // If there is no content bounds we ignore the layering as stated above and start with 2.
Chong Zhangc3bd5682016-01-25 12:01:12 -0800158
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700159 // Backdrop bounds in render target space
160 const Rect backdrop = nodeBounds(*nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800161
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700162 // Bounds that content will fill in render target space (note content node bounds may be bigger)
163 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
164 content.translate(backdrop.left, backdrop.top);
165 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
166 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
167
168 // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to
169 // also fill left/top. Currently, both 2up and freeform position content at the top/left of
170 // the backdrop, so this isn't necessary.
171 if (content.right < backdrop.right) {
172 // draw backdrop to right side of content
173 deferRenderNode(0, 0, Rect(content.right, backdrop.top,
174 backdrop.right, backdrop.bottom), *nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800175 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700176 if (content.bottom < backdrop.bottom) {
177 // draw backdrop to bottom of content
178 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
179 deferRenderNode(0, 0, Rect(content.left, content.bottom,
180 content.right, backdrop.bottom), *nodes[0]);
181 }
182 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800183
John Reck51c51df2017-01-23 10:24:27 -0800184 if (!nodes[1]->nothingToDraw()) {
185 if (!backdrop.isEmpty()) {
186 // content node translation to catch up with backdrop
187 float dx = contentDrawBounds.left - backdrop.left;
188 float dy = contentDrawBounds.top - backdrop.top;
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700189
John Reck51c51df2017-01-23 10:24:27 -0800190 Rect contentLocalClip = backdrop;
191 contentLocalClip.translate(dx, dy);
192 deferRenderNode(-dx, -dy, contentLocalClip, *nodes[1]);
193 } else {
194 deferRenderNode(*nodes[1]);
195 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700196 }
197
198 // remaining overlay nodes, simply defer
199 for (size_t index = 2; index < nodes.size(); index++) {
200 if (!nodes[index]->nothingToDraw()) {
201 deferRenderNode(*nodes[index]);
202 }
Chris Craikb565df12015-10-05 13:00:52 -0700203 }
204}
205
Chris Craikf158b492016-01-12 14:45:08 -0800206void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700207
Chris Craikf158b492016-01-12 14:45:08 -0800208void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700209
Chris Craikf158b492016-01-12 14:45:08 -0800210void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800211 const RenderProperties& properties = node.properties();
212 const Outline& outline = properties.getOutline();
213 if (properties.getAlpha() <= 0
214 || (outline.getShouldClip() && outline.isEmpty())
215 || properties.getScaleX() == 0
216 || properties.getScaleY() == 0) {
217 return; // rejected
218 }
219
220 if (properties.getLeft() != 0 || properties.getTop() != 0) {
221 mCanvasState.translate(properties.getLeft(), properties.getTop());
222 }
223 if (properties.getStaticMatrix()) {
224 mCanvasState.concatMatrix(*properties.getStaticMatrix());
225 } else if (properties.getAnimationMatrix()) {
226 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
227 }
228 if (properties.hasTransformMatrix()) {
229 if (properties.isTransformTranslateOnly()) {
230 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
231 } else {
232 mCanvasState.concatMatrix(*properties.getTransformMatrix());
233 }
234 }
235
236 const int width = properties.getWidth();
237 const int height = properties.getHeight();
238
239 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
240 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
241 int clipFlags = properties.getClippingFlags();
242 if (properties.getAlpha() < 1) {
243 if (isLayer) {
244 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
245 }
246 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
247 // simply scale rendering content's alpha
248 mCanvasState.scaleAlpha(properties.getAlpha());
249 } else {
250 // schedule saveLayer by initializing saveLayerBounds
251 saveLayerBounds.set(0, 0, width, height);
252 if (clipFlags) {
253 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
254 clipFlags = 0; // all clipping done by savelayer
255 }
256 }
257
258 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
259 // pretend alpha always causes savelayer to warn about
260 // performance problem affecting old versions
261 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
262 }
263 }
264 if (clipFlags) {
265 Rect clipRect;
266 properties.getClippingRectForFlags(clipFlags, &clipRect);
267 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
Mike Reed6c67f1d2016-12-14 10:29:54 -0500268 SkClipOp::kIntersect);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800269 }
270
271 if (properties.getRevealClip().willClip()) {
272 Rect bounds;
273 properties.getRevealClip().getBounds(&bounds);
274 mCanvasState.setClippingRoundRect(mAllocator,
275 bounds, properties.getRevealClip().getRadius());
276 } else if (properties.getOutline().willClip()) {
277 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
278 }
279
Chris Craik8913c892016-01-14 16:15:03 -0800280 bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty()
281 || (properties.getClipToBounds()
282 && mCanvasState.quickRejectConservative(0, 0, width, height));
Chris Craik7fc1b032016-02-03 19:45:06 -0800283 if (!quickRejected) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800284 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700285 if (node.getLayer()) {
286 // HW layer
John Reck7df9ff22016-02-10 16:08:08 -0800287 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700288 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
289 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800290 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700291 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
292 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800293 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
294 // draw DisplayList contents within temporary, since persisted layer could not be used.
295 // (temp layers are clipped to viewport, since they don't persist offscreen content)
296 SkPaint saveLayerPaint;
297 saveLayerPaint.setAlpha(properties.getAlpha());
John Reck7df9ff22016-02-10 16:08:08 -0800298 deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800299 saveLayerBounds,
300 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800301 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800302 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800303 deferNodeOps(node);
John Reck7df9ff22016-02-10 16:08:08 -0800304 deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
Chris Craik0b7e8242015-10-28 16:50:44 -0700305 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800306 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700307 }
308 }
309}
310
Chris Craik161f54b2015-11-05 11:08:52 -0800311typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
312
313template <typename V>
314static void buildZSortedChildList(V* zTranslatedNodes,
315 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
316 if (chunk.beginChildIndex == chunk.endChildIndex) return;
317
318 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
319 RenderNodeOp* childOp = displayList.getChildren()[i];
320 RenderNode* child = childOp->renderNode;
321 float childZ = child->properties().getZ();
322
323 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
324 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
325 childOp->skipInOrderDraw = true;
326 } else if (!child->properties().getProjectBackwards()) {
327 // regular, in order drawing DisplayList
328 childOp->skipInOrderDraw = false;
329 }
330 }
331
332 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
333 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
334}
335
336template <typename V>
337static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
338 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
339 if (zTranslatedNodes[i].key >= 0.0f) return i;
340 }
341 return zTranslatedNodes.size();
342}
343
344template <typename V>
Chris Craikd6456402016-04-11 12:24:23 -0700345void FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode,
346 const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800347 const int size = zTranslatedNodes.size();
348 if (size == 0
349 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
350 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
351 // no 3d children to draw
352 return;
353 }
354
355 /**
356 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
357 * with very similar Z heights to draw together.
358 *
359 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
360 * underneath both, and neither's shadow is drawn on top of the other.
361 */
362 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
363 size_t drawIndex, shadowIndex, endIndex;
364 if (mode == ChildrenSelectMode::Negative) {
365 drawIndex = 0;
366 endIndex = nonNegativeIndex;
367 shadowIndex = endIndex; // draw no shadows
368 } else {
369 drawIndex = nonNegativeIndex;
370 endIndex = size;
371 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
372 }
373
374 float lastCasterZ = 0.0f;
375 while (shadowIndex < endIndex || drawIndex < endIndex) {
376 if (shadowIndex < endIndex) {
377 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
378 const float casterZ = zTranslatedNodes[shadowIndex].key;
379 // attempt to render the shadow if the caster about to be drawn is its caster,
380 // OR if its caster's Z value is similar to the previous potential caster
381 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
Chris Craikd6456402016-04-11 12:24:23 -0700382 deferShadow(reorderClip, *casterNodeOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800383
384 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
385 shadowIndex++;
386 continue;
387 }
388 }
389
390 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800391 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800392 drawIndex++;
393 }
394}
395
Chris Craikd6456402016-04-11 12:24:23 -0700396void FrameBuilder::deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800397 auto& node = *casterNodeOp.renderNode;
398 auto& properties = node.properties();
399
400 if (properties.getAlpha() <= 0.0f
401 || properties.getOutline().getAlpha() <= 0.0f
402 || !properties.getOutline().getPath()
403 || properties.getScaleX() == 0
404 || properties.getScaleY() == 0) {
405 // no shadow to draw
406 return;
407 }
408
409 const SkPath* casterOutlinePath = properties.getOutline().getPath();
410 const SkPath* revealClipPath = properties.getRevealClip().getPath();
411 if (revealClipPath && revealClipPath->isEmpty()) return;
412
413 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
414
415 // holds temporary SkPath to store the result of intersections
416 SkPath* frameAllocatedPath = nullptr;
417 const SkPath* casterPath = casterOutlinePath;
418
419 // intersect the shadow-casting path with the reveal, if present
420 if (revealClipPath) {
421 frameAllocatedPath = createFrameAllocatedPath();
422
423 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
424 casterPath = frameAllocatedPath;
425 }
426
427 // intersect the shadow-casting path with the clipBounds, if present
428 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
429 if (!frameAllocatedPath) {
430 frameAllocatedPath = createFrameAllocatedPath();
431 }
432 Rect clipBounds;
433 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
434 SkPath clipBoundsPath;
435 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
436 clipBounds.right, clipBounds.bottom);
437
438 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
439 casterPath = frameAllocatedPath;
440 }
441
Chris Craikd6456402016-04-11 12:24:23 -0700442 // apply reorder clip to shadow, so it respects clip at beginning of reorderable chunk
443 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
444 mCanvasState.writableSnapshot()->applyClip(reorderClip,
445 *mCanvasState.currentSnapshot()->transform);
Chris Craik6e068c012016-01-15 16:15:30 -0800446 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
447 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
448 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
449 node.applyViewPropertyTransforms(shadowMatrixXY, false);
450 node.applyViewPropertyTransforms(shadowMatrixZ, true);
451
Chris Craik3a5811b2016-03-22 15:03:08 -0700452 sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask(
Chris Craik6e068c012016-01-15 16:15:30 -0800453 mCanvasState.currentTransform(),
454 mCanvasState.getLocalClipBounds(),
455 casterAlpha >= 1.0f,
456 casterPath,
457 &shadowMatrixXY, &shadowMatrixZ,
458 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
459 mLightRadius);
460 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
461 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
462 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
463 if (CC_LIKELY(bakedOpState)) {
464 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
465 }
Chris Craikd3daa312015-11-06 10:59:56 -0800466 }
Chris Craikd6456402016-04-11 12:24:23 -0700467 mCanvasState.restoreToCount(restoreTo);
Chris Craik161f54b2015-11-05 11:08:52 -0800468}
Chris Craikd3daa312015-11-06 10:59:56 -0800469
Chris Craikf158b492016-01-12 14:45:08 -0800470void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500471 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800472 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Chris Craik8d1f2122015-11-24 16:40:09 -0800473
Chris Craik678ff812016-03-01 13:27:54 -0800474 SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
475 if (projectionReceiverOutline) {
476 // transform the mask for this projector into render target space
477 // TODO: consider combining both transforms by stashing transform instead of applying
478 SkMatrix skCurrentTransform;
479 mCanvasState.currentTransform()->copyTo(skCurrentTransform);
480 projectionReceiverOutline->transform(
481 skCurrentTransform,
482 &transformedMaskPath);
Chris Craik5e00c7c2016-07-06 16:10:09 -0700483 mCanvasState.setProjectionPathMask(&transformedMaskPath);
Chris Craik678ff812016-03-01 13:27:54 -0800484 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800485
Chris Craik8d1f2122015-11-24 16:40:09 -0800486 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
487 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
Chris Craika748c082016-03-01 18:48:37 -0800488 RenderNode& childNode = *childOp->renderNode;
Chris Craik678ff812016-03-01 13:27:54 -0800489
Chris Craika748c082016-03-01 18:48:37 -0800490 // Draw child if it has content, but ignore state in childOp - matrix already applied to
491 // transformFromCompositingAncestor, and record-time clip is ignored when projecting
492 if (!childNode.nothingToDraw()) {
493 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800494
Chris Craika748c082016-03-01 18:48:37 -0800495 // Apply transform between ancestor and projected descendant
496 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
497
498 deferNodePropsAndOps(childNode);
499
500 mCanvasState.restoreToCount(restoreTo);
501 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800502 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800503 mCanvasState.restoreToCount(count);
504}
505
Chris Craikb565df12015-10-05 13:00:52 -0700506/**
Chris Craikf158b492016-01-12 14:45:08 -0800507 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700508 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800509 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800510 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700511 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700512#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800513 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
514void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
515 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800516 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800517
518 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
519 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikd6456402016-04-11 12:24:23 -0700520 for (auto& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800521 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
522 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
523
Chris Craikd6456402016-04-11 12:24:23 -0700524 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700525 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700526 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700527 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800528
529 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
530 && displayList.projectionReceiveIndex >= 0
531 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
532 deferProjectedChildren(renderNode);
533 }
Chris Craikb565df12015-10-05 13:00:52 -0700534 }
Chris Craikd6456402016-04-11 12:24:23 -0700535 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700536 }
537}
538
Chris Craikf158b492016-01-12 14:45:08 -0800539void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800540 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500541 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700542
Chris Craike4db79d2015-12-22 16:32:23 -0800543 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
Chris Craik04d46eb2016-04-07 13:51:07 -0700544 mCanvasState.writableSnapshot()->applyClip(op.localClip,
Chris Craike4db79d2015-12-22 16:32:23 -0800545 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700546 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700547
Chris Craik0b7e8242015-10-28 16:50:44 -0700548 // then apply state from node properties, and defer ops
549 deferNodePropsAndOps(*op.renderNode);
550
Chris Craik6fe991e52015-10-20 09:39:42 -0700551 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700552}
553
Chris Craikf158b492016-01-12 14:45:08 -0800554void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800555 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800556 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800557 }
558}
559
Chris Craik386aa032015-12-07 17:08:25 -0800560/**
561 * Defers an unmergeable, strokeable op, accounting correctly
562 * for paint's style on the bounds being computed.
563 */
Chris Craik80d2ade2016-03-28 12:54:07 -0700564BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik49b403d2017-03-06 13:51:43 -0800565 BakedOpState::StrokeBehavior strokeBehavior, bool expandForPathTexture) {
Chris Craik386aa032015-12-07 17:08:25 -0800566 // Note: here we account for stroke when baking the op
567 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craik49b403d2017-03-06 13:51:43 -0800568 mAllocator, *mCanvasState.writableSnapshot(), op,
569 strokeBehavior, expandForPathTexture);
Chris Craik3a5811b2016-03-22 15:03:08 -0700570 if (!bakedState) return nullptr; // quick rejected
Chris Craik80d2ade2016-03-28 12:54:07 -0700571
572 if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) {
573 bakedState->setupOpacity(op.paint);
574 }
575
Chris Craik386aa032015-12-07 17:08:25 -0800576 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
Chris Craik3a5811b2016-03-22 15:03:08 -0700577 return bakedState;
Chris Craik386aa032015-12-07 17:08:25 -0800578}
579
580/**
581 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
582 * be used, since they trigger significantly different rendering paths.
583 *
584 * Note: not used for lines/points, since they don't currently support path effects.
585 */
586static batchid_t tessBatchId(const RecordedOp& op) {
587 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700588 return paint.getPathEffect()
589 ? OpBatchType::AlphaMaskTexture
590 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
591}
592
Chris Craikf158b492016-01-12 14:45:08 -0800593void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik49b403d2017-03-06 13:51:43 -0800594 // Pass true below since arcs have a tendency to draw outside their expected bounds within
595 // their path textures. Passing true makes it more likely that we'll scissor, instead of
596 // corrupting the frame by drawing outside of clip bounds.
597 deferStrokeableOp(op, tessBatchId(op), BakedOpState::StrokeBehavior::StyleDefined, true);
Chris Craik386aa032015-12-07 17:08:25 -0800598}
599
Chris Craikb87eadd2016-01-06 09:16:05 -0800600static bool hasMergeableClip(const BakedOpState& state) {
Chris Craikf8f56cb2016-10-14 15:08:21 -0700601 return !state.computedState.clipState
Chris Craikb87eadd2016-01-06 09:16:05 -0800602 || state.computedState.clipState->mode == ClipMode::Rectangle;
603}
604
Chris Craikf158b492016-01-12 14:45:08 -0800605void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800606 BakedOpState* bakedState = tryBakeOpState(op);
607 if (!bakedState) return; // quick rejected
sergeyva82ffc52016-04-04 17:12:04 -0700608
609 if (op.bitmap->isOpaque()) {
610 bakedState->setupOpacity(op.paint);
611 }
Chris Craikb565df12015-10-05 13:00:52 -0700612
Chris Craik15c3f192015-12-03 12:16:56 -0800613 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
614 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
615 // MergingDrawBatch::canMergeWith()
616 if (bakedState->computedState.transform.isSimple()
617 && bakedState->computedState.transform.positiveScale()
Mike Reed260ab722016-10-07 15:59:20 -0400618 && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver
Chris Craikb87eadd2016-01-06 09:16:05 -0800619 && op.bitmap->colorType() != kAlpha_8_SkColorType
620 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800621 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800622 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
623 } else {
624 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
625 }
Chris Craikb565df12015-10-05 13:00:52 -0700626}
627
Chris Craikf158b492016-01-12 14:45:08 -0800628void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800629 BakedOpState* bakedState = tryBakeOpState(op);
630 if (!bakedState) return; // quick rejected
631 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
632}
633
Chris Craikf158b492016-01-12 14:45:08 -0800634void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800635 BakedOpState* bakedState = tryBakeOpState(op);
636 if (!bakedState) return; // quick rejected
637 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
638}
639
Doris Liu766431a2016-02-04 22:17:11 +0000640void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
sergeyvec4a4b12016-10-20 18:39:04 -0700641 Bitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
Doris Liu766431a2016-02-04 22:17:11 +0000642 SkPaint* paint = op.vectorDrawable->getPaint();
John Reck7df9ff22016-02-10 16:08:08 -0800643 const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds,
Doris Liu766431a2016-02-04 22:17:11 +0000644 op.localMatrix,
645 op.localClip,
646 paint,
sergeyvec4a4b12016-10-20 18:39:04 -0700647 &bitmap,
Doris Liu766431a2016-02-04 22:17:11 +0000648 Rect(bitmap.width(), bitmap.height()));
649 deferBitmapRectOp(*resolvedOp);
650}
651
Chris Craikf158b492016-01-12 14:45:08 -0800652void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800653 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
654 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
655 float x = *(op.x);
656 float y = *(op.y);
657 float radius = *(op.radius);
658 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
John Reck7df9ff22016-02-10 16:08:08 -0800659 const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800660 unmappedBounds,
661 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800662 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800663 op.paint);
664 deferOvalOp(*resolvedOp);
665}
666
Chris Craika2048482016-03-25 14:17:49 -0700667void FrameBuilder::deferColorOp(const ColorOp& op) {
668 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
669 if (!bakedState) return; // quick rejected
670 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
671}
672
Chris Craikf158b492016-01-12 14:45:08 -0800673void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700674 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craike29ce6f2015-12-10 16:25:13 -0800675 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800676 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800677}
678
Chris Craikf158b492016-01-12 14:45:08 -0800679void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800680 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800681 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800682}
683
Chris Craikf158b492016-01-12 14:45:08 -0800684void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800685 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800686}
687
Chris Craikf158b492016-01-12 14:45:08 -0800688void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800689 BakedOpState* bakedState = tryBakeOpState(op);
690 if (!bakedState) return; // quick rejected
691
692 if (bakedState->computedState.transform.isPureTranslate()
Mike Reed260ab722016-10-07 15:59:20 -0400693 && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver
Chris Craikb87eadd2016-01-06 09:16:05 -0800694 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800695 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800696
697 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
698 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
699 } else {
700 // Use Bitmap batchId since Bitmap+Patch use same shader
701 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
702 }
703}
704
Chris Craikf158b492016-01-12 14:45:08 -0800705void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700706 auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture);
707 if (CC_LIKELY(state)) {
708 mCaches.pathCache.precache(op.path, op.paint);
709 }
Chris Craik386aa032015-12-07 17:08:25 -0800710}
711
Chris Craikf158b492016-01-12 14:45:08 -0800712void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800713 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800714 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800715}
716
Chris Craikf158b492016-01-12 14:45:08 -0800717void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800718 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800719}
720
Chris Craikf158b492016-01-12 14:45:08 -0800721void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700722 auto state = deferStrokeableOp(op, tessBatchId(op));
723 if (CC_LIKELY(state && !op.paint->getPathEffect())) {
724 // TODO: consider storing tessellation task in BakedOpState
725 mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint),
726 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
727 }
Chris Craikb565df12015-10-05 13:00:52 -0700728}
729
Chris Craikf158b492016-01-12 14:45:08 -0800730void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800731 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
732 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
John Reck7df9ff22016-02-10 16:08:08 -0800733 const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800734 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
735 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800736 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800737 op.paint, *op.rx, *op.ry);
738 deferRoundRectOp(*resolvedOp);
739}
740
Chris Craikf158b492016-01-12 14:45:08 -0800741void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800742 BakedOpState* bakedState = tryBakeOpState(op);
743 if (!bakedState) return; // quick rejected
744 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700745}
746
Chris Craikd7448e62015-12-15 10:34:36 -0800747static batchid_t textBatchId(const SkPaint& paint) {
748 // TODO: better handling of shader (since we won't care about color then)
749 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
750}
751
Chris Craikf158b492016-01-12 14:45:08 -0800752void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik7c02cab2016-03-16 17:15:12 -0700753 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
754 mAllocator, *mCanvasState.writableSnapshot(), op,
Chris Craik49b403d2017-03-06 13:51:43 -0800755 BakedOpState::StrokeBehavior::StyleDefined, false);
Chris Craik15c3f192015-12-03 12:16:56 -0800756 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800757
Chris Craikd7448e62015-12-15 10:34:36 -0800758 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800759 if (bakedState->computedState.transform.isPureTranslate()
Mike Reed260ab722016-10-07 15:59:20 -0400760 && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver
Chris Craikb87eadd2016-01-06 09:16:05 -0800761 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800762 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
763 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
764 } else {
765 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
766 }
Chris Craik3a5811b2016-03-22 15:03:08 -0700767
768 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
769 auto& totalTransform = bakedState->computedState.transform;
770 if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) {
771 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
772 } else {
773 // Partial transform case, see BakedOpDispatcher::renderTextOp
774 float sx, sy;
775 totalTransform.decomposeScale(sx, sy);
776 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::MakeScale(
777 roundf(std::max(1.0f, sx)),
778 roundf(std::max(1.0f, sy))));
779 }
Chris Craika1717272015-11-19 13:02:43 -0800780}
781
Chris Craikf158b492016-01-12 14:45:08 -0800782void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700783 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craikd7448e62015-12-15 10:34:36 -0800784 if (!bakedState) return; // quick rejected
785 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
Chris Craik3a5811b2016-03-22 15:03:08 -0700786
787 mCaches.fontRenderer.getFontRenderer().precache(
788 op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
Chris Craikd7448e62015-12-15 10:34:36 -0800789}
790
Chris Craikf158b492016-01-12 14:45:08 -0800791void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
sergeyv3e9999b2017-01-19 15:37:02 -0800792 GlLayer* layer = static_cast<GlLayer*>(op.layerHandle->backingLayer());
793 if (CC_UNLIKELY(!layer || !layer->isRenderable())) return;
Chris Craikaafb01d2016-03-25 18:34:11 -0700794
795 const TextureLayerOp* textureLayerOp = &op;
796 // Now safe to access transform (which was potentially unready at record time)
sergeyv3e9999b2017-01-19 15:37:02 -0800797 if (!layer->getTransform().isIdentity()) {
Chris Craikaafb01d2016-03-25 18:34:11 -0700798 // non-identity transform present, so 'inject it' into op by copying + replacing matrix
799 Matrix4 combinedMatrix(op.localMatrix);
sergeyv3e9999b2017-01-19 15:37:02 -0800800 combinedMatrix.multiply(layer->getTransform());
Chris Craikaafb01d2016-03-25 18:34:11 -0700801 textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix);
802 }
803 BakedOpState* bakedState = tryBakeOpState(*textureLayerOp);
804
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800805 if (!bakedState) return; // quick rejected
806 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
807}
808
Chris Craikf158b492016-01-12 14:45:08 -0800809void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800810 float contentTranslateX, float contentTranslateY,
811 const Rect& repaintRect,
812 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700813 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500814 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700815 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700816 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800817 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800818 mCanvasState.writableSnapshot()->transform->loadTranslate(
819 contentTranslateX, contentTranslateY, 0);
820 mCanvasState.writableSnapshot()->setClip(
821 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800822
Chris Craik8ecf41c2015-11-16 10:27:59 -0800823 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800824 mLayerStack.push_back(mLayerBuilders.size());
825 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800826 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800827 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700828}
829
Chris Craikf158b492016-01-12 14:45:08 -0800830void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700831 // restore canvas, and pop finished layer off of the stack
832 mCanvasState.restore();
833 mLayerStack.pop_back();
834}
835
Chris Craikb87eadd2016-01-06 09:16:05 -0800836// TODO: defer time rejection (when bounds become empty) + tests
837// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800838void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800839 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
840 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
841
842 auto previous = mCanvasState.currentSnapshot();
843 Vector3 lightCenter = previous->getRelativeLightCenter();
844
845 // Combine all transforms used to present saveLayer content:
846 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800847 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800848 contentTransform.multiply(op.localMatrix);
849 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
850
851 Matrix4 inverseContentTransform;
852 inverseContentTransform.loadInverse(contentTransform);
853
854 // map the light center into layer-relative space
855 inverseContentTransform.mapPoint3d(lightCenter);
856
857 // Clip bounds of temporary layer to parent's clip rect, so:
858 Rect saveLayerBounds(layerWidth, layerHeight);
859 // 1) transform Rect(width, height) into parent's space
860 // note: left/top offsets put in contentTransform above
861 contentTransform.mapRect(saveLayerBounds);
862 // 2) intersect with parent's clip
863 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
864 // 3) and transform back
865 inverseContentTransform.mapRect(saveLayerBounds);
866 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
867 saveLayerBounds.roundOut();
868
869 // if bounds are reduced, will clip the layer's area by reducing required bounds...
870 layerWidth = saveLayerBounds.getWidth();
871 layerHeight = saveLayerBounds.getHeight();
872 // ...and shifting drawing content to account for left/top side clipping
873 float contentTranslateX = -saveLayerBounds.left;
874 float contentTranslateY = -saveLayerBounds.top;
875
876 saveForLayer(layerWidth, layerHeight,
877 contentTranslateX, contentTranslateY,
878 Rect(layerWidth, layerHeight),
879 lightCenter,
880 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700881}
Chris Craikb565df12015-10-05 13:00:52 -0700882
Chris Craikf158b492016-01-12 14:45:08 -0800883void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700884 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700885 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700886
887 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700888
John Reckc9bb1a32016-05-24 15:06:01 -0700889 // saveLayer will clip & translate the draw contents, so we need
890 // to translate the drawLayer by how much the contents was translated
891 // TODO: Unify this with beginLayerOp so we don't have to calculate this
892 // twice
893 uint32_t layerWidth = (uint32_t) beginLayerOp.unmappedBounds.getWidth();
894 uint32_t layerHeight = (uint32_t) beginLayerOp.unmappedBounds.getHeight();
895
896 auto previous = mCanvasState.currentSnapshot();
897 Vector3 lightCenter = previous->getRelativeLightCenter();
898
899 // Combine all transforms used to present saveLayer content:
900 // parent content transform * canvas transform * bounds offset
901 Matrix4 contentTransform(*(previous->transform));
902 contentTransform.multiply(beginLayerOp.localMatrix);
903 contentTransform.translate(beginLayerOp.unmappedBounds.left,
904 beginLayerOp.unmappedBounds.top);
905
906 Matrix4 inverseContentTransform;
907 inverseContentTransform.loadInverse(contentTransform);
908
909 // map the light center into layer-relative space
910 inverseContentTransform.mapPoint3d(lightCenter);
911
912 // Clip bounds of temporary layer to parent's clip rect, so:
913 Rect saveLayerBounds(layerWidth, layerHeight);
914 // 1) transform Rect(width, height) into parent's space
915 // note: left/top offsets put in contentTransform above
916 contentTransform.mapRect(saveLayerBounds);
917 // 2) intersect with parent's clip
918 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
919 // 3) and transform back
920 inverseContentTransform.mapRect(saveLayerBounds);
921 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
922 saveLayerBounds.roundOut();
923
924 Matrix4 localMatrix(beginLayerOp.localMatrix);
925 localMatrix.translate(saveLayerBounds.left, saveLayerBounds.top);
926
Chris Craik6fe991e52015-10-20 09:39:42 -0700927 // record the draw operation into the previous layer's list of draw commands
928 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
John Reck7df9ff22016-02-10 16:08:08 -0800929 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
Chris Craik6fe991e52015-10-20 09:39:42 -0700930 beginLayerOp.unmappedBounds,
John Reckc9bb1a32016-05-24 15:06:01 -0700931 localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800932 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700933 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800934 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700935 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
936
937 if (bakedOpState) {
938 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
939 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
940 } else {
941 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800942 // TODO: need to prevent any render work from being done
943 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800944 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700945 return;
Chris Craikb565df12015-10-05 13:00:52 -0700946 }
947}
948
Chris Craikf158b492016-01-12 14:45:08 -0800949void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800950 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
951 boundsTransform.multiply(op.localMatrix);
952
953 Rect dstRect(op.unmappedBounds);
954 boundsTransform.mapRect(dstRect);
Chris Craikd5a90112016-06-24 13:53:37 -0700955 dstRect.roundOut();
Chris Craikb87eadd2016-01-06 09:16:05 -0800956 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
957
Chris Craik4876de12016-02-25 16:54:08 -0800958 if (dstRect.isEmpty()) {
959 // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
960 currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
961 } else {
962 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
963 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800964
Chris Craik4876de12016-02-25 16:54:08 -0800965 /**
966 * First, defer an operation to copy out the content from the rendertarget into a layer.
967 */
968 auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
969 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
970 &(currentLayer().repaintClip), dstRect, *copyToOp);
971 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
Chris Craikb87eadd2016-01-06 09:16:05 -0800972
Chris Craik4876de12016-02-25 16:54:08 -0800973 /**
974 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
975 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
976 */
977 currentLayer().deferLayerClear(dstRect);
Chris Craikb87eadd2016-01-06 09:16:05 -0800978
Chris Craik4876de12016-02-25 16:54:08 -0800979 /**
980 * And stash an operation to copy that layer back under the rendertarget until
981 * a balanced EndUnclippedLayerOp is seen
982 */
983 auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
984 bakedState = BakedOpState::directConstruct(mAllocator,
985 &(currentLayer().repaintClip), dstRect, *copyFromOp);
986 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
987 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800988}
989
Chris Craikf158b492016-01-12 14:45:08 -0800990void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800991 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
992
993 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
Chris Craikb87eadd2016-01-06 09:16:05 -0800994 currentLayer().activeUnclippedSaveLayers.pop_back();
Chris Craik4876de12016-02-25 16:54:08 -0800995 if (copyFromLayerOp) {
996 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
997 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800998}
999
Chris Craik3a5811b2016-03-22 15:03:08 -07001000void FrameBuilder::finishDefer() {
1001 mCaches.fontRenderer.endPrecaching();
1002}
1003
Chris Craikb565df12015-10-05 13:00:52 -07001004} // namespace uirenderer
1005} // namespace android