Make GrGLShaderBuilder::TextureSampler extract only required info from GrTextureAccess.

This will make it possible to init a TextureSampler without a texture or a specific config.

Also unify two separate bitfields of color components in GPU code.
Review URL: https://codereview.chromium.org/13121002

git-svn-id: http://skia.googlecode.com/svn/trunk@8428 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/effects/SkBlendImageFilter.cpp b/src/effects/SkBlendImageFilter.cpp
index 1c85fe5..330e87d 100644
--- a/src/effects/SkBlendImageFilter.cpp
+++ b/src/effects/SkBlendImageFilter.cpp
@@ -215,7 +215,7 @@
     // communicate this.)
     if (GrPixelConfigIsOpaque(fForegroundAccess.getTexture()->config()) ||
         GrPixelConfigIsOpaque(fBackgroundAccess.getTexture()->config())) {
-        *validFlags = kA_ValidComponentFlag;
+        *validFlags = kA_GrColorComponentFlag;
         *color = GrColorPackRGBA(0, 0, 0, 0xff);
     } else {
         *validFlags = 0;
diff --git a/src/effects/SkColorMatrixFilter.cpp b/src/effects/SkColorMatrixFilter.cpp
index c2ab3fd..661544b 100644
--- a/src/effects/SkColorMatrixFilter.cpp
+++ b/src/effects/SkColorMatrixFilter.cpp
@@ -344,11 +344,11 @@
         // The matrix is defined such the 4th row determines the output alpha. The first four
         // columns of that row multiply the input r, g, b, and a, respectively, and the last column
         // is the "translation".
-        static const ValidComponentFlags kRGBAFlags[] = {
-            kR_ValidComponentFlag,
-            kG_ValidComponentFlag,
-            kB_ValidComponentFlag,
-            kA_ValidComponentFlag
+        static const uint32_t kRGBAFlags[] = {
+            kR_GrColorComponentFlag,
+            kG_GrColorComponentFlag,
+            kB_GrColorComponentFlag,
+            kA_GrColorComponentFlag
         };
         static const int kShifts[] = {
             GrColor_SHIFT_R, GrColor_SHIFT_G, GrColor_SHIFT_B, GrColor_SHIFT_A,
@@ -373,7 +373,7 @@
             }
         }
         outputA += fMatrix.fMat[kAlphaRowTranslateIdx];
-        *validFlags = kA_ValidComponentFlag;
+        *validFlags = kA_GrColorComponentFlag;
         // We pin the color to [0,1]. This would happen to the *final* color output from the frag
         // shader but currently the effect does not pin its own output. So in the case of over/
         // underflow this may deviate from the actual result. Maybe the effect should pin its
diff --git a/src/effects/SkTableColorFilter.cpp b/src/effects/SkTableColorFilter.cpp
index d20283f..9979fae 100644
--- a/src/effects/SkTableColorFilter.cpp
+++ b/src/effects/SkTableColorFilter.cpp
@@ -348,16 +348,16 @@
     // If we kept the table in the effect then we could actually run known inputs through the
     // table.
     if (fFlags & SkTable_ColorFilter::kR_Flag) {
-        *validFlags &= ~kR_ValidComponentFlag;
+        *validFlags &= ~kR_GrColorComponentFlag;
     }
     if (fFlags & SkTable_ColorFilter::kG_Flag) {
-        *validFlags &= ~kG_ValidComponentFlag;
+        *validFlags &= ~kG_GrColorComponentFlag;
     }
     if (fFlags & SkTable_ColorFilter::kB_Flag) {
-        *validFlags &= ~kB_ValidComponentFlag;
+        *validFlags &= ~kB_GrColorComponentFlag;
     }
     if (fFlags & SkTable_ColorFilter::kA_Flag) {
-        *validFlags &= ~kA_ValidComponentFlag;
+        *validFlags &= ~kA_GrColorComponentFlag;
     }
 }
 
