blob: 67da4ab0d7a723903832ad396248ff6d28a527fd [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
Robert Phillips5c809642020-11-20 12:28:45 -050054 GrProgramInfo programInfo(this->writeView(),
Robert Phillips67a625e2019-11-15 15:37:07 -050055 pipeline,
Chris Dalton1b6a43c2020-09-25 12:21:18 -060056 userStencilSettings,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050057 fCurrDraw->fGeometryProcessor,
Greg Danield358cbe2020-09-11 09:33:54 -040058 fCurrDraw->fPrimitiveType,
59 0,
Greg Daniel42dbca52020-11-20 10:22:43 -050060 this->renderPassBarriers(),
61 this->colorLoadOp());
Robert Phillips901aff02019-10-08 12:32:56 -040062
Chris Dalton304e14d2020-03-17 14:29:06 -060063 this->bindPipelineAndScissorClip(programInfo, chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -040064 this->bindTextures(programInfo.geomProc(), fCurrDraw->fGeomProcProxies,
Chris Dalton304e14d2020-03-17 14:29:06 -060065 programInfo.pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -060066 for (int i = 0; i < fCurrDraw->fMeshCnt; ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -060067 this->drawMesh(fCurrDraw->fMeshes[i]);
68 }
69
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();
Chris Dalton03fdf6a2020-04-07 12:31:59 -060078 fDrawIndirectPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050079 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040080 this->doUpload(upload);
81 }
82 // Setup execution iterators.
83 fCurrDraw = fDraws.begin();
84 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040085}
86
87void GrOpFlushState::reset() {
88 SkASSERT(fCurrDraw == fDraws.end());
89 SkASSERT(fCurrUpload == fInlineUploads.end());
90 fVertexPool.reset();
91 fIndexPool.reset();
Chris Dalton03fdf6a2020-04-07 12:31:59 -060092 fDrawIndirectPool.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040093 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050094 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040095 fInlineUploads.reset();
96 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040097 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
98}
99
Greg Danielb20d7e52019-09-03 13:54:39 -0400100void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload,
101 bool shouldPrepareSurfaceForSampling) {
102 GrDeferredTextureUploadWritePixelsFn wp = [this, shouldPrepareSurfaceForSampling](
Brian Salomone2078f12021-05-24 12:40:46 -0400103 GrTextureProxy* dstProxy,
104 SkIRect rect,
105 GrColorType colorType,
106 const void* buffer,
107 size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400108 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon42be09d2019-07-26 12:12:26 -0400109 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
110 return false;
111 }
Brian Salomon01915c02019-08-02 09:57:21 -0400112 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
113 colorType, dstSurface->backendFormat(), colorType);
Brian Salomone2078f12021-05-24 12:40:46 -0400114 size_t tightRB = rect.width()*GrColorTypeBytesPerPixel(supportedWrite.fColorType);
Brian Salomon77a684f2019-08-01 14:38:04 -0400115 SkASSERT(rowBytes >= tightRB);
Brian Salomon1047a492019-07-02 12:25:21 -0400116 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf77c1462019-08-01 15:19:29 -0400117 if (supportedWrite.fColorType != colorType ||
Brian Salomon77a684f2019-08-01 14:38:04 -0400118 (!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) {
Brian Salomone2078f12021-05-24 12:40:46 -0400119 tmpPixels.reset(new char[rect.height()*tightRB]);
Brian Salomon5392c942021-03-30 16:14:37 -0400120 // Use kUnknown to ensure no alpha type conversions or clamping occur.
121 static constexpr auto kAT = kUnknown_SkAlphaType;
Brian Salomone2078f12021-05-24 12:40:46 -0400122 GrImageInfo srcInfo(colorType, kAT, nullptr, rect.size());
123 GrImageInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, rect.size());
Brian Salomon5392c942021-03-30 16:14:37 -0400124 if (!GrConvertPixels( GrPixmap(tmpInfo, tmpPixels.get(), tightRB ),
125 GrCPixmap(srcInfo, buffer, rowBytes))) {
Brian Salomon77a684f2019-08-01 14:38:04 -0400126 return false;
127 }
Brian Salomon1047a492019-07-02 12:25:21 -0400128 rowBytes = tightRB;
Brian Salomon77a684f2019-08-01 14:38:04 -0400129 buffer = tmpPixels.get();
Brian Salomon1047a492019-07-02 12:25:21 -0400130 }
Brian Salomone2078f12021-05-24 12:40:46 -0400131 return this->fGpu->writePixels(dstSurface,
132 rect,
133 colorType,
134 supportedWrite.fColorType,
135 buffer,
136 rowBytes,
Greg Danielb20d7e52019-09-03 13:54:39 -0400137 shouldPrepareSurfaceForSampling);
Brian Salomon9bada542017-06-12 12:09:30 -0400138 };
139 upload(wp);
140}
Brian Salomon29b60c92017-10-31 14:42:10 -0400141
142GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500143 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400144 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400145}
146
147GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500148 fASAPUploads.append(&fArena, std::move(upload));
149 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400150}
151
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700152void GrOpFlushState::recordDraw(
Robert Phillips787fd9d2021-03-22 14:48:09 -0400153 const GrGeometryProcessor* geomProc,
Chris Daltoneb694b72020-03-16 09:25:50 -0600154 const GrSimpleMesh meshes[],
Robert Phillips6c59fe42020-02-27 09:30:37 -0500155 int meshCnt,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400156 const GrSurfaceProxy* const geomProcProxies[],
Robert Phillipscea290f2019-11-06 11:21:03 -0500157 GrPrimitiveType primitiveType) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400158 SkASSERT(fOpArgs);
Robert Phillips405413f2019-10-04 10:39:28 -0400159 SkDEBUGCODE(fOpArgs->validate());
Brian Salomon7dc6e752017-11-02 11:34:51 -0400160 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400161 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500162 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Robert Phillips787fd9d2021-03-22 14:48:09 -0400163 for (int i = 0; i < geomProc->numTextureSamplers(); ++i) {
164 SkASSERT(geomProcProxies && geomProcProxies[i]);
165 geomProcProxies[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400166 }
Robert Phillips787fd9d2021-03-22 14:48:09 -0400167 draw.fGeometryProcessor = geomProc;
168 draw.fGeomProcProxies = geomProcProxies;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000169 draw.fMeshes = meshes;
170 draw.fMeshCnt = meshCnt;
Robert Phillips405413f2019-10-04 10:39:28 -0400171 draw.fOp = fOpArgs->op();
Robert Phillipscea290f2019-11-06 11:21:03 -0500172 draw.fPrimitiveType = primitiveType;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400173 if (firstDraw) {
174 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400175 }
176}
177
Brian Salomon12d22642019-01-29 14:38:50 -0500178void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
179 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400180 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
181}
182
Brian Salomon12d22642019-01-29 14:38:50 -0500183uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
184 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400185 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
186}
187
188void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500189 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400190 int* startVertex, int* actualVertexCount) {
191 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
192 startVertex, actualVertexCount);
193}
194
195uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500196 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400197 int* actualIndexCount) {
198 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
199 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
200}
201
202void GrOpFlushState::putBackIndices(int indexCount) {
203 fIndexPool.putBack(indexCount * sizeof(uint16_t));
204}
205
206void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
207 fVertexPool.putBack(vertices * vertexStride);
208}
209
210GrAppliedClip GrOpFlushState::detachAppliedClip() {
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400211 return fOpArgs->appliedClip() ? std::move(*fOpArgs->appliedClip()) : GrAppliedClip::Disabled();
Brian Salomon29b60c92017-10-31 14:42:10 -0400212}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500213
Robert Phillips207d24b2020-04-09 10:23:42 -0400214GrStrikeCache* GrOpFlushState::strikeCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500215 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500216}
217
Robert Phillips5a66efb2018-03-07 15:13:18 -0500218GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500219 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500220}
Brian Salomonf7232642018-09-19 08:58:08 -0400221
Robert Phillips5edf5102020-08-10 16:30:36 -0400222GrSmallPathAtlasMgr* GrOpFlushState::smallPathAtlasManager() const {
223 return fGpu->getContext()->priv().getSmallPathAtlasMgr();
224}
225
Chris Dalton765ed362020-03-16 17:34:44 -0600226void GrOpFlushState::drawMesh(const GrSimpleMesh& mesh) {
227 SkASSERT(mesh.fIsInitialized);
228 if (!mesh.fIndexBuffer) {
Greg Daniel426274b2020-07-20 11:37:38 -0400229 this->bindBuffers(nullptr, nullptr, mesh.fVertexBuffer);
Chris Dalton765ed362020-03-16 17:34:44 -0600230 this->draw(mesh.fVertexCount, mesh.fBaseVertex);
231 } else {
Greg Daniel426274b2020-07-20 11:37:38 -0400232 this->bindBuffers(mesh.fIndexBuffer, nullptr, mesh.fVertexBuffer, mesh.fPrimitiveRestart);
Chris Dalton765ed362020-03-16 17:34:44 -0600233 if (0 == mesh.fPatternRepeatCount) {
234 this->drawIndexed(mesh.fIndexCount, mesh.fBaseIndex, mesh.fMinIndexValue,
235 mesh.fMaxIndexValue, mesh.fBaseVertex);
236 } else {
237 this->drawIndexPattern(mesh.fIndexCount, mesh.fPatternRepeatCount,
238 mesh.fMaxPatternRepetitionsInIndexBuffer, mesh.fVertexCount,
239 mesh.fBaseVertex);
240 }
241 }
242}
243
Brian Salomonf7232642018-09-19 08:58:08 -0400244//////////////////////////////////////////////////////////////////////////////
245
246GrOpFlushState::Draw::~Draw() {
Chris Dalton304e14d2020-03-17 14:29:06 -0600247 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
Robert Phillips787fd9d2021-03-22 14:48:09 -0400248 SkASSERT(fGeomProcProxies && fGeomProcProxies[i]);
249 fGeomProcProxies[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400250 }
Brian Salomonf7232642018-09-19 08:58:08 -0400251}