blob: 73dc9af01f714cd2af602170270c92b85ab73a65 [file] [log] [blame]
Chris Craikb565df12015-10-05 13:00:52 -07001/*
2 * Copyright (C) 2015 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#ifndef ANDROID_HWUI_OP_REORDERER_H
18#define ANDROID_HWUI_OP_REORDERER_H
19
20#include "BakedOpState.h"
21#include "CanvasState.h"
22#include "DisplayList.h"
23#include "RecordedOp.h"
24
25#include <vector>
26#include <unordered_map>
27
Chris Craikddf22152015-10-14 17:42:47 -070028struct SkRect;
29
Chris Craikb565df12015-10-05 13:00:52 -070030namespace android {
31namespace uirenderer {
32
33class BakedOpState;
34class BatchBase;
35class MergingOpBatch;
36class OpBatch;
37class Rect;
38
39typedef int batchid_t;
40typedef const void* mergeid_t;
41
42namespace OpBatchType {
43 enum {
44 None = 0, // Don't batch
45 Bitmap,
46 Patch,
47 AlphaVertices,
48 Vertices,
49 AlphaMaskTexture,
50 Text,
51 ColorText,
52
53 Count // must be last
54 };
55}
56
Chris Craik6fe991e52015-10-20 09:39:42 -070057class OpReorderer : public CanvasStateClient {
58 typedef std::function<void(void*, const RecordedOp&, const BakedOpState&)> BakedOpReceiver;
59
60 /**
61 * Stores the deferred render operations and state used to compute ordering
62 * for a single FBO/layer.
63 */
64 class LayerReorderer {
65 public:
66 // iterate back toward target to see if anything drawn since should overlap the new op
67 // if no target, merging ops still iterate to find similar batch to insert after
68 void locateInsertIndex(int batchId, const Rect& clippedBounds,
69 BatchBase** targetBatch, size_t* insertBatchIndex) const;
70
71 void deferUnmergeableOp(LinearAllocator& allocator, BakedOpState* op, batchid_t batchId);
72
73 // insertion point of a new batch, will hopefully be immediately after similar batch
74 // (generally, should be similar shader)
75 void deferMergeableOp(LinearAllocator& allocator,
76 BakedOpState* op, batchid_t batchId, mergeid_t mergeId);
77
78 void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers) const;
79
80 void clear() {
81 mBatches.clear();
82 }
83
84 void dump() const;
85
86 const BeginLayerOp* beginLayerOp = nullptr;
87
88 private:
89 std::vector<BatchBase*> mBatches;
90
91 /**
92 * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
93 * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
94 * collide, which avoids the need to resolve mergeid collisions.
95 */
96 std::unordered_map<mergeid_t, MergingOpBatch*> mMergingBatchLookup[OpBatchType::Count];
97
98 // Maps batch ids to the most recent *non-merging* batch of that id
99 OpBatch* mBatchLookup[OpBatchType::Count] = { nullptr };
100
101 };
Chris Craikb565df12015-10-05 13:00:52 -0700102public:
103 OpReorderer();
Chris Craik6fe991e52015-10-20 09:39:42 -0700104 virtual ~OpReorderer() {}
Chris Craikb565df12015-10-05 13:00:52 -0700105
106 // TODO: not final, just presented this way for simplicity. Layers too?
Chris Craikddf22152015-10-14 17:42:47 -0700107 void defer(const SkRect& clip, int viewportWidth, int viewportHeight,
108 const std::vector< sp<RenderNode> >& nodes);
Chris Craikb565df12015-10-05 13:00:52 -0700109
Chris Craikb36af872015-10-16 14:23:12 -0700110 void defer(int viewportWidth, int viewportHeight, const DisplayList& displayList);
Chris Craikb565df12015-10-05 13:00:52 -0700111
112 /**
Chris Craik6fe991e52015-10-20 09:39:42 -0700113 * replayBakedOps() is templated based on what class will receive ops being replayed.
Chris Craikb565df12015-10-05 13:00:52 -0700114 *
115 * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use
116 * state->op->opId to lookup a receiver that will be called when the op is replayed.
117 *
118 * For example a BitmapOp would resolve, via the lambda lookup, to calling:
119 *
120 * StaticReceiver::onBitmapOp(Arg* arg, const BitmapOp& op, const BakedOpState& state);
121 */
122#define BAKED_OP_RECEIVER(Type) \
123 [](void* internalArg, const RecordedOp& op, const BakedOpState& state) { \
Chris Craik6fe991e52015-10-20 09:39:42 -0700124 StaticReceiver::on##Type(*(static_cast<Arg*>(internalArg)), static_cast<const Type&>(op), state); \
Chris Craikb565df12015-10-05 13:00:52 -0700125 },
126 template <typename StaticReceiver, typename Arg>
Chris Craik6fe991e52015-10-20 09:39:42 -0700127 void replayBakedOps(Arg& arg) {
Chris Craikb565df12015-10-05 13:00:52 -0700128 static BakedOpReceiver receivers[] = {
129 MAP_OPS(BAKED_OP_RECEIVER)
130 };
Chris Craik6fe991e52015-10-20 09:39:42 -0700131 StaticReceiver::startFrame(arg);
132 replayBakedOpsImpl((void*)&arg, receivers);
133 StaticReceiver::endFrame(arg);
Chris Craikb565df12015-10-05 13:00:52 -0700134 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700135
136 void dump() const {
137 for (auto&& layer : mLayerReorderers) {
138 layer.dump();
139 }
140 }
141
142 ///////////////////////////////////////////////////////////////////
143 /// CanvasStateClient interface
144 ///////////////////////////////////////////////////////////////////
145 virtual void onViewportInitialized() override;
146 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override;
147 virtual GLuint getTargetFbo() const override { return 0; }
148
Chris Craikb565df12015-10-05 13:00:52 -0700149private:
Chris Craik6fe991e52015-10-20 09:39:42 -0700150 LayerReorderer& currentLayer() { return mLayerReorderers[mLayerStack.back()]; }
151
152 BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) {
153 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.currentSnapshot(), recordedOp);
154 }
Chris Craikb565df12015-10-05 13:00:52 -0700155
Chris Craikb36af872015-10-16 14:23:12 -0700156 void deferImpl(const DisplayList& displayList);
Chris Craikb565df12015-10-05 13:00:52 -0700157
158 void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers);
159
160 /**
161 * Declares all OpReorderer::onXXXXOp() methods for every RecordedOp type.
162 *
163 * These private methods are called from within deferImpl to defer each individual op
164 * type differently.
165 */
166#define INTERNAL_OP_HANDLER(Type) \
167 void on##Type(const Type& op);
168 MAP_OPS(INTERNAL_OP_HANDLER)
169
Chris Craik6fe991e52015-10-20 09:39:42 -0700170 // List of every deferred layer's render state. Replayed in reverse order to render a frame.
171 std::vector<LayerReorderer> mLayerReorderers;
Chris Craikb565df12015-10-05 13:00:52 -0700172
Chris Craik6fe991e52015-10-20 09:39:42 -0700173 /*
174 * Stack of indices within mLayerReorderers representing currently active layers. If drawing
175 * layerA within a layerB, will contain, in order:
176 * - 0 (representing FBO 0, always present)
177 * - layerB's index
178 * - layerA's index
179 *
180 * Note that this doesn't vector doesn't always map onto all values of mLayerReorderers. When a
181 * layer is finished deferring, it will still be represented in mLayerReorderers, but it's index
182 * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing
183 * ops added to it.
184 */
185 std::vector<size_t> mLayerStack;
Chris Craikb565df12015-10-05 13:00:52 -0700186
Chris Craikb565df12015-10-05 13:00:52 -0700187 CanvasState mCanvasState;
188
189 // contains ResolvedOps and Batches
190 LinearAllocator mAllocator;
Chris Craikb565df12015-10-05 13:00:52 -0700191};
192
193}; // namespace uirenderer
194}; // namespace android
195
196#endif // ANDROID_HWUI_OP_REORDERER_H