xbmc
TextureCache.h
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 
9 #pragma once
10 
11 #include "TextureCacheJob.h"
12 #include "TextureDatabase.h"
13 #include "threads/CriticalSection.h"
14 #include "threads/Event.h"
15 #include "utils/JobManager.h"
16 
17 #include <memory>
18 #include <set>
19 #include <string>
20 #include <vector>
21 
22 class CJob;
23 class CURL;
24 class CTexture;
25 
36 class CTextureCache : public CJobQueue
37 {
38 public:
39  CTextureCache();
40  ~CTextureCache() override;
41 
44  void Initialize();
45 
48  void Deinitialize();
49 
60  std::string CheckCachedImage(const std::string &image, bool &needsRecaching);
61 
71  void BackgroundCacheImage(const std::string &image);
72 
82  bool StartCacheImage(const std::string& image);
83 
94  std::string CacheImage(const std::string& image,
95  std::unique_ptr<CTexture>* texture = nullptr,
96  CTextureDetails* details = nullptr);
97 
104  bool CacheImage(const std::string &image, CTextureDetails &details);
105 
112  bool HasCachedImage(const std::string &image);
113 
118  void ClearCachedImage(const std::string &image, bool deleteSource = false);
119 
124  bool ClearCachedImage(int textureID);
125 
131  static std::string GetCacheFile(const std::string &url);
132 
137  static std::string GetCachedPath(const std::string &file);
138 
143  static bool CanCacheImageURL(const CURL &url);
144 
151  bool AddCachedTexture(const std::string &image, const CTextureDetails &details);
152 
159  bool Export(const std::string &image, const std::string &destination, bool overwrite);
160  bool Export(const std::string &image, const std::string &destination);
161 private:
162  // private construction, and no assignments; use the provided singleton methods
163  CTextureCache(const CTextureCache&) = delete;
164  CTextureCache const& operator=(CTextureCache const&) = delete;
165 
170  bool IsCachedImage(const std::string &image) const;
171 
179  std::string GetCachedImage(const std::string &image, CTextureDetails &details, bool trackUsage = false);
180 
187  bool GetCachedTexture(const std::string &url, CTextureDetails &details);
188 
195  bool ClearCachedTexture(const std::string &url, std::string &cacheFile);
196  bool ClearCachedTexture(int textureID, std::string &cacheFile);
197 
202  void IncrementUseCount(const CTextureDetails &details);
203 
210  bool SetCachedTextureValid(const std::string &url, bool updateable);
211 
212  void OnJobComplete(unsigned int jobID, bool success, CJob *job) override;
213 
220  void OnCachingComplete(bool success, CTextureCacheJob *job);
221 
222  CCriticalSection m_databaseSection;
223  CTextureDatabase m_database;
224  std::set<std::string> m_processinglist;
225  CCriticalSection m_processingSection;
226  CEvent m_completeEvent;
227  std::vector<CTextureDetails> m_useCounts;
228  CCriticalSection m_useCountSection;
229 };
230 
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Texture cache class for handling the caching of images.
Definition: TextureCache.h:36
Base class for jobs that are executed asynchronously.
Definition: Job.h:109
void BackgroundCacheImage(const std::string &image)
Cache image (if required) using a background job.
Definition: TextureCache.cpp:116
static std::string GetCachedPath(const std::string &file)
retrieve the full path of the given cached file
Definition: TextureCache.cpp:288
void Deinitialize()
Deinitialize the texture cache.
Definition: TextureCache.cpp:47
Simple class for passing texture detail around.
Definition: TextureCacheJob.h:26
Definition: URL.h:21
std::string CheckCachedImage(const std::string &image, bool &needsRecaching)
Check whether we already have this image cached.
Definition: TextureCache.cpp:106
void Initialize()
Initialize the texture cache.
Definition: TextureCache.cpp:40
bool AddCachedTexture(const std::string &image, const CTextureDetails &details)
Add this image to the database Thread-safe wrapper of CTextureDatabase::AddCachedTexture.
Definition: TextureCache.cpp:243
Job class for caching textures.
Definition: TextureCacheJob.h:55
Base texture class, subclasses of which depend on the render spec (DX, GL etc.)
Definition: Texture.h:34
void ClearCachedImage(const std::string &image, bool deleteSource=false)
clear the cached version of the given image
Definition: TextureCache.cpp:207
bool StartCacheImage(const std::string &image)
Updates the in-process list.
Definition: TextureCache.cpp:134
static bool CanCacheImageURL(const CURL &url)
check whether an image:// URL may be cached
Definition: TextureCache.cpp:97
bool HasCachedImage(const std::string &image)
Check whether an image is in the cache Note: If the image url won&#39;t normally be cached (eg a skin ima...
Definition: TextureCache.cpp:72
bool Export(const std::string &image, const std::string &destination, bool overwrite)
Export a (possibly) cached image to a file.
Definition: TextureCache.cpp:322
Definition: TextureDatabase.h:58
static std::string GetCacheFile(const std::string &url)
retrieve a cache file (relative to the cache path) to associate with the given image, excluding extension Use GetCachedPath(GetCacheFile(url)+extension) for the full path to the file.
Definition: TextureCache.cpp:280
std::string CacheImage(const std::string &image, std::unique_ptr< CTexture > *texture=nullptr, CTextureDetails *details=nullptr)
Cache an image to image cache, optionally return the texture.
Definition: TextureCache.cpp:146
Job Queue class to handle a queue of unique jobs to be processed sequentially.
Definition: JobManager.h:63