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 GLF_TEXTURE_REGISTRY_H
25 #define 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 <boost/noncopyable.hpp>
40 #include <boost/scoped_ptr.hpp>
41 #include <functional>
42 #include <map>
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
46 
47 TF_DECLARE_WEAK_AND_REF_PTRS(GlfTextureHandle);
49 
50 class GlfRankedTypeMap;
51 class GlfTextureFactoryBase;
52 
55 class GlfTextureRegistry : boost::noncopyable {
56 public:
57  GLF_API
58  static GlfTextureRegistry & GetInstance();
59 
60  GLF_API
61  GlfTextureHandleRefPtr GetTextureHandle(const TfToken &texture,
62  GlfImage::ImageOriginLocation originLocation =
63  GlfImage::OriginUpperLeft);
64  GLF_API
65  GlfTextureHandleRefPtr GetTextureHandle(const TfTokenVector &textures,
66  GlfImage::ImageOriginLocation originLocation =
67  GlfImage::OriginUpperLeft);
68  GLF_API
69  GlfTextureHandleRefPtr GetTextureHandle(GlfTextureRefPtr texture);
70  GLF_API
71  GlfTextureHandleRefPtr GetTextureHandle(
72  const TfToken& texture,
73  GlfImage::ImageOriginLocation originLocation,
74  const GlfTextureFactoryBase* textureFactory);
75 
76  // garbage collection methods
77  GLF_API
78  void RequiresGarbageCollection();
79  GLF_API
80  void GarbageCollectIfNeeded();
81 
82  // Returns true if the registry contains a texture sampler for \a texture;
83  GLF_API
84  bool HasTexture(const TfToken &texture,
85  GlfImage::ImageOriginLocation originLocation =
86  GlfImage::OriginUpperLeft) const;
87 
88  // diagnostics
89  GLF_API
90  std::vector<VtDictionary> GetTextureInfos() const;
91 
92  // Resets the registry contents. Clients that call this are expected to
93  // manage their texture handles accordingly.
94  GLF_API
95  void Reset();
96 
97 private:
98  friend class TfSingleton< GlfTextureRegistry >;
99  GlfTextureRegistry();
100 
101  GlfTextureHandleRefPtr _CreateTexture(const TfToken &texture,
102  GlfImage::ImageOriginLocation originLocation);
103  GlfTextureHandleRefPtr _CreateTexture(const TfTokenVector &textures,
104  const size_t numTextures,
105  GlfImage::ImageOriginLocation originLocation);
106  GlfTextureHandleRefPtr _CreateTexture(const TfToken &texture,
107  GlfImage::ImageOriginLocation originLocation,
108  const GlfTextureFactoryBase *textureFactory);
109 
110  GlfTextureFactoryBase* _GetTextureFactory(const TfToken &filename);
111 
112  // Metadata for texture files to aid in cache invalidation.
113  // Because texture arrays are stored as a single registry entry, their
114  // metadata is also aggregated into a single _TextureMetadata instance.
115  class _TextureMetadata
116  {
117  public:
118  _TextureMetadata();
119 
120  // Collect metadata for a texture.
121  explicit _TextureMetadata(const TfToken &texture);
122 
123  // Collect metadata for a texture array.
124  explicit _TextureMetadata(const TfTokenVector &textures);
125 
126  // Compares metadata (but not handles) to see if two _TextureMetadatas
127  // are the same (i.e. they are very likely to be the same on disk.)
128  bool IsMetadataEqual(const _TextureMetadata &other) const;
129 
130  const GlfTextureHandleRefPtr &GetHandle() const;
131  void SetHandle(const GlfTextureHandleRefPtr &handle);
132 
133  private:
134  _TextureMetadata(const TfToken *textures,
135  const std::uint32_t numTextures);
136 
137  std::uint32_t _numTextures;
138  off_t _fileSize;
139  double _mtime;
140  GlfTextureHandleRefPtr _handle;
141  };
142 
143 public:
144  typedef std::map<std::pair<TfToken, GlfImage::ImageOriginLocation>,
145  _TextureMetadata> TextureRegistryMap;
146  typedef std::map<GlfTexturePtr, GlfTextureHandlePtr>
147  TextureRegistryNonSharedMap;
148 
149 private:
150 
151  // Map of file extensions to texture types.
152  boost::scoped_ptr<GlfRankedTypeMap> _typeMap;
153 
154  // registry for shared textures
155  TextureRegistryMap _textureRegistry;
156 
157  // registry for non-shared textures (drawtargets)
158  TextureRegistryNonSharedMap _textureRegistryNonShared;
159 
160  bool _requiresGarbageCollection;
161 };
162 
163 
164 PXR_NAMESPACE_CLOSE_SCOPE
165 
166 #endif // 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:89
ImageOriginLocation
Specifies whether to treat the image origin as the upper-left corner or the lower left...
Definition: image.h:60
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
Represents a texture object in Glf.
Definition: texture.h:63