blob: 8fd5af161d31bb8f82da75c10b829ff905bde28d [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 "
Jamie Gennis09b11432012-09-20 15:47:44 -070055 "EGL_EXT_create_context_robustness "
Mathias Agopian4b9511c2011-11-13 23:52:47 -080056 "EGL_NV_system_time "
57 "EGL_ANDROID_image_native_buffer " // mandatory
58 ;
59
60// extensions not exposed to applications but used by the ANDROID system
61// "EGL_ANDROID_recordable " // mandatory
62// "EGL_ANDROID_blob_cache " // strongly recommended
Jamie Gennis331841b2012-09-06 14:52:00 -070063// "EGL_ANDROID_native_fence_sync " // strongly recommended
Jesse Hall25838592012-04-05 15:53:28 -070064// "EGL_IMG_hibernate_process " // optional
Mathias Agopian4b9511c2011-11-13 23:52:47 -080065
Mathias Agopian518ec112011-05-13 16:21:08 -070066extern void initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -080067extern void initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -070068extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
69
Mathias Agopian518ec112011-05-13 16:21:08 -070070// ----------------------------------------------------------------------------
71
72egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
73
74egl_display_t::egl_display_t() :
Jesse Halla0fef1c2012-04-17 12:02:26 -070075 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
Mathias Agopian518ec112011-05-13 16:21:08 -070076}
77
78egl_display_t::~egl_display_t() {
79 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080080 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070081}
82
83egl_display_t* egl_display_t::get(EGLDisplay dpy) {
84 uintptr_t index = uintptr_t(dpy)-1U;
85 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
86}
87
88void egl_display_t::addObject(egl_object_t* object) {
89 Mutex::Autolock _l(lock);
90 objects.add(object);
91}
92
Mathias Agopian5b287a62011-05-16 18:58:55 -070093void egl_display_t::removeObject(egl_object_t* object) {
94 Mutex::Autolock _l(lock);
95 objects.remove(object);
96}
97
Mathias Agopianf0480de2011-11-13 20:50:07 -080098bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070099 Mutex::Autolock _l(lock);
100 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800101 if (object->getDisplay() == this) {
102 object->incRef();
103 return true;
104 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700105 }
106 return false;
107}
108
Mathias Agopian518ec112011-05-13 16:21:08 -0700109EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
110 if (uintptr_t(disp) >= NUM_DISPLAYS)
111 return NULL;
112
113 return sDisplay[uintptr_t(disp)].getDisplay(disp);
114}
115
116EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
117
118 Mutex::Autolock _l(lock);
119
120 // get our driver loader
121 Loader& loader(Loader::getInstance());
122
Mathias Agopianada798b2012-02-13 17:09:30 -0800123 egl_connection_t* const cnx = &gEGLImpl;
124 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
125 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
126 disp.dpy = dpy;
127 if (dpy == EGL_NO_DISPLAY) {
128 loader.close(cnx->dso);
129 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700130 }
131 }
132
133 return EGLDisplay(uintptr_t(display) + 1U);
134}
135
136EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
137
138 Mutex::Autolock _l(lock);
139
140 if (refs > 0) {
141 if (major != NULL)
142 *major = VERSION_MAJOR;
143 if (minor != NULL)
144 *minor = VERSION_MINOR;
145 refs++;
146 return EGL_TRUE;
147 }
148
149#if EGL_TRACE
150
151 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
152 // the information from that call may be stale.)
153 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800154 initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -0700155
156#endif
157
158 setGLHooksThreadSpecific(&gHooksNoContext);
159
160 // initialize each EGL and
161 // build our own extension string first, based on the extension we know
162 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800163
164 egl_connection_t* const cnx = &gEGLImpl;
165 cnx->major = -1;
166 cnx->minor = -1;
167 if (cnx->dso) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700168
169#if defined(ADRENO130)
170#warning "Adreno-130 eglInitialize() workaround"
171 /*
172 * The ADRENO 130 driver returns a different EGLDisplay each time
173 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
174 * after eglTerminate() has been called, so that eglInitialize()
175 * cannot be called again. Therefore, we need to make sure to call
176 * eglGetDisplay() before calling eglInitialize();
177 */
178 if (i == IMPL_HARDWARE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800179 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian518ec112011-05-13 16:21:08 -0700180 }
181#endif
182
Mathias Agopianada798b2012-02-13 17:09:30 -0800183 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700184 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800185 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
186 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700187
188 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800189 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700190
191 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800192 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700193 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800194 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700195 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800196 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700197 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800198 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700199 EGL_CLIENT_APIS);
200
201 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800202 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700203 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
204 }
205 }
206
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800207 // the query strings are per-display
208 mVendorString.setTo(sVendorString);
209 mVersionString.setTo(sVersionString);
210 mClientApiString.setTo(sClientApiString);
211
Mathias Agopian7773c432012-02-13 20:06:08 -0800212 // we only add extensions that exist in the implementation
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800213 char const* start = sExtensionString;
214 char const* end;
215 do {
216 // find the space separating this extension for the next one
217 end = strchr(start, ' ');
218 if (end) {
219 // length of the extension string
220 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800221 if (len) {
222 // NOTE: we could avoid the copy if we had strnstr.
223 const String8 ext(start, len);
Mathias Agopianada798b2012-02-13 17:09:30 -0800224 // now look for this extension
225 if (disp.queryString.extensions) {
226 // if we find it, add this extension string to our list
227 // (and don't forget the space)
228 const char* match = strstr(disp.queryString.extensions, ext.string());
229 if (match && (match[len] == ' ' || match[len] == 0)) {
230 mExtensionString.append(start, len+1);
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800231 }
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800232 }
233 }
234 // process the next extension string, and skip the space.
235 start = end + 1;
236 }
237 } while (end);
238
Jamie Gennisaca51c02011-11-03 17:42:43 -0700239 egl_cache_t::get()->initialize(this);
240
Mathias Agopian7db993a2012-03-25 00:49:46 -0700241 char value[PROPERTY_VALUE_MAX];
242 property_get("debug.egl.finish", value, "0");
243 if (atoi(value)) {
244 finishOnSwap = true;
245 }
246
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700247 property_get("debug.egl.traceGpuCompletion", value, "0");
248 if (atoi(value)) {
249 traceGpuCompletion = true;
250 }
251
Mathias Agopian7773c432012-02-13 20:06:08 -0800252 refs++;
253 if (major != NULL)
254 *major = VERSION_MAJOR;
255 if (minor != NULL)
256 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700257
258 mHibernation.setDisplayValid(true);
259
Mathias Agopian7773c432012-02-13 20:06:08 -0800260 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700261}
262
263EGLBoolean egl_display_t::terminate() {
264
265 Mutex::Autolock _l(lock);
266
267 if (refs == 0) {
Mathias Agopianfe981272012-06-13 15:21:21 -0700268 /*
269 * From the EGL spec (3.2):
270 * "Termination of a display that has already been terminated,
271 * (...), is allowed, but the only effect of such a call is
272 * to return EGL_TRUE (...)
273 */
274 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700275 }
276
277 // this is specific to Android, display termination is ref-counted.
278 if (refs > 1) {
279 refs--;
280 return EGL_TRUE;
281 }
282
283 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800284 egl_connection_t* const cnx = &gEGLImpl;
285 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
286 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
287 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
288 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700289 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800290 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800291 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800292 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700293 }
294
Jesse Halla0fef1c2012-04-17 12:02:26 -0700295 mHibernation.setDisplayValid(false);
296
Jamie Gennisa08cf6e2012-09-16 14:02:20 -0700297 // Reset the extension string since it will be regenerated if we get
298 // reinitialized.
299 mExtensionString.setTo("");
300
Mathias Agopian5b287a62011-05-16 18:58:55 -0700301 // Mark all objects remaining in the list as terminated, unless
302 // there are no reference to them, it which case, we're free to
303 // delete them.
304 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000305 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700306 for (size_t i=0 ; i<count ; i++) {
307 egl_object_t* o = objects.itemAt(i);
308 o->destroy();
309 }
310
311 // this marks all object handles are "terminated"
312 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700313
314 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700315 return res;
316}
317
Mathias Agopianfb87e542012-01-30 18:20:52 -0800318void egl_display_t::loseCurrent(egl_context_t * cur_c)
319{
320 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800321 egl_display_t* display = cur_c->getDisplay();
322 if (display) {
323 display->loseCurrentImpl(cur_c);
324 }
325 }
326}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800327
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800328void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
329{
330 // by construction, these are either 0 or valid (possibly terminated)
331 // it should be impossible for these to be invalid
332 ContextRef _cur_c(cur_c);
333 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
334 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800335
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800336 { // scope for the lock
337 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800338 cur_c->onLooseCurrent();
339
Mathias Agopianfb87e542012-01-30 18:20:52 -0800340 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800341
342 // This cannot be called with the lock held because it might end-up
343 // calling back into EGL (in particular when a surface is destroyed
344 // it calls ANativeWindow::disconnect
345 _cur_c.release();
346 _cur_r.release();
347 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800348}
349
350EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
351 EGLSurface draw, EGLSurface read, EGLContext ctx,
352 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
353{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800354 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355
356 // by construction, these are either 0 or valid (possibly terminated)
357 // it should be impossible for these to be invalid
358 ContextRef _cur_c(cur_c);
359 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
360 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
361
362 { // scope for the lock
363 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800364 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800365 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800366 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800367 if (result == EGL_TRUE) {
368 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700369 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700370 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700371 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800372 }
373 } else {
374 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800375 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800376 if (result == EGL_TRUE) {
377 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700378 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800379 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800380 }
381 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800382
383 if (result == EGL_TRUE) {
384 // This cannot be called with the lock held because it might end-up
385 // calling back into EGL (in particular when a surface is destroyed
386 // it calls ANativeWindow::disconnect
387 _cur_c.release();
388 _cur_r.release();
389 _cur_d.release();
390 }
391
Mathias Agopianfb87e542012-01-30 18:20:52 -0800392 return result;
393}
Mathias Agopian518ec112011-05-13 16:21:08 -0700394
Jesse Halla0fef1c2012-04-17 12:02:26 -0700395// ----------------------------------------------------------------------------
396
397bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
398 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700399 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
400 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700401
Jesse Hallb29e5e82012-04-04 16:53:42 -0700402 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700403 if (strength == STRONG)
404 mAttemptHibernation = false;
405
Jesse Hall25838592012-04-05 15:53:28 -0700406 if (CC_UNLIKELY(mHibernating)) {
407 ALOGV("Awakening\n");
408 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700409
410 // These conditions should be guaranteed before entering hibernation;
411 // we don't want to get into a state where we can't wake up.
412 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
413 "Invalid hibernation state, unable to awaken\n");
414
Jesse Hall25838592012-04-05 15:53:28 -0700415 if (!cnx->egl.eglAwakenProcessIMG()) {
416 ALOGE("Failed to awaken EGL implementation\n");
417 return false;
418 }
419 mHibernating = false;
420 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700421 return true;
422}
423
Jesse Halla0fef1c2012-04-17 12:02:26 -0700424void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
425 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700426 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700427
428 mWakeCount--;
429 if (strength == STRONG)
430 mAttemptHibernation = true;
431
432 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700433 egl_connection_t* const cnx = &gEGLImpl;
434 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700435 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700436 cnx->egl.eglHibernateProcessIMG &&
437 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700438 ALOGV("Hibernating\n");
439 if (!cnx->egl.eglHibernateProcessIMG()) {
440 ALOGE("Failed to hibernate EGL implementation\n");
441 return;
442 }
443 mHibernating = true;
444 }
445 }
446}
447
Jesse Halla0fef1c2012-04-17 12:02:26 -0700448void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
449 Mutex::Autolock _l(mLock);
450 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700451}
452
Mathias Agopian518ec112011-05-13 16:21:08 -0700453// ----------------------------------------------------------------------------
454}; // namespace android
455// ----------------------------------------------------------------------------