Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 1 | /* |
| 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 Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 28 | struct SkRect; |
| 29 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | class BakedOpState; |
| 34 | class BatchBase; |
| 35 | class MergingOpBatch; |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 36 | class OffscreenBuffer; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 37 | class OpBatch; |
| 38 | class Rect; |
| 39 | |
| 40 | typedef int batchid_t; |
| 41 | typedef const void* mergeid_t; |
| 42 | |
| 43 | namespace 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 Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 58 | class OpReorderer : public CanvasStateClient { |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 59 | typedef std::function<void(void*, const RecordedOp&, const BakedOpState&)> BakedOpDispatcher; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 60 | |
| 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 Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 67 | LayerReorderer(uint32_t width, uint32_t height) |
| 68 | : width(width) |
| 69 | , height(height) {} |
| 70 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 71 | // 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 Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 83 | void replayBakedOpsImpl(void* arg, BakedOpDispatcher* receivers) const; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 84 | |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 85 | bool empty() const { |
| 86 | return mBatches.empty(); |
| 87 | } |
| 88 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 89 | void clear() { |
| 90 | mBatches.clear(); |
| 91 | } |
| 92 | |
| 93 | void dump() const; |
| 94 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 95 | OffscreenBuffer* offscreenBuffer = nullptr; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 96 | const BeginLayerOp* beginLayerOp = nullptr; |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 97 | const uint32_t width; |
| 98 | const uint32_t height; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 99 | private: |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 100 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 101 | 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 Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 114 | public: |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 115 | // TODO: not final, just presented this way for simplicity. Layers too? |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 116 | OpReorderer(const SkRect& clip, uint32_t viewportWidth, uint32_t viewportHeight, |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 117 | const std::vector< sp<RenderNode> >& nodes); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 118 | |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 119 | OpReorderer(int viewportWidth, int viewportHeight, const DisplayList& displayList); |
| 120 | |
| 121 | virtual ~OpReorderer() {} |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 122 | |
| 123 | /** |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 124 | * replayBakedOps() is templated based on what class will receive ops being replayed. |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 125 | * |
| 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 Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 131 | * StaticDispatcher::onBitmapOp(Renderer& renderer, const BitmapOp& op, const BakedOpState& state); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 132 | */ |
| 133 | #define BAKED_OP_RECEIVER(Type) \ |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 134 | [](void* internalRenderer, const RecordedOp& op, const BakedOpState& state) { \ |
| 135 | StaticDispatcher::on##Type(*(static_cast<Renderer*>(internalRenderer)), static_cast<const Type&>(op), state); \ |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 136 | }, |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 137 | template <typename StaticDispatcher, typename Renderer> |
| 138 | void replayBakedOps(Renderer& renderer) { |
| 139 | static BakedOpDispatcher receivers[] = { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 140 | MAP_OPS(BAKED_OP_RECEIVER) |
| 141 | }; |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 142 | |
| 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 Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 148 | layer.offscreenBuffer = renderer.startLayer(layer.width, layer.height); |
| 149 | layer.replayBakedOpsImpl((void*)&renderer, receivers); |
| 150 | renderer.endLayer(); |
Chris Craik | 818c9fb | 2015-10-23 14:33:42 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | const LayerReorderer& fbo0 = mLayerReorderers[0]; |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 155 | renderer.startFrame(fbo0.width, fbo0.height); |
| 156 | fbo0.replayBakedOpsImpl((void*)&renderer, receivers); |
| 157 | renderer.endFrame(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 158 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 159 | |
| 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 Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 173 | private: |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 174 | LayerReorderer& currentLayer() { return mLayerReorderers[mLayerStack.back()]; } |
| 175 | |
| 176 | BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) { |
| 177 | return BakedOpState::tryConstruct(mAllocator, *mCanvasState.currentSnapshot(), recordedOp); |
| 178 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 179 | |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 180 | void deferImpl(const DisplayList& displayList); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 181 | |
Chris Craik | 5854b34 | 2015-10-26 15:49:56 -0700 | [diff] [blame^] | 182 | void replayBakedOpsImpl(void* arg, BakedOpDispatcher* receivers); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 183 | |
| 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 Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 194 | // List of every deferred layer's render state. Replayed in reverse order to render a frame. |
| 195 | std::vector<LayerReorderer> mLayerReorderers; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 196 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame] | 197 | /* |
| 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 Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 210 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 211 | CanvasState mCanvasState; |
| 212 | |
| 213 | // contains ResolvedOps and Batches |
| 214 | LinearAllocator mAllocator; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | }; // namespace uirenderer |
| 218 | }; // namespace android |
| 219 | |
| 220 | #endif // ANDROID_HWUI_OP_REORDERER_H |