lltexturecache.h

Go to the documentation of this file.
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         friend class LLTextureCacheRemoteWorker;
00048         friend class LLTextureCacheLocalFileWorker;
00049 
00050 public:
00051 
00052         class Responder : public LLResponder
00053         {
00054         public:
00055                 virtual void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal) = 0;
00056         };
00057         
00058         class ReadResponder : public Responder
00059         {
00060         public:
00061                 ReadResponder();
00062                 void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal);
00063                 void setImage(LLImageFormatted* image) { mFormattedImage = image; }
00064         protected:
00065                 LLPointer<LLImageFormatted> mFormattedImage;
00066                 S32 mImageSize;
00067                 BOOL mImageLocal;
00068         };
00069 
00070         class WriteResponder : public Responder
00071         {
00072                 void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal)
00073                 {
00074                         // not used
00075                 }
00076         };
00077         
00078         LLTextureCache(bool threaded);
00079         ~LLTextureCache();
00080 
00081         /*virtual*/ S32 update(U32 max_time_ms);        
00082         
00083         void purgeCache(ELLPath location);
00084         S64 initCache(ELLPath location, S64 maxsize, BOOL read_only);
00085 
00086         handle_t readFromCache(const LLString& local_filename, const LLUUID& id, U32 priority, S32 offset, S32 size,
00087                                                    ReadResponder* responder);
00088 
00089         handle_t readFromCache(const LLUUID& id, U32 priority, S32 offset, S32 size,
00090                                                    ReadResponder* responder);
00091         bool readComplete(handle_t handle, bool abort);
00092         handle_t writeToCache(const LLUUID& id, U32 priority, U8* data, S32 datasize, S32 imagesize,
00093                                                   WriteResponder* responder);
00094         bool writeComplete(handle_t handle, bool abort = false);
00095         void prioritizeWrite(handle_t handle);
00096 
00097         void removeFromCache(const LLUUID& id);
00098 
00099         // For LLTextureCacheWorker::Responder
00100         LLTextureCacheWorker* getReader(handle_t handle);
00101         LLTextureCacheWorker* getWriter(handle_t handle);
00102         void lockWorkers() { mWorkersMutex.lock(); }
00103         void unlockWorkers() { mWorkersMutex.unlock(); }
00104 
00105         // debug
00106         S32 getNumReads() { return mReaders.size(); }
00107         S32 getNumWrites() { return mWriters.size(); }
00108 
00109 protected:
00110         // Accessed by LLTextureCacheWorker
00111         apr_pool_t* getFileAPRPool() { return mFileAPRPool; }
00112         bool appendToTextureEntryList(const LLUUID& id, S32 size);
00113         std::string getLocalFileName(const LLUUID& id);
00114         std::string getTextureFileName(const LLUUID& id);
00115         void addCompleted(Responder* responder, bool success);
00116         
00117 private:
00118         void setDirNames(ELLPath location);
00119         void readHeaderCache(apr_pool_t* poolp = NULL);
00120         void purgeAllTextures(bool purge_directories);
00121         void purgeTextures(bool validate);
00122         S32 getHeaderCacheEntry(const LLUUID& id, bool touch, S32* imagesize = NULL);
00123         bool removeHeaderCacheEntry(const LLUUID& id);
00124         void lockHeaders() { mHeaderMutex.lock(); }
00125         void unlockHeaders() { mHeaderMutex.unlock(); }
00126         
00127 private:
00128         // Internal
00129         LLMutex mWorkersMutex;
00130         LLMutex mHeaderMutex;
00131         LLMutex mListMutex;
00132         apr_pool_t* mFileAPRPool;
00133         
00134         typedef std::map<handle_t, LLTextureCacheWorker*> handle_map_t;
00135         handle_map_t mReaders;
00136         handle_map_t mWriters;
00137 
00138         typedef std::vector<handle_t> handle_list_t;
00139         handle_list_t mPrioritizeWriteList;
00140 
00141         typedef std::vector<std::pair<LLPointer<Responder>, bool> > responder_list_t;
00142         responder_list_t mCompletedList;
00143         
00144         BOOL mReadOnly;
00145         
00146         // Entries
00147         struct EntriesInfo
00148         {
00149                 F32 mVersion;
00150                 U32 mEntries;
00151         };
00152         struct Entry
00153         {
00154                 Entry() {}
00155                 Entry(const LLUUID& id, S32 size, U32 time) : mID(id), mSize(size), mTime(time) {}
00156                 LLUUID mID; // 128 bits
00157                 S32 mSize; // total size of image if known (NOT size cached)
00158                 U32 mTime; // seconds since 1/1/1970
00159         };
00160 
00161         // HEADERS (Include first mip)
00162         std::string mHeaderEntriesFileName;
00163         std::string mHeaderDataFileName;
00164         EntriesInfo mHeaderEntriesInfo;
00165         typedef std::map<S32,LLUUID> index_map_t;
00166         index_map_t mLRU; // index, id; stored as a map for fast removal
00167         typedef std::map<LLUUID,S32> id_map_t;
00168         id_map_t mHeaderIDMap;
00169 
00170         // BODIES (TEXTURES minus headers)
00171         std::string mTexturesDirName;
00172         std::string mTexturesDirEntriesFileName;
00173         typedef std::map<LLUUID,S32> size_map_t;
00174         size_map_t mTexturesSizeMap;
00175         S64 mTexturesSizeTotal;
00176         LLAtomic32<BOOL> mDoPurge;
00177         
00178         // Statics
00179         static F32 sHeaderCacheVersion;
00180         static U32 sCacheMaxEntries;
00181         static S64 sCacheMaxTexturesSize;
00182 };
00183 
00184 #endif // LL_LLTEXTURECACHE_H

Generated on Fri May 16 08:34:04 2008 for SecondLife by  doxygen 1.5.5