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

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

Issue 14794006: Refactor GpuDataManagerImpl to make it thread-safe, now and forever. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: better diff Created 7 years, 7 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 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_GPU_DATA_MANAGER_IMPL_H_ 5 #ifndef CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_PRIVATE_H_
6 #define CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_ 6 #define CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_PRIVATE_H_
7 7
8 #include <list> 8 #include <list>
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/callback.h"
15 #include "base/compiler_specific.h"
16 #include "base/files/file_path.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
19 #include "base/memory/singleton.h" 15 #include "base/memory/singleton.h"
20 #include "base/observer_list_threadsafe.h" 16 #include "base/observer_list_threadsafe.h"
21 #include "base/process_util.h"
22 #include "base/synchronization/lock.h"
23 #include "base/time.h"
24 #include "base/values.h"
25 #include "content/browser/gpu/gpu_blacklist.h" 17 #include "content/browser/gpu/gpu_blacklist.h"
18 #include "content/browser/gpu/gpu_data_manager_impl.h"
26 #include "content/browser/gpu/gpu_driver_bug_list.h" 19 #include "content/browser/gpu/gpu_driver_bug_list.h"
27 #include "content/browser/gpu/gpu_switching_list.h" 20 #include "content/browser/gpu/gpu_switching_list.h"
28 #include "content/public/browser/gpu_data_manager.h"
29 #include "content/public/common/gpu_info.h"
30 #include "content/public/common/gpu_memory_stats.h"
31 #include "content/public/common/gpu_switching_option.h"
32 #include "content/public/common/three_d_api_types.h"
33
34 class CommandLine;
35 class GURL;
36 struct WebPreferences;
37 21
38 namespace content { 22 namespace content {
39 23
40 class CONTENT_EXPORT GpuDataManagerImpl 24 class CONTENT_EXPORT GpuDataManagerImplPrivate {
Ken Russell (switch to Gerrit) 2013/05/09 20:21:16 Does this class really need to be CONTENT_EXPORT?
Zhenyao Mo 2013/05/09 23:35:12 Removed.
41 : public NON_EXPORTED_BASE(GpuDataManager) {
42 public: 25 public:
43 // Indicates the guilt level of a domain which caused a GPU reset. 26 void InitializeForTesting(
44 // If a domain is 100% known to be guilty of resetting the GPU, then 27 const std::string& gpu_blacklist_json,
45 // it will generally not cause other domains' use of 3D APIs to be 28 const GPUInfo& gpu_info);
46 // blocked, unless system stability would be compromised. 29 bool IsFeatureBlacklisted(int feature) const;
47 enum DomainGuilt { 30 GPUInfo GetGPUInfo() const;
48 DOMAIN_GUILT_KNOWN, 31 void GetGpuProcessHandles(
49 DOMAIN_GUILT_UNKNOWN 32 const GpuDataManager::GetGpuProcessHandlesCallback& callback) const;
50 }; 33 bool GpuAccessAllowed(std::string* reason) const;
34 void RequestCompleteGpuInfoIfNeeded();
35 bool IsCompleteGpuInfoAvailable() const;
36 void RequestVideoMemoryUsageStatsUpdate() const;
37 bool ShouldUseSwiftShader() const;
38 void RegisterSwiftShaderPath(const base::FilePath& path);
39 void AddObserver(GpuDataManagerObserver* observer);
40 void RemoveObserver(GpuDataManagerObserver* observer);
41 void UnblockDomainFrom3DAPIs(const GURL& url);
42 void DisableGpuWatchdog();
43 void SetGLStrings(const std::string& gl_vendor,
44 const std::string& gl_renderer,
45 const std::string& gl_version);
46 void GetGLStrings(std::string* gl_vendor,
47 std::string* gl_renderer,
48 std::string* gl_version);
49 void DisableHardwareAcceleration();
51 50
52 // Indicates the reason that access to a given client API (like
53 // WebGL or Pepper 3D) was blocked or not. This state is distinct
54 // from blacklisting of an entire feature.
55 enum DomainBlockStatus {
56 DOMAIN_BLOCK_STATUS_BLOCKED,
57 DOMAIN_BLOCK_STATUS_ALL_DOMAINS_BLOCKED,
58 DOMAIN_BLOCK_STATUS_NOT_BLOCKED
59 };
60
61 // Getter for the singleton. This will return NULL on failure.
62 static GpuDataManagerImpl* GetInstance();
63
64 // GpuDataManager implementation.
65 virtual void InitializeForTesting(
66 const std::string& gpu_blacklist_json,
67 const GPUInfo& gpu_info) OVERRIDE;
68 virtual bool IsFeatureBlacklisted(int feature) const OVERRIDE;
69 virtual GPUInfo GetGPUInfo() const OVERRIDE;
70 virtual void GetGpuProcessHandles(
71 const GetGpuProcessHandlesCallback& callback) const OVERRIDE;
72 virtual bool GpuAccessAllowed(std::string* reason) const OVERRIDE;
73 virtual void RequestCompleteGpuInfoIfNeeded() OVERRIDE;
74 virtual bool IsCompleteGpuInfoAvailable() const OVERRIDE;
75 virtual void RequestVideoMemoryUsageStatsUpdate() const OVERRIDE;
76 virtual bool ShouldUseSwiftShader() const OVERRIDE;
77 virtual void RegisterSwiftShaderPath(const base::FilePath& path) OVERRIDE;
78 virtual void AddObserver(GpuDataManagerObserver* observer) OVERRIDE;
79 virtual void RemoveObserver(GpuDataManagerObserver* observer) OVERRIDE;
80 virtual void UnblockDomainFrom3DAPIs(const GURL& url) OVERRIDE;
81 virtual void DisableGpuWatchdog() OVERRIDE;
82 virtual void SetGLStrings(const std::string& gl_vendor,
83 const std::string& gl_renderer,
84 const std::string& gl_version) OVERRIDE;
85 virtual void GetGLStrings(std::string* gl_vendor,
86 std::string* gl_renderer,
87 std::string* gl_version) OVERRIDE;
88 virtual void DisableHardwareAcceleration() OVERRIDE;
89
90 // This collects preliminary GPU info, load GpuBlacklist, and compute the
91 // preliminary blacklisted features; it should only be called at browser
92 // startup time in UI thread before the IO restriction is turned on.
93 void Initialize(); 51 void Initialize();
94 52
95 // Only update if the current GPUInfo is not finalized. If blacklist is
96 // loaded, run through blacklist and update blacklisted features.
97 void UpdateGpuInfo(const GPUInfo& gpu_info); 53 void UpdateGpuInfo(const GPUInfo& gpu_info);
98 54
99 void UpdateVideoMemoryUsageStats( 55 void UpdateVideoMemoryUsageStats(
100 const GPUVideoMemoryUsageStats& video_memory_usage_stats); 56 const GPUVideoMemoryUsageStats& video_memory_usage_stats);
101 57
102 // Insert disable-feature switches corresponding to preliminary gpu feature
103 // flags into the renderer process command line.
104 void AppendRendererCommandLine(CommandLine* command_line) const; 58 void AppendRendererCommandLine(CommandLine* command_line) const;
105 59
106 // Insert switches into gpu process command line: kUseGL,
107 // kDisableGLMultisampling.
108 void AppendGpuCommandLine(CommandLine* command_line) const; 60 void AppendGpuCommandLine(CommandLine* command_line) const;
109 61
110 // Insert switches into plugin process command line:
111 // kDisableCoreAnimationPlugins.
112 void AppendPluginCommandLine(CommandLine* command_line) const; 62 void AppendPluginCommandLine(CommandLine* command_line) const;
113 63
114 // Update WebPreferences for renderer based on blacklisting decisions.
115 void UpdateRendererWebPrefs(WebPreferences* prefs) const; 64 void UpdateRendererWebPrefs(WebPreferences* prefs) const;
116 65
117 GpuSwitchingOption GetGpuSwitchingOption() const; 66 GpuSwitchingOption GetGpuSwitchingOption() const;
118 67
119 std::string GetBlacklistVersion() const; 68 std::string GetBlacklistVersion() const;
120 69
121 // Returns the reasons for the latest run of blacklisting decisions.
122 // For the structure of returned value, see documentation for
123 // GpuBlacklist::GetBlacklistedReasons().
124 // Caller is responsible to release the returned value.
125 base::ListValue* GetBlacklistReasons() const; 70 base::ListValue* GetBlacklistReasons() const;
126 71
127 void AddLogMessage(int level, 72 void AddLogMessage(int level,
128 const std::string& header, 73 const std::string& header,
129 const std::string& message); 74 const std::string& message);
130 75
131 void ProcessCrashed(base::TerminationStatus exit_code); 76 void ProcessCrashed(base::TerminationStatus exit_code);
132 77
133 // Returns a new copy of the ListValue. Caller is responsible to release
134 // the returned value.
135 base::ListValue* GetLogMessages() const; 78 base::ListValue* GetLogMessages() const;
136 79
137 // Called when switching gpu.
138 void HandleGpuSwitch(); 80 void HandleGpuSwitch();
139 81
140 #if defined(OS_WIN) 82 #if defined(OS_WIN)
141 // Is the GPU process using the accelerated surface to present, instead of 83 // Is the GPU process using the accelerated surface to present, instead of
142 // presenting by itself. 84 // presenting by itself.
143 bool IsUsingAcceleratedSurface() const; 85 bool IsUsingAcceleratedSurface() const;
144 #endif 86 #endif
145 87
146 // Maintenance of domains requiring explicit user permission before 88 void BlockDomainFrom3DAPIs(
147 // using client-facing 3D APIs (WebGL, Pepper 3D), either because 89 const GURL& url, GpuDataManagerImpl::DomainGuilt guilt);
148 // the domain has caused the GPU to reset, or because too many GPU
149 // resets have been observed globally recently, and system stability
150 // might be compromised.
151 //
152 // The given URL may be a partial URL (including at least the host)
153 // or a full URL to a page.
154 //
155 // Note that the unblocking API must be part of the content API
156 // because it is called from Chrome side code.
157 void BlockDomainFrom3DAPIs(const GURL& url, DomainGuilt guilt);
158 bool Are3DAPIsBlocked(const GURL& url, 90 bool Are3DAPIsBlocked(const GURL& url,
159 int render_process_id, 91 int render_process_id,
160 int render_view_id, 92 int render_view_id,
161 ThreeDAPIType requester); 93 ThreeDAPIType requester);
162 94
163 // Disables domain blocking for 3D APIs. For use only in tests.
164 void DisableDomainBlockingFor3DAPIsForTesting(); 95 void DisableDomainBlockingFor3DAPIsForTesting();
165 96
166 // Get number of features being blacklisted.
167 size_t GetBlacklistedFeatureCount() const; 97 size_t GetBlacklistedFeatureCount() const;
168 98
169 typedef base::Callback<void()> GpuSwitchCallback; 99 typedef base::Callback<void()> GpuSwitchCallback;
170 100
171 // Add and remove gpu switch callback.
172 void AddGpuSwitchCallback(const GpuSwitchCallback& callback); 101 void AddGpuSwitchCallback(const GpuSwitchCallback& callback);
173 void RemoveGpuSwitchCallback(const GpuSwitchCallback& callback); 102 void RemoveGpuSwitchCallback(const GpuSwitchCallback& callback);
174 103
104 GpuDataManagerImplPrivate();
105 virtual ~GpuDataManagerImplPrivate();
106
175 private: 107 private:
176 struct DomainBlockEntry {
177 DomainGuilt last_guilt;
178 };
179
180 typedef std::map<std::string, DomainBlockEntry> DomainBlockMap;
181
182 typedef ObserverListThreadSafe<GpuDataManagerObserver>
183 GpuDataManagerObserverList;
184
185 friend class GpuDataManagerImplTest; 108 friend class GpuDataManagerImplTest;
186 friend struct DefaultSingletonTraits<GpuDataManagerImpl>;
187 109
188 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuSideBlacklisting); 110 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuSideBlacklisting);
189 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuSideExceptions); 111 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuSideExceptions);
190 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, 112 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest,
191 DisableHardwareAcceleration); 113 DisableHardwareAcceleration);
192 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, SwiftShaderRendering); 114 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, SwiftShaderRendering);
193 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, SwiftShaderRendering2); 115 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, SwiftShaderRendering2);
194 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuInfoUpdate); 116 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuInfoUpdate);
195 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, 117 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest,
196 NoGpuInfoUpdateWithSwiftShader); 118 NoGpuInfoUpdateWithSwiftShader);
197 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, 119 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest,
198 GPUVideoMemoryUsageStatsUpdate); 120 GPUVideoMemoryUsageStatsUpdate);
199 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, 121 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest,
200 BlockAllDomainsFrom3DAPIs); 122 BlockAllDomainsFrom3DAPIs);
201 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, 123 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest,
202 UnblockGuiltyDomainFrom3DAPIs); 124 UnblockGuiltyDomainFrom3DAPIs);
203 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, 125 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest,
204 UnblockDomainOfUnknownGuiltFrom3DAPIs); 126 UnblockDomainOfUnknownGuiltFrom3DAPIs);
205 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, 127 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest,
206 UnblockOtherDomainFrom3DAPIs); 128 UnblockOtherDomainFrom3DAPIs);
207 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, 129 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest,
208 UnblockThisDomainFrom3DAPIs); 130 UnblockThisDomainFrom3DAPIs);
209 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuDriverBugListSingle); 131 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuDriverBugListSingle);
210 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuDriverBugListMultiple); 132 FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplTest, GpuDriverBugListMultiple);
211 133
212 GpuDataManagerImpl(); 134 struct DomainBlockEntry {
213 virtual ~GpuDataManagerImpl(); 135 GpuDataManagerImpl::DomainGuilt last_guilt;
136 };
137
138 typedef std::map<std::string, DomainBlockEntry> DomainBlockMap;
139
140 typedef ObserverListThreadSafe<GpuDataManagerObserver>
141 GpuDataManagerObserverList;
214 142
215 void InitializeImpl(const std::string& gpu_blacklist_json, 143 void InitializeImpl(const std::string& gpu_blacklist_json,
216 const std::string& gpu_switching_list_json, 144 const std::string& gpu_switching_list_json,
217 const std::string& gpu_driver_bug_list_json, 145 const std::string& gpu_driver_bug_list_json,
218 const GPUInfo& gpu_info); 146 const GPUInfo& gpu_info);
219 147
220 void UpdateBlacklistedFeatures(const std::set<int>& features); 148 void UpdateBlacklistedFeatures(const std::set<int>& features);
221 149
222 // This should only be called once at initialization time, when preliminary 150 // This should only be called once at initialization time, when preliminary
223 // gpu info is collected. 151 // gpu info is collected.
224 void UpdatePreliminaryBlacklistedFeatures(); 152 void UpdatePreliminaryBlacklistedFeatures();
225 153
226 // Update the GPU switching status. 154 // Update the GPU switching status.
227 // This should only be called once at initialization time. 155 // This should only be called once at initialization time.
228 void UpdateGpuSwitchingManager(const GPUInfo& gpu_info); 156 void UpdateGpuSwitchingManager(const GPUInfo& gpu_info);
229 157
230 // Notify all observers whenever there is a GPU info update. 158 // Notify all observers whenever there is a GPU info update.
231 void NotifyGpuInfoUpdate(); 159 void NotifyGpuInfoUpdate();
232 160
233 // Try to switch to SwiftShader rendering, if possible and necessary. 161 // Try to switch to SwiftShader rendering, if possible and necessary.
234 void EnableSwiftShaderIfNecessary(); 162 void EnableSwiftShaderIfNecessary();
235 163
236 // Helper to extract the domain from a given URL. 164 // Helper to extract the domain from a given URL.
237 std::string GetDomainFromURL(const GURL& url) const; 165 std::string GetDomainFromURL(const GURL& url) const;
238 166
239 // Implementation functions for blocking of 3D graphics APIs, used 167 // Implementation functions for blocking of 3D graphics APIs, used
240 // for unit testing. 168 // for unit testing.
241 void BlockDomainFrom3DAPIsAtTime( 169 void BlockDomainFrom3DAPIsAtTime(const GURL& url,
242 const GURL& url, DomainGuilt guilt, base::Time at_time); 170 GpuDataManagerImpl::DomainGuilt guilt,
243 DomainBlockStatus Are3DAPIsBlockedAtTime( 171 base::Time at_time);
172 GpuDataManagerImpl::DomainBlockStatus Are3DAPIsBlockedAtTime(
244 const GURL& url, base::Time at_time) const; 173 const GURL& url, base::Time at_time) const;
245 int64 GetBlockAllDomainsDurationInMs() const; 174 int64 GetBlockAllDomainsDurationInMs() const;
246 175
247 void Notify3DAPIBlocked(const GURL& url, 176 void Notify3DAPIBlocked(const GURL& url,
248 int render_process_id, 177 int render_process_id,
249 int render_view_id, 178 int render_view_id,
250 ThreeDAPIType requester); 179 ThreeDAPIType requester);
251 180
252 bool complete_gpu_info_already_requested_; 181 bool complete_gpu_info_already_requested_;
253 182
254 std::set<int> blacklisted_features_; 183 std::set<int> blacklisted_features_;
255 std::set<int> preliminary_blacklisted_features_; 184 std::set<int> preliminary_blacklisted_features_;
256 185
257 GpuSwitchingOption gpu_switching_; 186 GpuSwitchingOption gpu_switching_;
258 187
259 std::set<int> gpu_driver_bugs_; 188 std::set<int> gpu_driver_bugs_;
260 189
261 GPUInfo gpu_info_; 190 GPUInfo gpu_info_;
262 mutable base::Lock gpu_info_lock_;
263 191
264 scoped_ptr<GpuBlacklist> gpu_blacklist_; 192 scoped_ptr<GpuBlacklist> gpu_blacklist_;
265 scoped_ptr<GpuSwitchingList> gpu_switching_list_; 193 scoped_ptr<GpuSwitchingList> gpu_switching_list_;
266 scoped_ptr<GpuDriverBugList> gpu_driver_bug_list_; 194 scoped_ptr<GpuDriverBugList> gpu_driver_bug_list_;
267 195
268 const scoped_refptr<GpuDataManagerObserverList> observer_list_; 196 const scoped_refptr<GpuDataManagerObserverList> observer_list_;
269 197
270 ListValue log_messages_; 198 ListValue log_messages_;
271 mutable base::Lock log_messages_lock_;
272 199
273 bool use_swiftshader_; 200 bool use_swiftshader_;
274 201
275 base::FilePath swiftshader_path_; 202 base::FilePath swiftshader_path_;
276 203
277 // Current card force-blacklisted due to GPU crashes, or disabled through 204 // Current card force-blacklisted due to GPU crashes, or disabled through
278 // the --disable-gpu commandline switch. 205 // the --disable-gpu commandline switch.
279 bool card_blacklisted_; 206 bool card_blacklisted_;
280 207
281 // We disable histogram stuff in testing, especially in unit tests because 208 // We disable histogram stuff in testing, especially in unit tests because
282 // they cause random failures. 209 // they cause random failures.
283 bool update_histograms_; 210 bool update_histograms_;
284 211
285 // Number of currently open windows, to be used in gpu memory allocation. 212 // Number of currently open windows, to be used in gpu memory allocation.
286 int window_count_; 213 int window_count_;
287 214
288 DomainBlockMap blocked_domains_; 215 DomainBlockMap blocked_domains_;
289 mutable std::list<base::Time> timestamps_of_gpu_resets_; 216 mutable std::list<base::Time> timestamps_of_gpu_resets_;
290 bool domain_blocking_enabled_; 217 bool domain_blocking_enabled_;
291 218
292 std::vector<GpuSwitchCallback> gpu_switch_callbacks_; 219 std::vector<GpuSwitchCallback> gpu_switch_callbacks_;
293 220
294 DISALLOW_COPY_AND_ASSIGN(GpuDataManagerImpl); 221 DISALLOW_COPY_AND_ASSIGN(GpuDataManagerImplPrivate);
295 }; 222 };
296 223
297 } // namespace content 224 } // namespace content
298 225
299 #endif // CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_ 226 #endif // CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_PRIVATE_H_
227
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698