blob: 936b6ed9566e40e09c83c2278337b34a7ec19f43 [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;
Chris Craik0b7e8242015-10-28 16:50:44 -070035class LayerUpdateQueue;
Chris Craikb565df12015-10-05 13:00:52 -070036class MergingOpBatch;
Chris Craik5854b342015-10-26 15:49:56 -070037class OffscreenBuffer;
Chris Craikb565df12015-10-05 13:00:52 -070038class OpBatch;
39class Rect;
40
41typedef int batchid_t;
42typedef const void* mergeid_t;
43
44namespace OpBatchType {
45 enum {
46 None = 0, // Don't batch
47 Bitmap,
48 Patch,
49 AlphaVertices,
50 Vertices,
51 AlphaMaskTexture,
52 Text,
53 ColorText,
54
55 Count // must be last
56 };
57}
58
Chris Craik6fe991e52015-10-20 09:39:42 -070059class OpReorderer : public CanvasStateClient {
Chris Craik5854b342015-10-26 15:49:56 -070060 typedef std::function<void(void*, const RecordedOp&, const BakedOpState&)> BakedOpDispatcher;
Chris Craik6fe991e52015-10-20 09:39:42 -070061
62 /**
63 * Stores the deferred render operations and state used to compute ordering
64 * for a single FBO/layer.
65 */
66 class LayerReorderer {
67 public:
Chris Craik0b7e8242015-10-28 16:50:44 -070068 // Create LayerReorderer for Fbo0
Chris Craik818c9fb2015-10-23 14:33:42 -070069 LayerReorderer(uint32_t width, uint32_t height)
Chris Craik0b7e8242015-10-28 16:50:44 -070070 : LayerReorderer(width, height, nullptr, nullptr) {};
71
72 // Create LayerReorderer for an offscreen layer, where beginLayerOp is present for a
73 // saveLayer, renderNode is present for a HW layer.
74 LayerReorderer(uint32_t width, uint32_t height,
75 const BeginLayerOp* beginLayerOp, RenderNode* renderNode);
Chris Craik818c9fb2015-10-23 14:33:42 -070076
Chris Craik6fe991e52015-10-20 09:39:42 -070077 // iterate back toward target to see if anything drawn since should overlap the new op
78 // if no target, merging ops still iterate to find similar batch to insert after
79 void locateInsertIndex(int batchId, const Rect& clippedBounds,
80 BatchBase** targetBatch, size_t* insertBatchIndex) const;
81
82 void deferUnmergeableOp(LinearAllocator& allocator, BakedOpState* op, batchid_t batchId);
83
84 // insertion point of a new batch, will hopefully be immediately after similar batch
85 // (generally, should be similar shader)
86 void deferMergeableOp(LinearAllocator& allocator,
87 BakedOpState* op, batchid_t batchId, mergeid_t mergeId);
88
Chris Craik5854b342015-10-26 15:49:56 -070089 void replayBakedOpsImpl(void* arg, BakedOpDispatcher* receivers) const;
Chris Craik6fe991e52015-10-20 09:39:42 -070090
Chris Craik818c9fb2015-10-23 14:33:42 -070091 bool empty() const {
92 return mBatches.empty();
93 }
94
Chris Craik6fe991e52015-10-20 09:39:42 -070095 void clear() {
96 mBatches.clear();
97 }
98
99 void dump() const;
100
Chris Craik818c9fb2015-10-23 14:33:42 -0700101 const uint32_t width;
102 const uint32_t height;
Chris Craik0b7e8242015-10-28 16:50:44 -0700103 OffscreenBuffer* offscreenBuffer;
104 const BeginLayerOp* beginLayerOp;
105 const RenderNode* renderNode;
Chris Craik6fe991e52015-10-20 09:39:42 -0700106 private:
107 std::vector<BatchBase*> mBatches;
108
109 /**
110 * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen
111 * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not
112 * collide, which avoids the need to resolve mergeid collisions.
113 */
114 std::unordered_map<mergeid_t, MergingOpBatch*> mMergingBatchLookup[OpBatchType::Count];
115
116 // Maps batch ids to the most recent *non-merging* batch of that id
117 OpBatch* mBatchLookup[OpBatchType::Count] = { nullptr };
118
119 };
Chris Craikb565df12015-10-05 13:00:52 -0700120public:
Chris Craik0b7e8242015-10-28 16:50:44 -0700121 OpReorderer(const LayerUpdateQueue& layers, const SkRect& clip,
122 uint32_t viewportWidth, uint32_t viewportHeight,
Chris Craikddf22152015-10-14 17:42:47 -0700123 const std::vector< sp<RenderNode> >& nodes);
Chris Craikb565df12015-10-05 13:00:52 -0700124
Chris Craik818c9fb2015-10-23 14:33:42 -0700125 OpReorderer(int viewportWidth, int viewportHeight, const DisplayList& displayList);
126
127 virtual ~OpReorderer() {}
Chris Craikb565df12015-10-05 13:00:52 -0700128
129 /**
Chris Craik6fe991e52015-10-20 09:39:42 -0700130 * replayBakedOps() is templated based on what class will receive ops being replayed.
Chris Craikb565df12015-10-05 13:00:52 -0700131 *
132 * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use
133 * state->op->opId to lookup a receiver that will be called when the op is replayed.
134 *
135 * For example a BitmapOp would resolve, via the lambda lookup, to calling:
136 *
Chris Craik5854b342015-10-26 15:49:56 -0700137 * StaticDispatcher::onBitmapOp(Renderer& renderer, const BitmapOp& op, const BakedOpState& state);
Chris Craikb565df12015-10-05 13:00:52 -0700138 */
139#define BAKED_OP_RECEIVER(Type) \
Chris Craik5854b342015-10-26 15:49:56 -0700140 [](void* internalRenderer, const RecordedOp& op, const BakedOpState& state) { \
141 StaticDispatcher::on##Type(*(static_cast<Renderer*>(internalRenderer)), static_cast<const Type&>(op), state); \
Chris Craikb565df12015-10-05 13:00:52 -0700142 },
Chris Craik5854b342015-10-26 15:49:56 -0700143 template <typename StaticDispatcher, typename Renderer>
144 void replayBakedOps(Renderer& renderer) {
145 static BakedOpDispatcher receivers[] = {
Chris Craikb565df12015-10-05 13:00:52 -0700146 MAP_OPS(BAKED_OP_RECEIVER)
147 };
Chris Craik818c9fb2015-10-23 14:33:42 -0700148
149 // Relay through layers in reverse order, since layers
150 // later in the list will be drawn by earlier ones
151 for (int i = mLayerReorderers.size() - 1; i >= 1; i--) {
152 LayerReorderer& layer = mLayerReorderers[i];
Chris Craik0b7e8242015-10-28 16:50:44 -0700153 if (layer.renderNode) {
154 // cached HW layer - can't skip layer if empty
155 renderer.startLayer(layer.offscreenBuffer);
156 layer.replayBakedOpsImpl((void*)&renderer, receivers);
157 renderer.endLayer();
158 } else if (!layer.empty()) { // save layer - skip entire layer if empty
159 layer.offscreenBuffer = renderer.createLayer(layer.width, layer.height);
Chris Craik5854b342015-10-26 15:49:56 -0700160 layer.replayBakedOpsImpl((void*)&renderer, receivers);
161 renderer.endLayer();
Chris Craik818c9fb2015-10-23 14:33:42 -0700162 }
163 }
164
165 const LayerReorderer& fbo0 = mLayerReorderers[0];
Chris Craik5854b342015-10-26 15:49:56 -0700166 renderer.startFrame(fbo0.width, fbo0.height);
167 fbo0.replayBakedOpsImpl((void*)&renderer, receivers);
168 renderer.endFrame();
Chris Craikb565df12015-10-05 13:00:52 -0700169 }
Chris Craik6fe991e52015-10-20 09:39:42 -0700170
171 void dump() const {
172 for (auto&& layer : mLayerReorderers) {
173 layer.dump();
174 }
175 }
176
177 ///////////////////////////////////////////////////////////////////
178 /// CanvasStateClient interface
179 ///////////////////////////////////////////////////////////////////
180 virtual void onViewportInitialized() override;
181 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override;
182 virtual GLuint getTargetFbo() const override { return 0; }
183
Chris Craikb565df12015-10-05 13:00:52 -0700184private:
Chris Craik161f54b2015-11-05 11:08:52 -0800185 enum class ChildrenSelectMode {
186 Negative,
187 Positive
188 };
Chris Craik0b7e8242015-10-28 16:50:44 -0700189 void saveForLayer(uint32_t layerWidth, uint32_t layerHeight,
190 const BeginLayerOp* beginLayerOp, RenderNode* renderNode);
191 void restoreForLayer();
192
Chris Craik6fe991e52015-10-20 09:39:42 -0700193 LayerReorderer& currentLayer() { return mLayerReorderers[mLayerStack.back()]; }
194
195 BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) {
196 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.currentSnapshot(), recordedOp);
197 }
Chris Craikb565df12015-10-05 13:00:52 -0700198
Chris Craik0b7e8242015-10-28 16:50:44 -0700199 // should always be surrounded by a save/restore pair
200 void deferNodePropsAndOps(RenderNode& node);
201
Chris Craik161f54b2015-11-05 11:08:52 -0800202 void deferShadow(const RenderNodeOp& casterOp);
203
Chris Craikb36af872015-10-16 14:23:12 -0700204 void deferImpl(const DisplayList& displayList);
Chris Craikb565df12015-10-05 13:00:52 -0700205
Chris Craik161f54b2015-11-05 11:08:52 -0800206 template <typename V>
207 void defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes);
208
209 void deferRenderNodeOp(const RenderNodeOp& op);
210
Chris Craik5854b342015-10-26 15:49:56 -0700211 void replayBakedOpsImpl(void* arg, BakedOpDispatcher* receivers);
Chris Craikb565df12015-10-05 13:00:52 -0700212
213 /**
214 * Declares all OpReorderer::onXXXXOp() methods for every RecordedOp type.
215 *
216 * These private methods are called from within deferImpl to defer each individual op
217 * type differently.
218 */
219#define INTERNAL_OP_HANDLER(Type) \
220 void on##Type(const Type& op);
221 MAP_OPS(INTERNAL_OP_HANDLER)
222
Chris Craik6fe991e52015-10-20 09:39:42 -0700223 // List of every deferred layer's render state. Replayed in reverse order to render a frame.
224 std::vector<LayerReorderer> mLayerReorderers;
Chris Craikb565df12015-10-05 13:00:52 -0700225
Chris Craik6fe991e52015-10-20 09:39:42 -0700226 /*
227 * Stack of indices within mLayerReorderers representing currently active layers. If drawing
228 * layerA within a layerB, will contain, in order:
229 * - 0 (representing FBO 0, always present)
230 * - layerB's index
231 * - layerA's index
232 *
233 * Note that this doesn't vector doesn't always map onto all values of mLayerReorderers. When a
234 * layer is finished deferring, it will still be represented in mLayerReorderers, but it's index
235 * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing
236 * ops added to it.
237 */
238 std::vector<size_t> mLayerStack;
Chris Craikb565df12015-10-05 13:00:52 -0700239
Chris Craikb565df12015-10-05 13:00:52 -0700240 CanvasState mCanvasState;
241
242 // contains ResolvedOps and Batches
243 LinearAllocator mAllocator;
Chris Craikb565df12015-10-05 13:00:52 -0700244};
245
246}; // namespace uirenderer
247}; // namespace android
248
249#endif // ANDROID_HWUI_OP_REORDERER_H