Move GrGpuGL::ProgramCache declaration to header

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



git-svn-id: http://skia.googlecode.com/svn/trunk@4113 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp
index 29c7900..8817068 100644
--- a/src/gpu/gl/GrGpuGL.cpp
+++ b/src/gpu/gl/GrGpuGL.cpp
@@ -207,7 +207,8 @@
 
     this->initCaps();
 
-    this->createProgramCache();
+    fProgramData = NULL;
+    fProgramCache = new ProgramCache(this->glContextInfo());
 
 #if 0
     this->programUnitTest();
@@ -225,7 +226,9 @@
         GL_CALL(UseProgram(0));
     }
 
-    this->deleteProgramCache();
+    delete fProgramCache;
+    fProgramCache = NULL;
+    fProgramData = NULL;
 
     // This must be called by before the GrDrawTarget destructor
     this->releaseGeometry();
diff --git a/src/gpu/gl/GrGpuGL.h b/src/gpu/gl/GrGpuGL.h
index 24d491d..7cbd230 100644
--- a/src/gpu/gl/GrGpuGL.h
+++ b/src/gpu/gl/GrGpuGL.h
@@ -10,6 +10,7 @@
 #ifndef GrGpuGL_DEFINED
 #define GrGpuGL_DEFINED
 
+#include "GrBinHashKey.h"
 #include "GrDrawState.h"
 #include "GrGpu.h"
 #include "GrGLContextInfo.h"
@@ -19,6 +20,7 @@
 #include "GrGLStencilBuffer.h"
 #include "GrGLTexture.h"
 #include "GrGLVertexBuffer.h"
