All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primvarDescCache.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
25 #define PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usdImaging/usdImaging/api.h"
31 #include "pxr/imaging/hd/sceneDelegate.h"
32 
33 #include "pxr/usd/sdf/path.h"
34 
35 #include "pxr/base/tf/token.h"
36 
37 #include <tbb/concurrent_unordered_map.h>
38 #include <tbb/concurrent_queue.h>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
47 {
48 public:
51  = delete;
52 
53  class Key
54  {
55  friend class UsdImagingPrimvarDescCache;
56  SdfPath _path;
57  TfToken _attribute;
58 
59  public:
60  Key(SdfPath const& path, TfToken const& attr)
61  : _path(path)
62  , _attribute(attr)
63  {}
64 
65  inline bool operator==(Key const& rhs) const {
66  return _path == rhs._path && _attribute == rhs._attribute;
67  }
68  inline bool operator!=(Key const& rhs) const {
69  return !(*this == rhs);
70  }
71 
72  struct Hash {
73  inline size_t operator()(Key const& key) const {
74  size_t hash = key._path.GetHash();
75  boost::hash_combine(hash, key._attribute.Hash());
76  return hash;
77  }
78  };
79 
80  private:
81  static Key Primvars(SdfPath const& path) {
82  static TfToken attr("primvars");
83  return Key(path, attr);
84  }
85  };
86 
88  : _locked(false)
89  { }
90 
91 private:
92  template <typename Element>
93  struct _TypedCache
94  {
95  typedef tbb::concurrent_unordered_map<Key, Element, Key::Hash> _MapType;
96  typedef typename _MapType::iterator _MapIt;
97  typedef typename _MapType::const_iterator _MapConstIt;
98  typedef tbb::concurrent_queue<_MapIt> _QueueType;
99 
100  _MapType _map;
101  _QueueType _deferredDeleteQueue;
102  };
103 
104 
107  template <typename T>
108  bool _Find(Key const& key, T* value) const {
109  typedef _TypedCache<T> Cache_t;
110 
111  Cache_t *cache = nullptr;
112 
113  _GetCache(&cache);
114  typename Cache_t::_MapConstIt it = cache->_map.find(key);
115  if (it == cache->_map.end()) {
116  return false;
117  }
118  *value = it->second;
119  return true;
120  }
121 
129  template <typename T>
130  bool _Extract(Key const& key, T* value) {
131  if (!TF_VERIFY(!_locked)) {
132  return false;
133  }
134 
135  typedef _TypedCache<T> Cache_t;
136  Cache_t *cache = nullptr;
137 
138  _GetCache(&cache);
139  typename Cache_t::_MapIt it = cache->_map.find(key);
140 
141  if (it == cache->_map.end()) {
142  return false;
143  }
144 
145  // If we're going to erase the old value, swap to avoid a copy.
146  std::swap(it->second, *value);
147  cache->_deferredDeleteQueue.push(it);
148  return true;
149  }
150 
153  template <typename T>
154  void _Erase(Key const& key) {
155  if (!TF_VERIFY(!_locked)) {
156  return;
157  }
158 
159  typedef _TypedCache<T> Cache_t;
160 
161  Cache_t *cache = nullptr;
162  _GetCache(&cache);
163  cache->_map.unsafe_erase(key);
164  }
165 
169  template <typename T>
170  T& _Get(Key const& key) const {
171  typedef _TypedCache<T> Cache_t;
172 
173  Cache_t *cache = nullptr;
174  _GetCache(&cache);
175 
176  // With concurrent_unordered_map, multi-threaded insertion is safe.
177  std::pair<typename Cache_t::_MapIt, bool> res =
178  cache->_map.insert(std::make_pair(key, T()));
179 
180  return res.first->second;
181  }
182 
186  template <typename T>
187  void _GarbageCollect(_TypedCache<T> &cache) {
188  typedef _TypedCache<T> Cache_t;
189 
190  typename Cache_t::_MapIt it;
191 
192  while (cache._deferredDeleteQueue.try_pop(it)) {
193  cache._map.unsafe_erase(it);
194  }
195  }
196 
197 public:
198 
199  void EnableMutation() { _locked = false; }
200  void DisableMutation() { _locked = true; }
201 
203  void Clear(SdfPath const& path) {
204  _Erase<HdPrimvarDescriptorVector>(Key::Primvars(path));
205  }
206 
207  HdPrimvarDescriptorVector& GetPrimvars(SdfPath const& path) const {
208  return _Get<HdPrimvarDescriptorVector>(Key::Primvars(path));
209  }
210 
211  bool FindPrimvars(SdfPath const& path, HdPrimvarDescriptorVector* value) const {
212  return _Find(Key::Primvars(path), value);
213  }
214 
215  bool ExtractPrimvars(SdfPath const& path, HdPrimvarDescriptorVector* value) {
216  return _Extract(Key::Primvars(path), value);
217  }
218 
221  {
222  _GarbageCollect(_pviCache);
223  }
224 
225 private:
226  bool _locked;
227 
228  typedef _TypedCache<HdPrimvarDescriptorVector> _PviCache;
229  mutable _PviCache _pviCache;
230 
231  void _GetCache(_PviCache **cache) const {
232  *cache = &_pviCache;
233  }
234 };
235 
236 
237 PXR_NAMESPACE_CLOSE_SCOPE
238 
239 #endif // PXR_USD_IMAGING_USD_IMAGING_PRIMVARDESC_CACHE_H
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
void GarbageCollect()
Remove any items from the cache that are marked for defered deletion.
void Clear(SdfPath const &path)
Clear all data associated with a specific path.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:288
A cache for primvar descriptors.
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...