forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchangeManager.cpp
550 lines (496 loc) · 20.1 KB
/
changeManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#include "pxr/usd/sdf/changeManager.h"
#include "pxr/usd/sdf/debugCodes.h"
#include "pxr/usd/sdf/layer.h"
#include "pxr/usd/sdf/notice.h"
#include "pxr/usd/sdf/schema.h"
#include "pxr/usd/sdf/spec.h"
#include "pxr/base/tracelite/trace.h"
#include "pxr/base/tf/instantiateSingleton.h"
#include "pxr/base/tf/stackTrace.h"
#include <tbb/atomic.h>
using std::string;
using std::vector;
TF_INSTANTIATE_SINGLETON(Sdf_ChangeManager);
Sdf_ChangeManager::_Data::_Data()
: changeBlockDepth(0)
{
}
Sdf_ChangeManager::Sdf_ChangeManager()
{
}
Sdf_ChangeManager::~Sdf_ChangeManager()
{
}
void
Sdf_ChangeManager::_SendNoticesForChangeList( const SdfLayerHandle & layer,
const SdfChangeList & changeList )
{
// Notice which is only sent if a layer changes it's 'dirtiness'
// This is useful in cases where I am just interested if a layer
// has been dirtied or un-dirtied (changes undone)
if (layer->_UpdateLastDirtinessState())
SdfNotice::LayerDirtinessChanged().Send(layer);
const SdfChangeList::EntryList & entryList = changeList.GetEntryList();
TF_FOR_ALL(pathChanges, entryList) {
const SdfPath & path = pathChanges->first;
const SdfChangeList::Entry & entry = pathChanges->second;
TF_FOR_ALL(it, entry.infoChanged) {
if (path == SdfPath::AbsoluteRootPath())
SdfNotice::LayerInfoDidChange(it->first).Send(layer);
}
if (entry.flags.didChangeIdentifier) {
SdfNotice::LayerIdentifierDidChange(
entry.oldIdentifier, layer->GetIdentifier()).Send(layer);
}
if (entry.flags.didReplaceContent)
SdfNotice::LayerDidReplaceContent().Send(layer);
if (entry.flags.didReloadContent)
SdfNotice::LayerDidReloadContent().Send(layer);
}
}
void
Sdf_ChangeManager::OpenChangeBlock()
{
++_data.local().changeBlockDepth;
}
void
Sdf_ChangeManager::CloseChangeBlock()
{
int &changeBlockDepth = _data.local().changeBlockDepth;
if (changeBlockDepth == 1) {
// Closing outermost (last) change block. Process removes while
// the change block is still open.
_ProcessRemoveIfInert();
// Send notices with no change block open.
--changeBlockDepth;
TF_VERIFY(changeBlockDepth == 0);
_SendNotices();
}
else {
// Not outermost.
TF_VERIFY(changeBlockDepth > 0);
--changeBlockDepth;
}
}
void
Sdf_ChangeManager::RemoveSpecIfInert(const SdfSpec& spec)
{
// Add spec. Process remove if we're not in a change block.
OpenChangeBlock();
_data.local().removeIfInert.push_back(spec);
CloseChangeBlock();
}
void
Sdf_ChangeManager::_ProcessRemoveIfInert()
{
_Data& data = _data.local();
// We expect to be in an outermost change block here.
TF_VERIFY(data.changeBlockDepth == 1);
// Swap pending removes into a local variable.
vector<SdfSpec> remove;
remove.swap(data.removeIfInert);
// Remove inert stuff.
TF_FOR_ALL(i, remove) {
i->GetLayer()->_RemoveIfInert(*i);
}
// We don't expect any deferred removes to have been added.
TF_VERIFY(data.removeIfInert.empty());
// We should still be in an outermost change block.
TF_VERIFY(data.changeBlockDepth == 1);
}
static tbb::atomic<size_t> &
_InitChangeSerialNumber() {
static tbb::atomic<size_t> value;
value = 1;
return value;
}
void
Sdf_ChangeManager::_SendNotices()
{
// Swap out the list of events to deliver so that notice listeners
// can safely queue up more changes. We also need to filter out any
// changes from layers that have since been destroyed, as the change
// manager should only send notifications for existing layers.
SdfLayerChangeListMap changes;
changes.swap(_data.local().changes);
SdfLayerChangeListMap::iterator mapIter = changes.begin();
while (mapIter != changes.end()) {
SdfLayerChangeListMap::iterator currIter = (mapIter++);
if (not currIter->first) {
changes.erase(currIter);
}
}
if (changes.empty())
return;
TF_FOR_ALL(it, changes) {
// Send layer-specific notices.
_SendNoticesForChangeList(it->first, it->second);
if (TfDebug::IsEnabled(SDF_CHANGES)) {
TF_DEBUG(SDF_CHANGES).Msg("Changes to layer %s:\n%s",
it->first->GetIdentifier().c_str(),
TfStringify(it->second).c_str());
}
}
// Obtain a serial number for this round of change processing.
static tbb::atomic<size_t> &changeSerialNumber = _InitChangeSerialNumber();
size_t serialNumber = changeSerialNumber.fetch_and_increment();
// Send global notice.
SdfNotice::LayersDidChange(changes, serialNumber).Send();
// Send per-layer notices with change round number. This is so clients
// don't have to be invoked on every round of change processing if they are
// only interested in a subset of layers.
SdfNotice::LayersDidChangeSentPerLayer n(changes, serialNumber);
TF_FOR_ALL(it, changes)
n.Send(it->first);
}
void
Sdf_ChangeManager::DidReplaceLayerContent(const SdfLayerHandle &layer)
{
if (not layer->_ShouldNotify())
return;
_data.local().changes[layer].DidReplaceLayerContent();
}
void
Sdf_ChangeManager::DidReloadLayerContent(const SdfLayerHandle &layer)
{
if (not layer->_ShouldNotify())
return;
_data.local().changes[layer].DidReloadLayerContent();
}
void
Sdf_ChangeManager::DidChangeLayerIdentifier(const SdfLayerHandle &layer,
const std::string &oldIdentifier)
{
if (not layer->_ShouldNotify())
return;
_data.local().changes[layer].DidChangeLayerIdentifier(oldIdentifier);
}
static bool
_IsOrderChangeOnly(const VtValue & oldVal, const VtValue & newVal )
{
// Note: As an optimization, we assume here that the caller has
// already guaranteed that oldVal != newVal.
if (not oldVal.IsEmpty() and not newVal.IsEmpty()) {
const TfTokenVector & oldNames = oldVal.Get<TfTokenVector>();
const TfTokenVector & newNames = newVal.Get<TfTokenVector>();
if (oldNames.size() == newNames.size()) {
TRACE_SCOPE("Sdf_ChangeManager::DidChangeField - "
"Comparing old/new PrimChildren order");
// XXX:optimization: This may turn out to be too slow,
// meriting a more sophisticated approach.
std::set<TfToken, TfTokenFastArbitraryLessThan>
oldNamesSet(oldNames.begin(), oldNames.end()),
newNamesSet(newNames.begin(), newNames.end());
return oldNamesSet == newNamesSet;
}
}
return false;
}
void
Sdf_ChangeManager::DidChangeField(const SdfLayerHandle &layer,
const SdfPath & path, const TfToken &field,
const VtValue & oldVal, const VtValue & newVal )
{
if (not layer->_ShouldNotify())
return;
SdfLayerChangeListMap &changes = _data.local().changes;
// Note: We intend to change the SdfChangeList protocol to provide a
// sequence of (layer, path, field, oldValue, newValue) tuples.
// For now, this function adapts field-based changes into the
// existing protocol.
if (field == SdfFieldKeys->PrimOrder) {
changes[layer].DidReorderPrims(path);
}
else if (field == SdfChildrenKeys->PrimChildren) {
// XXX:OrderNotification:
// Sdf's change protocol does not have a "children changed"
// message; instead it relies on a combination of "order changed"
// and "child added/removed" messages. To avoid sending a
// potentially misleading "order changed" message when in fact
// children have been added and removed, we compare the old
// and new children lists and only send an "order changed"
// message if this is a pure order change.
if (_IsOrderChangeOnly(oldVal, newVal)) {
changes[layer].DidReorderPrims(path);
}
}
else if (field == SdfFieldKeys->PropertyOrder) {
changes[layer].DidReorderProperties(path);
}
else if (field == SdfChildrenKeys->PropertyChildren) {
// XXX:OrderNotification: See above.
if (_IsOrderChangeOnly(oldVal, newVal)) {
changes[layer].DidReorderProperties(path);
}
}
else if (field == SdfFieldKeys->VariantSetNames or
field == SdfChildrenKeys->VariantSetChildren) {
changes[layer].DidChangePrimVariantSets(path);
}
else if (field == SdfFieldKeys->InheritPaths) {
changes[layer].DidChangePrimInheritPaths(path);
}
else if (field == SdfFieldKeys->Specializes) {
changes[layer].DidChangePrimSpecializes(path);
}
else if (field == SdfFieldKeys->References) {
changes[layer].DidChangePrimReferences(path);
}
else if (field == SdfFieldKeys->TimeSamples) {
changes[layer].DidChangeAttributeTimeSamples(path);
}
else if (field == SdfFieldKeys->ConnectionPaths) {
changes[layer].DidChangeAttributeConnection(path);
}
else if (field == SdfFieldKeys->MapperArgValue) {
changes[layer].DidChangeMapperArgument(path.GetParentPath());
}
else if (field == SdfChildrenKeys->MapperChildren) {
changes[layer].DidChangeAttributeConnection(path);
}
else if (field == SdfChildrenKeys->MapperArgChildren) {
changes[layer].DidChangeMapperArgument(path);
}
else if (field == SdfFieldKeys->TargetPaths) {
changes[layer].DidChangeRelationshipTargets(path);
}
else if (field == SdfFieldKeys->Marker) {
const SdfSpecType specType = layer->GetSpecType(path);
if (specType == SdfSpecTypeConnection) {
changes[layer].DidChangeAttributeConnection(path.GetParentPath());
}
else if (specType == SdfSpecTypeRelationshipTarget) {
changes[layer].DidChangeRelationshipTargets(path.GetParentPath());
}
else {
TF_CODING_ERROR("Unknown spec type for marker value change at "
"path <%s>", path.GetText());
}
}
else if (field == SdfFieldKeys->SubLayers) {
std::vector<std::string> addedLayers, removedLayers;
{
const vector<string> oldSubLayers =
oldVal.GetWithDefault<vector<string> >();
const vector<string> newSubLayers =
newVal.GetWithDefault<vector<string> >();
const std::set<std::string> oldSet(oldSubLayers.begin(),
oldSubLayers.end());
const std::set<std::string> newSet(newSubLayers.begin(),
newSubLayers.end());
std::set_difference(oldSet.begin(), oldSet.end(),
newSet.begin(), newSet.end(),
std::back_inserter(removedLayers));
std::set_difference(newSet.begin(), newSet.end(),
oldSet.begin(), oldSet.end(),
std::back_inserter(addedLayers));
}
TF_FOR_ALL(it, addedLayers) {
changes[layer]
.DidChangeSublayerPaths(*it, SdfChangeList::SubLayerAdded);
}
TF_FOR_ALL(it, removedLayers) {
changes[layer]
.DidChangeSublayerPaths(*it, SdfChangeList::SubLayerRemoved);
}
}
else if (field == SdfFieldKeys->SubLayerOffsets) {
const SdfLayerOffsetVector oldOffsets =
oldVal.GetWithDefault<SdfLayerOffsetVector>();
const SdfLayerOffsetVector newOffsets =
newVal.GetWithDefault<SdfLayerOffsetVector>();
// Only add changelist entries if the number of sublayer offsets hasn't
// changed. If the number of offsets has changed, it means sublayers
// have been added or removed. A changelist entry would have already
// been registered for that, so we don't need to add another one here.
if (oldOffsets.size() == newOffsets.size()) {
const SdfSubLayerProxy subLayers = layer->GetSubLayerPaths();
if (TF_VERIFY(newOffsets.size() == subLayers.size())) {
for (size_t i = 0; i < newOffsets.size(); ++i) {
if (oldOffsets[i] != newOffsets[i]) {
changes[layer].DidChangeSublayerPaths(subLayers[i],
SdfChangeList::SubLayerOffset);
}
}
}
}
}
else if (field == SdfFieldKeys->TypeName) {
if (path.IsMapperPath() or path.IsExpressionPath()) {
// Mapper and expression typename changes are treated as changes on
// the owning attribute connection.
changes[layer].DidChangeAttributeConnection(path.GetParentPath());
}
else if (path.IsPrimPath()) {
// Prim typename changes are tricky because typename isn't
// marked as a required field, but can be set during prim spec
// construction. In this case, we don't want to send notification
// as the spec addition notice should suffice. We can identify
// this situation by the fact that the c'tor will have created a
// non-inert prim spec.
//
// If we're *not* in this case, we need to let the world know the
// typename has changed.
const SdfChangeList::Entry& entry = changes[layer].GetEntry(path);
if (not entry.flags.didAddNonInertPrim) {
changes[layer].DidChangeInfo(path, field, oldVal, newVal);
}
}
else {
// Otherwise, this is a typename change on an attribute. Since
// typename is a required field in this case, the only time
// the old or new value will be empty is during the spec c'tor;
// during all other times, we need to send notification.
if (not oldVal.IsEmpty() and not newVal.IsEmpty() and
not oldVal.Get<TfToken>().IsEmpty() and
not newVal.Get<TfToken>().IsEmpty()) {
changes[layer].DidChangeInfo(path, field, oldVal, newVal);
}
}
}
else if (field == SdfFieldKeys->Script) {
changes[layer].DidChangeAttributeConnection(path.GetParentPath());
}
else if (field == SdfFieldKeys->Variability or
field == SdfFieldKeys->Custom or
field == SdfFieldKeys->Specifier) {
// These are all required fields. We only want to send notification
// that they are changing when both the old and new value are not
// empty. Otherwise, the change indicates that the spec is being
// created or removed, which will be handled through the Add/Remove
// change notification API.
if (not oldVal.IsEmpty() and not newVal.IsEmpty()) {
changes[layer].DidChangeInfo(path, field, oldVal, newVal);
}
}
else if (field == SdfChildrenKeys->ConnectionChildren or
field == SdfChildrenKeys->ExpressionChildren or
field == SdfChildrenKeys->MapperChildren or
field == SdfChildrenKeys->RelationshipTargetChildren or
field == SdfChildrenKeys->VariantChildren or
field == SdfChildrenKeys->VariantSetChildren) {
// These children fields are internal. We send notification that the
// child spec was created/deleted, not that the children field
// changed.
}
else {
// Handle any other field as a generic metadata key change.
//
// This is a bit of a lazy hodge. There's no good definition of what
// an "info key" is, but they are clearly a subset of the fields. It
// should be safe for now to simply report all field names as info keys.
// If this is problematic, we'll need to filter them down to the known
// set.
changes[layer].DidChangeInfo(path, field, oldVal, newVal);
}
}
void
Sdf_ChangeManager::DidChangeAttributeTimeSamples(const SdfLayerHandle &layer,
const SdfPath &attrPath)
{
_data.local().changes[layer].DidChangeAttributeTimeSamples(attrPath);
}
void
Sdf_ChangeManager::DidMoveSpec(const SdfLayerHandle &layer,
const SdfPath & oldPath, const SdfPath & newPath)
{
if (not layer->_ShouldNotify())
return;
SdfLayerChangeListMap &changes = _data.local().changes;
if (oldPath.GetParentPath() == newPath.GetParentPath()) {
// Rename
if (oldPath.IsPrimPath()) {
changes[layer].DidChangePrimName(oldPath, newPath);
} else if (oldPath.IsPropertyPath()) {
changes[layer].DidChangePropertyName(oldPath, newPath);
}
} else {
// Reparent
if (oldPath.IsPrimPath()) {
changes[layer].DidRemovePrim(oldPath, /* inert = */ false);
changes[layer].DidAddPrim(newPath, /* inert = */ false);
} else if (oldPath.IsPropertyPath()) {
changes[layer].DidRemoveProperty(oldPath,
/* hasOnlyRequiredFields = */ false);
changes[layer].DidAddProperty(newPath,
/* hasOnlyRequiredFields = */ false);
}
}
}
void
Sdf_ChangeManager::DidAddSpec(const SdfLayerHandle &layer, const SdfPath &path,
bool inert)
{
if (not layer->_ShouldNotify())
return;
SdfLayerChangeListMap &changes = _data.local().changes;
if (path.IsPrimPath() or path.IsPrimVariantSelectionPath()) {
changes[layer].DidAddPrim(path, /* inert = */ inert);
}
else if (path.IsPropertyPath()) {
changes[layer].DidAddProperty(path,
/* hasOnlyRequiredFields = */ inert);
}
else if (path.IsTargetPath()) {
changes[layer].DidAddTarget(path);
}
else if (path.IsMapperPath() or path.IsMapperArgPath()) {
// This is handled when the field on the parent changes
}
else if (path.IsExpressionPath()) {
changes[layer].DidChangeAttributeConnection(path.GetParentPath());
}
else {
TF_CODING_ERROR("Unsupported Spec Type for <" + path.GetString() + ">");
}
}
void
Sdf_ChangeManager::DidRemoveSpec(const SdfLayerHandle &layer, const SdfPath &path,
bool inert)
{
if (not layer->_ShouldNotify())
return;
SdfLayerChangeListMap &changes = _data.local().changes;
if (path.IsPrimPath() or path.IsPrimVariantSelectionPath()) {
changes[layer].DidRemovePrim(path, /* inert = */ inert);
}
else if (path.IsPropertyPath()) {
changes[layer].DidRemoveProperty(path,
/* hasOnlyRequiredFields = */ inert);
}
else if (path.IsTargetPath()) {
changes[layer].DidRemoveTarget(path);
}
else if (path.IsMapperPath() or path.IsMapperArgPath()) {
// This is handled when the field on the parent changes
}
else if (path.IsExpressionPath()) {
changes[layer].DidChangeAttributeConnection(path.GetParentPath());
}
else {
TF_CODING_ERROR("Unsupported Spec Type for <" + path.GetString() + ">");
}
}