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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/gfx/gl/gl_bindings.h" | 8 #include "ui/gfx/gl/gl_bindings.h" |
9 #include "ui/gfx/gl/gl_surface_cgl.h" | 9 #include "ui/gfx/gl/gl_surface_cgl.h" |
10 | 10 |
11 namespace gfx { | 11 namespace gfx { |
12 | 12 |
13 GLContextCGL::GLContextCGL(GLSurfaceCGL* surface) | 13 GLContextCGL::GLContextCGL() |
14 : surface_(surface), | 14 : context_(NULL) { |
15 context_(NULL) { | |
16 } | 15 } |
17 | 16 |
18 GLContextCGL::~GLContextCGL() { | 17 GLContextCGL::~GLContextCGL() { |
19 Destroy(); | 18 Destroy(); |
20 } | 19 } |
21 | 20 |
22 bool GLContextCGL::Initialize(GLContext* shared_context) { | 21 bool GLContextCGL::Initialize(GLContext* shared_context, |
| 22 GLSurface* compatible_surface) { |
| 23 DCHECK(compatible_surface); |
| 24 |
23 CGLError res = CGLCreateContext( | 25 CGLError res = CGLCreateContext( |
24 static_cast<CGLPixelFormatObj>(GLSurfaceCGL::GetPixelFormat()), | 26 static_cast<CGLPixelFormatObj>(GLSurfaceCGL::GetPixelFormat()), |
25 shared_context ? | 27 shared_context ? |
26 static_cast<CGLContextObj>(shared_context->GetHandle()) : NULL, | 28 static_cast<CGLContextObj>(shared_context->GetHandle()) : NULL, |
27 reinterpret_cast<CGLContextObj*>(&context_)); | 29 reinterpret_cast<CGLContextObj*>(&context_)); |
28 if (res != kCGLNoError) { | 30 if (res != kCGLNoError) { |
29 LOG(ERROR) << "Error creating context."; | 31 LOG(ERROR) << "Error creating context."; |
30 Destroy(); | 32 Destroy(); |
31 return false; | 33 return false; |
32 } | 34 } |
33 | 35 |
34 return true; | 36 return true; |
35 } | 37 } |
36 | 38 |
37 void GLContextCGL::Destroy() { | 39 void GLContextCGL::Destroy() { |
38 if (context_) { | 40 if (context_) { |
39 CGLDestroyContext(static_cast<CGLContextObj>(context_)); | 41 CGLDestroyContext(static_cast<CGLContextObj>(context_)); |
40 context_ = NULL; | 42 context_ = NULL; |
41 } | 43 } |
42 } | 44 } |
43 | 45 |
44 bool GLContextCGL::MakeCurrent() { | 46 bool GLContextCGL::MakeCurrent(GLSurface* surface) { |
45 if (IsCurrent()) | 47 DCHECK(context_); |
| 48 if (IsCurrent(surface)) |
46 return true; | 49 return true; |
47 | 50 |
48 if (CGLSetPBuffer(static_cast<CGLContextObj>(context_), | 51 if (CGLSetPBuffer(static_cast<CGLContextObj>(context_), |
49 static_cast<CGLPBufferObj>(surface_->GetHandle()), | 52 static_cast<CGLPBufferObj>(surface->GetHandle()), |
50 0, | 53 0, |
51 0, | 54 0, |
52 0) != kCGLNoError) { | 55 0) != kCGLNoError) { |
53 LOG(ERROR) << "Error attaching pbuffer to context."; | 56 LOG(ERROR) << "Error attaching pbuffer to context."; |
54 Destroy(); | 57 Destroy(); |
55 return false; | 58 return false; |
56 } | 59 } |
57 | 60 |
58 if (CGLSetCurrentContext( | 61 if (CGLSetCurrentContext( |
59 static_cast<CGLContextObj>(context_)) != kCGLNoError) { | 62 static_cast<CGLContextObj>(context_)) != kCGLNoError) { |
60 LOG(ERROR) << "Unable to make gl context current."; | 63 LOG(ERROR) << "Unable to make gl context current."; |
61 return false; | 64 return false; |
62 } | 65 } |
63 | 66 |
64 return true; | 67 return true; |
65 } | 68 } |
66 | 69 |
67 bool GLContextCGL::IsCurrent() { | 70 void GLContextCGL::ReleaseCurrent(GLSurface* surface) { |
68 return CGLGetCurrentContext() == context_; | 71 if (!IsCurrent(surface)) |
| 72 return; |
| 73 |
| 74 CGLSetCurrentContext(NULL); |
| 75 CGLSetPBuffer(static_cast<CGLContextObj>(context_), NULL, 0, 0, 0); |
69 } | 76 } |
70 | 77 |
71 bool GLContextCGL::IsOffscreen() { | 78 bool GLContextCGL::IsCurrent(GLSurface* surface) { |
72 // TODO(apatrick): remove this from GLContext interface. | 79 if (CGLGetCurrentContext() != context_) |
73 return surface_->IsOffscreen(); | 80 return false; |
74 } | |
75 | 81 |
76 bool GLContextCGL::SwapBuffers() { | 82 if (surface) { |
77 // TODO(apatrick): remove this from GLContext interface. | 83 CGLPBufferObj current_surface = NULL; |
78 return surface_->SwapBuffers(); | 84 GLenum face; |
79 } | 85 GLint level; |
| 86 GLint screen; |
| 87 CGLGetPBuffer(static_cast<CGLContextObj>(context_), |
| 88 ¤t_surface, |
| 89 &face, |
| 90 &level, |
| 91 &screen); |
| 92 if (current_surface != surface->GetHandle()) |
| 93 return false; |
| 94 } |
80 | 95 |
81 gfx::Size GLContextCGL::GetSize() { | 96 return true; |
82 // TODO(apatrick): remove this from GLContext interface. | |
83 return surface_->GetSize(); | |
84 } | 97 } |
85 | 98 |
86 void* GLContextCGL::GetHandle() { | 99 void* GLContextCGL::GetHandle() { |
87 return context_; | 100 return context_; |
88 } | 101 } |
89 | 102 |
90 void GLContextCGL::SetSwapInterval(int interval) { | 103 void GLContextCGL::SetSwapInterval(int interval) { |
91 DCHECK(IsCurrent()); | 104 DCHECK(IsCurrent(NULL)); |
92 NOTREACHED() << "Attempt to call SetSwapInterval on a GLContextCGL."; | 105 NOTREACHED() << "Attempt to call SetSwapInterval on a GLContextCGL."; |
93 } | 106 } |
94 | 107 |
95 } // namespace gfx | 108 } // namespace gfx |
OLD | NEW |