Loading...
Searching...
No Matches
stageData.h
Go to the documentation of this file.
1//
2// Copyright 2026 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_EXEC_ESF_USD_STAGE_DATA_H
8#define PXR_EXEC_ESF_USD_STAGE_DATA_H
9
11
12#include "pxr/pxr.h"
13
14#include "pxr/exec/esfUsd/api.h"
15
17#include "pxr/base/tf/hash.h"
18#include "pxr/usd/sdf/path.h"
19#include "pxr/usd/usd/notice.h"
20
21#ifdef TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS
22#include <tbb/concurrent_map.h>
23#else
24#define TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS 1
25#include <tbb/concurrent_map.h>
26#undef TBB_PREVIEW_CONCURRENT_ORDERED_CONTAINERS
27#endif
28
29#include <tbb/concurrent_unordered_map.h>
30#include <tbb/concurrent_unordered_set.h>
31#include <tbb/concurrent_vector.h>
32
33#include <memory>
34#include <mutex>
35#include <vector>
36
37PXR_NAMESPACE_OPEN_SCOPE
38
39class UsdAttribute;
40class UsdPrim;
41
43
55{
56 EsfUsdStageData(const UsdStageConstPtr &stage);
57
58public:
59
63 using ChangedPathSet = tbb::concurrent_unordered_set<SdfPath, TfHash>;
64
68 class ESFUSD_API_TYPE ListenerBase {
69 public:
70 ESFUSD_API
71 virtual ~ListenerBase();
72
73 private:
74 friend EsfUsdStageData;
75
78 virtual void _DidObjectsChanged(
79 const UsdNotice::ObjectsChanged &objectsChanged,
80 const ChangedPathSet &changedTargetPaths) const = 0;
81 };
82
83
84 ESFUSD_API
86
93 ESFUSD_API
94 static std::shared_ptr<EsfUsdStageData> RegisterStage(
95 const UsdStageConstPtr &stage,
96 const ListenerBase *listener);
97
100 ESFUSD_API
102 const ListenerBase *listener);
103
112 ESFUSD_API
114 const UsdStageConstPtr &stage);
115
119 ESFUSD_API
120 static const SdfPathVector &GetOutgoingConnections(
121 const UsdStageConstPtr &stage,
122 const SdfPath &attrPath);
123
127 ESFUSD_API
128 static SdfPathVector GetIncomingConnections(
129 const UsdStageConstPtr &stage,
130 const SdfPath &targetPath);
131
132private:
133 const UsdStageConstPtr &_GetStage() const { return _stage; }
134
135 const SdfPathVector &_GetOutgoingConnections(
136 const SdfPath &targetPath);
137
138 SdfPathVector _GetIncomingConnections(
139 const SdfPath &targetPath);
140
141 void _PopulateConnectionTables();
142
143 // Define a type and a corresponding transparent comparator so that we can
144 // use lower_bound to find the end of a map range with keys that are paths
145 // with a given prefix.
146
147 struct _PathPrefix
148 {
149 SdfPath prefix;
150 };
151
152 struct _PathRangeLessThan
153 {
154 using is_transparent = void;
155
156 bool operator()(const SdfPath &lhs, const SdfPath &rhs) const {
157 return lhs < rhs;
158 }
159
160 bool operator()(const SdfPath &lhs, const _PathPrefix& rhs) const {
161 return lhs < rhs.prefix || lhs.HasPrefix(rhs.prefix);
162 }
163 };
164
165 // A concurrent map from owning attribute paths to target object paths.
166 using _OutgoingPathTable =
167 tbb::concurrent_map<SdfPath, SdfPathVector, _PathRangeLessThan>;
168
169 // A concurent map from target object paths to owning attribute paths.
170 using _IncomingPathTable =
171 tbb::concurrent_unordered_map<
172 SdfPath, tbb::concurrent_vector<SdfPath>, TfHash>;
173
174 // Gets the new (i.e., current) connections for the given attribute and
175 // computes the added and removed target paths, relative to what was
176 // previously stored in the outgoing connection paths table.
177 //
178 void _GetChangedConnectionTargets(
179 const SdfPath &attrPath,
180 const UsdAttribute &attribute,
181 SdfPathVector *newConnections,
182 SdfPathVector *addedTargetPaths,
183 SdfPathVector *removedTargetPaths) const;
184
185 // Updates attribute connection caches for connections owned by the
186 // attribute at \p attrPath.
187 //
188 // Populates \p incomingConnectionsChanged with the paths of objects whose
189 // incoming connection paths have changed.
190 //
191 void _UpdateForChangedAttributeConnections(
192 const SdfPath &attrPath,
193 ChangedPathSet *incomingConnectionsChanged);
194
195 // Updates attribute connection caches for connections owned by attribute
196 // at or under \p resyncedPath.
197 //
198 void _UpdateForResync(
199 const SdfPath &resyncedPath,
200 ChangedPathSet *incomingConnectionsChanged);
201
202 // Updates outgoing and incoming connection tables for any changes to the
203 // given prim.
204 //
205 // New incoming connections are added immediately; new outgoing connections
206 // and removed incoming connections are queued up for deferred processing,
207 // to allow this method to be called in parallel.
208 //
209 void _UpdateForChangedPrim(
210 const UsdPrim &prim,
211 _IncomingPathTable *incomingToRemove,
212 ChangedPathSet *incomingConnectionsChanged);
213
214 // Find entries in the outgoing connections map for attributes on the
215 // resynced prim and its descendants that no longer exist in the scene,
216 // populating \p ownerAttrsToRemove and \p incomingToRemove.
217 //
218 void _UpdateForRemovedAttributes(
219 const SdfPath &resyncedPath,
220 _IncomingPathTable *incomingToRemove,
221 tbb::concurrent_vector<SdfPath> *ownerAttrsToRemove) const;
222
223 // Removes the entries indicated by \p incomingToRemove from _incoming.
224 void _RemoveIncomingTableEntries(
225 const _IncomingPathTable &incomingToRemove);
226
227 // Notifies all listeners of changes.
228 void _Notify(
229 const UsdNotice::ObjectsChanged &objectsChanged,
230 const ChangedPathSet &changedTargetPaths) const;
231
232private:
233 const UsdStageConstPtr _stage;
234
235 // Used to listen for change notification from the stage.
236 class _NoticeListener;
237 const std::unique_ptr<_NoticeListener> _noticeListener;
238
239 // Registered listeners, used to communicate change notfication to clients.
240 std::vector<const ListenerBase *> _listeners;
241 mutable std::mutex _listenersMutex;
242
243 _OutgoingPathTable _outgoing;
244
245 _IncomingPathTable _incoming;
246};
247
248PXR_NAMESPACE_CLOSE_SCOPE
249
250#endif
The base class for listeners defined by clients in order to be notified of scene changes.
Definition: stageData.h:68
Class that holds data that is cached per-stage.
Definition: stageData.h:55
ESFUSD_API void Unregister(const ListenerBase *listener)
Notifies that listener no longer needs to be informed of changes.
static ESFUSD_API const SdfPathVector & GetOutgoingConnections(const UsdStageConstPtr &stage, const SdfPath &attrPath)
Returns the paths of all objects that are targets of connections owned by the attribute at attrPath.
static ESFUSD_API SdfPathVector GetIncomingConnections(const UsdStageConstPtr &stage, const SdfPath &targetPath)
Returns the paths of all attributes that own connections that target the object at targetPath.
tbb::concurrent_unordered_set< SdfPath, TfHash > ChangedPathSet
A concurrent set of paths, used to indicate the set of targets for which incoming connections have ch...
Definition: stageData.h:63
static ESFUSD_API EsfUsdStageData & GetStageData(const UsdStageConstPtr &stage)
Get the cached stage data for stage.
static ESFUSD_API std::shared_ptr< EsfUsdStageData > RegisterStage(const UsdStageConstPtr &stage, const ListenerBase *listener)
Registers stage as a stage for which cached data should be held, returning a strong reference the cli...
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:281
SDF_API bool HasPrefix(const SdfPath &prefix) const
Return true if both this path and prefix are not the empty path and this path has prefix as a prefix.
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:472
Scenegraph object for authoring and retrieving numeric, string, and array valued data,...
Definition: attribute.h:183
Notice sent in response to authored changes that affect UsdObjects.
Definition: notice.h:111
UsdPrim is the sole persistent scenegraph object on a UsdStage, and is the embodiment of a "Prim" as ...
Definition: prim.h:117
The outermost container for scene description, which owns and presents composed prims as a scenegraph...
Definition: stage.h:135
Standard pointer typedefs.
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:45