gpu_data_manager_impl.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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_H_ 6#define CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_ 7 8#include <string> 9 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" 24 25class CommandLine; 26class GURL; 27struct WebPreferences; 28 29namespace content { 30 31class GpuDataManagerImplPrivate; 32 33class CONTENT_EXPORT GpuDataManagerImpl 34 : public NON_EXPORTED_BASE(GpuDataManager) { 35 public: 36 // Indicates the guilt level of a domain which caused a GPU reset. 37 // If a domain is 100% known to be guilty of resetting the GPU, then 38 // it will generally not cause other domains' use of 3D APIs to be 39 // blocked, unless system stability would be compromised. 40 enum DomainGuilt { 41 DOMAIN_GUILT_KNOWN, 42 DOMAIN_GUILT_UNKNOWN 43 }; 44 45 // Indicates the reason that access to a given client API (like 46 // WebGL or Pepper 3D) was blocked or not. This state is distinct 47 // from blacklisting of an entire feature. 48 enum DomainBlockStatus { 49 DOMAIN_BLOCK_STATUS_BLOCKED, 50 DOMAIN_BLOCK_STATUS_ALL_DOMAINS_BLOCKED, 51 DOMAIN_BLOCK_STATUS_NOT_BLOCKED 52 }; 53 54 // Getter for the singleton. This will return NULL on failure. 55 static GpuDataManagerImpl* GetInstance(); 56 57 // GpuDataManager implementation. 58 virtual void InitializeForTesting( 59 const std::string& gpu_blacklist_json, 60 const gpu::GPUInfo& gpu_info) OVERRIDE; 61 virtual bool IsFeatureBlacklisted(int feature) const OVERRIDE; 62 virtual gpu::GPUInfo GetGPUInfo() const OVERRIDE; 63 virtual void GetGpuProcessHandles( 64 const GetGpuProcessHandlesCallback& callback) const OVERRIDE; 65 virtual bool GpuAccessAllowed(std::string* reason) const OVERRIDE; 66 virtual void RequestCompleteGpuInfoIfNeeded() OVERRIDE; 67 virtual bool IsCompleteGpuInfoAvailable() const OVERRIDE; 68 virtual void RequestVideoMemoryUsageStatsUpdate() const OVERRIDE; 69 virtual bool ShouldUseSwiftShader() const OVERRIDE; 70 virtual void RegisterSwiftShaderPath(const base::FilePath& path) OVERRIDE; 71 virtual void AddObserver(GpuDataManagerObserver* observer) OVERRIDE; 72 virtual void RemoveObserver(GpuDataManagerObserver* observer) OVERRIDE; 73 virtual void UnblockDomainFrom3DAPIs(const GURL& url) OVERRIDE; 74 virtual void DisableGpuWatchdog() OVERRIDE; 75 virtual void SetGLStrings(const std::string& gl_vendor, 76 const std::string& gl_renderer, 77 const std::string& gl_version) OVERRIDE; 78 virtual void GetGLStrings(std::string* gl_vendor, 79 std::string* gl_renderer, 80 std::string* gl_version) OVERRIDE; 81 virtual void DisableHardwareAcceleration() OVERRIDE; 82 virtual bool CanUseGpuBrowserCompositor() const OVERRIDE; 83 84 // This collects preliminary GPU info, load GpuBlacklist, and compute the 85 // preliminary blacklisted features; it should only be called at browser 86 // startup time in UI thread before the IO restriction is turned on. 87 void Initialize(); 88 89 // Only update if the current GPUInfo is not finalized. If blacklist is 90 // loaded, run through blacklist and update blacklisted features. 91 void UpdateGpuInfo(const gpu::GPUInfo& gpu_info); 92 93 void UpdateVideoMemoryUsageStats( 94 const GPUVideoMemoryUsageStats& video_memory_usage_stats); 95 96 // Insert disable-feature switches corresponding to preliminary gpu feature 97 // flags into the renderer process command line. 98 void AppendRendererCommandLine(CommandLine* command_line) const; 99 100 // Insert switches into gpu process command line: kUseGL, etc. 101 void AppendGpuCommandLine(CommandLine* command_line) const; 102 103 // Insert switches into plugin process command line: 104 // kDisableCoreAnimationPlugins. 105 void AppendPluginCommandLine(CommandLine* command_line) const; 106 107 // Update WebPreferences for renderer based on blacklisting decisions. 108 void UpdateRendererWebPrefs(WebPreferences* prefs) const; 109 110 std::string GetBlacklistVersion() const; 111 std::string GetDriverBugListVersion() const; 112 113 // Returns the reasons for the latest run of blacklisting decisions. 114 // For the structure of returned value, see documentation for 115 // GpuBlacklist::GetBlacklistedReasons(). 116 void GetBlacklistReasons(base::ListValue* reasons) const; 117 118 // Returns the workarounds that are applied to the current system as 119 // a list of strings. 120 void GetDriverBugWorkarounds(base::ListValue* workarounds) const; 121 122 void AddLogMessage(int level, 123 const std::string& header, 124 const std::string& message); 125 126 void ProcessCrashed(base::TerminationStatus exit_code); 127 128 // Returns a new copy of the ListValue. Caller is responsible to release 129 // the returned value. 130 base::ListValue* GetLogMessages() const; 131 132 // Called when switching gpu. 133 void HandleGpuSwitch(); 134 135 // Maintenance of domains requiring explicit user permission before 136 // using client-facing 3D APIs (WebGL, Pepper 3D), either because 137 // the domain has caused the GPU to reset, or because too many GPU 138 // resets have been observed globally recently, and system stability 139 // might be compromised. 140 // 141 // The given URL may be a partial URL (including at least the host) 142 // or a full URL to a page. 143 // 144 // Note that the unblocking API must be part of the content API 145 // because it is called from Chrome side code. 146 void BlockDomainFrom3DAPIs(const GURL& url, DomainGuilt guilt); 147 bool Are3DAPIsBlocked(const GURL& url, 148 int render_process_id, 149 int render_view_id, 150 ThreeDAPIType requester); 151 152 // Disables domain blocking for 3D APIs. For use only in tests. 153 void DisableDomainBlockingFor3DAPIsForTesting(); 154 155 void Notify3DAPIBlocked(const GURL& url, 156 int render_process_id, 157 int render_view_id, 158 ThreeDAPIType requester); 159 160 // Get number of features being blacklisted. 161 size_t GetBlacklistedFeatureCount() const; 162 163 void SetDisplayCount(unsigned int display_count); 164 unsigned int GetDisplayCount() const; 165 166 // Called when GPU process initialization failed. 167 void OnGpuProcessInitFailure(); 168 169 bool IsDriverBugWorkaroundActive(int feature) const; 170 171 private: 172 friend class GpuDataManagerImplPrivate; 173 friend class GpuDataManagerImplPrivateTest; 174 friend struct DefaultSingletonTraits<GpuDataManagerImpl>; 175 176 // It's similar to AutoUnlock, but we want to make it a no-op 177 // if the owner GpuDataManagerImpl is null. 178 // This should only be used by GpuDataManagerImplPrivate where 179 // callbacks are called, during which re-entering 180 // GpuDataManagerImpl is possible. 181 class UnlockedSession { 182 public: 183 explicit UnlockedSession(GpuDataManagerImpl* owner) 184 : owner_(owner) { 185 DCHECK(owner_); 186 owner_->lock_.AssertAcquired(); 187 owner_->lock_.Release(); 188 } 189 190 ~UnlockedSession() { 191 DCHECK(owner_); 192 owner_->lock_.Acquire(); 193 } 194 195 private: 196 GpuDataManagerImpl* owner_; 197 DISALLOW_COPY_AND_ASSIGN(UnlockedSession); 198 }; 199 200 GpuDataManagerImpl(); 201 virtual ~GpuDataManagerImpl(); 202 203 mutable base::Lock lock_; 204 scoped_ptr<GpuDataManagerImplPrivate> private_; 205 206 DISALLOW_COPY_AND_ASSIGN(GpuDataManagerImpl); 207}; 208 209} // namespace content 210 211#endif // CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_ 212