make skpaint2grpaint functions be local to skgpudevice.cpp

Review URL: http://codereview.appspot.com/5726061/



git-svn-id: http://skia.googlecode.com/svn/trunk@3322 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/SkGpuDevice.h b/include/gpu/SkGpuDevice.h
index 6663ed5..46c15d6 100644
--- a/include/gpu/SkGpuDevice.h
+++ b/include/gpu/SkGpuDevice.h
@@ -114,6 +114,8 @@
                              const SkMatrix& ctm,
                              SkBitmap* result, SkIPoint* offset) SK_OVERRIDE;
 
+    class SkAutoCachedTexture; // used internally
+
 protected:
     typedef GrContext::TextureCacheEntry TexCache;
     enum TexType {
@@ -128,29 +130,11 @@
                                 const GrSamplerState& sampler) const;
     void unlockCachedTexture(TexCache);
 
-    class SkAutoCachedTexture {
-    public:
-        SkAutoCachedTexture();
-        SkAutoCachedTexture(SkGpuDevice* device,
-                            const SkBitmap& bitmap,
-                            const GrSamplerState* sampler,
-                            GrTexture** texture);
-        ~SkAutoCachedTexture();
-
-        GrTexture* set(SkGpuDevice*, const SkBitmap&, const GrSamplerState*);
-
-    private:
-        SkGpuDevice*    fDevice;
-        TexCache        fTex;
-    };
-    friend class SkAutoTexCache;
-
     // overrides from SkDevice
     virtual bool onReadPixels(const SkBitmap& bitmap,
                               int x, int y,
                               SkCanvas::Config8888 config8888) SK_OVERRIDE;
 
-
 private:
     GrContext*      fContext;
 
@@ -166,30 +150,6 @@
     // called from rt and tex cons
     void initFromRenderTarget(GrContext*, GrRenderTarget*);
 
-    // doesn't set the texture/sampler/matrix state
-    // caller needs to null out GrPaint's texture if
-    // non-textured drawing is desired.
-    // Set constantColor to true if a constant color
-    // will be used.  This is an optimization, and can
-    // always be set to false. constantColor should
-    // never be true if justAlpha is true.
-    bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
-                                 bool justAlpha,
-                                 GrPaint* grPaint,
-                                 bool constantColor);
-
-    // uses the SkShader to setup paint, act used to
-    // hold lock on cached texture and free it when
-    // destroyed.
-    // If there is no shader, constantColor will
-    // be passed to skPaint2GrPaintNoShader.  Otherwise
-    // it is ignored.
-    bool skPaint2GrPaintShader(const SkPaint& skPaint,
-                               SkAutoCachedTexture* act,
-                               const SkMatrix& ctm,
-                               GrPaint* grPaint,
-                               bool constantColor);
-
     // override from SkDevice
     virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
                                                int width, int height,
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 0ea3452..0f4dea0 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -56,42 +56,46 @@
 #define BLUR_SIGMA_SCALE 0.6f
 ///////////////////////////////////////////////////////////////////////////////
 
