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. Any layer encountered during composition
201  /// with the same identifier will be considered muted and ignored.
202  ///
203  /// Note that muting a layer will cause this cache to release all
204  /// references to that layer. If no other client is holding on to
205  /// references to that layer, it will be unloaded. In this case, if
206  /// there are unsaved edits to the muted layer, those edits are lost.
207  /// Since anonymous layers are not serialized, muting an anonymous
208  /// layer will cause that layer and its contents to be lost in this
209  /// case.
210  ///
211  /// If \p changes is not \c nullptr, it is adjusted to reflect the
212  /// changes necessary to see the change in muted layers. Otherwise,
213  /// those changes are applied immediately.
214  ///
215  /// \p newLayersMuted and \p newLayersUnmuted contains the pruned vector
216  /// of layers which are muted or unmuted by this call to RequestLayerMuting.
217  ///
218  PCP_API
219  void RequestLayerMuting(const std::vector<std::string>& layersToMute,
220  const std::vector<std::string>& layersToUnmute,
221  PcpChanges* changes = nullptr,
222  std::vector<std::string>* newLayersMuted = nullptr,
223  std::vector<std::string>* newLayersUnmuted =
224  nullptr);
225 
226  /// Returns the list of canonical identifiers for muted layers
227  /// in this cache. See documentation on RequestLayerMuting for
228  /// more details.
229  PCP_API
230  const std::vector<std::string>& GetMutedLayers() const;
231 
232  /// Returns true if the layer specified by \p layerIdentifier is
233  /// muted in this cache, false otherwise. If \p layerIdentifier
234  /// is relative, it is assumed to be relative to this cache's
235  /// root layer. See documentation on RequestLayerMuting for
236  /// more details.
237  PCP_API
238  bool IsLayerMuted(const std::string& layerIdentifier) const;
239 
240  /// Returns true if the layer specified by \p layerIdentifier is
241  /// muted in this cache, false otherwise. If \p layerIdentifier
242  /// is relative, it is assumed to be relative to \p anchorLayer.
243  /// If \p canonicalMutedLayerIdentifier is supplied, it will be
244  /// populated with the canonical identifier of the muted layer if this
245  /// function returns true. See documentation on RequestLayerMuting
246  /// for more details.
247  PCP_API
248  bool IsLayerMuted(const SdfLayerHandle& anchorLayer,
249  const std::string& layerIdentifier,
250  std::string* canonicalMutedLayerIdentifier
251  = nullptr) const;
252 
253  /// Returns parameter object containing all inputs for the prim index
254  /// computation used by this cache.
255  PCP_API
257 
258  /// @}
259 
260  /// \name Computations
261  /// @{
262 
263  /// Returns the layer stack for \p identifier if it exists, otherwise
264  /// creates a new layer stack for \p identifier. This returns \c NULL
265  /// if \p identifier is invalid (i.e. its root layer is \c NULL).
266  /// \p allErrors will contain any errors encountered while creating a
267  /// new layer stack. It'll be unchanged if the layer stack already existed.
268  PCP_API
269  PcpLayerStackRefPtr
270  ComputeLayerStack(const PcpLayerStackIdentifier &identifier,
271  PcpErrorVector *allErrors);
272 
273  /// Returns the layer stack for \p identifier if it has been computed
274  /// and cached, otherwise returns \c NULL.
275  PCP_API
276  PcpLayerStackPtr
277  FindLayerStack(const PcpLayerStackIdentifier &identifier) const;
278 
279  /// Return true if \p layerStack is used by this cache in its composition,
280  /// false otherwise.
281  PCP_API
282  bool UsesLayerStack(const PcpLayerStackPtr &layerStack) const;
283 
284  /// Compute and return a reference to the cached result for the
285  /// prim index for the given path. \p allErrors will contain any errors
286  /// encountered while performing this operation.
287  PCP_API
288  const PcpPrimIndex &
289  ComputePrimIndex(const SdfPath &primPath, PcpErrorVector *allErrors);
290 
291  /// Compute PcpPrimIndexes in the subtree rooted at path in parallel,
292  /// recursing to children based on the supplied \p childrenPred. Also
293  /// include payloads not already in this cache's included payloads (see
294  /// GetIncludedPayloads()) according to \p payloadPred.
295  ///
296  /// This is similar to ComputePrimIndex(), except it computes an entire
297  /// subtree of indexes in parallel so it can be much more efficient. This
298  /// function invokes both \p childrenPred and \p payloadPred concurrently,
299  /// so it must be safe to do so.
300  ///
301  /// When a PcpPrimIndex computation completes invoke \p childrenPred,
302  /// passing it the PcpPrimIndex. If \p childrenPred returns true, continue
303  /// indexing children prim indexes. In this case, \p childrenPred may
304  /// provide a list of names of the children prim indexes to compute.
305  /// If it does not, all children prim indexes will be computed.
306  /// If \p childrenPred returns false, stop indexing in that subtree.
307  ///
308  /// If payloads discovered during indexing do not already appear in this
309  /// cache's set of included payloads, invoke \p payloadPred, passing it the
310  /// path for the prim with the payload. If \p payloadPred returns true,
311  /// include its payload and add it to the cache's set of included payloads
312  /// upon completion.
313  template <class ChildrenPredicate, class PayloadPredicate>
315  PcpErrorVector *allErrors,
316  const ChildrenPredicate &childrenPred,
317  const PayloadPredicate &payloadPred) {
318  ComputePrimIndexesInParallel(SdfPathVector(1, path), allErrors,
319  childrenPred, payloadPred,
320  "Pcp", "ComputePrimIndexesInParallel");
321  }
322 
323  /// \overload
324  /// XXX Do not add new callers of this method. It is needed as a workaround
325  /// for bug #132031, which we hope to tackle soon (as of 6/2016)
326  template <class ChildrenPredicate, class PayloadPredicate>
328  PcpErrorVector *allErrors,
329  const ChildrenPredicate &childrenPred,
330  const PayloadPredicate &payloadPred,
331  const char *mallocTag1,
332  const char *mallocTag2) {
333  ComputePrimIndexesInParallel(SdfPathVector(1, path), allErrors,
334  childrenPred, payloadPred,
335  mallocTag1, mallocTag2);
336  }
337 
338  /// Vectorized form of ComputePrimIndexesInParallel(). Equivalent to
339  /// invoking that method for each path in \p paths, but more efficient.
340  template <class ChildrenPredicate, class PayloadPredicate>
342  PcpErrorVector *allErrors,
343  const ChildrenPredicate &childrenPred,
344  const PayloadPredicate &payloadPred) {
345  _UntypedIndexingChildrenPredicate cp(&childrenPred);
346  _UntypedIndexingPayloadPredicate pp(&payloadPred);
347  _ComputePrimIndexesInParallel(paths, allErrors, cp, pp,
348  "Pcp", "ComputePrimIndexesInParallel");
349  }
350 
351  /// \overload
352  /// XXX Do not add new callers of this method. It is needed as a workaround
353  /// for bug #132031, which we hope to tackle soon (as of 6/2016)
354  template <class ChildrenPredicate, class PayloadPredicate>
356  PcpErrorVector *allErrors,
357  const ChildrenPredicate &childrenPred,
358  const PayloadPredicate &payloadPred,
359  const char *mallocTag1,
360  const char *mallocTag2) {
361  _UntypedIndexingChildrenPredicate cp(&childrenPred);
362  _UntypedIndexingPayloadPredicate pp(&payloadPred);
363  _ComputePrimIndexesInParallel(paths, allErrors, cp, pp,
364  mallocTag1, mallocTag2);
365  }
366 
367  /// Returns a pointer to the cached computed prim index for the given
368  /// path, or NULL if it has not been computed.
369  PCP_API
370  const PcpPrimIndex *
371  FindPrimIndex(const SdfPath &primPath) const;
372 
373  /// Run the given \p callback on every prim index in the cache.
374  /// The callback must have the signature: void(const PcpPrimIndex&).
375  template <class Callback>
376  void
377  ForEachPrimIndex(const Callback& callback) const
378  {
380  _ForEachPrimIndex(fn);
381  }
382 
383  /// Compute and return a reference to the cached result for the
384  /// property index for the given path. \p allErrors will contain any
385  /// errors encountered while performing this operation.
386  PCP_API
387  const PcpPropertyIndex &
388  ComputePropertyIndex(const SdfPath &propPath, PcpErrorVector *allErrors);
389 
390  /// Returns a pointer to the cached computed property index for the given
391  /// path, or NULL if it has not been computed.
392  PCP_API
393  const PcpPropertyIndex *
394  FindPropertyIndex(const SdfPath &propPath) const;
395 
396  /// Compute the relationship target paths for the relationship at
397  /// \p relationshipPath into \p paths. If \p localOnly is \c true then
398  /// this will compose relationship targets from local nodes only. If
399  /// \p stopProperty is not \c NULL then this will stop composing
400  /// relationship targets at \p stopProperty, including \p stopProperty
401  /// iff \p includeStopProperty is \c true. If not \c NULL, \p deletedPaths
402  /// will be populated with target paths whose deletion contributed to
403  /// the computed result. \p allErrors will contain any errors encountered
404  /// while performing this operation.
405  PCP_API
406  void
407  ComputeRelationshipTargetPaths(const SdfPath &relationshipPath,
408  SdfPathVector *paths,
409  bool localOnly,
410  const SdfSpecHandle &stopProperty,
411  bool includeStopProperty,
412  SdfPathVector *deletedPaths,
413  PcpErrorVector *allErrors);
414 
415  /// Compute the attribute connection paths for the attribute at
416  /// \p attributePath into \p paths. If \p localOnly is \c true then
417  /// this will compose attribute connections from local nodes only. If
418  /// \p stopProperty is not \c NULL then this will stop composing
419  /// attribute connections at \p stopProperty, including \p stopProperty
420  /// iff \p includeStopProperty is \c true. If not \c NULL, \p deletedPaths
421  /// will be populated with connection paths whose deletion contributed to
422  /// the computed result. \p allErrors will contain any errors encountered
423  /// while performing this operation.
424  PCP_API
425  void
426  ComputeAttributeConnectionPaths(const SdfPath &attributePath,
427  SdfPathVector *paths,
428  bool localOnly,
429  const SdfSpecHandle &stopProperty,
430  bool includeStopProperty,
431  SdfPathVector *deletedPaths,
432  PcpErrorVector *allErrors);
433 
434  /// @}
435  /// \name Dependencies
436  /// @{
437 
438  /// Returns set of all layers used by this cache.
439  PCP_API
441 
442  /// Return a number that can be used to determine whether or not the set of
443  /// layers used by this cache may have changed or not. For example, if one
444  /// calls GetUsedLayers() and saves the GetUsedLayersRevision(), and then
445  /// later calls GetUsedLayersRevision() again, if the number is unchanged,
446  /// then GetUsedLayers() is guaranteed to be unchanged as well.
447  PCP_API
448  size_t GetUsedLayersRevision() const;
449 
450  /// Returns set of all root layers used by this cache.
451  PCP_API
453 
454  /// Returns every computed & cached layer stack that includes \p layer.
455  PCP_API
456  const PcpLayerStackPtrVector&
457  FindAllLayerStacksUsingLayer(const SdfLayerHandle& layer) const;
458 
459  /// Run the given \p callbcack on every layer stack used by prim
460  /// indexes in the cache. The callback must have the signature:
461  /// void(const PcpLayerStackPtr&).
462  template <class Callback>
463  void
464  ForEachLayerStack(const Callback& callback) const
465  {
467  _ForEachLayerStack(fn);
468  }
469 
470  /// Returns dependencies on the given site of scene description,
471  /// as discovered by the cached index computations.
472  ///
473  /// \param depMask specifies what classes of dependency to include;
474  /// see PcpDependencyFlags for details
475  /// \param recurseOnSite includes incoming dependencies on
476  /// children of sitePath
477  /// \param recurseOnIndex extends the result to include all PcpCache
478  /// child indexes below discovered results
479  /// \param filterForExistingCachesOnly filters the results to only
480  /// paths representing computed prim and property index caches;
481  /// otherwise a recursively-expanded result can include
482  /// un-computed paths that are expected to depend on the site
483  PCP_API
485  FindSiteDependencies(const PcpLayerStackPtr& siteLayerStack,
486  const SdfPath& sitePath,
487  PcpDependencyFlags depMask,
488  bool recurseOnSite,
489  bool recurseOnIndex,
490  bool filterForExistingCachesOnly) const;
491 
492  /// Returns dependencies on the given site of scene description,
493  /// as discovered by the cached index computations.
494  ///
495  /// This method overload takes a site layer rather than a layer
496  /// stack. It will check every layer stack using that layer, and
497  /// apply any relevant sublayer offsets to the map functions in the
498  /// returned PcpDependencyVector.
499  ///
500  /// See the other method for parameter details.
501  PCP_API
503  FindSiteDependencies(const SdfLayerHandle& siteLayer,
504  const SdfPath& sitePath,
505  PcpDependencyFlags depMask,
506  bool recurseOnSite,
507  bool recurseOnIndex,
508  bool filterForExistingCachesOnly) const;
509 
510  /// Returns \c true if an opinion for the site at \p localPcpSitePath
511  /// in the cache's layer stack can be provided by an opinion in \p layer,
512  /// \c false otherwise. If \c true and \p allowedPathInLayer is not
513  /// \c NULL then it's set to a path in \p layer that would provide an
514  /// opinion.
515  ///
516  /// This returns \c false if no prim index has yet been computed for
517  /// \p localPcpSitePath.
518  PCP_API
519  bool CanHaveOpinionForSite(const SdfPath& localPcpSitePath,
520  const SdfLayerHandle& layer,
521  SdfPath* allowedPathInLayer) const;
522 
523  /// Returns a vector of sublayer asset paths used in the layer stack
524  /// that didn't resolve to valid assets.
525  PCP_API
526  std::vector<std::string> GetInvalidSublayerIdentifiers() const;
527 
528  /// Returns true if \p identifier was used as a sublayer path in a
529  /// layer stack but did not identify a valid layer. This is functionally
530  /// equivalent to examining the values in the vector returned by
531  /// GetInvalidSublayerIdentifiers, but more efficient.
532  PCP_API
533  bool IsInvalidSublayerIdentifier(const std::string& identifier) const;
534 
535  /// Returns a map of prim paths to asset paths used by that prim
536  /// (e.g. in a reference) that didn't resolve to valid assets.
537  PCP_API
538  std::map<SdfPath, std::vector<std::string>, SdfPath::FastLessThan>
539  GetInvalidAssetPaths() const;
540 
541  /// Returns true if \p resolvedAssetPath was used by a prim (e.g. in
542  /// a reference) but did not resolve to a valid asset. This is
543  /// functionally equivalent to examining the values in the map returned
544  /// by GetInvalidAssetPaths, but more efficient.
545  PCP_API
546  bool IsInvalidAssetPath(const std::string& resolvedAssetPath) const;
547 
548  /// Returns true if any prim index in this cache has a dependency on a
549  /// dynamic file format argument field.
550  PCP_API
552 
553  /// Returns true if any prim index in this cache has a dependency on a
554  /// dynamic file format argument attribute's default value field.
555  PCP_API
557 
558  /// Returns true if the given \p field is the name of a field that
559  /// was composed while generating dynamic file format arguments for any prim
560  /// index in this cache.
561  PCP_API
562  bool IsPossibleDynamicFileFormatArgumentField(const TfToken &field) const;
563 
564  /// Returns true if the given \p attributeName is the name of an attribute
565  /// whose default value field was composed while generating dynamic file
566  /// format arguments for any prim index in this cache.
567  PCP_API
569  const TfToken &attributeName) const;
570 
571  /// Returns the dynamic file format dependency data object for the prim
572  /// index with the given \p primIndexPath. This will return an empty
573  /// dependency data if either there is no cache prim index for the path or
574  /// if the prim index has no dynamic file formats that it depends on.
575  PCP_API
578  const SdfPath &primIndexPath) const;
579 
580  /// Returns the list of prim index paths that depend on one or more
581  /// expression variables from \p layerStack.
582  PCP_API
584  const PcpLayerStackPtr &layerStack) const;
585 
586  /// Returns the set of expression variables in \p layerStack that are
587  /// used by the prim index at \p primIndexPath.
588  PCP_API
589  const std::unordered_set<std::string>&
591  const SdfPath &primIndexPath,
592  const PcpLayerStackPtr &layerStack) const;
593 
594  /// @}
595 
596  /// \name Change handling
597  /// @{
598 
599  /// Apply the changes in \p changes. This blows caches. It's up to
600  /// the client to pull on those caches again as needed.
601  ///
602  /// Objects that are no longer needed and would be destroyed are
603  /// retained in \p lifeboat and won't be destroyed until \p lifeboat is
604  /// itself destroyed. This gives the client control over the timing
605  /// of the destruction of those objects. Clients may choose to pull
606  /// on the caches before destroying \p lifeboat. That may cause the
607  /// caches to again retain the objects, meaning they won't be destroyed
608  /// when \p lifeboat is destroyed.
609  ///
610  /// For example, if blowing a cache means an SdfLayer is no longer
611  /// needed then \p lifeboat will hold an SdfLayerRefPtr to that layer.
612  /// The client can then pull on that cache, which could cause the
613  /// cache to hold an SdfLayerRefPtr to the layer again. If so then
614  /// destroying \p changes will not destroy the layer. In any case,
615  /// we don't destroy the layer and then read it again. However, if
616  /// the client destroys \p lifeboat before pulling on the cache then
617  /// we would destroy the layer then read it again.
618  PCP_API
619  void Apply(const PcpCacheChanges& changes, PcpLifeboat* lifeboat);
620 
621  /// Reload the layers of the layer stack, except session layers
622  /// and sublayers of session layers. This will also try to load
623  /// sublayers in this cache's layer stack that could not be loaded
624  /// previously. It will also try to load any referenced or payloaded
625  /// layer that could not be loaded previously. Clients should
626  /// subsequently \c Apply() \p changes to use any now-valid layers.
627  PCP_API
628  void Reload(PcpChanges* changes);
629 
630  /// Reload every layer used by the prim at \p primPath that's across
631  /// a reference or payload. Clients should subsequently apply the
632  /// changes to use any now valid layers.
633  ///
634  /// Note: If a reference or payload was to an invalid asset and this
635  /// asset is valid upon reloading then this call will not necessarily
636  /// reload every layer accessible across the reference or payload.
637  /// For example, say prim R has an invalid reference and prim Q has a
638  /// valid reference to layer X with sublayer Y. If on reload R now
639  /// has a valid reference to layer Z with sublayer Y, we will load Z
640  /// but we will not reload Y.
641  PCP_API
642  void ReloadReferences(PcpChanges* changes, const SdfPath& primPath);
643 
644  /// @}
645 
646  /// \name Diagnostics
647  /// @{
648 
649  /// Prints various statistics about the data stored in this cache.
650  PCP_API
651  void PrintStatistics() const;
652 
653  /// @}
654 
655 private:
656  friend class PcpChanges;
657  friend class Pcp_Dependencies;
658  friend class Pcp_Statistics;
659 
660  struct _ParallelIndexer;
661 
662  // Helper struct to type-erase a children predicate for the duration of
663  // ComputePrimIndexesInParallel.
664  //
665  // This lets us achieve two goals. First, clients may pass any arbitrary
666  // type as a predicate (e.g. they do not have to derive some base class).
667  // Second, it lets us keep the parallel indexing implementation in the .cpp
668  // file, avoiding any large template code instantiation.
669  //
670  // The cost we pay is this very thin indirect call. We instantiate a
671  // function template with the client's predicate type that simply does a
672  // typecast and predicate invocation, and pass that function pointer into
673  // the implementation. There is no heap allocation, no predicate copy, no
674  // argument marshalling, etc.
675  struct _UntypedIndexingChildrenPredicate {
676  _UntypedIndexingChildrenPredicate() : pred(nullptr), invoke(nullptr) {}
677  template <class Pred>
678  explicit _UntypedIndexingChildrenPredicate(const Pred *pred)
679  : pred(pred), invoke(_Invoke<Pred>) {}
680 
681  inline bool operator()(const PcpPrimIndex &index,
682  TfTokenVector *childNamesToCompose) const {
683  return invoke(pred, index, childNamesToCompose);
684  }
685  private:
686  template <class Pred>
687  static bool _Invoke(const void *pred, const PcpPrimIndex &index,
688  TfTokenVector *namesToCompose) {
689  return (*static_cast<const Pred *>(pred))(index, namesToCompose);
690  }
691  const void *pred;
692  bool (*invoke)(const void *, const PcpPrimIndex &, TfTokenVector *);
693  };
694 
695  // See doc for _UntypedIndexingChildrenPredicate above. This does the same
696  // for the payload inclusion predicate.
697  struct _UntypedIndexingPayloadPredicate {
698  template <class Pred>
699  explicit _UntypedIndexingPayloadPredicate(const Pred *pred)
700  : pred(pred), invoke(_Invoke<Pred>) {}
701 
702  inline bool operator()(const SdfPath &path) const {
703  return invoke(pred, path);
704  }
705  private:
706  template <class Pred>
707  static bool _Invoke(const void *pred, const SdfPath &path) {
708  return (*static_cast<const Pred *>(pred))(path);
709  }
710  const void *pred;
711  bool (*invoke)(const void *, const SdfPath &);
712  };
713 
714  // Internal helper for recursive indexing.
715  const PcpPrimIndex &
716  _ComputePrimIndexWithCompatibleInputs(
717  const SdfPath & path, const PcpPrimIndexInputs &inputs,
718  PcpErrorVector *allErrors);
719 
720  // Friend to allow low-level indexing code access to the above.
721  friend const PcpPrimIndex &
723  PcpCache &cache,
724  const SdfPath & path, const PcpPrimIndexInputs &inputs,
725  PcpErrorVector *allErrors);
726 
727  // Parallel indexing implementation.
728  PCP_API
729  void _ComputePrimIndexesInParallel(
730  const SdfPathVector &paths,
731  PcpErrorVector *allErrors,
732  _UntypedIndexingChildrenPredicate childrenPred,
733  _UntypedIndexingPayloadPredicate payloadPred,
734  const char *mallocTag1,
735  const char *mallocTag2);
736 
737  void _RemovePrimCache(const SdfPath& primPath, PcpLifeboat* lifeboat);
738  void _RemovePrimAndPropertyCaches(const SdfPath& root,
739  PcpLifeboat* lifeboat);
740  void _RemovePropertyCache(const SdfPath& root, PcpLifeboat* lifeboat);
741  void _RemovePropertyCaches(const SdfPath& root, PcpLifeboat* lifeboat);
742 
743  // Returns the prim index for \p path if it exists, NULL otherwise.
744  PcpPrimIndex* _GetPrimIndex(const SdfPath& path);
745  const PcpPrimIndex* _GetPrimIndex(const SdfPath& path) const;
746 
747  // Returns the property index for \p path if it exists, NULL otherwise.
748  PcpPropertyIndex* _GetPropertyIndex(const SdfPath& path);
749  const PcpPropertyIndex* _GetPropertyIndex(const SdfPath& path) const;
750 
751  PCP_API
752  void _ForEachPrimIndex(
753  const TfFunctionRef<void(const PcpPrimIndex&)>& fn) const;
754 
755  PCP_API
756  void _ForEachLayerStack(
757  const TfFunctionRef<void(const PcpLayerStackPtr&)>& fn) const;
758 
759 private:
760  // Fixed evaluation parameters, set when the cache is created. Note that
761  // _rootLayer and _sessionLayer are not const because we want to mutate them
762  // to enable parallel teardown in the destructor.
763  SdfLayerRefPtr _rootLayer;
764  SdfLayerRefPtr _sessionLayer;
765  const PcpLayerStackIdentifier _layerStackIdentifier;
766 
767  // Flag that configures PcpCache to use the restricted set of USD features.
768  // Currently it governs whether relocates, inherits, permissions,
769  // symmetry, or payloads are considered, and whether the prim stack
770  // is populated and its depdencies gathered during computation of
771  // prim indices and composition of prim child names.
772  const bool _usd;
773 
774  // File format target for all scene description layers this cache will
775  // find or open during prim index computation.
776  const std::string _fileFormatTarget;
777 
778  // The layer stack for this cache. Holding this by ref ptr means we
779  // hold all of our local layers by ref ptr (including the root and
780  // session layers, again).
781  PcpLayerStackRefPtr _layerStack;
782 
783  // Modifiable evaluation parameters.
784  // Anything that changes these should also yield a PcpChanges
785  // value describing the necessary cache invalidation.
786  PayloadSet _includedPayloads;
787  PcpVariantFallbackMap _variantFallbackMap;
788 
789  // Cached computation types.
790  typedef Pcp_LayerStackRegistryRefPtr _LayerStackCache;
791  typedef SdfPathTable<PcpPrimIndex> _PrimIndexCache;
792  typedef SdfPathTable<PcpPropertyIndex> _PropertyIndexCache;
793 
794  // Cached computations.
795  _LayerStackCache _layerStackCache;
796  _PrimIndexCache _primIndexCache;
797  _PropertyIndexCache _propertyIndexCache;
798  std::unique_ptr<Pcp_Dependencies> _primDependencies;
799 
800  // Parallel indexer state.
801  std::unique_ptr<_ParallelIndexer> _parallelIndexer;
802 };
803 
805 
806 #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:341
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:327
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:355
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:442
PCP_API void ReloadReferences(PcpChanges *changes, const SdfPath &primPath)
friend class Pcp_Statistics
Definition: cache.h:658
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:291
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:212
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:210
PCP_API std::map< SdfPath, std::vector< std::string >, SdfPath::FastLessThan > GetInvalidAssetPaths() const
friend class Pcp_Dependencies
Definition: cache.h:657
TF_DECLARE_WEAK_AND_REF_PTRS(PcpLayerStack)
void ForEachPrimIndex(const Callback &callback) const
Definition: cache.h:377
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
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:314
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:464
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
#define PCP_API
Definition: api.h:40