1 /* 2 * Copyright (C) 2016 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 #pragma once 18 19 #include "BakedOpState.h" 20 #include "CanvasState.h" 21 #include "DisplayList.h" 22 #include "LayerBuilder.h" 23 #include "RecordedOp.h" 24 #include "utils/GLUtils.h" 25 26 #include <unordered_map> 27 #include <vector> 28 29 struct SkRect; 30 31 namespace android { 32 namespace uirenderer { 33 34 class BakedOpState; 35 class LayerUpdateQueue; 36 class OffscreenBuffer; 37 class Rect; 38 39 /** 40 * Processes, optimizes, and stores rendering commands from RenderNodes and 41 * LayerUpdateQueue, building content needed to render a frame. 42 * 43 * Resolves final drawing state for each operation (including clip, alpha and matrix), and then 44 * reorder and merge each op as it is resolved for drawing efficiency. Each layer of content (either 45 * from the LayerUpdateQueue, or temporary layers created by saveLayer operations in the 46 * draw stream) will create different reorder contexts, each in its own LayerBuilder. 47 * 48 * Then the prepared or 'baked' drawing commands can be issued by calling the templated 49 * replayBakedOps() function, which will dispatch them (including any created merged op collections) 50 * to a Dispatcher and Renderer. See BakedOpDispatcher for how these baked drawing operations are 51 * resolved into Glops and rendered via BakedOpRenderer. 52 * 53 * This class is also the authoritative source for traversing RenderNodes, both for standard op 54 * traversal within a DisplayList, and for out of order RenderNode traversal for Z and projection. 55 */ 56 class FrameBuilder : public CanvasStateClient { 57 public: 58 struct LightGeometry { 59 Vector3 center; 60 float radius; 61 }; 62 63 FrameBuilder(const SkRect& clip, uint32_t viewportWidth, uint32_t viewportHeight, 64 const LightGeometry& lightGeometry, Caches& caches); 65 66 FrameBuilder(const LayerUpdateQueue& layerUpdateQueue, const LightGeometry& lightGeometry, 67 Caches& caches); 68 69 void deferLayers(const LayerUpdateQueue& layers); 70 71 void deferRenderNode(RenderNode& renderNode); 72 73 void deferRenderNode(float tx, float ty, Rect clipRect, RenderNode& renderNode); 74 75 void deferRenderNodeScene(const std::vector<sp<RenderNode> >& nodes, 76 const Rect& contentDrawBounds); 77 78 virtual ~FrameBuilder() {} 79 80 /** 81 * replayBakedOps() is templated based on what class will receive ops being replayed. 82 * 83 * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use 84 * state->op->opId to lookup a receiver that will be called when the op is replayed. 85 */ 86 template <typename StaticDispatcher, typename Renderer> 87 void replayBakedOps(Renderer& renderer) { 88 std::vector<OffscreenBuffer*> temporaryLayers; 89 finishDefer(); 90 /** 91 * Defines a LUT of lambdas which allow a recorded BakedOpState to use state->op->opId to 92 * dispatch the op via a method on a static dispatcher when the op is replayed. 93 * 94 * For example a BitmapOp would resolve, via the lambda lookup, to calling: 95 * 96 * StaticDispatcher::onBitmapOp(Renderer& renderer, const BitmapOp& op, const BakedOpState& state); 97 */ 98 #define X(Type) \ 99 [](void* renderer, const BakedOpState& state) { \ 100 StaticDispatcher::on##Type(*(static_cast<Renderer*>(renderer)), \ 101 static_cast<const Type&>(*(state.op)), state); \ 102 }, 103 static BakedOpReceiver unmergedReceivers[] = BUILD_RENDERABLE_OP_LUT(X); 104 #undef X 105 106 /** 107 * Defines a LUT of lambdas which allow merged arrays of BakedOpState* to be passed to a 108 * static dispatcher when the group of merged ops is replayed. 109 */ 110 #define X(Type) \ 111 [](void* renderer, const MergedBakedOpList& opList) { \ 112 StaticDispatcher::onMerged##Type##s(*(static_cast<Renderer*>(renderer)), opList); \ 113 }, 114 static MergedOpReceiver mergedReceivers[] = BUILD_MERGEABLE_OP_LUT(X); 115 #undef X 116 117 // Relay through layers in reverse order, since layers 118 // later in the list will be drawn by earlier ones 119 for (int i = mLayerBuilders.size() - 1; i >= 1; i--) { 120 GL_CHECKPOINT(MODERATE); 121 LayerBuilder& layer = *(mLayerBuilders[i]); 122 if (layer.renderNode) { 123 // cached HW layer - can't skip layer if empty 124 renderer.startRepaintLayer(layer.offscreenBuffer, layer.repaintRect); 125 GL_CHECKPOINT(MODERATE); 126 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); 127 GL_CHECKPOINT(MODERATE); 128 renderer.endLayer(); 129 } else if (!layer.empty()) { 130 // save layer - skip entire layer if empty (in which case, LayerOp has null layer). 131 layer.offscreenBuffer = renderer.startTemporaryLayer(layer.width, layer.height); 132 temporaryLayers.push_back(layer.offscreenBuffer); 133 GL_CHECKPOINT(MODERATE); 134 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); 135 GL_CHECKPOINT(MODERATE); 136 renderer.endLayer(); 137 } 138 } 139 140 GL_CHECKPOINT(MODERATE); 141 if (CC_LIKELY(mDrawFbo0)) { 142 const LayerBuilder& fbo0 = *(mLayerBuilders[0]); 143 renderer.startFrame(fbo0.width, fbo0.height, fbo0.repaintRect); 144 GL_CHECKPOINT(MODERATE); 145 fbo0.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); 146 GL_CHECKPOINT(MODERATE); 147 renderer.endFrame(fbo0.repaintRect); 148 } 149 150 for (auto& temporaryLayer : temporaryLayers) { 151 renderer.recycleTemporaryLayer(temporaryLayer); 152 } 153 } 154 155 void dump() const { 156 for (auto&& layer : mLayerBuilders) { 157 layer->dump(); 158 } 159 } 160 161 /////////////////////////////////////////////////////////////////// 162 /// CanvasStateClient interface 163 /////////////////////////////////////////////////////////////////// 164 virtual void onViewportInitialized() override; 165 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override; 166 virtual GLuint getTargetFbo() const override { return 0; } 167 168 private: 169 void finishDefer(); 170 enum class ChildrenSelectMode { Negative, Positive }; 171 void saveForLayer(uint32_t layerWidth, uint32_t layerHeight, float contentTranslateX, 172 float contentTranslateY, const Rect& repaintRect, const Vector3& lightCenter, 173 const BeginLayerOp* beginLayerOp, RenderNode* renderNode); 174 void restoreForLayer(); 175 176 LayerBuilder& currentLayer() { return *(mLayerBuilders[mLayerStack.back()]); } 177 178 BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) { 179 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.writableSnapshot(), recordedOp); 180 } 181 BakedOpState* tryBakeUnboundedOpState(const RecordedOp& recordedOp) { 182 return BakedOpState::tryConstructUnbounded(mAllocator, *mCanvasState.writableSnapshot(), 183 recordedOp); 184 } 185 186 // should always be surrounded by a save/restore pair, and not called if DisplayList is null 187 void deferNodePropsAndOps(RenderNode& node); 188 189 template <typename V> 190 void defer3dChildren(const ClipBase* reorderClip, ChildrenSelectMode mode, 191 const V& zTranslatedNodes); 192 193 void deferShadow(const ClipBase* reorderClip, const RenderNodeOp& casterOp); 194 195 void deferProjectedChildren(const RenderNode& renderNode); 196 197 void deferNodeOps(const RenderNode& renderNode); 198 199 void deferRenderNodeOpImpl(const RenderNodeOp& op); 200 201 void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers); 202 203 SkPath* createFrameAllocatedPath() { return mAllocator.create<SkPath>(); } 204 205 BakedOpState* deferStrokeableOp(const RecordedOp& op, batchid_t batchId, 206 BakedOpState::StrokeBehavior strokeBehavior = 207 BakedOpState::StrokeBehavior::StyleDefined, 208 bool expandForPathTexture = false); 209 210 /** 211 * Declares all FrameBuilder::deferXXXXOp() methods for every RecordedOp type. 212 * 213 * These private methods are called from within deferImpl to defer each individual op 214 * type differently. 215 */ 216 #define X(Type) void defer##Type(const Type& op); 217 MAP_DEFERRABLE_OPS(X) 218 #undef X 219 220 // contains single-frame objects, such as BakedOpStates, LayerBuilders, Batches 221 LinearAllocator mAllocator; 222 LinearStdAllocator<void*> mStdAllocator; 223 224 // List of every deferred layer's render state. Replayed in reverse order to render a frame. 225 LsaVector<LayerBuilder*> mLayerBuilders; 226 227 /* 228 * Stack of indices within mLayerBuilders representing currently active layers. If drawing 229 * layerA within a layerB, will contain, in order: 230 * - 0 (representing FBO 0, always present) 231 * - layerB's index 232 * - layerA's index 233 * 234 * Note that this doesn't vector doesn't always map onto all values of mLayerBuilders. When a 235 * layer is finished deferring, it will still be represented in mLayerBuilders, but it's index 236 * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing 237 * ops added to it. 238 */ 239 LsaVector<size_t> mLayerStack; 240 241 CanvasState mCanvasState; 242 243 Caches& mCaches; 244 245 float mLightRadius; 246 247 const bool mDrawFbo0; 248 }; 249 250 }; // namespace uirenderer 251 }; // namespace android 252