HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sceneIndex.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_SCENE_INDEX_H
25 #define PXR_IMAGING_HD_SCENE_INDEX_H
26 
27 #include "pxr/pxr.h"
28 
29 #include <set>
30 #include <unordered_map>
31 
33 #include "pxr/base/tf/singleton.h"
35 
36 
37 #include "pxr/usd/sdf/path.h"
38 
39 #include "pxr/imaging/hd/api.h"
43 
45 
46 
47 ///
48 /// Small struct representing a 'prim' in the Hydra scene index. A prim is
49 /// represented by a container data source which contains a tree of properties.
50 ///
52 {
54  HdContainerDataSourceHandle dataSource;
55 };
56 
57 ///
58 /// \class HdSceneIndexBase
59 ///
60 /// Abstract interface to scene data. This class can be queried for scene
61 /// data directly, and it can also register observers to be notified about
62 /// scene changes (see HdSceneIndexObserver).
63 ///
64 class HdSceneIndexBase : public TfRefBase, public TfWeakBase
65 {
66 public:
67  HD_API
69 
70  HD_API
71  ~HdSceneIndexBase() override;
72 
73  // ------------------------------------------------------------------------
74  // Scene Observer API
75  // ------------------------------------------------------------------------
76 
77  /// Adds an observer to this scene index. The given observer will be sent
78  /// notices for prims added, removed, or dirtied after it is added as an
79  /// observer. It will not be sent notices for prims already in the scene
80  /// index; the calling code is responsible for updating observer state
81  /// if the scene index has already been populated. This function is not
82  /// threadsafe.
83  HD_API
84  void AddObserver(const HdSceneIndexObserverPtr &observer);
85 
86  /// Removes an observer from this scene index; the given observer will no
87  /// longer be forwarded notices. Note that the observer won't get any
88  /// notices as a result of being detached from this scene index. If
89  /// \p observer is not registered on this scene index, this call does
90  /// nothing. This function is not threadsafe.
91  HD_API
92  void RemoveObserver(const HdSceneIndexObserverPtr &observer);
93 
94  // ------------------------------------------------------------------------
95  // Scene Data API
96  // ------------------------------------------------------------------------
97 
98  /// Returns a pair of (prim type, datasource) for the object at
99  /// \p primPath. If no such object exists, the type will be the empty
100  /// token and the datasource will be null. This function is expected to
101  /// be threadsafe.
102  virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const = 0;
103 
104  /// Returns the paths of all scene index prims located immediately below
105  /// \p primPath. This function can be used to traverse
106  /// the scene by recursing from \p SdfPath::AbsoluteRootPath(); such a
107  /// traversal is expected to give the same set of prims as the
108  /// flattening of the scene index's \p PrimsAdded and \p PrimsRemoved
109  /// messages. This function is expected to be threadsafe.
110  virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const = 0;
111 
112  /// A convenience function: look up the object at \p primPath, and if
113  /// successful return the datasource at \p locator within that prim. This
114  /// is equivalent to calling \p GetPrim(primPath), and then calling
115  /// \p HdContainerDataSource::Get(prim.dataSource, locator).
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 
128  /// Sends a message with optional arguments to this and any upstream input
129  /// scene indices. Scene indices may implement _SystemMessage to provide
130  /// custom handling. See systemMessages.h for common message definitions.
131  HD_API
132  void SystemMessage(
133  const TfToken &messageType,
134  const HdDataSourceBaseHandle &args);
135 
136  // ------------------------------------------------------------------------
137  // User Interface Utilities
138  // ------------------------------------------------------------------------
139 
140  /// Returns a value previously set by SetDisplayName. If no value (or an
141  /// empty string) was last set, this returns a symbol-demangled version of
142  /// the class type itself. This is in service of user interfaces with views
143  /// of scene index chains or graphs.
144  HD_API
145  std::string GetDisplayName() const;
146 
147  /// Allows for scene index instances to be identified in a more contextually
148  /// relevant way. This is in service of user interfaces with views of scene
149  /// index chains or graphs.
150  HD_API
151  void SetDisplayName(const std::string &n);
152 
153  /// Adds a specified tag token to a scene index instance. This is in service
154  /// of user interfaces which want to filter views of a scene index chain
155  /// or graph.
156  HD_API
157  void AddTag(const TfToken &tag);
158 
159  /// Removes a specified tag token to a scene index instance.
160  /// This is in service of user interfaces which want to filter views of a
161  /// scene index chain or graph.
162  HD_API
163  void RemoveTag(const TfToken &tag);
164 
165  /// Returns true if a specified tag token has been added to a scene index
166  /// instance. This is in service of user interfaces which want to filter
167  /// views of a scene index chain or graph.
168  HD_API
169  bool HasTag(const TfToken &tag) const;
170 
171  /// Returns all tag tokens currently added to a scene index instance. This
172  /// is in service of user interfaces which want to filter views of a scene
173  /// index chain or graph.
174  HD_API
175  TfTokenVector GetTags() const;
176 
177 protected:
178 
179  /// Notify attached observers of prims added to the scene. The set of
180  /// scene prims compiled from added/removed notices should match the set
181  /// from a traversal based on \p GetChildPrimNames. Each prim has a path
182  /// and type. It's possible for \p PrimsAdded to be called for prims that
183  /// already exist; in that case, observers should be sure to update the
184  /// prim type, in case it changed, and resync the prim. This function is
185  /// not threadsafe; some observers expect it to be called from a single
186  /// thread.
187  HD_API
188  void _SendPrimsAdded(
190 
191  /// Notify attached observers of prims removed from the scene. Note that
192  /// this message is considered hierarchical: if \p /Path is removed,
193  /// \p /Path/child is considered removed as well. This function is not
194  /// threadsafe; some observers expect it to be called from a single thread.
195  HD_API
196  void _SendPrimsRemoved(
198 
199  /// Notify attached observers of datasource invalidations from the scene.
200  /// This message is not considered hierarchical on \p primPath; if
201  /// \p /Path is dirtied, \p /Path/child is not necessarily dirtied.
202  /// However, locators are considered hierarchical: if \p primvars is
203  /// dirtied on a prim, \p primvars/color is considered dirtied as well.
204  /// This function is not threadsafe; some observers expect it to be called
205  /// from a single thread.
206  HD_API
207  void _SendPrimsDirtied(
209 
210 
211  /// Notify attached observers of prims (and their descendents) which have
212  /// been renamed or reparented.
213  /// This function is not threadsafe; some observers expect it to be called
214  /// from a single thread.
215  HD_API
216  void _SendPrimsRenamed(
218 
219 
220  /// Returns whether the scene index has any registered observers; this
221  /// information can be used to skip work preparing notices when there are
222  /// no observers.
223  HD_API
224  bool _IsObserved() const;
225 
226  /// Implement in order to react directly to system messages sent from
227  /// downstream.
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 
259 ///
260 /// \class HdSceneIndexNameRegistry
261 ///
262 /// A registry containing named instances of Hydra indexes. Scene Indexes
263 /// are not automatically registered here, and must be manually added
264 /// (generally by the application).
265 ///
267  : public TfSingleton<HdSceneIndexNameRegistry>
268 {
270 
271  HdSceneIndexNameRegistry() = default;
272 
273 public:
274 
275  /// Returns the singleton-instance of this registry.
276  ///
277  HD_API
280  }
281 
282  /// Registers an \p instance of a scene index with a given \p name.
283  ///
284  HD_API
286  const std::string &name, HdSceneIndexBasePtr instance);
287 
288  /// Returns the names of all registered scene indexes.
289  ///
290  HD_API
291  std::vector<std::string> GetRegisteredNames();
292 
293  /// Returns the scene index that was registered with the given \p name.
294  ///
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 
307 
308 #endif // PXR_IMAGING_HD_SCENE_INDEX_H
HD_API void SetDisplayName(const std::string &n)
static T & GetInstance()
Definition: singleton.h:137
HD_API void _SendPrimsRemoved(const HdSceneIndexObserver::RemovedPrimEntries &entries)
HD_API void RegisterNamedSceneIndex(const std::string &name, HdSceneIndexBasePtr instance)
HD_API void SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
TfToken primType
Definition: sceneIndex.h:53
#define HD_API
Definition: api.h:40
static HD_API HdSceneIndexNameRegistry & GetInstance()
Definition: sceneIndex.h:278
HD_API void _SendPrimsRenamed(const HdSceneIndexObserver::RenamedPrimEntries &entries)
HD_API void _SendPrimsDirtied(const HdSceneIndexObserver::DirtiedPrimEntries &entries)
virtual HD_API void _SystemMessage(const TfToken &messageType, const HdDataSourceBaseHandle &args)
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
HD_API ~HdSceneIndexBase() override
virtual SdfPathVector GetChildPrimPaths(const SdfPath &primPath) const =0
HD_API std::string GetDisplayName() const
GLdouble n
Definition: glcorearb.h:2008
Definition: token.h:87
HD_API void RemoveObserver(const HdSceneIndexObserverPtr &observer)
HdContainerDataSourceHandle dataSource
Definition: sceneIndex.h:54
HD_API void AddObserver(const HdSceneIndexObserverPtr &observer)
HD_API TfTokenVector GetTags() const
HD_API HdSceneIndexBase()
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
HdDataSourceBaseHandle GetDataSource(const SdfPath &primPath, const HdDataSourceLocator &locator) const
Definition: sceneIndex.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:290
HD_API void AddTag(const TfToken &tag)
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:211
HD_API void _SendPrimsAdded(const HdSceneIndexObserver::AddedPrimEntries &entries)
HD_API void RemoveTag(const TfToken &tag)
virtual HdSceneIndexPrim GetPrim(const SdfPath &primPath) const =0
HD_API HdSceneIndexBaseRefPtr GetNamedSceneIndex(const std::string &name)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
**If you just want to fire and args
Definition: thread.h:609
HD_API bool _IsObserved() const
HD_API bool HasTag(const TfToken &tag) const
HD_API std::vector< std::string > GetRegisteredNames()