blob: f2e3897bc506a80d0bfb6ef1ca4a8a1ce4303f43 [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";
35static char const * const sClientApiString = "OpenGL ES";
36
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
Jesse Hall25838592012-04-05 15:53:28 -070062// "EGL_IMG_hibernate_process " // optional
Mathias Agopian4b9511c2011-11-13 23:52:47 -080063
Mathias Agopian518ec112011-05-13 16:21:08 -070064extern void initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -080065extern void initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -070066extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
67
Mathias Agopian518ec112011-05-13 16:21:08 -070068// ----------------------------------------------------------------------------
69
70egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
71
72egl_display_t::egl_display_t() :
Jesse Halla0fef1c2012-04-17 12:02:26 -070073 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
Mathias Agopian518ec112011-05-13 16:21:08 -070074}
75
76egl_display_t::~egl_display_t() {
77 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080078 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070079}
80
81egl_display_t* egl_display_t::get(EGLDisplay dpy) {
82 uintptr_t index = uintptr_t(dpy)-1U;
83 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
84}
85
86void egl_display_t::addObject(egl_object_t* object) {
87 Mutex::Autolock _l(lock);
88 objects.add(object);
89}
90
Mathias Agopian5b287a62011-05-16 18:58:55 -070091void egl_display_t::removeObject(egl_object_t* object) {
92 Mutex::Autolock _l(lock);
93 objects.remove(object);
94}
95
Mathias Agopianf0480de2011-11-13 20:50:07 -080096bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070097 Mutex::Autolock _l(lock);
98 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080099 if (object->getDisplay() == this) {
100 object->incRef();
101 return true;
102 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700103 }
104 return false;
105}
106
Mathias Agopian518ec112011-05-13 16:21:08 -0700107EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
108 if (uintptr_t(disp) >= NUM_DISPLAYS)
109 return NULL;
110
111 return sDisplay[uintptr_t(disp)].getDisplay(disp);
112}
113
114EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
115
116 Mutex::Autolock _l(lock);
117
118 // get our driver loader
119 Loader& loader(Loader::getInstance());
120
Mathias Agopianada798b2012-02-13 17:09:30 -0800121 egl_connection_t* const cnx = &gEGLImpl;
122 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
123 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
124 disp.dpy = dpy;
125 if (dpy == EGL_NO_DISPLAY) {
126 loader.close(cnx->dso);
127 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700128 }
129 }
130
131 return EGLDisplay(uintptr_t(display) + 1U);
132}
133
134EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
135
136 Mutex::Autolock _l(lock);
137
138 if (refs > 0) {
139 if (major != NULL)
140 *major = VERSION_MAJOR;
141 if (minor != NULL)
142 *minor = VERSION_MINOR;
143 refs++;
144 return EGL_TRUE;
145 }
146
147#if EGL_TRACE
148
149 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
150 // the information from that call may be stale.)
151 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800152 initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -0700153
154#endif
155
156 setGLHooksThreadSpecific(&gHooksNoContext);
157
158 // initialize each EGL and
159 // build our own extension string first, based on the extension we know
160 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800161
162 egl_connection_t* const cnx = &gEGLImpl;
163 cnx->major = -1;
164 cnx->minor = -1;
165 if (cnx->dso) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700166
167#if defined(ADRENO130)
168#warning "Adreno-130 eglInitialize() workaround"
169 /*
170 * The ADRENO 130 driver returns a different EGLDisplay each time
171 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
172 * after eglTerminate() has been called, so that eglInitialize()
173 * cannot be called again. Therefore, we need to make sure to call
174 * eglGetDisplay() before calling eglInitialize();
175 */
176 if (i == IMPL_HARDWARE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800177 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian518ec112011-05-13 16:21:08 -0700178 }
179#endif
180
Mathias Agopianada798b2012-02-13 17:09:30 -0800181 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700182 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800183 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
184 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700185
186 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800187 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700188
189 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800190 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700191 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800192 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700193 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800194 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700195 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800196 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700197 EGL_CLIENT_APIS);
198
199 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800200 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700201 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
202 }
203 }
204
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800205 // the query strings are per-display
206 mVendorString.setTo(sVendorString);
207 mVersionString.setTo(sVersionString);
208 mClientApiString.setTo(sClientApiString);
209
Mathias Agopian7773c432012-02-13 20:06:08 -0800210 // we only add extensions that exist in the implementation
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800211 char const* start = sExtensionString;
212 char const* end;
213 do {
214 // find the space separating this extension for the next one
215 end = strchr(start, ' ');
216 if (end) {
217 // length of the extension string
218 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800219 if (len) {
220 // NOTE: we could avoid the copy if we had strnstr.
221 const String8 ext(start, len);
Mathias Agopianada798b2012-02-13 17:09:30 -0800222 // now look for this extension
223 if (disp.queryString.extensions) {
224 // if we find it, add this extension string to our list
225 // (and don't forget the space)
226 const char* match = strstr(disp.queryString.extensions, ext.string());
227 if (match && (match[len] == ' ' || match[len] == 0)) {
228 mExtensionString.append(start, len+1);
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800229 }
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800230 }
231 }
232 // process the next extension string, and skip the space.
233 start = end + 1;
234 }
235 } while (end);
236
Jamie Gennisaca51c02011-11-03 17:42:43 -0700237 egl_cache_t::get()->initialize(this);
238
Mathias Agopian7db993a2012-03-25 00:49:46 -0700239 char value[PROPERTY_VALUE_MAX];
240 property_get("debug.egl.finish", value, "0");
241 if (atoi(value)) {
242 finishOnSwap = true;
243 }
244
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700245 property_get("debug.egl.traceGpuCompletion", value, "0");
246 if (atoi(value)) {
247 traceGpuCompletion = true;
248 }
249
Mathias Agopian7773c432012-02-13 20:06:08 -0800250 refs++;
251 if (major != NULL)
252 *major = VERSION_MAJOR;
253 if (minor != NULL)
254 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700255
256 mHibernation.setDisplayValid(true);
257
Mathias Agopian7773c432012-02-13 20:06:08 -0800258 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700259}
260
261EGLBoolean egl_display_t::terminate() {
262
263 Mutex::Autolock _l(lock);
264
265 if (refs == 0) {
266 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
267 }
268
269 // this is specific to Android, display termination is ref-counted.
270 if (refs > 1) {
271 refs--;
272 return EGL_TRUE;
273 }
274
275 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800276 egl_connection_t* const cnx = &gEGLImpl;
277 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
278 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
279 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
280 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700281 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800282 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800283 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800284 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700285 }
286
Jesse Halla0fef1c2012-04-17 12:02:26 -0700287 mHibernation.setDisplayValid(false);
288
Mathias Agopian5b287a62011-05-16 18:58:55 -0700289 // Mark all objects remaining in the list as terminated, unless
290 // there are no reference to them, it which case, we're free to
291 // delete them.
292 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000293 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700294 for (size_t i=0 ; i<count ; i++) {
295 egl_object_t* o = objects.itemAt(i);
296 o->destroy();
297 }
298
299 // this marks all object handles are "terminated"
300 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700301
302 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700303 return res;
304}
305
Mathias Agopianfb87e542012-01-30 18:20:52 -0800306void egl_display_t::loseCurrent(egl_context_t * cur_c)
307{
308 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800309 egl_display_t* display = cur_c->getDisplay();
310 if (display) {
311 display->loseCurrentImpl(cur_c);
312 }
313 }
314}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800315
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800316void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
317{
318 // by construction, these are either 0 or valid (possibly terminated)
319 // it should be impossible for these to be invalid
320 ContextRef _cur_c(cur_c);
321 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
322 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800323
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800324 { // scope for the lock
325 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800326 cur_c->onLooseCurrent();
327
Mathias Agopianfb87e542012-01-30 18:20:52 -0800328 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800329
330 // This cannot be called with the lock held because it might end-up
331 // calling back into EGL (in particular when a surface is destroyed
332 // it calls ANativeWindow::disconnect
333 _cur_c.release();
334 _cur_r.release();
335 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800336}
337
338EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
339 EGLSurface draw, EGLSurface read, EGLContext ctx,
340 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
341{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800342 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800343
344 // by construction, these are either 0 or valid (possibly terminated)
345 // it should be impossible for these to be invalid
346 ContextRef _cur_c(cur_c);
347 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
348 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
349
350 { // scope for the lock
351 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800352 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800353 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800354 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355 if (result == EGL_TRUE) {
356 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700357 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700358 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700359 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800360 }
361 } else {
362 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800363 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800364 if (result == EGL_TRUE) {
365 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700366 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800367 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800368 }
369 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800370
371 if (result == EGL_TRUE) {
372 // This cannot be called with the lock held because it might end-up
373 // calling back into EGL (in particular when a surface is destroyed
374 // it calls ANativeWindow::disconnect
375 _cur_c.release();
376 _cur_r.release();
377 _cur_d.release();
378 }
379
Mathias Agopianfb87e542012-01-30 18:20:52 -0800380 return result;
381}
Mathias Agopian518ec112011-05-13 16:21:08 -0700382
Jesse Halla0fef1c2012-04-17 12:02:26 -0700383// ----------------------------------------------------------------------------
384
385bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
386 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700387 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
388 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700389
Jesse Hallb29e5e82012-04-04 16:53:42 -0700390 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700391 if (strength == STRONG)
392 mAttemptHibernation = false;
393
Jesse Hall25838592012-04-05 15:53:28 -0700394 if (CC_UNLIKELY(mHibernating)) {
395 ALOGV("Awakening\n");
396 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700397
398 // These conditions should be guaranteed before entering hibernation;
399 // we don't want to get into a state where we can't wake up.
400 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
401 "Invalid hibernation state, unable to awaken\n");
402
Jesse Hall25838592012-04-05 15:53:28 -0700403 if (!cnx->egl.eglAwakenProcessIMG()) {
404 ALOGE("Failed to awaken EGL implementation\n");
405 return false;
406 }
407 mHibernating = false;
408 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700409 return true;
410}
411
Jesse Halla0fef1c2012-04-17 12:02:26 -0700412void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
413 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700414 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700415
416 mWakeCount--;
417 if (strength == STRONG)
418 mAttemptHibernation = true;
419
420 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700421 egl_connection_t* const cnx = &gEGLImpl;
422 mAttemptHibernation = false;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700423 if (mDpyValid &&
424 cnx->egl.eglHibernateProcessIMG &&
425 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700426 ALOGV("Hibernating\n");
427 if (!cnx->egl.eglHibernateProcessIMG()) {
428 ALOGE("Failed to hibernate EGL implementation\n");
429 return;
430 }
431 mHibernating = true;
432 }
433 }
434}
435
Jesse Halla0fef1c2012-04-17 12:02:26 -0700436void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
437 Mutex::Autolock _l(mLock);
438 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700439}
440
Mathias Agopian518ec112011-05-13 16:21:08 -0700441// ----------------------------------------------------------------------------
442}; // namespace android
443// ----------------------------------------------------------------------------