Remove getter of writable GrEffectStage from GrDrawState.

Upcoming changes will require GrDrawState to know things about the set of installed effects. Thus all setting of effects must go through a GrDrawState function (setEffect()). This change accomplishes that.
Review URL: https://codereview.appspot.com/7214045

git-svn-id: http://skia.googlecode.com/svn/trunk@7411 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
bsalomon@google.com 2013-01-28 14:26:09 +00:00
parent 093455c116
commit adc6536fe5
10 changed files with 85 additions and 110 deletions

View File

@ -910,14 +910,17 @@ private:
// for use with textures released from an GrAutoScratchTexture.
void addExistingTextureToCache(GrTexture* texture);
bool installPMToUPMEffect(GrTexture* texture,
/**
* These functions create premul <-> unpremul effects if it is possible to generate a pair
* of effects that make a readToUPM->writeToPM->readToUPM cycle invariant. Otherwise, they
* return NULL.
*/
const GrEffectRef* createPMToUPMEffect(GrTexture* texture,
bool swapRAndB,
const SkMatrix& matrix,
GrEffectStage* stage);
bool installUPMToPMEffect(GrTexture* texture,
const SkMatrix& matrix);
const GrEffectRef* createUPMToPMEffect(GrTexture* texture,
bool swapRAndB,
const SkMatrix& matrix,
GrEffectStage* stage);
const SkMatrix& matrix);
typedef GrRefCnt INHERITED;
};

View File

