Make GrResourceProvider more sk_spified

Change-Id: If191553093031705756358f4551a5b1e96439742
Reviewed-on: https://skia-review.googlesource.com/60000
Commit-Queue: Brian Salomon <bsalomon@google.com>
Reviewed-by: Robert Phillips <robertphillips@google.com>
This commit is contained in:
Brian Salomon 2017-10-16 13:01:07 -04:00 committed by Skia Commit-Bot
parent 1bd76c468c
commit d28a79d495
20 changed files with 73 additions and 94 deletions

View File

@ -76,7 +76,7 @@ sk_sp<GrBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(const GrUnique
GrBufferType intendedType,
size_t size, const void* data) {
GrResourceProvider* rp = fDrawingMgr->getContext()->resourceProvider();
sk_sp<GrBuffer> buffer(rp->findAndRefTByUniqueKey<GrBuffer>(key));
sk_sp<GrBuffer> buffer(rp->findByUniqueKey<GrBuffer>(key));
if (!buffer) {
buffer.reset(rp->createBuffer(size, intendedType, kStatic_GrAccessPattern, 0, data));
if (!buffer) {

View File

@ -70,13 +70,13 @@ void GrRenderTarget::onAbandon() {
///////////////////////////////////////////////////////////////////////////////
bool GrRenderTargetPriv::attachStencilAttachment(GrStencilAttachment* stencil) {
bool GrRenderTargetPriv::attachStencilAttachment(sk_sp<GrStencilAttachment> stencil) {
if (!stencil && !fRenderTarget->fStencilAttachment) {
// No need to do any work since we currently don't have a stencil attachment and
// we're not actually adding one.
return true;
}
fRenderTarget->fStencilAttachment = stencil;
fRenderTarget->fStencilAttachment = stencil.release();
if (!fRenderTarget->completeStencilAttachment()) {
SkSafeSetNull(fRenderTarget->fStencilAttachment);
return false;

View File

@ -28,7 +28,7 @@ public:
* currently attached GrStencilAttachment will be removed if one was previously attached. This
* function returns false if there were any failure in attaching the GrStencilAttachment.
*/
bool attachStencilAttachment(GrStencilAttachment* stencil);
bool attachStencilAttachment(sk_sp<GrStencilAttachment> stencil);
int numStencilBits() const;

View File

@ -284,20 +284,10 @@ void GrResourceProvider::removeUniqueKeyFromProxy(const GrUniqueKey& key, GrText
fCache->processInvalidProxyUniqueKey(key, proxy, true);
}
GrGpuResource* GrResourceProvider::findAndRefResourceByUniqueKey(const GrUniqueKey& key) {
sk_sp<GrGpuResource> GrResourceProvider::findResourceByUniqueKey(const GrUniqueKey& key) {
ASSERT_SINGLE_OWNER
return this->isAbandoned() ? nullptr : fCache->findAndRefUniqueResource(key);
}
GrTexture* GrResourceProvider::findAndRefTextureByUniqueKey(const GrUniqueKey& key) {
ASSERT_SINGLE_OWNER
GrGpuResource* resource = this->findAndRefResourceByUniqueKey(key);
if (resource) {
GrTexture* texture = static_cast<GrSurface*>(resource)->asTexture();
SkASSERT(texture);
return texture;
}
return nullptr;
return this->isAbandoned() ? nullptr
: sk_sp<GrGpuResource>(fCache->findAndRefUniqueResource(key));
}
void GrResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key, GrTextureProxy* proxy) {
@ -322,7 +312,7 @@ sk_sp<GrTextureProxy> GrResourceProvider::findOrCreateProxyByUniqueKey(const GrU
return this->isAbandoned() ? nullptr : fCache->findOrCreateProxyByUniqueKey(key, origin);
}
const GrBuffer* GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
sk_sp<const GrBuffer> GrResourceProvider::createPatternedIndexBuffer(const uint16_t* pattern,
int patternSize,
int reps,
int vertCount,
@ -356,15 +346,14 @@ const GrBuffer* GrResourceProvider::createPatternedIndexBuffer(const uint16_t* p
buffer->unmap();
}
this->assignUniqueKeyToResource(key, buffer.get());
return buffer.release();
return std::move(buffer);
}
static constexpr int kMaxQuads = 1 << 12; // max possible: (1 << 14) - 1;
const GrBuffer* GrResourceProvider::createQuadIndexBuffer() {
sk_sp<const GrBuffer> GrResourceProvider::createQuadIndexBuffer() {
GR_STATIC_ASSERT(4 * kMaxQuads <= 65535);
static const uint16_t kPattern[] = { 0, 1, 2, 0, 2, 3 };
return this->createPatternedIndexBuffer(kPattern, 6, kMaxQuads, 4, fQuadIndexBufferKey);
}
@ -461,24 +450,23 @@ bool GrResourceProvider::attachStencilAttachment(GrRenderTarget* rt) {
SkDEBUGCODE(bool newStencil = false;)
GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
rt->numStencilSamples(), &sbKey);
GrStencilAttachment* stencil = static_cast<GrStencilAttachment*>(
this->findAndRefResourceByUniqueKey(sbKey));
auto stencil = this->findByUniqueKey<GrStencilAttachment>(sbKey);
if (!stencil) {
// Need to try and create a new stencil
stencil = this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height);
stencil.reset(this->gpu()->createStencilAttachmentForRenderTarget(rt, width, height));
if (stencil) {
this->assignUniqueKeyToResource(sbKey, stencil);
this->assignUniqueKeyToResource(sbKey, stencil.get());
SkDEBUGCODE(newStencil = true;)
}
}
if (rt->renderTargetPriv().attachStencilAttachment(stencil)) {
if (rt->renderTargetPriv().attachStencilAttachment(std::move(stencil))) {
#ifdef SK_DEBUG
// Fill the SB with an inappropriate value. opLists that use the
// SB should clear it properly.
if (newStencil) {
SkASSERT(stencil->isDirty());
SkASSERT(rt->renderTargetPriv().getStencilAttachment()->isDirty());
this->gpu()->clearStencil(rt, 0xFFFF);
SkASSERT(stencil->isDirty());
SkASSERT(rt->renderTargetPriv().getStencilAttachment()->isDirty());
}
#endif
}

View File

@ -41,8 +41,14 @@ class GrResourceProvider {
public:
GrResourceProvider(GrGpu* gpu, GrResourceCache* cache, GrSingleOwner* owner);
template <typename T> T* findAndRefTByUniqueKey(const GrUniqueKey& key) {
return static_cast<T*>(this->findAndRefResourceByUniqueKey(key));
/**
* Finds a resource in the cache, based on the specified key. Prior to calling this, the caller
* must be sure that if a resource of exists in the cache with the given unique key then it is
* of type T.
*/
template <typename T>
sk_sp<T> findByUniqueKey(const GrUniqueKey& key) {
return sk_sp<T>(static_cast<T*>(this->findResourceByUniqueKey(key).release()));
}
/*
@ -138,12 +144,12 @@ public:
*
* @return The index buffer if successful, otherwise nullptr.
*/
const GrBuffer* findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
sk_sp<const GrBuffer> findOrCreatePatternedIndexBuffer(const uint16_t* pattern,
int patternSize,
int reps,
int vertCount,
const GrUniqueKey& key) {
if (GrBuffer* buffer = this->findAndRefTByUniqueKey<GrBuffer>(key)) {
if (auto buffer = this->findByUniqueKey<GrBuffer>(key)) {
return buffer;
}
return this->createPatternedIndexBuffer(pattern, patternSize, reps, vertCount, key);
@ -156,9 +162,8 @@ public:
* Draw with GrPrimitiveType::kTriangles
* @ return the quad index buffer
*/
const GrBuffer* refQuadIndexBuffer() {
if (GrBuffer* buffer =
this->findAndRefTByUniqueKey<GrBuffer>(fQuadIndexBufferKey)) {
sk_sp<const GrBuffer> refQuadIndexBuffer() {
if (auto buffer = this->findByUniqueKey<const GrBuffer>(fQuadIndexBufferKey)) {
return buffer;
}
return this->createQuadIndexBuffer();
@ -230,13 +235,6 @@ public:
*/
void assignUniqueKeyToResource(const GrUniqueKey&, GrGpuResource*);
/**
* Finds a resource in the cache, based on the specified key. This is intended for use in
* conjunction with addResourceToCache(). The return value will be NULL if not found. The
* caller must balance with a call to unref().
*/
GrGpuResource* findAndRefResourceByUniqueKey(const GrUniqueKey&);
sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned = true);
sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
@ -263,7 +261,7 @@ public:
const GrCaps* caps() const { return fCaps.get(); }
private:
GrTexture* findAndRefTextureByUniqueKey(const GrUniqueKey& key);
sk_sp<GrGpuResource> findResourceByUniqueKey(const GrUniqueKey&);
// Attempts to find a resource in the cache that exactly matches the GrSurfaceDesc. Failing that
// it returns null. If non-null, the resulting texture is always budgeted.
@ -286,13 +284,13 @@ private:
return !SkToBool(fCache);
}
const GrBuffer* createPatternedIndexBuffer(const uint16_t* pattern,
sk_sp<const GrBuffer> createPatternedIndexBuffer(const uint16_t* pattern,
int patternSize,
int reps,
int vertCount,
const GrUniqueKey& key);
const GrBuffer* createQuadIndexBuffer();
sk_sp<const GrBuffer> createQuadIndexBuffer();
GrResourceCache* fCache;
GrGpu* fGpu;

View File

@ -32,7 +32,7 @@ static const int kNumAAFillRectsInIndexBuffer = 256;
static const int kVertsPerAAFillRect = 8;
static const int kIndicesPerAAFillRect = 30;
const GrBuffer* get_index_buffer(GrResourceProvider* resourceProvider) {
static sk_sp<const GrBuffer> get_index_buffer(GrResourceProvider* resourceProvider) {
GR_DEFINE_STATIC_UNIQUE_KEY(gAAFillRectIndexBufferKey);
// clang-format off
@ -251,7 +251,7 @@ private:
size_t vertexStride = gp->getVertexStride();
sk_sp<const GrBuffer> indexBuffer(get_index_buffer(target->resourceProvider()));
sk_sp<const GrBuffer> indexBuffer = get_index_buffer(target->resourceProvider());
PatternHelper helper(GrPrimitiveType::kTriangles);
void* vertices =
helper.init(target, vertexStride, indexBuffer.get(), kVertsPerAAFillRect,

View File

@ -62,7 +62,7 @@ static const int kQuadNumVertices = 5;
static const int kQuadsNumInIdxBuffer = 256;
GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
static const GrBuffer* ref_quads_index_buffer(GrResourceProvider* resourceProvider) {
static sk_sp<const GrBuffer> get_quads_index_buffer(GrResourceProvider* resourceProvider) {
GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
return resourceProvider->findOrCreatePatternedIndexBuffer(
kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices,
@ -96,7 +96,7 @@ static const int kLineSegsNumInIdxBuffer = 256;
GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
static const GrBuffer* ref_lines_index_buffer(GrResourceProvider* resourceProvider) {
static sk_sp<const GrBuffer> get_lines_index_buffer(GrResourceProvider* resourceProvider) {
GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
return resourceProvider->findOrCreatePatternedIndexBuffer(
kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices,
@ -945,8 +945,7 @@ void AAHairlineOp::onPrepareDraws(Target* target) {
*geometryProcessorViewM);
}
sk_sp<const GrBuffer> linesIndexBuffer(
ref_lines_index_buffer(target->resourceProvider()));
sk_sp<const GrBuffer> linesIndexBuffer = get_lines_index_buffer(target->resourceProvider());
const GrBuffer* vertexBuffer;
int firstVertex;
@ -994,8 +993,7 @@ void AAHairlineOp::onPrepareDraws(Target* target) {
const GrBuffer* vertexBuffer;
int firstVertex;
sk_sp<const GrBuffer> quadsIndexBuffer(
ref_quads_index_buffer(target->resourceProvider()));
sk_sp<const GrBuffer> quadsIndexBuffer = get_quads_index_buffer(target->resourceProvider());
size_t vertexStride = sizeof(BezierVertex);
int vertexCount = kQuadNumVertices * quadCount + kQuadNumVertices * conicCount;

View File

@ -213,7 +213,7 @@ private:
static const int kBevelVertexCnt = 24;
static const int kNumBevelRectsInIndexBuffer = 256;
static const GrBuffer* GetIndexBuffer(GrResourceProvider* resourceProvider, bool miterStroke);
static sk_sp<const GrBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke);
const SkMatrix& viewMatrix() const { return fViewMatrix; }
bool miterStroke() const { return fMiterStroke; }
@ -272,8 +272,7 @@ void AAStrokeRectOp::onPrepareDraws(Target* target) {
int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt;
int instanceCount = fRects.count();
const sk_sp<const GrBuffer> indexBuffer(
GetIndexBuffer(target->resourceProvider(), this->miterStroke()));
sk_sp<const GrBuffer> indexBuffer = GetIndexBuffer(target->resourceProvider(), this->miterStroke());
PatternHelper helper(GrPrimitiveType::kTriangles);
void* vertices =
helper.init(target, vertexStride, indexBuffer.get(),
@ -301,7 +300,7 @@ void AAStrokeRectOp::onPrepareDraws(Target* target) {
helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
}
const GrBuffer* AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
sk_sp<const GrBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider,
bool miterStroke) {
if (miterStroke) {
// clang-format off

View File

@ -117,7 +117,7 @@ void GrAtlasTextOp::onPrepareDraws(Target* target) {
void* vertices = target->makeVertexSpace(
vertexStride, glyphCount * kVerticesPerGlyph, &vertexBuffer, &flushInfo.fVertexOffset);
flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
if (!vertices || !flushInfo.fVertexBuffer) {
SkDebugf("Could not allocate vertices\n");
return;

View File

@ -106,7 +106,7 @@ private:
return;
}
sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
PatternHelper helper(GrPrimitiveType::kTriangles);
void* vertices = helper.init(target, vertexStride, indexBuffer.get(), kVertsPerRect,
kIndicesPerRect, numRects);

View File

@ -50,7 +50,7 @@ void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryPro
}
void* GrMeshDrawOp::QuadHelper::init(Target* target, size_t vertexStride, int quadsToDraw) {
sk_sp<const GrBuffer> quadIndexBuffer(target->resourceProvider()->refQuadIndexBuffer());
sk_sp<const GrBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
if (!quadIndexBuffer) {
SkDebugf("Could not get quad index buffer.");
return nullptr;

View File

@ -182,7 +182,7 @@ private:
size_t vertexStride = gp->getVertexStride();
int rectCount = fRects.count();
sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
PatternHelper helper(GrPrimitiveType::kTriangles);
void* vertices = helper.init(target, vertexStride, indexBuffer.get(), kVertsPerRect,
kIndicesPerRect, rectCount);
@ -310,7 +310,7 @@ private:
size_t vertexStride = gp->getVertexStride();
int rectCount = fRects.count();
sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
PatternHelper helper(GrPrimitiveType::kTriangles);
void* vertices = helper.init(target, vertexStride, indexBuffer.get(), kVertsPerRect,
kIndicesPerRect, rectCount);

View File

@ -2063,7 +2063,7 @@ static const int kNumRRectsInIndexBuffer = 256;
GR_DECLARE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
GR_DECLARE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
static const GrBuffer* ref_rrect_index_buffer(RRectType type,
static sk_sp<const GrBuffer> get_rrect_index_buffer(RRectType type,
GrResourceProvider* resourceProvider) {
GR_DEFINE_STATIC_UNIQUE_KEY(gStrokeRRectOnlyIndexBufferKey);
GR_DEFINE_STATIC_UNIQUE_KEY(gRRectOnlyIndexBufferKey);
@ -2203,8 +2203,8 @@ private:
// drop out the middle quad if we're stroked
int indicesPerInstance = fStroked ? kIndicesPerStrokeRRect : kIndicesPerFillRRect;
sk_sp<const GrBuffer> indexBuffer(ref_rrect_index_buffer(
fStroked ? kStroke_RRectType : kFill_RRectType, target->resourceProvider()));
sk_sp<const GrBuffer> indexBuffer = get_rrect_index_buffer(
fStroked ? kStroke_RRectType : kFill_RRectType, target->resourceProvider());
PatternHelper helper(GrPrimitiveType::kTriangles);
EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(

View File

@ -123,7 +123,7 @@ private:
return;
}
size_t vertexStride = gp->getVertexStride();
sk_sp<const GrBuffer> indexBuffer(target->resourceProvider()->refQuadIndexBuffer());
sk_sp<const GrBuffer> indexBuffer = target->resourceProvider()->refQuadIndexBuffer();
PatternHelper helper(GrPrimitiveType::kTriangles);
void* vertices =
helper.init(target, vertexStride, indexBuffer.get(), kVertsPerInstance,

View File

@ -276,7 +276,7 @@ private:
&vertexBuffer,
&flushInfo.fVertexOffset);
flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
flushInfo.fIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
if (!vertices || !flushInfo.fIndexBuffer) {
SkDebugf("Could not allocate vertices\n");
return;

View File

@ -49,14 +49,13 @@ GrStencilAndCoverPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const
return CanDrawPath::kYes;
}
static GrPath* get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) {
static sk_sp<GrPath> get_gr_path(GrResourceProvider* resourceProvider, const GrShape& shape) {
GrUniqueKey key;
bool isVolatile;
GrPath::ComputeKey(shape, &key, &isVolatile);
sk_sp<GrPath> path;
if (!isVolatile) {
path.reset(
static_cast<GrPath*>(resourceProvider->findAndRefResourceByUniqueKey(key)));
path = resourceProvider->findByUniqueKey<GrPath>(key);
}
if (!path) {
SkPath skPath;
@ -72,7 +71,7 @@ static GrPath* get_gr_path(GrResourceProvider* resourceProvider, const GrShape&
SkASSERT(path->isEqualTo(skPath, shape.style()));
#endif
}
return path.release();
return path;
}
void GrStencilAndCoverPathRenderer::onStencilPath(const StencilPathArgs& args) {

View File

@ -250,7 +250,7 @@ private:
memset(&builder[shapeKeyDataCnt], 0, sizeof(fDevClipBounds));
}
builder.finish();
sk_sp<GrBuffer> cachedVertexBuffer(rp->findAndRefTByUniqueKey<GrBuffer>(key));
sk_sp<GrBuffer> cachedVertexBuffer(rp->findByUniqueKey<GrBuffer>(key));
int actualCount;
SkScalar tol = GrPathUtils::kDefaultTolerance;
tol = GrPathUtils::scaleToleranceToSrc(tol, fViewMatrix, fShape.bounds());

View File

@ -346,7 +346,7 @@ private:
}
sk_sp<const GrBuffer> ibuffer;
if (fDraws.count() > 1) {
ibuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
ibuffer = target->resourceProvider()->refQuadIndexBuffer();
if (!ibuffer) {
SkDebugf("Could not allocate quad indices\n");
return;

View File

@ -528,10 +528,8 @@ void GrStencilAndCoverTextContext::TextRun::setPosText(const char text[], size_t
sk_sp<GrPathRange> GrStencilAndCoverTextContext::TextRun::createGlyphs(
GrResourceProvider* resourceProvider) const {
sk_sp<GrPathRange> glyphs;
glyphs.reset(static_cast<GrPathRange*>(
resourceProvider->findAndRefResourceByUniqueKey(fGlyphPathsKey)));
sk_sp<GrPathRange> glyphs =
resourceProvider->findByUniqueKey<GrPathRange>(fGlyphPathsKey);
if (!glyphs) {
if (fUsingRawGlyphPaths) {
SkScalerContextEffects noeffects;

View File

@ -362,9 +362,8 @@ sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count)
sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() {
GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
return sk_sp<const GrBuffer>(
fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey));
return fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey);
}
void DrawMeshHelper::drawMesh(const GrMesh& mesh) {