+#include "../GrTHashCache.h"
 
 class GrGpuGL : public GrGpu {
 public:
@@ -211,16 +213,52 @@
     static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
 
 private:
-
     // for readability of function impls
     typedef GrGLProgram::ProgramDesc ProgramDesc;
     typedef ProgramDesc::StageDesc   StageDesc;
     typedef GrGLProgram::CachedData  CachedData;
 
-    class ProgramCache;
+    class ProgramCache : public ::GrNoncopyable {
+    public:
+        ProgramCache(const GrGLContextInfo& gl);
+        ~ProgramCache();
 
-    void createProgramCache();
-    void deleteProgramCache();
+        void abandon();
+        void invalidateViewMatrices();
+        CachedData* getProgramData(const GrGLProgram& desc,
+                                   GrCustomStage** stages);
+    private:
+        enum {
+            kKeySize = GrGLProgram::kProgramKeySize,
+            // We may actually have kMaxEntries+1 shaders in the GL context
+            // because we create a new shader before evicting from the cache.
+            kMaxEntries = 32
+        };
+
+        class Entry;
+        typedef GrBinHashKey<Entry, kKeySize> ProgramHashKey;
+
+        class Entry : public ::GrNoncopyable {
+        public:
+            Entry() {}
+            void copyAndTakeOwnership(Entry& entry);
+            int compare(const ProgramHashKey& key) const {
+                return fKey.compare(key);
+            }
+
+        public:
+            CachedData      fProgramData;
+            ProgramHashKey  fKey;
+            unsigned int    fLRUStamp;
+        };
+
+        GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
+
+        Entry                       fEntries[kMaxEntries];
+        int                         fCount;
+        unsigned int                fCurrLRUStamp;
+        const GrGLContextInfo&      fGL;
+    };
 
     // sets the texture matrix uniform for currently bound program
     void flushTextureMatrix(int stage);
diff --git a/src/gpu/gl/GrGpuGL_program.cpp b/src/gpu/gl/GrGpuGL_program.cpp
index 2448fa4..b8fcb1b 100644
--- a/src/gpu/gl/GrGpuGL_program.cpp
+++ b/src/gpu/gl/GrGpuGL_program.cpp
@@ -10,7 +10,6 @@
 #include "effects/GrConvolutionEffect.h"
 #include "effects/GrMorphologyEffect.h"
 
-#include "GrBinHashKey.h"
 #include "GrCustomStage.h"
 #include "GrGLProgramStage.h"
 #include "GrGLSL.h"
@@ -23,109 +22,76 @@
 #define SKIP_CACHE_CHECK    true
 #define GR_UINT32_MAX   static_cast<uint32_t>(-1)
 
-#include "../GrTHashCache.h"
+void GrGpuGL::ProgramCache::Entry::copyAndTakeOwnership(Entry& entry) {
+    fProgramData.copyAndTakeOwnership(entry.fProgramData);
+    fKey = entry.fKey; // ownership transfer
+    fLRUStamp = entry.fLRUStamp;
+}
 
-class GrGpuGL::ProgramCache : public ::GrNoncopyable {
-private:
-    class Entry;
+GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl)
+    : fCount(0)
+    , fCurrLRUStamp(0)
+    , fGL(gl) {
+}
 
-    typedef GrBinHashKey<Entry, GrGLProgram::kProgramKeySize> ProgramHashKey;
-
-    class Entry : public ::GrNoncopyable {
-    public:
-        Entry() {}
-        void copyAndTakeOwnership(Entry& entry) {
-            fProgramData.copyAndTakeOwnership(entry.fProgramData);
-            fKey = entry.fKey; // ownership transfer
-            fLRUStamp = entry.fLRUStamp;
-        }
-
-    public:
-        int compare(const ProgramHashKey& key) const { return fKey.compare(key); }
-
-    public:
-        GrGLProgram::CachedData fProgramData;
-        ProgramHashKey          fKey;
-        unsigned int            fLRUStamp;
-    };
-
-    GrTHashTable<Entry, ProgramHashKey, 8> fHashCache;
-
-    // We may have kMaxEntries+1 shaders in the GL context because
-    // we create a new shader before evicting from the cache.
-    enum {
-        kMaxEntries = 32
-    };
-    Entry                       fEntries[kMaxEntries];
-    int                         fCount;
-    unsigned int                fCurrLRUStamp;
-    const GrGLContextInfo&      fGL;
-
-public:
-    ProgramCache(const GrGLContextInfo& gl)
-        : fCount(0)
-        , fCurrLRUStamp(0)
-        , fGL(gl) {
+GrGpuGL::ProgramCache::~ProgramCache() {
+    for (int i = 0; i < fCount; ++i) {
+        GrGpuGL::DeleteProgram(fGL.interface(),
+                                        &fEntries[i].fProgramData);
     }
+}
 
-    ~ProgramCache() {
-        for (int i = 0; i < fCount; ++i) {
-            GrGpuGL::DeleteProgram(fGL.interface(),
-                                          &fEntries[i].fProgramData);
-        }
+void GrGpuGL::ProgramCache::abandon() {
+    fCount = 0;
+}
+
+void GrGpuGL::ProgramCache::invalidateViewMatrices() {
+    for (int i = 0; i < fCount; ++i) {
+        // set to illegal matrix
+        fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
     }
+}
 
-    void abandon() {
-        fCount = 0;
-    }
-
-    void invalidateViewMatrices() {
-        for (int i = 0; i < fCount; ++i) {
-            // set to illegal matrix
-            fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix();
-        }
-    }
-
-    GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc,
-                                            GrCustomStage** stages) {
-        Entry newEntry;
-        newEntry.fKey.setKeyData(desc.keyData());
+GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData(
+                                        const GrGLProgram& desc,
+                                        GrCustomStage** stages) {
+    Entry newEntry;
+    newEntry.fKey.setKeyData(desc.keyData());
         
-        Entry* entry = fHashCache.find(newEntry.fKey);
-        if (NULL == entry) {
-            if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
-                return NULL;
-            }
-            if (fCount < kMaxEntries) {
-                entry = fEntries + fCount;
-                ++fCount;
-            } else {
-                GrAssert(kMaxEntries == fCount);
-                entry = fEntries;
-                for (int i = 1; i < kMaxEntries; ++i) {
-                    if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
-                        entry = fEntries + i;
-                    }
+    Entry* entry = fHashCache.find(newEntry.fKey);
+    if (NULL == entry) {
+        if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) {
+            return NULL;
+        }
+        if (fCount < kMaxEntries) {
+            entry = fEntries + fCount;
+            ++fCount;
+        } else {
+            GrAssert(kMaxEntries == fCount);
+            entry = fEntries;
+            for (int i = 1; i < kMaxEntries; ++i) {
+                if (fEntries[i].fLRUStamp < entry->fLRUStamp) {
+                    entry = fEntries + i;
                 }
-                fHashCache.remove(entry->fKey, entry);
-                GrGpuGL::DeleteProgram(fGL.interface(),
-                                              &entry->fProgramData);
             }
-            entry->copyAndTakeOwnership(newEntry);
-            fHashCache.insert(entry->fKey, entry);
+            fHashCache.remove(entry->fKey, entry);
+            GrGpuGL::DeleteProgram(fGL.interface(),
+                                            &entry->fProgramData);
         }
-
-        entry->fLRUStamp = fCurrLRUStamp;
-        if (GR_UINT32_MAX == fCurrLRUStamp) {
-            // wrap around! just trash our LRU, one time hit.
-            for (int i = 0; i < fCount; ++i) {
-                fEntries[i].fLRUStamp = 0;
-            }
-        }
-        ++fCurrLRUStamp;
-        return &entry->fProgramData;
+        entry->copyAndTakeOwnership(newEntry);
+        fHashCache.insert(entry->fKey, entry);
     }
-};
+
+    entry->fLRUStamp = fCurrLRUStamp;
+    if (GR_UINT32_MAX == fCurrLRUStamp) {
+        // wrap around! just trash our LRU, one time hit.
+        for (int i = 0; i < fCount; ++i) {
+            fEntries[i].fLRUStamp = 0;
+        }
+    }
+    ++fCurrLRUStamp;
+    return &entry->fProgramData;
+}
 
 void GrGpuGL::DeleteProgram(const GrGLInterface* gl,
                                    CachedData* programData) {
@@ -140,17 +106,6 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-void GrGpuGL::createProgramCache() {
-    fProgramData = NULL;
-    fProgramCache = new ProgramCache(this->glContextInfo());
-}
-
-void GrGpuGL::deleteProgramCache() {
-    delete fProgramCache;
-    fProgramCache = NULL;
-    fProgramData = NULL;
-}
-
 void GrGpuGL::abandonResources(){
     INHERITED::abandonResources();
     fProgramCache->abandon();