2009-04-13 14:52:14 +00:00
/ *
* Copyright ( c ) Contributors , http : //opensimulator.org/
* See CONTRIBUTORS . TXT for a full list of copyright holders .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
* * Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
* * Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
2009-06-01 06:37:14 +00:00
* * Neither the name of the OpenSimulator Project nor the
2009-04-13 14:52:14 +00:00
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission .
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ` ` AS IS ' ' AND ANY
* EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED . IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
* ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
* LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
* /
using System ;
using System.Collections.Generic ;
using OpenMetaverse ;
using OpenMetaverse.Imaging ;
using OpenSim.Framework ;
using OpenSim.Region.Framework.Interfaces ;
2009-05-15 05:00:25 +00:00
using OpenSim.Services.Interfaces ;
2009-04-13 14:52:14 +00:00
using log4net ;
using System.Reflection ;
namespace OpenSim.Region.ClientStack.LindenUDP
{
2009-05-05 09:59:15 +00:00
/// <summary>
2009-10-02 00:42:13 +00:00
/// Stores information about a current texture download and a reference to the texture asset
2009-05-05 09:59:15 +00:00
/// </summary>
2009-04-13 14:52:14 +00:00
public class J2KImage
{
2009-10-02 00:42:13 +00:00
private const int IMAGE_PACKET_SIZE = 1000 ;
private const int FIRST_PACKET_SIZE = 600 ;
2009-04-13 14:52:14 +00:00
private static readonly ILog m_log = LogManager . GetLogger ( MethodBase . GetCurrentMethod ( ) . DeclaringType ) ;
2009-05-05 09:59:15 +00:00
2009-10-02 20:08:15 +00:00
public uint LastSequence ;
public float Priority ;
public uint StartPacket ;
public sbyte DiscardLevel ;
public UUID TextureID ;
public IJ2KDecoder J2KDecoder ;
public IAssetService AssetService ;
2009-10-13 00:00:01 +00:00
public UUID AgentID ;
2010-07-04 21:59:15 +00:00
public IInventoryAccessModule InventoryAccessModule ;
2012-01-02 21:31:42 +00:00
private OpenJPEG . J2KLayerInfo [ ] m_layers ;
public bool IsDecoded { get ; private set ; }
public bool HasAsset { get ; private set ; }
2009-10-02 20:08:15 +00:00
public C5 . IPriorityQueueHandle < J2KImage > PriorityQueueHandle ;
private uint m_currentPacket ;
private bool m_decodeRequested ;
private bool m_assetRequested ;
private bool m_sentInfo ;
2009-10-02 00:42:13 +00:00
private uint m_stopPacket ;
2009-10-02 20:08:15 +00:00
private byte [ ] m_asset ;
2009-10-02 00:42:13 +00:00
private LLImageManager m_imageManager ;
2009-09-30 17:59:10 +00:00
2009-10-02 00:42:13 +00:00
public J2KImage ( LLImageManager imageManager )
2009-04-13 14:52:14 +00:00
{
2009-10-02 00:42:13 +00:00
m_imageManager = imageManager ;
2009-04-13 14:52:14 +00:00
}
2009-10-14 18:43:31 +00:00
/// <summary>
/// Sends packets for this texture to a client until packetsToSend is
/// hit or the transfer completes
/// </summary>
/// <param name="client">Reference to the client that the packets are destined for</param>
/// <param name="packetsToSend">Maximum number of packets to send during this call</param>
/// <param name="packetsSent">Number of packets sent during this call</param>
/// <returns>True if the transfer completes at the current discard level, otherwise false</returns>
public bool SendPackets ( LLClientView client , int packetsToSend , out int packetsSent )
2009-04-13 14:52:14 +00:00
{
2009-10-14 18:43:31 +00:00
packetsSent = 0 ;
2009-10-08 14:55:06 +00:00
2009-10-02 20:08:15 +00:00
if ( m_currentPacket < = m_stopPacket )
2009-04-13 14:52:14 +00:00
{
2009-10-06 09:38:00 +00:00
bool sendMore = true ;
2009-10-02 20:08:15 +00:00
if ( ! m_sentInfo | | ( m_currentPacket = = 0 ) )
2009-10-02 00:42:13 +00:00
{
2009-10-06 09:38:00 +00:00
sendMore = ! SendFirstPacket ( client ) ;
2009-10-02 20:08:15 +00:00
m_sentInfo = true ;
2009-10-06 09:38:00 +00:00
+ + m_currentPacket ;
2009-10-14 18:43:31 +00:00
+ + packetsSent ;
2009-10-02 00:42:13 +00:00
}
2009-10-02 20:08:15 +00:00
if ( m_currentPacket < 2 )
2009-10-02 00:42:13 +00:00
{
2009-10-02 20:08:15 +00:00
m_currentPacket = 2 ;
2009-10-02 00:42:13 +00:00
}
2009-10-06 09:38:00 +00:00
2009-10-14 18:43:31 +00:00
while ( sendMore & & packetsSent < packetsToSend & & m_currentPacket < = m_stopPacket )
2009-10-02 00:42:13 +00:00
{
2009-10-06 09:38:00 +00:00
sendMore = SendPacket ( client ) ;
+ + m_currentPacket ;
2009-10-14 18:43:31 +00:00
+ + packetsSent ;
2009-10-02 00:42:13 +00:00
}
Thank you kindly, RemedyTomm for a patch that:
Following feedback from 0003440, i've made some changes to the new texture pipeline to optimise
performance. The changes are:
- Fixed a math issue where a small percentage of images with a certain size (on the packet boundary) would not have their final data delivered. This issue has been present since pre- 0003440
- It was suggested that a discardlevel of -1 and a prioriy of 0 meant to abandon the transfer, this is incorrect and caused some textures to clog.
- The texture throttle blocking queue is now only filled in relation to the actual throttle amount.. i.e, on a connection throttled to 300k, only twenty packets will be placed in the queue at a time, on a larger connection it will be much more. This is to balance responsiveness to requests and speed, and to minimise wasted packets.
- The engine now keeps track of the number of pending textures, and the stack will not be walked if there's no textures pending, saving CPU. Textures are only considered "pending" when they've already been decoded.
- As part of the above, some textures may receive twice as much data per cycle if the number of pending textures is below the cycle threshold, this should prevent loading from slowing down when there are fewer textures in the queue.
2009-04-18 18:35:03 +00:00
}
2009-04-13 14:52:14 +00:00
2009-10-14 18:43:31 +00:00
return ( m_currentPacket > m_stopPacket ) ;
2009-04-13 14:52:14 +00:00
}
2009-04-13 15:08:06 +00:00
2009-10-02 00:42:13 +00:00
public void RunUpdate ( )
2009-04-13 14:52:14 +00:00
{
2009-10-02 00:42:13 +00:00
//This is where we decide what we need to update
//and assign the real discardLevel and packetNumber
//assuming of course that the connected client might be bonkers
2009-10-02 20:08:15 +00:00
if ( ! HasAsset )
2009-04-13 14:52:14 +00:00
{
2009-10-02 20:08:15 +00:00
if ( ! m_assetRequested )
2009-10-02 00:42:13 +00:00
{
2009-10-02 20:08:15 +00:00
m_assetRequested = true ;
AssetService . Get ( TextureID . ToString ( ) , this , AssetReceived ) ;
2009-10-02 00:42:13 +00:00
}
2009-04-13 14:52:14 +00:00
}
else
{
2009-10-02 20:08:15 +00:00
if ( ! IsDecoded )
2009-10-02 00:42:13 +00:00
{
//We need to decode the requested image first
2009-10-02 20:08:15 +00:00
if ( ! m_decodeRequested )
2009-10-02 00:42:13 +00:00
{
//Request decode
2009-10-02 20:08:15 +00:00
m_decodeRequested = true ;
2009-10-02 00:42:13 +00:00
// Do we have a jpeg decoder?
2009-10-02 20:08:15 +00:00
if ( J2KDecoder ! = null )
2009-10-02 00:42:13 +00:00
{
2009-10-02 20:08:15 +00:00
if ( m_asset = = null )
2009-10-02 00:42:13 +00:00
{
2009-10-02 20:08:15 +00:00
J2KDecodedCallback ( TextureID , new OpenJPEG . J2KLayerInfo [ 0 ] ) ;
2009-10-02 00:42:13 +00:00
}
else
{
// Send it off to the jpeg decoder
2009-10-02 20:08:15 +00:00
J2KDecoder . BeginDecode ( TextureID , m_asset , J2KDecodedCallback ) ;
2009-10-02 00:42:13 +00:00
}
}
else
{
2009-10-02 20:08:15 +00:00
J2KDecodedCallback ( TextureID , new OpenJPEG . J2KLayerInfo [ 0 ] ) ;
2009-10-02 00:42:13 +00:00
}
}
2009-04-13 14:52:14 +00:00
}
2009-10-02 00:42:13 +00:00
else
2009-04-13 14:52:14 +00:00
{
2009-10-02 00:42:13 +00:00
// Check for missing image asset data
2009-10-02 20:08:15 +00:00
if ( m_asset = = null )
2009-10-02 00:42:13 +00:00
{
2009-10-02 20:08:15 +00:00
m_log . Warn ( "[J2KIMAGE]: RunUpdate() called with missing asset data (no missing image texture?). Canceling texture transfer" ) ;
m_currentPacket = m_stopPacket ;
2009-10-02 00:42:13 +00:00
return ;
}
2009-10-04 14:41:10 +00:00
2009-10-02 20:08:15 +00:00
if ( DiscardLevel > = 0 | | m_stopPacket = = 0 )
2009-10-02 00:42:13 +00:00
{
2009-10-04 14:41:10 +00:00
// This shouldn't happen, but if it does, we really can't proceed
2012-01-02 21:31:42 +00:00
if ( m_layers = = null )
2009-10-04 14:41:10 +00:00
{
m_log . Warn ( "[J2KIMAGE]: RunUpdate() called with missing Layers. Canceling texture transfer" ) ;
m_currentPacket = m_stopPacket ;
return ;
}
2012-01-02 21:31:42 +00:00
int maxDiscardLevel = Math . Max ( 0 , m_layers . Length - 1 ) ;
2009-10-02 00:42:13 +00:00
// Treat initial texture downloads with a DiscardLevel of -1 a request for the highest DiscardLevel
2009-10-02 20:08:15 +00:00
if ( DiscardLevel < 0 & & m_stopPacket = = 0 )
DiscardLevel = ( sbyte ) maxDiscardLevel ;
2009-10-02 00:42:13 +00:00
// Clamp at the highest discard level
2009-10-02 20:08:15 +00:00
DiscardLevel = ( sbyte ) Math . Min ( DiscardLevel , maxDiscardLevel ) ;
2009-10-02 00:42:13 +00:00
//Calculate the m_stopPacket
2012-01-02 21:31:42 +00:00
if ( m_layers . Length > 0 )
2009-10-02 00:42:13 +00:00
{
2012-01-02 21:31:42 +00:00
m_stopPacket = ( uint ) GetPacketForBytePosition ( m_layers [ ( m_layers . Length - 1 ) - DiscardLevel ] . End ) ;
2009-10-02 00:42:13 +00:00
//I don't know why, but the viewer seems to expect the final packet if the file
//is just one packet bigger.
if ( TexturePacketCount ( ) = = m_stopPacket + 1 )
{
m_stopPacket = TexturePacketCount ( ) ;
}
}
else
{
m_stopPacket = TexturePacketCount ( ) ;
}
2009-10-02 20:08:15 +00:00
m_currentPacket = StartPacket ;
2009-10-02 00:42:13 +00:00
}
2009-04-13 14:52:14 +00:00
}
}
}
2009-04-13 15:08:06 +00:00
2009-10-02 20:08:15 +00:00
private bool SendFirstPacket ( LLClientView client )
{
2009-10-08 14:55:06 +00:00
if ( client = = null )
return false ;
2009-10-02 20:08:15 +00:00
if ( m_asset = = null )
{
m_log . Warn ( "[J2KIMAGE]: Sending ImageNotInDatabase for texture " + TextureID ) ;
client . SendImageNotFound ( TextureID ) ;
return true ;
}
else if ( m_asset . Length < = FIRST_PACKET_SIZE )
{
// We have less then one packet's worth of data
client . SendImageFirstPart ( 1 , TextureID , ( uint ) m_asset . Length , m_asset , 2 ) ;
m_stopPacket = 0 ;
return true ;
}
else
{
// This is going to be a multi-packet texture download
byte [ ] firstImageData = new byte [ FIRST_PACKET_SIZE ] ;
try { Buffer . BlockCopy ( m_asset , 0 , firstImageData , 0 , FIRST_PACKET_SIZE ) ; }
catch ( Exception )
{
m_log . ErrorFormat ( "[J2KIMAGE]: Texture block copy for the first packet failed. textureid={0}, assetlength={1}" , TextureID , m_asset . Length ) ;
return true ;
}
client . SendImageFirstPart ( TexturePacketCount ( ) , TextureID , ( uint ) m_asset . Length , firstImageData , ( byte ) ImageCodec . J2C ) ;
}
return false ;
}
2009-04-13 14:52:14 +00:00
private bool SendPacket ( LLClientView client )
{
2009-10-08 14:55:06 +00:00
if ( client = = null )
return false ;
2009-04-13 14:52:14 +00:00
bool complete = false ;
2009-10-02 20:08:15 +00:00
int imagePacketSize = ( ( int ) m_currentPacket = = ( TexturePacketCount ( ) ) ) ? LastPacketSize ( ) : IMAGE_PACKET_SIZE ;
2009-04-13 14:52:14 +00:00
2009-04-29 21:01:01 +00:00
try
2009-04-13 14:52:14 +00:00
{
2009-10-02 20:08:15 +00:00
if ( ( CurrentBytePosition ( ) + IMAGE_PACKET_SIZE ) > m_asset . Length )
2009-04-13 14:52:14 +00:00
{
2009-04-29 21:01:01 +00:00
imagePacketSize = LastPacketSize ( ) ;
2009-10-02 00:42:13 +00:00
complete = true ;
2009-10-02 20:08:15 +00:00
if ( ( CurrentBytePosition ( ) + imagePacketSize ) > m_asset . Length )
2009-04-29 21:01:01 +00:00
{
2009-10-02 20:08:15 +00:00
imagePacketSize = m_asset . Length - CurrentBytePosition ( ) ;
2009-04-29 21:01:01 +00:00
complete = true ;
}
2009-04-13 14:52:14 +00:00
}
2009-05-05 09:59:15 +00:00
2009-05-04 15:38:36 +00:00
// It's concievable that the client might request packet one
// from a one packet image, which is really packet 0,
// which would leave us with a negative imagePacketSize..
2009-04-29 21:01:01 +00:00
if ( imagePacketSize > 0 )
2009-04-13 14:52:14 +00:00
{
2009-04-29 21:01:01 +00:00
byte [ ] imageData = new byte [ imagePacketSize ] ;
2009-10-02 20:08:15 +00:00
int currentPosition = CurrentBytePosition ( ) ;
try { Buffer . BlockCopy ( m_asset , currentPosition , imageData , 0 , imagePacketSize ) ; }
2009-04-29 21:01:01 +00:00
catch ( Exception e )
{
2009-10-02 20:08:15 +00:00
m_log . ErrorFormat ( "[J2KIMAGE]: Texture block copy for the first packet failed. textureid={0}, assetlength={1}, currentposition={2}, imagepacketsize={3}, exception={4}" ,
TextureID , m_asset . Length , currentPosition , imagePacketSize , e . Message ) ;
2009-04-29 21:01:01 +00:00
return false ;
}
//Send the packet
2009-10-02 20:08:15 +00:00
client . SendImageNextPart ( ( ushort ) ( m_currentPacket - 1 ) , TextureID , imageData ) ;
2009-04-29 21:01:01 +00:00
}
2009-10-02 20:08:15 +00:00
return ! complete ;
2009-04-13 14:52:14 +00:00
}
2009-05-04 15:38:36 +00:00
catch ( Exception )
2009-04-13 14:52:14 +00:00
{
return false ;
}
}
2009-10-02 00:42:13 +00:00
2009-10-02 20:08:15 +00:00
private ushort TexturePacketCount ( )
{
if ( ! IsDecoded )
return 0 ;
if ( m_asset = = null )
return 0 ;
if ( m_asset . Length < = FIRST_PACKET_SIZE )
return 1 ;
return ( ushort ) ( ( ( m_asset . Length - FIRST_PACKET_SIZE + IMAGE_PACKET_SIZE - 1 ) / IMAGE_PACKET_SIZE ) + 1 ) ;
}
2009-10-02 00:42:13 +00:00
private int GetPacketForBytePosition ( int bytePosition )
2009-04-13 14:52:14 +00:00
{
2009-10-02 00:42:13 +00:00
return ( ( bytePosition - FIRST_PACKET_SIZE + IMAGE_PACKET_SIZE - 1 ) / IMAGE_PACKET_SIZE ) + 1 ;
}
2009-04-13 14:52:14 +00:00
2009-10-02 00:42:13 +00:00
private int LastPacketSize ( )
{
2009-10-02 20:08:15 +00:00
if ( m_currentPacket = = 1 )
return m_asset . Length ;
int lastsize = ( m_asset . Length - FIRST_PACKET_SIZE ) % IMAGE_PACKET_SIZE ;
2009-10-02 00:42:13 +00:00
//If the last packet size is zero, it's really cImagePacketSize, it sits on the boundary
if ( lastsize = = 0 )
2009-04-13 14:52:14 +00:00
{
2009-10-02 00:42:13 +00:00
lastsize = IMAGE_PACKET_SIZE ;
2009-04-13 14:52:14 +00:00
}
2009-10-02 00:42:13 +00:00
return lastsize ;
2009-04-13 14:52:14 +00:00
}
2009-10-02 00:42:13 +00:00
private int CurrentBytePosition ( )
2009-04-13 14:52:14 +00:00
{
2009-10-02 20:08:15 +00:00
if ( m_currentPacket = = 0 )
2009-10-02 00:42:13 +00:00
return 0 ;
2009-10-02 20:08:15 +00:00
if ( m_currentPacket = = 1 )
2009-10-02 00:42:13 +00:00
return FIRST_PACKET_SIZE ;
2009-04-13 14:52:14 +00:00
2009-10-02 20:08:15 +00:00
int result = FIRST_PACKET_SIZE + ( ( int ) m_currentPacket - 2 ) * IMAGE_PACKET_SIZE ;
2009-10-02 00:42:13 +00:00
if ( result < 0 )
2009-04-13 14:52:14 +00:00
{
2009-10-02 00:42:13 +00:00
result = FIRST_PACKET_SIZE ;
}
return result ;
}
2009-04-13 14:52:14 +00:00
2009-10-02 00:42:13 +00:00
private void J2KDecodedCallback ( UUID AssetId , OpenJPEG . J2KLayerInfo [ ] layers )
{
2012-01-02 21:31:42 +00:00
m_layers = layers ;
2009-10-02 20:08:15 +00:00
IsDecoded = true ;
2009-10-02 00:42:13 +00:00
RunUpdate ( ) ;
}
private void AssetDataCallback ( UUID AssetID , AssetBase asset )
{
2009-10-02 20:08:15 +00:00
HasAsset = true ;
2009-04-13 14:52:14 +00:00
2009-10-02 00:42:13 +00:00
if ( asset = = null | | asset . Data = = null )
{
2009-10-02 04:49:27 +00:00
if ( m_imageManager . MissingImage ! = null )
{
2009-10-02 20:08:15 +00:00
m_asset = m_imageManager . MissingImage . Data ;
2009-10-02 04:49:27 +00:00
}
else
{
m_asset = null ;
2009-10-02 20:08:15 +00:00
IsDecoded = true ;
2009-10-02 04:49:27 +00:00
}
2009-04-13 14:52:14 +00:00
}
else
{
2009-10-02 20:08:15 +00:00
m_asset = asset . Data ;
2009-10-02 00:42:13 +00:00
}
2009-04-13 14:52:14 +00:00
2009-10-02 00:42:13 +00:00
RunUpdate ( ) ;
}
2009-04-13 14:52:14 +00:00
2009-10-02 00:42:13 +00:00
private void AssetReceived ( string id , Object sender , AssetBase asset )
{
UUID assetID = UUID . Zero ;
if ( asset ! = null )
assetID = asset . FullID ;
2010-07-04 21:59:15 +00:00
else if ( ( InventoryAccessModule ! = null ) & & ( sender ! = InventoryAccessModule ) )
2009-10-13 00:00:01 +00:00
{
2010-07-04 21:59:15 +00:00
// Unfortunately we need this here, there's no other way.
// This is due to the fact that textures opened directly from the agent's inventory
// don't have any distinguishing feature. As such, in order to serve those when the
// foreign user is visiting, we need to try again after the first fail to the local
// asset service.
string assetServerURL = string . Empty ;
if ( InventoryAccessModule . IsForeignUser ( AgentID , out assetServerURL ) )
2009-10-13 00:00:01 +00:00
{
2011-12-24 15:44:26 +00:00
if ( ! assetServerURL . EndsWith ( "/" ) & & ! assetServerURL . EndsWith ( "=" ) )
assetServerURL = assetServerURL + "/" ;
m_log . DebugFormat ( "[J2KIMAGE]: texture {0} not found in local asset storage. Trying user's storage." , assetServerURL + id ) ;
AssetService . Get ( assetServerURL + id , InventoryAccessModule , AssetReceived ) ;
2009-10-13 00:00:01 +00:00
return ;
}
}
2009-05-05 09:59:15 +00:00
2009-10-02 00:42:13 +00:00
AssetDataCallback ( assetID , asset ) ;
2009-05-05 09:59:15 +00:00
2009-04-13 14:52:14 +00:00
}
}
}