FrameBuilder.cpp revision 6246d27813f25b85f6e4b5cb1121fe8484bcce2d
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
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
17#include "FrameBuilder.h"
18
19#include "LayerUpdateQueue.h"
20#include "RenderNode.h"
21#include "VectorDrawable.h"
22#include "renderstate/OffscreenBufferPool.h"
23#include "hwui/Canvas.h"
24#include "utils/FatVector.h"
25#include "utils/PaintUtils.h"
26#include "utils/TraceUtils.h"
27
28#include <SkPathOps.h>
29#include <utils/TypeHelpers.h>
30
31namespace android {
32namespace uirenderer {
33
34FrameBuilder::FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip,
35        uint32_t viewportWidth, uint32_t viewportHeight,
36        const std::vector< sp<RenderNode> >& nodes,
37        const LightGeometry& lightGeometry, const Rect &contentDrawBounds, Caches& caches)
38        : mCanvasState(*this)
39        , mCaches(caches)
40        , mLightRadius(lightGeometry.radius)
41        , mDrawFbo0(!nodes.empty()) {
42    ATRACE_NAME("prepare drawing commands");
43
44    mLayerBuilders.reserve(layers.entries().size());
45    mLayerStack.reserve(layers.entries().size());
46
47    // Prepare to defer Fbo0
48    auto fbo0 = mAllocator.create<LayerBuilder>(viewportWidth, viewportHeight, Rect(clip));
49    mLayerBuilders.push_back(fbo0);
50    mLayerStack.push_back(0);
51    mCanvasState.initializeSaveStack(viewportWidth, viewportHeight,
52            clip.fLeft, clip.fTop, clip.fRight, clip.fBottom,
53            lightGeometry.center);
54
55    // Render all layers to be updated, in order. Defer in reverse order, so that they'll be
56    // updated in the order they're passed in (mLayerBuilders are issued to Renderer in reverse)
57    for (int i = layers.entries().size() - 1; i >= 0; i--) {
58        RenderNode* layerNode = layers.entries()[i].renderNode;
59        // only schedule repaint if node still on layer - possible it may have been
60        // removed during a dropped frame, but layers may still remain scheduled so
61        // as not to lose info on what portion is damaged
62        if (CC_LIKELY(layerNode->getLayer() != nullptr)) {
63            const Rect& layerDamage = layers.entries()[i].damage;
64            layerNode->computeOrdering();
65
66            // map current light center into RenderNode's coordinate space
67            Vector3 lightCenter = mCanvasState.currentSnapshot()->getRelativeLightCenter();
68            layerNode->getLayer()->inverseTransformInWindow.mapPoint3d(lightCenter);
69
70            saveForLayer(layerNode->getWidth(), layerNode->getHeight(), 0, 0,
71                    layerDamage, lightCenter, nullptr, layerNode);
72
73            if (layerNode->getDisplayList()) {
74                deferNodeOps(*layerNode);
75            }
76            restoreForLayer();
77        }
78    }
79
80    // It there are multiple render nodes, they are laid out as follows:
81    // #0 - backdrop (content + caption)
82    // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
83    // #2 - additional overlay nodes
84    // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
85    // resizing however it might become partially visible. The following render loop will crop the
86    // backdrop against the content and draw the remaining part of it. It will then draw the content
87    // cropped to the backdrop (since that indicates a shrinking of the window).
88    //
89    // Additional nodes will be drawn on top with no particular clipping semantics.
90
91    // The bounds of the backdrop against which the content should be clipped.
92    Rect backdropBounds = contentDrawBounds;
93    // Usually the contents bounds should be mContentDrawBounds - however - we will
94    // move it towards the fixed edge to give it a more stable appearance (for the moment).
95    // If there is no content bounds we ignore the layering as stated above and start with 2.
96    int layer = (contentDrawBounds.isEmpty() || nodes.size() == 1) ? 2 : 0;
97
98    for (const sp<RenderNode>& node : nodes) {
99        if (node->nothingToDraw()) continue;
100        node->computeOrdering();
101        int count = mCanvasState.save(SaveFlags::MatrixClip);
102
103        if (layer == 0) {
104            const RenderProperties& properties = node->properties();
105            Rect targetBounds(properties.getLeft(), properties.getTop(),
106                              properties.getRight(), properties.getBottom());
107            // Move the content bounds towards the fixed corner of the backdrop.
108            const int x = targetBounds.left;
109            const int y = targetBounds.top;
110            // Remember the intersection of the target bounds and the intersection bounds against
111            // which we have to crop the content.
112            backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
113            backdropBounds.doIntersect(targetBounds);
114        } else if (layer == 1) {
115            // We shift and clip the content to match its final location in the window.
116            const float left = contentDrawBounds.left;
117            const float top = contentDrawBounds.top;
118            const float dx = backdropBounds.left - left;
119            const float dy = backdropBounds.top - top;
120            const float width = backdropBounds.getWidth();
121            const float height = backdropBounds.getHeight();
122            mCanvasState.translate(dx, dy);
123            // It gets cropped against the bounds of the backdrop to stay inside.
124            mCanvasState.clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op);
125        }
126
127        deferNodePropsAndOps(*node);
128        mCanvasState.restoreToCount(count);
129        layer++;
130    }
131}
132
133void FrameBuilder::onViewportInitialized() {}
134
135void FrameBuilder::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {}
136
137void FrameBuilder::deferNodePropsAndOps(RenderNode& node) {
138    const RenderProperties& properties = node.properties();
139    const Outline& outline = properties.getOutline();
140    if (properties.getAlpha() <= 0
141            || (outline.getShouldClip() && outline.isEmpty())
142            || properties.getScaleX() == 0
143            || properties.getScaleY() == 0) {
144        return; // rejected
145    }
146
147    if (properties.getLeft() != 0 || properties.getTop() != 0) {
148        mCanvasState.translate(properties.getLeft(), properties.getTop());
149    }
150    if (properties.getStaticMatrix()) {
151        mCanvasState.concatMatrix(*properties.getStaticMatrix());
152    } else if (properties.getAnimationMatrix()) {
153        mCanvasState.concatMatrix(*properties.getAnimationMatrix());
154    }
155    if (properties.hasTransformMatrix()) {
156        if (properties.isTransformTranslateOnly()) {
157            mCanvasState.translate(properties.getTranslationX(), properties.getTranslationY());
158        } else {
159            mCanvasState.concatMatrix(*properties.getTransformMatrix());
160        }
161    }
162
163    const int width = properties.getWidth();
164    const int height = properties.getHeight();
165
166    Rect saveLayerBounds; // will be set to non-empty if saveLayer needed
167    const bool isLayer = properties.effectiveLayerType() != LayerType::None;
168    int clipFlags = properties.getClippingFlags();
169    if (properties.getAlpha() < 1) {
170        if (isLayer) {
171            clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
172        }
173        if (CC_LIKELY(isLayer || !properties.getHasOverlappingRendering())) {
174            // simply scale rendering content's alpha
175            mCanvasState.scaleAlpha(properties.getAlpha());
176        } else {
177            // schedule saveLayer by initializing saveLayerBounds
178            saveLayerBounds.set(0, 0, width, height);
179            if (clipFlags) {
180                properties.getClippingRectForFlags(clipFlags, &saveLayerBounds);
181                clipFlags = 0; // all clipping done by savelayer
182            }
183        }
184
185        if (CC_UNLIKELY(ATRACE_ENABLED() && properties.promotedToLayer())) {
186            // pretend alpha always causes savelayer to warn about
187            // performance problem affecting old versions
188            ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", node.getName(), width, height);
189        }
190    }
191    if (clipFlags) {
192        Rect clipRect;
193        properties.getClippingRectForFlags(clipFlags, &clipRect);
194        mCanvasState.clipRect(clipRect.left, clipRect.top, clipRect.right, clipRect.bottom,
195                SkRegion::kIntersect_Op);
196    }
197
198    if (properties.getRevealClip().willClip()) {
199        Rect bounds;
200        properties.getRevealClip().getBounds(&bounds);
201        mCanvasState.setClippingRoundRect(mAllocator,
202                bounds, properties.getRevealClip().getRadius());
203    } else if (properties.getOutline().willClip()) {
204        mCanvasState.setClippingOutline(mAllocator, &(properties.getOutline()));
205    }
206
207    bool quickRejected = mCanvasState.currentSnapshot()->getRenderTargetClip().isEmpty()
208            || (properties.getClipToBounds()
209                    && mCanvasState.quickRejectConservative(0, 0, width, height));
210    if (!quickRejected) {
211        // not rejected, so defer render as either Layer, or direct (possibly wrapped in saveLayer)
212        if (node.getLayer()) {
213            // HW layer
214            LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(node);
215            BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
216            if (bakedOpState) {
217                // Node's layer already deferred, schedule it to render into parent layer
218                currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
219            }
220        } else if (CC_UNLIKELY(!saveLayerBounds.isEmpty())) {
221            // draw DisplayList contents within temporary, since persisted layer could not be used.
222            // (temp layers are clipped to viewport, since they don't persist offscreen content)
223            SkPaint saveLayerPaint;
224            saveLayerPaint.setAlpha(properties.getAlpha());
225            deferBeginLayerOp(*mAllocator.create_trivial<BeginLayerOp>(
226                    saveLayerBounds,
227                    Matrix4::identity(),
228                    nullptr, // no record-time clip - need only respect defer-time one
229                    &saveLayerPaint));
230            deferNodeOps(node);
231            deferEndLayerOp(*mAllocator.create_trivial<EndLayerOp>());
232        } else {
233            deferNodeOps(node);
234        }
235    }
236}
237
238typedef key_value_pair_t<float, const RenderNodeOp*> ZRenderNodeOpPair;
239
240template <typename V>
241static void buildZSortedChildList(V* zTranslatedNodes,
242        const DisplayList& displayList, const DisplayList::Chunk& chunk) {
243    if (chunk.beginChildIndex == chunk.endChildIndex) return;
244
245    for (size_t i = chunk.beginChildIndex; i < chunk.endChildIndex; i++) {
246        RenderNodeOp* childOp = displayList.getChildren()[i];
247        RenderNode* child = childOp->renderNode;
248        float childZ = child->properties().getZ();
249
250        if (!MathUtils::isZero(childZ) && chunk.reorderChildren) {
251            zTranslatedNodes->push_back(ZRenderNodeOpPair(childZ, childOp));
252            childOp->skipInOrderDraw = true;
253        } else if (!child->properties().getProjectBackwards()) {
254            // regular, in order drawing DisplayList
255            childOp->skipInOrderDraw = false;
256        }
257    }
258
259    // Z sort any 3d children (stable-ness makes z compare fall back to standard drawing order)
260    std::stable_sort(zTranslatedNodes->begin(), zTranslatedNodes->end());
261}
262
263template <typename V>
264static size_t findNonNegativeIndex(const V& zTranslatedNodes) {
265    for (size_t i = 0; i < zTranslatedNodes.size(); i++) {
266        if (zTranslatedNodes[i].key >= 0.0f) return i;
267    }
268    return zTranslatedNodes.size();
269}
270
271template <typename V>
272void FrameBuilder::defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes) {
273    const int size = zTranslatedNodes.size();
274    if (size == 0
275            || (mode == ChildrenSelectMode::Negative&& zTranslatedNodes[0].key > 0.0f)
276            || (mode == ChildrenSelectMode::Positive && zTranslatedNodes[size - 1].key < 0.0f)) {
277        // no 3d children to draw
278        return;
279    }
280
281    /**
282     * Draw shadows and (potential) casters mostly in order, but allow the shadows of casters
283     * with very similar Z heights to draw together.
284     *
285     * This way, if Views A & B have the same Z height and are both casting shadows, the shadows are
286     * underneath both, and neither's shadow is drawn on top of the other.
287     */
288    const size_t nonNegativeIndex = findNonNegativeIndex(zTranslatedNodes);
289    size_t drawIndex, shadowIndex, endIndex;
290    if (mode == ChildrenSelectMode::Negative) {
291        drawIndex = 0;
292        endIndex = nonNegativeIndex;
293        shadowIndex = endIndex; // draw no shadows
294    } else {
295        drawIndex = nonNegativeIndex;
296        endIndex = size;
297        shadowIndex = drawIndex; // potentially draw shadow for each pos Z child
298    }
299
300    float lastCasterZ = 0.0f;
301    while (shadowIndex < endIndex || drawIndex < endIndex) {
302        if (shadowIndex < endIndex) {
303            const RenderNodeOp* casterNodeOp = zTranslatedNodes[shadowIndex].value;
304            const float casterZ = zTranslatedNodes[shadowIndex].key;
305            // attempt to render the shadow if the caster about to be drawn is its caster,
306            // OR if its caster's Z value is similar to the previous potential caster
307            if (shadowIndex == drawIndex || casterZ - lastCasterZ < 0.1f) {
308                deferShadow(*casterNodeOp);
309
310                lastCasterZ = casterZ; // must do this even if current caster not casting a shadow
311                shadowIndex++;
312                continue;
313            }
314        }
315
316        const RenderNodeOp* childOp = zTranslatedNodes[drawIndex].value;
317        deferRenderNodeOpImpl(*childOp);
318        drawIndex++;
319    }
320}
321
322void FrameBuilder::deferShadow(const RenderNodeOp& casterNodeOp) {
323    auto& node = *casterNodeOp.renderNode;
324    auto& properties = node.properties();
325
326    if (properties.getAlpha() <= 0.0f
327            || properties.getOutline().getAlpha() <= 0.0f
328            || !properties.getOutline().getPath()
329            || properties.getScaleX() == 0
330            || properties.getScaleY() == 0) {
331        // no shadow to draw
332        return;
333    }
334
335    const SkPath* casterOutlinePath = properties.getOutline().getPath();
336    const SkPath* revealClipPath = properties.getRevealClip().getPath();
337    if (revealClipPath && revealClipPath->isEmpty()) return;
338
339    float casterAlpha = properties.getAlpha() * properties.getOutline().getAlpha();
340
341    // holds temporary SkPath to store the result of intersections
342    SkPath* frameAllocatedPath = nullptr;
343    const SkPath* casterPath = casterOutlinePath;
344
345    // intersect the shadow-casting path with the reveal, if present
346    if (revealClipPath) {
347        frameAllocatedPath = createFrameAllocatedPath();
348
349        Op(*casterPath, *revealClipPath, kIntersect_SkPathOp, frameAllocatedPath);
350        casterPath = frameAllocatedPath;
351    }
352
353    // intersect the shadow-casting path with the clipBounds, if present
354    if (properties.getClippingFlags() & CLIP_TO_CLIP_BOUNDS) {
355        if (!frameAllocatedPath) {
356            frameAllocatedPath = createFrameAllocatedPath();
357        }
358        Rect clipBounds;
359        properties.getClippingRectForFlags(CLIP_TO_CLIP_BOUNDS, &clipBounds);
360        SkPath clipBoundsPath;
361        clipBoundsPath.addRect(clipBounds.left, clipBounds.top,
362                clipBounds.right, clipBounds.bottom);
363
364        Op(*casterPath, clipBoundsPath, kIntersect_SkPathOp, frameAllocatedPath);
365        casterPath = frameAllocatedPath;
366    }
367
368    if (CC_LIKELY(!mCanvasState.getRenderTargetClipBounds().isEmpty())) {
369        Matrix4 shadowMatrixXY(casterNodeOp.localMatrix);
370        Matrix4 shadowMatrixZ(casterNodeOp.localMatrix);
371        node.applyViewPropertyTransforms(shadowMatrixXY, false);
372        node.applyViewPropertyTransforms(shadowMatrixZ, true);
373
374        sp<TessellationCache::ShadowTask> task = mCaches.tessellationCache.getShadowTask(
375                mCanvasState.currentTransform(),
376                mCanvasState.getLocalClipBounds(),
377                casterAlpha >= 1.0f,
378                casterPath,
379                &shadowMatrixXY, &shadowMatrixZ,
380                mCanvasState.currentSnapshot()->getRelativeLightCenter(),
381                mLightRadius);
382        ShadowOp* shadowOp = mAllocator.create<ShadowOp>(task, casterAlpha);
383        BakedOpState* bakedOpState = BakedOpState::tryShadowOpConstruct(
384                mAllocator, *mCanvasState.writableSnapshot(), shadowOp);
385        if (CC_LIKELY(bakedOpState)) {
386            currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Shadow);
387        }
388    }
389}
390
391void FrameBuilder::deferProjectedChildren(const RenderNode& renderNode) {
392    int count = mCanvasState.save(SaveFlags::MatrixClip);
393    const SkPath* projectionReceiverOutline = renderNode.properties().getOutline().getPath();
394
395    SkPath transformedMaskPath; // on stack, since BakedOpState makes a deep copy
396    if (projectionReceiverOutline) {
397        // transform the mask for this projector into render target space
398        // TODO: consider combining both transforms by stashing transform instead of applying
399        SkMatrix skCurrentTransform;
400        mCanvasState.currentTransform()->copyTo(skCurrentTransform);
401        projectionReceiverOutline->transform(
402                skCurrentTransform,
403                &transformedMaskPath);
404        mCanvasState.setProjectionPathMask(mAllocator, &transformedMaskPath);
405    }
406
407    for (size_t i = 0; i < renderNode.mProjectedNodes.size(); i++) {
408        RenderNodeOp* childOp = renderNode.mProjectedNodes[i];
409        RenderNode& childNode = *childOp->renderNode;
410
411        // Draw child if it has content, but ignore state in childOp - matrix already applied to
412        // transformFromCompositingAncestor, and record-time clip is ignored when projecting
413        if (!childNode.nothingToDraw()) {
414            int restoreTo = mCanvasState.save(SaveFlags::MatrixClip);
415
416            // Apply transform between ancestor and projected descendant
417            mCanvasState.concatMatrix(childOp->transformFromCompositingAncestor);
418
419            deferNodePropsAndOps(childNode);
420
421            mCanvasState.restoreToCount(restoreTo);
422        }
423    }
424    mCanvasState.restoreToCount(count);
425}
426
427/**
428 * Used to define a list of lambdas referencing private FrameBuilder::onXX::defer() methods.
429 *
430 * This allows opIds embedded in the RecordedOps to be used for dispatching to these lambdas.
431 * E.g. a BitmapOp op then would be dispatched to FrameBuilder::onBitmapOp(const BitmapOp&)
432 */
433#define OP_RECEIVER(Type) \
434        [](FrameBuilder& frameBuilder, const RecordedOp& op) { frameBuilder.defer##Type(static_cast<const Type&>(op)); },
435void FrameBuilder::deferNodeOps(const RenderNode& renderNode) {
436    typedef void (*OpDispatcher) (FrameBuilder& frameBuilder, const RecordedOp& op);
437    static OpDispatcher receivers[] = BUILD_DEFERRABLE_OP_LUT(OP_RECEIVER);
438
439    // can't be null, since DL=null node rejection happens before deferNodePropsAndOps
440    const DisplayList& displayList = *(renderNode.getDisplayList());
441    for (const DisplayList::Chunk& chunk : displayList.getChunks()) {
442        FatVector<ZRenderNodeOpPair, 16> zTranslatedNodes;
443        buildZSortedChildList(&zTranslatedNodes, displayList, chunk);
444
445        defer3dChildren(ChildrenSelectMode::Negative, zTranslatedNodes);
446        for (size_t opIndex = chunk.beginOpIndex; opIndex < chunk.endOpIndex; opIndex++) {
447            const RecordedOp* op = displayList.getOps()[opIndex];
448            receivers[op->opId](*this, *op);
449
450            if (CC_UNLIKELY(!renderNode.mProjectedNodes.empty()
451                    && displayList.projectionReceiveIndex >= 0
452                    && static_cast<int>(opIndex) == displayList.projectionReceiveIndex)) {
453                deferProjectedChildren(renderNode);
454            }
455        }
456        defer3dChildren(ChildrenSelectMode::Positive, zTranslatedNodes);
457    }
458}
459
460void FrameBuilder::deferRenderNodeOpImpl(const RenderNodeOp& op) {
461    if (op.renderNode->nothingToDraw()) return;
462    int count = mCanvasState.save(SaveFlags::MatrixClip);
463
464    // apply state from RecordedOp (clip first, since op's clip is transformed by current matrix)
465    mCanvasState.writableSnapshot()->mutateClipArea().applyClip(op.localClip,
466            *mCanvasState.currentSnapshot()->transform);
467    mCanvasState.concatMatrix(op.localMatrix);
468
469    // then apply state from node properties, and defer ops
470    deferNodePropsAndOps(*op.renderNode);
471
472    mCanvasState.restoreToCount(count);
473}
474
475void FrameBuilder::deferRenderNodeOp(const RenderNodeOp& op) {
476    if (!op.skipInOrderDraw) {
477        deferRenderNodeOpImpl(op);
478    }
479}
480
481/**
482 * Defers an unmergeable, strokeable op, accounting correctly
483 * for paint's style on the bounds being computed.
484 */
485BakedOpState* FrameBuilder::deferStrokeableOp(const RecordedOp& op, batchid_t batchId,
486        BakedOpState::StrokeBehavior strokeBehavior) {
487    // Note: here we account for stroke when baking the op
488    BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
489            mAllocator, *mCanvasState.writableSnapshot(), op, strokeBehavior);
490    if (!bakedState) return nullptr; // quick rejected
491
492    if (op.opId == RecordedOpId::RectOp && op.paint->getStyle() != SkPaint::kStroke_Style) {
493        bakedState->setupOpacity(op.paint);
494    }
495
496    currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
497    return bakedState;
498}
499
500/**
501 * Returns batch id for tessellatable shapes, based on paint. Checks to see if path effect/AA will
502 * be used, since they trigger significantly different rendering paths.
503 *
504 * Note: not used for lines/points, since they don't currently support path effects.
505 */
506static batchid_t tessBatchId(const RecordedOp& op) {
507    const SkPaint& paint = *(op.paint);
508    return paint.getPathEffect()
509            ? OpBatchType::AlphaMaskTexture
510            : (paint.isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices);
511}
512
513void FrameBuilder::deferArcOp(const ArcOp& op) {
514    deferStrokeableOp(op, tessBatchId(op));
515}
516
517static bool hasMergeableClip(const BakedOpState& state) {
518    return state.computedState.clipState
519            || state.computedState.clipState->mode == ClipMode::Rectangle;
520}
521
522void FrameBuilder::deferBitmapOp(const BitmapOp& op) {
523    BakedOpState* bakedState = tryBakeOpState(op);
524    if (!bakedState) return; // quick rejected
525    bakedState->setupOpacity(op.paint);
526
527    // Don't merge non-simply transformed or neg scale ops, SET_TEXTURE doesn't handle rotation
528    // Don't merge A8 bitmaps - the paint's color isn't compared by mergeId, or in
529    // MergingDrawBatch::canMergeWith()
530    if (bakedState->computedState.transform.isSimple()
531            && bakedState->computedState.transform.positiveScale()
532            && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
533            && op.bitmap->colorType() != kAlpha_8_SkColorType
534            && hasMergeableClip(*bakedState)) {
535        mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
536        // TODO: AssetAtlas in mergeId
537        currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::Bitmap, mergeId);
538    } else {
539        currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
540    }
541}
542
543void FrameBuilder::deferBitmapMeshOp(const BitmapMeshOp& op) {
544    BakedOpState* bakedState = tryBakeOpState(op);
545    if (!bakedState) return; // quick rejected
546    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
547}
548
549void FrameBuilder::deferBitmapRectOp(const BitmapRectOp& op) {
550    BakedOpState* bakedState = tryBakeOpState(op);
551    if (!bakedState) return; // quick rejected
552    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
553}
554
555void FrameBuilder::deferVectorDrawableOp(const VectorDrawableOp& op) {
556    const SkBitmap& bitmap = op.vectorDrawable->getBitmapUpdateIfDirty();
557    SkPaint* paint = op.vectorDrawable->getPaint();
558    const BitmapRectOp* resolvedOp = mAllocator.create_trivial<BitmapRectOp>(op.unmappedBounds,
559            op.localMatrix,
560            op.localClip,
561            paint,
562            &bitmap,
563            Rect(bitmap.width(), bitmap.height()));
564    deferBitmapRectOp(*resolvedOp);
565}
566
567void FrameBuilder::deferCirclePropsOp(const CirclePropsOp& op) {
568    // allocate a temporary oval op (with mAllocator, so it persists until render), so the
569    // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
570    float x = *(op.x);
571    float y = *(op.y);
572    float radius = *(op.radius);
573    Rect unmappedBounds(x - radius, y - radius, x + radius, y + radius);
574    const OvalOp* resolvedOp = mAllocator.create_trivial<OvalOp>(
575            unmappedBounds,
576            op.localMatrix,
577            op.localClip,
578            op.paint);
579    deferOvalOp(*resolvedOp);
580}
581
582void FrameBuilder::deferColorOp(const ColorOp& op) {
583    BakedOpState* bakedState = tryBakeUnboundedOpState(op);
584    if (!bakedState) return; // quick rejected
585    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
586}
587
588void FrameBuilder::deferFunctorOp(const FunctorOp& op) {
589    BakedOpState* bakedState = tryBakeUnboundedOpState(op);
590    if (!bakedState) return; // quick rejected
591    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Functor);
592}
593
594void FrameBuilder::deferLinesOp(const LinesOp& op) {
595    batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
596    deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
597}
598
599void FrameBuilder::deferOvalOp(const OvalOp& op) {
600    deferStrokeableOp(op, tessBatchId(op));
601}
602
603void FrameBuilder::deferPatchOp(const PatchOp& op) {
604    BakedOpState* bakedState = tryBakeOpState(op);
605    if (!bakedState) return; // quick rejected
606
607    if (bakedState->computedState.transform.isPureTranslate()
608            && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
609            && hasMergeableClip(*bakedState)) {
610        mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.bitmap->getGenerationID());
611        // TODO: AssetAtlas in mergeId
612
613        // Only use the MergedPatch batchId when merged, so Bitmap+Patch don't try to merge together
614        currentLayer().deferMergeableOp(mAllocator, bakedState, OpBatchType::MergedPatch, mergeId);
615    } else {
616        // Use Bitmap batchId since Bitmap+Patch use same shader
617        currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Bitmap);
618    }
619}
620
621void FrameBuilder::deferPathOp(const PathOp& op) {
622    auto state = deferStrokeableOp(op, OpBatchType::AlphaMaskTexture);
623    if (CC_LIKELY(state)) {
624        mCaches.pathCache.precache(op.path, op.paint);
625    }
626}
627
628void FrameBuilder::deferPointsOp(const PointsOp& op) {
629    batchid_t batch = op.paint->isAntiAlias() ? OpBatchType::AlphaVertices : OpBatchType::Vertices;
630    deferStrokeableOp(op, batch, BakedOpState::StrokeBehavior::Forced);
631}
632
633void FrameBuilder::deferRectOp(const RectOp& op) {
634    deferStrokeableOp(op, tessBatchId(op));
635}
636
637void FrameBuilder::deferRoundRectOp(const RoundRectOp& op) {
638    auto state = deferStrokeableOp(op, tessBatchId(op));
639    if (CC_LIKELY(state && !op.paint->getPathEffect())) {
640        // TODO: consider storing tessellation task in BakedOpState
641        mCaches.tessellationCache.precacheRoundRect(state->computedState.transform, *(op.paint),
642                op.unmappedBounds.getWidth(), op.unmappedBounds.getHeight(), op.rx, op.ry);
643    }
644}
645
646void FrameBuilder::deferRoundRectPropsOp(const RoundRectPropsOp& op) {
647    // allocate a temporary round rect op (with mAllocator, so it persists until render), so the
648    // renderer doesn't have to handle the RoundRectPropsOp type, and so state baking is simple.
649    const RoundRectOp* resolvedOp = mAllocator.create_trivial<RoundRectOp>(
650            Rect(*(op.left), *(op.top), *(op.right), *(op.bottom)),
651            op.localMatrix,
652            op.localClip,
653            op.paint, *op.rx, *op.ry);
654    deferRoundRectOp(*resolvedOp);
655}
656
657void FrameBuilder::deferSimpleRectsOp(const SimpleRectsOp& op) {
658    BakedOpState* bakedState = tryBakeOpState(op);
659    if (!bakedState) return; // quick rejected
660    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::Vertices);
661}
662
663static batchid_t textBatchId(const SkPaint& paint) {
664    // TODO: better handling of shader (since we won't care about color then)
665    return paint.getColor() == SK_ColorBLACK ? OpBatchType::Text : OpBatchType::ColorText;
666}
667
668void FrameBuilder::deferTextOp(const TextOp& op) {
669    BakedOpState* bakedState = BakedOpState::tryStrokeableOpConstruct(
670            mAllocator, *mCanvasState.writableSnapshot(), op,
671            BakedOpState::StrokeBehavior::StyleDefined);
672    if (!bakedState) return; // quick rejected
673
674    batchid_t batchId = textBatchId(*(op.paint));
675    if (bakedState->computedState.transform.isPureTranslate()
676            && PaintUtils::getXfermodeDirect(op.paint) == SkXfermode::kSrcOver_Mode
677            && hasMergeableClip(*bakedState)) {
678        mergeid_t mergeId = reinterpret_cast<mergeid_t>(op.paint->getColor());
679        currentLayer().deferMergeableOp(mAllocator, bakedState, batchId, mergeId);
680    } else {
681        currentLayer().deferUnmergeableOp(mAllocator, bakedState, batchId);
682    }
683
684    FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer();
685    auto& totalTransform = bakedState->computedState.transform;
686    if (totalTransform.isPureTranslate() || totalTransform.isPerspective()) {
687        fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
688    } else {
689        // Partial transform case, see BakedOpDispatcher::renderTextOp
690        float sx, sy;
691        totalTransform.decomposeScale(sx, sy);
692        fontRenderer.precache(op.paint, op.glyphs, op.glyphCount, SkMatrix::MakeScale(
693                roundf(std::max(1.0f, sx)),
694                roundf(std::max(1.0f, sy))));
695    }
696}
697
698void FrameBuilder::deferTextOnPathOp(const TextOnPathOp& op) {
699    BakedOpState* bakedState = tryBakeUnboundedOpState(op);
700    if (!bakedState) return; // quick rejected
701    currentLayer().deferUnmergeableOp(mAllocator, bakedState, textBatchId(*(op.paint)));
702
703    mCaches.fontRenderer.getFontRenderer().precache(
704            op.paint, op.glyphs, op.glyphCount, SkMatrix::I());
705}
706
707void FrameBuilder::deferTextureLayerOp(const TextureLayerOp& op) {
708    if (CC_UNLIKELY(!op.layer->isRenderable())) return;
709
710    const TextureLayerOp* textureLayerOp = &op;
711    // Now safe to access transform (which was potentially unready at record time)
712    if (!op.layer->getTransform().isIdentity()) {
713        // non-identity transform present, so 'inject it' into op by copying + replacing matrix
714        Matrix4 combinedMatrix(op.localMatrix);
715        combinedMatrix.multiply(op.layer->getTransform());
716        textureLayerOp = mAllocator.create<TextureLayerOp>(op, combinedMatrix);
717    }
718    BakedOpState* bakedState = tryBakeOpState(*textureLayerOp);
719
720    if (!bakedState) return; // quick rejected
721    currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::TextureLayer);
722}
723
724void FrameBuilder::saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
725        float contentTranslateX, float contentTranslateY,
726        const Rect& repaintRect,
727        const Vector3& lightCenter,
728        const BeginLayerOp* beginLayerOp, RenderNode* renderNode) {
729    mCanvasState.save(SaveFlags::MatrixClip);
730    mCanvasState.writableSnapshot()->initializeViewport(layerWidth, layerHeight);
731    mCanvasState.writableSnapshot()->roundRectClipState = nullptr;
732    mCanvasState.writableSnapshot()->setRelativeLightCenter(lightCenter);
733    mCanvasState.writableSnapshot()->transform->loadTranslate(
734            contentTranslateX, contentTranslateY, 0);
735    mCanvasState.writableSnapshot()->setClip(
736            repaintRect.left, repaintRect.top, repaintRect.right, repaintRect.bottom);
737
738    // create a new layer repaint, and push its index on the stack
739    mLayerStack.push_back(mLayerBuilders.size());
740    auto newFbo = mAllocator.create<LayerBuilder>(layerWidth, layerHeight,
741            repaintRect, beginLayerOp, renderNode);
742    mLayerBuilders.push_back(newFbo);
743}
744
745void FrameBuilder::restoreForLayer() {
746    // restore canvas, and pop finished layer off of the stack
747    mCanvasState.restore();
748    mLayerStack.pop_back();
749}
750
751// TODO: defer time rejection (when bounds become empty) + tests
752// Option - just skip layers with no bounds at playback + defer?
753void FrameBuilder::deferBeginLayerOp(const BeginLayerOp& op) {
754    uint32_t layerWidth = (uint32_t) op.unmappedBounds.getWidth();
755    uint32_t layerHeight = (uint32_t) op.unmappedBounds.getHeight();
756
757    auto previous = mCanvasState.currentSnapshot();
758    Vector3 lightCenter = previous->getRelativeLightCenter();
759
760    // Combine all transforms used to present saveLayer content:
761    // parent content transform * canvas transform * bounds offset
762    Matrix4 contentTransform(*(previous->transform));
763    contentTransform.multiply(op.localMatrix);
764    contentTransform.translate(op.unmappedBounds.left, op.unmappedBounds.top);
765
766    Matrix4 inverseContentTransform;
767    inverseContentTransform.loadInverse(contentTransform);
768
769    // map the light center into layer-relative space
770    inverseContentTransform.mapPoint3d(lightCenter);
771
772    // Clip bounds of temporary layer to parent's clip rect, so:
773    Rect saveLayerBounds(layerWidth, layerHeight);
774    //     1) transform Rect(width, height) into parent's space
775    //        note: left/top offsets put in contentTransform above
776    contentTransform.mapRect(saveLayerBounds);
777    //     2) intersect with parent's clip
778    saveLayerBounds.doIntersect(previous->getRenderTargetClip());
779    //     3) and transform back
780    inverseContentTransform.mapRect(saveLayerBounds);
781    saveLayerBounds.doIntersect(Rect(layerWidth, layerHeight));
782    saveLayerBounds.roundOut();
783
784    // if bounds are reduced, will clip the layer's area by reducing required bounds...
785    layerWidth = saveLayerBounds.getWidth();
786    layerHeight = saveLayerBounds.getHeight();
787    // ...and shifting drawing content to account for left/top side clipping
788    float contentTranslateX = -saveLayerBounds.left;
789    float contentTranslateY = -saveLayerBounds.top;
790
791    saveForLayer(layerWidth, layerHeight,
792            contentTranslateX, contentTranslateY,
793            Rect(layerWidth, layerHeight),
794            lightCenter,
795            &op, nullptr);
796}
797
798void FrameBuilder::deferEndLayerOp(const EndLayerOp& /* ignored */) {
799    const BeginLayerOp& beginLayerOp = *currentLayer().beginLayerOp;
800    int finishedLayerIndex = mLayerStack.back();
801
802    restoreForLayer();
803
804    // record the draw operation into the previous layer's list of draw commands
805    // uses state from the associated beginLayerOp, since it has all the state needed for drawing
806    LayerOp* drawLayerOp = mAllocator.create_trivial<LayerOp>(
807            beginLayerOp.unmappedBounds,
808            beginLayerOp.localMatrix,
809            beginLayerOp.localClip,
810            beginLayerOp.paint,
811            &(mLayerBuilders[finishedLayerIndex]->offscreenBuffer));
812    BakedOpState* bakedOpState = tryBakeOpState(*drawLayerOp);
813
814    if (bakedOpState) {
815        // Layer will be drawn into parent layer (which is now current, since we popped mLayerStack)
816        currentLayer().deferUnmergeableOp(mAllocator, bakedOpState, OpBatchType::Bitmap);
817    } else {
818        // Layer won't be drawn - delete its drawing batches to prevent it from doing any work
819        // TODO: need to prevent any render work from being done
820        // - create layerop earlier for reject purposes?
821        mLayerBuilders[finishedLayerIndex]->clear();
822        return;
823    }
824}
825
826void FrameBuilder::deferBeginUnclippedLayerOp(const BeginUnclippedLayerOp& op) {
827    Matrix4 boundsTransform(*(mCanvasState.currentSnapshot()->transform));
828    boundsTransform.multiply(op.localMatrix);
829
830    Rect dstRect(op.unmappedBounds);
831    boundsTransform.mapRect(dstRect);
832    dstRect.doIntersect(mCanvasState.currentSnapshot()->getRenderTargetClip());
833
834    if (dstRect.isEmpty()) {
835        // Unclipped layer rejected - push a null op, so next EndUnclippedLayerOp is ignored
836        currentLayer().activeUnclippedSaveLayers.push_back(nullptr);
837    } else {
838        // Allocate a holding position for the layer object (copyTo will produce, copyFrom will consume)
839        OffscreenBuffer** layerHandle = mAllocator.create<OffscreenBuffer*>(nullptr);
840
841        /**
842         * First, defer an operation to copy out the content from the rendertarget into a layer.
843         */
844        auto copyToOp = mAllocator.create_trivial<CopyToLayerOp>(op, layerHandle);
845        BakedOpState* bakedState = BakedOpState::directConstruct(mAllocator,
846                &(currentLayer().repaintClip), dstRect, *copyToOp);
847        currentLayer().deferUnmergeableOp(mAllocator, bakedState, OpBatchType::CopyToLayer);
848
849        /**
850         * Defer a clear rect, so that clears from multiple unclipped layers can be drawn
851         * both 1) simultaneously, and 2) as long after the copyToLayer executes as possible
852         */
853        currentLayer().deferLayerClear(dstRect);
854
855        /**
856         * And stash an operation to copy that layer back under the rendertarget until
857         * a balanced EndUnclippedLayerOp is seen
858         */
859        auto copyFromOp = mAllocator.create_trivial<CopyFromLayerOp>(op, layerHandle);
860        bakedState = BakedOpState::directConstruct(mAllocator,
861                &(currentLayer().repaintClip), dstRect, *copyFromOp);
862        currentLayer().activeUnclippedSaveLayers.push_back(bakedState);
863    }
864}
865
866void FrameBuilder::deferEndUnclippedLayerOp(const EndUnclippedLayerOp& /* ignored */) {
867    LOG_ALWAYS_FATAL_IF(currentLayer().activeUnclippedSaveLayers.empty(), "no layer to end!");
868
869    BakedOpState* copyFromLayerOp = currentLayer().activeUnclippedSaveLayers.back();
870    currentLayer().activeUnclippedSaveLayers.pop_back();
871    if (copyFromLayerOp) {
872        currentLayer().deferUnmergeableOp(mAllocator, copyFromLayerOp, OpBatchType::CopyFromLayer);
873    }
874}
875
876void FrameBuilder::finishDefer() {
877    mCaches.fontRenderer.endPrecaching();
878}
879
880} // namespace uirenderer
881} // namespace android
882