Templatize GrNonAtomicRef
authorcdalton <cdalton@nvidia.com>
Wed, 3 Feb 2016 06:46:16 +0000 (22:46 -0800)
committerCommit bot <commit-bot@chromium.org>
Wed, 3 Feb 2016 06:46:16 +0000 (22:46 -0800)
Templatizes GrNonAtomicRef so it does not necessarily require a virtual
destructor. This also gives us the flexibility to specialize how
different types get deleted.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1664613002

Review URL: https://codereview.chromium.org/1664613002

src/gpu/GrNonAtomicRef.h
src/gpu/GrPipeline.h
src/gpu/batches/GrBatch.h

index efa2881..e1503bc 100644 (file)
  * A simple non-atomic ref used in the GrBackend when we don't want to pay for the overhead of a
  * threadsafe ref counted object
  */
-class GrNonAtomicRef : public SkNoncopyable {
+template<typename TSubclass> class GrNonAtomicRef : public SkNoncopyable {
 public:
     GrNonAtomicRef() : fRefCnt(1) {}
-    virtual ~GrNonAtomicRef() {
+
+#ifdef SK_DEBUG
+    ~GrNonAtomicRef() {
         // fRefCnt can be one when a subclass is created statically
         SkASSERT((0 == fRefCnt || 1 == fRefCnt));
         // Set to invalid values.
-        SkDEBUGCODE(fRefCnt = -10;)
+        fRefCnt = -10;
     }
+#endif
 
     void ref() const {
         // Once the ref cnt reaches zero it should never be ref'ed again.
@@ -35,7 +38,7 @@ public:
         SkASSERT(fRefCnt > 0);
         --fRefCnt;
         if (0 == fRefCnt) {
-            delete this;
+            GrTDeleteNonAtomicRef(static_cast<const TSubclass*>(this));
             return;
         }
     }
@@ -46,4 +49,8 @@ private:
     typedef SkNoncopyable INHERITED;
 };
 
+template<typename T> inline void GrTDeleteNonAtomicRef(const T* ref) {
+    delete ref;
+}
+
 #endif
index 10ffc86..4f81ae4 100644 (file)
@@ -41,7 +41,7 @@ struct GrPipelineOptimizations {
  * Class that holds an optimized version of a GrPipelineBuilder. It is meant to be an immutable
  * class, and contains all data needed to set the state for a gpu draw.
  */
-class GrPipeline : public GrNonAtomicRef {
+class GrPipeline : public GrNonAtomicRef<GrPipeline> {
 public:
     ///////////////////////////////////////////////////////////////////////////
     /// @name Creation
index a66d6ae..b29c8a4 100644 (file)
@@ -53,10 +53,10 @@ class GrRenderTarget;
         return kClassID; \
     }
 
-class GrBatch : public GrNonAtomicRef {
+class GrBatch : public GrNonAtomicRef<GrBatch> {
 public:
     GrBatch(uint32_t classID);
-    ~GrBatch() override;
+    virtual ~GrBatch();
 
     virtual const char* name() const = 0;
 
@@ -159,7 +159,6 @@ private:
     static int32_t                      gCurrBatchUniqueID;
 #endif
     static int32_t                      gCurrBatchClassID;
-    typedef GrNonAtomicRef INHERITED;
 };
 
 #endif