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