blob: 7f47488677c94df4b1fffb86d36ce11f9a769d6d [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040013#include "src/gpu/GrDataUtils.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
Chris Dalton07cdcfc92019-02-26 11:13:22 -070037void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
Robert Phillips3968fcb2019-12-05 16:40:31 -050038 const GrOp* op, const SkRect& chainBounds, const GrPipeline* pipeline) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040039 SkASSERT(this->opsRenderPass());
Chris Dalton07cdcfc92019-02-26 11:13:22 -070040
Brian Osmane8012102018-11-29 14:05:07 -050041 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
Robert Phillips40a29d72018-01-18 12:59:22 -050042 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
Brian Salomon7dc6e752017-11-02 11:34:51 -040043 while (fCurrUpload != fInlineUploads.end() &&
44 fCurrUpload->fUploadBeforeToken == drawToken) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -040045 this->opsRenderPass()->inlineUpload(this, fCurrUpload->fUpload);
Brian Salomon7dc6e752017-11-02 11:34:51 -040046 ++fCurrUpload;
47 }
Robert Phillips901aff02019-10-08 12:32:56 -040048
49 GrProgramInfo programInfo(this->proxy()->numSamples(),
Chris Dalton5e8cdfd2019-11-11 15:23:30 -070050 this->proxy()->numStencilSamples(),
Robert Phillips933484f2019-11-26 09:38:55 -050051 this->proxy()->backendFormat(),
Brian Salomon8afde5f2020-04-01 16:22:00 -040052 this->writeView()->origin(),
Robert Phillips67a625e2019-11-15 15:37:07 -050053 pipeline,
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050054 fCurrDraw->fGeometryProcessor,
Robert Phillipscea290f2019-11-06 11:21:03 -050055 fCurrDraw->fPrimitiveType);
Robert Phillips901aff02019-10-08 12:32:56 -040056
Chris Dalton304e14d2020-03-17 14:29:06 -060057 this->bindPipelineAndScissorClip(programInfo, chainBounds);
58 this->bindTextures(programInfo.primProc(), fCurrDraw->fPrimProcProxies,
59 programInfo.pipeline());
Chris Dalton765ed362020-03-16 17:34:44 -060060 for (int i = 0; i < fCurrDraw->fMeshCnt; ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -060061 this->drawMesh(fCurrDraw->fMeshes[i]);
62 }
63
Robert Phillips40a29d72018-01-18 12:59:22 -050064 fTokenTracker->flushToken();
Brian Salomon7dc6e752017-11-02 11:34:51 -040065 ++fCurrDraw;
66 }
67}
68
69void GrOpFlushState::preExecuteDraws() {
70 fVertexPool.unmap();
71 fIndexPool.unmap();
Chris Dalton03fdf6a2020-04-07 12:31:59 -060072 fDrawIndirectPool.unmap();
Robert Phillips40a29d72018-01-18 12:59:22 -050073 for (auto& upload : fASAPUploads) {
Brian Salomon7dc6e752017-11-02 11:34:51 -040074 this->doUpload(upload);
75 }
76 // Setup execution iterators.
77 fCurrDraw = fDraws.begin();
78 fCurrUpload = fInlineUploads.begin();
Brian Salomon7dc6e752017-11-02 11:34:51 -040079}
80
81void GrOpFlushState::reset() {
82 SkASSERT(fCurrDraw == fDraws.end());
83 SkASSERT(fCurrUpload == fInlineUploads.end());
84 fVertexPool.reset();
85 fIndexPool.reset();
Chris Dalton03fdf6a2020-04-07 12:31:59 -060086 fDrawIndirectPool.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040087 fArena.reset();
Robert Phillips40a29d72018-01-18 12:59:22 -050088 fASAPUploads.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040089 fInlineUploads.reset();
90 fDraws.reset();
Brian Salomon7dc6e752017-11-02 11:34:51 -040091 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
92}
93
Greg Danielb20d7e52019-09-03 13:54:39 -040094void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload,
95 bool shouldPrepareSurfaceForSampling) {
96 GrDeferredTextureUploadWritePixelsFn wp = [this, shouldPrepareSurfaceForSampling](
97 GrTextureProxy* dstProxy, int left, int top, int width, int height,
98 GrColorType colorType, const void* buffer, size_t rowBytes) {
Brian Salomonfd98c2c2018-07-31 17:25:29 -040099 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon42be09d2019-07-26 12:12:26 -0400100 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
101 return false;
102 }
Brian Salomon01915c02019-08-02 09:57:21 -0400103 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
104 colorType, dstSurface->backendFormat(), colorType);
Brian Salomon77a684f2019-08-01 14:38:04 -0400105 size_t tightRB = width * GrColorTypeBytesPerPixel(supportedWrite.fColorType);
106 SkASSERT(rowBytes >= tightRB);
Brian Salomon1047a492019-07-02 12:25:21 -0400107 std::unique_ptr<char[]> tmpPixels;
Brian Salomonf77c1462019-08-01 15:19:29 -0400108 if (supportedWrite.fColorType != colorType ||
Brian Salomon77a684f2019-08-01 14:38:04 -0400109 (!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) {
110 tmpPixels.reset(new char[height * tightRB]);
111 // Use kUnpremul to ensure no alpha type conversions or clamping occur.
112 static constexpr auto kAT = kUnpremul_SkAlphaType;
Brian Salomonf2ebdd92019-09-30 12:15:30 -0400113 GrImageInfo srcInfo(colorType, kAT, nullptr, width, height);
114 GrImageInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, width,
Brian Salomon77a684f2019-08-01 14:38:04 -0400115 height);
116 if (!GrConvertPixels(tmpInfo, tmpPixels.get(), tightRB, srcInfo, buffer, rowBytes)) {
117 return false;
118 }
Brian Salomon1047a492019-07-02 12:25:21 -0400119 rowBytes = tightRB;
Brian Salomon77a684f2019-08-01 14:38:04 -0400120 buffer = tmpPixels.get();
Brian Salomon1047a492019-07-02 12:25:21 -0400121 }
Brian Salomonf77c1462019-08-01 15:19:29 -0400122 return this->fGpu->writePixels(dstSurface, left, top, width, height, colorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400123 supportedWrite.fColorType, buffer, rowBytes,
124 shouldPrepareSurfaceForSampling);
Brian Salomon9bada542017-06-12 12:09:30 -0400125 };
126 upload(wp);
127}
Brian Salomon29b60c92017-10-31 14:42:10 -0400128
129GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500130 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
Brian Salomon7dc6e752017-11-02 11:34:51 -0400131 .fUploadBeforeToken;
Brian Salomon29b60c92017-10-31 14:42:10 -0400132}
133
134GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
Robert Phillips40a29d72018-01-18 12:59:22 -0500135 fASAPUploads.append(&fArena, std::move(upload));
136 return fTokenTracker->nextTokenToFlush();
Brian Salomon29b60c92017-10-31 14:42:10 -0400137}
138
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700139void GrOpFlushState::recordDraw(
Robert Phillips6c59fe42020-02-27 09:30:37 -0500140 const GrGeometryProcessor* gp,
Chris Daltoneb694b72020-03-16 09:25:50 -0600141 const GrSimpleMesh meshes[],
Robert Phillips6c59fe42020-02-27 09:30:37 -0500142 int meshCnt,
Chris Dalton304e14d2020-03-17 14:29:06 -0600143 const GrSurfaceProxy* const primProcProxies[],
Robert Phillipscea290f2019-11-06 11:21:03 -0500144 GrPrimitiveType primitiveType) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400145 SkASSERT(fOpArgs);
Robert Phillips405413f2019-10-04 10:39:28 -0400146 SkDEBUGCODE(fOpArgs->validate());
Brian Salomon7dc6e752017-11-02 11:34:51 -0400147 bool firstDraw = fDraws.begin() == fDraws.end();
Brian Salomon7dc6e752017-11-02 11:34:51 -0400148 auto& draw = fDraws.append(&fArena);
Robert Phillips40a29d72018-01-18 12:59:22 -0500149 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
Chris Dalton304e14d2020-03-17 14:29:06 -0600150 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
151 SkASSERT(primProcProxies && primProcProxies[i]);
152 primProcProxies[i]->ref();
Brian Salomonf7232642018-09-19 08:58:08 -0400153 }
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500154 draw.fGeometryProcessor = gp;
Chris Dalton304e14d2020-03-17 14:29:06 -0600155 draw.fPrimProcProxies = primProcProxies;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000156 draw.fMeshes = meshes;
157 draw.fMeshCnt = meshCnt;
Robert Phillips405413f2019-10-04 10:39:28 -0400158 draw.fOp = fOpArgs->op();
Robert Phillipscea290f2019-11-06 11:21:03 -0500159 draw.fPrimitiveType = primitiveType;
Brian Salomon7dc6e752017-11-02 11:34:51 -0400160 if (firstDraw) {
161 fBaseDrawToken = token;
Brian Salomon29b60c92017-10-31 14:42:10 -0400162 }
163}
164
Brian Salomon12d22642019-01-29 14:38:50 -0500165void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
166 sk_sp<const GrBuffer>* buffer, int* startVertex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400167 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
168}
169
Brian Salomon12d22642019-01-29 14:38:50 -0500170uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
171 int* startIndex) {
Brian Salomon29b60c92017-10-31 14:42:10 -0400172 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
173}
174
175void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500176 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
Brian Salomon29b60c92017-10-31 14:42:10 -0400177 int* startVertex, int* actualVertexCount) {
178 return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer,
179 startVertex, actualVertexCount);
180}
181
182uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
Brian Salomon12d22642019-01-29 14:38:50 -0500183 sk_sp<const GrBuffer>* buffer, int* startIndex,
Brian Salomon29b60c92017-10-31 14:42:10 -0400184 int* actualIndexCount) {
185 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast(
186 minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount));
187}
188
189void GrOpFlushState::putBackIndices(int indexCount) {
190 fIndexPool.putBack(indexCount * sizeof(uint16_t));
191}
192
193void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) {
194 fVertexPool.putBack(vertices * vertexStride);
195}
196
197GrAppliedClip GrOpFlushState::detachAppliedClip() {
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400198 return fOpArgs->appliedClip() ? std::move(*fOpArgs->appliedClip()) : GrAppliedClip::Disabled();
Brian Salomon29b60c92017-10-31 14:42:10 -0400199}
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500200
Robert Phillips207d24b2020-04-09 10:23:42 -0400201GrStrikeCache* GrOpFlushState::strikeCache() const {
Herb Derbya00da612019-03-04 17:10:01 -0500202 return fGpu->getContext()->priv().getGrStrikeCache();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500203}
204
Robert Phillips5a66efb2018-03-07 15:13:18 -0500205GrAtlasManager* GrOpFlushState::atlasManager() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500206 return fGpu->getContext()->priv().getAtlasManager();
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500207}
Brian Salomonf7232642018-09-19 08:58:08 -0400208
Chris Dalton765ed362020-03-16 17:34:44 -0600209void GrOpFlushState::drawMesh(const GrSimpleMesh& mesh) {
210 SkASSERT(mesh.fIsInitialized);
211 if (!mesh.fIndexBuffer) {
Greg Daniel426274b2020-07-20 11:37:38 -0400212 this->bindBuffers(nullptr, nullptr, mesh.fVertexBuffer);
Chris Dalton765ed362020-03-16 17:34:44 -0600213 this->draw(mesh.fVertexCount, mesh.fBaseVertex);
214 } else {
Greg Daniel426274b2020-07-20 11:37:38 -0400215 this->bindBuffers(mesh.fIndexBuffer, nullptr, mesh.fVertexBuffer, mesh.fPrimitiveRestart);
Chris Dalton765ed362020-03-16 17:34:44 -0600216 if (0 == mesh.fPatternRepeatCount) {
217 this->drawIndexed(mesh.fIndexCount, mesh.fBaseIndex, mesh.fMinIndexValue,
218 mesh.fMaxIndexValue, mesh.fBaseVertex);
219 } else {
220 this->drawIndexPattern(mesh.fIndexCount, mesh.fPatternRepeatCount,
221 mesh.fMaxPatternRepetitionsInIndexBuffer, mesh.fVertexCount,
222 mesh.fBaseVertex);
223 }
224 }
225}
226
Brian Salomonf7232642018-09-19 08:58:08 -0400227//////////////////////////////////////////////////////////////////////////////
228
229GrOpFlushState::Draw::~Draw() {
Chris Dalton304e14d2020-03-17 14:29:06 -0600230 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
231 SkASSERT(fPrimProcProxies && fPrimProcProxies[i]);
232 fPrimProcProxies[i]->unref();
Brian Salomonf7232642018-09-19 08:58:08 -0400233 }
Brian Salomonf7232642018-09-19 08:58:08 -0400234}