PageRenderTime 31ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/lltexturecache.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 220 lines | 152 code | 33 blank | 35 comment | 0 complexity | fbfcdaac8c4e14862be57f6064dba730 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lltexturecache.h
  3. * @brief Object for managing texture cachees.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLTEXTURECACHE_
  27. #define LL_LLTEXTURECACHE_H
  28. #include "lldir.h"
  29. #include "llstl.h"
  30. #include "llstring.h"
  31. #include "lluuid.h"
  32. #include "llworkerthread.h"
  33. class LLImageFormatted;
  34. class LLTextureCacheWorker;
  35. class LLTextureCache : public LLWorkerThread
  36. {
  37. friend class LLTextureCacheWorker;
  38. friend class LLTextureCacheRemoteWorker;
  39. friend class LLTextureCacheLocalFileWorker;
  40. private:
  41. // Entries
  42. struct EntriesInfo
  43. {
  44. EntriesInfo() : mVersion(0.f), mEntries(0) {}
  45. F32 mVersion;
  46. U32 mEntries;
  47. };
  48. struct Entry
  49. {
  50. Entry() :
  51. mBodySize(0),
  52. mImageSize(0),
  53. mTime(0)
  54. {
  55. }
  56. Entry(const LLUUID& id, S32 imagesize, S32 bodysize, U32 time) :
  57. mID(id), mImageSize(imagesize), mBodySize(bodysize), mTime(time) {}
  58. void init(const LLUUID& id, U32 time) { mID = id, mImageSize = 0; mBodySize = 0; mTime = time; }
  59. Entry& operator=(const Entry& entry) {mID = entry.mID, mImageSize = entry.mImageSize; mBodySize = entry.mBodySize; mTime = entry.mTime; return *this;}
  60. LLUUID mID; // 16 bytes
  61. S32 mImageSize; // total size of image if known
  62. S32 mBodySize; // size of body file in body cache
  63. U32 mTime; // seconds since 1/1/1970
  64. };
  65. public:
  66. class Responder : public LLResponder
  67. {
  68. public:
  69. virtual void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal) = 0;
  70. };
  71. class ReadResponder : public Responder
  72. {
  73. public:
  74. ReadResponder();
  75. void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal);
  76. void setImage(LLImageFormatted* image) { mFormattedImage = image; }
  77. protected:
  78. LLPointer<LLImageFormatted> mFormattedImage;
  79. S32 mImageSize;
  80. BOOL mImageLocal;
  81. };
  82. class WriteResponder : public Responder
  83. {
  84. void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal)
  85. {
  86. // not used
  87. }
  88. };
  89. LLTextureCache(bool threaded);
  90. ~LLTextureCache();
  91. /*virtual*/ S32 update(F32 max_time_ms);
  92. void purgeCache(ELLPath location);
  93. void setReadOnly(BOOL read_only) ;
  94. S64 initCache(ELLPath location, S64 maxsize, BOOL texture_cache_mismatch);
  95. handle_t readFromCache(const std::string& local_filename, const LLUUID& id, U32 priority, S32 offset, S32 size,
  96. ReadResponder* responder);
  97. handle_t readFromCache(const LLUUID& id, U32 priority, S32 offset, S32 size,
  98. ReadResponder* responder);
  99. bool readComplete(handle_t handle, bool abort);
  100. handle_t writeToCache(const LLUUID& id, U32 priority, U8* data, S32 datasize, S32 imagesize,
  101. WriteResponder* responder);
  102. bool writeComplete(handle_t handle, bool abort = false);
  103. void prioritizeWrite(handle_t handle);
  104. bool removeFromCache(const LLUUID& id);
  105. // For LLTextureCacheWorker::Responder
  106. LLTextureCacheWorker* getReader(handle_t handle);
  107. LLTextureCacheWorker* getWriter(handle_t handle);
  108. void lockWorkers() { mWorkersMutex.lock(); }
  109. void unlockWorkers() { mWorkersMutex.unlock(); }
  110. // debug
  111. S32 getNumReads() { return mReaders.size(); }
  112. S32 getNumWrites() { return mWriters.size(); }
  113. S64 getUsage() { return mTexturesSizeTotal; }
  114. S64 getMaxUsage() { return sCacheMaxTexturesSize; }
  115. U32 getEntries() { return mHeaderEntriesInfo.mEntries; }
  116. U32 getMaxEntries() { return sCacheMaxEntries; };
  117. BOOL isInCache(const LLUUID& id) ;
  118. BOOL isInLocal(const LLUUID& id) ;
  119. protected:
  120. // Accessed by LLTextureCacheWorker
  121. std::string getLocalFileName(const LLUUID& id);
  122. std::string getTextureFileName(const LLUUID& id);
  123. void addCompleted(Responder* responder, bool success);
  124. protected:
  125. //void setFileAPRPool(apr_pool_t* pool) { mFileAPRPool = pool ; }
  126. private:
  127. void setDirNames(ELLPath location);
  128. void readHeaderCache();
  129. void clearCorruptedCache();
  130. void purgeAllTextures(bool purge_directories);
  131. void purgeTextures(bool validate);
  132. LLAPRFile* openHeaderEntriesFile(bool readonly, S32 offset);
  133. void closeHeaderEntriesFile();
  134. void readEntriesHeader();
  135. void writeEntriesHeader();
  136. S32 openAndReadEntry(const LLUUID& id, Entry& entry, bool create);
  137. bool updateEntry(S32& idx, Entry& entry, S32 new_image_size, S32 new_body_size);
  138. void updateEntryTimeStamp(S32 idx, Entry& entry) ;
  139. U32 openAndReadEntries(std::vector<Entry>& entries);
  140. void writeEntriesAndClose(const std::vector<Entry>& entries);
  141. void readEntryFromHeaderImmediately(S32& idx, Entry& entry) ;
  142. void writeEntryToHeaderImmediately(S32& idx, Entry& entry, bool write_header = false) ;
  143. void removeEntry(S32 idx, Entry& entry, std::string& filename);
  144. void removeCachedTexture(const LLUUID& id) ;
  145. S32 getHeaderCacheEntry(const LLUUID& id, Entry& entry);
  146. S32 setHeaderCacheEntry(const LLUUID& id, Entry& entry, S32 imagesize, S32 datasize);
  147. void writeUpdatedEntries() ;
  148. void updatedHeaderEntriesFile() ;
  149. void lockHeaders() { mHeaderMutex.lock(); }
  150. void unlockHeaders() { mHeaderMutex.unlock(); }
  151. private:
  152. // Internal
  153. LLMutex mWorkersMutex;
  154. LLMutex mHeaderMutex;
  155. LLMutex mListMutex;
  156. LLAPRFile* mHeaderAPRFile;
  157. typedef std::map<handle_t, LLTextureCacheWorker*> handle_map_t;
  158. handle_map_t mReaders;
  159. handle_map_t mWriters;
  160. typedef std::vector<handle_t> handle_list_t;
  161. handle_list_t mPrioritizeWriteList;
  162. typedef std::vector<std::pair<LLPointer<Responder>, bool> > responder_list_t;
  163. responder_list_t mCompletedList;
  164. BOOL mReadOnly;
  165. // HEADERS (Include first mip)
  166. std::string mHeaderEntriesFileName;
  167. std::string mHeaderDataFileName;
  168. EntriesInfo mHeaderEntriesInfo;
  169. std::set<S32> mFreeList; // deleted entries
  170. std::set<LLUUID> mLRU;
  171. typedef std::map<LLUUID,S32> id_map_t;
  172. id_map_t mHeaderIDMap;
  173. // BODIES (TEXTURES minus headers)
  174. std::string mTexturesDirName;
  175. typedef std::map<LLUUID,S32> size_map_t;
  176. size_map_t mTexturesSizeMap;
  177. S64 mTexturesSizeTotal;
  178. LLAtomic32<BOOL> mDoPurge;
  179. typedef std::map<S32, Entry> idx_entry_map_t;
  180. idx_entry_map_t mUpdatedEntryMap;
  181. // Statics
  182. static F32 sHeaderCacheVersion;
  183. static U32 sCacheMaxEntries;
  184. static S64 sCacheMaxTexturesSize;
  185. };
  186. extern const S32 TEXTURE_CACHE_ENTRY_SIZE;
  187. #endif // LL_LLTEXTURECACHE_H