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 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | class BakedOpState; |
| 32 | class BatchBase; |
| 33 | class MergingOpBatch; |
| 34 | class OpBatch; |
| 35 | class Rect; |
| 36 | |
| 37 | typedef int batchid_t; |
| 38 | typedef const void* mergeid_t; |
| 39 | |
| 40 | namespace OpBatchType { |
| 41 | enum { |
| 42 | None = 0, // Don't batch |
| 43 | Bitmap, |
| 44 | Patch, |
| 45 | AlphaVertices, |
| 46 | Vertices, |
| 47 | AlphaMaskTexture, |
| 48 | Text, |
| 49 | ColorText, |
| 50 | |
| 51 | Count // must be last |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | class OpReorderer { |
| 56 | public: |
| 57 | OpReorderer(); |
| 58 | |
| 59 | // TODO: not final, just presented this way for simplicity. Layers too? |
| 60 | void defer(int viewportWidth, int viewportHeight, const std::vector< sp<RenderNode> >& nodes); |
| 61 | |
| 62 | void defer(int viewportWidth, int viewportHeight, |
| 63 | const std::vector<DisplayListData::Chunk>& chunks, const std::vector<RecordedOp*>& ops); |
| 64 | typedef std::function<void(void*, const RecordedOp&, const BakedOpState&)> BakedOpReceiver; |
| 65 | |
| 66 | /** |
| 67 | * replayBakedOps() is templated based on what class will recieve ops being replayed. |
| 68 | * |
| 69 | * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use |
| 70 | * state->op->opId to lookup a receiver that will be called when the op is replayed. |
| 71 | * |
| 72 | * For example a BitmapOp would resolve, via the lambda lookup, to calling: |
| 73 | * |
| 74 | * StaticReceiver::onBitmapOp(Arg* arg, const BitmapOp& op, const BakedOpState& state); |
| 75 | */ |
| 76 | #define BAKED_OP_RECEIVER(Type) \ |
| 77 | [](void* internalArg, const RecordedOp& op, const BakedOpState& state) { \ |
| 78 | StaticReceiver::on##Type(static_cast<Arg*>(internalArg), static_cast<const Type&>(op), state); \ |
| 79 | }, |
| 80 | template <typename StaticReceiver, typename Arg> |
| 81 | void replayBakedOps(Arg* arg) { |
| 82 | static BakedOpReceiver receivers[] = { |
| 83 | MAP_OPS(BAKED_OP_RECEIVER) |
| 84 | }; |
| 85 | StaticReceiver::startFrame(*arg); |
| 86 | replayBakedOpsImpl((void*)arg, receivers); |
| 87 | StaticReceiver::endFrame(*arg); |
| 88 | } |
| 89 | private: |
| 90 | BakedOpState* bakeOpState(const RecordedOp& recordedOp); |
| 91 | |
| 92 | void deferImpl(const std::vector<DisplayListData::Chunk>& chunks, |
| 93 | const std::vector<RecordedOp*>& ops); |
| 94 | |
| 95 | void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers); |
| 96 | |
| 97 | /** |
| 98 | * Declares all OpReorderer::onXXXXOp() methods for every RecordedOp type. |
| 99 | * |
| 100 | * These private methods are called from within deferImpl to defer each individual op |
| 101 | * type differently. |
| 102 | */ |
| 103 | #define INTERNAL_OP_HANDLER(Type) \ |
| 104 | void on##Type(const Type& op); |
| 105 | MAP_OPS(INTERNAL_OP_HANDLER) |
| 106 | |
| 107 | // iterate back toward target to see if anything drawn since should overlap the new op |
| 108 | // if no target, merging ops still iterate to find similar batch to insert after |
| 109 | void locateInsertIndex(int batchId, const Rect& clippedBounds, |
| 110 | BatchBase** targetBatch, size_t* insertBatchIndex) const; |
| 111 | |
| 112 | void deferUnmergeableOp(BakedOpState* op, batchid_t batchId); |
| 113 | |
| 114 | // insertion point of a new batch, will hopefully be immediately after similar batch |
| 115 | // (generally, should be similar shader) |
| 116 | void deferMergeableOp(BakedOpState* op, batchid_t batchId, mergeid_t mergeId); |
| 117 | |
| 118 | void dump(); |
| 119 | |
| 120 | std::vector<BatchBase*> mBatches; |
| 121 | |
| 122 | /** |
| 123 | * Maps the mergeid_t returned by an op's getMergeId() to the most recently seen |
| 124 | * MergingDrawBatch of that id. These ids are unique per draw type and guaranteed to not |
| 125 | * collide, which avoids the need to resolve mergeid collisions. |
| 126 | */ |
| 127 | std::unordered_map<mergeid_t, MergingOpBatch*> mMergingBatches[OpBatchType::Count]; |
| 128 | |
| 129 | // Maps batch ids to the most recent *non-merging* batch of that id |
| 130 | OpBatch* mBatchLookup[OpBatchType::Count] = { nullptr }; |
| 131 | CanvasState mCanvasState; |
| 132 | |
| 133 | // contains ResolvedOps and Batches |
| 134 | LinearAllocator mAllocator; |
| 135 | |
| 136 | size_t mEarliestBatchIndex = 0; |
| 137 | }; |
| 138 | |
| 139 | }; // namespace uirenderer |
| 140 | }; // namespace android |
| 141 | |
| 142 | #endif // ANDROID_HWUI_OP_REORDERER_H |