Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / gpu / gpu_data_manager_impl_private.h
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 CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_PRIVATE_H_
6 #define CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_PRIVATE_H_
7
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <string>
12 #include <vector>
13
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/singleton.h"
16 #include "base/observer_list_threadsafe.h"
17 #include "content/browser/gpu/gpu_data_manager_impl.h"
18 #include "gpu/config/gpu_blacklist.h"
19 #include "gpu/config/gpu_driver_bug_list.h"
20
21 namespace base {
22 class CommandLine;
23 }
24
25 namespace content {
26
27 class CONTENT_EXPORT GpuDataManagerImplPrivate {
28  public:
29   static GpuDataManagerImplPrivate* Create(GpuDataManagerImpl* owner);
30
31   void InitializeForTesting(
32       const std::string& gpu_blacklist_json,
33       const gpu::GPUInfo& gpu_info);
34   bool IsFeatureBlacklisted(int feature) const;
35   bool IsDriverBugWorkaroundActive(int feature) const;
36   gpu::GPUInfo GetGPUInfo() const;
37   void GetGpuProcessHandles(
38       const GpuDataManager::GetGpuProcessHandlesCallback& callback) const;
39   bool GpuAccessAllowed(std::string* reason) const;
40   void RequestCompleteGpuInfoIfNeeded();
41   bool IsCompleteGpuInfoAvailable() const;
42   void RequestVideoMemoryUsageStatsUpdate() const;
43   bool ShouldUseSwiftShader() const;
44   void RegisterSwiftShaderPath(const base::FilePath& path);
45   void AddObserver(GpuDataManagerObserver* observer);
46   void RemoveObserver(GpuDataManagerObserver* observer);
47   void UnblockDomainFrom3DAPIs(const GURL& url);
48   void DisableGpuWatchdog();
49   void SetGLStrings(const std::string& gl_vendor,
50                     const std::string& gl_renderer,
51                     const std::string& gl_version);
52   void GetGLStrings(std::string* gl_vendor,
53                     std::string* gl_renderer,
54                     std::string* gl_version);
55   void DisableHardwareAcceleration();
56
57   void Initialize();
58
59   void UpdateGpuInfo(const gpu::GPUInfo& gpu_info);
60
61   void UpdateVideoMemoryUsageStats(
62       const GPUVideoMemoryUsageStats& video_memory_usage_stats);
63
64   void AppendRendererCommandLine(base::CommandLine* command_line) const;
65
66   void AppendGpuCommandLine(base::CommandLine* command_line) const;
67
68   void AppendPluginCommandLine(base::CommandLine* command_line) const;
69
70   void UpdateRendererWebPrefs(WebPreferences* prefs) const;
71
72   std::string GetBlacklistVersion() const;
73   std::string GetDriverBugListVersion() const;
74
75   void GetBlacklistReasons(base::ListValue* reasons) const;
76
77   void GetDriverBugWorkarounds(base::ListValue* workarounds) const;
78
79   void AddLogMessage(int level,
80                      const std::string& header,
81                      const std::string& message);
82
83   void ProcessCrashed(base::TerminationStatus exit_code);
84
85   base::ListValue* GetLogMessages() const;
86
87   void HandleGpuSwitch();
88
89   bool CanUseGpuBrowserCompositor() const;
90
91   void BlockDomainFrom3DAPIs(
92       const GURL& url, GpuDataManagerImpl::DomainGuilt guilt);
93   bool Are3DAPIsBlocked(const GURL& url,
94                         int render_process_id,
95                         int render_view_id,
96                         ThreeDAPIType requester);
97
98   void DisableDomainBlockingFor3DAPIsForTesting();
99
100   void Notify3DAPIBlocked(const GURL& url,
101                           int render_process_id,
102                           int render_view_id,
103                           ThreeDAPIType requester);
104
105   size_t GetBlacklistedFeatureCount() const;
106
107   void SetDisplayCount(unsigned int display_count);
108   unsigned int GetDisplayCount() const;
109
110   bool UpdateActiveGpu(uint32 vendor_id, uint32 device_id);
111
112   void OnGpuProcessInitFailure();
113
114   virtual ~GpuDataManagerImplPrivate();
115
116  private:
117   friend class GpuDataManagerImplPrivateTest;
118
119   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
120                            GpuSideBlacklisting);
121   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
122                            GpuSideExceptions);
123   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
124                            DisableHardwareAcceleration);
125   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
126                            SwiftShaderRendering);
127   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
128                            SwiftShaderRendering2);
129   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
130                            GpuInfoUpdate);
131   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
132                            NoGpuInfoUpdateWithSwiftShader);
133   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
134                            GPUVideoMemoryUsageStatsUpdate);
135   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
136                            BlockAllDomainsFrom3DAPIs);
137   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
138                            UnblockGuiltyDomainFrom3DAPIs);
139   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
140                            UnblockDomainOfUnknownGuiltFrom3DAPIs);
141   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
142                            UnblockOtherDomainFrom3DAPIs);
143   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
144                            UnblockThisDomainFrom3DAPIs);
145 #if defined(OS_LINUX)
146   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
147                            SetGLStrings);
148   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
149                            SetGLStringsNoEffects);
150 #endif
151   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
152                            GpuDriverBugListSingle);
153   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
154                            GpuDriverBugListMultiple);
155   FRIEND_TEST_ALL_PREFIXES(GpuDataManagerImplPrivateTest,
156                            BlacklistAllFeatures);
157
158   struct DomainBlockEntry {
159     GpuDataManagerImpl::DomainGuilt last_guilt;
160   };
161
162   typedef std::map<std::string, DomainBlockEntry> DomainBlockMap;
163
164   typedef ObserverListThreadSafe<GpuDataManagerObserver>
165       GpuDataManagerObserverList;
166
167   struct LogMessage {
168     int level;
169     std::string header;
170     std::string message;
171
172     LogMessage(int _level,
173                const std::string& _header,
174                const std::string& _message)
175         : level(_level),
176           header(_header),
177           message(_message) { }
178   };
179
180   explicit GpuDataManagerImplPrivate(GpuDataManagerImpl* owner);
181
182   void InitializeImpl(const std::string& gpu_blacklist_json,
183                       const std::string& gpu_driver_bug_list_json,
184                       const gpu::GPUInfo& gpu_info);
185
186   void UpdateGpuInfoHelper();
187
188   void UpdateBlacklistedFeatures(const std::set<int>& features);
189
190   // This should only be called once at initialization time, when preliminary
191   // gpu info is collected.
192   void UpdatePreliminaryBlacklistedFeatures();
193
194   // Update the GPU switching status.
195   // This should only be called once at initialization time.
196   void UpdateGpuSwitchingManager(const gpu::GPUInfo& gpu_info);
197
198   // Notify all observers whenever there is a GPU info update.
199   void NotifyGpuInfoUpdate();
200
201   // Try to switch to SwiftShader rendering, if possible and necessary.
202   void EnableSwiftShaderIfNecessary();
203
204   // Helper to extract the domain from a given URL.
205   std::string GetDomainFromURL(const GURL& url) const;
206
207   // Implementation functions for blocking of 3D graphics APIs, used
208   // for unit testing.
209   void BlockDomainFrom3DAPIsAtTime(const GURL& url,
210                                    GpuDataManagerImpl::DomainGuilt guilt,
211                                    base::Time at_time);
212   GpuDataManagerImpl::DomainBlockStatus Are3DAPIsBlockedAtTime(
213       const GURL& url, base::Time at_time) const;
214   int64 GetBlockAllDomainsDurationInMs() const;
215
216   bool complete_gpu_info_already_requested_;
217
218   std::set<int> blacklisted_features_;
219   std::set<int> preliminary_blacklisted_features_;
220
221   std::set<int> gpu_driver_bugs_;
222
223   gpu::GPUInfo gpu_info_;
224
225   scoped_ptr<gpu::GpuBlacklist> gpu_blacklist_;
226   scoped_ptr<gpu::GpuDriverBugList> gpu_driver_bug_list_;
227
228   const scoped_refptr<GpuDataManagerObserverList> observer_list_;
229
230   std::vector<LogMessage> log_messages_;
231
232   bool use_swiftshader_;
233
234   base::FilePath swiftshader_path_;
235
236   // Current card force-blacklisted due to GPU crashes, or disabled through
237   // the --disable-gpu commandline switch.
238   bool card_blacklisted_;
239
240   // We disable histogram stuff in testing, especially in unit tests because
241   // they cause random failures.
242   bool update_histograms_;
243
244   // Number of currently open windows, to be used in gpu memory allocation.
245   int window_count_;
246
247   DomainBlockMap blocked_domains_;
248   mutable std::list<base::Time> timestamps_of_gpu_resets_;
249   bool domain_blocking_enabled_;
250
251   GpuDataManagerImpl* owner_;
252
253   unsigned int display_count_;
254
255   bool gpu_process_accessible_;
256
257   // True if all future Initialize calls should be ignored.
258   bool finalized_;
259
260   DISALLOW_COPY_AND_ASSIGN(GpuDataManagerImplPrivate);
261 };
262
263 }  // namespace content
264
265 #endif  // CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_PRIVATE_H_
266