1/*
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
8#include "src/gpu/GrOpFlushState.h"
9
10#include "include/gpu/GrDirectContext.h"
11#include "src/core/SkConvertPixels.h"
12#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrDataUtils.h"
14#include "src/gpu/GrDrawOpAtlas.h"
15#include "src/gpu/GrGpu.h"
16#include "src/gpu/GrImageInfo.h"
17#include "src/gpu/GrProgramInfo.h"
18#include "src/gpu/GrResourceProvider.h"
19#include "src/gpu/GrTexture.h"
20
21//////////////////////////////////////////////////////////////////////////////
22
23GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider,
24 GrTokenTracker* tokenTracker,
25 sk_sp<GrBufferAllocPool::CpuBufferCache> cpuBufferCache)
26 : fVertexPool(gpu, cpuBufferCache)
27 , fIndexPool(gpu, cpuBufferCache)
28 , fDrawIndirectPool(gpu, std::move(cpuBufferCache))
29 , fGpu(gpu)
30 , fResourceProvider(resourceProvider)
31 , fTokenTracker(tokenTracker) {}
32
33const GrCaps& GrOpFlushState::caps() const {
34 return *fGpu->caps();
35}
36
37void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp(
38 const GrOp* op, const SkRect& chainBounds, const GrPipeline* pipeline) {
39 SkASSERT(this->opsRenderPass());
40
41 while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) {
42 GrDeferredUploadToken drawToken = fTokenTracker->nextTokenToFlush();
43 while (fCurrUpload != fInlineUploads.end() &&
44 fCurrUpload->fUploadBeforeToken == drawToken) {
45 this->opsRenderPass()->inlineUpload(this, fCurrUpload->fUpload);
46 ++fCurrUpload;
47 }
48
49 GrProgramInfo programInfo(this->proxy()->numSamples(),
50 this->proxy()->numStencilSamples(),
51 this->proxy()->backendFormat(),
52 this->writeView()->origin(),
53 pipeline,
54 fCurrDraw->fGeometryProcessor,
55 fCurrDraw->fPrimitiveType);
56
57 this->bindPipelineAndScissorClip(programInfo, chainBounds);
58 this->bindTextures(programInfo.primProc(), fCurrDraw->fPrimProcProxies,
59 programInfo.pipeline());
60 for (int i = 0; i < fCurrDraw->fMeshCnt; ++i) {
61 this->drawMesh(fCurrDraw->fMeshes[i]);
62 }
63
64 fTokenTracker->flushToken();
65 ++fCurrDraw;
66 }
67}
68
69void GrOpFlushState::preExecuteDraws() {
70 fVertexPool.unmap();
71 fIndexPool.unmap();
72 fDrawIndirectPool.unmap();
73 for (auto& upload : fASAPUploads) {
74 this->doUpload(upload);
75 }
76 // Setup execution iterators.
77 fCurrDraw = fDraws.begin();
78 fCurrUpload = fInlineUploads.begin();
79}
80
81void GrOpFlushState::reset() {
82 SkASSERT(fCurrDraw == fDraws.end());
83 SkASSERT(fCurrUpload == fInlineUploads.end());
84 fVertexPool.reset();
85 fIndexPool.reset();
86 fDrawIndirectPool.reset();
87 fArena.reset();
88 fASAPUploads.reset();
89 fInlineUploads.reset();
90 fDraws.reset();
91 fBaseDrawToken = GrDeferredUploadToken::AlreadyFlushedToken();
92}
93
94void 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) {
99 GrSurface* dstSurface = dstProxy->peekSurface();
100 if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) {
101 return false;
102 }
103 GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType(
104 colorType, dstSurface->backendFormat(), colorType);
105 size_t tightRB = width * GrColorTypeBytesPerPixel(supportedWrite.fColorType);
106 SkASSERT(rowBytes >= tightRB);
107 std::unique_ptr<char[]> tmpPixels;
108 if (supportedWrite.fColorType != colorType ||
109 (!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;
113 GrImageInfo srcInfo(colorType, kAT, nullptr, width, height);
114 GrImageInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, width,
115 height);
116 if (!GrConvertPixels(tmpInfo, tmpPixels.get(), tightRB, srcInfo, buffer, rowBytes)) {
117 return false;
118 }
119 rowBytes = tightRB;
120 buffer = tmpPixels.get();
121 }
122 return this->fGpu->writePixels(dstSurface, left, top, width, height, colorType,
123 supportedWrite.fColorType, buffer, rowBytes,
124 shouldPrepareSurfaceForSampling);
125 };
126 upload(wp);
127}
128
129GrDeferredUploadToken GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) {
130 return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken())
131 .fUploadBeforeToken;
132}
133
134GrDeferredUploadToken GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) {
135 fASAPUploads.append(&fArena, std::move(upload));
136 return fTokenTracker->nextTokenToFlush();
137}
138
139void GrOpFlushState::recordDraw(
140 const GrGeometryProcessor* gp,
141 const GrSimpleMesh meshes[],
142 int meshCnt,
143 const GrSurfaceProxy* const primProcProxies[],
144 GrPrimitiveType primitiveType) {
145 SkASSERT(fOpArgs);
146 SkDEBUGCODE(fOpArgs->validate());
147 bool firstDraw = fDraws.begin() == fDraws.end();
148 auto& draw = fDraws.append(&fArena);
149 GrDeferredUploadToken token = fTokenTracker->issueDrawToken();
150 for (int i = 0; i < gp->numTextureSamplers(); ++i) {
151 SkASSERT(primProcProxies && primProcProxies[i]);
152 primProcProxies[i]->ref();
153 }
154 draw.fGeometryProcessor = gp;
155 draw.fPrimProcProxies = primProcProxies;
156 draw.fMeshes = meshes;
157 draw.fMeshCnt = meshCnt;
158 draw.fOp = fOpArgs->op();
159 draw.fPrimitiveType = primitiveType;
160 if (firstDraw) {
161 fBaseDrawToken = token;
162 }
163}
164
165void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount,
166 sk_sp<const GrBuffer>* buffer, int* startVertex) {
167 return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex);
168}
169
170uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer,
171 int* startIndex) {
172 return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex));
173}
174
175void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
176 int fallbackVertexCount, sk_sp<const GrBuffer>* buffer,
177 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,
183 sk_sp<const GrBuffer>* buffer, int* startIndex,
184 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() {
198 return fOpArgs->appliedClip() ? std::move(*fOpArgs->appliedClip()) : GrAppliedClip::Disabled();
199}
200
201GrStrikeCache* GrOpFlushState::strikeCache() const {
202 return fGpu->getContext()->priv().getGrStrikeCache();
203}
204
205GrAtlasManager* GrOpFlushState::atlasManager() const {
206 return fGpu->getContext()->priv().getAtlasManager();
207}
208
209GrSmallPathAtlasMgr* GrOpFlushState::smallPathAtlasManager() const {
210 return fGpu->getContext()->priv().getSmallPathAtlasMgr();
211}
212
213void GrOpFlushState::drawMesh(const GrSimpleMesh& mesh) {
214 SkASSERT(mesh.fIsInitialized);
215 if (!mesh.fIndexBuffer) {
216 this->bindBuffers(nullptr, nullptr, mesh.fVertexBuffer);
217 this->draw(mesh.fVertexCount, mesh.fBaseVertex);
218 } else {
219 this->bindBuffers(mesh.fIndexBuffer, nullptr, mesh.fVertexBuffer, mesh.fPrimitiveRestart);
220 if (0 == mesh.fPatternRepeatCount) {
221 this->drawIndexed(mesh.fIndexCount, mesh.fBaseIndex, mesh.fMinIndexValue,
222 mesh.fMaxIndexValue, mesh.fBaseVertex);
223 } else {
224 this->drawIndexPattern(mesh.fIndexCount, mesh.fPatternRepeatCount,
225 mesh.fMaxPatternRepetitionsInIndexBuffer, mesh.fVertexCount,
226 mesh.fBaseVertex);
227 }
228 }
229}
230
231//////////////////////////////////////////////////////////////////////////////
232
233GrOpFlushState::Draw::~Draw() {
234 for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) {
235 SkASSERT(fPrimProcProxies && fPrimProcProxies[i]);
236 fPrimProcProxies[i]->unref();
237 }
238}
239