VR-Forces 4.1.1 Class Documentation
DtOsgFileCache.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2008 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 /******************************************************************************
6 ** $RCSfile: DtOsgFileCache.h,v $ $Revision: 1.22 $ $State: Exp $
7 ******************************************************************************/
8 
12 
13 #pragma once
14 #include <vrvOsg/vrvOsg.h>
16 #include <vrvCore/DtFileCache.h>
17 #include <vrvUtil/signalslib.h>
18 #include <osgDB/ReaderWriter>
19 
20 #include <boost/unordered_map.hpp>
21 #include <boost/unordered_set.hpp>
22 #include <osgDB/ReaderWriter>
23 #define _WINSOCKAPI_
24 #include <boost/thread/mutex.hpp>
25 #include <boost/signals2.hpp>
26 
27 namespace osg
28 {
29  class Drawable;
30  class GraphicsContext;
31  class Node;
32  class Texture;
33 }
34 
35 namespace makVrv
36 {
37  class DtMeifConverter;
38 
44  {
45  public:
46 
49  friend class DtOsgFileCacheCreator;
50 
52  virtual ~DtOsgFileCache();
53 
55  virtual void releaseResources()
56  {
57  releaseResources(0);
58  }
59 
61  virtual void releaseResources(osg::State* state);
62 
66  osg::Node* readNodeFile( const std::string& inputFilename,
67  const LoadFileOptions& loadFileOptions);
68 
71  inline osg::ref_ptr<osg::Node> findNodeFile( const std::string& inputFilename )
72  {
73  return ts_findNodeFile(inputFilename);
74  }
75 
79  osg::Image* readImageFile( const std::string& inputFilename,
80  const LoadFileOptions& loadFileOptions);
81 
84  virtual osg::Texture* getTextureInstance(const std::string& filePath,
85  osg::Texture::FilterMode minFilter, osg::Texture::FilterMode magFilter,
86  osg::Texture::WrapMode s, osg::Texture::WrapMode t,
87  const LoadFileOptions& loadFileOptions);
88 
90  virtual void encodeOptionsString(const LoadFileOptions& loadFileOptions,
91  osgDB::ReaderWriter::Options& options) const;
92 
94  virtual void decodeOptions(const osgDB::ReaderWriter::Options& options,
95  LoadFileOptions& loadFileOptions) const;
96 
101  boost::signals2::signal<void (osg::Node*, const std::string& origFileName,
102  const std::string& actualFileName)> signal_nodeFileLoaded;
103 
108  boost::signals2::signal<void (osg::Image*, const std::string& origFileName,
109  const std::string& actualFileName)> signal_imageFileLoaded;
110 
114  boost::signals2::signal<void (const std::string& filename,
115  osgDB::ReaderWriter::ReadResult&,const osgDB::ReaderWriter::Options*)>
117 
121  boost::signals2::signal<void (const std::string& filename,
122  osgDB::ReaderWriter::ReadResult&,const osgDB::ReaderWriter::Options*)>
124 
125  class TextureCollection : public osg::Referenced
126  {
127  public:
128  union KeyType
129  {
130  unsigned short key;
131  struct
132  {
133  unsigned int min : 4;
134  unsigned int max : 4;
135  unsigned int wrap_s : 4;
136  unsigned int wrap_t : 4;
137  };
138  };
139 
140  // \Returns trush if the entire texture collection should be flushed.
141  bool flush()
142  {
143  TextureList replacementList;
144 
145  TextureList::iterator i = myTextures.begin();
146  TextureList::iterator e = myTextures.end();
147  for(;i != e;++i)
148  {
149  if(i->second->referenceCount() > 1)
150  {
151  replacementList.push_back(*i);
152  }
153  }
154  if(replacementList.size() != myTextures.size())
155  {
156  myTextures.swap(replacementList);
157  }
158  return myTextures.size() == 0;
159  }
160 
161  void releaseGLObjects()
162  {
163  TextureList::iterator i = myTextures.begin();
164  TextureList::iterator e = myTextures.end();
165  for(;i != e;++i)
166  {
167  i->second->releaseGLObjects();
168  }
169  }
170 
171  bool findTexture(unsigned int minFilter, unsigned int magFilter,
172  unsigned int wrapS, unsigned wrapT, osg::ref_ptr<osg::Texture>& texture)
173  {
174  KeyType k;
175  k.min = ((minFilter ^ 0x2600) & 0x3) + ((minFilter ^ 0x2600) >> 7);
176  k.max = ((magFilter ^ 0x2600) & 0x3) + ((magFilter ^ 0x2600) >> 7);
177  k.wrap_s = (wrapS & 0x0F) | ((wrapS & 0x10) >> 3);
178  k.wrap_t = (wrapT & 0x0F) | ((wrapT & 0x10) >> 3);
179 
180  TextureList::iterator i = myTextures.begin();
181  TextureList::iterator e = myTextures.end();
182  for(;i != e;++i)
183  {
184  if(i->first.key == k.key)
185  {
186  texture = i->second;
187  return true;
188  }
189  }
190  return false;
191  }
192 
193  void addTexture(osg::Texture* texture)
194  {
195  KeyTexturePair pair;
196  pair.first.min = ((texture->getFilter(osg::Texture::MIN_FILTER) ^ 0x2600) & 0x3) +
197  ((texture->getFilter(osg::Texture::MIN_FILTER) ^ 0x2600) >> 7);
198 
199  pair.first.max = ((texture->getFilter(osg::Texture::MAG_FILTER) ^ 0x2600) & 0x3) +
200  ((texture->getFilter(osg::Texture::MAG_FILTER) ^ 0x2600) >> 7);
201 
202  pair.first.wrap_s = (texture->getWrap(osg::Texture::WRAP_S) & 0x0F) |
203  ((texture->getWrap(osg::Texture::WRAP_S) & 0x10) >> 3);
204 
205  pair.first.wrap_t = (texture->getWrap(osg::Texture::WRAP_S) & 0x0F) |
206  ((texture->getWrap(osg::Texture::WRAP_S) & 0x10) >> 3);
207  pair.second = texture;
208  myTextures.push_back(pair);
209  }
210 
211  typedef std::pair<KeyType,osg::ref_ptr<osg::Texture> > KeyTexturePair;
212  typedef std::vector<KeyTexturePair> TextureList;
214  };
215 
216  protected:
217 
218  friend class DtReadFileCallback;
219 
221  virtual osg::ref_ptr<osg::Node> ts_readNodeFile( const std::string& inputFilename,
222  const LoadFileOptions& loadFileOptions);
223 
225  virtual osg::ref_ptr<osg::Node> ts_findNodeFile( const std::string& inputFilename );
226 
228  virtual osg::ref_ptr<osg::Image> ts_readImageFile( const std::string& inputFilename,
229  const LoadFileOptions& loadFileOptions);
230 
232  void ts_encryptImage( const std::string& inputFileName,
233  const std::string& outputFilename, bool printToConsole);
234 
236  bool ts_resolveRelativeFilename( const LoadFileOptions& loadFileOptions,
237  std::string& absoluteInputFilename );
238 
240  virtual void ts_clearInstancingCache();
241 
243  virtual void ts_flushInstancingCache( bool multipass,
244  bool flushNodes, bool flushTextures, bool flushImages);
245 
247  DtOsgFileCache(DtDe& de);
248 
252  std::string ts_buildAcceleratedFilename( const std::string& filePathIn,
253  const std::string& cachedDir,
254  const std::string& cachedExtension) const;
255 
258  virtual bool save(const std::string& origFilename,
259  const std::string& cachedFilename, bool optimize);
260 
262  bool shouldSaveFile( const std::string& filename);
263 
265  bool saveToAcceleratedFile( const std::string& origFileName,
266  osg::Node* nodeRef, const std::string& cachedFilename,
267  bool writeExternalReferences);
268 
270  virtual void addMetaFlightOptions(std::string& optionsString);
271 
272  protected:
273 
274  typedef boost::unordered_map<std::string,std::string> RelativePathCache;
275 
279 
281  unsigned int myMaxFlushNodeCount;
282  unsigned int myMaxFlushImageCount;
283 
284  boost::mutex myRelativePathMutex;
287  };
288 
292  {
293 
294  public:
295 
297  virtual DtFileCache* create(DtDe& de);
298 
299  };
300 }
301 
302 // Include the inl to solve a circular dependency problem for gcc
303 #include <vrvOsg/DtOsgReferencedCollection.inl>
304 

Document ID: Generated on Mon Apr 8 19:24:01 EDT 2013 from SVN revision 125877
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)