 Chromium Code Reviews
 Chromium Code Reviews Issue 8233027:
  Support dynamic switching between integrated and discrete GPUs on Mac OS X.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src/
    
  
    Issue 8233027:
  Support dynamic switching between integrated and discrete GPUs on Mac OS X.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src/| OLD | NEW | 
|---|---|
| 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 "ui/gfx/gl/gl_context_cgl.h" | 5 #include "ui/gfx/gl/gl_context_cgl.h" | 
| 6 | 6 | 
| 7 #include <vector> | |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" | 
| 8 #include "ui/gfx/gl/gl_bindings.h" | 10 #include "ui/gfx/gl/gl_bindings.h" | 
| 9 #include "ui/gfx/gl/gl_surface_cgl.h" | 11 #include "ui/gfx/gl/gl_surface_cgl.h" | 
| 10 | 12 | 
| 11 namespace gfx { | 13 namespace gfx { | 
| 12 | 14 | 
| 13 GLContextCGL::GLContextCGL(GLShareGroup* share_group) | 15 GLContextCGL::GLContextCGL(GLShareGroup* share_group) | 
| 14 : GLContext(share_group), | 16 : GLContext(share_group), | 
| 15 context_(NULL) { | 17 context_(NULL), | 
| 18 gpu_preference_(PreferIntegratedGpu) { | |
| 16 } | 19 } | 
| 17 | 20 | 
| 18 GLContextCGL::~GLContextCGL() { | 21 GLContextCGL::~GLContextCGL() { | 
| 19 Destroy(); | 22 Destroy(); | 
| 20 } | 23 } | 
| 21 | 24 | 
| 22 bool GLContextCGL::Initialize(GLSurface* compatible_surface) { | 25 bool GLContextCGL::Initialize( | 
| 26 GLSurface* compatible_surface, GpuPreference gpu_preference) { | |
| 23 DCHECK(compatible_surface); | 27 DCHECK(compatible_surface); | 
| 24 | 28 | 
| 29 // Ensure the GPU preference is compatible with contexts already in the | |
| 30 // share group. | |
| 31 GLContextCGL* share_context = share_group() ? | |
| 32 static_cast<GLContextCGL*>(share_group()->GetContext()) : NULL; | |
| 33 if (share_context && gpu_preference != share_context->GetGpuPreference()) | |
| 34 return false; | |
| 35 | |
| 36 std::vector<CGLPixelFormatAttribute> attribs; | |
| 37 attribs.push_back(kCGLPFAPBuffer); | |
| 38 bool using_offline_renderer = | |
| 39 SupportsDualGpus() && gpu_preference == PreferIntegratedGpu; | |
| 40 if (using_offline_renderer) { | |
| 41 attribs.push_back(kCGLPFAAllowOfflineRenderers); | |
| 42 } | |
| 43 attribs.push_back((CGLPixelFormatAttribute) 0); | |
| 44 | |
| 45 CGLPixelFormatObj format; | |
| 46 GLint num_pixel_formats; | |
| 47 if (CGLChoosePixelFormat(&attribs.front(), | |
| 48 &format, | |
| 49 &num_pixel_formats) != kCGLNoError) { | |
| 50 LOG(ERROR) << "Error choosing pixel format."; | |
| 51 return false; | |
| 52 } | |
| 53 if (num_pixel_formats == 0) { | |
| 54 LOG(ERROR) << "num_pixel_formats == 0."; | |
| 55 return false; | |
| 56 } | |
| 57 if (!format) { | |
| 
Mark Mentovai
2011/10/13 03:50:33
This is similar to the other maybe-I-leak-format t
 
Ken Russell (switch to Gerrit)
2011/10/13 20:09:32
I reviewed the documentation for CGLChoosePixelFor
 | |
| 58 LOG(ERROR) << "pixel_format == 0."; | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 25 CGLError res = CGLCreateContext( | 62 CGLError res = CGLCreateContext( | 
| 26 static_cast<CGLPixelFormatObj>(GLSurfaceCGL::GetPixelFormat()), | 63 format, | 
| 27 share_group() ? | 64 share_context ? | 
| 28 static_cast<CGLContextObj>(share_group()->GetHandle()) : NULL, | 65 static_cast<CGLContextObj>(share_context->GetHandle()) : NULL, | 
| 29 reinterpret_cast<CGLContextObj*>(&context_)); | 66 reinterpret_cast<CGLContextObj*>(&context_)); | 
| 67 CGLReleasePixelFormat(format); | |
| 30 if (res != kCGLNoError) { | 68 if (res != kCGLNoError) { | 
| 31 LOG(ERROR) << "Error creating context."; | 69 LOG(ERROR) << "Error creating context."; | 
| 32 Destroy(); | 70 Destroy(); | 
| 33 return false; | 71 return false; | 
| 34 } | 72 } | 
| 35 | 73 | 
| 74 gpu_preference_ = gpu_preference; | |
| 36 return true; | 75 return true; | 
| 37 } | 76 } | 
| 38 | 77 | 
| 39 void GLContextCGL::Destroy() { | 78 void GLContextCGL::Destroy() { | 
| 40 if (context_) { | 79 if (context_) { | 
| 41 CGLDestroyContext(static_cast<CGLContextObj>(context_)); | 80 CGLDestroyContext(static_cast<CGLContextObj>(context_)); | 
| 42 context_ = NULL; | 81 context_ = NULL; | 
| 43 } | 82 } | 
| 44 } | 83 } | 
| 45 | 84 | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 | 147 | 
| 109 void* GLContextCGL::GetHandle() { | 148 void* GLContextCGL::GetHandle() { | 
| 110 return context_; | 149 return context_; | 
| 111 } | 150 } | 
| 112 | 151 | 
| 113 void GLContextCGL::SetSwapInterval(int interval) { | 152 void GLContextCGL::SetSwapInterval(int interval) { | 
| 114 DCHECK(IsCurrent(NULL)); | 153 DCHECK(IsCurrent(NULL)); | 
| 115 LOG(WARNING) << "GLContex: GLContextCGL::SetSwapInterval is ignored."; | 154 LOG(WARNING) << "GLContex: GLContextCGL::SetSwapInterval is ignored."; | 
| 116 } | 155 } | 
| 117 | 156 | 
| 157 GpuPreference GLContextCGL::GetGpuPreference() { | |
| 158 return gpu_preference_; | |
| 159 } | |
| 160 | |
| 118 } // namespace gfx | 161 } // namespace gfx | 
| OLD | NEW |