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.
5 #ifndef CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_
6 #define CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_
10 #include "base/compiler_specific.h"
11 #include "base/files/file_path.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h"
16 #include "base/process/kill.h"
17 #include "base/synchronization/lock.h"
18 #include "base/time/time.h"
19 #include "base/values.h"
20 #include "content/public/browser/gpu_data_manager.h"
21 #include "content/public/common/gpu_memory_stats.h"
22 #include "content/public/common/three_d_api_types.h"
23 #include "gpu/config/gpu_info.h"
33 class GpuDataManagerImplPrivate
;
34 struct WebPreferences
;
36 class CONTENT_EXPORT GpuDataManagerImpl
37 : public NON_EXPORTED_BASE(GpuDataManager
) {
39 // Indicates the guilt level of a domain which caused a GPU reset.
40 // If a domain is 100% known to be guilty of resetting the GPU, then
41 // it will generally not cause other domains' use of 3D APIs to be
42 // blocked, unless system stability would be compromised.
48 // Indicates the reason that access to a given client API (like
49 // WebGL or Pepper 3D) was blocked or not. This state is distinct
50 // from blacklisting of an entire feature.
51 enum DomainBlockStatus
{
52 DOMAIN_BLOCK_STATUS_BLOCKED
,
53 DOMAIN_BLOCK_STATUS_ALL_DOMAINS_BLOCKED
,
54 DOMAIN_BLOCK_STATUS_NOT_BLOCKED
57 // Getter for the singleton. This will return NULL on failure.
58 static GpuDataManagerImpl
* GetInstance();
60 // GpuDataManager implementation.
61 void InitializeForTesting(const std::string
& gpu_blacklist_json
,
62 const gpu::GPUInfo
& gpu_info
) override
;
63 bool IsFeatureBlacklisted(int feature
) const override
;
64 gpu::GPUInfo
GetGPUInfo() const override
;
65 void GetGpuProcessHandles(
66 const GetGpuProcessHandlesCallback
& callback
) const override
;
67 bool GpuAccessAllowed(std::string
* reason
) const override
;
68 void RequestCompleteGpuInfoIfNeeded() override
;
69 bool IsEssentialGpuInfoAvailable() const override
;
70 bool IsCompleteGpuInfoAvailable() const override
;
71 void RequestVideoMemoryUsageStatsUpdate() const override
;
72 bool ShouldUseSwiftShader() const override
;
73 void RegisterSwiftShaderPath(const base::FilePath
& path
) override
;
74 bool ShouldUseWarp() const override
;
75 void AddObserver(GpuDataManagerObserver
* observer
) override
;
76 void RemoveObserver(GpuDataManagerObserver
* observer
) override
;
77 void UnblockDomainFrom3DAPIs(const GURL
& url
) override
;
78 void DisableGpuWatchdog() override
;
79 void SetGLStrings(const std::string
& gl_vendor
,
80 const std::string
& gl_renderer
,
81 const std::string
& gl_version
) override
;
82 void GetGLStrings(std::string
* gl_vendor
,
83 std::string
* gl_renderer
,
84 std::string
* gl_version
) override
;
85 void DisableHardwareAcceleration() override
;
86 bool CanUseGpuBrowserCompositor() const override
;
88 // This collects preliminary GPU info, load GpuBlacklist, and compute the
89 // preliminary blacklisted features; it should only be called at browser
90 // startup time in UI thread before the IO restriction is turned on.
93 // Only update if the current GPUInfo is not finalized. If blacklist is
94 // loaded, run through blacklist and update blacklisted features.
95 void UpdateGpuInfo(const gpu::GPUInfo
& gpu_info
);
97 void UpdateVideoMemoryUsageStats(
98 const GPUVideoMemoryUsageStats
& video_memory_usage_stats
);
100 // Insert disable-feature switches corresponding to preliminary gpu feature
101 // flags into the renderer process command line.
102 void AppendRendererCommandLine(base::CommandLine
* command_line
) const;
104 // Insert switches into gpu process command line: kUseGL, etc.
105 void AppendGpuCommandLine(base::CommandLine
* command_line
) const;
107 // Insert switches into plugin process command line:
108 // kDisableCoreAnimationPlugins.
109 void AppendPluginCommandLine(base::CommandLine
* command_line
) const;
111 // Update WebPreferences for renderer based on blacklisting decisions.
112 void UpdateRendererWebPrefs(WebPreferences
* prefs
) const;
114 std::string
GetBlacklistVersion() const;
115 std::string
GetDriverBugListVersion() const;
117 // Returns the reasons for the latest run of blacklisting decisions.
118 // For the structure of returned value, see documentation for
119 // GpuBlacklist::GetBlacklistedReasons().
120 void GetBlacklistReasons(base::ListValue
* reasons
) const;
122 // Returns the workarounds that are applied to the current system as
123 // a vector of strings.
124 std::vector
<std::string
> GetDriverBugWorkarounds() const;
126 void AddLogMessage(int level
,
127 const std::string
& header
,
128 const std::string
& message
);
130 void ProcessCrashed(base::TerminationStatus exit_code
);
132 // Returns a new copy of the ListValue. Caller is responsible to release
133 // the returned value.
134 base::ListValue
* GetLogMessages() const;
136 // Called when switching gpu.
137 void HandleGpuSwitch();
139 // Maintenance of domains requiring explicit user permission before
140 // using client-facing 3D APIs (WebGL, Pepper 3D), either because
141 // the domain has caused the GPU to reset, or because too many GPU
142 // resets have been observed globally recently, and system stability
143 // might be compromised.
145 // The given URL may be a partial URL (including at least the host)
146 // or a full URL to a page.
148 // Note that the unblocking API must be part of the content API
149 // because it is called from Chrome side code.
150 void BlockDomainFrom3DAPIs(const GURL
& url
, DomainGuilt guilt
);
151 bool Are3DAPIsBlocked(const GURL
& url
,
152 int render_process_id
,
154 ThreeDAPIType requester
);
156 // Disables domain blocking for 3D APIs. For use only in tests.
157 void DisableDomainBlockingFor3DAPIsForTesting();
159 void Notify3DAPIBlocked(const GURL
& url
,
160 int render_process_id
,
162 ThreeDAPIType requester
);
164 // Get number of features being blacklisted.
165 size_t GetBlacklistedFeatureCount() const;
167 void SetDisplayCount(unsigned int display_count
);
168 unsigned int GetDisplayCount() const;
170 // Set the active gpu.
171 // Return true if it's a different GPU from the previous active one.
172 bool UpdateActiveGpu(uint32 vendor_id
, uint32 device_id
);
174 // Called when GPU process initialization failed.
175 void OnGpuProcessInitFailure();
177 bool IsDriverBugWorkaroundActive(int feature
) const;
180 friend class GpuDataManagerImplPrivate
;
181 friend class GpuDataManagerImplPrivateTest
;
182 friend struct DefaultSingletonTraits
<GpuDataManagerImpl
>;
184 // It's similar to AutoUnlock, but we want to make it a no-op
185 // if the owner GpuDataManagerImpl is null.
186 // This should only be used by GpuDataManagerImplPrivate where
187 // callbacks are called, during which re-entering
188 // GpuDataManagerImpl is possible.
189 class UnlockedSession
{
191 explicit UnlockedSession(GpuDataManagerImpl
* owner
)
194 owner_
->lock_
.AssertAcquired();
195 owner_
->lock_
.Release();
200 owner_
->lock_
.Acquire();
204 GpuDataManagerImpl
* owner_
;
205 DISALLOW_COPY_AND_ASSIGN(UnlockedSession
);
208 GpuDataManagerImpl();
209 ~GpuDataManagerImpl() override
;
211 mutable base::Lock lock_
;
212 scoped_ptr
<GpuDataManagerImplPrivate
> private_
;
214 DISALLOW_COPY_AND_ASSIGN(GpuDataManagerImpl
);
217 } // namespace content
219 #endif // CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_