147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Copyright (c) 2013 The Chromium Authors. All rights reserved.
247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// Use of this source code is governed by a BSD-style license that can be
347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt// found in the LICENSE file.
447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#ifndef CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_
647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#define CONTENT_BROWSER_GPU_GPU_DATA_MANAGER_IMPL_H_
747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include <string>
947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
1047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/compiler_specific.h"
1147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/files/file_path.h"
1247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/gtest_prod_util.h"
1347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/logging.h"
1447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/memory/scoped_ptr.h"
1547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/memory/singleton.h"
1647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/process/kill.h"
1747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/synchronization/lock.h"
1847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/time/time.h"
1947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "base/values.h"
2047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "content/public/browser/gpu_data_manager.h"
2147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "content/public/common/gpu_memory_stats.h"
2247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "content/public/common/three_d_api_types.h"
2347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt#include "gpu/config/gpu_info.h"
2447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
2547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltclass GURL;
2647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
2747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltnamespace base {
2847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltclass CommandLine;
2947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt}
3047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltnamespace content {
3247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltclass GpuDataManagerImplPrivate;
3447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltstruct WebPreferences;
3547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
3647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwaltclass CONTENT_EXPORT GpuDataManagerImpl
3747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    : public NON_EXPORTED_BASE(GpuDataManager) {
3847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt public:
3947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Indicates the guilt level of a domain which caused a GPU reset.
4047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // If a domain is 100% known to be guilty of resetting the GPU, then
4147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // it will generally not cause other domains' use of 3D APIs to be
4247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // blocked, unless system stability would be compromised.
4347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  enum DomainGuilt {
4447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    DOMAIN_GUILT_KNOWN,
4547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    DOMAIN_GUILT_UNKNOWN
4647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  };
4747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
4847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Indicates the reason that access to a given client API (like
4947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // WebGL or Pepper 3D) was blocked or not. This state is distinct
5047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // from blacklisting of an entire feature.
5147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  enum DomainBlockStatus {
5247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    DOMAIN_BLOCK_STATUS_BLOCKED,
5347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    DOMAIN_BLOCK_STATUS_ALL_DOMAINS_BLOCKED,
5447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt    DOMAIN_BLOCK_STATUS_NOT_BLOCKED
5547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  };
5647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
5747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Getter for the singleton. This will return NULL on failure.
5847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  static GpuDataManagerImpl* GetInstance();
5947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
6047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // GpuDataManager implementation.
6147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void InitializeForTesting(
6247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt      const std::string& gpu_blacklist_json,
6347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt      const gpu::GPUInfo& gpu_info) OVERRIDE;
6447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual bool IsFeatureBlacklisted(int feature) const OVERRIDE;
6547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual gpu::GPUInfo GetGPUInfo() const OVERRIDE;
6647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void GetGpuProcessHandles(
6747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt      const GetGpuProcessHandlesCallback& callback) const OVERRIDE;
6847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual bool GpuAccessAllowed(std::string* reason) const OVERRIDE;
6947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void RequestCompleteGpuInfoIfNeeded() OVERRIDE;
7047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual bool IsEssentialGpuInfoAvailable() const OVERRIDE;
7147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual bool IsCompleteGpuInfoAvailable() const OVERRIDE;
7247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void RequestVideoMemoryUsageStatsUpdate() const OVERRIDE;
7347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual bool ShouldUseSwiftShader() const OVERRIDE;
7447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void RegisterSwiftShaderPath(const base::FilePath& path) OVERRIDE;
7547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual bool ShouldUseWarp() const OVERRIDE;
7647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void AddObserver(GpuDataManagerObserver* observer) OVERRIDE;
7747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void RemoveObserver(GpuDataManagerObserver* observer) OVERRIDE;
7847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void UnblockDomainFrom3DAPIs(const GURL& url) OVERRIDE;
7947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void DisableGpuWatchdog() OVERRIDE;
8047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void SetGLStrings(const std::string& gl_vendor,
8147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                            const std::string& gl_renderer,
8247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                            const std::string& gl_version) OVERRIDE;
8347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void GetGLStrings(std::string* gl_vendor,
8447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                            std::string* gl_renderer,
8547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                            std::string* gl_version) OVERRIDE;
8647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual void DisableHardwareAcceleration() OVERRIDE;
8747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  virtual bool CanUseGpuBrowserCompositor() const OVERRIDE;
8847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
8947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // This collects preliminary GPU info, load GpuBlacklist, and compute the
9047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // preliminary blacklisted features; it should only be called at browser
9147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // startup time in UI thread before the IO restriction is turned on.
9247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void Initialize();
9347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
9447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Only update if the current GPUInfo is not finalized.  If blacklist is
9547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // loaded, run through blacklist and update blacklisted features.
9647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void UpdateGpuInfo(const gpu::GPUInfo& gpu_info);
9747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
9847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void UpdateVideoMemoryUsageStats(
9947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt      const GPUVideoMemoryUsageStats& video_memory_usage_stats);
10047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
10147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Insert disable-feature switches corresponding to preliminary gpu feature
10247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // flags into the renderer process command line.
10347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void AppendRendererCommandLine(base::CommandLine* command_line) const;
10447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
10547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Insert switches into gpu process command line: kUseGL, etc.
10647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void AppendGpuCommandLine(base::CommandLine* command_line) const;
10747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
10847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Insert switches into plugin process command line:
10947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // kDisableCoreAnimationPlugins.
11047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void AppendPluginCommandLine(base::CommandLine* command_line) const;
11147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
11247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Update WebPreferences for renderer based on blacklisting decisions.
11347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void UpdateRendererWebPrefs(WebPreferences* prefs) const;
11447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
11547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  std::string GetBlacklistVersion() const;
11647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  std::string GetDriverBugListVersion() const;
11747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
11847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Returns the reasons for the latest run of blacklisting decisions.
11947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // For the structure of returned value, see documentation for
12047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // GpuBlacklist::GetBlacklistedReasons().
12147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void GetBlacklistReasons(base::ListValue* reasons) const;
12247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
12347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Returns the workarounds that are applied to the current system as
12447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // a list of strings.
12547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void GetDriverBugWorkarounds(base::ListValue* workarounds) const;
12647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
12747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void AddLogMessage(int level,
12847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                     const std::string& header,
12947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                     const std::string& message);
13047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
13147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void ProcessCrashed(base::TerminationStatus exit_code);
13247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
13347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Returns a new copy of the ListValue.  Caller is responsible to release
13447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // the returned value.
13547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  base::ListValue* GetLogMessages() const;
13647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
13747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Called when switching gpu.
13847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void HandleGpuSwitch();
13947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
14047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Maintenance of domains requiring explicit user permission before
14147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // using client-facing 3D APIs (WebGL, Pepper 3D), either because
14247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // the domain has caused the GPU to reset, or because too many GPU
14347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // resets have been observed globally recently, and system stability
14447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // might be compromised.
14547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  //
14647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // The given URL may be a partial URL (including at least the host)
14747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // or a full URL to a page.
14847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  //
14947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Note that the unblocking API must be part of the content API
15047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // because it is called from Chrome side code.
15147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void BlockDomainFrom3DAPIs(const GURL& url, DomainGuilt guilt);
15247e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  bool Are3DAPIsBlocked(const GURL& url,
15347e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                        int render_process_id,
15447e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                        int render_view_id,
15547e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                        ThreeDAPIType requester);
15647e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
15747e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  // Disables domain blocking for 3D APIs. For use only in tests.
15847e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void DisableDomainBlockingFor3DAPIsForTesting();
15947e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt
16047e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt  void Notify3DAPIBlocked(const GURL& url,
16147e4cebad7397422144bb03a21f3f7682c062c4aRobert Greenwalt                          int render_process_id,
162                          int render_view_id,
163                          ThreeDAPIType requester);
164
165  // Get number of features being blacklisted.
166  size_t GetBlacklistedFeatureCount() const;
167
168  void SetDisplayCount(unsigned int display_count);
169  unsigned int GetDisplayCount() const;
170
171  // Set the active gpu.
172  // Return true if it's a different GPU from the previous active one.
173  bool UpdateActiveGpu(uint32 vendor_id, uint32 device_id);
174
175  // Called when GPU process initialization failed.
176  void OnGpuProcessInitFailure();
177
178  bool IsDriverBugWorkaroundActive(int feature) const;
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