Browse Source

pgraph: Add RenderState::get_unused_states() by analogy with TransformState

rdb 4 years ago
parent
commit
bcbef3f6c8

+ 1 - 0
panda/src/pgraph/renderState.h

@@ -145,6 +145,7 @@ PUBLISHED:
   static void list_states(std::ostream &out);
   static void list_states(std::ostream &out);
   static bool validate_states();
   static bool validate_states();
   EXTENSION(static PyObject *get_states());
   EXTENSION(static PyObject *get_states());
+  EXTENSION(static PyObject *get_unused_states());
 
 
 PUBLISHED:
 PUBLISHED:
   // These methods are intended for use by low-level code, but they're also
   // These methods are intended for use by low-level code, but they're also

+ 26 - 0
panda/src/pgraph/renderState_ext.cxx

@@ -142,6 +142,32 @@ get_states() {
   return list;
   return list;
 }
 }
 
 
+/**
+ * Returns a list of all of the "unused" RenderState objects in the state
+ * cache.  See get_num_unused_states().
+ */
+PyObject *Extension<RenderState>::
+get_unused_states() {
+  extern struct Dtool_PyTypedObject Dtool_RenderState;
+  if (RenderState::_states == nullptr) {
+    return PyList_New(0);
+  }
+  LightReMutexHolder holder(*RenderState::_states_lock);
 
 
+  PyObject *list = PyList_New(0);
+  size_t size = RenderState::_states->get_num_entries();
+  for (size_t si = 0; si < size; ++si) {
+    const RenderState *state = RenderState::_states->get_key(si);
+    if (state->get_cache_ref_count() == state->get_ref_count()) {
+      state->ref();
+      PyObject *a =
+        DTool_CreatePyInstanceTyped((void *)state, Dtool_RenderState,
+                                    true, true, state->get_type_index());
+      PyList_Append(list, a);
+      Py_DECREF(a);
+    }
+  }
+  return list;
+}
 
 
 #endif  // HAVE_PYTHON
 #endif  // HAVE_PYTHON

+ 1 - 0
panda/src/pgraph/renderState_ext.h

@@ -32,6 +32,7 @@ public:
   PyObject *get_composition_cache() const;
   PyObject *get_composition_cache() const;
   PyObject *get_invert_composition_cache() const;
   PyObject *get_invert_composition_cache() const;
   static PyObject *get_states();
   static PyObject *get_states();
+  static PyObject *get_unused_states();
 };
 };
 
 
 #endif  // HAVE_PYTHON
 #endif  // HAVE_PYTHON