@ -45,11 +45,9 @@ void setup_drawstate_aaclip(GrGpu* gpu,
SkIntToScalar(-devBound.fTop));
mat.preConcat(drawState->getViewMatrix());
drawState->stage(kMaskStage)->reset();
SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
// This could be a long-lived effect that is cached with the alpha-mask.
drawState->stage(kMaskStage)->setEffect(
drawState->setEffect(kMaskStage,
GrTextureDomainEffect::Create(result,
mat,
GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
@ -352,7 +350,7 @@ void GrClipMaskManager::mergeMask(GrTexture* dstMask,
SkMatrix sampleM;
sampleM.setIDiv(srcMask->width(), srcMask->height());
drawState->stage(0)->setEffect(
drawState->setEffect(0,
GrTextureDomainEffect::Create(srcMask,
sampleM,
GrTextureDomainEffect::MakeTexelDomain(srcMask, srcBound),

View File

@ -204,7 +204,7 @@ void convolve_gaussian(GrDrawTarget* target,
direction,
radius,
sigma));
drawState->stage(0)->setEffect(conv);
drawState->setEffect(0, conv);
target->drawSimpleRect(rect, NULL);
}
@ -1375,7 +1375,6 @@ bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
ast.set(this, desc, match);
GrTexture* texture = ast.texture();
if (texture) {
GrEffectStage stage;
// compute a matrix to perform the draw
SkMatrix textureMatrix;
if (flipY) {
@ -1387,30 +1386,30 @@ bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
}
textureMatrix.postIDiv(src->width(), src->height());
bool effectInstalled = false;
SkAutoTUnref<const GrEffectRef> effect;
if (unpremul) {
if (this->installPMToUPMEffect(src, swapRAndB, textureMatrix, &stage)) {
effectInstalled = true;
effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
if (NULL != effect) {
unpremul = false; // we no longer need to do this on CPU after the readback.
}
}
// If we failed to create a PM->UPM effect and have no other conversions to perform then
// there is no longer any point to using the scratch.
if (effectInstalled || flipY || swapRAndB) {
if (!effectInstalled) {
SkAssertResult(GrConfigConversionEffect::InstallEffect(
if (NULL != effect || flipY || swapRAndB) {
if (!effect) {
effect.reset(GrConfigConversionEffect::Create(
src,
swapRAndB,
GrConfigConversionEffect::kNone_PMConversion,
textureMatrix,
&stage));
textureMatrix));
}
swapRAndB = false; // we will handle the swap in the draw.
flipY = false; // we already incorporated the y flip in the matrix
GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
GrDrawState* drawState = fGpu->drawState();
*drawState->stage(0) = stage;
GrAssert(effect);
*drawState->setEffect(0, effect);
drawState->setRenderTarget(texture->asRenderTarget());
GrRect rect = GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
@ -1503,7 +1502,7 @@ void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint*
// Writes pending to the source texture are not tracked, so a flush
// is required to ensure that the copy captures the most recent contents
// of the source texture. See similar behaviour in
// of the source texture. See similar behavior in
// GrContext::resolveRenderTarget.
this->flush();
@ -1584,23 +1583,19 @@ void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
return;
}
GrEffectStage stage;
SkAutoTUnref<const GrEffectRef> effect;
SkMatrix textureMatrix;
textureMatrix.setIDiv(texture->width(), texture->height());
// allocate a tmp buffer and sw convert the pixels to premul
SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
bool effectInstalled = false;
if (kUnpremul_PixelOpsFlag & flags) {
if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
return;
}
effectInstalled = this->installUPMToPMEffect(texture,
swapRAndB,
textureMatrix,
&stage);
if (!effectInstalled) {
effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
if (NULL == effect) {
SkCanvas::Config8888 srcConfig8888, dstConfig8888;
GR_DEBUGCODE(bool success = )
grconfig_to_config8888(config, true, &srcConfig8888);
@ -1617,13 +1612,11 @@ void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
rowBytes = 4 * width;
}
}
if (!effectInstalled) {
SkAssertResult(GrConfigConversionEffect::InstallEffect(
texture,
if (NULL == effect) {
effect.reset(GrConfigConversionEffect::Create(texture,
swapRAndB,
GrConfigConversionEffect::kNone_PMConversion,
textureMatrix,
&stage));
textureMatrix));
}
this->writeTexturePixels(texture,
@ -1633,7 +1626,8 @@ void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
GrDrawState* drawState = fGpu->drawState();
*drawState->stage(0) = stage;
GrAssert(effect);
*drawState->setEffect(0, effect);
SkMatrix matrix;
matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
@ -1822,10 +1816,9 @@ void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
}
}
bool GrContext::installPMToUPMEffect(GrTexture* texture,
const GrEffectRef* GrContext::createPMToUPMEffect(GrTexture* texture,
bool swapRAndB,
const SkMatrix& matrix,
GrEffectStage* stage) {
const SkMatrix& matrix) {
if (!fDidTestPMConversions) {
test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
fDidTestPMConversions = true;
@ -1833,17 +1826,15 @@ bool GrContext::installPMToUPMEffect(GrTexture* texture,
GrConfigConversionEffect::PMConversion pmToUPM =
static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
GrConfigConversionEffect::InstallEffect(texture, swapRAndB, pmToUPM, matrix, stage);
return true;
return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
} else {
return false;
return NULL;
}
}
bool GrContext::installUPMToPMEffect(GrTexture* texture,
const GrEffectRef* GrContext::createUPMToPMEffect(GrTexture* texture,
bool swapRAndB,
const SkMatrix& matrix,
GrEffectStage* stage) {
const SkMatrix& matrix) {
if (!fDidTestPMConversions) {
test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
fDidTestPMConversions = true;
@ -1851,10 +1842,9 @@ bool GrContext::installUPMToPMEffect(GrTexture* texture,
GrConfigConversionEffect::PMConversion upmToPM =
static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
GrConfigConversionEffect::InstallEffect(texture, swapRAndB, upmToPM, matrix, stage);
return true;
return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
} else {
return false;
return NULL;
}
}

View File

@ -13,7 +13,9 @@ void GrDrawState::setFromPaint(const GrPaint& paint) {
for (int i = 0; i < GrPaint::kMaxColorStages; ++i) {
int s = i + GrPaint::kFirstColorStage;
if (paint.isColorStageEnabled(i)) {
*this->stage(s) = paint.getColorStage(i);
fStages[s] = paint.getColorStage(i);
} else {
fStages[s].setEffect(NULL);
}
}
@ -22,7 +24,9 @@ void GrDrawState::setFromPaint(const GrPaint& paint) {
for (int i = 0; i < GrPaint::kMaxCoverageStages; ++i) {
int s = i + GrPaint::kFirstCoverageStage;
if (paint.isCoverageStageEnabled(i)) {
*this->stage(s) = paint.getCoverageStage(i);
fStages[s] = paint.getCoverageStage(i);
} else {
fStages[s].setEffect(NULL);
}
}
@ -48,7 +52,7 @@ void GrDrawState::AutoViewMatrixRestore::restore() {
fDrawState->setViewMatrix(fViewMatrix);
for (int s = 0; s < GrDrawState::kNumStages; ++s) {
if (fRestoreMask & (1 << s)) {
fDrawState->stage(s)->restoreCoordChange(fSavedCoordChanges[s]);
fDrawState->fStages[s].restoreCoordChange(fSavedCoordChanges[s]);
}
}
}
@ -71,8 +75,8 @@ void GrDrawState::AutoViewMatrixRestore::set(GrDrawState* drawState,
for (int s = 0; s < GrDrawState::kNumStages; ++s) {
if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) {
fRestoreMask |= (1 << s);
fDrawState->stage(s)->saveCoordChange(&fSavedCoordChanges[s]);
drawState->stage(s)->preConcatCoordChange(preconcatMatrix);
fDrawState->fStages[s].saveCoordChange(&fSavedCoordChanges[s]);
drawState->fStages[s].preConcatCoordChange(preconcatMatrix);
}
}
}
@ -84,7 +88,7 @@ void GrDrawState::AutoDeviceCoordDraw::restore() {
fDrawState->setViewMatrix(fViewMatrix);
for (int s = 0; s < GrDrawState::kNumStages; ++s) {
if (fRestoreMask & (1 << s)) {
fDrawState->stage(s)->restoreCoordChange(fSavedCoordChanges[s]);
fDrawState->fStages[s].restoreCoordChange(fSavedCoordChanges[s]);
}
}
}
@ -117,7 +121,7 @@ bool GrDrawState::AutoDeviceCoordDraw::set(GrDrawState* drawState,
inverted = true;
}
fRestoreMask |= (1 << s);
GrEffectStage* stage = drawState->stage(s);
GrEffectStage* stage = drawState->fStages + s;
stage->saveCoordChange(&fSavedCoordChanges[s]);
stage->preConcatCoordChange(invVM);
}

View File

@ -185,16 +185,21 @@ public:
/// @}
///////////////////////////////////////////////////////////////////////////
/// @name Textures
/// @name Effect Stages
////
const GrEffectRef* setEffect(int stageIdx, const GrEffectRef* effect) {
fStages[stageIdx].setEffect(effect);
return effect;
}
/**
* Creates a GrSimpleTextureEffect.
*/
void createTextureEffect(int stageIdx, GrTexture* texture, const SkMatrix& matrix) {
GrAssert(!this->getStage(stageIdx).getEffect());
GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix);
this->stage(stageIdx)->setEffect(effect)->unref();
this->setEffect(stageIdx, effect)->unref();
}
void createTextureEffect(int stageIdx,
GrTexture* texture,
@ -202,7 +207,7 @@ public:
const GrTextureParams& params) {
GrAssert(!this->getStage(stageIdx).getEffect());
GrEffectRef* effect = GrSimpleTextureEffect::Create(texture, matrix, params);
this->stage(stageIdx)->setEffect(effect)->unref();
this->setEffect(stageIdx, effect)->unref();
}
bool stagesDisabled() {
@ -214,9 +219,7 @@ public:
return true;
}
void disableStage(int stageIdx) {
fStages[stageIdx].setEffect(NULL);
}
void disableStage(int stageIdx) { this->setEffect(stageIdx, NULL); }
/**
* Release all the GrEffects referred to by this draw state.
@ -239,12 +242,6 @@ public:
GrDrawState* fDrawState;
};
/// @}
///////////////////////////////////////////////////////////////////////////
/// @name Stages
////
/**
* Returns the current stage by index.
*/
@ -253,14 +250,6 @@ public:
return fStages[stageIdx];
}
/**
* Writable pointer to a stage.
*/
GrEffectStage* stage(int stageIdx) {
GrAssert((unsigned)stageIdx < kNumStages);
return fStages + stageIdx;
}
/**
* Called when the source coord system is changing. preConcat gives the transformation from the
* old coord system to the new coord system.

View File

@ -197,7 +197,6 @@ void GrSWMaskHelper::DrawToTargetWithPathMask(GrTexture* texture,
kPathMaskStage = GrPaint::kTotalStages,
};
GrAssert(!drawState->isStageEnabled(kPathMaskStage));
drawState->stage(kPathMaskStage)->reset();
drawState->createTextureEffect(kPathMaskStage, texture, SkMatrix::I());
SkScalar w = SkIntToScalar(rect.width());
SkScalar h = SkIntToScalar(rect.height());

View File

@ -30,8 +30,6 @@ void GrTextContext::flushGlyphs() {
GrDrawState* drawState = fDrawTarget->drawState();
if (fCurrVertex > 0) {
// setup our sampler state for our text texture/atlas
drawState->stage(kGlyphMaskStage)->reset();
GrAssert(GrIsALIGN4(fCurrVertex));
GrAssert(fCurrTexture);
GrTextureParams params(SkShader::kRepeat_TileMode, false);

View File

@ -261,29 +261,26 @@ void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context
}
}
bool GrConfigConversionEffect::InstallEffect(GrTexture* texture,
const GrEffectRef* GrConfigConversionEffect::Create(GrTexture* texture,
bool swapRedAndBlue,
PMConversion pmConversion,
const SkMatrix& matrix,
GrEffectStage* stage) {
const SkMatrix& matrix) {
if (!swapRedAndBlue && kNone_PMConversion == pmConversion) {
// If we returned a GrConfigConversionEffect that was equivalent to a GrSingleTextureEffect
// If we returned a GrConfigConversionEffect that was equivalent to a GrSimpleTextureEffect
// then we may pollute our texture cache with redundant shaders. So in the case that no
// conversions were requested we instead return a GrSingleTextureEffect.
stage->setEffect(GrSimpleTextureEffect::Create(texture, matrix))->unref();
return true;
// conversions were requested we instead return a GrSimpleTextureEffect.
return GrSimpleTextureEffect::Create(texture, matrix);
} else {
if (kRGBA_8888_GrPixelConfig != texture->config() &&
kBGRA_8888_GrPixelConfig != texture->config() &&
kNone_PMConversion != pmConversion) {
// The PM conversions assume colors are 0..255
return false;
return NULL;
}
AutoEffectUnref effect(SkNEW_ARGS(GrConfigConversionEffect, (texture,
swapRedAndBlue,
pmConversion,
matrix)));
stage->setEffect(CreateEffectRef(effect))->unref();
return true;
return CreateEffectRef(effect);
}
}

View File

@ -35,11 +35,10 @@ public:
};
// Installs an effect in the GrEffectStage to perform a config conversion.
static bool InstallEffect(GrTexture*,
static const GrEffectRef* Create(GrTexture*,
bool swapRedAndBlue,
PMConversion pmConversion,
const SkMatrix& matrix,
GrEffectStage* stage);
const SkMatrix& matrix);
static const char* Name() { return "Config Conversion"; }
typedef GrGLConfigConversionEffect GLEffect;

View File

@ -169,12 +169,10 @@ void forceLinking();
void forceLinking() {
SkLightingImageFilter::CreateDistantLitDiffuse(SkPoint3(0,0,0), 0, 0, 0);
SkMagnifierImageFilter mag(SkRect::MakeWH(SK_Scalar1, SK_Scalar1), SK_Scalar1);
GrEffectStage dummyStage;
GrConfigConversionEffect::InstallEffect(NULL,
GrConfigConversionEffect::Create(NULL,
false,
GrConfigConversionEffect::kNone_PMConversion,
SkMatrix::I(),
&dummyStage);
SkMatrix::I());
SkScalar matrix[20];
SkColorMatrixFilter cmf(matrix);
}