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 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_CHANGES_H
25 #define PXR_USD_PCP_CHANGES_H
26 
27 /// \file pcp/changes.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/pcp/api.h"
32 #include "pxr/usd/sdf/changeList.h"
34 #include "pxr/usd/sdf/path.h"
35 #include "pxr/usd/sdf/types.h"
37 
38 #include <map>
39 #include <set>
40 
42 
45 
46 class PcpCache;
47 class PcpSite;
48 
49 /// \class PcpLayerStackChanges
50 ///
51 /// Types of changes per layer stack.
52 ///
54 public:
55  /// Must rebuild the layer tree. Implies didChangeLayerOffsets.
57 
58  /// Must rebuild the layer offsets.
60 
61  /// Must rebuild the relocation tables.
63 
64  /// Must rebuild expression variables.
66 
67  /// A significant layer stack change means the composed opinions of
68  /// the layer stack may have changed in arbitrary ways. This
69  /// represents a coarse invalidation. By way of contrast, an example
70  /// of an insignificant change is adding or removing a layer empty
71  /// of opinions.
73 
74  /// New relocation maps for this layer stack.
75  /// If didChangeRelocates is true, these fields will be populated
76  /// as part of determining the changes to this layer stack.
77  /// However, we do not immediately apply those changes to the
78  /// layer stack; we store them here and commit them in Apply().
84 
85  /// Paths that are affected by the above relocation changes.
87 
88  /// New expression variables for this layer stack.
90 
92  : didChangeLayers(false)
93  , didChangeLayerOffsets(false)
94  , didChangeRelocates(false)
96  , didChangeSignificantly(false)
97  , _didChangeExpressionVariablesSource(false)
98  {}
99 
100 private:
101  friend class PcpChanges;
102  friend class PcpLayerStack;
103 
104  // Expression variables source has changed.
105  bool _didChangeExpressionVariablesSource;
106 
107  // New source for expression variables for this layer stack.
108  PcpExpressionVariablesSource _newExpressionVariablesSource;
109 };
110 
111 /// \class PcpCacheChanges
112 ///
113 /// Types of changes per cache.
114 ///
116 public:
117  enum TargetType {
120  };
121 
122  /// Must rebuild the indexes at and below each path. This
123  /// implies rebuilding the prim/property stacks at
124  /// and below each path.
126 
127  /// Must rebuild the prim/property stacks at each path.
129 
130  /// Must rebuild the prim indexes at each path. This implies rebuilding
131  /// the prim stack at each path.
133 
134  /// Must rebuild the connections/targets at each path.
135  std::map<SdfPath, int, SdfPath::FastLessThan> didChangeTargets;
136 
137  /// Must update the path on every namespace object at and below each
138  /// given path. The first path is the old path to the object and the
139  /// second path is the new path. The order of the vector matters and
140  /// indicates the order in which the namespace edits occur.
141  std::vector<std::pair<SdfPath, SdfPath>> didChangePath;
142 
143  /// Layers used in the composition may have changed.
144  bool didMaybeChangeLayers = false;
145 
146 private:
147  friend class PcpCache;
148  friend class PcpChanges;
149 
150  // Must rebuild the prim/property stacks at each path due to a change
151  // that only affects the internal representation of the stack and
152  // not its contents. Because this causes no externally-observable
153  // changes in state, clients do not need to be aware of these changes.
154  SdfPathSet _didChangeSpecsInternal;
155 };
156 
157 /// Structure used to temporarily retain layers and layerStacks within
158 /// a code block. Analogous to the autorelease pool in obj-c.
159 class PcpLifeboat {
160 public:
161  PcpLifeboat();
162  ~PcpLifeboat();
163 
164  /// Ensure that \p layer exists until this object is destroyed.
165  void Retain(const SdfLayerRefPtr& layer);
166 
167  /// Ensure that \p layerStack exists until this object is destroyed.
168  void Retain(const PcpLayerStackRefPtr& layerStack);
169 
170  /// Returns reference to the set of layer stacks currently being held
171  /// in the lifeboat.
172  const std::set<PcpLayerStackRefPtr>& GetLayerStacks() const;
173 
174  /// Swap the contents of this and \p other.
175  void Swap(PcpLifeboat& other);
176 
177 private:
178  std::set<SdfLayerRefPtr> _layers;
179  std::set<PcpLayerStackRefPtr> _layerStacks;
180 };
181 
182 /// \class PcpChanges
183 ///
184 /// Describes Pcp changes.
185 ///
186 /// Collects changes to Pcp necessary to reflect changes in Sdf. It does
187 /// not cause any changes to any Pcp caches, layer stacks, etc; it only
188 /// computes what changes would be necessary to Pcp to reflect the Sdf
189 /// changes.
190 ///
191 class PcpChanges {
192 public:
195 
196  /// Breaks down \p changes into individual changes on \p cache. This
197  /// simply translates data in \p changes into other Did...() calls on
198  /// this object.
199  ///
200  /// Clients will typically call this method once then call \c Apply() or
201  /// get the changes using \c GetLayerStackChanges() and
202  /// \c GetCacheChanges().
203  PCP_API
204  void DidChange(const PcpCache* cache,
205  const SdfLayerChangeListVec& changes);
206 
207  /// Tries to load the sublayer of \p layer at \p sublayerPath. If
208  /// successful, any layer stack using \p layer is marked as having changed
209  /// and all prims in \p cache using any prim in any of those layer stacks
210  /// are marked as changed.
211  PCP_API
212  void DidMaybeFixSublayer(const PcpCache* cache,
213  const SdfLayerHandle& layer,
214  const std::string& assetPath);
215 
216  /// Tries to load the asset at \p assetPath. If successful, any prim
217  /// in \p cache using the site \p site is marked as changed.
218  PCP_API
219  void DidMaybeFixAsset(const PcpCache* cache,
220  const PcpSite& site,
221  const SdfLayerHandle& srcLayer,
222  const std::string& assetPath);
223 
224  /// The layer identified by \p layerId was muted in \p cache.
225  PCP_API
226  void DidMuteLayer(const PcpCache* cache, const std::string& layerId);
227 
228  /// The layer identified by \p layerId was unmuted in \p cache.
229  PCP_API
230  void DidUnmuteLayer(const PcpCache* cache, const std::string& layerId);
231 
232  /// The sublayer tree changed. This often, but doesn't always, imply that
233  /// anything and everything may have changed. If clients want to indicate
234  /// that anything and everything may have changed they should call this
235  /// method and \c DidChangePrimGraph() with the absolute root path.
236  PCP_API
237  void DidChangeLayers(const PcpCache* cache);
238 
239  /// The sublayer offsets changed.
240  PCP_API
241  void DidChangeLayerOffsets(const PcpCache* cache);
242 
243  /// The object at \p path changed significantly enough to require
244  /// recomputing the entire prim or property index. A significant change
245  /// implies changes to every namespace descendant's index, specs, and
246  /// dependencies.
247  PCP_API
248  void DidChangeSignificantly(const PcpCache* cache, const SdfPath& path);
249 
250  /// The spec stack for the prim or property has changed, due to the
251  /// addition or removal of the spec in \p changedLayer at \p changedPath.
252  /// This is used when inert prims/properties are added or removed or when
253  /// any change requires rebuilding the property stack. It implies that
254  /// dependencies on those specs has changed.
255  PCP_API
256  void DidChangeSpecs(const PcpCache* cache, const SdfPath& path,
257  const SdfLayerHandle& changedLayer,
258  const SdfPath& changedPath);
259 
260  /// The spec stack for the prim or property at \p path in \p cache has
261  /// changed.
262  PCP_API
263  void DidChangeSpecStack(const PcpCache* cache, const SdfPath& path);
264 
265  /// The connections on the attribute or targets on the relationship have
266  /// changed.
267  PCP_API
268  void DidChangeTargets(const PcpCache* cache, const SdfPath& path,
269  PcpCacheChanges::TargetType targetType);
270 
271  /// The relocates that affect prims and properties at and below
272  /// the given cache path have changed.
273  PCP_API
274  void DidChangeRelocates(const PcpCache* cache, const SdfPath& path);
275 
276  /// The composed object at \p oldPath was moved to \p newPath. This
277  /// implies every corresponding Sd change. This object will subsume
278  /// those Sd changes under this higher-level move. Sd path changes
279  /// that are not so subsumed will be converted to DidChangePrimGraph()
280  /// and/or DidChangeSpecs() changes.
281  PCP_API
282  void DidChangePaths(const PcpCache* cache,
283  const SdfPath& oldPath, const SdfPath& newPath);
284 
285  /// Remove any changes for \p cache.
286  PCP_API
287  void DidDestroyCache(const PcpCache* cache);
288 
289  /// The asset resolver has changed, invalidating previously-resolved
290  /// asset paths. This function will check all prim indexes in \p cache
291  /// for composition arcs that may now refer to a different asset and
292  /// mark them as needing significant resyncs.
293  PCP_API
294  void DidChangeAssetResolver(const PcpCache* cache);
295 
296  /// Swap the contents of this and \p other.
297  PCP_API
298  void Swap(PcpChanges& other);
299 
300  /// Returns \c true iff there are no changes.
301  PCP_API
302  bool IsEmpty() const;
303 
304  typedef std::map<PcpLayerStackPtr, PcpLayerStackChanges> LayerStackChanges;
305  typedef std::map<PcpCache*, PcpCacheChanges> CacheChanges;
306 
307  /// Returns a map of all of the layer stack changes. Note that some
308  /// keys may be to expired layer stacks.
309  PCP_API
311 
312  /// Returns a map of all of the cache changes.
313  PCP_API
314  const CacheChanges& GetCacheChanges() const;
315 
316  /// Returns the lifeboat responsible for maintaining the lifetime of
317  /// layers and layer stacks during change processing. Consumers may
318  /// inspect this object to determine which of these objects, if any,
319  /// had their lifetimes affected during change processing.
320  PCP_API
321  const PcpLifeboat& GetLifeboat() const;
322 
323  /// Applies the changes to the layer stacks and caches.
324  PCP_API
325  void Apply() const;
326 
327 private:
328  // Internal data types for namespace edits from Sd.
329  typedef std::map<SdfPath, SdfPath> _PathEditMap;
330  typedef std::map<PcpCache*, _PathEditMap> _RenameChanges;
331 
332  // Returns the PcpLayerStackChanges for the given cache's layer stack.
333  PcpLayerStackChanges& _GetLayerStackChanges(const PcpCache* cache);
334 
335  // Returns the PcpLayerStackChanges for the given layer stack.
336  PcpLayerStackChanges& _GetLayerStackChanges(const PcpLayerStackPtr&);
337 
338  // Returns the PcpCacheChanges for the given cache.
339  PcpCacheChanges& _GetCacheChanges(const PcpCache* cache);
340 
341  // Returns the _PathEditMap for the given cache.
342  _PathEditMap& _GetRenameChanges(const PcpCache* cache);
343 
344 
345  // Optimize the changes.
346  void _Optimize() const;
347 
348  // Optimize the changes.
349  void _Optimize();
350 
351  // Optimize the changes for a given cache.
352  void _Optimize(PcpCacheChanges*);
353 
354  // Optimize path changes.
355  void _OptimizePathChanges(const PcpCache* cache, PcpCacheChanges* changes,
356  const _PathEditMap* pathChanges);
357 
358  // Sublayer change type for _DidChangeSublayer.
359  enum _SublayerChangeType {
360  _SublayerAdded,
361  _SublayerRemoved
362  };
363 
364  // Helper function for loading a sublayer of \p layer at \p sublayerPath
365  // for processing changes described by \p sublayerChange.
366  SdfLayerRefPtr _LoadSublayerForChange(const PcpCache* cache,
367  const SdfLayerHandle& layer,
368  const std::string& sublayerPath,
369  _SublayerChangeType changeType) const;
370 
371  // Helper function for loading a sublayer at \p sublayerPath
372  // for processing changes described by \p sublayerChange.
373  SdfLayerRefPtr _LoadSublayerForChange(const PcpCache* cache,
374  const std::string& sublayerPath,
375  _SublayerChangeType changeType) const;
376 
377  // Propagates changes to \p sublayer specified by \p sublayerChange to
378  // the dependents of that sublayer. This includes all layer stacks
379  // that include the sublayer.
380  void _DidChangeSublayerAndLayerStacks(const PcpCache* cache,
381  const PcpLayerStackPtrVector& stacks,
382  const std::string& sublayerPath,
383  const SdfLayerHandle& sublayer,
384  _SublayerChangeType sublayerChange,
385  std::string* debugSummary);
386 
387  // Propagates changes to \p sublayer specified by \p sublayerChange to
388  // the dependents of that sublayer.
389  void _DidChangeSublayer(const PcpCache* cache,
390  const PcpLayerStackPtrVector& layerStacks,
391  const std::string& sublayerPath,
392  const SdfLayerHandle& sublayer,
393  _SublayerChangeType sublayerChange,
394  std::string* debugSummary,
395  bool *significant);
396 
397  // Propagates changes due to the addition/removal of the sublayer
398  // at the given \p sublayerPath to/from the parent \p layer.
399  void _DidAddOrRemoveSublayer(const PcpCache* cache,
400  const PcpLayerStackPtrVector& layerStacks,
401  const SdfLayerHandle& layer,
402  const std::string& sublayerPath,
403  _SublayerChangeType sublayerChange,
404  std::string* debugSummary,
405  std::vector<bool> *significant);
406 
407  // Mark the layer stack as having changed.
408  void _DidChangeLayerStack(
409  const PcpCache* cache,
410  const PcpLayerStackPtr& layerStack,
411  bool requiresLayerStackChange,
412  bool requiresLayerStackOffsetsChange,
413  bool requiresSignificantChange);
414 
415  // Mark the layer stack's relocations as having changed.
416  // Recompute the new relocations, storing the result in the Changes,
417  // so that change-processing can determine which other caches it
418  // needs to invalidate.
419  void _DidChangeLayerStackRelocations(
420  const PcpCache* cache,
421  const PcpLayerStackPtr& layerStack,
422  std::string* debugSummary);
423 
424  // Register changes to any prim indexes in \p caches that are affected
425  // by a change to a layer's resolved path used by \p layerStack.
426  void _DidChangeLayerStackResolvedPath(
427  const PcpCache* cache,
428  const PcpLayerStackPtr& layerStack,
429  bool requiresLayerStackChange,
430  std::string* debugSummary);
431 
432  // Register changes to layer stacks and prim indexes in \p cache that are
433  // affected by a change to a layer's expression variables used by
434  // \p layerStack.
435  void _DidChangeLayerStackExpressionVariables(
436  const PcpCache* cache,
437  const PcpLayerStackPtr& layerStack,
438  std::string* debugSummary);
439 
440  // The spec stack for the prim or property index at \p path must be
441  // recomputed due to a change that affects only the internal representation
442  // of the stack and not its contents.
443  void _DidChangeSpecStackInternal(
444  const PcpCache* cache, const SdfPath& path);
445 
446 private:
447  LayerStackChanges _layerStackChanges;
448  CacheChanges _cacheChanges;
449  _RenameChanges _renameChanges;
450  mutable PcpLifeboat _lifeboat;
451 };
452 
454 
455 #endif // PXR_USD_PCP_CHANGES_H
Definition: layer.h:94
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:82
VtDictionary newExpressionVariables
New expression variables for this layer stack.
Definition: changes.h:89
SdfPathSet didChangePrims
Definition: changes.h:132
PCP_API void DidChangeLayers(const PcpCache *cache)
std::map< SdfPath, SdfPath > SdfRelocatesMap
A map of source SdfPaths to target SdfPaths for relocation.
Definition: types.h:292
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
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:65
Definition: cache.h:93
PCP_API void Apply() const
Applies the changes to the layer stacks and caches.
Definition: site.h:45
PCP_API const CacheChanges & GetCacheChanges() const
Returns a map of all of the cache changes.
SdfPathVector newRelocatesPrimPaths
Definition: changes.h:83
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 DidChangeRelocates(const PcpCache *cache, const SdfPath &path)
PCP_API void DidChangeSignificantly(const PcpCache *cache, const SdfPath &path)
SdfPathSet pathsAffectedByRelocationChanges
Paths that are affected by the above relocation changes.
Definition: changes.h:86
PCP_API const PcpLifeboat & GetLifeboat() const
std::map< SdfPath, int, SdfPath::FastLessThan > didChangeTargets
Must rebuild the connections/targets at each path.
Definition: changes.h:135
PCP_API void DidChangeSpecs(const PcpCache *cache, const SdfPath &path, const SdfLayerHandle &changedLayer, const SdfPath &changedPath)
PCP_API PcpChanges()
PCP_API void DidDestroyCache(const PcpCache *cache)
Remove any changes for cache.
PCP_API void DidMuteLayer(const PcpCache *cache, const std::string &layerId)
The layer identified by layerId was muted in cache.
void Swap(PcpLifeboat &other)
Swap the contents of this and other.
SdfRelocatesMap newIncrementalRelocatesSourceToTarget
Definition: changes.h:81
bool didChangeLayers
Must rebuild the layer tree. Implies didChangeLayerOffsets.
Definition: changes.h:56
std::map< PcpLayerStackPtr, PcpLayerStackChanges > LayerStackChanges
Definition: changes.h:304
Definition: path.h:291
PCP_API void Swap(PcpChanges &other)
Swap the contents of this and other.
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:212
PCP_API void DidChangePaths(const PcpCache *cache, const SdfPath &oldPath, const SdfPath &newPath)
bool didMaybeChangeLayers
Layers used in the composition may have changed.
Definition: changes.h:144
std::set< class SdfPath > SdfPathSet
A set of SdfPaths.
Definition: path.h:210
std::map< PcpCache *, PcpCacheChanges > CacheChanges
Definition: changes.h:305
SdfPathSet didChangeSpecs
Must rebuild the prim/property stacks at each path.
Definition: changes.h:128
bool didChangeRelocates
Must rebuild the relocation tables.
Definition: changes.h:62
PCP_API void DidChangeLayerOffsets(const PcpCache *cache)
The sublayer offsets changed.
std::vector< std::pair< SdfLayerHandle, SdfChangeList > > SdfLayerChangeListVec
Definition: changeList.h:42
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
SdfPathSet didChangeSignificantly
Definition: changes.h:125
PCP_API bool IsEmpty() const
Returns true iff there are no changes.
SdfRelocatesMap newRelocatesTargetToSource
Definition: changes.h:79
PCP_API void DidChangeTargets(const PcpCache *cache, const SdfPath &path, PcpCacheChanges::TargetType targetType)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool didChangeSignificantly
Definition: changes.h:72
PCP_API void DidMaybeFixAsset(const PcpCache *cache, const PcpSite &site, const SdfLayerHandle &srcLayer, const std::string &assetPath)
SdfRelocatesMap newRelocatesSourceToTarget
Definition: changes.h:80
const std::set< PcpLayerStackRefPtr > & GetLayerStacks() const
PCP_API void DidUnmuteLayer(const PcpCache *cache, const std::string &layerId)
The layer identified by layerId was unmuted in cache.
void Retain(const SdfLayerRefPtr &layer)
Ensure that layer exists until this object is destroyed.
PCP_API void DidChangeSpecStack(const PcpCache *cache, const SdfPath &path)
PCP_API void DidChange(const PcpCache *cache, const SdfLayerChangeListVec &changes)
bool didChangeLayerOffsets
Must rebuild the layer offsets.
Definition: changes.h:59
std::vector< std::pair< SdfPath, SdfPath > > didChangePath
Definition: changes.h:141
#define PCP_API
Definition: api.h:40