2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "threads/SystemClock.h"
22 #include "GUILargeTextureManager.h"
23 #include "settings/Settings.h"
24 #include "guilib/Texture.h"
25 #include "threads/SingleLock.h"
26 #include "utils/TimeUtils.h"
27 #include "utils/JobManager.h"
28 #include "guilib/GraphicContext.h"
29 #include "utils/log.h"
30 #include "TextureCache.h"
35 CImageLoader::CImageLoader(const CStdString &path, const bool useCache)
39 m_use_cache = useCache;
42 CImageLoader::~CImageLoader()
47 bool CImageLoader::DoWork()
49 bool needsChecking = false;
52 CStdString texturePath = g_TextureManager.GetTexturePath(m_path);
54 loadPath = CTextureCache::Get().CheckCachedImage(texturePath, true, needsChecking);
56 loadPath = texturePath;
58 if (m_use_cache && loadPath.empty())
60 // not in our texture cache, so try and load directly and then cache the result
61 loadPath = CTextureCache::Get().CacheImage(texturePath, &m_texture);
63 return true; // we're done
65 if (!m_use_cache || !loadPath.empty())
67 // direct route - load the image
68 unsigned int start = XbmcThreads::SystemClockMillis();
69 m_texture = CBaseTexture::LoadFromFile(loadPath, g_graphicsContext.GetWidth(), g_graphicsContext.GetHeight(), CSettings::Get().GetBool("pictures.useexifrotation"));
72 if (XbmcThreads::SystemClockMillis() - start > 100)
73 CLog::Log(LOGDEBUG, "%s - took %u ms to load %s", __FUNCTION__, XbmcThreads::SystemClockMillis() - start, loadPath.c_str());
76 CTextureCache::Get().BackgroundCacheImage(texturePath);
81 CGUILargeTextureManager::CLargeTexture::CLargeTexture(const CStdString &path)
88 CGUILargeTextureManager::CLargeTexture::~CLargeTexture()
90 assert(m_refCount == 0);
94 void CGUILargeTextureManager::CLargeTexture::AddRef()
99 bool CGUILargeTextureManager::CLargeTexture::DecrRef(bool deleteImmediately)
105 if (deleteImmediately)
108 m_timeToDelete = CTimeUtils::GetFrameTime() + TIME_TO_DELETE;
114 bool CGUILargeTextureManager::CLargeTexture::DeleteIfRequired(bool deleteImmediately)
116 if (m_refCount == 0 && (deleteImmediately || m_timeToDelete < CTimeUtils::GetFrameTime()))
124 void CGUILargeTextureManager::CLargeTexture::SetTexture(CBaseTexture* texture)
126 assert(!m_texture.size());
128 m_texture.Set(texture, texture->GetWidth(), texture->GetHeight());
131 CGUILargeTextureManager::CGUILargeTextureManager()
135 CGUILargeTextureManager::~CGUILargeTextureManager()
139 void CGUILargeTextureManager::CleanupUnusedImages(bool immediately)
141 CSingleLock lock(m_listSection);
142 // check for items to remove from allocated list, and remove
143 listIterator it = m_allocated.begin();
144 while (it != m_allocated.end())
146 CLargeTexture *image = *it;
147 if (image->DeleteIfRequired(immediately))
148 it = m_allocated.erase(it);
154 // if available, increment reference count, and return the image.
155 // else, add to the queue list if appropriate.
156 bool CGUILargeTextureManager::GetImage(const CStdString &path, CTextureArray &texture, bool firstRequest, const bool useCache)
158 CSingleLock lock(m_listSection);
159 for (listIterator it = m_allocated.begin(); it != m_allocated.end(); ++it)
161 CLargeTexture *image = *it;
162 if (image->GetPath() == path)
166 texture = image->GetTexture();
167 return texture.size() > 0;
172 QueueImage(path, useCache);
177 void CGUILargeTextureManager::ReleaseImage(const CStdString &path, bool immediately)
179 CSingleLock lock(m_listSection);
180 for (listIterator it = m_allocated.begin(); it != m_allocated.end(); ++it)
182 CLargeTexture *image = *it;
183 if (image->GetPath() == path)
185 if (image->DecrRef(immediately) && immediately)
186 m_allocated.erase(it);
190 for (queueIterator it = m_queued.begin(); it != m_queued.end(); ++it)
192 unsigned int id = it->first;
193 CLargeTexture *image = it->second;
194 if (image->GetPath() == path && image->DecrRef(true))
197 CJobManager::GetInstance().CancelJob(id);
204 // queue the image, and start the background loader if necessary
205 void CGUILargeTextureManager::QueueImage(const CStdString &path, bool useCache)
207 CSingleLock lock(m_listSection);
208 for (queueIterator it = m_queued.begin(); it != m_queued.end(); ++it)
210 CLargeTexture *image = it->second;
211 if (image->GetPath() == path)
214 return; // already queued
219 CLargeTexture *image = new CLargeTexture(path);
220 unsigned int jobID = CJobManager::GetInstance().AddJob(new CImageLoader(path, useCache), this, CJob::PRIORITY_NORMAL);
221 m_queued.push_back(make_pair(jobID, image));
224 void CGUILargeTextureManager::OnJobComplete(unsigned int jobID, bool success, CJob *job)
226 // see if we still have this job id
227 CSingleLock lock(m_listSection);
228 for (queueIterator it = m_queued.begin(); it != m_queued.end(); ++it)
230 if (it->first == jobID)
232 CImageLoader *loader = (CImageLoader *)job;
233 CLargeTexture *image = it->second;
234 image->SetTexture(loader->m_texture);
235 loader->m_texture = NULL; // we want to keep the texture, and jobs are auto-deleted.
237 m_allocated.push_back(image);