| 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 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 8 |
|
| 9 |
#include "include/core/SkAlphaType.h" |
| 10 |
#include "include/core/SkColorSpace.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 13 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 14 |
#include "src/gpu/ganesh/GrCaps.h" |
| 15 |
#include "src/gpu/ganesh/GrDataUtils.h" |
| 16 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 17 |
#include "src/gpu/ganesh/GrGpu.h" |
| 18 |
#include "src/gpu/ganesh/GrImageInfo.h" |
| 19 |
#include "src/gpu/ganesh/GrPixmap.h" |
| 20 |
#include "src/gpu/ganesh/GrProgramInfo.h" |
| 21 |
#include "src/gpu/ganesh/GrSimpleMesh.h" |
| 22 |
#include "src/gpu/ganesh/GrSurface.h" |
| 23 |
#include "src/gpu/ganesh/GrSurfaceProxy.h" |
| 24 |
#include "src/gpu/ganesh/GrTextureProxy.h" |
| 25 |
|
| 26 |
#include <functional> |
| 27 |
#include <memory> |
| 28 |
|
| 29 |
////////////////////////////////////////////////////////////////////////////// |
| 30 |
|
| 31 |
GrOpFlushState::GrOpFlushState(GrGpu* gpu, GrResourceProvider* resourceProvider, |
| 32 |
skgpu::TokenTracker* tokenTracker, |
| 33 |
sk_sp<GrBufferAllocPool::CpuBufferCache> cpuBufferCache) |
| 34 |
: fVertexPool(gpu, cpuBufferCache) |
| 35 |
, fIndexPool(gpu, cpuBufferCache) |
| 36 |
, fDrawIndirectPool(gpu, std::move(cpuBufferCache)) |
| 37 |
, fGpu(gpu) |
| 38 |
, fResourceProvider(resourceProvider) |
| 39 |
, fTokenTracker(tokenTracker) {} |
| 40 |
|
| 41 |
const GrCaps& GrOpFlushState::caps() const { |
| 42 |
return *fGpu->caps(); |
| 43 |
} |
| 44 |
|
| 45 |
GrThreadSafeCache* GrOpFlushState::threadSafeCache() const { |
| 46 |
return fGpu->getContext()->priv().threadSafeCache(); |
| 47 |
} |
| 48 |
|
| 49 |
void GrOpFlushState::executeDrawsAndUploadsForMeshDrawOp( |
| 50 |
const GrOp* op, const SkRect& chainBounds, const GrPipeline* pipeline, |
| 51 |
const GrUserStencilSettings* userStencilSettings) { |
| 52 |
SkASSERT(this->opsRenderPass()); |
| 53 |
|
| 54 |
while (fCurrDraw != fDraws.end() && fCurrDraw->fOp == op) { |
| 55 |
skgpu::Token drawToken = fTokenTracker->nextFlushToken(); |
| •56 |
|
| 57 |
bool drawUploadFailure = false; |
| 58 |
while (fCurrUpload != fInlineUploads.end() && |
| 59 |
fCurrUpload->fUploadBeforeToken == drawToken) { |
| •60 |
if (!this->opsRenderPass()->inlineUpload(this, fCurrUpload->fUpload)) { |
| 61 |
drawUploadFailure = true; |
| 62 |
} |
| 63 |
// Attempt subsequent uploads even if one fails (future draws may depend upon them). |
| 64 |
++fCurrUpload; |
| 65 |
} |
| 66 |
|
| •67 |
// If not all of the uploads succeeded, do not attempt to execute the draw and relevant |
| 68 |
// preparations. Continue on to the next draw operation (which may also fail if subsequent |
| 69 |
// draw calls depended upon these uploads, but we cannot know that at this point). |
| 70 |
if (drawUploadFailure) { |
| 71 |
fGpu->stats()->incNumFailedDraws(); |
| 72 |
} else { |
| 73 |
GrProgramInfo programInfo(this->caps(), |
| 74 |
this->writeView(), |
| 75 |
this->usesMSAASurface(), |
| 76 |
pipeline, |
| 77 |
userStencilSettings, |
| 78 |
fCurrDraw->fGeometryProcessor, |
| 79 |
fCurrDraw->fPrimitiveType, |
| 80 |
this->renderPassBarriers(), |
| 81 |
this->colorLoadOp()); |
| 82 |
|
| •83 |
this->bindPipelineAndScissorClip(programInfo, chainBounds); |
| 84 |
this->bindTextures(programInfo.geomProc(), fCurrDraw->fGeomProcProxies, |
| 85 |
programInfo.pipeline()); |
| 86 |
for (int i = 0; i < fCurrDraw->fMeshCnt; ++i) { |
| 87 |
this->drawMesh(fCurrDraw->fMeshes[i]); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
fTokenTracker->issueFlushToken(); |
| 92 |
++fCurrDraw; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void GrOpFlushState::preExecuteDraws() { |
| 97 |
fVertexPool.unmap(); |
| 98 |
fIndexPool.unmap(); |
| 99 |
fDrawIndirectPool.unmap(); |
| 100 |
for (auto& upload : fASAPUploads) { |
| 101 |
this->doUpload(upload); |
| 102 |
} |
| 103 |
// Setup execution iterators. |
| 104 |
fCurrDraw = fDraws.begin(); |
| 105 |
fCurrUpload = fInlineUploads.begin(); |
| 106 |
fGpu->willExecute(); |
| 107 |
} |
| 108 |
|
| 109 |
void GrOpFlushState::reset() { |
| 110 |
SkASSERT(fCurrDraw == fDraws.end()); |
| 111 |
SkASSERT(fCurrUpload == fInlineUploads.end()); |
| 112 |
fVertexPool.reset(); |
| 113 |
fIndexPool.reset(); |
| 114 |
fDrawIndirectPool.reset(); |
| 115 |
fArena.reset(); |
| 116 |
fASAPUploads.reset(); |
| 117 |
fInlineUploads.reset(); |
| 118 |
fDraws.reset(); |
| 119 |
fBaseDrawToken = skgpu::Token::InvalidToken(); |
| 120 |
} |
| 121 |
|
| 122 |
void GrOpFlushState::doUpload(GrDeferredTextureUploadFn& upload, |
| 123 |
bool shouldPrepareSurfaceForSampling) { |
| 124 |
GrDeferredTextureUploadWritePixelsFn wp = [this, shouldPrepareSurfaceForSampling]( |
| 125 |
GrTextureProxy* dstProxy, |
| 126 |
SkIRect rect, |
| 127 |
GrColorType colorType, |
| 128 |
const void* buffer, |
| 129 |
size_t rowBytes) { |
| 130 |
GrSurface* dstSurface = dstProxy->peekSurface(); |
| 131 |
if (!fGpu->caps()->surfaceSupportsWritePixels(dstSurface)) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
GrCaps::SupportedWrite supportedWrite = fGpu->caps()->supportedWritePixelsColorType( |
| 135 |
colorType, dstSurface->backendFormat(), colorType); |
| 136 |
size_t tightRB = rect.width()*GrColorTypeBytesPerPixel(supportedWrite.fColorType); |
| 137 |
SkASSERT(rowBytes >= tightRB); |
| 138 |
std::unique_ptr<char[]> tmpPixels; |
| 139 |
if (supportedWrite.fColorType != colorType || |
| 140 |
(!fGpu->caps()->writePixelsRowBytesSupport() && rowBytes != tightRB)) { |
| 141 |
tmpPixels.reset(new char[rect.height()*tightRB]); |
| 142 |
// Use kUnknown to ensure no alpha type conversions or clamping occur. |
| 143 |
static constexpr auto kAT = kUnknown_SkAlphaType; |
| 144 |
GrImageInfo srcInfo(colorType, kAT, nullptr, rect.size()); |
| 145 |
GrImageInfo tmpInfo(supportedWrite.fColorType, kAT, nullptr, rect.size()); |
| 146 |
if (!GrConvertPixels( GrPixmap(tmpInfo, tmpPixels.get(), tightRB ), |
| 147 |
GrCPixmap(srcInfo, buffer, rowBytes))) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
rowBytes = tightRB; |
| 151 |
buffer = tmpPixels.get(); |
| 152 |
} |
| 153 |
return this->fGpu->writePixels(dstSurface, |
| 154 |
rect, |
| 155 |
colorType, |
| 156 |
supportedWrite.fColorType, |
| 157 |
buffer, |
| 158 |
rowBytes, |
| 159 |
shouldPrepareSurfaceForSampling); |
| 160 |
}; |
| 161 |
upload(wp); |
| 162 |
} |
| 163 |
|
| 164 |
skgpu::Token GrOpFlushState::addInlineUpload(GrDeferredTextureUploadFn&& upload) { |
| 165 |
return fInlineUploads.append(&fArena, std::move(upload), fTokenTracker->nextDrawToken()) |
| 166 |
.fUploadBeforeToken; |
| 167 |
} |
| 168 |
|
| 169 |
skgpu::Token GrOpFlushState::addASAPUpload(GrDeferredTextureUploadFn&& upload) { |
| 170 |
fASAPUploads.append(&fArena, std::move(upload)); |
| 171 |
return fTokenTracker->nextFlushToken(); |
| 172 |
} |
| 173 |
|
| 174 |
void GrOpFlushState::recordDraw( |
| 175 |
const GrGeometryProcessor* geomProc, |
| 176 |
const GrSimpleMesh meshes[], |
| 177 |
int meshCnt, |
| 178 |
const GrSurfaceProxy* const geomProcProxies[], |
| 179 |
GrPrimitiveType primitiveType) { |
| 180 |
SkASSERT(fOpArgs); |
| 181 |
SkDEBUGCODE(fOpArgs->validate()); |
| 182 |
bool firstDraw = fDraws.begin() == fDraws.end(); |
| 183 |
auto& draw = fDraws.append(&fArena); |
| 184 |
skgpu::Token token = fTokenTracker->issueDrawToken(); |
| 185 |
for (int i = 0; i < geomProc->numTextureSamplers(); ++i) { |
| 186 |
SkASSERT(geomProcProxies && geomProcProxies[i]); |
| 187 |
geomProcProxies[i]->ref(); |
| 188 |
} |
| 189 |
draw.fGeometryProcessor = geomProc; |
| 190 |
draw.fGeomProcProxies = geomProcProxies; |
| 191 |
draw.fMeshes = meshes; |
| 192 |
draw.fMeshCnt = meshCnt; |
| 193 |
draw.fOp = fOpArgs->op(); |
| 194 |
draw.fPrimitiveType = primitiveType; |
| 195 |
if (firstDraw) { |
| 196 |
fBaseDrawToken = token; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void* GrOpFlushState::makeVertexSpace(size_t vertexSize, int vertexCount, |
| 201 |
sk_sp<const GrBuffer>* buffer, int* startVertex) { |
| 202 |
return fVertexPool.makeSpace(vertexSize, vertexCount, buffer, startVertex); |
| 203 |
} |
| 204 |
|
| 205 |
uint16_t* GrOpFlushState::makeIndexSpace(int indexCount, sk_sp<const GrBuffer>* buffer, |
| 206 |
int* startIndex) { |
| 207 |
return reinterpret_cast<uint16_t*>(fIndexPool.makeSpace(indexCount, buffer, startIndex)); |
| 208 |
} |
| 209 |
|
| 210 |
void* GrOpFlushState::makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, |
| 211 |
int fallbackVertexCount, sk_sp<const GrBuffer>* buffer, |
| 212 |
int* startVertex, int* actualVertexCount) { |
| 213 |
return fVertexPool.makeSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount, buffer, |
| 214 |
startVertex, actualVertexCount); |
| 215 |
} |
| 216 |
|
| 217 |
uint16_t* GrOpFlushState::makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, |
| 218 |
sk_sp<const GrBuffer>* buffer, int* startIndex, |
| 219 |
int* actualIndexCount) { |
| 220 |
return reinterpret_cast<uint16_t*>(fIndexPool.makeSpaceAtLeast( |
| 221 |
minIndexCount, fallbackIndexCount, buffer, startIndex, actualIndexCount)); |
| 222 |
} |
| 223 |
|
| 224 |
void GrOpFlushState::putBackIndices(int indexCount) { |
| 225 |
fIndexPool.putBack(indexCount * sizeof(uint16_t)); |
| 226 |
} |
| 227 |
|
| 228 |
void GrOpFlushState::putBackVertices(int vertices, size_t vertexStride) { |
| 229 |
fVertexPool.putBack(vertices * vertexStride); |
| 230 |
} |
| 231 |
|
| 232 |
GrAppliedClip GrOpFlushState::detachAppliedClip() { |
| 233 |
return fOpArgs->appliedClip() ? std::move(*fOpArgs->appliedClip()) : GrAppliedClip::Disabled(); |
| 234 |
} |
| 235 |
|
| 236 |
sktext::gpu::StrikeCache* GrOpFlushState::strikeCache() const { |
| 237 |
return fGpu->getContext()->priv().getStrikeCache(); |
| 238 |
} |
| 239 |
|
| 240 |
GrAtlasManager* GrOpFlushState::atlasManager() const { |
| 241 |
return fGpu->getContext()->priv().getAtlasManager(); |
| 242 |
} |
| 243 |
|
| 244 |
#if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
| 245 |
skgpu::ganesh::SmallPathAtlasMgr* GrOpFlushState::smallPathAtlasManager() const { |
| 246 |
return fGpu->getContext()->priv().getSmallPathAtlasMgr(); |
| 247 |
} |
| 248 |
#endif |
| 249 |
|
| 250 |
void GrOpFlushState::drawMesh(const GrSimpleMesh& mesh) { |
| 251 |
SkASSERT(mesh.fIsInitialized); |
| 252 |
if (!mesh.fIndexBuffer) { |
| 253 |
this->bindBuffers(nullptr, nullptr, mesh.fVertexBuffer); |
| 254 |
this->draw(mesh.fVertexCount, mesh.fBaseVertex); |
| 255 |
} else { |
| 256 |
this->bindBuffers(mesh.fIndexBuffer, nullptr, mesh.fVertexBuffer, mesh.fPrimitiveRestart); |
| 257 |
if (0 == mesh.fPatternRepeatCount) { |
| 258 |
this->drawIndexed(mesh.fIndexCount, mesh.fBaseIndex, mesh.fMinIndexValue, |
| 259 |
mesh.fMaxIndexValue, mesh.fBaseVertex); |
| 260 |
} else { |
| 261 |
this->drawIndexPattern(mesh.fIndexCount, mesh.fPatternRepeatCount, |
| 262 |
mesh.fMaxPatternRepetitionsInIndexBuffer, mesh.fVertexCount, |
| 263 |
mesh.fBaseVertex); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
////////////////////////////////////////////////////////////////////////////// |
| 269 |
|
| 270 |
GrOpFlushState::Draw::~Draw() { |
| 271 |
for (int i = 0; i < fGeometryProcessor->numTextureSamplers(); ++i) { |
| 272 |
SkASSERT(fGeomProcProxies && fGeomProcProxies[i]); |
| 273 |
fGeomProcProxies[i]->unref(); |
| 274 |
} |
| 275 |
} |