All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dependencies.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 PCP_DEPENDENCIES_H
25 #define PCP_DEPENDENCIES_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/pcp/node.h"
31 #include "pxr/usd/pcp/types.h"
32 #include "pxr/usd/pcp/layerStack.h"
33 #include "pxr/usd/pcp/layerStackRegistry.h"
34 #include "pxr/usd/pcp/primIndex.h"
35 #include "pxr/usd/pcp/cache.h"
36 #include "pxr/usd/sdf/layer.h"
37 #include "pxr/usd/sdf/path.h"
38 #include "pxr/usd/sdf/site.h"
39 
40 #include <boost/noncopyable.hpp>
41 
42 #include <iosfwd>
43 #include <set>
44 #include <unordered_map>
45 
46 PXR_NAMESPACE_OPEN_SCOPE
47 
48 class PcpLifeboat;
49 class PcpPrimIndexDependencies;
50 
52 
58 class Pcp_Dependencies : boost::noncopyable {
59 public:
61  Pcp_Dependencies();
62  ~Pcp_Dependencies();
63 
66 
74  void Add(const PcpPrimIndex &primIndex,
75  PcpDynamicFileFormatDependencyData &&fileFormatDependencyData);
76 
83  void Remove(const PcpPrimIndex &primIndex, PcpLifeboat *lifeboat);
84 
87  void RemoveAll(PcpLifeboat* lifeboat);
88 
92 
107  template <typename FN>
108  void
109  ForEachDependencyOnSite( const PcpLayerStackPtr &siteLayerStack,
110  const SdfPath &sitePath,
111  bool includeAncestral,
112  bool recurseBelowSite,
113  const FN &fn ) const
114  {
115  _LayerStackDepMap::const_iterator i = _deps.find(siteLayerStack);
116  if (i == _deps.end()) {
117  return;
118  }
119  const _SiteDepMap & siteDepMap = i->second;
120  if (recurseBelowSite) {
121  auto range = siteDepMap.FindSubtreeRange(sitePath);
122  for (auto iter = range.first; iter != range.second; ++iter) {
123  for(const SdfPath &primIndexPath: iter->second) {
124  fn(primIndexPath, iter->first);
125  }
126  }
127  } else {
128  _SiteDepMap::const_iterator j = siteDepMap.find(sitePath);
129  if (j != siteDepMap.end()) {
130  for(const SdfPath &primIndexPath: j->second) {
131  fn(primIndexPath, sitePath);
132  }
133  }
134  }
135  if (includeAncestral) {
136  for (SdfPath ancestorSitePath = sitePath.GetParentPath();
137  !ancestorSitePath.IsEmpty();
138  ancestorSitePath = ancestorSitePath.GetParentPath())
139  {
140  _SiteDepMap::const_iterator j =
141  siteDepMap.find(ancestorSitePath);
142  if (j != siteDepMap.end()) {
143  for(const SdfPath &ancestorPrimIndexPath: j->second) {
144  fn(ancestorPrimIndexPath, ancestorSitePath);
145  }
146  }
147  }
148  }
149  }
150 
153  SdfLayerHandleSet GetUsedLayers() const;
154 
157  SdfLayerHandleSet GetUsedRootLayers() const;
158 
161  bool UsesLayerStack(const PcpLayerStackPtr& layerStack) const;
162 
165  bool HasAnyDynamicFileFormatArgumentDependencies() const;
166 
170  bool IsPossibleDynamicFileFormatArgumentField(
171  const TfToken &field) const;
172 
178  GetDynamicFileFormatArgumentDependencyData(
179  const SdfPath &primIndexPath) const;
180 
182 
183 private:
184  // Map of site paths to dependencies, as cache paths. Stores cache
185  // paths as an unordered vector: for our datasets this is both more
186  // compact and faster than std::set.
187  using _SiteDepMap = SdfPathTable<SdfPathVector>;
188 
189  // Map of layer stacks to dependencies on that layerStack.
190  // Retains references to those layer stacks, which in turn
191  // retain references to their constituent layers.
192  using _LayerStackDepMap =
193  std::unordered_map<PcpLayerStackRefPtr, _SiteDepMap, TfHash>;
194  _LayerStackDepMap _deps;
195 
196  // Map of prim index paths to the dynamic file format dependency info for
197  // the prim index.
198  using _FileFormatArgumentDependencyMap = std::unordered_map<
200  _FileFormatArgumentDependencyMap _fileFormatArgumentDependencyMap;
201 
202  // Map of field name to the number of cached prim indices that depend on
203  // the field for dynamic file format arguments. This for quick lookup of
204  // possible file format argument relevant field changes.
205  using _FileFormatArgumentFieldDepMap =
206  std::unordered_map<TfToken, int, TfToken::HashFunctor>;
207  _FileFormatArgumentFieldDepMap _possibleDynamicFileFormatArgumentFields;
208 
209 };
210 
211 template <typename FN>
212 static void
213 Pcp_ForEachDependentNode( const SdfPath &sitePath,
214  const SdfLayerHandle &layer,
215  const SdfPath &depIndexPath,
216  const PcpCache &cache,
217  const FN &fn )
218 {
219  PcpNodeRef nodeUsingSite;
220 
221  // Walk up as needed to find a containing prim index.
222  SdfPath indexPath;
223  const PcpPrimIndex *primIndex = nullptr;
224  for (indexPath = depIndexPath.GetAbsoluteRootOrPrimPath();
225  indexPath != SdfPath();
226  indexPath = indexPath.GetParentPath())
227  {
228  primIndex = cache.FindPrimIndex(indexPath);
229  if (primIndex) {
230  break;
231  }
232  }
233  if (primIndex) {
234  // Find which node corresponds to (layer, oldPath).
235  for (const PcpNodeRef &node: primIndex->GetNodeRange()) {
236  if (PcpNodeIntroducesDependency(node) &&
237  node.GetLayerStack()->HasLayer(layer) &&
238  sitePath.HasPrefix(node.GetPath()))
239  {
240  nodeUsingSite = node;
241  fn(depIndexPath, nodeUsingSite);
242  }
243  }
244  }
245 
246  TF_VERIFY(
247  nodeUsingSite,
248  "Unable to find node that introduced dependency on site "
249  "<%s>@%s@ for prim <%s>",
250  sitePath.GetText(),
251  layer->GetIdentifier().c_str(),
252  depIndexPath.GetText());
253 }
254 
255 template <typename FN>
256 static void
257 Pcp_ForEachDependentNode( const SdfPath &sitePath,
258  const PcpLayerStackPtr &layerStack,
259  const SdfPath &depIndexPath,
260  const PcpCache &cache,
261  const FN &fn )
262 {
263  PcpNodeRef nodeUsingSite;
264 
265  // Walk up as needed to find a containing prim index.
266  SdfPath indexPath;
267  const PcpPrimIndex *primIndex = nullptr;
268  for (indexPath = depIndexPath.GetAbsoluteRootOrPrimPath();
269  indexPath != SdfPath();
270  indexPath = indexPath.GetParentPath())
271  {
272  primIndex = cache.FindPrimIndex(indexPath);
273  if (primIndex) {
274  break;
275  }
276  }
277  if (primIndex) {
278  // Find which node corresponds to (layerStack, oldPath).
279  for (const PcpNodeRef &node: primIndex->GetNodeRange()) {
280  if (PcpNodeIntroducesDependency(node) &&
281  node.GetLayerStack() == layerStack &&
282  sitePath.HasPrefix(node.GetPath()))
283  {
284  nodeUsingSite = node;
285  fn(depIndexPath, nodeUsingSite);
286  }
287  }
288  }
289 
290  TF_VERIFY(
291  nodeUsingSite,
292  "Unable to find node that introduced dependency on site "
293  "<%s>%s for prim <%s> in %s",
294  sitePath.GetText(),
295  TfStringify(layerStack->GetIdentifier()).c_str(),
296  depIndexPath.GetText(),
297  TfStringify(cache.GetLayerStack()->GetIdentifier()).c_str()
298  );
299 }
300 
301 PXR_NAMESPACE_CLOSE_SCOPE
302 
303 #endif // PCP_DEPENDENCIES_H
PCP_API const PcpPrimIndex * FindPrimIndex(const SdfPath &primPath) const
Returns a pointer to the cached computed prim index for the given path, or NULL if it has not been co...
PcpPrimIndex is an index of the all sites of scene description that contribute opinions to a specific...
Definition: primIndex.h:77
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:62
Represents a stack of layers that contribute opinions to composition.
Definition: layerStack.h:65
PcpNode represents a node in an expression tree for compositing scene description.
Definition: node.h:65
PCP_API PcpLayerStackPtr GetLayerStack() const
Get the layer stack for GetLayerStackIdentifier().
SDF_API SdfPath GetParentPath() const
Return the path that identifies this path&#39;s namespace parent.
PcpCache is the context required to make requests of the Pcp composition algorithm and cache the resu...
Definition: cache.h:91
SDF_API SdfPath GetAbsoluteRootOrPrimPath() const
Creates a path by stripping all properties and relational attributes from this path, leaving the path to the containing prim.
PCP_API PcpNodeRange GetNodeRange(PcpRangeType rangeType=PcpRangeTypeAll) const
Returns range of iterators that encompass all children of the root node with the given arc type as we...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:289
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
SDF_API bool HasPrefix(const SdfPath &prefix) const
Return true if both this path and prefix are not the empty path and this path has prefix as a prefix...
A mapping from SdfPath to MappedType, somewhat similar to map&lt;SdfPath, MappedType&gt; and TfHashMap&lt;SdfP...
Definition: pathTable.h:84
std::enable_if<!std::is_enum< T >::value, std::string >::type TfStringify(const T &v)
Convert an arbitrary type into a string.
Definition: stringUtils.h:523
Structure used to temporarily retain layers and layerStacks within a code block.
Definition: changes.h:139
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:409
Contains the necessary information for storing a prim index&#39;s dependency on dynamic file format argum...
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.