blob: 0b0d9ba5cd4724b6f3ad84b505bc73990daf76c6 [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 Salomon742e31d2016-12-07 17:06:19 -05008#include "GrOpFlushState.h"
bsalomon75398562015-08-17 12:55:38 -07009
Robert Phillipsc4039ea2018-03-01 11:36:45 -050010#include "GrContextPriv.h"
Brian Salomon903da792016-12-16 14:24:46 -050011#include "GrDrawOpAtlas.h"
Robert Phillips646e4292017-06-13 12:44:56 -040012#include "GrGpu.h"
Brian Salomon9bada542017-06-12 12:09:30 -040013#include "GrResourceProvider.h"
Robert Phillips646e4292017-06-13 12:44:56 -040014#include "GrTexture.h"
bsalomon75398562015-08-17 12:55:38 -070015
Brian Salomon7dc6e752017-11-02 11:34:51 -040016//////////////////////////////////////////////////////////////////////////////
17
Brian Salomon58f153c2018-10-18 21:51:15 -040018GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider,
Brian Salomon2c791fc2019-04-02 11:52:03 -040019 GrResourceCache* cache, GrTokenTracker* tokenTracker,
Brian Salomon601ac802019-02-07 13:37:16 -050020 sk_sp<GrBufferAllocPool::CpuBufferCache> cpuBufferCache)
21 : fVertexPool(gpu, cpuBufferCache)
22 , fIndexPool(gpu, std::move(cpuBufferCache))
Robert Phillips40a29d72018-01-18 12:59:22 -050023 , fGpu(gpu)
24 , fResourceProvider(resourceProvider)
Brian Salomon2c791fc2019-04-02 11:52:03 -040025 , fTokenTracker(tokenTracker)
26 , fDeinstantiateProxyTracker(cache) {}
bsalomon75398562015-08-17 12:55:38 -070027
Robert Phillips646e4292017-06-13 12:44:56 -040028const GrCaps& GrOpFlushState::caps() const {
29 return *fGpu->caps();
30}
31
Greg Daniel500d58b2017-08-24 15:59:33 -040032GrGpuRTCommandBuffer* GrOpFlushState::rtCommandBuffer() {
33 return fCommandBuffer->asRTCommandBuffer();
34}
35
Chris Dalton07cdcfc92019-02-26 11:13:22 -070036void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
37 const GrOp* op, const SkRect& chainBounds, GrProcessorSet&& processorSet,
Chris Daltonbaa1b352019-04-03 12:03:00 -060038 GrPipeline::InputFlags pipelineFlags, const GrUserStencilSettings* stencilSettings) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040039 SkASSERT(this->rtCommandBuffer());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070040
41 GrPipeline::InitArgs pipelineArgs;
Chris Daltonbaa1b352019-04-03 12:03:00 -060042 pipelineArgs.fInputFlags = pipelineFlags;
Chris Dalton07cdcfc92019-02-26 11:13:22 -070043 pipelineArgs.fDstProxy = this->dstProxy();
44 pipelineArgs.fCaps = &this->caps();
45 pipelineArgs.fResourceProvider = this->resourceProvider();
46 pipelineArgs.fUserStencil = stencilSettings;
47 GrPipeline pipeline(pipelineArgs, std::move(processorSet), this->detachAppliedClip());
48
Brian Osmane8012102018-11-29 14:05:07 -050049 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050050 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040051 while (fCurrUpload != fInlineUploads.end() &&
52 fCurrUpload->fUploadBeforeToken == drawToken) {
53 this->rtCommandBuffer()->inlineUpload(this, fCurrUpload->fUpload);
54 ++fCurrUpload;
55 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -070056 this->rtCommandBuffer()->draw(
57 *fCurrDraw->fGeometryProcessor, pipeline, fCurrDraw->fFixedDynamicState,
58 fCurrDraw->fDynamicStateArrays, fCurrDraw->fMeshes, fCurrDraw->fMeshCnt,
59 chainBounds);
Robert Phillips40a29d72018-01-18 12:59:22 -050060 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040061 ++fCurrDraw;
62 }
63}
64
65void GrOpFlushState::preExecuteDraws() {
66 fVertexPool.unmap();
67 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050068 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040069 this->doUpload(upload);
70 }
71 // Setup execution iterators.
72 fCurrDraw = fDraws.begin();
73 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040074}
75
76void GrOpFlushState::reset() {
77 SkASSERT(fCurrDraw == fDraws.end());
78 SkASSERT(fCurrUpload == fInlineUploads.end());
79 fVertexPool.reset();
80 fIndexPool.reset();
81 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050082 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040083 fInlineUploads.reset();
84 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040085 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
86}
87
Brian Salomon943ed792017-10-30 09:37:55 -040088void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload) {
Robert Phillips88260b52018-01-19 12:56:09 -050089 GrDeferredTextureUploadWritePixelsFn wp = [this](GrTextureProxy* dstProxy, int left, int top,
Brian Salomonc320b152018-02-20 14:05:36 -050090 int width, int height,
91 GrColorType srcColorType, const void* buffer,
92 size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040093 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon0ba9fa02018-06-01 11:55:08 -040094 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface) &&
95 fGpu->caps()->supportedWritePixelsColorType(dstSurface->config(), srcColorType) != srcColorType) {
Robert Phillips88260b52018-01-19 12:56:09 -050096 return false;
97 }
Brian Salomona9b04b92018-06-01 15:04:28 -040098 return this->fGpu->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
99 rowBytes);
Brian Salomon9bada542017-06-12 12:09:30 -0400100 };
101 upload(wp);
102}
Brian Salomon29b60c92017-10-31 14:42:10 -0400103
104GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500105 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400106 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400107}
108
109GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500110 fASAPUploads.append(&fArena, std::move(upload));
111 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400112}
113
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700114void GrOpFlushState::recordDraw(
115 sk_sp<const GrGeometryProcessor> gp, const GrMesh meshes[], int meshCnt,
116 const GrPipeline::FixedDynamicState* fixedDynamicState,
117 const GrPipeline::DynamicStateArrays* dynamicStateArrays) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400118 SkASSERT(fOpArgs);
119 SkASSERT(fOpArgs->fOp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400120 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400121 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500122 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomonf7232642018-09-19 08:58:08 -0400123 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
124 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
125 fixedDynamicState->fPrimitiveProcessorTextures[i]->addPendingRead();
126 }
127 }
128 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
129 int n = gp->numTextureSamplers() * meshCnt;
130 for (int i = 0; i < n; ++i) {
131 dynamicStateArrays->fPrimitiveProcessorTextures[i]->addPendingRead();
132 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000133 }
134 draw.fGeometryProcessor = std::move(gp);
Brian Salomon49348902018-06-26 09:12:38 -0400135 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400136 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000137 draw.fMeshes = meshes;
138 draw.fMeshCnt = meshCnt;
Brian Osmane8012102018-11-29 14:05:07 -0500139 draw.fOp = fOpArgs->fOp;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400140 if (firstDraw) {
141 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400142 }
143}
144
Brian Salomon12d22642019-01-29 14:38:50 -0500145void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
146 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400147 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
148}
149
Brian Salomon12d22642019-01-29 14:38:50 -0500150uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
151 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400152 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
153}
154
155void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500156 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400157 int* startVertex, int* actualVertexCount) {
158 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
159 startVertex, actualVertexCount);
160}
161
162uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500163 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400164 int* actualIndexCount) {
165 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
166 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
167}
168
169void GrOpFlushState::putBackIndices(int indexCount) {
170 fIndexPool.putBack(indexCount * sizeof(uint16_t));
171}
172
173void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
174 fVertexPool.putBack(vertices * vertexStride);
175}
176
177GrAppliedClip GrOpFlushState::detachAppliedClip() {
178 return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip();
179}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500180
Herb Derby081e6f32019-01-16 13:46:02 -0500181GrStrikeCache* GrOpFlushState::glyphCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500182 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500183}
184
Robert Phillips5a66efb2018-03-07 15:13:18 -0500185GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500186 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500187}
Brian Salomonf7232642018-09-19 08:58:08 -0400188
189//////////////////////////////////////////////////////////////////////////////
190
191GrOpFlushState::Draw::~Draw() {
192 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
193 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
194 fFixedDynamicState->fPrimitiveProcessorTextures[i]->completedRead();
195 }
196 }
197 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
198 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
199 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
200 for (int i = 0; i < n; ++i) {
201 textures[i]->completedRead();
202 }
203 }
204}