LLImageManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. public class LLImageManager
  41. {
  42. private sealed class J2KImageComparer : IComparer<J2KImage>
  43. {
  44. public int Compare(J2KImage x, J2KImage y)
  45. {
  46. return x.Priority.CompareTo(y.Priority);
  47. }
  48. }
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private bool m_shuttingdown;
  51. private AssetBase m_missingImage;
  52. private LLClientView m_client; //Client we're assigned to
  53. private IAssetService m_assetCache; //Asset Cache
  54. private IJ2KDecoder m_j2kDecodeModule; //Our J2K module
  55. private C5.IntervalHeap<J2KImage> m_priorityQueue = new C5.IntervalHeap<J2KImage>(10, new J2KImageComparer());
  56. private object m_syncRoot = new object();
  57. private IHyperAssetService m_hyperAssets;
  58. public LLClientView Client { get { return m_client; } }
  59. public AssetBase MissingImage { get { return m_missingImage; } }
  60. public LLImageManager(LLClientView client, IAssetService pAssetCache, IJ2KDecoder pJ2kDecodeModule)
  61. {
  62. m_client = client;
  63. m_assetCache = pAssetCache;
  64. if (pAssetCache != null)
  65. m_missingImage = pAssetCache.Get("5748decc-f629-461c-9a36-a35a221fe21f");
  66. if (m_missingImage == null)
  67. m_log.Error("[ClientView] - Couldn't set missing image asset, falling back to missing image packet. This is known to crash the client");
  68. m_j2kDecodeModule = pJ2kDecodeModule;
  69. m_hyperAssets = client.Scene.RequestModuleInterface<IHyperAssetService>();
  70. }
  71. /// <summary>
  72. /// Handles an incoming texture request or update to an existing texture request
  73. /// </summary>
  74. /// <param name="newRequest"></param>
  75. public void EnqueueReq(TextureRequestArgs newRequest)
  76. {
  77. //Make sure we're not shutting down..
  78. if (!m_shuttingdown)
  79. {
  80. J2KImage imgrequest;
  81. // Do a linear search for this texture download
  82. lock (m_syncRoot)
  83. m_priorityQueue.Find(delegate(J2KImage img) { return img.TextureID == newRequest.RequestedAssetID; }, out imgrequest);
  84. if (imgrequest != null)
  85. {
  86. if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f)
  87. {
  88. //m_log.Debug("[TEX]: (CAN) ID=" + newRequest.RequestedAssetID);
  89. try
  90. {
  91. lock (m_syncRoot)
  92. m_priorityQueue.Delete(imgrequest.PriorityQueueHandle);
  93. }
  94. catch (Exception) { }
  95. }
  96. else
  97. {
  98. //m_log.DebugFormat("[TEX]: (UPD) ID={0}: D={1}, S={2}, P={3}",
  99. // newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
  100. //Check the packet sequence to make sure this isn't older than
  101. //one we've already received
  102. if (newRequest.requestSequence > imgrequest.LastSequence)
  103. {
  104. //Update the sequence number of the last RequestImage packet
  105. imgrequest.LastSequence = newRequest.requestSequence;
  106. //Update the requested discard level
  107. imgrequest.DiscardLevel = newRequest.DiscardLevel;
  108. //Update the requested packet number
  109. imgrequest.StartPacket = Math.Max(1, newRequest.PacketNumber);
  110. //Update the requested priority
  111. imgrequest.Priority = newRequest.Priority;
  112. UpdateImageInQueue(imgrequest);
  113. //Run an update
  114. imgrequest.RunUpdate();
  115. }
  116. }
  117. }
  118. else
  119. {
  120. if (newRequest.DiscardLevel == -1 && newRequest.Priority == 0f)
  121. {
  122. //m_log.DebugFormat("[TEX]: (IGN) ID={0}: D={1}, S={2}, P={3}",
  123. // newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
  124. }
  125. else
  126. {
  127. //m_log.DebugFormat("[TEX]: (NEW) ID={0}: D={1}, S={2}, P={3}",
  128. // newRequest.RequestedAssetID, newRequest.DiscardLevel, newRequest.PacketNumber, newRequest.Priority);
  129. imgrequest = new J2KImage(this);
  130. imgrequest.J2KDecoder = m_j2kDecodeModule;
  131. imgrequest.AssetService = m_assetCache;
  132. imgrequest.AgentID = m_client.AgentId;
  133. imgrequest.HyperAssets = m_hyperAssets;
  134. imgrequest.DiscardLevel = newRequest.DiscardLevel;
  135. imgrequest.StartPacket = Math.Max(1, newRequest.PacketNumber);
  136. imgrequest.Priority = newRequest.Priority;
  137. imgrequest.TextureID = newRequest.RequestedAssetID;
  138. imgrequest.Priority = newRequest.Priority;
  139. //Add this download to the priority queue
  140. AddImageToQueue(imgrequest);
  141. //Run an update
  142. imgrequest.RunUpdate();
  143. }
  144. }
  145. }
  146. }
  147. public bool ProcessImageQueue(int packetsToSend)
  148. {
  149. int packetsSent = 0;
  150. while (packetsSent < packetsToSend)
  151. {
  152. J2KImage image = GetHighestPriorityImage();
  153. // If null was returned, the texture priority queue is currently empty
  154. if (image == null)
  155. return false;
  156. if (image.IsDecoded)
  157. {
  158. int sent;
  159. bool imageDone = image.SendPackets(m_client, packetsToSend - packetsSent, out sent);
  160. packetsSent += sent;
  161. // If the send is complete, destroy any knowledge of this transfer
  162. if (imageDone)
  163. RemoveImageFromQueue(image);
  164. }
  165. else
  166. {
  167. // TODO: This is a limitation of how LLImageManager is currently
  168. // written. Undecoded textures should not be going into the priority
  169. // queue, because a high priority undecoded texture will clog up the
  170. // pipeline for a client
  171. return true;
  172. }
  173. }
  174. return m_priorityQueue.Count > 0;
  175. }
  176. /// <summary>
  177. /// Faux destructor
  178. /// </summary>
  179. public void Close()
  180. {
  181. m_shuttingdown = true;
  182. }
  183. #region Priority Queue Helpers
  184. J2KImage GetHighestPriorityImage()
  185. {
  186. J2KImage image = null;
  187. lock (m_syncRoot)
  188. {
  189. if (m_priorityQueue.Count > 0)
  190. {
  191. try { image = m_priorityQueue.FindMax(); }
  192. catch (Exception) { }
  193. }
  194. }
  195. return image;
  196. }
  197. void AddImageToQueue(J2KImage image)
  198. {
  199. image.PriorityQueueHandle = null;
  200. lock (m_syncRoot)
  201. try { m_priorityQueue.Add(ref image.PriorityQueueHandle, image); }
  202. catch (Exception) { }
  203. }
  204. void RemoveImageFromQueue(J2KImage image)
  205. {
  206. lock (m_syncRoot)
  207. try { m_priorityQueue.Delete(image.PriorityQueueHandle); }
  208. catch (Exception) { }
  209. }
  210. void UpdateImageInQueue(J2KImage image)
  211. {
  212. lock (m_syncRoot)
  213. {
  214. try { m_priorityQueue.Replace(image.PriorityQueueHandle, image); }
  215. catch (Exception)
  216. {
  217. image.PriorityQueueHandle = null;
  218. m_priorityQueue.Add(ref image.PriorityQueueHandle, image);
  219. }
  220. }
  221. }
  222. #endregion Priority Queue Helpers
  223. }
  224. }