| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gl/gpu_switching_manager.h" | 5 #include "ui/gl/gpu_switching_manager.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "ui/gl/gl_switches.h" | 9 #include "ui/gl/gl_switches.h" |
| 10 | 10 |
| 11 #if defined(OS_MACOSX) | |
| 12 #include <OpenGL/OpenGL.h> | |
| 13 #include "base/mac/mac_util.h" | |
| 14 #include "ui/gl/gl_context_cgl.h" | |
| 15 #endif // OS_MACOSX | |
| 16 | |
| 17 namespace ui { | 11 namespace ui { |
| 18 | 12 |
| 19 struct GpuSwitchingManager::PlatformSpecific { | |
| 20 #if defined(OS_MACOSX) | |
| 21 CGLPixelFormatObj discrete_pixel_format; | |
| 22 #endif // OS_MACOSX | |
| 23 }; | |
| 24 | |
| 25 // static | 13 // static |
| 26 GpuSwitchingManager* GpuSwitchingManager::GetInstance() { | 14 GpuSwitchingManager* GpuSwitchingManager::GetInstance() { |
| 27 return Singleton<GpuSwitchingManager>::get(); | 15 return Singleton<GpuSwitchingManager>::get(); |
| 28 } | 16 } |
| 29 | 17 |
| 30 GpuSwitchingManager::GpuSwitchingManager() | 18 GpuSwitchingManager::GpuSwitchingManager() |
| 31 : gpu_switching_option_(gfx::PreferIntegratedGpu), | 19 : gpu_switching_option_(gfx::PreferIntegratedGpu), |
| 32 gpu_switching_option_set_(false), | 20 gpu_switching_option_set_(false), |
| 33 supports_dual_gpus_(false), | 21 supports_dual_gpus_(false), |
| 34 supports_dual_gpus_set_(false), | 22 supports_dual_gpus_set_(false), |
| 35 gpu_count_(0), | 23 gpu_count_(0) { |
| 36 platform_specific_(new PlatformSpecific) { | |
| 37 #if defined(OS_MACOSX) | |
| 38 platform_specific_->discrete_pixel_format = nullptr; | |
| 39 #endif // OS_MACOSX | |
| 40 } | 24 } |
| 41 | 25 |
| 42 GpuSwitchingManager::~GpuSwitchingManager() { | 26 GpuSwitchingManager::~GpuSwitchingManager() { |
| 43 #if defined(OS_MACOSX) | |
| 44 if (platform_specific_->discrete_pixel_format) | |
| 45 CGLReleasePixelFormat(platform_specific_->discrete_pixel_format); | |
| 46 #endif // OS_MACOSX | |
| 47 } | 27 } |
| 48 | 28 |
| 49 void GpuSwitchingManager::ForceUseOfIntegratedGpu() { | 29 void GpuSwitchingManager::ForceUseOfIntegratedGpu() { |
| 50 DCHECK(SupportsDualGpus()); | 30 DCHECK(SupportsDualGpus()); |
| 51 if (gpu_switching_option_set_) { | 31 if (gpu_switching_option_set_) { |
| 52 DCHECK_EQ(gpu_switching_option_, gfx::PreferIntegratedGpu); | 32 DCHECK_EQ(gpu_switching_option_, gfx::PreferIntegratedGpu); |
| 53 } else { | 33 } else { |
| 54 gpu_switching_option_ = gfx::PreferIntegratedGpu; | 34 gpu_switching_option_ = gfx::PreferIntegratedGpu; |
| 55 gpu_switching_option_set_ = true; | 35 gpu_switching_option_set_ = true; |
| 56 } | 36 } |
| 57 } | 37 } |
| 58 | 38 |
| 59 void GpuSwitchingManager::ForceUseOfDiscreteGpu() { | 39 void GpuSwitchingManager::ForceUseOfDiscreteGpu() { |
| 60 DCHECK(SupportsDualGpus()); | 40 DCHECK(SupportsDualGpus()); |
| 61 if (gpu_switching_option_set_) { | 41 if (gpu_switching_option_set_) { |
| 62 DCHECK_EQ(gpu_switching_option_, gfx::PreferDiscreteGpu); | 42 DCHECK_EQ(gpu_switching_option_, gfx::PreferDiscreteGpu); |
| 63 } else { | 43 } else { |
| 64 gpu_switching_option_ = gfx::PreferDiscreteGpu; | 44 gpu_switching_option_ = gfx::PreferDiscreteGpu; |
| 65 gpu_switching_option_set_ = true; | 45 gpu_switching_option_set_ = true; |
| 66 #if defined(OS_MACOSX) | |
| 67 // Create a pixel format that lasts the lifespan of Chrome, so Chrome | |
| 68 // stays on the discrete GPU. | |
| 69 SwitchToDiscreteGpuMac(); | |
| 70 #endif // OS_MACOSX | |
| 71 } | 46 } |
| 72 } | 47 } |
| 73 | 48 |
| 74 bool GpuSwitchingManager::SupportsDualGpus() { | 49 bool GpuSwitchingManager::SupportsDualGpus() { |
| 75 if (!supports_dual_gpus_set_) { | 50 if (!supports_dual_gpus_set_) { |
| 76 const base::CommandLine& command_line = | 51 const base::CommandLine& command_line = |
| 77 *base::CommandLine::ForCurrentProcess(); | 52 *base::CommandLine::ForCurrentProcess(); |
| 78 bool flag = false; | 53 bool flag = false; |
| 79 if (command_line.HasSwitch(switches::kSupportsDualGpus)) { | 54 if (command_line.HasSwitch(switches::kSupportsDualGpus)) { |
| 80 // GPU process, flag is passed down from browser process. | 55 // GPU process, flag is passed down from browser process. |
| 81 std::string flag_string = command_line.GetSwitchValueASCII( | 56 std::string flag_string = command_line.GetSwitchValueASCII( |
| 82 switches::kSupportsDualGpus); | 57 switches::kSupportsDualGpus); |
| 83 if (flag_string == "true") { | 58 if (flag_string == "true") { |
| 84 flag = true; | 59 flag = true; |
| 85 } else if (flag_string == "false") { | 60 } else if (flag_string == "false") { |
| 86 flag = false; | 61 flag = false; |
| 87 } else { | 62 } else { |
| 88 NOTIMPLEMENTED(); | 63 NOTIMPLEMENTED(); |
| 89 } | 64 } |
| 90 } else { | |
| 91 // Browser process. | |
| 92 // We only compute this flag in the browser process. | |
| 93 #if defined(OS_MACOSX) | |
| 94 flag = (gpu_count_ == 2); | |
| 95 if (flag && command_line.HasSwitch(switches::kUseGL) && | |
| 96 command_line.GetSwitchValueASCII(switches::kUseGL) != | |
| 97 gfx::kGLImplementationDesktopName) | |
| 98 flag = false; | |
| 99 | |
| 100 if (flag && !base::mac::IsOSLionOrLater()) | |
| 101 flag = false; | |
| 102 #endif // OS_MACOSX | |
| 103 } | 65 } |
| 104 supports_dual_gpus_ = flag; | 66 supports_dual_gpus_ = flag; |
| 105 supports_dual_gpus_set_ = true; | 67 supports_dual_gpus_set_ = true; |
| 106 } | 68 } |
| 107 return supports_dual_gpus_; | 69 return supports_dual_gpus_; |
| 108 } | 70 } |
| 109 | 71 |
| 110 void GpuSwitchingManager::SetGpuCount(size_t gpu_count) { | 72 void GpuSwitchingManager::SetGpuCount(size_t gpu_count) { |
| 111 gpu_count_ = gpu_count; | 73 gpu_count_ = gpu_count; |
| 112 } | 74 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 123 FOR_EACH_OBSERVER(GpuSwitchingObserver, observer_list_, OnGpuSwitched()); | 85 FOR_EACH_OBSERVER(GpuSwitchingObserver, observer_list_, OnGpuSwitched()); |
| 124 } | 86 } |
| 125 | 87 |
| 126 gfx::GpuPreference GpuSwitchingManager::AdjustGpuPreference( | 88 gfx::GpuPreference GpuSwitchingManager::AdjustGpuPreference( |
| 127 gfx::GpuPreference gpu_preference) { | 89 gfx::GpuPreference gpu_preference) { |
| 128 if (!gpu_switching_option_set_) | 90 if (!gpu_switching_option_set_) |
| 129 return gpu_preference; | 91 return gpu_preference; |
| 130 return gpu_switching_option_; | 92 return gpu_switching_option_; |
| 131 } | 93 } |
| 132 | 94 |
| 133 #if defined(OS_MACOSX) | |
| 134 void GpuSwitchingManager::SwitchToDiscreteGpuMac() { | |
| 135 if (platform_specific_->discrete_pixel_format) | |
| 136 return; | |
| 137 CGLPixelFormatAttribute attribs[1]; | |
| 138 attribs[0] = static_cast<CGLPixelFormatAttribute>(0); | |
| 139 GLint num_pixel_formats = 0; | |
| 140 CGLChoosePixelFormat(attribs, &platform_specific_->discrete_pixel_format, | |
| 141 &num_pixel_formats); | |
| 142 } | |
| 143 #endif // OS_MACOSX | |
| 144 | |
| 145 } // namespace ui | 95 } // namespace ui |
| OLD | NEW |