forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileFormat.cpp
610 lines (529 loc) · 15.6 KB
/
fileFormat.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//
// 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.
//
///
/// \file Sdf/fileFormat.cpp
#include "pxr/pxr.h"
#include "pxr/usd/sdf/fileFormat.h"
#include "pxr/usd/sdf/assetPathResolver.h"
#include "pxr/usd/sdf/data.h"
#include "pxr/usd/sdf/fileFormatRegistry.h"
#include "pxr/usd/sdf/layer.h"
#include "pxr/usd/sdf/layerHints.h"
#include "pxr/usd/ar/resolver.h"
#include "pxr/base/trace/trace.h"
#include "pxr/base/tf/registryManager.h"
#include "pxr/base/tf/staticData.h"
#include "pxr/base/tf/stringUtils.h"
#include "pxr/base/tf/type.h"
PXR_NAMESPACE_OPEN_SCOPE
static TfStaticData<Sdf_FileFormatRegistry> _FileFormatRegistry;
TF_REGISTRY_FUNCTION(TfType)
{
TfType::Define<SdfFileFormat>();
}
TF_DEFINE_PUBLIC_TOKENS(SdfFileFormatTokens, SDF_FILE_FORMAT_TOKENS);
SdfFileFormat::SdfFileFormat(
const TfToken& formatId,
const TfToken& versionString,
const TfToken& target,
const std::string& extension,
const SdfSchemaBase& schema)
: SdfFileFormat(
formatId, versionString, target, std::vector<std::string>{extension},
schema)
{
}
SdfFileFormat::SdfFileFormat(
const TfToken& formatId,
const TfToken& versionString,
const TfToken& target,
const std::string& extension)
: SdfFileFormat(
formatId, versionString, target, std::vector<std::string>{extension},
SdfSchema::GetInstance())
{
}
SdfFileFormat::SdfFileFormat(
const TfToken& formatId,
const TfToken& versionString,
const TfToken& target,
const std::vector<std::string> &extensions)
: SdfFileFormat(
formatId, versionString, target, extensions, SdfSchema::GetInstance())
{
}
SdfFileFormat::SdfFileFormat(
const TfToken& formatId,
const TfToken& versionString,
const TfToken& target,
const std::vector<std::string>& extensions,
const SdfSchemaBase& schema)
: _schema(schema)
, _formatId(formatId)
, _target(target)
, _cookie("#" + formatId.GetString())
, _versionString(versionString)
, _extensions(extensions)
// If a file format is marked as primary, then it must be the
// primary format for all of the extensions it supports. So,
// it's sufficient to just check the first extension in the list.
, _isPrimaryFormat(
_FileFormatRegistry
->GetPrimaryFormatForExtension(extensions[0]) == formatId)
{
// Do Nothing.
}
SdfFileFormat::~SdfFileFormat()
{
// Do Nothing.
}
SdfFileFormat::FileFormatArguments
SdfFileFormat::GetDefaultFileFormatArguments() const
{
return FileFormatArguments();
}
namespace
{
SdfAbstractDataRefPtr
_CreateData(const SdfFileFormat::FileFormatArguments& args)
{
SdfData* metadata = new SdfData;
// The pseudo-root spec must always exist in a layer's SdfData, so
// add it here.
metadata->CreateSpec(SdfPath::AbsoluteRootPath(), SdfSpecTypePseudoRoot);
return TfCreateRefPtr(metadata);
}
} // end anonymous namespace
SdfAbstractDataRefPtr
SdfFileFormat::InitData(const FileFormatArguments& args) const
{
return _CreateData(args);
}
SdfAbstractDataRefPtr
SdfFileFormat::InitDetachedData(const FileFormatArguments& args) const
{
SdfAbstractDataRefPtr detachedData = _InitDetachedData(args);
if (detachedData && !detachedData->IsDetached()) {
TF_CODING_ERROR("File format did not return detached data object.");
return _CreateData(args);
}
return detachedData;
}
SdfLayerRefPtr
SdfFileFormat::NewLayer(const SdfFileFormatConstPtr &fileFormat,
const std::string &identifier,
const std::string &realPath,
const ArAssetInfo& assetInfo,
const FileFormatArguments &args) const
{
return TfCreateRefPtr(
_InstantiateNewLayer(
fileFormat, identifier, realPath, assetInfo, args));
}
bool
SdfFileFormat::ShouldSkipAnonymousReload() const
{
return _ShouldSkipAnonymousReload();
}
bool
SdfFileFormat::ShouldReadAnonymousLayers() const
{
return _ShouldReadAnonymousLayers();
}
const SdfSchemaBase&
SdfFileFormat::GetSchema() const
{
return _schema;
}
const TfToken&
SdfFileFormat::GetFormatId() const
{
return _formatId;
}
const TfToken&
SdfFileFormat::GetTarget() const
{
return _target;
}
const std::string&
SdfFileFormat::GetFileCookie() const
{
return _cookie;
}
const TfToken&
SdfFileFormat::GetVersionString() const
{
return _versionString;
}
bool
SdfFileFormat::IsPrimaryFormatForExtensions() const
{
return _isPrimaryFormat;
}
const std::vector<std::string>&
SdfFileFormat::GetFileExtensions() const {
return _extensions;
}
const std::string&
SdfFileFormat::GetPrimaryFileExtension() const
{
static std::string emptyString;
return TF_VERIFY(!_extensions.empty()) ? _extensions[0] : emptyString;
}
bool
SdfFileFormat::IsSupportedExtension(
const std::string& extension) const
{
std::string ext = GetFileExtension(extension);
return ext.empty() ?
false : std::count(_extensions.begin(), _extensions.end(), ext);
}
bool
SdfFileFormat::IsPackage() const
{
return false;
}
std::string
SdfFileFormat::GetPackageRootLayerPath(
const std::string& resolvedPath) const
{
return std::string();
}
bool
SdfFileFormat::WriteToFile(
const SdfLayer&,
const std::string&,
const std::string&,
const FileFormatArguments&) const
{
return false;
}
bool
SdfFileFormat::ReadDetached(
SdfLayer* layer,
const std::string& resolvedPath,
bool metadataOnly) const
{
const bool readSuccess = _ReadDetached(layer, resolvedPath, metadataOnly);
if (readSuccess && !_GetLayerData(*layer)->IsDetached()) {
TF_CODING_ERROR(
"File format did not return detached layer when reading layer %s.",
layer->GetIdentifier() == resolvedPath || resolvedPath.empty() ?
TfStringPrintf("@%s@", layer->GetIdentifier().c_str()).c_str() :
TfStringPrintf("@%s@ (%s)",
layer->GetIdentifier().c_str(), resolvedPath.c_str()).c_str());
return false;
}
return readSuccess;
}
SdfAbstractDataRefPtr
SdfFileFormat::_InitDetachedData(
const FileFormatArguments& args) const
{
return _CreateData(args);
}
namespace
{
class _WarnedFormatTracker
{
public:
bool NeedToWarn(const SdfFileFormat& format) const
{
std::lock_guard<std::mutex> lock(_mutex);
return _formatIds.insert(format.GetFormatId()).second;
};
private:
mutable std::set<TfToken> _formatIds;
mutable std::mutex _mutex;
};
} // end anonymous namespace
static TfStaticData<_WarnedFormatTracker> _WarnedFormats;
bool
SdfFileFormat::_ReadDetached(
SdfLayer* layer,
const std::string& resolvedPath,
bool metadataOnly) const
{
bool didCopyData = false;
if (!_ReadAndCopyLayerDataToMemory(
layer, resolvedPath, metadataOnly, &didCopyData)) {
return false;
}
if (didCopyData && _WarnedFormats->NeedToWarn(*this)) {
TF_WARN(
"File format plugin '%s' did not produce a detached layer when "
"requested. Layer data has been copied to produce a detached "
"layer, which may impact performance. The file format should be "
"updated to avoid this issue. This was first encountered when"
"reading %s",
GetFormatId().GetText(),
layer->GetIdentifier() == resolvedPath || resolvedPath.empty() ?
TfStringPrintf("@%s@", layer->GetIdentifier().c_str()).c_str() :
TfStringPrintf("@%s@ (%s)",
layer->GetIdentifier().c_str(), resolvedPath.c_str()).c_str());
}
return true;
}
bool
SdfFileFormat::_ReadAndCopyLayerDataToMemory(
SdfLayer* layer,
const std::string& resolvedPath,
bool metadataOnly,
bool* didCopyData) const
{
if (!Read(layer, resolvedPath, metadataOnly)) {
return false;
}
SdfAbstractDataConstPtr layerData = _GetLayerData(*layer);
if (layerData && !layerData->IsDetached()) {
SdfAbstractDataRefPtr detachedData = TfCreateRefPtr(new SdfData);
detachedData->CopyFrom(layerData);
_SetLayerData(layer, detachedData);
if (didCopyData) {
*didCopyData = true;
}
}
else {
if (didCopyData) {
*didCopyData = false;
}
}
return true;
}
bool
SdfFileFormat::ReadFromString(
SdfLayer* layer,
const std::string& str) const
{
return false;
}
bool
SdfFileFormat::WriteToStream(
const SdfSpecHandle &spec,
std::ostream& out,
size_t indent) const
{
return false;
}
bool
SdfFileFormat::WriteToString(
const SdfLayer& layer,
std::string* str,
const std::string& comment) const
{
return false;
}
std::set<std::string>
SdfFileFormat::GetExternalAssetDependencies(
const SdfLayer& layer) const
{
return std::set<std::string>();
}
// XXX:
// fileFormatRegistry.cpp: file format creation does not provide a
// straightforward way to pass parsed capabilities to format constructor.
// As a result, instance methods defer this check to the registry itself.
bool SdfFileFormat::SupportsReading() const
{
return _FileFormatRegistry->FormatSupportsReading(
GetPrimaryFileExtension(), GetTarget());
}
bool SdfFileFormat::SupportsWriting() const
{
return _FileFormatRegistry->FormatSupportsWriting(
GetPrimaryFileExtension(), GetTarget());
}
bool SdfFileFormat::SupportsEditing() const
{
return _FileFormatRegistry->FormatSupportsEditing(
GetPrimaryFileExtension(), GetTarget());
}
/* static */
std::string
SdfFileFormat::GetFileExtension(
const std::string& s)
{
if (s.empty()) {
return s;
}
const std::string extension = Sdf_GetExtension(s);
return extension.empty() ? s : extension;
}
/* static */
std::set<std::string>
SdfFileFormat::FindAllFileFormatExtensions()
{
return _FileFormatRegistry->FindAllFileFormatExtensions();
}
/* static */
std::set<std::string>
SdfFileFormat::FindAllDerivedFileFormatExtensions(const TfType& baseType)
{
return _FileFormatRegistry->FindAllDerivedFileFormatExtensions(baseType);
}
/* static */
bool SdfFileFormat::FormatSupportsReading(
const std::string& extension,
const std::string& target)
{
return _FileFormatRegistry->FormatSupportsReading(extension, target);
}
/* static */
bool SdfFileFormat::FormatSupportsWriting(
const std::string& extension,
const std::string& target)
{
return _FileFormatRegistry->FormatSupportsWriting(extension, target);
}
/* static */
bool SdfFileFormat::FormatSupportsEditing(
const std::string& extension,
const std::string& target)
{
return _FileFormatRegistry->FormatSupportsEditing(extension, target);
}
/* static */
SdfFileFormatConstPtr
SdfFileFormat::FindById(
const TfToken& formatId)
{
return _FileFormatRegistry->FindById(formatId);
}
/* static */
SdfFileFormatConstPtr
SdfFileFormat::FindByExtension(
const std::string& extension,
const std::string& target)
{
return _FileFormatRegistry->FindByExtension(extension, target);
}
/* static */
SdfFileFormatConstPtr
SdfFileFormat::FindByExtension(
const std::string &path,
const FileFormatArguments &args)
{
// Find a file format that can handle this extension and the
// specified target (if any).
const std::string* targets =
TfMapLookupPtr(args, SdfFileFormatTokens->TargetArg);
if (targets) {
for (std::string& target : TfStringTokenize(*targets, ",")) {
target = TfStringTrim(target);
if (target.empty()) {
continue;
}
if (const SdfFileFormatConstPtr format =
SdfFileFormat::FindByExtension(path, target)) {
return format;
}
}
return TfNullPtr;
}
return SdfFileFormat::FindByExtension(path);
}
bool
SdfFileFormat::_ShouldSkipAnonymousReload() const
{
return true;
}
bool
SdfFileFormat::_ShouldReadAnonymousLayers() const
{
return false;
}
void
SdfFileFormat::_SetLayerData(
SdfLayer* layer,
SdfAbstractDataRefPtr& data)
{
_SetLayerData(layer, data, SdfLayerHints{});
}
void
SdfFileFormat::_SetLayerData(
SdfLayer* layer,
SdfAbstractDataRefPtr& data,
SdfLayerHints hints)
{
// If layer initialization has not completed, then this
// is being loaded as a new layer; otherwise we are loading
// data into an existing layer.
//
// Note that this is an optional::bool and we are checking if it has
// been set, not what its held value is.
//
const bool layerIsLoadingAsNew = !layer->_initializationWasSuccessful;
if (layerIsLoadingAsNew) {
layer->_SwapData(data);
}
else {
// If we're reading into an existing layer (e.g. due to a Reload), we
// want the layer to use whatever data implementation the file format
// wants to set because that object may have special behaviors specific
// to the format. If we detect the layer is currently using a different
// implementation, call _AdoptData to have the layer take ownership of
// the new data object and emit coarse invalidation. However, if we
// detect the layer is already using the same implementation, we call
// _SetData, which may perform finer-grained copies and change
// notification.
//
// We consider data implementations to differ if the object types don't
// match or if one streams data and the other doesn't, or if one is
// detached and the other isn't. In the latter cases, we want the layer
// to have the qualities the file format dictates, even if the
// underlying data object type is the same.
const SdfAbstractDataConstPtr oldData = _GetLayerData(*layer);
const bool differentDataImpl =
data->StreamsData() != oldData->StreamsData() ||
data->IsDetached() != oldData->IsDetached() ||
!TfSafeTypeCompare(
typeid(*get_pointer(data)), typeid(*get_pointer(oldData)));
if (differentDataImpl) {
layer->_AdoptData(data);
}
else {
layer->_SetData(data);
}
}
layer->_hints = hints;
}
SdfAbstractDataConstPtr
SdfFileFormat::_GetLayerData(const SdfLayer& layer)
{
return layer._GetData();
}
/* virtual */
SdfLayer*
SdfFileFormat::_InstantiateNewLayer(
const SdfFileFormatConstPtr &fileFormat,
const std::string &identifier,
const std::string &realPath,
const ArAssetInfo& assetInfo,
const FileFormatArguments &args) const
{
return new SdfLayer(fileFormat, identifier, realPath, assetInfo, args);
}
// ------------------------------------------------------------
Sdf_FileFormatFactoryBase::~Sdf_FileFormatFactoryBase() = default;
PXR_NAMESPACE_CLOSE_SCOPE