Home | History | Annotate | Download | only in gpu
      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 
     25 class GURL;
     26 struct WebPreferences;
     27 
     28 namespace base {
     29 class CommandLine;
     30 }
     31 
     32 namespace content {
     33 
     34 class GpuDataManagerImplPrivate;
     35 
     36 class CONTENT_EXPORT GpuDataManagerImpl
     37     : public NON_EXPORTED_BASE(GpuDataManager) {
     38  public:
     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.
     43   enum DomainGuilt {
     44     DOMAIN_GUILT_KNOWN,
     45     DOMAIN_GUILT_UNKNOWN
     46   };
     47 
     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
     55   };
     56 
     57   // Getter for the singleton. This will return NULL on failure.
     58   static GpuDataManagerImpl* GetInstance();
     59 
     60   // GpuDataManager implementation.
     61   virtual void InitializeForTesting(
     62       const std::string& gpu_blacklist_json,
     63       const gpu::GPUInfo& gpu_info) OVERRIDE;
     64   virtual bool IsFeatureBlacklisted(int feature) const OVERRIDE;
     65   virtual gpu::GPUInfo GetGPUInfo() const OVERRIDE;
     66   virtual void GetGpuProcessHandles(
     67       const GetGpuProcessHandlesCallback& callback) const OVERRIDE;
     68   virtual bool GpuAccessAllowed(std::string* reason) const OVERRIDE;
     69   virtual void RequestCompleteGpuInfoIfNeeded() OVERRIDE;
     70   virtual bool IsCompleteGpuInfoAvailable() const OVERRIDE;
     71   virtual void RequestVideoMemoryUsageStatsUpdate() const OVERRIDE;
     72   virtual bool ShouldUseSwiftShader() const OVERRIDE;
     73   virtual void RegisterSwiftShaderPath(const base::FilePath& path) OVERRIDE;
     74   virtual void AddObserver(GpuDataManagerObserver* observer) OVERRIDE;
     75   virtual void RemoveObserver(GpuDataManagerObserver* observer) OVERRIDE;
     76   virtual void UnblockDomainFrom3DAPIs(const GURL& url) OVERRIDE;
     77   virtual void DisableGpuWatchdog() OVERRIDE;
     78   virtual void SetGLStrings(const std::string& gl_vendor,
     79                             const std::string& gl_renderer,
     80                             const std::string& gl_version) OVERRIDE;
     81   virtual void GetGLStrings(std::string* gl_vendor,
     82                             std::string* gl_renderer,
     83                             std::string* gl_version) OVERRIDE;
     84   virtual void DisableHardwareAcceleration() OVERRIDE;
     85   virtual bool CanUseGpuBrowserCompositor() const OVERRIDE;
     86 
     87   // This collects preliminary GPU info, load GpuBlacklist, and compute the
     88   // preliminary blacklisted features; it should only be called at browser
     89   // startup time in UI thread before the IO restriction is turned on.
     90   void Initialize();
     91 
     92   // Only update if the current GPUInfo is not finalized.  If blacklist is
     93   // loaded, run through blacklist and update blacklisted features.
     94   void UpdateGpuInfo(const gpu::GPUInfo& gpu_info);
     95 
     96   void UpdateVideoMemoryUsageStats(
     97       const GPUVideoMemoryUsageStats& video_memory_usage_stats);
     98 
     99   // Insert disable-feature switches corresponding to preliminary gpu feature
    100   // flags into the renderer process command line.
    101   void AppendRendererCommandLine(base::CommandLine* command_line) const;
    102 
    103   // Insert switches into gpu process command line: kUseGL, etc.
    104   void AppendGpuCommandLine(base::CommandLine* command_line) const;
    105 
    106   // Insert switches into plugin process command line:
    107   // kDisableCoreAnimationPlugins.
    108   void AppendPluginCommandLine(base::CommandLine* command_line) const;
    109 
    110   // Update WebPreferences for renderer based on blacklisting decisions.
    111   void UpdateRendererWebPrefs(WebPreferences* prefs) 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   // Maintenance of domains requiring explicit user permission before
    139   // using client-facing 3D APIs (WebGL, Pepper 3D), either because
    140   // the domain has caused the GPU to reset, or because too many GPU
    141   // resets have been observed globally recently, and system stability
    142   // might be compromised.
    143   //
    144   // The given URL may be a partial URL (including at least the host)
    145   // or a full URL to a page.
    146   //
    147   // Note that the unblocking API must be part of the content API
    148   // because it is called from Chrome side code.
    149   void BlockDomainFrom3DAPIs(const GURL& url, DomainGuilt guilt);
    150   bool Are3DAPIsBlocked(const GURL& url,
    151                         int render_process_id,
    152                         int render_view_id,
    153                         ThreeDAPIType requester);
    154 
    155   // Disables domain blocking for 3D APIs. For use only in tests.
    156   void DisableDomainBlockingFor3DAPIsForTesting();
    157 
    158   void Notify3DAPIBlocked(const GURL& url,
    159                           int render_process_id,
    160                           int render_view_id,
    161                           ThreeDAPIType requester);
    162 
    163   // Get number of features being blacklisted.
    164   size_t GetBlacklistedFeatureCount() const;
    165 
    166   void SetDisplayCount(unsigned int display_count);
    167   unsigned int GetDisplayCount() const;
    168 
    169   // Set the active gpu.
    170   // Return true if it's a different GPU from the previous active one.
    171   bool UpdateActiveGpu(uint32 vendor_id, uint32 device_id);
    172 
    173   // Called when GPU process initialization failed.
    174   void OnGpuProcessInitFailure();
    175 
    176   bool IsDriverBugWorkaroundActive(int feature) const;
    177 
    178  private:
    179   friend class GpuDataManagerImplPrivate;
    180   friend class GpuDataManagerImplPrivateTest;
    181   friend struct DefaultSingletonTraits<GpuDataManagerImpl>;
    182 
    183   // It's similar to AutoUnlock, but we want to make it a no-op
    184   // if the owner GpuDataManagerImpl is null.
    185   // This should only be used by GpuDataManagerImplPrivate where
    186   // callbacks are called, during which re-entering
    187   // GpuDataManagerImpl is possible.
    188   class UnlockedSession {
    189    public:
    190     explicit UnlockedSession(GpuDataManagerImpl* owner)
    191         : owner_(owner) {
    192       DCHECK(owner_);
    193       owner_->lock_.AssertAcquired();
    194       owner_->lock_.Release();
    195     }
    196 
    197     ~UnlockedSession() {
    198       DCHECK(owner_);
    199       owner_->lock_.Acquire();
    200     }
    201 
    202    private:
    203     GpuDataManagerImpl* owner_;
    204     DISALLOW_COPY_AND_ASSIGN(UnlockedSession);
    205   };
    206 
    207   GpuDataManagerImpl();
    208   virtual ~GpuDataManagerImpl();
    209 
    210   mutable base::Lock lock_;
    211   scoped_ptr<GpuDataManagerImplPrivate> private_;
    212 
    213   DISALLOW_COPY_AND_ASSIGN(GpuDataManagerImpl);
    214 };
    215 
    216 }  // namespace content
    217 
    218 #endif  // CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_
    219