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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gl/SkNativeGLContext.h"
SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
fOldEGLContext = eglGetCurrentContext();
fOldDisplay = eglGetCurrentDisplay();
fOldSurface = eglGetCurrentSurface(EGL_DRAW);
}
SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
if (NULL != fOldDisplay) {
eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
}
}
///////////////////////////////////////////////////////////////////////////////
SkNativeGLContext::SkNativeGLContext()
: fContext(EGL_NO_CONTEXT)
, fDisplay(EGL_NO_DISPLAY)
, fSurface(EGL_NO_SURFACE) {
}
SkNativeGLContext::~SkNativeGLContext() {
this->destroyGLContext();
}
void SkNativeGLContext::destroyGLContext() {
if (fDisplay) {
eglMakeCurrent(fDisplay, 0, 0, 0);
if (fContext) {
eglDestroyContext(fDisplay, fContext);
fContext = EGL_NO_CONTEXT;
}
if (fSurface) {
eglDestroySurface(fDisplay, fSurface);
fSurface = EGL_NO_SURFACE;
}
//TODO should we close the display?
fDisplay = EGL_NO_DISPLAY;
}
}
const GrGLInterface* SkNativeGLContext::createGLContext() {
static const EGLint kEGLContextAttribsForOpenGL[] = {
EGL_NONE
};
static const EGLint kEGLContextAttribsForOpenGLES[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
static const struct {
const EGLint* fContextAttribs;
EGLenum fAPI;
EGLint fRenderableTypeBit;
GrGLBinding fBinding;
} kAPIs[] = {
{ // OpenGL
kEGLContextAttribsForOpenGL,
EGL_OPENGL_API,
EGL_OPENGL_BIT,
kDesktop_GrGLBinding
},
{ // OpenGL ES. This seems to work for both ES2 and 3 (when available).
kEGLContextAttribsForOpenGLES,
EGL_OPENGL_ES_API,
EGL_OPENGL_ES2_BIT,
kES_GrGLBinding
},
};
const GrGLInterface* interface = NULL;
for (size_t api = 0; NULL == interface && api < SK_ARRAY_COUNT(kAPIs); ++api) {
fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
EGLint majorVersion;
EGLint minorVersion;
eglInitialize(fDisplay, &majorVersion, &minorVersion);
#if 0
SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR));
SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS));
SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION));
SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS));
#endif
if (!eglBindAPI(kAPIs[api].fAPI)) {
continue;
}
EGLint numConfigs;
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_NONE
};
EGLConfig surfaceConfig;
if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError());
continue;
}
fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, kAPIs[api].fContextAttribs);
if (EGL_NO_CONTEXT == fContext) {
SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetError());
continue;
}
static const EGLint kSurfaceAttribs[] = {
EGL_WIDTH, 1,
EGL_HEIGHT, 1,
EGL_NONE
};
fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs);
if (EGL_NO_SURFACE == fSurface) {
SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglGetError());
this->destroyGLContext();
continue;
}
if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError());
this->destroyGLContext();
continue;
}
interface = GrGLCreateNativeInterface();
if (NULL == interface) {
SkDebugf("Failed to create gl interface.\n");
this->destroyGLContext();
continue;
}
if (!interface->validate(kAPIs[api].fBinding)) {
interface->unref();
interface = NULL;
this->destroyGLContext();
}
}
return interface;
}
void SkNativeGLContext::makeCurrent() const {
if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
SkDebugf("Could not set the context.\n");
}
}
void SkNativeGLContext::swapBuffers() const {
if (!eglSwapBuffers(fDisplay, fSurface)) {
SkDebugf("Could not complete eglSwapBuffers.\n");
}
}
|