HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
primManagingSceneIndexObserver.h
Go to the documentation of this file.
1 //
2 // Copyright 2023 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_HDSI_PRIM_MANAGING_SCENE_INDEX_OBSERVER_H
8 #define PXR_IMAGING_HDSI_PRIM_MANAGING_SCENE_INDEX_OBSERVER_H
9 
10 /// \file hdsi/primManagingSceneIndexObserver.h
11 
12 #include "pxr/imaging/hdsi/api.h"
13 
16 
18 
19 #define HDSI_PRIM_MANAGING_SCENE_INDEX_OBSERVER_TOKENS \
20  (primFactory)
21 
22 TF_DECLARE_PUBLIC_TOKENS(HdsiPrimManagingSceneIndexObserverTokens, HDSI_API,
24 
26 
27 /// \class HdsiPrimManagingSceneIndexObserver
28 ///
29 /// A scene index observer that turns prims in the observed scene index
30 /// into instances (of RAII subclasses) of PrimBase using the given prim
31 /// factory.
32 ///
33 /// This observer is an analogue to the HdPrimTypeIndex in the old hydra
34 /// API (though we do not have separate observers for b/s/r-prims and
35 /// instead rely on the observed filtering scene index (e.g.,
36 /// the HdsiPrimTypeNoticeBatchingSceneIndex) to batch notices
37 /// in a way respecting dependencies).
38 ///
39 /// More precisely, a AddedPrimEntry results in a call to the prim factory
40 /// (this also applies to prims that exist in the observed scene index at the
41 /// time the observer was instantiated).
42 ///
43 /// The observer manages a map from paths to PrimBase handles so that
44 /// subsequent a DirtiedPrimEntry or RemovedPrimEntry results in a call to
45 /// PrimBase::Dirty or releases the handles to the PrimBase's at paths
46 /// prefixed by the RemovedPrimEntry's path.
47 ///
49  : public HdSceneIndexObserver, public TfRefBase
50 {
51 public:
52  /// \class HdsiPrimManagingSceneIndexObserver::PrimBase
53  ///
54  /// Base class for prims managed by the observer.
55  ///
56  class PrimBase
57  {
58  public:
59  HDSI_API
60  virtual ~PrimBase();
61 
62  void Dirty(
63  const DirtiedPrimEntry &entry,
64  const HdsiPrimManagingSceneIndexObserver * observer)
65  {
66  // NVI so that we can later implement things like a
67  // mutex guarding against Dirty from several threads
68  // on the same prim.
69  _Dirty(entry, observer);
70  }
71 
72  private:
73  virtual void _Dirty(
74  const DirtiedPrimEntry &entry,
75  const HdsiPrimManagingSceneIndexObserver * observer) = 0;
76  };
77  using PrimBaseHandle = std::shared_ptr<PrimBase>;
78 
79  /// \class HdsiPrimManagingSceneIndexObserver::PrimFactoryBase
80  ///
81  /// Base class for a prim factory given to the observer.
82  ///
84  {
85  public:
86  HDSI_API
87  virtual ~PrimFactoryBase();
88  virtual PrimBaseHandle CreatePrim(
89  const AddedPrimEntry &entry,
90  const HdsiPrimManagingSceneIndexObserver * observer) = 0;
91  };
92  using PrimFactoryBaseHandle = std::shared_ptr<PrimFactoryBase>;
93 
94  /// C'tor. Prim factory can be given through inputArgs as
95  /// PrimFactoryBaseHandle typed data source under
96  /// HdsiPrimManagingSceneIndexObserverTokens->primFactory key.
97  ///
98  static HdsiPrimManagingSceneIndexObserverRefPtr New(
99  HdSceneIndexBaseRefPtr const &sceneIndex,
100  HdContainerDataSourceHandle const &inputArgs) {
101  return TfCreateRefPtr(
103  sceneIndex, inputArgs));
104  }
105 
106  HDSI_API
108 
109  /// Get observed scene index.
110  ///
111  const HdSceneIndexBaseRefPtr &GetSceneIndex() const {
112  return _sceneIndex;
113  }
114 
115  /// Get managed prim at path.
116  ///
117  /// Clients can prolong life-time of prim by holding on to the
118  /// resulting handle.
119  ///
120  HDSI_API
121  const PrimBaseHandle &GetPrim(const SdfPath &primPath) const;
122 
123  /// Get managed prim cast to a particular type.
124  template<typename PrimType>
125  std::shared_ptr<PrimType> GetTypedPrim(const SdfPath &primPath) const
126  {
127  return std::dynamic_pointer_cast<PrimType>(GetPrim(primPath));
128  }
129 
130 protected:
131  void PrimsAdded(
132  const HdSceneIndexBase &sender,
133  const AddedPrimEntries &entries) override;
134 
135  void PrimsDirtied(
136  const HdSceneIndexBase &sender,
137  const DirtiedPrimEntries &entries) override;
138 
139  void PrimsRemoved(
140  const HdSceneIndexBase &sender,
141  const RemovedPrimEntries &entries) override;
142 
143  void PrimsRenamed(
144  const HdSceneIndexBase &sender,
145  const RenamedPrimEntries &entries) override;
146 
147 private:
148  HDSI_API
150  HdSceneIndexBaseRefPtr const &sceneIndex,
151  HdContainerDataSourceHandle const &inputArgs);
152 
153  HdSceneIndexBaseRefPtr const _sceneIndex;
154  PrimFactoryBaseHandle const _primFactory;
155 
156  // _prims defined after _primFactory so that all prims
157  // are destroyed before the handle to _primFactory is
158  // released.
159  std::map<SdfPath, PrimBaseHandle> _prims;
160 };
161 
163 
164 #endif
TF_DECLARE_REF_PTRS(HdsiPrimManagingSceneIndexObserver)
TfRefPtr< T > TfCreateRefPtr(T *ptr)
Definition: refPtr.h:1190
static HdsiPrimManagingSceneIndexObserverRefPtr New(HdSceneIndexBaseRefPtr const &sceneIndex, HdContainerDataSourceHandle const &inputArgs)
TfSmallVector< AddedPrimEntry, 16 > AddedPrimEntries
void Dirty(const DirtiedPrimEntry &entry, const HdsiPrimManagingSceneIndexObserver *observer)
HDSI_API ~HdsiPrimManagingSceneIndexObserver() override
#define HDSI_PRIM_MANAGING_SCENE_INDEX_OBSERVER_TOKENS
TfSmallVector< RenamedPrimEntry, 16 > RenamedPrimEntries
TfSmallVector< RemovedPrimEntry, 16 > RemovedPrimEntries
void PrimsRenamed(const HdSceneIndexBase &sender, const RenamedPrimEntries &entries) override
virtual PrimBaseHandle CreatePrim(const AddedPrimEntry &entry, const HdsiPrimManagingSceneIndexObserver *observer)=0
HDSI_API const PrimBaseHandle & GetPrim(const SdfPath &primPath) const
TF_DECLARE_PUBLIC_TOKENS(HdsiPrimManagingSceneIndexObserverTokens, HDSI_API, HDSI_PRIM_MANAGING_SCENE_INDEX_OBSERVER_TOKENS)
TfSmallVector< DirtiedPrimEntry, 16 > DirtiedPrimEntries
std::shared_ptr< PrimFactoryBase > PrimFactoryBaseHandle
Definition: path.h:273
void PrimsRemoved(const HdSceneIndexBase &sender, const RemovedPrimEntries &entries) override
#define HDSI_API
Definition: api.h:23
std::shared_ptr< PrimType > GetTypedPrim(const SdfPath &primPath) const
Get managed prim cast to a particular type.
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
void PrimsDirtied(const HdSceneIndexBase &sender, const DirtiedPrimEntries &entries) override
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
void PrimsAdded(const HdSceneIndexBase &sender, const AddedPrimEntries &entries) override
const HdSceneIndexBaseRefPtr & GetSceneIndex() const