blob: 502f027029a78c6759407c5ce53f7d0d60a79f7f [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
84 if (CC_LIKELY(layerNode->getLayer() != nullptr)) {
85 const Rect& layerDamage = layers.entries()[i].damage;
86 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070087
Chris Craike9c5fd82016-01-12 18:59:38 -080088 // map current light center into RenderNode's coordinate space
89 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
90 layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080091
Chris Craike9c5fd82016-01-12 18:59:38 -080092 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
93 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070094
Chris Craike9c5fd82016-01-12 18:59:38 -080095 if (layerNode->getDisplayList()) {
96 deferNodeOps(*layerNode);
97 }
98 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -070099 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700100 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700101}
Chris Craik0b7e8242015-10-28 16:50:44 -0700102
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700103void FrameBuilder::deferRenderNode(RenderNode& renderNode) {
104 renderNode.computeOrdering();
105
106 mCanvasState.save(SaveFlags::MatrixClip);
107 deferNodePropsAndOps(renderNode);
108 mCanvasState.restore();
109}
110
111void FrameBuilder::deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode) {
112 renderNode.computeOrdering();
113
114 mCanvasState.save(SaveFlags::MatrixClip);
115 mCanvasState.translate(tx, ty);
116 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
117 SkRegion::kIntersect_Op);
118 deferNodePropsAndOps(renderNode);
119 mCanvasState.restore();
120}
121
122static Rect nodeBounds(RenderNode& node) {
123 auto& props = node.properties();
124 return Rect(props.getLeft(), props.getTop(),
125 props.getRight(), props.getBottom());
126}
127
128void FrameBuilder::deferRenderNodeScene(const std::vector< sp<RenderNode> >& nodes,
129 const Rect& contentDrawBounds) {
130 if (nodes.size() < 1) return;
131 if (nodes.size() == 1) {
132 if (!nodes[0]->nothingToDraw()) {
133 deferRenderNode(*nodes[0]);
134 }
135 return;
136 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800137 // It there are multiple render nodes, they are laid out as follows:
138 // #0 - backdrop (content + caption)
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700139 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
Chong Zhangc3bd5682016-01-25 12:01:12 -0800140 // #2 - additional overlay nodes
141 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
142 // resizing however it might become partially visible. The following render loop will crop the
143 // backdrop against the content and draw the remaining part of it. It will then draw the content
144 // cropped to the backdrop (since that indicates a shrinking of the window).
145 //
146 // Additional nodes will be drawn on top with no particular clipping semantics.
147
Chong Zhangc3bd5682016-01-25 12:01:12 -0800148 // Usually the contents bounds should be mContentDrawBounds - however - we will
149 // move it towards the fixed edge to give it a more stable appearance (for the moment).
150 // If there is no content bounds we ignore the layering as stated above and start with 2.
Chong Zhangc3bd5682016-01-25 12:01:12 -0800151
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700152 // Backdrop bounds in render target space
153 const Rect backdrop = nodeBounds(*nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800154
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700155 // Bounds that content will fill in render target space (note content node bounds may be bigger)
156 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
157 content.translate(backdrop.left, backdrop.top);
158 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
159 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
160
161 // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to
162 // also fill left/top. Currently, both 2up and freeform position content at the top/left of
163 // the backdrop, so this isn't necessary.
164 if (content.right < backdrop.right) {
165 // draw backdrop to right side of content
166 deferRenderNode(0, 0, Rect(content.right, backdrop.top,
167 backdrop.right, backdrop.bottom), *nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800168 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700169 if (content.bottom < backdrop.bottom) {
170 // draw backdrop to bottom of content
171 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
172 deferRenderNode(0, 0, Rect(content.left, content.bottom,
173 content.right, backdrop.bottom), *nodes[0]);
174 }
175 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800176
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700177 if (!backdrop.isEmpty()) {
178 // content node translation to catch up with backdrop
179 float dx = contentDrawBounds.left - backdrop.left;
180 float dy = contentDrawBounds.top - backdrop.top;
181
182 Rect contentLocalClip = backdrop;
183 contentLocalClip.translate(dx, dy);
184 deferRenderNode(-dx, -dy, contentLocalClip, *nodes[1]);
185 } else {
186 deferRenderNode(*nodes[1]);
187 }
188
189 // remaining overlay nodes, simply defer
190 for (size_t index = 2; index < nodes.size(); index++) {
191 if (!nodes[index]->nothingToDraw()) {
192 deferRenderNode(*nodes[index]);
193 }
Chris Craikb565df12015-10-05 13:00:52 -0700194 }
195}
196
Chris Craikf158b492016-01-12 14:45:08 -0800197void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700198
Chris Craikf158b492016-01-12 14:45:08 -0800199void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700200
Chris Craikf158b492016-01-12 14:45:08 -0800201void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800202 const RenderProperties& properties = node.properties();
203 const Outline& outline = properties.getOutline();
204 if (properties.getAlpha() <= 0
205 || (outline.getShouldClip() && outline.isEmpty())
206 || properties.getScaleX() == 0
207 || properties.getScaleY() == 0) {
208 return; // rejected
209 }
210
211 if (properties.getLeft() != 0 || properties.getTop() != 0) {
212 mCanvasState.translate(properties.getLeft(), properties.getTop());
213 }
214 if (properties.getStaticMatrix()) {
215 mCanvasState.concatMatrix(*properties.getStaticMatrix());
216 } else if (properties.getAnimationMatrix()) {
217 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
218 }
219 if (properties.hasTransformMatrix()) {
220 if (properties.isTransformTranslateOnly()) {
221 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
222 } else {
223 mCanvasState.concatMatrix(*properties.getTransformMatrix());
224 }
225 }
226
227 const int width = properties.getWidth();
228 const int height = properties.getHeight();
229
230 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
231 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
232 int clipFlags = properties.getClippingFlags();
233 if (properties.getAlpha() < 1) {
234 if (isLayer) {
235 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
236 }
237 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
238 // simply scale rendering content's alpha
239 mCanvasState.scaleAlpha(properties.getAlpha());
240 } else {
241 // schedule saveLayer by initializing saveLayerBounds
242 saveLayerBounds.set(0, 0, width, height);
243 if (clipFlags) {
244 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
245 clipFlags = 0; // all clipping done by savelayer
246 }
247 }
248
249 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
250 // pretend alpha always causes savelayer to warn about
251 // performance problem affecting old versions
252 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
253 }
254 }
255 if (clipFlags) {
256 Rect clipRect;
257 properties.getClippingRectForFlags(clipFlags, &clipRect);
258 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
259 SkRegion::kIntersect_Op);
260 }
261
262 if (properties.getRevealClip().willClip()) {
263 Rect bounds;
264 properties.getRevealClip().getBounds(&bounds);
265 mCanvasState.setClippingRoundRect(mAllocator,
266 bounds, properties.getRevealClip().getRadius());
267 } else if (properties.getOutline().willClip()) {
268 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
269 }
270
Chris Craik8913c892016-01-14 16:15:03 -0800271 bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty()
272 || (properties.getClipToBounds()
273 && mCanvasState.quickRejectConservative(0, 0, width, height));
Chris Craik7fc1b032016-02-03 19:45:06 -0800274 if (!quickRejected) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800275 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700276 if (node.getLayer()) {
277 // HW layer
John Reck7df9ff22016-02-10 16:08:08 -0800278 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700279 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
280 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800281 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700282 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
283 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800284 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
285 // draw DisplayList contents within temporary, since persisted layer could not be used.
286 // (temp layers are clipped to viewport, since they don't persist offscreen content)
287 SkPaint saveLayerPaint;
288 saveLayerPaint.setAlpha(properties.getAlpha());
John Reck7df9ff22016-02-10 16:08:08 -0800289 deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800290 saveLayerBounds,
291 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800292 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800293 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800294 deferNodeOps(node);
John Reck7df9ff22016-02-10 16:08:08 -0800295 deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
Chris Craik0b7e8242015-10-28 16:50:44 -0700296 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800297 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700298 }
299 }
300}
301
Chris Craik161f54b2015-11-05 11:08:52 -0800302typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
303
304template <typename V>
305static void buildZSortedChildList(V* zTranslatedNodes,
306 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
307 if (chunk.beginChildIndex == chunk.endChildIndex) return;
308
309 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
310 RenderNodeOp* childOp = displayList.getChildren()[i];
311 RenderNode* child = childOp->renderNode;
312 float childZ = child->properties().getZ();
313
314 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
315 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
316 childOp->skipInOrderDraw = true;
317 } else if (!child->properties().getProjectBackwards()) {
318 // regular, in order drawing DisplayList
319 childOp->skipInOrderDraw = false;
320 }
321 }
322
323 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
324 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
325}
326
327template <typename V>
328static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
329 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
330 if (zTranslatedNodes[i].key >= 0.0f) return i;
331 }
332 return zTranslatedNodes.size();
333}
334
335template <typename V>
Chris Craikd6456402016-04-11 12:24:23 -0700336void FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode,
337 const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800338 const int size = zTranslatedNodes.size();
339 if (size == 0
340 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
341 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
342 // no 3d children to draw
343 return;
344 }
345
346 /**
347 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
348 * with very similar Z heights to draw together.
349 *
350 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
351 * underneath both, and neither's shadow is drawn on top of the other.
352 */
353 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
354 size_t drawIndex, shadowIndex, endIndex;
355 if (mode == ChildrenSelectMode::Negative) {
356 drawIndex = 0;
357 endIndex = nonNegativeIndex;
358 shadowIndex = endIndex; // draw no shadows
359 } else {
360 drawIndex = nonNegativeIndex;
361 endIndex = size;
362 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
363 }
364
365 float lastCasterZ = 0.0f;
366 while (shadowIndex < endIndex || drawIndex < endIndex) {
367 if (shadowIndex < endIndex) {
368 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
369 const float casterZ = zTranslatedNodes[shadowIndex].key;
370 // attempt to render the shadow if the caster about to be drawn is its caster,
371 // OR if its caster's Z value is similar to the previous potential caster
372 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
Chris Craikd6456402016-04-11 12:24:23 -0700373 deferShadow(reorderClip, *casterNodeOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800374
375 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
376 shadowIndex++;
377 continue;
378 }
379 }
380
381 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800382 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800383 drawIndex++;
384 }
385}
386
Chris Craikd6456402016-04-11 12:24:23 -0700387void FrameBuilder::deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800388 auto& node = *casterNodeOp.renderNode;
389 auto& properties = node.properties();
390
391 if (properties.getAlpha() <= 0.0f
392 || properties.getOutline().getAlpha() <= 0.0f
393 || !properties.getOutline().getPath()
394 || properties.getScaleX() == 0
395 || properties.getScaleY() == 0) {
396 // no shadow to draw
397 return;
398 }
399
400 const SkPath* casterOutlinePath = properties.getOutline().getPath();
401 const SkPath* revealClipPath = properties.getRevealClip().getPath();
402 if (revealClipPath && revealClipPath->isEmpty()) return;
403
404 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
405
406 // holds temporary SkPath to store the result of intersections
407 SkPath* frameAllocatedPath = nullptr;
408 const SkPath* casterPath = casterOutlinePath;
409
410 // intersect the shadow-casting path with the reveal, if present
411 if (revealClipPath) {
412 frameAllocatedPath = createFrameAllocatedPath();
413
414 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
415 casterPath = frameAllocatedPath;
416 }
417
418 // intersect the shadow-casting path with the clipBounds, if present
419 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
420 if (!frameAllocatedPath) {
421 frameAllocatedPath = createFrameAllocatedPath();
422 }
423 Rect clipBounds;
424 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
425 SkPath clipBoundsPath;
426 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
427 clipBounds.right, clipBounds.bottom);
428
429 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
430 casterPath = frameAllocatedPath;
431 }
432
Chris Craikd6456402016-04-11 12:24:23 -0700433 // apply reorder clip to shadow, so it respects clip at beginning of reorderable chunk
434 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
435 mCanvasState.writableSnapshot()->applyClip(reorderClip,
436 *mCanvasState.currentSnapshot()->transform);
Chris Craik6e068c012016-01-15 16:15:30 -0800437 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
438 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
439 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
440 node.applyViewPropertyTransforms(shadowMatrixXY, false);
441 node.applyViewPropertyTransforms(shadowMatrixZ, true);
442
Chris Craik3a5811b2016-03-22 15:03:08 -0700443 sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask(
Chris Craik6e068c012016-01-15 16:15:30 -0800444 mCanvasState.currentTransform(),
445 mCanvasState.getLocalClipBounds(),
446 casterAlpha >= 1.0f,
447 casterPath,
448 &shadowMatrixXY, &shadowMatrixZ,
449 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
450 mLightRadius);
451 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
452 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
453 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
454 if (CC_LIKELY(bakedOpState)) {
455 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
456 }
Chris Craikd3daa312015-11-06 10:59:56 -0800457 }
Chris Craikd6456402016-04-11 12:24:23 -0700458 mCanvasState.restoreToCount(restoreTo);
Chris Craik161f54b2015-11-05 11:08:52 -0800459}
Chris Craikd3daa312015-11-06 10:59:56 -0800460
Chris Craikf158b492016-01-12 14:45:08 -0800461void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500462 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800463 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Chris Craik8d1f2122015-11-24 16:40:09 -0800464
Chris Craik678ff812016-03-01 13:27:54 -0800465 SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
466 if (projectionReceiverOutline) {
467 // transform the mask for this projector into render target space
468 // TODO: consider combining both transforms by stashing transform instead of applying
469 SkMatrix skCurrentTransform;
470 mCanvasState.currentTransform()->copyTo(skCurrentTransform);
471 projectionReceiverOutline->transform(
472 skCurrentTransform,
473 &transformedMaskPath);
474 mCanvasState.setProjectionPathMask(mAllocator, &transformedMaskPath);
475 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800476
Chris Craik8d1f2122015-11-24 16:40:09 -0800477 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
478 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
Chris Craika748c082016-03-01 18:48:37 -0800479 RenderNode& childNode = *childOp->renderNode;
Chris Craik678ff812016-03-01 13:27:54 -0800480
Chris Craika748c082016-03-01 18:48:37 -0800481 // Draw child if it has content, but ignore state in childOp - matrix already applied to
482 // transformFromCompositingAncestor, and record-time clip is ignored when projecting
483 if (!childNode.nothingToDraw()) {
484 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800485
Chris Craika748c082016-03-01 18:48:37 -0800486 // Apply transform between ancestor and projected descendant
487 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
488
489 deferNodePropsAndOps(childNode);
490
491 mCanvasState.restoreToCount(restoreTo);
492 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800493 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800494 mCanvasState.restoreToCount(count);
495}
496
Chris Craikb565df12015-10-05 13:00:52 -0700497/**
Chris Craikf158b492016-01-12 14:45:08 -0800498 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700499 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800500 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800501 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700502 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700503#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800504 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
505void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
506 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800507 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800508
509 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
510 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikd6456402016-04-11 12:24:23 -0700511 for (auto& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800512 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
513 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
514
Chris Craikd6456402016-04-11 12:24:23 -0700515 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700516 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700517 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700518 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800519
520 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
521 && displayList.projectionReceiveIndex >= 0
522 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
523 deferProjectedChildren(renderNode);
524 }
Chris Craikb565df12015-10-05 13:00:52 -0700525 }
Chris Craikd6456402016-04-11 12:24:23 -0700526 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700527 }
528}
529
Chris Craikf158b492016-01-12 14:45:08 -0800530void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800531 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500532 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700533
Chris Craike4db79d2015-12-22 16:32:23 -0800534 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
Chris Craik04d46eb2016-04-07 13:51:07 -0700535 mCanvasState.writableSnapshot()->applyClip(op.localClip,
Chris Craike4db79d2015-12-22 16:32:23 -0800536 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700537 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700538
Chris Craik0b7e8242015-10-28 16:50:44 -0700539 // then apply state from node properties, and defer ops
540 deferNodePropsAndOps(*op.renderNode);
541
Chris Craik6fe991e52015-10-20 09:39:42 -0700542 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700543}
544
Chris Craikf158b492016-01-12 14:45:08 -0800545void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800546 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800547 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800548 }
549}
550
Chris Craik386aa032015-12-07 17:08:25 -0800551/**
552 * Defers an unmergeable, strokeable op, accounting correctly
553 * for paint's style on the bounds being computed.
554 */
Chris Craik80d2ade2016-03-28 12:54:07 -0700555BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800556 BakedOpState::StrokeBehavior strokeBehavior) {
557 // Note: here we account for stroke when baking the op
558 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800559 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik3a5811b2016-03-22 15:03:08 -0700560 if (!bakedState) return nullptr; // quick rejected
Chris Craik80d2ade2016-03-28 12:54:07 -0700561
562 if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) {
563 bakedState->setupOpacity(op.paint);
564 }
565
Chris Craik386aa032015-12-07 17:08:25 -0800566 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
Chris Craik3a5811b2016-03-22 15:03:08 -0700567 return bakedState;
Chris Craik386aa032015-12-07 17:08:25 -0800568}
569
570/**
571 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
572 * be used, since they trigger significantly different rendering paths.
573 *
574 * Note: not used for lines/points, since they don't currently support path effects.
575 */
576static batchid_t tessBatchId(const RecordedOp& op) {
577 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700578 return paint.getPathEffect()
579 ? OpBatchType::AlphaMaskTexture
580 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
581}
582
Chris Craikf158b492016-01-12 14:45:08 -0800583void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800584 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800585}
586
Chris Craikb87eadd2016-01-06 09:16:05 -0800587static bool hasMergeableClip(const BakedOpState& state) {
588 return state.computedState.clipState
589 || state.computedState.clipState->mode == ClipMode::Rectangle;
590}
591
Chris Craikf158b492016-01-12 14:45:08 -0800592void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800593 BakedOpState* bakedState = tryBakeOpState(op);
594 if (!bakedState) return; // quick rejected
sergeyva82ffc52016-04-04 17:12:04 -0700595
596 if (op.bitmap->isOpaque()) {
597 bakedState->setupOpacity(op.paint);
598 }
Chris Craikb565df12015-10-05 13:00:52 -0700599
Chris Craik15c3f192015-12-03 12:16:56 -0800600 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
601 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
602 // MergingDrawBatch::canMergeWith()
603 if (bakedState->computedState.transform.isSimple()
604 && bakedState->computedState.transform.positiveScale()
605 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800606 && op.bitmap->colorType() != kAlpha_8_SkColorType
607 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800608 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800609 // TODO: AssetAtlas in mergeId
610 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
611 } else {
612 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
613 }
Chris Craikb565df12015-10-05 13:00:52 -0700614}
615
Chris Craikf158b492016-01-12 14:45:08 -0800616void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800617 BakedOpState* bakedState = tryBakeOpState(op);
618 if (!bakedState) return; // quick rejected
619 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
620}
621
Chris Craikf158b492016-01-12 14:45:08 -0800622void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800623 BakedOpState* bakedState = tryBakeOpState(op);
624 if (!bakedState) return; // quick rejected
625 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
626}
627
Doris Liu766431a2016-02-04 22:17:11 +0000628void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
629 const SkBitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
630 SkPaint* paint = op.vectorDrawable->getPaint();
John Reck7df9ff22016-02-10 16:08:08 -0800631 const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds,
Doris Liu766431a2016-02-04 22:17:11 +0000632 op.localMatrix,
633 op.localClip,
634 paint,
635 &bitmap,
636 Rect(bitmap.width(), bitmap.height()));
637 deferBitmapRectOp(*resolvedOp);
638}
639
Chris Craikf158b492016-01-12 14:45:08 -0800640void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800641 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
642 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
643 float x = *(op.x);
644 float y = *(op.y);
645 float radius = *(op.radius);
646 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
John Reck7df9ff22016-02-10 16:08:08 -0800647 const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800648 unmappedBounds,
649 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800650 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800651 op.paint);
652 deferOvalOp(*resolvedOp);
653}
654
Chris Craika2048482016-03-25 14:17:49 -0700655void FrameBuilder::deferColorOp(const ColorOp& op) {
656 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
657 if (!bakedState) return; // quick rejected
658 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
659}
660
Chris Craikf158b492016-01-12 14:45:08 -0800661void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700662 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craike29ce6f2015-12-10 16:25:13 -0800663 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800664 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800665}
666
Chris Craikf158b492016-01-12 14:45:08 -0800667void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800668 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800669 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800670}
671
Chris Craikf158b492016-01-12 14:45:08 -0800672void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800673 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800674}
675
Chris Craikf158b492016-01-12 14:45:08 -0800676void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800677 BakedOpState* bakedState = tryBakeOpState(op);
678 if (!bakedState) return; // quick rejected
679
680 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800681 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
682 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800683 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800684 // TODO: AssetAtlas in mergeId
685
686 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
687 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
688 } else {
689 // Use Bitmap batchId since Bitmap+Patch use same shader
690 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
691 }
692}
693
Chris Craikf158b492016-01-12 14:45:08 -0800694void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700695 auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture);
696 if (CC_LIKELY(state)) {
697 mCaches.pathCache.precache(op.path, op.paint);
698 }
Chris Craik386aa032015-12-07 17:08:25 -0800699}
700
Chris Craikf158b492016-01-12 14:45:08 -0800701void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800702 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800703 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800704}
705
Chris Craikf158b492016-01-12 14:45:08 -0800706void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800707 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800708}
709
Chris Craikf158b492016-01-12 14:45:08 -0800710void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700711 auto state = deferStrokeableOp(op, tessBatchId(op));
712 if (CC_LIKELY(state && !op.paint->getPathEffect())) {
713 // TODO: consider storing tessellation task in BakedOpState
714 mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint),
715 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
716 }
Chris Craikb565df12015-10-05 13:00:52 -0700717}
718
Chris Craikf158b492016-01-12 14:45:08 -0800719void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800720 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
721 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
John Reck7df9ff22016-02-10 16:08:08 -0800722 const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800723 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
724 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800725 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800726 op.paint, *op.rx, *op.ry);
727 deferRoundRectOp(*resolvedOp);
728}
729
Chris Craikf158b492016-01-12 14:45:08 -0800730void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800731 BakedOpState* bakedState = tryBakeOpState(op);
732 if (!bakedState) return; // quick rejected
733 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700734}
735
Chris Craikd7448e62015-12-15 10:34:36 -0800736static batchid_t textBatchId(const SkPaint& paint) {
737 // TODO: better handling of shader (since we won't care about color then)
738 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
739}
740
Chris Craikf158b492016-01-12 14:45:08 -0800741void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik7c02cab2016-03-16 17:15:12 -0700742 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
743 mAllocator, *mCanvasState.writableSnapshot(), op,
744 BakedOpState::StrokeBehavior::StyleDefined);
Chris Craik15c3f192015-12-03 12:16:56 -0800745 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800746
Chris Craikd7448e62015-12-15 10:34:36 -0800747 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800748 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800749 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
750 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800751 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
752 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
753 } else {
754 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
755 }
Chris Craik3a5811b2016-03-22 15:03:08 -0700756
757 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
758 auto& totalTransform = bakedState->computedState.transform;
759 if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) {
760 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
761 } else {
762 // Partial transform case, see BakedOpDispatcher::renderTextOp
763 float sx, sy;
764 totalTransform.decomposeScale(sx, sy);
765 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::MakeScale(
766 roundf(std::max(1.0f, sx)),
767 roundf(std::max(1.0f, sy))));
768 }
Chris Craika1717272015-11-19 13:02:43 -0800769}
770
Chris Craikf158b492016-01-12 14:45:08 -0800771void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700772 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craikd7448e62015-12-15 10:34:36 -0800773 if (!bakedState) return; // quick rejected
774 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
Chris Craik3a5811b2016-03-22 15:03:08 -0700775
776 mCaches.fontRenderer.getFontRenderer().precache(
777 op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
Chris Craikd7448e62015-12-15 10:34:36 -0800778}
779
Chris Craikf158b492016-01-12 14:45:08 -0800780void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
John Reck417ed6d2016-03-22 16:01:08 -0700781 if (CC_UNLIKELY(!op.layer->isRenderable())) return;
Chris Craikaafb01d2016-03-25 18:34:11 -0700782
783 const TextureLayerOp* textureLayerOp = &op;
784 // Now safe to access transform (which was potentially unready at record time)
785 if (!op.layer->getTransform().isIdentity()) {
786 // non-identity transform present, so 'inject it' into op by copying + replacing matrix
787 Matrix4 combinedMatrix(op.localMatrix);
788 combinedMatrix.multiply(op.layer->getTransform());
789 textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix);
790 }
791 BakedOpState* bakedState = tryBakeOpState(*textureLayerOp);
792
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800793 if (!bakedState) return; // quick rejected
794 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
795}
796
Chris Craikf158b492016-01-12 14:45:08 -0800797void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800798 float contentTranslateX, float contentTranslateY,
799 const Rect& repaintRect,
800 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700801 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500802 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700803 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700804 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800805 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800806 mCanvasState.writableSnapshot()->transform->loadTranslate(
807 contentTranslateX, contentTranslateY, 0);
808 mCanvasState.writableSnapshot()->setClip(
809 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800810
Chris Craik8ecf41c2015-11-16 10:27:59 -0800811 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800812 mLayerStack.push_back(mLayerBuilders.size());
813 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800814 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800815 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700816}
817
Chris Craikf158b492016-01-12 14:45:08 -0800818void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700819 // restore canvas, and pop finished layer off of the stack
820 mCanvasState.restore();
821 mLayerStack.pop_back();
822}
823
Chris Craikb87eadd2016-01-06 09:16:05 -0800824// TODO: defer time rejection (when bounds become empty) + tests
825// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800826void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800827 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
828 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
829
830 auto previous = mCanvasState.currentSnapshot();
831 Vector3 lightCenter = previous->getRelativeLightCenter();
832
833 // Combine all transforms used to present saveLayer content:
834 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800835 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800836 contentTransform.multiply(op.localMatrix);
837 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
838
839 Matrix4 inverseContentTransform;
840 inverseContentTransform.loadInverse(contentTransform);
841
842 // map the light center into layer-relative space
843 inverseContentTransform.mapPoint3d(lightCenter);
844
845 // Clip bounds of temporary layer to parent's clip rect, so:
846 Rect saveLayerBounds(layerWidth, layerHeight);
847 // 1) transform Rect(width, height) into parent's space
848 // note: left/top offsets put in contentTransform above
849 contentTransform.mapRect(saveLayerBounds);
850 // 2) intersect with parent's clip
851 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
852 // 3) and transform back
853 inverseContentTransform.mapRect(saveLayerBounds);
854 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
855 saveLayerBounds.roundOut();
856
857 // if bounds are reduced, will clip the layer's area by reducing required bounds...
858 layerWidth = saveLayerBounds.getWidth();
859 layerHeight = saveLayerBounds.getHeight();
860 // ...and shifting drawing content to account for left/top side clipping
861 float contentTranslateX = -saveLayerBounds.left;
862 float contentTranslateY = -saveLayerBounds.top;
863
864 saveForLayer(layerWidth, layerHeight,
865 contentTranslateX, contentTranslateY,
866 Rect(layerWidth, layerHeight),
867 lightCenter,
868 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700869}
Chris Craikb565df12015-10-05 13:00:52 -0700870
Chris Craikf158b492016-01-12 14:45:08 -0800871void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700872 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700873 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700874
875 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700876
877 // record the draw operation into the previous layer's list of draw commands
878 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
John Reck7df9ff22016-02-10 16:08:08 -0800879 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
Chris Craik6fe991e52015-10-20 09:39:42 -0700880 beginLayerOp.unmappedBounds,
881 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800882 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700883 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800884 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700885 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
886
887 if (bakedOpState) {
888 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
889 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
890 } else {
891 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800892 // TODO: need to prevent any render work from being done
893 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800894 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700895 return;
Chris Craikb565df12015-10-05 13:00:52 -0700896 }
897}
898
Chris Craikf158b492016-01-12 14:45:08 -0800899void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800900 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
901 boundsTransform.multiply(op.localMatrix);
902
903 Rect dstRect(op.unmappedBounds);
904 boundsTransform.mapRect(dstRect);
905 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
906
Chris Craik4876de12016-02-25 16:54:08 -0800907 if (dstRect.isEmpty()) {
908 // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
909 currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
910 } else {
911 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
912 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800913
Chris Craik4876de12016-02-25 16:54:08 -0800914 /**
915 * First, defer an operation to copy out the content from the rendertarget into a layer.
916 */
917 auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
918 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
919 &(currentLayer().repaintClip), dstRect, *copyToOp);
920 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
Chris Craikb87eadd2016-01-06 09:16:05 -0800921
Chris Craik4876de12016-02-25 16:54:08 -0800922 /**
923 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
924 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
925 */
926 currentLayer().deferLayerClear(dstRect);
Chris Craikb87eadd2016-01-06 09:16:05 -0800927
Chris Craik4876de12016-02-25 16:54:08 -0800928 /**
929 * And stash an operation to copy that layer back under the rendertarget until
930 * a balanced EndUnclippedLayerOp is seen
931 */
932 auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
933 bakedState = BakedOpState::directConstruct(mAllocator,
934 &(currentLayer().repaintClip), dstRect, *copyFromOp);
935 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
936 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800937}
938
Chris Craikf158b492016-01-12 14:45:08 -0800939void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800940 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
941
942 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
Chris Craikb87eadd2016-01-06 09:16:05 -0800943 currentLayer().activeUnclippedSaveLayers.pop_back();
Chris Craik4876de12016-02-25 16:54:08 -0800944 if (copyFromLayerOp) {
945 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
946 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800947}
948
Chris Craik3a5811b2016-03-22 15:03:08 -0700949void FrameBuilder::finishDefer() {
950 mCaches.fontRenderer.endPrecaching();
951}
952
Chris Craikb565df12015-10-05 13:00:52 -0700953} // namespace uirenderer
954} // namespace android