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; |
| 36 | class OpBatch; |
| 37 | class Rect; |
| 38 | |
| 39 | typedef int batchid_t; |
| 40 | typedef const void* mergeid_t; |
| 41 | |
| 42 | namespace 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 Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 57 | class 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 Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 102 | public: |
| 103 | OpReorderer(); |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 104 | virtual ~OpReorderer() {} |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 105 | |
| 106 | // TODO: not final, just presented this way for simplicity. Layers too? |
Chris Craik | ddf2215 | 2015-10-14 17:42:47 -0700 | [diff] [blame] | 107 | void defer(const SkRect& clip, int viewportWidth, int viewportHeight, |
| 108 | const std::vector< sp<RenderNode> >& nodes); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 109 | |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 110 | void defer(int viewportWidth, int viewportHeight, const DisplayList& displayList); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 113 | * replayBakedOps() is templated based on what class will receive ops being replayed. |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 114 | * |
| 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 Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 124 | StaticReceiver::on##Type(*(static_cast<Arg*>(internalArg)), static_cast<const Type&>(op), state); \ |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 125 | }, |
| 126 | template <typename StaticReceiver, typename Arg> |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 127 | void replayBakedOps(Arg& arg) { |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 128 | static BakedOpReceiver receivers[] = { |
| 129 | MAP_OPS(BAKED_OP_RECEIVER) |
| 130 | }; |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 131 | StaticReceiver::startFrame(arg); |
| 132 | replayBakedOpsImpl((void*)&arg, receivers); |
| 133 | StaticReceiver::endFrame(arg); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 134 | } |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 135 | |
| 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 Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 149 | private: |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 150 | LayerReorderer& currentLayer() { return mLayerReorderers[mLayerStack.back()]; } |
| 151 | |
| 152 | BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) { |
| 153 | return BakedOpState::tryConstruct(mAllocator, *mCanvasState.currentSnapshot(), recordedOp); |
| 154 | } |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 155 | |
Chris Craik | b36af87 | 2015-10-16 14:23:12 -0700 | [diff] [blame] | 156 | void deferImpl(const DisplayList& displayList); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 157 | |
| 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 Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 170 | // List of every deferred layer's render state. Replayed in reverse order to render a frame. |
| 171 | std::vector<LayerReorderer> mLayerReorderers; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 172 | |
Chris Craik | 6fe991e5 | 2015-10-20 09:39:42 -0700 | [diff] [blame^] | 173 | /* |
| 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 Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 186 | |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 187 | CanvasState mCanvasState; |
| 188 | |
| 189 | // contains ResolvedOps and Batches |
| 190 | LinearAllocator mAllocator; |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | }; // namespace uirenderer |
| 194 | }; // namespace android |
| 195 | |
| 196 | #endif // ANDROID_HWUI_OP_REORDERER_H |