Index: src/gpu/GrContext.cpp |
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp |
index 9a180ee6539ec487cb64191630c303994504f650..3a93404af17832b421c94bc3f8df1ed08cb81a73 100755 |
--- a/src/gpu/GrContext.cpp |
+++ b/src/gpu/GrContext.cpp |
@@ -25,6 +25,7 @@ |
#include "GrOvalRenderer.h" |
#include "GrPathRenderer.h" |
#include "GrPathUtils.h" |
+#include "GrResourceCache.h" |
#include "GrResourceCache2.h" |
#include "GrSoftwarePathRenderer.h" |
#include "GrStencilBuffer.h" |
@@ -51,6 +52,9 @@ |
#define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0 |
#endif |
+static const size_t MAX_RESOURCE_CACHE_COUNT = GR_DEFAULT_RESOURCE_CACHE_COUNT_LIMIT; |
+static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMIT * 1024 * 1024; |
+ |
static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15; |
static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; |
@@ -61,6 +65,20 @@ |
// Glorified typedef to avoid including GrDrawState.h in GrContext.h |
class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {}; |
+ |
+class GrContext::AutoCheckFlush { |
+public: |
+ AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); } |
+ |
+ ~AutoCheckFlush() { |
+ if (fContext->fFlushToReduceCacheSize) { |
+ fContext->flush(); |
+ } |
+ } |
+ |
+private: |
+ GrContext* fContext; |
+}; |
GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext, |
const Options* opts) { |
@@ -85,11 +103,13 @@ |
fClip = NULL; |
fPathRendererChain = NULL; |
fSoftwarePathRenderer = NULL; |
+ fResourceCache = NULL; |
fResourceCache2 = NULL; |
fFontCache = NULL; |
fDrawBuffer = NULL; |
fDrawBufferVBAllocPool = NULL; |
fDrawBufferIBAllocPool = NULL; |
+ fFlushToReduceCacheSize = false; |
fAARectRenderer = NULL; |
fOvalRenderer = NULL; |
fViewMatrix.reset(); |
@@ -110,8 +130,11 @@ |
void GrContext::initCommon() { |
fDrawState = SkNEW(GrDrawState); |
+ fResourceCache = SkNEW_ARGS(GrResourceCache, (fGpu->caps(), |
+ MAX_RESOURCE_CACHE_COUNT, |
+ MAX_RESOURCE_CACHE_BYTES)); |
+ fResourceCache->setOverbudgetCallback(OverbudgetCB, this); |
fResourceCache2 = SkNEW(GrResourceCache2); |
- fResourceCache2->setOverBudgetCallback(OverBudgetCB, this); |
fFontCache = SkNEW_ARGS(GrFontCache, (fGpu)); |
@@ -137,6 +160,9 @@ |
} |
SkDELETE(fResourceCache2); |
+ fResourceCache2 = NULL; |
+ SkDELETE(fResourceCache); |
+ fResourceCache = NULL; |
SkDELETE(fFontCache); |
SkDELETE(fDrawBuffer); |
SkDELETE(fDrawBufferVBAllocPool); |
@@ -175,6 +201,8 @@ |
fAARectRenderer->reset(); |
fOvalRenderer->reset(); |
+ fResourceCache->purgeAllUnlocked(); |
+ |
fFontCache->freeAll(); |
fLayerCache->freeAll(); |
} |
@@ -193,6 +221,7 @@ |
fAARectRenderer->reset(); |
fOvalRenderer->reset(); |
+ fResourceCache->purgeAllUnlocked(); |
fFontCache->freeAll(); |
fLayerCache->freeAll(); |
// a path renderer may be holding onto resources |
@@ -201,12 +230,12 @@ |
} |
void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const { |
- if (resourceCount) { |
- *resourceCount = fResourceCache2->getResourceCount(); |
- } |
- if (resourceBytes) { |
- *resourceBytes = fResourceCache2->getResourceBytes(); |
- } |
+ if (resourceCount) { |
+ *resourceCount = fResourceCache->getCachedResourceCount(); |
+ } |
+ if (resourceBytes) { |
+ *resourceBytes = fResourceCache->getCachedResourceBytes(); |
+ } |
} |
GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget, |
@@ -244,13 +273,12 @@ |
} |
void GrContext::addStencilBuffer(GrStencilBuffer* sb) { |
- // TODO: Make GrStencilBuffers use the scratch mechanism rather than content keys. |
ASSERT_OWNED_RESOURCE(sb); |
GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(), |
sb->height(), |
sb->numSamples()); |
- SkAssertResult(sb->cacheAccess().setContentKey(resourceKey)); |
+ fResourceCache->addResource(resourceKey, sb); |
} |
GrStencilBuffer* GrContext::findAndRefStencilBuffer(int width, int height, int sampleCnt) { |
@@ -392,16 +420,22 @@ |
} |
if (texture) { |
- if (texture->cacheAccess().setContentKey(resourceKey)) { |
- if (cacheKey) { |
- *cacheKey = resourceKey; |
- } |
- } else { |
- texture->unref(); |
- texture = NULL; |
- } |
- } |
- |
+ fResourceCache->addResource(resourceKey, texture); |
+ |
+ if (cacheKey) { |
+ *cacheKey = resourceKey; |
+ } |
+ } |
+ |
+ return texture; |
+} |
+ |
+GrTexture* GrContext::createNewScratchTexture(const GrSurfaceDesc& desc) { |
+ GrTexture* texture = fGpu->createTexture(desc, NULL, 0); |
+ if (!texture) { |
+ return NULL; |
+ } |
+ fResourceCache->addResource(texture->cacheAccess().getScratchKey(), texture); |
return texture; |
} |
@@ -439,6 +473,7 @@ |
} |
GrGpuResource* resource = fResourceCache2->findAndRefScratchResource(key, scratchFlags); |
if (resource) { |
+ fResourceCache->makeResourceMRU(resource); |
return static_cast<GrSurface*>(resource)->asTexture(); |
} |
@@ -461,17 +496,21 @@ |
desc.writable()->fFlags = origFlags; |
} |
- GrTexture* texture = fGpu->createTexture(*desc, NULL, 0); |
+ GrTexture* texture = this->createNewScratchTexture(*desc); |
SkASSERT(NULL == texture || |
texture->cacheAccess().getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc)); |
return texture; |
} |
-void GrContext::OverBudgetCB(void* data) { |
+bool GrContext::OverbudgetCB(void* data) { |
+ SkASSERT(data); |
+ |
+ GrContext* context = reinterpret_cast<GrContext*>(data); |
+ |
// Flush the InOrderDrawBuffer to possibly free up some textures |
- SkASSERT(data); |
- GrContext* context = reinterpret_cast<GrContext*>(data); |
- context->flush(); |
+ context->fFlushToReduceCacheSize = true; |
+ |
+ return true; |
} |
@@ -483,16 +522,11 @@ |
} |
void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const { |
- if (maxTextures) { |
- *maxTextures = fResourceCache2->getMaxResourceCount(); |
- } |
- if (maxTextureBytes) { |
- *maxTextureBytes = fResourceCache2->getMaxResourceBytes(); |
- } |
+ fResourceCache->getLimits(maxTextures, maxTextureBytes); |
} |
void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) { |
- fResourceCache2->setLimits(maxTextures, maxTextureBytes); |
+ fResourceCache->setLimits(maxTextures, maxTextureBytes); |
} |
int GrContext::getMaxTextureSize() const { |
@@ -548,8 +582,9 @@ |
SkASSERT(renderTarget); |
AutoRestoreEffects are; |
+ AutoCheckFlush acf(this); |
GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this); |
- GrDrawTarget* target = this->prepareToDraw(NULL, &are); |
+ GrDrawTarget* target = this->prepareToDraw(NULL, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -681,7 +716,8 @@ |
} |
AutoRestoreEffects are; |
- GrDrawTarget* target = this->prepareToDraw(&paint, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -791,7 +827,8 @@ |
const SkRect& localRect, |
const SkMatrix* localMatrix) { |
AutoRestoreEffects are; |
- GrDrawTarget* target = this->prepareToDraw(&paint, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -854,9 +891,10 @@ |
const uint16_t indices[], |
int indexCount) { |
AutoRestoreEffects are; |
- GrDrawTarget::AutoReleaseGeometry geo; |
- |
- GrDrawTarget* target = this->prepareToDraw(&paint, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope |
+ |
+ GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -916,7 +954,8 @@ |
} |
AutoRestoreEffects are; |
- GrDrawTarget* target = this->prepareToDraw(&paint, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -942,7 +981,8 @@ |
} |
AutoRestoreEffects are; |
- GrDrawTarget* target = this->prepareToDraw(&paint, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf); |
GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target); |
@@ -974,7 +1014,8 @@ |
} |
AutoRestoreEffects are; |
- GrDrawTarget* target = this->prepareToDraw(&paint, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -1061,7 +1102,8 @@ |
SkPoint pts[2]; |
if (path.isLine(pts)) { |
AutoRestoreEffects are; |
- GrDrawTarget* target = this->prepareToDraw(&paint, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -1097,7 +1139,8 @@ |
// the writePixels that uploads to the scratch will perform a flush so we're |
// OK. |
AutoRestoreEffects are; |
- GrDrawTarget* target = this->prepareToDraw(&paint, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget* target = this->prepareToDraw(&paint, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -1199,6 +1242,8 @@ |
} else { |
fDrawBuffer->flush(); |
} |
+ fResourceCache->purgeAsNeeded(); |
+ fFlushToReduceCacheSize = false; |
} |
bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes, |
@@ -1318,7 +1363,7 @@ |
// drawing a rect to the render target. |
// The bracket ensures we pop the stack if we wind up flushing below. |
{ |
- GrDrawTarget* drawTarget = this->prepareToDraw(NULL, NULL); |
+ GrDrawTarget* drawTarget = this->prepareToDraw(NULL, NULL, NULL); |
GrDrawTarget::AutoGeometryAndStatePush agasp(drawTarget, GrDrawTarget::kReset_ASRInit, |
&matrix); |
GrDrawState* drawState = drawTarget->drawState(); |
@@ -1498,7 +1543,8 @@ |
SkASSERT(renderTarget); |
ASSERT_OWNED_RESOURCE(renderTarget); |
AutoRestoreEffects are; |
- GrDrawTarget* target = this->prepareToDraw(NULL, &are); |
+ AutoCheckFlush acf(this); |
+ GrDrawTarget* target = this->prepareToDraw(NULL, &are, &acf); |
if (NULL == target) { |
return; |
} |
@@ -1516,7 +1562,7 @@ |
// Since we're going to the draw target and not GPU, no need to check kNoFlush |
// here. |
- GrDrawTarget* target = this->prepareToDraw(NULL, NULL); |
+ GrDrawTarget* target = this->prepareToDraw(NULL, NULL, NULL); |
if (NULL == target) { |
return; |
} |
@@ -1535,7 +1581,9 @@ |
//////////////////////////////////////////////////////////////////////////////// |
-GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, AutoRestoreEffects* are) { |
+GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, |
+ AutoRestoreEffects* are, |
+ AutoCheckFlush* acf) { |
// All users of this draw state should be freeing up all effects when they're done. |
// Otherwise effects that own resources may keep those resources alive indefinitely. |
SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageStages() && |
@@ -1548,6 +1596,7 @@ |
ASSERT_OWNED_RESOURCE(fRenderTarget.get()); |
if (paint) { |
SkASSERT(are); |
+ SkASSERT(acf); |
are->set(fDrawState); |
fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get()); |
#if GR_DEBUG_PARTIAL_COVERAGE_CHECK |
@@ -1647,7 +1696,7 @@ |
} |
GrDrawTarget* GrContext::getTextTarget() { |
- return this->prepareToDraw(NULL, NULL); |
+ return this->prepareToDraw(NULL, NULL, NULL); |
} |
const GrIndexBuffer* GrContext::getQuadIndexBuffer() const { |
@@ -1697,11 +1746,15 @@ |
} |
void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) { |
- resource->cacheAccess().setContentKey(resourceKey); |
+ fResourceCache->addResource(resourceKey, resource); |
} |
GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) { |
- return fResourceCache2->findAndRefContentResource(resourceKey); |
+ GrGpuResource* resource = fResourceCache2->findAndRefContentResource(resourceKey); |
+ if (resource) { |
+ fResourceCache->makeResourceMRU(resource); |
+ } |
+ return resource; |
} |
void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) { |
@@ -1721,7 +1774,7 @@ |
/////////////////////////////////////////////////////////////////////////////// |
#if GR_CACHE_STATS |
void GrContext::printCacheStats() const { |
- fResourceCache2->printStats(); |
+ fResourceCache->printStats(); |
} |
#endif |