4 * Copyright (C) 2005-2013 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
23 #include "threads/CriticalSection.h"
24 #include "utils/Job.h"
25 #include "guilib/TextureManager.h"
28 \ingroup textures,jobs
29 \brief Image loader job class
31 Used by the CGUILargeTextureManager to perform asynchronous loading of textures.
33 \sa CGUILargeTextureManager and CJob
35 class CImageLoader : public CJob
38 CImageLoader(const CStdString &path);
39 virtual ~CImageLoader();
42 \brief Work function that loads in a particular image.
44 virtual bool DoWork();
46 CStdString m_path; ///< path of image to load
47 CBaseTexture *m_texture; ///< Texture object to load the image into \sa CBaseTexture.
52 \brief Background texture loading manager
54 Used to load textures for the user interface asynchronously, allowing fluid framerates
55 while background loading textures.
57 \sa IJobCallback, CGUITexture
59 class CGUILargeTextureManager : public IJobCallback
62 CGUILargeTextureManager();
63 virtual ~CGUILargeTextureManager();
66 \brief Callback from CImageLoader on completion of a loaded image
68 Transfers texture information from the loading job to our allocated texture list.
70 \sa CImageLoader, IJobCallback
72 virtual void OnJobComplete(unsigned int jobID, bool success, CJob *job);
75 \brief Request a texture to be loaded in the background.
77 Loaded textures are reference counted, hence this call may immediately return with the texture
78 object filled if the texture has been previously loaded, else will return with an empty texture
79 object if it is being loaded.
81 \param path path of the image to load.
82 \param texture texture object to hold the resulting texture
83 \param orientation orientation of resulting texture
84 \param firstRequest true if this is the first time we are requesting this texture
85 \return true if the image exists, else false.
86 \sa CGUITextureArray and CGUITexture
88 bool GetImage(const CStdString &path, CTextureArray &texture, bool firstRequest);
91 \brief Request a texture to be unloaded.
93 When textures are finished with, this function should be called. This decrements the texture's
94 reference count, and schedules it to be unloaded once the reference count reaches zero. If the
95 texture is still queued for loading, or is in the process of loading, the image load is cancelled.
97 \param path path of the image to release.
98 \param immediately if set true the image is immediately unloaded once its reference count reaches zero
99 rather than being unloaded after a delay.
101 void ReleaseImage(const CStdString &path, bool immediately = false);
104 \brief Cleanup images that are no longer in use.
106 Loaded textures are reference counted, and upon reaching reference count 0 through ReleaseImage()
107 they are flagged as unused with the current time. After a delay they may be unloaded, hence
108 CleanupUnusedImages() should be called periodically to ensure this occurs.
110 \param immediately set to true to cleanup images regardless of whether the delay has passed
112 void CleanupUnusedImages(bool immediately = false);
118 CLargeTexture(const CStdString &path);
119 virtual ~CLargeTexture();
122 bool DecrRef(bool deleteImmediately);
123 bool DeleteIfRequired(bool deleteImmediately = false);
124 void SetTexture(CBaseTexture* texture);
126 const CStdString &GetPath() const { return m_path; };
127 const CTextureArray &GetTexture() const { return m_texture; };
130 static const unsigned int TIME_TO_DELETE = 2000;
132 unsigned int m_refCount;
134 CTextureArray m_texture;
135 unsigned int m_timeToDelete;
138 void QueueImage(const CStdString &path);
140 std::vector< std::pair<unsigned int, CLargeTexture *> > m_queued;
141 std::vector<CLargeTexture *> m_allocated;
142 typedef std::vector<CLargeTexture *>::iterator listIterator;
143 typedef std::vector< std::pair<unsigned int, CLargeTexture *> >::iterator queueIterator;
145 CCriticalSection m_listSection;
148 extern CGUILargeTextureManager g_largeTextureManager;