| 1 |
using System; |
/* |
| 2 |
|
* this class quoted code from: |
| 3 |
|
* http://www.openmetaverse.org/viewvc/index.cgi/omf/libopenmetaverse/trunk/Programs/PrimWorkshop/TexturePipeline.cs?view=markup |
| 4 |
|
* |
| 5 |
|
*/ |
| 6 |
|
|
| 7 |
|
using System; |
| 8 |
using System.Collections.Generic; |
using System.Collections.Generic; |
| 9 |
using System.Linq; |
using System.Linq; |
| 10 |
using System.Text; |
using System.Text; |
| 26 |
{ |
{ |
| 27 |
private event TextureDownloadFinished onDownloadFinished; |
private event TextureDownloadFinished onDownloadFinished; |
| 28 |
|
|
|
//the textures have load in memory. |
|
|
// private readonly Dictionary<UUID, byte[]> CurruntTextures = new Dictionary<UUID, byte[]>(); |
|
|
|
|
| 29 |
private string cachefolderPath = EnvironmentSettings.TextureCacheFolder; |
private string cachefolderPath = EnvironmentSettings.TextureCacheFolder; |
| 30 |
private string bmpFilePath = EnvironmentSettings.TextureTempFolder; |
private string bmpFilePath = EnvironmentSettings.TextureTempFolder; |
| 31 |
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
| 41 |
private static int[] threadpoolSlots; |
private static int[] threadpoolSlots; |
| 42 |
|
|
| 43 |
// maximum allowed concurrent requests at once |
// maximum allowed concurrent requests at once |
| 44 |
const int MAX_TEXTURE_REQUESTS = 1; |
const int MAX_TEXTURE_REQUESTS = 10; |
| 45 |
|
|
| 46 |
private GridClient gridClient; |
private GridClient gridClient; |
| 47 |
|
|
| 56 |
|
|
| 57 |
#region Singleton |
#region Singleton |
| 58 |
|
|
| 59 |
private static readonly DefaultTexture singleton = new DefaultTexture(); |
private static volatile DefaultTexture singleton = new DefaultTexture(); |
| 60 |
|
|
| 61 |
public static DefaultTexture SingleTextureManager() |
public static DefaultTexture SingleTextureManager() |
| 62 |
{ |
{ |
| 66 |
private DefaultTexture() |
private DefaultTexture() |
| 67 |
{ |
{ |
| 68 |
RequestQueue = new Queue<UUID>(); |
RequestQueue = new Queue<UUID>(); |
|
|
|
| 69 |
CurrentRequests = new Dictionary<UUID, int>(MAX_TEXTURE_REQUESTS); |
CurrentRequests = new Dictionary<UUID, int>(MAX_TEXTURE_REQUESTS); |
| 70 |
|
|
| 71 |
resetEvents = new AutoResetEvent[MAX_TEXTURE_REQUESTS]; |
resetEvents = new AutoResetEvent[MAX_TEXTURE_REQUESTS]; |
| 103 |
|
|
| 104 |
while (true) |
while (true) |
| 105 |
{ |
{ |
|
lock (RequestQueue) |
|
|
//rwRequestQueue.EnterUpgradeableReadLock(); |
|
|
{ |
|
| 106 |
if (RequestQueue.Count > 0) |
if (RequestQueue.Count > 0) |
| 107 |
{ |
{ |
| 108 |
reqNbr = -1; |
reqNbr = -1; |
| 121 |
{ |
{ |
| 122 |
|
|
| 123 |
UUID requestID; |
UUID requestID; |
| 124 |
//rwRequestQueue.EnterWriteLock(); |
lock (RequestQueue) |
| 125 |
requestID = RequestQueue.Dequeue(); |
requestID = RequestQueue.Dequeue(); |
|
//rwRequestQueue.ExitWriteLock(); |
|
| 126 |
|
|
| 127 |
Logger.DebugLog(String.Format("Sending Worker thread new download request {0}", reqNbr)); |
Logger.DebugLog(String.Format("Sending Worker thread new download request {0}", reqNbr)); |
| 128 |
ThreadPool.QueueUserWorkItem(new WaitCallback(textureRequestDoWork), new TaskInfo(requestID, reqNbr)); |
ThreadPool.QueueUserWorkItem(new WaitCallback(textureRequestDoWork), new TaskInfo(requestID, reqNbr)); |
| 129 |
|
|
|
//rwRequestQueue.ExitUpgradeableReadLock(); |
|
|
|
|
| 130 |
continue; |
continue; |
| 131 |
} |
} |
| 132 |
} |
} |
|
} |
|
|
// rwRequestQueue.ExitUpgradeableReadLock(); |
|
| 133 |
|
|
| 134 |
Thread.Sleep(1000); |
Thread.Sleep(500); |
| 135 |
} |
} |
| 136 |
|
|
| 137 |
} |
} |
| 221 |
} |
} |
| 222 |
else |
else |
| 223 |
{ |
{ |
|
//rwCurrentRequests.EnterReadLock(); |
|
|
//rwRequestQueue.EnterWriteLock(); |
|
| 224 |
lock (RequestQueue) |
lock (RequestQueue) |
| 225 |
{ |
{ |
| 226 |
// Make sure we aren't already downloading the texture |
// Make sure we aren't already downloading the texture |
| 229 |
RequestQueue.Enqueue(textureID); |
RequestQueue.Enqueue(textureID); |
| 230 |
} |
} |
| 231 |
} |
} |
|
|
|
|
//rwRequestQueue.ExitWriteLock(); |
|
|
// rwCurrentRequests.ExitReadLock(); |
|
| 232 |
} |
} |
| 233 |
|
|
| 234 |
return image; |
return image; |
| 240 |
{ |
{ |
| 241 |
if (File.Exists(Path.Combine(bmpFilePath, textureID.ToString() + ".png")) == true) |
if (File.Exists(Path.Combine(bmpFilePath, textureID.ToString() + ".png")) == true) |
| 242 |
return; |
return; |
| 243 |
|
} |
| 244 |
if (gridClient.Assets.Cache.HasImage(textureID) == false) |
if (gridClient.Assets.Cache.HasImage(textureID) == false) |
| 245 |
{ |
{ |
|
|
|
| 246 |
//rwRequestQueue.EnterWriteLock(); |
//rwRequestQueue.EnterWriteLock(); |
| 247 |
//rwCurrentRequests.EnterReadLock(); |
//rwCurrentRequests.EnterReadLock(); |
| 248 |
lock (RequestQueue) |
lock (RequestQueue) |
| 254 |
//rwCurrentRequests.ExitReadLock(); |
//rwCurrentRequests.ExitReadLock(); |
| 255 |
//rwRequestQueue.ExitWriteLock(); |
//rwRequestQueue.ExitWriteLock(); |
| 256 |
} |
} |
| 257 |
|
else |
| 258 |
|
{ |
| 259 |
|
lock (objFileAccess) |
| 260 |
|
{ |
| 261 |
|
ImageDownload d = gridClient.Assets.Cache.GetCachedImage(textureID); |
| 262 |
|
SaveTexture2Image(d); |
| 263 |
|
} |
| 264 |
} |
} |
| 265 |
|
|
| 266 |
} |
} |
| 267 |
|
|
| 268 |
object objFileAccess = new object(); |
object objFileAccess = new object(); |