blob: 620de1867c2bdd57ac4d5733f86eaeae0adf36bf [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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 Robinson646469d2014-10-03 15:33:28 -070011namespace ui {
12
James Robinson646469d2014-10-03 15:33:28 -070013// static
14GpuSwitchingManager* GpuSwitchingManager::GetInstance() {
15 return Singleton<GpuSwitchingManager>::get();
16}
17
18GpuSwitchingManager::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 Robinson60709e12015-04-03 16:59:15 -070023 gpu_count_(0) {
James Robinson646469d2014-10-03 15:33:28 -070024}
25
26GpuSwitchingManager::~GpuSwitchingManager() {
James Robinson646469d2014-10-03 15:33:28 -070027}
28
29void 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
39void 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 Robinson646469d2014-10-03 15:33:28 -070046 }
47}
48
49bool GpuSwitchingManager::SupportsDualGpus() {
50 if (!supports_dual_gpus_set_) {
James Robinson9127e722014-12-29 14:41:55 -080051 const base::CommandLine& command_line =
52 *base::CommandLine::ForCurrentProcess();
James Robinson646469d2014-10-03 15:33:28 -070053 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 Robinson646469d2014-10-03 15:33:28 -070065 }
66 supports_dual_gpus_ = flag;
67 supports_dual_gpus_set_ = true;
68 }
69 return supports_dual_gpus_;
70}
71
72void GpuSwitchingManager::SetGpuCount(size_t gpu_count) {
73 gpu_count_ = gpu_count;
74}
75
James Robinson61650152014-10-26 23:24:55 -070076void GpuSwitchingManager::AddObserver(GpuSwitchingObserver* observer) {
77 observer_list_.AddObserver(observer);
78}
79
80void GpuSwitchingManager::RemoveObserver(GpuSwitchingObserver* observer) {
81 observer_list_.RemoveObserver(observer);
82}
83
84void GpuSwitchingManager::NotifyGpuSwitched() {
85 FOR_EACH_OBSERVER(GpuSwitchingObserver, observer_list_, OnGpuSwitched());
86}
87
James Robinson646469d2014-10-03 15:33:28 -070088gfx::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 Robinson646469d2014-10-03 15:33:28 -070095} // namespace ui