LLImageManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Threading;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Reflection;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Imaging;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Services.Interfaces;
  37. using log4net;
  38. namespace OpenSim.Region.ClientStack.LindenUDP
  39. {
  40. /// <summary>
  41. /// This class handles UDP texture requests.
  42. /// </summary>
  43. public class LLImageManager
  44. {
  45. private sealed class J2KImageComparer : IComparer<J2KImage>
  46. {
  47. public int Compare(J2KImage x, J2KImage y)
  48. {
  49. return x.Priority.CompareTo(y.Priority);
  50. }
  51. }
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. private bool m_shuttingdown;
  54. private AssetBase m_missingImage;
  55. private IAssetService m_assetCache;
  56. private IJ2KDecoder m_j2kDecodeModule;
  57. /// <summary>
  58. /// Priority queue for determining which image to send first.
  59. /// </summary>
  60. private C5.IntervalHeap<J2KImage> m_priorityQueue = new C5.IntervalHeap<J2KImage>(10, new J2KImageComparer());
  61. /// <summary>
  62. /// Used to control thread access to the priority queue.
  63. /// </summary>
  64. private object m_syncRoot = new object();
  65. /// <summary>
  66. /// Client served by this image manager
  67. /// </summary>
  68. public IClientAPI Client { get; private set; }
  69. public AssetBase MissingImage { get { return m_missingImage; } }
  70. public LLImageManager(IClientAPI client, IAssetService pAssetCache, IJ2KDecoder pJ2kDecodeModule)
  71. {
  72. Client = client;
  73. m_assetCache = pAssetCache;
  74. if (pAssetCache != null)
  75. m_missingImage = pAssetCache.Get("5748decc-f629-461c-9a36-a35a221fe21f");
  76. if (m_missingImage == null)
  77. m_log.Error("[ClientView] - Couldn't set missing image asset, falling back to missing image packet. This is known to crash the client");
  78. m_j2kDecodeModule = pJ2kDecodeModule;
  79. }
  80. /// <summary>
  81. /// Handles an incoming texture request or update to an existing texture request
  82. /// </summary>
  83. /// <param name="newRequest"></param>
  84. public void EnqueueReq(TextureRequestArgs newRequest)
  85. {
  86. if (!m_shuttingdown)
  87. {
  88. J2KImage imgrequest;
  89. // Do a linear search for this texture download
  90. lock (m_syncRoot)
  91. m_priorityQueue.Find(delegate(J2KImage img) { return img.TextureID == newRequest.RequestedAssetID; }, out imgrequest);
  92. if (imgrequest != null)
  93. {
  94. if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f)
  95. {
  96. //m_log.Debug("[TEX]: (CAN) ID=" + newRequest.RequestedAssetID);
  97. try
  98. {
  99. lock (m_syncRoot)
  100. m_priorityQueue.Delete(imgrequest.PriorityQueueHandle);
  101. }
  102. catch (Exception) { }
  103. }
  104. else
  105. {
  106. // m_log.DebugFormat(
  107. // "[LL IMAGE MANAGER]: Received duplicate of existing request for {0}, start packet {1} from {2}",
  108. // newRequest.RequestedAssetID, newRequest.PacketNumber, m_client.Name);
  109. // m_log.DebugFormat("[TEX]: (UPD) ID={0}: D={1}, S={2}, P={3}",
  110. // newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
  111. //Check the packet sequence to make sure this isn't older than
  112. //one we've already received
  113. if (newRequest.requestSequence > imgrequest.LastSequence)
  114. {
  115. //Update the sequence number of the last RequestImage packet
  116. imgrequest.LastSequence = newRequest.requestSequence;
  117. //Update the requested discard level
  118. imgrequest.DiscardLevel = newRequest.DiscardLevel;
  119. //Update the requested packet number
  120. imgrequest.StartPacket = Math.Max(1, newRequest.PacketNumber);
  121. //Update the requested priority
  122. imgrequest.Priority = newRequest.Priority;
  123. UpdateImageInQueue(imgrequest);
  124. imgrequest.RunUpdate();
  125. // J2KImage imgrequest2 = new J2KImage(this);
  126. // imgrequest2.J2KDecoder = m_j2kDecodeModule;
  127. // imgrequest2.AssetService = m_assetCache;
  128. // imgrequest2.AgentID = m_client.AgentId;
  129. // imgrequest2.InventoryAccessModule = m_client.Scene.RequestModuleInterface<IInventoryAccessModule>();
  130. // imgrequest2.DiscardLevel = newRequest.DiscardLevel;
  131. // imgrequest2.StartPacket = Math.Max(1, newRequest.PacketNumber);
  132. // imgrequest2.Priority = newRequest.Priority;
  133. // imgrequest2.TextureID = newRequest.RequestedAssetID;
  134. // imgrequest2.Priority = newRequest.Priority;
  135. //
  136. // //Add this download to the priority queue
  137. // AddImageToQueue(imgrequest2);
  138. //
  139. // imgrequest2.RunUpdate();
  140. }
  141. // else
  142. // {
  143. // m_log.DebugFormat(
  144. // "[LL IMAGE MANAGER]: Ignoring duplicate of existing request for {0} (sequence {1}) from {2} as its request sequence {3} is not greater",
  145. // newRequest.RequestedAssetID, imgrequest.LastSequence, m_client.Name, newRequest.requestSequence);
  146. // }
  147. }
  148. }
  149. else
  150. {
  151. if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f)
  152. {
  153. //m_log.DebugFormat("[TEX]: (IGN) ID={0}: D={1}, S={2}, P={3}",
  154. // newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
  155. }
  156. else
  157. {
  158. // m_log.DebugFormat(
  159. // "[LL IMAGE MANAGER]: Received request for {0}, start packet {1} from {2}",
  160. // newRequest.RequestedAssetID, newRequest.PacketNumber, m_client.Name);
  161. //m_log.DebugFormat("[TEX]: (NEW) ID={0}: D={1}, S={2}, P={3}",
  162. // newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
  163. imgrequest = new J2KImage(this);
  164. imgrequest.J2KDecoder = m_j2kDecodeModule;
  165. imgrequest.AssetService = m_assetCache;
  166. imgrequest.AgentID = Client.AgentId;
  167. imgrequest.InventoryAccessModule = Client.Scene.RequestModuleInterface<IInventoryAccessModule>();
  168. imgrequest.DiscardLevel = newRequest.DiscardLevel;
  169. imgrequest.StartPacket = Math.Max(1, newRequest.PacketNumber);
  170. imgrequest.Priority = newRequest.Priority;
  171. imgrequest.TextureID = newRequest.RequestedAssetID;
  172. imgrequest.Priority = newRequest.Priority;
  173. //Add this download to the priority queue
  174. AddImageToQueue(imgrequest);
  175. imgrequest.RunUpdate();
  176. }
  177. }
  178. }
  179. }
  180. public bool ProcessImageQueue(int packetsToSend)
  181. {
  182. int packetsSent = 0;
  183. while (packetsSent < packetsToSend)
  184. {
  185. J2KImage image = GetHighestPriorityImage();
  186. // If null was returned, the texture priority queue is currently empty
  187. if (image == null)
  188. break;
  189. if (image.IsDecoded)
  190. {
  191. int sent;
  192. bool imageDone = image.SendPackets(Client, packetsToSend - packetsSent, out sent);
  193. packetsSent += sent;
  194. // If the send is complete, destroy any knowledge of this transfer
  195. if (imageDone)
  196. RemoveImageFromQueue(image);
  197. }
  198. else
  199. {
  200. // TODO: This is a limitation of how LLImageManager is currently
  201. // written. Undecoded textures should not be going into the priority
  202. // queue, because a high priority undecoded texture will clog up the
  203. // pipeline for a client
  204. // m_log.DebugFormat(
  205. // "[LL IMAGE MANAGER]: Exiting image queue processing early on encountering undecoded image {0}",
  206. // image.TextureID);
  207. break;
  208. }
  209. }
  210. // if (packetsSent != 0)
  211. // m_log.DebugFormat("[LL IMAGE MANAGER]: Processed {0} packets from image queue", packetsSent);
  212. return m_priorityQueue.Count > 0;
  213. }
  214. /// <summary>
  215. /// Faux destructor
  216. /// </summary>
  217. public void Close()
  218. {
  219. m_shuttingdown = true;
  220. }
  221. /// <summary>
  222. /// Clear the image queue.
  223. /// </summary>
  224. /// <returns>The number of requests cleared.</returns>
  225. public int ClearImageQueue()
  226. {
  227. int requestsDeleted;
  228. lock (m_priorityQueue)
  229. {
  230. requestsDeleted = m_priorityQueue.Count;
  231. // Surprisingly, there doesn't seem to be a clear method at this time.
  232. while (!m_priorityQueue.IsEmpty)
  233. m_priorityQueue.DeleteMax();
  234. }
  235. return requestsDeleted;
  236. }
  237. /// <summary>
  238. /// Returns an array containing all the images in the queue.
  239. /// </summary>
  240. /// <returns></returns>
  241. public J2KImage[] GetImages()
  242. {
  243. lock (m_priorityQueue)
  244. return m_priorityQueue.ToArray();
  245. }
  246. #region Priority Queue Helpers
  247. private J2KImage GetHighestPriorityImage()
  248. {
  249. J2KImage image = null;
  250. lock (m_syncRoot)
  251. {
  252. if (m_priorityQueue.Count > 0)
  253. {
  254. try
  255. {
  256. image = m_priorityQueue.FindMax();
  257. }
  258. catch (Exception) { }
  259. }
  260. }
  261. return image;
  262. }
  263. private void AddImageToQueue(J2KImage image)
  264. {
  265. image.PriorityQueueHandle = null;
  266. lock (m_syncRoot)
  267. {
  268. try
  269. {
  270. m_priorityQueue.Add(ref image.PriorityQueueHandle, image);
  271. }
  272. catch (Exception) { }
  273. }
  274. }
  275. private void RemoveImageFromQueue(J2KImage image)
  276. {
  277. lock (m_syncRoot)
  278. {
  279. try
  280. {
  281. m_priorityQueue.Delete(image.PriorityQueueHandle);
  282. }
  283. catch (Exception) { }
  284. }
  285. }
  286. private void UpdateImageInQueue(J2KImage image)
  287. {
  288. lock (m_syncRoot)
  289. {
  290. try
  291. {
  292. m_priorityQueue.Replace(image.PriorityQueueHandle, image);
  293. }
  294. catch (Exception)
  295. {
  296. image.PriorityQueueHandle = null;
  297. m_priorityQueue.Add(ref image.PriorityQueueHandle, image);
  298. }
  299. }
  300. }
  301. #endregion Priority Queue Helpers
  302. }
  303. }