1 //
2 // Copyright 2016 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/imaging/hio/imageRegistry.h"
25 #include "pxr/imaging/hio/image.h"
26 #include "pxr/imaging/hio/rankedTypeMap.h"
27
28 #include "pxr/imaging/hio/debugCodes.h"
29
30 #include "pxr/usd/ar/resolver.h"
31
32 #include "pxr/base/plug/plugin.h"
33 #include "pxr/base/plug/registry.h"
34
35 #include "pxr/base/tf/envSetting.h"
36 #include "pxr/base/tf/instantiateSingleton.h"
37 #include "pxr/base/tf/token.h"
38 #include "pxr/base/tf/type.h"
39
40 #include "pxr/base/trace/trace.h"
41
42 #include <set>
43 #include <string>
44
45 PXR_NAMESPACE_OPEN_SCOPE
46
47
48 TF_DEFINE_ENV_SETTING(HIO_IMAGE_PLUGIN_RESTRICTION, "",
49 "Restricts HioImage plugin loading to the specified plugin");
50
51 TF_INSTANTIATE_SINGLETON(HioImageRegistry);
52
53 HioImageRegistry&
GetInstance()54 HioImageRegistry::GetInstance()
55 {
56 return TfSingleton<HioImageRegistry>::GetInstance();
57 }
58
HioImageRegistry()59 HioImageRegistry::HioImageRegistry() :
60 _typeMap(std::make_unique<HioRankedTypeMap>())
61 {
62 // Register all image types using plugin metadata.
63 _typeMap->Add(TfType::Find<HioImage>(), "imageTypes",
64 HIO_DEBUG_TEXTURE_IMAGE_PLUGINS,
65 TfGetEnvSetting(HIO_IMAGE_PLUGIN_RESTRICTION));
66 }
67
68 HioImageSharedPtr
_ConstructImage(std::string const & filename)69 HioImageRegistry::_ConstructImage(std::string const & filename)
70 {
71 TRACE_FUNCTION();
72
73 // Lookup the plug-in type name based on the filename.
74 const TfToken fileExtension(
75 TfStringToLower(ArGetResolver().GetExtension(filename)));
76
77 TfType const & pluginType = _typeMap->Find(fileExtension);
78
79 if (!pluginType) {
80 // Unknown prim type.
81 TF_DEBUG(HIO_DEBUG_TEXTURE_IMAGE_PLUGINS).Msg(
82 "[PluginLoad] Unknown image type '%s' for file '%s'\n",
83 fileExtension.GetText(),
84 filename.c_str());
85 return nullptr;
86 }
87
88 PlugRegistry& plugReg = PlugRegistry::GetInstance();
89 PlugPluginPtr const plugin = plugReg.GetPluginForType(pluginType);
90 if (!plugin || !plugin->Load()) {
91 TF_CODING_ERROR("[PluginLoad] PlugPlugin could not be loaded for "
92 "TfType '%s'\n",
93 pluginType.GetTypeName().c_str());
94 return nullptr;
95 }
96
97 HioImageFactoryBase* const factory =
98 pluginType.GetFactory<HioImageFactoryBase>();
99 if (!factory) {
100 TF_CODING_ERROR("[PluginLoad] Cannot manufacture type '%s' "
101 "for image type '%s' for file '%s'\n",
102 pluginType.GetTypeName().c_str(),
103 fileExtension.GetText(),
104 filename.c_str());
105
106 return nullptr;
107 }
108
109 HioImageSharedPtr const instance = factory->New();
110 if (!instance) {
111 TF_CODING_ERROR("[PluginLoad] Cannot construct instance of type '%s' "
112 "for image type '%s' for file '%s'\n",
113 pluginType.GetTypeName().c_str(),
114 fileExtension.GetText(),
115 filename.c_str());
116 return nullptr;
117 }
118
119 TF_DEBUG(HIO_DEBUG_TEXTURE_IMAGE_PLUGINS).Msg(
120 "[PluginLoad] Loaded plugin '%s' for image type '%s' for "
121 "file '%s'\n",
122 pluginType.GetTypeName().c_str(),
123 fileExtension.GetText(),
124 filename.c_str());
125
126 return instance;
127 }
128
129 bool
IsSupportedImageFile(std::string const & filename)130 HioImageRegistry::IsSupportedImageFile(std::string const & filename)
131 {
132 // We support image files for which we can construct an image object.
133 return _ConstructImage(filename) != 0;
134 }
135
136 PXR_NAMESPACE_CLOSE_SCOPE
137
138