sceneIndex.h
1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_IMAGING_HD_SCENE_INDEX_H
8 #define PXR_IMAGING_HD_SCENE_INDEX_H
9 
10 #include "pxr/pxr.h"
11 
12 #include <set>
13 #include <unordered_map>
14 
16 #include "pxr/base/tf/singleton.h"
18 
19 
20 #include "pxr/usd/sdf/path.h"
21 
22 #include "pxr/imaging/hd/api.h"
23 #include "pxr/imaging/hd/dataSource.h"
24 #include "pxr/imaging/hd/dataSourceLocator.h"
25 #include "pxr/imaging/hd/sceneIndexObserver.h"
26 
27 PXR_NAMESPACE_OPEN_SCOPE
28 
30 
36 {
37  TfToken primType;
38  HdContainerDataSourceHandle dataSource;
39 
42  bool IsDefined() const { return bool(dataSource); }
44  explicit operator bool() const { return IsDefined(); }
45 };
46 
54 class HdSceneIndexBase : public TfRefBase, public TfWeakBase
55 {
56 public:
57  HD_API
59 
60  HD_API
61  ~HdSceneIndexBase() override;
62 
63  // ------------------------------------------------------------------------
64  // Scene Observer API
65  // ------------------------------------------------------------------------
66 
73  HD_API
74  void AddObserver(const HdSceneIndexObserverPtr &observer);
75 
81  HD_API
82  void RemoveObserver(const HdSceneIndexObserverPtr &observer);
83 
84  // ------------------------------------------------------------------------
85  // Scene Data API
86  // ------------------------------------------------------------------------
87 
99  virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const = 0;
100 
110  virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const = 0;
111 
116  HdDataSourceBaseHandle GetDataSource(
117  const SdfPath &primPath,
118  const HdDataSourceLocator &locator) const
119  {
121  GetPrim(primPath).dataSource, locator);
122  }
123 
124  // ------------------------------------------------------------------------
125  // System-wide API
126  // ------------------------------------------------------------------------
127 
131  HD_API
132  void SystemMessage(
133  const TfToken &messageType,
134  const HdDataSourceBaseHandle &args);
135 
136  // ------------------------------------------------------------------------
137  // User Interface Utilities
138  // ------------------------------------------------------------------------
139 
144  HD_API
145  std::string GetDisplayName() const;
146 
150  HD_API
151  void SetDisplayName(const std::string &n);
152 
156  HD_API
157  void AddTag(const TfToken &tag);
158 
162  HD_API
163  void RemoveTag(const TfToken &tag);
164 
168  HD_API
169  bool HasTag(const TfToken &tag) const;
170 
174  HD_API
175  TfTokenVector GetTags() const;
176 
177 protected:
178 
187  HD_API
188  void _SendPrimsAdded(
190 
195  HD_API
196  void _SendPrimsRemoved(
198 
206  HD_API
207  void _SendPrimsDirtied(
209 
210 
215  HD_API
216  void _SendPrimsRenamed(
218 
219 
223  HD_API
224  bool _IsObserved() const;
225 
228  HD_API
229  virtual void _SystemMessage(
230  const TfToken &messageType,
231  const HdDataSourceBaseHandle &args);
232 
233 private:
234  void _RemoveExpiredObservers();
235 
236  // Scoped (RAII) helper to manage tracking recursion depth,
237  // and to remove expired observers after completing delivery.
238  struct _NotifyScope;
239 
240  // Registered observers, in order of registration.
241  using _Observers = std::vector<HdSceneIndexObserverPtr>;
242  _Observers _observers;
243 
244  // Count of in-flight observer notifications
245  int _notifyDepth;
246 
247  // Flag hinting that expired observers may exist.
248  bool _shouldRemoveExpiredObservers;
249 
250  // User-visible label for this scene index
251  std::string _displayName;
252 
253  // Tags used to categorize this scene index
255  _TagSet _tags;
256 };
257 
258 
267  : public TfSingleton<HdSceneIndexNameRegistry>
268 {
270 
271  HdSceneIndexNameRegistry() = default;
272 
273 public:
274 
277  HD_API
280  }
281 
284  HD_API
286  const std::string &name, HdSceneIndexBasePtr instance);
287 
290  HD_API
291  std::vector<std::string> GetRegisteredNames();
292 
295  HD_API
296  HdSceneIndexBaseRefPtr GetNamedSceneIndex(const std::string &name);
297 
298 private:
299 
300  using _NamedInstanceMap =
301  std::unordered_map<std::string, HdSceneIndexBasePtr>;
302 
303  _NamedInstanceMap _namedInstances;
304 };
305 
306 PXR_NAMESPACE_CLOSE_SCOPE
307 
308 #endif // PXR_IMAGING_HD_SCENE_INDEX_H
Manage a single instance of an object.
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
Returns the child datasource of the given name.
Manage a single instance of an object (see.
Definition: singleton.h:107
virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const =0
Returns a pair of (prim type, datasource).
Standard pointer typedefs.
HD_API bool HasTag(const TfToken &tag) const
Returns true if a specified tag token has been added to a scene index instance.
bool IsDefined() const
Does this prim returned by HdSceneIndex::GetPrim exist in the scene index?
Definition: sceneIndex.h:42
HD_API void _SendPrimsRenamed(const HdSceneIndexObserver::RenamedPrimEntries &entries)
Notify attached observers of prims (and their descendents) which have been renamed or reparented.
virtual HD_API void _SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args)
Implement in order to react directly to system messages sent from downstream.
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:72
HD_API TfTokenVector GetTags() const
Returns all tag tokens currently added to a scene index instance.
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:56
Small struct representing a 'prim' in the Hydra scene index.
Definition: sceneIndex.h:35
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:80
HD_API void RemoveTag(const TfToken &tag)
Removes a specified tag token to a scene index instance.
virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const =0
Returns the paths of all scene index prims located immediately below primPath.
Represents an object that can identify the location of a data source.
A registry containing named instances of Hydra indexes.
Definition: sceneIndex.h:266
HD_API void RegisterNamedSceneIndex(const std::string &name, HdSceneIndexBasePtr instance)
Registers an instance of a scene index with a given name.
Abstract interface to scene data.
Definition: sceneIndex.h:54
HD_API void SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args)
Sends a message with optional arguments to this and any upstream input scene indices.
HD_API void SetDisplayName(const std::string &n)
Allows for scene index instances to be identified in a more contextually relevant way.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:489
HD_API bool _IsObserved() const
Returns whether the scene index has any registered observers; this information can be used to skip wo...
HdDataSourceBaseHandle GetDataSource(const SdfPath &primPath, const HdDataSourceLocator &locator) const
A convenience function: look up the object at primPath, and if successful return the datasource at lo...
Definition: sceneIndex.h:116
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:122
HD_API void RemoveObserver(const HdSceneIndexObserverPtr &observer)
Removes an observer from this scene index; the given observer will no longer be forwarded notices.
HD_API void _SendPrimsAdded(const HdSceneIndexObserver::AddedPrimEntries &entries)
Notify attached observers of prims added to the scene.
HD_API void AddTag(const TfToken &tag)
Adds a specified tag token to a scene index instance.
HD_API std::string GetDisplayName() const
Returns a value previously set by SetDisplayName.
HD_API void _SendPrimsDirtied(const HdSceneIndexObserver::DirtiedPrimEntries &entries)
Notify attached observers of datasource invalidations from the scene.
HD_API void AddObserver(const HdSceneIndexObserverPtr &observer)
Adds an observer to this scene index.
A hash set with contiguous storage, suitable for use with TfSpan, e.g.
Definition: denseHashSet.h:39
static HD_API HdSceneIndexNameRegistry & GetInstance()
Returns the singleton-instance of this registry.
Definition: sceneIndex.h:278
HD_API HdSceneIndexBaseRefPtr GetNamedSceneIndex(const std::string &name)
Returns the scene index that was registered with the given name.
HD_API std::vector< std::string > GetRegisteredNames()
Returns the names of all registered scene indexes.
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
HD_API void _SendPrimsRemoved(const HdSceneIndexObserver::RemovedPrimEntries &entries)
Notify attached observers of prims removed from the scene.