blob: 84c8a745ad1e2bd95bf9e3d8949969af3d3d1439 [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)) {
Chris Craikaff230f2016-05-04 16:27:28 -070085 ATRACE_FORMAT("Optimize HW Layer DisplayList %s %ux%u",
86 layerNode->getName(), layerNode->getWidth(), layerNode->getHeight());
87
Chris Craike9c5fd82016-01-12 18:59:38 -080088 const Rect& layerDamage = layers.entries()[i].damage;
89 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070090
Chris Craike9c5fd82016-01-12 18:59:38 -080091 // map current light center into RenderNode's coordinate space
92 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
93 layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -080094
Chris Craike9c5fd82016-01-12 18:59:38 -080095 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
96 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070097
Chris Craike9c5fd82016-01-12 18:59:38 -080098 if (layerNode->getDisplayList()) {
99 deferNodeOps(*layerNode);
100 }
101 restoreForLayer();
Chris Craik0b7e8242015-10-28 16:50:44 -0700102 }
Chris Craik0b7e8242015-10-28 16:50:44 -0700103 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700104}
Chris Craik0b7e8242015-10-28 16:50:44 -0700105
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700106void FrameBuilder::deferRenderNode(RenderNode& renderNode) {
107 renderNode.computeOrdering();
108
109 mCanvasState.save(SaveFlags::MatrixClip);
110 deferNodePropsAndOps(renderNode);
111 mCanvasState.restore();
112}
113
114void FrameBuilder::deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode) {
115 renderNode.computeOrdering();
116
117 mCanvasState.save(SaveFlags::MatrixClip);
118 mCanvasState.translate(tx, ty);
119 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
120 SkRegion::kIntersect_Op);
121 deferNodePropsAndOps(renderNode);
122 mCanvasState.restore();
123}
124
125static Rect nodeBounds(RenderNode& node) {
126 auto& props = node.properties();
127 return Rect(props.getLeft(), props.getTop(),
128 props.getRight(), props.getBottom());
129}
130
131void FrameBuilder::deferRenderNodeScene(const std::vector< sp<RenderNode> >& nodes,
132 const Rect& contentDrawBounds) {
133 if (nodes.size() < 1) return;
134 if (nodes.size() == 1) {
135 if (!nodes[0]->nothingToDraw()) {
136 deferRenderNode(*nodes[0]);
137 }
138 return;
139 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800140 // It there are multiple render nodes, they are laid out as follows:
141 // #0 - backdrop (content + caption)
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700142 // #1 - content (local bounds are at (0,0), will be translated and clipped to backdrop)
Chong Zhangc3bd5682016-01-25 12:01:12 -0800143 // #2 - additional overlay nodes
144 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
145 // resizing however it might become partially visible. The following render loop will crop the
146 // backdrop against the content and draw the remaining part of it. It will then draw the content
147 // cropped to the backdrop (since that indicates a shrinking of the window).
148 //
149 // Additional nodes will be drawn on top with no particular clipping semantics.
150
Chong Zhangc3bd5682016-01-25 12:01:12 -0800151 // Usually the contents bounds should be mContentDrawBounds - however - we will
152 // move it towards the fixed edge to give it a more stable appearance (for the moment).
153 // If there is no content bounds we ignore the layering as stated above and start with 2.
Chong Zhangc3bd5682016-01-25 12:01:12 -0800154
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700155 // Backdrop bounds in render target space
156 const Rect backdrop = nodeBounds(*nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800157
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700158 // Bounds that content will fill in render target space (note content node bounds may be bigger)
159 Rect content(contentDrawBounds.getWidth(), contentDrawBounds.getHeight());
160 content.translate(backdrop.left, backdrop.top);
161 if (!content.contains(backdrop) && !nodes[0]->nothingToDraw()) {
162 // Content doesn't entirely overlap backdrop, so fill around content (right/bottom)
163
164 // Note: in the future, if content doesn't snap to backdrop's left/top, this may need to
165 // also fill left/top. Currently, both 2up and freeform position content at the top/left of
166 // the backdrop, so this isn't necessary.
167 if (content.right < backdrop.right) {
168 // draw backdrop to right side of content
169 deferRenderNode(0, 0, Rect(content.right, backdrop.top,
170 backdrop.right, backdrop.bottom), *nodes[0]);
Chong Zhangc3bd5682016-01-25 12:01:12 -0800171 }
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700172 if (content.bottom < backdrop.bottom) {
173 // draw backdrop to bottom of content
174 // Note: bottom fill uses content left/right, to avoid overdrawing left/right fill
175 deferRenderNode(0, 0, Rect(content.left, content.bottom,
176 content.right, backdrop.bottom), *nodes[0]);
177 }
178 }
Chong Zhangc3bd5682016-01-25 12:01:12 -0800179
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700180 if (!backdrop.isEmpty()) {
181 // content node translation to catch up with backdrop
182 float dx = contentDrawBounds.left - backdrop.left;
183 float dy = contentDrawBounds.top - backdrop.top;
184
185 Rect contentLocalClip = backdrop;
186 contentLocalClip.translate(dx, dy);
187 deferRenderNode(-dx, -dy, contentLocalClip, *nodes[1]);
188 } else {
189 deferRenderNode(*nodes[1]);
190 }
191
192 // remaining overlay nodes, simply defer
193 for (size_t index = 2; index < nodes.size(); index++) {
194 if (!nodes[index]->nothingToDraw()) {
195 deferRenderNode(*nodes[index]);
196 }
Chris Craikb565df12015-10-05 13:00:52 -0700197 }
198}
199
Chris Craikf158b492016-01-12 14:45:08 -0800200void FrameBuilder::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700201
Chris Craikf158b492016-01-12 14:45:08 -0800202void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -0700203
Chris Craikf158b492016-01-12 14:45:08 -0800204void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800205 const RenderProperties& properties = node.properties();
206 const Outline& outline = properties.getOutline();
207 if (properties.getAlpha() <= 0
208 || (outline.getShouldClip() && outline.isEmpty())
209 || properties.getScaleX() == 0
210 || properties.getScaleY() == 0) {
211 return; // rejected
212 }
213
214 if (properties.getLeft() != 0 || properties.getTop() != 0) {
215 mCanvasState.translate(properties.getLeft(), properties.getTop());
216 }
217 if (properties.getStaticMatrix()) {
218 mCanvasState.concatMatrix(*properties.getStaticMatrix());
219 } else if (properties.getAnimationMatrix()) {
220 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
221 }
222 if (properties.hasTransformMatrix()) {
223 if (properties.isTransformTranslateOnly()) {
224 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
225 } else {
226 mCanvasState.concatMatrix(*properties.getTransformMatrix());
227 }
228 }
229
230 const int width = properties.getWidth();
231 const int height = properties.getHeight();
232
233 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
234 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
235 int clipFlags = properties.getClippingFlags();
236 if (properties.getAlpha() < 1) {
237 if (isLayer) {
238 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
239 }
240 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
241 // simply scale rendering content's alpha
242 mCanvasState.scaleAlpha(properties.getAlpha());
243 } else {
244 // schedule saveLayer by initializing saveLayerBounds
245 saveLayerBounds.set(0, 0, width, height);
246 if (clipFlags) {
247 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
248 clipFlags = 0; // all clipping done by savelayer
249 }
250 }
251
252 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
253 // pretend alpha always causes savelayer to warn about
254 // performance problem affecting old versions
255 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
256 }
257 }
258 if (clipFlags) {
259 Rect clipRect;
260 properties.getClippingRectForFlags(clipFlags, &clipRect);
261 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
262 SkRegion::kIntersect_Op);
263 }
264
265 if (properties.getRevealClip().willClip()) {
266 Rect bounds;
267 properties.getRevealClip().getBounds(&bounds);
268 mCanvasState.setClippingRoundRect(mAllocator,
269 bounds, properties.getRevealClip().getRadius());
270 } else if (properties.getOutline().willClip()) {
271 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
272 }
273
Chris Craik8913c892016-01-14 16:15:03 -0800274 bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty()
275 || (properties.getClipToBounds()
276 && mCanvasState.quickRejectConservative(0, 0, width, height));
Chris Craik7fc1b032016-02-03 19:45:06 -0800277 if (!quickRejected) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800278 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700279 if (node.getLayer()) {
280 // HW layer
John Reck7df9ff22016-02-10 16:08:08 -0800281 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700282 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
283 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800284 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700285 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
286 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800287 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
288 // draw DisplayList contents within temporary, since persisted layer could not be used.
289 // (temp layers are clipped to viewport, since they don't persist offscreen content)
290 SkPaint saveLayerPaint;
291 saveLayerPaint.setAlpha(properties.getAlpha());
John Reck7df9ff22016-02-10 16:08:08 -0800292 deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800293 saveLayerBounds,
294 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800295 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800296 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800297 deferNodeOps(node);
John Reck7df9ff22016-02-10 16:08:08 -0800298 deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
Chris Craik0b7e8242015-10-28 16:50:44 -0700299 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800300 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700301 }
302 }
303}
304
Chris Craik161f54b2015-11-05 11:08:52 -0800305typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
306
307template <typename V>
308static void buildZSortedChildList(V* zTranslatedNodes,
309 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
310 if (chunk.beginChildIndex == chunk.endChildIndex) return;
311
312 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
313 RenderNodeOp* childOp = displayList.getChildren()[i];
314 RenderNode* child = childOp->renderNode;
315 float childZ = child->properties().getZ();
316
317 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
318 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
319 childOp->skipInOrderDraw = true;
320 } else if (!child->properties().getProjectBackwards()) {
321 // regular, in order drawing DisplayList
322 childOp->skipInOrderDraw = false;
323 }
324 }
325
326 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
327 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
328}
329
330template <typename V>
331static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
332 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
333 if (zTranslatedNodes[i].key >= 0.0f) return i;
334 }
335 return zTranslatedNodes.size();
336}
337
338template <typename V>
Chris Craikd6456402016-04-11 12:24:23 -0700339void FrameBuilder::defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode,
340 const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800341 const int size = zTranslatedNodes.size();
342 if (size == 0
343 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
344 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
345 // no 3d children to draw
346 return;
347 }
348
349 /**
350 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
351 * with very similar Z heights to draw together.
352 *
353 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
354 * underneath both, and neither's shadow is drawn on top of the other.
355 */
356 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
357 size_t drawIndex, shadowIndex, endIndex;
358 if (mode == ChildrenSelectMode::Negative) {
359 drawIndex = 0;
360 endIndex = nonNegativeIndex;
361 shadowIndex = endIndex; // draw no shadows
362 } else {
363 drawIndex = nonNegativeIndex;
364 endIndex = size;
365 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
366 }
367
368 float lastCasterZ = 0.0f;
369 while (shadowIndex < endIndex || drawIndex < endIndex) {
370 if (shadowIndex < endIndex) {
371 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
372 const float casterZ = zTranslatedNodes[shadowIndex].key;
373 // attempt to render the shadow if the caster about to be drawn is its caster,
374 // OR if its caster's Z value is similar to the previous potential caster
375 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
Chris Craikd6456402016-04-11 12:24:23 -0700376 deferShadow(reorderClip, *casterNodeOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800377
378 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
379 shadowIndex++;
380 continue;
381 }
382 }
383
384 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800385 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800386 drawIndex++;
387 }
388}
389
Chris Craikd6456402016-04-11 12:24:23 -0700390void FrameBuilder::deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800391 auto& node = *casterNodeOp.renderNode;
392 auto& properties = node.properties();
393
394 if (properties.getAlpha() <= 0.0f
395 || properties.getOutline().getAlpha() <= 0.0f
396 || !properties.getOutline().getPath()
397 || properties.getScaleX() == 0
398 || properties.getScaleY() == 0) {
399 // no shadow to draw
400 return;
401 }
402
403 const SkPath* casterOutlinePath = properties.getOutline().getPath();
404 const SkPath* revealClipPath = properties.getRevealClip().getPath();
405 if (revealClipPath && revealClipPath->isEmpty()) return;
406
407 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
408
409 // holds temporary SkPath to store the result of intersections
410 SkPath* frameAllocatedPath = nullptr;
411 const SkPath* casterPath = casterOutlinePath;
412
413 // intersect the shadow-casting path with the reveal, if present
414 if (revealClipPath) {
415 frameAllocatedPath = createFrameAllocatedPath();
416
417 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
418 casterPath = frameAllocatedPath;
419 }
420
421 // intersect the shadow-casting path with the clipBounds, if present
422 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
423 if (!frameAllocatedPath) {
424 frameAllocatedPath = createFrameAllocatedPath();
425 }
426 Rect clipBounds;
427 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
428 SkPath clipBoundsPath;
429 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
430 clipBounds.right, clipBounds.bottom);
431
432 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
433 casterPath = frameAllocatedPath;
434 }
435
Chris Craikd6456402016-04-11 12:24:23 -0700436 // apply reorder clip to shadow, so it respects clip at beginning of reorderable chunk
437 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
438 mCanvasState.writableSnapshot()->applyClip(reorderClip,
439 *mCanvasState.currentSnapshot()->transform);
Chris Craik6e068c012016-01-15 16:15:30 -0800440 if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
441 Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
442 Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
443 node.applyViewPropertyTransforms(shadowMatrixXY, false);
444 node.applyViewPropertyTransforms(shadowMatrixZ, true);
445
Chris Craik3a5811b2016-03-22 15:03:08 -0700446 sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask(
Chris Craik6e068c012016-01-15 16:15:30 -0800447 mCanvasState.currentTransform(),
448 mCanvasState.getLocalClipBounds(),
449 casterAlpha >= 1.0f,
450 casterPath,
451 &shadowMatrixXY, &shadowMatrixZ,
452 mCanvasState.currentSnapshot()->getRelativeLightCenter(),
453 mLightRadius);
454 ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
455 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
456 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
457 if (CC_LIKELY(bakedOpState)) {
458 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
459 }
Chris Craikd3daa312015-11-06 10:59:56 -0800460 }
Chris Craikd6456402016-04-11 12:24:23 -0700461 mCanvasState.restoreToCount(restoreTo);
Chris Craik161f54b2015-11-05 11:08:52 -0800462}
Chris Craikd3daa312015-11-06 10:59:56 -0800463
Chris Craikf158b492016-01-12 14:45:08 -0800464void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500465 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800466 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
Chris Craik8d1f2122015-11-24 16:40:09 -0800467
Chris Craik678ff812016-03-01 13:27:54 -0800468 SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
469 if (projectionReceiverOutline) {
470 // transform the mask for this projector into render target space
471 // TODO: consider combining both transforms by stashing transform instead of applying
472 SkMatrix skCurrentTransform;
473 mCanvasState.currentTransform()->copyTo(skCurrentTransform);
474 projectionReceiverOutline->transform(
475 skCurrentTransform,
476 &transformedMaskPath);
477 mCanvasState.setProjectionPathMask(mAllocator, &transformedMaskPath);
478 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800479
Chris Craik8d1f2122015-11-24 16:40:09 -0800480 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
481 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
Chris Craika748c082016-03-01 18:48:37 -0800482 RenderNode& childNode = *childOp->renderNode;
Chris Craik678ff812016-03-01 13:27:54 -0800483
Chris Craika748c082016-03-01 18:48:37 -0800484 // Draw child if it has content, but ignore state in childOp - matrix already applied to
485 // transformFromCompositingAncestor, and record-time clip is ignored when projecting
486 if (!childNode.nothingToDraw()) {
487 int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik678ff812016-03-01 13:27:54 -0800488
Chris Craika748c082016-03-01 18:48:37 -0800489 // Apply transform between ancestor and projected descendant
490 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
491
492 deferNodePropsAndOps(childNode);
493
494 mCanvasState.restoreToCount(restoreTo);
495 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800496 }
Chris Craik8d1f2122015-11-24 16:40:09 -0800497 mCanvasState.restoreToCount(count);
498}
499
Chris Craikb565df12015-10-05 13:00:52 -0700500/**
Chris Craikf158b492016-01-12 14:45:08 -0800501 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700502 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800503 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craikf158b492016-01-12 14:45:08 -0800504 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700505 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700506#define OP_RECEIVER(Type) \
Chris Craikf158b492016-01-12 14:45:08 -0800507 [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
508void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
509 typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800510 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800511
512 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
513 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikd6456402016-04-11 12:24:23 -0700514 for (auto& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800515 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
516 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
517
Chris Craikd6456402016-04-11 12:24:23 -0700518 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700519 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700520 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700521 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800522
523 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
524 && displayList.projectionReceiveIndex >= 0
525 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
526 deferProjectedChildren(renderNode);
527 }
Chris Craikb565df12015-10-05 13:00:52 -0700528 }
Chris Craikd6456402016-04-11 12:24:23 -0700529 defer3dChildren(chunk.reorderClip, ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700530 }
531}
532
Chris Craikf158b492016-01-12 14:45:08 -0800533void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800534 if (op.renderNode->nothingToDraw()) return;
Florin Malitaeecff562015-12-21 10:43:01 -0500535 int count = mCanvasState.save(SaveFlags::MatrixClip);
Chris Craikb565df12015-10-05 13:00:52 -0700536
Chris Craike4db79d2015-12-22 16:32:23 -0800537 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
Chris Craik04d46eb2016-04-07 13:51:07 -0700538 mCanvasState.writableSnapshot()->applyClip(op.localClip,
Chris Craike4db79d2015-12-22 16:32:23 -0800539 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700540 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700541
Chris Craik0b7e8242015-10-28 16:50:44 -0700542 // then apply state from node properties, and defer ops
543 deferNodePropsAndOps(*op.renderNode);
544
Chris Craik6fe991e52015-10-20 09:39:42 -0700545 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700546}
547
Chris Craikf158b492016-01-12 14:45:08 -0800548void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800549 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800550 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800551 }
552}
553
Chris Craik386aa032015-12-07 17:08:25 -0800554/**
555 * Defers an unmergeable, strokeable op, accounting correctly
556 * for paint's style on the bounds being computed.
557 */
Chris Craik80d2ade2016-03-28 12:54:07 -0700558BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800559 BakedOpState::StrokeBehavior strokeBehavior) {
560 // Note: here we account for stroke when baking the op
561 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800562 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik3a5811b2016-03-22 15:03:08 -0700563 if (!bakedState) return nullptr; // quick rejected
Chris Craik80d2ade2016-03-28 12:54:07 -0700564
565 if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) {
566 bakedState->setupOpacity(op.paint);
567 }
568
Chris Craik386aa032015-12-07 17:08:25 -0800569 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
Chris Craik3a5811b2016-03-22 15:03:08 -0700570 return bakedState;
Chris Craik386aa032015-12-07 17:08:25 -0800571}
572
573/**
574 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
575 * be used, since they trigger significantly different rendering paths.
576 *
577 * Note: not used for lines/points, since they don't currently support path effects.
578 */
579static batchid_t tessBatchId(const RecordedOp& op) {
580 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700581 return paint.getPathEffect()
582 ? OpBatchType::AlphaMaskTexture
583 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
584}
585
Chris Craikf158b492016-01-12 14:45:08 -0800586void FrameBuilder::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800587 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800588}
589
Chris Craikb87eadd2016-01-06 09:16:05 -0800590static bool hasMergeableClip(const BakedOpState& state) {
591 return state.computedState.clipState
592 || state.computedState.clipState->mode == ClipMode::Rectangle;
593}
594
Chris Craikf158b492016-01-12 14:45:08 -0800595void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800596 BakedOpState* bakedState = tryBakeOpState(op);
597 if (!bakedState) return; // quick rejected
sergeyva82ffc52016-04-04 17:12:04 -0700598
599 if (op.bitmap->isOpaque()) {
600 bakedState->setupOpacity(op.paint);
601 }
Chris Craikb565df12015-10-05 13:00:52 -0700602
Chris Craik15c3f192015-12-03 12:16:56 -0800603 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
604 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
605 // MergingDrawBatch::canMergeWith()
606 if (bakedState->computedState.transform.isSimple()
607 && bakedState->computedState.transform.positiveScale()
608 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800609 && op.bitmap->colorType() != kAlpha_8_SkColorType
610 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800611 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craik15c3f192015-12-03 12:16:56 -0800612 // TODO: AssetAtlas in mergeId
613 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
614 } else {
615 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
616 }
Chris Craikb565df12015-10-05 13:00:52 -0700617}
618
Chris Craikf158b492016-01-12 14:45:08 -0800619void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800620 BakedOpState* bakedState = tryBakeOpState(op);
621 if (!bakedState) return; // quick rejected
622 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
623}
624
Chris Craikf158b492016-01-12 14:45:08 -0800625void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800626 BakedOpState* bakedState = tryBakeOpState(op);
627 if (!bakedState) return; // quick rejected
628 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
629}
630
Doris Liu766431a2016-02-04 22:17:11 +0000631void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
632 const SkBitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
633 SkPaint* paint = op.vectorDrawable->getPaint();
John Reck7df9ff22016-02-10 16:08:08 -0800634 const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds,
Doris Liu766431a2016-02-04 22:17:11 +0000635 op.localMatrix,
636 op.localClip,
637 paint,
638 &bitmap,
639 Rect(bitmap.width(), bitmap.height()));
640 deferBitmapRectOp(*resolvedOp);
641}
642
Chris Craikf158b492016-01-12 14:45:08 -0800643void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800644 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
645 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
646 float x = *(op.x);
647 float y = *(op.y);
648 float radius = *(op.radius);
649 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
John Reck7df9ff22016-02-10 16:08:08 -0800650 const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800651 unmappedBounds,
652 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800653 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800654 op.paint);
655 deferOvalOp(*resolvedOp);
656}
657
Chris Craika2048482016-03-25 14:17:49 -0700658void FrameBuilder::deferColorOp(const ColorOp& op) {
659 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
660 if (!bakedState) return; // quick rejected
661 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
662}
663
Chris Craikf158b492016-01-12 14:45:08 -0800664void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700665 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craike29ce6f2015-12-10 16:25:13 -0800666 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800667 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800668}
669
Chris Craikf158b492016-01-12 14:45:08 -0800670void FrameBuilder::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800671 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800672 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800673}
674
Chris Craikf158b492016-01-12 14:45:08 -0800675void FrameBuilder::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800676 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800677}
678
Chris Craikf158b492016-01-12 14:45:08 -0800679void FrameBuilder::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800680 BakedOpState* bakedState = tryBakeOpState(op);
681 if (!bakedState) return; // quick rejected
682
683 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800684 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
685 && hasMergeableClip(*bakedState)) {
Chris Craikb250a832016-01-11 19:28:17 -0800686 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
Chris Craikf09ff5a2015-12-08 17:21:58 -0800687 // TODO: AssetAtlas in mergeId
688
689 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
690 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
691 } else {
692 // Use Bitmap batchId since Bitmap+Patch use same shader
693 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
694 }
695}
696
Chris Craikf158b492016-01-12 14:45:08 -0800697void FrameBuilder::deferPathOp(const PathOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700698 auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture);
699 if (CC_LIKELY(state)) {
700 mCaches.pathCache.precache(op.path, op.paint);
701 }
Chris Craik386aa032015-12-07 17:08:25 -0800702}
703
Chris Craikf158b492016-01-12 14:45:08 -0800704void FrameBuilder::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800705 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800706 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800707}
708
Chris Craikf158b492016-01-12 14:45:08 -0800709void FrameBuilder::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800710 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800711}
712
Chris Craikf158b492016-01-12 14:45:08 -0800713void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik3a5811b2016-03-22 15:03:08 -0700714 auto state = deferStrokeableOp(op, tessBatchId(op));
715 if (CC_LIKELY(state && !op.paint->getPathEffect())) {
716 // TODO: consider storing tessellation task in BakedOpState
717 mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint),
718 op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
719 }
Chris Craikb565df12015-10-05 13:00:52 -0700720}
721
Chris Craikf158b492016-01-12 14:45:08 -0800722void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800723 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
724 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
John Reck7df9ff22016-02-10 16:08:08 -0800725 const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
Chris Craik268a9c02015-12-09 18:05:12 -0800726 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
727 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800728 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800729 op.paint, *op.rx, *op.ry);
730 deferRoundRectOp(*resolvedOp);
731}
732
Chris Craikf158b492016-01-12 14:45:08 -0800733void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800734 BakedOpState* bakedState = tryBakeOpState(op);
735 if (!bakedState) return; // quick rejected
736 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700737}
738
Chris Craikd7448e62015-12-15 10:34:36 -0800739static batchid_t textBatchId(const SkPaint& paint) {
740 // TODO: better handling of shader (since we won't care about color then)
741 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
742}
743
Chris Craikf158b492016-01-12 14:45:08 -0800744void FrameBuilder::deferTextOp(const TextOp& op) {
Chris Craik7c02cab2016-03-16 17:15:12 -0700745 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
746 mAllocator, *mCanvasState.writableSnapshot(), op,
747 BakedOpState::StrokeBehavior::StyleDefined);
Chris Craik15c3f192015-12-03 12:16:56 -0800748 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800749
Chris Craikd7448e62015-12-15 10:34:36 -0800750 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800751 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800752 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
753 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800754 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
755 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
756 } else {
757 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
758 }
Chris Craik3a5811b2016-03-22 15:03:08 -0700759
760 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
761 auto& totalTransform = bakedState->computedState.transform;
762 if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) {
763 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
764 } else {
765 // Partial transform case, see BakedOpDispatcher::renderTextOp
766 float sx, sy;
767 totalTransform.decomposeScale(sx, sy);
768 fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::MakeScale(
769 roundf(std::max(1.0f, sx)),
770 roundf(std::max(1.0f, sy))));
771 }
Chris Craika1717272015-11-19 13:02:43 -0800772}
773
Chris Craikf158b492016-01-12 14:45:08 -0800774void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craik4c3980b2016-03-15 14:20:18 -0700775 BakedOpState* bakedState = tryBakeUnboundedOpState(op);
Chris Craikd7448e62015-12-15 10:34:36 -0800776 if (!bakedState) return; // quick rejected
777 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
Chris Craik3a5811b2016-03-22 15:03:08 -0700778
779 mCaches.fontRenderer.getFontRenderer().precache(
780 op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
Chris Craikd7448e62015-12-15 10:34:36 -0800781}
782
Chris Craikf158b492016-01-12 14:45:08 -0800783void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
John Reck417ed6d2016-03-22 16:01:08 -0700784 if (CC_UNLIKELY(!op.layer->isRenderable())) return;
Chris Craikaafb01d2016-03-25 18:34:11 -0700785
786 const TextureLayerOp* textureLayerOp = &op;
787 // Now safe to access transform (which was potentially unready at record time)
788 if (!op.layer->getTransform().isIdentity()) {
789 // non-identity transform present, so 'inject it' into op by copying + replacing matrix
790 Matrix4 combinedMatrix(op.localMatrix);
791 combinedMatrix.multiply(op.layer->getTransform());
792 textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix);
793 }
794 BakedOpState* bakedState = tryBakeOpState(*textureLayerOp);
795
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800796 if (!bakedState) return; // quick rejected
797 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
798}
799
Chris Craikf158b492016-01-12 14:45:08 -0800800void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800801 float contentTranslateX, float contentTranslateY,
802 const Rect& repaintRect,
803 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700804 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Florin Malitaeecff562015-12-21 10:43:01 -0500805 mCanvasState.save(SaveFlags::MatrixClip);
Chris Craik818c9fb2015-10-23 14:33:42 -0700806 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700807 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800808 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800809 mCanvasState.writableSnapshot()->transform->loadTranslate(
810 contentTranslateX, contentTranslateY, 0);
811 mCanvasState.writableSnapshot()->setClip(
812 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800813
Chris Craik8ecf41c2015-11-16 10:27:59 -0800814 // create a new layer repaint, and push its index on the stack
Chris Craikf158b492016-01-12 14:45:08 -0800815 mLayerStack.push_back(mLayerBuilders.size());
816 auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
Chris Craik84ad6142016-01-12 12:09:19 -0800817 repaintRect, beginLayerOp, renderNode);
Chris Craikf158b492016-01-12 14:45:08 -0800818 mLayerBuilders.push_back(newFbo);
Chris Craik0b7e8242015-10-28 16:50:44 -0700819}
820
Chris Craikf158b492016-01-12 14:45:08 -0800821void FrameBuilder::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700822 // restore canvas, and pop finished layer off of the stack
823 mCanvasState.restore();
824 mLayerStack.pop_back();
825}
826
Chris Craikb87eadd2016-01-06 09:16:05 -0800827// TODO: defer time rejection (when bounds become empty) + tests
828// Option - just skip layers with no bounds at playback + defer?
Chris Craikf158b492016-01-12 14:45:08 -0800829void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800830 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
831 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
832
833 auto previous = mCanvasState.currentSnapshot();
834 Vector3 lightCenter = previous->getRelativeLightCenter();
835
836 // Combine all transforms used to present saveLayer content:
837 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800838 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800839 contentTransform.multiply(op.localMatrix);
840 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
841
842 Matrix4 inverseContentTransform;
843 inverseContentTransform.loadInverse(contentTransform);
844
845 // map the light center into layer-relative space
846 inverseContentTransform.mapPoint3d(lightCenter);
847
848 // Clip bounds of temporary layer to parent's clip rect, so:
849 Rect saveLayerBounds(layerWidth, layerHeight);
850 // 1) transform Rect(width, height) into parent's space
851 // note: left/top offsets put in contentTransform above
852 contentTransform.mapRect(saveLayerBounds);
853 // 2) intersect with parent's clip
854 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
855 // 3) and transform back
856 inverseContentTransform.mapRect(saveLayerBounds);
857 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
858 saveLayerBounds.roundOut();
859
860 // if bounds are reduced, will clip the layer's area by reducing required bounds...
861 layerWidth = saveLayerBounds.getWidth();
862 layerHeight = saveLayerBounds.getHeight();
863 // ...and shifting drawing content to account for left/top side clipping
864 float contentTranslateX = -saveLayerBounds.left;
865 float contentTranslateY = -saveLayerBounds.top;
866
867 saveForLayer(layerWidth, layerHeight,
868 contentTranslateX, contentTranslateY,
869 Rect(layerWidth, layerHeight),
870 lightCenter,
871 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700872}
Chris Craikb565df12015-10-05 13:00:52 -0700873
Chris Craikf158b492016-01-12 14:45:08 -0800874void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700875 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700876 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700877
878 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700879
880 // record the draw operation into the previous layer's list of draw commands
881 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
John Reck7df9ff22016-02-10 16:08:08 -0800882 LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
Chris Craik6fe991e52015-10-20 09:39:42 -0700883 beginLayerOp.unmappedBounds,
884 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800885 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700886 beginLayerOp.paint,
Chris Craikf158b492016-01-12 14:45:08 -0800887 &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
Chris Craik6fe991e52015-10-20 09:39:42 -0700888 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
889
890 if (bakedOpState) {
891 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
892 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
893 } else {
894 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800895 // TODO: need to prevent any render work from being done
896 // - create layerop earlier for reject purposes?
Chris Craikf158b492016-01-12 14:45:08 -0800897 mLayerBuilders[finishedLayerIndex]->clear();
Chris Craik6fe991e52015-10-20 09:39:42 -0700898 return;
Chris Craikb565df12015-10-05 13:00:52 -0700899 }
900}
901
Chris Craikf158b492016-01-12 14:45:08 -0800902void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800903 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
904 boundsTransform.multiply(op.localMatrix);
905
906 Rect dstRect(op.unmappedBounds);
907 boundsTransform.mapRect(dstRect);
908 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
909
Chris Craik4876de12016-02-25 16:54:08 -0800910 if (dstRect.isEmpty()) {
911 // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
912 currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
913 } else {
914 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
915 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800916
Chris Craik4876de12016-02-25 16:54:08 -0800917 /**
918 * First, defer an operation to copy out the content from the rendertarget into a layer.
919 */
920 auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
921 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
922 &(currentLayer().repaintClip), dstRect, *copyToOp);
923 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
Chris Craikb87eadd2016-01-06 09:16:05 -0800924
Chris Craik4876de12016-02-25 16:54:08 -0800925 /**
926 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
927 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
928 */
929 currentLayer().deferLayerClear(dstRect);
Chris Craikb87eadd2016-01-06 09:16:05 -0800930
Chris Craik4876de12016-02-25 16:54:08 -0800931 /**
932 * And stash an operation to copy that layer back under the rendertarget until
933 * a balanced EndUnclippedLayerOp is seen
934 */
935 auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
936 bakedState = BakedOpState::directConstruct(mAllocator,
937 &(currentLayer().repaintClip), dstRect, *copyFromOp);
938 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
939 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800940}
941
Chris Craikf158b492016-01-12 14:45:08 -0800942void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800943 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
944
945 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
Chris Craikb87eadd2016-01-06 09:16:05 -0800946 currentLayer().activeUnclippedSaveLayers.pop_back();
Chris Craik4876de12016-02-25 16:54:08 -0800947 if (copyFromLayerOp) {
948 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
949 }
Chris Craikb87eadd2016-01-06 09:16:05 -0800950}
951
Chris Craik3a5811b2016-03-22 15:03:08 -0700952void FrameBuilder::finishDefer() {
953 mCaches.fontRenderer.endPrecaching();
954}
955
Chris Craikb565df12015-10-05 13:00:52 -0700956} // namespace uirenderer
957} // namespace android