blob: 14a487dd66a408b57763e3676fb82d012db45782 [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"
Robert Phillipse19babf2020-04-06 13:57:30 -040012#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#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"
Greg Daniel456f9b52020-03-05 19:14:18 +000018#include "src/gpu/GrTexture.h"
bsalomon75398562015-08-17 12:55:38 -070019
Brian Salomon7dc6e752017-11-02 11:34:51 -040020//////////////////////////////////////////////////////////////////////////////
21
Brian Salomon58f153c2018-10-18 21:51:15 -040022GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider,
Robert Phillipse5f73282019-06-18 17:15:04 -040023 GrTokenTracker* tokenTracker,
Brian Salomon601ac802019-02-07 13:37:16 -050024 sk_sp<GrBufferAllocPool::CpuBufferCache> cpuBufferCache)
25 : fVertexPool(gpu, cpuBufferCache)
26 , fIndexPool(gpu, std::move(cpuBufferCache))
Robert Phillips40a29d72018-01-18 12:59:22 -050027 , fGpu(gpu)
28 , fResourceProvider(resourceProvider)
Brian Salomonbeb7f522019-08-30 16:19:42 -040029 , fTokenTracker(tokenTracker) {}
bsalomon75398562015-08-17 12:55:38 -070030
Robert Phillips646e4292017-06-13 12:44:56 -040031const GrCaps& GrOpFlushState::caps() const {
32 return *fGpu->caps();
33}
34
Chris Dalton07cdcfc92019-02-26 11:13:22 -070035void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
Robert Phillips3968fcb2019-12-05 16:40:31 -050036 const GrOp* op, const SkRect& chainBounds, const GrPipeline* pipeline) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040037 SkASSERT(this->opsRenderPass());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070038
Brian Osmane8012102018-11-29 14:05:07 -050039 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050040 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040041 while (fCurrUpload != fInlineUploads.end() &&
42 fCurrUpload->fUploadBeforeToken == drawToken) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040043 this->opsRenderPass()->inlineUpload(this, fCurrUpload->fUpload);
Brian Salomon7dc6e752017-11-02 11:34:51 -040044 ++fCurrUpload;
45 }
Robert Phillips901aff02019-10-08 12:32:56 -040046
47 GrProgramInfo programInfo(this->proxy()->numSamples(),
Chris Dalton5e8cdfd2019-11-11 15:23:30 -070048 this->proxy()->numStencilSamples(),
Robert Phillips933484f2019-11-26 09:38:55 -050049 this->proxy()->backendFormat(),
Brian Salomon8afde5f2020-04-01 16:22:00 -040050 this->writeView()->origin(),
Robert Phillips67a625e2019-11-15 15:37:07 -050051 pipeline,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050052 fCurrDraw->fGeometryProcessor,
Robert Phillipscea290f2019-11-06 11:21:03 -050053 fCurrDraw->fPrimitiveType);
Robert Phillips901aff02019-10-08 12:32:56 -040054
Chris Dalton304e14d2020-03-17 14:29:06 -060055 this->bindPipelineAndScissorClip(programInfo, chainBounds);
56 this->bindTextures(programInfo.primProc(), fCurrDraw->fPrimProcProxies,
57 programInfo.pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -060058 for (int i = 0; i < fCurrDraw->fMeshCnt; ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -060059 this->drawMesh(fCurrDraw->fMeshes[i]);
60 }
61
Robert Phillips40a29d72018-01-18 12:59:22 -050062 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040063 ++fCurrDraw;
64 }
65}
66
67void GrOpFlushState::preExecuteDraws() {
68 fVertexPool.unmap();
69 fIndexPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050070 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040071 this->doUpload(upload);
72 }
73 // Setup execution iterators.
74 fCurrDraw = fDraws.begin();
75 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040076}
77
78void GrOpFlushState::reset() {
79 SkASSERT(fCurrDraw == fDraws.end());
80 SkASSERT(fCurrUpload == fInlineUploads.end());
81 fVertexPool.reset();
82 fIndexPool.reset();
83 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050084 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040085 fInlineUploads.reset();
86 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040087 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
88}
89
Greg Danielb20d7e52019-09-03 13:54:39 -040090void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload,
91 bool shouldPrepareSurfaceForSampling) {
92 GrDeferredTextureUploadWritePixelsFn wp = [this, shouldPrepareSurfaceForSampling](
93 GrTextureProxy* dstProxy, int left, int top, int width, int height,
94 GrColorType colorType, const void* buffer, size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040095 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon42be09d2019-07-26 12:12:26 -040096 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
97 return false;
98 }
Brian Salomon01915c02019-08-02 09:57:21 -040099 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
100 colorType, dstSurface->backendFormat(), colorType);
Brian Salomon77a684f2019-08-01 14:38:04 -0400101 size_t tightRB = width * GrColorTypeBytesPerPixel(supportedWrite.fColorType);
102 SkASSERT(rowBytes >= tightRB);
Brian Salomon1047a492019-07-02 12:25:21 -0400103 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf77c1462019-08-01 15:19:29 -0400104 if (supportedWrite.fColorType != colorType ||
Brian Salomon77a684f2019-08-01 14:38:04 -0400105 (!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) {
106 tmpPixels.reset(new char[height * tightRB]);
107 // Use kUnpremul to ensure no alpha type conversions or clamping occur.
108 static constexpr auto kAT = kUnpremul_SkAlphaType;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400109 GrImageInfo srcInfo(colorType, kAT, nullptr, width, height);
110 GrImageInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, width,
Brian Salomon77a684f2019-08-01 14:38:04 -0400111 height);
112 if (!GrConvertPixels(tmpInfo, tmpPixels.get(), tightRB, srcInfo, buffer, rowBytes)) {
113 return false;
114 }
Brian Salomon1047a492019-07-02 12:25:21 -0400115 rowBytes = tightRB;
Brian Salomon77a684f2019-08-01 14:38:04 -0400116 buffer = tmpPixels.get();
Brian Salomon1047a492019-07-02 12:25:21 -0400117 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400118 return this->fGpu->writePixels(dstSurface, left, top, width, height, colorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400119 supportedWrite.fColorType, buffer, rowBytes,
120 shouldPrepareSurfaceForSampling);
Brian Salomon9bada542017-06-12 12:09:30 -0400121 };
122 upload(wp);
123}
Brian Salomon29b60c92017-10-31 14:42:10 -0400124
125GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500126 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400127 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400128}
129
130GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500131 fASAPUploads.append(&fArena, std::move(upload));
132 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400133}
134
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700135void GrOpFlushState::recordDraw(
Robert Phillips6c59fe42020-02-27 09:30:37 -0500136 const GrGeometryProcessor* gp,
Chris Daltoneb694b72020-03-16 09:25:50 -0600137 const GrSimpleMesh meshes[],
Robert Phillips6c59fe42020-02-27 09:30:37 -0500138 int meshCnt,
Chris Dalton304e14d2020-03-17 14:29:06 -0600139 const GrSurfaceProxy* const primProcProxies[],
Robert Phillipscea290f2019-11-06 11:21:03 -0500140 GrPrimitiveType primitiveType) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400141 SkASSERT(fOpArgs);
Robert Phillips405413f2019-10-04 10:39:28 -0400142 SkDEBUGCODE(fOpArgs->validate());
Brian Salomon7dc6e752017-11-02 11:34:51 -0400143 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400144 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500145 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Chris Dalton304e14d2020-03-17 14:29:06 -0600146 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
147 SkASSERT(primProcProxies && primProcProxies[i]);
148 primProcProxies[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400149 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500150 draw.fGeometryProcessor = gp;
Chris Dalton304e14d2020-03-17 14:29:06 -0600151 draw.fPrimProcProxies = primProcProxies;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000152 draw.fMeshes = meshes;
153 draw.fMeshCnt = meshCnt;
Robert Phillips405413f2019-10-04 10:39:28 -0400154 draw.fOp = fOpArgs->op();
Robert Phillipscea290f2019-11-06 11:21:03 -0500155 draw.fPrimitiveType = primitiveType;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400156 if (firstDraw) {
157 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400158 }
159}
160
Brian Salomon12d22642019-01-29 14:38:50 -0500161void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
162 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400163 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
164}
165
Brian Salomon12d22642019-01-29 14:38:50 -0500166uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
167 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400168 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
169}
170
171void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500172 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400173 int* startVertex, int* actualVertexCount) {
174 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
175 startVertex, actualVertexCount);
176}
177
178uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500179 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400180 int* actualIndexCount) {
181 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
182 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
183}
184
185void GrOpFlushState::putBackIndices(int indexCount) {
186 fIndexPool.putBack(indexCount * sizeof(uint16_t));
187}
188
189void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
190 fVertexPool.putBack(vertices * vertexStride);
191}
192
193GrAppliedClip GrOpFlushState::detachAppliedClip() {
Robert Phillips405413f2019-10-04 10:39:28 -0400194 return fOpArgs->appliedClip() ? std::move(*fOpArgs->appliedClip()) : GrAppliedClip();
Brian Salomon29b60c92017-10-31 14:42:10 -0400195}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500196
Herb Derby081e6f32019-01-16 13:46:02 -0500197GrStrikeCache* GrOpFlushState::glyphCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500198 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500199}
200
Robert Phillips5a66efb2018-03-07 15:13:18 -0500201GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500202 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500203}
Brian Salomonf7232642018-09-19 08:58:08 -0400204
Chris Dalton765ed362020-03-16 17:34:44 -0600205void GrOpFlushState::drawMesh(const GrSimpleMesh& mesh) {
206 SkASSERT(mesh.fIsInitialized);
207 if (!mesh.fIndexBuffer) {
208 this->bindBuffers(nullptr, nullptr, mesh.fVertexBuffer.get());
209 this->draw(mesh.fVertexCount, mesh.fBaseVertex);
210 } else {
211 this->bindBuffers(mesh.fIndexBuffer.get(), nullptr, mesh.fVertexBuffer.get(),
212 mesh.fPrimitiveRestart);
213 if (0 == mesh.fPatternRepeatCount) {
214 this->drawIndexed(mesh.fIndexCount, mesh.fBaseIndex, mesh.fMinIndexValue,
215 mesh.fMaxIndexValue, mesh.fBaseVertex);
216 } else {
217 this->drawIndexPattern(mesh.fIndexCount, mesh.fPatternRepeatCount,
218 mesh.fMaxPatternRepetitionsInIndexBuffer, mesh.fVertexCount,
219 mesh.fBaseVertex);
220 }
221 }
222}
223
Brian Salomonf7232642018-09-19 08:58:08 -0400224//////////////////////////////////////////////////////////////////////////////
225
226GrOpFlushState::Draw::~Draw() {
Chris Dalton304e14d2020-03-17 14:29:06 -0600227 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
228 SkASSERT(fPrimProcProxies && fPrimProcProxies[i]);
229 fPrimProcProxies[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400230 }
Brian Salomonf7232642018-09-19 08:58:08 -0400231}