blob: 6385c9fdcee242f706f3b515ce3425fb3d6af29b [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 Salomon7eae3e02018-08-07 14:02:38 +0000105 const GrMesh meshes[], int meshCnt) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400106 SkASSERT(fOpArgs);
107 SkASSERT(fOpArgs->fOp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400108 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400109 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500110 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomoncd7907b2018-08-30 08:36:18 -0400111 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
112 fixedDynamicState->fPrimitiveProcessorTextures[i]->addPendingRead();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000113 }
114 draw.fGeometryProcessor = std::move(gp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400115 draw.fPipeline = pipeline;
Brian Salomon49348902018-06-26 09:12:38 -0400116 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomoncd7907b2018-08-30 08:36:18 -0400117 draw.fDynamicStateArrays = nullptr;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000118 draw.fMeshes = meshes;
119 draw.fMeshCnt = meshCnt;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400120 draw.fOpID = fOpArgs->fOp->uniqueID();
121 if (firstDraw) {
122 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400123 }
124}
125
126void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer** buffer,
127 int* startVertex) {
128 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
129}
130
131uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, const GrBuffer** buffer, int* startIndex) {
132 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
133}
134
135void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
136 int fallbackVertexCount, const GrBuffer** buffer,
137 int* startVertex, int* actualVertexCount) {
138 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
139 startVertex, actualVertexCount);
140}
141
142uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
143 const GrBuffer** buffer, int* startIndex,
144 int* actualIndexCount) {
145 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
146 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
147}
148
149void GrOpFlushState::putBackIndices(int indexCount) {
150 fIndexPool.putBack(indexCount * sizeof(uint16_t));
151}
152
153void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
154 fVertexPool.putBack(vertices * vertexStride);
155}
156
157GrAppliedClip GrOpFlushState::detachAppliedClip() {
158 return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip();
159}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500160
161GrGlyphCache* GrOpFlushState::glyphCache() const {
162 return fGpu->getContext()->contextPriv().getGlyphCache();
163}
164
Robert Phillips5a66efb2018-03-07 15:13:18 -0500165GrAtlasManager* GrOpFlushState::atlasManager() const {
166 return fGpu->getContext()->contextPriv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500167}