blob: d535b5acfaa93d29b2b83d303dbe4a1f811aff34 [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"
Brian Salomon1047a492019-07-02 12:25:21 -040011#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrDrawOpAtlas.h"
14#include "src/gpu/GrGpu.h"
15#include "src/gpu/GrResourceProvider.h"
bsalomon75398562015-08-17 12:55:38 -070016
Brian Salomon7dc6e752017-11-02 11:34:51 -040017//////////////////////////////////////////////////////////////////////////////
18
Brian Salomon58f153c2018-10-18 21:51:15 -040019GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider,
Robert Phillipse5f73282019-06-18 17:15:04 -040020 GrTokenTracker* tokenTracker,
Brian Salomon601ac802019-02-07 13:37:16 -050021 sk_sp<GrBufferAllocPool::CpuBufferCache> cpuBufferCache)
22 : fVertexPool(gpu, cpuBufferCache)
23 , fIndexPool(gpu, std::move(cpuBufferCache))
Robert Phillips40a29d72018-01-18 12:59:22 -050024 , fGpu(gpu)
25 , fResourceProvider(resourceProvider)
Brian Salomon2c791fc2019-04-02 11:52:03 -040026 , fTokenTracker(tokenTracker)
Robert Phillipse5f73282019-06-18 17:15:04 -040027 , fDeinstantiateProxyTracker() {}
bsalomon75398562015-08-17 12:55:38 -070028
Robert Phillips646e4292017-06-13 12:44:56 -040029const GrCaps& GrOpFlushState::caps() const {
30 return *fGpu->caps();
31}
32
Greg Daniel500d58b2017-08-24 15:59:33 -040033GrGpuRTCommandBuffer* GrOpFlushState::rtCommandBuffer() {
34 return fCommandBuffer->asRTCommandBuffer();
35}
36
Chris Dalton07cdcfc92019-02-26 11:13:22 -070037void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
38 const GrOp* op, const SkRect& chainBounds, GrProcessorSet&& processorSet,
Chris Daltonbaa1b352019-04-03 12:03:00 -060039 GrPipeline::InputFlags pipelineFlags, const GrUserStencilSettings* stencilSettings) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040040 SkASSERT(this->rtCommandBuffer());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070041
42 GrPipeline::InitArgs pipelineArgs;
Chris Daltonbaa1b352019-04-03 12:03:00 -060043 pipelineArgs.fInputFlags = pipelineFlags;
Chris Dalton07cdcfc92019-02-26 11:13:22 -070044 pipelineArgs.fDstProxy = this->dstProxy();
45 pipelineArgs.fCaps = &this->caps();
Chris Dalton07cdcfc92019-02-26 11:13:22 -070046 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 Salomonf77c1462019-08-01 15:19:29 -040091 int width, int height, GrColorType colorType,
92 const void* buffer, size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040093 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon42be09d2019-07-26 12:12:26 -040094 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
95 return false;
96 }
Brian Salomon01915c02019-08-02 09:57:21 -040097 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
98 colorType, dstSurface->backendFormat(), colorType);
Brian Salomon77a684f2019-08-01 14:38:04 -040099 size_t tightRB = width * GrColorTypeBytesPerPixel(supportedWrite.fColorType);
100 SkASSERT(rowBytes >= tightRB);
Brian Salomon1047a492019-07-02 12:25:21 -0400101 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf77c1462019-08-01 15:19:29 -0400102 if (supportedWrite.fColorType != colorType ||
Brian Salomon77a684f2019-08-01 14:38:04 -0400103 (!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) {
104 tmpPixels.reset(new char[height * tightRB]);
105 // Use kUnpremul to ensure no alpha type conversions or clamping occur.
106 static constexpr auto kAT = kUnpremul_SkAlphaType;
Brian Salomonf77c1462019-08-01 15:19:29 -0400107 GrPixelInfo srcInfo(colorType, kAT, nullptr, width, height);
Brian Salomon77a684f2019-08-01 14:38:04 -0400108 GrPixelInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, width,
109 height);
110 if (!GrConvertPixels(tmpInfo, tmpPixels.get(), tightRB, srcInfo, buffer, rowBytes)) {
111 return false;
112 }
Brian Salomon1047a492019-07-02 12:25:21 -0400113 rowBytes = tightRB;
Brian Salomon77a684f2019-08-01 14:38:04 -0400114 buffer = tmpPixels.get();
Brian Salomon1047a492019-07-02 12:25:21 -0400115 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400116 return this->fGpu->writePixels(dstSurface, left, top, width, height, colorType,
117 supportedWrite.fColorType, buffer, rowBytes);
Brian Salomon9bada542017-06-12 12:09:30 -0400118 };
119 upload(wp);
120}
Brian Salomon29b60c92017-10-31 14:42:10 -0400121
122GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500123 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400124 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400125}
126
127GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500128 fASAPUploads.append(&fArena, std::move(upload));
129 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400130}
131
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700132void GrOpFlushState::recordDraw(
133 sk_sp<const GrGeometryProcessor> gp, const GrMesh meshes[], int meshCnt,
134 const GrPipeline::FixedDynamicState* fixedDynamicState,
135 const GrPipeline::DynamicStateArrays* dynamicStateArrays) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400136 SkASSERT(fOpArgs);
137 SkASSERT(fOpArgs->fOp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400138 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400139 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500140 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomonf7232642018-09-19 08:58:08 -0400141 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
142 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400143 fixedDynamicState->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400144 }
145 }
146 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
147 int n = gp->numTextureSamplers() * meshCnt;
148 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400149 dynamicStateArrays->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400150 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000151 }
152 draw.fGeometryProcessor = std::move(gp);
Brian Salomon49348902018-06-26 09:12:38 -0400153 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400154 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000155 draw.fMeshes = meshes;
156 draw.fMeshCnt = meshCnt;
Brian Osmane8012102018-11-29 14:05:07 -0500157 draw.fOp = fOpArgs->fOp;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400158 if (firstDraw) {
159 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400160 }
161}
162
Brian Salomon12d22642019-01-29 14:38:50 -0500163void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
164 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400165 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
166}
167
Brian Salomon12d22642019-01-29 14:38:50 -0500168uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
169 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400170 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
171}
172
173void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500174 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400175 int* startVertex, int* actualVertexCount) {
176 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
177 startVertex, actualVertexCount);
178}
179
180uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500181 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400182 int* actualIndexCount) {
183 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
184 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
185}
186
187void GrOpFlushState::putBackIndices(int indexCount) {
188 fIndexPool.putBack(indexCount * sizeof(uint16_t));
189}
190
191void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
192 fVertexPool.putBack(vertices * vertexStride);
193}
194
195GrAppliedClip GrOpFlushState::detachAppliedClip() {
196 return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip();
197}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500198
Herb Derby081e6f32019-01-16 13:46:02 -0500199GrStrikeCache* GrOpFlushState::glyphCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500200 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500201}
202
Robert Phillips5a66efb2018-03-07 15:13:18 -0500203GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500204 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500205}
Brian Salomonf7232642018-09-19 08:58:08 -0400206
207//////////////////////////////////////////////////////////////////////////////
208
209GrOpFlushState::Draw::~Draw() {
210 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
211 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400212 fFixedDynamicState->fPrimitiveProcessorTextures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400213 }
214 }
215 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
216 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
217 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
218 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400219 textures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400220 }
221 }
222}