HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
layerStack.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_LAYER_STACK_H
8 #define PXR_USD_PCP_LAYER_STACK_H
9 
10 /// \file pcp/layerStack.h
11 
12 #include "pxr/pxr.h"
13 #include "pxr/usd/pcp/api.h"
14 #include "pxr/usd/pcp/errors.h"
17 #include "pxr/usd/sdf/layerTree.h"
19 
20 #include <tbb/spin_mutex.h>
21 #include <iosfwd>
22 #include <memory>
23 #include <string>
24 #include <unordered_set>
25 #include <vector>
26 
28 
31 TF_DECLARE_WEAK_AND_REF_PTRS(Pcp_LayerStackRegistry);
32 
33 class ArResolverContext;
34 class Pcp_LayerStackRegistry;
35 class Pcp_MutedLayers;
38 class PcpLifeboat;
39 
40 /// \class PcpLayerStack
41 ///
42 /// Represents a stack of layers that contribute opinions to composition.
43 ///
44 /// Each PcpLayerStack is identified by a PcpLayerStackIdentifier. This
45 /// identifier contains all of the parameters needed to construct a layer stack,
46 /// such as the root layer, session layer, and path resolver context.
47 ///
48 /// PcpLayerStacks are constructed and managed by a Pcp_LayerStackRegistry.
49 ///
50 class PcpLayerStack : public TfRefBase, public TfWeakBase {
51  PcpLayerStack(const PcpLayerStack&) = delete;
52  PcpLayerStack& operator=(const PcpLayerStack&) = delete;
53 
54 public:
55  // See Pcp_LayerStackRegistry for creating layer stacks.
56  PCP_API
57  virtual ~PcpLayerStack();
58 
59  /// Returns the identifier for this layer stack.
60  PCP_API
62 
63  /// Return true if this layer stack is in USD mode.
64  bool IsUsd() const {
65  return _isUsd;
66  };
67 
68  /// Returns the layers in this layer stack in strong-to-weak order.
69  /// Note that this is only the *local* layer stack -- it does not
70  /// include any layers brought in by references inside prims.
71  PCP_API
72  const SdfLayerRefPtrVector& GetLayers() const;
73 
74  /// Returns only the session layers in the layer stack in strong-to-weak
75  /// order.
76  PCP_API
77  SdfLayerHandleVector GetSessionLayers() const;
78 
79  /// Returns the layer tree representing the structure of the non-session
80  /// layers in the layer stack.
81  PCP_API
82  const SdfLayerTreeHandle& GetLayerTree() const;
83 
84  /// Returns the layer tree representing the structure of the session
85  /// layers in the layer stack or null if there are no session layers.
86  PCP_API
88 
89  /// Returns the layer offset for the given layer, or NULL if the layer
90  /// can't be found or is the identity.
91  PCP_API
92  const SdfLayerOffset* GetLayerOffsetForLayer(const SdfLayerHandle&) const;
93 
94  /// Return the layer offset for the given layer, or NULL if the layer
95  /// can't be found or is the identity.
96  PCP_API
98 
99  /// Returns the layer offset for the layer at the given index in this
100  /// layer stack. Returns NULL if the offset is the identity.
101  PCP_API
102  const SdfLayerOffset* GetLayerOffsetForLayer(size_t layerIdx) const;
103 
104  /// Returns the set of layers that were muted in this layer
105  /// stack.
106  PCP_API
107  const std::set<std::string>& GetMutedLayers() const;
108 
109  /// Return the list of errors local to this layer stack.
111  return _localErrors ? *_localErrors.get() : PcpErrorVector();
112  }
113 
114  /// Returns true if this layer stack contains the given layer, false
115  /// otherwise.
116  PCP_API
117  bool HasLayer(const SdfLayerHandle& layer) const;
118  PCP_API
119  bool HasLayer(const SdfLayerRefPtr& layer) const;
120 
121  /// Return the composed expression variables for this layer stack.
123  { return *_expressionVariables; }
124 
125  /// Return the set of expression variables used during the computation
126  /// of this layer stack. For example, this may include the variables
127  /// used in expression variable expressions in sublayer asset paths.
128  const std::unordered_set<std::string>&
130  { return _expressionVariableDependencies; }
131 
132  /// Return the time codes per second value of the layer stack. This is
133  /// usually the same as the computed time codes per second of the root layer
134  /// but may be computed from the session layer when its present.
135  double GetTimeCodesPerSecond() const { return _timeCodesPerSecond; }
136 
137  /// Returns relocation source-to-target mapping for this layer stack.
138  ///
139  /// This map combines the individual relocation entries found across
140  /// all layers in this layer stack; multiple entries that affect a single
141  /// prim will be combined into a single entry. For instance, if this
142  /// layer stack contains relocations { /A: /B } and { /A/C: /A/D }, this
143  /// map will contain { /A: /B } and { /B/C: /B/D }. This allows consumers
144  /// to go from unrelocated namespace to relocated namespace in a single
145  /// step.
146  PCP_API
148 
149  /// Returns relocation target-to-source mapping for this layer stack.
150  ///
151  /// See GetRelocatesSourceToTarget for more details.
152  PCP_API
154 
155  /// Returns incremental relocation source-to-target mapping for this layer
156  /// stack.
157  ///
158  /// This map contains the individual relocation entries found across
159  /// all layers in this layer stack; it does not combine ancestral
160  /// entries with descendant entries. For instance, if this
161  /// layer stack contains relocations { /A: /B } and { /A/C: /A/D }, this
162  /// map will contain { /A: /B } and { /A/C: /A/D }.
163  PCP_API
165 
166  /// Returns incremental relocation target-to-source mapping for this layer
167  /// stack.
168  ///
169  /// See GetIncrementalRelocatesTargetToSource for more details.
170  PCP_API
172 
173  /// Returns a list of paths to all prims across all layers in this
174  /// layer stack that contained relocates.
175  PCP_API
177 
178  /// Apply the changes in \p changes. This blows caches. It's up to
179  /// the client to pull on those caches again as needed.
180  ///
181  /// Objects that are no longer needed and would be destroyed are
182  /// retained in \p lifeboat and won't be destroyed until \p lifeboat is
183  /// itself destroyed. This gives the client control over the timing
184  /// of the destruction of those objects. Clients may choose to pull
185  /// on the caches before destroying \p lifeboat. That may cause the
186  /// caches to again retain the objects, meaning they won't be destroyed
187  /// when \p lifeboat is destroyed.
188  ///
189  /// For example, if blowing a cache means an SdfLayer is no longer
190  /// needed then \p lifeboat will hold an SdfLayerRefPtr to that layer.
191  /// The client can then pull on that cache, which could cause the
192  /// cache to hold an SdfLayerRefPtr to the layer again. If so then
193  /// destroying \p changes will not destroy the layer. In any case,
194  /// we don't destroy the layer and then read it again. However, if
195  /// the client destroys \p lifeboat before pulling on the cache then
196  /// we would destroy the layer then read it again.
197  PCP_API
198  void Apply(const PcpLayerStackChanges& changes, PcpLifeboat* lifeboat);
199 
200  /// Return a PcpMapExpression representing the relocations that affect
201  /// namespace at and below the given path. The value of this
202  /// expression will continue to track the effective relocations if
203  /// they are changed later. In USD mode only, this will return a null
204  /// expression if there are no relocations on this layer stack.
205  PCP_API
207 
208  /// Return true if there are any relocated prim paths in this layer
209  /// stack.
210  PCP_API
211  bool HasRelocates() const;
212 
213 private:
214  // Only a registry can create a layer stack.
216  // PcpCache needs access to check the _registry.
217  friend class PcpCache;
218  // Needs access to _sublayerSourceInfo
219  friend bool Pcp_NeedToRecomputeDueToAssetPathChange(const PcpLayerStackPtr&);
220 
221  // Construct a layer stack for the given \p identifier that will be
222  // installed into \p registry. This installation is managed by
223  // \p registry and does not occur within the c'tor. See comments on
224  // _registry for more details.
225  PcpLayerStack(const PcpLayerStackIdentifier &identifier,
226  const Pcp_LayerStackRegistry &registry);
227 
228  void _BlowLayers();
229  void _BlowRelocations();
230  void _Compute(const std::string &fileFormatTarget,
231  const Pcp_MutedLayers &mutedLayers);
232 
233  SdfLayerTreeHandle _BuildLayerStack(
234  const SdfLayerHandle & layer,
235  const SdfLayerOffset & offset,
236  double layerTcps,
237  const ArResolverContext & pathResolverContext,
238  const SdfLayer::FileFormatArguments & layerArgs,
239  const std::string & sessionOwner,
240  const Pcp_MutedLayers & mutedLayers,
241  SdfLayerHandleSet *seenLayers,
242  PcpErrorVector *errors);
243 
244 private:
245  /// The identifier that uniquely identifies this layer stack.
246  const PcpLayerStackIdentifier _identifier;
247 
248  /// The registry (1:1 with a PcpCache) this layer stack belongs to. This
249  /// may not be set, particularly when a registry is creating a layer stack
250  /// but before it's been installed in the registry.
251  Pcp_LayerStackRegistryPtr _registry;
252 
253  /// Data representing the computed layer stack contents.
254  ///
255  /// This is built by examining the session and root layers for
256  /// sublayers, resolving their asset paths with the path resolver context,
257  /// and recursively building up the layer stack.
258  ///
259  /// Note that this is only the *local* layer stack -- it does not
260  /// include any layers brought in by references inside prims.
261 
262  /// Retained references to the layers in the stack,
263  /// in strong-to-weak order.
264  SdfLayerRefPtrVector _layers;
265 
266  /// The corresponding map functions for each entry in 'layers'.
267  /// Each map function contains a time offset that should be applied
268  /// to its corresponding layer.
269  std::vector<PcpMapFunction> _mapFunctions;
270 
271  /// Stores the computed time codes per second value of the layer stack which
272  /// has some special logic when a session layer is present.
273  double _timeCodesPerSecond;
274 
275  /// The tree structure of the layer stack.
276  /// Stored separately because this is needed only occasionally.
277  SdfLayerTreeHandle _layerTree;
278 
279  /// The tree structure of the session layer stack.
280  /// Stored separately because this is needed only occasionally.
281  SdfLayerTreeHandle _sessionLayerTree;
282 
283  /// Tracks information used to compute sublayer asset paths.
284  struct _SublayerSourceInfo {
285  _SublayerSourceInfo() = default;
286  _SublayerSourceInfo(
287  const SdfLayerHandle& layer_,
288  const std::string& authoredSublayerPath_,
289  const std::string& computedSublayerPath_)
290  : layer(layer_)
291  , authoredSublayerPath(authoredSublayerPath_)
292  , computedSublayerPath(computedSublayerPath_) { }
293 
294  SdfLayerHandle layer;
295  std::string authoredSublayerPath;
296  std::string computedSublayerPath;
297  };
298 
299  /// List of source info for sublayer asset path computations.
300  std::vector<_SublayerSourceInfo> _sublayerSourceInfo;
301 
302  /// Set of asset paths that were muted in this layer stack.
303  std::set<std::string> _mutedAssetPaths;
304 
305  /// The errors, if any, discovered while computing this layer stack.
306  /// NULL if no errors were found (the expected common case).
307  std::unique_ptr<PcpErrorVector> _localErrors;
308 
309  /// Pre-computed table of local relocates.
310  SdfRelocatesMap _relocatesSourceToTarget;
311  SdfRelocatesMap _relocatesTargetToSource;
312  SdfRelocatesMap _incrementalRelocatesSourceToTarget;
313  SdfRelocatesMap _incrementalRelocatesTargetToSource;
314 
315  /// A map of PcpMapExpressions::Variable instances used to represent
316  /// the current value of relocations given out by
317  /// GetExpressionForRelocatesAtPath(). This map is used to update
318  /// those values when relocations change.
320  SdfPath::FastLessThan> _RelocatesVarMap;
321  _RelocatesVarMap _relocatesVariables;
322  tbb::spin_mutex _relocatesVariablesMutex;
323 
324  /// List of all prim spec paths where relocations were found.
325  SdfPathVector _relocatesPrimPaths;
326 
327  /// Composed expression variables.
328  std::shared_ptr<PcpExpressionVariables> _expressionVariables;
329 
330  /// Set of expression variables this layer stack depends on.
331  std::unordered_set<std::string> _expressionVariableDependencies;
332 
333  bool _isUsd;
334 };
335 
336 PCP_API
337 std::ostream& operator<<(std::ostream&, const PcpLayerStackPtr&);
338 PCP_API
339 std::ostream& operator<<(std::ostream&, const PcpLayerStackRefPtr&);
340 
341 /// Returns true if negative layer offsets and scales are allowed.
342 ///
343 /// Negative layer offset scales are deprecated and a warning will be issued
344 /// when commulative scale during composition is negative with
345 /// PCP_ALLOW_NEGATIVE_LAYER_OFFSET_SCALE is set to true (default right now).
346 /// If PCP_ALLOW_NEGATIVE_LAYER_OFFSET_SCALE is set to false, a coding error
347 /// will be issued when a negative scale is encountered.
348 PCP_API
350 
351 /// Checks if the source and target paths constitute a valid relocates. This
352 /// validation is not context specific, i.e. if this returns false, the
353 /// combination of source and target paths is always invalid for any attempted
354 /// relocation.
355 bool
357  const SdfPath &source, const SdfPath &target, std::string *errorMessage);
358 
359 /// Builds a relocates map from a list of layer and SdfRelocates value pairs.
360 void
362  const std::vector<std::pair<SdfLayerHandle, SdfRelocates>> &layerRelocates,
363  SdfRelocatesMap *relocatesMap,
364  PcpErrorVector *errors);
365 
366 /// Compose the relocation arcs in the given stack of layers,
367 /// putting the results into the given sourceToTarget and targetToSource
368 /// maps.
369 void
371  const PcpLayerStack &layerStack,
372  SdfRelocatesMap *relocatesSourceToTarget,
373  SdfRelocatesMap *relocatesTargetToSource,
374  SdfRelocatesMap *incrementalRelocatesSourceToTarget,
375  SdfRelocatesMap *incrementalRelocatesTargetToSource,
376  SdfPathVector *relocatesPrimPaths,
377  PcpErrorVector *errors);
378 
379 // Returns true if \p layerStack should be recomputed due to changes to
380 // any computed asset paths that were used to find or open layers
381 // when originally composing \p layerStack. This may be due to scene
382 // description changes or external changes to asset resolution that
383 // may affect the computation of those asset paths.
384 bool
385 Pcp_NeedToRecomputeDueToAssetPathChange(const PcpLayerStackPtr& layerStack);
386 
387 // Returns true if the \p layerStack should be recomputed because
388 // \p changedLayer has had changes that would cause the layer stack to have
389 // a different computed overall time codes per second value.
390 bool
392  const PcpLayerStackPtr& layerStack, const SdfLayerHandle &changedLayer);
393 
395 
396 #endif // PXR_USD_PCP_LAYER_STACK_H
Definition: layer.h:81
virtual PCP_API ~PcpLayerStack()
PCP_API const SdfRelocatesMap & GetRelocatesTargetToSource() const
PCP_API const SdfLayerOffset * GetLayerOffsetForLayer(const SdfLayerHandle &) const
PCP_API std::ostream & operator<<(std::ostream &, const PcpLayerStackPtr &)
void Pcp_BuildRelocateMap(const std::vector< std::pair< SdfLayerHandle, SdfRelocates >> &layerRelocates, SdfRelocatesMap *relocatesMap, PcpErrorVector *errors)
Builds a relocates map from a list of layer and SdfRelocates value pairs.
PCP_API const PcpLayerStackIdentifier & GetIdentifier() const
Returns the identifier for this layer stack.
friend bool Pcp_NeedToRecomputeDueToAssetPathChange(const PcpLayerStackPtr &)
std::map< SdfPath, SdfPath > SdfRelocatesMap
A map of source SdfPaths to target SdfPaths for relocation.
Definition: types.h:267
PCP_API SdfLayerHandleVector GetSessionLayers() const
PCP_API const SdfRelocatesMap & GetIncrementalRelocatesSourceToTarget() const
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
std::map< std::string, std::string > FileFormatArguments
Definition: layer.h:107
PCP_API const SdfRelocatesMap & GetRelocatesSourceToTarget() const
Definition: cache.h:76
std::set< SdfHandleTo< SdfLayer >::Handle > SdfLayerHandleSet
PCP_API void Apply(const PcpLayerStackChanges &changes, PcpLifeboat *lifeboat)
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_REF_PTRS(SdfLayer)
friend class Pcp_LayerStackRegistry
Definition: layerStack.h:215
TF_DECLARE_WEAK_AND_REF_PTRS(PcpLayerStack)
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
PCP_API const SdfRelocatesMap & GetIncrementalRelocatesTargetToSource() const
void Pcp_ComputeRelocationsForLayerStack(const PcpLayerStack &layerStack, SdfRelocatesMap *relocatesSourceToTarget, SdfRelocatesMap *relocatesTargetToSource, SdfRelocatesMap *incrementalRelocatesSourceToTarget, SdfRelocatesMap *incrementalRelocatesTargetToSource, SdfPathVector *relocatesPrimPaths, PcpErrorVector *errors)
GLintptr offset
Definition: glcorearb.h:665
std::vector< TfRefPtr< SdfLayer > > SdfLayerRefPtrVector
std::vector< class SdfPath > SdfPathVector
double GetTimeCodesPerSecond() const
Definition: layerStack.h:135
PCP_API const SdfPathVector & GetPathsToPrimsWithRelocates() const
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:803
PCP_API bool PcpNegativeLayerOffsetScaleAllowed()
GLenum target
Definition: glcorearb.h:1667
PcpErrorVector GetLocalErrors() const
Return the list of errors local to this layer stack.
Definition: layerStack.h:110
bool Pcp_NeedToRecomputeDueToAssetPathChange(const PcpLayerStackPtr &layerStack)
std::unique_ptr< Variable > VariableUniquePtr
Variables are held by reference.
Definition: mapExpression.h:97
Definition: path.h:273
const std::unordered_set< std::string > & GetExpressionVariableDependencies() const
Definition: layerStack.h:129
std::vector< PcpErrorBasePtr > PcpErrorVector
Definition: errors.h:65
PCP_API bool HasLayer(const SdfLayerHandle &layer) const
PCP_API const SdfLayerTreeHandle & GetSessionLayerTree() const
PCP_API bool HasRelocates() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
PCP_API const SdfLayerRefPtrVector & GetLayers() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const PcpExpressionVariables & GetExpressionVariables() const
Return the composed expression variables for this layer stack.
Definition: layerStack.h:122
bool IsUsd() const
Return true if this layer stack is in USD mode.
Definition: layerStack.h:64
PCP_API const std::set< std::string > & GetMutedLayers() const
PCP_API PcpMapExpression GetExpressionForRelocatesAtPath(const SdfPath &path)
bool Pcp_NeedToRecomputeLayerStackTimeCodesPerSecond(const PcpLayerStackPtr &layerStack, const SdfLayerHandle &changedLayer)
PCP_API const SdfLayerTreeHandle & GetLayerTree() const
bool Pcp_IsValidRelocatesEntry(const SdfPath &source, const SdfPath &target, std::string *errorMessage)
#define PCP_API
Definition: api.h:23