blob: ced37ede0746d590037883f369f6da628d121ce3 [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"
sergeyvdccca442016-03-21 15:38:21 -070023#include "hwui/Canvas.h"
John Reck1bcacfd2017-11-03 10:12:19 -070024#include "renderstate/OffscreenBufferPool.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
John Reck1bcacfd2017-11-03 10:12:19 -070035FrameBuilder::FrameBuilder(const SkRect& clip, uint32_t viewportWidth, uint32_t viewportHeight,
36 const LightGeometry& lightGeometry, Caches& caches)
Chris Craik9cd1bbe2016-04-14 16:08:25 -070037 : mStdAllocator(mAllocator)
38 , mLayerBuilders(mStdAllocator)
39 , mLayerStack(mStdAllocator)
40 , mCanvasState(*this)
Chris Craik6e068c012016-01-15 16:15:30 -080041 , mCaches(caches)
Chris Craik6246d2782016-03-29 15:01:41 -070042 , mLightRadius(lightGeometry.radius)
Chris Craik9cd1bbe2016-04-14 16:08:25 -070043 , mDrawFbo0(true) {
Chris Craik98787e62015-11-13 10:55:30 -080044 // Prepare to defer Fbo0
Chris Craikf158b492016-01-12 14:45:08 -080045 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
46 mLayerBuilders.push_back(fbo0);
Chris Craik98787e62015-11-13 10:55:30 -080047 mLayerStack.push_back(0);
John Reck1bcacfd2017-11-03 10:12:19 -070048 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight, clip.fLeft, clip.fTop,
49 clip.fRight, clip.fBottom, lightGeometry.center);
Chris Craik9cd1bbe2016-04-14 16:08:25 -070050}
Chris Craik0b7e8242015-10-28 16:50:44 -070051
John Reck1bcacfd2017-11-03 10:12:19 -070052FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers, const LightGeometry& lightGeometry,
53 Caches& caches)
Chris Craik9cd1bbe2016-04-14 16:08:25 -070054 : mStdAllocator(mAllocator)
55 , mLayerBuilders(mStdAllocator)
56 , mLayerStack(mStdAllocator)
57 , mCanvasState(*this)
58 , mCaches(caches)
59 , mLightRadius(lightGeometry.radius)
60 , mDrawFbo0(false) {
61 // TODO: remove, with each layer on its own save stack
62
63 // Prepare to defer Fbo0 (which will be empty)
64 auto fbo0 = mAllocator.create<LayerBuilder>(1, 1, Rect(1, 1));
65 mLayerBuilders.push_back(fbo0);
66 mLayerStack.push_back(0);
John Reck1bcacfd2017-11-03 10:12:19 -070067 mCanvasState.initializeSaveStack(1, 1, 0, 0, 1, 1, lightGeometry.center);
Chris Craik9cd1bbe2016-04-14 16:08:25 -070068
69 deferLayers(layers);
70}
71
72void FrameBuilder::deferLayers(const LayerUpdateQueue& layers) {
Chris Craik0b7e8242015-10-28 16:50:44 -070073 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
Chris Craikf158b492016-01-12 14:45:08 -080074 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
Chris Craik0b7e8242015-10-28 16:50:44 -070075 for (int i = layers.entries().size() - 1; i >= 0; i--) {
John Reckfc29f7acd2017-03-02 13:23:16 -080076 RenderNode* layerNode = layers.entries()[i].renderNode.get();
Chris Craike9c5fd82016-01-12 18:59:38 -080077 // only schedule repaint if node still on layer - possible it may have been
78 // removed during a dropped frame, but layers may still remain scheduled so
79 // as not to lose info on what portion is damaged
Chris Craik37413282016-05-12 17:48:51 -070080 OffscreenBuffer* layer = layerNode->getLayer();
81 if (CC_LIKELY(layer)) {
John Reck1bcacfd2017-11-03 10:12:19 -070082 ATRACE_FORMAT("Optimize HW Layer DisplayList %s %ux%u", layerNode->getName(),
83 layerNode->getWidth(), layerNode->getHeight());
Chris Craikaff230f2016-05-04 16:27:28 -070084
Chris Craikd4fe4d32016-06-09 16:57:11 -070085 Rect layerDamage = layers.entries()[i].damage;
86 // TODO: ensure layer damage can't be larger than layer
87 layerDamage.doIntersect(0, 0, layer->viewportWidth, layer->viewportHeight);
Chris Craike9c5fd82016-01-12 18:59:38 -080088 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070089
Chris Craike9c5fd82016-01-12 18:59:38 -080090 // map current light center into RenderNode's coordinate space
91 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
Chris Craik37413282016-05-12 17:48:51 -070092 layer->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080093
John Reck1bcacfd2017-11-03 10:12:19 -070094 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0, layerDamage,
95 lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070096
Chris Craike9c5fd82016-01-12 18:59:38 -080097 if (layerNode->getDisplayList()) {
98 deferNodeOps(*layerNode);
99 }
100 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -0700101 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700102 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700103}
Chris Craik0b7e8242015-10-28 16:50:44 -0700104
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700105void FrameBuilder::deferRenderNode(RenderNode& renderNode) {
106 renderNode.computeOrdering();
107
108 mCanvasState.save(SaveFlags::MatrixClip);
109 deferNodePropsAndOps(renderNode);
110 mCanvasState.restore();
111}
112
113void FrameBuilder::deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode) {
114 renderNode.computeOrdering();
115
116 mCanvasState.save(SaveFlags::MatrixClip);
117 mCanvasState.translate(tx, ty);
118 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700119 SkClipOp::kIntersect);
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700120 deferNodePropsAndOps(renderNode);
121 mCanvasState.restore();
122}
123
124static Rect nodeBounds(RenderNode& node) {
125 auto& props = node.properties();
John Reck1bcacfd2017-11-03 10:12:19 -0700126 return Rect(props.getLeft(), props.getTop(), props.getRight(), props.getBottom());
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700127}
128
John Reck1bcacfd2017-11-03 10:12:19 -0700129void FrameBuilder::deferRenderNodeScene(const std::vector<sp<RenderNode> >& nodes,
130 const Rect& contentDrawBounds) {
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700131 if (nodes.size() < 1) return;
132 if (nodes.size() == 1) {
133 if (!nodes[0]->nothingToDraw()) {
134 deferRenderNode(*nodes[0]);
135 }
136 return;
137 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800138 // It there are multiple render nodes, they are laid out as follows:
139 // #0 - backdrop (content + caption)
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700140 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
Chong Zhangc3bd5682016-01-25 12:01:12 -0800141 // #2 - additional overlay nodes
142 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
143 // resizing however it might become partially visible. The following render loop will crop the
144 // backdrop against the content and draw the remaining part of it. It will then draw the content
145 // cropped to the backdrop (since that indicates a shrinking of the window).
146 //
147 // Additional nodes will be drawn on top with no particular clipping semantics.
148
Chong Zhangc3bd5682016-01-25 12:01:12 -0800149 // Usually the contents bounds should be mContentDrawBounds - however - we will
150 // move it towards the fixed edge to give it a more stable appearance (for the moment).
151 // If there is no content bounds we ignore the layering as stated above and start with 2.
Chong Zhangc3bd5682016-01-25 12:01:12 -0800152
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700153 // Backdrop bounds in render target space
154 const Rect backdrop = nodeBounds(*nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800155
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700156 // Bounds that content will fill in render target space (note content node bounds may be bigger)
157 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
158 content.translate(backdrop.left, backdrop.top);
159 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
160 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
161
162 // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to
163 // also fill left/top. Currently, both 2up and freeform position content at the top/left of
164 // the backdrop, so this isn't necessary.
165 if (content.right < backdrop.right) {
166 // draw backdrop to right side of content
John Reck1bcacfd2017-11-03 10:12:19 -0700167 deferRenderNode(0, 0,
168 Rect(content.right, backdrop.top, backdrop.right, backdrop.bottom),
169 *nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800170 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700171 if (content.bottom < backdrop.bottom) {
172 // draw backdrop to bottom of content
173 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
John Reck1bcacfd2017-11-03 10:12:19 -0700174 deferRenderNode(0, 0,
175 Rect(content.left, content.bottom, content.right, backdrop.bottom),
176 *nodes[0]);
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700177 }
178 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800179
John Reck51c51df2017-01-23 10:24:27 -0800180 if (!nodes[1]->nothingToDraw()) {
181 if (!backdrop.isEmpty()) {
182 // content node translation to catch up with backdrop
183 float dx = contentDrawBounds.left - backdrop.left;
184 float dy = contentDrawBounds.top - backdrop.top;
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700185
John Reck51c51df2017-01-23 10:24:27 -0800186 Rect contentLocalClip = backdrop;
187 contentLocalClip.translate(dx, dy);
188 deferRenderNode(-dx, -dy, contentLocalClip, *nodes[1]);
189 } else {
190 deferRenderNode(*nodes[1]);
191 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700192 }
193
194 // remaining overlay nodes, simply defer
195 for (size_t index = 2; index < nodes.size(); index++) {
196 if (!nodes[index]->nothingToDraw()) {
197 deferRenderNode(*nodes[index]);
198 }
Chris Craikb565df12015-10-05 13:00:52 -0700199 }
200}
201
Chris Craikf158b492016-01-12 14:45:08 -0800202void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700203
Chris Craikf158b492016-01-12 14:45:08 -0800204void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700205
Chris Craikf158b492016-01-12 14:45:08 -0800206void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800207 const RenderProperties& properties = node.properties();
208 const Outline& outline = properties.getOutline();
John Reck1bcacfd2017-11-03 10:12:19 -0700209 if (properties.getAlpha() <= 0 || (outline.getShouldClip() && outline.isEmpty()) ||
210 properties.getScaleX() == 0 || properties.getScaleY() == 0) {
211 return; // rejected
Chris Craik8ecf41c2015-11-16 10:27:59 -0800212 }
213
214 if (properties.getLeft() != 0 || properties.getTop() != 0) {
215 mCanvasState.translate(properties.getLeft(), properties.getTop());
216 }
217 if (properties.getStaticMatrix()) {
218 mCanvasState.concatMatrix(*properties.getStaticMatrix());
219 } else if (properties.getAnimationMatrix()) {
220 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
221 }
222 if (properties.hasTransformMatrix()) {
223 if (properties.isTransformTranslateOnly()) {
224 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
225 } else {
226 mCanvasState.concatMatrix(*properties.getTransformMatrix());
227 }
228 }
229
230 const int width = properties.getWidth();
231 const int height = properties.getHeight();
232
John Reck1bcacfd2017-11-03 10:12:19 -0700233 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
Chris Craik8ecf41c2015-11-16 10:27:59 -0800234 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
235 int clipFlags = properties.getClippingFlags();
236 if (properties.getAlpha() < 1) {
237 if (isLayer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700238 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
Chris Craik8ecf41c2015-11-16 10:27:59 -0800239 }
240 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
241 // simply scale rendering content's alpha
242 mCanvasState.scaleAlpha(properties.getAlpha());
243 } else {
244 // schedule saveLayer by initializing saveLayerBounds
245 saveLayerBounds.set(0, 0, width, height);
246 if (clipFlags) {
247 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700248 clipFlags = 0; // all clipping done by savelayer
Chris Craik8ecf41c2015-11-16 10:27:59 -0800249 }
250 }
251
252 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
253 // pretend alpha always causes savelayer to warn about
254 // performance problem affecting old versions
255 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
256 }
257 }
258 if (clipFlags) {
259 Rect clipRect;
260 properties.getClippingRectForFlags(clipFlags, &clipRect);
261 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
John Reck1bcacfd2017-11-03 10:12:19 -0700262 SkClipOp::kIntersect);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800263 }
264
265 if (properties.getRevealClip().willClip()) {
266 Rect bounds;
267 properties.getRevealClip().getBounds(&bounds);
John Reck1bcacfd2017-11-03 10:12:19 -0700268 mCanvasState.setClippingRoundRect(mAllocator, bounds,
269 properties.getRevealClip().getRadius());
Chris Craik8ecf41c2015-11-16 10:27:59 -0800270 } else if (properties.getOutline().willClip()) {
271 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
272 }
273
John Reck1bcacfd2017-11-03 10:12:19 -0700274 bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty() ||
275 (properties.getClipToBounds() &&
276 mCanvasState.quickRejectConservative(0, 0, width, height));
Chris Craik7fc1b032016-02-03 19:45:06 -0800277 if (!quickRejected) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800278 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700279 if (node.getLayer()) {
280 // HW layer
John Reck7df9ff22016-02-10 16:08:08 -0800281 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700282 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
283 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800284 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700285 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
286 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800287 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
288 // draw DisplayList contents within temporary, since persisted layer could not be used.
289 // (temp layers are clipped to viewport, since they don't persist offscreen content)
290 SkPaint saveLayerPaint;
291 saveLayerPaint.setAlpha(properties.getAlpha());
John Reck7df9ff22016-02-10 16:08:08 -0800292 deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700293 saveLayerBounds, Matrix4::identity(),
294 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800295 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800296 deferNodeOps(node);
John Reck7df9ff22016-02-10 16:08:08 -0800297 deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
Chris Craik0b7e8242015-10-28 16:50:44 -0700298 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800299 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700300 }
301 }
302}
303
Chris Craik161f54b2015-11-05 11:08:52 -0800304typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
305
306template <typename V>
John Reck1bcacfd2017-11-03 10:12:19 -0700307static void buildZSortedChildList(V* zTranslatedNodes, const DisplayList& displayList,
308 const DisplayList::Chunk& chunk) {
Chris Craik161f54b2015-11-05 11:08:52 -0800309 if (chunk.beginChildIndex == chunk.endChildIndex) return;
310
311 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
312 RenderNodeOp* childOp = displayList.getChildren()[i];
313 RenderNode* child = childOp->renderNode;
314 float childZ = child->properties().getZ();
315
316 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
317 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
318 childOp->skipInOrderDraw = true;
319 } else if (!child->properties().getProjectBackwards()) {
320 // regular, in order drawing DisplayList
321 childOp->skipInOrderDraw = false;
322 }
323 }
324
325 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
326 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
327}
328
329template <typename V>
330static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
331 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
332 if (zTranslatedNodes[i].key >= 0.0f) return i;
333 }
334 return zTranslatedNodes.size();
335}
336
337template <typename V>
Chris Craikd6456402016-04-11 12:24:23 -0700338void FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode,
John Reck1bcacfd2017-11-03 10:12:19 -0700339 const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800340 const int size = zTranslatedNodes.size();
John Reck1bcacfd2017-11-03 10:12:19 -0700341 if (size == 0 || (mode == ChildrenSelectMode::Negative && zTranslatedNodes[0].key > 0.0f) ||
342 (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
Chris Craik161f54b2015-11-05 11:08:52 -0800343 // no 3d children to draw
344 return;
345 }
346
347 /**
348 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
349 * with very similar Z heights to draw together.
350 *
351 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
352 * underneath both, and neither's shadow is drawn on top of the other.
353 */
354 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
355 size_t drawIndex, shadowIndex, endIndex;
356 if (mode == ChildrenSelectMode::Negative) {
357 drawIndex = 0;
358 endIndex = nonNegativeIndex;
John Reck1bcacfd2017-11-03 10:12:19 -0700359 shadowIndex = endIndex; // draw no shadows
Chris Craik161f54b2015-11-05 11:08:52 -0800360 } else {
361 drawIndex = nonNegativeIndex;
362 endIndex = size;
John Reck1bcacfd2017-11-03 10:12:19 -0700363 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
Chris Craik161f54b2015-11-05 11:08:52 -0800364 }
365
366 float lastCasterZ = 0.0f;
367 while (shadowIndex < endIndex || drawIndex < endIndex) {
368 if (shadowIndex < endIndex) {
369 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
370 const float casterZ = zTranslatedNodes[shadowIndex].key;
371 // attempt to render the shadow if the caster about to be drawn is its caster,
372 // OR if its caster's Z value is similar to the previous potential caster
373 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
Chris Craikd6456402016-04-11 12:24:23 -0700374 deferShadow(reorderClip, *casterNodeOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800375
John Reck1bcacfd2017-11-03 10:12:19 -0700376 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
Chris Craik161f54b2015-11-05 11:08:52 -0800377 shadowIndex++;
378 continue;
379 }
380 }
381
382 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800383 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800384 drawIndex++;
385 }
386}
387
Chris Craikd6456402016-04-11 12:24:23 -0700388void FrameBuilder::deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800389 auto& node = *casterNodeOp.renderNode;
390 auto& properties = node.properties();
391
John Reck1bcacfd2017-11-03 10:12:19 -0700392 if (properties.getAlpha() <= 0.0f || properties.getOutline().getAlpha() <= 0.0f ||
393 !properties.getOutline().getPath() || properties.getScaleX() == 0 ||
394 properties.getScaleY() == 0) {
Chris Craikd3daa312015-11-06 10:59:56 -0800395 // no shadow to draw
396 return;
397 }
398
399 const SkPath* casterOutlinePath = properties.getOutline().getPath();
400 const SkPath* revealClipPath = properties.getRevealClip().getPath();
401 if (revealClipPath && revealClipPath->isEmpty()) return;
402
403 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
404
405 // holds temporary SkPath to store the result of intersections
406 SkPath* frameAllocatedPath = nullptr;
407 const SkPath* casterPath = casterOutlinePath;
408
409 // intersect the shadow-casting path with the reveal, if present
410 if (revealClipPath) {
411 frameAllocatedPath = createFrameAllocatedPath();
412
413 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
414 casterPath = frameAllocatedPath;
415 }
416
417 // intersect the shadow-casting path with the clipBounds, if present
418 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
419 if (!frameAllocatedPath) {
420 frameAllocatedPath = createFrameAllocatedPath();
421 }
422 Rect clipBounds;
423 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
424 SkPath clipBoundsPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700425 clipBoundsPath.addRect(clipBounds.left, clipBounds.top, clipBounds.right,
426 clipBounds.bottom);
Chris Craikd3daa312015-11-06 10:59:56 -0800427
428 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
429 casterPath = frameAllocatedPath;
430 }
431
Chris Craikd6456402016-04-11 12:24:23 -0700432 // apply reorder clip to shadow, so it respects clip at beginning of reorderable chunk
433 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
434 mCanvasState.writableSnapshot()->applyClip(reorderClip,
John Reck1bcacfd2017-11-03 10:12:19 -0700435 *mCanvasState.currentSnapshot()->transform);
Chris Craik6e068c012016-01-15 16:15:30 -0800436 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
437 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
438 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
439 node.applyViewPropertyTransforms(shadowMatrixXY, false);
440 node.applyViewPropertyTransforms(shadowMatrixZ, true);
441
Chris Craik3a5811b2016-03-22 15:03:08 -0700442 sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask(
John Reck1bcacfd2017-11-03 10:12:19 -0700443 mCanvasState.currentTransform(), mCanvasState.getLocalClipBounds(),
444 casterAlpha >= 1.0f, casterPath, &shadowMatrixXY, &shadowMatrixZ,
445 mCanvasState.currentSnapshot()->getRelativeLightCenter(), mLightRadius);
Chris Craik6e068c012016-01-15 16:15:30 -0800446 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
447 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
448 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
449 if (CC_LIKELY(bakedOpState)) {
450 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
451 }
Chris Craikd3daa312015-11-06 10:59:56 -0800452 }
Chris Craikd6456402016-04-11 12:24:23 -0700453 mCanvasState.restoreToCount(restoreTo);
Chris Craik161f54b2015-11-05 11:08:52 -0800454}
Chris Craikd3daa312015-11-06 10:59:56 -0800455
Chris Craikf158b492016-01-12 14:45:08 -0800456void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500457 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800458 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Chris Craik8d1f2122015-11-24 16:40:09 -0800459
John Reck1bcacfd2017-11-03 10:12:19 -0700460 SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
Chris Craik678ff812016-03-01 13:27:54 -0800461 if (projectionReceiverOutline) {
462 // transform the mask for this projector into render target space
463 // TODO: consider combining both transforms by stashing transform instead of applying
464 SkMatrix skCurrentTransform;
465 mCanvasState.currentTransform()->copyTo(skCurrentTransform);
John Reck1bcacfd2017-11-03 10:12:19 -0700466 projectionReceiverOutline->transform(skCurrentTransform, &transformedMaskPath);
Chris Craik5e00c7c2016-07-06 16:10:09 -0700467 mCanvasState.setProjectionPathMask(&transformedMaskPath);
Chris Craik678ff812016-03-01 13:27:54 -0800468 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800469
Chris Craik8d1f2122015-11-24 16:40:09 -0800470 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
471 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
Chris Craika748c082016-03-01 18:48:37 -0800472 RenderNode& childNode = *childOp->renderNode;
Chris Craik678ff812016-03-01 13:27:54 -0800473
Chris Craika748c082016-03-01 18:48:37 -0800474 // Draw child if it has content, but ignore state in childOp - matrix already applied to
475 // transformFromCompositingAncestor, and record-time clip is ignored when projecting
476 if (!childNode.nothingToDraw()) {
477 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800478
Chris Craika748c082016-03-01 18:48:37 -0800479 // Apply transform between ancestor and projected descendant
480 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
481
482 deferNodePropsAndOps(childNode);
483
484 mCanvasState.restoreToCount(restoreTo);
485 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800486 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800487 mCanvasState.restoreToCount(count);
488}
489
Chris Craikb565df12015-10-05 13:00:52 -0700490/**
Chris Craikf158b492016-01-12 14:45:08 -0800491 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700492 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800493 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800494 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700495 */
John Reck1bcacfd2017-11-03 10:12:19 -0700496#define OP_RECEIVER(Type) \
497 [](FrameBuilder& frameBuilder, const RecordedOp& op) { \
498 frameBuilder.defer##Type(static_cast<const Type&>(op)); \
499 },
Chris Craikf158b492016-01-12 14:45:08 -0800500void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
John Reck1bcacfd2017-11-03 10:12:19 -0700501 typedef void (*OpDispatcher)(FrameBuilder & frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800502 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800503
504 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
505 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikd6456402016-04-11 12:24:23 -0700506 for (auto& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800507 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
508 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
509
Chris Craikd6456402016-04-11 12:24:23 -0700510 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700511 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700512 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700513 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800514
John Reck1bcacfd2017-11-03 10:12:19 -0700515 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty() &&
516 displayList.projectionReceiveIndex >= 0 &&
517 static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800518 deferProjectedChildren(renderNode);
519 }
Chris Craikb565df12015-10-05 13:00:52 -0700520 }
Chris Craikd6456402016-04-11 12:24:23 -0700521 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700522 }
523}
524
Chris Craikf158b492016-01-12 14:45:08 -0800525void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800526 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500527 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700528
Chris Craike4db79d2015-12-22 16:32:23 -0800529 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
Chris Craik04d46eb2016-04-07 13:51:07 -0700530 mCanvasState.writableSnapshot()->applyClip(op.localClip,
John Reck1bcacfd2017-11-03 10:12:19 -0700531 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700532 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700533
Chris Craik0b7e8242015-10-28 16:50:44 -0700534 // then apply state from node properties, and defer ops
535 deferNodePropsAndOps(*op.renderNode);
536
Chris Craik6fe991e52015-10-20 09:39:42 -0700537 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700538}
539
Chris Craikf158b492016-01-12 14:45:08 -0800540void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800541 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800542 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800543 }
544}
545
Chris Craik386aa032015-12-07 17:08:25 -0800546/**
547 * Defers an unmergeable, strokeable op, accounting correctly
548 * for paint's style on the bounds being computed.
549 */
Chris Craik80d2ade2016-03-28 12:54:07 -0700550BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
John Reck1bcacfd2017-11-03 10:12:19 -0700551 BakedOpState::StrokeBehavior strokeBehavior,
552 bool expandForPathTexture) {
Chris Craik386aa032015-12-07 17:08:25 -0800553 // Note: here we account for stroke when baking the op
554 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
John Reck1bcacfd2017-11-03 10:12:19 -0700555 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior, expandForPathTexture);
556 if (!bakedState) return nullptr; // quick rejected
Chris Craik80d2ade2016-03-28 12:54:07 -0700557
558 if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) {
559 bakedState->setupOpacity(op.paint);
560 }
561
Chris Craik386aa032015-12-07 17:08:25 -0800562 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
Chris Craik3a5811b2016-03-22 15:03:08 -0700563 return bakedState;
Chris Craik386aa032015-12-07 17:08:25 -0800564}
565
566/**
567 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
568 * be used, since they trigger significantly different rendering paths.
569 *
570 * Note: not used for lines/points, since they don't currently support path effects.
571 */
572static batchid_t tessBatchId(const RecordedOp& op) {
573 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700574 return paint.getPathEffect()
John Reck1bcacfd2017-11-03 10:12:19 -0700575 ? OpBatchType::AlphaMaskTexture
576 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700577}
578
Chris Craikf158b492016-01-12 14:45:08 -0800579void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik49b403d2017-03-06 13:51:43 -0800580 // Pass true below since arcs have a tendency to draw outside their expected bounds within
581 // their path textures. Passing true makes it more likely that we'll scissor, instead of
582 // corrupting the frame by drawing outside of clip bounds.
583 deferStrokeableOp(op, tessBatchId(op), BakedOpState::StrokeBehavior::StyleDefined, true);
Chris Craik386aa032015-12-07 17:08:25 -0800584}
585
Chris Craikb87eadd2016-01-06 09:16:05 -0800586static bool hasMergeableClip(const BakedOpState& state) {
John Reck1bcacfd2017-11-03 10:12:19 -0700587 return !state.computedState.clipState ||
588 state.computedState.clipState->mode == ClipMode::Rectangle;
Chris Craikb87eadd2016-01-06 09:16:05 -0800589}
590
Chris Craikf158b492016-01-12 14:45:08 -0800591void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800592 BakedOpState* bakedState = tryBakeOpState(op);
John Reck1bcacfd2017-11-03 10:12:19 -0700593 if (!bakedState) return; // quick rejected
sergeyva82ffc52016-04-04 17:12:04 -0700594
595 if (op.bitmap->isOpaque()) {
596 bakedState->setupOpacity(op.paint);
597 }
Chris Craikb565df12015-10-05 13:00:52 -0700598
Chris Craik15c3f192015-12-03 12:16:56 -0800599 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
600 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
601 // MergingDrawBatch::canMergeWith()
John Reck1bcacfd2017-11-03 10:12:19 -0700602 if (bakedState->computedState.transform.isSimple() &&
603 bakedState->computedState.transform.positiveScale() &&
604 PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver &&
605 op.bitmap->colorType() != kAlpha_8_SkColorType && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800606 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800607 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
608 } else {
609 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
610 }
Chris Craikb565df12015-10-05 13:00:52 -0700611}
612
Chris Craikf158b492016-01-12 14:45:08 -0800613void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800614 BakedOpState* bakedState = tryBakeOpState(op);
John Reck1bcacfd2017-11-03 10:12:19 -0700615 if (!bakedState) return; // quick rejected
Chris Craikf09ff5a2015-12-08 17:21:58 -0800616 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
617}
618
Chris Craikf158b492016-01-12 14:45:08 -0800619void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800620 BakedOpState* bakedState = tryBakeOpState(op);
John Reck1bcacfd2017-11-03 10:12:19 -0700621 if (!bakedState) return; // quick rejected
Chris Craikf09ff5a2015-12-08 17:21:58 -0800622 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
623}
624
Doris Liu766431a2016-02-04 22:17:11 +0000625void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
sergeyvec4a4b12016-10-20 18:39:04 -0700626 Bitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
Doris Liu766431a2016-02-04 22:17:11 +0000627 SkPaint* paint = op.vectorDrawable->getPaint();
John Reck1bcacfd2017-11-03 10:12:19 -0700628 const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(
629 op.unmappedBounds, op.localMatrix, op.localClip, paint, &bitmap,
Doris Liu766431a2016-02-04 22:17:11 +0000630 Rect(bitmap.width(), bitmap.height()));
631 deferBitmapRectOp(*resolvedOp);
632}
633
Chris Craikf158b492016-01-12 14:45:08 -0800634void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800635 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
636 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
637 float x = *(op.x);
638 float y = *(op.y);
639 float radius = *(op.radius);
640 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
John Reck1bcacfd2017-11-03 10:12:19 -0700641 const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(unmappedBounds, op.localMatrix,
642 op.localClip, op.paint);
Chris Craik268a9c02015-12-09 18:05:12 -0800643 deferOvalOp(*resolvedOp);
644}
645
Chris Craika2048482016-03-25 14:17:49 -0700646void FrameBuilder::deferColorOp(const ColorOp& op) {
647 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
John Reck1bcacfd2017-11-03 10:12:19 -0700648 if (!bakedState) return; // quick rejected
Chris Craika2048482016-03-25 14:17:49 -0700649 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
650}
651
Chris Craikf158b492016-01-12 14:45:08 -0800652void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700653 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
John Reck1bcacfd2017-11-03 10:12:19 -0700654 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800655 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800656}
657
Chris Craikf158b492016-01-12 14:45:08 -0800658void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800659 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800660 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800661}
662
Chris Craikf158b492016-01-12 14:45:08 -0800663void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800664 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800665}
666
Chris Craikf158b492016-01-12 14:45:08 -0800667void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800668 BakedOpState* bakedState = tryBakeOpState(op);
John Reck1bcacfd2017-11-03 10:12:19 -0700669 if (!bakedState) return; // quick rejected
Chris Craikf09ff5a2015-12-08 17:21:58 -0800670
John Reck1bcacfd2017-11-03 10:12:19 -0700671 if (bakedState->computedState.transform.isPureTranslate() &&
672 PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver &&
673 hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800674 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800675
676 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
677 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
678 } else {
679 // Use Bitmap batchId since Bitmap+Patch use same shader
680 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
681 }
682}
683
Chris Craikf158b492016-01-12 14:45:08 -0800684void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700685 auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture);
686 if (CC_LIKELY(state)) {
687 mCaches.pathCache.precache(op.path, op.paint);
688 }
Chris Craik386aa032015-12-07 17:08:25 -0800689}
690
Chris Craikf158b492016-01-12 14:45:08 -0800691void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800692 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800693 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800694}
695
Chris Craikf158b492016-01-12 14:45:08 -0800696void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800697 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800698}
699
Chris Craikf158b492016-01-12 14:45:08 -0800700void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700701 auto state = deferStrokeableOp(op, tessBatchId(op));
702 if (CC_LIKELY(state && !op.paint->getPathEffect())) {
703 // TODO: consider storing tessellation task in BakedOpState
704 mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint),
John Reck1bcacfd2017-11-03 10:12:19 -0700705 op.unmappedBounds.getWidth(),
706 op.unmappedBounds.getHeight(), op.rx, op.ry);
Chris Craik3a5811b2016-03-22 15:03:08 -0700707 }
Chris Craikb565df12015-10-05 13:00:52 -0700708}
709
Chris Craikf158b492016-01-12 14:45:08 -0800710void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800711 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
712 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
John Reck7df9ff22016-02-10 16:08:08 -0800713 const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700714 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)), op.localMatrix, op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800715 op.paint, *op.rx, *op.ry);
716 deferRoundRectOp(*resolvedOp);
717}
718
Chris Craikf158b492016-01-12 14:45:08 -0800719void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800720 BakedOpState* bakedState = tryBakeOpState(op);
John Reck1bcacfd2017-11-03 10:12:19 -0700721 if (!bakedState) return; // quick rejected
Chris Craik15c3f192015-12-03 12:16:56 -0800722 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700723}
724
Chris Craikd7448e62015-12-15 10:34:36 -0800725static batchid_t textBatchId(const SkPaint& paint) {
726 // TODO: better handling of shader (since we won't care about color then)
727 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
728}
729
Chris Craikf158b492016-01-12 14:45:08 -0800730void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik7c02cab2016-03-16 17:15:12 -0700731 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
732 mAllocator, *mCanvasState.writableSnapshot(), op,
Chris Craik49b403d2017-03-06 13:51:43 -0800733 BakedOpState::StrokeBehavior::StyleDefined, false);
John Reck1bcacfd2017-11-03 10:12:19 -0700734 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800735
Chris Craikd7448e62015-12-15 10:34:36 -0800736 batchid_t batchId = textBatchId(*(op.paint));
John Reck1bcacfd2017-11-03 10:12:19 -0700737 if (bakedState->computedState.transform.isPureTranslate() &&
738 PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver &&
739 hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800740 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
741 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
742 } else {
743 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
744 }
Chris Craik3a5811b2016-03-22 15:03:08 -0700745
746 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
747 auto& totalTransform = bakedState->computedState.transform;
748 if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) {
749 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
750 } else {
751 // Partial transform case, see BakedOpDispatcher::renderTextOp
752 float sx, sy;
753 totalTransform.decomposeScale(sx, sy);
John Reck1bcacfd2017-11-03 10:12:19 -0700754 fontRenderer.precache(
755 op.paint, op.glyphs, op.glyphCount,
756 SkMatrix::MakeScale(roundf(std::max(1.0f, sx)), roundf(std::max(1.0f, sy))));
Chris Craik3a5811b2016-03-22 15:03:08 -0700757 }
Chris Craika1717272015-11-19 13:02:43 -0800758}
759
Chris Craikf158b492016-01-12 14:45:08 -0800760void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700761 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
John Reck1bcacfd2017-11-03 10:12:19 -0700762 if (!bakedState) return; // quick rejected
Chris Craikd7448e62015-12-15 10:34:36 -0800763 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
Chris Craik3a5811b2016-03-22 15:03:08 -0700764
John Reck1bcacfd2017-11-03 10:12:19 -0700765 mCaches.fontRenderer.getFontRenderer().precache(op.paint, op.glyphs, op.glyphCount,
766 SkMatrix::I());
Chris Craikd7448e62015-12-15 10:34:36 -0800767}
768
Chris Craikf158b492016-01-12 14:45:08 -0800769void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
sergeyv3e9999b2017-01-19 15:37:02 -0800770 GlLayer* layer = static_cast<GlLayer*>(op.layerHandle->backingLayer());
771 if (CC_UNLIKELY(!layer || !layer->isRenderable())) return;
Chris Craikaafb01d2016-03-25 18:34:11 -0700772
773 const TextureLayerOp* textureLayerOp = &op;
774 // Now safe to access transform (which was potentially unready at record time)
sergeyv3e9999b2017-01-19 15:37:02 -0800775 if (!layer->getTransform().isIdentity()) {
Chris Craikaafb01d2016-03-25 18:34:11 -0700776 // non-identity transform present, so 'inject it' into op by copying + replacing matrix
777 Matrix4 combinedMatrix(op.localMatrix);
sergeyv3e9999b2017-01-19 15:37:02 -0800778 combinedMatrix.multiply(layer->getTransform());
Chris Craikaafb01d2016-03-25 18:34:11 -0700779 textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix);
780 }
781 BakedOpState* bakedState = tryBakeOpState(*textureLayerOp);
782
John Reck1bcacfd2017-11-03 10:12:19 -0700783 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800784 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
785}
786
John Reck1bcacfd2017-11-03 10:12:19 -0700787void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight, float contentTranslateX,
788 float contentTranslateY, const Rect& repaintRect,
789 const Vector3& lightCenter, const BeginLayerOp* beginLayerOp,
790 RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500791 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700792 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700793 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800794 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
John Reck1bcacfd2017-11-03 10:12:19 -0700795 mCanvasState.writableSnapshot()->transform->loadTranslate(contentTranslateX, contentTranslateY,
796 0);
797 mCanvasState.writableSnapshot()->setClip(repaintRect.left, repaintRect.top, repaintRect.right,
798 repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800799
Chris Craik8ecf41c2015-11-16 10:27:59 -0800800 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800801 mLayerStack.push_back(mLayerBuilders.size());
John Reck1bcacfd2017-11-03 10:12:19 -0700802 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight, repaintRect,
803 beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800804 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700805}
806
Chris Craikf158b492016-01-12 14:45:08 -0800807void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700808 // restore canvas, and pop finished layer off of the stack
809 mCanvasState.restore();
810 mLayerStack.pop_back();
811}
812
Chris Craikb87eadd2016-01-06 09:16:05 -0800813// TODO: defer time rejection (when bounds become empty) + tests
814// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800815void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
John Reck1bcacfd2017-11-03 10:12:19 -0700816 uint32_t layerWidth = (uint32_t)op.unmappedBounds.getWidth();
817 uint32_t layerHeight = (uint32_t)op.unmappedBounds.getHeight();
Chris Craik8ecf41c2015-11-16 10:27:59 -0800818
819 auto previous = mCanvasState.currentSnapshot();
820 Vector3 lightCenter = previous->getRelativeLightCenter();
821
822 // Combine all transforms used to present saveLayer content:
823 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800824 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800825 contentTransform.multiply(op.localMatrix);
826 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
827
828 Matrix4 inverseContentTransform;
829 inverseContentTransform.loadInverse(contentTransform);
830
831 // map the light center into layer-relative space
832 inverseContentTransform.mapPoint3d(lightCenter);
833
834 // Clip bounds of temporary layer to parent's clip rect, so:
835 Rect saveLayerBounds(layerWidth, layerHeight);
836 // 1) transform Rect(width, height) into parent's space
837 // note: left/top offsets put in contentTransform above
838 contentTransform.mapRect(saveLayerBounds);
839 // 2) intersect with parent's clip
840 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
841 // 3) and transform back
842 inverseContentTransform.mapRect(saveLayerBounds);
843 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
844 saveLayerBounds.roundOut();
845
846 // if bounds are reduced, will clip the layer's area by reducing required bounds...
847 layerWidth = saveLayerBounds.getWidth();
848 layerHeight = saveLayerBounds.getHeight();
849 // ...and shifting drawing content to account for left/top side clipping
850 float contentTranslateX = -saveLayerBounds.left;
851 float contentTranslateY = -saveLayerBounds.top;
852
John Reck1bcacfd2017-11-03 10:12:19 -0700853 saveForLayer(layerWidth, layerHeight, contentTranslateX, contentTranslateY,
854 Rect(layerWidth, layerHeight), lightCenter, &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700855}
Chris Craikb565df12015-10-05 13:00:52 -0700856
Chris Craikf158b492016-01-12 14:45:08 -0800857void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700858 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700859 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700860
861 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700862
John Reckc9bb1a32016-05-24 15:06:01 -0700863 // saveLayer will clip & translate the draw contents, so we need
864 // to translate the drawLayer by how much the contents was translated
865 // TODO: Unify this with beginLayerOp so we don't have to calculate this
866 // twice
John Reck1bcacfd2017-11-03 10:12:19 -0700867 uint32_t layerWidth = (uint32_t)beginLayerOp.unmappedBounds.getWidth();
868 uint32_t layerHeight = (uint32_t)beginLayerOp.unmappedBounds.getHeight();
John Reckc9bb1a32016-05-24 15:06:01 -0700869
870 auto previous = mCanvasState.currentSnapshot();
871 Vector3 lightCenter = previous->getRelativeLightCenter();
872
873 // Combine all transforms used to present saveLayer content:
874 // parent content transform * canvas transform * bounds offset
875 Matrix4 contentTransform(*(previous->transform));
876 contentTransform.multiply(beginLayerOp.localMatrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700877 contentTransform.translate(beginLayerOp.unmappedBounds.left, beginLayerOp.unmappedBounds.top);
John Reckc9bb1a32016-05-24 15:06:01 -0700878
879 Matrix4 inverseContentTransform;
880 inverseContentTransform.loadInverse(contentTransform);
881
882 // map the light center into layer-relative space
883 inverseContentTransform.mapPoint3d(lightCenter);
884
885 // Clip bounds of temporary layer to parent's clip rect, so:
886 Rect saveLayerBounds(layerWidth, layerHeight);
887 // 1) transform Rect(width, height) into parent's space
888 // note: left/top offsets put in contentTransform above
889 contentTransform.mapRect(saveLayerBounds);
890 // 2) intersect with parent's clip
891 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
892 // 3) and transform back
893 inverseContentTransform.mapRect(saveLayerBounds);
894 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
895 saveLayerBounds.roundOut();
896
897 Matrix4 localMatrix(beginLayerOp.localMatrix);
898 localMatrix.translate(saveLayerBounds.left, saveLayerBounds.top);
899
Chris Craik6fe991e52015-10-20 09:39:42 -0700900 // record the draw operation into the previous layer's list of draw commands
901 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
John Reck7df9ff22016-02-10 16:08:08 -0800902 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
John Reck1bcacfd2017-11-03 10:12:19 -0700903 beginLayerOp.unmappedBounds, localMatrix, beginLayerOp.localClip, beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800904 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700905 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
906
907 if (bakedOpState) {
908 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
909 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
910 } else {
911 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800912 // TODO: need to prevent any render work from being done
913 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800914 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700915 return;
Chris Craikb565df12015-10-05 13:00:52 -0700916 }
917}
918
Chris Craikf158b492016-01-12 14:45:08 -0800919void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800920 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
921 boundsTransform.multiply(op.localMatrix);
922
923 Rect dstRect(op.unmappedBounds);
924 boundsTransform.mapRect(dstRect);
Chris Craikd5a90112016-06-24 13:53:37 -0700925 dstRect.roundOut();
Chris Craikb87eadd2016-01-06 09:16:05 -0800926 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
927
Chris Craik4876de12016-02-25 16:54:08 -0800928 if (dstRect.isEmpty()) {
929 // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
930 currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
931 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700932 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will
933 // consume)
Chris Craik4876de12016-02-25 16:54:08 -0800934 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800935
Chris Craik4876de12016-02-25 16:54:08 -0800936 /**
937 * First, defer an operation to copy out the content from the rendertarget into a layer.
938 */
939 auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
John Reck1bcacfd2017-11-03 10:12:19 -0700940 BakedOpState* bakedState = BakedOpState::directConstruct(
941 mAllocator, &(currentLayer().repaintClip), dstRect, *copyToOp);
Chris Craik4876de12016-02-25 16:54:08 -0800942 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
Chris Craikb87eadd2016-01-06 09:16:05 -0800943
Chris Craik4876de12016-02-25 16:54:08 -0800944 /**
945 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
946 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
947 */
948 currentLayer().deferLayerClear(dstRect);
Chris Craikb87eadd2016-01-06 09:16:05 -0800949
Chris Craik4876de12016-02-25 16:54:08 -0800950 /**
951 * And stash an operation to copy that layer back under the rendertarget until
952 * a balanced EndUnclippedLayerOp is seen
953 */
954 auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
John Reck1bcacfd2017-11-03 10:12:19 -0700955 bakedState = BakedOpState::directConstruct(mAllocator, &(currentLayer().repaintClip),
956 dstRect, *copyFromOp);
Chris Craik4876de12016-02-25 16:54:08 -0800957 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
958 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800959}
960
Chris Craikf158b492016-01-12 14:45:08 -0800961void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800962 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
963
964 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
Chris Craikb87eadd2016-01-06 09:16:05 -0800965 currentLayer().activeUnclippedSaveLayers.pop_back();
Chris Craik4876de12016-02-25 16:54:08 -0800966 if (copyFromLayerOp) {
967 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
968 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800969}
970
Chris Craik3a5811b2016-03-22 15:03:08 -0700971void FrameBuilder::finishDefer() {
972 mCaches.fontRenderer.endPrecaching();
973}
974
John Reck1bcacfd2017-11-03 10:12:19 -0700975} // namespace uirenderer
976} // namespace android