llimage.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /**
  2. * @file llimage.h
  3. * @brief Object for managing images and their textures.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, 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. #ifndef LL_LLIMAGE_H
  33. #define LL_LLIMAGE_H
  34. #if LL_JEMALLOC
  35. # include "jemalloc/jemalloc.h"
  36. #endif
  37. #include "llatomic.h"
  38. #include "llmemory.h"
  39. #include "llpointer.h"
  40. #include "llthread.h"
  41. #include "hbtracy.h"
  42. // To reduce the sources verbosity and typing, you may use this macro in the
  43. // public methods declaration block for classes dealing with images. HB
  44. #define LL_IMAGE_AREANA_NEW_DELETE \
  45. LL_INLINE void* operator new(size_t size) \
  46. { return allocate_texture_mem(size); } \
  47. LL_INLINE void* operator new[](size_t size) \
  48. { return allocate_texture_mem(size); } \
  49. LL_INLINE void operator delete(void* ptr) noexcept \
  50. { free_texture_mem(ptr); } \
  51. LL_INLINE void operator delete[](void* ptr) noexcept \
  52. { free_texture_mem(ptr); }
  53. // Forward declarations
  54. LL_INLINE void* allocate_texture_mem(size_t size);
  55. LL_INLINE void free_texture_mem(void* addr) noexcept;
  56. constexpr S32 MIN_IMAGE_MIP = 2; // 4x4, only used for expand/contract power of 2
  57. constexpr S32 MAX_IMAGE_MIP = 12; // 4096x4096
  58. constexpr S32 MAX_DISCARD_LEVEL = 5;
  59. // MIN_IMAGE_SIZE = 4: only used for expand/contract power of 2:
  60. constexpr S32 MIN_IMAGE_SIZE = 1 << MIN_IMAGE_MIP;
  61. constexpr S32 MAX_IMAGE_SIZE = 1 << MAX_IMAGE_MIP; // 4096
  62. constexpr S32 MIN_IMAGE_AREA = MIN_IMAGE_SIZE * MIN_IMAGE_SIZE;
  63. constexpr S32 MAX_IMAGE_AREA = MAX_IMAGE_SIZE * MAX_IMAGE_SIZE;
  64. constexpr S32 MAX_IMAGE_COMPONENTS = 8;
  65. constexpr S32 MAX_IMAGE_DATA_SIZE = MAX_IMAGE_AREA * MAX_IMAGE_COMPONENTS;
  66. // Note: these CANNOT be changed without modifying simulator code
  67. // *TODO: change both to 1024 when SIM texture fetching is deprecated
  68. constexpr S32 FIRST_PACKET_SIZE = 600;
  69. constexpr S32 MAX_IMG_PACKET_SIZE = 1000;
  70. // Base classes for images. There are two major parts for the image: the
  71. // compressed representation and the decompressed representation.
  72. class LLImageFormatted;
  73. class LLImageRaw;
  74. class LLColor3;
  75. class LLColor4U;
  76. typedef enum e_image_codec
  77. {
  78. IMG_CODEC_INVALID = 0,
  79. IMG_CODEC_RGB = 1,
  80. IMG_CODEC_J2C = 2,
  81. IMG_CODEC_BMP = 3,
  82. IMG_CODEC_TGA = 4,
  83. IMG_CODEC_JPEG = 5,
  84. IMG_CODEC_PNG = 6,
  85. IMG_CODEC_EOF = 7
  86. } EImageCodec;
  87. //============================================================================
  88. // Library initialization class
  89. class LLImage
  90. {
  91. protected:
  92. LOG_CLASS(LLImage);
  93. public:
  94. static void initClass();
  95. static void cleanupClass();
  96. static void dumpStats();
  97. static const std::string& getLastError();
  98. static void setLastError(const std::string& message);
  99. #if LL_JEMALLOC
  100. LL_INLINE static U32 getMallocxFlags() { return sMallocxFlags; }
  101. #endif
  102. protected:
  103. static LLMutex* sErrorMutex;
  104. static std::string sLastErrorMessage;
  105. public:
  106. static U8* sTempDataBuffer;
  107. static U32 sTempDataBufferUsageCount;
  108. static U32 sDynamicBufferAllocationsCount;
  109. static S32 sMaxMainThreadTempBufferSizeRequest;
  110. #if LL_JEMALLOC
  111. private:
  112. static U32 sMallocxFlags;
  113. #endif
  114. };
  115. //============================================================================
  116. // Image base class
  117. class LLImageBase : public LLThreadSafeRefCount
  118. {
  119. protected:
  120. LOG_CLASS(LLImageBase);
  121. virtual ~LLImageBase();
  122. public:
  123. #if LL_JEMALLOC
  124. // Take benefit of jemalloc arenas for images, especially since they are
  125. // used in various threads. HB
  126. LL_IMAGE_AREANA_NEW_DELETE
  127. #endif
  128. LLImageBase();
  129. enum
  130. {
  131. TYPE_NORMAL = 0,
  132. TYPE_AVATAR_BAKE = 1,
  133. };
  134. void deleteData();
  135. U8* allocateData(S32 size = -1);
  136. U8* reallocateData(S32 size = -1);
  137. virtual void dump();
  138. U16 getWidth() const { return mWidth; }
  139. U16 getHeight() const { return mHeight; }
  140. S8 getComponents() const { return mComponents; }
  141. S32 getDataSize() const { return mDataSize; }
  142. const U8* getData() const;
  143. U8* getData();
  144. bool isBufferInvalid();
  145. void setSize(S32 width, S32 height, S32 ncomponents);
  146. // setSize() + allocateData()
  147. U8* allocateDataSize(S32 width, S32 height, S32 ncomponents,
  148. S32 size = -1);
  149. protected:
  150. // Special accessor to allow direct setting of mData and mDataSize by
  151. // LLImageFormatted
  152. void setDataAndSize(U8* data, S32 size);
  153. public:
  154. static void generateMip(const U8* indata, U8* mipdata,
  155. int width, int height, S32 nchannels);
  156. // Function for calculating the download priority for textures
  157. // <= 0 priority means that there's no need for more data.
  158. static F32 calc_download_priority(F32 virtual_size, F32 visible_area,
  159. S32 bytes_sent);
  160. static void setSizeOverride(bool enabled) { sSizeOverride = enabled; }
  161. static EImageCodec getCodecFromExtension(const std::string& exten);
  162. private:
  163. U8* mData;
  164. S32 mDataSize;
  165. U16 mWidth;
  166. U16 mHeight;
  167. S8 mComponents;
  168. bool mBadBufferAllocation;
  169. public:
  170. static bool sSizeOverride;
  171. };
  172. // Raw representation of an image used for textures, and other uncompressed
  173. // formats
  174. class LLImageRaw : public LLImageBase
  175. {
  176. protected:
  177. LOG_CLASS(LLImageRaw);
  178. ~LLImageRaw() override;
  179. public:
  180. LLImageRaw();
  181. LLImageRaw(U16 width, U16 height, S8 components);
  182. LLImageRaw(U8* data, U16 width, U16 height, S8 components,
  183. bool no_copy = false);
  184. LLImageRaw(const U8* data, U16 width, U16 height, S8 components);
  185. // Construct using createFromFile (used by tools)
  186. LLImageRaw(const std::string& filename, bool j2c_lowest_mip_only = false);
  187. // Use in conjunction with "no_copy" constructor to release data pointer
  188. // before deleting so that deletion of this LLImageRaw will not free the
  189. // memory at the "data" parameter provided to "no_copy" constructor.
  190. void releaseData();
  191. bool resize(U16 width, U16 height, S8 components);
  192. U8* getSubImage(U32 x_pos, U32 y_pos, U32 width, U32 height) const;
  193. bool setSubImage(U32 x_pos, U32 y_pos, U32 width, U32 height,
  194. const U8* data, U32 stride = 0, bool reverse_y = false);
  195. void clear(U8 r = 0, U8 g = 0, U8 b = 0, U8 a = 255);
  196. void verticalFlip();
  197. // Returns true if the image is not fully opaque
  198. bool checkHasTransparentPixels();
  199. // When all pixels are opaque, deletes the alpha channel and returns true,
  200. // or does nothing and returns false otherwise.
  201. bool optimizeAwayAlpha();
  202. // Creates an alpha channel if this image does not have one.
  203. bool makeAlpha();
  204. void expandToPowerOfTwo(S32 max_dim = MAX_IMAGE_SIZE,
  205. bool scale_image = true);
  206. void contractToPowerOfTwo(S32 max_dim = MAX_IMAGE_SIZE,
  207. bool scale_image = true);
  208. void biasedScaleToPowerOfTwo(S32 max_dim = MAX_IMAGE_SIZE);
  209. bool scale(S32 new_width, S32 new_height, bool scale_image = true);
  210. LLPointer<LLImageRaw> scaled(S32 new_width, S32 new_height);
  211. // Fill the buffer with a constant color
  212. void fill(const LLColor4U& color);
  213. // Multiply this raw image by the given color
  214. void tint(const LLColor3& color);
  215. // Copy operations
  216. // Duplicate this raw image if refCount > 1.
  217. LLPointer<LLImageRaw> duplicate();
  218. // Src and dst can be any size. Src and dst can each have 3 or 4
  219. // components.
  220. void copy(LLImageRaw* src);
  221. // Src and dst are same size. Src and dst have same number of components.
  222. void copyUnscaled(LLImageRaw* src);
  223. // Src and dst are same size. Src has 4 components. Dst has 3 components.
  224. void copyUnscaled4onto3(LLImageRaw* src);
  225. // Src and dst are same size. Src has 3 components. Dst has 4 components.
  226. void copyUnscaled3onto4(LLImageRaw* src);
  227. // Src and dst are same size. Src has 1 component. Dst has 4 components.
  228. // Alpha component is set to source alpha mask component.
  229. // RGB components are set to fill color.
  230. void copyUnscaledAlphaMask(LLImageRaw* src, const LLColor4U& fill);
  231. // Src and dst can be any size. Src and dst have same number of components.
  232. void copyScaled(LLImageRaw* src);
  233. // Src and dst can be any size. Src has 3 components. Dst has 4 components.
  234. void copyScaled3onto4(LLImageRaw* src);
  235. // Src and dst can be any size. Src has 4 components. Dst has 3 components.
  236. void copyScaled4onto3(LLImageRaw* src);
  237. // Composite operations
  238. // Src and dst can be any size. Src and dst can each have 3 or 4
  239. // components.
  240. void composite(LLImageRaw* src);
  241. // Src and dst can be any size. Src has 4 components. Dst has 3 components.
  242. void compositeScaled4onto3(LLImageRaw* src);
  243. // Src and dst are same size. Src has 4 components. Dst has 3 components.
  244. void compositeUnscaled4onto3(LLImageRaw* src);
  245. protected:
  246. // Create an image from a local file (generally used in tools)
  247. bool createFromFile(const std::string& filename,
  248. bool j2c_lowest_mip_only = false);
  249. U8* getTempBuffer(S32 size);
  250. void freeTempBuffer(U8* addr);
  251. void copyLineScaled(U8* in, U8* out, S32 in_pixel_len, S32 out_pixel_len,
  252. S32 in_pixel_step, S32 out_pixel_step);
  253. void compositeRowScaled4onto3(U8* in, U8* out, S32 in_pixel_len,
  254. S32 out_pixel_len);
  255. void setDataAndSize(U8* data, S32 width, S32 height, S8 components);
  256. public:
  257. // NOTE: written by several image decode threads, so need to be atomic
  258. static LLAtomicS32 sRawImageCount;
  259. };
  260. // Compressed representation of image.
  261. // Subclass from this class for the different representations (J2C, bmp)
  262. class LLImageFormatted : public LLImageBase
  263. {
  264. protected:
  265. LOG_CLASS(LLImageFormatted);
  266. public:
  267. static LLImageFormatted* createFromType(S8 codec);
  268. static LLImageFormatted* createFromMimeType(const std::string& mimetype);
  269. static LLImageFormatted* createFromExtension(const std::string& instring);
  270. public:
  271. LLImageFormatted(S8 codec);
  272. void dump() override;
  273. // Subclasses must return a prefered file extension (lowercase without a
  274. // leading dot)
  275. virtual std::string getExtension() = 0;
  276. // Returns the maximum size of header; 0 indicates we don't know have a
  277. // header and have to lead the entire file
  278. virtual S32 calcHeaderSize() { return 0; };
  279. // Returns how many bytes to read to load discard_level (including header)
  280. virtual S32 calcDataSize(S32 discard_level);
  281. // Returns the smallest valid discard level based on the number of input
  282. // bytes
  283. virtual S32 calcDiscardLevelBytes(S32 bytes);
  284. // By default getRawDiscardLevel() returns mDiscardLevel, but may be
  285. // overridden (LLImageJ2C)
  286. virtual S8 getRawDiscardLevel() { return mDiscardLevel; }
  287. bool load(const std::string& filename);
  288. bool save(const std::string& filename);
  289. virtual bool updateData() = 0; // Pure virtual
  290. void setData(U8* data, S32 size);
  291. void appendData(U8* data, S32 size);
  292. // Loads first 4 channels.
  293. virtual bool decode(LLImageRaw* raw_image) = 0;
  294. // Subclasses that can handle more than 4 channels should override this
  295. // function.
  296. virtual bool decodeChannels(LLImageRaw* raw_image, S32 first_channel,
  297. S32 max_channel);
  298. virtual bool encode(const LLImageRaw* raw_image) = 0;
  299. S8 getCodec() const;
  300. bool isDecoding() const { return mDecoding != 0; }
  301. bool isDecoded() const { return mDecoded != 0; }
  302. void setDiscardLevel(S8 discard_level) { mDiscardLevel = discard_level; }
  303. S8 getDiscardLevel() const { return mDiscardLevel; }
  304. // setLastError needs to be deferred for J2C images since it may be called
  305. // from a DLL
  306. virtual void resetLastError();
  307. virtual void setLastError(const std::string& message,
  308. const std::string& filename = std::string());
  309. protected:
  310. bool copyData(U8* data, S32 size); // calls updateData()
  311. protected:
  312. S8 mCodec;
  313. S8 mDecoding;
  314. // mDecoded is unused, but changing LLImage layout requires recompiling
  315. // static Mac/Linux libs. 2009-01-30 JC
  316. S8 mDecoded;
  317. S8 mDiscardLevel;
  318. };
  319. // NOTE: since the memory functions below use void* pointers instead of char*
  320. // (because void* is the type used by malloc and jemalloc), strict aliasing is
  321. // not possible on structures allocated with them. Make sure you forbid your
  322. // compiler to optimize with strict aliasing assumption (i.e. for gcc, DO use
  323. // the -fno-strict-aliasing option) !
  324. LL_INLINE void* allocate_texture_mem(size_t size)
  325. {
  326. if (LL_UNLIKELY(size <= 0)) return NULL;
  327. void* addr;
  328. #if LL_JEMALLOC
  329. addr = mallocx(size, LLImage::getMallocxFlags());
  330. #else
  331. addr = malloc(size);
  332. #endif
  333. LL_TRACY_ALLOC(addr, size, trc_mem_image);
  334. if (LL_UNLIKELY(addr == NULL))
  335. {
  336. LLMemory::allocationFailed(size);
  337. }
  338. return addr;
  339. }
  340. LL_INLINE void free_texture_mem(void* addr) noexcept
  341. {
  342. if (LL_UNLIKELY(addr == NULL)) return;
  343. LL_TRACY_FREE(addr, trc_mem_image);
  344. #if LL_JEMALLOC
  345. dallocx(addr, 0);
  346. #else
  347. free(addr);
  348. #endif
  349. }
  350. #endif