| 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; |
| 22 |
namespace Xenki.DefaultRenderer |
namespace Xenki.DefaultRenderer |
| 23 |
{ |
{ |
| 24 |
|
|
| 25 |
public class DefaultTexture |
public class DefaultTexture :ITextureManage |
| 26 |
{ |
{ |
| 27 |
private event TextureDownloadFinished onDownloadFinished; |
//private event TextureDownloadFinished m_OnDownloadFinished ; |
|
|
|
|
//the textures have load in memory. |
|
|
// private readonly Dictionary<UUID, byte[]> CurruntTextures = new Dictionary<UUID, byte[]>(); |
|
| 28 |
|
|
| 29 |
private string cachefolderPath = EnvironmentSettings.TextureCacheFolder; |
private string cachefolderPath = EnvironmentSettings.TextureCacheFolder; |
| 30 |
private string bmpFilePath = EnvironmentSettings.TextureTempFolder; |
private string bmpFilePath = EnvironmentSettings.TextureTempFolder; |
| 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 = EnvironmentSettings.DownloadTextureThreadNumber; |
| 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 |
} |
} |
| 187 |
if (image.Success) |
if (image.Success) |
| 188 |
{ |
{ |
| 189 |
lock (objFileAccess) |
lock (objFileAccess) |
| 190 |
if (File.Exists(Path.Combine(bmpFilePath, image.ID.ToString() + ".png")) == false) |
if (File.Exists(Path.Combine(bmpFilePath, |
| 191 |
|
image.ID.ToString() + References.EnvironmentSettings.TextureImageExtension)) == false) |
| 192 |
{ |
{ |
| 193 |
SaveTexture2Image(image); |
SaveTexture2Image(image); |
| 194 |
} |
} |
| 195 |
} |
} |
| 196 |
|
|
| 197 |
if (onDownloadFinished != null) |
if (OnDownloadFinished != null) |
| 198 |
{ |
{ |
| 199 |
onDownloadFinished(image.ID, image.Success); |
OnDownloadFinished(image.ID, image.Success); |
| 200 |
} |
} |
| 201 |
} |
} |
| 202 |
|
|
| 203 |
ManagedImage managedimg; |
ManagedImage managedimg=null; |
| 204 |
Image img; Bitmap bitmap; |
Image img; Bitmap bitmap; |
| 205 |
private void SaveTexture2Image(ImageDownload image) |
private void SaveTexture2Image(ImageDownload image) |
| 206 |
{ |
{ |
| 207 |
|
bool alpha = false; |
| 208 |
|
|
| 209 |
if (OpenJPEG.DecodeToImage(image.AssetData, out managedimg, out img)) |
if (OpenJPEG.DecodeToImage(image.AssetData, out managedimg, out img)) |
| 210 |
{ |
{ |
| 211 |
|
|
| 212 |
|
if (managedimg != null) |
| 213 |
|
{ |
| 214 |
|
if ((managedimg.Channels & ManagedImage.ImageChannels.Alpha) != 0) |
| 215 |
|
alpha = true; |
| 216 |
|
} |
| 217 |
|
//System.Drawing.Image pngimage = new Bitmap(img); |
| 218 |
bitmap = new Bitmap(img); |
bitmap = new Bitmap(img); |
| 219 |
bitmap.Save(Path.Combine(bmpFilePath, image.ID.ToString() + ".png"), |
|
| 220 |
System.Drawing.Imaging.ImageFormat.Png); |
//if (alpha) |
| 221 |
|
// bitmap.MakeTransparent(); |
| 222 |
|
//System.Drawing.Imaging.EncoderParameters eps = new System.Drawing.Imaging.EncoderParameters(); |
| 223 |
|
//eps.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.ColorDepth |
| 224 |
|
|
| 225 |
|
bitmap.Save(Path.Combine(bmpFilePath, image.ID.ToString() + |
| 226 |
|
References.EnvironmentSettings.TextureImageExtension)); |
| 227 |
} |
} |
| 228 |
} |
} |
| 229 |
|
|
| 237 |
} |
} |
| 238 |
else |
else |
| 239 |
{ |
{ |
|
//rwCurrentRequests.EnterReadLock(); |
|
|
//rwRequestQueue.EnterWriteLock(); |
|
| 240 |
lock (RequestQueue) |
lock (RequestQueue) |
| 241 |
{ |
{ |
| 242 |
// Make sure we aren't already downloading the texture |
// Make sure we aren't already downloading the texture |
| 245 |
RequestQueue.Enqueue(textureID); |
RequestQueue.Enqueue(textureID); |
| 246 |
} |
} |
| 247 |
} |
} |
|
|
|
|
//rwRequestQueue.ExitWriteLock(); |
|
|
// rwCurrentRequests.ExitReadLock(); |
|
| 248 |
} |
} |
| 249 |
|
|
| 250 |
return image; |
return image; |
| 254 |
{ |
{ |
| 255 |
lock (objFileAccess) |
lock (objFileAccess) |
| 256 |
{ |
{ |
| 257 |
if (File.Exists(Path.Combine(bmpFilePath, textureID.ToString() + ".png")) == true) |
if (File.Exists(Path.Combine(bmpFilePath, textureID.ToString() + |
| 258 |
|
References.EnvironmentSettings.TextureImageExtension)) == true) |
| 259 |
return; |
return; |
| 260 |
|
} |
| 261 |
if (gridClient.Assets.Cache.HasImage(textureID) == false) |
if (gridClient.Assets.Cache.HasImage(textureID) == false) |
| 262 |
{ |
{ |
|
|
|
| 263 |
//rwRequestQueue.EnterWriteLock(); |
//rwRequestQueue.EnterWriteLock(); |
| 264 |
//rwCurrentRequests.EnterReadLock(); |
//rwCurrentRequests.EnterReadLock(); |
| 265 |
lock (RequestQueue) |
lock (RequestQueue) |
| 271 |
//rwCurrentRequests.ExitReadLock(); |
//rwCurrentRequests.ExitReadLock(); |
| 272 |
//rwRequestQueue.ExitWriteLock(); |
//rwRequestQueue.ExitWriteLock(); |
| 273 |
} |
} |
| 274 |
|
else |
| 275 |
|
{ |
| 276 |
|
//lock (objFileAccess) |
| 277 |
|
//{ |
| 278 |
|
// ImageDownload d = gridClient.Assets.Cache.GetCachedImage(textureID); |
| 279 |
|
// SaveTexture2Image(d); |
| 280 |
|
//} |
| 281 |
} |
} |
| 282 |
|
|
| 283 |
} |
} |
| 284 |
|
|
| 285 |
object objFileAccess = new object(); |
object objFileAccess = new object(); |
| 286 |
|
public event TextureDownloadFinished OnDownloadFinished; |
| 287 |
|
|
| 288 |
|
|
|
public TextureDownloadFinished OnDownloadFinished |
|
|
{ |
|
|
get |
|
|
{ |
|
|
return onDownloadFinished; |
|
|
} |
|
|
set |
|
|
{ |
|
|
onDownloadFinished = value; |
|
|
} |
|
|
} |
|
| 289 |
} |
} |
| 290 |
|
|
| 291 |
class TaskInfo |
class TaskInfo |