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