/xbmc/GUILargeTextureManager.h

http://github.com/xbmc/xbmc · C Header · 139 lines · 51 code · 20 blank · 68 comment · 0 complexity · fd0629b1ab7bc2311d87dc8185e14f0d 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 "guilib/TextureManager.h"
  10. #include "threads/CriticalSection.h"
  11. #include "utils/Job.h"
  12. #include <utility>
  13. #include <vector>
  14. /*!
  15. \ingroup textures,jobs
  16. \brief Image loader job class
  17. Used by the CGUILargeTextureManager to perform asynchronous loading of textures.
  18. \sa CGUILargeTextureManager and CJob
  19. */
  20. class CImageLoader : public CJob
  21. {
  22. public:
  23. CImageLoader(const std::string &path, const bool useCache);
  24. ~CImageLoader() override;
  25. /*!
  26. \brief Work function that loads in a particular image.
  27. */
  28. bool DoWork() override;
  29. bool m_use_cache; ///< Whether or not to use any caching with this image
  30. std::string m_path; ///< path of image to load
  31. CBaseTexture *m_texture; ///< Texture object to load the image into \sa CBaseTexture.
  32. };
  33. /*!
  34. \ingroup textures
  35. \brief Background texture loading manager
  36. Used to load textures for the user interface asynchronously, allowing fluid framerates
  37. while background loading textures.
  38. \sa IJobCallback, CGUITexture
  39. */
  40. class CGUILargeTextureManager : public IJobCallback
  41. {
  42. public:
  43. CGUILargeTextureManager();
  44. ~CGUILargeTextureManager() override;
  45. /*!
  46. \brief Callback from CImageLoader on completion of a loaded image
  47. Transfers texture information from the loading job to our allocated texture list.
  48. \sa CImageLoader, IJobCallback
  49. */
  50. void OnJobComplete(unsigned int jobID, bool success, CJob *job) override;
  51. /*!
  52. \brief Request a texture to be loaded in the background.
  53. Loaded textures are reference counted, hence this call may immediately return with the texture
  54. object filled if the texture has been previously loaded, else will return with an empty texture
  55. object if it is being loaded.
  56. \param path path of the image to load.
  57. \param texture texture object to hold the resulting texture
  58. \param orientation orientation of resulting texture
  59. \param firstRequest true if this is the first time we are requesting this texture
  60. \return true if the image exists, else false.
  61. \sa CGUITextureArray and CGUITexture
  62. */
  63. bool GetImage(const std::string &path, CTextureArray &texture, bool firstRequest, bool useCache = true);
  64. /*!
  65. \brief Request a texture to be unloaded.
  66. When textures are finished with, this function should be called. This decrements the texture's
  67. reference count, and schedules it to be unloaded once the reference count reaches zero. If the
  68. texture is still queued for loading, or is in the process of loading, the image load is cancelled.
  69. \param path path of the image to release.
  70. \param immediately if set true the image is immediately unloaded once its reference count reaches zero
  71. rather than being unloaded after a delay.
  72. */
  73. void ReleaseImage(const std::string &path, bool immediately = false);
  74. /*!
  75. \brief Cleanup images that are no longer in use.
  76. Loaded textures are reference counted, and upon reaching reference count 0 through ReleaseImage()
  77. they are flagged as unused with the current time. After a delay they may be unloaded, hence
  78. CleanupUnusedImages() should be called periodically to ensure this occurs.
  79. \param immediately set to true to cleanup images regardless of whether the delay has passed
  80. */
  81. void CleanupUnusedImages(bool immediately = false);
  82. private:
  83. class CLargeTexture
  84. {
  85. public:
  86. explicit CLargeTexture(const std::string &path);
  87. virtual ~CLargeTexture();
  88. void AddRef();
  89. bool DecrRef(bool deleteImmediately);
  90. bool DeleteIfRequired(bool deleteImmediately = false);
  91. void SetTexture(CBaseTexture* texture);
  92. const std::string &GetPath() const { return m_path; };
  93. const CTextureArray &GetTexture() const { return m_texture; };
  94. private:
  95. static const unsigned int TIME_TO_DELETE = 2000;
  96. unsigned int m_refCount;
  97. std::string m_path;
  98. CTextureArray m_texture;
  99. unsigned int m_timeToDelete;
  100. };
  101. void QueueImage(const std::string &path, bool useCache = true);
  102. std::vector< std::pair<unsigned int, CLargeTexture *> > m_queued;
  103. std::vector<CLargeTexture *> m_allocated;
  104. typedef std::vector<CLargeTexture *>::iterator listIterator;
  105. typedef std::vector< std::pair<unsigned int, CLargeTexture *> >::iterator queueIterator;
  106. CCriticalSection m_listSection;
  107. };