[Bf-blender-cvs] [c009ab8e7d1] usd-importer-T81257: Restored ref counting for USDReaderPrim.

makowalski noreply at git.blender.org
Sun Dec 27 22:18:12 CET 2020


Commit: c009ab8e7d12e5f96b3f748187e5049fb413cb46
Author: makowalski
Date:   Fri Dec 4 21:55:50 2020 -0500
Branches: usd-importer-T81257
https://developer.blender.org/rBc009ab8e7d12e5f96b3f748187e5049fb413cb46

Restored ref counting for USDReaderPrim.

Added back manual ref counting for USD readers, since
we now need this for implementing constraints, and
since it doesn't appear that we will be transitioning
to shared pointers in the near future.

===================================================================

M	source/blender/io/usd/import/usd_prim_iterator.cc
M	source/blender/io/usd/import/usd_reader_prim.cc
M	source/blender/io/usd/import/usd_reader_prim.h
M	source/blender/io/usd/intern/usd_capi.cc

===================================================================

diff --git a/source/blender/io/usd/import/usd_prim_iterator.cc b/source/blender/io/usd/import/usd_prim_iterator.cc
index 18d4bc11d46..946e4c05f4e 100644
--- a/source/blender/io/usd/import/usd_prim_iterator.cc
+++ b/source/blender/io/usd/import/usd_prim_iterator.cc
@@ -204,7 +204,7 @@ void USDPrimIterator::cache_prototype_data(USDDataCache &r_cache) const
 
     /* Clean up the readers. */
     for (USDXformableReader *reader : proto_readers) {
-      delete reader;
+      reader->decref();
     }
   }
 }
diff --git a/source/blender/io/usd/import/usd_reader_prim.cc b/source/blender/io/usd/import/usd_reader_prim.cc
index bfa1630fa11..b342ed4eb4b 100644
--- a/source/blender/io/usd/import/usd_reader_prim.cc
+++ b/source/blender/io/usd/import/usd_reader_prim.cc
@@ -20,6 +20,8 @@
 #include "usd_reader_prim.h"
 #include "BLI_assert.h"
 
+#include <iostream>
+
 namespace blender::io::usd {
 
 USDPrimReader::USDPrimReader(const pxr::UsdPrim &prim, const USDImporterContext &context)
@@ -27,7 +29,8 @@ USDPrimReader::USDPrimReader(const pxr::UsdPrim &prim, const USDImporterContext
       prim_path_(""),
       context_(context),
       min_time_(std::numeric_limits<double>::max()),
-      max_time_(std::numeric_limits<double>::min())
+      max_time_(std::numeric_limits<double>::min()),
+      refcount_(1)
 {
   if (prim) {
     prim_path_ = prim.GetPath().GetString();
@@ -53,4 +56,20 @@ double USDPrimReader::max_time() const
   return max_time_;
 }
 
+int USDPrimReader::incref()
+{
+  return ++refcount_;
+}
+
+int USDPrimReader::decref()
+{
+  --refcount_;
+  BLI_assert(refcount_ >= 0);
+  if (refcount_ == 0) {
+    delete this;
+    return 0;
+  }
+  return refcount_;
+}
+
 } /* namespace blender::io::usd */
diff --git a/source/blender/io/usd/import/usd_reader_prim.h b/source/blender/io/usd/import/usd_reader_prim.h
index 0f46452895e..436e6da350f 100644
--- a/source/blender/io/usd/import/usd_reader_prim.h
+++ b/source/blender/io/usd/import/usd_reader_prim.h
@@ -42,9 +42,17 @@ class USDPrimReader {
   double min_time_;
   double max_time_;
 
+  /* Use reference counting since the same reader may be used by multiple
+   * modifiers and/or
+   * constraints. */
+  int refcount_;
+
  public:
   explicit USDPrimReader(const pxr::UsdPrim &prim, const USDImporterContext &context);
 
+  /* TODO(makowalsk): Consider making the destructor protected here and for derived
+   * classes,
+   * to force the use of incref and decref. */
   virtual ~USDPrimReader();
 
   // Disallow assignment and copying.
@@ -76,6 +84,13 @@ class USDPrimReader {
 
   double min_time() const;
   double max_time() const;
+
+  int refcount() const
+  {
+    return refcount_;
+  }
+  int incref();
+  int decref();
 };
 
 } /* namespace blender::io::usd */
diff --git a/source/blender/io/usd/intern/usd_capi.cc b/source/blender/io/usd/intern/usd_capi.cc
index 4532335b799..06d634f8d1d 100644
--- a/source/blender/io/usd/intern/usd_capi.cc
+++ b/source/blender/io/usd/intern/usd_capi.cc
@@ -406,7 +406,7 @@ static void import_endjob(void *user_data)
   /* Delete the reders. */
 
   for (iter = data->readers.begin(); iter != data->readers.end(); ++iter) {
-    delete *iter;
+    (*iter)->decref();
   }
 
   data->readers.clear();



More information about the Bf-blender-cvs mailing list