1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrOpFlushState_DEFINED 9 #define GrOpFlushState_DEFINED 10 11 #include <utility> 12 #include "GrAppliedClip.h" 13 #include "GrBufferAllocPool.h" 14 #include "GrDeferredUpload.h" 15 #include "GrDeinstantiateProxyTracker.h" 16 #include "GrRenderTargetProxy.h" 17 #include "SkArenaAlloc.h" 18 #include "SkArenaAllocList.h" 19 #include "ops/GrMeshDrawOp.h" 20 21 class GrGpu; 22 class GrGpuCommandBuffer; 23 class GrGpuRTCommandBuffer; 24 class GrResourceProvider; 25 26 /** Tracks the state across all the GrOps (really just the GrDrawOps) in a GrOpList flush. */ 27 class GrOpFlushState final : public GrDeferredUploadTarget, public GrMeshDrawOp::Target { 28 public: 29 // vertexSpace and indexSpace may either be null or an alloation of size 30 // GrBufferAllocPool::kDefaultBufferSize. If the latter, then CPU memory is only allocated for 31 // vertices/indices when a buffer larger than kDefaultBufferSize is required. 32 GrOpFlushState(GrGpu*, GrResourceProvider*, GrTokenTracker*, 33 sk_sp<GrBufferAllocPool::CpuBufferCache> = nullptr); 34 ~GrOpFlushState()35 ~GrOpFlushState() final { this->reset(); } 36 37 /** This is called after each op has a chance to prepare its draws and before the draws are 38 executed. */ 39 void preExecuteDraws(); 40 41 void doUpload(GrDeferredTextureUploadFn&); 42 43 /** Called as ops are executed. Must be called in the same order as the ops were prepared. */ 44 void executeDrawsAndUploadsForMeshDrawOp( 45 const GrOp* op, const SkRect& chainBounds, GrProcessorSet&&, uint32_t pipelineFlags = 0, 46 const GrUserStencilSettings* = &GrUserStencilSettings::kUnused); 47 commandBuffer()48 GrGpuCommandBuffer* commandBuffer() { return fCommandBuffer; } 49 // Helper function used by Ops that are only called via RenderTargetOpLists 50 GrGpuRTCommandBuffer* rtCommandBuffer(); setCommandBuffer(GrGpuCommandBuffer * buffer)51 void setCommandBuffer(GrGpuCommandBuffer* buffer) { fCommandBuffer = buffer; } 52 gpu()53 GrGpu* gpu() { return fGpu; } 54 55 void reset(); 56 57 /** Additional data required on a per-op basis when executing GrOps. */ 58 struct OpArgs { originOpArgs59 GrSurfaceOrigin origin() const { return fProxy->origin(); } renderTargetOpArgs60 GrRenderTarget* renderTarget() const { return fProxy->peekRenderTarget(); } 61 62 GrOp* fOp; 63 // TODO: do we still need the dst proxy here? 64 GrRenderTargetProxy* fProxy; 65 GrAppliedClip* fAppliedClip; 66 GrXferProcessor::DstProxy fDstProxy; 67 }; 68 setOpArgs(OpArgs * opArgs)69 void setOpArgs(OpArgs* opArgs) { fOpArgs = opArgs; } 70 drawOpArgs()71 const OpArgs& drawOpArgs() const { 72 SkASSERT(fOpArgs); 73 SkASSERT(fOpArgs->fOp); 74 return *fOpArgs; 75 } 76 77 /** Overrides of GrDeferredUploadTarget. */ 78 tokenTracker()79 const GrTokenTracker* tokenTracker() final { return fTokenTracker; } 80 GrDeferredUploadToken addInlineUpload(GrDeferredTextureUploadFn&&) final; 81 GrDeferredUploadToken addASAPUpload(GrDeferredTextureUploadFn&&) final; 82 83 /** Overrides of GrMeshDrawOp::Target. */ 84 void recordDraw( 85 sk_sp<const GrGeometryProcessor>, const GrMesh[], int meshCnt, 86 const GrPipeline::FixedDynamicState*, const GrPipeline::DynamicStateArrays*) final; 87 void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*, 88 int* startVertex) final; 89 uint16_t* makeIndexSpace(int indexCount, sk_sp<const GrBuffer>*, int* startIndex) final; 90 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount, 91 sk_sp<const GrBuffer>*, int* startVertex, 92 int* actualVertexCount) final; 93 uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, 94 sk_sp<const GrBuffer>*, int* startIndex, 95 int* actualIndexCount) final; 96 void putBackIndices(int indexCount) final; 97 void putBackVertices(int vertices, size_t vertexStride) final; proxy()98 GrRenderTargetProxy* proxy() const final { return fOpArgs->fProxy; } appliedClip()99 const GrAppliedClip* appliedClip() final { return fOpArgs->fAppliedClip; } 100 GrAppliedClip detachAppliedClip() final; dstProxy()101 const GrXferProcessor::DstProxy& dstProxy() const final { return fOpArgs->fDstProxy; } deferredUploadTarget()102 GrDeferredUploadTarget* deferredUploadTarget() final { return this; } 103 const GrCaps& caps() const final; resourceProvider()104 GrResourceProvider* resourceProvider() const final { return fResourceProvider; } 105 106 GrStrikeCache* glyphCache() const final; 107 108 // At this point we know we're flushing so full access to the GrAtlasManager is required (and 109 // permissible). 110 GrAtlasManager* atlasManager() const final; 111 deinstantiateProxyTracker()112 GrDeinstantiateProxyTracker* deinstantiateProxyTracker() { return &fDeinstantiateProxyTracker; } 113 114 private: 115 /** GrMeshDrawOp::Target override. */ allocator()116 SkArenaAlloc* allocator() override { return &fArena; } 117 118 struct InlineUpload { InlineUploadInlineUpload119 InlineUpload(GrDeferredTextureUploadFn&& upload, GrDeferredUploadToken token) 120 : fUpload(std::move(upload)), fUploadBeforeToken(token) {} 121 GrDeferredTextureUploadFn fUpload; 122 GrDeferredUploadToken fUploadBeforeToken; 123 }; 124 125 // A set of contiguous draws that share a draw token, geometry processor, and pipeline. The 126 // meshes for the draw are stored in the fMeshes array. The reason for coalescing meshes 127 // that share a geometry processor into a Draw is that it allows the Gpu object to setup 128 // the shared state once and then issue draws for each mesh. 129 struct Draw { 130 ~Draw(); 131 sk_sp<const GrGeometryProcessor> fGeometryProcessor; 132 const GrPipeline::FixedDynamicState* fFixedDynamicState; 133 const GrPipeline::DynamicStateArrays* fDynamicStateArrays; 134 const GrMesh* fMeshes = nullptr; 135 const GrOp* fOp = nullptr; 136 int fMeshCnt = 0; 137 }; 138 139 // Storage for ops' pipelines, draws, and inline uploads. 140 SkArenaAlloc fArena{sizeof(GrPipeline) * 100}; 141 142 // Store vertex and index data on behalf of ops that are flushed. 143 GrVertexBufferAllocPool fVertexPool; 144 GrIndexBufferAllocPool fIndexPool; 145 146 // Data stored on behalf of the ops being flushed. 147 SkArenaAllocList<GrDeferredTextureUploadFn> fASAPUploads; 148 SkArenaAllocList<InlineUpload> fInlineUploads; 149 SkArenaAllocList<Draw> fDraws; 150 151 // All draws we store have an implicit draw token. This is the draw token for the first draw 152 // in fDraws. 153 GrDeferredUploadToken fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken(); 154 155 // Info about the op that is currently preparing or executing using the flush state or null if 156 // an op is not currently preparing of executing. 157 OpArgs* fOpArgs = nullptr; 158 159 GrGpu* fGpu; 160 GrResourceProvider* fResourceProvider; 161 GrTokenTracker* fTokenTracker; 162 GrGpuCommandBuffer* fCommandBuffer = nullptr; 163 164 // Variables that are used to track where we are in lists as ops are executed 165 SkArenaAllocList<Draw>::Iter fCurrDraw; 166 SkArenaAllocList<InlineUpload>::Iter fCurrUpload; 167 168 // Used to track the proxies that need to be deinstantiated after we finish a flush 169 GrDeinstantiateProxyTracker fDeinstantiateProxyTracker; 170 }; 171 172 #endif 173