DynamicTextureModule.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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.Collections.Generic;
  29. using System.Drawing;
  30. using System.Drawing.Imaging;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenMetaverse.Imaging;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using log4net;
  38. using System.Reflection;
  39. namespace OpenSim.Region.CoreModules.Scripting.DynamicTexture
  40. {
  41. public class DynamicTextureModule : IRegionModule, IDynamicTextureManager
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private const int ALL_SIDES = -1;
  45. public const int DISP_EXPIRE = 1;
  46. public const int DISP_TEMP = 2;
  47. /// <summary>
  48. /// If true then where possible dynamic textures are reused.
  49. /// </summary>
  50. public bool ReuseTextures { get; set; }
  51. /// <summary>
  52. /// If false, then textures which have a low data size are not reused when ReuseTextures = true.
  53. /// </summary>
  54. /// <remarks>
  55. /// LL viewers 3.3.4 and before appear to not fully render textures pulled from the viewer cache if those
  56. /// textures have a relatively high pixel surface but a small data size. Typically, this appears to happen
  57. /// if the data size is smaller than the viewer's discard level 2 size estimate. So if this is setting is
  58. /// false, textures smaller than the calculation in IsSizeReuseable are always regenerated rather than reused
  59. /// to work around this problem.</remarks>
  60. public bool ReuseLowDataTextures { get; set; }
  61. private Dictionary<UUID, Scene> RegisteredScenes = new Dictionary<UUID, Scene>();
  62. private Dictionary<string, IDynamicTextureRender> RenderPlugins =
  63. new Dictionary<string, IDynamicTextureRender>();
  64. private Dictionary<UUID, DynamicTextureUpdater> Updaters = new Dictionary<UUID, DynamicTextureUpdater>();
  65. /// <summary>
  66. /// Record dynamic textures that we can reuse for a given data and parameter combination rather than
  67. /// regenerate.
  68. /// </summary>
  69. /// <remarks>
  70. /// Key is string.Format("{0}{1}", data
  71. /// </remarks>
  72. private Cache m_reuseableDynamicTextures;
  73. #region IDynamicTextureManager Members
  74. public void RegisterRender(string handleType, IDynamicTextureRender render)
  75. {
  76. if (!RenderPlugins.ContainsKey(handleType))
  77. {
  78. RenderPlugins.Add(handleType, render);
  79. }
  80. }
  81. /// <summary>
  82. /// Called by code which actually renders the dynamic texture to supply texture data.
  83. /// </summary>
  84. /// <param name="updaterId"></param>
  85. /// <param name="texture"></param>
  86. public void ReturnData(UUID updaterId, IDynamicTexture texture)
  87. {
  88. DynamicTextureUpdater updater = null;
  89. lock (Updaters)
  90. {
  91. if (Updaters.ContainsKey(updaterId))
  92. {
  93. updater = Updaters[updaterId];
  94. }
  95. }
  96. if (updater != null)
  97. {
  98. if (RegisteredScenes.ContainsKey(updater.SimUUID))
  99. {
  100. Scene scene = RegisteredScenes[updater.SimUUID];
  101. UUID newTextureID = updater.DataReceived(texture.Data, scene);
  102. if (ReuseTextures
  103. && !updater.BlendWithOldTexture
  104. && texture.IsReuseable
  105. && (ReuseLowDataTextures || IsDataSizeReuseable(texture)))
  106. {
  107. m_reuseableDynamicTextures.Store(
  108. GenerateReusableTextureKey(texture.InputCommands, texture.InputParams), newTextureID);
  109. }
  110. }
  111. }
  112. if (updater.UpdateTimer == 0)
  113. {
  114. lock (Updaters)
  115. {
  116. if (!Updaters.ContainsKey(updater.UpdaterID))
  117. {
  118. Updaters.Remove(updater.UpdaterID);
  119. }
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// Determines whether the texture is reuseable based on its data size.
  125. /// </summary>
  126. /// <remarks>
  127. /// This is a workaround for a viewer bug where very small data size textures relative to their pixel size
  128. /// are not redisplayed properly when pulled from cache. The calculation here is based on the typical discard
  129. /// level of 2, a 'rate' of 0.125 and 4 components (which makes for a factor of 0.5).
  130. /// </remarks>
  131. /// <returns></returns>
  132. private bool IsDataSizeReuseable(IDynamicTexture texture)
  133. {
  134. // Console.WriteLine("{0} {1}", texture.Size.Width, texture.Size.Height);
  135. int discardLevel2DataThreshold = (int)Math.Ceiling((texture.Size.Width >> 2) * (texture.Size.Height >> 2) * 0.5);
  136. // m_log.DebugFormat(
  137. // "[DYNAMIC TEXTURE MODULE]: Discard level 2 threshold {0}, texture data length {1}",
  138. // discardLevel2DataThreshold, texture.Data.Length);
  139. return discardLevel2DataThreshold < texture.Data.Length;
  140. }
  141. public UUID AddDynamicTextureURL(UUID simID, UUID primID, string contentType, string url,
  142. string extraParams, int updateTimer)
  143. {
  144. return AddDynamicTextureURL(simID, primID, contentType, url, extraParams, updateTimer, false, 255);
  145. }
  146. public UUID AddDynamicTextureURL(UUID simID, UUID primID, string contentType, string url,
  147. string extraParams, int updateTimer, bool SetBlending, byte AlphaValue)
  148. {
  149. return AddDynamicTextureURL(simID, primID, contentType, url,
  150. extraParams, updateTimer, SetBlending,
  151. (int)(DISP_TEMP|DISP_EXPIRE), AlphaValue, ALL_SIDES);
  152. }
  153. public UUID AddDynamicTextureURL(UUID simID, UUID primID, string contentType, string url,
  154. string extraParams, int updateTimer, bool SetBlending,
  155. int disp, byte AlphaValue, int face)
  156. {
  157. if (RenderPlugins.ContainsKey(contentType))
  158. {
  159. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  160. updater.SimUUID = simID;
  161. updater.PrimID = primID;
  162. updater.ContentType = contentType;
  163. updater.Url = url;
  164. updater.UpdateTimer = updateTimer;
  165. updater.UpdaterID = UUID.Random();
  166. updater.Params = extraParams;
  167. updater.BlendWithOldTexture = SetBlending;
  168. updater.FrontAlpha = AlphaValue;
  169. updater.Face = face;
  170. updater.Disp = disp;
  171. lock (Updaters)
  172. {
  173. if (!Updaters.ContainsKey(updater.UpdaterID))
  174. {
  175. Updaters.Add(updater.UpdaterID, updater);
  176. }
  177. }
  178. RenderPlugins[contentType].AsyncConvertUrl(updater.UpdaterID, url, extraParams);
  179. return updater.UpdaterID;
  180. }
  181. return UUID.Zero;
  182. }
  183. public UUID AddDynamicTextureData(UUID simID, UUID primID, string contentType, string data,
  184. string extraParams, int updateTimer)
  185. {
  186. return AddDynamicTextureData(simID, primID, contentType, data, extraParams, updateTimer, false, 255);
  187. }
  188. public UUID AddDynamicTextureData(UUID simID, UUID primID, string contentType, string data,
  189. string extraParams, int updateTimer, bool SetBlending, byte AlphaValue)
  190. {
  191. return AddDynamicTextureData(simID, primID, contentType, data, extraParams, updateTimer, SetBlending,
  192. (int) (DISP_TEMP|DISP_EXPIRE), AlphaValue, ALL_SIDES);
  193. }
  194. public UUID AddDynamicTextureData(UUID simID, UUID primID, string contentType, string data,
  195. string extraParams, int updateTimer, bool SetBlending, int disp, byte AlphaValue, int face)
  196. {
  197. if (!RenderPlugins.ContainsKey(contentType))
  198. return UUID.Zero;
  199. Scene scene;
  200. RegisteredScenes.TryGetValue(simID, out scene);
  201. if (scene == null)
  202. return UUID.Zero;
  203. SceneObjectPart part = scene.GetSceneObjectPart(primID);
  204. if (part == null)
  205. return UUID.Zero;
  206. // If we want to reuse dynamic textures then we have to ignore any request from the caller to expire
  207. // them.
  208. if (ReuseTextures)
  209. disp = disp & ~DISP_EXPIRE;
  210. DynamicTextureUpdater updater = new DynamicTextureUpdater();
  211. updater.SimUUID = simID;
  212. updater.PrimID = primID;
  213. updater.ContentType = contentType;
  214. updater.BodyData = data;
  215. updater.UpdateTimer = updateTimer;
  216. updater.UpdaterID = UUID.Random();
  217. updater.Params = extraParams;
  218. updater.BlendWithOldTexture = SetBlending;
  219. updater.FrontAlpha = AlphaValue;
  220. updater.Face = face;
  221. updater.Url = "Local image";
  222. updater.Disp = disp;
  223. object objReusableTextureUUID = null;
  224. if (ReuseTextures && !updater.BlendWithOldTexture)
  225. {
  226. string reuseableTextureKey = GenerateReusableTextureKey(data, extraParams);
  227. objReusableTextureUUID = m_reuseableDynamicTextures.Get(reuseableTextureKey);
  228. if (objReusableTextureUUID != null)
  229. {
  230. // If something else has removed this temporary asset from the cache, detect and invalidate
  231. // our cached uuid.
  232. if (scene.AssetService.GetMetadata(objReusableTextureUUID.ToString()) == null)
  233. {
  234. m_reuseableDynamicTextures.Invalidate(reuseableTextureKey);
  235. objReusableTextureUUID = null;
  236. }
  237. }
  238. }
  239. // We cannot reuse a dynamic texture if the data is going to be blended with something already there.
  240. if (objReusableTextureUUID == null)
  241. {
  242. lock (Updaters)
  243. {
  244. if (!Updaters.ContainsKey(updater.UpdaterID))
  245. {
  246. Updaters.Add(updater.UpdaterID, updater);
  247. }
  248. }
  249. // m_log.DebugFormat(
  250. // "[DYNAMIC TEXTURE MODULE]: Requesting generation of new dynamic texture for {0} in {1}",
  251. // part.Name, part.ParentGroup.Scene.Name);
  252. RenderPlugins[contentType].AsyncConvertData(updater.UpdaterID, data, extraParams);
  253. }
  254. else
  255. {
  256. // m_log.DebugFormat(
  257. // "[DYNAMIC TEXTURE MODULE]: Reusing cached texture {0} for {1} in {2}",
  258. // objReusableTextureUUID, part.Name, part.ParentGroup.Scene.Name);
  259. // No need to add to updaters as the texture is always the same. Not that this functionality
  260. // apppears to be implemented anyway.
  261. updater.UpdatePart(part, (UUID)objReusableTextureUUID);
  262. }
  263. return updater.UpdaterID;
  264. }
  265. private string GenerateReusableTextureKey(string data, string extraParams)
  266. {
  267. return string.Format("{0}{1}", data, extraParams);
  268. }
  269. public void GetDrawStringSize(string contentType, string text, string fontName, int fontSize,
  270. out double xSize, out double ySize)
  271. {
  272. xSize = 0;
  273. ySize = 0;
  274. if (RenderPlugins.ContainsKey(contentType))
  275. {
  276. RenderPlugins[contentType].GetDrawStringSize(text, fontName, fontSize, out xSize, out ySize);
  277. }
  278. }
  279. #endregion
  280. #region IRegionModule Members
  281. public void Initialise(Scene scene, IConfigSource config)
  282. {
  283. IConfig texturesConfig = config.Configs["Textures"];
  284. if (texturesConfig != null)
  285. {
  286. ReuseTextures = texturesConfig.GetBoolean("ReuseDynamicTextures", false);
  287. ReuseLowDataTextures = texturesConfig.GetBoolean("ReuseDynamicLowDataTextures", false);
  288. }
  289. if (!RegisteredScenes.ContainsKey(scene.RegionInfo.RegionID))
  290. {
  291. RegisteredScenes.Add(scene.RegionInfo.RegionID, scene);
  292. scene.RegisterModuleInterface<IDynamicTextureManager>(this);
  293. }
  294. }
  295. public void PostInitialise()
  296. {
  297. if (ReuseTextures)
  298. {
  299. m_reuseableDynamicTextures = new Cache(CacheMedium.Memory, CacheStrategy.Conservative);
  300. m_reuseableDynamicTextures.DefaultTTL = new TimeSpan(24, 0, 0);
  301. }
  302. }
  303. public void Close()
  304. {
  305. }
  306. public string Name
  307. {
  308. get { return "DynamicTextureModule"; }
  309. }
  310. public bool IsSharedModule
  311. {
  312. get { return true; }
  313. }
  314. #endregion
  315. #region Nested type: DynamicTextureUpdater
  316. public class DynamicTextureUpdater
  317. {
  318. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  319. public bool BlendWithOldTexture = false;
  320. public string BodyData;
  321. public string ContentType;
  322. public byte FrontAlpha = 255;
  323. public string Params;
  324. public UUID PrimID;
  325. public bool SetNewFrontAlpha = false;
  326. public UUID SimUUID;
  327. public UUID UpdaterID;
  328. public int UpdateTimer;
  329. public int Face;
  330. public int Disp;
  331. public string Url;
  332. public DynamicTextureUpdater()
  333. {
  334. UpdateTimer = 0;
  335. BodyData = null;
  336. }
  337. /// <summary>
  338. /// Update the given part with the new texture.
  339. /// </summary>
  340. /// <returns>
  341. /// The old texture UUID.
  342. /// </returns>
  343. public UUID UpdatePart(SceneObjectPart part, UUID textureID)
  344. {
  345. UUID oldID;
  346. lock (part)
  347. {
  348. // mostly keep the values from before
  349. Primitive.TextureEntry tmptex = part.Shape.Textures;
  350. // FIXME: Need to return the appropriate ID if only a single face is replaced.
  351. oldID = tmptex.DefaultTexture.TextureID;
  352. if (Face == ALL_SIDES)
  353. {
  354. oldID = tmptex.DefaultTexture.TextureID;
  355. tmptex.DefaultTexture.TextureID = textureID;
  356. }
  357. else
  358. {
  359. try
  360. {
  361. Primitive.TextureEntryFace texface = tmptex.CreateFace((uint)Face);
  362. texface.TextureID = textureID;
  363. tmptex.FaceTextures[Face] = texface;
  364. }
  365. catch (Exception)
  366. {
  367. tmptex.DefaultTexture.TextureID = textureID;
  368. }
  369. }
  370. // I'm pretty sure we always want to force this to true
  371. // I'm pretty sure noone whats to set fullbright true if it wasn't true before.
  372. // tmptex.DefaultTexture.Fullbright = true;
  373. part.UpdateTextureEntry(tmptex.GetBytes());
  374. }
  375. return oldID;
  376. }
  377. /// <summary>
  378. /// Called once new texture data has been received for this updater.
  379. /// </summary>
  380. /// <param name="data"></param>
  381. /// <param name="scene"></param>
  382. /// <param name="isReuseable">True if the data given is reuseable.</param>
  383. /// <returns>The asset UUID given to the incoming data.</returns>
  384. public UUID DataReceived(byte[] data, Scene scene)
  385. {
  386. SceneObjectPart part = scene.GetSceneObjectPart(PrimID);
  387. if (part == null || data == null || data.Length <= 1)
  388. {
  389. string msg =
  390. String.Format("DynamicTextureModule: Error preparing image using URL {0}", Url);
  391. scene.SimChat(Utils.StringToBytes(msg), ChatTypeEnum.Say,
  392. 0, part.ParentGroup.RootPart.AbsolutePosition, part.Name, part.UUID, false);
  393. return UUID.Zero;
  394. }
  395. byte[] assetData = null;
  396. AssetBase oldAsset = null;
  397. if (BlendWithOldTexture)
  398. {
  399. Primitive.TextureEntryFace defaultFace = part.Shape.Textures.DefaultTexture;
  400. if (defaultFace != null)
  401. {
  402. oldAsset = scene.AssetService.Get(defaultFace.TextureID.ToString());
  403. if (oldAsset != null)
  404. assetData = BlendTextures(data, oldAsset.Data, SetNewFrontAlpha, FrontAlpha);
  405. }
  406. }
  407. if (assetData == null)
  408. {
  409. assetData = new byte[data.Length];
  410. Array.Copy(data, assetData, data.Length);
  411. }
  412. // Create a new asset for user
  413. AssetBase asset
  414. = new AssetBase(
  415. UUID.Random(), "DynamicImage" + Util.RandomClass.Next(1, 10000), (sbyte)AssetType.Texture,
  416. scene.RegionInfo.RegionID.ToString());
  417. asset.Data = assetData;
  418. asset.Description = String.Format("URL image : {0}", Url);
  419. asset.Local = false;
  420. asset.Temporary = ((Disp & DISP_TEMP) != 0);
  421. scene.AssetService.Store(asset);
  422. IJ2KDecoder cacheLayerDecode = scene.RequestModuleInterface<IJ2KDecoder>();
  423. if (cacheLayerDecode != null)
  424. {
  425. if (!cacheLayerDecode.Decode(asset.FullID, asset.Data))
  426. m_log.WarnFormat(
  427. "[DYNAMIC TEXTURE MODULE]: Decoding of dynamically generated asset {0} for {1} in {2} failed",
  428. asset.ID, part.Name, part.ParentGroup.Scene.Name);
  429. }
  430. UUID oldID = UpdatePart(part, asset.FullID);
  431. if (oldID != UUID.Zero && ((Disp & DISP_EXPIRE) != 0))
  432. {
  433. if (oldAsset == null)
  434. oldAsset = scene.AssetService.Get(oldID.ToString());
  435. if (oldAsset != null)
  436. {
  437. if (oldAsset.Temporary)
  438. {
  439. scene.AssetService.Delete(oldID.ToString());
  440. }
  441. }
  442. }
  443. return asset.FullID;
  444. }
  445. private byte[] BlendTextures(byte[] frontImage, byte[] backImage, bool setNewAlpha, byte newAlpha)
  446. {
  447. ManagedImage managedImage;
  448. Image image;
  449. if (OpenJPEG.DecodeToImage(frontImage, out managedImage, out image))
  450. {
  451. Bitmap image1 = new Bitmap(image);
  452. if (OpenJPEG.DecodeToImage(backImage, out managedImage, out image))
  453. {
  454. Bitmap image2 = new Bitmap(image);
  455. if (setNewAlpha)
  456. SetAlpha(ref image1, newAlpha);
  457. Bitmap joint = MergeBitMaps(image1, image2);
  458. byte[] result = new byte[0];
  459. try
  460. {
  461. result = OpenJPEG.EncodeFromImage(joint, true);
  462. }
  463. catch (Exception e)
  464. {
  465. m_log.ErrorFormat(
  466. "[DYNAMICTEXTUREMODULE]: OpenJpeg Encode Failed. Exception {0}{1}",
  467. e.Message, e.StackTrace);
  468. }
  469. return result;
  470. }
  471. }
  472. return null;
  473. }
  474. public Bitmap MergeBitMaps(Bitmap front, Bitmap back)
  475. {
  476. Bitmap joint;
  477. Graphics jG;
  478. joint = new Bitmap(back.Width, back.Height, PixelFormat.Format32bppArgb);
  479. jG = Graphics.FromImage(joint);
  480. jG.DrawImage(back, 0, 0, back.Width, back.Height);
  481. jG.DrawImage(front, 0, 0, back.Width, back.Height);
  482. return joint;
  483. }
  484. private void SetAlpha(ref Bitmap b, byte alpha)
  485. {
  486. for (int w = 0; w < b.Width; w++)
  487. {
  488. for (int h = 0; h < b.Height; h++)
  489. {
  490. b.SetPixel(w, h, Color.FromArgb(alpha, b.GetPixel(w, h)));
  491. }
  492. }
  493. }
  494. }
  495. #endregion
  496. }
  497. }