blob: 4bfc0b413c563f0d18028787ef4794913cc26a13 [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 Craik5ea17242016-01-11 14:07:59 -080017#include "FrameReorderer.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"
Chris Craik98787e62015-11-13 10:55:30 -080021#include "renderstate/OffscreenBufferPool.h"
Chris Craik161f54b2015-11-05 11:08:52 -080022#include "utils/FatVector.h"
23#include "utils/PaintUtils.h"
Chris Craik8ecf41c2015-11-16 10:27:59 -080024#include "utils/TraceUtils.h"
Chris Craikb565df12015-10-05 13:00:52 -070025
Chris Craik161f54b2015-11-05 11:08:52 -080026#include <SkCanvas.h>
Chris Craikd3daa312015-11-06 10:59:56 -080027#include <SkPathOps.h>
Chris Craik161f54b2015-11-05 11:08:52 -080028#include <utils/TypeHelpers.h>
Chris Craikb565df12015-10-05 13:00:52 -070029
30namespace android {
31namespace uirenderer {
32
Chris Craik5ea17242016-01-11 14:07:59 -080033FrameReorderer::FrameReorderer(const LayerUpdateQueue& layers, const SkRect& clip,
Chris Craik0b7e8242015-10-28 16:50:44 -070034 uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craik98787e62015-11-13 10:55:30 -080035 const std::vector< sp<RenderNode> >& nodes, const Vector3& lightCenter)
Chris Craik6fe991e52015-10-20 09:39:42 -070036 : mCanvasState(*this) {
Chris Craik818c9fb2015-10-23 14:33:42 -070037 ATRACE_NAME("prepare drawing commands");
Chris Craikb565df12015-10-05 13:00:52 -070038
Chris Craik98787e62015-11-13 10:55:30 -080039 mLayerReorderers.reserve(layers.entries().size());
40 mLayerStack.reserve(layers.entries().size());
41
42 // Prepare to defer Fbo0
43 mLayerReorderers.emplace_back(viewportWidth, viewportHeight, Rect(clip));
44 mLayerStack.push_back(0);
Chris Craikb565df12015-10-05 13:00:52 -070045 mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -070046 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
Chris Craik98787e62015-11-13 10:55:30 -080047 lightCenter);
Chris Craik0b7e8242015-10-28 16:50:44 -070048
49 // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
50 // updated in the order they're passed in (mLayerReorderers are issued to Renderer in reverse)
51 for (int i = layers.entries().size() - 1; i >= 0; i--) {
52 RenderNode* layerNode = layers.entries()[i].renderNode;
53 const Rect& layerDamage = layers.entries()[i].damage;
Chris Craik8d1f2122015-11-24 16:40:09 -080054 layerNode->computeOrdering();
Chris Craik0b7e8242015-10-28 16:50:44 -070055
Chris Craik8ecf41c2015-11-16 10:27:59 -080056 // map current light center into RenderNode's coordinate space
57 Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
58 layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
59
60 saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
61 layerDamage, lightCenter, nullptr, layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070062
63 if (layerNode->getDisplayList()) {
Chris Craik8d1f2122015-11-24 16:40:09 -080064 deferNodeOps(*layerNode);
Chris Craik0b7e8242015-10-28 16:50:44 -070065 }
66 restoreForLayer();
67 }
68
69 // Defer Fbo0
Chris Craikb565df12015-10-05 13:00:52 -070070 for (const sp<RenderNode>& node : nodes) {
71 if (node->nothingToDraw()) continue;
Chris Craik8d1f2122015-11-24 16:40:09 -080072 node->computeOrdering();
Chris Craikb565df12015-10-05 13:00:52 -070073
Chris Craik0b7e8242015-10-28 16:50:44 -070074 int count = mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
75 deferNodePropsAndOps(*node);
76 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -070077 }
78}
79
Chris Craik5ea17242016-01-11 14:07:59 -080080void FrameReorderer::onViewportInitialized() {}
Chris Craik818c9fb2015-10-23 14:33:42 -070081
Chris Craik5ea17242016-01-11 14:07:59 -080082void FrameReorderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
Chris Craik818c9fb2015-10-23 14:33:42 -070083
Chris Craik5ea17242016-01-11 14:07:59 -080084void FrameReorderer::deferNodePropsAndOps(RenderNode& node) {
Chris Craik8ecf41c2015-11-16 10:27:59 -080085 const RenderProperties& properties = node.properties();
86 const Outline& outline = properties.getOutline();
87 if (properties.getAlpha() <= 0
88 || (outline.getShouldClip() && outline.isEmpty())
89 || properties.getScaleX() == 0
90 || properties.getScaleY() == 0) {
91 return; // rejected
92 }
93
94 if (properties.getLeft() != 0 || properties.getTop() != 0) {
95 mCanvasState.translate(properties.getLeft(), properties.getTop());
96 }
97 if (properties.getStaticMatrix()) {
98 mCanvasState.concatMatrix(*properties.getStaticMatrix());
99 } else if (properties.getAnimationMatrix()) {
100 mCanvasState.concatMatrix(*properties.getAnimationMatrix());
101 }
102 if (properties.hasTransformMatrix()) {
103 if (properties.isTransformTranslateOnly()) {
104 mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
105 } else {
106 mCanvasState.concatMatrix(*properties.getTransformMatrix());
107 }
108 }
109
110 const int width = properties.getWidth();
111 const int height = properties.getHeight();
112
113 Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
114 const bool isLayer = properties.effectiveLayerType() != LayerType::None;
115 int clipFlags = properties.getClippingFlags();
116 if (properties.getAlpha() < 1) {
117 if (isLayer) {
118 clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
119 }
120 if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
121 // simply scale rendering content's alpha
122 mCanvasState.scaleAlpha(properties.getAlpha());
123 } else {
124 // schedule saveLayer by initializing saveLayerBounds
125 saveLayerBounds.set(0, 0, width, height);
126 if (clipFlags) {
127 properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
128 clipFlags = 0; // all clipping done by savelayer
129 }
130 }
131
132 if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
133 // pretend alpha always causes savelayer to warn about
134 // performance problem affecting old versions
135 ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
136 }
137 }
138 if (clipFlags) {
139 Rect clipRect;
140 properties.getClippingRectForFlags(clipFlags, &clipRect);
141 mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
142 SkRegion::kIntersect_Op);
143 }
144
145 if (properties.getRevealClip().willClip()) {
146 Rect bounds;
147 properties.getRevealClip().getBounds(&bounds);
148 mCanvasState.setClippingRoundRect(mAllocator,
149 bounds, properties.getRevealClip().getRadius());
150 } else if (properties.getOutline().willClip()) {
151 mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
152 }
153
154 if (!mCanvasState.quickRejectConservative(0, 0, width, height)) {
155 // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
Chris Craik0b7e8242015-10-28 16:50:44 -0700156 if (node.getLayer()) {
157 // HW layer
158 LayerOp* drawLayerOp = new (mAllocator) LayerOp(node);
159 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
160 if (bakedOpState) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800161 // Node's layer already deferred, schedule it to render into parent layer
Chris Craik0b7e8242015-10-28 16:50:44 -0700162 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
163 }
Chris Craik8ecf41c2015-11-16 10:27:59 -0800164 } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
165 // draw DisplayList contents within temporary, since persisted layer could not be used.
166 // (temp layers are clipped to viewport, since they don't persist offscreen content)
167 SkPaint saveLayerPaint;
168 saveLayerPaint.setAlpha(properties.getAlpha());
Chris Craik268a9c02015-12-09 18:05:12 -0800169 deferBeginLayerOp(*new (mAllocator) BeginLayerOp(
Chris Craik8ecf41c2015-11-16 10:27:59 -0800170 saveLayerBounds,
171 Matrix4::identity(),
Chris Craike4db79d2015-12-22 16:32:23 -0800172 nullptr, // no record-time clip - need only respect defer-time one
Chris Craik8ecf41c2015-11-16 10:27:59 -0800173 &saveLayerPaint));
Chris Craik8d1f2122015-11-24 16:40:09 -0800174 deferNodeOps(node);
Chris Craik268a9c02015-12-09 18:05:12 -0800175 deferEndLayerOp(*new (mAllocator) EndLayerOp());
Chris Craik0b7e8242015-10-28 16:50:44 -0700176 } else {
Chris Craik8d1f2122015-11-24 16:40:09 -0800177 deferNodeOps(node);
Chris Craik0b7e8242015-10-28 16:50:44 -0700178 }
179 }
180}
181
Chris Craik161f54b2015-11-05 11:08:52 -0800182typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
183
184template <typename V>
185static void buildZSortedChildList(V* zTranslatedNodes,
186 const DisplayList& displayList, const DisplayList::Chunk& chunk) {
187 if (chunk.beginChildIndex == chunk.endChildIndex) return;
188
189 for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
190 RenderNodeOp* childOp = displayList.getChildren()[i];
191 RenderNode* child = childOp->renderNode;
192 float childZ = child->properties().getZ();
193
194 if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
195 zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
196 childOp->skipInOrderDraw = true;
197 } else if (!child->properties().getProjectBackwards()) {
198 // regular, in order drawing DisplayList
199 childOp->skipInOrderDraw = false;
200 }
201 }
202
203 // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
204 std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
205}
206
207template <typename V>
208static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
209 for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
210 if (zTranslatedNodes[i].key >= 0.0f) return i;
211 }
212 return zTranslatedNodes.size();
213}
214
215template <typename V>
Chris Craik5ea17242016-01-11 14:07:59 -0800216void FrameReorderer::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) {
Chris Craik161f54b2015-11-05 11:08:52 -0800217 const int size = zTranslatedNodes.size();
218 if (size == 0
219 || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
220 || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
221 // no 3d children to draw
222 return;
223 }
224
225 /**
226 * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
227 * with very similar Z heights to draw together.
228 *
229 * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
230 * underneath both, and neither's shadow is drawn on top of the other.
231 */
232 const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
233 size_t drawIndex, shadowIndex, endIndex;
234 if (mode == ChildrenSelectMode::Negative) {
235 drawIndex = 0;
236 endIndex = nonNegativeIndex;
237 shadowIndex = endIndex; // draw no shadows
238 } else {
239 drawIndex = nonNegativeIndex;
240 endIndex = size;
241 shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
242 }
243
244 float lastCasterZ = 0.0f;
245 while (shadowIndex < endIndex || drawIndex < endIndex) {
246 if (shadowIndex < endIndex) {
247 const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
248 const float casterZ = zTranslatedNodes[shadowIndex].key;
249 // attempt to render the shadow if the caster about to be drawn is its caster,
250 // OR if its caster's Z value is similar to the previous potential caster
251 if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
252 deferShadow(*casterNodeOp);
253
254 lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
255 shadowIndex++;
256 continue;
257 }
258 }
259
260 const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
Chris Craik268a9c02015-12-09 18:05:12 -0800261 deferRenderNodeOpImpl(*childOp);
Chris Craik161f54b2015-11-05 11:08:52 -0800262 drawIndex++;
263 }
264}
265
Chris Craik5ea17242016-01-11 14:07:59 -0800266void FrameReorderer::deferShadow(const RenderNodeOp& casterNodeOp) {
Chris Craikd3daa312015-11-06 10:59:56 -0800267 auto& node = *casterNodeOp.renderNode;
268 auto& properties = node.properties();
269
270 if (properties.getAlpha() <= 0.0f
271 || properties.getOutline().getAlpha() <= 0.0f
272 || !properties.getOutline().getPath()
273 || properties.getScaleX() == 0
274 || properties.getScaleY() == 0) {
275 // no shadow to draw
276 return;
277 }
278
279 const SkPath* casterOutlinePath = properties.getOutline().getPath();
280 const SkPath* revealClipPath = properties.getRevealClip().getPath();
281 if (revealClipPath && revealClipPath->isEmpty()) return;
282
283 float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
284
285 // holds temporary SkPath to store the result of intersections
286 SkPath* frameAllocatedPath = nullptr;
287 const SkPath* casterPath = casterOutlinePath;
288
289 // intersect the shadow-casting path with the reveal, if present
290 if (revealClipPath) {
291 frameAllocatedPath = createFrameAllocatedPath();
292
293 Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
294 casterPath = frameAllocatedPath;
295 }
296
297 // intersect the shadow-casting path with the clipBounds, if present
298 if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
299 if (!frameAllocatedPath) {
300 frameAllocatedPath = createFrameAllocatedPath();
301 }
302 Rect clipBounds;
303 properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
304 SkPath clipBoundsPath;
305 clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
306 clipBounds.right, clipBounds.bottom);
307
308 Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
309 casterPath = frameAllocatedPath;
310 }
311
312 ShadowOp* shadowOp = new (mAllocator) ShadowOp(casterNodeOp, casterAlpha, casterPath,
Chris Craik98787e62015-11-13 10:55:30 -0800313 mCanvasState.getLocalClipBounds(),
314 mCanvasState.currentSnapshot()->getRelativeLightCenter());
Chris Craikd3daa312015-11-06 10:59:56 -0800315 BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800316 mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
Chris Craikd3daa312015-11-06 10:59:56 -0800317 if (CC_LIKELY(bakedOpState)) {
318 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
319 }
Chris Craik161f54b2015-11-05 11:08:52 -0800320}
Chris Craikd3daa312015-11-06 10:59:56 -0800321
Chris Craik5ea17242016-01-11 14:07:59 -0800322void FrameReorderer::deferProjectedChildren(const RenderNode& renderNode) {
Chris Craik8d1f2122015-11-24 16:40:09 -0800323 const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
324 int count = mCanvasState.save(SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
325
326 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
327 const DisplayList& displayList = *(renderNode.getDisplayList());
328
329 const RecordedOp* op = (displayList.getOps()[displayList.projectionReceiveIndex]);
330 const RenderNodeOp* backgroundOp = static_cast<const RenderNodeOp*>(op);
331 const RenderProperties& backgroundProps = backgroundOp->renderNode->properties();
332
333 // Transform renderer to match background we're projecting onto
334 // (by offsetting canvas by translationX/Y of background rendernode, since only those are set)
335 mCanvasState.translate(backgroundProps.getTranslationX(), backgroundProps.getTranslationY());
336
337 // If the projection receiver has an outline, we mask projected content to it
338 // (which we know, apriori, are all tessellated paths)
339 mCanvasState.setProjectionPathMask(mAllocator, projectionReceiverOutline);
340
341 // draw projected nodes
342 for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
343 RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
344
345 int restoreTo = mCanvasState.save(SkCanvas::kMatrix_SaveFlag);
346 mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
Chris Craik268a9c02015-12-09 18:05:12 -0800347 deferRenderNodeOpImpl(*childOp);
Chris Craik8d1f2122015-11-24 16:40:09 -0800348 mCanvasState.restoreToCount(restoreTo);
349 }
350
351 mCanvasState.restoreToCount(count);
352}
353
Chris Craikb565df12015-10-05 13:00:52 -0700354/**
Chris Craik5ea17242016-01-11 14:07:59 -0800355 * Used to define a list of lambdas referencing private FrameReorderer::onXX::defer() methods.
Chris Craikb565df12015-10-05 13:00:52 -0700356 *
Chris Craik8d1f2122015-11-24 16:40:09 -0800357 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
Chris Craik5ea17242016-01-11 14:07:59 -0800358 * E.g. a BitmapOp op then would be dispatched to FrameReorderer::onBitmapOp(const BitmapOp&)
Chris Craikb565df12015-10-05 13:00:52 -0700359 */
Chris Craik6fe991e52015-10-20 09:39:42 -0700360#define OP_RECEIVER(Type) \
Chris Craik5ea17242016-01-11 14:07:59 -0800361 [](FrameReorderer& reorderer, const RecordedOp& op) { reorderer.defer##Type(static_cast<const Type&>(op)); },
362void FrameReorderer::deferNodeOps(const RenderNode& renderNode) {
363 typedef void (*OpDispatcher) (FrameReorderer& reorderer, const RecordedOp& op);
Chris Craik7cbf63d2016-01-06 13:46:52 -0800364 static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
Chris Craik8d1f2122015-11-24 16:40:09 -0800365
366 // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
367 const DisplayList& displayList = *(renderNode.getDisplayList());
Chris Craikb36af872015-10-16 14:23:12 -0700368 for (const DisplayList::Chunk& chunk : displayList.getChunks()) {
Chris Craik161f54b2015-11-05 11:08:52 -0800369 FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
370 buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
371
372 defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700373 for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
Chris Craikb36af872015-10-16 14:23:12 -0700374 const RecordedOp* op = displayList.getOps()[opIndex];
Chris Craikb565df12015-10-05 13:00:52 -0700375 receivers[op->opId](*this, *op);
Chris Craik8d1f2122015-11-24 16:40:09 -0800376
377 if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
378 && displayList.projectionReceiveIndex >= 0
379 && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
380 deferProjectedChildren(renderNode);
381 }
Chris Craikb565df12015-10-05 13:00:52 -0700382 }
Chris Craik161f54b2015-11-05 11:08:52 -0800383 defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes);
Chris Craikb565df12015-10-05 13:00:52 -0700384 }
385}
386
Chris Craik5ea17242016-01-11 14:07:59 -0800387void FrameReorderer::deferRenderNodeOpImpl(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800388 if (op.renderNode->nothingToDraw()) return;
Chris Craik6fe991e52015-10-20 09:39:42 -0700389 int count = mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
Chris Craikb565df12015-10-05 13:00:52 -0700390
Chris Craike4db79d2015-12-22 16:32:23 -0800391 // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
392 mCanvasState.writableSnapshot()->mutateClipArea().applyClip(op.localClip,
393 *mCanvasState.currentSnapshot()->transform);
Chris Craikb565df12015-10-05 13:00:52 -0700394 mCanvasState.concatMatrix(op.localMatrix);
Chris Craikb565df12015-10-05 13:00:52 -0700395
Chris Craik0b7e8242015-10-28 16:50:44 -0700396 // then apply state from node properties, and defer ops
397 deferNodePropsAndOps(*op.renderNode);
398
Chris Craik6fe991e52015-10-20 09:39:42 -0700399 mCanvasState.restoreToCount(count);
Chris Craikb565df12015-10-05 13:00:52 -0700400}
401
Chris Craik5ea17242016-01-11 14:07:59 -0800402void FrameReorderer::deferRenderNodeOp(const RenderNodeOp& op) {
Chris Craik161f54b2015-11-05 11:08:52 -0800403 if (!op.skipInOrderDraw) {
Chris Craik268a9c02015-12-09 18:05:12 -0800404 deferRenderNodeOpImpl(op);
Chris Craik161f54b2015-11-05 11:08:52 -0800405 }
406}
407
Chris Craik386aa032015-12-07 17:08:25 -0800408/**
409 * Defers an unmergeable, strokeable op, accounting correctly
410 * for paint's style on the bounds being computed.
411 */
Chris Craik5ea17242016-01-11 14:07:59 -0800412void FrameReorderer::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
Chris Craik386aa032015-12-07 17:08:25 -0800413 BakedOpState::StrokeBehavior strokeBehavior) {
414 // Note: here we account for stroke when baking the op
415 BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
Chris Craike4db79d2015-12-22 16:32:23 -0800416 mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
Chris Craik386aa032015-12-07 17:08:25 -0800417 if (!bakedState) return; // quick rejected
418 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
419}
420
421/**
422 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
423 * be used, since they trigger significantly different rendering paths.
424 *
425 * Note: not used for lines/points, since they don't currently support path effects.
426 */
427static batchid_t tessBatchId(const RecordedOp& op) {
428 const SkPaint& paint = *(op.paint);
Chris Craikb565df12015-10-05 13:00:52 -0700429 return paint.getPathEffect()
430 ? OpBatchType::AlphaMaskTexture
431 : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
432}
433
Chris Craik5ea17242016-01-11 14:07:59 -0800434void FrameReorderer::deferArcOp(const ArcOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800435 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800436}
437
Chris Craikb87eadd2016-01-06 09:16:05 -0800438static bool hasMergeableClip(const BakedOpState& state) {
439 return state.computedState.clipState
440 || state.computedState.clipState->mode == ClipMode::Rectangle;
441}
442
Chris Craik5ea17242016-01-11 14:07:59 -0800443void FrameReorderer::deferBitmapOp(const BitmapOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800444 BakedOpState* bakedState = tryBakeOpState(op);
445 if (!bakedState) return; // quick rejected
Chris Craikb565df12015-10-05 13:00:52 -0700446
Chris Craik15c3f192015-12-03 12:16:56 -0800447 // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
448 // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
449 // MergingDrawBatch::canMergeWith()
450 if (bakedState->computedState.transform.isSimple()
451 && bakedState->computedState.transform.positiveScale()
452 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
Chris Craikb87eadd2016-01-06 09:16:05 -0800453 && op.bitmap->colorType() != kAlpha_8_SkColorType
454 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800455 mergeid_t mergeId = (mergeid_t) op.bitmap->getGenerationID();
456 // TODO: AssetAtlas in mergeId
457 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
458 } else {
459 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
460 }
Chris Craikb565df12015-10-05 13:00:52 -0700461}
462
Chris Craik5ea17242016-01-11 14:07:59 -0800463void FrameReorderer::deferBitmapMeshOp(const BitmapMeshOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800464 BakedOpState* bakedState = tryBakeOpState(op);
465 if (!bakedState) return; // quick rejected
466 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
467}
468
Chris Craik5ea17242016-01-11 14:07:59 -0800469void FrameReorderer::deferBitmapRectOp(const BitmapRectOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800470 BakedOpState* bakedState = tryBakeOpState(op);
471 if (!bakedState) return; // quick rejected
472 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
473}
474
Chris Craik5ea17242016-01-11 14:07:59 -0800475void FrameReorderer::deferCirclePropsOp(const CirclePropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800476 // allocate a temporary oval op (with mAllocator, so it persists until render), so the
477 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
478 float x = *(op.x);
479 float y = *(op.y);
480 float radius = *(op.radius);
481 Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
482 const OvalOp* resolvedOp = new (mAllocator) OvalOp(
483 unmappedBounds,
484 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800485 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800486 op.paint);
487 deferOvalOp(*resolvedOp);
488}
489
Chris Craik5ea17242016-01-11 14:07:59 -0800490void FrameReorderer::deferFunctorOp(const FunctorOp& op) {
Chris Craike29ce6f2015-12-10 16:25:13 -0800491 BakedOpState* bakedState = tryBakeOpState(op);
492 if (!bakedState) return; // quick rejected
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800493 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
Chris Craike29ce6f2015-12-10 16:25:13 -0800494}
495
Chris Craik5ea17242016-01-11 14:07:59 -0800496void FrameReorderer::deferLinesOp(const LinesOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800497 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800498 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craik386aa032015-12-07 17:08:25 -0800499}
500
Chris Craik5ea17242016-01-11 14:07:59 -0800501void FrameReorderer::deferOvalOp(const OvalOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800502 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800503}
504
Chris Craik5ea17242016-01-11 14:07:59 -0800505void FrameReorderer::deferPatchOp(const PatchOp& op) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800506 BakedOpState* bakedState = tryBakeOpState(op);
507 if (!bakedState) return; // quick rejected
508
509 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800510 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
511 && hasMergeableClip(*bakedState)) {
Chris Craikf09ff5a2015-12-08 17:21:58 -0800512 mergeid_t mergeId = (mergeid_t) op.bitmap->getGenerationID();
513 // TODO: AssetAtlas in mergeId
514
515 // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
516 currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
517 } else {
518 // Use Bitmap batchId since Bitmap+Patch use same shader
519 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
520 }
521}
522
Chris Craik5ea17242016-01-11 14:07:59 -0800523void FrameReorderer::deferPathOp(const PathOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800524 deferStrokeableOp(op, OpBatchType::Bitmap);
Chris Craik386aa032015-12-07 17:08:25 -0800525}
526
Chris Craik5ea17242016-01-11 14:07:59 -0800527void FrameReorderer::deferPointsOp(const PointsOp& op) {
Chris Craik386aa032015-12-07 17:08:25 -0800528 batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
Chris Craik268a9c02015-12-09 18:05:12 -0800529 deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
Chris Craika1717272015-11-19 13:02:43 -0800530}
531
Chris Craik5ea17242016-01-11 14:07:59 -0800532void FrameReorderer::deferRectOp(const RectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800533 deferStrokeableOp(op, tessBatchId(op));
Chris Craik386aa032015-12-07 17:08:25 -0800534}
535
Chris Craik5ea17242016-01-11 14:07:59 -0800536void FrameReorderer::deferRoundRectOp(const RoundRectOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800537 deferStrokeableOp(op, tessBatchId(op));
Chris Craikb565df12015-10-05 13:00:52 -0700538}
539
Chris Craik5ea17242016-01-11 14:07:59 -0800540void FrameReorderer::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
Chris Craik268a9c02015-12-09 18:05:12 -0800541 // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
542 // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
543 const RoundRectOp* resolvedOp = new (mAllocator) RoundRectOp(
544 Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
545 op.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800546 op.localClip,
Chris Craik268a9c02015-12-09 18:05:12 -0800547 op.paint, *op.rx, *op.ry);
548 deferRoundRectOp(*resolvedOp);
549}
550
Chris Craik5ea17242016-01-11 14:07:59 -0800551void FrameReorderer::deferSimpleRectsOp(const SimpleRectsOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800552 BakedOpState* bakedState = tryBakeOpState(op);
553 if (!bakedState) return; // quick rejected
554 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
Chris Craikb565df12015-10-05 13:00:52 -0700555}
556
Chris Craikd7448e62015-12-15 10:34:36 -0800557static batchid_t textBatchId(const SkPaint& paint) {
558 // TODO: better handling of shader (since we won't care about color then)
559 return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
560}
561
Chris Craik5ea17242016-01-11 14:07:59 -0800562void FrameReorderer::deferTextOp(const TextOp& op) {
Chris Craik15c3f192015-12-03 12:16:56 -0800563 BakedOpState* bakedState = tryBakeOpState(op);
564 if (!bakedState) return; // quick rejected
Chris Craika1717272015-11-19 13:02:43 -0800565
Chris Craikd7448e62015-12-15 10:34:36 -0800566 batchid_t batchId = textBatchId(*(op.paint));
Chris Craik15c3f192015-12-03 12:16:56 -0800567 if (bakedState->computedState.transform.isPureTranslate()
Chris Craikb87eadd2016-01-06 09:16:05 -0800568 && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
569 && hasMergeableClip(*bakedState)) {
Chris Craik15c3f192015-12-03 12:16:56 -0800570 mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
571 currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
572 } else {
573 currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
574 }
Chris Craika1717272015-11-19 13:02:43 -0800575}
576
Chris Craik5ea17242016-01-11 14:07:59 -0800577void FrameReorderer::deferTextOnPathOp(const TextOnPathOp& op) {
Chris Craikd7448e62015-12-15 10:34:36 -0800578 BakedOpState* bakedState = tryBakeOpState(op);
579 if (!bakedState) return; // quick rejected
580 currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
581}
582
Chris Craik5ea17242016-01-11 14:07:59 -0800583void FrameReorderer::deferTextureLayerOp(const TextureLayerOp& op) {
Chris Craikd2dfd8f2015-12-16 14:27:20 -0800584 BakedOpState* bakedState = tryBakeOpState(op);
585 if (!bakedState) return; // quick rejected
586 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
587}
588
Chris Craik5ea17242016-01-11 14:07:59 -0800589void FrameReorderer::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
Chris Craik8ecf41c2015-11-16 10:27:59 -0800590 float contentTranslateX, float contentTranslateY,
591 const Rect& repaintRect,
592 const Vector3& lightCenter,
Chris Craik0b7e8242015-10-28 16:50:44 -0700593 const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700594 mCanvasState.save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
Chris Craik818c9fb2015-10-23 14:33:42 -0700595 mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
Chris Craik6fe991e52015-10-20 09:39:42 -0700596 mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800597 mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
Chris Craik8ecf41c2015-11-16 10:27:59 -0800598 mCanvasState.writableSnapshot()->transform->loadTranslate(
599 contentTranslateX, contentTranslateY, 0);
600 mCanvasState.writableSnapshot()->setClip(
601 repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
Chris Craik98787e62015-11-13 10:55:30 -0800602
Chris Craik8ecf41c2015-11-16 10:27:59 -0800603 // create a new layer repaint, and push its index on the stack
Chris Craik6fe991e52015-10-20 09:39:42 -0700604 mLayerStack.push_back(mLayerReorderers.size());
Chris Craik98787e62015-11-13 10:55:30 -0800605 mLayerReorderers.emplace_back(layerWidth, layerHeight, repaintRect, beginLayerOp, renderNode);
Chris Craik0b7e8242015-10-28 16:50:44 -0700606}
607
Chris Craik5ea17242016-01-11 14:07:59 -0800608void FrameReorderer::restoreForLayer() {
Chris Craik0b7e8242015-10-28 16:50:44 -0700609 // restore canvas, and pop finished layer off of the stack
610 mCanvasState.restore();
611 mLayerStack.pop_back();
612}
613
Chris Craikb87eadd2016-01-06 09:16:05 -0800614// TODO: defer time rejection (when bounds become empty) + tests
615// Option - just skip layers with no bounds at playback + defer?
Chris Craik5ea17242016-01-11 14:07:59 -0800616void FrameReorderer::deferBeginLayerOp(const BeginLayerOp& op) {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800617 uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
618 uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
619
620 auto previous = mCanvasState.currentSnapshot();
621 Vector3 lightCenter = previous->getRelativeLightCenter();
622
623 // Combine all transforms used to present saveLayer content:
624 // parent content transform * canvas transform * bounds offset
Chris Craikb87eadd2016-01-06 09:16:05 -0800625 Matrix4 contentTransform(*(previous->transform));
Chris Craik8ecf41c2015-11-16 10:27:59 -0800626 contentTransform.multiply(op.localMatrix);
627 contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
628
629 Matrix4 inverseContentTransform;
630 inverseContentTransform.loadInverse(contentTransform);
631
632 // map the light center into layer-relative space
633 inverseContentTransform.mapPoint3d(lightCenter);
634
635 // Clip bounds of temporary layer to parent's clip rect, so:
636 Rect saveLayerBounds(layerWidth, layerHeight);
637 // 1) transform Rect(width, height) into parent's space
638 // note: left/top offsets put in contentTransform above
639 contentTransform.mapRect(saveLayerBounds);
640 // 2) intersect with parent's clip
641 saveLayerBounds.doIntersect(previous->getRenderTargetClip());
642 // 3) and transform back
643 inverseContentTransform.mapRect(saveLayerBounds);
644 saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
645 saveLayerBounds.roundOut();
646
647 // if bounds are reduced, will clip the layer's area by reducing required bounds...
648 layerWidth = saveLayerBounds.getWidth();
649 layerHeight = saveLayerBounds.getHeight();
650 // ...and shifting drawing content to account for left/top side clipping
651 float contentTranslateX = -saveLayerBounds.left;
652 float contentTranslateY = -saveLayerBounds.top;
653
654 saveForLayer(layerWidth, layerHeight,
655 contentTranslateX, contentTranslateY,
656 Rect(layerWidth, layerHeight),
657 lightCenter,
658 &op, nullptr);
Chris Craik6fe991e52015-10-20 09:39:42 -0700659}
Chris Craikb565df12015-10-05 13:00:52 -0700660
Chris Craik5ea17242016-01-11 14:07:59 -0800661void FrameReorderer::deferEndLayerOp(const EndLayerOp& /* ignored */) {
Chris Craik6fe991e52015-10-20 09:39:42 -0700662 const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
Chris Craik6fe991e52015-10-20 09:39:42 -0700663 int finishedLayerIndex = mLayerStack.back();
Chris Craik0b7e8242015-10-28 16:50:44 -0700664
665 restoreForLayer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700666
667 // record the draw operation into the previous layer's list of draw commands
668 // uses state from the associated beginLayerOp, since it has all the state needed for drawing
669 LayerOp* drawLayerOp = new (mAllocator) LayerOp(
670 beginLayerOp.unmappedBounds,
671 beginLayerOp.localMatrix,
Chris Craike4db79d2015-12-22 16:32:23 -0800672 beginLayerOp.localClip,
Chris Craik818c9fb2015-10-23 14:33:42 -0700673 beginLayerOp.paint,
Chris Craik5854b342015-10-26 15:49:56 -0700674 &mLayerReorderers[finishedLayerIndex].offscreenBuffer);
Chris Craik6fe991e52015-10-20 09:39:42 -0700675 BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
676
677 if (bakedOpState) {
678 // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
679 currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
680 } else {
681 // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
Chris Craikb87eadd2016-01-06 09:16:05 -0800682 // TODO: need to prevent any render work from being done
683 // - create layerop earlier for reject purposes?
Chris Craik6fe991e52015-10-20 09:39:42 -0700684 mLayerReorderers[finishedLayerIndex].clear();
685 return;
Chris Craikb565df12015-10-05 13:00:52 -0700686 }
687}
688
Chris Craik5ea17242016-01-11 14:07:59 -0800689void FrameReorderer::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800690 Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
691 boundsTransform.multiply(op.localMatrix);
692
693 Rect dstRect(op.unmappedBounds);
694 boundsTransform.mapRect(dstRect);
695 dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
696
697 // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
Chris Craik7435eb12016-01-07 17:41:40 -0800698 OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
Chris Craikb87eadd2016-01-06 09:16:05 -0800699
700 /**
701 * First, defer an operation to copy out the content from the rendertarget into a layer.
702 */
703 auto copyToOp = new (mAllocator) CopyToLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800704 BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
705 &(currentLayer().viewportClip), dstRect, *copyToOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800706 currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
707
708 /**
709 * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
710 * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
711 */
712 currentLayer().deferLayerClear(dstRect);
713
714 /**
715 * And stash an operation to copy that layer back under the rendertarget until
716 * a balanced EndUnclippedLayerOp is seen
717 */
718 auto copyFromOp = new (mAllocator) CopyFromLayerOp(op, layerHandle);
Chris Craik7435eb12016-01-07 17:41:40 -0800719 bakedState = BakedOpState::directConstruct(mAllocator,
720 &(currentLayer().viewportClip), dstRect, *copyFromOp);
Chris Craikb87eadd2016-01-06 09:16:05 -0800721 currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
722}
723
Chris Craik5ea17242016-01-11 14:07:59 -0800724void FrameReorderer::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
Chris Craikb87eadd2016-01-06 09:16:05 -0800725 LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
726
727 BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
728 currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
729 currentLayer().activeUnclippedSaveLayers.pop_back();
730}
731
Chris Craikb565df12015-10-05 13:00:52 -0700732} // namespace uirenderer
733} // namespace android