Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: ui/gfx/gl/gl_context_mac.cc

Issue 8233027: Support dynamic switching between integrated and discrete GPUs on Mac OS X. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/mac/mac_util.h"
8 #include "base/mac/scoped_generic_obj.h"
7 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
8 #include "third_party/mesa/MesaLib/include/GL/osmesa.h" 10 #include "third_party/mesa/MesaLib/include/GL/osmesa.h"
9 #include "ui/gfx/gl/gl_bindings.h" 11 #include "ui/gfx/gl/gl_bindings.h"
10 #include "ui/gfx/gl/gl_context_cgl.h" 12 #include "ui/gfx/gl/gl_context_cgl.h"
11 #include "ui/gfx/gl/gl_context_osmesa.h" 13 #include "ui/gfx/gl/gl_context_osmesa.h"
12 #include "ui/gfx/gl/gl_context_stub.h" 14 #include "ui/gfx/gl/gl_context_stub.h"
13 #include "ui/gfx/gl/gl_implementation.h" 15 #include "ui/gfx/gl/gl_implementation.h"
14 #include "ui/gfx/gl/gl_surface_cgl.h" 16 #include "ui/gfx/gl/gl_surface_cgl.h"
15 #include "ui/gfx/gl/gl_surface_osmesa.h" 17 #include "ui/gfx/gl/gl_surface_osmesa.h"
16 18
19 namespace {
20
21 // ScopedGenericObj functor for CGLDestroyRendererInfo().
22 class ScopedDestroyRendererInfo {
23 public:
24 void operator()(CGLRendererInfoObj x) const {
25 CGLDestroyRendererInfo(x);
26 }
27 };
28
29 } // anonymous namespace
Mark Mentovai 2011/10/13 21:13:01 I thought you said you switched this to } // name
Ken Russell (switch to Gerrit) 2011/10/13 21:43:48 Sorry, I must have accidentally uploaded the last
30
17 namespace gfx { 31 namespace gfx {
18 32
19 class GLShareGroup; 33 class GLShareGroup;
20 34
21 scoped_refptr<GLContext> GLContext::CreateGLContext( 35 scoped_refptr<GLContext> GLContext::CreateGLContext(
22 GLShareGroup* share_group, 36 GLShareGroup* share_group,
23 GLSurface* compatible_surface) { 37 GLSurface* compatible_surface,
38 GpuPreference gpu_preference) {
24 switch (GetGLImplementation()) { 39 switch (GetGLImplementation()) {
25 case kGLImplementationDesktopGL: { 40 case kGLImplementationDesktopGL: {
26 scoped_refptr<GLContext> context(new GLContextCGL(share_group)); 41 scoped_refptr<GLContext> context(new GLContextCGL(share_group));
27 if (!context->Initialize(compatible_surface)) 42 if (!context->Initialize(compatible_surface, gpu_preference))
28 return NULL; 43 return NULL;
29 44
30 return context; 45 return context;
31 } 46 }
32 case kGLImplementationOSMesaGL: { 47 case kGLImplementationOSMesaGL: {
33 scoped_refptr<GLContext> context(new GLContextOSMesa(share_group)); 48 scoped_refptr<GLContext> context(new GLContextOSMesa(share_group));
34 if (!context->Initialize(compatible_surface)) 49 if (!context->Initialize(compatible_surface, gpu_preference))
35 return NULL; 50 return NULL;
36 51
37 return context; 52 return context;
38 } 53 }
39 case kGLImplementationMockGL: 54 case kGLImplementationMockGL:
40 return new GLContextStub; 55 return new GLContextStub;
41 default: 56 default:
42 NOTREACHED(); 57 NOTREACHED();
43 return NULL; 58 return NULL;
44 } 59 }
45 } 60 }
46 61
62 bool GLContext::SupportsDualGpus() {
63 // We need to know the GL implementation in order to correctly
64 // answer whether dual GPUs are supported. This introduces an
65 // initialization cycle with GLSurface::InitializeOneOff() which we
66 // need to break.
67 static bool initialized = false;
68 static bool initializing = false;
69 static bool supports_dual_gpus = false;
70
71 if (initialized) {
72 return supports_dual_gpus;
73 } else {
74 if (!initializing) {
75 initializing = true;
76 if (!GLSurface::InitializeOneOff()) {
77 return false;
78 }
79 }
80 initialized = true;
81 }
82
83 if (!base::mac::IsOSLionOrLater()) {
84 return false;
85 }
86
87 if (GetGLImplementation() != kGLImplementationDesktopGL) {
88 return false;
89 }
90
91 // Enumerate all hardware-accelerated renderers. If we find one
92 // online and one offline, assume we're on a dual-GPU system.
93 GLuint display_mask = static_cast<GLuint>(-1);
94 CGLRendererInfoObj renderer_info = NULL;
95 GLint num_renderers = 0;
96
97 bool found_online = false;
98 bool found_offline = false;
99
100 if (CGLQueryRendererInfo(display_mask,
101 &renderer_info,
102 &num_renderers) != kCGLNoError) {
103 return false;
104 }
105
106 base::mac::ScopedGenericObj<CGLRendererInfoObj, ScopedDestroyRendererInfo>
107 scoper(renderer_info);
108
109 for (GLint i = 0; i < num_renderers; ++i) {
110 GLint accelerated = 0;
111 if (CGLDescribeRenderer(renderer_info,
112 i,
113 kCGLRPAccelerated,
114 &accelerated) != kCGLNoError) {
115 return false;
116 }
117
118 if (!accelerated)
119 continue;
120
121 GLint online = 0;
122 if (CGLDescribeRenderer(renderer_info,
123 i,
124 kCGLRPOnline,
125 &online) != kCGLNoError) {
126 return false;
127 }
128
129 if (online) {
130 found_online = true;
131 } else {
132 found_offline = true;
133 }
134 }
135
136 if (found_online && found_offline) {
137 supports_dual_gpus = true;
138 }
139
140 return supports_dual_gpus;
141 }
142
47 } // namespace gfx 143 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698