| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/basictypes.h" | |
| 6 #include "base/logging.h" | |
| 7 #include "base/mac/mac_util.h" | |
| 8 #include "base/memory/scoped_generic_obj.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "third_party/mesa/MesaLib/include/GL/osmesa.h" | |
| 11 #include "ui/gfx/gl/gl_bindings.h" | |
| 12 #include "ui/gfx/gl/gl_context_cgl.h" | |
| 13 #include "ui/gfx/gl/gl_context_osmesa.h" | |
| 14 #include "ui/gfx/gl/gl_context_stub.h" | |
| 15 #include "ui/gfx/gl/gl_implementation.h" | |
| 16 #include "ui/gfx/gl/gl_surface_cgl.h" | |
| 17 #include "ui/gfx/gl/gl_surface_osmesa.h" | |
| 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 } // namespace | |
| 30 | |
| 31 namespace gfx { | |
| 32 | |
| 33 class GLShareGroup; | |
| 34 | |
| 35 scoped_refptr<GLContext> GLContext::CreateGLContext( | |
| 36 GLShareGroup* share_group, | |
| 37 GLSurface* compatible_surface, | |
| 38 GpuPreference gpu_preference) { | |
| 39 switch (GetGLImplementation()) { | |
| 40 case kGLImplementationDesktopGL: { | |
| 41 scoped_refptr<GLContext> context(new GLContextCGL(share_group)); | |
| 42 if (!context->Initialize(compatible_surface, gpu_preference)) | |
| 43 return NULL; | |
| 44 | |
| 45 return context; | |
| 46 } | |
| 47 case kGLImplementationOSMesaGL: { | |
| 48 scoped_refptr<GLContext> context(new GLContextOSMesa(share_group)); | |
| 49 if (!context->Initialize(compatible_surface, gpu_preference)) | |
| 50 return NULL; | |
| 51 | |
| 52 return context; | |
| 53 } | |
| 54 case kGLImplementationMockGL: | |
| 55 return new GLContextStub; | |
| 56 default: | |
| 57 NOTREACHED(); | |
| 58 return NULL; | |
| 59 } | |
| 60 } | |
| 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 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 | |
| 143 } // namespace gfx | |
| OLD | NEW |