-SkGpuDevice::SkAutoCachedTexture::
-             SkAutoCachedTexture(SkGpuDevice* device,
-                                 const SkBitmap& bitmap,
-                                 const GrSamplerState* sampler,
-                                 GrTexture** texture) {
-    GrAssert(texture);
-    *texture = this->set(device, bitmap, sampler);
-}
-
-SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
-}
-
-GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
-                                                 const SkBitmap& bitmap,
-                                                 const GrSamplerState* sampler) {
-    if (fTex.texture()) {
-        fDevice->unlockCachedTexture(fTex);
+class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
+public:
+    SkAutoCachedTexture() { }    
+    SkAutoCachedTexture(SkGpuDevice* device,
+                        const SkBitmap& bitmap,
+                        const GrSamplerState* sampler,
+                        GrTexture** texture) {
+        GrAssert(texture);
+        *texture = this->set(device, bitmap, sampler);
     }
-    fDevice = device;
-    GrTexture* texture = (GrTexture*)bitmap.getTexture();
-    if (texture) {
-        // return the native texture
-        fTex.reset();
-    } else {
-        // look it up in our cache
-        fTex = device->lockCachedTexture(bitmap, sampler);
-        texture = fTex.texture();
-    }
-    return texture;
-}
 
-SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
-    if (fTex.texture()) {
-        fDevice->unlockCachedTexture(fTex);
+    ~SkAutoCachedTexture() {
+        if (fTex.texture()) {
+            fDevice->unlockCachedTexture(fTex);
+        }
     }
-}
+
+    GrTexture* set(SkGpuDevice* device,
+                   const SkBitmap& bitmap,
+                   const GrSamplerState* sampler) {
+        if (fTex.texture()) {
+            fDevice->unlockCachedTexture(fTex);
+        }
+        fDevice = device;
+        GrTexture* texture = (GrTexture*)bitmap.getTexture();
+        if (texture) {
+            // return the native texture
+            fTex.reset();
+        } else {
+            // look it up in our cache
+            fTex = device->lockCachedTexture(bitmap, sampler);
+            texture = fTex.texture();
+        }
+        return texture;
+    }
+    
+private:
+    SkGpuDevice* fDevice;
+    GrContext::TextureCacheEntry fTex;
+};
 
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -415,10 +419,18 @@
     GrSamplerState::kRadial2_SampleMode,                // kTwoPointRadial_BitmapType
 };
 
-bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
-                                          bool justAlpha,
-                                          GrPaint* grPaint,
-                                          bool constantColor) {
+namespace {
+
+// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
+// justAlpha indicates that skPaint's alpha should be used rather than the color
+// Callers may subsequently modify the GrPaint. Setting constantColor indicates
+// that the final paint will draw the same color at every pixel. This allows
+// an optimization where the the color filter can be applied to the skPaint's
+// color once while converting to GrPain and then ignored.
+inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
+                                    bool justAlpha,
+                                    bool constantColor,
+                                    GrPaint* grPaint) {
 
     grPaint->fDither    = skPaint.isDither();
     grPaint->fAntiAlias = skPaint.isAntiAlias();
@@ -473,21 +485,26 @@
     return true;
 }
 
-bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
-                                        SkAutoCachedTexture* act,
-                                        const SkMatrix& ctm,
-                                        GrPaint* grPaint,
-                                        bool constantColor) {
+// This function is similar to skPaint2GrPaintNoShader but also converts
+// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
+// be used is set on grPaint and returned in param act. constantColor has the
+// same meaning as in skPaint2GrPaintNoShader.
+inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
+                                  const SkPaint& skPaint,
+                                  const SkMatrix& ctm,
+                                  bool constantColor,
+                                  SkGpuDevice::SkAutoCachedTexture* act,
+                                  GrPaint* grPaint) {
 
     SkASSERT(NULL != act);
 
     SkShader* shader = skPaint.getShader();
     if (NULL == shader) {
-        return this->skPaint2GrPaintNoShader(skPaint,
-                                             false,
-                                             grPaint,
-                                             constantColor);
-    } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
+        return skPaint2GrPaintNoShader(skPaint,
+                                       false,
+                                       constantColor,
+                                       grPaint);
+    } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
         return false;
     }
 
@@ -512,10 +529,10 @@
             // modulate the paint alpha by the shader's solid color alpha
             U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
             copy.setColor(SkColorSetA(color, newA));
-            return this->skPaint2GrPaintNoShader(copy,
-                                                 false,
-                                                 grPaint,
-                                                 constantColor);
+            return skPaint2GrPaintNoShader(copy,
+                                           false,
+                                           constantColor,
+                                           grPaint);
         }
         return false;
     }
@@ -534,7 +551,7 @@
                                   twoPointParams[2] < 0);
     }
 
-    GrTexture* texture = act->set(this, bitmap, sampler);
+    GrTexture* texture = act->set(dev, bitmap, sampler);
     if (NULL == texture) {
         SkDebugf("Couldn't convert bitmap to texture.\n");
         return false;
@@ -561,6 +578,7 @@
 
     return true;
 }
+}
 
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -573,11 +591,12 @@
 
     GrPaint grPaint;
     SkAutoCachedTexture act;
