blob: 68e4c50af64fa97c47d2b8d57d219bec3ec8c6b5 [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
Adlai Holler3d0359a2020-07-09 15:35:55 -040010#include "include/gpu/GrDirectContext.h"
Brian Salomon1047a492019-07-02 12:25:21 -040011#include "src/core/SkConvertPixels.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040012#include "src/gpu/GrDataUtils.h"
Adlai Hollera0693042020-10-14 11:23:11 -040013#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrDrawOpAtlas.h"
15#include "src/gpu/GrGpu.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040016#include "src/gpu/GrImageInfo.h"
Robert Phillips901aff02019-10-08 12:32:56 -040017#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrResourceProvider.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000019#include "src/gpu/GrTexture.h"
bsalomon75398562015-08-17 12:55:38 -070020
Brian Salomon7dc6e752017-11-02 11:34:51 -040021//////////////////////////////////////////////////////////////////////////////
22
Brian Salomon58f153c2018-10-18 21:51:15 -040023GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider,
Robert Phillipse5f73282019-06-18 17:15:04 -040024 GrTokenTracker* tokenTracker,
Brian Salomon601ac802019-02-07 13:37:16 -050025 sk_sp<GrBufferAllocPool::CpuBufferCache> cpuBufferCache)
26 : fVertexPool(gpu, cpuBufferCache)
Chris Dalton03fdf6a2020-04-07 12:31:59 -060027 , fIndexPool(gpu, cpuBufferCache)
28 , fDrawIndirectPool(gpu, std::move(cpuBufferCache))
Robert Phillips40a29d72018-01-18 12:59:22 -050029 , fGpu(gpu)
30 , fResourceProvider(resourceProvider)
Brian Salomonbeb7f522019-08-30 16:19:42 -040031 , fTokenTracker(tokenTracker) {}
bsalomon75398562015-08-17 12:55:38 -070032
Robert Phillips646e4292017-06-13 12:44:56 -040033const GrCaps& GrOpFlushState::caps() const {
34 return *fGpu->caps();
35}
36
Robert Phillips83c38a82020-10-28 14:57:53 -040037GrThreadSafeCache* GrOpFlushState::threadSafeCache() const {
38 return fGpu->getContext()->priv().threadSafeCache();
39}
40
Chris Dalton07cdcfc92019-02-26 11:13:22 -070041void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
Chris Dalton1b6a43c2020-09-25 12:21:18 -060042 const GrOp* op, const SkRect& chainBounds, const GrPipeline* pipeline,
43 const GrUserStencilSettings* userStencilSettings) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040044 SkASSERT(this->opsRenderPass());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070045
Brian Osmane8012102018-11-29 14:05:07 -050046 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050047 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040048 while (fCurrUpload != fInlineUploads.end() &&
49 fCurrUpload->fUploadBeforeToken == drawToken) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040050 this->opsRenderPass()->inlineUpload(this, fCurrUpload->fUpload);
Brian Salomon7dc6e752017-11-02 11:34:51 -040051 ++fCurrUpload;
52 }
Robert Phillips901aff02019-10-08 12:32:56 -040053
54 GrProgramInfo programInfo(this->proxy()->numSamples(),
Chris Dalton5e8cdfd2019-11-11 15:23:30 -070055 this->proxy()->numStencilSamples(),
Robert Phillips933484f2019-11-26 09:38:55 -050056 this->proxy()->backendFormat(),
Adlai Hollere2296f72020-11-19 13:41:26 -050057 this->writeView().origin(),
Robert Phillips67a625e2019-11-15 15:37:07 -050058 pipeline,
Chris Dalton1b6a43c2020-09-25 12:21:18 -060059 userStencilSettings,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050060 fCurrDraw->fGeometryProcessor,
Greg Danield358cbe2020-09-11 09:33:54 -040061 fCurrDraw->fPrimitiveType,
62 0,
63 this->renderPassBarriers());
Robert Phillips901aff02019-10-08 12:32:56 -040064
Chris Dalton304e14d2020-03-17 14:29:06 -060065 this->bindPipelineAndScissorClip(programInfo, chainBounds);
66 this->bindTextures(programInfo.primProc(), fCurrDraw->fPrimProcProxies,
67 programInfo.pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -060068 for (int i = 0; i < fCurrDraw->fMeshCnt; ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -060069 this->drawMesh(fCurrDraw->fMeshes[i]);
70 }
71
Robert Phillips40a29d72018-01-18 12:59:22 -050072 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040073 ++fCurrDraw;
74 }
75}
76
77void GrOpFlushState::preExecuteDraws() {
78 fVertexPool.unmap();
79 fIndexPool.unmap();
Chris Dalton03fdf6a2020-04-07 12:31:59 -060080 fDrawIndirectPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050081 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040082 this->doUpload(upload);
83 }
84 // Setup execution iterators.
85 fCurrDraw = fDraws.begin();
86 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040087}
88
89void GrOpFlushState::reset() {
90 SkASSERT(fCurrDraw == fDraws.end());
91 SkASSERT(fCurrUpload == fInlineUploads.end());
92 fVertexPool.reset();
93 fIndexPool.reset();
Chris Dalton03fdf6a2020-04-07 12:31:59 -060094 fDrawIndirectPool.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040095 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050096 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040097 fInlineUploads.reset();
98 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040099 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
100}
101
Greg Danielb20d7e52019-09-03 13:54:39 -0400102void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload,
103 bool shouldPrepareSurfaceForSampling) {
104 GrDeferredTextureUploadWritePixelsFn wp = [this, shouldPrepareSurfaceForSampling](
105 GrTextureProxy* dstProxy, int left, int top, int width, int height,
106 GrColorType colorType, const void* buffer, size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400107 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon42be09d2019-07-26 12:12:26 -0400108 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
109 return false;
110 }
Brian Salomon01915c02019-08-02 09:57:21 -0400111 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
112 colorType, dstSurface->backendFormat(), colorType);
Brian Salomon77a684f2019-08-01 14:38:04 -0400113 size_t tightRB = width * GrColorTypeBytesPerPixel(supportedWrite.fColorType);
114 SkASSERT(rowBytes >= tightRB);
Brian Salomon1047a492019-07-02 12:25:21 -0400115 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf77c1462019-08-01 15:19:29 -0400116 if (supportedWrite.fColorType != colorType ||
Brian Salomon77a684f2019-08-01 14:38:04 -0400117 (!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) {
118 tmpPixels.reset(new char[height * tightRB]);
119 // Use kUnpremul to ensure no alpha type conversions or clamping occur.
120 static constexpr auto kAT = kUnpremul_SkAlphaType;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400121 GrImageInfo srcInfo(colorType, kAT, nullptr, width, height);
122 GrImageInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, width,
Brian Salomon77a684f2019-08-01 14:38:04 -0400123 height);
124 if (!GrConvertPixels(tmpInfo, tmpPixels.get(), tightRB, srcInfo, buffer, rowBytes)) {
125 return false;
126 }
Brian Salomon1047a492019-07-02 12:25:21 -0400127 rowBytes = tightRB;
Brian Salomon77a684f2019-08-01 14:38:04 -0400128 buffer = tmpPixels.get();
Brian Salomon1047a492019-07-02 12:25:21 -0400129 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400130 return this->fGpu->writePixels(dstSurface, left, top, width, height, colorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400131 supportedWrite.fColorType, buffer, rowBytes,
132 shouldPrepareSurfaceForSampling);
Brian Salomon9bada542017-06-12 12:09:30 -0400133 };
134 upload(wp);
135}
Brian Salomon29b60c92017-10-31 14:42:10 -0400136
137GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500138 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400139 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400140}
141
142GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500143 fASAPUploads.append(&fArena, std::move(upload));
144 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400145}
146
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700147void GrOpFlushState::recordDraw(
Robert Phillips6c59fe42020-02-27 09:30:37 -0500148 const GrGeometryProcessor* gp,
Chris Daltoneb694b72020-03-16 09:25:50 -0600149 const GrSimpleMesh meshes[],
Robert Phillips6c59fe42020-02-27 09:30:37 -0500150 int meshCnt,
Chris Dalton304e14d2020-03-17 14:29:06 -0600151 const GrSurfaceProxy* const primProcProxies[],
Robert Phillipscea290f2019-11-06 11:21:03 -0500152 GrPrimitiveType primitiveType) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400153 SkASSERT(fOpArgs);
Robert Phillips405413f2019-10-04 10:39:28 -0400154 SkDEBUGCODE(fOpArgs->validate());
Brian Salomon7dc6e752017-11-02 11:34:51 -0400155 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400156 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500157 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Chris Dalton304e14d2020-03-17 14:29:06 -0600158 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
159 SkASSERT(primProcProxies && primProcProxies[i]);
160 primProcProxies[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400161 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500162 draw.fGeometryProcessor = gp;
Chris Dalton304e14d2020-03-17 14:29:06 -0600163 draw.fPrimProcProxies = primProcProxies;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000164 draw.fMeshes = meshes;
165 draw.fMeshCnt = meshCnt;
Robert Phillips405413f2019-10-04 10:39:28 -0400166 draw.fOp = fOpArgs->op();
Robert Phillipscea290f2019-11-06 11:21:03 -0500167 draw.fPrimitiveType = primitiveType;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400168 if (firstDraw) {
169 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400170 }
171}
172
Brian Salomon12d22642019-01-29 14:38:50 -0500173void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
174 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400175 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
176}
177
Brian Salomon12d22642019-01-29 14:38:50 -0500178uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
179 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400180 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
181}
182
183void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500184 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400185 int* startVertex, int* actualVertexCount) {
186 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
187 startVertex, actualVertexCount);
188}
189
190uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500191 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400192 int* actualIndexCount) {
193 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
194 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
195}
196
197void GrOpFlushState::putBackIndices(int indexCount) {
198 fIndexPool.putBack(indexCount * sizeof(uint16_t));
199}
200
201void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
202 fVertexPool.putBack(vertices * vertexStride);
203}
204
205GrAppliedClip GrOpFlushState::detachAppliedClip() {
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400206 return fOpArgs->appliedClip() ? std::move(*fOpArgs->appliedClip()) : GrAppliedClip::Disabled();
Brian Salomon29b60c92017-10-31 14:42:10 -0400207}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500208
Robert Phillips207d24b2020-04-09 10:23:42 -0400209GrStrikeCache* GrOpFlushState::strikeCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500210 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500211}
212
Robert Phillips5a66efb2018-03-07 15:13:18 -0500213GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500214 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500215}
Brian Salomonf7232642018-09-19 08:58:08 -0400216
Robert Phillips5edf5102020-08-10 16:30:36 -0400217GrSmallPathAtlasMgr* GrOpFlushState::smallPathAtlasManager() const {
218 return fGpu->getContext()->priv().getSmallPathAtlasMgr();
219}
220
Chris Dalton765ed362020-03-16 17:34:44 -0600221void GrOpFlushState::drawMesh(const GrSimpleMesh& mesh) {
222 SkASSERT(mesh.fIsInitialized);
223 if (!mesh.fIndexBuffer) {
Greg Daniel426274b2020-07-20 11:37:38 -0400224 this->bindBuffers(nullptr, nullptr, mesh.fVertexBuffer);
Chris Dalton765ed362020-03-16 17:34:44 -0600225 this->draw(mesh.fVertexCount, mesh.fBaseVertex);
226 } else {
Greg Daniel426274b2020-07-20 11:37:38 -0400227 this->bindBuffers(mesh.fIndexBuffer, nullptr, mesh.fVertexBuffer, mesh.fPrimitiveRestart);
Chris Dalton765ed362020-03-16 17:34:44 -0600228 if (0 == mesh.fPatternRepeatCount) {
229 this->drawIndexed(mesh.fIndexCount, mesh.fBaseIndex, mesh.fMinIndexValue,
230 mesh.fMaxIndexValue, mesh.fBaseVertex);
231 } else {
232 this->drawIndexPattern(mesh.fIndexCount, mesh.fPatternRepeatCount,
233 mesh.fMaxPatternRepetitionsInIndexBuffer, mesh.fVertexCount,
234 mesh.fBaseVertex);
235 }
236 }
237}
238
Brian Salomonf7232642018-09-19 08:58:08 -0400239//////////////////////////////////////////////////////////////////////////////
240
241GrOpFlushState::Draw::~Draw() {
Chris Dalton304e14d2020-03-17 14:29:06 -0600242 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
243 SkASSERT(fPrimProcProxies && fPrimProcProxies[i]);
244 fPrimProcProxies[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400245 }
Brian Salomonf7232642018-09-19 08:58:08 -0400246}