HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XUSD_Data.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Side Effects Software Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef __HUSD_Data_h__
19 #define __HUSD_Data_h__
20 
21 #include "HUSD_API.h"
22 #include "HUSD_DataHandle.h"
23 #include "HUSD_PostLayers.h"
24 #include "HUSD_Overrides.h"
25 #include "XUSD_DataLock.h"
26 #include "XUSD_LockedGeo.h"
27 #include "XUSD_PathSet.h"
28 #include "XUSD_RootLayerData.h"
29 #include <UT/UT_Color.h>
30 #include <UT/UT_StringArray.h>
31 #include <UT/UT_StringHolder.h>
32 #include <UT/UT_StringSet.h>
33 #include <UT/UT_IntrusivePtr.h>
34 #include <UT/UT_SharedPtr.h>
35 #include <UT/UT_UniquePtr.h>
36 #include <pxr/usd/usd/stage.h>
39 #include <pxr/usd/sdf/path.h>
40 
42 
43 // Control the addLayers method. Most of these values only affect the version
44 // of addLayer which takes a vector of file paths (instead of layers pointers).
46 {
47  // Simply add the layers "as-is" to the root layer's list of sublayers,
48  // using the layer's identifier or the provided file paths. The data
49  // handle's active layer is set to be created after all the added layers.
51  // Each layer is copied into an anonymous layer, and these anonymous
52  // layers get added to the sublayer list. This means these layers will
53  // we saved as new USD files during a save process. The last layer becomes
54  // the data handle's active layer for following edits.
56  // All layers are added "as-is", except the last layer in the list, which
57  // is copied to an anonymous layer. This last anonymous layer becomes the
58  // data handle's active layer for following edits.
60 
61  // Used only by HUSD_Stitch, layers authored by LOP nodes all get copied
62  // to new anonymous layers (so they can be safely modified when stitching
63  // in the next time sample of data, if there is one.). If the last layer
64  // is a LOP layer, it becomes the active layer modified by following LOP
65  // nodes.
67  // Used only by HUSD_Stitch, if the last layer was authored by LOP nodes,
68  // it gets copied to a new anonymous layer (so it can be modified by
69  // following LOP nodes as the active layer). Preceding layers are not
70  // copied, which is fine if this is the last time sample that will be
71  // stitched into this data handle.
73 };
74 
76 {
77 public:
79  explicit XUSD_LayerAtPath(const SdfLayerRefPtr &layer,
81  int layer_badge_index = 0);
82  explicit XUSD_LayerAtPath(const SdfLayerRefPtr &layer,
83  const std::string &filepath,
85  int layer_badge_index = 0);
86 
87  bool hasLayerColorIndex(int &clridx) const;
88  bool isLopLayer() const;
89 
96 };
97 
99 
101 {
102 public:
103  XUSD_OverridesInfo(const UsdStageRefPtr &stage);
105 
106  bool isEmpty() const
107  { return !myReadOverrides && !myWriteOverrides; }
108 
113 };
114 
116 {
117 public:
118  XUSD_PostLayersInfo(const UsdStageRefPtr &stage);
120 
121  bool isEmpty() const
122  { return !myPostLayers; }
123 
127 };
128 
129 class HUSD_API XUSD_Data : public UT_IntrusiveRefCounter<XUSD_Data>,
130  public UT_NonCopyable
131 {
132 public:
133  XUSD_Data(HUSD_MirroringType mirroring);
134  ~XUSD_Data();
135 
136  // Return true if our stage value is set and has a valid root prim.
137  bool isStageValid() const;
138  // Returns our stage. Should only be accessed when this data is locked.
139  UsdStageRefPtr stage() const;
140  // Returns the active layer on our composed stage.
141  SdfLayerRefPtr activeLayer() const;
142  // Return the on-stage identifiers of any layers that are marked in the
143  // source layers array to be removed due to a layer break.
144  std::set<std::string> getStageLayersToRemoveFromLayerBreak() const;
145  // Creates a layer by flattening all our source layers together. Also
146  // strips out any layers tagged by a Layer Break LOP.
147  SdfLayerRefPtr createFlattenedLayer(
148  HUSD_StripLayerResponse response) const;
149  // Creates a layer by flattening a stage consisting of our source layers
150  // after stripping out any layers tagged by a Layer Break LOP.
151  SdfLayerRefPtr createFlattenedStage(
152  HUSD_StripLayerResponse response) const;
153 
154  // Return the array of source layers that are combined to make our stage.
155  const XUSD_LayerAtPathArray &sourceLayers() const;
156  // Return the current session layer overrides set on our stage.
157  const HUSD_ConstOverridesPtr&overrides() const;
158  // Return the current session layer post layers set on our stage.
159  const HUSD_ConstPostLayersPtr&postLayers() const;
160  // Return a specific session layer object on our stage.
161  const SdfLayerRefPtr &sessionLayer(HUSD_OverridesLayerId id) const;
162  // Return the current load masks set on our stge.
163  const HUSD_LoadMasksPtr &loadMasks() const;
164  // Return the identifier of our stage's root layer. This can be used as
165  // a quick check as to whether we have create a brand new stage.
166  const std::string &rootLayerIdentifier() const;
167 
168  // Add a layer using a file path, layer pointer, or an empty layer.
169  // Files and layer pointers can be inserted at any position in the
170  // sublayer list (0 is strongest, -1 is weakest). The layer op value
171  // indicates whether the layer should be editable, which implies that
172  // it must be added to the strongest position (the only place where a
173  // layer can be edited).
174  bool addLayer(const std::string &filepath,
175  const SdfLayerOffset &offset,
176  int position,
177  XUSD_AddLayerOp add_layer_op,
178  bool copy_root_prim_metadata);
179  bool addLayer(const XUSD_LayerAtPath &layer,
180  int position,
181  XUSD_AddLayerOp add_layer_op,
182  bool copy_root_prim_metadata);
183  // Methods for adding a bunch of layers to our stage in one call. These
184  // methods only unlock and re-lock the data once (meaning only a single
185  // recomposition is required). The resulting behavior of this call is the
186  // same as calling addLayer on each individual layer in order.
187  bool addLayers(
188  const std::vector<std::string> &paths,
189  const std::vector<bool> &above_breaks,
191  int position,
192  XUSD_AddLayerOp add_layer_op,
193  bool copy_root_prim_metadata);
194  bool addLayers(
195  const std::vector<std::string> &paths,
196  const SdfLayerOffsetVector &offsets,
197  int position,
198  XUSD_AddLayerOp add_layer_op,
199  bool copy_root_prim_metadata);
200  bool addLayers(const XUSD_LayerAtPathArray &layers,
201  int position,
202  XUSD_AddLayerOp add_layer_op,
203  bool copy_root_prim_metadata);
204  // Add a single new empty layer.
205  bool addLayer();
206 
207  // Remove one or more of our source layers.
208  bool removeLayers(
209  const std::set<std::string> &filepaths);
210 
211  // Apply a layer break, which tags all existing layers, and adds a new
212  // empty layer for holding future modification.
213  bool applyLayerBreak();
214 
215  // Set a piece of metadata on the root prim of the root layer of the
216  // stage. Also record this change to myRootLayerData so that we can
217  // restore this metadata value if we lock this stage to another state
218  // then return to this state.
219  void setStageRootPrimMetadata(const TfToken &field,
220  const VtValue &value);
221  void setStageRootLayerData(
223  &rootlayerdata);
224  void setStageRootLayerData(
225  const SdfLayerRefPtr &layer);
226 
227  // Store a lockedgeo in with this data to keep alive cooked sop data in the
228  // XUSD_LockedGeoRegistry as long as it might be referenced by our stage.
229  void addLockedGeo(
230  const XUSD_LockedGeoPtr &lockedgeo);
231  void addLockedGeos(
232  const XUSD_LockedGeoArray &lockedgeos);
233  const XUSD_LockedGeoArray &lockedGeos() const;
234 
235  // Store pointers to arrays that were created automatically as part of a
236  // process of replacing a layer on disk with an anonymous layer.
237  void addReplacements(
238  const XUSD_LayerArray &replacements);
239  const XUSD_LayerArray &replacements() const;
240 
241  // Store a locked stage with this data to keep alive cooked lop data in the
242  // HUSD_LockedStageRegistry as long as it might be referenced by our stage.
243  void addLockedStage(
244  const HUSD_LockedStagePtr &stage);
245  void addLockedStages(
247  const HUSD_LockedStageArray &lockedStages() const;
248 
249 private:
250  void reset();
251  void createNewData(const HUSD_LoadMasksPtr &load_masks,
252  int resolver_context_nodeid,
253  const UsdStageWeakPtr &resolver_context_stage,
254  const ArResolverContext *resolver_context);
255  void createHardCopy(const XUSD_Data &src);
256  void createSoftCopy(const XUSD_Data &src,
257  const HUSD_LoadMasksPtr &load_masks,
258  bool make_new_implicit_layer);
259  void createCopyWithReplacement(
260  const XUSD_Data &src,
261  const UT_StringRef &frompath,
262  const UT_StringRef &topath,
263  int nodeid,
264  HUSD_MakeNewPathFunc make_new_path,
265  UT_StringSet &replaced_layers);
266 
267  // Return the resolver context for our stage. Note this this method does
268  // not require that the stage be locked, becaue it is unchanging data set
269  // on the stage when it was created.
270  ArResolverContext resolverContext() const;
271 
272  // Return a layer created by flattening all source layers up to the
273  // strongest layer excluded by a Layer Break node.
274  void flattenLayers(const XUSD_Data &src,
275  int creator_node_id);
276  // Return a layer created by flattening the stage composed of all source
277  // layers up to the strongest layer excluded by a Layer Break node.
278  void flattenStage(const XUSD_Data &src,
279  int creator_node_id);
280  // Utility method used by the two flatten methods above to separate all
281  // layers preceding a layer break point. Optionally creates a warning if
282  // a layer break is found.
283  UsdStageRefPtr getOrCreateStageForFlattening(
284  HUSD_StripLayerResponse response,
285  UsdStage::InitialLoadSet loadset) const;
286 
287  void mirror(const XUSD_Data &src,
288  const HUSD_LoadMasks &load_masks);
289  bool mirrorUpdateRootLayer(
290  const HUSD_MirrorRootLayer &rootlayer);
291 
292  void afterLock(bool for_write,
294  &read_overrides = HUSD_ConstOverridesPtr(),
295  const HUSD_OverridesPtr
296  &write_overrides = HUSD_OverridesPtr(),
298  &postlayers = HUSD_ConstPostLayersPtr(),
299  bool remove_layer_breaks = false);
300  XUSD_LayerPtr editActiveSourceLayer(bool create_change_block);
301  void createInitialPlaceholderSublayers();
302  void applyRootLayerDataToStage();
303  void afterRelease();
304 
305  static void exitCallback(void *);
306 
307  UsdStageRefPtr myStage;
308  UT_SharedPtr<UT_StringArray> myStageLayerAssignments;
309  UT_SharedPtr<XUSD_LayerArray> myStageLayers;
310  UT_SharedPtr<int> myStageLayerCount;
311  UT_SharedPtr<XUSD_OverridesInfo> myOverridesInfo;
312  UT_SharedPtr<XUSD_PostLayersInfo> myPostLayersInfo;
313  UT_SharedPtr<XUSD_RootLayerData> myRootLayerData;
314  XUSD_LayerAtPathArray mySourceLayers;
315  HUSD_LoadMasksPtr myLoadMasks;
316  XUSD_DataLockPtr myDataLock;
317  XUSD_LockedGeoArray myLockedGeoArray;
318  XUSD_LayerArray myReplacementLayerArray;
319  HUSD_LockedStageArray myLockedStages;
320  HUSD_MirroringType myMirroring;
321  UsdStageLoadRules myMirrorLoadRules;
322  bool myMirrorLoadRulesChanged;
323  int myActiveLayerIndex;
324  bool myOwnsActiveLayer;
325 
326  friend class ::HUSD_DataHandle;
327 };
328 
329 class HUSD_API XUSD_Layer : public UT_IntrusiveRefCounter<XUSD_Layer>
330 {
331 public:
333  bool create_change_block)
334  : myLayer(layer),
335  myChangeBlock(nullptr)
336  {
337  if (create_change_block)
338  myChangeBlock.reset(
339  new SdfChangeBlock());
340  }
342  { }
343 
344  const SdfLayerRefPtr &layer() const
345  { return myLayer; }
346 
347 private:
348  SdfLayerRefPtr myLayer;
349  UT_UniquePtr<SdfChangeBlock> myChangeBlock;
350 };
351 
353 
354 #endif
355 
std::string myIdentifier
Definition: XUSD_Data.h:91
const SdfLayerRefPtr & layer() const
Definition: XUSD_Data.h:344
Definition: XUSD_Data.h:115
SdfLayerOffset myOffset
Definition: XUSD_Data.h:92
UT_SharedPtr< HUSD_LoadMasks > HUSD_LoadMasksPtr
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1298
GLbitfield stages
Definition: glcorearb.h:1930
HUSD_MirroringType
HUSD_OverridesPtr myWriteOverrides
Definition: XUSD_Data.h:110
exint myVersionId
Definition: XUSD_Data.h:126
bool isLopLayer() const
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glcorearb.h:2620
UT_IntrusivePtr< const HUSD_Overrides > HUSD_ConstOverridesPtr
int64 exint
Definition: SYS_Types.h:125
#define HUSD_API
Definition: HUSD_API.h:32
XUSD_Layer(const SdfLayerRefPtr &layer, bool create_change_block)
Definition: XUSD_Data.h:332
bool isEmpty() const
Definition: XUSD_Data.h:106
InitialLoadSet
Definition: stage.h:158
GLenum src
Definition: glcorearb.h:1792
A reference counter base class for use with UT_IntrusivePtr.
UT_IntrusivePtr< const HUSD_PostLayers > HUSD_ConstPostLayersPtr
UT_IntrusivePtr< HUSD_Overrides > HUSD_OverridesPtr
bool myLayerIsMissingFile
Definition: XUSD_Data.h:95
std::function< UT_StringHolder(const UT_StringRef &oldpath)> HUSD_MakeNewPathFunc
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:33
HUSD_OverridesLayerId
Definition: HUSD_Utils.h:70
std::vector< SdfLayerOffset > SdfLayerOffsetVector
Definition: layerOffset.h:155
Definition: token.h:87
HUSD_ConstOverridesPtr myReadOverrides
Definition: XUSD_Data.h:109
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:30
std::vector< TfRefPtr< SdfLayer > > SdfLayerRefPtrVector
~XUSD_PostLayersInfo()
#define HUSD_OVERRIDES_NUM_LAYERS
Definition: HUSD_Utils.h:76
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
HUSD_ConstPostLayersPtr myPostLayers
Definition: XUSD_Data.h:124
HUSD_StripLayerResponse
Definition: HUSD_Utils.h:86
bool isEmpty() const
Definition: XUSD_Data.h:121
GLboolean reset
Definition: glew.h:4989
bool hasLayerColorIndex(int &clridx) const
UsdStagePtr UsdStageWeakPtr
Definition: common.h:55
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
UT_Array< XUSD_LayerAtPath > XUSD_LayerAtPathArray
Definition: XUSD_Data.h:98
XUSD_AddLayerOp
Definition: XUSD_Data.h:45
UT_SharedPtr< HUSD_LockedStage > HUSD_LockedStagePtr
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
SdfLayerRefPtr mySessionLayers[HUSD_OVERRIDES_NUM_LAYERS]
Definition: XUSD_Data.h:111
SdfLayerRefPtr myLayer
Definition: XUSD_Data.h:90
GLsizei const GLfloat * value
Definition: glcorearb.h:823
XUSD_PostLayersInfo(const UsdStageRefPtr &stage)
bool myRemoveWithLayerBreak
Definition: XUSD_Data.h:94
GLintptr offset
Definition: glcorearb.h:664
SdfLayerRefPtrVector mySessionLayers
Definition: XUSD_Data.h:125
GLenum GLsizei GLsizei GLsizei GLsizei layers
Definition: glew.h:2878
XUSD_OverridesInfo(const UsdStageRefPtr &stage)
Definition: value.h:168
int myLayerColorIndex
Definition: XUSD_Data.h:93
GLenum const void * paths
Definition: glew.h:13872