blob: a53a55a069647766569a63769a258e139343c35e [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
Chris Craik5ea17242016-01-11 14:07:59 -08002 * Copyright (C) 2016 The Android Open Source Project
Chris Craikb565df12015-10-05 13:00:52 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chris Craikf158b492016-01-12 14:45:08 -080017#include "FrameBuilder.h"
Chris Craikb565df12015-10-05 13:00:52 -070018
Chris Craik0b7e8242015-10-28 16:50:44 -070019#include "LayerUpdateQueue.h"
Chris Craik161f54b2015-11-05 11:08:52 -080020#include "RenderNode.h"
Doris Liu766431a2016-02-04 22:17:11 +000021#include "VectorDrawable.h"
Chris Craik98787e62015-11-13 10:55:30 -080022#include "renderstate/OffscreenBufferPool.h"
sergeyvdccca442016-03-21 15:38:21 -070023#include "hwui/Canvas.h"
Chris Craik161f54b2015-11-05 11:08:52 -080024#include "utils/FatVector.h"
25#include "utils/PaintUtils.h"
Chris Craik8ecf41c2015-11-16 10:27:59 -080026#include "utils/TraceUtils.h"
Chris Craikb565df12015-10-05 13:00:52 -070027
Chris Craikd3daa312015-11-06 10:59:56 -080028#include <SkPathOps.h>
Chris Craik161f54b2015-11-05 11:08:52 -080029#include <utils/TypeHelpers.h>
Chris Craikb565df12015-10-05 13:00:52 -070030
31namespace android {
32namespace uirenderer {
33
Chris Craik9cd1bbe2016-04-14 16:08:25 -070034FrameBuilder::FrameBuilder(const SkRect& clip,
Chris Craik0b7e8242015-10-28 16:50:44 -070035 uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craik9cd1bbe2016-04-14 16:08:25 -070036 const LightGeometry& lightGeometry, Caches& caches)
37 : 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
45 // Prepare to defer Fbo0
Chris Craikf158b492016-01-12 14:45:08 -080046 auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
47 mLayerBuilders.push_back(fbo0);
Chris Craik98787e62015-11-13 10:55:30 -080048 mLayerStack.push_back(0);
Chris Craikb565df12015-10-05 13:00:52 -070049 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -070050 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
Chris Craik6e068c012016-01-15 16:15:30 -080051 lightGeometry.center);
Chris Craik9cd1bbe2016-04-14 16:08:25 -070052}
Chris Craik0b7e8242015-10-28 16:50:44 -070053
Chris Craik9cd1bbe2016-04-14 16:08:25 -070054FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers,
55 const LightGeometry& lightGeometry, Caches& caches)
56 : mStdAllocator(mAllocator)
57 , mLayerBuilders(mStdAllocator)
58 , mLayerStack(mStdAllocator)
59 , mCanvasState(*this)
60 , mCaches(caches)
61 , mLightRadius(lightGeometry.radius)
62 , mDrawFbo0(false) {
63 // TODO: remove, with each layer on its own save stack
64
65 // Prepare to defer Fbo0 (which will be empty)
66 auto fbo0 = mAllocator.create<LayerBuilder>(1, 1, Rect(1, 1));
67 mLayerBuilders.push_back(fbo0);
68 mLayerStack.push_back(0);
69 mCanvasState.initializeSaveStack(1, 1,
70 0, 0, 1, 1,
71 lightGeometry.center);
72
73 deferLayers(layers);
74}
75
76void FrameBuilder::deferLayers(const LayerUpdateQueue& layers) {
Chris Craik0b7e8242015-10-28 16:50:44 -070077 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
Chris Craikf158b492016-01-12 14:45:08 -080078 // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
Chris Craik0b7e8242015-10-28 16:50:44 -070079 for (int i = layers.entries().size() - 1; i >= 0; i--) {
80 RenderNode* layerNode = layers.entries()[i].renderNode;
Chris Craike9c5fd82016-01-12 18:59:38 -080081 // only schedule repaint if node still on layer - possible it may have been
82 // removed during a dropped frame, but layers may still remain scheduled so
83 // as not to lose info on what portion is damaged
Chris Craik37413282016-05-12 17:48:51 -070084 OffscreenBuffer* layer = layerNode->getLayer();
85 if (CC_LIKELY(layer)) {
Chris Craikaff230f2016-05-04 16:27:28 -070086 ATRACE_FORMAT("Optimize HW Layer DisplayList %s %ux%u",
87 layerNode->getName(), layerNode->getWidth(), layerNode->getHeight());
88
Chris Craikd4fe4d32016-06-09 16:57:11 -070089 Rect layerDamage = layers.entries()[i].damage;
90 // TODO: ensure layer damage can't be larger than layer
91 layerDamage.doIntersect(0, 0, layer->viewportWidth, layer->viewportHeight);
Chris Craike9c5fd82016-01-12 18:59:38 -080092 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070093
Chris Craike9c5fd82016-01-12 18:59:38 -080094 // map current light center into RenderNode's coordinate space
95 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
Chris Craik37413282016-05-12 17:48:51 -070096 layer->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080097
Chris Craike9c5fd82016-01-12 18:59:38 -080098 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
99 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -0700100
Chris Craike9c5fd82016-01-12 18:59:38 -0800101 if (layerNode->getDisplayList()) {
102 deferNodeOps(*layerNode);
103 }
104 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -0700105 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700106 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700107}
Chris Craik0b7e8242015-10-28 16:50:44 -0700108
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700109void FrameBuilder::deferRenderNode(RenderNode& renderNode) {
110 renderNode.computeOrdering();
111
112 mCanvasState.save(SaveFlags::MatrixClip);
113 deferNodePropsAndOps(renderNode);
114 mCanvasState.restore();
115}
116
117void FrameBuilder::deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode) {
118 renderNode.computeOrdering();
119
120 mCanvasState.save(SaveFlags::MatrixClip);
121 mCanvasState.translate(tx, ty);
122 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
Mike Reed6c67f1d2016-12-14 10:29:54 -0500123 SkClipOp::kIntersect);
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700124 deferNodePropsAndOps(renderNode);
125 mCanvasState.restore();
126}
127
128static Rect nodeBounds(RenderNode& node) {
129 auto& props = node.properties();
130 return Rect(props.getLeft(), props.getTop(),
131 props.getRight(), props.getBottom());
132}
133
134void FrameBuilder::deferRenderNodeScene(const std::vector< sp<RenderNode> >& nodes,
135 const Rect& contentDrawBounds) {
136 if (nodes.size() < 1) return;
137 if (nodes.size() == 1) {
138 if (!nodes[0]->nothingToDraw()) {
139 deferRenderNode(*nodes[0]);
140 }
141 return;
142 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800143 // It there are multiple render nodes, they are laid out as follows:
144 // #0 - backdrop (content + caption)
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700145 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
Chong Zhangc3bd5682016-01-25 12:01:12 -0800146 // #2 - additional overlay nodes
147 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
148 // resizing however it might become partially visible. The following render loop will crop the
149 // backdrop against the content and draw the remaining part of it. It will then draw the content
150 // cropped to the backdrop (since that indicates a shrinking of the window).
151 //
152 // Additional nodes will be drawn on top with no particular clipping semantics.
153
Chong Zhangc3bd5682016-01-25 12:01:12 -0800154 // Usually the contents bounds should be mContentDrawBounds - however - we will
155 // move it towards the fixed edge to give it a more stable appearance (for the moment).
156 // If there is no content bounds we ignore the layering as stated above and start with 2.
Chong Zhangc3bd5682016-01-25 12:01:12 -0800157
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700158 // Backdrop bounds in render target space
159 const Rect backdrop = nodeBounds(*nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800160
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700161 // Bounds that content will fill in render target space (note content node bounds may be bigger)
162 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
163 content.translate(backdrop.left, backdrop.top);
164 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
165 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
166
167 // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to
168 // also fill left/top. Currently, both 2up and freeform position content at the top/left of
169 // the backdrop, so this isn't necessary.
170 if (content.right < backdrop.right) {
171 // draw backdrop to right side of content
172 deferRenderNode(0, 0, Rect(content.right, backdrop.top,
173 backdrop.right, backdrop.bottom), *nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800174 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700175 if (content.bottom < backdrop.bottom) {
176 // draw backdrop to bottom of content
177 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
178 deferRenderNode(0, 0, Rect(content.left, content.bottom,
179 content.right, backdrop.bottom), *nodes[0]);
180 }
181 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800182
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700183 if (!backdrop.isEmpty()) {
184 // content node translation to catch up with backdrop
185 float dx = contentDrawBounds.left - backdrop.left;
186 float dy = contentDrawBounds.top - backdrop.top;
187
188 Rect contentLocalClip = backdrop;
189 contentLocalClip.translate(dx, dy);
190 deferRenderNode(-dx, -dy, contentLocalClip, *nodes[1]);
191 } else {
192 deferRenderNode(*nodes[1]);
193 }
194
195 // remaining overlay nodes, simply defer
196 for (size_t index = 2; index < nodes.size(); index++) {
197 if (!nodes[index]->nothingToDraw()) {
198 deferRenderNode(*nodes[index]);
199 }
Chris Craikb565df12015-10-05 13:00:52 -0700200 }
201}
202
Chris Craikf158b492016-01-12 14:45:08 -0800203void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700204
Chris Craikf158b492016-01-12 14:45:08 -0800205void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700206
Chris Craikf158b492016-01-12 14:45:08 -0800207void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800208 const RenderProperties& properties = node.properties();
209 const Outline& outline = properties.getOutline();
210 if (properties.getAlpha() <= 0
211 || (outline.getShouldClip() && outline.isEmpty())
212 || properties.getScaleX() == 0
213 || properties.getScaleY() == 0) {
214 return; // rejected
215 }
216
217 if (properties.getLeft() != 0 || properties.getTop() != 0) {
218 mCanvasState.translate(properties.getLeft(), properties.getTop());
219 }
220 if (properties.getStaticMatrix()) {
221 mCanvasState.concatMatrix(*properties.getStaticMatrix());
222 } else if (properties.getAnimationMatrix()) {
223 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
224 }
225 if (properties.hasTransformMatrix()) {
226 if (properties.isTransformTranslateOnly()) {
227 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
228 } else {
229 mCanvasState.concatMatrix(*properties.getTransformMatrix());
230 }
231 }
232
233 const int width = properties.getWidth();
234 const int height = properties.getHeight();
235
236 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
237 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
238 int clipFlags = properties.getClippingFlags();
239 if (properties.getAlpha() < 1) {
240 if (isLayer) {
241 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
242 }
243 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
244 // simply scale rendering content's alpha
245 mCanvasState.scaleAlpha(properties.getAlpha());
246 } else {
247 // schedule saveLayer by initializing saveLayerBounds
248 saveLayerBounds.set(0, 0, width, height);
249 if (clipFlags) {
250 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
251 clipFlags = 0; // all clipping done by savelayer
252 }
253 }
254
255 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
256 // pretend alpha always causes savelayer to warn about
257 // performance problem affecting old versions
258 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
259 }
260 }
261 if (clipFlags) {
262 Rect clipRect;
263 properties.getClippingRectForFlags(clipFlags, &clipRect);
264 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
Mike Reed6c67f1d2016-12-14 10:29:54 -0500265 SkClipOp::kIntersect);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800266 }
267
268 if (properties.getRevealClip().willClip()) {
269 Rect bounds;
270 properties.getRevealClip().getBounds(&bounds);
271 mCanvasState.setClippingRoundRect(mAllocator,
272 bounds, properties.getRevealClip().getRadius());
273 } else if (properties.getOutline().willClip()) {
274 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
275 }
276
Chris Craik8913c892016-01-14 16:15:03 -0800277 bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty()
278 || (properties.getClipToBounds()
279 && mCanvasState.quickRejectConservative(0, 0, width, height));
Chris Craik7fc1b032016-02-03 19:45:06 -0800280 if (!quickRejected) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800281 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700282 if (node.getLayer()) {
283 // HW layer
John Reck7df9ff22016-02-10 16:08:08 -0800284 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700285 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
286 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800287 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700288 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
289 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800290 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
291 // draw DisplayList contents within temporary, since persisted layer could not be used.
292 // (temp layers are clipped to viewport, since they don't persist offscreen content)
293 SkPaint saveLayerPaint;
294 saveLayerPaint.setAlpha(properties.getAlpha());
John Reck7df9ff22016-02-10 16:08:08 -0800295 deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800296 saveLayerBounds,
297 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800298 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800299 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800300 deferNodeOps(node);
John Reck7df9ff22016-02-10 16:08:08 -0800301 deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
Chris Craik0b7e8242015-10-28 16:50:44 -0700302 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800303 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700304 }
305 }
306}
307
Chris Craik161f54b2015-11-05 11:08:52 -0800308typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
309
310template <typename V>
311static void buildZSortedChildList(V* zTranslatedNodes,
312 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
313 if (chunk.beginChildIndex == chunk.endChildIndex) return;
314
315 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
316 RenderNodeOp* childOp = displayList.getChildren()[i];
317 RenderNode* child = childOp->renderNode;
318 float childZ = child->properties().getZ();
319
320 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
321 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
322 childOp->skipInOrderDraw = true;
323 } else if (!child->properties().getProjectBackwards()) {
324 // regular, in order drawing DisplayList
325 childOp->skipInOrderDraw = false;
326 }
327 }
328
329 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
330 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
331}
332
333template <typename V>
334static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
335 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
336 if (zTranslatedNodes[i].key >= 0.0f) return i;
337 }
338 return zTranslatedNodes.size();
339}
340
341template <typename V>
Chris Craikd6456402016-04-11 12:24:23 -0700342void FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode,
343 const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800344 const int size = zTranslatedNodes.size();
345 if (size == 0
346 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
347 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
348 // no 3d children to draw
349 return;
350 }
351
352 /**
353 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
354 * with very similar Z heights to draw together.
355 *
356 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
357 * underneath both, and neither's shadow is drawn on top of the other.
358 */
359 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
360 size_t drawIndex, shadowIndex, endIndex;
361 if (mode == ChildrenSelectMode::Negative) {
362 drawIndex = 0;
363 endIndex = nonNegativeIndex;
364 shadowIndex = endIndex; // draw no shadows
365 } else {
366 drawIndex = nonNegativeIndex;
367 endIndex = size;
368 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
369 }
370
371 float lastCasterZ = 0.0f;
372 while (shadowIndex < endIndex || drawIndex < endIndex) {
373 if (shadowIndex < endIndex) {
374 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
375 const float casterZ = zTranslatedNodes[shadowIndex].key;
376 // attempt to render the shadow if the caster about to be drawn is its caster,
377 // OR if its caster's Z value is similar to the previous potential caster
378 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
Chris Craikd6456402016-04-11 12:24:23 -0700379 deferShadow(reorderClip, *casterNodeOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800380
381 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
382 shadowIndex++;
383 continue;
384 }
385 }
386
387 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800388 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800389 drawIndex++;
390 }
391}
392
Chris Craikd6456402016-04-11 12:24:23 -0700393void FrameBuilder::deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800394 auto& node = *casterNodeOp.renderNode;
395 auto& properties = node.properties();
396
397 if (properties.getAlpha() <= 0.0f
398 || properties.getOutline().getAlpha() <= 0.0f
399 || !properties.getOutline().getPath()
400 || properties.getScaleX() == 0
401 || properties.getScaleY() == 0) {
402 // no shadow to draw
403 return;
404 }
405
406 const SkPath* casterOutlinePath = properties.getOutline().getPath();
407 const SkPath* revealClipPath = properties.getRevealClip().getPath();
408 if (revealClipPath && revealClipPath->isEmpty()) return;
409
410 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
411
412 // holds temporary SkPath to store the result of intersections
413 SkPath* frameAllocatedPath = nullptr;
414 const SkPath* casterPath = casterOutlinePath;
415
416 // intersect the shadow-casting path with the reveal, if present
417 if (revealClipPath) {
418 frameAllocatedPath = createFrameAllocatedPath();
419
420 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
421 casterPath = frameAllocatedPath;
422 }
423
424 // intersect the shadow-casting path with the clipBounds, if present
425 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
426 if (!frameAllocatedPath) {
427 frameAllocatedPath = createFrameAllocatedPath();
428 }
429 Rect clipBounds;
430 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
431 SkPath clipBoundsPath;
432 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
433 clipBounds.right, clipBounds.bottom);
434
435 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
436 casterPath = frameAllocatedPath;
437 }
438
Chris Craikd6456402016-04-11 12:24:23 -0700439 // apply reorder clip to shadow, so it respects clip at beginning of reorderable chunk
440 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
441 mCanvasState.writableSnapshot()->applyClip(reorderClip,
442 *mCanvasState.currentSnapshot()->transform);
Chris Craik6e068c012016-01-15 16:15:30 -0800443 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
444 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
445 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
446 node.applyViewPropertyTransforms(shadowMatrixXY, false);
447 node.applyViewPropertyTransforms(shadowMatrixZ, true);
448
Chris Craik3a5811b2016-03-22 15:03:08 -0700449 sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask(
Chris Craik6e068c012016-01-15 16:15:30 -0800450 mCanvasState.currentTransform(),
451 mCanvasState.getLocalClipBounds(),
452 casterAlpha >= 1.0f,
453 casterPath,
454 &shadowMatrixXY, &shadowMatrixZ,
455 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
456 mLightRadius);
457 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
458 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
459 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
460 if (CC_LIKELY(bakedOpState)) {
461 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
462 }
Chris Craikd3daa312015-11-06 10:59:56 -0800463 }
Chris Craikd6456402016-04-11 12:24:23 -0700464 mCanvasState.restoreToCount(restoreTo);
Chris Craik161f54b2015-11-05 11:08:52 -0800465}
Chris Craikd3daa312015-11-06 10:59:56 -0800466
Chris Craikf158b492016-01-12 14:45:08 -0800467void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500468 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800469 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Chris Craik8d1f2122015-11-24 16:40:09 -0800470
Chris Craik678ff812016-03-01 13:27:54 -0800471 SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
472 if (projectionReceiverOutline) {
473 // transform the mask for this projector into render target space
474 // TODO: consider combining both transforms by stashing transform instead of applying
475 SkMatrix skCurrentTransform;
476 mCanvasState.currentTransform()->copyTo(skCurrentTransform);
477 projectionReceiverOutline->transform(
478 skCurrentTransform,
479 &transformedMaskPath);
Chris Craik5e00c7c2016-07-06 16:10:09 -0700480 mCanvasState.setProjectionPathMask(&transformedMaskPath);
Chris Craik678ff812016-03-01 13:27:54 -0800481 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800482
Chris Craik8d1f2122015-11-24 16:40:09 -0800483 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
484 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
Chris Craika748c082016-03-01 18:48:37 -0800485 RenderNode& childNode = *childOp->renderNode;
Chris Craik678ff812016-03-01 13:27:54 -0800486
Chris Craika748c082016-03-01 18:48:37 -0800487 // Draw child if it has content, but ignore state in childOp - matrix already applied to
488 // transformFromCompositingAncestor, and record-time clip is ignored when projecting
489 if (!childNode.nothingToDraw()) {
490 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800491
Chris Craika748c082016-03-01 18:48:37 -0800492 // Apply transform between ancestor and projected descendant
493 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
494
495 deferNodePropsAndOps(childNode);
496
497 mCanvasState.restoreToCount(restoreTo);
498 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800499 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800500 mCanvasState.restoreToCount(count);
501}
502
Chris Craikb565df12015-10-05 13:00:52 -0700503/**
Chris Craikf158b492016-01-12 14:45:08 -0800504 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700505 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800506 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800507 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700508 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700509#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800510 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
511void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
512 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800513 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800514
515 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
516 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikd6456402016-04-11 12:24:23 -0700517 for (auto& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800518 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
519 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
520
Chris Craikd6456402016-04-11 12:24:23 -0700521 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700522 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700523 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700524 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800525
526 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
527 && displayList.projectionReceiveIndex >= 0
528 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
529 deferProjectedChildren(renderNode);
530 }
Chris Craikb565df12015-10-05 13:00:52 -0700531 }
Chris Craikd6456402016-04-11 12:24:23 -0700532 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700533 }
534}
535
Chris Craikf158b492016-01-12 14:45:08 -0800536void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800537 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500538 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700539
Chris Craike4db79d2015-12-22 16:32:23 -0800540 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
Chris Craik04d46eb2016-04-07 13:51:07 -0700541 mCanvasState.writableSnapshot()->applyClip(op.localClip,
Chris Craike4db79d2015-12-22 16:32:23 -0800542 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700543 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700544
Chris Craik0b7e8242015-10-28 16:50:44 -0700545 // then apply state from node properties, and defer ops
546 deferNodePropsAndOps(*op.renderNode);
547
Chris Craik6fe991e52015-10-20 09:39:42 -0700548 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700549}
550
Chris Craikf158b492016-01-12 14:45:08 -0800551void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800552 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800553 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800554 }
555}
556
Chris Craik386aa032015-12-07 17:08:25 -0800557/**
558 * Defers an unmergeable, strokeable op, accounting correctly
559 * for paint's style on the bounds being computed.
560 */
Chris Craik80d2ade2016-03-28 12:54:07 -0700561BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800562 BakedOpState::StrokeBehavior strokeBehavior) {
563 // Note: here we account for stroke when baking the op
564 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800565 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik3a5811b2016-03-22 15:03:08 -0700566 if (!bakedState) return nullptr; // quick rejected
Chris Craik80d2ade2016-03-28 12:54:07 -0700567
568 if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) {
569 bakedState->setupOpacity(op.paint);
570 }
571
Chris Craik386aa032015-12-07 17:08:25 -0800572 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
Chris Craik3a5811b2016-03-22 15:03:08 -0700573 return bakedState;
Chris Craik386aa032015-12-07 17:08:25 -0800574}
575
576/**
577 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
578 * be used, since they trigger significantly different rendering paths.
579 *
580 * Note: not used for lines/points, since they don't currently support path effects.
581 */
582static batchid_t tessBatchId(const RecordedOp& op) {
583 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700584 return paint.getPathEffect()
585 ? OpBatchType::AlphaMaskTexture
586 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
587}
588
Chris Craikf158b492016-01-12 14:45:08 -0800589void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800590 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800591}
592
Chris Craikb87eadd2016-01-06 09:16:05 -0800593static bool hasMergeableClip(const BakedOpState& state) {
Chris Craikf8f56cb2016-10-14 15:08:21 -0700594 return !state.computedState.clipState
Chris Craikb87eadd2016-01-06 09:16:05 -0800595 || state.computedState.clipState->mode == ClipMode::Rectangle;
596}
597
Chris Craikf158b492016-01-12 14:45:08 -0800598void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800599 BakedOpState* bakedState = tryBakeOpState(op);
600 if (!bakedState) return; // quick rejected
sergeyva82ffc52016-04-04 17:12:04 -0700601
602 if (op.bitmap->isOpaque()) {
603 bakedState->setupOpacity(op.paint);
604 }
Chris Craikb565df12015-10-05 13:00:52 -0700605
Chris Craik15c3f192015-12-03 12:16:56 -0800606 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
607 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
608 // MergingDrawBatch::canMergeWith()
609 if (bakedState->computedState.transform.isSimple()
610 && bakedState->computedState.transform.positiveScale()
Mike Reed260ab722016-10-07 15:59:20 -0400611 && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver
Chris Craikb87eadd2016-01-06 09:16:05 -0800612 && op.bitmap->colorType() != kAlpha_8_SkColorType
613 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800614 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800615 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
616 } else {
617 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
618 }
Chris Craikb565df12015-10-05 13:00:52 -0700619}
620
Chris Craikf158b492016-01-12 14:45:08 -0800621void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800622 BakedOpState* bakedState = tryBakeOpState(op);
623 if (!bakedState) return; // quick rejected
624 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
625}
626
Chris Craikf158b492016-01-12 14:45:08 -0800627void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800628 BakedOpState* bakedState = tryBakeOpState(op);
629 if (!bakedState) return; // quick rejected
630 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
631}
632
Doris Liu766431a2016-02-04 22:17:11 +0000633void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
sergeyvec4a4b12016-10-20 18:39:04 -0700634 Bitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
Doris Liu766431a2016-02-04 22:17:11 +0000635 SkPaint* paint = op.vectorDrawable->getPaint();
John Reck7df9ff22016-02-10 16:08:08 -0800636 const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds,
Doris Liu766431a2016-02-04 22:17:11 +0000637 op.localMatrix,
638 op.localClip,
639 paint,
sergeyvec4a4b12016-10-20 18:39:04 -0700640 &bitmap,
Doris Liu766431a2016-02-04 22:17:11 +0000641 Rect(bitmap.width(), bitmap.height()));
642 deferBitmapRectOp(*resolvedOp);
643}
644
Chris Craikf158b492016-01-12 14:45:08 -0800645void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800646 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
647 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
648 float x = *(op.x);
649 float y = *(op.y);
650 float radius = *(op.radius);
651 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
John Reck7df9ff22016-02-10 16:08:08 -0800652 const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800653 unmappedBounds,
654 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800655 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800656 op.paint);
657 deferOvalOp(*resolvedOp);
658}
659
Chris Craika2048482016-03-25 14:17:49 -0700660void FrameBuilder::deferColorOp(const ColorOp& op) {
661 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
662 if (!bakedState) return; // quick rejected
663 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
664}
665
Chris Craikf158b492016-01-12 14:45:08 -0800666void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700667 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craike29ce6f2015-12-10 16:25:13 -0800668 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800669 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800670}
671
Chris Craikf158b492016-01-12 14:45:08 -0800672void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800673 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800674 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800675}
676
Chris Craikf158b492016-01-12 14:45:08 -0800677void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800678 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800679}
680
Chris Craikf158b492016-01-12 14:45:08 -0800681void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800682 BakedOpState* bakedState = tryBakeOpState(op);
683 if (!bakedState) return; // quick rejected
684
685 if (bakedState->computedState.transform.isPureTranslate()
Mike Reed260ab722016-10-07 15:59:20 -0400686 && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver
Chris Craikb87eadd2016-01-06 09:16:05 -0800687 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800688 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800689
690 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
691 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
692 } else {
693 // Use Bitmap batchId since Bitmap+Patch use same shader
694 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
695 }
696}
697
Chris Craikf158b492016-01-12 14:45:08 -0800698void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700699 auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture);
700 if (CC_LIKELY(state)) {
701 mCaches.pathCache.precache(op.path, op.paint);
702 }
Chris Craik386aa032015-12-07 17:08:25 -0800703}
704
Chris Craikf158b492016-01-12 14:45:08 -0800705void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800706 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800707 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800708}
709
Chris Craikf158b492016-01-12 14:45:08 -0800710void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800711 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800712}
713
Chris Craikf158b492016-01-12 14:45:08 -0800714void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700715 auto state = deferStrokeableOp(op, tessBatchId(op));
716 if (CC_LIKELY(state && !op.paint->getPathEffect())) {
717 // TODO: consider storing tessellation task in BakedOpState
718 mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint),
719 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
720 }
Chris Craikb565df12015-10-05 13:00:52 -0700721}
722
Chris Craikf158b492016-01-12 14:45:08 -0800723void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800724 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
725 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
John Reck7df9ff22016-02-10 16:08:08 -0800726 const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800727 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
728 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800729 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800730 op.paint, *op.rx, *op.ry);
731 deferRoundRectOp(*resolvedOp);
732}
733
Chris Craikf158b492016-01-12 14:45:08 -0800734void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800735 BakedOpState* bakedState = tryBakeOpState(op);
736 if (!bakedState) return; // quick rejected
737 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700738}
739
Chris Craikd7448e62015-12-15 10:34:36 -0800740static batchid_t textBatchId(const SkPaint& paint) {
741 // TODO: better handling of shader (since we won't care about color then)
742 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
743}
744
Chris Craikf158b492016-01-12 14:45:08 -0800745void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik7c02cab2016-03-16 17:15:12 -0700746 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
747 mAllocator, *mCanvasState.writableSnapshot(), op,
748 BakedOpState::StrokeBehavior::StyleDefined);
Chris Craik15c3f192015-12-03 12:16:56 -0800749 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800750
Chris Craikd7448e62015-12-15 10:34:36 -0800751 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800752 if (bakedState->computedState.transform.isPureTranslate()
Mike Reed260ab722016-10-07 15:59:20 -0400753 && PaintUtils::getBlendModeDirect(op.paint) == SkBlendMode::kSrcOver
Chris Craikb87eadd2016-01-06 09:16:05 -0800754 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800755 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
756 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
757 } else {
758 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
759 }
Chris Craik3a5811b2016-03-22 15:03:08 -0700760
761 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
762 auto& totalTransform = bakedState->computedState.transform;
763 if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) {
764 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
765 } else {
766 // Partial transform case, see BakedOpDispatcher::renderTextOp
767 float sx, sy;
768 totalTransform.decomposeScale(sx, sy);
769 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::MakeScale(
770 roundf(std::max(1.0f, sx)),
771 roundf(std::max(1.0f, sy))));
772 }
Chris Craika1717272015-11-19 13:02:43 -0800773}
774
Chris Craikf158b492016-01-12 14:45:08 -0800775void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700776 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craikd7448e62015-12-15 10:34:36 -0800777 if (!bakedState) return; // quick rejected
778 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
Chris Craik3a5811b2016-03-22 15:03:08 -0700779
780 mCaches.fontRenderer.getFontRenderer().precache(
781 op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
Chris Craikd7448e62015-12-15 10:34:36 -0800782}
783
Chris Craikf158b492016-01-12 14:45:08 -0800784void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
John Reck417ed6d2016-03-22 16:01:08 -0700785 if (CC_UNLIKELY(!op.layer->isRenderable())) return;
Chris Craikaafb01d2016-03-25 18:34:11 -0700786
787 const TextureLayerOp* textureLayerOp = &op;
788 // Now safe to access transform (which was potentially unready at record time)
789 if (!op.layer->getTransform().isIdentity()) {
790 // non-identity transform present, so 'inject it' into op by copying + replacing matrix
791 Matrix4 combinedMatrix(op.localMatrix);
792 combinedMatrix.multiply(op.layer->getTransform());
793 textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix);
794 }
795 BakedOpState* bakedState = tryBakeOpState(*textureLayerOp);
796
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800797 if (!bakedState) return; // quick rejected
798 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
799}
800
Chris Craikf158b492016-01-12 14:45:08 -0800801void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800802 float contentTranslateX, float contentTranslateY,
803 const Rect& repaintRect,
804 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700805 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500806 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700807 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700808 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800809 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800810 mCanvasState.writableSnapshot()->transform->loadTranslate(
811 contentTranslateX, contentTranslateY, 0);
812 mCanvasState.writableSnapshot()->setClip(
813 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800814
Chris Craik8ecf41c2015-11-16 10:27:59 -0800815 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800816 mLayerStack.push_back(mLayerBuilders.size());
817 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800818 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800819 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700820}
821
Chris Craikf158b492016-01-12 14:45:08 -0800822void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700823 // restore canvas, and pop finished layer off of the stack
824 mCanvasState.restore();
825 mLayerStack.pop_back();
826}
827
Chris Craikb87eadd2016-01-06 09:16:05 -0800828// TODO: defer time rejection (when bounds become empty) + tests
829// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800830void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800831 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
832 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
833
834 auto previous = mCanvasState.currentSnapshot();
835 Vector3 lightCenter = previous->getRelativeLightCenter();
836
837 // Combine all transforms used to present saveLayer content:
838 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800839 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800840 contentTransform.multiply(op.localMatrix);
841 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
842
843 Matrix4 inverseContentTransform;
844 inverseContentTransform.loadInverse(contentTransform);
845
846 // map the light center into layer-relative space
847 inverseContentTransform.mapPoint3d(lightCenter);
848
849 // Clip bounds of temporary layer to parent's clip rect, so:
850 Rect saveLayerBounds(layerWidth, layerHeight);
851 // 1) transform Rect(width, height) into parent's space
852 // note: left/top offsets put in contentTransform above
853 contentTransform.mapRect(saveLayerBounds);
854 // 2) intersect with parent's clip
855 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
856 // 3) and transform back
857 inverseContentTransform.mapRect(saveLayerBounds);
858 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
859 saveLayerBounds.roundOut();
860
861 // if bounds are reduced, will clip the layer's area by reducing required bounds...
862 layerWidth = saveLayerBounds.getWidth();
863 layerHeight = saveLayerBounds.getHeight();
864 // ...and shifting drawing content to account for left/top side clipping
865 float contentTranslateX = -saveLayerBounds.left;
866 float contentTranslateY = -saveLayerBounds.top;
867
868 saveForLayer(layerWidth, layerHeight,
869 contentTranslateX, contentTranslateY,
870 Rect(layerWidth, layerHeight),
871 lightCenter,
872 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700873}
Chris Craikb565df12015-10-05 13:00:52 -0700874
Chris Craikf158b492016-01-12 14:45:08 -0800875void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700876 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700877 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700878
879 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700880
John Reckc9bb1a32016-05-24 15:06:01 -0700881 // saveLayer will clip & translate the draw contents, so we need
882 // to translate the drawLayer by how much the contents was translated
883 // TODO: Unify this with beginLayerOp so we don't have to calculate this
884 // twice
885 uint32_t layerWidth = (uint32_t) beginLayerOp.unmappedBounds.getWidth();
886 uint32_t layerHeight = (uint32_t) beginLayerOp.unmappedBounds.getHeight();
887
888 auto previous = mCanvasState.currentSnapshot();
889 Vector3 lightCenter = previous->getRelativeLightCenter();
890
891 // Combine all transforms used to present saveLayer content:
892 // parent content transform * canvas transform * bounds offset
893 Matrix4 contentTransform(*(previous->transform));
894 contentTransform.multiply(beginLayerOp.localMatrix);
895 contentTransform.translate(beginLayerOp.unmappedBounds.left,
896 beginLayerOp.unmappedBounds.top);
897
898 Matrix4 inverseContentTransform;
899 inverseContentTransform.loadInverse(contentTransform);
900
901 // map the light center into layer-relative space
902 inverseContentTransform.mapPoint3d(lightCenter);
903
904 // Clip bounds of temporary layer to parent's clip rect, so:
905 Rect saveLayerBounds(layerWidth, layerHeight);
906 // 1) transform Rect(width, height) into parent's space
907 // note: left/top offsets put in contentTransform above
908 contentTransform.mapRect(saveLayerBounds);
909 // 2) intersect with parent's clip
910 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
911 // 3) and transform back
912 inverseContentTransform.mapRect(saveLayerBounds);
913 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
914 saveLayerBounds.roundOut();
915
916 Matrix4 localMatrix(beginLayerOp.localMatrix);
917 localMatrix.translate(saveLayerBounds.left, saveLayerBounds.top);
918
Chris Craik6fe991e52015-10-20 09:39:42 -0700919 // record the draw operation into the previous layer's list of draw commands
920 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
John Reck7df9ff22016-02-10 16:08:08 -0800921 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
Chris Craik6fe991e52015-10-20 09:39:42 -0700922 beginLayerOp.unmappedBounds,
John Reckc9bb1a32016-05-24 15:06:01 -0700923 localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800924 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700925 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800926 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700927 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
928
929 if (bakedOpState) {
930 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
931 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
932 } else {
933 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800934 // TODO: need to prevent any render work from being done
935 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800936 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700937 return;
Chris Craikb565df12015-10-05 13:00:52 -0700938 }
939}
940
Chris Craikf158b492016-01-12 14:45:08 -0800941void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800942 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
943 boundsTransform.multiply(op.localMatrix);
944
945 Rect dstRect(op.unmappedBounds);
946 boundsTransform.mapRect(dstRect);
Chris Craikd5a90112016-06-24 13:53:37 -0700947 dstRect.roundOut();
Chris Craikb87eadd2016-01-06 09:16:05 -0800948 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
949
Chris Craik4876de12016-02-25 16:54:08 -0800950 if (dstRect.isEmpty()) {
951 // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
952 currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
953 } else {
954 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
955 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800956
Chris Craik4876de12016-02-25 16:54:08 -0800957 /**
958 * First, defer an operation to copy out the content from the rendertarget into a layer.
959 */
960 auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
961 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
962 &(currentLayer().repaintClip), dstRect, *copyToOp);
963 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
Chris Craikb87eadd2016-01-06 09:16:05 -0800964
Chris Craik4876de12016-02-25 16:54:08 -0800965 /**
966 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
967 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
968 */
969 currentLayer().deferLayerClear(dstRect);
Chris Craikb87eadd2016-01-06 09:16:05 -0800970
Chris Craik4876de12016-02-25 16:54:08 -0800971 /**
972 * And stash an operation to copy that layer back under the rendertarget until
973 * a balanced EndUnclippedLayerOp is seen
974 */
975 auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
976 bakedState = BakedOpState::directConstruct(mAllocator,
977 &(currentLayer().repaintClip), dstRect, *copyFromOp);
978 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
979 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800980}
981
Chris Craikf158b492016-01-12 14:45:08 -0800982void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800983 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
984
985 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
Chris Craikb87eadd2016-01-06 09:16:05 -0800986 currentLayer().activeUnclippedSaveLayers.pop_back();
Chris Craik4876de12016-02-25 16:54:08 -0800987 if (copyFromLayerOp) {
988 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
989 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800990}
991
Chris Craik3a5811b2016-03-22 15:03:08 -0700992void FrameBuilder::finishDefer() {
993 mCaches.fontRenderer.endPrecaching();
994}
995
Chris Craikb565df12015-10-05 13:00:52 -0700996} // namespace uirenderer
997} // namespace android