blob: f963c6956356f26ee3142723d4061ff6b14f3df6 [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"
Brian Salomon967df202018-12-07 11:15:53 -050015#include "GrDeinstantiateProxyTracker.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:
Brian Salomon58f153c2018-10-18 21:51:15 -040028 // vertexSpace and indexSpace may either be null or an alloation of size
29 // GrBufferAllocPool::kDefaultBufferSize. If the latter, then CPU memory is only allocated for
30 // vertices/indices when a buffer larger than kDefaultBufferSize is required.
31 GrOpFlushState(GrGpu*, GrResourceProvider*, GrTokenTracker*, void* vertexSpace,
32 void* indexSpace);
bsalomon75398562015-08-17 12:55:38 -070033
Brian Salomon29b60c92017-10-31 14:42:10 -040034 ~GrOpFlushState() final { this->reset(); }
bsalomon75398562015-08-17 12:55:38 -070035
Brian Salomon742e31d2016-12-07 17:06:19 -050036 /** 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 -040037 executed. */
38 void preExecuteDraws();
bsalomon75398562015-08-17 12:55:38 -070039
Brian Salomon943ed792017-10-30 09:37:55 -040040 void doUpload(GrDeferredTextureUploadFn&);
bsalomon342bfc22016-04-01 06:06:20 -070041
Brian Salomon7dc6e752017-11-02 11:34:51 -040042 /** Called as ops are executed. Must be called in the same order as the ops were prepared. */
Brian Osmane8012102018-11-29 14:05:07 -050043 void executeDrawsAndUploadsForMeshDrawOp(const GrOp* op, const SkRect& opBounds);
Brian Salomon7dc6e752017-11-02 11:34:51 -040044
egdaniel9cb63402016-06-23 08:37:05 -070045 GrGpuCommandBuffer* commandBuffer() { return fCommandBuffer; }
Greg Daniel500d58b2017-08-24 15:59:33 -040046 // Helper function used by Ops that are only called via RenderTargetOpLists
47 GrGpuRTCommandBuffer* rtCommandBuffer();
egdaniel9cb63402016-06-23 08:37:05 -070048 void setCommandBuffer(GrGpuCommandBuffer* buffer) { fCommandBuffer = buffer; }
49
bsalomon75398562015-08-17 12:55:38 -070050 GrGpu* gpu() { return fGpu; }
51
Brian Salomon7dc6e752017-11-02 11:34:51 -040052 void reset();
joshualittf6d259b2015-10-02 11:27:14 -070053
Brian Salomon29b60c92017-10-31 14:42:10 -040054 /** Additional data required on a per-op basis when executing GrOps. */
55 struct OpArgs {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040056 GrRenderTarget* renderTarget() const { return fProxy->peekRenderTarget(); }
Robert Phillips2890fbf2017-07-26 15:48:41 -040057
Brian Salomon29b60c92017-10-31 14:42:10 -040058 GrOp* fOp;
Robert Phillips19e51dc2017-08-09 09:30:51 -040059 // TODO: do we still need the dst proxy here?
Brian Salomonbfd18cd2017-08-09 16:27:09 -040060 GrRenderTargetProxy* fProxy;
61 GrAppliedClip* fAppliedClip;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040062 GrXferProcessor::DstProxy fDstProxy;
Brian Salomon54d212e2017-03-21 14:22:38 -040063 };
64
Brian Salomon29b60c92017-10-31 14:42:10 -040065 void setOpArgs(OpArgs* opArgs) { fOpArgs = opArgs; }
Brian Salomon54d212e2017-03-21 14:22:38 -040066
Brian Salomon29b60c92017-10-31 14:42:10 -040067 const OpArgs& drawOpArgs() const {
Brian Salomon54d212e2017-03-21 14:22:38 -040068 SkASSERT(fOpArgs);
Brian Salomon29b60c92017-10-31 14:42:10 -040069 SkASSERT(fOpArgs->fOp);
Brian Salomon54d212e2017-03-21 14:22:38 -040070 return *fOpArgs;
71 }
72
Brian Salomon29b60c92017-10-31 14:42:10 -040073 /** Overrides of GrDeferredUploadTarget. */
Brian Salomonbfd18cd2017-08-09 16:27:09 -040074
Robert Phillips40a29d72018-01-18 12:59:22 -050075 const GrTokenTracker* tokenTracker() final { return fTokenTracker; }
Brian Salomon29b60c92017-10-31 14:42:10 -040076 GrDeferredUploadToken addInlineUpload(GrDeferredTextureUploadFn&&) final;
77 GrDeferredUploadToken addASAPUpload(GrDeferredTextureUploadFn&&) final;
Brian Salomon29b60c92017-10-31 14:42:10 -040078
79 /** Overrides of GrMeshDrawOp::Target. */
Brian Salomon7eae3e02018-08-07 14:02:38 +000080 void draw(sk_sp<const GrGeometryProcessor>,
81 const GrPipeline*,
82 const GrPipeline::FixedDynamicState*,
Brian Salomonf7232642018-09-19 08:58:08 -040083 const GrPipeline::DynamicStateArrays*,
Brian Salomon7eae3e02018-08-07 14:02:38 +000084 const GrMesh[],
Brian Salomonf7232642018-09-19 08:58:08 -040085 int meshCnt) final;
Brian Salomon29b60c92017-10-31 14:42:10 -040086 void* makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer**,
87 int* startVertex) final;
88 uint16_t* makeIndexSpace(int indexCount, const GrBuffer**, int* startIndex) final;
89 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
90 const GrBuffer**, int* startVertex, int* actualVertexCount) final;
91 uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, const GrBuffer**,
92 int* startIndex, int* actualIndexCount) final;
93 void putBackIndices(int indexCount) final;
94 void putBackVertices(int vertices, size_t vertexStride) final;
95 GrRenderTargetProxy* proxy() const final { return fOpArgs->fProxy; }
96 GrAppliedClip detachAppliedClip() final;
97 const GrXferProcessor::DstProxy& dstProxy() const final { return fOpArgs->fDstProxy; }
98 GrDeferredUploadTarget* deferredUploadTarget() final { return this; }
99 const GrCaps& caps() const final;
100 GrResourceProvider* resourceProvider() const final { return fResourceProvider; }
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400101
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500102 GrGlyphCache* glyphCache() const final;
103
104 // At this point we know we're flushing so full access to the GrAtlasManager is required (and
105 // permissible).
Robert Phillips5a66efb2018-03-07 15:13:18 -0500106 GrAtlasManager* atlasManager() const final;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500107
Brian Salomon967df202018-12-07 11:15:53 -0500108 GrDeinstantiateProxyTracker* deinstantiateProxyTracker() { return &fDeinstantiateProxyTracker; }
Greg Daniel4684f822018-03-08 15:27:36 -0500109
bsalomon75398562015-08-17 12:55:38 -0700110private:
Brian Salomon29b60c92017-10-31 14:42:10 -0400111 /** GrMeshDrawOp::Target override. */
Brian Salomon7dc6e752017-11-02 11:34:51 -0400112 SkArenaAlloc* pipelineArena() override { return &fArena; }
113
114 struct InlineUpload {
115 InlineUpload(GrDeferredTextureUploadFn&& upload, GrDeferredUploadToken token)
116 : fUpload(std::move(upload)), fUploadBeforeToken(token) {}
117 GrDeferredTextureUploadFn fUpload;
118 GrDeferredUploadToken fUploadBeforeToken;
119 };
120
121 // A set of contiguous draws that share a draw token, geometry processor, and pipeline. The
122 // meshes for the draw are stored in the fMeshes array. The reason for coalescing meshes
123 // that share a geometry processor into a Draw is that it allows the Gpu object to setup
124 // the shared state once and then issue draws for each mesh.
125 struct Draw {
Brian Salomonf7232642018-09-19 08:58:08 -0400126 ~Draw();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000127 sk_sp<const GrGeometryProcessor> fGeometryProcessor;
128 const GrPipeline* fPipeline = nullptr;
Brian Salomon49348902018-06-26 09:12:38 -0400129 const GrPipeline::FixedDynamicState* fFixedDynamicState;
130 const GrPipeline::DynamicStateArrays* fDynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000131 const GrMesh* fMeshes = nullptr;
Brian Osmane8012102018-11-29 14:05:07 -0500132 const GrOp* fOp = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000133 int fMeshCnt = 0;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400134 };
135
Brian Salomon7dc6e752017-11-02 11:34:51 -0400136 // Storage for ops' pipelines, draws, and inline uploads.
137 SkArenaAlloc fArena{sizeof(GrPipeline) * 100};
138
139 // Store vertex and index data on behalf of ops that are flushed.
140 GrVertexBufferAllocPool fVertexPool;
141 GrIndexBufferAllocPool fIndexPool;
142
143 // Data stored on behalf of the ops being flushed.
Robert Phillips40a29d72018-01-18 12:59:22 -0500144 SkArenaAllocList<GrDeferredTextureUploadFn> fASAPUploads;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500145 SkArenaAllocList<InlineUpload> fInlineUploads;
146 SkArenaAllocList<Draw> fDraws;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400147
148 // All draws we store have an implicit draw token. This is the draw token for the first draw
149 // in fDraws.
150 GrDeferredUploadToken fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
151
152 // Info about the op that is currently preparing or executing using the flush state or null if
153 // an op is not currently preparing of executing.
154 OpArgs* fOpArgs = nullptr;
Brian Salomon29b60c92017-10-31 14:42:10 -0400155
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400156 GrGpu* fGpu;
157 GrResourceProvider* fResourceProvider;
Robert Phillips40a29d72018-01-18 12:59:22 -0500158 GrTokenTracker* fTokenTracker;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400159 GrGpuCommandBuffer* fCommandBuffer = nullptr;
160
161 // Variables that are used to track where we are in lists as ops are executed
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500162 SkArenaAllocList<Draw>::Iter fCurrDraw;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500163 SkArenaAllocList<InlineUpload>::Iter fCurrUpload;
Greg Daniel4684f822018-03-08 15:27:36 -0500164
Brian Salomon967df202018-12-07 11:15:53 -0500165 // Used to track the proxies that need to be deinstantiated after we finish a flush
166 GrDeinstantiateProxyTracker fDeinstantiateProxyTracker;
bsalomon75398562015-08-17 12:55:38 -0700167};
168
bsalomon75398562015-08-17 12:55:38 -0700169#endif