OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This file implements the GLContextWGL and PbufferGLContext classes. | |
6 | |
7 #include "ui/gfx/gl/gl_context_wgl.h" | |
8 | |
9 #include "base/debug/trace_event.h" | |
10 #include "base/logging.h" | |
11 #include "ui/gfx/gl/gl_bindings.h" | |
12 #include "ui/gfx/gl/gl_implementation.h" | |
13 #include "ui/gfx/gl/gl_surface_wgl.h" | |
14 | |
15 namespace gfx { | |
16 | |
17 GLContextWGL::GLContextWGL(GLShareGroup* share_group) | |
18 : GLContext(share_group), | |
19 context_(NULL) { | |
20 } | |
21 | |
22 GLContextWGL::~GLContextWGL() { | |
23 Destroy(); | |
24 } | |
25 | |
26 std::string GLContextWGL::GetExtensions() { | |
27 const char* extensions = NULL; | |
28 if (wglGetExtensionsStringARB) | |
29 extensions = wglGetExtensionsStringARB(GLSurfaceWGL::GetDisplayDC()); | |
30 else if (wglGetExtensionsStringEXT) | |
31 extensions = wglGetExtensionsStringEXT(); | |
32 | |
33 if (extensions) | |
34 return GLContext::GetExtensions() + " " + extensions; | |
35 | |
36 return GLContext::GetExtensions(); | |
37 } | |
38 | |
39 bool GLContextWGL::Initialize( | |
40 GLSurface* compatible_surface, GpuPreference gpu_preference) { | |
41 // Get the handle of another initialized context in the share group _before_ | |
42 // setting context_. Otherwise this context will be considered initialized | |
43 // and could potentially be returned by GetHandle. | |
44 HGLRC share_handle = static_cast<HGLRC>(share_group()->GetHandle()); | |
45 | |
46 context_ = wglCreateContext( | |
47 static_cast<HDC>(compatible_surface->GetHandle())); | |
48 if (!context_) { | |
49 LOG(ERROR) << "Failed to create GL context."; | |
50 Destroy(); | |
51 return false; | |
52 } | |
53 | |
54 if (share_handle) { | |
55 if (!wglShareLists(share_handle, context_)) { | |
56 LOG(ERROR) << "Could not share GL contexts."; | |
57 Destroy(); | |
58 return false; | |
59 } | |
60 } | |
61 | |
62 return true; | |
63 } | |
64 | |
65 void GLContextWGL::Destroy() { | |
66 if (context_) { | |
67 wglDeleteContext(context_); | |
68 context_ = NULL; | |
69 } | |
70 } | |
71 | |
72 bool GLContextWGL::MakeCurrent(GLSurface* surface) { | |
73 DCHECK(context_); | |
74 if (IsCurrent(surface)) | |
75 return true; | |
76 | |
77 TRACE_EVENT0("gpu", "GLContextWGL::MakeCurrent"); | |
78 | |
79 if (!wglMakeCurrent(static_cast<HDC>(surface->GetHandle()), context_)) { | |
80 LOG(ERROR) << "Unable to make gl context current."; | |
81 return false; | |
82 } | |
83 | |
84 SetCurrent(this, surface); | |
85 if (!InitializeExtensionBindings()) { | |
86 ReleaseCurrent(surface); | |
87 return false; | |
88 } | |
89 | |
90 if (!surface->OnMakeCurrent(this)) { | |
91 LOG(ERROR) << "Could not make current."; | |
92 return false; | |
93 } | |
94 | |
95 return true; | |
96 } | |
97 | |
98 void GLContextWGL::ReleaseCurrent(GLSurface* surface) { | |
99 if (!IsCurrent(surface)) | |
100 return; | |
101 | |
102 SetCurrent(NULL, NULL); | |
103 wglMakeCurrent(NULL, NULL); | |
104 } | |
105 | |
106 bool GLContextWGL::IsCurrent(GLSurface* surface) { | |
107 bool native_context_is_current = | |
108 wglGetCurrentContext() == context_; | |
109 | |
110 // If our context is current then our notion of which GLContext is | |
111 // current must be correct. On the other hand, third-party code | |
112 // using OpenGL might change the current context. | |
113 DCHECK(!native_context_is_current || (GetCurrent() == this)); | |
114 | |
115 if (!native_context_is_current) | |
116 return false; | |
117 | |
118 if (surface) { | |
119 if (wglGetCurrentDC() != surface->GetHandle()) | |
120 return false; | |
121 } | |
122 | |
123 return true; | |
124 } | |
125 | |
126 void* GLContextWGL::GetHandle() { | |
127 return context_; | |
128 } | |
129 | |
130 void GLContextWGL::SetSwapInterval(int interval) { | |
131 DCHECK(IsCurrent(NULL)); | |
132 if (gfx::g_WGL_EXT_swap_control) { | |
133 wglSwapIntervalEXT(interval); | |
134 } else { | |
135 LOG(WARNING) << | |
136 "Could not disable vsync: driver does not " | |
137 "support WGL_EXT_swap_control"; | |
138 } | |
139 } | |
140 | |
141 } // namespace gfx | |
OLD | NEW |