2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "guilib/TextureManager.h"
12 #include "threads/CriticalSection.h"
13 #include "utils/Job.h"
23 \ingroup textures,jobs
24 \brief Image loader job class
26 Used by the CGUILargeTextureManager to perform asynchronous loading of textures.
28 \sa CGUILargeTextureManager and CJob
30 class CImageLoader
: public CJob
33 CImageLoader(const std::string
&path
, const bool useCache
);
34 ~CImageLoader() override
;
37 \brief Work function that loads in a particular image.
39 bool DoWork() override
;
41 bool m_use_cache
; ///< Whether or not to use any caching with this image
42 std::string m_path
; ///< path of image to load
43 std::unique_ptr
<CTexture
> m_texture
; ///< Texture object to load the image into \sa CTexture.
48 \brief Background texture loading manager
50 Used to load textures for the user interface asynchronously, allowing fluid framerates
51 while background loading textures.
53 \sa IJobCallback, CGUITexture
55 class CGUILargeTextureManager
: public IJobCallback
58 CGUILargeTextureManager();
59 ~CGUILargeTextureManager() override
;
62 \brief Callback from CImageLoader on completion of a loaded image
64 Transfers texture information from the loading job to our allocated texture list.
66 \sa CImageLoader, IJobCallback
68 void OnJobComplete(unsigned int jobID
, bool success
, CJob
*job
) override
;
71 \brief Request a texture to be loaded in the background.
73 Loaded textures are reference counted, hence this call may immediately return with the texture
74 object filled if the texture has been previously loaded, else will return with an empty texture
75 object if it is being loaded.
77 \param path path of the image to load.
78 \param texture texture object to hold the resulting texture
79 \param orientation orientation of resulting texture
80 \param firstRequest true if this is the first time we are requesting this texture
81 \return true if the image exists, else false.
82 \sa CGUITextureArray and CGUITexture
84 bool GetImage(const std::string
&path
, CTextureArray
&texture
, bool firstRequest
, bool useCache
= true);
87 \brief Request a texture to be unloaded.
89 When textures are finished with, this function should be called. This decrements the texture's
90 reference count, and schedules it to be unloaded once the reference count reaches zero. If the
91 texture is still queued for loading, or is in the process of loading, the image load is cancelled.
93 \param path path of the image to release.
94 \param immediately if set true the image is immediately unloaded once its reference count reaches zero
95 rather than being unloaded after a delay.
97 void ReleaseImage(const std::string
&path
, bool immediately
= false);
100 \brief Cleanup images that are no longer in use.
102 Loaded textures are reference counted, and upon reaching reference count 0 through ReleaseImage()
103 they are flagged as unused with the current time. After a delay they may be unloaded, hence
104 CleanupUnusedImages() should be called periodically to ensure this occurs.
106 \param immediately set to true to cleanup images regardless of whether the delay has passed
108 void CleanupUnusedImages(bool immediately
= false);
114 explicit CLargeTexture(const std::string
&path
);
115 virtual ~CLargeTexture();
118 bool DecrRef(bool deleteImmediately
);
119 bool DeleteIfRequired(bool deleteImmediately
= false);
120 void SetTexture(std::unique_ptr
<CTexture
> texture
);
122 const std::string
& GetPath() const { return m_path
; }
123 const CTextureArray
& GetTexture() const { return m_texture
; }
126 static const unsigned int TIME_TO_DELETE
= 2000;
128 unsigned int m_refCount
;
130 CTextureArray m_texture
;
131 unsigned int m_timeToDelete
;
134 void QueueImage(const std::string
&path
, bool useCache
= true);
136 std::vector
< std::pair
<unsigned int, CLargeTexture
*> > m_queued
;
137 std::vector
<CLargeTexture
*> m_allocated
;
138 typedef std::vector
<CLargeTexture
*>::iterator listIterator
;
139 typedef std::vector
< std::pair
<unsigned int, CLargeTexture
*> >::iterator queueIterator
;
141 CCriticalSection m_listSection
;