Chromium Code Reviews| Index: src/gpu/GrContext.cpp |
| diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp |
| index 6b1efbeac2e7c6e5b7408dcf9d68c7152cf22d42..21fe3a52613e8fd534e333afaa3be08dea42d039 100644 |
| --- a/src/gpu/GrContext.cpp |
| +++ b/src/gpu/GrContext.cpp |
| @@ -68,6 +68,22 @@ static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4; |
| // Glorified typedef to avoid including GrDrawState.h in GrContext.h |
| class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {}; |
| +class GrContext::AutoCheckFlush { |
| +public: |
|
robertphillips
2013/10/04 15:28:12
What do you think about requiring the context in t
|
| + AutoCheckFlush() : fContext(NULL) {} |
| + |
| + ~AutoCheckFlush() { |
| + if (NULL != fContext && fContext->fFlushToReduceCacheSize) { |
| + fContext->flush(); |
| + } |
| + } |
| + |
| + void set(GrContext* context) { fContext = context; } |
| + |
| +private: |
| + GrContext* fContext; |
| +}; |
| + |
| GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) { |
| GrContext* context = SkNEW(GrContext); |
| if (context->init(backend, backendContext)) { |
| @@ -101,6 +117,7 @@ GrContext::GrContext() { |
| fDrawBuffer = NULL; |
| fDrawBufferVBAllocPool = NULL; |
| fDrawBufferIBAllocPool = NULL; |
| + fFlushToReduceCacheSize = false; |
| fAARectRenderer = NULL; |
| fOvalRenderer = NULL; |
| fViewMatrix.reset(); |
| @@ -533,10 +550,8 @@ bool GrContext::OverbudgetCB(void* data) { |
| GrContext* context = reinterpret_cast<GrContext*>(data); |
| // Flush the InOrderDrawBuffer to possibly free up some textures |
| - context->flush(); |
| + context->fFlushToReduceCacheSize = true; |
| - // TODO: actually track flush's behavior rather than always just |
| - // returning true. |
| return true; |
| } |
| @@ -606,7 +621,8 @@ void GrContext::clear(const SkIRect* rect, |
| const GrColor color, |
| GrRenderTarget* target) { |
| AutoRestoreEffects are; |
| - this->prepareToDraw(NULL, BUFFERED_DRAW, &are)->clear(rect, color, target); |
| + AutoCheckFlush acf; |
| + this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf)->clear(rect, color, target); |
| } |
| void GrContext::drawPaint(const GrPaint& origPaint) { |
| @@ -755,7 +771,8 @@ void GrContext::drawRect(const GrPaint& paint, |
| SK_TRACE_EVENT0("GrContext::drawRect"); |
| AutoRestoreEffects are; |
| - GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are); |
| + AutoCheckFlush acf; |
| + GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
| SkMatrix combinedMatrix = target->drawState()->getViewMatrix(); |
| if (NULL != matrix) { |
| @@ -874,7 +891,8 @@ void GrContext::drawRectToRect(const GrPaint& paint, |
| const SkMatrix* localMatrix) { |
| SK_TRACE_EVENT0("GrContext::drawRectToRect"); |
| AutoRestoreEffects are; |
| - GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are); |
| + AutoCheckFlush acf; |
| + GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
| target->drawRect(dstRect, dstMatrix, &localRect, localMatrix); |
| } |
| @@ -930,7 +948,8 @@ void GrContext::drawVertices(const GrPaint& paint, |
| GrDrawTarget::AutoReleaseGeometry geo; |
| AutoRestoreEffects are; |
| - GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are); |
| + AutoCheckFlush acf; |
| + GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
| GrDrawState* drawState = target->drawState(); |
| @@ -982,7 +1001,8 @@ void GrContext::drawRRect(const GrPaint& paint, |
| } |
| AutoRestoreEffects are; |
| - GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are); |
| + AutoCheckFlush acf; |
| + GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
| bool useAA = paint.isAntiAlias() && |
| !target->getDrawState().getRenderTarget()->isMultisampled() && |
| @@ -1005,7 +1025,8 @@ void GrContext::drawOval(const GrPaint& paint, |
| } |
| AutoRestoreEffects are; |
| - GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are); |
| + AutoCheckFlush acf; |
| + GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
| bool useAA = paint.isAntiAlias() && |
| !target->getDrawState().getRenderTarget()->isMultisampled() && |
| @@ -1088,7 +1109,8 @@ void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrok |
| // the writePixels that uploads to the scratch will perform a flush so we're |
| // OK. |
| AutoRestoreEffects are; |
| - GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are); |
| + AutoCheckFlush acf; |
| + GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf); |
| bool useAA = paint.isAntiAlias() && !target->getDrawState().getRenderTarget()->isMultisampled(); |
| if (useAA && stroke.getWidth() < 0 && !path.isConvex()) { |
| @@ -1183,6 +1205,7 @@ void GrContext::flush(int flagsBitfield) { |
| } else { |
| fDrawBuffer->flush(); |
| } |
| + fFlushToReduceCacheSize = false; |
| } |
| bool GrContext::writeTexturePixels(GrTexture* texture, |
| @@ -1590,7 +1613,8 @@ bool GrContext::writeRenderTargetPixels(GrRenderTarget* target, |
| GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, |
| BufferedDraw buffered, |
| - AutoRestoreEffects* are) { |
| + 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()); |
| @@ -1602,7 +1626,9 @@ GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, |
| ASSERT_OWNED_RESOURCE(fRenderTarget.get()); |
| if (NULL != paint) { |
| SkASSERT(NULL != are); |
| + SkASSERT(NULL != acf); |
| are->set(fDrawState); |
| + acf->set(this); |
| fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get()); |
| #if GR_DEBUG_PARTIAL_COVERAGE_CHECK |
| if ((paint->hasMask() || 0xff != paint->fCoverage) && |
| @@ -1701,7 +1727,7 @@ void GrContext::setupDrawBuffer() { |
| } |
| GrDrawTarget* GrContext::getTextTarget() { |
| - return this->prepareToDraw(NULL, BUFFERED_DRAW, NULL); |
| + return this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL); |
| } |
| const GrIndexBuffer* GrContext::getQuadIndexBuffer() const { |