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

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

Powered by Google App Engine
This is Rietveld 408576698