Un-trifurcate GrTextContext: get rid of Default and Batched subclasses,

folding their functionality back into the base class.

Requires gyp changes.

http://codereview.appspot.com/6357048/



git-svn-id: http://skia.googlecode.com/svn/trunk@4411 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
tomhudson@google.com 2012-06-29 18:37:57 +00:00
parent e7290ef304
commit 375ff85e96
8 changed files with 128 additions and 398 deletions

View File

@ -217,8 +217,6 @@
'../src/gpu/GrAllocPool.cpp',
'../src/gpu/GrAtlas.cpp',
'../src/gpu/GrAtlas.h',
'../src/gpu/GrBatchedTextContext.cpp',
'../src/gpu/GrBatchedTextContext.h',
'../src/gpu/GrBinHashKey.h',
'../src/gpu/GrBufferAllocPool.cpp',
'../src/gpu/GrBufferAllocPool.h',
@ -227,8 +225,6 @@
'../src/gpu/GrCustomStage.cpp',
'../src/gpu/GrDefaultPathRenderer.cpp',
'../src/gpu/GrDefaultPathRenderer.h',
'../src/gpu/GrDefaultTextContext.cpp',
'../src/gpu/GrDefaultTextContext.h',
'../src/gpu/GrDrawState.h',
'../src/gpu/GrDrawTarget.cpp',
'../src/gpu/GrDrawTarget.h',
@ -277,6 +273,7 @@
'../src/gpu/GrSoftwarePathRenderer.h',
'../src/gpu/GrSurface.cpp',
'../src/gpu/GrTemplates.h',
'../src/gpu/GrTextContext.cpp',
'../src/gpu/GrTextStrike.cpp',
'../src/gpu/GrTextStrike.h',
'../src/gpu/GrTextStrike_impl.h',

View File

@ -12,106 +12,56 @@
#define GrTextContext_DEFINED
#include "GrGlyph.h"
#include "GrPaint.h"
#include "GrMatrix.h"
#include "GrRefCnt.h"
struct GrGpuTextVertex;
class GrContext;
class GrTextStrike;
class GrFontScaler;
class GrPaint;
class SkGpuDevice;
class SkPaint;
/**
* Derived classes can use stages GrPaint::kTotalStages through
* GrDrawState::kNumStages-1. The stages before GrPaint::kTotalStages
* are reserved for setting up the draw (i.e., textures and filter masks).
*/
class GrTextContext: public GrRefCnt {
protected:
GrContext* fContext;
class GrDrawTarget;
class GrTextContext {
public:
SK_DECLARE_INST_COUNT(GrTextContext)
GrTextContext(GrContext*,
const GrPaint& paint,
const GrMatrix* extMatrix = NULL);
~GrTextContext();
/**
* To use a text context it must be wrapped in an AutoFinish. AutoFinish's
* destructor ensures all drawing is flushed to the GrContext.
*/
class AutoFinish {
public:
AutoFinish(GrTextContext* textContext, GrContext* context,
const GrPaint&, const GrMatrix* extMatrix);
~AutoFinish();
GrTextContext* getTextContext() const;
void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
GrFontScaler*);
private:
GrTextContext* fTextContext;
};
virtual void drawPackedGlyph(GrGlyph::PackedID, GrFixed left, GrFixed top,
GrFontScaler*) = 0;
virtual ~GrTextContext() {}
protected:
GrTextContext() {
fContext = NULL;
}
bool isValid() const {
return (NULL != fContext);
}
/**
* Initialize the object.
*
* Before call to this method, the instance is considered to be in
* invalid state. I.e. call to any method other than isValid will result in
* undefined behaviour.
*
* @see finish
*/
virtual void init(GrContext* context, const GrPaint&,
const GrMatrix* extMatrix) {
fContext = context;
}
/**
* Reset the object to invalid state.
*
* After call to this method, the instance is considered to be in
* invalid state.
*
* It might be brought back to a valid state by calling init.
*
* @see init
*/
virtual void finish() {
fContext = NULL;
}
void flush(); // optional; automatically called by destructor
private:
typedef GrRefCnt INHERITED;
GrPaint fPaint;
GrVertexLayout fVertexLayout;
GrContext* fContext;
GrDrawTarget* fDrawTarget;
GrMatrix fExtMatrix;
GrFontScaler* fScaler;
GrTextStrike* fStrike;
inline void flushGlyphs();
void setupDrawTarget();
enum {
kMinRequestedGlyphs = 1,
kDefaultRequestedGlyphs = 64,
kMinRequestedVerts = kMinRequestedGlyphs * 4,
kDefaultRequestedVerts = kDefaultRequestedGlyphs * 4,
};
GrGpuTextVertex* fVertices;
int32_t fMaxVertices;
GrTexture* fCurrTexture;
int fCurrVertex;
GrIRect fClipRect;
GrMatrix fOrigViewMatrix; // restore previous viewmatrix
};
inline GrTextContext::AutoFinish::AutoFinish(GrTextContext* textContext,
GrContext* context,
const GrPaint& grPaint,
const GrMatrix* extMatrix) {
GrAssert(NULL != textContext);
fTextContext = textContext;
fTextContext->ref();
fTextContext->init(context, grPaint, extMatrix);
}
inline GrTextContext::AutoFinish::~AutoFinish() {
fTextContext->finish();
fTextContext->unref();
}
inline GrTextContext* GrTextContext::AutoFinish::getTextContext() const {
return fTextContext;
}
#endif

View File

@ -138,8 +138,6 @@ private:
bool fNeedClear;
bool fNeedPrepareRenderTarget;
GrTextContext* fTextContext;
// called from rt and tex cons
void initFromRenderTarget(GrContext*, GrRenderTarget*);

View File

@ -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);
}
}

View File

@ -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

View File

@ -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

View File

@ -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"
void GrDefaultTextContext::flushGlyphs() {
GrAssert(this->isValid());
enum {
kGlyphMaskStage = GrPaint::kTotalStages,
};
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;
}

View File

@ -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;
}