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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
/*
** 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.
*/
#define __STDC_LIMIT_MACROS 1
#include <string.h>
#include "egl_cache.h"
#include "egl_display.h"
#include "egl_object.h"
#include "egl_tls.h"
#include "egl_impl.h"
#include "Loader.h"
#include <cutils/properties.h>
// ----------------------------------------------------------------------------
namespace android {
// ----------------------------------------------------------------------------
static char const * const sVendorString = "Android";
static char const * const sVersionString = "1.4 Android META-EGL";
static char const * const sClientApiString = "OpenGL_ES";
// this is the list of EGL extensions that are exposed to applications
// some of them are mandatory because used by the ANDROID system.
//
// mandatory extensions are required per the CDD and not explicitly
// checked during EGL initialization. the system *assumes* these extensions
// are present. the system may not function properly if some mandatory
// extensions are missing.
//
// NOTE: sExtensionString MUST be have a single space as the last character.
//
static char const * const sExtensionString =
"EGL_KHR_image " // mandatory
"EGL_KHR_image_base " // mandatory
"EGL_KHR_image_pixmap "
"EGL_KHR_gl_texture_2D_image "
"EGL_KHR_gl_texture_cubemap_image "
"EGL_KHR_gl_renderbuffer_image "
"EGL_KHR_fence_sync "
"EGL_NV_system_time "
"EGL_ANDROID_image_native_buffer " // mandatory
;
// extensions not exposed to applications but used by the ANDROID system
// "EGL_ANDROID_recordable " // mandatory
// "EGL_ANDROID_blob_cache " // strongly recommended
// "EGL_IMG_hibernate_process " // optional
extern void initEglTraceLevel();
extern void initEglDebugLevel();
extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
// ----------------------------------------------------------------------------
egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
egl_display_t::egl_display_t() :
magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), 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());
egl_connection_t* const cnx = &gEGLImpl;
if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
disp.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();
initEglDebugLevel();
#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
egl_connection_t* const cnx = &gEGLImpl;
cnx->major = -1;
cnx->minor = -1;
if (cnx->dso) {
#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.dpy;
if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
//ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
// idpy, cnx->major, cnx->minor, cnx);
// display is now initialized
disp.state = egl_display_t::INITIALIZED;
// get the query-strings for this display for each implementation
disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
EGL_VENDOR);
disp.queryString.version = cnx->egl.eglQueryString(idpy,
EGL_VERSION);
disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
EGL_EXTENSIONS);
disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
EGL_CLIENT_APIS);
} else {
ALOGW("eglInitialize(%p) failed (%s)", idpy,
egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
}
}
// the query strings are per-display
mVendorString.setTo(sVendorString);
mVersionString.setTo(sVersionString);
mClientApiString.setTo(sClientApiString);
// we only add extensions that exist in the implementation
char const* start = sExtensionString;
char const* end;
do {
// find the space separating this extension for the next one
end = strchr(start, ' ');
if (end) {
// length of the extension string
const size_t len = end - start;
if (len) {
// NOTE: we could avoid the copy if we had strnstr.
const String8 ext(start, len);
// now look for this extension
if (disp.queryString.extensions) {
// if we find it, add this extension string to our list
// (and don't forget the space)
const char* match = strstr(disp.queryString.extensions, ext.string());
if (match && (match[len] == ' ' || match[len] == 0)) {
mExtensionString.append(start, len+1);
}
}
}
// process the next extension string, and skip the space.
start = end + 1;
}
} while (end);
egl_cache_t::get()->initialize(this);
char value[PROPERTY_VALUE_MAX];
property_get("debug.egl.finish", value, "0");
if (atoi(value)) {
finishOnSwap = true;
}
property_get("debug.egl.traceGpuCompletion", value, "0");
if (atoi(value)) {
traceGpuCompletion = true;
}
refs++;
if (major != NULL)
*major = VERSION_MAJOR;
if (minor != NULL)
*minor = VERSION_MINOR;
mHibernation.setDisplayValid(true);
return EGL_TRUE;
}
EGLBoolean egl_display_t::terminate() {
Mutex::Autolock _l(lock);
if (refs == 0) {
/*
* From the EGL spec (3.2):
* "Termination of a display that has already been terminated,
* (...), is allowed, but the only effect of such a call is
* to return EGL_TRUE (...)
*/
return EGL_TRUE;
}
// this is specific to Android, display termination is ref-counted.
if (refs > 1) {
refs--;
return EGL_TRUE;
}
EGLBoolean res = EGL_FALSE;
egl_connection_t* const cnx = &gEGLImpl;
if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
}
// REVISIT: it's unclear what to do if eglTerminate() fails
disp.state = egl_display_t::TERMINATED;
res = EGL_TRUE;
}
mHibernation.setDisplayValid(false);
// 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();
ALOGW_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--;
return res;
}
void egl_display_t::loseCurrent(egl_context_t * cur_c)
{
if (cur_c) {
egl_display_t* display = cur_c->getDisplay();
if (display) {
display->loseCurrentImpl(cur_c);
}
}
}
void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
{
// by construction, these are either 0 or valid (possibly terminated)
// it should be impossible for these to be invalid
ContextRef _cur_c(cur_c);
SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
{ // scope for the lock
Mutex::Autolock _l(lock);
cur_c->onLooseCurrent();
}
// This cannot be called with the lock held because it might end-up
// calling back into EGL (in particular when a surface is destroyed
// it calls ANativeWindow::disconnect
_cur_c.release();
_cur_r.release();
_cur_d.release();
}
EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
EGLSurface draw, EGLSurface read, EGLContext ctx,
EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
{
EGLBoolean result;
// by construction, these are either 0 or valid (possibly terminated)
// it should be impossible for these to be invalid
ContextRef _cur_c(cur_c);
SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
{ // scope for the lock
Mutex::Autolock _l(lock);
if (c) {
result = c->cnx->egl.eglMakeCurrent(
disp.dpy, impl_draw, impl_read, impl_ctx);
if (result == EGL_TRUE) {
c->onMakeCurrent(draw, read);
if (!cur_c) {
mHibernation.incWakeCount(HibernationMachine::STRONG);
}
}
} else {
result = cur_c->cnx->egl.eglMakeCurrent(
disp.dpy, impl_draw, impl_read, impl_ctx);
if (result == EGL_TRUE) {
cur_c->onLooseCurrent();
mHibernation.decWakeCount(HibernationMachine::STRONG);
}
}
}
if (result == EGL_TRUE) {
// This cannot be called with the lock held because it might end-up
// calling back into EGL (in particular when a surface is destroyed
// it calls ANativeWindow::disconnect
_cur_c.release();
_cur_r.release();
_cur_d.release();
}
return result;
}
// ----------------------------------------------------------------------------
bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
Mutex::Autolock _l(mLock);
ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
"Invalid WakeCount (%d) on enter\n", mWakeCount);
mWakeCount++;
if (strength == STRONG)
mAttemptHibernation = false;
if (CC_UNLIKELY(mHibernating)) {
ALOGV("Awakening\n");
egl_connection_t* const cnx = &gEGLImpl;
// These conditions should be guaranteed before entering hibernation;
// we don't want to get into a state where we can't wake up.
ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
"Invalid hibernation state, unable to awaken\n");
if (!cnx->egl.eglAwakenProcessIMG()) {
ALOGE("Failed to awaken EGL implementation\n");
return false;
}
mHibernating = false;
}
return true;
}
void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
Mutex::Autolock _l(mLock);
ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
mWakeCount--;
if (strength == STRONG)
mAttemptHibernation = true;
if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
egl_connection_t* const cnx = &gEGLImpl;
mAttemptHibernation = false;
if (mAllowHibernation && mDpyValid &&
cnx->egl.eglHibernateProcessIMG &&
cnx->egl.eglAwakenProcessIMG) {
ALOGV("Hibernating\n");
if (!cnx->egl.eglHibernateProcessIMG()) {
ALOGE("Failed to hibernate EGL implementation\n");
return;
}
mHibernating = true;
}
}
}
void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
Mutex::Autolock _l(mLock);
mDpyValid = valid;
}
// ----------------------------------------------------------------------------
}; // namespace android
// ----------------------------------------------------------------------------
|