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