1
0

LLImageManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 HasUpdates()
  181. {
  182. J2KImage image = GetHighestPriorityImage();
  183. return image != null && image.IsDecoded;
  184. }
  185. public bool ProcessImageQueue(int packetsToSend)
  186. {
  187. int packetsSent = 0;
  188. while (packetsSent < packetsToSend)
  189. {
  190. J2KImage image = GetHighestPriorityImage();
  191. // If null was returned, the texture priority queue is currently empty
  192. if (image == null)
  193. break;
  194. if (image.IsDecoded)
  195. {
  196. int sent;
  197. bool imageDone = image.SendPackets(Client, packetsToSend - packetsSent, out sent);
  198. packetsSent += sent;
  199. // If the send is complete, destroy any knowledge of this transfer
  200. if (imageDone)
  201. RemoveImageFromQueue(image);
  202. }
  203. else
  204. {
  205. // TODO: This is a limitation of how LLImageManager is currently
  206. // written. Undecoded textures should not be going into the priority
  207. // queue, because a high priority undecoded texture will clog up the
  208. // pipeline for a client
  209. // m_log.DebugFormat(
  210. // "[LL IMAGE MANAGER]: Exiting image queue processing early on encountering undecoded image {0}",
  211. // image.TextureID);
  212. break;
  213. }
  214. }
  215. // if (packetsSent != 0)
  216. // m_log.DebugFormat("[LL IMAGE MANAGER]: Processed {0} packets from image queue", packetsSent);
  217. return m_priorityQueue.Count > 0;
  218. }
  219. /// <summary>
  220. /// Faux destructor
  221. /// </summary>
  222. public void Close()
  223. {
  224. m_shuttingdown = true;
  225. }
  226. /// <summary>
  227. /// Clear the image queue.
  228. /// </summary>
  229. /// <returns>The number of requests cleared.</returns>
  230. public int ClearImageQueue()
  231. {
  232. int requestsDeleted;
  233. lock (m_priorityQueue)
  234. {
  235. requestsDeleted = m_priorityQueue.Count;
  236. // Surprisingly, there doesn't seem to be a clear method at this time.
  237. while (!m_priorityQueue.IsEmpty)
  238. m_priorityQueue.DeleteMax();
  239. }
  240. return requestsDeleted;
  241. }
  242. /// <summary>
  243. /// Returns an array containing all the images in the queue.
  244. /// </summary>
  245. /// <returns></returns>
  246. public J2KImage[] GetImages()
  247. {
  248. lock (m_priorityQueue)
  249. return m_priorityQueue.ToArray();
  250. }
  251. #region Priority Queue Helpers
  252. private J2KImage GetHighestPriorityImage()
  253. {
  254. J2KImage image = null;
  255. lock (m_syncRoot)
  256. {
  257. if (m_priorityQueue.Count > 0)
  258. {
  259. try
  260. {
  261. image = m_priorityQueue.FindMax();
  262. }
  263. catch (Exception) { }
  264. }
  265. }
  266. return image;
  267. }
  268. private void AddImageToQueue(J2KImage image)
  269. {
  270. image.PriorityQueueHandle = null;
  271. lock (m_syncRoot)
  272. {
  273. try
  274. {
  275. m_priorityQueue.Add(ref image.PriorityQueueHandle, image);
  276. }
  277. catch (Exception) { }
  278. }
  279. }
  280. private void RemoveImageFromQueue(J2KImage image)
  281. {
  282. lock (m_syncRoot)
  283. {
  284. try
  285. {
  286. m_priorityQueue.Delete(image.PriorityQueueHandle);
  287. }
  288. catch (Exception) { }
  289. }
  290. }
  291. private void UpdateImageInQueue(J2KImage image)
  292. {
  293. lock (m_syncRoot)
  294. {
  295. try
  296. {
  297. m_priorityQueue.Replace(image.PriorityQueueHandle, image);
  298. }
  299. catch (Exception)
  300. {
  301. image.PriorityQueueHandle = null;
  302. m_priorityQueue.Add(ref image.PriorityQueueHandle, image);
  303. }
  304. }
  305. }
  306. #endregion Priority Queue Helpers
  307. }
  308. }