diff --git a/src/effects/gradients/SkGradientShader.cpp b/src/effects/gradients/SkGradientShader.cpp
index d3b2fd4..684355d 100644
--- a/src/effects/gradients/SkGradientShader.cpp
+++ b/src/effects/gradients/SkGradientShader.cpp
@@ -866,8 +866,8 @@
 }
 
 void GrGradientEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
-    if (fIsOpaque && (kA_ValidComponentFlag & *validFlags) && 0xff == GrColorUnpackA(*color)) {
-        *validFlags = kA_ValidComponentFlag;
+    if (fIsOpaque && (kA_GrColorComponentFlag & *validFlags) && 0xff == GrColorUnpackA(*color)) {
+        *validFlags = kA_GrColorComponentFlag;
     } else {
         *validFlags = 0;
     }
diff --git a/src/gpu/GrDrawState.cpp b/src/gpu/GrDrawState.cpp
index e108999..f9e12cc 100644
--- a/src/gpu/GrDrawState.cpp
+++ b/src/gpu/GrDrawState.cpp
@@ -236,7 +236,7 @@
         validComponentFlags = 0;
         color = 0; // not strictly necessary but we get false alarms from tools about uninit.
     } else {
-        validComponentFlags = GrEffect::kAll_ValidComponentFlags;
+        validComponentFlags = kRGBA_GrColorComponentFlags;
         color = this->getColor();
     }
 
@@ -275,7 +275,7 @@
             }
         }
     }
-    return (GrEffect::kA_ValidComponentFlag & validComponentFlags) && 0xff == GrColorUnpackA(color);
+    return (kA_GrColorComponentFlag & validComponentFlags) && 0xff == GrColorUnpackA(color);
 }
 
 bool GrDrawState::hasSolidCoverage(GrAttribBindings bindings) const {
@@ -291,7 +291,7 @@
         validComponentFlags = 0;
     } else {
         coverage = fCommon.fCoverage;
-        validComponentFlags = GrEffect::kAll_ValidComponentFlags;
+        validComponentFlags = kRGBA_GrColorComponentFlags;
     }
 
     // Run through the coverage stages and see if the coverage will be all ones at the end.
@@ -301,7 +301,7 @@
             (*effect)->getConstantColorComponents(&coverage, &validComponentFlags);
         }
     }
-    return (GrEffect::kAll_ValidComponentFlags == validComponentFlags)  && (0xffffffff == coverage);
+    return (kRGBA_GrColorComponentFlags == validComponentFlags)  && (0xffffffff == coverage);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/GrGpu.cpp b/src/gpu/GrGpu.cpp
index c9e876a..73e4d3e 100644
--- a/src/gpu/GrGpu.cpp
+++ b/src/gpu/GrGpu.cpp
@@ -49,7 +49,7 @@
     poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
 #endif
 
-    for (int i = 0; i < kGrPixelConfigCount; ++i) {
+    for (int i = 0; i < kGrPixelConfigCnt; ++i) {
         fConfigRenderSupport[i] = false;
     };
 }
diff --git a/src/gpu/GrGpu.h b/src/gpu/GrGpu.h
index 41ebb73..602197b 100644
--- a/src/gpu/GrGpu.h
+++ b/src/gpu/GrGpu.h
@@ -299,7 +299,7 @@
      * Can the provided configuration act as a color render target?
      */
     bool isConfigRenderable(GrPixelConfig config) const {
-        GrAssert(kGrPixelConfigCount > config);
+        GrAssert(kGrPixelConfigCnt > config);
         return fConfigRenderSupport[config];
     }
 
@@ -403,7 +403,7 @@
 
     // Derived classes need access to this so they can fill it out in their
     // constructors
-    bool    fConfigRenderSupport[kGrPixelConfigCount];
+    bool    fConfigRenderSupport[kGrPixelConfigCnt];
 
     // Helpers for setting up geometry state
     void finalizeReservedVertices();
