blob: 88a17d9eecfe1e3d70021a2d1f7588750e4a2f7d [file] [log] [blame]
Mathias Agopian518ec112011-05-13 16:21:08 -07001/*
2 ** Copyright 2007, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
Jesse Hallb29e5e82012-04-04 16:53:42 -070017#define __STDC_LIMIT_MACROS 1
18
Mathias Agopian4b9511c2011-11-13 23:52:47 -080019#include <string.h>
20
Jamie Gennisaca51c02011-11-03 17:42:43 -070021#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070022#include "egl_display.h"
23#include "egl_object.h"
24#include "egl_tls.h"
25#include "egl_impl.h"
26#include "Loader.h"
Mathias Agopian7db993a2012-03-25 00:49:46 -070027#include <cutils/properties.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070028
29// ----------------------------------------------------------------------------
30namespace android {
31// ----------------------------------------------------------------------------
32
Mathias Agopian4b9511c2011-11-13 23:52:47 -080033static char const * const sVendorString = "Android";
34static char const * const sVersionString = "1.4 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070035static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080036
37// this is the list of EGL extensions that are exposed to applications
38// some of them are mandatory because used by the ANDROID system.
39//
40// mandatory extensions are required per the CDD and not explicitly
41// checked during EGL initialization. the system *assumes* these extensions
42// are present. the system may not function properly if some mandatory
43// extensions are missing.
44//
45// NOTE: sExtensionString MUST be have a single space as the last character.
46//
47static char const * const sExtensionString =
48 "EGL_KHR_image " // mandatory
49 "EGL_KHR_image_base " // mandatory
50 "EGL_KHR_image_pixmap "
51 "EGL_KHR_gl_texture_2D_image "
52 "EGL_KHR_gl_texture_cubemap_image "
53 "EGL_KHR_gl_renderbuffer_image "
54 "EGL_KHR_fence_sync "
55 "EGL_NV_system_time "
56 "EGL_ANDROID_image_native_buffer " // mandatory
57 ;
58
59// extensions not exposed to applications but used by the ANDROID system
60// "EGL_ANDROID_recordable " // mandatory
61// "EGL_ANDROID_blob_cache " // strongly recommended
Jamie Gennis331841b2012-09-06 14:52:00 -070062// "EGL_ANDROID_native_fence_sync " // strongly recommended
Jesse Hall25838592012-04-05 15:53:28 -070063// "EGL_IMG_hibernate_process " // optional
Mathias Agopian4b9511c2011-11-13 23:52:47 -080064
Mathias Agopian518ec112011-05-13 16:21:08 -070065extern void initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -080066extern void initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -070067extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
68
Mathias Agopian518ec112011-05-13 16:21:08 -070069// ----------------------------------------------------------------------------
70
71egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
72
73egl_display_t::egl_display_t() :
Jesse Halla0fef1c2012-04-17 12:02:26 -070074 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
Mathias Agopian518ec112011-05-13 16:21:08 -070075}
76
77egl_display_t::~egl_display_t() {
78 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080079 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070080}
81
82egl_display_t* egl_display_t::get(EGLDisplay dpy) {
83 uintptr_t index = uintptr_t(dpy)-1U;
84 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
85}
86
87void egl_display_t::addObject(egl_object_t* object) {
88 Mutex::Autolock _l(lock);
89 objects.add(object);
90}
91
Mathias Agopian5b287a62011-05-16 18:58:55 -070092void egl_display_t::removeObject(egl_object_t* object) {
93 Mutex::Autolock _l(lock);
94 objects.remove(object);
95}
96
Mathias Agopianf0480de2011-11-13 20:50:07 -080097bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070098 Mutex::Autolock _l(lock);
99 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800100 if (object->getDisplay() == this) {
101 object->incRef();
102 return true;
103 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700104 }
105 return false;
106}
107
Mathias Agopian518ec112011-05-13 16:21:08 -0700108EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
109 if (uintptr_t(disp) >= NUM_DISPLAYS)
110 return NULL;
111
112 return sDisplay[uintptr_t(disp)].getDisplay(disp);
113}
114
115EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
116
117 Mutex::Autolock _l(lock);
118
119 // get our driver loader
120 Loader& loader(Loader::getInstance());
121
Mathias Agopianada798b2012-02-13 17:09:30 -0800122 egl_connection_t* const cnx = &gEGLImpl;
123 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
124 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
125 disp.dpy = dpy;
126 if (dpy == EGL_NO_DISPLAY) {
127 loader.close(cnx->dso);
128 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700129 }
130 }
131
132 return EGLDisplay(uintptr_t(display) + 1U);
133}
134
135EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
136
137 Mutex::Autolock _l(lock);
138
139 if (refs > 0) {
140 if (major != NULL)
141 *major = VERSION_MAJOR;
142 if (minor != NULL)
143 *minor = VERSION_MINOR;
144 refs++;
145 return EGL_TRUE;
146 }
147
148#if EGL_TRACE
149
150 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
151 // the information from that call may be stale.)
152 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800153 initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -0700154
155#endif
156
157 setGLHooksThreadSpecific(&gHooksNoContext);
158
159 // initialize each EGL and
160 // build our own extension string first, based on the extension we know
161 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800162
163 egl_connection_t* const cnx = &gEGLImpl;
164 cnx->major = -1;
165 cnx->minor = -1;
166 if (cnx->dso) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700167
168#if defined(ADRENO130)
169#warning "Adreno-130 eglInitialize() workaround"
170 /*
171 * The ADRENO 130 driver returns a different EGLDisplay each time
172 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
173 * after eglTerminate() has been called, so that eglInitialize()
174 * cannot be called again. Therefore, we need to make sure to call
175 * eglGetDisplay() before calling eglInitialize();
176 */
177 if (i == IMPL_HARDWARE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800178 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian518ec112011-05-13 16:21:08 -0700179 }
180#endif
181
Mathias Agopianada798b2012-02-13 17:09:30 -0800182 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700183 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800184 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
185 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700186
187 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800188 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700189
190 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800191 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700192 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800193 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700194 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800195 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700196 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800197 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700198 EGL_CLIENT_APIS);
199
200 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800201 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700202 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
203 }
204 }
205
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800206 // the query strings are per-display
207 mVendorString.setTo(sVendorString);
208 mVersionString.setTo(sVersionString);
209 mClientApiString.setTo(sClientApiString);
210
Mathias Agopian7773c432012-02-13 20:06:08 -0800211 // we only add extensions that exist in the implementation
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800212 char const* start = sExtensionString;
213 char const* end;
214 do {
215 // find the space separating this extension for the next one
216 end = strchr(start, ' ');
217 if (end) {
218 // length of the extension string
219 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800220 if (len) {
221 // NOTE: we could avoid the copy if we had strnstr.
222 const String8 ext(start, len);
Mathias Agopianada798b2012-02-13 17:09:30 -0800223 // now look for this extension
224 if (disp.queryString.extensions) {
225 // if we find it, add this extension string to our list
226 // (and don't forget the space)
227 const char* match = strstr(disp.queryString.extensions, ext.string());
228 if (match && (match[len] == ' ' || match[len] == 0)) {
229 mExtensionString.append(start, len+1);
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800230 }
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800231 }
232 }
233 // process the next extension string, and skip the space.
234 start = end + 1;
235 }
236 } while (end);
237
Jamie Gennisaca51c02011-11-03 17:42:43 -0700238 egl_cache_t::get()->initialize(this);
239
Mathias Agopian7db993a2012-03-25 00:49:46 -0700240 char value[PROPERTY_VALUE_MAX];
241 property_get("debug.egl.finish", value, "0");
242 if (atoi(value)) {
243 finishOnSwap = true;
244 }
245
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700246 property_get("debug.egl.traceGpuCompletion", value, "0");
247 if (atoi(value)) {
248 traceGpuCompletion = true;
249 }
250
Mathias Agopian7773c432012-02-13 20:06:08 -0800251 refs++;
252 if (major != NULL)
253 *major = VERSION_MAJOR;
254 if (minor != NULL)
255 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700256
257 mHibernation.setDisplayValid(true);
258
Mathias Agopian7773c432012-02-13 20:06:08 -0800259 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700260}
261
262EGLBoolean egl_display_t::terminate() {
263
264 Mutex::Autolock _l(lock);
265
266 if (refs == 0) {
Mathias Agopianfe981272012-06-13 15:21:21 -0700267 /*
268 * From the EGL spec (3.2):
269 * "Termination of a display that has already been terminated,
270 * (...), is allowed, but the only effect of such a call is
271 * to return EGL_TRUE (...)
272 */
273 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700274 }
275
276 // this is specific to Android, display termination is ref-counted.
277 if (refs > 1) {
278 refs--;
279 return EGL_TRUE;
280 }
281
282 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800283 egl_connection_t* const cnx = &gEGLImpl;
284 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
285 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
286 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
287 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700288 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800289 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800290 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800291 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700292 }
293
Jesse Halla0fef1c2012-04-17 12:02:26 -0700294 mHibernation.setDisplayValid(false);
295
Jamie Gennisa08cf6e2012-09-16 14:02:20 -0700296 // Reset the extension string since it will be regenerated if we get
297 // reinitialized.
298 mExtensionString.setTo("");
299
Mathias Agopian5b287a62011-05-16 18:58:55 -0700300 // Mark all objects remaining in the list as terminated, unless
301 // there are no reference to them, it which case, we're free to
302 // delete them.
303 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000304 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700305 for (size_t i=0 ; i<count ; i++) {
306 egl_object_t* o = objects.itemAt(i);
307 o->destroy();
308 }
309
310 // this marks all object handles are "terminated"
311 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700312
313 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700314 return res;
315}
316
Mathias Agopianfb87e542012-01-30 18:20:52 -0800317void egl_display_t::loseCurrent(egl_context_t * cur_c)
318{
319 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800320 egl_display_t* display = cur_c->getDisplay();
321 if (display) {
322 display->loseCurrentImpl(cur_c);
323 }
324 }
325}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800326
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800327void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
328{
329 // by construction, these are either 0 or valid (possibly terminated)
330 // it should be impossible for these to be invalid
331 ContextRef _cur_c(cur_c);
332 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
333 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800334
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800335 { // scope for the lock
336 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800337 cur_c->onLooseCurrent();
338
Mathias Agopianfb87e542012-01-30 18:20:52 -0800339 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800340
341 // This cannot be called with the lock held because it might end-up
342 // calling back into EGL (in particular when a surface is destroyed
343 // it calls ANativeWindow::disconnect
344 _cur_c.release();
345 _cur_r.release();
346 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800347}
348
349EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
350 EGLSurface draw, EGLSurface read, EGLContext ctx,
351 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
352{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800353 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800354
355 // by construction, these are either 0 or valid (possibly terminated)
356 // it should be impossible for these to be invalid
357 ContextRef _cur_c(cur_c);
358 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
359 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
360
361 { // scope for the lock
362 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800363 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800364 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800365 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800366 if (result == EGL_TRUE) {
367 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700368 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700369 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700370 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800371 }
372 } else {
373 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800374 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800375 if (result == EGL_TRUE) {
376 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700377 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800378 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800379 }
380 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800381
382 if (result == EGL_TRUE) {
383 // This cannot be called with the lock held because it might end-up
384 // calling back into EGL (in particular when a surface is destroyed
385 // it calls ANativeWindow::disconnect
386 _cur_c.release();
387 _cur_r.release();
388 _cur_d.release();
389 }
390
Mathias Agopianfb87e542012-01-30 18:20:52 -0800391 return result;
392}
Mathias Agopian518ec112011-05-13 16:21:08 -0700393
Jesse Halla0fef1c2012-04-17 12:02:26 -0700394// ----------------------------------------------------------------------------
395
396bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
397 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700398 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
399 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700400
Jesse Hallb29e5e82012-04-04 16:53:42 -0700401 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700402 if (strength == STRONG)
403 mAttemptHibernation = false;
404
Jesse Hall25838592012-04-05 15:53:28 -0700405 if (CC_UNLIKELY(mHibernating)) {
406 ALOGV("Awakening\n");
407 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700408
409 // These conditions should be guaranteed before entering hibernation;
410 // we don't want to get into a state where we can't wake up.
411 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
412 "Invalid hibernation state, unable to awaken\n");
413
Jesse Hall25838592012-04-05 15:53:28 -0700414 if (!cnx->egl.eglAwakenProcessIMG()) {
415 ALOGE("Failed to awaken EGL implementation\n");
416 return false;
417 }
418 mHibernating = false;
419 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700420 return true;
421}
422
Jesse Halla0fef1c2012-04-17 12:02:26 -0700423void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
424 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700425 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700426
427 mWakeCount--;
428 if (strength == STRONG)
429 mAttemptHibernation = true;
430
431 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700432 egl_connection_t* const cnx = &gEGLImpl;
433 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700434 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700435 cnx->egl.eglHibernateProcessIMG &&
436 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700437 ALOGV("Hibernating\n");
438 if (!cnx->egl.eglHibernateProcessIMG()) {
439 ALOGE("Failed to hibernate EGL implementation\n");
440 return;
441 }
442 mHibernating = true;
443 }
444 }
445}
446
Jesse Halla0fef1c2012-04-17 12:02:26 -0700447void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
448 Mutex::Autolock _l(mLock);
449 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700450}
451
Mathias Agopian518ec112011-05-13 16:21:08 -0700452// ----------------------------------------------------------------------------
453}; // namespace android
454// ----------------------------------------------------------------------------