blob: 7a6aa187664e6bd6896f55f4f569470d9e4f0235 [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 Salomon601ac802019-02-07 13:37:16 -050019 GrTokenTracker* tokenTracker,
20 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 Salomon58f153c2018-10-18 21:51:15 -040025 , fTokenTracker(tokenTracker) {}
bsalomon75398562015-08-17 12:55:38 -070026
Robert Phillips646e4292017-06-13 12:44:56 -040027const GrCaps& GrOpFlushState::caps() const {
28 return *fGpu->caps();
29}
30
Greg Daniel500d58b2017-08-24 15:59:33 -040031GrGpuRTCommandBuffer* GrOpFlushState::rtCommandBuffer() {
32 return fCommandBuffer->asRTCommandBuffer();
33}
34
Chris Dalton07cdcfc92019-02-26 11:13:22 -070035void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
36 const GrOp* op, const SkRect& chainBounds, GrProcessorSet&& processorSet,
37 uint32_t pipelineFlags, const GrUserStencilSettings* stencilSettings) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040038 SkASSERT(this->rtCommandBuffer());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070039
40 GrPipeline::InitArgs pipelineArgs;
41 pipelineArgs.fFlags = pipelineFlags;
42 pipelineArgs.fDstProxy = this->dstProxy();
43 pipelineArgs.fCaps = &this->caps();
44 pipelineArgs.fResourceProvider = this->resourceProvider();
45 pipelineArgs.fUserStencil = stencilSettings;
46 GrPipeline pipeline(pipelineArgs, std::move(processorSet), this->detachAppliedClip());
47
Brian Osmane8012102018-11-29 14:05:07 -050048 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050049 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040050 while (fCurrUpload != fInlineUploads.end() &&
51 fCurrUpload->fUploadBeforeToken == drawToken) {
52 this->rtCommandBuffer()->inlineUpload(this, fCurrUpload->fUpload);
53 ++fCurrUpload;
54 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -070055 this->rtCommandBuffer()->draw(
56 *fCurrDraw->fGeometryProcessor, pipeline, fCurrDraw->fFixedDynamicState,
57 fCurrDraw->fDynamicStateArrays, fCurrDraw->fMeshes, fCurrDraw->fMeshCnt,
58 chainBounds);
Robert Phillips40a29d72018-01-18 12:59:22 -050059 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040060 ++fCurrDraw;
61 }
62}
63
64void GrOpFlushState::preExecuteDraws() {
65 fVertexPool.unmap();
66 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050067 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040068 this->doUpload(upload);
69 }
70 // Setup execution iterators.
71 fCurrDraw = fDraws.begin();
72 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040073}
74
75void GrOpFlushState::reset() {
76 SkASSERT(fCurrDraw == fDraws.end());
77 SkASSERT(fCurrUpload == fInlineUploads.end());
78 fVertexPool.reset();
79 fIndexPool.reset();
80 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050081 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040082 fInlineUploads.reset();
83 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040084 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
85}
86
Brian Salomon943ed792017-10-30 09:37:55 -040087void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload) {
Robert Phillips88260b52018-01-19 12:56:09 -050088 GrDeferredTextureUploadWritePixelsFn wp = [this](GrTextureProxy* dstProxy, int left, int top,
Brian Salomonc320b152018-02-20 14:05:36 -050089 int width, int height,
90 GrColorType srcColorType, const void* buffer,
91 size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040092 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon0ba9fa02018-06-01 11:55:08 -040093 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface) &&
94 fGpu->caps()->supportedWritePixelsColorType(dstSurface->config(), srcColorType) != srcColorType) {
Robert Phillips88260b52018-01-19 12:56:09 -050095 return false;
96 }
Brian Salomona9b04b92018-06-01 15:04:28 -040097 return this->fGpu->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
98 rowBytes);
Brian Salomon9bada542017-06-12 12:09:30 -040099 };
100 upload(wp);
101}
Brian Salomon29b60c92017-10-31 14:42:10 -0400102
103GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500104 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400105 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400106}
107
108GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500109 fASAPUploads.append(&fArena, std::move(upload));
110 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400111}
112
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700113void GrOpFlushState::recordDraw(
114 sk_sp<const GrGeometryProcessor> gp, const GrMesh meshes[], int meshCnt,
115 const GrPipeline::FixedDynamicState* fixedDynamicState,
116 const GrPipeline::DynamicStateArrays* dynamicStateArrays) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400117 SkASSERT(fOpArgs);
118 SkASSERT(fOpArgs->fOp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400119 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400120 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500121 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomonf7232642018-09-19 08:58:08 -0400122 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
123 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
124 fixedDynamicState->fPrimitiveProcessorTextures[i]->addPendingRead();
125 }
126 }
127 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
128 int n = gp->numTextureSamplers() * meshCnt;
129 for (int i = 0; i < n; ++i) {
130 dynamicStateArrays->fPrimitiveProcessorTextures[i]->addPendingRead();
131 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000132 }
133 draw.fGeometryProcessor = std::move(gp);
Brian Salomon49348902018-06-26 09:12:38 -0400134 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400135 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000136 draw.fMeshes = meshes;
137 draw.fMeshCnt = meshCnt;
Brian Osmane8012102018-11-29 14:05:07 -0500138 draw.fOp = fOpArgs->fOp;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400139 if (firstDraw) {
140 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400141 }
142}
143
Brian Salomon12d22642019-01-29 14:38:50 -0500144void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
145 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400146 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
147}
148
Brian Salomon12d22642019-01-29 14:38:50 -0500149uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
150 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400151 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
152}
153
154void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500155 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400156 int* startVertex, int* actualVertexCount) {
157 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
158 startVertex, actualVertexCount);
159}
160
161uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500162 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400163 int* actualIndexCount) {
164 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
165 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
166}
167
168void GrOpFlushState::putBackIndices(int indexCount) {
169 fIndexPool.putBack(indexCount * sizeof(uint16_t));
170}
171
172void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
173 fVertexPool.putBack(vertices * vertexStride);
174}
175
176GrAppliedClip GrOpFlushState::detachAppliedClip() {
177 return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip();
178}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500179
Herb Derby081e6f32019-01-16 13:46:02 -0500180GrStrikeCache* GrOpFlushState::glyphCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500181 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500182}
183
Robert Phillips5a66efb2018-03-07 15:13:18 -0500184GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500185 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500186}
Brian Salomonf7232642018-09-19 08:58:08 -0400187
188//////////////////////////////////////////////////////////////////////////////
189
190GrOpFlushState::Draw::~Draw() {
191 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
192 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
193 fFixedDynamicState->fPrimitiveProcessorTextures[i]->completedRead();
194 }
195 }
196 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
197 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
198 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
199 for (int i = 0; i < n; ++i) {
200 textures[i]->completedRead();
201 }
202 }
203}