llviewertexlayer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /**
  2. * @file llviewertexlayer.cpp
  3. * @brief Viewer texture layer. Used for avatars.
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2010, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llviewertexlayer.h"
  34. #include "imageids.h"
  35. #include "llcorehttputil.h"
  36. #include "llfilesystem.h"
  37. #include "llimagej2c.h"
  38. #include "llnotifications.h"
  39. #include "llsdserialize.h"
  40. #include "llagent.h"
  41. #include "llagentwearables.h"
  42. #include "llpipeline.h"
  43. #include "llviewercontrol.h"
  44. #include "llviewershadermgr.h"
  45. #include "llviewerstats.h"
  46. #include "llvoavatarself.h"
  47. using namespace LLAvatarAppearanceDefines;
  48. static constexpr S32 BAKE_UPLOAD_ATTEMPTS = 7;
  49. // Actual delay grows by power of 2 each attempt:
  50. static constexpr F32 BAKE_UPLOAD_RETRY_DELAY = 2.f;
  51. //-----------------------------------------------------------------------------
  52. // LLBakedUploadData()
  53. // Used by LLTexLayerSetBuffer for a callback.
  54. //-----------------------------------------------------------------------------
  55. struct LLBakedUploadData
  56. {
  57. LLBakedUploadData(const LLVOAvatarSelf* avatar,
  58. LLViewerTexLayerSet* layerset,
  59. const LLUUID& id, bool highest_res)
  60. : mAvatar(avatar),
  61. mTexLayerSet(layerset),
  62. mID(id),
  63. mStartTime(LLFrameTimer::getTotalTime()), // Record starting time
  64. mIsHighestRes(highest_res)
  65. {
  66. }
  67. const LLUUID mID;
  68. // Note: backlink only; do not use a LLPointer:
  69. const LLVOAvatarSelf* mAvatar;
  70. LLViewerTexLayerSet* mTexLayerSet;
  71. // For measuring baked texture upload time
  72. const U64 mStartTime;
  73. // Whether this is a "final" bake, or intermediate low res
  74. const bool mIsHighestRes;
  75. };
  76. //-----------------------------------------------------------------------------
  77. // LLViewerTexLayerSetBuffer
  78. // The composite image that a LLViewerTexLayerSet writes to. Each
  79. // LLViewerTexLayerSet has one.
  80. //-----------------------------------------------------------------------------
  81. //static
  82. S32 LLViewerTexLayerSetBuffer::sGLByteCount = 0;
  83. LLViewerTexLayerSetBuffer::LLViewerTexLayerSetBuffer(LLTexLayerSet* const owner,
  84. S32 width, S32 height)
  85. : LLTexLayerSetBuffer(owner),
  86. LLViewerDynamicTexture(width, height, 4,
  87. // ORDER_LAST => must render these after the hints
  88. // are created.
  89. LLViewerDynamicTexture::ORDER_LAST, false),
  90. // Not used for any logic here, just to sync sending of updates:
  91. mUploadPending(false),
  92. mNeedsUpload(false),
  93. mNumLowresUploads(0),
  94. mUploadFailCount(0),
  95. mNeedsUpdate(true),
  96. mNumLowresUpdates(0)
  97. {
  98. mImageGLp->setNeedsAlphaAndPickMask(false);
  99. sGLByteCount += getSize();
  100. mNeedsUploadTimer.start();
  101. mNeedsUpdateTimer.start();
  102. }
  103. LLViewerTexLayerSetBuffer::~LLViewerTexLayerSetBuffer()
  104. {
  105. sGLByteCount -= getSize();
  106. destroyGLTexture();
  107. for (S32 order = 0; order < ORDER_COUNT; ++order)
  108. {
  109. // Will fail in all but one case:
  110. LLViewerDynamicTexture::sInstances[order].erase(this);
  111. }
  112. }
  113. //virtual
  114. S8 LLViewerTexLayerSetBuffer::getType() const
  115. {
  116. return LLViewerDynamicTexture::LL_TEX_LAYER_SET_BUFFER;
  117. }
  118. //static
  119. void LLViewerTexLayerSetBuffer::dumpTotalByteCount()
  120. {
  121. llinfos << "Composite System GL Buffers: " << sGLByteCount / 1024 << "KB"
  122. << llendl;
  123. }
  124. void LLViewerTexLayerSetBuffer::requestUpdate()
  125. {
  126. restartUpdateTimer();
  127. mNeedsUpdate = true;
  128. mNumLowresUpdates = 0;
  129. // If we are in the middle of uploading a baked texture, we do not care
  130. // about it any more. When it is downloaded, ignore it.
  131. mUploadID.setNull();
  132. }
  133. void LLViewerTexLayerSetBuffer::requestUpload()
  134. {
  135. conditionalRestartUploadTimer();
  136. mNeedsUpload = true;
  137. mNumLowresUploads = 0;
  138. mUploadPending = true;
  139. }
  140. void LLViewerTexLayerSetBuffer::conditionalRestartUploadTimer()
  141. {
  142. // If we requested a new upload but have not even uploaded a low res
  143. // version of our last upload request, then keep the timer ticking instead
  144. // of resetting it.
  145. if (mNeedsUpload && mNumLowresUploads == 0)
  146. {
  147. mNeedsUploadTimer.unpause();
  148. }
  149. else
  150. {
  151. mNeedsUploadTimer.reset();
  152. mNeedsUploadTimer.start();
  153. }
  154. }
  155. void LLViewerTexLayerSetBuffer::restartUpdateTimer()
  156. {
  157. mNeedsUpdateTimer.reset();
  158. mNeedsUpdateTimer.start();
  159. }
  160. void LLViewerTexLayerSetBuffer::cancelUpload()
  161. {
  162. mNeedsUpload = false;
  163. mUploadPending = false;
  164. mNeedsUploadTimer.pause();
  165. mUploadRetryTimer.reset();
  166. }
  167. //virtual
  168. bool LLViewerTexLayerSetBuffer::needsRender()
  169. {
  170. llassert(mTexLayerSet->getAvatarAppearance() == gAgentAvatarp);
  171. if (!isAgentAvatarValid()) return false;
  172. bool update_now = mNeedsUpdate && isReadyToUpdate();
  173. bool upload_now = mNeedsUpload && isReadyToUpload();
  174. // Do not render if we do not want to (or are not ready to) upload or
  175. // update
  176. if (!update_now && !upload_now)
  177. {
  178. return false;
  179. }
  180. // Do not render if we are animating our appearance.
  181. if (gAgentAvatarp->getIsAppearanceAnimating())
  182. {
  183. return false;
  184. }
  185. // Do not render if we are trying to create a skirt texture but are not
  186. // wearing a skirt.
  187. if (gAgentAvatarp->getBakedTE(getViewerTexLayerSet()) == TEX_SKIRT_BAKED &&
  188. !gAgentAvatarp->isWearingWearableType(LLWearableType::WT_SKIRT))
  189. {
  190. cancelUpload();
  191. return false;
  192. }
  193. // Render if we have at least minimal level of detail for each local
  194. // texture.
  195. return getViewerTexLayerSet()->isLocalTextureDataAvailable();
  196. }
  197. //virtual
  198. void LLViewerTexLayerSetBuffer::preRenderTexLayerSet()
  199. {
  200. LLTexLayerSetBuffer::preRenderTexLayerSet();
  201. // Keep depth buffer, we do not need to clear it
  202. LLViewerDynamicTexture::preRender(false);
  203. }
  204. //virtual
  205. void LLViewerTexLayerSetBuffer::postRenderTexLayerSet(bool success)
  206. {
  207. LLTexLayerSetBuffer::postRenderTexLayerSet(success);
  208. LLViewerDynamicTexture::postRender(success);
  209. }
  210. //virtual
  211. void LLViewerTexLayerSetBuffer::midRenderTexLayerSet(bool success)
  212. {
  213. // Do we need to upload, and do we have sufficient data to create an
  214. // uploadable composite ?
  215. // *TODO: When do we upload the texture if gAgent.mNumPendingQueries is
  216. // non-zero ?
  217. bool update_now = mNeedsUpdate && isReadyToUpdate();
  218. bool upload_now = mNeedsUpload && isReadyToUpload();
  219. if (upload_now)
  220. {
  221. if (success)
  222. {
  223. doUpload();
  224. }
  225. else
  226. {
  227. llinfos << "Failed attempt to bake "
  228. << mTexLayerSet->getBodyRegionName() << llendl;
  229. mUploadPending = false;
  230. }
  231. }
  232. if (update_now)
  233. {
  234. doUpdate();
  235. }
  236. // *TODO: old logic does not check success before setGLTextureCreated
  237. // We have valid texture data now
  238. mImageGLp->setGLTextureCreated(true);
  239. }
  240. bool LLViewerTexLayerSetBuffer::isInitialized() const
  241. {
  242. return mImageGLp.notNull() && mImageGLp->isGLTextureCreated();
  243. }
  244. bool LLViewerTexLayerSetBuffer::isReadyToUpload()
  245. {
  246. if (!gAgentQueryManager.hasNoPendingQueries())
  247. {
  248. return false; // Cannot upload if there are pending queries.
  249. }
  250. if (!isAgentAvatarValid() || gAgentAvatarp->isEditingAppearance())
  251. {
  252. return false; // Do not upload if avatar is using composites.
  253. }
  254. LLViewerTexLayerSet* layer_set = getViewerTexLayerSet();
  255. if (layer_set->isLocalTextureDataFinal())
  256. {
  257. // If we requested an upload and have the final is LOD ready, upload
  258. // (or wait a while if this is a retry)
  259. return mUploadFailCount == 0 ||
  260. mUploadRetryTimer.getElapsedTimeF32() >=
  261. BAKE_UPLOAD_RETRY_DELAY * (1 << (mUploadFailCount - 1));
  262. }
  263. // Upload if we have hit a timeout. Upload is a pretty expensive process so
  264. // we need to make sure we are not doing uploads too frequently.
  265. static LLCachedControl<U32> timeout(gSavedSettings,
  266. "AvatarBakedTextureUploadTimeout");
  267. if (!timeout)
  268. {
  269. return false;
  270. }
  271. // The timeout period increases exponentially between every lowres
  272. // upload in order to prevent spamming the server with frequent uploads.
  273. U32 threshold = timeout * (1 << mNumLowresUploads);
  274. // If we hit our timeout and have textures available at even lower
  275. // resolution, then upload.
  276. return layer_set->isLocalTextureDataAvailable() &&
  277. mNeedsUploadTimer.getElapsedTimeF32() >= threshold;
  278. }
  279. bool LLViewerTexLayerSetBuffer::isReadyToUpdate()
  280. {
  281. // If we requested an update and have the final LOD ready, then update.
  282. LLViewerTexLayerSet* layer_set = getViewerTexLayerSet();
  283. if (layer_set->isLocalTextureDataFinal())
  284. {
  285. return true;
  286. }
  287. // If we have not done an update yet, then just do one now regardless of
  288. // state of textures.
  289. if (mNumLowresUpdates == 0)
  290. {
  291. return true;
  292. }
  293. // Update if we have hit a timeout. Unlike for uploads, we can make this
  294. // timeout fairly small since render unnecessarily does not cost much.
  295. static LLCachedControl<U32> timeout(gSavedSettings,
  296. "AvatarBakedLocalTextureUpdateTimeout");
  297. if (!timeout)
  298. {
  299. return false;
  300. }
  301. // If we hit our timeout and have textures available at even lower
  302. // resolution, then update.
  303. return layer_set->isLocalTextureDataAvailable() &&
  304. mNeedsUpdateTimer.getElapsedTimeF32() >= (F32)timeout;
  305. }
  306. bool LLViewerTexLayerSetBuffer::requestUpdateImmediate()
  307. {
  308. mNeedsUpdate = true;
  309. bool result = false;
  310. if (needsRender())
  311. {
  312. preRender(false);
  313. result = render();
  314. postRender(result);
  315. }
  316. return result;
  317. }
  318. // If needed, create the baked texture and send it out to the server, then wait
  319. // for it to come back so we can switch to using it.
  320. void LLViewerTexLayerSetBuffer::doUpload()
  321. {
  322. LLViewerTexLayerSet* layer_set = getViewerTexLayerSet();
  323. EBakedTextureIndex baked_idx = layer_set->getBakedTexIndex();
  324. bool visible = layer_set->isVisible();
  325. bool skip = (U8)baked_idx >= gAgent.mUploadedBakes;
  326. if (!visible || skip)
  327. {
  328. // Do not wait for any upload result: this bake is invisible anyway
  329. mUploadPending = mNeedsUpload = false;
  330. mNeedsUploadTimer.pause();
  331. // Set bake image as invisible
  332. layer_set->getAvatar()->setNewBakedTexture(baked_idx, IMG_INVISIBLE);
  333. }
  334. if (skip)
  335. {
  336. // Do not upload this bake
  337. return;
  338. }
  339. bool highest_lod = layer_set->isLocalTextureDataFinal();
  340. llinfos << "Uploading baked " << layer_set->getBodyRegionName()
  341. << (highest_lod ? " (full res)" : "(low res)") << llendl;
  342. gViewerStats.incStat(LLViewerStats::ST_TEX_BAKES);
  343. // Do not need caches since we are baked now (note: we would not *really*
  344. // be baked until this image is sent to the server and the AvatarAppearance
  345. // message is received).
  346. layer_set->deleteCaches();
  347. // Get the COLOR information from our texture
  348. U8* baked_color_data = new U8[mFullWidth * mFullHeight * 4];
  349. glReadPixels(mOrigin.mX, mOrigin.mY, mFullWidth, mFullHeight, GL_RGBA,
  350. GL_UNSIGNED_BYTE, baked_color_data);
  351. stop_glerror();
  352. // Get the MASK information from our texture
  353. LLGLSUIDefault gls_ui;
  354. LLPointer<LLImageRaw> baked_mask_image = new LLImageRaw(mFullWidth,
  355. mFullHeight, 1);
  356. U8* baked_mask_data = baked_mask_image->getData();
  357. layer_set->gatherMorphMaskAlpha(baked_mask_data,
  358. mOrigin.mX, mOrigin.mY,
  359. mFullWidth, mFullHeight);
  360. // Create the baked image from our color and mask information
  361. constexpr S32 baked_image_components = 5; // red green blue [bump] clothing
  362. LLPointer<LLImageRaw> baked_image = new LLImageRaw(mFullWidth, mFullHeight,
  363. baked_image_components);
  364. U8* baked_image_data = baked_image->getData();
  365. S32 i = 0;
  366. for (S32 u = 0; u < mFullWidth; ++u)
  367. {
  368. for (S32 v = 0; v < mFullHeight; ++v)
  369. {
  370. S32 k = 4 * i;
  371. S32 j = k + i; // 5 * i
  372. baked_image_data[j++] = baked_color_data[k++];
  373. baked_image_data[j++] = baked_color_data[k++];
  374. baked_image_data[j++] = baked_color_data[k++];
  375. // Alpha should be correct for eyelashes:
  376. baked_image_data[j++] = baked_color_data[k];
  377. baked_image_data[j] = baked_mask_data[i++];
  378. }
  379. }
  380. LLPointer<LLImageJ2C> j2c_img = new LLImageJ2C;
  381. // Writes into baked_color_data. 5 channels (RGB, heightfield/alpha, mask)
  382. if (j2c_img->encode(baked_image, "LL_RGBHM"))
  383. {
  384. LLTransactionID tid;
  385. tid.generate();
  386. const LLAssetID asset_id =
  387. tid.makeAssetID(gAgent.getSecureSessionID());
  388. LLFileSystem j2c_file(asset_id, LLFileSystem::OVERWRITE);
  389. if (j2c_file.write(j2c_img->getData(), j2c_img->getDataSize()))
  390. {
  391. // Read back the file and validate.
  392. bool valid = false;
  393. LLPointer<LLImageJ2C> integrity_test = new LLImageJ2C;
  394. S32 file_size = 0;
  395. LLFileSystem file(asset_id);
  396. file_size = file.getSize();
  397. // Data buffer MUST be allocated using LLImageBase
  398. U8* data = integrity_test->allocateData(file_size);
  399. if (data)
  400. {
  401. file.read(data, file_size);
  402. if (data)
  403. {
  404. // integrity_test will delete 'data'
  405. valid = integrity_test->validate(data, file_size);
  406. }
  407. else
  408. {
  409. integrity_test->setLastError("Unable to read entire file");
  410. }
  411. }
  412. else
  413. {
  414. integrity_test->setLastError("Unable to allocate memory");
  415. }
  416. if (valid)
  417. {
  418. // Baked_upload_data is owned by the responder and deleted
  419. // after the request completes.
  420. LLBakedUploadData* baked_upload_data =
  421. new LLBakedUploadData(gAgentAvatarp, layer_set, asset_id,
  422. highest_lod);
  423. // Upload ID is used to avoid overlaps, e.g. when the user
  424. // rapidly makes two changes outside of Face Edit.
  425. mUploadID = asset_id;
  426. // Upload the image
  427. static LLCachedControl<bool> use_udp(gSavedSettings,
  428. "BakedTexUploadForceUDP");
  429. const std::string& url =
  430. gAgent.getRegionCapability("UploadBakedTexture");
  431. if (!url.empty() && !use_udp &&
  432. // Try last ditch attempt via asset store if cap upload is
  433. // failing
  434. mUploadFailCount < BAKE_UPLOAD_ATTEMPTS - 1)
  435. {
  436. llinfos << "Baked texture upload via capability of "
  437. << mUploadID << " to " << url << llendl;
  438. gCoros.launch("uploadBakedTextureCoro",
  439. boost::bind(&LLViewerTexLayerSetBuffer::uploadBakedTextureCoro,
  440. url, mUploadID,
  441. baked_upload_data));
  442. }
  443. else if (gAssetStoragep)
  444. {
  445. gAssetStoragep->storeAssetData(tid,
  446. LLAssetType::AT_TEXTURE,
  447. onTextureUploadComplete,
  448. baked_upload_data,
  449. true, // temp_file
  450. true, // is_priority
  451. true); // store_local
  452. llinfos << "Baked texture upload via Asset Store."
  453. << llendl;
  454. }
  455. if (highest_lod)
  456. {
  457. // Sending the final LOD for the baked texture. All done,
  458. // pause the upload timer so we know how long it took.
  459. mNeedsUpload = false;
  460. mNeedsUploadTimer.pause();
  461. }
  462. else
  463. {
  464. // Sending a lower level LOD for the baked texture. Restart
  465. // the upload timer.
  466. ++mNumLowresUploads;
  467. mNeedsUploadTimer.unpause();
  468. mNeedsUploadTimer.reset();
  469. }
  470. }
  471. else
  472. {
  473. // The read back and validate operation failed. Remove the
  474. // uploaded file.
  475. mUploadPending = false;
  476. LLFileSystem::removeFile(asset_id);
  477. llinfos << "Unable to create baked upload file (reason: corrupted)."
  478. << llendl;
  479. }
  480. }
  481. }
  482. else
  483. {
  484. // The cache write file operation failed.
  485. mUploadPending = false;
  486. llinfos << "Unable to create baked upload file (reason: failed to write file)"
  487. << llendl;
  488. }
  489. delete[] baked_color_data;
  490. }
  491. void upload_failure(const LLUUID& vfile_id, const std::string& reason)
  492. {
  493. LLSD args;
  494. args["FILE"] = vfile_id.asString();
  495. args["REASON"] = reason;
  496. gNotifications.add("CannotUploadReason", args);
  497. }
  498. //static
  499. void LLViewerTexLayerSetBuffer::uploadBakedTextureCoro(const std::string& url,
  500. LLUUID vfile_id,
  501. LLBakedUploadData* data)
  502. {
  503. if (!data)
  504. {
  505. llwarns << "No baked upload data for baked teture " << vfile_id
  506. << ". Baked texture upload aborted." << llendl;
  507. return;
  508. }
  509. if (!LLFileSystem::getExists(vfile_id))
  510. {
  511. llwarns << "Non-existent cache file Id: " << vfile_id
  512. << ". Baked texture upload aborted." << llendl;
  513. delete data;
  514. return;
  515. }
  516. LLCoreHttpUtil::HttpCoroutineAdapter adapter("uploadBakedTextureCoro");
  517. LLSD result = adapter.postAndSuspend(url, LLSD());
  518. LLCore::HttpStatus status =
  519. LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  520. if (!status)
  521. {
  522. upload_failure(vfile_id, status.toString());
  523. delete data;
  524. return;
  525. }
  526. result.erase(LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS);
  527. if (!result.has("state"))
  528. {
  529. llwarns << "Error: " << result << llendl;
  530. upload_failure(vfile_id, "malformed response contents.");
  531. delete data;
  532. return;
  533. }
  534. std::string state = result["state"].asString();
  535. if (state != "upload")
  536. {
  537. llwarns << "Error: " << result << llendl;
  538. std::string message = result["message"].asString();
  539. if (message.empty())
  540. {
  541. message = "unexpected state in response: " + state;
  542. }
  543. upload_failure(vfile_id, message);
  544. delete data;
  545. return;
  546. }
  547. std::string uploader = result["uploader"].asString();
  548. if (uploader.empty())
  549. {
  550. llwarns << "Error: " << result << llendl;
  551. upload_failure(vfile_id, "no uploader URL in response.");
  552. delete data;
  553. return;
  554. }
  555. result = adapter.postFileAndSuspend(uploader, vfile_id,
  556. LLAssetType::AT_TEXTURE);
  557. status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(result);
  558. if (!status)
  559. {
  560. upload_failure(vfile_id, status.toString());
  561. delete data;
  562. return;
  563. }
  564. result.erase(LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS);
  565. state = result["state"].asString();
  566. if (state != "complete")
  567. {
  568. llwarns << "Error: " << result << llendl;
  569. std::string message = result["message"].asString();
  570. if (message.empty())
  571. {
  572. message = "unexpected state in response: " + state;
  573. }
  574. upload_failure(vfile_id, message);
  575. delete data;
  576. return;
  577. }
  578. LLUUID new_id = result["new_asset"].asUUID();
  579. if (new_id.isNull())
  580. {
  581. llwarns << "Error: " << result << llendl;
  582. upload_failure(vfile_id, "missing new asset Id in response.");
  583. delete data;
  584. return;
  585. }
  586. // Rename the file in the cache to the actual asset id
  587. LLFileSystem::renameFile(vfile_id, new_id);
  588. state = result["state"].asString();
  589. llinfos << "Result: " << state << " - New Id: " << new_id << llendl;
  590. S32 success = state == "complete" ? 0 : -1;
  591. // Note: the baked upload data will be deleted by the following call
  592. onTextureUploadComplete(new_id, data, success);
  593. }
  594. // Mostly bookkeeping; don't need to actually "do" anything since render() will
  595. // actually do the update.
  596. void LLViewerTexLayerSetBuffer::doUpdate()
  597. {
  598. LLViewerTexLayerSet* layer_set = getViewerTexLayerSet();
  599. if (layer_set->isLocalTextureDataFinal())
  600. {
  601. mNeedsUpdate = false;
  602. }
  603. else
  604. {
  605. ++mNumLowresUpdates;
  606. }
  607. restartUpdateTimer();
  608. // need to swtich to using this layerset if this is the first update after
  609. // getting the lowest LOD
  610. layer_set->getAvatar()->updateMeshTextures();
  611. }
  612. // StoreAssetData callback (not fixed)
  613. //static
  614. void LLViewerTexLayerSetBuffer::onTextureUploadComplete(const LLUUID& uuid,
  615. void* userdata,
  616. S32 result, LLExtStat)
  617. {
  618. if (!userdata) return;
  619. LLBakedUploadData* baked_upload_data = (LLBakedUploadData*)userdata;
  620. if (isAgentAvatarValid() &&
  621. // Sanity check: only the user's avatar should be uploading textures:
  622. baked_upload_data->mAvatar == gAgentAvatarp &&
  623. baked_upload_data->mTexLayerSet->hasComposite())
  624. {
  625. LLViewerTexLayerSetBuffer* layerset_buffer =
  626. baked_upload_data->mTexLayerSet->getViewerComposite();
  627. S32 failures = layerset_buffer->mUploadFailCount;
  628. layerset_buffer->mUploadFailCount = 0;
  629. if (layerset_buffer->mUploadID.isNull())
  630. {
  631. // The upload got cancelled, we should be in the process of baking
  632. // a new texture so request an upload with the new data.
  633. // BAP: does this really belong in this callback, as opposed to
  634. // where the cancellation takes place ? Suspect this does nothing.
  635. layerset_buffer->requestUpload();
  636. }
  637. else if (baked_upload_data->mID == layerset_buffer->mUploadID)
  638. {
  639. // This is the upload we are currently waiting for.
  640. layerset_buffer->mUploadID.setNull();
  641. const std::string name =
  642. baked_upload_data->mTexLayerSet->getBodyRegionName();
  643. const std::string resolution =
  644. baked_upload_data->mIsHighestRes ? " full res " : " low res ";
  645. if (result >= 0)
  646. {
  647. // Allows sending of AgentSetAppearance later:
  648. layerset_buffer->mUploadPending = false;
  649. ETextureIndex baked_te =
  650. gAgentAvatarp->getBakedTE(layerset_buffer->getViewerTexLayerSet());
  651. // Update baked texture info with the new UUID
  652. U64 now = LLFrameTimer::getTotalTime(); // Record starting time
  653. llinfos << "Baked" << resolution << "texture upload for "
  654. << name << " took "
  655. << (S32)((now - baked_upload_data->mStartTime) / 1000)
  656. << " ms" << llendl;
  657. gAgentAvatarp->setNewBakedTexture(baked_te, uuid);
  658. }
  659. else
  660. {
  661. ++failures;
  662. S32 max_attempts =
  663. baked_upload_data->mIsHighestRes ? BAKE_UPLOAD_ATTEMPTS
  664. : 1; // only retry final bakes
  665. llwarns << "Baked" << resolution << "texture upload for "
  666. << name << " failed (attempt " << failures << "/"
  667. << max_attempts << ")" << llendl;
  668. if (failures < max_attempts)
  669. {
  670. layerset_buffer->mUploadFailCount = failures;
  671. layerset_buffer->mUploadRetryTimer.start();
  672. layerset_buffer->requestUpload();
  673. }
  674. }
  675. }
  676. else
  677. {
  678. llinfos << "Received baked texture out of date, ignored."
  679. << llendl;
  680. }
  681. gAgentAvatarp->dirtyMesh();
  682. }
  683. else
  684. {
  685. // Baked texture failed to upload (in which case since we didn't set
  686. // the new baked texture, it means that they'll try and rebake it at
  687. // some point in the future (after login ?)), or this response to
  688. // upload is out of date, in which case a current response should be on
  689. // the way or already processed.
  690. llwarns << "Baked upload failed" << llendl;
  691. }
  692. delete baked_upload_data;
  693. }
  694. //-----------------------------------------------------------------------------
  695. // LLViewerTexLayerSet
  696. // An ordered set of texture layers that get composited into a single texture.
  697. //-----------------------------------------------------------------------------
  698. LLViewerTexLayerSet::LLViewerTexLayerSet(LLAvatarAppearance* const appearance)
  699. : LLTexLayerSet(appearance),
  700. mUpdatesEnabled(false)
  701. {
  702. }
  703. // Returns true if at least one packet of data has been received for each of
  704. // the textures that this layerset depends on.
  705. bool LLViewerTexLayerSet::isLocalTextureDataAvailable()
  706. {
  707. return mAvatarAppearance->isSelf() &&
  708. getAvatar()->isLocalTextureDataAvailable(this);
  709. }
  710. // Returns true if all of the data for the textures that this layerset depends
  711. // on have arrived.
  712. bool LLViewerTexLayerSet::isLocalTextureDataFinal()
  713. {
  714. return mAvatarAppearance->isSelf() &&
  715. getAvatar()->isLocalTextureDataFinal(this);
  716. }
  717. void LLViewerTexLayerSet::requestUpdate()
  718. {
  719. if (mUpdatesEnabled)
  720. {
  721. createComposite();
  722. getViewerComposite()->requestUpdate();
  723. }
  724. }
  725. void LLViewerTexLayerSet::requestUpload()
  726. {
  727. createComposite();
  728. getViewerComposite()->requestUpload();
  729. }
  730. void LLViewerTexLayerSet::cancelUpload()
  731. {
  732. LLViewerTexLayerSetBuffer* composite = getViewerComposite();
  733. if (composite)
  734. {
  735. composite->cancelUpload();
  736. }
  737. }
  738. void LLViewerTexLayerSet::updateComposite()
  739. {
  740. createComposite();
  741. getViewerComposite()->requestUpdateImmediate();
  742. }
  743. //virtual
  744. void LLViewerTexLayerSet::createComposite()
  745. {
  746. if (!mComposite)
  747. {
  748. S32 width = mInfo->getWidth();
  749. S32 height = mInfo->getHeight();
  750. if (!mAvatarAppearance->isSelf())
  751. {
  752. llerrs << "composites should not be created for non-self avatars !"
  753. << llendl;
  754. }
  755. mComposite = new LLViewerTexLayerSetBuffer(this, width, height);
  756. }
  757. }
  758. LLVOAvatarSelf* LLViewerTexLayerSet::getAvatar()
  759. {
  760. // Note: this is a legit static cast, because LLAvatarAppearance is only
  761. // used as a parent class for LLVOAvatar: should this change in the future,
  762. // the cast below would become illegal.
  763. LLVOAvatar* avatarp = (LLVOAvatar*)mAvatarAppearance;
  764. return avatarp->isSelf() ? (LLVOAvatarSelf*)avatarp : NULL;
  765. }
  766. const LLVOAvatarSelf* LLViewerTexLayerSet::getAvatar() const
  767. {
  768. // Note: this is a legit static cast, because LLAvatarAppearance is only
  769. // used as a parent class for LLVOAvatar: should this change in the future,
  770. // the cast below would become illegal.
  771. const LLVOAvatar* avatarp = (const LLVOAvatar*)mAvatarAppearance;
  772. return avatarp->isSelf() ? (const LLVOAvatarSelf*)avatarp : NULL;
  773. }
  774. LLViewerTexLayerSetBuffer* LLViewerTexLayerSet::getViewerComposite()
  775. {
  776. LLTexLayerSetBuffer* bufferp = getComposite();
  777. return bufferp ? bufferp->asViewerTexLayerSetBuffer() : NULL;
  778. }