| 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) { |
| 41 // TODO(apatrick): When contexts and surfaces are separated, we won't be | 40 // 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 | 41 // 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 | 42 // surface that the context is compatible with not necessarily limited to |
| 44 // rendering to. | 43 // rendering to. |
| 45 context_ = wglCreateContext(static_cast<HDC>(surface_->GetHandle())); | 44 context_ = wglCreateContext(GLSurfaceWGL::GetDisplay()); |
| 46 if (!context_) { | 45 if (!context_) { |
| 47 LOG(ERROR) << "Failed to create GL context."; | 46 LOG(ERROR) << "Failed to create GL context."; |
| 48 Destroy(); | 47 Destroy(); |
| 49 return false; | 48 return false; |
| 50 } | 49 } |
| 51 | 50 |
| 52 if (shared_context) { | 51 if (shared_context) { |
| 53 if (!wglShareLists( | 52 if (!wglShareLists( |
| 54 static_cast<HGLRC>(shared_context->GetHandle()), | 53 static_cast<HGLRC>(shared_context->GetHandle()), |
| 55 context_)) { | 54 context_)) { |
| 56 LOG(ERROR) << "Could not share GL contexts."; | 55 LOG(ERROR) << "Could not share GL contexts."; |
| 57 Destroy(); | 56 Destroy(); |
| 58 return false; | 57 return false; |
| 59 } | 58 } |
| 60 } | 59 } |
| 61 | 60 |
| 62 return true; | 61 return true; |
| 63 } | 62 } |
| 64 | 63 |
| 65 void GLContextWGL::Destroy() { | 64 void GLContextWGL::Destroy() { |
| 66 if (context_) { | 65 if (context_) { |
| 67 wglDeleteContext(context_); | 66 wglDeleteContext(context_); |
| 68 context_ = NULL; | 67 context_ = NULL; |
| 69 } | 68 } |
| 70 | |
| 71 if (surface_.get()) { | |
| 72 surface_->Destroy(); | |
| 73 surface_.reset(); | |
| 74 } | |
| 75 } | 69 } |
| 76 | 70 |
| 77 bool GLContextWGL::MakeCurrent() { | 71 bool GLContextWGL::MakeCurrent(GLSurface* surface) { |
| 78 if (IsCurrent()) { | 72 DCHECK(context_); |
| 73 if (IsCurrent(surface)) |
| 79 return true; | 74 return true; |
| 80 } | |
| 81 | 75 |
| 82 if (!wglMakeCurrent(static_cast<HDC>(surface_->GetHandle()), | 76 if (!wglMakeCurrent(static_cast<HDC>(surface->GetHandle()), context_)) { |
| 83 context_)) { | |
| 84 LOG(ERROR) << "Unable to make gl context current."; | 77 LOG(ERROR) << "Unable to make gl context current."; |
| 85 return false; | 78 return false; |
| 86 } | 79 } |
| 87 | 80 |
| 88 return true; | 81 return true; |
| 89 } | 82 } |
| 90 | 83 |
| 91 bool GLContextWGL::IsCurrent() { | 84 void GLContextWGL::ReleaseCurrent(GLSurface* surface) { |
| 92 return wglGetCurrentDC() == surface_->GetHandle() && | 85 if (!IsCurrent(surface)) |
| 93 wglGetCurrentContext() == context_; | 86 return; |
| 87 |
| 88 wglMakeCurrent(NULL, NULL); |
| 94 } | 89 } |
| 95 | 90 |
| 96 bool GLContextWGL::IsOffscreen() { | 91 bool GLContextWGL::IsCurrent(GLSurface* surface) { |
| 97 // TODO(apatrick): remove this from GLContext interface. | 92 if (wglGetCurrentContext() != context_) |
| 98 return surface_->IsOffscreen(); | 93 return false; |
| 99 } | |
| 100 | 94 |
| 101 bool GLContextWGL::SwapBuffers() { | 95 if (surface) { |
| 102 // TODO(apatrick): remove this from GLContext interface. | 96 if (wglGetCurrentDC() != surface->GetHandle()) |
| 103 return surface_->SwapBuffers(); | 97 return false; |
| 104 } | 98 } |
| 105 | 99 |
| 106 gfx::Size GLContextWGL::GetSize() { | 100 return true; |
| 107 // TODO(apatrick): remove this from GLContext interface. | |
| 108 return surface_->GetSize(); | |
| 109 } | 101 } |
| 110 | 102 |
| 111 void* GLContextWGL::GetHandle() { | 103 void* GLContextWGL::GetHandle() { |
| 112 return context_; | 104 return context_; |
| 113 } | 105 } |
| 114 | 106 |
| 115 void GLContextWGL::SetSwapInterval(int interval) { | 107 void GLContextWGL::SetSwapInterval(int interval) { |
| 116 DCHECK(IsCurrent()); | 108 DCHECK(IsCurrent(NULL)); |
| 117 if (HasExtension("WGL_EXT_swap_control") && wglSwapIntervalEXT) { | 109 if (HasExtension("WGL_EXT_swap_control") && wglSwapIntervalEXT) { |
| 118 wglSwapIntervalEXT(interval); | 110 wglSwapIntervalEXT(interval); |
| 119 } | 111 } |
| 120 } | 112 } |
| 121 | 113 |
| 122 } // namespace gfx | 114 } // namespace gfx |
| OLD | NEW |