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 (!format) { |
| 54 LOG(ERROR) << "format == 0."; |
| 55 return false; |
| 56 } |
| 57 DCHECK_NE(num_pixel_formats, 0); |
| 58 |
25 CGLError res = CGLCreateContext( | 59 CGLError res = CGLCreateContext( |
26 static_cast<CGLPixelFormatObj>(GLSurfaceCGL::GetPixelFormat()), | 60 format, |
27 share_group() ? | 61 share_context ? |
28 static_cast<CGLContextObj>(share_group()->GetHandle()) : NULL, | 62 static_cast<CGLContextObj>(share_context->GetHandle()) : NULL, |
29 reinterpret_cast<CGLContextObj*>(&context_)); | 63 reinterpret_cast<CGLContextObj*>(&context_)); |
| 64 CGLReleasePixelFormat(format); |
30 if (res != kCGLNoError) { | 65 if (res != kCGLNoError) { |
31 LOG(ERROR) << "Error creating context."; | 66 LOG(ERROR) << "Error creating context."; |
32 Destroy(); | 67 Destroy(); |
33 return false; | 68 return false; |
34 } | 69 } |
35 | 70 |
| 71 gpu_preference_ = gpu_preference; |
36 return true; | 72 return true; |
37 } | 73 } |
38 | 74 |
39 void GLContextCGL::Destroy() { | 75 void GLContextCGL::Destroy() { |
40 if (context_) { | 76 if (context_) { |
41 CGLDestroyContext(static_cast<CGLContextObj>(context_)); | 77 CGLDestroyContext(static_cast<CGLContextObj>(context_)); |
42 context_ = NULL; | 78 context_ = NULL; |
43 } | 79 } |
44 } | 80 } |
45 | 81 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 144 |
109 void* GLContextCGL::GetHandle() { | 145 void* GLContextCGL::GetHandle() { |
110 return context_; | 146 return context_; |
111 } | 147 } |
112 | 148 |
113 void GLContextCGL::SetSwapInterval(int interval) { | 149 void GLContextCGL::SetSwapInterval(int interval) { |
114 DCHECK(IsCurrent(NULL)); | 150 DCHECK(IsCurrent(NULL)); |
115 LOG(WARNING) << "GLContex: GLContextCGL::SetSwapInterval is ignored."; | 151 LOG(WARNING) << "GLContex: GLContextCGL::SetSwapInterval is ignored."; |
116 } | 152 } |
117 | 153 |
| 154 GpuPreference GLContextCGL::GetGpuPreference() { |
| 155 return gpu_preference_; |
| 156 } |
| 157 |
118 } // namespace gfx | 158 } // namespace gfx |
OLD | NEW |