blob: a67b379560487a96291982592b160956622a45ba [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
Brian Salomon1047a492019-07-02 12:25:21 -040010#include "src/core/SkConvertPixels.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrDrawOpAtlas.h"
13#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040014#include "src/gpu/GrImageInfo.h"
Robert Phillips901aff02019-10-08 12:32:56 -040015#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000017#include "src/gpu/GrTexture.h"
bsalomon75398562015-08-17 12:55:38 -070018
Brian Salomon7dc6e752017-11-02 11:34:51 -040019//////////////////////////////////////////////////////////////////////////////
20
Brian Salomon58f153c2018-10-18 21:51:15 -040021GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider,
Robert Phillipse5f73282019-06-18 17:15:04 -040022 GrTokenTracker* tokenTracker,
Brian Salomon601ac802019-02-07 13:37:16 -050023 sk_sp<GrBufferAllocPool::CpuBufferCache> cpuBufferCache)
24 : fVertexPool(gpu, cpuBufferCache)
25 , fIndexPool(gpu, std::move(cpuBufferCache))
Robert Phillips40a29d72018-01-18 12:59:22 -050026 , fGpu(gpu)
27 , fResourceProvider(resourceProvider)
Brian Salomonbeb7f522019-08-30 16:19:42 -040028 , fTokenTracker(tokenTracker) {}
bsalomon75398562015-08-17 12:55:38 -070029
Robert Phillips646e4292017-06-13 12:44:56 -040030const GrCaps& GrOpFlushState::caps() const {
31 return *fGpu->caps();
32}
33
Chris Dalton07cdcfc92019-02-26 11:13:22 -070034void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
Robert Phillips3968fcb2019-12-05 16:40:31 -050035 const GrOp* op, const SkRect& chainBounds, const GrPipeline* pipeline) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040036 SkASSERT(this->opsRenderPass());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070037
Brian Osmane8012102018-11-29 14:05:07 -050038 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
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) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040042 this->opsRenderPass()->inlineUpload(this, fCurrUpload->fUpload);
Brian Salomon7dc6e752017-11-02 11:34:51 -040043 ++fCurrUpload;
44 }
Robert Phillips901aff02019-10-08 12:32:56 -040045
46 GrProgramInfo programInfo(this->proxy()->numSamples(),
Chris Dalton5e8cdfd2019-11-11 15:23:30 -070047 this->proxy()->numStencilSamples(),
Robert Phillips933484f2019-11-26 09:38:55 -050048 this->proxy()->backendFormat(),
Robert Phillipsb58098f2020-03-02 16:25:29 -050049 this->outputView()->origin(),
Robert Phillips67a625e2019-11-15 15:37:07 -050050 pipeline,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050051 fCurrDraw->fGeometryProcessor,
Robert Phillips901aff02019-10-08 12:32:56 -040052 fCurrDraw->fFixedDynamicState,
Robert Phillips2d8a95e2019-10-10 12:50:22 -040053 fCurrDraw->fDynamicStateArrays,
Robert Phillipscea290f2019-11-06 11:21:03 -050054 fCurrDraw->fMeshCnt,
55 fCurrDraw->fPrimitiveType);
Robert Phillips901aff02019-10-08 12:32:56 -040056
Chris Dalton4386ad12020-02-19 16:42:06 -070057 this->opsRenderPass()->bindPipeline(programInfo, chainBounds);
58 this->opsRenderPass()->drawMeshes(programInfo, fCurrDraw->fMeshes, fCurrDraw->fMeshCnt);
Robert Phillips40a29d72018-01-18 12:59:22 -050059 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040060 ++fCurrDraw;
61 }
62}
63
64void GrOpFlushState::preExecuteDraws() {
65 fVertexPool.unmap();
66 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050067 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040068 this->doUpload(upload);
69 }
70 // Setup execution iterators.
71 fCurrDraw = fDraws.begin();
72 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040073}
74
75void GrOpFlushState::reset() {
76 SkASSERT(fCurrDraw == fDraws.end());
77 SkASSERT(fCurrUpload == fInlineUploads.end());
78 fVertexPool.reset();
79 fIndexPool.reset();
80 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050081 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040082 fInlineUploads.reset();
83 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040084 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
85}
86
Greg Danielb20d7e52019-09-03 13:54:39 -040087void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload,
88 bool shouldPrepareSurfaceForSampling) {
89 GrDeferredTextureUploadWritePixelsFn wp = [this, shouldPrepareSurfaceForSampling](
90 GrTextureProxy* dstProxy, int left, int top, int width, int height,
91 GrColorType colorType, const void* buffer, size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040092 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon42be09d2019-07-26 12:12:26 -040093 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
94 return false;
95 }
Brian Salomon01915c02019-08-02 09:57:21 -040096 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
97 colorType, dstSurface->backendFormat(), colorType);
Brian Salomon77a684f2019-08-01 14:38:04 -040098 size_t tightRB = width * GrColorTypeBytesPerPixel(supportedWrite.fColorType);
99 SkASSERT(rowBytes >= tightRB);
Brian Salomon1047a492019-07-02 12:25:21 -0400100 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf77c1462019-08-01 15:19:29 -0400101 if (supportedWrite.fColorType != colorType ||
Brian Salomon77a684f2019-08-01 14:38:04 -0400102 (!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) {
103 tmpPixels.reset(new char[height * tightRB]);
104 // Use kUnpremul to ensure no alpha type conversions or clamping occur.
105 static constexpr auto kAT = kUnpremul_SkAlphaType;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400106 GrImageInfo srcInfo(colorType, kAT, nullptr, width, height);
107 GrImageInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, width,
Brian Salomon77a684f2019-08-01 14:38:04 -0400108 height);
109 if (!GrConvertPixels(tmpInfo, tmpPixels.get(), tightRB, srcInfo, buffer, rowBytes)) {
110 return false;
111 }
Brian Salomon1047a492019-07-02 12:25:21 -0400112 rowBytes = tightRB;
Brian Salomon77a684f2019-08-01 14:38:04 -0400113 buffer = tmpPixels.get();
Brian Salomon1047a492019-07-02 12:25:21 -0400114 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400115 return this->fGpu->writePixels(dstSurface, left, top, width, height, colorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400116 supportedWrite.fColorType, buffer, rowBytes,
117 shouldPrepareSurfaceForSampling);
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(
Robert Phillips6c59fe42020-02-27 09:30:37 -0500133 const GrGeometryProcessor* gp,
Chris Daltoneb694b72020-03-16 09:25:50 -0600134 const GrSimpleMesh meshes[],
Robert Phillips6c59fe42020-02-27 09:30:37 -0500135 int meshCnt,
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700136 const GrPipeline::FixedDynamicState* fixedDynamicState,
Robert Phillipscea290f2019-11-06 11:21:03 -0500137 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
138 GrPrimitiveType primitiveType) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400139 SkASSERT(fOpArgs);
Robert Phillips405413f2019-10-04 10:39:28 -0400140 SkDEBUGCODE(fOpArgs->validate());
Brian Salomon7dc6e752017-11-02 11:34:51 -0400141 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400142 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500143 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomonf7232642018-09-19 08:58:08 -0400144 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
145 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400146 fixedDynamicState->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400147 }
148 }
149 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
150 int n = gp->numTextureSamplers() * meshCnt;
151 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400152 dynamicStateArrays->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400153 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000154 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500155 draw.fGeometryProcessor = gp;
Brian Salomon49348902018-06-26 09:12:38 -0400156 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400157 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000158 draw.fMeshes = meshes;
159 draw.fMeshCnt = meshCnt;
Robert Phillips405413f2019-10-04 10:39:28 -0400160 draw.fOp = fOpArgs->op();
Robert Phillipscea290f2019-11-06 11:21:03 -0500161 draw.fPrimitiveType = primitiveType;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400162 if (firstDraw) {
163 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400164 }
165}
166
Brian Salomon12d22642019-01-29 14:38:50 -0500167void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
168 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400169 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
170}
171
Brian Salomon12d22642019-01-29 14:38:50 -0500172uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
173 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400174 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
175}
176
177void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500178 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400179 int* startVertex, int* actualVertexCount) {
180 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
181 startVertex, actualVertexCount);
182}
183
184uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500185 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400186 int* actualIndexCount) {
187 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
188 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
189}
190
191void GrOpFlushState::putBackIndices(int indexCount) {
192 fIndexPool.putBack(indexCount * sizeof(uint16_t));
193}
194
195void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
196 fVertexPool.putBack(vertices * vertexStride);
197}
198
199GrAppliedClip GrOpFlushState::detachAppliedClip() {
Robert Phillips405413f2019-10-04 10:39:28 -0400200 return fOpArgs->appliedClip() ? std::move(*fOpArgs->appliedClip()) : GrAppliedClip();
Brian Salomon29b60c92017-10-31 14:42:10 -0400201}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500202
Herb Derby081e6f32019-01-16 13:46:02 -0500203GrStrikeCache* GrOpFlushState::glyphCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500204 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500205}
206
Robert Phillips5a66efb2018-03-07 15:13:18 -0500207GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500208 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500209}
Brian Salomonf7232642018-09-19 08:58:08 -0400210
211//////////////////////////////////////////////////////////////////////////////
212
213GrOpFlushState::Draw::~Draw() {
214 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
215 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400216 fFixedDynamicState->fPrimitiveProcessorTextures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400217 }
218 }
219 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
220 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
221 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
222 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400223 textures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400224 }
225 }
226}