blob: 0b27827d108b913281dda9ac8f15c40ce1f7bcbe [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,
19 GrTokenTracker* tokenTracker, void* vertexSpace, void* indexSpace)
20 : fVertexPool(gpu, vertexSpace)
21 , fIndexPool(gpu, indexSpace)
Robert Phillips40a29d72018-01-18 12:59:22 -050022 , fGpu(gpu)
23 , fResourceProvider(resourceProvider)
Brian Salomon58f153c2018-10-18 21:51:15 -040024 , fTokenTracker(tokenTracker) {}
bsalomon75398562015-08-17 12:55:38 -070025
Robert Phillips646e4292017-06-13 12:44:56 -040026const GrCaps& GrOpFlushState::caps() const {
27 return *fGpu->caps();
28}
29
Greg Daniel500d58b2017-08-24 15:59:33 -040030GrGpuRTCommandBuffer* GrOpFlushState::rtCommandBuffer() {
31 return fCommandBuffer->asRTCommandBuffer();
32}
33
Brian Osmane8012102018-11-29 14:05:07 -050034void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(const GrOp* op, const SkRect& opBounds) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040035 SkASSERT(this->rtCommandBuffer());
Brian Osmane8012102018-11-29 14:05:07 -050036 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050037 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040038 while (fCurrUpload != fInlineUploads.end() &&
39 fCurrUpload->fUploadBeforeToken == drawToken) {
40 this->rtCommandBuffer()->inlineUpload(this, fCurrUpload->fUpload);
41 ++fCurrUpload;
42 }
43 SkASSERT(fCurrDraw->fPipeline->proxy() == this->drawOpArgs().fProxy);
Brian Salomonff168d92018-06-23 15:17:27 -040044 this->rtCommandBuffer()->draw(*fCurrDraw->fGeometryProcessor, *fCurrDraw->fPipeline,
Brian Salomon49348902018-06-26 09:12:38 -040045 fCurrDraw->fFixedDynamicState, fCurrDraw->fDynamicStateArrays,
Brian Salomon7eae3e02018-08-07 14:02:38 +000046 fCurrDraw->fMeshes, fCurrDraw->fMeshCnt, opBounds);
Robert Phillips40a29d72018-01-18 12:59:22 -050047 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040048 ++fCurrDraw;
49 }
50}
51
52void GrOpFlushState::preExecuteDraws() {
53 fVertexPool.unmap();
54 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050055 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040056 this->doUpload(upload);
57 }
58 // Setup execution iterators.
59 fCurrDraw = fDraws.begin();
60 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040061}
62
63void GrOpFlushState::reset() {
64 SkASSERT(fCurrDraw == fDraws.end());
65 SkASSERT(fCurrUpload == fInlineUploads.end());
66 fVertexPool.reset();
67 fIndexPool.reset();
68 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050069 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040070 fInlineUploads.reset();
71 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040072 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
73}
74
Brian Salomon943ed792017-10-30 09:37:55 -040075void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload) {
Robert Phillips88260b52018-01-19 12:56:09 -050076 GrDeferredTextureUploadWritePixelsFn wp = [this](GrTextureProxy* dstProxy, int left, int top,
Brian Salomonc320b152018-02-20 14:05:36 -050077 int width, int height,
78 GrColorType srcColorType, const void* buffer,
79 size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040080 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon0ba9fa02018-06-01 11:55:08 -040081 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface) &&
82 fGpu->caps()->supportedWritePixelsColorType(dstSurface->config(), srcColorType) != srcColorType) {
Robert Phillips88260b52018-01-19 12:56:09 -050083 return false;
84 }
Brian Salomona9b04b92018-06-01 15:04:28 -040085 return this->fGpu->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
86 rowBytes);
Brian Salomon9bada542017-06-12 12:09:30 -040087 };
88 upload(wp);
89}
Brian Salomon29b60c92017-10-31 14:42:10 -040090
91GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -050092 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -040093 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -040094}
95
96GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -050097 fASAPUploads.append(&fArena, std::move(upload));
98 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -040099}
100
Brian Salomon7eae3e02018-08-07 14:02:38 +0000101void GrOpFlushState::draw(sk_sp<const GrGeometryProcessor> gp, const GrPipeline* pipeline,
Brian Salomon49348902018-06-26 09:12:38 -0400102 const GrPipeline::FixedDynamicState* fixedDynamicState,
Brian Salomonf7232642018-09-19 08:58:08 -0400103 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000104 const GrMesh meshes[], int meshCnt) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400105 SkASSERT(fOpArgs);
106 SkASSERT(fOpArgs->fOp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400107 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400108 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500109 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomonf7232642018-09-19 08:58:08 -0400110 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
111 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
112 fixedDynamicState->fPrimitiveProcessorTextures[i]->addPendingRead();
113 }
114 }
115 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
116 int n = gp->numTextureSamplers() * meshCnt;
117 for (int i = 0; i < n; ++i) {
118 dynamicStateArrays->fPrimitiveProcessorTextures[i]->addPendingRead();
119 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000120 }
121 draw.fGeometryProcessor = std::move(gp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400122 draw.fPipeline = pipeline;
Brian Salomon49348902018-06-26 09:12:38 -0400123 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400124 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000125 draw.fMeshes = meshes;
126 draw.fMeshCnt = meshCnt;
Brian Osmane8012102018-11-29 14:05:07 -0500127 draw.fOp = fOpArgs->fOp;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400128 if (firstDraw) {
129 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400130 }
131}
132
133void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer** buffer,
134 int* startVertex) {
135 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
136}
137
138uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex) {
139 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
140}
141
142void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
143 int fallbackVertexCount, const GrBuffer** buffer,
144 int* startVertex, int* actualVertexCount) {
145 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
146 startVertex, actualVertexCount);
147}
148
149uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
150 const GrBuffer** buffer, int* startIndex,
151 int* actualIndexCount) {
152 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
153 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
154}
155
156void GrOpFlushState::putBackIndices(int indexCount) {
157 fIndexPool.putBack(indexCount * sizeof(uint16_t));
158}
159
160void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
161 fVertexPool.putBack(vertices * vertexStride);
162}
163
164GrAppliedClip GrOpFlushState::detachAppliedClip() {
165 return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip();
166}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500167
168GrGlyphCache* GrOpFlushState::glyphCache() const {
169 return fGpu->getContext()->contextPriv().getGlyphCache();
170}
171
Robert Phillips5a66efb2018-03-07 15:13:18 -0500172GrAtlasManager* GrOpFlushState::atlasManager() const {
173 return fGpu->getContext()->contextPriv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500174}
Brian Salomonf7232642018-09-19 08:58:08 -0400175
176//////////////////////////////////////////////////////////////////////////////
177
178GrOpFlushState::Draw::~Draw() {
179 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
180 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
181 fFixedDynamicState->fPrimitiveProcessorTextures[i]->completedRead();
182 }
183 }
184 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
185 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
186 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
187 for (int i = 0; i < n; ++i) {
188 textures[i]->completedRead();
189 }
190 }
191}