diff --git a/src/gpu/GrTextureAccess.cpp b/src/gpu/GrTextureAccess.cpp
index 5c3a36d..499b1f2 100644
--- a/src/gpu/GrTextureAccess.cpp
+++ b/src/gpu/GrTextureAccess.cpp
@@ -6,7 +6,7 @@
  */
 
 #include "GrTextureAccess.h"
-
+#include "GrColor.h"
 #include "GrTexture.h"
 
 GrTextureAccess::GrTextureAccess() {
@@ -68,7 +68,7 @@
     fTexture.reset(SkRef(texture));
     fParams = params;
     memcpy(fSwizzle, "rgba", 5);
-    fSwizzleMask = (kRGB_SwizzleMask | kA_SwizzleFlag);
+    fSwizzleMask = kRGBA_GrColorComponentFlags;
 }
 
 void GrTextureAccess::reset(GrTexture* texture,
@@ -78,7 +78,7 @@
     fTexture.reset(SkRef(texture));
     fParams.reset(tileXAndY, bilerp);
     memcpy(fSwizzle, "rgba", 5);
-    fSwizzleMask = (kRGB_SwizzleMask | kA_SwizzleFlag);
+    fSwizzleMask = kRGBA_GrColorComponentFlags;
 }
 
 void GrTextureAccess::setSwizzle(const char* swizzle) {
@@ -88,16 +88,16 @@
         fSwizzle[i] = swizzle[i];
         switch (swizzle[i]) {
             case 'r':
-                fSwizzleMask |= kR_SwizzleFlag;
+                fSwizzleMask |= kR_GrColorComponentFlag;
                 break;
             case 'g':
-                fSwizzleMask |= kG_SwizzleFlag;
+                fSwizzleMask |= kG_GrColorComponentFlag;
                 break;
             case 'b':
-                fSwizzleMask |= kB_SwizzleFlag;
+                fSwizzleMask |= kB_GrColorComponentFlag;
                 break;
             case 'a':
-                fSwizzleMask |= kA_SwizzleFlag;
+                fSwizzleMask |= kA_GrColorComponentFlag;
                 break;
             default:
                 GrCrash("Unexpected swizzle string character.");
diff --git a/src/gpu/effects/GrSingleTextureEffect.h b/src/gpu/effects/GrSingleTextureEffect.h
index 82037cf..3e0af65 100644
--- a/src/gpu/effects/GrSingleTextureEffect.h
+++ b/src/gpu/effects/GrSingleTextureEffect.h
@@ -54,9 +54,9 @@
      * texture.
      */
     void updateConstantColorComponentsForModulation(GrColor* color, uint32_t* validFlags) const {
-        if ((*validFlags & kA_ValidComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
+        if ((*validFlags & kA_GrColorComponentFlag) && 0xFF == GrColorUnpackA(*color) &&
             GrPixelConfigIsOpaque(this->texture(0)->config())) {
-            *validFlags = kA_ValidComponentFlag;
+            *validFlags = kA_GrColorComponentFlag;
         } else {
             *validFlags = 0;
         }
diff --git a/src/gpu/gl/GrGLCaps.cpp b/src/gpu/gl/GrGLCaps.cpp
index 5e555f9..1f3cf16 100644
--- a/src/gpu/gl/GrGLCaps.cpp
+++ b/src/gpu/gl/GrGLCaps.cpp
@@ -462,7 +462,7 @@
 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
     return;
 #endif
-    GrAssert((unsigned)config < kGrPixelConfigCount);
+    GrAssert((unsigned)config < kGrPixelConfigCnt);
     GrAssert(fStencilFormats.count() == fStencilVerifiedColorConfigs.count());
     int count = fStencilFormats.count();
     // we expect a really small number of possible formats so linear search
@@ -485,7 +485,7 @@
 #if !GR_GL_CHECK_FBO_STATUS_ONCE_PER_FORMAT
     return false;
 #endif
-    GrAssert((unsigned)config < kGrPixelConfigCount);
+    GrAssert((unsigned)config < kGrPixelConfigCnt);
     int count = fStencilFormats.count();
     // we expect a really small number of possible formats so linear search
     // should be OK
diff --git a/src/gpu/gl/GrGLCaps.h b/src/gpu/gl/GrGLCaps.h
index 920140f..4599e57 100644
--- a/src/gpu/gl/GrGLCaps.h
+++ b/src/gpu/gl/GrGLCaps.h
@@ -252,7 +252,7 @@
             }
         }
 
-        static const int kNumUints = (kGrPixelConfigCount  + 31) / 32;
+        static const int kNumUints = (kGrPixelConfigCnt  + 31) / 32;
         uint32_t fVerifiedColorConfigs[kNumUints];
 
         void markVerified(GrPixelConfig config) {
diff --git a/src/gpu/gl/GrGLShaderBuilder.cpp b/src/gpu/gl/GrGLShaderBuilder.cpp
index 3808402..a5c96c7 100644
--- a/src/gpu/gl/GrGLShaderBuilder.cpp
+++ b/src/gpu/gl/GrGLShaderBuilder.cpp
@@ -40,10 +40,10 @@
 inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps,
                                              const GrTextureAccess& access) {
     if (GrPixelConfigIsAlphaOnly(access.getTexture()->config())) {
-        if (caps.textureRedSupport() && (GrTextureAccess::kA_SwizzleFlag & access.swizzleMask())) {
+        if (caps.textureRedSupport() && (kA_GrColorComponentFlag & access.swizzleMask())) {
             return true;
         }
-        if (GrTextureAccess::kRGB_SwizzleMask & access.swizzleMask()) {
+        if (kRGB_GrColorComponentFlags & access.swizzleMask()) {
             return true;
         }
     }
@@ -51,14 +51,15 @@
 }
 
 void append_swizzle(SkString* outAppend,
-                    const GrTextureAccess& access,
+                    const GrGLShaderBuilder::TextureSampler& texSampler,
                     const GrGLCaps& caps) {
-    const char* swizzle = access.getSwizzle();
+    const char* swizzle = texSampler.swizzle();
     char mangledSwizzle[5];
 
     // The swizzling occurs using texture params instead of shader-mangling if ARB_texture_swizzle
     // is available.
-    if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(access.getTexture()->config())) {
+    if (!caps.textureSwizzleSupport() &&
+        (kA_GrColorComponentFlag == texSampler.configComponentMask())) {
         char alphaChar = caps.textureRedSupport() ? 'r' : 'a';
         int i;
         for (i = 0; '\0' != swizzle[i]; ++i) {
@@ -151,14 +152,13 @@
                                             const GrGLShaderBuilder::TextureSampler& sampler,
                                             const char* coordName,
                                             GrSLType varyingType) const {
-    GrAssert(NULL != sampler.textureAccess());
     GrAssert(NULL != coordName);
 
     out->appendf("%s(%s, %s)",
                  sample_function_name(varyingType, fCtxInfo.glslGeneration()),
                  this->getUniformCStr(sampler.fSamplerUniform),
                  coordName);
-    append_swizzle(out, *sampler.textureAccess(), *fCtxInfo.caps());
+    append_swizzle(out, sampler, *fCtxInfo.caps());
 }
 
 void GrGLShaderBuilder::appendTextureLookup(ShaderType type,
@@ -191,17 +191,6 @@
     if (!caps.textureSwizzleSupport() && swizzle_requires_alpha_remapping(caps, access)) {
         key = 1;
     }
-#if GR_DEBUG
-    // Assert that key is set iff the swizzle will be modified.
-    SkString origString(access.getSwizzle());
-    origString.prepend(".");
-    SkString modifiedString;
-    append_swizzle(&modifiedString, access, caps);
-    if (!modifiedString.size()) {
-        modifiedString = ".rgba";
-    }
-    GrAssert(SkToBool(key) == (modifiedString != origString));
-#endif
     return key;
 }
 
diff --git a/src/gpu/gl/GrGLShaderBuilder.h b/src/gpu/gl/GrGLShaderBuilder.h
index 9d64143..d947771 100644
--- a/src/gpu/gl/GrGLShaderBuilder.h
+++ b/src/gpu/gl/GrGLShaderBuilder.h
@@ -10,6 +10,7 @@
 
 #include "GrAllocator.h"
 #include "GrBackendEffectFactory.h"
+#include "GrColor.h"
 #include "GrEffect.h"
 #include "gl/GrGLSL.h"
 #include "gl/GrGLUniformManager.h"
@@ -31,31 +32,40 @@
     class TextureSampler {
     public:
         TextureSampler()
-            : fTextureAccess(NULL)
-            , fSamplerUniform(GrGLUniformManager::kInvalidUniformHandle) {}
+            : fConfigComponentMask(0)
+            , fSamplerUniform(GrGLUniformManager::kInvalidUniformHandle) {
+            // we will memcpy the first 4 bytes from passed in swizzle. This ensures the string is
+            // terminated.
+            fSwizzle[4] = '\0';
+        }
 
         TextureSampler(const TextureSampler& other) { *this = other; }
 
         TextureSampler& operator= (const TextureSampler& other) {
-            GrAssert(NULL == fTextureAccess);
+            GrAssert(0 == fConfigComponentMask);
             GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUniform);
 
-            fTextureAccess = other.fTextureAccess;
+            fConfigComponentMask = other.fConfigComponentMask;
             fSamplerUniform = other.fSamplerUniform;
             return *this;
         }
 
-        const GrTextureAccess* textureAccess() const { return fTextureAccess; }
+        // bitfield of GrColorComponentFlags present in the texture's config.
+        uint32_t configComponentMask() const { return fConfigComponentMask; }
+
+        const char* swizzle() const { return fSwizzle; }
 
     private:
         // The idx param is used to ensure multiple samplers within a single effect have unique
-        // uniform names.
-        void init(GrGLShaderBuilder* builder, const GrTextureAccess* access, int idx) {
-            GrAssert(NULL == fTextureAccess);
+        // uniform names. swizzle is a four char max string made up of chars 'r', 'g', 'b', and 'a'.
+        void init(GrGLShaderBuilder* builder,
+                  uint32_t configComponentMask,
+                  const char* swizzle,
+                  int idx) {
+            GrAssert(0 == fConfigComponentMask);
             GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUniform);
 
             GrAssert(NULL != builder);
-            GrAssert(NULL != access);
             SkString name;
             name.printf("Sampler%d_", idx);
             fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
@@ -63,14 +73,23 @@
                                                   name.c_str());
             GrAssert(GrGLUniformManager::kInvalidUniformHandle != fSamplerUniform);
 
-            fTextureAccess = access;
+            fConfigComponentMask = configComponentMask;
+            memcpy(fSwizzle, swizzle, 4);
         }
 
-        const GrTextureAccess*            fTextureAccess;
+        void init(GrGLShaderBuilder* builder, const GrTextureAccess* access, int idx) {
+            GrAssert(NULL != access);
+            this->init(builder,
+                       GrPixelConfigComponentMask(access->getTexture()->config()),
+                       access->getSwizzle(),
+                       idx);
+        }
+
+        uint32_t                          fConfigComponentMask;
+        char                              fSwizzle[5];
         GrGLUniformManager::UniformHandle fSamplerUniform;
 
-        friend class GrGLShaderBuilder; // to access fSamplerUniform
-        friend class GrGLProgram;       // to construct these and access fSamplerUniform.
+        friend class GrGLShaderBuilder; // to call init().
     };
 
     typedef SkTArray<TextureSampler> TextureSamplerArray;