12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/gpu/gpu_mode_manager.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/metrics/histogram.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_registry_simple.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/browser_process.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/pref_names.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/gpu_data_manager.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/browser/user_metrics.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using base::UserMetricsAction;
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace {
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool GetPreviousGpuModePref() {
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PrefService* service = g_browser_process->local_state();
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(service);
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return service->GetBoolean(prefs::kHardwareAccelerationModePrevious);
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void SetPreviousGpuModePref(bool enabled) {
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PrefService* service = g_browser_process->local_state();
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK(service);
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  service->SetBoolean(prefs::kHardwareAccelerationModePrevious, enabled);
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace anonymous
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void GpuModeManager::RegisterPrefs(PrefRegistrySimple* registry) {
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  registry->RegisterBooleanPref(
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      prefs::kHardwareAccelerationModeEnabled, true);
38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterBooleanPref(
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      prefs::kHardwareAccelerationModePrevious, true);
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GpuModeManager::GpuModeManager()
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : initial_gpu_mode_pref_(true) {
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (g_browser_process->local_state()) {  // Skip for unit tests
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    pref_registrar_.Init(g_browser_process->local_state());
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Do nothing when the pref changes. It takes effect after
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // chrome restarts.
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    pref_registrar_.Add(
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        prefs::kHardwareAccelerationModeEnabled,
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        base::Bind(&base::DoNothing));
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    initial_gpu_mode_pref_ = IsGpuModePrefEnabled();
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    bool previous_gpu_mode_pref = GetPreviousGpuModePref();
54c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SetPreviousGpuModePref(initial_gpu_mode_pref_);
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    UMA_HISTOGRAM_BOOLEAN("GPU.HardwareAccelerationModeEnabled",
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                          initial_gpu_mode_pref_);
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (previous_gpu_mode_pref && !initial_gpu_mode_pref_)
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::RecordAction(UserMetricsAction("GpuAccelerationDisabled"));
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!previous_gpu_mode_pref && initial_gpu_mode_pref_)
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::RecordAction(UserMetricsAction("GpuAccelerationEnabled"));
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!initial_gpu_mode_pref_) {
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      content::GpuDataManager* gpu_data_manager =
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          content::GpuDataManager::GetInstance();
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DCHECK(gpu_data_manager);
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gpu_data_manager->DisableHardwareAcceleration();
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)GpuModeManager::~GpuModeManager() {
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool GpuModeManager::initial_gpu_mode_pref() const {
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return initial_gpu_mode_pref_;
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// static
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool GpuModeManager::IsGpuModePrefEnabled() {
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PrefService* service = g_browser_process->local_state();
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(service);
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return service->GetBoolean(
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      prefs::kHardwareAccelerationModeEnabled);
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
87