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  // User Interface Utilities
126  // ------------------------------------------------------------------------
127 
128  /// Returns a value previously set by SetDisplayName. If no value (or an
129  /// empty string) was last set, this returns a symbol-demangled version of
130  /// the class type itself. This is in service of user interfaces with views
131  /// of scene index chains or graphs.
132  HD_API
133  std::string GetDisplayName() const;
134 
135  /// Allows for scene index instances to be identified in a more contextually
136  /// relevant way. This is in service of user interfaces with views of scene
137  /// index chains or graphs.
138  HD_API
139  void SetDisplayName(const std::string &n);
140 
141  /// Adds a specified tag token to a scene index instance. This is in service
142  /// of user interfaces which want to filter views of a scene index chain
143  /// or graph.
144  HD_API
145  void AddTag(const TfToken &tag);
146 
147  /// Removes a specified tag token to a scene index instance.
148  /// This is in service of user interfaces which want to filter views of a
149  /// scene index chain or graph.
150  HD_API
151  void RemoveTag(const TfToken &tag);
152 
153  /// Returns true if a specified tag token has been added to a scene index
154  /// instance. This is in service of user interfaces which want to filter
155  /// views of a scene index chain or graph.
156  HD_API
157  bool HasTag(const TfToken &tag) const;
158 
159  /// Returns all tag tokens currently added to a scene index instance. This
160  /// is in service of user interfaces which want to filter views of a scene
161  /// index chain or graph.
162  HD_API
163  TfTokenVector GetTags() const;
164 
165 protected:
166 
167  /// Notify attached observers of prims added to the scene. The set of
168  /// scene prims compiled from added/removed notices should match the set
169  /// from a traversal based on \p GetChildPrimNames. Each prim has a path
170  /// and type. It's possible for \p PrimsAdded to be called for prims that
171  /// already exist; in that case, observers should be sure to update the
172  /// prim type, in case it changed, and resync the prim. This function is
173  /// not threadsafe; some observers expect it to be called from a single
174  /// thread.
175  HD_API
176  void _SendPrimsAdded(
178 
179  /// Notify attached observers of prims removed from the scene. Note that
180  /// this message is considered hierarchical: if \p /Path is removed,
181  /// \p /Path/child is considered removed as well. This function is not
182  /// threadsafe; some observers expect it to be called from a single thread.
183  HD_API
184  void _SendPrimsRemoved(
186 
187  /// Notify attached observers of datasource invalidations from the scene.
188  /// This message is not considered hierarchical on \p primPath; if
189  /// \p /Path is dirtied, \p /Path/child is not necessarily dirtied.
190  /// However, locators are considered hierarchical: if \p primvars is
191  /// dirtied on a prim, \p primvars/color is considered dirtied as well.
192  /// This function is not threadsafe; some observers expect it to be called
193  /// from a single thread.
194  HD_API
195  void _SendPrimsDirtied(
197 
198 
199  /// Notify attached observers of prims (and their descendents) which have
200  /// been renamed or reparented.
201  /// This function is not threadsafe; some observers expect it to be called
202  /// from a single thread.
203  HD_API
204  void _SendPrimsRenamed(
206 
207 
208  /// Returns whether the scene index has any registered observers; this
209  /// information can be used to skip work preparing notices when there are
210  /// no observers.
211  HD_API
212  bool _IsObserved() const;
213 
214 private:
215  void _RemoveExpiredObservers();
216 
217  // Scoped (RAII) helper to manage tracking recursion depth,
218  // and to remove expired observers after completing delivery.
219  struct _NotifyScope;
220 
221  // Registered observers, in order of registration.
222  using _Observers = std::vector<HdSceneIndexObserverPtr>;
223  _Observers _observers;
224 
225  // Count of in-flight observer notifications
226  int _notifyDepth;
227 
228  // Flag hinting that expired observers may exist.
229  bool _shouldRemoveExpiredObservers;
230 
231  // User-visible label for this scene index
232  std::string _displayName;
233 
234  // Tags used to categorize this scene index
236  _TagSet _tags;
237 };
238 
239 
240 ///
241 /// \class HdSceneIndexNameRegistry
242 ///
243 /// A registry containing named instances of Hydra indexes. Scene Indexes
244 /// are not automatically registered here, and must be manually added
245 /// (generally by the application).
246 ///
248  : public TfSingleton<HdSceneIndexNameRegistry>
249 {
251 
252  HdSceneIndexNameRegistry() = default;
253 
254 public:
255 
256  /// Returns the singleton-instance of this registry.
257  ///
258  HD_API
261  }
262 
263  /// Registers an \p instance of a scene index with a given \p name.
264  ///
265  HD_API
267  const std::string &name, HdSceneIndexBasePtr instance);
268 
269  /// Returns the names of all registered scene indexes.
270  ///
271  HD_API
272  std::vector<std::string> GetRegisteredNames();
273 
274  /// Returns the scene index that was registered with the given \p name.
275  ///
276  HD_API
277  HdSceneIndexBaseRefPtr GetNamedSceneIndex(const std::string &name);
278 
279 private:
280 
281  using _NamedInstanceMap =
282  std::unordered_map<std::string, HdSceneIndexBasePtr>;
283 
284  _NamedInstanceMap _namedInstances;
285 };
286 
288 
289 #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)
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:259
HD_API void _SendPrimsRenamed(const HdSceneIndexObserver::RenamedPrimEntries &entries)
HD_API void _SendPrimsDirtied(const HdSceneIndexObserver::DirtiedPrimEntries &entries)
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:442
HdDataSourceBaseHandle GetDataSource(const SdfPath &primPath, const HdDataSourceLocator &locator) const
Definition: sceneIndex.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
Definition: path.h:291
HD_API void AddTag(const TfToken &tag)
std::vector< class SdfPath > SdfPathVector
A vector of SdfPaths.
Definition: path.h:212
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:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
HD_API bool _IsObserved() const
HD_API bool HasTag(const TfToken &tag) const
HD_API std::vector< std::string > GetRegisteredNames()