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