blob: a1d92294add721dd077bfb75f450092c5b44989c [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
Robert Phillips40a29d72018-01-18 12:59:22 -050018GrOpFlushState::GrOpFlushState(GrGpu* gpu,
19 GrResourceProvider* resourceProvider,
20 GrTokenTracker* tokenTracker)
21 : fVertexPool(gpu)
22 , fIndexPool(gpu)
23 , fGpu(gpu)
24 , fResourceProvider(resourceProvider)
25 , fTokenTracker(tokenTracker) {
26}
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
Brian Salomon7dc6e752017-11-02 11:34:51 -040036void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(uint32_t opID, const SkRect& opBounds) {
37 SkASSERT(this->rtCommandBuffer());
38 while (fCurrDraw != fDraws.end() && fCurrDraw->fOpID == opID) {
Robert Phillips40a29d72018-01-18 12:59:22 -050039 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040040 while (fCurrUpload != fInlineUploads.end() &&
41 fCurrUpload->fUploadBeforeToken == drawToken) {
42 this->rtCommandBuffer()->inlineUpload(this, fCurrUpload->fUpload);
43 ++fCurrUpload;
44 }
45 SkASSERT(fCurrDraw->fPipeline->proxy() == this->drawOpArgs().fProxy);
Brian Salomonff168d92018-06-23 15:17:27 -040046 this->rtCommandBuffer()->draw(*fCurrDraw->fGeometryProcessor, *fCurrDraw->fPipeline,
Brian Salomon49348902018-06-26 09:12:38 -040047 fCurrDraw->fFixedDynamicState, fCurrDraw->fDynamicStateArrays,
Brian Salomon7eae3e02018-08-07 14:02:38 +000048 fCurrDraw->fMeshes, fCurrDraw->fMeshCnt, opBounds);
Robert Phillips40a29d72018-01-18 12:59:22 -050049 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040050 ++fCurrDraw;
51 }
52}
53
54void GrOpFlushState::preExecuteDraws() {
55 fVertexPool.unmap();
56 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050057 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040058 this->doUpload(upload);
59 }
60 // Setup execution iterators.
61 fCurrDraw = fDraws.begin();
62 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040063}
64
65void GrOpFlushState::reset() {
66 SkASSERT(fCurrDraw == fDraws.end());
67 SkASSERT(fCurrUpload == fInlineUploads.end());
68 fVertexPool.reset();
69 fIndexPool.reset();
70 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050071 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040072 fInlineUploads.reset();
73 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040074 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
75}
76
Brian Salomon943ed792017-10-30 09:37:55 -040077void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload) {
Robert Phillips88260b52018-01-19 12:56:09 -050078 GrDeferredTextureUploadWritePixelsFn wp = [this](GrTextureProxy* dstProxy, int left, int top,
Brian Salomonc320b152018-02-20 14:05:36 -050079 int width, int height,
80 GrColorType srcColorType, const void* buffer,
81 size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040082 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon0ba9fa02018-06-01 11:55:08 -040083 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface) &&
84 fGpu->caps()->supportedWritePixelsColorType(dstSurface->config(), srcColorType) != srcColorType) {
Robert Phillips88260b52018-01-19 12:56:09 -050085 return false;
86 }
Brian Salomona9b04b92018-06-01 15:04:28 -040087 return this->fGpu->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
88 rowBytes);
Brian Salomon9bada542017-06-12 12:09:30 -040089 };
90 upload(wp);
91}
Brian Salomon29b60c92017-10-31 14:42:10 -040092
93GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -050094 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -040095 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -040096}
97
98GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -050099 fASAPUploads.append(&fArena, std::move(upload));
100 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400101}
102
Brian Salomon7eae3e02018-08-07 14:02:38 +0000103void GrOpFlushState::draw(sk_sp<const GrGeometryProcessor> gp, const GrPipeline* pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400104 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomon2d0a6a12018-08-22 15:22:24 +0000105 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000106 const GrMesh meshes[], int meshCnt) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400107 SkASSERT(fOpArgs);
108 SkASSERT(fOpArgs->fOp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400109 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400110 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500111 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomon2d0a6a12018-08-22 15:22:24 +0000112 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
113 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
114 fixedDynamicState->fPrimitiveProcessorTextures[i]->addPendingRead();
115 }
116 }
117 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
118 int n = gp->numTextureSamplers() * meshCnt;
119 for (int i = 0; i < n; ++i) {
120 dynamicStateArrays->fPrimitiveProcessorTextures[i]->addPendingRead();
121 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000122 }
123 draw.fGeometryProcessor = std::move(gp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400124 draw.fPipeline = pipeline;
Brian Salomon49348902018-06-26 09:12:38 -0400125 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomon2d0a6a12018-08-22 15:22:24 +0000126 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000127 draw.fMeshes = meshes;
128 draw.fMeshCnt = meshCnt;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400129 draw.fOpID = fOpArgs->fOp->uniqueID();
130 if (firstDraw) {
131 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400132 }
133}
134
135void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer** buffer,
136 int* startVertex) {
137 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
138}
139
140uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex) {
141 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
142}
143
144void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
145 int fallbackVertexCount, const GrBuffer** buffer,
146 int* startVertex, int* actualVertexCount) {
147 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
148 startVertex, actualVertexCount);
149}
150
151uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
152 const GrBuffer** buffer, int* startIndex,
153 int* actualIndexCount) {
154 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
155 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
156}
157
158void GrOpFlushState::putBackIndices(int indexCount) {
159 fIndexPool.putBack(indexCount * sizeof(uint16_t));
160}
161
162void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
163 fVertexPool.putBack(vertices * vertexStride);
164}
165
166GrAppliedClip GrOpFlushState::detachAppliedClip() {
167 return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip();
168}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500169
170GrGlyphCache* GrOpFlushState::glyphCache() const {
171 return fGpu->getContext()->contextPriv().getGlyphCache();
172}
173
Robert Phillips5a66efb2018-03-07 15:13:18 -0500174GrAtlasManager* GrOpFlushState::atlasManager() const {
175 return fGpu->getContext()->contextPriv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500176}
Brian Salomon2d0a6a12018-08-22 15:22:24 +0000177
178//////////////////////////////////////////////////////////////////////////////
179
180GrOpFlushState::Draw::~Draw() {
181 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
182 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
183 fFixedDynamicState->fPrimitiveProcessorTextures[i]->completedRead();
184 }
185 }
186 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
187 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
188 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
189 for (int i = 0; i < n; ++i) {
190 textures[i]->completedRead();
191 }
192 }
193}