blob: 0a46ed2fd310ecc8c596d77d030cfb0559caf3c3 [file] [log] [blame]
bsalomon75398562015-08-17 12:55:38 -07001/*
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
Brian Salomon09d994e2016-12-21 11:14:46 -05008#ifndef GrOpFlushState_DEFINED
9#define GrOpFlushState_DEFINED
bsalomon75398562015-08-17 12:55:38 -070010
Brian Salomon7dc6e752017-11-02 11:34:51 -040011#include <utility>
Brian Salomonbfd18cd2017-08-09 16:27:09 -040012#include "GrAppliedClip.h"
bsalomon75398562015-08-17 12:55:38 -070013#include "GrBufferAllocPool.h"
Brian Salomon29b60c92017-10-31 14:42:10 -040014#include "GrDeferredUpload.h"
Greg Daniel4684f822018-03-08 15:27:36 -050015#include "GrUninstantiateProxyTracker.h"
Brian Salomon6d4b65e2017-05-03 17:06:09 -040016#include "SkArenaAlloc.h"
Brian Salomoncbcb0a12017-11-19 13:20:13 -050017#include "SkArenaAllocList.h"
Brian Salomon89527432016-12-16 09:52:16 -050018#include "ops/GrMeshDrawOp.h"
bsalomon75398562015-08-17 12:55:38 -070019
Robert Phillips646e4292017-06-13 12:44:56 -040020class GrGpu;
egdaniel9cb63402016-06-23 08:37:05 -070021class GrGpuCommandBuffer;
Greg Daniel500d58b2017-08-24 15:59:33 -040022class GrGpuRTCommandBuffer;
bsalomon75398562015-08-17 12:55:38 -070023class GrResourceProvider;
24
Brian Salomon742e31d2016-12-07 17:06:19 -050025/** Tracks the state across all the GrOps (really just the GrDrawOps) in a GrOpList flush. */
Brian Salomon29b60c92017-10-31 14:42:10 -040026class GrOpFlushState final : public GrDeferredUploadTarget, public GrMeshDrawOp::Target {
bsalomon75398562015-08-17 12:55:38 -070027public:
Robert Phillips40a29d72018-01-18 12:59:22 -050028 GrOpFlushState(GrGpu*, GrResourceProvider*, GrTokenTracker*);
bsalomon75398562015-08-17 12:55:38 -070029
Brian Salomon29b60c92017-10-31 14:42:10 -040030 ~GrOpFlushState() final { this->reset(); }
bsalomon75398562015-08-17 12:55:38 -070031
Brian Salomon742e31d2016-12-07 17:06:19 -050032 /** This is called after each op has a chance to prepare its draws and before the draws are
Brian Salomon7dc6e752017-11-02 11:34:51 -040033 executed. */
34 void preExecuteDraws();
bsalomon75398562015-08-17 12:55:38 -070035
Brian Salomon943ed792017-10-30 09:37:55 -040036 void doUpload(GrDeferredTextureUploadFn&);
bsalomon342bfc22016-04-01 06:06:20 -070037
Brian Salomon7dc6e752017-11-02 11:34:51 -040038 /** Called as ops are executed. Must be called in the same order as the ops were prepared. */
39 void executeDrawsAndUploadsForMeshDrawOp(uint32_t opID, const SkRect& opBounds);
40
egdaniel9cb63402016-06-23 08:37:05 -070041 GrGpuCommandBuffer* commandBuffer() { return fCommandBuffer; }
Greg Daniel500d58b2017-08-24 15:59:33 -040042 // Helper function used by Ops that are only called via RenderTargetOpLists
43 GrGpuRTCommandBuffer* rtCommandBuffer();
egdaniel9cb63402016-06-23 08:37:05 -070044 void setCommandBuffer(GrGpuCommandBuffer* buffer) { fCommandBuffer = buffer; }
45
bsalomon75398562015-08-17 12:55:38 -070046 GrGpu* gpu() { return fGpu; }
47
Brian Salomon7dc6e752017-11-02 11:34:51 -040048 void reset();
joshualittf6d259b2015-10-02 11:27:14 -070049
Brian Salomon29b60c92017-10-31 14:42:10 -040050 /** Additional data required on a per-op basis when executing GrOps. */
51 struct OpArgs {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040052 GrRenderTarget* renderTarget() const { return fProxy->peekRenderTarget(); }
Robert Phillips2890fbf2017-07-26 15:48:41 -040053
Brian Salomon29b60c92017-10-31 14:42:10 -040054 GrOp* fOp;
Robert Phillips19e51dc2017-08-09 09:30:51 -040055 // TODO: do we still need the dst proxy here?
Brian Salomonbfd18cd2017-08-09 16:27:09 -040056 GrRenderTargetProxy* fProxy;
57 GrAppliedClip* fAppliedClip;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040058 GrXferProcessor::DstProxy fDstProxy;
Brian Salomon54d212e2017-03-21 14:22:38 -040059 };
60
Brian Salomon29b60c92017-10-31 14:42:10 -040061 void setOpArgs(OpArgs* opArgs) { fOpArgs = opArgs; }
Brian Salomon54d212e2017-03-21 14:22:38 -040062
Brian Salomon29b60c92017-10-31 14:42:10 -040063 const OpArgs& drawOpArgs() const {
Brian Salomon54d212e2017-03-21 14:22:38 -040064 SkASSERT(fOpArgs);
Brian Salomon29b60c92017-10-31 14:42:10 -040065 SkASSERT(fOpArgs->fOp);
Brian Salomon54d212e2017-03-21 14:22:38 -040066 return *fOpArgs;
67 }
68
Brian Salomon29b60c92017-10-31 14:42:10 -040069 /** Overrides of GrDeferredUploadTarget. */
Brian Salomonbfd18cd2017-08-09 16:27:09 -040070
Robert Phillips40a29d72018-01-18 12:59:22 -050071 const GrTokenTracker* tokenTracker() final { return fTokenTracker; }
Brian Salomon29b60c92017-10-31 14:42:10 -040072 GrDeferredUploadToken addInlineUpload(GrDeferredTextureUploadFn&&) final;
73 GrDeferredUploadToken addASAPUpload(GrDeferredTextureUploadFn&&) final;
Brian Salomon29b60c92017-10-31 14:42:10 -040074
75 /** Overrides of GrMeshDrawOp::Target. */
Brian Salomon7eae3e02018-08-07 14:02:38 +000076 void draw(sk_sp<const GrGeometryProcessor>,
77 const GrPipeline*,
78 const GrPipeline::FixedDynamicState*,
79 const GrMesh[],
Brian Salomoncd7907b2018-08-30 08:36:18 -040080 int meshCount) final;
Brian Salomon29b60c92017-10-31 14:42:10 -040081 void* makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer**,
82 int* startVertex) final;
83 uint16_t* makeIndexSpace(int indexCount, const GrBuffer**, int* startIndex) final;
84 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
85 const GrBuffer**, int* startVertex, int* actualVertexCount) final;
86 uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, const GrBuffer**,
87 int* startIndex, int* actualIndexCount) final;
88 void putBackIndices(int indexCount) final;
89 void putBackVertices(int vertices, size_t vertexStride) final;
90 GrRenderTargetProxy* proxy() const final { return fOpArgs->fProxy; }
91 GrAppliedClip detachAppliedClip() final;
92 const GrXferProcessor::DstProxy& dstProxy() const final { return fOpArgs->fDstProxy; }
93 GrDeferredUploadTarget* deferredUploadTarget() final { return this; }
94 const GrCaps& caps() const final;
95 GrResourceProvider* resourceProvider() const final { return fResourceProvider; }
Brian Salomon6d4b65e2017-05-03 17:06:09 -040096
Robert Phillipsc4039ea2018-03-01 11:36:45 -050097 GrGlyphCache* glyphCache() const final;
98
99 // At this point we know we're flushing so full access to the GrAtlasManager is required (and
100 // permissible).
Robert Phillips5a66efb2018-03-07 15:13:18 -0500101 GrAtlasManager* atlasManager() const final;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500102
Greg Daniel4684f822018-03-08 15:27:36 -0500103 GrUninstantiateProxyTracker* uninstantiateProxyTracker() {
104 return &fUninstantiateProxyTracker;
105 }
106
bsalomon75398562015-08-17 12:55:38 -0700107private:
Brian Salomon29b60c92017-10-31 14:42:10 -0400108 /** GrMeshDrawOp::Target override. */
Brian Salomon7dc6e752017-11-02 11:34:51 -0400109 SkArenaAlloc* pipelineArena() override { return &fArena; }
110
111 struct InlineUpload {
112 InlineUpload(GrDeferredTextureUploadFn&& upload, GrDeferredUploadToken token)
113 : fUpload(std::move(upload)), fUploadBeforeToken(token) {}
114 GrDeferredTextureUploadFn fUpload;
115 GrDeferredUploadToken fUploadBeforeToken;
116 };
117
118 // A set of contiguous draws that share a draw token, geometry processor, and pipeline. The
119 // meshes for the draw are stored in the fMeshes array. The reason for coalescing meshes
120 // that share a geometry processor into a Draw is that it allows the Gpu object to setup
121 // the shared state once and then issue draws for each mesh.
122 struct Draw {
Brian Salomoncd7907b2018-08-30 08:36:18 -0400123 ~Draw() {
124 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
125 fFixedDynamicState->fPrimitiveProcessorTextures[i]->completedRead();
126 }
127 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000128 sk_sp<const GrGeometryProcessor> fGeometryProcessor;
129 const GrPipeline* fPipeline = nullptr;
Brian Salomon49348902018-06-26 09:12:38 -0400130 const GrPipeline::FixedDynamicState* fFixedDynamicState;
131 const GrPipeline::DynamicStateArrays* fDynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000132 const GrMesh* fMeshes = nullptr;
133 int fMeshCnt = 0;
134 uint32_t fOpID = SK_InvalidUniqueID;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400135 };
136
Brian Salomon7dc6e752017-11-02 11:34:51 -0400137 // Storage for ops' pipelines, draws, and inline uploads.
138 SkArenaAlloc fArena{sizeof(GrPipeline) * 100};
139
140 // Store vertex and index data on behalf of ops that are flushed.
141 GrVertexBufferAllocPool fVertexPool;
142 GrIndexBufferAllocPool fIndexPool;
143
144 // Data stored on behalf of the ops being flushed.
Robert Phillips40a29d72018-01-18 12:59:22 -0500145 SkArenaAllocList<GrDeferredTextureUploadFn> fASAPUploads;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500146 SkArenaAllocList<InlineUpload> fInlineUploads;
147 SkArenaAllocList<Draw> fDraws;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400148
149 // All draws we store have an implicit draw token. This is the draw token for the first draw
150 // in fDraws.
151 GrDeferredUploadToken fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
152
153 // Info about the op that is currently preparing or executing using the flush state or null if
154 // an op is not currently preparing of executing.
155 OpArgs* fOpArgs = nullptr;
Brian Salomon29b60c92017-10-31 14:42:10 -0400156
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400157 GrGpu* fGpu;
158 GrResourceProvider* fResourceProvider;
Robert Phillips40a29d72018-01-18 12:59:22 -0500159 GrTokenTracker* fTokenTracker;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400160 GrGpuCommandBuffer* fCommandBuffer = nullptr;
161
162 // Variables that are used to track where we are in lists as ops are executed
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500163 SkArenaAllocList<Draw>::Iter fCurrDraw;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500164 SkArenaAllocList<InlineUpload>::Iter fCurrUpload;
Greg Daniel4684f822018-03-08 15:27:36 -0500165
166 // Used to track the proxies that need to be uninstantiated after we finish a flush
167 GrUninstantiateProxyTracker fUninstantiateProxyTracker;
bsalomon75398562015-08-17 12:55:38 -0700168};
169
bsalomon75398562015-08-17 12:55:38 -0700170#endif