| 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 // This file implements the GLContextWGL and PbufferGLContext classes. | 5 // This file implements the GLContextWGL and PbufferGLContext classes. |
| 6 | 6 |
| 7 #include "ui/gfx/gl/gl_context_wgl.h" | 7 #include "ui/gfx/gl/gl_context_wgl.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "ui/gfx/gl/gl_bindings.h" | 10 #include "ui/gfx/gl/gl_bindings.h" |
| 11 #include "ui/gfx/gl/gl_implementation.h" | 11 #include "ui/gfx/gl/gl_implementation.h" |
| 12 #include "ui/gfx/gl/gl_surface_wgl.h" |
| 12 | 13 |
| 13 namespace gfx { | 14 namespace gfx { |
| 14 | 15 |
| 15 GLContextWGL::GLContextWGL(GLSurfaceWGL* surface) | 16 GLContextWGL::GLContextWGL() |
| 16 : surface_(surface), | 17 : context_(NULL) { |
| 17 context_(NULL) { | |
| 18 DCHECK(surface); | |
| 19 } | 18 } |
| 20 | 19 |
| 21 GLContextWGL::~GLContextWGL() { | 20 GLContextWGL::~GLContextWGL() { |
| 22 Destroy(); | 21 Destroy(); |
| 23 } | 22 } |
| 24 | 23 |
| 25 std::string GLContextWGL::GetExtensions() { | 24 std::string GLContextWGL::GetExtensions() { |
| 26 if (wglGetExtensionsStringARB) { | 25 if (wglGetExtensionsStringARB) { |
| 27 // TODO(apatrick): When contexts and surfaces are separated, we won't be | 26 // TODO(apatrick): When contexts and surfaces are separated, we won't be |
| 28 // able to use surface_ here. Either use a display device context or the | 27 // able to use surface_ here. Either use a display device context or the |
| 29 // surface that was passed to MakeCurrent. | 28 // surface that was passed to MakeCurrent. |
| 30 const char* extensions = wglGetExtensionsStringARB( | 29 const char* extensions = wglGetExtensionsStringARB( |
| 31 static_cast<HDC>(surface_->GetHandle())); | 30 GLSurfaceWGL::GetDisplay()); |
| 32 if (extensions) { | 31 if (extensions) { |
| 33 return GLContext::GetExtensions() + " " + extensions; | 32 return GLContext::GetExtensions() + " " + extensions; |
| 34 } | 33 } |
| 35 } | 34 } |
| 36 | 35 |
| 37 return GLContext::GetExtensions(); | 36 return GLContext::GetExtensions(); |
| 38 } | 37 } |
| 39 | 38 |
| 40 bool GLContextWGL::Initialize(GLContext* shared_context) { | 39 bool GLContextWGL::Initialize(GLContext* shared_context, |
| 40 GLSurface* compatible_surface) { |
| 41 GLSurfaceWGL* surface_wgl = static_cast<GLSurfaceWGL*>(compatible_surface); |
| 42 |
| 41 // TODO(apatrick): When contexts and surfaces are separated, we won't be | 43 // TODO(apatrick): When contexts and surfaces are separated, we won't be |
| 42 // able to use surface_ here. Either use a display device context or a | 44 // able to use surface_ here. Either use a display device context or a |
| 43 // surface that the context is compatible with not necessarily limited to | 45 // surface that the context is compatible with not necessarily limited to |
| 44 // rendering to. | 46 // rendering to. |
| 45 context_ = wglCreateContext(static_cast<HDC>(surface_->GetHandle())); | 47 context_ = wglCreateContext(static_cast<HDC>(surface_wgl->GetHandle())); |
| 46 if (!context_) { | 48 if (!context_) { |
| 47 LOG(ERROR) << "Failed to create GL context."; | 49 LOG(ERROR) << "Failed to create GL context."; |
| 48 Destroy(); | 50 Destroy(); |
| 49 return false; | 51 return false; |
| 50 } | 52 } |
| 51 | 53 |
| 52 if (shared_context) { | 54 if (shared_context) { |
| 53 if (!wglShareLists( | 55 if (!wglShareLists( |
| 54 static_cast<HGLRC>(shared_context->GetHandle()), | 56 static_cast<HGLRC>(shared_context->GetHandle()), |
| 55 context_)) { | 57 context_)) { |
| 56 LOG(ERROR) << "Could not share GL contexts."; | 58 LOG(ERROR) << "Could not share GL contexts."; |
| 57 Destroy(); | 59 Destroy(); |
| 58 return false; | 60 return false; |
| 59 } | 61 } |
| 60 } | 62 } |
| 61 | 63 |
| 62 return true; | 64 return true; |
| 63 } | 65 } |
| 64 | 66 |
| 65 void GLContextWGL::Destroy() { | 67 void GLContextWGL::Destroy() { |
| 66 if (context_) { | 68 if (context_) { |
| 67 wglDeleteContext(context_); | 69 wglDeleteContext(context_); |
| 68 context_ = NULL; | 70 context_ = NULL; |
| 69 } | 71 } |
| 70 | |
| 71 if (surface_.get()) { | |
| 72 surface_->Destroy(); | |
| 73 surface_.reset(); | |
| 74 } | |
| 75 } | 72 } |
| 76 | 73 |
| 77 bool GLContextWGL::MakeCurrent() { | 74 bool GLContextWGL::MakeCurrent(GLSurface* surface) { |
| 78 if (IsCurrent()) { | 75 DCHECK(context_); |
| 76 if (IsCurrent(surface)) |
| 79 return true; | 77 return true; |
| 80 } | |
| 81 | 78 |
| 82 if (!wglMakeCurrent(static_cast<HDC>(surface_->GetHandle()), | 79 if (!wglMakeCurrent(static_cast<HDC>(surface->GetHandle()), context_)) { |
| 83 context_)) { | |
| 84 LOG(ERROR) << "Unable to make gl context current."; | 80 LOG(ERROR) << "Unable to make gl context current."; |
| 85 return false; | 81 return false; |
| 86 } | 82 } |
| 87 | 83 |
| 88 return true; | 84 return true; |
| 89 } | 85 } |
| 90 | 86 |
| 91 bool GLContextWGL::IsCurrent() { | 87 void GLContextWGL::ReleaseCurrent(GLSurface* surface) { |
| 92 return wglGetCurrentDC() == surface_->GetHandle() && | 88 if (!IsCurrent(surface)) |
| 93 wglGetCurrentContext() == context_; | 89 return; |
| 90 |
| 91 wglMakeCurrent(NULL, NULL); |
| 94 } | 92 } |
| 95 | 93 |
| 96 bool GLContextWGL::IsOffscreen() { | 94 bool GLContextWGL::IsCurrent(GLSurface* surface) { |
| 97 // TODO(apatrick): remove this from GLContext interface. | 95 if (wglGetCurrentContext() != context_) |
| 98 return surface_->IsOffscreen(); | 96 return false; |
| 99 } | |
| 100 | 97 |
| 101 bool GLContextWGL::SwapBuffers() { | 98 if (surface) { |
| 102 // TODO(apatrick): remove this from GLContext interface. | 99 if (wglGetCurrentDC() != surface->GetHandle()) |
| 103 return surface_->SwapBuffers(); | 100 return false; |
| 104 } | 101 } |
| 105 | 102 |
| 106 gfx::Size GLContextWGL::GetSize() { | 103 return true; |
| 107 // TODO(apatrick): remove this from GLContext interface. | |
| 108 return surface_->GetSize(); | |
| 109 } | 104 } |
| 110 | 105 |
| 111 void* GLContextWGL::GetHandle() { | 106 void* GLContextWGL::GetHandle() { |
| 112 return context_; | 107 return context_; |
| 113 } | 108 } |
| 114 | 109 |
| 115 void GLContextWGL::SetSwapInterval(int interval) { | 110 void GLContextWGL::SetSwapInterval(int interval) { |
| 116 DCHECK(IsCurrent()); | 111 DCHECK(IsCurrent(NULL)); |
| 117 if (HasExtension("WGL_EXT_swap_control") && wglSwapIntervalEXT) { | 112 if (HasExtension("WGL_EXT_swap_control") && wglSwapIntervalEXT) { |
| 118 wglSwapIntervalEXT(interval); | 113 wglSwapIntervalEXT(interval); |
| 119 } | 114 } |
| 120 } | 115 } |
| 121 | 116 |
| 122 } // namespace gfx | 117 } // namespace gfx |
| OLD | NEW |