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 #include "gpu/config/gpu_switching_option.h" 25 26 class CommandLine; 27 class GURL; 28 struct WebPreferences; 29 30 namespace content { 31 32 class GpuDataManagerImplPrivate; 33 34 class CONTENT_EXPORT GpuDataManagerImpl 35 : public NON_EXPORTED_BASE(GpuDataManager) { 36 public: 37 // Indicates the guilt level of a domain which caused a GPU reset. 38 // If a domain is 100% known to be guilty of resetting the GPU, then 39 // it will generally not cause other domains' use of 3D APIs to be 40 // blocked, unless system stability would be compromised. 41 enum DomainGuilt { 42 DOMAIN_GUILT_KNOWN, 43 DOMAIN_GUILT_UNKNOWN 44 }; 45 46 // Indicates the reason that access to a given client API (like 47 // WebGL or Pepper 3D) was blocked or not. This state is distinct 48 // from blacklisting of an entire feature. 49 enum DomainBlockStatus { 50 DOMAIN_BLOCK_STATUS_BLOCKED, 51 DOMAIN_BLOCK_STATUS_ALL_DOMAINS_BLOCKED, 52 DOMAIN_BLOCK_STATUS_NOT_BLOCKED 53 }; 54 55 // Getter for the singleton. This will return NULL on failure. 56 static GpuDataManagerImpl* GetInstance(); 57 58 // GpuDataManager implementation. 59 virtual void InitializeForTesting( 60 const std::string& gpu_blacklist_json, 61 const gpu::GPUInfo& gpu_info) OVERRIDE; 62 virtual bool IsFeatureBlacklisted(int feature) const OVERRIDE; 63 virtual gpu::GPUInfo GetGPUInfo() const OVERRIDE; 64 virtual void GetGpuProcessHandles( 65 const GetGpuProcessHandlesCallback& callback) const OVERRIDE; 66 virtual bool GpuAccessAllowed(std::string* reason) const OVERRIDE; 67 virtual void RequestCompleteGpuInfoIfNeeded() OVERRIDE; 68 virtual bool IsCompleteGpuInfoAvailable() const OVERRIDE; 69 virtual void RequestVideoMemoryUsageStatsUpdate() const OVERRIDE; 70 virtual bool ShouldUseSwiftShader() const OVERRIDE; 71 virtual void RegisterSwiftShaderPath(const base::FilePath& path) OVERRIDE; 72 virtual void AddObserver(GpuDataManagerObserver* observer) OVERRIDE; 73 virtual void RemoveObserver(GpuDataManagerObserver* observer) OVERRIDE; 74 virtual void UnblockDomainFrom3DAPIs(const GURL& url) OVERRIDE; 75 virtual void DisableGpuWatchdog() OVERRIDE; 76 virtual void SetGLStrings(const std::string& gl_vendor, 77 const std::string& gl_renderer, 78 const std::string& gl_version) OVERRIDE; 79 virtual void GetGLStrings(std::string* gl_vendor, 80 std::string* gl_renderer, 81 std::string* gl_version) OVERRIDE; 82 virtual void DisableHardwareAcceleration() 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, 101 // kDisableGLMultisampling. 102 void AppendGpuCommandLine(CommandLine* command_line) const; 103 104 // Insert switches into plugin process command line: 105 // kDisableCoreAnimationPlugins. 106 void AppendPluginCommandLine(CommandLine* command_line) const; 107 108 // Update WebPreferences for renderer based on blacklisting decisions. 109 void UpdateRendererWebPrefs(WebPreferences* prefs) const; 110 111 gpu::GpuSwitchingOption GetGpuSwitchingOption() const; 112 113 std::string GetBlacklistVersion() const; 114 std::string GetDriverBugListVersion() const; 115 116 // Returns the reasons for the latest run of blacklisting decisions. 117 // For the structure of returned value, see documentation for 118 // GpuBlacklist::GetBlacklistedReasons(). 119 void GetBlacklistReasons(base::ListValue* reasons) const; 120 121 // Returns the workarounds that are applied to the current system as 122 // a list of strings. 123 void GetDriverBugWorkarounds(base::ListValue* workarounds) const; 124 125 void AddLogMessage(int level, 126 const std::string& header, 127 const std::string& message); 128 129 void ProcessCrashed(base::TerminationStatus exit_code); 130 131 // Returns a new copy of the ListValue. Caller is responsible to release 132 // the returned value. 133 base::ListValue* GetLogMessages() const; 134 135 // Called when switching gpu. 136 void HandleGpuSwitch(); 137 138 #if defined(OS_WIN) 139 // Is the GPU process using the accelerated surface to present, instead of 140 // presenting by itself. 141 bool IsUsingAcceleratedSurface() const; 142 #endif 143 144 bool CanUseGpuBrowserCompositor() const; 145 146 // Maintenance of domains requiring explicit user permission before 147 // using client-facing 3D APIs (WebGL, Pepper 3D), either because 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, 159 int render_process_id, 160 int render_view_id, 161 ThreeDAPIType requester); 162 163 // Disables domain blocking for 3D APIs. For use only in tests. 164 void DisableDomainBlockingFor3DAPIsForTesting(); 165 166 void Notify3DAPIBlocked(const GURL& url, 167 int render_process_id, 168 int render_view_id, 169 ThreeDAPIType requester); 170 171 // Get number of features being blacklisted. 172 size_t GetBlacklistedFeatureCount() const; 173 174 void SetDisplayCount(unsigned int display_count); 175 unsigned int GetDisplayCount() const; 176 177 // Called when GPU process initialization failed. 178 void OnGpuProcessInitFailure(); 179 180 private: 181 friend class GpuDataManagerImplPrivate; 182 friend class GpuDataManagerImplPrivateTest; 183 friend struct DefaultSingletonTraits<GpuDataManagerImpl>; 184 185 // It's similar to AutoUnlock, but we want to make it a no-op 186 // if the owner GpuDataManagerImpl is null. 187 // This should only be used by GpuDataManagerImplPrivate where 188 // callbacks are called, during which re-entering 189 // GpuDataManagerImpl is possible. 190 class UnlockedSession { 191 public: 192 explicit UnlockedSession(GpuDataManagerImpl* owner) 193 : owner_(owner) { 194 DCHECK(owner_); 195 owner_->lock_.AssertAcquired(); 196 owner_->lock_.Release(); 197 } 198 199 ~UnlockedSession() { 200 DCHECK(owner_); 201 owner_->lock_.Acquire(); 202 } 203 204 private: 205 GpuDataManagerImpl* owner_; 206 DISALLOW_COPY_AND_ASSIGN(UnlockedSession); 207 }; 208 209 GpuDataManagerImpl(); 210 virtual ~GpuDataManagerImpl(); 211 212 mutable base::Lock lock_; 213 scoped_ptr<GpuDataManagerImplPrivate> private_; 214 215 DISALLOW_COPY_AND_ASSIGN(GpuDataManagerImpl); 216 }; 217 218 } // namespace content 219 220 #endif // CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_ 221