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  explicit XUSD_Data(const UsdStageRefPtr &stage);
135  ~XUSD_Data();
136 
137  // Return true if our stage value is set and has a valid root prim.
138  bool isStageValid() const;
139  // Returns our stage. Should only be accessed when this data is locked.
140  UsdStageRefPtr stage() const;
141  // Returns the active layer on our composed stage.
142  SdfLayerRefPtr activeLayer() const;
143  // Return true if the active layer of this data will be returned as the
144  // active layer when a downstream copy of this data is locked for writing.
145  bool activeLayerIsReusable() const;
146  // Return the on-stage identifiers of any layers that are marked in the
147  // source layers array to be removed due to a layer break.
148  std::set<std::string> getStageLayersToRemoveFromLayerBreak() const;
149  // Creates a layer by flattening all our source layers together. Also
150  // strips out any layers tagged by a Layer Break LOP.
151  SdfLayerRefPtr createFlattenedLayer(
152  HUSD_StripLayerResponse response) const;
153  // Creates a layer by flattening a stage consisting of our source layers
154  // after stripping out any layers tagged by a Layer Break LOP.
155  SdfLayerRefPtr createFlattenedStage(
156  HUSD_StripLayerResponse response) const;
157 
158  // Return the array of source layers that are combined to make our stage.
159  const XUSD_LayerAtPathArray &sourceLayers() const;
160  // Return the current session layer overrides set on our stage.
161  const HUSD_ConstOverridesPtr&overrides() const;
162  // Return the current session layer post layers set on our stage.
163  const HUSD_ConstPostLayersPtr&postLayers() const;
164  // Return a specific session layer object on our stage.
165  const SdfLayerRefPtr &sessionLayer(HUSD_OverridesLayerId id) const;
166  // Return the current load masks set on our stage.
167  const HUSD_LoadMasksPtr &loadMasks() const;
168  // Return the identifier of our stage's root layer. This can be used as
169  // a quick check as to whether we have created a brand new stage.
170  const std::string &rootLayerIdentifier() const;
171 
172  // Add a layer using a file path, layer pointer, or an empty layer.
173  // Files and layer pointers can be inserted at any position in the
174  // sublayer list (0 is strongest, -1 is weakest). The layer op value
175  // indicates whether the layer should be editable, which implies that
176  // it must be added to the strongest position (the only place where a
177  // layer can be edited).
178  bool addLayer(const std::string &filepath,
179  const SdfLayerOffset &offset,
180  int position,
181  XUSD_AddLayerOp add_layer_op,
182  bool copy_root_prim_metadata);
183  bool addLayer(const XUSD_LayerAtPath &layer,
184  int position,
185  XUSD_AddLayerOp add_layer_op,
186  bool copy_root_prim_metadata);
187  // Methods for adding a bunch of layers to our stage in one call. These
188  // methods only unlock and re-lock the data once (meaning only a single
189  // recomposition is required). The resulting behavior of this call is the
190  // same as calling addLayer on each individual layer in order.
191  bool addLayers(
192  const std::vector<std::string> &paths,
193  const std::vector<bool> &above_breaks,
195  int position,
196  XUSD_AddLayerOp add_layer_op,
197  bool copy_root_prim_metadata);
198  bool addLayers(
199  const std::vector<std::string> &paths,
200  const SdfLayerOffsetVector &offsets,
201  int position,
202  XUSD_AddLayerOp add_layer_op,
203  bool copy_root_prim_metadata);
204  bool addLayers(const XUSD_LayerAtPathArray &layers,
205  int position,
206  XUSD_AddLayerOp add_layer_op,
207  bool copy_root_prim_metadata);
208  // Add a single new empty layer.
209  bool addLayer();
210 
211  // Remove one or more of our source layers.
212  bool removeLayers(
213  const std::set<std::string> &filepaths);
214 
215  // Used to do something roughyl equivalent to a mirror operation, but on
216  // a data handle not meant for mirroring.
217  bool replaceAllSourceLayers(
218  const XUSD_LayerAtPathArray &layers,
219  const XUSD_LockedGeoSet &locked_geos,
220  const XUSD_LayerSet &held_layers,
221  const XUSD_LayerSet &replacement_layers,
222  const HUSD_LockedStageSet &locked_stages,
224  &root_layer_data,
225  bool last_sublayer_is_editable);
226 
227  // Apply a layer break, which tags all existing layers, and adds a new
228  // empty layer for holding future modification.
229  bool applyLayerBreak();
230 
231  // Set a piece of metadata on the root prim of the root layer of the
232  // stage. Also record this change to myRootLayerData so that we can
233  // restore this metadata value if we lock this stage to another state
234  // then return to this state.
235  void setStageRootPrimMetadata(const TfToken &field,
236  const VtValue &value);
237  void setStageRootLayerData(
239  &rootlayerdata);
240  void setStageRootLayerData(
241  const SdfLayerRefPtr &layer);
242 
243  // Store a lockedgeo in with this data to keep alive cooked sop data in the
244  // XUSD_LockedGeoRegistry as long as it might be referenced by our stage.
245  void addLockedGeo(
246  const XUSD_LockedGeoPtr &lockedgeo);
247  void addLockedGeos(
248  const XUSD_LockedGeoSet &lockedgeos);
249  const XUSD_LockedGeoSet &lockedGeos() const;
250 
251  // Store a reference to a layer with this data, to keep it alive.
252  // Anonymous layers can be dropped by the stage in certain circumstances
253  // if there are no external references to them.
254  void addHeldLayer(
255  const SdfLayerRefPtr &layer);
256  void addHeldLayers(
257  const XUSD_LayerSet &layers);
258  const XUSD_LayerSet &heldLayers() const;
259 
260  // Store pointers to arrays that were created automatically as part of a
261  // process of replacing a layer on disk with an anonymous layer.
262  void addReplacements(
263  const XUSD_LayerSet &replacements);
264  const XUSD_LayerSet &replacements() const;
265 
266  // Store a locked stage with this data to keep alive cooked lop data in the
267  // HUSD_LockedStageRegistry as long as it might be referenced by our stage.
268  void addLockedStage(
269  const HUSD_LockedStagePtr &stage);
270  void addLockedStages(
271  const HUSD_LockedStageSet &stages);
272  const HUSD_LockedStageSet &lockedStages() const;
273 
274  // Clear out all mirrored stages completely. They must be rebuilt from
275  // scratch the next time they are asked to mirror a stage.
276  static void clearAllMirroredData();
277 
278 private:
279  void reset();
280  void createNewData(const HUSD_LoadMasksPtr &load_masks,
281  int resolver_context_nodeid,
282  const UsdStageWeakPtr &resolver_context_stage,
283  const ArResolverContext *resolver_context);
284  void createHardCopy(const XUSD_Data &src);
285  void createSoftCopy(const XUSD_Data &src,
286  const HUSD_LoadMasksPtr &load_masks,
287  bool make_new_implicit_layer);
288  void createCopyWithReplacement(
289  const XUSD_Data &src,
290  const UT_StringRef &frompath,
291  const UT_StringRef &topath,
292  int nodeid,
293  HUSD_MakeNewPathFunc make_new_path,
294  UT_StringSet &replaced_layers);
295 
296  // Return the resolver context for our stage. Note that this method does
297  // not require that the stage be locked, because it is unchanging data set
298  // on the stage when it was created.
299  ArResolverContext resolverContext() const;
300 
301  // Return a layer created by flattening all source layers up to the
302  // strongest layer excluded by a Layer Break node.
303  void flattenLayers(const XUSD_Data &src,
304  int creator_node_id);
305  // Return a layer created by flattening the stage composed of all source
306  // layers up to the strongest layer excluded by a Layer Break node.
307  void flattenStage(const XUSD_Data &src,
308  int creator_node_id);
309  // Utility method used by the two flatten methods above to separate all
310  // layers preceding a layer break point. Optionally creates a warning if
311  // a layer break is found.
312  UsdStageRefPtr getOrCreateStageForFlattening(
313  HUSD_StripLayerResponse response,
314  UsdStage::InitialLoadSet loadset) const;
315 
316  void mirror(const XUSD_Data &src,
317  const HUSD_LoadMasks &load_masks);
318  bool mirrorUpdateRootLayer(
319  const HUSD_MirrorRootLayer &rootlayer);
320 
321  void afterLock(bool for_write,
323  &read_overrides = HUSD_ConstOverridesPtr(),
324  const HUSD_OverridesPtr
325  &write_overrides = HUSD_OverridesPtr(),
327  &postlayers = HUSD_ConstPostLayersPtr(),
328  bool remove_layer_breaks = false);
329  XUSD_LayerPtr editActiveSourceLayer(bool create_change_block);
330  void createInitialPlaceholderSublayers();
331  void applyRootLayerDataToStage();
332  void afterRelease();
333 
334  static void exitCallback(void *);
335 
336  UsdStageRefPtr myStage;
337  UT_SharedPtr<UT_StringArray> myStageLayerAssignments;
338  UT_SharedPtr<XUSD_LayerArray> myStageLayers;
339  UT_SharedPtr<int> myStageLayerCount;
340  UT_SharedPtr<XUSD_OverridesInfo> myOverridesInfo;
341  UT_SharedPtr<XUSD_PostLayersInfo> myPostLayersInfo;
342  UT_SharedPtr<XUSD_RootLayerData> myRootLayerData;
343  XUSD_LayerAtPathArray mySourceLayers;
344  HUSD_LoadMasksPtr myLoadMasks;
345  XUSD_DataLockPtr myDataLock;
346  XUSD_LockedGeoSet myLockedGeos;
347  XUSD_LayerSet myReplacementLayers;
348  HUSD_LockedStageSet myLockedStages;
349  XUSD_LayerSet myHeldLayers;
350  HUSD_MirroringType myMirroring;
351  UsdStageLoadRules myMirrorLoadRules;
352  UT_StringMap<UT_StringArray> myMirrorVariantSelectionFallbacks;
353  bool myMirrorLoadRulesChanged;
354  int myActiveLayerIndex;
355  bool myOwnsActiveLayer;
356 
357  friend class ::HUSD_DataHandle;
358 };
359 
360 class HUSD_API XUSD_Layer : public UT_IntrusiveRefCounter<XUSD_Layer>
361 {
362 public:
364  bool create_change_block)
365  : myLayer(layer),
366  myChangeBlock(nullptr)
367  {
368  if (create_change_block)
369  myChangeBlock.reset(
370  new SdfChangeBlock());
371  }
373  { }
374 
375  const SdfLayerRefPtr &layer() const
376  { return myLayer; }
377 
378 private:
379  SdfLayerRefPtr myLayer;
380  UT_UniquePtr<SdfChangeBlock> myChangeBlock;
381 };
382 
384 
385 #endif
386 
std::string myIdentifier
Definition: XUSD_Data.h:91
const SdfLayerRefPtr & layer() const
Definition: XUSD_Data.h:375
Definition: XUSD_Data.h:115
SdfLayerOffset myOffset
Definition: XUSD_Data.h:92
UT_SharedPtr< HUSD_LoadMasks > HUSD_LoadMasksPtr
GLbitfield stages
Definition: glcorearb.h:1931
HUSD_MirroringType
HUSD_OverridesPtr myWriteOverrides
Definition: XUSD_Data.h:110
exint myVersionId
Definition: XUSD_Data.h:126
bool isLopLayer() const
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
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:363
bool isEmpty() const
Definition: XUSD_Data.h:106
InitialLoadSet
Definition: stage.h:165
A reference counter base class for use with UT_IntrusivePtr.
UsdStagePtr UsdStageWeakPtr
Definition: common.h:55
UT_IntrusivePtr< const HUSD_PostLayers > HUSD_ConstPostLayersPtr
UT_IntrusivePtr< HUSD_Overrides > HUSD_OverridesPtr
bool myLayerIsMissingFile
Definition: XUSD_Data.h:95
GLenum GLuint GLint GLint layer
Definition: glcorearb.h:1299
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
GLuint GLsizei const GLuint const GLintptr * offsets
Definition: glcorearb.h:2621
HUSD_OverridesLayerId
Definition: HUSD_Utils.h:73
std::vector< SdfLayerOffset > SdfLayerOffsetVector
Definition: layerOffset.h:155
Definition: token.h:87
GLintptr offset
Definition: glcorearb.h:665
GLboolean reset
Definition: glad.h:5138
HUSD_ConstOverridesPtr myReadOverrides
Definition: XUSD_Data.h:109
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
std::vector< TfRefPtr< SdfLayer > > SdfLayerRefPtrVector
~XUSD_PostLayersInfo()
#define HUSD_OVERRIDES_NUM_LAYERS
Definition: HUSD_Utils.h:81
HUSD_ConstPostLayersPtr myPostLayers
Definition: XUSD_Data.h:124
HUSD_StripLayerResponse
Definition: HUSD_Utils.h:91
bool isEmpty() const
Definition: XUSD_Data.h:121
bool hasLayerColorIndex(int &clridx) const
UT_Function< UT_StringHolder(const UT_StringRef &oldpath)> HUSD_MakeNewPathFunc
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
UT_Array< XUSD_LayerAtPath > XUSD_LayerAtPathArray
Definition: XUSD_Data.h:98
XUSD_AddLayerOp
Definition: XUSD_Data.h:45
UT_SharedPtr< HUSD_LockedStage > HUSD_LockedStagePtr
SIM_API const UT_StringHolder position
#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
XUSD_PostLayersInfo(const UsdStageRefPtr &stage)
bool myRemoveWithLayerBreak
Definition: XUSD_Data.h:94
Definition: core.h:1131
SdfLayerRefPtrVector mySessionLayers
Definition: XUSD_Data.h:125
XUSD_OverridesInfo(const UsdStageRefPtr &stage)
Definition: value.h:167
int myLayerColorIndex
Definition: XUSD_Data.h:93
GLenum src
Definition: glcorearb.h:1793