| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ | 5 #ifndef CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ |
| 6 #define CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ | 6 #define CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <queue> | 11 #include <queue> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/singleton.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
| 19 #include "net/disk_cache/disk_cache.h" | 19 #include "net/disk_cache/disk_cache.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 class ShaderDiskCacheEntry; | 23 class ShaderDiskCacheEntry; |
| 24 class ShaderDiskReadHelper; | 24 class ShaderDiskReadHelper; |
| 25 class ShaderClearHelper; | 25 class ShaderClearHelper; |
| 26 | 26 |
| 27 // ShaderDiskCache is the interface to the on disk cache for | 27 // ShaderDiskCache is the interface to the on disk cache for |
| 28 // GL shaders. | 28 // GL shaders. |
| 29 // | |
| 30 // While this class is both RefCounted and SupportsWeakPtr | |
| 31 // when using this class you should work with the RefCounting. | |
| 32 // The WeakPtr is needed interally. | |
| 33 class CONTENT_EXPORT ShaderDiskCache | 29 class CONTENT_EXPORT ShaderDiskCache |
| 34 : public base::RefCounted<ShaderDiskCache>, | 30 : public base::RefCounted<ShaderDiskCache> { |
| 35 public base::SupportsWeakPtr<ShaderDiskCache> { | |
| 36 public: | 31 public: |
| 37 using ShaderLoadedCallback = | 32 using ShaderLoadedCallback = |
| 38 base::Callback<void(const std::string&, const std::string&)>; | 33 base::Callback<void(const std::string&, const std::string&)>; |
| 39 void Init(); | |
| 40 | 34 |
| 41 void set_shader_loaded_callback(const ShaderLoadedCallback& callback) { | 35 void set_shader_loaded_callback(const ShaderLoadedCallback& callback) { |
| 42 shader_loaded_callback_ = callback; | 36 shader_loaded_callback_ = callback; |
| 43 } | 37 } |
| 44 | 38 |
| 45 // Store the |shader| into the cache under |key|. | 39 // Store the |shader| into the cache under |key|. |
| 46 void Cache(const std::string& key, const std::string& shader); | 40 void Cache(const std::string& key, const std::string& shader); |
| 47 | 41 |
| 48 // Clear a range of entries. This supports unbounded deletes in either | 42 // Clear a range of entries. This supports unbounded deletes in either |
| 49 // direction by using null Time values for either |begin_time| or |end_time|. | 43 // direction by using null Time values for either |begin_time| or |end_time|. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 73 | 67 |
| 74 private: | 68 private: |
| 75 friend class base::RefCounted<ShaderDiskCache>; | 69 friend class base::RefCounted<ShaderDiskCache>; |
| 76 friend class ShaderDiskCacheEntry; | 70 friend class ShaderDiskCacheEntry; |
| 77 friend class ShaderDiskReadHelper; | 71 friend class ShaderDiskReadHelper; |
| 78 friend class ShaderCacheFactory; | 72 friend class ShaderCacheFactory; |
| 79 | 73 |
| 80 explicit ShaderDiskCache(const base::FilePath& cache_path); | 74 explicit ShaderDiskCache(const base::FilePath& cache_path); |
| 81 ~ShaderDiskCache(); | 75 ~ShaderDiskCache(); |
| 82 | 76 |
| 77 void Init(scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
| 83 void CacheCreatedCallback(int rv); | 78 void CacheCreatedCallback(int rv); |
| 84 | 79 |
| 85 disk_cache::Backend* backend() { return backend_.get(); } | 80 disk_cache::Backend* backend() { return backend_.get(); } |
| 86 | 81 |
| 87 void EntryComplete(void* entry); | 82 void EntryComplete(ShaderDiskCacheEntry* entry); |
| 88 void ReadComplete(); | 83 void ReadComplete(); |
| 89 | 84 |
| 90 bool cache_available_; | 85 bool cache_available_; |
| 91 base::FilePath cache_path_; | 86 base::FilePath cache_path_; |
| 92 bool is_initialized_; | 87 bool is_initialized_; |
| 93 net::CompletionCallback available_callback_; | 88 net::CompletionCallback available_callback_; |
| 94 net::CompletionCallback cache_complete_callback_; | 89 net::CompletionCallback cache_complete_callback_; |
| 95 ShaderLoadedCallback shader_loaded_callback_; | 90 ShaderLoadedCallback shader_loaded_callback_; |
| 96 | 91 |
| 97 std::unique_ptr<disk_cache::Backend> backend_; | 92 std::unique_ptr<disk_cache::Backend> backend_; |
| 98 | 93 |
| 99 scoped_refptr<ShaderDiskReadHelper> helper_; | 94 std::unique_ptr<ShaderDiskReadHelper> helper_; |
| 100 std::map<void*, scoped_refptr<ShaderDiskCacheEntry> > entry_map_; | 95 std::unordered_map<ShaderDiskCacheEntry*, |
| 96 std::unique_ptr<ShaderDiskCacheEntry>> |
| 97 entries_; |
| 101 | 98 |
| 102 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCache); | 99 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCache); |
| 103 }; | 100 }; |
| 104 | 101 |
| 105 // ShaderCacheFactory maintains a cache of ShaderDiskCache objects | 102 // ShaderCacheFactory maintains a cache of ShaderDiskCache objects |
| 106 // so we only create one per profile directory. | 103 // so we only create one per profile directory. |
| 107 class CONTENT_EXPORT ShaderCacheFactory { | 104 class CONTENT_EXPORT ShaderCacheFactory |
| 105 : NON_EXPORTED_BASE(public base::ThreadChecker) { |
| 108 public: | 106 public: |
| 107 // Initializes the ShaderCacheFactory singleton instance. The singleton |
| 108 // instance is created and used in the thread associated with |task_runner|. |
| 109 // |cache_task_runner| is associated with the thread responsible for managing |
| 110 // the disk cache. |
| 111 static void InitInstance( |
| 112 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 113 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
| 114 |
| 115 // Returns an instance previously created by InitInstance(). This can return |
| 116 // nullptr if an instance has not yet been created. |
| 109 static ShaderCacheFactory* GetInstance(); | 117 static ShaderCacheFactory* GetInstance(); |
| 110 | 118 |
| 111 // Clear the shader disk cache for the given |path|. This supports unbounded | 119 // Clear the shader disk cache for the given |path|. This supports unbounded |
| 112 // deletes in either direction by using null Time values for either | 120 // deletes in either direction by using null Time values for either |
| 113 // |begin_time| or |end_time|. The |callback| will be executed when the | 121 // |begin_time| or |end_time|. The |callback| will be executed when the |
| 114 // clear is complete. | 122 // clear is complete. |
| 115 void ClearByPath(const base::FilePath& path, | 123 void ClearByPath(const base::FilePath& path, |
| 116 const base::Time& begin_time, | 124 const base::Time& begin_time, |
| 117 const base::Time& end_time, | 125 const base::Time& end_time, |
| 118 const base::Closure& callback); | 126 const base::Closure& callback); |
| 119 | 127 |
| 120 // Retrieve the shader disk cache for the provided |client_id|. | 128 // Retrieve the shader disk cache for the provided |client_id|. |
| 121 scoped_refptr<ShaderDiskCache> Get(int32_t client_id); | 129 scoped_refptr<ShaderDiskCache> Get(int32_t client_id); |
| 122 | 130 |
| 123 // Set the |path| to be used for the disk cache for |client_id|. | 131 // Set the |path| to be used for the disk cache for |client_id|. |
| 124 void SetCacheInfo(int32_t client_id, const base::FilePath& path); | 132 void SetCacheInfo(int32_t client_id, const base::FilePath& path); |
| 125 | 133 |
| 126 // Remove the path mapping for |client_id|. | 134 // Remove the path mapping for |client_id|. |
| 127 void RemoveCacheInfo(int32_t client_id); | 135 void RemoveCacheInfo(int32_t client_id); |
| 128 | 136 |
| 129 // Set the provided |cache| into the cache map for the given |path|. | 137 // Set the provided |cache| into the cache map for the given |path|. |
| 130 void AddToCache(const base::FilePath& path, ShaderDiskCache* cache); | 138 void AddToCache(const base::FilePath& path, ShaderDiskCache* cache); |
| 131 | 139 |
| 132 // Remove the provided |path| from our cache map. | 140 // Remove the provided |path| from our cache map. |
| 133 void RemoveFromCache(const base::FilePath& path); | 141 void RemoveFromCache(const base::FilePath& path); |
| 134 | 142 |
| 135 private: | 143 private: |
| 136 friend struct base::DefaultSingletonTraits<ShaderCacheFactory>; | |
| 137 friend class ShaderClearHelper; | 144 friend class ShaderClearHelper; |
| 138 | 145 |
| 139 ShaderCacheFactory(); | 146 explicit ShaderCacheFactory( |
| 147 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
| 140 ~ShaderCacheFactory(); | 148 ~ShaderCacheFactory(); |
| 141 | 149 |
| 150 static void CreateFactoryInstance( |
| 151 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
| 152 |
| 153 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner_; |
| 154 |
| 142 scoped_refptr<ShaderDiskCache> GetByPath(const base::FilePath& path); | 155 scoped_refptr<ShaderDiskCache> GetByPath(const base::FilePath& path); |
| 143 void CacheCleared(const base::FilePath& path); | 156 void CacheCleared(const base::FilePath& path); |
| 144 | 157 |
| 145 typedef std::map<base::FilePath, ShaderDiskCache*> ShaderCacheMap; | 158 typedef std::map<base::FilePath, ShaderDiskCache*> ShaderCacheMap; |
| 146 ShaderCacheMap shader_cache_map_; | 159 ShaderCacheMap shader_cache_map_; |
| 147 | 160 |
| 148 typedef std::map<int32_t, base::FilePath> ClientIdToPathMap; | 161 typedef std::map<int32_t, base::FilePath> ClientIdToPathMap; |
| 149 ClientIdToPathMap client_id_to_path_map_; | 162 ClientIdToPathMap client_id_to_path_map_; |
| 150 | 163 |
| 151 typedef std::queue<scoped_refptr<ShaderClearHelper> > ShaderClearQueue; | 164 typedef std::queue<scoped_refptr<ShaderClearHelper> > ShaderClearQueue; |
| 152 typedef std::map<base::FilePath, ShaderClearQueue> ShaderClearMap; | 165 typedef std::map<base::FilePath, ShaderClearQueue> ShaderClearMap; |
| 153 ShaderClearMap shader_clear_map_; | 166 ShaderClearMap shader_clear_map_; |
| 154 | 167 |
| 155 DISALLOW_COPY_AND_ASSIGN(ShaderCacheFactory); | 168 DISALLOW_COPY_AND_ASSIGN(ShaderCacheFactory); |
| 156 }; | 169 }; |
| 157 | 170 |
| 158 } // namespace content | 171 } // namespace content |
| 159 | 172 |
| 160 #endif // CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ | 173 #endif // CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ |
| 161 | 174 |
| OLD | NEW |