lllocalbitmaps.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /**
  2. * @file lllocalbitmaps.cpp
  3. * @author Vaalith Jinn, code cleanup by Henri Beauchamp
  4. * @brief Local Bitmaps source
  5. *
  6. * $LicenseInfo:firstyear=2011&license=viewergpl$
  7. *
  8. * Copyright (c) 2011, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #include "llviewerprecompiledheaders.h"
  34. #include <time.h>
  35. #include "lllocalbitmaps.h"
  36. #include "imageids.h"
  37. #include "lldir.h"
  38. #include "llimagebmp.h"
  39. #include "llimagejpeg.h"
  40. #include "llimagepng.h"
  41. #include "llimagetga.h"
  42. #include "lllocaltextureobject.h"
  43. #include "llnotifications.h"
  44. #include "llagent.h" // For getRegionMaxTextureSize()
  45. #include "llagentwearables.h"
  46. #include "llface.h"
  47. #include "llfetchedgltfmaterial.h"
  48. #include "llmaterialmgr.h"
  49. #include "llviewerobjectlist.h"
  50. #include "llviewertexturelist.h"
  51. #include "llviewerwearable.h"
  52. #include "llvoavatarself.h"
  53. #include "llvovolume.h"
  54. S32 LLLocalBitmap::sBitmapsListVersion = 0;
  55. bool LLLocalBitmap::sNeedsRebake = false;
  56. std::list<LLLocalBitmap*> LLLocalBitmap::sBitmapList;
  57. LLLocalBitmapTimer LLLocalBitmap::sTimer;
  58. constexpr F32 LL_LOCAL_TIMER_HEARTBEAT = 3.f;
  59. constexpr S32 LL_LOCAL_UPDATE_RETRIES = 5;
  60. ///////////////////////////////////////////////////////////////////////////////
  61. // LLLocalBitmap class
  62. ///////////////////////////////////////////////////////////////////////////////
  63. LLLocalBitmap::LLLocalBitmap(std::string filename)
  64. : mFilename(filename),
  65. mShortName(gDirUtil.getBaseFileName(filename, true)),
  66. mLastModified(0),
  67. mValid(false),
  68. mLinkStatus(LS_ON),
  69. mUpdateRetries(LL_LOCAL_UPDATE_RETRIES)
  70. {
  71. mTrackingID.generate();
  72. std::string temp_exten = gDirUtil.getExtension(mFilename);
  73. if (temp_exten == "bmp")
  74. {
  75. mExtension = ET_IMG_BMP;
  76. }
  77. else if (temp_exten == "tga")
  78. {
  79. mExtension = ET_IMG_TGA;
  80. }
  81. else if (temp_exten == "jpg" || temp_exten == "jpeg")
  82. {
  83. mExtension = ET_IMG_JPG;
  84. }
  85. else if (temp_exten == "png")
  86. {
  87. mExtension = ET_IMG_PNG;
  88. }
  89. else
  90. {
  91. llwarns << "File of no valid extension given, local bitmap creation aborted. Filename: "
  92. << mFilename << llendl;
  93. return; // no valid extension.
  94. }
  95. // Next phase of unit creation is nearly the same as an update cycle. We
  96. // are running updateSelf as a special case with the optional UT_FIRSTUSE
  97. // which omits the parts associated with removing the outdated texture.
  98. mValid = updateSelf(UT_FIRSTUSE);
  99. }
  100. LLLocalBitmap::~LLLocalBitmap()
  101. {
  102. // Replace IDs with defaults
  103. if (mValid && isAgentAvatarValid())
  104. {
  105. replaceIDs(mWorldID, IMG_DEFAULT);
  106. LLLocalBitmap::doRebake();
  107. }
  108. for (U32 i = 0, count = mGLTFMaterialWithLocalTextures.size(); i < count;
  109. ++i)
  110. {
  111. LLGLTFMaterial* matp = mGLTFMaterialWithLocalTextures[i].get();
  112. if (matp)
  113. {
  114. matp->removeLocalTextureTracking(mTrackingID);
  115. }
  116. }
  117. mChangedSignal(mTrackingID, mWorldID, LLUUID::null);
  118. mChangedSignal.disconnect_all_slots();
  119. // Delete self from the textures list
  120. LLViewerFetchedTexture* texp = gTextureList.findImage(mWorldID);
  121. if (texp)
  122. {
  123. gTextureList.deleteImage(texp);
  124. texp->unref();
  125. }
  126. }
  127. //static
  128. void LLLocalBitmap::cleanupClass()
  129. {
  130. std::for_each(sBitmapList.begin(), sBitmapList.end(), DeletePointer());
  131. sBitmapList.clear();
  132. }
  133. bool LLLocalBitmap::updateSelf(EUpdateType optional_firstupdate)
  134. {
  135. if (mLinkStatus != LS_ON)
  136. {
  137. return false;
  138. }
  139. if (!LLFile::exists(mFilename))
  140. {
  141. mLinkStatus = LS_BROKEN;
  142. LLSD args;
  143. args["FNAME"] = mFilename;
  144. gNotifications.add("LocalBitmapsUpdateFileNotFound", args);
  145. return false;
  146. }
  147. // Verifying that the file has indeed been modified
  148. time_t new_last_modified = LLFile::lastModidied(mFilename);
  149. if (mLastModified == new_last_modified)
  150. {
  151. return false;
  152. }
  153. // Loading the image file and decoding it; here is a critical point which,
  154. // if fails, invalidates the whole update (or unit creation) process.
  155. LLPointer<LLImageRaw> raw_image = new LLImageRaw();
  156. if (decodeBitmap(raw_image))
  157. {
  158. // Decode is successful, we can safely proceed.
  159. LLUUID old_id;
  160. if (optional_firstupdate != UT_FIRSTUSE && mWorldID.notNull())
  161. {
  162. old_id = mWorldID;
  163. }
  164. mWorldID.generate();
  165. mLastModified = new_last_modified;
  166. LLPointer<LLViewerFetchedTexture> texp =
  167. new LLViewerFetchedTexture("file://" + mFilename, FTT_LOCAL_FILE,
  168. mWorldID, true);
  169. texp->createGLTexture(0, raw_image);
  170. texp->setCachedRawImage(0, raw_image);
  171. texp->ref();
  172. gTextureList.addImage(texp);
  173. if (optional_firstupdate != UT_FIRSTUSE)
  174. {
  175. // Seek out everything old_id uses and replace it with mWorldID
  176. replaceIDs(old_id, mWorldID);
  177. // Remove old_id from gimagelist
  178. LLViewerFetchedTexture* oldtexp = gTextureList.findImage(old_id);
  179. if (oldtexp)
  180. {
  181. gTextureList.deleteImage(oldtexp);
  182. oldtexp->unref();
  183. }
  184. else
  185. {
  186. llwarns_once << "Could not find texture for id: " << old_id
  187. << llendl;
  188. }
  189. }
  190. mUpdateRetries = LL_LOCAL_UPDATE_RETRIES;
  191. return true;
  192. }
  193. // If decoding failed, we get here and it will attempt to decode it in the
  194. // next cycles until mUpdateRetries runs out. this is done because some
  195. // software lock the bitmap while writing to it.
  196. if (mUpdateRetries)
  197. {
  198. --mUpdateRetries;
  199. }
  200. else
  201. {
  202. mLinkStatus = LS_BROKEN;
  203. LLSD args;
  204. args["FNAME"] = mFilename;
  205. args["NRETRIES"] = LL_LOCAL_UPDATE_RETRIES;
  206. gNotifications.add("LocalBitmapsUpdateFailedFinal", args);
  207. }
  208. return false;
  209. }
  210. boost::signals2::connection LLLocalBitmap::setChangedCallback(const changed_cb_t& cb)
  211. {
  212. return mChangedSignal.connect(cb);
  213. }
  214. void LLLocalBitmap::addGLTFMaterial(LLGLTFMaterial* new_matp)
  215. {
  216. if (!new_matp)
  217. {
  218. return;
  219. }
  220. for (U32 i = 0, count = mGLTFMaterialWithLocalTextures.size(); i < count; )
  221. {
  222. const LLPointer<LLGLTFMaterial>& matp =
  223. mGLTFMaterialWithLocalTextures[i];
  224. if (matp.get() == new_matp)
  225. {
  226. return;
  227. }
  228. if (matp.isNull() || matp->getNumRefs() == 1)
  229. {
  230. // This material is no more in use by anyone else: remove it.
  231. if (i != --count)
  232. {
  233. mGLTFMaterialWithLocalTextures[i] =
  234. std::move(mGLTFMaterialWithLocalTextures.back());
  235. }
  236. mGLTFMaterialWithLocalTextures.pop_back();
  237. continue;
  238. }
  239. ++i;
  240. }
  241. new_matp->addLocalTextureTracking(mTrackingID, mWorldID);
  242. mGLTFMaterialWithLocalTextures.emplace_back(new_matp);
  243. }
  244. bool LLLocalBitmap::decodeBitmap(LLPointer<LLImageRaw> rawimg)
  245. {
  246. bool decode_successful = false;
  247. S32 max_texture_size = gAgent.getRegionMaxTextureSize();
  248. switch (mExtension)
  249. {
  250. case ET_IMG_BMP:
  251. {
  252. LLPointer<LLImageBMP> bmp_image = new LLImageBMP;
  253. if (bmp_image->load(mFilename) && bmp_image->decode(rawimg))
  254. {
  255. rawimg->biasedScaleToPowerOfTwo(max_texture_size);
  256. decode_successful = true;
  257. }
  258. break;
  259. }
  260. case ET_IMG_TGA:
  261. {
  262. LLPointer<LLImageTGA> tga_image = new LLImageTGA;
  263. if (tga_image->load(mFilename) && tga_image->decode(rawimg) &&
  264. (tga_image->getComponents() == 3 ||
  265. tga_image->getComponents() == 4))
  266. {
  267. rawimg->biasedScaleToPowerOfTwo(max_texture_size);
  268. decode_successful = true;
  269. }
  270. break;
  271. }
  272. case ET_IMG_JPG:
  273. {
  274. LLPointer<LLImageJPEG> jpeg_image = new LLImageJPEG;
  275. if (jpeg_image->load(mFilename) &&
  276. jpeg_image->decode(rawimg))
  277. {
  278. rawimg->biasedScaleToPowerOfTwo(max_texture_size);
  279. decode_successful = true;
  280. }
  281. break;
  282. }
  283. case ET_IMG_PNG:
  284. {
  285. LLPointer<LLImagePNG> png_image = new LLImagePNG;
  286. if (png_image->load(mFilename) &&
  287. png_image->decode(rawimg))
  288. {
  289. rawimg->biasedScaleToPowerOfTwo(max_texture_size);
  290. decode_successful = true;
  291. }
  292. break;
  293. }
  294. default:
  295. {
  296. llwarns << "During a decode attempt, the following local bitmap had no properly assigned extension: "
  297. << mFilename
  298. << ". Disabling further update attempts for this file."
  299. << llendl;
  300. mLinkStatus = LS_BROKEN;
  301. }
  302. }
  303. return decode_successful;
  304. }
  305. void LLLocalBitmap::replaceIDs(const LLUUID& old_id, LLUUID new_id)
  306. {
  307. // Checking for misuse.
  308. if (old_id == new_id)
  309. {
  310. llinfos << "An attempt was made to replace a texture with itself (matching UUIDs): "
  311. << old_id.asString() << llendl;
  312. return;
  313. }
  314. mChangedSignal(mTrackingID, old_id, new_id);
  315. // Processing updates per channel; makes the process scalable. The only
  316. // actual difference is in SetTE* call i.e. SetTETexture, SetTENormal, etc.
  317. updateUserPrims(old_id, new_id, LLRender::DIFFUSE_MAP);
  318. updateUserPrims(old_id, new_id, LLRender::NORMAL_MAP);
  319. updateUserPrims(old_id, new_id, LLRender::SPECULAR_MAP);
  320. updateUserVolumes(old_id, new_id, LLRender::LIGHT_TEX);
  321. // Is not there supposed to be an IMG_DEFAULT_SCULPT or something ?
  322. updateUserVolumes(old_id, new_id, LLRender::SCULPT_TEX);
  323. // Default safeguard image for layers
  324. if (new_id == IMG_DEFAULT)
  325. {
  326. new_id = IMG_DEFAULT_AVATAR;
  327. }
  328. // It does not actually update all of those, it merely checks if any of
  329. // them contains the referenced ID and if so, updates.
  330. updateUserLayers(old_id, new_id, LLWearableType::WT_ALPHA);
  331. updateUserLayers(old_id, new_id, LLWearableType::WT_EYES);
  332. updateUserLayers(old_id, new_id, LLWearableType::WT_GLOVES);
  333. updateUserLayers(old_id, new_id, LLWearableType::WT_JACKET);
  334. updateUserLayers(old_id, new_id, LLWearableType::WT_PANTS);
  335. updateUserLayers(old_id, new_id, LLWearableType::WT_SHIRT);
  336. updateUserLayers(old_id, new_id, LLWearableType::WT_SHOES);
  337. updateUserLayers(old_id, new_id, LLWearableType::WT_SKIN);
  338. updateUserLayers(old_id, new_id, LLWearableType::WT_SKIRT);
  339. updateUserLayers(old_id, new_id, LLWearableType::WT_SOCKS);
  340. updateUserLayers(old_id, new_id, LLWearableType::WT_TATTOO);
  341. updateUserLayers(old_id, new_id, LLWearableType::WT_UNIVERSAL);
  342. updateUserLayers(old_id, new_id, LLWearableType::WT_UNDERPANTS);
  343. updateUserLayers(old_id, new_id, LLWearableType::WT_UNDERSHIRT);
  344. updateGLTFMaterials(old_id, new_id);
  345. }
  346. // This function sorts the faces from a getFaceList[getNumFaces] into a list of
  347. // objects in order to prevent multiple sendTEUpdate calls per object during
  348. // updateUserPrims.
  349. void LLLocalBitmap::prepUpdateObjects(const LLUUID& old_id, U32 channel,
  350. std::vector<LLViewerObject*>& obj_list)
  351. {
  352. LLViewerFetchedTexture* oldtextp = gTextureList.findImage(old_id);
  353. if (!oldtextp)
  354. {
  355. llwarns_once << "Could not find texture for id: " << old_id << llendl;
  356. return;
  357. }
  358. U32 count = oldtextp->getNumFaces(channel);
  359. obj_list.reserve(count);
  360. for (U32 i = 0; i < count; ++i)
  361. {
  362. // Getting an object from a face
  363. LLFace* facep = (*oldtextp->getFaceList(channel))[i];
  364. if (facep)
  365. {
  366. LLViewerObject* objectp = facep->getViewerObject();
  367. if (objectp)
  368. {
  369. // We have an object, we'll take its UUID and compare it to
  370. // whatever we already have in the returnable object list. If
  371. // there is a match, we do not add it, to prevent duplicates.
  372. const LLUUID& mainlist_obj_id = objectp->getID();
  373. // Look for duplicates
  374. bool add_object = true;
  375. for (U32 j = 0, count2 = obj_list.size(); j < count2; ++j)
  376. {
  377. LLViewerObject* objp = obj_list[j];
  378. if (objp->getID() == mainlist_obj_id)
  379. {
  380. add_object = false; // Duplicate found.
  381. break;
  382. }
  383. }
  384. if (add_object)
  385. {
  386. obj_list.push_back(objectp);
  387. }
  388. }
  389. }
  390. }
  391. }
  392. void LLLocalBitmap::updateUserPrims(const LLUUID& old_id, const LLUUID& new_id,
  393. U32 channel)
  394. {
  395. std::vector<LLViewerObject*> objectlist;
  396. prepUpdateObjects(old_id, channel, objectlist);
  397. for (U32 i = 0, count = objectlist.size(); i < count; ++i)
  398. {
  399. LLViewerObject* objectp = objectlist[i];
  400. LLDrawable* drawablep = objectp->mDrawable;
  401. if (!drawablep)
  402. {
  403. continue;
  404. }
  405. bool update_tex = false;
  406. bool update_mat = false;
  407. for (U8 te = 0, faces = objectp->getNumFaces(); te < faces; ++te)
  408. {
  409. LLFace* facep = drawablep->getFace(te);
  410. if (facep && facep->getTexture(channel) &&
  411. facep->getTexture(channel)->getID() == old_id)
  412. {
  413. switch (channel)
  414. {
  415. case LLRender::DIFFUSE_MAP:
  416. objectp->setTETexture(te, new_id);
  417. update_tex = true;
  418. break;
  419. case LLRender::NORMAL_MAP:
  420. objectp->setTENormalMap(te, new_id);
  421. update_mat = update_tex = true;
  422. break;
  423. case LLRender::SPECULAR_MAP:
  424. objectp->setTESpecularMap(te, new_id);
  425. update_mat = update_tex = true;
  426. }
  427. }
  428. }
  429. if (update_tex)
  430. {
  431. objectp->sendTEUpdate();
  432. }
  433. if (update_mat && drawablep->getVOVolume())
  434. {
  435. drawablep->getVOVolume()->faceMappingChanged();
  436. }
  437. }
  438. }
  439. void LLLocalBitmap::updateUserVolumes(const LLUUID& old_id,
  440. const LLUUID& new_id, U32 channel)
  441. {
  442. LLViewerFetchedTexture* oldtextp = gTextureList.findImage(old_id);
  443. if (!oldtextp)
  444. {
  445. llwarns_once << "Could not find texture for id: " << old_id << llendl;
  446. return;
  447. }
  448. if (channel != LLRender::LIGHT_TEX && channel != LLRender::SCULPT_TEX)
  449. {
  450. llwarns_once << "Bad texture channel: " << channel << llendl;
  451. llassert(false);
  452. return;
  453. }
  454. for (U32 i = 0, count = oldtextp->getNumVolumes(channel); i < count; ++i)
  455. {
  456. LLVOVolume* vovolp = (*oldtextp->getVolumeList(channel))[i];
  457. if (!vovolp) continue; // Paranoia
  458. if (channel == LLRender::LIGHT_TEX)
  459. {
  460. if (vovolp->getLightTextureID() == old_id)
  461. {
  462. vovolp->setLightTextureID(new_id);
  463. }
  464. }
  465. else
  466. {
  467. LLViewerObject* objectp = (LLViewerObject*)vovolp;
  468. if (objectp && objectp->isSculpted() && objectp->getVolume() &&
  469. objectp->getVolume()->getParams().getSculptID() == old_id)
  470. {
  471. const LLSculptParams* paramsp = objectp->getSculptParams();
  472. if (!paramsp) continue;
  473. LLSculptParams new_params(*paramsp);
  474. new_params.setSculptTexture(new_id, paramsp->getSculptType());
  475. objectp->setParameterEntry(LLNetworkData::PARAMS_SCULPT,
  476. new_params, true);
  477. }
  478. }
  479. }
  480. }
  481. void LLLocalBitmap::updateUserLayers(const LLUUID& old_id,
  482. const LLUUID& new_id,
  483. LLWearableType::EType type)
  484. {
  485. std::vector<LLLocalTextureObject*> texture_list;
  486. for (U32 i = 0, count = gAgentWearables.getWearableCount(type); i < count;
  487. ++i)
  488. {
  489. LLViewerWearable* vwp = gAgentWearables.getViewerWearable(type, i);
  490. if (!vwp)
  491. {
  492. continue;
  493. }
  494. vwp->getLocalTextureListSeq(texture_list);
  495. for (size_t j = 0, count2 = texture_list.size(); j < count2; ++j)
  496. {
  497. LLLocalTextureObject* ltop = texture_list[j];
  498. if (!ltop || ltop->getID() != old_id)
  499. {
  500. continue;
  501. }
  502. LLTexLayer* tex_layerp = ltop->getTexLayerByIdx(0);
  503. if (!tex_layerp)
  504. {
  505. continue;
  506. }
  507. LLTexLayerSet* tex_layer_setp = tex_layerp->getTexLayerSet();
  508. if (!tex_layer_setp)
  509. {
  510. continue;
  511. }
  512. auto baked_texind = tex_layer_setp->getBakedTexIndex();
  513. auto reg_texind = getTexIndex(type, baked_texind);
  514. if (reg_texind == LLAvatarAppearanceDefines::TEX_NUM_INDICES)
  515. {
  516. continue;
  517. }
  518. U32 index;
  519. if (gAgentWearables.getWearableIndex(vwp, index))
  520. {
  521. gAgentAvatarp->setLocalTexture(reg_texind,
  522. gTextureList.getImage(new_id),
  523. false, index);
  524. gAgentAvatarp->wearableUpdated(type, false);
  525. // Flag for rebake once this update cycle is finished.
  526. LLLocalBitmap::setNeedsRebake();
  527. }
  528. }
  529. }
  530. }
  531. void LLLocalBitmap::updateGLTFMaterials(const LLUUID& old_id,
  532. const LLUUID& new_id)
  533. {
  534. for (U32 i = 0, count = mGLTFMaterialWithLocalTextures.size(); i < count; )
  535. {
  536. LLPointer<LLGLTFMaterial>& matp = mGLTFMaterialWithLocalTextures[i];
  537. if (matp.notNull() && matp->getNumRefs() > 1 &&
  538. matp->replaceLocalTexture(mTrackingID, old_id, new_id))
  539. {
  540. ++i;
  541. }
  542. // Matching Id not found or material no more in use: remove it.
  543. if (i != --count)
  544. {
  545. mGLTFMaterialWithLocalTextures[i] =
  546. std::move(mGLTFMaterialWithLocalTextures.back());
  547. }
  548. mGLTFMaterialWithLocalTextures.pop_back();
  549. }
  550. // Render material consists of base and override materials, make sure
  551. // replaceLocalTexture() gets called for base and override before
  552. // applyOverride().
  553. typedef LLFetchedGLTFMaterial::te_list_t entries_list_t;
  554. for (U32 i = 0, count = mGLTFMaterialWithLocalTextures.size(); i < count;
  555. ++i)
  556. {
  557. LLFetchedGLTFMaterial* matp =
  558. mGLTFMaterialWithLocalTextures[i]->asFetched();
  559. if (!matp)
  560. {
  561. continue;
  562. }
  563. // Normally a change in applied material id is supposed to drop
  564. // overrides thus reset material, but local materials currently reuse
  565. // their existing asset Id, since their purpose is to preview how
  566. // material will work in-world, overrides included, so do an override
  567. // to render update instead.
  568. const entries_list_t& entries = matp->getTexEntries();
  569. for (entries_list_t::const_iterator it = entries.begin(),
  570. end = entries.end();
  571. it != end; ++it)
  572. {
  573. LLTextureEntry* tep = *it;
  574. if (!tep) // Paranoia
  575. {
  576. continue;
  577. }
  578. LLGLTFMaterial* omatp = tep->getGLTFMaterialOverride();
  579. if (!omatp || !tep->getGLTFRenderMaterial())
  580. {
  581. continue;
  582. }
  583. // Do not create a new material, reuse existing pointer.
  584. LLFetchedGLTFMaterial* rmatp =
  585. tep->getGLTFRenderMaterial()->asFetched();
  586. if (rmatp)
  587. {
  588. *rmatp = *matp;
  589. rmatp->applyOverride(*omatp);
  590. }
  591. }
  592. }
  593. }
  594. LLAvatarAppearanceDefines::ETextureIndex LLLocalBitmap::getTexIndex(LLWearableType::EType type,
  595. LLAvatarAppearanceDefines::EBakedTextureIndex baked_texind)
  596. {
  597. // Using TEX_NUM_INDICES as a default/fail return
  598. LLAvatarAppearanceDefines::ETextureIndex result = LLAvatarAppearanceDefines::TEX_NUM_INDICES;
  599. switch (type)
  600. {
  601. case LLWearableType::WT_ALPHA:
  602. {
  603. switch (baked_texind)
  604. {
  605. case LLAvatarAppearanceDefines::BAKED_EYES:
  606. result = LLAvatarAppearanceDefines::TEX_EYES_ALPHA;
  607. break;
  608. case LLAvatarAppearanceDefines::BAKED_HAIR:
  609. result = LLAvatarAppearanceDefines::TEX_HAIR_ALPHA;
  610. break;
  611. case LLAvatarAppearanceDefines::BAKED_HEAD:
  612. result = LLAvatarAppearanceDefines::TEX_HEAD_ALPHA;
  613. break;
  614. case LLAvatarAppearanceDefines::BAKED_LOWER:
  615. result = LLAvatarAppearanceDefines::TEX_LOWER_ALPHA;
  616. break;
  617. case LLAvatarAppearanceDefines::BAKED_UPPER:
  618. result = LLAvatarAppearanceDefines::TEX_UPPER_ALPHA;
  619. break;
  620. default:
  621. break;
  622. }
  623. break;
  624. }
  625. case LLWearableType::WT_EYES:
  626. {
  627. if (baked_texind == LLAvatarAppearanceDefines::BAKED_EYES)
  628. {
  629. result = LLAvatarAppearanceDefines::TEX_EYES_IRIS;
  630. }
  631. break;
  632. }
  633. case LLWearableType::WT_GLOVES:
  634. {
  635. if (baked_texind == LLAvatarAppearanceDefines::BAKED_UPPER)
  636. {
  637. result = LLAvatarAppearanceDefines::TEX_UPPER_GLOVES;
  638. }
  639. break;
  640. }
  641. case LLWearableType::WT_JACKET:
  642. {
  643. if (baked_texind == LLAvatarAppearanceDefines::BAKED_LOWER)
  644. {
  645. result = LLAvatarAppearanceDefines::TEX_LOWER_JACKET;
  646. }
  647. else if (baked_texind == LLAvatarAppearanceDefines::BAKED_UPPER)
  648. {
  649. result = LLAvatarAppearanceDefines::TEX_UPPER_JACKET;
  650. }
  651. break;
  652. }
  653. case LLWearableType::WT_PANTS:
  654. {
  655. if (baked_texind == LLAvatarAppearanceDefines::BAKED_LOWER)
  656. {
  657. result = LLAvatarAppearanceDefines::TEX_LOWER_PANTS;
  658. }
  659. break;
  660. }
  661. case LLWearableType::WT_SHIRT:
  662. {
  663. if (baked_texind == LLAvatarAppearanceDefines::BAKED_UPPER)
  664. {
  665. result = LLAvatarAppearanceDefines::TEX_UPPER_SHIRT;
  666. }
  667. break;
  668. }
  669. case LLWearableType::WT_SHOES:
  670. {
  671. if (baked_texind == LLAvatarAppearanceDefines::BAKED_LOWER)
  672. {
  673. result = LLAvatarAppearanceDefines::TEX_LOWER_SHOES;
  674. }
  675. break;
  676. }
  677. case LLWearableType::WT_SKIN:
  678. {
  679. switch (baked_texind)
  680. {
  681. case LLAvatarAppearanceDefines::BAKED_HEAD:
  682. result = LLAvatarAppearanceDefines::TEX_HEAD_BODYPAINT;
  683. break;
  684. case LLAvatarAppearanceDefines::BAKED_LOWER:
  685. result = LLAvatarAppearanceDefines::TEX_LOWER_BODYPAINT;
  686. break;
  687. case LLAvatarAppearanceDefines::BAKED_UPPER:
  688. result = LLAvatarAppearanceDefines::TEX_UPPER_BODYPAINT;
  689. break;
  690. default:
  691. break;
  692. }
  693. break;
  694. }
  695. case LLWearableType::WT_SKIRT:
  696. {
  697. if (baked_texind == LLAvatarAppearanceDefines::BAKED_SKIRT)
  698. {
  699. result = LLAvatarAppearanceDefines::TEX_SKIRT;
  700. }
  701. break;
  702. }
  703. case LLWearableType::WT_SOCKS:
  704. {
  705. if (baked_texind == LLAvatarAppearanceDefines::BAKED_LOWER)
  706. {
  707. result = LLAvatarAppearanceDefines::TEX_LOWER_SOCKS;
  708. }
  709. break;
  710. }
  711. case LLWearableType::WT_TATTOO:
  712. {
  713. switch (baked_texind)
  714. {
  715. case LLAvatarAppearanceDefines::BAKED_HEAD:
  716. result = LLAvatarAppearanceDefines::TEX_HEAD_TATTOO;
  717. break;
  718. case LLAvatarAppearanceDefines::BAKED_LOWER:
  719. result = LLAvatarAppearanceDefines::TEX_LOWER_TATTOO;
  720. break;
  721. case LLAvatarAppearanceDefines::BAKED_UPPER:
  722. result = LLAvatarAppearanceDefines::TEX_UPPER_TATTOO;
  723. break;
  724. default:
  725. break;
  726. }
  727. break;
  728. }
  729. case LLWearableType::WT_UNIVERSAL:
  730. {
  731. switch (baked_texind)
  732. {
  733. case LLAvatarAppearanceDefines::BAKED_HEAD:
  734. result = LLAvatarAppearanceDefines::TEX_HEAD_UNIVERSAL_TATTOO;
  735. break;
  736. case LLAvatarAppearanceDefines::BAKED_UPPER:
  737. result = LLAvatarAppearanceDefines::TEX_UPPER_UNIVERSAL_TATTOO;
  738. break;
  739. case LLAvatarAppearanceDefines::BAKED_LOWER:
  740. result = LLAvatarAppearanceDefines::TEX_LOWER_UNIVERSAL_TATTOO;
  741. break;
  742. case LLAvatarAppearanceDefines::BAKED_HAIR:
  743. result = LLAvatarAppearanceDefines::TEX_HAIR_TATTOO;
  744. break;
  745. case LLAvatarAppearanceDefines::BAKED_EYES:
  746. result = LLAvatarAppearanceDefines::TEX_EYES_TATTOO;
  747. break;
  748. case LLAvatarAppearanceDefines::BAKED_LEFT_ARM:
  749. result = LLAvatarAppearanceDefines::TEX_LEFT_ARM_TATTOO;
  750. break;
  751. case LLAvatarAppearanceDefines::BAKED_LEFT_LEG:
  752. result = LLAvatarAppearanceDefines::TEX_LEFT_LEG_TATTOO;
  753. break;
  754. case LLAvatarAppearanceDefines::BAKED_SKIRT:
  755. result = LLAvatarAppearanceDefines::TEX_SKIRT_TATTOO;
  756. break;
  757. case LLAvatarAppearanceDefines::BAKED_AUX1:
  758. result = LLAvatarAppearanceDefines::TEX_AUX1_TATTOO;
  759. break;
  760. case LLAvatarAppearanceDefines::BAKED_AUX2:
  761. result = LLAvatarAppearanceDefines::TEX_AUX2_TATTOO;
  762. break;
  763. case LLAvatarAppearanceDefines::BAKED_AUX3:
  764. result = LLAvatarAppearanceDefines::TEX_AUX3_TATTOO;
  765. break;
  766. default:
  767. break;
  768. }
  769. break;
  770. }
  771. case LLWearableType::WT_UNDERPANTS:
  772. {
  773. if (baked_texind == LLAvatarAppearanceDefines::BAKED_LOWER)
  774. {
  775. result = LLAvatarAppearanceDefines::TEX_LOWER_UNDERPANTS;
  776. }
  777. break;
  778. }
  779. case LLWearableType::WT_UNDERSHIRT:
  780. {
  781. if (baked_texind == LLAvatarAppearanceDefines::BAKED_UPPER)
  782. {
  783. result = LLAvatarAppearanceDefines::TEX_UPPER_UNDERSHIRT;
  784. }
  785. break;
  786. }
  787. default:
  788. {
  789. llwarns << "Unknown wearable type: " << (S32)type
  790. << " - Baked texture index: " << (S32)baked_texind
  791. << " - Filename: " << mFilename
  792. << " - TrackingID: " << mTrackingID
  793. << " - InworldID: " << mWorldID << llendl;
  794. }
  795. }
  796. return result;
  797. }
  798. //static
  799. LLLocalBitmap* LLLocalBitmap::addUnit(const std::string& filename)
  800. {
  801. if (filename.empty() || !LLFile::exists(filename))
  802. {
  803. return NULL;
  804. }
  805. LLLocalBitmap* bitmapp = new LLLocalBitmap(filename);
  806. if (!bitmapp || !bitmapp->getValid())
  807. {
  808. delete bitmapp;
  809. return NULL;
  810. }
  811. sTimer.stopTimer();
  812. sBitmapList.push_back(bitmapp);
  813. ++sBitmapsListVersion;
  814. sTimer.startTimer();
  815. return bitmapp;
  816. }
  817. //static
  818. void LLLocalBitmap::addUnitsCallback(HBFileSelector::ELoadFilter type,
  819. std::deque<std::string>& files, void*)
  820. {
  821. bool updated = false;
  822. while (!files.empty())
  823. {
  824. std::string filename = files.front();
  825. files.pop_front();
  826. if (!filename.empty())
  827. {
  828. sTimer.stopTimer();
  829. LLLocalBitmap* bitmapp = new LLLocalBitmap(filename);
  830. if (bitmapp && bitmapp->getValid())
  831. {
  832. sBitmapList.push_back(bitmapp);
  833. updated = true;
  834. }
  835. else
  836. {
  837. LLSD notif_args;
  838. notif_args["FNAME"] = filename;
  839. gNotifications.add("LocalBitmapsVerifyFail", notif_args);
  840. delete bitmapp;
  841. }
  842. sTimer.startTimer();
  843. }
  844. }
  845. if (updated)
  846. {
  847. ++sBitmapsListVersion;
  848. }
  849. }
  850. //static
  851. void LLLocalBitmap::addUnits()
  852. {
  853. HBFileSelector::loadFiles(HBFileSelector::FFLOAD_IMAGE, addUnitsCallback);
  854. }
  855. //static
  856. void LLLocalBitmap::delUnit(const LLUUID& tracking_id)
  857. {
  858. bool updated = false;
  859. for (list_t::iterator iter = sBitmapList.begin(), end = sBitmapList.end();
  860. iter != end; )
  861. {
  862. list_t::iterator curiter = iter++;
  863. LLLocalBitmap* bitmapp = *curiter;
  864. if (bitmapp->getTrackingID() == tracking_id)
  865. {
  866. // std::list:erase() preserves all iterators but curiter
  867. sBitmapList.erase(curiter);
  868. delete bitmapp;
  869. updated = true;
  870. }
  871. }
  872. if (updated)
  873. {
  874. ++sBitmapsListVersion;
  875. }
  876. }
  877. //static
  878. const LLUUID& LLLocalBitmap::getWorldID(const LLUUID& tracking_id)
  879. {
  880. for (list_t::const_iterator it = sBitmapList.begin(),
  881. end = sBitmapList.end();
  882. it != end; ++it)
  883. {
  884. LLLocalBitmap* bitmapp = *it;
  885. if (bitmapp && bitmapp->getTrackingID() == tracking_id)
  886. {
  887. return bitmapp->getWorldID();
  888. }
  889. }
  890. return LLUUID::null;
  891. }
  892. //static
  893. bool LLLocalBitmap::isLocal(const LLUUID& world_id)
  894. {
  895. for (list_t::const_iterator it = sBitmapList.begin(),
  896. end = sBitmapList.end();
  897. it != end; ++it)
  898. {
  899. LLLocalBitmap* bitmapp = *it;
  900. if (bitmapp && bitmapp->getWorldID() == world_id)
  901. {
  902. return true;
  903. }
  904. }
  905. return false;
  906. }
  907. //static
  908. const std::string& LLLocalBitmap::getFilename(const LLUUID& tracking_id)
  909. {
  910. for (list_t::const_iterator it = sBitmapList.begin(),
  911. end = sBitmapList.end();
  912. it != end; ++it)
  913. {
  914. LLLocalBitmap* bitmapp = *it;
  915. if (bitmapp && bitmapp->getTrackingID() == tracking_id)
  916. {
  917. return bitmapp->getFilename();
  918. }
  919. }
  920. return LLStringUtil::null;
  921. }
  922. //static
  923. void LLLocalBitmap::doUpdates()
  924. {
  925. // Preventing theoretical overlap in cases of huge number of loaded images.
  926. sTimer.stopTimer();
  927. sNeedsRebake = false;
  928. for (list_t::iterator iter = sBitmapList.begin(), end = sBitmapList.end();
  929. iter != end; ++iter)
  930. {
  931. (*iter)->updateSelf();
  932. }
  933. doRebake();
  934. sTimer.startTimer();
  935. }
  936. //static
  937. void LLLocalBitmap::setNeedsRebake()
  938. {
  939. sNeedsRebake = true;
  940. }
  941. // Separated that from doUpdates to insure a rebake can be called separately
  942. // during deletion
  943. //static
  944. void LLLocalBitmap::doRebake()
  945. {
  946. if (sNeedsRebake)
  947. {
  948. gAgentAvatarp->forceBakeAllTextures(true);
  949. sNeedsRebake = false;
  950. }
  951. }
  952. //static
  953. boost::signals2::connection LLLocalBitmap::setOnChangedCallback(const LLUUID& id,
  954. const changed_cb_t& cb)
  955. {
  956. for (list_t::iterator it = sBitmapList.begin(), end = sBitmapList.end();
  957. it != end; ++it)
  958. {
  959. LLLocalBitmap* bitmapp = *it;
  960. if (bitmapp->mTrackingID == id)
  961. {
  962. return bitmapp->setChangedCallback(cb);
  963. }
  964. }
  965. return boost::signals2::connection();
  966. }
  967. //static
  968. void LLLocalBitmap::associateGLTFMaterial(const LLUUID& id,
  969. LLGLTFMaterial* matp)
  970. {
  971. for (list_t::iterator it = sBitmapList.begin(), end = sBitmapList.end();
  972. it != end; ++it)
  973. {
  974. LLLocalBitmap* bitmapp = *it;
  975. if (bitmapp->mTrackingID == id)
  976. {
  977. bitmapp->addGLTFMaterial(matp);
  978. // There should be only one such tracking Id in the list... HB
  979. return;
  980. }
  981. }
  982. }
  983. ///////////////////////////////////////////////////////////////////////////////
  984. // LLLocalBitmapTimer class
  985. ///////////////////////////////////////////////////////////////////////////////
  986. LLLocalBitmapTimer::LLLocalBitmapTimer()
  987. : LLEventTimer(LL_LOCAL_TIMER_HEARTBEAT)
  988. {
  989. }
  990. void LLLocalBitmapTimer::startTimer()
  991. {
  992. mEventTimer.start();
  993. }
  994. void LLLocalBitmapTimer::stopTimer()
  995. {
  996. mEventTimer.stop();
  997. }
  998. bool LLLocalBitmapTimer::isRunning()
  999. {
  1000. return mEventTimer.getStarted();
  1001. }
  1002. bool LLLocalBitmapTimer::tick()
  1003. {
  1004. LLLocalBitmap::doUpdates();
  1005. return false;
  1006. }