All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
textureRegistry.h
Go to the documentation of this file.
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 #ifndef PXR_IMAGING_GLF_TEXTURE_REGISTRY_H
25 #define PXR_IMAGING_GLF_TEXTURE_REGISTRY_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/imaging/glf/api.h"
31 #include "pxr/imaging/glf/image.h"
32 #include "pxr/base/tf/declarePtrs.h"
33 #include "pxr/base/tf/refPtr.h"
34 #include "pxr/base/tf/singleton.h"
35 #include "pxr/base/tf/token.h"
36 #include "pxr/base/tf/weakPtr.h"
37 #include "pxr/base/vt/dictionary.h"
38 
39 #include <functional>
40 #include <map>
41 
42 PXR_NAMESPACE_OPEN_SCOPE
43 
44 
45 TF_DECLARE_WEAK_AND_REF_PTRS(GlfTextureHandle);
47 
48 class GlfRankedTypeMap;
49 class GlfTextureFactoryBase;
50 
53 class GlfTextureRegistry
54 {
55 public:
56  GLF_API
57  static GlfTextureRegistry & GetInstance();
58 
59  GLF_API
60  GlfTextureHandleRefPtr GetTextureHandle(const TfToken &texture,
61  GlfImage::ImageOriginLocation originLocation =
62  GlfImage::OriginUpperLeft);
63  GLF_API
64  GlfTextureHandleRefPtr GetTextureHandle(const TfTokenVector &textures,
65  GlfImage::ImageOriginLocation originLocation =
66  GlfImage::OriginUpperLeft);
67  GLF_API
68  GlfTextureHandleRefPtr GetTextureHandle(GlfTextureRefPtr texture);
69  GLF_API
70  GlfTextureHandleRefPtr GetTextureHandle(
71  const TfToken& texture,
72  GlfImage::ImageOriginLocation originLocation,
73  const GlfTextureFactoryBase* textureFactory);
74 
75  // garbage collection methods
76  GLF_API
77  void RequiresGarbageCollection();
78  GLF_API
79  void GarbageCollectIfNeeded();
80 
81  // Returns true if the registry contains a texture sampler for \a texture;
82  GLF_API
83  bool HasTexture(const TfToken &texture,
84  GlfImage::ImageOriginLocation originLocation =
85  GlfImage::OriginUpperLeft) const;
86 
87  // diagnostics
88  GLF_API
89  std::vector<VtDictionary> GetTextureInfos() const;
90 
91  // Resets the registry contents. Clients that call this are expected to
92  // manage their texture handles accordingly.
93  GLF_API
94  void Reset();
95 
96 private:
97  friend class TfSingleton< GlfTextureRegistry >;
98  GlfTextureRegistry();
99 
100  // Disallow copies
101  GlfTextureRegistry(const GlfTextureRegistry&) = delete;
102  GlfTextureRegistry& operator=(const GlfTextureRegistry&) = delete;
103 
104  GlfTextureHandleRefPtr _CreateTexture(const TfToken &texture,
105  GlfImage::ImageOriginLocation originLocation);
106  GlfTextureHandleRefPtr _CreateTexture(const TfTokenVector &textures,
107  const size_t numTextures,
108  GlfImage::ImageOriginLocation originLocation);
109  GlfTextureHandleRefPtr _CreateTexture(const TfToken &texture,
110  GlfImage::ImageOriginLocation originLocation,
111  const GlfTextureFactoryBase *textureFactory);
112 
113  GlfTextureFactoryBase* _GetTextureFactory(const TfToken &filename);
114 
115  // Metadata for texture files to aid in cache invalidation.
116  // Because texture arrays are stored as a single registry entry, their
117  // metadata is also aggregated into a single _TextureMetadata instance.
118  class _TextureMetadata
119  {
120  public:
121  _TextureMetadata();
122 
123  // Collect metadata for a texture.
124  explicit _TextureMetadata(const TfToken &texture);
125 
126  // Collect metadata for a texture array.
127  explicit _TextureMetadata(const TfTokenVector &textures);
128 
129  // Compares metadata (but not handles) to see if two _TextureMetadatas
130  // are the same (i.e. they are very likely to be the same on disk.)
131  bool IsMetadataEqual(const _TextureMetadata &other) const;
132 
133  const GlfTextureHandleRefPtr &GetHandle() const;
134  void SetHandle(const GlfTextureHandleRefPtr &handle);
135 
136  private:
137  _TextureMetadata(const TfToken *textures,
138  const std::uint32_t numTextures);
139 
140  std::uint32_t _numTextures;
141  off_t _fileSize;
142  double _mtime;
143  GlfTextureHandleRefPtr _handle;
144  };
145 
146 public:
147  typedef std::map<std::pair<TfToken, GlfImage::ImageOriginLocation>,
148  _TextureMetadata> TextureRegistryMap;
149  typedef std::map<GlfTexturePtr, GlfTextureHandlePtr>
150  TextureRegistryNonSharedMap;
151 
152 private:
153 
154  // Map of file extensions to texture types.
155  std::unique_ptr<GlfRankedTypeMap> _typeMap;
156 
157  // registry for shared textures
158  TextureRegistryMap _textureRegistry;
159 
160  // registry for non-shared textures (drawtargets)
161  TextureRegistryNonSharedMap _textureRegistryNonShared;
162 
163  bool _requiresGarbageCollection;
164 };
165 
166 
167 PXR_NAMESPACE_CLOSE_SCOPE
168 
169 #endif // PXR_IMAGING_GLF_TEXTURE_REGISTRY_H
Manage a single instance of an object (see.
Definition: singleton.h:122
Holds a token-to-type map with support for precedence per type.
Definition: rankedTypeMap.h:45
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
ImageOriginLocation
Specifies whether to treat the image origin as the upper-left corner or the lower left...
Definition: image.h:58
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:431
Represents a texture object in Glf.
Definition: texture.h:62