blob: 5c13afe7bc29b1bafe3eaadc5eabd15d09ee4450 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrOpFlushState.h"
bsalomon75398562015-08-17 12:55:38 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrTexture.h"
11#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrDrawOpAtlas.h"
13#include "src/gpu/GrGpu.h"
14#include "src/gpu/GrResourceProvider.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,
Robert Phillipse5f73282019-06-18 17:15:04 -040019 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)
Robert Phillipse5f73282019-06-18 17:15:04 -040026 , fDeinstantiateProxyTracker() {}
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;
Greg Daniel2c3398d2019-06-19 11:58:01 -040047 pipelineArgs.fOutputSwizzle = this->drawOpArgs().fOutputSwizzle;
Chris Dalton07cdcfc92019-02-26 11:13:22 -070048 GrPipeline pipeline(pipelineArgs, std::move(processorSet), this->detachAppliedClip());
49
Brian Osmane8012102018-11-29 14:05:07 -050050 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050051 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040052 while (fCurrUpload != fInlineUploads.end() &&
53 fCurrUpload->fUploadBeforeToken == drawToken) {
54 this->rtCommandBuffer()->inlineUpload(this, fCurrUpload->fUpload);
55 ++fCurrUpload;
56 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -070057 this->rtCommandBuffer()->draw(
58 *fCurrDraw->fGeometryProcessor, pipeline, fCurrDraw->fFixedDynamicState,
59 fCurrDraw->fDynamicStateArrays, fCurrDraw->fMeshes, fCurrDraw->fMeshCnt,
60 chainBounds);
Robert Phillips40a29d72018-01-18 12:59:22 -050061 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040062 ++fCurrDraw;
63 }
64}
65
66void GrOpFlushState::preExecuteDraws() {
67 fVertexPool.unmap();
68 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050069 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040070 this->doUpload(upload);
71 }
72 // Setup execution iterators.
73 fCurrDraw = fDraws.begin();
74 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040075}
76
77void GrOpFlushState::reset() {
78 SkASSERT(fCurrDraw == fDraws.end());
79 SkASSERT(fCurrUpload == fInlineUploads.end());
80 fVertexPool.reset();
81 fIndexPool.reset();
82 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050083 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040084 fInlineUploads.reset();
85 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040086 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
87}
88
Brian Salomon943ed792017-10-30 09:37:55 -040089void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload) {
Robert Phillips88260b52018-01-19 12:56:09 -050090 GrDeferredTextureUploadWritePixelsFn wp = [this](GrTextureProxy* dstProxy, int left, int top,
Brian Salomonc320b152018-02-20 14:05:36 -050091 int width, int height,
92 GrColorType srcColorType, const void* buffer,
93 size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040094 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon0ba9fa02018-06-01 11:55:08 -040095 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface) &&
96 fGpu->caps()->supportedWritePixelsColorType(dstSurface->config(), srcColorType) != srcColorType) {
Robert Phillips88260b52018-01-19 12:56:09 -050097 return false;
98 }
Brian Salomona9b04b92018-06-01 15:04:28 -040099 return this->fGpu->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
100 rowBytes);
Brian Salomon9bada542017-06-12 12:09:30 -0400101 };
102 upload(wp);
103}
Brian Salomon29b60c92017-10-31 14:42:10 -0400104
105GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500106 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400107 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400108}
109
110GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500111 fASAPUploads.append(&fArena, std::move(upload));
112 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400113}
114
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700115void GrOpFlushState::recordDraw(
116 sk_sp<const GrGeometryProcessor> gp, const GrMesh meshes[], int meshCnt,
117 const GrPipeline::FixedDynamicState* fixedDynamicState,
118 const GrPipeline::DynamicStateArrays* dynamicStateArrays) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400119 SkASSERT(fOpArgs);
120 SkASSERT(fOpArgs->fOp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400121 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400122 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500123 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomonf7232642018-09-19 08:58:08 -0400124 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
125 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400126 fixedDynamicState->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400127 }
128 }
129 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
130 int n = gp->numTextureSamplers() * meshCnt;
131 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400132 dynamicStateArrays->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400133 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000134 }
135 draw.fGeometryProcessor = std::move(gp);
Brian Salomon49348902018-06-26 09:12:38 -0400136 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400137 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000138 draw.fMeshes = meshes;
139 draw.fMeshCnt = meshCnt;
Brian Osmane8012102018-11-29 14:05:07 -0500140 draw.fOp = fOpArgs->fOp;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400141 if (firstDraw) {
142 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400143 }
144}
145
Brian Salomon12d22642019-01-29 14:38:50 -0500146void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
147 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400148 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
149}
150
Brian Salomon12d22642019-01-29 14:38:50 -0500151uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
152 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400153 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
154}
155
156void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500157 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400158 int* startVertex, int* actualVertexCount) {
159 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
160 startVertex, actualVertexCount);
161}
162
163uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500164 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400165 int* actualIndexCount) {
166 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
167 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
168}
169
170void GrOpFlushState::putBackIndices(int indexCount) {
171 fIndexPool.putBack(indexCount * sizeof(uint16_t));
172}
173
174void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
175 fVertexPool.putBack(vertices * vertexStride);
176}
177
178GrAppliedClip GrOpFlushState::detachAppliedClip() {
179 return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip();
180}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500181
Herb Derby081e6f32019-01-16 13:46:02 -0500182GrStrikeCache* GrOpFlushState::glyphCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500183 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500184}
185
Robert Phillips5a66efb2018-03-07 15:13:18 -0500186GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500187 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500188}
Brian Salomonf7232642018-09-19 08:58:08 -0400189
190//////////////////////////////////////////////////////////////////////////////
191
192GrOpFlushState::Draw::~Draw() {
193 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
194 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400195 fFixedDynamicState->fPrimitiveProcessorTextures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400196 }
197 }
198 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
199 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
200 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
201 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400202 textures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400203 }
204 }
205}