-    if (!this->skPaint2GrPaintShader(paint,
-                                     &act,
-                                     *draw.fMatrix,
-                                     &grPaint,
-                                     true)) {
+    if (!skPaint2GrPaintShader(this,
+                               paint,
+                               *draw.fMatrix,
+                               true,
+                               &act,
+                               &grPaint)) {
         return;
     }
 
@@ -608,11 +627,12 @@
 
     GrPaint grPaint;
     SkAutoCachedTexture act;
-    if (!this->skPaint2GrPaintShader(paint,
-                                     &act,
-                                     *draw.fMatrix,
-                                     &grPaint,
-                                     true)) {
+    if (!skPaint2GrPaintShader(this,
+                               paint,
+                               *draw.fMatrix,
+                               true,
+                               &act,
+                               &grPaint)) {
         return;
     }
 
@@ -669,11 +689,12 @@
 
     GrPaint grPaint;
     SkAutoCachedTexture act;
-    if (!this->skPaint2GrPaintShader(paint,
-                                     &act,
-                                     *draw.fMatrix,
-                                     &grPaint,
-                                     true)) {
+    if (!skPaint2GrPaintShader(this,
+                               paint,
+                               *draw.fMatrix,
+                               true,
+                               &act,
+                               &grPaint)) {
         return;
     }
     fContext->drawRect(grPaint, rect, doStroke ? width : -1);
@@ -1112,11 +1133,12 @@
 
     GrPaint grPaint;
     SkAutoCachedTexture act;
-    if (!this->skPaint2GrPaintShader(paint,
-                                     &act,
-                                     *draw.fMatrix,
-                                     &grPaint,
-                                     true)) {
+    if (!skPaint2GrPaintShader(this,
+                               paint,
+                               *draw.fMatrix,
+                               true,
+                               &act,
+                               &grPaint)) {
         return;
     }
 
@@ -1345,7 +1367,7 @@
     }
 
     GrPaint grPaint;
-    if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
+    if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
         return;
     }
     GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
@@ -1499,7 +1521,7 @@
     int h = bitmap.height();
 
     GrPaint grPaint;
-    if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
+    if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
         return;
     }
 
@@ -1568,7 +1590,7 @@
 
     GrPaint grPaint;
     if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
-        !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
+        !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
         return;
     }
 
@@ -1641,17 +1663,19 @@
     SkAutoCachedTexture act;
     // we ignore the shader if texs is null.
     if (NULL == texs) {
-        if (!this->skPaint2GrPaintNoShader(paint,
-                                           false,
-                                           &grPaint,
-                                           NULL == colors)) {
+        if (!skPaint2GrPaintNoShader(paint,
+                                     false,
+                                     NULL == colors,
+                                     &grPaint)) {
             return;
         }
     } else {
-        if (!this->skPaint2GrPaintShader(paint, &act,
-                                         *draw.fMatrix,
-                                         &grPaint,
-                                         NULL == colors)) {
+        if (!skPaint2GrPaintShader(this,
+                                   paint,
+                                   *draw.fMatrix,
+                                   NULL == colors,
+                                   &act,
+                                   &grPaint)) {
             return;
         }
     }
@@ -1751,11 +1775,12 @@
         GrPaint grPaint;
         SkAutoCachedTexture act;
 
-        if (!this->skPaint2GrPaintShader(paint,
-                                         &act,
-                                         *draw.fMatrix,
-                                         &grPaint,
-                                         true)) {
+        if (!skPaint2GrPaintShader(this,
+                                   paint,
+                                   *draw.fMatrix,
+                                   true,
+                                   &act,
+                                   &grPaint)) {
             return;
         }
         GrTextContext context(fContext, grPaint, draw.fExtMatrix);
@@ -1779,11 +1804,12 @@
 
         GrPaint grPaint;
         SkAutoCachedTexture act;
-        if (!this->skPaint2GrPaintShader(paint,
-                                         &act,
-                                         *draw.fMatrix,
-                                         &grPaint,
-                                         true)) {
+        if (!skPaint2GrPaintShader(this,
+                                   paint,
+                                   *draw.fMatrix,
+                                   true,
+                                   &act,
+                                   &grPaint)) {
             return;
         }