blob: 60ffa51c4781cf455eb1f3cbbc896d0172515644 [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"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040015#include "src/gpu/GrImageInfo.h"
Robert Phillips901aff02019-10-08 12:32:56 -040016#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrResourceProvider.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(
35 const GrOp* op, const SkRect& chainBounds, GrProcessorSet&& processorSet,
Chris Daltonbaa1b352019-04-03 12:03:00 -060036 GrPipeline::InputFlags pipelineFlags, const GrUserStencilSettings* stencilSettings) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040037 SkASSERT(this->opsRenderPass());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070038
39 GrPipeline::InitArgs pipelineArgs;
Chris Daltonbaa1b352019-04-03 12:03:00 -060040 pipelineArgs.fInputFlags = pipelineFlags;
Greg Daniel524e28b2019-11-01 11:48:53 -040041 pipelineArgs.fDstProxyView = this->dstProxyView();
Chris Dalton07cdcfc92019-02-26 11:13:22 -070042 pipelineArgs.fCaps = &this->caps();
Chris Dalton07cdcfc92019-02-26 11:13:22 -070043 pipelineArgs.fUserStencil = stencilSettings;
Robert Phillips405413f2019-10-04 10:39:28 -040044 pipelineArgs.fOutputSwizzle = this->drawOpArgs().outputSwizzle();
Robert Phillips8053c972019-11-21 10:44:53 -050045 auto pipeline = this->allocator()->make<GrPipeline>(pipelineArgs,
46 std::move(processorSet),
47 this->detachAppliedClip());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070048
Brian Osmane8012102018-11-29 14:05:07 -050049 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050050 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040051 while (fCurrUpload != fInlineUploads.end() &&
52 fCurrUpload->fUploadBeforeToken == drawToken) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040053 this->opsRenderPass()->inlineUpload(this, fCurrUpload->fUpload);
Brian Salomon7dc6e752017-11-02 11:34:51 -040054 ++fCurrUpload;
55 }
Robert Phillips901aff02019-10-08 12:32:56 -040056
57 GrProgramInfo programInfo(this->proxy()->numSamples(),
Chris Dalton5e8cdfd2019-11-11 15:23:30 -070058 this->proxy()->numStencilSamples(),
Robert Phillips933484f2019-11-26 09:38:55 -050059 this->proxy()->backendFormat(),
60 this->view()->origin(),
Robert Phillips67a625e2019-11-15 15:37:07 -050061 pipeline,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050062 fCurrDraw->fGeometryProcessor,
Robert Phillips901aff02019-10-08 12:32:56 -040063 fCurrDraw->fFixedDynamicState,
Robert Phillips2d8a95e2019-10-10 12:50:22 -040064 fCurrDraw->fDynamicStateArrays,
Robert Phillipscea290f2019-11-06 11:21:03 -050065 fCurrDraw->fMeshCnt,
66 fCurrDraw->fPrimitiveType);
Robert Phillips901aff02019-10-08 12:32:56 -040067
68 this->opsRenderPass()->draw(programInfo, fCurrDraw->fMeshes,
69 fCurrDraw->fMeshCnt, chainBounds);
Robert Phillips40a29d72018-01-18 12:59:22 -050070 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040071 ++fCurrDraw;
72 }
73}
74
75void GrOpFlushState::preExecuteDraws() {
76 fVertexPool.unmap();
77 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050078 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040079 this->doUpload(upload);
80 }
81 // Setup execution iterators.
82 fCurrDraw = fDraws.begin();
83 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040084}
85
86void GrOpFlushState::reset() {
87 SkASSERT(fCurrDraw == fDraws.end());
88 SkASSERT(fCurrUpload == fInlineUploads.end());
89 fVertexPool.reset();
90 fIndexPool.reset();
91 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050092 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040093 fInlineUploads.reset();
94 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040095 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
96}
97
Greg Danielb20d7e52019-09-03 13:54:39 -040098void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload,
99 bool shouldPrepareSurfaceForSampling) {
100 GrDeferredTextureUploadWritePixelsFn wp = [this, shouldPrepareSurfaceForSampling](
101 GrTextureProxy* dstProxy, int left, int top, int width, int height,
102 GrColorType colorType, const void* buffer, size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400103 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon42be09d2019-07-26 12:12:26 -0400104 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
105 return false;
106 }
Brian Salomon01915c02019-08-02 09:57:21 -0400107 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
108 colorType, dstSurface->backendFormat(), colorType);
Brian Salomon77a684f2019-08-01 14:38:04 -0400109 size_t tightRB = width * GrColorTypeBytesPerPixel(supportedWrite.fColorType);
110 SkASSERT(rowBytes >= tightRB);
Brian Salomon1047a492019-07-02 12:25:21 -0400111 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf77c1462019-08-01 15:19:29 -0400112 if (supportedWrite.fColorType != colorType ||
Brian Salomon77a684f2019-08-01 14:38:04 -0400113 (!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) {
114 tmpPixels.reset(new char[height * tightRB]);
115 // Use kUnpremul to ensure no alpha type conversions or clamping occur.
116 static constexpr auto kAT = kUnpremul_SkAlphaType;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400117 GrImageInfo srcInfo(colorType, kAT, nullptr, width, height);
118 GrImageInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, width,
Brian Salomon77a684f2019-08-01 14:38:04 -0400119 height);
120 if (!GrConvertPixels(tmpInfo, tmpPixels.get(), tightRB, srcInfo, buffer, rowBytes)) {
121 return false;
122 }
Brian Salomon1047a492019-07-02 12:25:21 -0400123 rowBytes = tightRB;
Brian Salomon77a684f2019-08-01 14:38:04 -0400124 buffer = tmpPixels.get();
Brian Salomon1047a492019-07-02 12:25:21 -0400125 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400126 return this->fGpu->writePixels(dstSurface, left, top, width, height, colorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400127 supportedWrite.fColorType, buffer, rowBytes,
128 shouldPrepareSurfaceForSampling);
Brian Salomon9bada542017-06-12 12:09:30 -0400129 };
130 upload(wp);
131}
Brian Salomon29b60c92017-10-31 14:42:10 -0400132
133GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500134 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400135 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400136}
137
138GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500139 fASAPUploads.append(&fArena, std::move(upload));
140 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400141}
142
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700143void GrOpFlushState::recordDraw(
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500144 const GrGeometryProcessor* gp, const GrMesh meshes[], int meshCnt,
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700145 const GrPipeline::FixedDynamicState* fixedDynamicState,
Robert Phillipscea290f2019-11-06 11:21:03 -0500146 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
147 GrPrimitiveType primitiveType) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400148 SkASSERT(fOpArgs);
Robert Phillips405413f2019-10-04 10:39:28 -0400149 SkDEBUGCODE(fOpArgs->validate());
Brian Salomon7dc6e752017-11-02 11:34:51 -0400150 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400151 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500152 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Brian Salomonf7232642018-09-19 08:58:08 -0400153 if (fixedDynamicState && fixedDynamicState->fPrimitiveProcessorTextures) {
154 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400155 fixedDynamicState->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400156 }
157 }
158 if (dynamicStateArrays && dynamicStateArrays->fPrimitiveProcessorTextures) {
159 int n = gp->numTextureSamplers() * meshCnt;
160 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400161 dynamicStateArrays->fPrimitiveProcessorTextures[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400162 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000163 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500164 draw.fGeometryProcessor = gp;
Brian Salomon49348902018-06-26 09:12:38 -0400165 draw.fFixedDynamicState = fixedDynamicState;
Brian Salomonf7232642018-09-19 08:58:08 -0400166 draw.fDynamicStateArrays = dynamicStateArrays;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000167 draw.fMeshes = meshes;
168 draw.fMeshCnt = meshCnt;
Robert Phillips405413f2019-10-04 10:39:28 -0400169 draw.fOp = fOpArgs->op();
Robert Phillipscea290f2019-11-06 11:21:03 -0500170 draw.fPrimitiveType = primitiveType;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400171 if (firstDraw) {
172 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400173 }
174}
175
Brian Salomon12d22642019-01-29 14:38:50 -0500176void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
177 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400178 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
179}
180
Brian Salomon12d22642019-01-29 14:38:50 -0500181uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
182 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400183 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
184}
185
186void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500187 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400188 int* startVertex, int* actualVertexCount) {
189 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
190 startVertex, actualVertexCount);
191}
192
193uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500194 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400195 int* actualIndexCount) {
196 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
197 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
198}
199
200void GrOpFlushState::putBackIndices(int indexCount) {
201 fIndexPool.putBack(indexCount * sizeof(uint16_t));
202}
203
204void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
205 fVertexPool.putBack(vertices * vertexStride);
206}
207
208GrAppliedClip GrOpFlushState::detachAppliedClip() {
Robert Phillips405413f2019-10-04 10:39:28 -0400209 return fOpArgs->appliedClip() ? std::move(*fOpArgs->appliedClip()) : GrAppliedClip();
Brian Salomon29b60c92017-10-31 14:42:10 -0400210}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500211
Herb Derby081e6f32019-01-16 13:46:02 -0500212GrStrikeCache* GrOpFlushState::glyphCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500213 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500214}
215
Robert Phillips5a66efb2018-03-07 15:13:18 -0500216GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500217 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500218}
Brian Salomonf7232642018-09-19 08:58:08 -0400219
220//////////////////////////////////////////////////////////////////////////////
221
222GrOpFlushState::Draw::~Draw() {
223 if (fFixedDynamicState && fFixedDynamicState->fPrimitiveProcessorTextures) {
224 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400225 fFixedDynamicState->fPrimitiveProcessorTextures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400226 }
227 }
228 if (fDynamicStateArrays && fDynamicStateArrays->fPrimitiveProcessorTextures) {
229 int n = fGeometryProcessor->numTextureSamplers() * fMeshCnt;
230 const auto* textures = fDynamicStateArrays->fPrimitiveProcessorTextures;
231 for (int i = 0; i < n; ++i) {
Robert Phillips3d4cac52019-06-11 08:08:08 -0400232 textures[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400233 }
234 }
235}