Switch GrGpu's GrResource list over to using SkTDLinkedList
authorrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 4 Sep 2012 13:34:32 +0000 (13:34 +0000)
committerrobertphillips@google.com <robertphillips@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 4 Sep 2012 13:34:32 +0000 (13:34 +0000)
https://codereview.appspot.com/6500062/

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

include/core/SkTDLinkedList.h
include/gpu/GrResource.h
src/gpu/GrGpu.cpp
src/gpu/GrGpu.h
src/gpu/GrResource.cpp

index 88bf96d..9247812 100644 (file)
@@ -48,6 +48,7 @@ public:
     }
 
     void remove(T* entry) {
+        SkASSERT(NULL != fHead && NULL != fTail);
         SkASSERT(this->isInList(entry));
 
         T* prev = entry->fPrev;
index 6b85729..5c3e24a 100644 (file)
@@ -12,6 +12,8 @@
 
 #include "GrRefCnt.h"
 
+#include "SkTDLinkedList.h"
+
 class GrGpu;
 class GrContext;
 class GrResourceEntry;
@@ -80,14 +82,17 @@ protected:
 
 private:
 
-    friend class GrGpu; // GrGpu manages list of resources.
+#if GR_DEBUG
+    friend class GrGpu; // for assert in GrGpu to access getGpu
+#endif
 
     GrGpu*      fGpu;       // not reffed. The GrGpu can be deleted while there
                             // are still live GrResources. It will call
                             // release() on all such resources in its
                             // destructor.
-    GrResource* fNext;      // dl-list of resources per-GrGpu
-    GrResource* fPrevious;
+
+    // we're a dlinklist
+    SK_DEFINE_DLINKEDLIST_INTERFACE(GrResource);
 
     GrResourceEntry* fCacheEntry;  // NULL if not in cache
 
index b6f5bb7..148d572 100644 (file)
@@ -37,8 +37,7 @@ GrGpu::GrGpu()
     , fIndexPoolUseCnt(0)
     , fQuadIndexBuffer(NULL)
     , fUnitSquareVertexBuffer(NULL)
-    , fContextIsDirty(true)
-    , fResourceHead(NULL) {
+    , fContextIsDirty(true) {
 
     fClipMaskManager.setGpu(this);
 
@@ -68,8 +67,8 @@ void GrGpu::abandonResources() {
 
     fClipMaskManager.releaseResources();
 
-    while (NULL != fResourceHead) {
-        fResourceHead->abandon();
+    while (NULL != fResourceList.head()) {
+        fResourceList.head()->abandon();
     }
 
     GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
@@ -87,8 +86,8 @@ void GrGpu::releaseResources() {
 
     fClipMaskManager.releaseResources();
 
-    while (NULL != fResourceHead) {
-        fResourceHead->release();
+    while (NULL != fResourceList.head()) {
+        fResourceList.head()->release();
     }
 
     GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
@@ -105,33 +104,15 @@ void GrGpu::releaseResources() {
 void GrGpu::insertResource(GrResource* resource) {
     GrAssert(NULL != resource);
     GrAssert(this == resource->getGpu());
-    GrAssert(NULL == resource->fNext);
-    GrAssert(NULL == resource->fPrevious);
 
-    resource->fNext = fResourceHead;
-    if (NULL != fResourceHead) {
-        GrAssert(NULL == fResourceHead->fPrevious);
-        fResourceHead->fPrevious = resource;
-    }
-    fResourceHead = resource;
+    fResourceList.addToHead(resource);
 }
 
 void GrGpu::removeResource(GrResource* resource) {
     GrAssert(NULL != resource);
-    GrAssert(NULL != fResourceHead);
+    GrAssert(this == resource->getGpu());
 
-    if (fResourceHead == resource) {
-        GrAssert(NULL == resource->fPrevious);
-        fResourceHead = resource->fNext;
-    } else {
-        GrAssert(NULL != fResourceHead);
-        resource->fPrevious->fNext = resource->fNext;
-    }
-    if (NULL != resource->fNext) {
-        resource->fNext->fPrevious = resource->fPrevious;
-    }
-    resource->fNext = NULL;
-    resource->fPrevious = NULL;
+    fResourceList.remove(resource);
 }
 
 
index fb5c1f5..60e49e7 100644 (file)
@@ -554,7 +554,8 @@ private:
 
     bool                        fContextIsDirty;
 
-    GrResource*                 fResourceHead;
+    typedef SkTDLinkedList<GrResource> ResourceList;
+    ResourceList                fResourceList;
 
     // Given a rt, find or create a stencil buffer and attach it
     bool attachStencilBufferToRenderTarget(GrRenderTarget* target);
index 74fc63d..2ff7df6 100644 (file)
@@ -14,8 +14,6 @@ SK_DEFINE_INST_COUNT(GrResource)
 
 GrResource::GrResource(GrGpu* gpu) {
     fGpu        = gpu;
-    fNext       = NULL;
-    fPrevious   = NULL;
     fCacheEntry = NULL;
     fGpu->insertResource(this);
 }