1 //
2 // Copyright 2020 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 #include "pxr/pxr.h"
25 #include "pxr/usd/usd/primTypeInfo.h"
26
27 PXR_NAMESPACE_OPEN_SCOPE
28
UsdPrimTypeInfo(_TypeId && typeId)29 UsdPrimTypeInfo::UsdPrimTypeInfo(_TypeId &&typeId)
30 : _typeId(std::move(typeId))
31 , _primDefinition(nullptr)
32 {
33 // Helper for initializing the schema type from either the mapped type
34 // name or the prim type name.
35 auto _SetSchemaType = [this](const TfToken &typeName)
36 {
37 // Empty type name returns false.
38 if (typeName.IsEmpty()) {
39 return false;
40 }
41 _schemaType =
42 UsdSchemaRegistry::GetConcreteTypeFromSchemaTypeName(typeName);
43 if (_schemaType) {
44 _schemaTypeName = typeName;
45 }
46 // Return true even if the schema type is invalid since the type name
47 // is not empty.
48 return true;
49 };
50
51 // Set the schema type using the mapped type name over the prim type
52 // name if we have a mapped type name.
53 _SetSchemaType(_typeId.mappedTypeName) ||
54 _SetSchemaType(_typeId.primTypeName);
55 }
56
57 const UsdPrimDefinition *
_FindOrCreatePrimDefinition() const58 UsdPrimTypeInfo::_FindOrCreatePrimDefinition() const
59 {
60 const UsdPrimDefinition *primDef = nullptr;
61 const UsdSchemaRegistry ® = UsdSchemaRegistry::GetInstance();
62 if (_typeId.appliedAPISchemas.empty()) {
63 // With no applied schemas we can just get the concrete typed prim
64 // definition from the schema registry. Prim definitions for all
65 // concrete types are created with the schema registry when it is
66 // instantiated so if the type exists, the definition will be there.
67 primDef = reg.FindConcretePrimDefinition(_schemaTypeName);
68 if (!primDef) {
69 // For invalid types, we use the empty prim definition so we don't
70 // have to check again.
71 primDef = reg.GetEmptyPrimDefinition();
72 }
73 // Cache the prim definition pointer. The schema registry created the
74 // prim definition and will continue to own it so the pointer value
75 // will be constant. Thus, we don't have to check if another thread
76 // cached it first as all threads would store the same pointer.
77 _primDefinition.store(primDef, std::memory_order_relaxed);
78 } else {
79 // If we have applied schemas, then we need ask the schema registry to
80 // compose a prim definition for us from the list of types. The schema
81 // registry does NOT take ownership of this new prim definition; this
82 // type info will own it instead.
83 std::unique_ptr<UsdPrimDefinition> composedPrimDef =
84 reg.BuildComposedPrimDefinition(_schemaTypeName,
85 _typeId.appliedAPISchemas);
86 // Try to cache the new prim definition, but if another thread beat us
87 // to it, we'll use its definition instead and just let ours get
88 // deleted.
89 if (_primDefinition.compare_exchange_strong(
90 primDef, composedPrimDef.get(), std::memory_order_acq_rel)) {
91 // Since we succeeded, transfer ownership of the new prim definition
92 // to this type info.
93 _ownedPrimDefinition = std::move(composedPrimDef);
94 primDef = _ownedPrimDefinition.get();
95 }
96 }
97 return primDef;
98 }
99
100 /*static*/
101 const UsdPrimTypeInfo &
GetEmptyPrimType()102 UsdPrimTypeInfo::GetEmptyPrimType()
103 {
104 static const UsdPrimTypeInfo empty;
105 return empty;
106 }
107
108 PXR_NAMESPACE_CLOSE_SCOPE
109
110