HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
changes.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_USD_PCP_CHANGES_H
8 #define PXR_USD_PCP_CHANGES_H
9 
10 /// \file pcp/changes.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/pcp/api.h"
14 #include "pxr/usd/pcp/errors.h"
16 #include "pxr/usd/sdf/changeList.h"
18 #include "pxr/usd/sdf/path.h"
19 #include "pxr/usd/sdf/types.h"
21 
22 #include <map>
23 #include <set>
24 #include <unordered_set>
25 
27 
30 
31 class PcpCache;
32 class PcpSite;
33 
34 /// \class PcpLayerStackChanges
35 ///
36 /// Types of changes per layer stack.
37 ///
39 public:
40  /// Must rebuild the layer tree. Implies didChangeLayerOffsets.
42 
43  /// Must rebuild the layer offsets.
45 
46  /// Must rebuild the relocation tables.
48 
49  /// Must rebuild expression variables.
51 
52  /// A significant layer stack change means the composed opinions of
53  /// the layer stack may have changed in arbitrary ways. This
54  /// represents a coarse invalidation. By way of contrast, an example
55  /// of an insignificant change is adding or removing a layer empty
56  /// of opinions.
58 
59  /// New relocation maps for this layer stack.
60  /// If didChangeRelocates is true, these fields will be populated
61  /// as part of determining the changes to this layer stack.
62  /// However, we do not immediately apply those changes to the
63  /// layer stack; we store them here and commit them in Apply().
70 
71  /// Paths that are affected by the above relocation changes.
73 
74  /// New expression variables for this layer stack.
76 
78  : didChangeLayers(false)
79  , didChangeLayerOffsets(false)
80  , didChangeRelocates(false)
82  , didChangeSignificantly(false)
83  , _didChangeExpressionVariablesSource(false)
84  {}
85 
86 private:
87  friend class PcpChanges;
88  friend class PcpLayerStack;
89 
90  // Expression variables source has changed.
91  bool _didChangeExpressionVariablesSource;
92 
93  // New source for expression variables for this layer stack.
94  PcpExpressionVariablesSource _newExpressionVariablesSource;
95 };
96 
97 /// \class PcpCacheChanges
98 ///
99 /// Types of changes per cache.
100 ///
102 public:
103  enum TargetType {
106  };
107 
108  /// Must rebuild the indexes at and below each path. This
109  /// implies rebuilding the prim/property stacks at
110  /// and below each path.
112 
113  /// Must rebuild the prim/property stacks at each path.
115 
116  /// Must rebuild the prim indexes at each path. This implies rebuilding
117  /// the prim stack at each path.
119 
120  /// Must rebuild the connections/targets at each path.
121  std::map<SdfPath, int, SdfPath::FastLessThan> didChangeTargets;
122 
123  /// Must update the path on every namespace object at and below each
124  /// given path. The first path is the old path to the object and the
125  /// second path is the new path. The order of the vector matters and
126  /// indicates the order in which the namespace edits occur.
127  std::vector<std::pair<SdfPath, SdfPath>> didChangePath;
128 
129  /// Layers used in the composition may have changed.
130  bool didMaybeChangeLayers = false;
131 
132  /// Will be true if a muting operation took place on a non empty layer.
134 
135  /// Will be true if a non empty sublayer was added or removed.
137 
138  /// Set of layers that were explicitly muted or removed from a sublayer
139  /// list and all sublayers of those layers, recursively.
140  std::unordered_set<SdfLayerHandle, TfHash> layersAffectedByMutingOrRemoval;
141 
142  // Holds all the diff changelists that were computed when adding/removing
143  // sublayers or muting/unmuting layers.
145 
146 private:
147  friend class PcpCache;
148  friend class PcpChanges;
149 
150  using _ProcessedLayerSublayerPathPairsKey =
151  std::pair<SdfLayerHandle, std::string>;
152 
153  // Set of hashed layer / sublayer path pairs that have been processed in
154  // in this round of changes. These values are checked in order to avoid
155  // recursively processing cycles created in layer stacks.
156  std::unordered_set<_ProcessedLayerSublayerPathPairsKey, TfHash>
157  _processedLayerSublayerPathPairs;
158 
159  // Must rebuild the prim/property stacks at each path due to a change
160  // that only affects the internal representation of the stack and
161  // not its contents. Because this causes no externally-observable
162  // changes in state, clients do not need to be aware of these changes.
163  SdfPathSet _didChangeSpecsInternal;
164 
165  // This set serves a similar purpose to _didChangeSpecsInternal above,
166  // however, during processing descendants of the specs in this set will also
167  // be marked as changed. A performance gain is accomplished by placing the
168  // ancestor specs in this set and processing children iteratively when
169  // applying changes to the cache.
170  SdfPathSet _didChangePrimSpecsAndChildrenInternal;
171 };
172 
173 /// Structure used to temporarily retain layers and layerStacks within
174 /// a code block. Analogous to the autorelease pool in obj-c.
175 class PcpLifeboat {
176 public:
177  PcpLifeboat();
178  ~PcpLifeboat();
179 
180  /// Ensure that \p layer exists until this object is destroyed.
181  void Retain(const SdfLayerRefPtr& layer);
182 
183  /// Ensure that \p layerStack exists until this object is destroyed.
184  void Retain(const PcpLayerStackRefPtr& layerStack);
185 
186  /// Returns reference to the set of layer stacks currently being held
187  /// in the lifeboat.
188  const std::set<PcpLayerStackRefPtr>& GetLayerStacks() const;
189 
190  /// Swap the contents of this and \p other.
191  void Swap(PcpLifeboat& other);
192 
193 private:
194  std::set<SdfLayerRefPtr> _layers;
195  std::set<PcpLayerStackRefPtr> _layerStacks;
196 };
197 
198 /// \class PcpChanges
199 ///
200 /// Describes Pcp changes.
201 ///
202 /// Collects changes to Pcp necessary to reflect changes in Sdf. It does
203 /// not cause any changes to any Pcp caches, layer stacks, etc; it only
204 /// computes what changes would be necessary to Pcp to reflect the Sdf
205 /// changes.
206 ///
207 class PcpChanges {
208 public:
211 
212  /// Breaks down \p changes into individual changes on \p cache. This
213  /// simply translates data in \p changes into other Did...() calls on
214  /// this object.
215  ///
216  /// Clients will typically call this method once then call \c Apply() or
217  /// get the changes using \c GetLayerStackChanges() and
218  /// \c GetCacheChanges().
219  PCP_API
220  void DidChange(const PcpCache* cache,
221  const SdfLayerChangeListVec& changes);
222 
223  /// Tries to load the sublayer of \p layer at \p sublayerPath. If
224  /// successful, any layer stack using \p layer is marked as having changed
225  /// and all prims in \p cache using any prim in any of those layer stacks
226  /// are marked as changed.
227  PCP_API
228  void DidMaybeFixSublayer(const PcpCache* cache,
229  const SdfLayerHandle& layer,
230  const std::string& assetPath);
231 
232  /// Tries to load the asset at \p assetPath. If successful, any prim
233  /// in \p cache using the site \p site is marked as changed.
234  PCP_API
235  void DidMaybeFixAsset(const PcpCache* cache,
236  const PcpSite& site,
237  const SdfLayerHandle& srcLayer,
238  const std::string& assetPath);
239 
240  /// The layer identified by \p layerId was muted in \p cache.
241  PCP_API
242  void _DidMuteLayer(const PcpCache* cache, const std::string& layerId);
243 
244  /// The layer identified by \p layerId was unmuted in \p cache.
245  PCP_API
246  void _DidUnmuteLayer(const PcpCache* cache, const std::string& layerId);
247 
248  /// Sets the list of layers that will ultimately be muted and unmuted for
249  /// this round of changes. This is used as hints for various change
250  /// processing methods. Note that identifiers passed into this function
251  /// will be opened and placed in the lifeboat associated with this
252  /// PcpChanges object.
253  void DidMuteAndUnmuteLayers(const PcpCache* cache,
254  const std::vector<std::string>& layersToMute,
255  const std::vector<std::string>& layersToUnmute);
256 
257  /// The object at \p path changed significantly enough to require
258  /// recomputing the entire prim or property index. A significant change
259  /// implies changes to every namespace descendant's index, specs, and
260  /// dependencies.
261  PCP_API
262  void DidChangeSignificantly(const PcpCache* cache, const SdfPath& path);
263 
267  };
268 
269  /// The spec stack for the prim or property has changed, due to the
270  /// addition or removal of the spec in \p changedLayer at \p changedPath.
271  /// This is used when inert prims/properties are added or removed or when
272  /// any change requires rebuilding the property stack. It implies that
273  /// dependencies on those specs has changed.
274  PCP_API
275  void DidChangeSpecs(const PcpCache* cache, const SdfPath& path,
276  const SdfLayerHandle& changedLayer,
277  const SdfPath& changedPath, ChangeSpecsType changeType);
278 
279  /// The spec stack for the prim or property at \p path in \p cache has
280  /// changed.
281  PCP_API
282  void DidChangeSpecStack(const PcpCache* cache, const SdfPath& path);
283 
284  /// The connections on the attribute or targets on the relationship have
285  /// changed.
286  PCP_API
287  void DidChangeTargets(const PcpCache* cache, const SdfPath& path,
288  PcpCacheChanges::TargetType targetType);
289 
290  /// The composed object at \p oldPath was moved to \p newPath. This
291  /// implies every corresponding Sd change. This object will subsume
292  /// those Sd changes under this higher-level move. Sd path changes
293  /// that are not so subsumed will be converted to DidChangePrimGraph()
294  /// and/or DidChangeSpecs() changes.
295  PCP_API
296  void DidChangePaths(const PcpCache* cache,
297  const SdfPath& oldPath, const SdfPath& newPath);
298 
299  /// Remove any changes for \p cache.
300  PCP_API
301  void DidDestroyCache(const PcpCache* cache);
302 
303  /// The asset resolver has changed, invalidating previously-resolved
304  /// asset paths. This function will check all prim indexes in \p cache
305  /// for composition arcs that may now refer to a different asset and
306  /// mark them as needing significant resyncs.
307  PCP_API
308  void DidChangeAssetResolver(const PcpCache* cache);
309 
310  /// Swap the contents of this and \p other.
311  PCP_API
312  void Swap(PcpChanges& other);
313 
314  /// Returns \c true iff there are no changes.
315  PCP_API
316  bool IsEmpty() const;
317 
318  typedef std::map<PcpLayerStackPtr, PcpLayerStackChanges> LayerStackChanges;
319  typedef std::map<PcpCache*, PcpCacheChanges> CacheChanges;
320 
321  /// Returns a map of all of the layer stack changes. Note that some
322  /// keys may be to expired layer stacks.
323  PCP_API
325 
326  /// Returns a map of all of the cache changes.
327  PCP_API
328  const CacheChanges& GetCacheChanges() const;
329 
330  /// Returns the lifeboat responsible for maintaining the lifetime of
331  /// layers and layer stacks during change processing. Consumers may
332  /// inspect this object to determine which of these objects, if any,
333  /// had their lifetimes affected during change processing.
334  PCP_API
335  const PcpLifeboat& GetLifeboat() const;
336 
337  /// Applies the changes to the layer stacks and caches.
338  PCP_API
339  void Apply() const;
340 
341 private:
342  // Internal data types for namespace edits from Sd.
343  typedef std::map<SdfPath, SdfPath> _PathEditMap;
344  typedef std::map<PcpCache*, _PathEditMap> _RenameChanges;
345 
346  // Returns the PcpLayerStackChanges for the given layer stack.
347  PcpLayerStackChanges& _GetLayerStackChanges(const PcpLayerStackPtr&);
348 
349  // Returns the PcpCacheChanges for the given cache.
350  PcpCacheChanges& _GetCacheChanges(const PcpCache* cache);
351 
352  // Returns the _PathEditMap for the given cache.
353  _PathEditMap& _GetRenameChanges(const PcpCache* cache);
354 
355 
356  // Optimize the changes.
357  void _Optimize() const;
358 
359  // Optimize the changes.
360  void _Optimize();
361 
362  // Optimize the changes for a given cache.
363  void _Optimize(PcpCacheChanges*);
364 
365  // Optimize path changes.
366  void _OptimizePathChanges(const PcpCache* cache, PcpCacheChanges* changes,
367  const _PathEditMap* pathChanges);
368 
369  // Sublayer change type for _DidChangeSublayer.
370  enum _SublayerChangeType {
371  _SublayerAdded,
372  _SublayerRemoved
373  };
374 
375  // Helper function for loading a sublayer of \p layer at \p sublayerPath
376  // for processing changes described by \p sublayerChange.
377  SdfLayerRefPtr _LoadSublayerForChange(const PcpCache* cache,
378  const SdfLayerHandle& layer,
379  const std::string& sublayerPath,
380  _SublayerChangeType changeType) const;
381 
382  // Helper function for loading a sublayer at \p sublayerPath
383  // for processing changes described by \p sublayerChange.
384  SdfLayerRefPtr _LoadSublayerForChange(const PcpCache* cache,
385  const std::string& sublayerPath,
386  _SublayerChangeType changeType) const;
387 
388  // Propagates changes to \p sublayer specified by \p sublayerChange to
389  // the dependents of that sublayer. This includes all layer stacks
390  // that include the sublayer.
391  void _DidChangeSublayerAndLayerStacks(const PcpCache* cache,
392  const PcpLayerStackPtrVector& stacks,
393  const std::string& sublayerPath,
394  const SdfLayerHandle& sublayer,
395  _SublayerChangeType sublayerChange,
396  std::string* debugSummary);
397 
398  // Propagates changes to \p sublayer specified by \p sublayerChange to
399  // the dependents of that sublayer.
400  void _DidChangeSublayer(const PcpCache* cache,
401  const PcpLayerStackPtrVector& layerStacks,
402  const std::string& sublayerPath,
403  const SdfLayerHandle& sublayer,
404  _SublayerChangeType sublayerChange,
405  std::string* debugSummary,
406  bool *significant);
407 
408  // Propagates changes due to the addition/removal of the sublayer
409  // at the given \p sublayerPath to/from the parent \p layer.
410  void _DidAddOrRemoveSublayer(const PcpCache* cache,
411  const PcpLayerStackPtrVector& layerStacks,
412  const SdfLayerHandle& layer,
413  const std::string& sublayerPath,
414  _SublayerChangeType sublayerChange,
415  std::string* debugSummary,
416  std::vector<bool> *significant);
417 
418  // Mark the layer stack as having changed.
419  void _DidChangeLayerStack(
420  const PcpCache* cache,
421  const PcpLayerStackPtr& layerStack,
422  bool requiresLayerStackChange,
423  bool requiresLayerStackOffsetsChange,
424  bool requiresSignificantChange);
425 
426  // Mark the layer stack's relocations as having changed.
427  // Recompute the new relocations, storing the result in the Changes,
428  // so that change-processing can determine which other caches it
429  // needs to invalidate.
430  void _DidChangeLayerStackRelocations(
431  const PcpCache* cache,
432  const PcpLayerStackPtr& layerStack,
433  std::string* debugSummary);
434 
435  // Register changes to any prim indexes in \p caches that are affected
436  // by a change to a layer's resolved path used by \p layerStack.
437  void _DidChangeLayerStackResolvedPath(
438  const PcpCache* cache,
439  const PcpLayerStackPtr& layerStack,
440  bool requiresLayerStackChange,
441  std::string* debugSummary);
442 
443  // Register changes to layer stacks and prim indexes in \p cache that are
444  // affected by a change to a layer's expression variables used by
445  // \p layerStack.
446  void _DidChangeLayerStackExpressionVariables(
447  const PcpCache* cache,
448  const PcpLayerStackPtr& layerStack,
449  std::string* debugSummary);
450 
451  // The spec stack for the prim or property index at \p path must be
452  // recomputed due to a change that affects only the internal representation
453  // of the stack and not its contents.
454  void _DidChangeSpecStackInternal(
455  const PcpCache* cache, const SdfPath& path);
456 
457  void _DidChangeSpecStackAndChildrenInternal(
458  const PcpCache* cache, const SdfPath& path);
459 
460  // This method is used when processing layer operations. It ensures that
461  // affected layer stacks and their dependent spec stacks are marked as
462  // changed.
463  void _ProcessLayerStackAndDependencyChanges(
464  const PcpCache* cache,
465  const PcpLayerStackPtrVector& layerStacks);
466 
467  // When muting or unmuting a layer that is being referenced or payloaded,
468  // we need to ensure that all the relevant sites are recomposed. This
469  // function searches site dependencies of the provided layer stacks and
470  // marks those that are introduced via reference or payload arcs as
471  // significantly changed.
472  void _MarkReferencingSitesAsSignificantlyChanged(
473  const PcpCache* cache,
474  const PcpLayerStackPtrVector& layerStacks);
475 
476 private:
477  LayerStackChanges _layerStackChanges;
478  CacheChanges _cacheChanges;
479  _RenameChanges _renameChanges;
480  mutable PcpLifeboat _lifeboat;
481 };
482 
484 
485 #endif // PXR_USD_PCP_CHANGES_H
Definition: layer.h:81
PXR_NAMESPACE_OPEN_SCOPE SDF_DECLARE_HANDLES(SdfLayer)
PCP_API void DidMaybeFixSublayer(const PcpCache *cache, const SdfLayerHandle &layer, const std::string &assetPath)
SdfRelocatesMap newIncrementalRelocatesTargetToSource
Definition: changes.h:67
VtDictionary newExpressionVariables
New expression variables for this layer stack.
Definition: changes.h:75
SdfPathSet didChangePrims
Definition: changes.h:118
SdfLayerChangeListVec layerChangeListVec
Definition: changes.h:144
std::map< SdfPath, SdfPath > SdfRelocatesMap
A map of source SdfPaths to target SdfPaths for relocation.
Definition: types.h:267
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
TF_DECLARE_WEAK_AND_REF_PTRS(PcpLayerStack)
PCP_API ~PcpChanges()
bool didChangeExpressionVariables
Must rebuild expression variables.
Definition: changes.h:50
Definition: cache.h:76
PCP_API void Apply() const
Applies the changes to the layer stacks and caches.
Definition: site.h:28
PCP_API const CacheChanges & GetCacheChanges() const
Returns a map of all of the cache changes.
SdfPathVector newRelocatesPrimPaths
Definition: changes.h:68
PCP_API void DidChangeAssetResolver(const PcpCache *cache)
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
PCP_API const LayerStackChanges & GetLayerStackChanges() const
PCP_API void DidChangeSignificantly(const PcpCache *cache, const SdfPath &path)
SdfPathSet pathsAffectedByRelocationChanges
Paths that are affected by the above relocation changes.
Definition: changes.h:72
std::vector< class SdfPath > SdfPathVector
PCP_API const PcpLifeboat & GetLifeboat() const
std::map< SdfPath, int, SdfPath::FastLessThan > didChangeTargets
Must rebuild the connections/targets at each path.
Definition: changes.h:121
PCP_API PcpChanges()
PCP_API void DidDestroyCache(const PcpCache *cache)
Remove any changes for cache.
PcpErrorVector newRelocatesErrors
Definition: changes.h:69
void Swap(PcpLifeboat &other)
Swap the contents of this and other.
SdfRelocatesMap newIncrementalRelocatesSourceToTarget
Definition: changes.h:66
bool didChangeLayers
Must rebuild the layer tree. Implies didChangeLayerOffsets.
Definition: changes.h:41
std::map< PcpLayerStackPtr, PcpLayerStackChanges > LayerStackChanges
Definition: changes.h:318
Definition: path.h:273
PCP_API void Swap(PcpChanges &other)
Swap the contents of this and other.
PCP_API void DidChangeSpecs(const PcpCache *cache, const SdfPath &path, const SdfLayerHandle &changedLayer, const SdfPath &changedPath, ChangeSpecsType changeType)
PCP_API void DidChangePaths(const PcpCache *cache, const SdfPath &oldPath, const SdfPath &newPath)
std::vector< PcpErrorBasePtr > PcpErrorVector
Definition: errors.h:65
bool didMaybeChangeLayers
Layers used in the composition may have changed.
Definition: changes.h:130
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:192
std::map< PcpCache *, PcpCacheChanges > CacheChanges
Definition: changes.h:319
SdfPathSet didChangeSpecs
Must rebuild the prim/property stacks at each path.
Definition: changes.h:114
bool didChangeRelocates
Must rebuild the relocation tables.
Definition: changes.h:47
std::vector< std::pair< SdfLayerHandle, SdfChangeList > > SdfLayerChangeListVec
Definition: changeList.h:25
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
SdfPathSet didChangeSignificantly
Definition: changes.h:111
PCP_API bool IsEmpty() const
Returns true iff there are no changes.
SdfRelocatesMap newRelocatesTargetToSource
Definition: changes.h:64
PCP_API void DidChangeTargets(const PcpCache *cache, const SdfPath &path, PcpCacheChanges::TargetType targetType)
PCP_API void _DidUnmuteLayer(const PcpCache *cache, const std::string &layerId)
The layer identified by layerId was unmuted in cache.
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
ChangeSpecsType
Definition: changes.h:264
bool didChangeSignificantly
Definition: changes.h:57
PCP_API void DidMaybeFixAsset(const PcpCache *cache, const PcpSite &site, const SdfLayerHandle &srcLayer, const std::string &assetPath)
SdfRelocatesMap newRelocatesSourceToTarget
Definition: changes.h:65
PCP_API void _DidMuteLayer(const PcpCache *cache, const std::string &layerId)
The layer identified by layerId was muted in cache.
const std::set< PcpLayerStackRefPtr > & GetLayerStacks() const
void Retain(const SdfLayerRefPtr &layer)
Ensure that layer exists until this object is destroyed.
PCP_API void DidChangeSpecStack(const PcpCache *cache, const SdfPath &path)
bool didMuteOrUnmuteNonEmptyLayer
Will be true if a muting operation took place on a non empty layer.
Definition: changes.h:133
PCP_API void DidChange(const PcpCache *cache, const SdfLayerChangeListVec &changes)
void DidMuteAndUnmuteLayers(const PcpCache *cache, const std::vector< std::string > &layersToMute, const std::vector< std::string > &layersToUnmute)
bool didChangeLayerOffsets
Must rebuild the layer offsets.
Definition: changes.h:44
std::unordered_set< SdfLayerHandle, TfHash > layersAffectedByMutingOrRemoval
Definition: changes.h:140
std::vector< std::pair< SdfPath, SdfPath > > didChangePath
Definition: changes.h:127
#define PCP_API
Definition: api.h:23
bool didAddOrRemoveNonEmptySublayer
Will be true if a non empty sublayer was added or removed.
Definition: changes.h:136