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#pragma once
18
19#include "ClipArea.h"
20#include "Rect.h"
21#include "utils/Macros.h"
22
23#include <vector>
24#include <unordered_map>
25
26struct SkRect;
27
28namespace android {
29namespace uirenderer {
30
31class BakedOpState;
32struct BeginLayerOp;
33class BatchBase;
34class LinearAllocator;
35struct MergedBakedOpList;
36class MergingOpBatch;
37class OffscreenBuffer;
38class OpBatch;
39class RenderNode;
40
41typedef int batchid_t;
42typedef const void* mergeid_t;
43
44namespace OpBatchType {
45    enum {
46        Bitmap,
47        MergedPatch,
48        AlphaVertices,
49        Vertices,
50        AlphaMaskTexture,
51        Text,
52        ColorText,
53        Shadow,
54        TextureLayer,
55        Functor,
56        CopyToLayer,
57        CopyFromLayer,
58
59        Count // must be last
60    };
61}
62
63typedef void (*BakedOpReceiver)(void*, const BakedOpState&);
64typedef void (*MergedOpReceiver)(void*, const MergedBakedOpList& opList);
65
66/**
67 * Stores the deferred render operations and state used to compute ordering
68 * for a single FBO/layer.
69 */
70class LayerBuilder {
71// Prevent copy/assign because users may stash pointer to offscreenBuffer and viewportClip
72PREVENT_COPY_AND_ASSIGN(LayerBuilder);
73public:
74    // Create LayerBuilder for Fbo0
75    LayerBuilder(uint32_t width, uint32_t height, const Rect& repaintRect)
76            : LayerBuilder(width, height, repaintRect, nullptr, nullptr) {};
77
78    // Create LayerBuilder for an offscreen layer, where beginLayerOp is present for a
79    // saveLayer, renderNode is present for a HW layer.
80    LayerBuilder(uint32_t width, uint32_t height,
81            const Rect& repaintRect, const BeginLayerOp* beginLayerOp, RenderNode* renderNode);
82
83    // iterate back toward target to see if anything drawn since should overlap the new op
84    // if no target, merging ops still iterate to find similar batch to insert after
85    void locateInsertIndex(int batchId, const Rect& clippedBounds,
86            BatchBase** targetBatch, size_t* insertBatchIndex) const;
87
88    void deferUnmergeableOp(LinearAllocator& allocator, BakedOpState* op, batchid_t batchId);
89
90    // insertion point of a new batch, will hopefully be immediately after similar batch
91    // (generally, should be similar shader)
92    void deferMergeableOp(LinearAllocator& allocator,
93            BakedOpState* op, batchid_t batchId, mergeid_t mergeId);
94
95    void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers, MergedOpReceiver*) const;
96
97    void deferLayerClear(const Rect& dstRect);
98
99    bool empty() const {
100        return mBatches.empty();
101    }
102
103    void clear();
104
105    void dump() const;
106
107    const uint32_t width;
108    const uint32_t height;
109    const Rect repaintRect;
110    const ClipRect repaintClip;
111    OffscreenBuffer* offscreenBuffer;
112    const BeginLayerOp* beginLayerOp;
113    const RenderNode* renderNode;
114
115    // list of deferred CopyFromLayer ops, to be deferred upon encountering EndUnclippedLayerOps
116    std::vector<BakedOpState*> activeUnclippedSaveLayers;
117private:
118    void onDeferOp(LinearAllocator& allocator, const BakedOpState* bakedState);
119    void flushLayerClears(LinearAllocator& allocator);
120
121    std::vector<BatchBase*> mBatches;
122
123    /**
124     * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
125     * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
126     * collide, which avoids the need to resolve mergeid collisions.
127     */
128    std::unordered_map<mergeid_t, MergingOpBatch*> mMergingBatchLookup[OpBatchType::Count];
129
130    // Maps batch ids to the most recent *non-merging* batch of that id
131    OpBatch* mBatchLookup[OpBatchType::Count] = { nullptr };
132
133    std::vector<Rect> mClearRects;
134};
135
136}; // namespace uirenderer
137}; // namespace android
138