Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: content/browser/gpu/shader_disk_cache.h

Issue 2472473002: gpu shader cache: Clarify lifetime/ownership/threadiness of objects. (Closed)
Patch Set: . Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/memory/singleton.h" 17 #include "base/memory/singleton.h"
18 #include "base/threading/thread_checker.h"
18 #include "content/common/content_export.h" 19 #include "content/common/content_export.h"
19 #include "net/disk_cache/disk_cache.h" 20 #include "net/disk_cache/disk_cache.h"
20 21
21 namespace content { 22 namespace content {
22 23
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 //
30 // While this class is both RefCounted and SupportsWeakPtr
31 // when using this class you should work with the RefCounting.
32 // The WeakPtr is needed interally.
33 class CONTENT_EXPORT ShaderDiskCache 30 class CONTENT_EXPORT ShaderDiskCache
34 : public base::RefCounted<ShaderDiskCache>, 31 : public base::RefCounted<ShaderDiskCache> {
35 public base::SupportsWeakPtr<ShaderDiskCache> {
36 public: 32 public:
37 using ShaderLoadedCallback = 33 using ShaderLoadedCallback =
38 base::Callback<void(const std::string&, const std::string&)>; 34 base::Callback<void(const std::string&, const std::string&)>;
39 void Init();
40 35
41 void set_shader_loaded_callback(const ShaderLoadedCallback& callback) { 36 void set_shader_loaded_callback(const ShaderLoadedCallback& callback) {
42 shader_loaded_callback_ = callback; 37 shader_loaded_callback_ = callback;
43 } 38 }
44 39
45 // Store the |shader| into the cache under |key|. 40 // Store the |shader| into the cache under |key|.
46 void Cache(const std::string& key, const std::string& shader); 41 void Cache(const std::string& key, const std::string& shader);
47 42
48 // Clear a range of entries. This supports unbounded deletes in either 43 // Clear a range of entries. This supports unbounded deletes in either
49 // direction by using null Time values for either |begin_time| or |end_time|. 44 // direction by using null Time values for either |begin_time| or |end_time|.
(...skipping 23 matching lines...) Expand all
73 68
74 private: 69 private:
75 friend class base::RefCounted<ShaderDiskCache>; 70 friend class base::RefCounted<ShaderDiskCache>;
76 friend class ShaderDiskCacheEntry; 71 friend class ShaderDiskCacheEntry;
77 friend class ShaderDiskReadHelper; 72 friend class ShaderDiskReadHelper;
78 friend class ShaderCacheFactory; 73 friend class ShaderCacheFactory;
79 74
80 explicit ShaderDiskCache(const base::FilePath& cache_path); 75 explicit ShaderDiskCache(const base::FilePath& cache_path);
81 ~ShaderDiskCache(); 76 ~ShaderDiskCache();
82 77
78 void Init(scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner);
83 void CacheCreatedCallback(int rv); 79 void CacheCreatedCallback(int rv);
84 80
85 disk_cache::Backend* backend() { return backend_.get(); } 81 disk_cache::Backend* backend() { return backend_.get(); }
86 82
87 void EntryComplete(void* entry); 83 void EntryComplete(ShaderDiskCacheEntry* entry);
88 void ReadComplete(); 84 void ReadComplete();
89 85
90 bool cache_available_; 86 bool cache_available_;
91 base::FilePath cache_path_; 87 base::FilePath cache_path_;
92 bool is_initialized_; 88 bool is_initialized_;
93 net::CompletionCallback available_callback_; 89 net::CompletionCallback available_callback_;
94 net::CompletionCallback cache_complete_callback_; 90 net::CompletionCallback cache_complete_callback_;
95 ShaderLoadedCallback shader_loaded_callback_; 91 ShaderLoadedCallback shader_loaded_callback_;
96 92
97 std::unique_ptr<disk_cache::Backend> backend_; 93 std::unique_ptr<disk_cache::Backend> backend_;
98 94
99 scoped_refptr<ShaderDiskReadHelper> helper_; 95 std::unique_ptr<ShaderDiskReadHelper> helper_;
100 std::map<void*, scoped_refptr<ShaderDiskCacheEntry> > entry_map_; 96 std::vector<std::unique_ptr<ShaderDiskCacheEntry>> entries_;
101 97
102 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCache); 98 DISALLOW_COPY_AND_ASSIGN(ShaderDiskCache);
103 }; 99 };
104 100
105 // ShaderCacheFactory maintains a cache of ShaderDiskCache objects 101 // ShaderCacheFactory maintains a cache of ShaderDiskCache objects
106 // so we only create one per profile directory. 102 // so we only create one per profile directory.
107 class CONTENT_EXPORT ShaderCacheFactory { 103 class CONTENT_EXPORT ShaderCacheFactory
104 : NON_EXPORTED_BASE(public base::ThreadChecker) {
108 public: 105 public:
106 // Initializes the ShaderCacheFactory singleton instance. The singleton
107 // instance is created and used in the thread associated with |task_runner|.
108 // |cache_task_runner| is associated with the thread responsible for managing
109 // the disk cache.
110 static void InitInstance(
111 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
112 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner);
113
109 static ShaderCacheFactory* GetInstance(); 114 static ShaderCacheFactory* GetInstance();
110 115
111 // Clear the shader disk cache for the given |path|. This supports unbounded 116 // Clear the shader disk cache for the given |path|. This supports unbounded
112 // deletes in either direction by using null Time values for either 117 // deletes in either direction by using null Time values for either
113 // |begin_time| or |end_time|. The |callback| will be executed when the 118 // |begin_time| or |end_time|. The |callback| will be executed when the
114 // clear is complete. 119 // clear is complete.
115 void ClearByPath(const base::FilePath& path, 120 void ClearByPath(const base::FilePath& path,
116 const base::Time& begin_time, 121 const base::Time& begin_time,
117 const base::Time& end_time, 122 const base::Time& end_time,
118 const base::Closure& callback); 123 const base::Closure& callback);
(...skipping 13 matching lines...) Expand all
132 // Remove the provided |path| from our cache map. 137 // Remove the provided |path| from our cache map.
133 void RemoveFromCache(const base::FilePath& path); 138 void RemoveFromCache(const base::FilePath& path);
134 139
135 private: 140 private:
136 friend struct base::DefaultSingletonTraits<ShaderCacheFactory>; 141 friend struct base::DefaultSingletonTraits<ShaderCacheFactory>;
137 friend class ShaderClearHelper; 142 friend class ShaderClearHelper;
138 143
139 ShaderCacheFactory(); 144 ShaderCacheFactory();
140 ~ShaderCacheFactory(); 145 ~ShaderCacheFactory();
141 146
147 static void CreateFactoryInstance(
148 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner);
149
150 scoped_refptr<base::SingleThreadTaskRunner> cache_task_runner_;
151
142 scoped_refptr<ShaderDiskCache> GetByPath(const base::FilePath& path); 152 scoped_refptr<ShaderDiskCache> GetByPath(const base::FilePath& path);
143 void CacheCleared(const base::FilePath& path); 153 void CacheCleared(const base::FilePath& path);
144 154
145 typedef std::map<base::FilePath, ShaderDiskCache*> ShaderCacheMap; 155 typedef std::map<base::FilePath, ShaderDiskCache*> ShaderCacheMap;
146 ShaderCacheMap shader_cache_map_; 156 ShaderCacheMap shader_cache_map_;
147 157
148 typedef std::map<int32_t, base::FilePath> ClientIdToPathMap; 158 typedef std::map<int32_t, base::FilePath> ClientIdToPathMap;
149 ClientIdToPathMap client_id_to_path_map_; 159 ClientIdToPathMap client_id_to_path_map_;
150 160
151 typedef std::queue<scoped_refptr<ShaderClearHelper> > ShaderClearQueue; 161 typedef std::queue<scoped_refptr<ShaderClearHelper> > ShaderClearQueue;
152 typedef std::map<base::FilePath, ShaderClearQueue> ShaderClearMap; 162 typedef std::map<base::FilePath, ShaderClearQueue> ShaderClearMap;
153 ShaderClearMap shader_clear_map_; 163 ShaderClearMap shader_clear_map_;
154 164
155 DISALLOW_COPY_AND_ASSIGN(ShaderCacheFactory); 165 DISALLOW_COPY_AND_ASSIGN(ShaderCacheFactory);
156 }; 166 };
157 167
158 } // namespace content 168 } // namespace content
159 169
160 #endif // CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_ 170 #endif // CONTENT_BROWSER_GPU_SHADER_DISK_CACHE_H_
161 171
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698