blob: 927ecfae3b9f1606c8ae2ca296a326d2ff44c661 [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;
Chris Craik5854b342015-10-26 15:49:56 -070036class OffscreenBuffer;
Chris Craikb565df12015-10-05 13:00:52 -070037class OpBatch;
38class Rect;
39
40typedef int batchid_t;
41typedef const void* mergeid_t;
42
43namespace OpBatchType {
44 enum {
45 None = 0, // Don't batch
46 Bitmap,
47 Patch,
48 AlphaVertices,
49 Vertices,
50 AlphaMaskTexture,
51 Text,
52 ColorText,
53
54 Count // must be last
55 };
56}
57
Chris Craik6fe991e52015-10-20 09:39:42 -070058class OpReorderer : public CanvasStateClient {
Chris Craik5854b342015-10-26 15:49:56 -070059 typedef std::function<void(void*, const RecordedOp&, const BakedOpState&)> BakedOpDispatcher;
Chris Craik6fe991e52015-10-20 09:39:42 -070060
61 /**
62 * Stores the deferred render operations and state used to compute ordering
63 * for a single FBO/layer.
64 */
65 class LayerReorderer {
66 public:
Chris Craik818c9fb2015-10-23 14:33:42 -070067 LayerReorderer(uint32_t width, uint32_t height)
68 : width(width)
69 , height(height) {}
70
Chris Craik6fe991e52015-10-20 09:39:42 -070071 // iterate back toward target to see if anything drawn since should overlap the new op
72 // if no target, merging ops still iterate to find similar batch to insert after
73 void locateInsertIndex(int batchId, const Rect& clippedBounds,
74 BatchBase** targetBatch, size_t* insertBatchIndex) const;
75
76 void deferUnmergeableOp(LinearAllocator& allocator, BakedOpState* op, batchid_t batchId);
77
78 // insertion point of a new batch, will hopefully be immediately after similar batch
79 // (generally, should be similar shader)
80 void deferMergeableOp(LinearAllocator& allocator,
81 BakedOpState* op, batchid_t batchId, mergeid_t mergeId);
82
Chris Craik5854b342015-10-26 15:49:56 -070083 void replayBakedOpsImpl(void* arg, BakedOpDispatcher* receivers) const;
Chris Craik6fe991e52015-10-20 09:39:42 -070084
Chris Craik818c9fb2015-10-23 14:33:42 -070085 bool empty() const {
86 return mBatches.empty();
87 }
88
Chris Craik6fe991e52015-10-20 09:39:42 -070089 void clear() {
90 mBatches.clear();
91 }
92
93 void dump() const;
94
Chris Craik5854b342015-10-26 15:49:56 -070095 OffscreenBuffer* offscreenBuffer = nullptr;
Chris Craik6fe991e52015-10-20 09:39:42 -070096 const BeginLayerOp* beginLayerOp = nullptr;
Chris Craik818c9fb2015-10-23 14:33:42 -070097 const uint32_t width;
98 const uint32_t height;
Chris Craik6fe991e52015-10-20 09:39:42 -070099 private:
Chris Craik818c9fb2015-10-23 14:33:42 -0700100
Chris Craik6fe991e52015-10-20 09:39:42 -0700101 std::vector<BatchBase*> mBatches;
102
103 /**
104 * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
105 * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
106 * collide, which avoids the need to resolve mergeid collisions.
107 */
108 std::unordered_map<mergeid_t, MergingOpBatch*> mMergingBatchLookup[OpBatchType::Count];
109
110 // Maps batch ids to the most recent *non-merging* batch of that id
111 OpBatch* mBatchLookup[OpBatchType::Count] = { nullptr };
112
113 };
Chris Craikb565df12015-10-05 13:00:52 -0700114public:
Chris Craikb565df12015-10-05 13:00:52 -0700115 // TODO: not final, just presented this way for simplicity. Layers too?
Chris Craik818c9fb2015-10-23 14:33:42 -0700116 OpReorderer(const SkRect& clip, uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -0700117 const std::vector< sp<RenderNode> >& nodes);
Chris Craikb565df12015-10-05 13:00:52 -0700118
Chris Craik818c9fb2015-10-23 14:33:42 -0700119 OpReorderer(int viewportWidth, int viewportHeight, const DisplayList& displayList);
120
121 virtual ~OpReorderer() {}
Chris Craikb565df12015-10-05 13:00:52 -0700122
123 /**
Chris Craik6fe991e52015-10-20 09:39:42 -0700124 * replayBakedOps() is templated based on what class will receive ops being replayed.
Chris Craikb565df12015-10-05 13:00:52 -0700125 *
126 * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use
127 * state->op->opId to lookup a receiver that will be called when the op is replayed.
128 *
129 * For example a BitmapOp would resolve, via the lambda lookup, to calling:
130 *
Chris Craik5854b342015-10-26 15:49:56 -0700131 * StaticDispatcher::onBitmapOp(Renderer& renderer, const BitmapOp& op, const BakedOpState& state);
Chris Craikb565df12015-10-05 13:00:52 -0700132 */
133#define BAKED_OP_RECEIVER(Type) \
Chris Craik5854b342015-10-26 15:49:56 -0700134 [](void* internalRenderer, const RecordedOp& op, const BakedOpState& state) { \
135 StaticDispatcher::on##Type(*(static_cast<Renderer*>(internalRenderer)), static_cast<const Type&>(op), state); \
Chris Craikb565df12015-10-05 13:00:52 -0700136 },
Chris Craik5854b342015-10-26 15:49:56 -0700137 template <typename StaticDispatcher, typename Renderer>
138 void replayBakedOps(Renderer& renderer) {
139 static BakedOpDispatcher receivers[] = {
Chris Craikb565df12015-10-05 13:00:52 -0700140 MAP_OPS(BAKED_OP_RECEIVER)
141 };
Chris Craik818c9fb2015-10-23 14:33:42 -0700142
143 // Relay through layers in reverse order, since layers
144 // later in the list will be drawn by earlier ones
145 for (int i = mLayerReorderers.size() - 1; i >= 1; i--) {
146 LayerReorderer& layer = mLayerReorderers[i];
147 if (!layer.empty()) {
Chris Craik5854b342015-10-26 15:49:56 -0700148 layer.offscreenBuffer = renderer.startLayer(layer.width, layer.height);
149 layer.replayBakedOpsImpl((void*)&renderer, receivers);
150 renderer.endLayer();
Chris Craik818c9fb2015-10-23 14:33:42 -0700151 }
152 }
153
154 const LayerReorderer& fbo0 = mLayerReorderers[0];
Chris Craik5854b342015-10-26 15:49:56 -0700155 renderer.startFrame(fbo0.width, fbo0.height);
156 fbo0.replayBakedOpsImpl((void*)&renderer, receivers);
157 renderer.endFrame();
Chris Craikb565df12015-10-05 13:00:52 -0700158 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700159
160 void dump() const {
161 for (auto&& layer : mLayerReorderers) {
162 layer.dump();
163 }
164 }
165
166 ///////////////////////////////////////////////////////////////////
167 /// CanvasStateClient interface
168 ///////////////////////////////////////////////////////////////////
169 virtual void onViewportInitialized() override;
170 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override;
171 virtual GLuint getTargetFbo() const override { return 0; }
172
Chris Craikb565df12015-10-05 13:00:52 -0700173private:
Chris Craik6fe991e52015-10-20 09:39:42 -0700174 LayerReorderer& currentLayer() { return mLayerReorderers[mLayerStack.back()]; }
175
176 BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) {
177 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.currentSnapshot(), recordedOp);
178 }
Chris Craikb565df12015-10-05 13:00:52 -0700179
Chris Craikb36af872015-10-16 14:23:12 -0700180 void deferImpl(const DisplayList& displayList);
Chris Craikb565df12015-10-05 13:00:52 -0700181
Chris Craik5854b342015-10-26 15:49:56 -0700182 void replayBakedOpsImpl(void* arg, BakedOpDispatcher* receivers);
Chris Craikb565df12015-10-05 13:00:52 -0700183
184 /**
185 * Declares all OpReorderer::onXXXXOp() methods for every RecordedOp type.
186 *
187 * These private methods are called from within deferImpl to defer each individual op
188 * type differently.
189 */
190#define INTERNAL_OP_HANDLER(Type) \
191 void on##Type(const Type& op);
192 MAP_OPS(INTERNAL_OP_HANDLER)
193
Chris Craik6fe991e52015-10-20 09:39:42 -0700194 // List of every deferred layer's render state. Replayed in reverse order to render a frame.
195 std::vector<LayerReorderer> mLayerReorderers;
Chris Craikb565df12015-10-05 13:00:52 -0700196
Chris Craik6fe991e52015-10-20 09:39:42 -0700197 /*
198 * Stack of indices within mLayerReorderers representing currently active layers. If drawing
199 * layerA within a layerB, will contain, in order:
200 * - 0 (representing FBO 0, always present)
201 * - layerB's index
202 * - layerA's index
203 *
204 * Note that this doesn't vector doesn't always map onto all values of mLayerReorderers. When a
205 * layer is finished deferring, it will still be represented in mLayerReorderers, but it's index
206 * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing
207 * ops added to it.
208 */
209 std::vector<size_t> mLayerStack;
Chris Craikb565df12015-10-05 13:00:52 -0700210
Chris Craikb565df12015-10-05 13:00:52 -0700211 CanvasState mCanvasState;
212
213 // contains ResolvedOps and Batches
214 LinearAllocator mAllocator;
Chris Craikb565df12015-10-05 13:00:52 -0700215};
216
217}; // namespace uirenderer
218}; // namespace android
219
220#endif // ANDROID_HWUI_OP_REORDERER_H