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 object at \p path changed significantly enough to require
233  /// recomputing the entire prim or property index. A significant change
234  /// implies changes to every namespace descendant's index, specs, and
235  /// dependencies.
236  PCP_API
237  void DidChangeSignificantly(const PcpCache* cache, const SdfPath& path);
238 
239  /// The spec stack for the prim or property has changed, due to the
240  /// addition or removal of the spec in \p changedLayer at \p changedPath.
241  /// This is used when inert prims/properties are added or removed or when
242  /// any change requires rebuilding the property stack. It implies that
243  /// dependencies on those specs has changed.
244  PCP_API
245  void DidChangeSpecs(const PcpCache* cache, const SdfPath& path,
246  const SdfLayerHandle& changedLayer,
247  const SdfPath& changedPath);
248 
249  /// The spec stack for the prim or property at \p path in \p cache has
250  /// changed.
251  PCP_API
252  void DidChangeSpecStack(const PcpCache* cache, const SdfPath& path);
253 
254  /// The connections on the attribute or targets on the relationship have
255  /// changed.
256  PCP_API
257  void DidChangeTargets(const PcpCache* cache, const SdfPath& path,
258  PcpCacheChanges::TargetType targetType);
259 
260  /// The composed object at \p oldPath was moved to \p newPath. This
261  /// implies every corresponding Sd change. This object will subsume
262  /// those Sd changes under this higher-level move. Sd path changes
263  /// that are not so subsumed will be converted to DidChangePrimGraph()
264  /// and/or DidChangeSpecs() changes.
265  PCP_API
266  void DidChangePaths(const PcpCache* cache,
267  const SdfPath& oldPath, const SdfPath& newPath);
268 
269  /// Remove any changes for \p cache.
270  PCP_API
271  void DidDestroyCache(const PcpCache* cache);
272 
273  /// The asset resolver has changed, invalidating previously-resolved
274  /// asset paths. This function will check all prim indexes in \p cache
275  /// for composition arcs that may now refer to a different asset and
276  /// mark them as needing significant resyncs.
277  PCP_API
278  void DidChangeAssetResolver(const PcpCache* cache);
279 
280  /// Swap the contents of this and \p other.
281  PCP_API
282  void Swap(PcpChanges& other);
283 
284  /// Returns \c true iff there are no changes.
285  PCP_API
286  bool IsEmpty() const;
287 
288  typedef std::map<PcpLayerStackPtr, PcpLayerStackChanges> LayerStackChanges;
289  typedef std::map<PcpCache*, PcpCacheChanges> CacheChanges;
290 
291  /// Returns a map of all of the layer stack changes. Note that some
292  /// keys may be to expired layer stacks.
293  PCP_API
295 
296  /// Returns a map of all of the cache changes.
297  PCP_API
298  const CacheChanges& GetCacheChanges() const;
299 
300  /// Returns the lifeboat responsible for maintaining the lifetime of
301  /// layers and layer stacks during change processing. Consumers may
302  /// inspect this object to determine which of these objects, if any,
303  /// had their lifetimes affected during change processing.
304  PCP_API
305  const PcpLifeboat& GetLifeboat() const;
306 
307  /// Applies the changes to the layer stacks and caches.
308  PCP_API
309  void Apply() const;
310 
311 private:
312  // Internal data types for namespace edits from Sd.
313  typedef std::map<SdfPath, SdfPath> _PathEditMap;
314  typedef std::map<PcpCache*, _PathEditMap> _RenameChanges;
315 
316  // Returns the PcpLayerStackChanges for the given layer stack.
317  PcpLayerStackChanges& _GetLayerStackChanges(const PcpLayerStackPtr&);
318 
319  // Returns the PcpCacheChanges for the given cache.
320  PcpCacheChanges& _GetCacheChanges(const PcpCache* cache);
321 
322  // Returns the _PathEditMap for the given cache.
323  _PathEditMap& _GetRenameChanges(const PcpCache* cache);
324 
325 
326  // Optimize the changes.
327  void _Optimize() const;
328 
329  // Optimize the changes.
330  void _Optimize();
331 
332  // Optimize the changes for a given cache.
333  void _Optimize(PcpCacheChanges*);
334 
335  // Optimize path changes.
336  void _OptimizePathChanges(const PcpCache* cache, PcpCacheChanges* changes,
337  const _PathEditMap* pathChanges);
338 
339  // Sublayer change type for _DidChangeSublayer.
340  enum _SublayerChangeType {
341  _SublayerAdded,
342  _SublayerRemoved
343  };
344 
345  // Helper function for loading a sublayer of \p layer at \p sublayerPath
346  // for processing changes described by \p sublayerChange.
347  SdfLayerRefPtr _LoadSublayerForChange(const PcpCache* cache,
348  const SdfLayerHandle& layer,
349  const std::string& sublayerPath,
350  _SublayerChangeType changeType) const;
351 
352  // Helper function for loading a sublayer at \p sublayerPath
353  // for processing changes described by \p sublayerChange.
354  SdfLayerRefPtr _LoadSublayerForChange(const PcpCache* cache,
355  const std::string& sublayerPath,
356  _SublayerChangeType changeType) const;
357 
358  // Propagates changes to \p sublayer specified by \p sublayerChange to
359  // the dependents of that sublayer. This includes all layer stacks
360  // that include the sublayer.
361  void _DidChangeSublayerAndLayerStacks(const PcpCache* cache,
362  const PcpLayerStackPtrVector& stacks,
363  const std::string& sublayerPath,
364  const SdfLayerHandle& sublayer,
365  _SublayerChangeType sublayerChange,
366  std::string* debugSummary);
367 
368  // Propagates changes to \p sublayer specified by \p sublayerChange to
369  // the dependents of that sublayer.
370  void _DidChangeSublayer(const PcpCache* cache,
371  const PcpLayerStackPtrVector& layerStacks,
372  const std::string& sublayerPath,
373  const SdfLayerHandle& sublayer,
374  _SublayerChangeType sublayerChange,
375  std::string* debugSummary,
376  bool *significant);
377 
378  // Propagates changes due to the addition/removal of the sublayer
379  // at the given \p sublayerPath to/from the parent \p layer.
380  void _DidAddOrRemoveSublayer(const PcpCache* cache,
381  const PcpLayerStackPtrVector& layerStacks,
382  const SdfLayerHandle& layer,
383  const std::string& sublayerPath,
384  _SublayerChangeType sublayerChange,
385  std::string* debugSummary,
386  std::vector<bool> *significant);
387 
388  // Mark the layer stack as having changed.
389  void _DidChangeLayerStack(
390  const PcpCache* cache,
391  const PcpLayerStackPtr& layerStack,
392  bool requiresLayerStackChange,
393  bool requiresLayerStackOffsetsChange,
394  bool requiresSignificantChange);
395 
396  // Mark the layer stack's relocations as having changed.
397  // Recompute the new relocations, storing the result in the Changes,
398  // so that change-processing can determine which other caches it
399  // needs to invalidate.
400  void _DidChangeLayerStackRelocations(
401  const PcpCache* cache,
402  const PcpLayerStackPtr& layerStack,
403  std::string* debugSummary);
404 
405  // Register changes to any prim indexes in \p caches that are affected
406  // by a change to a layer's resolved path used by \p layerStack.
407  void _DidChangeLayerStackResolvedPath(
408  const PcpCache* cache,
409  const PcpLayerStackPtr& layerStack,
410  bool requiresLayerStackChange,
411  std::string* debugSummary);
412 
413  // Register changes to layer stacks and prim indexes in \p cache that are
414  // affected by a change to a layer's expression variables used by
415  // \p layerStack.
416  void _DidChangeLayerStackExpressionVariables(
417  const PcpCache* cache,
418  const PcpLayerStackPtr& layerStack,
419  std::string* debugSummary);
420 
421  // The spec stack for the prim or property index at \p path must be
422  // recomputed due to a change that affects only the internal representation
423  // of the stack and not its contents.
424  void _DidChangeSpecStackInternal(
425  const PcpCache* cache, const SdfPath& path);
426 
427 private:
428  LayerStackChanges _layerStackChanges;
429  CacheChanges _cacheChanges;
430  _RenameChanges _renameChanges;
431  mutable PcpLifeboat _lifeboat;
432 };
433 
435 
436 #endif // PXR_USD_PCP_CHANGES_H
Definition: layer.h:97
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
std::map< SdfPath, SdfPath > SdfRelocatesMap
A map of source SdfPaths to target SdfPaths for relocation.
Definition: types.h:284
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 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:288
Definition: path.h:290
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:211
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:209
std::map< PcpCache *, PcpCacheChanges > CacheChanges
Definition: changes.h:289
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
std::vector< std::pair< SdfLayerHandle, SdfChangeList > > SdfLayerChangeListVec
Definition: changeList.h:42
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
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