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_LAYER_STACK_REGISTRY_H
25 #define PXR_USD_PCP_LAYER_STACK_REGISTRY_H
26 
27 /// \file pcp/layerStackRegistry.h
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/pcp/errors.h"
31 #include "pxr/base/tf/declarePtrs.h"
32 #include "pxr/base/tf/functionRef.h"
33 #include "pxr/base/tf/refBase.h"
34 
35 #include <memory>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 TF_DECLARE_WEAK_AND_REF_PTRS(PcpLayerStack);
40 TF_DECLARE_REF_PTRS(Pcp_LayerStackRegistry);
41 
42 class PcpLayerStackIdentifier;
43 class Pcp_LayerStackRegistryData;
44 class Pcp_MutedLayers;
45 
46 /// \class Pcp_LayerStackRegistry
47 ///
48 /// A registry of layer stacks.
49 ///
50 class Pcp_LayerStackRegistry : public TfRefBase, public TfWeakBase {
51 public:
52     /// Create a new Pcp_LayerStackRegistry.
53     static Pcp_LayerStackRegistryRefPtr New(
54         const std::string& fileFormatTarget = std::string(),
55         bool isUsd=false);
56 
57     /// Adds layers specified in \p layersToMute and removes layers
58     /// specified in \p layersToUnmute from the registry's set of muted
59     /// layers.  Any relative paths will be anchored to the given
60     /// \p anchorLayer. On completion, \p layersToMute and \p layersToUnmute
61     /// will be filled with the canonical identifiers for layers that were
62     /// actually added or removed.
63     void MuteAndUnmuteLayers(const SdfLayerHandle& anchorLayer,
64                              std::vector<std::string>* layersToMute,
65                              std::vector<std::string>* layersToUnmute);
66 
67     /// Returns the list of canonical identifiers for muted layers
68     /// in this cache.
69     const std::vector<std::string>& GetMutedLayers() const;
70 
71     /// Returns true if the layer identified by \p layerIdentifier is muted,
72     /// false otherwise.  If \p layerIdentifier is relative, \p anchorLayer
73     /// used to anchor the layer.  If this function returns true,
74     /// \p canonicalLayerIdentifier will be populated with the canonical
75     /// identifier for the muted layer.
76     bool IsLayerMuted(const SdfLayerHandle& anchorLayer,
77                       const std::string& layerIdentifier,
78                       std::string* canonicalLayerIdentifier = nullptr) const;
79 
80     /// Returns the layer stack for \p identifier if it exists, otherwise
81     /// creates a new layer stack for \p identifier.  This returns \c NULL
82     /// if \p identifier is invalid (i.e. its root layer is \c NULL).
83     PcpLayerStackRefPtr FindOrCreate(const PcpLayerStackIdentifier& identifier,
84                                      PcpErrorVector *allErrors);
85 
86     /// Returns the layer stack for \p identifier if it exists, otherwise
87     /// returns \c NULL.
88     PcpLayerStackPtr Find(const PcpLayerStackIdentifier&) const;
89 
90     /// Return true if this registry contains \p layerStack, false otherwise.
91     bool Contains(const PcpLayerStackPtr &layerStack) const;
92 
93     /// Returns every layer stack that includes \p layer.
94     const PcpLayerStackPtrVector&
95     FindAllUsingLayer(const SdfLayerHandle& layer) const;
96 
97     /// Returns every layer stack that uses the muted layer identified
98     /// \p layerId, which is assumed to be a canonical muted layer
99     /// identifier.
100     const PcpLayerStackPtrVector&
101     FindAllUsingMutedLayer(const std::string& layerId) const;
102 
103     /// Returns every layer stack known to this registry.
104     std::vector<PcpLayerStackPtr> GetAllLayerStacks() const;
105 
106     /// Runs \p fn on all layer stacks known to this registry.
107     void
108     ForEachLayerStack(const TfFunctionRef<void(const PcpLayerStackPtr&)>& fn);
109 
110 private:
111     /// Private constructor -- see New().
112     Pcp_LayerStackRegistry(const std::string& fileFormatTarget,
113                            bool isUsd);
114     ~Pcp_LayerStackRegistry();
115 
116     // Find that doesn't lock.
117     PcpLayerStackPtr _Find(const PcpLayerStackIdentifier&) const;
118 
119     // Remove the layer stack with the given identifier from the registry.
120     void _SetLayersAndRemove(const PcpLayerStackIdentifier&,
121                              const PcpLayerStack *);
122 
123     // Update the layer-stack-by-layer maps by setting the layers for the
124     // given layer stack.
125     void _SetLayers(const PcpLayerStack*);
126 
127     // Returns the file format target for layer stacks managed by this
128     // registry.
129     const std::string& _GetFileFormatTarget() const;
130 
131     // Returns whether or not we are in USD mode for avoiding
132     // extra calls such as Pcp_ComputeRelocationForLayerStack()
133     bool _IsUsd() const;
134 
135     // Returns the muted layer collection so that layer stack
136     // computation can easily query whether a layer is muted.
137     const Pcp_MutedLayers& _GetMutedLayers() const;
138 
139     // PcpLayerStack can access private _GetFileFormatTarget(),
140     // _SetLayersAndRemove(), and _SetLayers().
141     friend class PcpLayerStack;
142 
143 private:
144     std::unique_ptr<Pcp_LayerStackRegistryData> _data;
145 };
146 
147 /// \class Pcp_MutedLayers
148 ///
149 /// Helper for maintaining and querying a collection of muted layers.
150 ///
151 class Pcp_MutedLayers
152 {
153 public:
154     const std::vector<std::string>& GetMutedLayers() const;
155     void MuteAndUnmuteLayers(const SdfLayerHandle& anchorLayer,
156                              std::vector<std::string>* layersToMute,
157                              std::vector<std::string>* layersToUnmute);
158     bool IsLayerMuted(const SdfLayerHandle& anchorLayer,
159                       const std::string& layerIdentifier,
160                       std::string* canonicalLayerIdentifier = nullptr) const;
161 
162 private:
163     std::vector<std::string> _layers;
164 };
165 
166 PXR_NAMESPACE_CLOSE_SCOPE
167 
168 #endif // PXR_USD_PCP_LAYER_STACK_REGISTRY_H
169