OLD | NEW |
| (Empty) |
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 "base/basictypes.h" | |
6 #include "base/command_line.h" | |
7 #include "base/logging.h" | |
8 #include "base/mac/mac_util.h" | |
9 #include "base/memory/scoped_generic_obj.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "third_party/mesa/MesaLib/include/GL/osmesa.h" | |
12 #include "ui/gfx/gl/gl_bindings.h" | |
13 #include "ui/gfx/gl/gl_context_cgl.h" | |
14 #include "ui/gfx/gl/gl_context_osmesa.h" | |
15 #include "ui/gfx/gl/gl_context_stub.h" | |
16 #include "ui/gfx/gl/gl_implementation.h" | |
17 #include "ui/gfx/gl/gl_surface.h" | |
18 #include "ui/gfx/gl/gl_switches.h" | |
19 | |
20 #if defined(USE_AURA) | |
21 #include "ui/gfx/gl/gl_context_nsview.h" | |
22 #endif | |
23 | |
24 namespace { | |
25 | |
26 // ScopedGenericObj functor for CGLDestroyRendererInfo(). | |
27 class ScopedDestroyRendererInfo { | |
28 public: | |
29 void operator()(CGLRendererInfoObj x) const { | |
30 CGLDestroyRendererInfo(x); | |
31 } | |
32 }; | |
33 | |
34 } // namespace | |
35 | |
36 namespace gfx { | |
37 | |
38 class GLShareGroup; | |
39 | |
40 scoped_refptr<GLContext> GLContext::CreateGLContext( | |
41 GLShareGroup* share_group, | |
42 GLSurface* compatible_surface, | |
43 GpuPreference gpu_preference) { | |
44 switch (GetGLImplementation()) { | |
45 case kGLImplementationDesktopGL: | |
46 case kGLImplementationAppleGL: { | |
47 scoped_refptr<GLContext> context; | |
48 #if defined(USE_AURA) | |
49 if (compatible_surface->IsOffscreen()) | |
50 context = new GLContextCGL(share_group); | |
51 else | |
52 context = new GLContextNSView(share_group); | |
53 #else | |
54 context = new GLContextCGL(share_group); | |
55 #endif // USE_AURA | |
56 if (!context->Initialize(compatible_surface, gpu_preference)) | |
57 return NULL; | |
58 | |
59 return context; | |
60 } | |
61 case kGLImplementationOSMesaGL: { | |
62 scoped_refptr<GLContext> context(new GLContextOSMesa(share_group)); | |
63 if (!context->Initialize(compatible_surface, gpu_preference)) | |
64 return NULL; | |
65 | |
66 return context; | |
67 } | |
68 case kGLImplementationMockGL: | |
69 return new GLContextStub; | |
70 default: | |
71 NOTREACHED(); | |
72 return NULL; | |
73 } | |
74 } | |
75 | |
76 bool GLContext::SupportsDualGpus() { | |
77 // We need to know the GL implementation in order to correctly | |
78 // answer whether dual GPUs are supported. This introduces an | |
79 // initialization cycle with GLSurface::InitializeOneOff() which we | |
80 // need to break. | |
81 static bool initialized = false; | |
82 static bool initializing = false; | |
83 static bool supports_dual_gpus = false; | |
84 | |
85 if (initialized) { | |
86 return supports_dual_gpus; | |
87 } else { | |
88 if (!initializing) { | |
89 initializing = true; | |
90 if (!GLSurface::InitializeOneOff()) { | |
91 return false; | |
92 } | |
93 } | |
94 initialized = true; | |
95 } | |
96 | |
97 if (!base::mac::IsOSLionOrLater()) { | |
98 return false; | |
99 } | |
100 | |
101 if (GetGLImplementation() != kGLImplementationDesktopGL) { | |
102 return false; | |
103 } | |
104 | |
105 // Enumerate all hardware-accelerated renderers. If we find one | |
106 // online and one offline, assume we're on a dual-GPU system. | |
107 GLuint display_mask = static_cast<GLuint>(-1); | |
108 CGLRendererInfoObj renderer_info = NULL; | |
109 GLint num_renderers = 0; | |
110 | |
111 bool found_online = false; | |
112 bool found_offline = false; | |
113 | |
114 if (CGLQueryRendererInfo(display_mask, | |
115 &renderer_info, | |
116 &num_renderers) != kCGLNoError) { | |
117 return false; | |
118 } | |
119 | |
120 ScopedGenericObj<CGLRendererInfoObj, ScopedDestroyRendererInfo> | |
121 scoper(renderer_info); | |
122 | |
123 for (GLint i = 0; i < num_renderers; ++i) { | |
124 GLint accelerated = 0; | |
125 if (CGLDescribeRenderer(renderer_info, | |
126 i, | |
127 kCGLRPAccelerated, | |
128 &accelerated) != kCGLNoError) { | |
129 return false; | |
130 } | |
131 | |
132 if (!accelerated) | |
133 continue; | |
134 | |
135 GLint online = 0; | |
136 if (CGLDescribeRenderer(renderer_info, | |
137 i, | |
138 kCGLRPOnline, | |
139 &online) != kCGLNoError) { | |
140 return false; | |
141 } | |
142 | |
143 if (online) { | |
144 found_online = true; | |
145 } else { | |
146 found_offline = true; | |
147 } | |
148 } | |
149 | |
150 if (found_online && found_offline) { | |
151 // Only switch GPUs dynamically on recent MacBook Pro models. | |
152 // Otherwise, keep the system on the discrete GPU for the lifetime | |
153 // of the browser process, since switching back and forth causes | |
154 // system stability issues. http://crbug.com/113703 | |
155 std::string model; | |
156 int32 model_major, model_minor; | |
157 if (!base::mac::ParseModelIdentifier(base::mac::GetModelIdentifier(), | |
158 &model, &model_major, &model_minor)) { | |
159 return false; | |
160 } | |
161 | |
162 const int kMacBookProFirstDualAMDIntelGPUModel = 8; | |
163 | |
164 bool forcibly_disable = | |
165 ((model == "MacBookPro") && | |
166 (model_major < kMacBookProFirstDualAMDIntelGPUModel)) || | |
167 CommandLine::ForCurrentProcess()->HasSwitch( | |
168 switches::kDisableGpuSwitching) || | |
169 // http://crbug.com/127713 : disable dynamic GPU switching on | |
170 // 10.8 until system stability issues are resolved by Apple. | |
171 base::mac::IsOSMountainLion(); | |
172 | |
173 if (forcibly_disable) { | |
174 GLContextCGL::ForceUseOfDiscreteGPU(); | |
175 return false; | |
176 } | |
177 | |
178 supports_dual_gpus = true; | |
179 } | |
180 | |
181 return supports_dual_gpus; | |
182 } | |
183 | |
184 } // namespace gfx | |
OLD | NEW |