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 // TODO(kbr): the threading model for the GpuDataManagerObservers is
76 // not well defined, and it's impossible for callers to correctly
77 // delete observers from anywhere except in one of the observer's
78 // notification methods. Observer addition and removal, and their
79 // callbacks, should probably be required to occur on the UI thread.
80 void AddObserver(GpuDataManagerObserver
* observer
) override
;
81 void RemoveObserver(GpuDataManagerObserver
* observer
) override
;
82 void UnblockDomainFrom3DAPIs(const GURL
& url
) override
;
83 void DisableGpuWatchdog() override
;
84 void SetGLStrings(const std::string
& gl_vendor
,
85 const std::string
& gl_renderer
,
86 const std::string
& gl_version
) override
;
87 void GetGLStrings(std::string
* gl_vendor
,
88 std::string
* gl_renderer
,
89 std::string
* gl_version
) override
;
90 void DisableHardwareAcceleration() override
;
91 bool CanUseGpuBrowserCompositor() const override
;
92 void GetDisabledExtensions(std::string
* disabled_extensions
) const override
;
94 // This collects preliminary GPU info, load GpuBlacklist, and compute the
95 // preliminary blacklisted features; it should only be called at browser
96 // startup time in UI thread before the IO restriction is turned on.
99 // Only update if the current GPUInfo is not finalized. If blacklist is
100 // loaded, run through blacklist and update blacklisted features.
101 void UpdateGpuInfo(const gpu::GPUInfo
& gpu_info
);
103 void UpdateVideoMemoryUsageStats(
104 const GPUVideoMemoryUsageStats
& video_memory_usage_stats
);
106 // Insert disable-feature switches corresponding to preliminary gpu feature
107 // flags into the renderer process command line.
108 void AppendRendererCommandLine(base::CommandLine
* command_line
) const;
110 // Insert switches into gpu process command line: kUseGL, etc.
111 void AppendGpuCommandLine(base::CommandLine
* command_line
) const;
113 // Insert switches into plugin process command line:
114 // kDisableCoreAnimationPlugins.
115 void AppendPluginCommandLine(base::CommandLine
* command_line
) const;
117 // Update WebPreferences for renderer based on blacklisting decisions.
118 void UpdateRendererWebPrefs(WebPreferences
* prefs
) const;
120 std::string
GetBlacklistVersion() const;
121 std::string
GetDriverBugListVersion() const;
123 // Returns the reasons for the latest run of blacklisting decisions.
124 // For the structure of returned value, see documentation for
125 // GpuBlacklist::GetBlacklistedReasons().
126 void GetBlacklistReasons(base::ListValue
* reasons
) const;
128 // Returns the workarounds that are applied to the current system as
129 // a vector of strings.
130 std::vector
<std::string
> GetDriverBugWorkarounds() const;
132 void AddLogMessage(int level
,
133 const std::string
& header
,
134 const std::string
& message
);
136 void ProcessCrashed(base::TerminationStatus exit_code
);
138 // Returns a new copy of the ListValue. Caller is responsible to release
139 // the returned value.
140 base::ListValue
* GetLogMessages() const;
142 // Called when switching gpu.
143 void HandleGpuSwitch();
145 // Maintenance of domains requiring explicit user permission before
146 // using client-facing 3D APIs (WebGL, Pepper 3D), either because
147 // the domain has caused the GPU to reset, or because too many GPU
148 // resets have been observed globally recently, and system stability
149 // might be compromised.
151 // The given URL may be a partial URL (including at least the host)
152 // or a full URL to a page.
154 // Note that the unblocking API must be part of the content API
155 // because it is called from Chrome side code.
156 void BlockDomainFrom3DAPIs(const GURL
& url
, DomainGuilt guilt
);
157 bool Are3DAPIsBlocked(const GURL
& url
,
158 int render_process_id
,
160 ThreeDAPIType requester
);
162 // Disables domain blocking for 3D APIs. For use only in tests.
163 void DisableDomainBlockingFor3DAPIsForTesting();
165 void Notify3DAPIBlocked(const GURL
& url
,
166 int render_process_id
,
168 ThreeDAPIType requester
);
170 // Get number of features being blacklisted.
171 size_t GetBlacklistedFeatureCount() const;
173 void SetDisplayCount(unsigned int display_count
);
174 unsigned int GetDisplayCount() const;
176 // Set the active gpu.
177 // Return true if it's a different GPU from the previous active one.
178 bool UpdateActiveGpu(uint32 vendor_id
, uint32 device_id
);
180 // Called when GPU process initialization failed.
181 void OnGpuProcessInitFailure();
183 bool IsDriverBugWorkaroundActive(int feature
) const;
186 friend class GpuDataManagerImplPrivate
;
187 friend class GpuDataManagerImplPrivateTest
;
188 friend struct DefaultSingletonTraits
<GpuDataManagerImpl
>;
190 // It's similar to AutoUnlock, but we want to make it a no-op
191 // if the owner GpuDataManagerImpl is null.
192 // This should only be used by GpuDataManagerImplPrivate where
193 // callbacks are called, during which re-entering
194 // GpuDataManagerImpl is possible.
195 class UnlockedSession
{
197 explicit UnlockedSession(GpuDataManagerImpl
* owner
)
200 owner_
->lock_
.AssertAcquired();
201 owner_
->lock_
.Release();
206 owner_
->lock_
.Acquire();
210 GpuDataManagerImpl
* owner_
;
211 DISALLOW_COPY_AND_ASSIGN(UnlockedSession
);
214 GpuDataManagerImpl();
215 ~GpuDataManagerImpl() override
;
217 mutable base::Lock lock_
;
218 scoped_ptr
<GpuDataManagerImplPrivate
> private_
;
220 DISALLOW_COPY_AND_ASSIGN(GpuDataManagerImpl
);
223 } // namespace content
225 #endif // CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_