HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cache.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_PCP_CACHE_H
25 #define PXR_USD_PCP_CACHE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/pcp/api.h"
29 #include "pxr/usd/pcp/dependency.h"
30 #include "pxr/usd/pcp/errors.h"
32 #include "pxr/usd/pcp/primIndex.h"
35 #include "pxr/usd/sdf/path.h"
36 #include "pxr/usd/sdf/pathTable.h"
37 
38 #include "pxr/usd/ar/ar.h"
42 #include "pxr/base/tf/hashset.h"
43 
44 #include <memory>
45 #include <string>
46 #include <unordered_set>
47 #include <vector>
48 
50 
51 // Forward declarations:
52 class PcpChanges;
53 class PcpCacheChanges;
54 class Pcp_Dependencies;
56 class PcpLifeboat;
57 class PcpNodeRef;
58 class PcpMapFunction;
59 
61 TF_DECLARE_WEAK_AND_REF_PTRS(Pcp_LayerStackRegistry);
63 
64 /// \class PcpCache
65 ///
66 /// PcpCache is the context required to make requests of the Pcp
67 /// composition algorithm and cache the results.
68 ///
69 /// Because the algorithms are recursive -- making a request typically
70 /// makes other internal requests to solve subproblems -- caching
71 /// subproblem results is required for reasonable performance, and
72 /// so this cache is the only entrypoint to the algorithms.
73 ///
74 /// There is a set of parameters that affect the composition results:
75 ///
76 /// \li variant fallbacks: per named variant set, an ordered list of
77 /// fallback values to use when composing a prim that defines
78 /// a variant set but does not specify a selection
79 /// \li payload inclusion set: an SdfPath set used to identify which
80 /// prims should have their payloads included during composition;
81 /// this is the basis for explicit control over the "working set"
82 /// of composition
83 /// \li file format target: the file format target that Pcp will request
84 /// when opening scene description layers
85 /// \li "USD mode" configures the Pcp composition algorithm to provide
86 /// only a custom, lighter subset of the full feature set, as needed
87 /// by the Universal Scene Description system
88 ///
89 /// There are a number of different computations that can be requested.
90 /// These include computing a layer stack from a PcpLayerStackIdentifier,
91 /// computing a prim index or prim stack, and computing a property index.
92 ///
93 class PcpCache
94 {
95  PcpCache(PcpCache const &) = delete;
96  PcpCache &operator=(PcpCache const &) = delete;
97 public:
98  /// Construct a PcpCache to compose results for the layer stack identified
99  /// by \a layerStackIdentifier.
100  ///
101  /// If \p fileFormatTarget is given, Pcp will specify \p fileFormatTarget
102  /// as the file format target when searching for or opening a layer.
103  ///
104  /// If \p usd is true, computation of prim indices and composition of prim
105  /// child names are performed without relocates, inherits, permissions,
106  /// symmetry, or payloads, and without populating the prim stack and
107  /// gathering its dependencies.
108  PCP_API
109  PcpCache(const PcpLayerStackIdentifier & layerStackIdentifier,
110  const std::string& fileFormatTarget = std::string(),
111  bool usd = false);
112  PCP_API ~PcpCache();
113 
114  /// \name Parameters
115  /// @{
116 
117  /// Get the identifier of the layerStack used for composition.
118  PCP_API
120 
121  /// Get the layer stack for GetLayerStackIdentifier(). Note that
122  /// this will neither compute the layer stack nor report errors.
123  /// So if the layer stack has not been computed yet this will
124  /// return \c NULL. Use ComputeLayerStack() if you need to
125  /// compute the layer stack if it hasn't been computed already
126  /// and/or get errors caused by computing the layer stack.
127  PCP_API
128  PcpLayerStackPtr GetLayerStack() const;
129 
130  /// Return true if this cache's root layer stack is \p layerStack, false
131  /// otherwise. This is functionally equivalent to comparing against the
132  /// result of GetLayerStack(), but does not require constructing a TfWeakPtr
133  /// or any refcount operations.
134  bool HasRootLayerStack(PcpLayerStackRefPtr const &layerStack) const {
135  return layerStack == _layerStack;
136  }
137 
138  /// \overload
139  PCP_API
140  bool HasRootLayerStack(PcpLayerStackPtr const &layerStack) const;
141 
142  /// Return true if the cache is configured in Usd mode.
143  PCP_API
144  bool IsUsd() const;
145 
146  /// Returns the file format target this cache is configured for.
147  PCP_API
148  const std::string& GetFileFormatTarget() const;
149 
150  /// Get the list of fallbacks to attempt to use when evaluating
151  /// variant sets that lack an authored selection.
152  PCP_API
154 
155  /// Set the list of fallbacks to attempt to use when evaluating
156  /// variant sets that lack an authored selection.
157  ///
158  /// If \p changes is not \c NULL then it's adjusted to reflect the
159  /// changes necessary to see the change in standin preferences,
160  /// otherwise those changes are applied immediately.
161  PCP_API
162  void SetVariantFallbacks( const PcpVariantFallbackMap & map,
163  PcpChanges* changes = NULL );
164 
165  /// Return true if the payload is included for the given path.
166  PCP_API
167  bool IsPayloadIncluded(const SdfPath &path) const;
168 
169  /// Returns the payloads requested for inclusion.
170  using PayloadSet = std::unordered_set<SdfPath, SdfPath::Hash>;
171  PCP_API
172  PayloadSet const &GetIncludedPayloads() const;
173 
174  /// Request payloads to be included or excluded from composition.
175  /// \param pathsToInclude is a set of paths to add to the set for
176  /// payload inclusion.
177  /// \param pathsToExclude is a set of paths to remove from the set for
178  /// payload inclusion.
179  /// \param changes if not \c NULL, is adjusted to reflect the changes
180  /// necessary to see the change in payloads; otherwise those
181  /// changes are applied immediately.
182  /// \note If a path is listed in both pathsToInclude and pathsToExclude,
183  /// it will be treated as an inclusion only.
184  ///
185  PCP_API
186  void RequestPayloads( const SdfPathSet & pathsToInclude,
187  const SdfPathSet & pathsToExclude,
188  PcpChanges* changes = NULL );
189 
190  /// Request layers to be muted or unmuted in this cache. Muted layers
191  /// are ignored during composition and do not appear in any layer
192  /// stacks. The root layer of this stage may not be muted; attempting
193  /// to do so will generate a coding error. If the root layer of a
194  /// reference or payload layer stack is muted, the behavior is as if
195  /// the muted layer did not exist, which means a composition error will
196  /// be generated.
197  ///
198  /// A canonical identifier for each layer in \p layersToMute will be
199  /// computed using ArResolver::CreateIdentifier using the cache's root
200  /// layer as the anchoring asset. If an identifier contains a file
201  /// format target that matches this cache's file format target, that
202  /// argument will be removed from the identifier. Any layer encountered
203  /// during composition with the same canonical identifier will be
204  /// considered muted and ignored.
205  ///
206  /// Note that muting a layer will cause this cache to release all
207  /// references to that layer. If no other client is holding on to
208  /// references to that layer, it will be unloaded. In this case, if
209  /// there are unsaved edits to the muted layer, those edits are lost.
210  /// Since anonymous layers are not serialized, muting an anonymous
211  /// layer will cause that layer and its contents to be lost in this
212  /// case.
213  ///
214  /// If \p changes is not \c nullptr, it is adjusted to reflect the
215  /// changes necessary to see the change in muted layers. Otherwise,
216  /// those changes are applied immediately.
217  ///
218  /// \p newLayersMuted and \p newLayersUnmuted contains the pruned vector
219  /// of layers which are muted or unmuted by this call to RequestLayerMuting.
220  ///
221  PCP_API
222  void RequestLayerMuting(const std::vector<std::string>& layersToMute,
223  const std::vector<std::string>& layersToUnmute,
224  PcpChanges* changes = nullptr,
225  std::vector<std::string>* newLayersMuted = nullptr,
226  std::vector<std::string>* newLayersUnmuted =
227  nullptr);
228 
229  /// Returns the list of canonical identifiers for muted layers
230  /// in this cache. See documentation on RequestLayerMuting for
231  /// more details.
232  PCP_API
233  const std::vector<std::string>& GetMutedLayers() const;
234 
235  /// Returns true if the layer specified by \p layerIdentifier is
236  /// muted in this cache, false otherwise. If \p layerIdentifier
237  /// is relative, it is assumed to be relative to this cache's
238  /// root layer. See documentation on RequestLayerMuting for
239  /// more details.
240  PCP_API
241  bool IsLayerMuted(const std::string& layerIdentifier) const;
242 
243  /// Returns true if the layer specified by \p layerIdentifier is
244  /// muted in this cache, false otherwise. If \p layerIdentifier
245  /// is relative, it is assumed to be relative to \p anchorLayer.
246  /// If \p canonicalMutedLayerIdentifier is supplied, it will be
247  /// populated with the canonical identifier of the muted layer if this
248  /// function returns true. See documentation on RequestLayerMuting
249  /// for more details.
250  PCP_API
251  bool IsLayerMuted(const SdfLayerHandle& anchorLayer,
252  const std::string& layerIdentifier,
253  std::string* canonicalMutedLayerIdentifier
254  = nullptr) const;
255 
256  /// Returns parameter object containing all inputs for the prim index
257  /// computation used by this cache.
258  PCP_API
260 
261  /// @}
262 
263  /// \name Computations
264  /// @{
265 
266  /// Returns the layer stack for \p identifier if it exists, otherwise
267  /// creates a new layer stack for \p identifier. This returns \c NULL
268  /// if \p identifier is invalid (i.e. its root layer is \c NULL).
269  /// \p allErrors will contain any errors encountered while creating a
270  /// new layer stack. It'll be unchanged if the layer stack already existed.
271  PCP_API
272  PcpLayerStackRefPtr
273  ComputeLayerStack(const PcpLayerStackIdentifier &identifier,
274  PcpErrorVector *allErrors);
275 
276  /// Returns the layer stack for \p identifier if it has been computed
277  /// and cached, otherwise returns \c NULL.
278  PCP_API
279  PcpLayerStackPtr
280  FindLayerStack(const PcpLayerStackIdentifier &identifier) const;
281 
282  /// Return true if \p layerStack is used by this cache in its composition,
283  /// false otherwise.
284  PCP_API
285  bool UsesLayerStack(const PcpLayerStackPtr &layerStack) const;
286 
287  /// Compute and return a reference to the cached result for the
288  /// prim index for the given path. \p allErrors will contain any errors
289  /// encountered while performing this operation.
290  PCP_API
291  const PcpPrimIndex &
292  ComputePrimIndex(const SdfPath &primPath, PcpErrorVector *allErrors);
293 
294  /// Compute PcpPrimIndexes in the subtree rooted at path in parallel,
295  /// recursing to children based on the supplied \p childrenPred. Also
296  /// include payloads not already in this cache's included payloads (see
297  /// GetIncludedPayloads()) according to \p payloadPred.
298  ///
299  /// This is similar to ComputePrimIndex(), except it computes an entire
300  /// subtree of indexes in parallel so it can be much more efficient. This
301  /// function invokes both \p childrenPred and \p payloadPred concurrently,
302  /// so it must be safe to do so.
303  ///
304  /// When a PcpPrimIndex computation completes invoke \p childrenPred,
305  /// passing it the PcpPrimIndex. If \p childrenPred returns true, continue
306  /// indexing children prim indexes. In this case, \p childrenPred may
307  /// provide a list of names of the children prim indexes to compute.
308  /// If it does not, all children prim indexes will be computed.
309  /// If \p childrenPred returns false, stop indexing in that subtree.
310  ///
311  /// If payloads discovered during indexing do not already appear in this
312  /// cache's set of included payloads, invoke \p payloadPred, passing it the
313  /// path for the prim with the payload. If \p payloadPred returns true,
314  /// include its payload and add it to the cache's set of included payloads
315  /// upon completion.
316  template <class ChildrenPredicate, class PayloadPredicate>
318  PcpErrorVector *allErrors,
319  const ChildrenPredicate &childrenPred,
320  const PayloadPredicate &payloadPred) {
321  ComputePrimIndexesInParallel(SdfPathVector(1, path), allErrors,
322  childrenPred, payloadPred,
323  "Pcp", "ComputePrimIndexesInParallel");
324  }
325 
326  /// \overload
327  /// XXX Do not add new callers of this method. It is needed as a workaround
328  /// for bug #132031, which we hope to tackle soon (as of 6/2016)
329  template <class ChildrenPredicate, class PayloadPredicate>
331  PcpErrorVector *allErrors,
332  const ChildrenPredicate &childrenPred,
333  const PayloadPredicate &payloadPred,
334  const char *mallocTag1,
335  const char *mallocTag2) {
336  ComputePrimIndexesInParallel(SdfPathVector(1, path), allErrors,
337  childrenPred, payloadPred,
338  mallocTag1, mallocTag2);
339  }
340 
341  /// Vectorized form of ComputePrimIndexesInParallel(). Equivalent to
342  /// invoking that method for each path in \p paths, but more efficient.
343  template <class ChildrenPredicate, class PayloadPredicate>
345  PcpErrorVector *allErrors,
346  const ChildrenPredicate &childrenPred,
347  const PayloadPredicate &payloadPred) {
348  _UntypedIndexingChildrenPredicate cp(&childrenPred);
349  _UntypedIndexingPayloadPredicate pp(&payloadPred);
350  _ComputePrimIndexesInParallel(paths, allErrors, cp, pp,
351  "Pcp", "ComputePrimIndexesInParallel");
352  }
353 
354  /// \overload
355  /// XXX Do not add new callers of this method. It is needed as a workaround
356  /// for bug #132031, which we hope to tackle soon (as of 6/2016)
357  template <class ChildrenPredicate, class PayloadPredicate>
359  PcpErrorVector *allErrors,
360  const ChildrenPredicate &childrenPred,
361  const PayloadPredicate &payloadPred,
362  const char *mallocTag1,
363  const char *mallocTag2) {
364  _UntypedIndexingChildrenPredicate cp(&childrenPred);
365  _UntypedIndexingPayloadPredicate pp(&payloadPred);
366  _ComputePrimIndexesInParallel(paths, allErrors, cp, pp,
367  mallocTag1, mallocTag2);
368  }
369 
370  /// Returns a pointer to the cached computed prim index for the given
371  /// path, or NULL if it has not been computed.
372  PCP_API
373  const PcpPrimIndex *
374  FindPrimIndex(const SdfPath &primPath) const;
375 
376  /// Run the given \p callback on every prim index in the cache.
377  /// The callback must have the signature: void(const PcpPrimIndex&).
378  template <class Callback>
379  void
380  ForEachPrimIndex(const Callback& callback) const
381  {
383  _ForEachPrimIndex(fn);
384  }
385 
386  /// Compute and return a reference to the cached result for the
387  /// property index for the given path. \p allErrors will contain any
388  /// errors encountered while performing this operation.
389  PCP_API
390  const PcpPropertyIndex &
391  ComputePropertyIndex(const SdfPath &propPath, PcpErrorVector *allErrors);
392 
393  /// Returns a pointer to the cached computed property index for the given
394  /// path, or NULL if it has not been computed.
395  PCP_API
396  const PcpPropertyIndex *
397  FindPropertyIndex(const SdfPath &propPath) const;
398 
399  /// Compute the relationship target paths for the relationship at
400  /// \p relationshipPath into \p paths. If \p localOnly is \c true then
401  /// this will compose relationship targets from local nodes only. If
402  /// \p stopProperty is not \c NULL then this will stop composing
403  /// relationship targets at \p stopProperty, including \p stopProperty
404  /// iff \p includeStopProperty is \c true. If not \c NULL, \p deletedPaths
405  /// will be populated with target paths whose deletion contributed to
406  /// the computed result. \p allErrors will contain any errors encountered
407  /// while performing this operation.
408  PCP_API
409  void
410  ComputeRelationshipTargetPaths(const SdfPath &relationshipPath,
411  SdfPathVector *paths,
412  bool localOnly,
413  const SdfSpecHandle &stopProperty,
414  bool includeStopProperty,
415  SdfPathVector *deletedPaths,
416  PcpErrorVector *allErrors);
417 
418  /// Compute the attribute connection paths for the attribute at
419  /// \p attributePath into \p paths. If \p localOnly is \c true then
420  /// this will compose attribute connections from local nodes only. If
421  /// \p stopProperty is not \c NULL then this will stop composing
422  /// attribute connections at \p stopProperty, including \p stopProperty
423  /// iff \p includeStopProperty is \c true. If not \c NULL, \p deletedPaths
424  /// will be populated with connection paths whose deletion contributed to
425  /// the computed result. \p allErrors will contain any errors encountered
426  /// while performing this operation.
427  PCP_API
428  void
429  ComputeAttributeConnectionPaths(const SdfPath &attributePath,
430  SdfPathVector *paths,
431  bool localOnly,
432  const SdfSpecHandle &stopProperty,
433  bool includeStopProperty,
434  SdfPathVector *deletedPaths,
435  PcpErrorVector *allErrors);
436 
437  /// @}
438  /// \name Dependencies
439  /// @{
440 
441  /// Returns set of all layers used by this cache.
442  PCP_API
444 
445  /// Return a number that can be used to determine whether or not the set of
446  /// layers used by this cache may have changed or not. For example, if one
447  /// calls GetUsedLayers() and saves the GetUsedLayersRevision(), and then
448  /// later calls GetUsedLayersRevision() again, if the number is unchanged,
449  /// then GetUsedLayers() is guaranteed to be unchanged as well.
450  PCP_API
451  size_t GetUsedLayersRevision() const;
452 
453  /// Returns set of all root layers used by this cache.
454  PCP_API
456 
457  /// Returns every computed & cached layer stack that includes \p layer.
458  PCP_API
459  const PcpLayerStackPtrVector&
460  FindAllLayerStacksUsingLayer(const SdfLayerHandle& layer) const;
461 
462  /// Run the given \p callbcack on every layer stack used by prim
463  /// indexes in the cache. The callback must have the signature:
464  /// void(const PcpLayerStackPtr&).
465  template <class Callback>
466  void
467  ForEachLayerStack(const Callback& callback) const
468  {
470  _ForEachLayerStack(fn);
471  }
472 
473  /// Returns dependencies on the given site of scene description,
474  /// as discovered by the cached index computations.
475  ///
476  /// \param depMask specifies what classes of dependency to include;
477  /// see PcpDependencyFlags for details
478  /// \param recurseOnSite includes incoming dependencies on
479  /// children of sitePath
480  /// \param recurseOnIndex extends the result to include all PcpCache
481  /// child indexes below discovered results
482  /// \param filterForExistingCachesOnly filters the results to only
483  /// paths representing computed prim and property index caches;
484  /// otherwise a recursively-expanded result can include
485  /// un-computed paths that are expected to depend on the site
486  PCP_API
488  FindSiteDependencies(const PcpLayerStackPtr& siteLayerStack,
489  const SdfPath& sitePath,
490  PcpDependencyFlags depMask,
491  bool recurseOnSite,
492  bool recurseOnIndex,
493  bool filterForExistingCachesOnly) const;
494 
495  /// Returns dependencies on the given site of scene description,
496  /// as discovered by the cached index computations.
497  ///
498  /// This method overload takes a site layer rather than a layer
499  /// stack. It will check every layer stack using that layer, and
500  /// apply any relevant sublayer offsets to the map functions in the
501  /// returned PcpDependencyVector.
502  ///
503  /// See the other method for parameter details.
504  PCP_API
506  FindSiteDependencies(const SdfLayerHandle& siteLayer,
507  const SdfPath& sitePath,
508  PcpDependencyFlags depMask,
509  bool recurseOnSite,
510  bool recurseOnIndex,
511  bool filterForExistingCachesOnly) const;
512 
513  /// Returns \c true if an opinion for the site at \p localPcpSitePath
514  /// in the cache's layer stack can be provided by an opinion in \p layer,
515  /// \c false otherwise. If \c true and \p allowedPathInLayer is not
516  /// \c NULL then it's set to a path in \p layer that would provide an
517  /// opinion.
518  ///
519  /// This returns \c false if no prim index has yet been computed for
520  /// \p localPcpSitePath.
521  PCP_API
522  bool CanHaveOpinionForSite(const SdfPath& localPcpSitePath,
523  const SdfLayerHandle& layer,
524  SdfPath* allowedPathInLayer) const;
525 
526  /// Returns a vector of sublayer asset paths used in the layer stack
527  /// that didn't resolve to valid assets.
528  PCP_API
529  std::vector<std::string> GetInvalidSublayerIdentifiers() const;
530 
531  /// Returns true if \p identifier was used as a sublayer path in a
532  /// layer stack but did not identify a valid layer. This is functionally
533  /// equivalent to examining the values in the vector returned by
534  /// GetInvalidSublayerIdentifiers, but more efficient.
535  PCP_API
536  bool IsInvalidSublayerIdentifier(const std::string& identifier) const;
537 
538  /// Returns a map of prim paths to asset paths used by that prim
539  /// (e.g. in a reference) that didn't resolve to valid assets.
540  PCP_API
541  std::map<SdfPath, std::vector<std::string>, SdfPath::FastLessThan>
542  GetInvalidAssetPaths() const;
543 
544  /// Returns true if \p resolvedAssetPath was used by a prim (e.g. in
545  /// a reference) but did not resolve to a valid asset. This is
546  /// functionally equivalent to examining the values in the map returned
547  /// by GetInvalidAssetPaths, but more efficient.
548  PCP_API
549  bool IsInvalidAssetPath(const std::string& resolvedAssetPath) const;
550 
551  /// Returns true if any prim index in this cache has a dependency on a
552  /// dynamic file format argument field.
553  PCP_API
555 
556  /// Returns true if any prim index in this cache has a dependency on a
557  /// dynamic file format argument attribute's default value field.
558  PCP_API
560 
561  /// Returns true if the given \p field is the name of a field that
562  /// was composed while generating dynamic file format arguments for any prim
563  /// index in this cache.
564  PCP_API
565  bool IsPossibleDynamicFileFormatArgumentField(const TfToken &field) const;
566 
567  /// Returns true if the given \p attributeName is the name of an attribute
568  /// whose default value field was composed while generating dynamic file
569  /// format arguments for any prim index in this cache.
570  PCP_API
572  const TfToken &attributeName) const;
573 
574  /// Returns the dynamic file format dependency data object for the prim
575  /// index with the given \p primIndexPath. This will return an empty
576  /// dependency data if either there is no cache prim index for the path or
577  /// if the prim index has no dynamic file formats that it depends on.
578  PCP_API
581  const SdfPath &primIndexPath) const;
582 
583  /// Returns the list of prim index paths that depend on one or more
584  /// expression variables from \p layerStack.
585  PCP_API
587  const PcpLayerStackPtr &layerStack) const;
588 
589  /// Returns the set of expression variables in \p layerStack that are
590  /// used by the prim index at \p primIndexPath.
591  PCP_API
592  const std::unordered_set<std::string>&
594  const SdfPath &primIndexPath,
595  const PcpLayerStackPtr &layerStack) const;
596 
597  /// @}
598 
599  /// \name Change handling
600  /// @{
601 
602  /// Apply the changes in \p changes. This blows caches. It's up to
603  /// the client to pull on those caches again as needed.
604  ///
605  /// Objects that are no longer needed and would be destroyed are
606  /// retained in \p lifeboat and won't be destroyed until \p lifeboat is
607  /// itself destroyed. This gives the client control over the timing
608  /// of the destruction of those objects. Clients may choose to pull
609  /// on the caches before destroying \p lifeboat. That may cause the
610  /// caches to again retain the objects, meaning they won't be destroyed
611  /// when \p lifeboat is destroyed.
612  ///
613  /// For example, if blowing a cache means an SdfLayer is no longer
614  /// needed then \p lifeboat will hold an SdfLayerRefPtr to that layer.
615  /// The client can then pull on that cache, which could cause the
616  /// cache to hold an SdfLayerRefPtr to the layer again. If so then
617  /// destroying \p changes will not destroy the layer. In any case,
618  /// we don't destroy the layer and then read it again. However, if
619  /// the client destroys \p lifeboat before pulling on the cache then
620  /// we would destroy the layer then read it again.
621  PCP_API
622  void Apply(const PcpCacheChanges& changes, PcpLifeboat* lifeboat);
623 
624  /// Reload the layers of the layer stack, except session layers
625  /// and sublayers of session layers. This will also try to load
626  /// sublayers in this cache's layer stack that could not be loaded
627  /// previously. It will also try to load any referenced or payloaded
628  /// layer that could not be loaded previously. Clients should
629  /// subsequently \c Apply() \p changes to use any now-valid layers.
630  PCP_API
631  void Reload(PcpChanges* changes);
632 
633  /// Reload every layer used by the prim at \p primPath that's across
634  /// a reference or payload. Clients should subsequently apply the
635  /// changes to use any now valid layers.
636  ///
637  /// Note: If a reference or payload was to an invalid asset and this
638  /// asset is valid upon reloading then this call will not necessarily
639  /// reload every layer accessible across the reference or payload.
640  /// For example, say prim R has an invalid reference and prim Q has a
641  /// valid reference to layer X with sublayer Y. If on reload R now
642  /// has a valid reference to layer Z with sublayer Y, we will load Z
643  /// but we will not reload Y.
644  PCP_API
645  void ReloadReferences(PcpChanges* changes, const SdfPath& primPath);
646 
647  /// @}
648 
649  /// \name Diagnostics
650  /// @{
651 
652  /// Prints various statistics about the data stored in this cache.
653  PCP_API
654  void PrintStatistics() const;
655 
656  /// @}
657 
658 private:
659  friend class PcpChanges;
660  friend class Pcp_Dependencies;
661  friend class Pcp_Statistics;
662 
663  struct _ParallelIndexer;
664 
665  // Helper struct to type-erase a children predicate for the duration of
666  // ComputePrimIndexesInParallel.
667  //
668  // This lets us achieve two goals. First, clients may pass any arbitrary
669  // type as a predicate (e.g. they do not have to derive some base class).
670  // Second, it lets us keep the parallel indexing implementation in the .cpp
671  // file, avoiding any large template code instantiation.
672  //
673  // The cost we pay is this very thin indirect call. We instantiate a
674  // function template with the client's predicate type that simply does a
675  // typecast and predicate invocation, and pass that function pointer into
676  // the implementation. There is no heap allocation, no predicate copy, no
677  // argument marshalling, etc.
678  struct _UntypedIndexingChildrenPredicate {
679  _UntypedIndexingChildrenPredicate() : pred(nullptr), invoke(nullptr) {}
680  template <class Pred>
681  explicit _UntypedIndexingChildrenPredicate(const Pred *pred)
682  : pred(pred), invoke(_Invoke<Pred>) {}
683 
684  inline bool operator()(const PcpPrimIndex &index,
685  TfTokenVector *childNamesToCompose) const {
686  return invoke(pred, index, childNamesToCompose);
687  }
688  private:
689  template <class Pred>
690  static bool _Invoke(const void *pred, const PcpPrimIndex &index,
691  TfTokenVector *namesToCompose) {
692  return (*static_cast<const Pred *>(pred))(index, namesToCompose);
693  }
694  const void *pred;
695  bool (*invoke)(const void *, const PcpPrimIndex &, TfTokenVector *);
696  };
697 
698  // See doc for _UntypedIndexingChildrenPredicate above. This does the same
699  // for the payload inclusion predicate.
700  struct _UntypedIndexingPayloadPredicate {
701  template <class Pred>
702  explicit _UntypedIndexingPayloadPredicate(const Pred *pred)
703  : pred(pred), invoke(_Invoke<Pred>) {}
704 
705  inline bool operator()(const SdfPath &path) const {
706  return invoke(pred, path);
707  }
708  private:
709  template <class Pred>
710  static bool _Invoke(const void *pred, const SdfPath &path) {
711  return (*static_cast<const Pred *>(pred))(path);
712  }
713  const void *pred;
714  bool (*invoke)(const void *, const SdfPath &);
715  };
716 
717  // Internal helper for recursive indexing.
718  const PcpPrimIndex &
719  _ComputePrimIndexWithCompatibleInputs(
720  const SdfPath & path, const PcpPrimIndexInputs &inputs,
721  PcpErrorVector *allErrors);
722 
723  // Friend to allow low-level indexing code access to the above.
724  friend const PcpPrimIndex &
726  PcpCache &cache,
727  const SdfPath & path, const PcpPrimIndexInputs &inputs,
728  PcpErrorVector *allErrors);
729 
730  // Parallel indexing implementation.
731  PCP_API
732  void _ComputePrimIndexesInParallel(
733  const SdfPathVector &paths,
734  PcpErrorVector *allErrors,
735  _UntypedIndexingChildrenPredicate childrenPred,
736  _UntypedIndexingPayloadPredicate payloadPred,
737  const char *mallocTag1,
738  const char *mallocTag2);
739 
740  void _RemovePrimCache(const SdfPath& primPath, PcpLifeboat* lifeboat);
741  void _RemovePrimAndPropertyCaches(const SdfPath& root,
742  PcpLifeboat* lifeboat);
743  void _RemovePropertyCache(const SdfPath& root, PcpLifeboat* lifeboat);
744  void _RemovePropertyCaches(const SdfPath& root, PcpLifeboat* lifeboat);
745 
746  // Returns the prim index for \p path if it exists, NULL otherwise.
747  PcpPrimIndex* _GetPrimIndex(const SdfPath& path);
748  const PcpPrimIndex* _GetPrimIndex(const SdfPath& path) const;
749 
750  // Returns the property index for \p path if it exists, NULL otherwise.
751  PcpPropertyIndex* _GetPropertyIndex(const SdfPath& path);
752  const PcpPropertyIndex* _GetPropertyIndex(const SdfPath& path) const;
753 
754  PCP_API
755  void _ForEachPrimIndex(
756  const TfFunctionRef<void(const PcpPrimIndex&)>& fn) const;
757 
758  PCP_API
759  void _ForEachLayerStack(
760  const TfFunctionRef<void(const PcpLayerStackPtr&)>& fn) const;
761 
762 private:
763  // Fixed evaluation parameters, set when the cache is created. Note that
764  // _rootLayer and _sessionLayer are not const because we want to mutate them
765  // to enable parallel teardown in the destructor.
766  SdfLayerRefPtr _rootLayer;
767  SdfLayerRefPtr _sessionLayer;
768  const PcpLayerStackIdentifier _layerStackIdentifier;
769 
770  // Flag that configures PcpCache to use the restricted set of USD features.
771  // Currently it governs whether relocates, inherits, permissions,
772  // symmetry, or payloads are considered, and whether the prim stack
773  // is populated and its depdencies gathered during computation of
774  // prim indices and composition of prim child names.
775  const bool _usd;
776 
777  // File format target for all scene description layers this cache will
778  // find or open during prim index computation.
779  const std::string _fileFormatTarget;
780 
781  // The layer stack for this cache. Holding this by ref ptr means we
782  // hold all of our local layers by ref ptr (including the root and
783  // session layers, again).
784  PcpLayerStackRefPtr _layerStack;
785 
786  // Modifiable evaluation parameters.
787  // Anything that changes these should also yield a PcpChanges
788  // value describing the necessary cache invalidation.
789  PayloadSet _includedPayloads;
790  PcpVariantFallbackMap _variantFallbackMap;
791 
792  // Cached computation types.
793  typedef Pcp_LayerStackRegistryRefPtr _LayerStackCache;
794  typedef SdfPathTable<PcpPrimIndex> _PrimIndexCache;
795  typedef SdfPathTable<PcpPropertyIndex> _PropertyIndexCache;
796 
797  // Cached computations.
798  _LayerStackCache _layerStackCache;
799  _PrimIndexCache _primIndexCache;
800  _PropertyIndexCache _propertyIndexCache;
801  std::unique_ptr<Pcp_Dependencies> _primDependencies;
802 
803  // Parallel indexer state.
804  std::unique_ptr<_ParallelIndexer> _parallelIndexer;
805 };
806 
808 
809 #endif // PXR_USD_PCP_CACHE_H
PCP_API bool CanHaveOpinionForSite(const SdfPath &localPcpSitePath, const SdfLayerHandle &layer, SdfPath *allowedPathInLayer) const
std::vector< PcpDependency > PcpDependencyVector
Definition: dependency.h:131
void ComputePrimIndexesInParallel(const SdfPathVector &paths, PcpErrorVector *allErrors, const ChildrenPredicate &childrenPred, const PayloadPredicate &payloadPred)
Definition: cache.h:344
PCP_API bool IsPayloadIncluded(const SdfPath &path) const
Return true if the payload is included for the given path.
PCP_API void Reload(PcpChanges *changes)
PCP_API bool IsLayerMuted(const std::string &layerIdentifier) const
PCP_API bool HasAnyDynamicFileFormatArgumentFieldDependencies() const
PCP_API const PcpPropertyIndex & ComputePropertyIndex(const SdfPath &propPath, PcpErrorVector *allErrors)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
PCP_API const std::string & GetFileFormatTarget() const
Returns the file format target this cache is configured for.
PCP_API PcpDependencyVector FindSiteDependencies(const PcpLayerStackPtr &siteLayerStack, const SdfPath &sitePath, PcpDependencyFlags depMask, bool recurseOnSite, bool recurseOnIndex, bool filterForExistingCachesOnly) const
PCP_API ~PcpCache()
Definition: cache.h:93
Definition: spec.h:49
PCP_API SdfLayerHandleSet GetUsedRootLayers() const
Returns set of all root layers used by this cache.
std::set< SdfHandleTo< SdfLayer >::Handle > SdfLayerHandleSet
PCP_API bool IsUsd() const
Return true if the cache is configured in Usd mode.
PCP_API bool HasAnyDynamicFileFormatArgumentAttributeDependencies() const
PCP_API const std::vector< std::string > & GetMutedLayers() const
PCP_API const PcpPrimIndex & ComputePrimIndex(const SdfPath &primPath, PcpErrorVector *allErrors)
PCP_API std::vector< std::string > GetInvalidSublayerIdentifiers() const
void ComputePrimIndexesInParallel(const SdfPath &path, PcpErrorVector *allErrors, const ChildrenPredicate &childrenPred, const PayloadPredicate &payloadPred, const char *mallocTag1, const char *mallocTag2)
Definition: cache.h:330
PCP_API const PcpDynamicFileFormatDependencyData & GetDynamicFileFormatArgumentDependencyData(const SdfPath &primIndexPath) const
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
PCP_API bool IsInvalidAssetPath(const std::string &resolvedAssetPath) const
friend const PcpPrimIndex & Pcp_ComputePrimIndexWithCompatibleInputs(PcpCache &cache, const SdfPath &path, const PcpPrimIndexInputs &inputs, PcpErrorVector *allErrors)
void ComputePrimIndexesInParallel(const SdfPathVector &paths, PcpErrorVector *allErrors, const ChildrenPredicate &childrenPred, const PayloadPredicate &payloadPred, const char *mallocTag1, const char *mallocTag2)
Definition: cache.h:358
PCP_API const SdfPathVector & GetPrimsUsingExpressionVariablesFromLayerStack(const PcpLayerStackPtr &layerStack) const
PCP_API void ComputeAttributeConnectionPaths(const SdfPath &attributePath, SdfPathVector *paths, bool localOnly, const SdfSpecHandle &stopProperty, bool includeStopProperty, SdfPathVector *deletedPaths, PcpErrorVector *allErrors)
Definition: token.h:87
PCP_API const PcpPropertyIndex * FindPropertyIndex(const SdfPath &propPath) const
PCP_API size_t GetUsedLayersRevision() const
PCP_API void Apply(const PcpCacheChanges &changes, PcpLifeboat *lifeboat)
std::unordered_set< SdfPath, SdfPath::Hash > PayloadSet
Returns the payloads requested for inclusion.
Definition: cache.h:170
PCP_API PcpLayerStackRefPtr ComputeLayerStack(const PcpLayerStackIdentifier &identifier, PcpErrorVector *allErrors)
PCP_API void RequestPayloads(const SdfPathSet &pathsToInclude, const SdfPathSet &pathsToExclude, PcpChanges *changes=NULL)
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
PCP_API void ReloadReferences(PcpChanges *changes, const SdfPath &primPath)
friend class Pcp_Statistics
Definition: cache.h:661
PCP_API const PcpLayerStackPtrVector & FindAllLayerStacksUsingLayer(const SdfLayerHandle &layer) const
Returns every computed & cached layer stack that includes layer.
PCP_API bool IsInvalidSublayerIdentifier(const std::string &identifier) const
SDF_DECLARE_HANDLES(SdfSpec)
Definition: path.h:290
PCP_API PcpLayerStackPtr GetLayerStack() const
PCP_API const std::unordered_set< std::string > & GetExpressionVariablesFromLayerStackUsedByPrim(const SdfPath &primIndexPath, const PcpLayerStackPtr &layerStack) const
PCP_API PcpLayerStackPtr FindLayerStack(const PcpLayerStackIdentifier &identifier) const
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:211
std::vector< PcpErrorBasePtr > PcpErrorVector
Definition: errors.h:78
PCP_API const PcpPrimIndex * FindPrimIndex(const SdfPath &primPath) const
PCP_API void SetVariantFallbacks(const PcpVariantFallbackMap &map, PcpChanges *changes=NULL)
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:209
PCP_API std::map< SdfPath, std::vector< std::string >, SdfPath::FastLessThan > GetInvalidAssetPaths() const
friend class Pcp_Dependencies
Definition: cache.h:660
TF_DECLARE_WEAK_AND_REF_PTRS(PcpLayerStack)
void ForEachPrimIndex(const Callback &callback) const
Definition: cache.h:380
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
PCP_API PcpVariantFallbackMap GetVariantFallbacks() const
PCP_API SdfLayerHandleSet GetUsedLayers() const
Returns set of all layers used by this cache.
GLuint index
Definition: glcorearb.h:786
bool HasRootLayerStack(PcpLayerStackRefPtr const &layerStack) const
Definition: cache.h:134
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
PCP_API PcpPrimIndexInputs GetPrimIndexInputs()
PCP_API const PcpLayerStackIdentifier & GetLayerStackIdentifier() const
Get the identifier of the layerStack used for composition.
PCP_API bool IsPossibleDynamicFileFormatArgumentField(const TfToken &field) const
void ComputePrimIndexesInParallel(const SdfPath &path, PcpErrorVector *allErrors, const ChildrenPredicate &childrenPred, const PayloadPredicate &payloadPred)
Definition: cache.h:317
PCP_API void RequestLayerMuting(const std::vector< std::string > &layersToMute, const std::vector< std::string > &layersToUnmute, PcpChanges *changes=nullptr, std::vector< std::string > *newLayersMuted=nullptr, std::vector< std::string > *newLayersUnmuted=nullptr)
void ForEachLayerStack(const Callback &callback) const
Definition: cache.h:467
PCP_API void ComputeRelationshipTargetPaths(const SdfPath &relationshipPath, SdfPathVector *paths, bool localOnly, const SdfSpecHandle &stopProperty, bool includeStopProperty, SdfPathVector *deletedPaths, PcpErrorVector *allErrors)
PCP_API bool IsPossibleDynamicFileFormatArgumentAttribute(const TfToken &attributeName) const
PCP_API PayloadSet const & GetIncludedPayloads() const
Returns the payloads requested for inclusion.
unsigned int PcpDependencyFlags
A typedef for a bitmask of flags from PcpDependencyType.
Definition: dependency.h:108
PCP_API void PrintStatistics() const
Prints various statistics about the data stored in this cache.
std::map< std::string, std::vector< std::string > > PcpVariantFallbackMap
Definition: types.h:206
PCP_API bool UsesLayerStack(const PcpLayerStackPtr &layerStack) const
int invoke(const Func &taskFunc1, Rest...taskFuncN)
Definition: Invoke.h:64
#define PCP_API
Definition: api.h:40