Merge pull request #3004 from nikolai-r/master
[vuplus_xbmc] / xbmc / GUILargeTextureManager.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-2013 Team XBMC
5  *      http://xbmc.org
6  *
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)
10  *  any later version.
11  *
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.
16  *
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/>.
20  *
21  */
22
23 #include "threads/CriticalSection.h"
24 #include "utils/Job.h"
25 #include "guilib/TextureManager.h"
26
27 /*!
28  \ingroup textures,jobs
29  \brief Image loader job class
30
31  Used by the CGUILargeTextureManager to perform asynchronous loading of textures.
32
33  \sa CGUILargeTextureManager and CJob
34  */
35 class CImageLoader : public CJob
36 {
37 public:
38   CImageLoader(const CStdString &path);
39   virtual ~CImageLoader();
40
41   /*!
42    \brief Work function that loads in a particular image.
43    */
44   virtual bool DoWork();
45
46   CStdString    m_path; ///< path of image to load
47   CBaseTexture *m_texture; ///< Texture object to load the image into \sa CBaseTexture.
48 };
49
50 /*!
51  \ingroup textures
52  \brief Background texture loading manager
53
54  Used to load textures for the user interface asynchronously, allowing fluid framerates
55  while background loading textures.
56
57  \sa IJobCallback, CGUITexture
58  */
59 class CGUILargeTextureManager : public IJobCallback
60 {
61 public:
62   CGUILargeTextureManager();
63   virtual ~CGUILargeTextureManager();
64
65   /*!
66    \brief Callback from CImageLoader on completion of a loaded image
67
68    Transfers texture information from the loading job to our allocated texture list.
69
70    \sa CImageLoader, IJobCallback
71    */
72   virtual void OnJobComplete(unsigned int jobID, bool success, CJob *job);
73
74   /*!
75    \brief Request a texture to be loaded in the background.
76
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.
80
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
87    */
88   bool GetImage(const CStdString &path, CTextureArray &texture, bool firstRequest);
89
90   /*!
91    \brief Request a texture to be unloaded.
92
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.
96
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.
100    */
101   void ReleaseImage(const CStdString &path, bool immediately = false);
102
103   /*!
104    \brief Cleanup images that are no longer in use.
105
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.
109
110    \param immediately set to true to cleanup images regardless of whether the delay has passed
111    */
112   void CleanupUnusedImages(bool immediately = false);
113
114 private:
115   class CLargeTexture
116   {
117   public:
118     CLargeTexture(const CStdString &path);
119     virtual ~CLargeTexture();
120
121     void AddRef();
122     bool DecrRef(bool deleteImmediately);
123     bool DeleteIfRequired(bool deleteImmediately = false);
124     void SetTexture(CBaseTexture* texture);
125
126     const CStdString &GetPath() const { return m_path; };
127     const CTextureArray &GetTexture() const { return m_texture; };
128
129   private:
130     static const unsigned int TIME_TO_DELETE = 2000;
131
132     unsigned int m_refCount;
133     CStdString m_path;
134     CTextureArray m_texture;
135     unsigned int m_timeToDelete;
136   };
137
138   void QueueImage(const CStdString &path);
139
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;
144
145   CCriticalSection m_listSection;
146 };
147
148 extern CGUILargeTextureManager g_largeTextureManager;
149
150