FrameBuilder.h revision 6e068c0182f6f85bccb855a647510724d1c65a13
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 <vector> 27#include <unordered_map> 28 29struct SkRect; 30 31namespace android { 32namespace uirenderer { 33 34class BakedOpState; 35class LayerUpdateQueue; 36class OffscreenBuffer; 37class Rect; 38 39/** 40 * Traverses all of the drawing commands from the layers and RenderNodes passed into it, preparing 41 * them to be rendered. 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 */ 56class FrameBuilder : public CanvasStateClient { 57public: 58 struct LightGeometry { 59 Vector3 center; 60 float radius; 61 }; 62 63 // TODO: remove 64 FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip, 65 uint32_t viewportWidth, uint32_t viewportHeight, 66 const std::vector< sp<RenderNode> >& nodes, 67 const LightGeometry& lightGeometry, 68 Caches* caches) 69 : FrameBuilder(layers, clip, viewportWidth, viewportHeight, nodes, lightGeometry, Rect(), caches) {} 70 71 FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip, 72 uint32_t viewportWidth, uint32_t viewportHeight, 73 const std::vector< sp<RenderNode> >& nodes, 74 const LightGeometry& lightGeometry, 75 const Rect &contentDrawBounds, Caches* caches); 76 77 virtual ~FrameBuilder() {} 78 79 /** 80 * replayBakedOps() is templated based on what class will receive ops being replayed. 81 * 82 * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use 83 * state->op->opId to lookup a receiver that will be called when the op is replayed. 84 * 85 */ 86 template <typename StaticDispatcher, typename Renderer> 87 void replayBakedOps(Renderer& renderer) { 88 /** 89 * Defines a LUT of lambdas which allow a recorded BakedOpState to use state->op->opId to 90 * dispatch the op via a method on a static dispatcher when the op is replayed. 91 * 92 * For example a BitmapOp would resolve, via the lambda lookup, to calling: 93 * 94 * StaticDispatcher::onBitmapOp(Renderer& renderer, const BitmapOp& op, const BakedOpState& state); 95 */ 96 #define X(Type) \ 97 [](void* renderer, const BakedOpState& state) { \ 98 StaticDispatcher::on##Type(*(static_cast<Renderer*>(renderer)), \ 99 static_cast<const Type&>(*(state.op)), state); \ 100 }, 101 static BakedOpReceiver unmergedReceivers[] = BUILD_RENDERABLE_OP_LUT(X); 102 #undef X 103 104 /** 105 * Defines a LUT of lambdas which allow merged arrays of BakedOpState* to be passed to a 106 * static dispatcher when the group of merged ops is replayed. 107 */ 108 #define X(Type) \ 109 [](void* renderer, const MergedBakedOpList& opList) { \ 110 StaticDispatcher::onMerged##Type##s(*(static_cast<Renderer*>(renderer)), opList); \ 111 }, 112 static MergedOpReceiver mergedReceivers[] = BUILD_MERGEABLE_OP_LUT(X); 113 #undef X 114 115 // Relay through layers in reverse order, since layers 116 // later in the list will be drawn by earlier ones 117 for (int i = mLayerBuilders.size() - 1; i >= 1; i--) { 118 GL_CHECKPOINT(MODERATE); 119 LayerBuilder& layer = *(mLayerBuilders[i]); 120 if (layer.renderNode) { 121 // cached HW layer - can't skip layer if empty 122 renderer.startRepaintLayer(layer.offscreenBuffer, layer.repaintRect); 123 GL_CHECKPOINT(MODERATE); 124 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); 125 GL_CHECKPOINT(MODERATE); 126 renderer.endLayer(); 127 } else if (!layer.empty()) { // save layer - skip entire layer if empty 128 layer.offscreenBuffer = renderer.startTemporaryLayer(layer.width, layer.height); 129 GL_CHECKPOINT(MODERATE); 130 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); 131 GL_CHECKPOINT(MODERATE); 132 renderer.endLayer(); 133 } 134 } 135 136 GL_CHECKPOINT(MODERATE); 137 const LayerBuilder& fbo0 = *(mLayerBuilders[0]); 138 renderer.startFrame(fbo0.width, fbo0.height, fbo0.repaintRect); 139 GL_CHECKPOINT(MODERATE); 140 fbo0.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); 141 GL_CHECKPOINT(MODERATE); 142 renderer.endFrame(fbo0.repaintRect); 143 } 144 145 void dump() const { 146 for (auto&& layer : mLayerBuilders) { 147 layer->dump(); 148 } 149 } 150 151 /////////////////////////////////////////////////////////////////// 152 /// CanvasStateClient interface 153 /////////////////////////////////////////////////////////////////// 154 virtual void onViewportInitialized() override; 155 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override; 156 virtual GLuint getTargetFbo() const override { return 0; } 157 158private: 159 enum class ChildrenSelectMode { 160 Negative, 161 Positive 162 }; 163 void saveForLayer(uint32_t layerWidth, uint32_t layerHeight, 164 float contentTranslateX, float contentTranslateY, 165 const Rect& repaintRect, 166 const Vector3& lightCenter, 167 const BeginLayerOp* beginLayerOp, RenderNode* renderNode); 168 void restoreForLayer(); 169 170 LayerBuilder& currentLayer() { return *(mLayerBuilders[mLayerStack.back()]); } 171 172 BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) { 173 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.writableSnapshot(), recordedOp); 174 } 175 176 // should always be surrounded by a save/restore pair, and not called if DisplayList is null 177 void deferNodePropsAndOps(RenderNode& node); 178 179 template <typename V> 180 void defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes); 181 182 void deferShadow(const RenderNodeOp& casterOp); 183 184 void deferProjectedChildren(const RenderNode& renderNode); 185 186 void deferNodeOps(const RenderNode& renderNode); 187 188 void deferRenderNodeOpImpl(const RenderNodeOp& op); 189 190 void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers); 191 192 SkPath* createFrameAllocatedPath() { 193 return mAllocator.create<SkPath>(); 194 } 195 196 void deferStrokeableOp(const RecordedOp& op, batchid_t batchId, 197 BakedOpState::StrokeBehavior strokeBehavior = BakedOpState::StrokeBehavior::StyleDefined); 198 199 /** 200 * Declares all FrameBuilder::deferXXXXOp() methods for every RecordedOp type. 201 * 202 * These private methods are called from within deferImpl to defer each individual op 203 * type differently. 204 */ 205#define X(Type) void defer##Type(const Type& op); 206 MAP_DEFERRABLE_OPS(X) 207#undef X 208 209 // List of every deferred layer's render state. Replayed in reverse order to render a frame. 210 std::vector<LayerBuilder*> mLayerBuilders; 211 212 /* 213 * Stack of indices within mLayerBuilders representing currently active layers. If drawing 214 * layerA within a layerB, will contain, in order: 215 * - 0 (representing FBO 0, always present) 216 * - layerB's index 217 * - layerA's index 218 * 219 * Note that this doesn't vector doesn't always map onto all values of mLayerBuilders. When a 220 * layer is finished deferring, it will still be represented in mLayerBuilders, but it's index 221 * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing 222 * ops added to it. 223 */ 224 std::vector<size_t> mLayerStack; 225 226 CanvasState mCanvasState; 227 228 Caches* mCaches = nullptr; 229 230 float mLightRadius; 231 232 // contains single-frame objects, such as BakedOpStates, LayerBuilders, Batches 233 LinearAllocator mAllocator; 234}; 235 236}; // namespace uirenderer 237}; // namespace android 238