Remove getter of writable GrEffectStage from GrDrawState.
authorbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 28 Jan 2013 14:26:09 +0000 (14:26 +0000)
committerbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 28 Jan 2013 14:26:09 +0000 (14:26 +0000)
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

include/gpu/GrContext.h
src/gpu/GrClipMaskManager.cpp
src/gpu/GrContext.cpp
src/gpu/GrDrawState.cpp
src/gpu/GrDrawState.h
src/gpu/GrSWMaskHelper.cpp
src/gpu/GrTextContext.cpp
src/gpu/effects/GrConfigConversionEffect.cpp
src/gpu/effects/GrConfigConversionEffect.h
tests/GLProgramsTest.cpp

index 556893895a222d9316e0fa6dc2e140ffd76fb873..98683a475c9df89f7286234af16d992ca6d94331 100644 (file)
@@ -910,14 +910,17 @@ private:
     // for use with textures released from an GrAutoScratchTexture.
     void addExistingTextureToCache(GrTexture* texture);
 
-    bool installPMToUPMEffect(GrTexture* texture,
-                              bool swapRAndB,
-                              const SkMatrix& matrix,
-                              GrEffectStage* stage);
-    bool installUPMToPMEffect(GrTexture* texture,
-                              bool swapRAndB,
-                              const SkMatrix& matrix,
-                              GrEffectStage* stage);
+    /** 
+     * 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);
+    const GrEffectRef* createUPMToPMEffect(GrTexture* texture,
+                                           bool swapRAndB,
+                                           const SkMatrix& matrix);
 
     typedef GrRefCnt INHERITED;
 };
index f3f773d536901a6054b54008703c0ed26960e523..bc7b79188bd3175af036d54d7509941550ade932 100644 (file)
@@ -45,12 +45,10 @@ 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(
-        GrTextureDomainEffect::Create(result,
+    drawState->setEffect(kMaskStage,
+                         GrTextureDomainEffect::Create(result,
                                       mat,
                                       GrTextureDomainEffect::MakeTexelDomain(result, domainTexels),
                                       GrTextureDomainEffect::kDecal_WrapMode))->unref();
@@ -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),
index 46415834b394272e3a1465baac57c36d47ccefbf..b2df67c7995ebeabc192e4fd3c4c465c9ca8e3a3 100644 (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(
-                                            src,
-                                            swapRAndB,
-                                            GrConfigConversionEffect::kNone_PMConversion,
-                                            textureMatrix,
-                                            &stage));
+            if (NULL != effect || flipY || swapRAndB) {
+                if (!effect) {
+                    effect.reset(GrConfigConversionEffect::Create(
+                                                    src,
+                                                    swapRAndB,
+                                                    GrConfigConversionEffect::kNone_PMConversion,
+                                                    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,
-                                                    swapRAndB,
-                                                    GrConfigConversionEffect::kNone_PMConversion,
-                                                    textureMatrix,
-                                                    &stage));
+    if (NULL == effect) {
+        effect.reset(GrConfigConversionEffect::Create(texture,
+                                                      swapRAndB,
+                                                      GrConfigConversionEffect::kNone_PMConversion,
+                                                      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,
-                                     bool swapRAndB,
-                                     const SkMatrix& matrix,
-                                     GrEffectStage* stage) {
+const GrEffectRef* GrContext::createPMToUPMEffect(GrTexture* texture,
+                                                  bool swapRAndB,
+                                                  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,
-                                     bool swapRAndB,
-                                     const SkMatrix& matrix,
-                                     GrEffectStage* stage) {
+const GrEffectRef* GrContext::createUPMToPMEffect(GrTexture* texture,
+                                                  bool swapRAndB,
+                                                  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;
     }
 }
 
index bdb12302a14f7260fb2d0b04bf1ebb03bc1ad1c7..2d5c7aa68f76c9d0d502375e1356fc65d24aec8a 100644 (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);
         }
index ba9eb217d6336b717a2c32916f839c86ebbd86e1..326df0bf953a915491671e0fcffb3d19f5c750b7 100644 (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.
index 6d27d873b30ac2307d597f5c3f58e44d633254eb..50fcc2be1901596e442a354e0fa56027ee0f2d34 100644 (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());
index 9daa31572fa59f50c13fd308d300eb528d0fce70..beb3f91f6b92e4bb702f81c7dd4b911c4e10771b 100644 (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);
index 4c4d0792c08ecaba6f7e3838c0deb92383cddaec..92df82d9404fc4269b458c9b54dff2e9f81ea180 100644 (file)
@@ -261,29 +261,26 @@ void GrConfigConversionEffect::TestForPreservingPMConversions(GrContext* context
     }
 }
 
-bool GrConfigConversionEffect::InstallEffect(GrTexture* texture,
-                                             bool swapRedAndBlue,
-                                             PMConversion pmConversion,
-                                             const SkMatrix& matrix,
-                                             GrEffectStage* stage) {
+const GrEffectRef* GrConfigConversionEffect::Create(GrTexture* texture,
+                                                    bool swapRedAndBlue,
+                                                    PMConversion pmConversion,
+                                                    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);
     }
 }
index 3845d32bbd65cfb544e3f92c3a1903c9e217ae46..169c3d7db8fabc7e19480e4554550be8a09f4b02 100644 (file)
@@ -35,11 +35,10 @@ public:
     };
 
     // Installs an effect in the GrEffectStage to perform a config conversion.
-    static bool InstallEffect(GrTexture*,
-                              bool swapRedAndBlue,
-                              PMConversion pmConversion,
-                              const SkMatrix& matrix,
-                              GrEffectStage* stage);
+    static const GrEffectRef* Create(GrTexture*,
+                                     bool swapRedAndBlue,
+                                     PMConversion pmConversion,
+                                     const SkMatrix& matrix);
 
     static const char* Name() { return "Config Conversion"; }
     typedef GrGLConfigConversionEffect GLEffect;
index e83ddebfa97b1a6f1dc253b88e0dc3e8916f33e7..f9dd9188e451cd592460c465873b4068c3f5fcf0 100644 (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,
-                                            false,
-                                            GrConfigConversionEffect::kNone_PMConversion,
-                                            SkMatrix::I(),
-                                            &dummyStage);
+    GrConfigConversionEffect::Create(NULL,
+                                     false,
+                                     GrConfigConversionEffect::kNone_PMConversion,
+                                     SkMatrix::I());
     SkScalar matrix[20];
     SkColorMatrixFilter cmf(matrix);
 }