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