FrameBuilder.h revision 80d2ade939153da87b3cd3b0a69a713bf68b64ba
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, 70 nodes, lightGeometry, Rect(), caches) {} 71 72 FrameBuilder(const LayerUpdateQueue& layers, const SkRect& clip, 73 uint32_t viewportWidth, uint32_t viewportHeight, 74 const std::vector< sp<RenderNode> >& nodes, 75 const LightGeometry& lightGeometry, 76 const Rect &contentDrawBounds, 77 Caches& caches); 78 79 virtual ~FrameBuilder() {} 80 81 /** 82 * replayBakedOps() is templated based on what class will receive ops being replayed. 83 * 84 * It constructs a lookup array of lambdas, which allows a recorded BakeOpState to use 85 * state->op->opId to lookup a receiver that will be called when the op is replayed. 86 */ 87 template <typename StaticDispatcher, typename Renderer> 88 void replayBakedOps(Renderer& renderer) { 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 GL_CHECKPOINT(MODERATE); 133 layer.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); 134 GL_CHECKPOINT(MODERATE); 135 renderer.endLayer(); 136 } 137 } 138 139 GL_CHECKPOINT(MODERATE); 140 const LayerBuilder& fbo0 = *(mLayerBuilders[0]); 141 renderer.startFrame(fbo0.width, fbo0.height, fbo0.repaintRect); 142 GL_CHECKPOINT(MODERATE); 143 fbo0.replayBakedOpsImpl((void*)&renderer, unmergedReceivers, mergedReceivers); 144 GL_CHECKPOINT(MODERATE); 145 renderer.endFrame(fbo0.repaintRect); 146 } 147 148 void dump() const { 149 for (auto&& layer : mLayerBuilders) { 150 layer->dump(); 151 } 152 } 153 154 /////////////////////////////////////////////////////////////////// 155 /// CanvasStateClient interface 156 /////////////////////////////////////////////////////////////////// 157 virtual void onViewportInitialized() override; 158 virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override; 159 virtual GLuint getTargetFbo() const override { return 0; } 160 161private: 162 void finishDefer(); 163 enum class ChildrenSelectMode { 164 Negative, 165 Positive 166 }; 167 void saveForLayer(uint32_t layerWidth, uint32_t layerHeight, 168 float contentTranslateX, float contentTranslateY, 169 const Rect& repaintRect, 170 const Vector3& lightCenter, 171 const BeginLayerOp* beginLayerOp, RenderNode* renderNode); 172 void restoreForLayer(); 173 174 LayerBuilder& currentLayer() { return *(mLayerBuilders[mLayerStack.back()]); } 175 176 BakedOpState* tryBakeOpState(const RecordedOp& recordedOp) { 177 return BakedOpState::tryConstruct(mAllocator, *mCanvasState.writableSnapshot(), recordedOp); 178 } 179 BakedOpState* tryBakeUnboundedOpState(const RecordedOp& recordedOp) { 180 return BakedOpState::tryConstructUnbounded(mAllocator, *mCanvasState.writableSnapshot(), recordedOp); 181 } 182 183 184 // should always be surrounded by a save/restore pair, and not called if DisplayList is null 185 void deferNodePropsAndOps(RenderNode& node); 186 187 template <typename V> 188 void defer3dChildren(ChildrenSelectMode mode, const V& zTranslatedNodes); 189 190 void deferShadow(const RenderNodeOp& casterOp); 191 192 void deferProjectedChildren(const RenderNode& renderNode); 193 194 void deferNodeOps(const RenderNode& renderNode); 195 196 void deferRenderNodeOpImpl(const RenderNodeOp& op); 197 198 void replayBakedOpsImpl(void* arg, BakedOpReceiver* receivers); 199 200 SkPath* createFrameAllocatedPath() { 201 return mAllocator.create<SkPath>(); 202 } 203 204 BakedOpState* deferStrokeableOp(const RecordedOp& op, batchid_t batchId, 205 BakedOpState::StrokeBehavior strokeBehavior = BakedOpState::StrokeBehavior::StyleDefined); 206 207 /** 208 * Declares all FrameBuilder::deferXXXXOp() methods for every RecordedOp type. 209 * 210 * These private methods are called from within deferImpl to defer each individual op 211 * type differently. 212 */ 213#define X(Type) void defer##Type(const Type& op); 214 MAP_DEFERRABLE_OPS(X) 215#undef X 216 217 // List of every deferred layer's render state. Replayed in reverse order to render a frame. 218 std::vector<LayerBuilder*> mLayerBuilders; 219 220 /* 221 * Stack of indices within mLayerBuilders representing currently active layers. If drawing 222 * layerA within a layerB, will contain, in order: 223 * - 0 (representing FBO 0, always present) 224 * - layerB's index 225 * - layerA's index 226 * 227 * Note that this doesn't vector doesn't always map onto all values of mLayerBuilders. When a 228 * layer is finished deferring, it will still be represented in mLayerBuilders, but it's index 229 * won't be in mLayerStack. This is because it can be replayed, but can't have any more drawing 230 * ops added to it. 231 */ 232 std::vector<size_t> mLayerStack; 233 234 CanvasState mCanvasState; 235 236 Caches& mCaches; 237 238 float mLightRadius; 239 240 // contains single-frame objects, such as BakedOpStates, LayerBuilders, Batches 241 LinearAllocator mAllocator; 242}; 243 244}; // namespace uirenderer 245}; // namespace android 246