/xbmc/TextureCache.h

http://github.com/xbmc/xbmc · C Header · 219 lines · 51 code · 29 blank · 139 comment · 0 complexity · c3440ddb03cc630dabd85ac4798d00b5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2018 Team Kodi
  3. * This file is part of Kodi - https://kodi.tv
  4. *
  5. * SPDX-License-Identifier: GPL-2.0-or-later
  6. * See LICENSES/README.md for more information.
  7. */
  8. #pragma once
  9. #include "TextureDatabase.h"
  10. #include "threads/Event.h"
  11. #include "utils/JobManager.h"
  12. #include <set>
  13. #include <string>
  14. #include <vector>
  15. class CURL;
  16. class CBaseTexture;
  17. /*!
  18. \ingroup textures
  19. \brief Texture cache class for handling the caching of images.
  20. Manages the caching of images for use as control textures. Images are cached
  21. both as originals (direct copies) and as .dds textures for fast loading. Images
  22. may be periodically checked for updates and may be purged from the cache if
  23. unused for a set period of time.
  24. */
  25. class CTextureCache : public CJobQueue
  26. {
  27. public:
  28. /*!
  29. \brief The only way through which the global instance of the CTextureCache should be accessed.
  30. \return the global instance.
  31. */
  32. static CTextureCache &GetInstance();
  33. /*! \brief Initialize the texture cache
  34. */
  35. void Initialize();
  36. /*! \brief Deinitialize the texture cache
  37. */
  38. void Deinitialize();
  39. /*! \brief Check whether we already have this image cached
  40. Check and return URL to cached image if it exists; If not, return empty string.
  41. If the image is cached, return URL (for original image or .dds version if requested)
  42. \param image url of the image to check
  43. \param needsRecaching [out] whether the image needs recaching.
  44. \return cached url of this image
  45. \sa GetCachedImage
  46. */
  47. std::string CheckCachedImage(const std::string &image, bool &needsRecaching);
  48. /*! \brief Cache image (if required) using a background job
  49. Checks firstly whether an image is already cached, and return URL if so [see CheckCacheImage]
  50. If the image is not yet in the database, a background job is started to
  51. cache the image and add to the database [see CTextureCacheJob]
  52. \param image url of the image to cache
  53. \sa CacheImage
  54. */
  55. void BackgroundCacheImage(const std::string &image);
  56. /*! \brief Cache an image to image cache, optionally return the texture
  57. Caches the given image, returning the texture if the caller wants it.
  58. \param image url of the image to cache
  59. \param texture [out] the loaded image
  60. \param details [out] details of the cached image
  61. \return cached url of this image
  62. \sa CTextureCacheJob::CacheTexture
  63. */
  64. std::string CacheImage(const std::string &image, CBaseTexture **texture = NULL, CTextureDetails *details = NULL);
  65. /*! \brief Cache an image to image cache if not already cached, returning the image details.
  66. \param image url of the image to cache.
  67. \param details [out] the image details.
  68. \return true if the image is in the cache, false otherwise.
  69. \sa CTextureCacheJob::CacheTexture
  70. */
  71. bool CacheImage(const std::string &image, CTextureDetails &details);
  72. /*! \brief Check whether an image is in the cache
  73. Note: If the image url won't normally be cached (eg a skin image) this function will return false.
  74. \param image url of the image
  75. \return true if the image is cached, false otherwise
  76. \sa ClearCachedImage
  77. */
  78. bool HasCachedImage(const std::string &image);
  79. /*! \brief clear the cached version of the given image
  80. \param image url of the image
  81. \sa GetCachedImage
  82. */
  83. void ClearCachedImage(const std::string &image, bool deleteSource = false);
  84. /*! \brief clear the cached version of the image with given id
  85. \param database id of the image
  86. \sa GetCachedImage
  87. */
  88. bool ClearCachedImage(int textureID);
  89. /*! \brief retrieve a cache file (relative to the cache path) to associate with the given image, excluding extension
  90. Use GetCachedPath(GetCacheFile(url)+extension) for the full path to the file.
  91. \param url location of the image
  92. \return a "unique" filename for the associated cache file, excluding extension
  93. */
  94. static std::string GetCacheFile(const std::string &url);
  95. /*! \brief retrieve the full path of the given cached file
  96. \param file name of the file
  97. \return full path of the cached file
  98. */
  99. static std::string GetCachedPath(const std::string &file);
  100. /*! \brief check whether an image:// URL may be cached
  101. \param url the URL to the image
  102. \return true if the given URL may be cached, false otherwise
  103. */
  104. static bool CanCacheImageURL(const CURL &url);
  105. /*! \brief Add this image to the database
  106. Thread-safe wrapper of CTextureDatabase::AddCachedTexture
  107. \param image url of the original image
  108. \param details the texture details to add
  109. \return true if we successfully added to the database, false otherwise.
  110. */
  111. bool AddCachedTexture(const std::string &image, const CTextureDetails &details);
  112. /*! \brief Export a (possibly) cached image to a file
  113. \param image url of the original image
  114. \param destination url of the destination image, excluding extension.
  115. \param overwrite whether to overwrite the destination if it exists (TODO: Defaults to false)
  116. \return true if we successfully exported the file, false otherwise.
  117. */
  118. bool Export(const std::string &image, const std::string &destination, bool overwrite);
  119. bool Export(const std::string &image, const std::string &destination); //! @todo BACKWARD COMPATIBILITY FOR MUSIC THUMBS
  120. private:
  121. // private construction, and no assignments; use the provided singleton methods
  122. CTextureCache();
  123. CTextureCache(const CTextureCache&) = delete;
  124. CTextureCache const& operator=(CTextureCache const&) = delete;
  125. ~CTextureCache() override;
  126. /*! \brief Check if the given image is a cached image
  127. \param image url of the image
  128. \return true if this is a cached image, false otherwise.
  129. */
  130. bool IsCachedImage(const std::string &image) const;
  131. /*! \brief retrieve the cached version of the given image (if it exists)
  132. \param image url of the image
  133. \param details [out] the details of the texture.
  134. \param trackUsage whether this call should track usage of the image (defaults to false)
  135. \return cached url of this image, empty if none exists
  136. \sa ClearCachedImage, CTextureDetails
  137. */
  138. std::string GetCachedImage(const std::string &image, CTextureDetails &details, bool trackUsage = false);
  139. /*! \brief Get an image from the database
  140. Thread-safe wrapper of CTextureDatabase::GetCachedTexture
  141. \param image url of the original image
  142. \param details [out] texture details from the database (if available)
  143. \return true if we have a cached version of this image, false otherwise.
  144. */
  145. bool GetCachedTexture(const std::string &url, CTextureDetails &details);
  146. /*! \brief Clear an image from the database
  147. Thread-safe wrapper of CTextureDatabase::ClearCachedTexture
  148. \param image url of the original image
  149. \param cacheFile [out] url of the cached original (if available)
  150. \return true if we had a cached version of this image, false otherwise.
  151. */
  152. bool ClearCachedTexture(const std::string &url, std::string &cacheFile);
  153. bool ClearCachedTexture(int textureID, std::string &cacheFile);
  154. /*! \brief Increment the use count of a texture
  155. Stores locally before calling CTextureDatabase::IncrementUseCount via a CUseCountJob
  156. \sa CUseCountJob, CTextureDatabase::IncrementUseCount
  157. */
  158. void IncrementUseCount(const CTextureDetails &details);
  159. /*! \brief Set a previously cached texture as valid in the database
  160. Thread-safe wrapper of CTextureDatabase::SetCachedTextureValid
  161. \param image url of the original image
  162. \param updateable whether this image should be checked for updates
  163. \return true if successful, false otherwise.
  164. */
  165. bool SetCachedTextureValid(const std::string &url, bool updateable);
  166. void OnJobComplete(unsigned int jobID, bool success, CJob *job) override;
  167. void OnJobProgress(unsigned int jobID, unsigned int progress, unsigned int total, const CJob *job) override;
  168. /*! \brief Called when a caching job has completed.
  169. Removes the job from our processing list, updates the database
  170. and fires a DDS job if appropriate.
  171. \param success whether the job was successful.
  172. \param job the caching job.
  173. */
  174. void OnCachingComplete(bool success, CTextureCacheJob *job);
  175. CCriticalSection m_databaseSection;
  176. CTextureDatabase m_database;
  177. std::set<std::string> m_processinglist; ///< currently processing list to avoid 2 jobs being processed at once
  178. CCriticalSection m_processingSection;
  179. CEvent m_completeEvent; ///< Set whenever a job has finished
  180. std::vector<CTextureDetails> m_useCounts; ///< Use count tracking
  181. CCriticalSection m_useCountSection;
  182. };