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