PageRenderTime 40ms CodeModel.GetById 25ms app.highlight 12ms RepoModel.GetById 1ms 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
  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
 27#ifndef LL_LLTEXTURECACHE_
 28#define LL_LLTEXTURECACHE_H
 29
 30#include "lldir.h"
 31#include "llstl.h"
 32#include "llstring.h"
 33#include "lluuid.h"
 34
 35#include "llworkerthread.h"
 36
 37class LLImageFormatted;
 38class LLTextureCacheWorker;
 39
 40class LLTextureCache : public LLWorkerThread
 41{
 42	friend class LLTextureCacheWorker;
 43	friend class LLTextureCacheRemoteWorker;
 44	friend class LLTextureCacheLocalFileWorker;
 45
 46private:
 47	// Entries
 48	struct EntriesInfo
 49	{
 50		EntriesInfo() : mVersion(0.f), mEntries(0) {}
 51		F32 mVersion;
 52		U32 mEntries;
 53	};
 54	struct Entry
 55	{
 56        	Entry() :
 57		        mBodySize(0),
 58			mImageSize(0),
 59			mTime(0)
 60		{
 61		}
 62		Entry(const LLUUID& id, S32 imagesize, S32 bodysize, U32 time) :
 63			mID(id), mImageSize(imagesize), mBodySize(bodysize), mTime(time) {}
 64		void init(const LLUUID& id, U32 time) { mID = id, mImageSize = 0; mBodySize = 0; mTime = time; }
 65		Entry& operator=(const Entry& entry) {mID = entry.mID, mImageSize = entry.mImageSize; mBodySize = entry.mBodySize; mTime = entry.mTime; return *this;}
 66		LLUUID mID; // 16 bytes
 67		S32 mImageSize; // total size of image if known
 68		S32 mBodySize; // size of body file in body cache
 69		U32 mTime; // seconds since 1/1/1970
 70	};
 71
 72	
 73public:
 74
 75	class Responder : public LLResponder
 76	{
 77	public:
 78		virtual void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal) = 0;
 79	};
 80	
 81	class ReadResponder : public Responder
 82	{
 83	public:
 84		ReadResponder();
 85		void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal);
 86		void setImage(LLImageFormatted* image) { mFormattedImage = image; }
 87	protected:
 88		LLPointer<LLImageFormatted> mFormattedImage;
 89		S32 mImageSize;
 90		BOOL mImageLocal;
 91	};
 92
 93	class WriteResponder : public Responder
 94	{
 95		void setData(U8* data, S32 datasize, S32 imagesize, S32 imageformat, BOOL imagelocal)
 96		{
 97			// not used
 98		}
 99	};
