1
0

LLImageManager.cs 10 KB

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