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/threading/thread_checker.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 ShaderCacheFactory; |
23 class ShaderDiskCacheEntry; | 24 class ShaderDiskCacheEntry; |
24 class ShaderDiskReadHelper; | 25 class ShaderDiskReadHelper; |
25 class ShaderClearHelper; | 26 class ShaderClearHelper; |
26 | 27 |
27 // ShaderDiskCache is the interface to the on disk cache for | 28 // ShaderDiskCache is the interface to the on disk cache for |
28 // GL shaders. | 29 // GL shaders. |
29 class CONTENT_EXPORT ShaderDiskCache | 30 class CONTENT_EXPORT ShaderDiskCache |
30 : public base::RefCounted<ShaderDiskCache> { | 31 : public base::RefCounted<ShaderDiskCache> { |
31 public: | 32 public: |
32 using ShaderLoadedCallback = | 33 using ShaderLoadedCallback = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // ERR_IO_PENDING, the |callback| will be invoked when all entries have | 65 // ERR_IO_PENDING, the |callback| will be invoked when all entries have |
65 // been written to the cache. | 66 // been written to the cache. |
66 int SetCacheCompleteCallback(const net::CompletionCallback& callback); | 67 int SetCacheCompleteCallback(const net::CompletionCallback& callback); |
67 | 68 |
68 private: | 69 private: |
69 friend class base::RefCounted<ShaderDiskCache>; | 70 friend class base::RefCounted<ShaderDiskCache>; |
70 friend class ShaderDiskCacheEntry; | 71 friend class ShaderDiskCacheEntry; |
71 friend class ShaderDiskReadHelper; | 72 friend class ShaderDiskReadHelper; |
72 friend class ShaderCacheFactory; | 73 friend class ShaderCacheFactory; |
73 | 74 |
74 explicit ShaderDiskCache(const base::FilePath& cache_path); | 75 ShaderDiskCache(ShaderCacheFactory* factory, |
| 76 const base::FilePath& cache_path); |
75 ~ShaderDiskCache(); | 77 ~ShaderDiskCache(); |
76 | 78 |
77 void Init(scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); | 79 void Init(scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
78 void CacheCreatedCallback(int rv); | 80 void CacheCreatedCallback(int rv); |
79 | 81 |
80 disk_cache::Backend* backend() { return backend_.get(); } | 82 disk_cache::Backend* backend() { return backend_.get(); } |
81 | 83 |
82 void EntryComplete(ShaderDiskCacheEntry* entry); | 84 void EntryComplete(ShaderDiskCacheEntry* entry); |
83 void ReadComplete(); | 85 void ReadComplete(); |
84 | 86 |
| 87 ShaderCacheFactory* factory_; |
85 bool cache_available_; | 88 bool cache_available_; |
86 base::FilePath cache_path_; | 89 base::FilePath cache_path_; |
87 bool is_initialized_; | 90 bool is_initialized_; |
88 net::CompletionCallback available_callback_; | 91 net::CompletionCallback available_callback_; |
89 net::CompletionCallback cache_complete_callback_; | 92 net::CompletionCallback cache_complete_callback_; |
90 ShaderLoadedCallback shader_loaded_callback_; | 93 ShaderLoadedCallback shader_loaded_callback_; |
91 | 94 |
92 std::unique_ptr<disk_cache::Backend> backend_; | 95 std::unique_ptr<disk_cache::Backend> backend_; |
93 | 96 |
94 std::unique_ptr<ShaderDiskReadHelper> helper_; | 97 std::unique_ptr<ShaderDiskReadHelper> helper_; |
95 std::unordered_map<ShaderDiskCacheEntry*, | 98 std::unordered_map<ShaderDiskCacheEntry*, |
96 std::unique_ptr<ShaderDiskCacheEntry>> | 99 std::unique_ptr<ShaderDiskCacheEntry>> |
97 entries_; | 100 entries_; |
98 | 101 |
99 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCache); | 102 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCache); |
100 }; | 103 }; |
101 | 104 |
102 // ShaderCacheFactory maintains a cache of ShaderDiskCache objects | 105 // ShaderCacheFactory maintains a cache of ShaderDiskCache objects |
103 // so we only create one per profile directory. | 106 // so we only create one per profile directory. |
104 class CONTENT_EXPORT ShaderCacheFactory | 107 class CONTENT_EXPORT ShaderCacheFactory |
105 : NON_EXPORTED_BASE(public base::ThreadChecker) { | 108 : NON_EXPORTED_BASE(public base::ThreadChecker) { |
106 public: | 109 public: |
107 // Initializes the ShaderCacheFactory singleton instance. The singleton | 110 explicit ShaderCacheFactory( |
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); | 111 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner); |
114 | 112 ~ShaderCacheFactory(); |
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 | 113 |
119 // Clear the shader disk cache for the given |path|. This supports unbounded | 114 // Clear the shader disk cache for the given |path|. This supports unbounded |
120 // deletes in either direction by using null Time values for either | 115 // deletes in either direction by using null Time values for either |
121 // |begin_time| or |end_time|. The |callback| will be executed when the | 116 // |begin_time| or |end_time|. The |callback| will be executed when the |
122 // clear is complete. | 117 // clear is complete. |
123 void ClearByPath(const base::FilePath& path, | 118 void ClearByPath(const base::FilePath& path, |
124 const base::Time& begin_time, | 119 const base::Time& begin_time, |
125 const base::Time& end_time, | 120 const base::Time& end_time, |
126 const base::Closure& callback); | 121 const base::Closure& callback); |
127 | 122 |
128 // Retrieve the shader disk cache for the provided |client_id|. | 123 // Retrieve the shader disk cache for the provided |client_id|. |
129 scoped_refptr<ShaderDiskCache> Get(int32_t client_id); | 124 scoped_refptr<ShaderDiskCache> Get(int32_t client_id); |
130 | 125 |
131 // Set the |path| to be used for the disk cache for |client_id|. | 126 // Set the |path| to be used for the disk cache for |client_id|. |
132 void SetCacheInfo(int32_t client_id, const base::FilePath& path); | 127 void SetCacheInfo(int32_t client_id, const base::FilePath& path); |
133 | 128 |
134 // Remove the path mapping for |client_id|. | 129 // Remove the path mapping for |client_id|. |
135 void RemoveCacheInfo(int32_t client_id); | 130 void RemoveCacheInfo(int32_t client_id); |
136 | 131 |
137 // Set the provided |cache| into the cache map for the given |path|. | 132 // Set the provided |cache| into the cache map for the given |path|. |
138 void AddToCache(const base::FilePath& path, ShaderDiskCache* cache); | 133 void AddToCache(const base::FilePath& path, ShaderDiskCache* cache); |
139 | 134 |
140 // Remove the provided |path| from our cache map. | 135 // Remove the provided |path| from our cache map. |
141 void RemoveFromCache(const base::FilePath& path); | 136 void RemoveFromCache(const base::FilePath& path); |
142 | 137 |
143 private: | 138 private: |
144 friend class ShaderClearHelper; | 139 friend class ShaderClearHelper; |
145 | 140 |
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_; | 141 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner_; |
154 | 142 |
155 scoped_refptr<ShaderDiskCache> GetByPath(const base::FilePath& path); | 143 scoped_refptr<ShaderDiskCache> GetByPath(const base::FilePath& path); |
156 void CacheCleared(const base::FilePath& path); | 144 void CacheCleared(const base::FilePath& path); |
157 | 145 |
158 using ShaderCacheMap = std::map<base::FilePath, ShaderDiskCache*>; | 146 using ShaderCacheMap = std::map<base::FilePath, ShaderDiskCache*>; |
159 ShaderCacheMap shader_cache_map_; | 147 ShaderCacheMap shader_cache_map_; |
160 | 148 |
161 using ClientIdToPathMap = std::map<int32_t, base::FilePath>; | 149 using ClientIdToPathMap = std::map<int32_t, base::FilePath>; |
162 ClientIdToPathMap client_id_to_path_map_; | 150 ClientIdToPathMap client_id_to_path_map_; |
163 | 151 |
164 using ShaderClearQueue = std::queue<std::unique_ptr<ShaderClearHelper>>; | 152 using ShaderClearQueue = std::queue<std::unique_ptr<ShaderClearHelper>>; |
165 using ShaderClearMap = std::map<base::FilePath, ShaderClearQueue>; | 153 using ShaderClearMap = std::map<base::FilePath, ShaderClearQueue>; |
166 ShaderClearMap shader_clear_map_; | 154 ShaderClearMap shader_clear_map_; |
167 | 155 |
168 DISALLOW_COPY_AND_ASSIGN(ShaderCacheFactory); | 156 DISALLOW_COPY_AND_ASSIGN(ShaderCacheFactory); |
169 }; | 157 }; |
170 | 158 |
171 } // namespace content | 159 } // namespace content |
172 | 160 |
173 #endif // CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ | 161 #endif // CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ |
174 | |
OLD | NEW |