blob: 586a1dfcb75395ead42a5927dcc2276672d1ba85 [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 Salomonbeb7f522019-08-30 16:19:42 -040026 , fTokenTracker(tokenTracker) {}
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
Chris Dalton07cdcfc92019-02-26 11:13:22 -070032void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
33 const GrOp* op, const SkRect& chainBounds, GrProcessorSet&& processorSet,
Chris Daltonbaa1b352019-04-03 12:03:00 -060034 GrPipeline::InputFlags pipelineFlags, const GrUserStencilSettings* stencilSettings) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040035 SkASSERT(this->opsRenderPass());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070036
37 GrPipeline::InitArgs pipelineArgs;
Chris Daltonbaa1b352019-04-03 12:03:00 -060038 pipelineArgs.fInputFlags = pipelineFlags;
Chris Dalton07cdcfc92019-02-26 11:13:22 -070039 pipelineArgs.fDstProxy = this->dstProxy();
40 pipelineArgs.fCaps = &this->caps();
Chris Dalton07cdcfc92019-02-26 11:13:22 -070041 pipelineArgs.fUserStencil = stencilSettings;
Greg Daniel2c3398d2019-06-19 11:58:01 -040042 pipelineArgs.fOutputSwizzle = this->drawOpArgs().fOutputSwizzle;
Greg Daniel0b002e22019-08-12 13:25:36 -040043 GrPipeline* pipeline = this->allocator()->make<GrPipeline>(pipelineArgs,
44 std::move(processorSet),
45 this->detachAppliedClip());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070046
Brian Osmane8012102018-11-29 14:05:07 -050047 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050048 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040049 while (fCurrUpload != fInlineUploads.end() &&
50 fCurrUpload->fUploadBeforeToken == drawToken) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040051 this->opsRenderPass()->inlineUpload(this, fCurrUpload->fUpload);
Brian Salomon7dc6e752017-11-02 11:34:51 -040052 ++fCurrUpload;
53 }
Greg Daniel2d41d0d2019-08-26 11:08:51 -040054 this->opsRenderPass()->draw(
Greg Daniel0b002e22019-08-12 13:25:36 -040055 *fCurrDraw->fGeometryProcessor, *pipeline, fCurrDraw->fFixedDynamicState,
Chris Dalton07cdcfc92019-02-26 11:13:22 -070056 fCurrDraw->fDynamicStateArrays, fCurrDraw->fMeshes, fCurrDraw->fMeshCnt,
57 chainBounds);
Robert Phillips40a29d72018-01-18 12:59:22 -050058 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040059 ++fCurrDraw;
60 }
61}
62
63void GrOpFlushState::preExecuteDraws() {
64 fVertexPool.unmap();
65 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050066 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040067 this->doUpload(upload);
68 }
69 // Setup execution iterators.
70 fCurrDraw = fDraws.begin();
71 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040072}
73
74void GrOpFlushState::reset() {
75 SkASSERT(fCurrDraw == fDraws.end());
76 SkASSERT(fCurrUpload == fInlineUploads.end());
77 fVertexPool.reset();
78 fIndexPool.reset();
79 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050080 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040081 fInlineUploads.reset();
82 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040083 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
84}
85
Greg Danielb20d7e52019-09-03 13:54:39 -040086void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload,
87 bool shouldPrepareSurfaceForSampling) {
88 GrDeferredTextureUploadWritePixelsFn wp = [this, shouldPrepareSurfaceForSampling](
89 GrTextureProxy* dstProxy, int left, int top, int width, int height,
90 GrColorType colorType, const void* buffer, size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040091 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon42be09d2019-07-26 12:12:26 -040092 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
93 return false;
94 }
Brian Salomon01915c02019-08-02 09:57:21 -040095 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
96 colorType, dstSurface->backendFormat(), colorType);
Brian Salomon77a684f2019-08-01 14:38:04 -040097 size_t tightRB = width * GrColorTypeBytesPerPixel(supportedWrite.fColorType);
98 SkASSERT(rowBytes >= tightRB);
Brian Salomon1047a492019-07-02 12:25:21 -040099 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf77c1462019-08-01 15:19:29 -0400100 if (supportedWrite.fColorType != colorType ||
Brian Salomon77a684f2019-08-01 14:38:04 -0400101 (!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) {
102 tmpPixels.reset(new char[height * tightRB]);
103 // Use kUnpremul to ensure no alpha type conversions or clamping occur.
104 static constexpr auto kAT = kUnpremul_SkAlphaType;
Brian Salomonf77c1462019-08-01 15:19:29 -0400105 GrPixelInfo srcInfo(colorType, kAT, nullptr, width, height);
Brian Salomon77a684f2019-08-01 14:38:04 -0400106 GrPixelInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, width,
107 height);
108 if (!GrConvertPixels(tmpInfo, tmpPixels.get(), tightRB, srcInfo, buffer, rowBytes)) {
109 return false;
110 }
Brian Salomon1047a492019-07-02 12:25:21 -0400111 rowBytes = tightRB;
Brian Salomon77a684f2019-08-01 14:38:04 -0400112 buffer = tmpPixels.get();
Brian Salomon1047a492019-07-02 12:25:21 -0400113 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400114 return this->fGpu->writePixels(dstSurface, left, top, width, height, colorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400115 supportedWrite.fColorType, buffer, rowBytes,
116 shouldPrepareSurfaceForSampling);
Brian Salomon9bada542017-06-12 12:09:30 -0400117 };
118 upload(wp);
119}
Brian Salomon29b60c92017-10-31 14:42:10 -0400120
121GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500122 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400123 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400124}
125
126GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500127 fASAPUploads.append(&fArena, std::move(upload));
128 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400129}
130
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700131void GrOpFlushState::recordDraw(
132 sk_sp<const GrGeometryProcessor> gp, const GrMesh meshes[], int meshCnt,
133 const GrPipeline::FixedDynamicState* fixedDynamicState,
134 const GrPipeline::DynamicStateArrays* dynamicStateArrays) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400135 SkASSERT(fOpArgs);
136 SkASSERT(fOpArgs->fOp);
Brian Salomon7dc6e752017-11-02 11:34:51 -0400137 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400138 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500139 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomonf7232642018-09-19 08:58:08 -0400140 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
141 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400142 fixedDynamicState->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400143 }
144 }
145 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
146 int n = gp->numTextureSamplers() * meshCnt;
147 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400148 dynamicStateArrays->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400149 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000150 }
151 draw.fGeometryProcessor = std::move(gp);
Brian Salomon49348902018-06-26 09:12:38 -0400152 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400153 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000154 draw.fMeshes = meshes;
155 draw.fMeshCnt = meshCnt;
Brian Osmane8012102018-11-29 14:05:07 -0500156 draw.fOp = fOpArgs->fOp;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400157 if (firstDraw) {
158 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400159 }
160}
161
Brian Salomon12d22642019-01-29 14:38:50 -0500162void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
163 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400164 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
165}
166
Brian Salomon12d22642019-01-29 14:38:50 -0500167uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
168 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400169 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
170}
171
172void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500173 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400174 int* startVertex, int* actualVertexCount) {
175 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
176 startVertex, actualVertexCount);
177}
178
179uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500180 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400181 int* actualIndexCount) {
182 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
183 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
184}
185
186void GrOpFlushState::putBackIndices(int indexCount) {
187 fIndexPool.putBack(indexCount * sizeof(uint16_t));
188}
189
190void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
191 fVertexPool.putBack(vertices * vertexStride);
192}
193
194GrAppliedClip GrOpFlushState::detachAppliedClip() {
195 return fOpArgs->fAppliedClip ? std::move(*fOpArgs->fAppliedClip) : GrAppliedClip();
196}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500197
Herb Derby081e6f32019-01-16 13:46:02 -0500198GrStrikeCache* GrOpFlushState::glyphCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500199 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500200}
201
Robert Phillips5a66efb2018-03-07 15:13:18 -0500202GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500203 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500204}
Brian Salomonf7232642018-09-19 08:58:08 -0400205
206//////////////////////////////////////////////////////////////////////////////
207
208GrOpFlushState::Draw::~Draw() {
209 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
210 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400211 fFixedDynamicState->fPrimitiveProcessorTextures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400212 }
213 }
214 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
215 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
216 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
217 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400218 textures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400219 }
220 }
221}