blob: a990553cbc1aa51b43cf350acf5abc165d259774 [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>
Ben Wagner729a23f2019-05-17 16:29:34 -040012#include "src/core/SkArenaAlloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/core/SkArenaAllocList.h"
14#include "src/gpu/GrAppliedClip.h"
15#include "src/gpu/GrBufferAllocPool.h"
16#include "src/gpu/GrDeferredUpload.h"
17#include "src/gpu/GrDeinstantiateProxyTracker.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrRenderTargetProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/ops/GrMeshDrawOp.h"
bsalomon75398562015-08-17 12:55:38 -070020
Robert Phillips646e4292017-06-13 12:44:56 -040021class GrGpu;
egdaniel9cb63402016-06-23 08:37:05 -070022class GrGpuCommandBuffer;
Greg Daniel500d58b2017-08-24 15:59:33 -040023class GrGpuRTCommandBuffer;
bsalomon75398562015-08-17 12:55:38 -070024class GrResourceProvider;
25
Brian Salomon742e31d2016-12-07 17:06:19 -050026/** Tracks the state across all the GrOps (really just the GrDrawOps) in a GrOpList flush. */
Brian Salomon29b60c92017-10-31 14:42:10 -040027class GrOpFlushState final : public GrDeferredUploadTarget, public GrMeshDrawOp::Target {
bsalomon75398562015-08-17 12:55:38 -070028public:
Brian Salomon58f153c2018-10-18 21:51:15 -040029 // 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.
Robert Phillipse5f73282019-06-18 17:15:04 -040032 GrOpFlushState(GrGpu*, GrResourceProvider*, GrTokenTracker*,
Brian Salomon601ac802019-02-07 13:37:16 -050033 sk_sp<GrBufferAllocPool::CpuBufferCache> = nullptr);
bsalomon75398562015-08-17 12:55:38 -070034
Brian Salomon29b60c92017-10-31 14:42:10 -040035 ~GrOpFlushState() final { this->reset(); }
bsalomon75398562015-08-17 12:55:38 -070036
Brian Salomon742e31d2016-12-07 17:06:19 -050037 /** 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 -040038 executed. */
39 void preExecuteDraws();
bsalomon75398562015-08-17 12:55:38 -070040
Brian Salomon943ed792017-10-30 09:37:55 -040041 void doUpload(GrDeferredTextureUploadFn&);
bsalomon342bfc22016-04-01 06:06:20 -070042
Brian Salomon7dc6e752017-11-02 11:34:51 -040043 /** Called as ops are executed. Must be called in the same order as the ops were prepared. */
Chris Dalton07cdcfc92019-02-26 11:13:22 -070044 void executeDrawsAndUploadsForMeshDrawOp(
Chris Daltonbaa1b352019-04-03 12:03:00 -060045 const GrOp* op, const SkRect& chainBounds, GrProcessorSet&&,
46 GrPipeline::InputFlags = GrPipeline::InputFlags::kNone,
Chris Dalton07cdcfc92019-02-26 11:13:22 -070047 const GrUserStencilSettings* = &GrUserStencilSettings::kUnused);
Brian Salomon7dc6e752017-11-02 11:34:51 -040048
egdaniel9cb63402016-06-23 08:37:05 -070049 GrGpuCommandBuffer* commandBuffer() { return fCommandBuffer; }
Greg Daniel500d58b2017-08-24 15:59:33 -040050 // Helper function used by Ops that are only called via RenderTargetOpLists
51 GrGpuRTCommandBuffer* rtCommandBuffer();
egdaniel9cb63402016-06-23 08:37:05 -070052 void setCommandBuffer(GrGpuCommandBuffer* buffer) { fCommandBuffer = buffer; }
53
bsalomon75398562015-08-17 12:55:38 -070054 GrGpu* gpu() { return fGpu; }
55
Brian Salomon7dc6e752017-11-02 11:34:51 -040056 void reset();
joshualittf6d259b2015-10-02 11:27:14 -070057
Brian Salomon29b60c92017-10-31 14:42:10 -040058 /** Additional data required on a per-op basis when executing GrOps. */
59 struct OpArgs {
Robert Phillipsd0fe8752019-01-31 14:13:59 -050060 GrSurfaceOrigin origin() const { return fProxy->origin(); }
Brian Salomonfd98c2c2018-07-31 17:25:29 -040061 GrRenderTarget* renderTarget() const { return fProxy->peekRenderTarget(); }
Robert Phillips2890fbf2017-07-26 15:48:41 -040062
Brian Salomon29b60c92017-10-31 14:42:10 -040063 GrOp* fOp;
Robert Phillips19e51dc2017-08-09 09:30:51 -040064 // TODO: do we still need the dst proxy here?
Brian Salomonbfd18cd2017-08-09 16:27:09 -040065 GrRenderTargetProxy* fProxy;
66 GrAppliedClip* fAppliedClip;
Greg Daniel2c3398d2019-06-19 11:58:01 -040067 GrSwizzle fOutputSwizzle;
Robert Phillipsbb581ce2017-05-29 15:05:15 -040068 GrXferProcessor::DstProxy fDstProxy;
Brian Salomon54d212e2017-03-21 14:22:38 -040069 };
70
Brian Salomon29b60c92017-10-31 14:42:10 -040071 void setOpArgs(OpArgs* opArgs) { fOpArgs = opArgs; }
Brian Salomon54d212e2017-03-21 14:22:38 -040072
Brian Salomon29b60c92017-10-31 14:42:10 -040073 const OpArgs& drawOpArgs() const {
Brian Salomon54d212e2017-03-21 14:22:38 -040074 SkASSERT(fOpArgs);
Brian Salomon29b60c92017-10-31 14:42:10 -040075 SkASSERT(fOpArgs->fOp);
Brian Salomon54d212e2017-03-21 14:22:38 -040076 return *fOpArgs;
77 }
78
Brian Salomon29b60c92017-10-31 14:42:10 -040079 /** Overrides of GrDeferredUploadTarget. */
Brian Salomonbfd18cd2017-08-09 16:27:09 -040080
Robert Phillips40a29d72018-01-18 12:59:22 -050081 const GrTokenTracker* tokenTracker() final { return fTokenTracker; }
Brian Salomon29b60c92017-10-31 14:42:10 -040082 GrDeferredUploadToken addInlineUpload(GrDeferredTextureUploadFn&&) final;
83 GrDeferredUploadToken addASAPUpload(GrDeferredTextureUploadFn&&) final;
Brian Salomon29b60c92017-10-31 14:42:10 -040084
85 /** Overrides of GrMeshDrawOp::Target. */
Chris Dalton07cdcfc92019-02-26 11:13:22 -070086 void recordDraw(
87 sk_sp<const GrGeometryProcessor>, const GrMesh[], int meshCnt,
88 const GrPipeline::FixedDynamicState*, const GrPipeline::DynamicStateArrays*) final;
Brian Salomon12d22642019-01-29 14:38:50 -050089 void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*,
Brian Salomon29b60c92017-10-31 14:42:10 -040090 int* startVertex) final;
Brian Salomon12d22642019-01-29 14:38:50 -050091 uint16_t* makeIndexSpace(int indexCount, sk_sp<const GrBuffer>*, int* startIndex) final;
Brian Salomon29b60c92017-10-31 14:42:10 -040092 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -050093 sk_sp<const GrBuffer>*, int* startVertex,
94 int* actualVertexCount) final;
95 uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
96 sk_sp<const GrBuffer>*, int* startIndex,
97 int* actualIndexCount) final;
Brian Salomon29b60c92017-10-31 14:42:10 -040098 void putBackIndices(int indexCount) final;
99 void putBackVertices(int vertices, size_t vertexStride) final;
100 GrRenderTargetProxy* proxy() const final { return fOpArgs->fProxy; }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700101 const GrAppliedClip* appliedClip() final { return fOpArgs->fAppliedClip; }
Brian Salomon29b60c92017-10-31 14:42:10 -0400102 GrAppliedClip detachAppliedClip() final;
103 const GrXferProcessor::DstProxy& dstProxy() const final { return fOpArgs->fDstProxy; }
104 GrDeferredUploadTarget* deferredUploadTarget() final { return this; }
105 const GrCaps& caps() const final;
106 GrResourceProvider* resourceProvider() const final { return fResourceProvider; }
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400107
Herb Derby081e6f32019-01-16 13:46:02 -0500108 GrStrikeCache* glyphCache() const final;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500109
110 // At this point we know we're flushing so full access to the GrAtlasManager is required (and
111 // permissible).
Robert Phillips5a66efb2018-03-07 15:13:18 -0500112 GrAtlasManager* atlasManager() const final;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500113
Brian Salomon876a0172019-03-08 11:12:14 -0500114 GrDeinstantiateProxyTracker* deinstantiateProxyTracker() { return &fDeinstantiateProxyTracker; }
115
bsalomon75398562015-08-17 12:55:38 -0700116private:
Brian Salomon29b60c92017-10-31 14:42:10 -0400117 /** GrMeshDrawOp::Target override. */
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700118 SkArenaAlloc* allocator() override { return &fArena; }
Brian Salomon7dc6e752017-11-02 11:34:51 -0400119
120 struct InlineUpload {
121 InlineUpload(GrDeferredTextureUploadFn&& upload, GrDeferredUploadToken token)
122 : fUpload(std::move(upload)), fUploadBeforeToken(token) {}
123 GrDeferredTextureUploadFn fUpload;
124 GrDeferredUploadToken fUploadBeforeToken;
125 };
126
127 // A set of contiguous draws that share a draw token, geometry processor, and pipeline. The
128 // meshes for the draw are stored in the fMeshes array. The reason for coalescing meshes
129 // that share a geometry processor into a Draw is that it allows the Gpu object to setup
130 // the shared state once and then issue draws for each mesh.
131 struct Draw {
Brian Salomonf7232642018-09-19 08:58:08 -0400132 ~Draw();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000133 sk_sp<const GrGeometryProcessor> fGeometryProcessor;
Brian Salomon49348902018-06-26 09:12:38 -0400134 const GrPipeline::FixedDynamicState* fFixedDynamicState;
135 const GrPipeline::DynamicStateArrays* fDynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000136 const GrMesh* fMeshes = nullptr;
Brian Osmane8012102018-11-29 14:05:07 -0500137 const GrOp* fOp = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000138 int fMeshCnt = 0;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400139 };
140
Brian Salomon7dc6e752017-11-02 11:34:51 -0400141 // Storage for ops' pipelines, draws, and inline uploads.
142 SkArenaAlloc fArena{sizeof(GrPipeline) * 100};
143
144 // Store vertex and index data on behalf of ops that are flushed.
145 GrVertexBufferAllocPool fVertexPool;
146 GrIndexBufferAllocPool fIndexPool;
147
148 // Data stored on behalf of the ops being flushed.
Robert Phillips40a29d72018-01-18 12:59:22 -0500149 SkArenaAllocList<GrDeferredTextureUploadFn> fASAPUploads;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500150 SkArenaAllocList<InlineUpload> fInlineUploads;
151 SkArenaAllocList<Draw> fDraws;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400152
153 // All draws we store have an implicit draw token. This is the draw token for the first draw
154 // in fDraws.
155 GrDeferredUploadToken fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
156
157 // Info about the op that is currently preparing or executing using the flush state or null if
158 // an op is not currently preparing of executing.
159 OpArgs* fOpArgs = nullptr;
Brian Salomon29b60c92017-10-31 14:42:10 -0400160
Brian Salomon6d4b65e2017-05-03 17:06:09 -0400161 GrGpu* fGpu;
162 GrResourceProvider* fResourceProvider;
Robert Phillips40a29d72018-01-18 12:59:22 -0500163 GrTokenTracker* fTokenTracker;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400164 GrGpuCommandBuffer* fCommandBuffer = nullptr;
165
166 // Variables that are used to track where we are in lists as ops are executed
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500167 SkArenaAllocList<Draw>::Iter fCurrDraw;
Brian Salomoncbcb0a12017-11-19 13:20:13 -0500168 SkArenaAllocList<InlineUpload>::Iter fCurrUpload;
Brian Salomon876a0172019-03-08 11:12:14 -0500169
170 // Used to track the proxies that need to be deinstantiated after we finish a flush
171 GrDeinstantiateProxyTracker fDeinstantiateProxyTracker;
bsalomon75398562015-08-17 12:55:38 -0700172};
173
bsalomon75398562015-08-17 12:55:38 -0700174#endif