OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ |
| 6 #define CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <map> |
| 11 #include <queue> |
| 12 #include <string> |
| 13 |
| 14 #include "base/files/file_path.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/threading/thread_checker.h" |
| 18 #include "content/common/content_export.h" |
| 19 #include "net/disk_cache/disk_cache.h" |
| 20 |
| 21 namespace content { |
| 22 |
| 23 class ShaderDiskCacheEntry; |
| 24 class ShaderDiskReadHelper; |
| 25 class ShaderClearHelper; |
| 26 |
| 27 // ShaderDiskCache is the interface to the on disk cache for |
| 28 // GL shaders. |
| 29 class CONTENT_EXPORT ShaderDiskCache |
| 30 : public base::RefCounted<ShaderDiskCache> { |
| 31 public: |
| 32 using ShaderLoadedCallback = |
| 33 base::Callback<void(const std::string&, const std::string&)>; |
| 34 |
| 35 void set_shader_loaded_callback(const ShaderLoadedCallback& callback) { |
| 36 shader_loaded_callback_ = callback; |
| 37 } |
| 38 |
| 39 // Store the |shader| into the cache under |key|. |
| 40 void Cache(const std::string& key, const std::string& shader); |
| 41 |
| 42 // Clear a range of entries. This supports unbounded deletes in either |
| 43 // direction by using null Time values for either |begin_time| or |end_time|. |
| 44 // The return value is a net error code. If this method returns |
| 45 // ERR_IO_PENDING, the |completion_callback| will be invoked when the |
| 46 // operation completes. |
| 47 int Clear( |
| 48 const base::Time begin_time, |
| 49 const base::Time end_time, |
| 50 const net::CompletionCallback& completion_callback); |
| 51 |
| 52 // Sets a callback for when the cache is available. If the cache is |
| 53 // already available the callback will not be called and net::OK is returned. |
| 54 // If the callback is set net::ERR_IO_PENDING is returned and the callback |
| 55 // will be executed when the cache is available. |
| 56 int SetAvailableCallback(const net::CompletionCallback& callback); |
| 57 |
| 58 // Returns the number of elements currently in the cache. |
| 59 int32_t Size(); |
| 60 |
| 61 // Set a callback notification for when all current entries have been |
| 62 // written to the cache. |
| 63 // The return value is a net error code. If this method returns |
| 64 // ERR_IO_PENDING, the |callback| will be invoked when all entries have |
| 65 // been written to the cache. |
| 66 int SetCacheCompleteCallback(const net::CompletionCallback& callback); |
| 67 |
| 68 private: |
| 69 friend class base::RefCounted<ShaderDiskCache>; |
| 70 friend class ShaderDiskCacheEntry; |
| 71 friend class ShaderDiskReadHelper; |
| 72 friend class ShaderCacheFactory; |
| 73 |
| 74 explicit ShaderDiskCache(const base::FilePath& cache_path); |
| 75 ~ShaderDiskCache(); |
| 76 |
| 77 void Init(scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
| 78 void CacheCreatedCallback(int rv); |
| 79 |
| 80 disk_cache::Backend* backend() { return backend_.get(); } |
| 81 |
| 82 void EntryComplete(ShaderDiskCacheEntry* entry); |
| 83 void ReadComplete(); |
| 84 |
| 85 bool cache_available_; |
| 86 base::FilePath cache_path_; |
| 87 bool is_initialized_; |
| 88 net::CompletionCallback available_callback_; |
| 89 net::CompletionCallback cache_complete_callback_; |
| 90 ShaderLoadedCallback shader_loaded_callback_; |
| 91 |
| 92 std::unique_ptr<disk_cache::Backend> backend_; |
| 93 |
| 94 std::unique_ptr<ShaderDiskReadHelper> helper_; |
| 95 std::unordered_map<ShaderDiskCacheEntry*, |
| 96 std::unique_ptr<ShaderDiskCacheEntry>> |
| 97 entries_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCache); |
| 100 }; |
| 101 |
| 102 // ShaderCacheFactory maintains a cache of ShaderDiskCache objects |
| 103 // so we only create one per profile directory. |
| 104 class CONTENT_EXPORT ShaderCacheFactory |
| 105 : NON_EXPORTED_BASE(public base::ThreadChecker) { |
| 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. |
| 117 static ShaderCacheFactory* GetInstance(); |
| 118 |
| 119 // Clear the shader disk cache for the given |path|. This supports unbounded |
| 120 // deletes in either direction by using null Time values for either |
| 121 // |begin_time| or |end_time|. The |callback| will be executed when the |
| 122 // clear is complete. |
| 123 void ClearByPath(const base::FilePath& path, |
| 124 const base::Time& begin_time, |
| 125 const base::Time& end_time, |
| 126 const base::Closure& callback); |
| 127 |
| 128 // Retrieve the shader disk cache for the provided |client_id|. |
| 129 scoped_refptr<ShaderDiskCache> Get(int32_t client_id); |
| 130 |
| 131 // Set the |path| to be used for the disk cache for |client_id|. |
| 132 void SetCacheInfo(int32_t client_id, const base::FilePath& path); |
| 133 |
| 134 // Remove the path mapping for |client_id|. |
| 135 void RemoveCacheInfo(int32_t client_id); |
| 136 |
| 137 // Set the provided |cache| into the cache map for the given |path|. |
| 138 void AddToCache(const base::FilePath& path, ShaderDiskCache* cache); |
| 139 |
| 140 // Remove the provided |path| from our cache map. |
| 141 void RemoveFromCache(const base::FilePath& path); |
| 142 |
| 143 private: |
| 144 friend class ShaderClearHelper; |
| 145 |
| 146 explicit ShaderCacheFactory( |
| 147 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
| 148 ~ShaderCacheFactory(); |
| 149 |
| 150 static void CreateFactoryInstance( |
| 151 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
| 152 |
| 153 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner_; |
| 154 |
| 155 scoped_refptr<ShaderDiskCache> GetByPath(const base::FilePath& path); |
| 156 void CacheCleared(const base::FilePath& path); |
| 157 |
| 158 using ShaderCacheMap = std::map<base::FilePath, ShaderDiskCache*>; |
| 159 ShaderCacheMap shader_cache_map_; |
| 160 |
| 161 using ClientIdToPathMap = std::map<int32_t, base::FilePath>; |
| 162 ClientIdToPathMap client_id_to_path_map_; |
| 163 |
| 164 using ShaderClearQueue = std::queue<std::unique_ptr<ShaderClearHelper>>; |
| 165 using ShaderClearMap = std::map<base::FilePath, ShaderClearQueue>; |
| 166 ShaderClearMap shader_clear_map_; |
| 167 |
| 168 DISALLOW_COPY_AND_ASSIGN(ShaderCacheFactory); |
| 169 }; |
| 170 |
| 171 } // namespace content |
| 172 |
| 173 #endif // CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ |
| 174 |
OLD | NEW |