llmaterialmgr.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * @file llmaterialmgr.h
  3. * @brief Material manager
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewergpl$
  6. *
  7. * Copyright (c) 2012, Linden Research, Inc.
  8. * Copyright (c) 2012-2024, Henri Beauchamp.
  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. #ifndef LL_LLMATERIALMGR_H
  34. #define LL_LLMATERIALMGR_H
  35. #include "boost/signals2.hpp"
  36. #include "llcorehttputil.h"
  37. #include "hbfastmap.h"
  38. #include "llmaterial.h"
  39. #include "llmaterialid.h"
  40. #include "llsingleton.h"
  41. #include "lluuid.h"
  42. class LLViewerRegion;
  43. class LLMaterialMgr : public LLSingleton<LLMaterialMgr>
  44. {
  45. friend class LLSingleton<LLMaterialMgr>;
  46. protected:
  47. LOG_CLASS(LLMaterialMgr);
  48. LLMaterialMgr();
  49. virtual ~LLMaterialMgr();
  50. public:
  51. typedef fast_hmap<LLMaterialID, LLMaterialPtr> material_map_t;
  52. typedef boost::signals2::signal<void(const LLMaterialID&,
  53. const LLMaterialPtr)> get_cb_t;
  54. typedef boost::signals2::signal<void(const LLMaterialID&,
  55. const LLMaterialPtr,
  56. U32 te)> get_te_cb_t;
  57. typedef boost::signals2::signal<void(const LLUUID&,
  58. const material_map_t&)> get_all_cb_t;
  59. const LLMaterialPtr get(const LLUUID& region_id,
  60. const LLMaterialID& material_id);
  61. void get(const LLUUID& region_id, const LLMaterialID& material_id,
  62. get_cb_t::slot_type cb);
  63. void getTE(const LLUUID& region_id, const LLMaterialID& material_id,
  64. U32 te, get_te_cb_t::slot_type cb);
  65. void getAll(const LLUUID& region_id);
  66. void getAll(const LLUUID& region_id, get_all_cb_t::slot_type cb);
  67. void put(const LLUUID& object_id, U8 te, const LLMaterial& material);
  68. void remove(const LLUUID& object_id, U8 te);
  69. // Explicitly add new material to material manager
  70. void setLocalMaterial(const LLUUID& region_id, LLMaterialPtr material_ptr);
  71. // To be called on viewer exit, to cleanup boost signal callbacks and
  72. // material queues. HB
  73. static void cleanupClass();
  74. private:
  75. void clearGetQueues(const LLUUID& region_id);
  76. bool isGetPending(const LLUUID& region_id,
  77. const LLMaterialID& material_id) const;
  78. bool isGetAllPending(const LLUUID& region_id) const;
  79. void markGetPending(const LLUUID& region_id,
  80. const LLMaterialID& material_id);
  81. const LLMaterialPtr setMaterial(const LLUUID& region_id,
  82. const LLMaterialID& material_id,
  83. const LLSD& material_data);
  84. void setMaterialCallbacks(const LLMaterialID& material_id,
  85. const LLMaterialPtr& material_ptr);
  86. static void onIdle(void*);
  87. void processGetQueue();
  88. void onGetResponse(bool success, const LLSD& content,
  89. const LLUUID& region_id);
  90. void processGetAllQueue();
  91. void processGetAllQueueCoro(const std::string& url, LLUUID region_id);
  92. void onGetAllResponse(bool success, const LLSD& content,
  93. const LLUUID& region_id);
  94. void processPutQueue();
  95. void onPutResponse(bool success, const LLSD& content);
  96. void onRegionRemoved(LLViewerRegion* regionp);
  97. public:
  98. // Class for TE-specific material ID query
  99. class TEMaterialPair
  100. {
  101. public:
  102. LL_INLINE bool operator==(const TEMaterialPair& rhs) const
  103. {
  104. return mMaterialId == rhs.mMaterialId && mTE == rhs.mTE;
  105. }
  106. public:
  107. U32 mTE;
  108. LLMaterialID mMaterialId;
  109. };
  110. friend LL_INLINE bool operator<(const LLMaterialMgr::TEMaterialPair& lhs,
  111. const LLMaterialMgr::TEMaterialPair& rhs)
  112. {
  113. return lhs.mTE < rhs.mTE ? true : lhs.mMaterialId < rhs.mMaterialId;
  114. }
  115. // Class for pending material in a given region
  116. class RegionMaterialPair
  117. {
  118. public:
  119. LL_INLINE RegionMaterialPair(const LLUUID& region_id,
  120. const LLMaterialID& material_id)
  121. : mRegionId(region_id),
  122. mMaterialId(material_id)
  123. {
  124. }
  125. LL_INLINE bool operator==(const RegionMaterialPair& rhs) const
  126. {
  127. return mRegionId == rhs.mRegionId &&
  128. mMaterialId == rhs.mMaterialId;
  129. }
  130. public:
  131. LLUUID mRegionId;
  132. LLMaterialID mMaterialId;
  133. };
  134. friend LL_INLINE bool operator<(const LLMaterialMgr::RegionMaterialPair& lhs,
  135. const LLMaterialMgr::RegionMaterialPair& rhs)
  136. {
  137. return lhs.mRegionId < rhs.mRegionId ? true
  138. : lhs.mMaterialId < rhs.mMaterialId;
  139. }
  140. private:
  141. material_map_t mMaterials;
  142. uuid_list_t mGetAllQueue;
  143. uuid_list_t mGetAllRequested;
  144. typedef std::set<LLMaterialID> material_queue_t;
  145. typedef fast_hmap<LLUUID, material_queue_t> get_queue_t;
  146. get_queue_t mGetQueue;
  147. typedef fast_hmap<RegionMaterialPair, F64> get_pending_map_t;
  148. get_pending_map_t mGetPending;
  149. typedef fast_hmap<LLUUID, F64> getall_pending_map_t;
  150. getall_pending_map_t mGetAllPending;
  151. typedef fast_hmap<LLMaterialID, get_cb_t*> get_cb_map_t;
  152. get_cb_map_t mGetCallbacks;
  153. typedef fast_hmap<TEMaterialPair, get_te_cb_t*> get_te_cb_map_t;
  154. get_te_cb_map_t mGetTECallbacks;
  155. typedef fast_hmap<LLUUID, get_all_cb_t*> get_all_cb_map_t;
  156. get_all_cb_map_t mGetAllCallbacks;
  157. typedef fast_hmap<U8, LLMaterial> facematerial_map_t;
  158. typedef fast_hmap<LLUUID, facematerial_map_t> put_queue_t;
  159. put_queue_t mPutQueue;
  160. LLCore::HttpRequest::ptr_t mHttpRequest;
  161. LLCore::HttpHeaders::ptr_t mHttpHeaders;
  162. LLCore::HttpOptions::ptr_t mHttpOptions;
  163. LLCore::HttpRequest::policy_t mHttpPolicy;
  164. LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t mHttpAdapter;
  165. };
  166. // std::hash implementation for TEMaterialPair. HB
  167. namespace std
  168. {
  169. template<> struct hash<LLMaterialMgr::TEMaterialPair>
  170. {
  171. LL_INLINE size_t operator()(const LLMaterialMgr::TEMaterialPair& p) const noexcept
  172. {
  173. return (p.mTE + 1) * p.mMaterialId.getDigest64();
  174. }
  175. };
  176. }
  177. // For use with boost::unordered_map and boost::unordered_set. HB
  178. LL_INLINE size_t hash_value(const LLMaterialMgr::TEMaterialPair& p) noexcept
  179. {
  180. return (p.mTE + 1) * p.mMaterialId.getDigest64();
  181. }
  182. // std::hash implementation for RegionMaterialPair. HB
  183. namespace std
  184. {
  185. template<> struct hash<LLMaterialMgr::RegionMaterialPair>
  186. {
  187. LL_INLINE size_t operator()(const LLMaterialMgr::RegionMaterialPair& p) const noexcept
  188. {
  189. return p.mRegionId.getDigest64() ^ p.mMaterialId.getDigest64();
  190. }
  191. };
  192. }
  193. // For use with boost::unordered_map and boost::unordered_set. HB
  194. LL_INLINE size_t hash_value(const LLMaterialMgr::RegionMaterialPair& p) noexcept
  195. {
  196. return p.mRegionId.getDigest64() ^ p.mMaterialId.getDigest64();
  197. }
  198. #endif // LL_LLMATERIALMGR_H