00001
00032 #ifndef LL_LLTEXTURECACHE_
00033 #define LL_LLTEXTURECACHE_H
00034
00035 #include "lldir.h"
00036 #include "llstl.h"
00037 #include "llstring.h"
00038 #include "lluuid.h"
00039
00040 #include "llworkerthread.h"
00041
00042 class LLTextureCacheWorker;
00043
00044 class LLTextureCache : public LLWorkerThread
00045 {
00046 friend class LLTextureCacheWorker;
00047
00048 public:
00049
00050 class Responder : public LLResponder
00051 {
00052 public:
00053 virtual void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal) = 0;
00054 };
00055
00056 class ReadResponder : public Responder
00057 {
00058 public:
00059 ReadResponder();
00060 void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal);
00061 void setImage(LLImageFormatted* image) { mFormattedImage = image; }
00062 protected:
00063 LLPointer<LLImageFormatted> mFormattedImage;
00064 S32 mImageSize;
00065 BOOL mImageLocal;
00066 };
00067
00068 class WriteResponder : public Responder
00069 {
00070 void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal)
00071 {
00072
00073 }
00074 };
00075
00076 LLTextureCache(bool threaded);
00077 ~LLTextureCache();
00078
00079 S32 update(U32 max_time_ms);
00080
00081 void purgeCache(ELLPath location);
00082 S64 initCache(ELLPath location, S64 maxsize, BOOL read_only);
00083
00084 handle_t readFromCache(const LLUUID& id, U32 priority, S32 offset, S32 size,
00085 ReadResponder* responder);
00086 bool readComplete(handle_t handle, bool abort);
00087 handle_t writeToCache(const LLUUID& id, U32 priority, U8* data, S32 datasize, S32 imagesize,
00088 WriteResponder* responder);
00089 bool writeComplete(handle_t handle, bool abort = false);
00090 void prioritizeWrite(handle_t handle);
00091
00092 void removeFromCache(const LLUUID& id);
00093
00094
00095 LLTextureCacheWorker* getReader(handle_t handle);
00096 LLTextureCacheWorker* getWriter(handle_t handle);
00097 void lockWorkers() { mWorkersMutex.lock(); }
00098 void unlockWorkers() { mWorkersMutex.unlock(); }
00099
00100
00101 S32 getNumReads() { return mReaders.size(); }
00102 S32 getNumWrites() { return mWriters.size(); }
00103
00104 protected:
00105
00106 apr_pool_t* getFileAPRPool() { return mFileAPRPool; }
00107 bool appendToTextureEntryList(const LLUUID& id, S32 size);
00108 std::string getLocalFileName(const LLUUID& id);
00109 std::string getTextureFileName(const LLUUID& id);
00110 void addCompleted(Responder* responder, bool success);
00111
00112 private:
00113 void setDirNames(ELLPath location);
00114 void readHeaderCache(apr_pool_t* poolp = NULL);
00115 void purgeAllTextures(bool purge_directories);
00116 void purgeTextures(bool validate);
00117 S32 getHeaderCacheEntry(const LLUUID& id, bool touch, S32* imagesize = NULL);
00118 bool removeHeaderCacheEntry(const LLUUID& id);
00119 void lockHeaders() { mHeaderMutex.lock(); }
00120 void unlockHeaders() { mHeaderMutex.unlock(); }
00121
00122 private:
00123
00124 LLMutex mWorkersMutex;
00125 LLMutex mHeaderMutex;
00126 LLMutex mListMutex;
00127 apr_pool_t* mFileAPRPool;
00128
00129 typedef std::map<handle_t, LLTextureCacheWorker*> handle_map_t;
00130 handle_map_t mReaders;
00131 handle_map_t mWriters;
00132
00133 typedef std::vector<handle_t> handle_list_t;
00134 handle_list_t mPrioritizeWriteList;
00135
00136 typedef std::vector<std::pair<LLPointer<Responder>, bool> > responder_list_t;
00137 responder_list_t mCompletedList;
00138
00139 BOOL mReadOnly;
00140
00141
00142 struct EntriesInfo
00143 {
00144 F32 mVersion;
00145 U32 mEntries;
00146 };
00147 struct Entry
00148 {
00149 Entry() {}
00150 Entry(const LLUUID& id, S32 size, U32 time) : mID(id), mSize(size), mTime(time) {}
00151 LLUUID mID;
00152 S32 mSize;
00153 U32 mTime;
00154 };
00155
00156
00157 std::string mHeaderEntriesFileName;
00158 std::string mHeaderDataFileName;
00159 EntriesInfo mHeaderEntriesInfo;
00160 typedef std::map<S32,LLUUID> index_map_t;
00161 index_map_t mLRU;
00162 typedef std::map<LLUUID,S32> id_map_t;
00163 id_map_t mHeaderIDMap;
00164
00165
00166 std::string mTexturesDirName;
00167 std::string mTexturesDirEntriesFileName;
00168 typedef std::map<LLUUID,S32> size_map_t;
00169 size_map_t mTexturesSizeMap;
00170 S64 mTexturesSizeTotal;
00171 LLAtomic32<BOOL> mDoPurge;
00172
00173
00174 static F32 sHeaderCacheVersion;
00175 static U32 sCacheMaxEntries;
00176 static S64 sCacheMaxTexturesSize;
00177 };
00178
00179 #endif // LL_LLTEXTURECACHE_H