James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #include "ui/gl/gpu_switching_manager.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "ui/gl/gl_switches.h" |
| 10 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 11 | namespace ui { |
| 12 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 13 | // static |
| 14 | GpuSwitchingManager* GpuSwitchingManager::GetInstance() { |
| 15 | return Singleton<GpuSwitchingManager>::get(); |
| 16 | } |
| 17 | |
| 18 | GpuSwitchingManager::GpuSwitchingManager() |
| 19 | : gpu_switching_option_(gfx::PreferIntegratedGpu), |
| 20 | gpu_switching_option_set_(false), |
| 21 | supports_dual_gpus_(false), |
| 22 | supports_dual_gpus_set_(false), |
James Robinson | 60709e1 | 2015-04-03 16:59:15 -0700 | [diff] [blame] | 23 | gpu_count_(0) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | GpuSwitchingManager::~GpuSwitchingManager() { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void GpuSwitchingManager::ForceUseOfIntegratedGpu() { |
| 30 | DCHECK(SupportsDualGpus()); |
| 31 | if (gpu_switching_option_set_) { |
| 32 | DCHECK_EQ(gpu_switching_option_, gfx::PreferIntegratedGpu); |
| 33 | } else { |
| 34 | gpu_switching_option_ = gfx::PreferIntegratedGpu; |
| 35 | gpu_switching_option_set_ = true; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void GpuSwitchingManager::ForceUseOfDiscreteGpu() { |
| 40 | DCHECK(SupportsDualGpus()); |
| 41 | if (gpu_switching_option_set_) { |
| 42 | DCHECK_EQ(gpu_switching_option_, gfx::PreferDiscreteGpu); |
| 43 | } else { |
| 44 | gpu_switching_option_ = gfx::PreferDiscreteGpu; |
| 45 | gpu_switching_option_set_ = true; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | bool GpuSwitchingManager::SupportsDualGpus() { |
| 50 | if (!supports_dual_gpus_set_) { |
James Robinson | 9127e72 | 2014-12-29 14:41:55 -0800 | [diff] [blame] | 51 | const base::CommandLine& command_line = |
| 52 | *base::CommandLine::ForCurrentProcess(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 53 | bool flag = false; |
| 54 | if (command_line.HasSwitch(switches::kSupportsDualGpus)) { |
| 55 | // GPU process, flag is passed down from browser process. |
| 56 | std::string flag_string = command_line.GetSwitchValueASCII( |
| 57 | switches::kSupportsDualGpus); |
| 58 | if (flag_string == "true") { |
| 59 | flag = true; |
| 60 | } else if (flag_string == "false") { |
| 61 | flag = false; |
| 62 | } else { |
| 63 | NOTIMPLEMENTED(); |
| 64 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 65 | } |
| 66 | supports_dual_gpus_ = flag; |
| 67 | supports_dual_gpus_set_ = true; |
| 68 | } |
| 69 | return supports_dual_gpus_; |
| 70 | } |
| 71 | |
| 72 | void GpuSwitchingManager::SetGpuCount(size_t gpu_count) { |
| 73 | gpu_count_ = gpu_count; |
| 74 | } |
| 75 | |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 76 | void GpuSwitchingManager::AddObserver(GpuSwitchingObserver* observer) { |
| 77 | observer_list_.AddObserver(observer); |
| 78 | } |
| 79 | |
| 80 | void GpuSwitchingManager::RemoveObserver(GpuSwitchingObserver* observer) { |
| 81 | observer_list_.RemoveObserver(observer); |
| 82 | } |
| 83 | |
| 84 | void GpuSwitchingManager::NotifyGpuSwitched() { |
| 85 | FOR_EACH_OBSERVER(GpuSwitchingObserver, observer_list_, OnGpuSwitched()); |
| 86 | } |
| 87 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 88 | gfx::GpuPreference GpuSwitchingManager::AdjustGpuPreference( |
| 89 | gfx::GpuPreference gpu_preference) { |
| 90 | if (!gpu_switching_option_set_) |
| 91 | return gpu_preference; |
| 92 | return gpu_switching_option_; |
| 93 | } |
| 94 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 95 | } // namespace ui |