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_DataHandle();
96 
97  // Test if myData has been set.
98  bool hasData() const;
99  // Compare the resolver context on this data handle with another.
100  bool matchesResolverContext(
101  const HUSD_DataHandle &other) const;
102 
103  void reset(int nodeid);
104  const HUSD_DataHandle &operator=(const HUSD_DataHandle &src);
105  // Create a new, empty stage in our XUSD_Data.
106  void createNewData(
107  const HUSD_LoadMasksPtr &
108  load_masks = HUSD_LoadMasksPtr(),
109  const HUSD_DataHandle *
110  resolver_context_data = nullptr);
111  // Share the stage from the src XUSD_Data object, unless the load_masks
112  // is supplied, in which case we may need to create a new stage with the
113  // specified load masks.
114  bool createSoftCopy(const HUSD_DataHandle &src,
115  const HUSD_LoadMasksPtr &load_masks,
116  bool make_new_implicit_layer);
117  // Create a new stage that is a copy of the src stage, but replacing any
118  // composition of the "frompath" with the "topath". This check works
119  // recursively, which may require performing other layer replacements
120  // as we make modified versions of referencing layers (and layers that
121  // reference those layers, and so on).
122  bool createCopyWithReplacement(
123  const HUSD_DataHandle &src,
124  const UT_StringRef &frompath,
125  const UT_StringRef &topath,
126  bool replace_sublayers_only,
127  HUSD_MakeNewPathFunc make_new_path,
128  UT_StringSet &replaced_layers);
129  // Looks for an existing layer in the sublayer stack that matches the
130  // supplied pattern. Creates a copy of this found layer, and stashes it
131  // in preparation for making it the edit target. This call must be
132  // followed by a call to createCopyWithReplacement (to make an editable
133  // copy of the source data handle) using the returned identifiers as the
134  // "frompath" and "topath" arguments. Then a call to setEditLayer to
135  // set the edit target using the returned layer indices.
136  bool createReplacementLayer(
137  const HUSD_DataHandle &src,
138  const UT_StringHolder &pattern,
139  bool clear_source,
140  bool create_new_sublayer,
141  const UT_StringHolder &new_layer_name,
142  int new_layer_position,
143  UT_StringHolder &out_found_identifier,
144  UT_StringHolder &out_replacement_identifier,
145  int &out_found_layer_root_index,
146  UT_IntArray &out_found_layer_indices);
147  // Set the edit target layer to a specific layer in the sublayer stack.
148  // These values should come from a call to createReplacementLayer.
149  bool setEditLayer(int root_index,
150  const UT_IntArray &nested_indices);
151  bool endEditLayer();
152  bool inEditLayerBlock() const;
153 
154  UT_StringArray outputPaths() const;
155  void editableLayerIdentifiers(
156  UT_StringArray &identifiers) const;
157 
158  // Make a new stage by flattening the layer stack of our current stage.
159  bool flattenLayers();
160  // Make a new stage by flattening our current stage.
161  bool flattenStage();
162 
163  // For an HUSD_FOR_MIRRORING data handle, create a duplicate of the src
164  // data handle's stage and layer stack.
165  bool mirror(const HUSD_DataHandle &src,
166  const HUSD_LoadMasks &load_masks);
167  void clearMirror();
168 
169  // For an HUSD_FOR_MIRRORING data handle, update our stage's root layer
170  // with the information in the HUSD_MirrorRootLayer. This is currently
171  // just a description of the viewport camera.
172  bool mirrorUpdateRootLayer(
173  const HUSD_MirrorRootLayer &rootlayer);
174 
175  bool hasLayerColorIndex(int &clridx) const;
176  int layerCount() const;
177  int nodeId() const
178  { return myNodeId; }
179  HUSD_LoadMasksPtr loadMasks() const;
180  const std::string &rootLayerIdentifier() const;
181 
182  bool isLocked() const;
183  PXR_NS::XUSD_ConstDataPtr readLock(
185  &overrides,
187  &postlayers,
188  bool remove_layer_break) const;
189  PXR_NS::XUSD_DataPtr writeOverridesLock(
190  const HUSD_OverridesPtr
191  &overrides) const;
192  PXR_NS::XUSD_DataPtr writeLock() const;
194  bool create_change_block) const;
195  void release() const;
196 
197  // Return true if this data handle's node was the most recent node to
198  // create a lock on the stage we may be sharing with many other nodes.
199  bool isMostRecentStageLock() const;
200 
201  // Clear out all mirrored stages completely. They must be rebuilt from
202  // scratch the next time they are asked to mirror a stage.
203  static void clearAllMirroredData();
204 
205 private:
206  HUSD_ConstOverridesPtr currentOverrides() const;
207  HUSD_ConstPostLayersPtr currentPostLayers() const;
208 
209  friend class HUSD_AutoReadLock;
210 
211  PXR_NS::XUSD_DataPtr myData;
212  PXR_NS::XUSD_DataLockPtr myDataLock;
213  HUSD_MirroringType myMirroring;
214  int myNodeId;
215  int myEditBlockCount;
216 };
217 
218 // Parent class for read and write locks that permits reading.
220 {
221 public:
223  const HUSD_DataHandle &handle)
224  : myHandle(handle)
225  { }
227  { }
228 
229  bool isStageValid() const;
231  { return myHandle; }
232  virtual PXR_NS::XUSD_ConstDataPtr constData() const = 0;
233 
234 private:
235  const HUSD_DataHandle &myHandle;
236 };
237 
238 // Locks an HUSD_DataHandle for read-only operations.
240 {
241 public:
242  enum HUSD_OverridesChangeType { OVERRIDES_UNCHANGED, OVERRIDES_CLEARED };
243  enum HUSD_RemoveLayerBreaksType { REMOVE_LAYER_BREAKS, KEEP_LAYER_BREAKS };
244 
245  explicit HUSD_AutoReadLock(
246  const HUSD_DataHandle &handle);
247  explicit HUSD_AutoReadLock(
248  const HUSD_DataHandle &handle,
249  HUSD_OverridesChangeType);
250  explicit HUSD_AutoReadLock(
251  const HUSD_DataHandle &handle,
253  &overrides,
255  &postlayers,
256  HUSD_RemoveLayerBreaksType
257  lbtype = KEEP_LAYER_BREAKS);
258  ~HUSD_AutoReadLock() override;
259 
261  { return myData; }
262  PXR_NS::XUSD_ConstDataPtr constData() const override;
263 
264 private:
266 };
267 
268 // Locks an HUSD_DataHandle in a way that allows read-only operations on the
269 // data, but write access to the supplied overrides layer.
271 {
272 public:
274  const HUSD_DataHandle &handle,
275  const HUSD_OverridesPtr
276  &overrides);
277  ~HUSD_AutoWriteOverridesLock() override;
278 
279  const PXR_NS::XUSD_DataPtr &data() const
280  { return myData; }
282  { return myOverrides; }
283  PXR_NS::XUSD_ConstDataPtr constData() const override;
284 
285 private:
286  PXR_NS::XUSD_DataPtr myData;
287  HUSD_OverridesPtr myOverrides;
288 };
289 
290 // Locks an HUSD_DataHandle for writing to the active layer in the context
291 // of the current stage.
293 {
294 public:
295  explicit HUSD_AutoWriteLock(
296  const HUSD_DataHandle &handle);
297  ~HUSD_AutoWriteLock() override;
298 
299  const PXR_NS::XUSD_DataPtr &data() const
300  { return myData; }
301  void addLockedStages(const
303  PXR_NS::XUSD_ConstDataPtr constData() const override;
304 
305 private:
306  PXR_NS::XUSD_DataPtr myData;
307 };
308 
309 // Locks an HUSD_DataHandle for writing to the active layer outside the
310 // context of the current stage (which is accessible read-only while editing
311 // this off-stage layer).
312 //
313 // A layer lock can be constructed from an HUSD_AutoWriteLock as well. This
314 // doesn't re-lock the data handle, but instead grabs from the write lock all
315 // the data it needs to implement the interface of this class. If the
316 // ScopedLock flag is passed as a second argument, an SdfChangeBlock is
317 // created for the lifetime of this object. This is only safe if this
318 // object will be destroyed before the next time the WriteLock is used
319 // to edit the stage.
321 {
322 public:
325  NoChangeBlock
326  };
327 
328  explicit HUSD_AutoLayerLock(
329  const HUSD_DataHandle &handle,
330  ChangeBlockTag change_block =
331  ChangeBlock);
332  explicit HUSD_AutoLayerLock(
333  const HUSD_AutoWriteLock &lock,
334  ChangeBlockTag change_block =
335  NoChangeBlock);
336  ~HUSD_AutoLayerLock() override;
337 
339  { return myLayer; }
340  void addLockedGeos(const PXR_NS::
341  XUSD_LockedGeoSet &lockedgeos);
342  void addHeldLayers(const PXR_NS::
343  XUSD_LayerSet &layers);
344  void addReplacements(const PXR_NS::
345  XUSD_LayerSet &replacements);
346  void addLockedStages(const
348  PXR_NS::XUSD_ConstDataPtr constData() const override;
349 
350 private:
351  PXR_NS::XUSD_DataPtr myData;
352  PXR_NS::XUSD_LayerPtr myLayer;
353  bool myOwnsHandleLock;
354 };
355 
356 #endif
357 
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