100	
101	LLTextureCache(bool threaded);
102	~LLTextureCache();
103
104	/*virtual*/ S32 update(F32 max_time_ms);	
105	
106	void purgeCache(ELLPath location);
107	void setReadOnly(BOOL read_only) ;
108	S64 initCache(ELLPath location, S64 maxsize, BOOL texture_cache_mismatch);
109
110	handle_t readFromCache(const std::string& local_filename, const LLUUID& id, U32 priority, S32 offset, S32 size,
111						   ReadResponder* responder);
112
113	handle_t readFromCache(const LLUUID& id, U32 priority, S32 offset, S32 size,
114						   ReadResponder* responder);
115	bool readComplete(handle_t handle, bool abort);
116	handle_t writeToCache(const LLUUID& id, U32 priority, U8* data, S32 datasize, S32 imagesize,
117						  WriteResponder* responder);
118	bool writeComplete(handle_t handle, bool abort = false);
119	void prioritizeWrite(handle_t handle);
120
121	bool removeFromCache(const LLUUID& id);
122
123	// For LLTextureCacheWorker::Responder
124	LLTextureCacheWorker* getReader(handle_t handle);
125	LLTextureCacheWorker* getWriter(handle_t handle);
126	void lockWorkers() { mWorkersMutex.lock(); }
127	void unlockWorkers() { mWorkersMutex.unlock(); }
128
129	// debug
130	S32 getNumReads() { return mReaders.size(); }
131	S32 getNumWrites() { return mWriters.size(); }
132	S64 getUsage() { return mTexturesSizeTotal; }
133	S64 getMaxUsage() { return sCacheMaxTexturesSize; }
134	U32 getEntries() { return mHeaderEntriesInfo.mEntries; }
135	U32 getMaxEntries() { return sCacheMaxEntries; };
136	BOOL isInCache(const LLUUID& id) ;
137	BOOL isInLocal(const LLUUID& id) ;
138
139protected:
140	// Accessed by LLTextureCacheWorker
141	std::string getLocalFileName(const LLUUID& id);
142	std::string getTextureFileName(const LLUUID& id);
143	void addCompleted(Responder* responder, bool success);
144	
145protected:
146	//void setFileAPRPool(apr_pool_t* pool) { mFileAPRPool = pool ; }
147
148private:
149	void setDirNames(ELLPath location);
150	void readHeaderCache();
151	void clearCorruptedCache();
152	void purgeAllTextures(bool purge_directories);
153	void purgeTextures(bool validate);
154	LLAPRFile* openHeaderEntriesFile(bool readonly, S32 offset);
155	void closeHeaderEntriesFile();
156	void readEntriesHeader();
157	void writeEntriesHeader();
158	S32 openAndReadEntry(const LLUUID& id, Entry& entry, bool create);
159	bool updateEntry(S32& idx, Entry& entry, S32 new_image_size, S32 new_body_size);
160	void updateEntryTimeStamp(S32 idx, Entry& entry) ;
161	U32 openAndReadEntries(std::vector<Entry>& entries);
162	void writeEntriesAndClose(const std::vector<Entry>& entries);
163	void readEntryFromHeaderImmediately(S32& idx, Entry& entry) ;
164	void writeEntryToHeaderImmediately(S32& idx, Entry& entry, bool write_header = false) ;
165	void removeEntry(S32 idx, Entry& entry, std::string& filename);
166	void removeCachedTexture(const LLUUID& id) ;
167	S32 getHeaderCacheEntry(const LLUUID& id, Entry& entry);
168	S32 setHeaderCacheEntry(const LLUUID& id, Entry& entry, S32 imagesize, S32 datasize);
169	void writeUpdatedEntries() ;
170	void updatedHeaderEntriesFile() ;
171	void lockHeaders() { mHeaderMutex.lock(); }
172	void unlockHeaders() { mHeaderMutex.unlock(); }
173	
174private:
175	// Internal
176	LLMutex mWorkersMutex;
177	LLMutex mHeaderMutex;
178	LLMutex mListMutex;
179	LLAPRFile* mHeaderAPRFile;
180	
181	typedef std::map<handle_t, LLTextureCacheWorker*> handle_map_t;
182	handle_map_t mReaders;
183	handle_map_t mWriters;
184
185	typedef std::vector<handle_t> handle_list_t;
186	handle_list_t mPrioritizeWriteList;
187
188	typedef std::vector<std::pair<LLPointer<Responder>, bool> > responder_list_t;
189	responder_list_t mCompletedList;
190	
191	BOOL mReadOnly;
192	
193	// HEADERS (Include first mip)
194	std::string mHeaderEntriesFileName;
195	std::string mHeaderDataFileName;
196	EntriesInfo mHeaderEntriesInfo;
197	std::set<S32> mFreeList; // deleted entries
198	std::set<LLUUID> mLRU;
199	typedef std::map<LLUUID,S32> id_map_t;
200	id_map_t mHeaderIDMap;
201
202	// BODIES (TEXTURES minus headers)
203	std::string mTexturesDirName;
204	typedef std::map<LLUUID,S32> size_map_t;
205	size_map_t mTexturesSizeMap;
206	S64 mTexturesSizeTotal;
207	LLAtomic32<BOOL> mDoPurge;
208
209	typedef std::map<S32, Entry> idx_entry_map_t;
210	idx_entry_map_t mUpdatedEntryMap;
211
212	// Statics
213	static F32 sHeaderCacheVersion;
214	static U32 sCacheMaxEntries;
215	static S64 sCacheMaxTexturesSize;
216};
217
218extern const S32 TEXTURE_CACHE_ENTRY_SIZE;
219
220#endif // LL_LLTEXTURECACHE_H