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