Panda3D
materialPool.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file materialPool.cxx
10  * @author drose
11  * @date 2001-04-30
12  */
13 
14 #include "materialPool.h"
15 #include "config_gobj.h"
16 #include "lightMutexHolder.h"
17 
18 MaterialPool *MaterialPool::_global_ptr = nullptr;
19 
20 
21 /**
22  * Lists the contents of the material pool to the indicated output stream.
23  */
25 write(std::ostream &out) {
26  get_global_ptr()->ns_list_contents(out);
27 }
28 
29 /**
30  * The nonstatic implementation of get_material().
31  */
32 Material *MaterialPool::
33 ns_get_material(Material *temp) {
34  LightMutexHolder holder(_lock);
35 
36  CPT(Material) cpttemp = temp;
37  Materials::iterator mi = _materials.find(cpttemp);
38  if (mi == _materials.end()) {
39  mi = _materials.insert(Materials::value_type(new Material(*temp), temp)).first;
40  } else {
41  if (*(*mi).first != *(*mi).second) {
42  // The pointer no longer matches its original value. Save a new one.
43  (*mi).second = temp;
44  }
45  }
46  return (*mi).second;
47 }
48 
49 /**
50  * The nonstatic implementation of release_material().
51  */
52 void MaterialPool::
53 ns_release_material(Material *temp) {
54  LightMutexHolder holder(_lock);
55 
56  CPT(Material) cpttemp = temp;
57  _materials.erase(cpttemp);
58 }
59 
60 /**
61  * The nonstatic implementation of release_all_materials().
62  */
63 void MaterialPool::
64 ns_release_all_materials() {
65  LightMutexHolder holder(_lock);
66 
67  _materials.clear();
68 }
69 
70 /**
71  * The nonstatic implementation of garbage_collect().
72  */
73 int MaterialPool::
74 ns_garbage_collect() {
75  LightMutexHolder holder(_lock);
76 
77  int num_released = 0;
78  Materials new_set;
79 
80  Materials::iterator mi;
81  for (mi = _materials.begin(); mi != _materials.end(); ++mi) {
82  const Material *mat1 = (*mi).first;
83  Material *mat2 = (*mi).second;
84  if ((*mat1) != (*mat2) || mat2->get_ref_count() == 1) {
85  if (gobj_cat.is_debug()) {
86  gobj_cat.debug()
87  << "Releasing " << *mat1 << "\n";
88  }
89  ++num_released;
90  } else {
91  new_set.insert(new_set.end(), *mi);
92  }
93  }
94 
95  _materials.swap(new_set);
96  return num_released;
97 }
98 
99 /**
100  * The nonstatic implementation of list_contents().
101  */
102 void MaterialPool::
103 ns_list_contents(std::ostream &out) const {
104  LightMutexHolder holder(_lock);
105 
106  out << _materials.size() << " materials:\n";
107  Materials::const_iterator mi;
108  for (mi = _materials.begin(); mi != _materials.end(); ++mi) {
109  const Material *mat1 = (*mi).first;
110  Material *mat2 = (*mi).second;
111  out << " " << *mat1
112  << " (count = " << mat2->get_ref_count() << ")\n";
113  }
114 }
115 
116 /**
117  * Initializes and/or returns the global pointer to the one MaterialPool
118  * object in the system.
119  */
120 MaterialPool *MaterialPool::
121 get_global_ptr() {
122  if (_global_ptr == nullptr) {
123  _global_ptr = new MaterialPool;
124  }
125  return _global_ptr;
126 }
Similar to MutexHolder, but for a light mutex.
The MaterialPool (there is only one in the universe) serves to unify different pointers to the same M...
Definition: materialPool.h:36
static void write(std::ostream &out)
Lists the contents of the material pool to the indicated output stream.
Defines the way an object appears in the presence of lighting.
Definition: material.h:43
get_ref_count
Returns the current reference count.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.