Keep program cache consistent when program creation fails.
authorbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 11 May 2011 16:52:59 +0000 (16:52 +0000)
committerbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 11 May 2011 16:52:59 +0000 (16:52 +0000)
Review URL: http://codereview.appspot.com/4523056/

git-svn-id: http://skia.googlecode.com/svn/trunk@1298 2bbb7eff-a529-9590-31e7-b0007b416f81

gpu/src/GrGLProgram.h
gpu/src/GrGpuGLShaders.cpp

index 3b926a78c4504266adc55dd5e24b6a958546572d..993fbe82396ab25e7f0ee5da53466c7a09c44d99 100644 (file)
@@ -188,7 +188,7 @@ public:
         }
 
         void copyAndTakeOwnership(CachedData& other) {
-            memcpy(this, &other, sizeof(this));
+            memcpy(this, &other, sizeof(*this));
             other.fEffectUniLocationsExtended = NULL; // ownership transfer
             GR_DEBUGCODE(other.fEffectUniCount = 0;)
         }
index 2487563122973e6cabe6e1b0fbaf624df8cb2383..be030f1e6d2a123a7e8f2cc32dfc6a2550761ce8 100644 (file)
@@ -42,7 +42,6 @@ private:
     class Entry : public ::GrNoncopyable {
     public:
         Entry() {}
-    private:
         void copyAndTakeOwnership(Entry& entry) {
             fProgramData.copyAndTakeOwnership(entry.fProgramData);
             fKey.copyAndTakeOwnership(entry.fKey); // ownership transfer
@@ -60,6 +59,8 @@ private:
 
     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
     };
@@ -91,12 +92,15 @@ public:
     }
 
     GrGLProgram::CachedData* getProgramData(const GrGLProgram& desc) {
-        ProgramHashKey key;
-        while (key.doPass()) {
-            desc.buildKey(key);
+        Entry newEntry;
+        while (newEntry.fKey.doPass()) {
+            desc.buildKey(newEntry.fKey);
         }
-        Entry* entry = fHashCache.find(key);
+        Entry* entry = fHashCache.find(newEntry.fKey);
         if (NULL == entry) {
+            if (!desc.genProgram(&newEntry.fProgramData)) {
+                return NULL;
+            }
             if (fCount < kMaxEntries) {
                 entry = fEntries + fCount;
                 ++fCount;
@@ -111,10 +115,7 @@ public:
                 fHashCache.remove(entry->fKey, entry);
                 GrGpuGLShaders::DeleteProgram(&entry->fProgramData);
             }
-            entry->fKey.copyAndTakeOwnership(key);
-            if (!desc.genProgram(&entry->fProgramData)) {
-                return NULL;
-            }
+            entry->copyAndTakeOwnership(newEntry);
             fHashCache.insert(entry->fKey, entry);
         }