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
|
// 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.
#include "ui/gfx/gl/gl_context_egl.h"
#include "build/build_config.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "third_party/angle/include/EGL/egl.h"
#include "ui/gfx/gl/gl_surface_egl.h"
#include "ui/gfx/gl/egl_util.h"
// This header must come after the above third-party include, as
// it brings in #defines that cause conflicts.
#include "ui/gfx/gl/gl_bindings.h"
#if defined(USE_X11)
extern "C" {
#include <X11/Xlib.h>
}
#endif
namespace gfx {
std::string GLContextEGL::GetExtensions() {
const char* extensions = eglQueryString(GLSurfaceEGL::GetDisplay(),
EGL_EXTENSIONS);
if (!extensions)
return GLContext::GetExtensions();
return GLContext::GetExtensions() + " " + extensions;
}
GLContextEGL::GLContextEGL(GLShareGroup* share_group)
: GLContext(share_group),
context_(NULL)
{
}
GLContextEGL::~GLContextEGL() {
Destroy();
}
bool GLContextEGL::Initialize(GLSurface* compatible_surface) {
DCHECK(compatible_surface);
DCHECK(!context_);
static const EGLint kContextAttributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
context_ = eglCreateContext(
GLSurfaceEGL::GetDisplay(),
GLSurfaceEGL::GetConfig(),
share_group() ? share_group()->GetHandle() : NULL,
kContextAttributes);
if (!context_) {
LOG(ERROR) << "eglCreateContext failed with error "
<< GetLastEGLErrorString();
Destroy();
return false;
}
return true;
}
void GLContextEGL::Destroy() {
if (context_) {
if (!eglDestroyContext(GLSurfaceEGL::GetDisplay(), context_)) {
LOG(ERROR) << "eglDestroyContext failed with error "
<< GetLastEGLErrorString();
}
context_ = NULL;
}
}
bool GLContextEGL::MakeCurrent(GLSurface* surface) {
DCHECK(context_);
if (IsCurrent(surface))
return true;
if (!eglMakeCurrent(GLSurfaceEGL::GetDisplay(),
surface->GetHandle(),
surface->GetHandle(),
context_)) {
VLOG(1) << "eglMakeCurrent failed with error "
<< GetLastEGLErrorString();
return false;
}
return true;
}
void GLContextEGL::ReleaseCurrent(GLSurface* surface) {
if (!IsCurrent(surface))
return;
eglMakeCurrent(GLSurfaceEGL::GetDisplay(),
EGL_NO_SURFACE,
EGL_NO_SURFACE,
EGL_NO_CONTEXT);
}
bool GLContextEGL::IsCurrent(GLSurface* surface) {
DCHECK(context_);
if (context_ != eglGetCurrentContext())
return false;
if (surface) {
if (surface->GetHandle() != eglGetCurrentSurface(EGL_DRAW))
return false;
}
return true;
}
void* GLContextEGL::GetHandle() {
return context_;
}
void GLContextEGL::SetSwapInterval(int interval) {
DCHECK(IsCurrent(NULL));
if (!eglSwapInterval(GLSurfaceEGL::GetDisplay(), interval)) {
LOG(ERROR) << "eglSwapInterval failed with error "
<< GetLastEGLErrorString();
}
}
} // namespace gfx
|