blob: a6f0c26e5ef87efd768a8c4f3088ac558bfc257c [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 {
Robert Phillips2890fbf2017-07-26 15:48:41 -040052 GrRenderTarget* renderTarget() const { return fProxy->priv().peekRenderTarget(); }
53
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. */
76
77 void draw(const GrGeometryProcessor*, const GrPipeline*, const GrMesh&) final;
78 void* makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer**,
79 int* startVertex) final;
80 uint16_t* makeIndexSpace(int indexCount, const GrBuffer**, int* startIndex) final;
81 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
82 const GrBuffer**, int* startVertex, int* actualVertexCount) final;
83 uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, const GrBuffer**,
84 int* startIndex, int* actualIndexCount) final;
85 void putBackIndices(int indexCount) final;
86 void putBackVertices(int vertices, size_t vertexStride) final;
87 GrRenderTargetProxy* proxy() const final { return fOpArgs->fProxy; }
88 GrAppliedClip detachAppliedClip() final;
89 const GrXferProcessor::DstProxy& dstProxy() const final { return fOpArgs->fDstProxy; }
90 GrDeferredUploadTarget* deferredUploadTarget() final { return this; }
91 const GrCaps& caps() const final;
92 GrResourceProvider* resourceProvider() const final { return fResourceProvider; }
Brian Salomon6d4b65e2017-05-03 17:06:09 -040093
Robert Phillipsc4039ea2018-03-01 11:36:45 -050094 GrGlyphCache* glyphCache() const final;
95
96 // At this point we know we're flushing so full access to the GrAtlasManager is required (and
97 // permissible).
Robert Phillips5a66efb2018-03-07 15:13:18 -050098 GrAtlasManager* atlasManager() const final;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050099
Greg Daniel4684f822018-03-08 15:27:36 -0500100 GrUninstantiateProxyTracker* uninstantiateProxyTracker() {
101 return &fUninstantiateProxyTracker;
102 }
103
bsalomon75398562015-08-17 12:55:38 -0700104private:
Brian Salomon29b60c92017-10-31 14:42:10 -0400105 /** GrMeshDrawOp::Target override. */
Brian Salomon7dc6e752017-11-02 11:34:51 -0400106 SkArenaAlloc* pipelineArena() override { return &fArena; }
107
108 struct InlineUpload {
109 InlineUpload(GrDeferredTextureUploadFn&& upload, GrDeferredUploadToken token)
110 : fUpload(std::move(upload)), fUploadBeforeToken(token) {}
111 GrDeferredTextureUploadFn fUpload;
112 GrDeferredUploadToken fUploadBeforeToken;
113 };
114
115 // A set of contiguous draws that share a draw token, geometry processor, and pipeline. The
116 // meshes for the draw are stored in the fMeshes array. The reason for coalescing meshes
117 // that share a geometry processor into a Draw is that it allows the Gpu object to setup
118 // the shared state once and then issue draws for each mesh.
119 struct Draw {
120 int fMeshCnt = 0;
121 GrPendingProgramElement<const GrGeometryProcessor> fGeometryProcessor;
122 const GrPipeline* fPipeline;
123 uint32_t fOpID;
124 };
125
Brian Salomon7dc6e752017-11-02 11:34:51 -0400126 // Storage for ops' pipelines, draws, and inline uploads.
127 SkArenaAlloc fArena{sizeof(GrPipeline) * 100};
128
129 // Store vertex and index data on behalf of ops that are flushed.
130 GrVertexBufferAllocPool fVertexPool;
131 GrIndexBufferAllocPool fIndexPool;
132
133 // Data stored on behalf of the ops being flushed.
Robert Phillips40a29d72018-01-18 12:59:22 -0500134 SkArenaAllocList<GrDeferredTextureUploadFn> fASAPUploads;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500135 SkArenaAllocList<InlineUpload> fInlineUploads;
136 SkArenaAllocList<Draw> fDraws;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400137 // TODO: These should go in the arena. However, GrGpuCommandBuffer and other classes currently
138 // accept contiguous arrays of meshes.
139 SkSTArray<16, GrMesh> fMeshes;
140
141 // All draws we store have an implicit draw token. This is the draw token for the first draw
142 // in fDraws.
143 GrDeferredUploadToken fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
144
145 // Info about the op that is currently preparing or executing using the flush state or null if
146 // an op is not currently preparing of executing.
147 OpArgs* fOpArgs = nullptr;
Brian Salomon29b60c92017-10-31 14:42:10 -0400148
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400149 GrGpu* fGpu;
150 GrResourceProvider* fResourceProvider;
Robert Phillips40a29d72018-01-18 12:59:22 -0500151 GrTokenTracker* fTokenTracker;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400152 GrGpuCommandBuffer* fCommandBuffer = nullptr;
153
154 // Variables that are used to track where we are in lists as ops are executed
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500155 SkArenaAllocList<Draw>::Iter fCurrDraw;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400156 int fCurrMesh;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500157 SkArenaAllocList<InlineUpload>::Iter fCurrUpload;
Greg Daniel4684f822018-03-08 15:27:36 -0500158
159 // Used to track the proxies that need to be uninstantiated after we finish a flush
160 GrUninstantiateProxyTracker fUninstantiateProxyTracker;
bsalomon75398562015-08-17 12:55:38 -0700161};
162
bsalomon75398562015-08-17 12:55:38 -0700163#endif