HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HUSD_DataHandle.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_DataHandle_h__
19 #define __HUSD_DataHandle_h__
20 
21 #include "HUSD_API.h"
22 #include <UT/UT_Assert.h>
23 #include <UT/UT_Function.h>
24 #include <UT/UT_IntrusivePtr.h>
25 #include <UT/UT_NonCopyable.h>
26 #include <UT/UT_Set.h>
27 #include <UT/UT_SharedPtr.h>
28 #include <UT/UT_StringArray.h>
29 #include <UT/UT_StringHolder.h>
30 #include <UT/UT_StringMap.h>
31 #include <UT/UT_StringSet.h>
32 #include <UT/UT_WeakPtr.h>
33 #include <pxr/pxr.h>
34 
35 class UT_Color;
36 class UT_StringArray;
37 class HUSD_FindPrims;
39 
46 
51 
52 // Use a SharedPtr instead of an IntrusivePtr for HUSD_LoadMasks because we
53 // want to be able to copy these objects.
56 
58 
59 class XUSD_Data;
62 class XUSD_Layer;
70 template <class T> class TfRefPtr;
71 class SdfLayer;
75 
77 
82 };
83 
86 
88 {
89 public:
91  mirroring = HUSD_NOT_FOR_MIRRORING);
93  explicit HUSD_DataHandle(void *stage_ptr);
94  explicit HUSD_DataHandle(const UT_StringRef &filepath,
95  HUSD_LoadMasks *loadmasks = nullptr);
96  ~HUSD_DataHandle();
97 
98  // Test if myData has been set.
99  bool hasData() const;
100  // Compare the resolver context on this data handle with another.
101  bool matchesResolverContext(
102  const HUSD_DataHandle &other) const;
103 
104  void reset(int nodeid);
105  const HUSD_DataHandle &operator=(const HUSD_DataHandle &src);
106  // Create a new, empty stage in our XUSD_Data.
107  void createNewData(
108  const HUSD_LoadMasksPtr &
109  load_masks = HUSD_LoadMasksPtr(),
110  const HUSD_DataHandle *
111  resolver_context_data = nullptr);
112  // Share the stage from the src XUSD_Data object, unless the load_masks
113  // is supplied, in which case we may need to create a new stage with the
114  // specified load masks.
115  bool createSoftCopy(const HUSD_DataHandle &src,
116  const HUSD_LoadMasksPtr &load_masks,
117  bool make_new_implicit_layer);
118  // Create a new stage that is a copy of the src stage, but replacing any
119  // composition of the "frompath" with the "topath". This check works
120  // recursively, which may require performing other layer replacements
121  // as we make modified versions of referencing layers (and layers that
122  // reference those layers, and so on).
123  bool createCopyWithReplacement(
124  const HUSD_DataHandle &src,
125  const UT_StringRef &frompath,
126  const UT_StringRef &topath,
127  bool replace_sublayers_only,
128  HUSD_MakeNewPathFunc make_new_path,
129  UT_StringSet &replaced_layers);
130  // Looks for an existing layer in the sublayer stack that matches the
131  // supplied pattern. Creates a copy of this found layer, and stashes it
132  // in preparation for making it the edit target. This call must be
133  // followed by a call to createCopyWithReplacement (to make an editable
134  // copy of the source data handle) using the returned identifiers as the
135  // "frompath" and "topath" arguments. Then a call to setEditLayer to
136  // set the edit target using the returned layer indices.
137  bool createReplacementLayer(
138  const HUSD_DataHandle &src,
139  const UT_StringHolder &pattern,
140  bool clear_source,
141  bool create_new_sublayer,
142  const UT_StringHolder &new_layer_name,
143  int new_layer_position,
144  UT_StringHolder &out_found_identifier,
145  UT_StringHolder &out_replacement_identifier,
146  int &out_found_layer_root_index,
147  UT_IntArray &out_found_layer_indices);
148  // Set the edit target layer to a specific layer in the sublayer stack.
149  // These values should come from a call to createReplacementLayer.
150  bool setEditLayer(int root_index,
151  const UT_IntArray &nested_indices);
152  bool endEditLayer();
153  bool inEditLayerBlock() const;
154 
155  UT_StringArray outputPaths() const;
156  void editableLayerIdentifiers(
157  UT_StringArray &identifiers) const;
158 
159  // Make a new stage by flattening the layer stack of our current stage.
160  bool flattenLayers();
161  // Make a new stage by flattening our current stage.
162  bool flattenStage();
163 
164  // For an HUSD_FOR_MIRRORING data handle, create a duplicate of the src
165  // data handle's stage and layer stack.
166  bool mirror(const HUSD_DataHandle &src,
167  const HUSD_LoadMasks &load_masks);
168  void clearMirror();
169 
170  // For an HUSD_FOR_MIRRORING data handle, update our stage's root layer
171  // with the information in the HUSD_MirrorRootLayer. This is currently
172  // just a description of the viewport camera.
173  bool mirrorUpdateRootLayer(
174  const HUSD_MirrorRootLayer &rootlayer);
175 
176  bool hasLayerColorIndex(int &clridx) const;
177  int layerCount() const;
178  int nodeId() const
179  { return myNodeId; }
180  HUSD_LoadMasksPtr loadMasks() const;
181  const std::string &rootLayerIdentifier() const;
182 
183  bool isLocked() const;
184  PXR_NS::XUSD_ConstDataPtr readLock(
186  &overrides,
188  &postlayers,
189  bool remove_layer_break) const;
190  PXR_NS::XUSD_DataPtr writeOverridesLock(
191  const HUSD_OverridesPtr
192  &overrides) const;
193  PXR_NS::XUSD_DataPtr writeLock() const;
195  bool create_change_block) const;
196  void release() const;
197 
198  // Return true if this data handle's node was the most recent node to
199  // create a lock on the stage we may be sharing with many other nodes.
200  bool isMostRecentStageLock() const;
201 
202  // Clear out all mirrored stages completely. They must be rebuilt from
203  // scratch the next time they are asked to mirror a stage.
204  static void clearAllMirroredData();
205 
206 private:
207  HUSD_ConstOverridesPtr currentOverrides() const;
208  HUSD_ConstPostLayersPtr currentPostLayers() const;
209 
210  friend class HUSD_AutoReadLock;
211 
212  PXR_NS::XUSD_DataPtr myData;
213  PXR_NS::XUSD_DataLockPtr myDataLock;
214  HUSD_MirroringType myMirroring;
215  int myNodeId;
216  int myEditBlockCount;
217 };
218 
219 // Parent class for read and write locks that permits reading.
221 {
222 public:
224  const HUSD_DataHandle &handle)
225  : myHandle(handle)
226  { }
228  { }
229 
230  bool isStageValid() const;
232  { return myHandle; }
233  virtual PXR_NS::XUSD_ConstDataPtr constData() const = 0;
234 
235 private:
236  const HUSD_DataHandle &myHandle;
237 };
238 
239 // Locks an HUSD_DataHandle for read-only operations.
241 {
242 public:
243  enum HUSD_OverridesChangeType { OVERRIDES_UNCHANGED, OVERRIDES_CLEARED };
244  enum HUSD_RemoveLayerBreaksType { REMOVE_LAYER_BREAKS, KEEP_LAYER_BREAKS };
245 
246  explicit HUSD_AutoReadLock(
247  const HUSD_DataHandle &handle);
248  explicit HUSD_AutoReadLock(
249  const HUSD_DataHandle &handle,
250  HUSD_OverridesChangeType);
251  explicit HUSD_AutoReadLock(
252  const HUSD_DataHandle &handle,
254  &overrides,
256  &postlayers,
257  HUSD_RemoveLayerBreaksType
258  lbtype = KEEP_LAYER_BREAKS);
259  ~HUSD_AutoReadLock() override;
260 
262  { return myData; }
263  PXR_NS::XUSD_ConstDataPtr constData() const override;
264 
265 private:
267 };
268 
269 // Locks an HUSD_DataHandle in a way that allows read-only operations on the
270 // data, but write access to the supplied overrides layer.
272 {
273 public:
275  const HUSD_DataHandle &handle,
276  const HUSD_OverridesPtr
277  &overrides);
278  ~HUSD_AutoWriteOverridesLock() override;
279 
280  const PXR_NS::XUSD_DataPtr &data() const
281  { return myData; }
283  { return myOverrides; }
284  PXR_NS::XUSD_ConstDataPtr constData() const override;
285 
286 private:
287  PXR_NS::XUSD_DataPtr myData;
288  HUSD_OverridesPtr myOverrides;
289 };
290 
291 // Locks an HUSD_DataHandle for writing to the active layer in the context
292 // of the current stage.
294 {
295 public:
296  explicit HUSD_AutoWriteLock(
297  const HUSD_DataHandle &handle);
298  ~HUSD_AutoWriteLock() override;
299 
300  const PXR_NS::XUSD_DataPtr &data() const
301  { return myData; }
302  void addLockedStages(const
304  PXR_NS::XUSD_ConstDataPtr constData() const override;
305 
306 private:
307  PXR_NS::XUSD_DataPtr myData;
308 };
309 
310 // Locks an HUSD_DataHandle for writing to the active layer outside the
311 // context of the current stage (which is accessible read-only while editing
312 // this off-stage layer).
313 //
314 // A layer lock can be constructed from an HUSD_AutoWriteLock as well. This
315 // doesn't re-lock the data handle, but instead grabs from the write lock all
316 // the data it needs to implement the interface of this class. If the
317 // ScopedLock flag is passed as a second argument, an SdfChangeBlock is
318 // created for the lifetime of this object. This is only safe if this
319 // object will be destroyed before the next time the WriteLock is used
320 // to edit the stage.
322 {
323 public:
326  NoChangeBlock
327  };
328 
329  explicit HUSD_AutoLayerLock(
330  const HUSD_DataHandle &handle,
331  ChangeBlockTag change_block =
332  ChangeBlock);
333  explicit HUSD_AutoLayerLock(
334  const HUSD_AutoWriteLock &lock,
335  ChangeBlockTag change_block =
336  NoChangeBlock);
337  ~HUSD_AutoLayerLock() override;
338 
340  { return myLayer; }
341  void addLockedGeos(const PXR_NS::
342  XUSD_LockedGeoSet &lockedgeos);
343  void addHeldLayers(const PXR_NS::
344  XUSD_LayerSet &layers);
345  void addReplacements(const PXR_NS::
346  XUSD_LayerSet &replacements);
347  void addLockedStages(const
349  PXR_NS::XUSD_ConstDataPtr constData() const override;
350 
351 private:
352  PXR_NS::XUSD_DataPtr myData;
353  PXR_NS::XUSD_LayerPtr myLayer;
354  bool myOwnsHandleLock;
355 };
356 
357 #endif
358 
UT_IntrusivePtr< HUSD_PostLayers > HUSD_PostLayersPtr
Definition: layer.h:81
UT_SharedPtr< HUSD_LoadMasks > HUSD_LoadMasksPtr
GLbitfield stages
Definition: glcorearb.h:1931
HUSD_MirroringType
UT_IntrusivePtr< XUSD_DataLock > XUSD_DataLockPtr
UT_IntrusivePtr< const HUSD_Overrides > HUSD_ConstOverridesPtr
#define HUSD_API
Definition: HUSD_API.h:31
UT_IntrusivePtr< XUSD_Data > XUSD_DataPtr
const PXR_NS::XUSD_DataPtr & data() const
UT_IntrusivePtr< XUSD_LockedGeo > XUSD_LockedGeoPtr
UT_IntrusivePtr< const HUSD_PostLayers > HUSD_ConstPostLayersPtr
const PXR_NS::XUSD_LayerPtr & layer() const
UT_IntrusivePtr< HUSD_Overrides > HUSD_OverridesPtr
const HUSD_OverridesPtr & overrides() const
UT_IntrusivePtr< const XUSD_Data > XUSD_ConstDataPtr
#define PXR_NS
Definition: pxr.h:54
int nodeId() const
GLboolean reset
Definition: glad.h:5138
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
const PXR_NS::XUSD_DataPtr & data() const
UT_IntrusivePtr< const XUSD_Layer > XUSD_ConstLayerPtr
UT_WeakPtr< HUSD_LockedStage > HUSD_LockedStageWeakPtr
GLushort pattern
Definition: glad.h:2583
std::function< T > UT_Function
Definition: UT_Function.h:37
virtual PXR_NS::XUSD_ConstDataPtr constData() const =0
UT_Function< UT_StringHolder(const UT_StringRef &oldpath)> HUSD_MakeNewPathFunc
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
virtual ~HUSD_AutoAnyLock()
UT_SharedPtr< HUSD_LockedStage > HUSD_LockedStagePtr
LeafData & operator=(const LeafData &)=delete
HUSD_AutoAnyLock(const HUSD_DataHandle &handle)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
const PXR_NS::XUSD_ConstDataPtr & data() const
const HUSD_DataHandle & dataHandle() const
UT_Set< XUSD_LockedGeoPtr > XUSD_LockedGeoSet
UT_Set< HUSD_LockedStagePtr > HUSD_LockedStageSet
TfRefPtr< SdfLayer > SdfLayerRefPtr
UT_Array< SdfLayerRefPtr > XUSD_LayerArray
std::weak_ptr< T > UT_WeakPtr
Definition: UT_SharedPtr.h:49
UT_IntrusivePtr< XUSD_Layer > XUSD_LayerPtr
Definition: format.h:1821
UT_Set< SdfLayerRefPtr > XUSD_LayerSet
GLenum src
Definition: glcorearb.h:1793