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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
#include "egl_cache.h"
#include "egl_display.h"
#include "egl_object.h"
#include "egl_tls.h"
#include "egl_impl.h"
#include "Loader.h"
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
extern void initEglTraceLevel();
extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
static int cmp_configs(const void* a, const void *b) {
const egl_config_t& c0 = *(egl_config_t const *)a;
const egl_config_t& c1 = *(egl_config_t const *)b;
return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
}
// ----------------------------------------------------------------------------
egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
egl_display_t::egl_display_t() :
magic('_dpy'), numTotalConfigs(0), configs(0), refs(0) {
}
egl_display_t::~egl_display_t() {
magic = 0;
egl_cache_t::get()->terminate();
}
egl_display_t* egl_display_t::get(EGLDisplay dpy) {
uintptr_t index = uintptr_t(dpy)-1U;
return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
}
void egl_display_t::addObject(egl_object_t* object) {
Mutex::Autolock _l(lock);
objects.add(object);
}
void egl_display_t::removeObject(egl_object_t* object) {
Mutex::Autolock _l(lock);
objects.remove(object);
}
bool egl_display_t::getObject(egl_object_t* object) const {
Mutex::Autolock _l(lock);
if (objects.indexOf(object) >= 0) {
if (object->getDisplay() == this) {
object->incRef();
return true;
}
}
return false;
}
EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
if (uintptr_t(disp) >= NUM_DISPLAYS)
return NULL;
return sDisplay[uintptr_t(disp)].getDisplay(disp);
}
EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
Mutex::Autolock _l(lock);
// get our driver loader
Loader& loader(Loader::getInstance());
for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
if (cnx->dso && disp[i].dpy == EGL_NO_DISPLAY) {
EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
disp[i].dpy = dpy;
if (dpy == EGL_NO_DISPLAY) {
loader.close(cnx->dso);
cnx->dso = NULL;
}
}
}
return EGLDisplay(uintptr_t(display) + 1U);
}
EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
Mutex::Autolock _l(lock);
if (refs > 0) {
if (major != NULL)
*major = VERSION_MAJOR;
if (minor != NULL)
*minor = VERSION_MINOR;
refs++;
return EGL_TRUE;
}
#if EGL_TRACE
// Called both at early_init time and at this time. (Early_init is pre-zygote, so
// the information from that call may be stale.)
initEglTraceLevel();
#endif
setGLHooksThreadSpecific(&gHooksNoContext);
// initialize each EGL and
// build our own extension string first, based on the extension we know
// and the extension supported by our client implementation
for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
cnx->major = -1;
cnx->minor = -1;
if (!cnx->dso)
continue;
#if defined(ADRENO130)
#warning "Adreno-130 eglInitialize() workaround"
/*
* The ADRENO 130 driver returns a different EGLDisplay each time
* eglGetDisplay() is called, but also makes the EGLDisplay invalid
* after eglTerminate() has been called, so that eglInitialize()
* cannot be called again. Therefore, we need to make sure to call
* eglGetDisplay() before calling eglInitialize();
*/
if (i == IMPL_HARDWARE) {
disp[i].dpy =
cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
}
#endif
EGLDisplay idpy = disp[i].dpy;
if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
//LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p",
// i, idpy, cnx->major, cnx->minor, cnx);
// display is now initialized
disp[i].state = egl_display_t::INITIALIZED;
// get the query-strings for this display for each implementation
disp[i].queryString.vendor = cnx->egl.eglQueryString(idpy,
EGL_VENDOR);
disp[i].queryString.version = cnx->egl.eglQueryString(idpy,
EGL_VERSION);
disp[i].queryString.extensions = cnx->egl.eglQueryString(idpy,
EGL_EXTENSIONS);
disp[i].queryString.clientApi = cnx->egl.eglQueryString(idpy,
EGL_CLIENT_APIS);
} else {
LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy,
egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
}
}
egl_cache_t::get()->initialize(this);
EGLBoolean res = EGL_FALSE;
for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
EGLint n;
if (cnx->egl.eglGetConfigs(disp[i].dpy, 0, 0, &n)) {
disp[i].config = (EGLConfig*) malloc(sizeof(EGLConfig) * n);
if (disp[i].config) {
if (cnx->egl.eglGetConfigs(disp[i].dpy, disp[i].config, n,
&disp[i].numConfigs)) {
numTotalConfigs += n;
res = EGL_TRUE;
}
}
}
}
}
if (res == EGL_TRUE) {
configs = new egl_config_t[numTotalConfigs];
for (int i = 0, k = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
for (int j = 0; j < disp[i].numConfigs; j++) {
configs[k].impl = i;
configs[k].config = disp[i].config[j];
configs[k].configId = k + 1; // CONFIG_ID start at 1
// store the implementation's CONFIG_ID
cnx->egl.eglGetConfigAttrib(disp[i].dpy, disp[i].config[j],
EGL_CONFIG_ID, &configs[k].implConfigId);
k++;
}
}
}
// sort our configurations so we can do binary-searches
qsort(configs, numTotalConfigs, sizeof(egl_config_t), cmp_configs);
refs++;
if (major != NULL)
*major = VERSION_MAJOR;
if (minor != NULL)
*minor = VERSION_MINOR;
return EGL_TRUE;
}
return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
}
EGLBoolean egl_display_t::terminate() {
Mutex::Autolock _l(lock);
if (refs == 0) {
return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
}
// this is specific to Android, display termination is ref-counted.
if (refs > 1) {
refs--;
return EGL_TRUE;
}
EGLBoolean res = EGL_FALSE;
for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) {
egl_connection_t* const cnx = &gEGLImpl[i];
if (cnx->dso && disp[i].state == egl_display_t::INITIALIZED) {
if (cnx->egl.eglTerminate(disp[i].dpy) == EGL_FALSE) {
LOGW("%d: eglTerminate(%p) failed (%s)", i, disp[i].dpy,
egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
}
// REVISIT: it's unclear what to do if eglTerminate() fails
free(disp[i].config);
disp[i].numConfigs = 0;
disp[i].config = 0;
disp[i].state = egl_display_t::TERMINATED;
res = EGL_TRUE;
}
}
// Mark all objects remaining in the list as terminated, unless
// there are no reference to them, it which case, we're free to
// delete them.
size_t count = objects.size();
LOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
for (size_t i=0 ; i<count ; i++) {
egl_object_t* o = objects.itemAt(i);
o->destroy();
}
// this marks all object handles are "terminated"
objects.clear();
refs--;
numTotalConfigs = 0;
delete[] configs;
return res;
}
// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------
|