diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gpu/GrBatchedTextContext.cpp | 102 | ||||
-rw-r--r-- | src/gpu/GrBatchedTextContext.h | 84 | ||||
-rw-r--r-- | src/gpu/GrDefaultTextContext.h | 48 | ||||
-rw-r--r-- | src/gpu/GrTextContext.cpp (renamed from src/gpu/GrDefaultTextContext.cpp) | 127 | ||||
-rw-r--r-- | src/gpu/SkGpuDevice.cpp | 28 |
5 files changed, 87 insertions, 302 deletions
diff --git a/src/gpu/GrBatchedTextContext.cpp b/src/gpu/GrBatchedTextContext.cpp deleted file mode 100644 index e94bf3dd7d..0000000000 --- a/src/gpu/GrBatchedTextContext.cpp +++ /dev/null @@ -1,102 +0,0 @@ - -/* - * Copyright 2010 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - - -#include "GrBatchedTextContext.h" -#include "GrContext.h" -#include "GrDrawTarget.h" -#include "GrIndexBuffer.h" -#include "GrTextContext.h" - - -GrBatchedTextContext::GrBatchedTextContext() { -} - -GrBatchedTextContext::~GrBatchedTextContext() { -} - -void GrBatchedTextContext::init(GrContext* context, - const GrPaint& grPaint, - const GrMatrix* extMatrix) { - this->INHERITED::init(context, grPaint, extMatrix); - fGrPaint = grPaint; - fDrawTarget = NULL; - - fMaxVertices = 0; - fCurrTexture = NULL; - fCurrVertex = 0; -} - -void GrBatchedTextContext::finish() { - GrAssert(fDrawTarget); - if (fDrawTarget) { - fDrawTarget->drawState()->disableStages(); - } - fDrawTarget = NULL; - - this->INHERITED::finish(); -} - -void GrBatchedTextContext::reset() { - GrAssert(this->isValid()); - GrAssert(fDrawTarget); - fDrawTarget->resetVertexSource(); - fMaxVertices = 0; - fCurrVertex = 0; - GrSafeSetNull(fCurrTexture); -} - -void GrBatchedTextContext::prepareForGlyph(GrTexture* texture) { - GrAssert(this->isValid()); - GrAssert(texture); - if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { - this->flush(); - fCurrTexture = texture; - fCurrTexture->ref(); - } -} - -void GrBatchedTextContext::setupVertexBuff(void** vertexBuff, - GrVertexLayout vertexLayout) { - GrAssert(this->isValid()); - GrAssert(fDrawTarget); - if (NULL == *vertexBuff) { - // If we need to reserve vertices allow the draw target to suggest - // a number of verts to reserve and whether to perform a flush. - fMaxVertices = kMinRequestedVerts; - bool flush = fDrawTarget->geometryHints(vertexLayout, - &fMaxVertices, - NULL); - if (flush) { - this->flush(); - fContext->flush(); - fDrawTarget = fContext->getTextTarget(fGrPaint); - fMaxVertices = kDefaultRequestedVerts; - // ignore return, no point in flushing again. - fDrawTarget->geometryHints(vertexLayout, - &fMaxVertices, - NULL); - } - - int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); - if (fMaxVertices < kMinRequestedVerts) { - fMaxVertices = kDefaultRequestedVerts; - } else if (fMaxVertices > maxQuadVertices) { - // don't exceed the limit of the index buffer - fMaxVertices = maxQuadVertices; - } - bool success = fDrawTarget->reserveVertexAndIndexSpace( - vertexLayout, - fMaxVertices, - 0, - vertexBuff, - NULL); - GrAlwaysAssert(success); - } -} diff --git a/src/gpu/GrBatchedTextContext.h b/src/gpu/GrBatchedTextContext.h deleted file mode 100644 index e1f4736631..0000000000 --- a/src/gpu/GrBatchedTextContext.h +++ /dev/null @@ -1,84 +0,0 @@ - -/* - * Copyright 2010 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - - -#ifndef GrBatchedTextContext_DEFINED -#define GrBatchedTextContext_DEFINED - -#include "GrPaint.h" -#include "GrTextContext.h" - -class GrDrawTarget; -class GrTexture; - -/** - * Base class for TextContexts that can batch multiple glyphs into single draw. - * - * Every glyph is encoded on a single texture. - * Every glyph is enclosed within a quad (formed by triangle fan) represented - * by 4 vertices. - */ -class GrBatchedTextContext: public GrTextContext { -public: - virtual ~GrBatchedTextContext(); - -protected: - enum { - kMinRequestedGlyphs = 1, - kDefaultRequestedGlyphs = 64, - kMinRequestedVerts = kMinRequestedGlyphs * 4, - kDefaultRequestedVerts = kDefaultRequestedGlyphs * 4, - kGlyphMaskStage = GrPaint::kTotalStages, - }; - - GrPaint fGrPaint; - - /** fDrawTarget is not set by init() - MUST be set by subclasses! */ - GrDrawTarget* fDrawTarget; - - int32_t fMaxVertices; - GrTexture* fCurrTexture; - int fCurrVertex; - - GrBatchedTextContext(); - virtual void init(GrContext* context, const GrPaint&, - const GrMatrix* extMatrix) SK_OVERRIDE; - virtual void finish() SK_OVERRIDE; - - /** - * Prepare to add another glyph to buffer. The glyph is encoded on the - * texture provided. Make sure we are using the right texture (or switch - * to a new texture) and that our buffer is big enough. - */ - void prepareForGlyph(GrTexture*); - - /** - * Flush the buffer. Called when switching textures. - * Must be called in finish() method of all derived classes. - */ - virtual void flush() = 0; - - /** - * Set up a buffer to hold vertices of given layout. - * If NULL != *vertexBuff, don't do anything. - * Might cause flushing, if draw target suggests it. - */ - void setupVertexBuff(void** vertexBuff, GrVertexLayout vertexLayout); - - /** - * Reset after flushing. - */ - void reset(); - -private: - - typedef GrTextContext INHERITED; -}; - -#endif diff --git a/src/gpu/GrDefaultTextContext.h b/src/gpu/GrDefaultTextContext.h deleted file mode 100644 index 4a935dc4ec..0000000000 --- a/src/gpu/GrDefaultTextContext.h +++ /dev/null @@ -1,48 +0,0 @@ - -/* - * Copyright 2010 Google Inc. - * - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - - - -#ifndef GrDefaultTextContext_DEFINED -#define GrDefaultTextContext_DEFINED - -#include "GrBatchedTextContext.h" - -struct GrGpuTextVertex; -class GrTextStrike; - -class GrDefaultTextContext: public GrBatchedTextContext { -public: - GrDefaultTextContext(); - ~GrDefaultTextContext(); - - virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top, - GrFontScaler*) SK_OVERRIDE; - -protected: - virtual void flush() SK_OVERRIDE; - virtual void init(GrContext* context, const GrPaint&, - const GrMatrix* extMatrix) SK_OVERRIDE; - virtual void finish() SK_OVERRIDE; - -private: - GrVertexLayout fVertexLayout; - GrGpuTextVertex* fVertices; - GrIRect fClipRect; - - GrTextStrike* fStrike; - - GrMatrix fExtMatrix; - GrMatrix fOrigViewMatrix; // restore previous viewmatrix - - inline void flushGlyphs(); - - typedef GrBatchedTextContext INHERITED; -}; - -#endif diff --git a/src/gpu/GrDefaultTextContext.cpp b/src/gpu/GrTextContext.cpp index 07a01621a4..d642f13d84 100644 --- a/src/gpu/GrDefaultTextContext.cpp +++ b/src/gpu/GrTextContext.cpp @@ -1,4 +1,3 @@ - /* * Copyright 2010 Google Inc. * @@ -8,18 +7,22 @@ +#include "GrTextContext.h" #include "GrAtlas.h" -#include "GrDefaultTextContext.h" #include "GrContext.h" #include "GrDrawTarget.h" #include "GrFontScaler.h" #include "GrGpuVertex.h" -#include "GrTemplates.h" +#include "GrIndexBuffer.h" #include "GrTextStrike.h" #include "GrTextStrike_impl.h" +#include "SkPath.h" + +enum { + kGlyphMaskStage = GrPaint::kTotalStages, +}; -void GrDefaultTextContext::flushGlyphs() { - GrAssert(this->isValid()); +void GrTextContext::flushGlyphs() { if (fCurrVertex > 0) { GrDrawState* drawState = fDrawTarget->drawState(); // setup our sampler state for our text texture/atlas @@ -37,47 +40,47 @@ void GrDefaultTextContext::flushGlyphs() { drawState->setTexture(kGlyphMaskStage, fCurrTexture); if (!GrPixelConfigIsAlphaOnly(fCurrTexture->config())) { - if (kOne_GrBlendCoeff != fGrPaint.fSrcBlendCoeff || - kISA_GrBlendCoeff != fGrPaint.fDstBlendCoeff || - fGrPaint.hasTexture()) { + if (kOne_GrBlendCoeff != fPaint.fSrcBlendCoeff || + kISA_GrBlendCoeff != fPaint.fDstBlendCoeff || + fPaint.hasTexture()) { GrPrintf("LCD Text will not draw correctly.\n"); } // setup blend so that we get mask * paintColor + (1-mask)*dstColor - drawState->setBlendConstant(fGrPaint.fColor); + drawState->setBlendConstant(fPaint.fColor); drawState->setBlendFunc(kConstC_GrBlendCoeff, kISC_GrBlendCoeff); // don't modulate by the paint's color in the frag since we're // already doing it via the blend const. drawState->setColor(0xffffffff); } else { // set back to normal in case we took LCD path previously. - drawState->setBlendFunc(fGrPaint.fSrcBlendCoeff, fGrPaint.fDstBlendCoeff); - drawState->setColor(fGrPaint.fColor); + drawState->setBlendFunc(fPaint.fSrcBlendCoeff, + fPaint.fDstBlendCoeff); + drawState->setColor(fPaint.fColor); } - fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); int nGlyphs = fCurrVertex / 4; + fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); fDrawTarget->drawIndexedInstances(kTriangles_GrPrimitiveType, nGlyphs, 4, 6); + fDrawTarget->resetVertexSource(); fVertices = NULL; - this->INHERITED::reset(); + fMaxVertices = 0; + fCurrVertex = 0; + GrSafeSetNull(fCurrTexture); drawState->setTexture(kGlyphMaskStage, NULL); } } -GrDefaultTextContext::GrDefaultTextContext() { -} - -GrDefaultTextContext::~GrDefaultTextContext() { -} - -void GrDefaultTextContext::init(GrContext* context, - const GrPaint& paint, - const GrMatrix* extMatrix) { - this->INHERITED::init(context, paint, extMatrix); - +GrTextContext::GrTextContext(GrContext* context, + const GrPaint& paint, + const GrMatrix* extMatrix) : fPaint(paint) { + fContext = context; fStrike = NULL; + fCurrTexture = NULL; + fCurrVertex = 0; + if (NULL != extMatrix) { fExtMatrix = *extMatrix; } else { @@ -99,7 +102,7 @@ void GrDefaultTextContext::init(GrContext* context, } // save the context's original matrix off and restore in destructor - // getTextTarget should be called after that + // this must be done before getTextTarget. fOrigViewMatrix = fContext->getMatrix(); fContext->setMatrix(fExtMatrix); @@ -115,42 +118,41 @@ void GrDefaultTextContext::init(GrContext* context, bool invVMComputed = false; GrMatrix invVM; for (int t = 0; t < GrPaint::kMaxTextures; ++t) { - if (fGrPaint.isTextureStageEnabled(t)) { + if (fPaint.isTextureStageEnabled(t)) { if (invVMComputed || fOrigViewMatrix.invert(&invVM)) { invVMComputed = true; - fGrPaint.textureSampler(t)->preConcatMatrix(invVM); + fPaint.textureSampler(t)->preConcatMatrix(invVM); } } } for (int m = 0; m < GrPaint::kMaxMasks; ++m) { - if (fGrPaint.isMaskStageEnabled(m)) { + if (fPaint.isMaskStageEnabled(m)) { if (invVMComputed || fOrigViewMatrix.invert(&invVM)) { invVMComputed = true; - fGrPaint.maskSampler(m)->preConcatMatrix(invVM); + fPaint.maskSampler(m)->preConcatMatrix(invVM); } } } - fDrawTarget = fContext->getTextTarget(fGrPaint); + fDrawTarget = fContext->getTextTarget(fPaint); fVertices = NULL; + fMaxVertices = 0; fVertexLayout = GrDrawTarget::kTextFormat_VertexLayoutBit | GrDrawTarget::StageTexCoordVertexLayoutBit(kGlyphMaskStage, 0); } -void GrDefaultTextContext::finish() { - this->flush(); - - fStrike = NULL; +GrTextContext::~GrTextContext() { + this->flushGlyphs(); + if (fDrawTarget) { + fDrawTarget->drawState()->disableStages(); + } fContext->setMatrix(fOrigViewMatrix); - - this->INHERITED::finish(); } -void GrDefaultTextContext::flush() { - GrAssert(this->isValid()); +void GrTextContext::flush() { this->flushGlyphs(); } @@ -162,10 +164,9 @@ static inline void setRectFan(GrGpuTextVertex v[4], int l, int t, int r, int b, v[3 * stride].setI(r, t); } -void GrDefaultTextContext::drawPackedGlyph(GrGlyph::PackedID packed, +void GrTextContext::drawPackedGlyph(GrGlyph::PackedID packed, GrFixed vx, GrFixed vy, GrFontScaler* scaler) { - GrAssert(this->isValid()); if (NULL == fStrike) { fStrike = fContext->getFontCache()->getStrike(scaler); } @@ -220,7 +221,7 @@ void GrDefaultTextContext::drawPackedGlyph(GrGlyph::PackedID packed, GrPoint translate; translate.set(GrFixedToScalar(vx - GrIntToFixed(glyph->fBounds.fLeft)), GrFixedToScalar(vy - GrIntToFixed(glyph->fBounds.fTop))); - fContext->drawPath(fGrPaint, *glyph->fPath, kWinding_GrPathFill, + fContext->drawPath(fPaint, *glyph->fPath, kWinding_GrPathFill, &translate); return; } @@ -233,10 +234,47 @@ HAS_ATLAS: height = GrIntToFixed(height); GrTexture* texture = glyph->fAtlas->texture(); - this->prepareForGlyph(texture); + GrAssert(texture); - this->setupVertexBuff(GrTCast<void**>(&fVertices), - fVertexLayout); + if (fCurrTexture != texture || fCurrVertex + 4 > fMaxVertices) { + this->flushGlyphs(); + fCurrTexture = texture; + fCurrTexture->ref(); + } + + if (NULL == fVertices) { + // If we need to reserve vertices allow the draw target to suggest + // a number of verts to reserve and whether to perform a flush. + fMaxVertices = kMinRequestedVerts; + bool flush = fDrawTarget->geometryHints(fVertexLayout, + &fMaxVertices, + NULL); + if (flush) { + this->flushGlyphs(); + fContext->flush(); + fDrawTarget = fContext->getTextTarget(fPaint); + fMaxVertices = kDefaultRequestedVerts; + // ignore return, no point in flushing again. + fDrawTarget->geometryHints(fVertexLayout, + &fMaxVertices, + NULL); + } + + int maxQuadVertices = 4 * fContext->getQuadIndexBuffer()->maxQuads(); + if (fMaxVertices < kMinRequestedVerts) { + fMaxVertices = kDefaultRequestedVerts; + } else if (fMaxVertices > maxQuadVertices) { + // don't exceed the limit of the index buffer + fMaxVertices = maxQuadVertices; + } + bool success = fDrawTarget->reserveVertexAndIndexSpace( + fVertexLayout, + fMaxVertices, + 0, + GrTCast<void**>(&fVertices), + NULL); + GrAlwaysAssert(success); + } GrFixed tx = GrIntToFixed(glyph->fAtlasLocation.fX); GrFixed ty = GrIntToFixed(glyph->fAtlasLocation.fY); @@ -265,3 +303,4 @@ HAS_ATLAS: #endif fCurrVertex += 4; } + diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp index c6f377d230..c3e495ae28 100644 --- a/src/gpu/SkGpuDevice.cpp +++ b/src/gpu/SkGpuDevice.cpp @@ -1,4 +1,3 @@ - /* * Copyright 2011 Google Inc. * @@ -11,7 +10,6 @@ #include "effects/GrGradientEffects.h" #include "GrContext.h" -#include "GrDefaultTextContext.h" #include "GrTextContext.h" #include "SkGrTexturePixelRef.h" @@ -23,8 +21,6 @@ #include "SkTLazy.h" #include "SkUtils.h" -SK_DEFINE_INST_COUNT(GrTextContext) - #define CACHE_COMPATIBLE_DEVICE_TEXTURES 1 #if 0 @@ -203,8 +199,6 @@ void SkGpuDevice::initFromRenderTarget(GrContext* context, pr = new SkGrRenderTargetPixelRef(fRenderTarget); } this->setPixelRef(pr, 0)->unref(); - - fTextContext = NULL; } SkGpuDevice::SkGpuDevice(GrContext* context, @@ -250,8 +244,6 @@ SkGpuDevice::SkGpuDevice(GrContext* context, width, height); GrAssert(false); } - - fTextContext = NULL; } SkGpuDevice::~SkGpuDevice() { @@ -271,10 +263,6 @@ SkGpuDevice::~SkGpuDevice() { fContext->unlockTexture(fCache); } fContext->unref(); - - if (NULL != fTextContext) { - fTextContext->unref(); - } } /////////////////////////////////////////////////////////////////////////////// @@ -1745,9 +1733,8 @@ void SkGpuDevice::drawText(const SkDraw& draw, const void* text, &grPaint)) { return; } - GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext, - grPaint, draw.fExtMatrix); - myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext()); + GrTextContext context(fContext, grPaint, draw.fExtMatrix); + myDraw.fProcs = this->initDrawForText(&context); this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint); } } @@ -1774,9 +1761,8 @@ void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text, &grPaint)) { return; } - GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext, - grPaint, draw.fExtMatrix); - myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext()); + GrTextContext context(fContext, grPaint, draw.fExtMatrix); + myDraw.fProcs = this->initDrawForText(&context); this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY, scalarsPerPos, paint); } @@ -1927,9 +1913,3 @@ SkGpuDevice::SkGpuDevice(GrContext* context, fNeedClear = needClear; } -GrTextContext* SkGpuDevice::getTextContext() { - if (NULL == fTextContext) { - fTextContext = new GrDefaultTextContext(); - } - return fTextContext; -} |