blob: 1955904c77e426ac0c2b3385dbd851c7522675c4 [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
Mathias Agopian39c24a22013-04-04 23:17:56 -070021#include "../egl_impl.h"
22
Jamie Gennisaca51c02011-11-03 17:42:43 -070023#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070024#include "egl_display.h"
25#include "egl_object.h"
26#include "egl_tls.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070027#include "Loader.h"
Mathias Agopian7db993a2012-03-25 00:49:46 -070028#include <cutils/properties.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070029
30// ----------------------------------------------------------------------------
31namespace android {
32// ----------------------------------------------------------------------------
33
Mathias Agopian4b9511c2011-11-13 23:52:47 -080034static char const * const sVendorString = "Android";
35static char const * const sVersionString = "1.4 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070036static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080037
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070038extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080039
Mathias Agopian518ec112011-05-13 16:21:08 -070040extern void initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -080041extern void initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -070042extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
43
Mathias Agopian518ec112011-05-13 16:21:08 -070044// ----------------------------------------------------------------------------
45
46egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
47
48egl_display_t::egl_display_t() :
Jesse Halla0fef1c2012-04-17 12:02:26 -070049 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
Mathias Agopian518ec112011-05-13 16:21:08 -070050}
51
52egl_display_t::~egl_display_t() {
53 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080054 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070055}
56
57egl_display_t* egl_display_t::get(EGLDisplay dpy) {
58 uintptr_t index = uintptr_t(dpy)-1U;
59 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
60}
61
62void egl_display_t::addObject(egl_object_t* object) {
63 Mutex::Autolock _l(lock);
64 objects.add(object);
65}
66
Mathias Agopian5b287a62011-05-16 18:58:55 -070067void egl_display_t::removeObject(egl_object_t* object) {
68 Mutex::Autolock _l(lock);
69 objects.remove(object);
70}
71
Mathias Agopianf0480de2011-11-13 20:50:07 -080072bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070073 Mutex::Autolock _l(lock);
74 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080075 if (object->getDisplay() == this) {
76 object->incRef();
77 return true;
78 }
Mathias Agopian518ec112011-05-13 16:21:08 -070079 }
80 return false;
81}
82
Mathias Agopian518ec112011-05-13 16:21:08 -070083EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
84 if (uintptr_t(disp) >= NUM_DISPLAYS)
85 return NULL;
86
87 return sDisplay[uintptr_t(disp)].getDisplay(disp);
88}
89
90EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
91
92 Mutex::Autolock _l(lock);
93
94 // get our driver loader
95 Loader& loader(Loader::getInstance());
96
Mathias Agopianada798b2012-02-13 17:09:30 -080097 egl_connection_t* const cnx = &gEGLImpl;
98 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
99 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
100 disp.dpy = dpy;
101 if (dpy == EGL_NO_DISPLAY) {
102 loader.close(cnx->dso);
103 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700104 }
105 }
106
107 return EGLDisplay(uintptr_t(display) + 1U);
108}
109
110EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
111
112 Mutex::Autolock _l(lock);
113
114 if (refs > 0) {
115 if (major != NULL)
116 *major = VERSION_MAJOR;
117 if (minor != NULL)
118 *minor = VERSION_MINOR;
119 refs++;
120 return EGL_TRUE;
121 }
122
123#if EGL_TRACE
124
125 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
126 // the information from that call may be stale.)
127 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800128 initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -0700129
130#endif
131
132 setGLHooksThreadSpecific(&gHooksNoContext);
133
134 // initialize each EGL and
135 // build our own extension string first, based on the extension we know
136 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800137
138 egl_connection_t* const cnx = &gEGLImpl;
139 cnx->major = -1;
140 cnx->minor = -1;
141 if (cnx->dso) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700142
143#if defined(ADRENO130)
144#warning "Adreno-130 eglInitialize() workaround"
145 /*
146 * The ADRENO 130 driver returns a different EGLDisplay each time
147 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
148 * after eglTerminate() has been called, so that eglInitialize()
149 * cannot be called again. Therefore, we need to make sure to call
150 * eglGetDisplay() before calling eglInitialize();
151 */
152 if (i == IMPL_HARDWARE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800153 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 }
155#endif
156
Mathias Agopianada798b2012-02-13 17:09:30 -0800157 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700158 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800159 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
160 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700161
162 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800163 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700164
165 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800166 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700167 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800168 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700169 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800170 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700171 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800172 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700173 EGL_CLIENT_APIS);
174
175 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800176 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700177 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
178 }
179 }
180
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800181 // the query strings are per-display
182 mVendorString.setTo(sVendorString);
183 mVersionString.setTo(sVersionString);
184 mClientApiString.setTo(sClientApiString);
185
Mathias Agopian7773c432012-02-13 20:06:08 -0800186 // we only add extensions that exist in the implementation
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700187 char const* start = gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800188 char const* end;
189 do {
190 // find the space separating this extension for the next one
191 end = strchr(start, ' ');
192 if (end) {
193 // length of the extension string
194 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800195 if (len) {
196 // NOTE: we could avoid the copy if we had strnstr.
197 const String8 ext(start, len);
Mathias Agopianada798b2012-02-13 17:09:30 -0800198 // now look for this extension
199 if (disp.queryString.extensions) {
200 // if we find it, add this extension string to our list
201 // (and don't forget the space)
202 const char* match = strstr(disp.queryString.extensions, ext.string());
203 if (match && (match[len] == ' ' || match[len] == 0)) {
204 mExtensionString.append(start, len+1);
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800205 }
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800206 }
207 }
208 // process the next extension string, and skip the space.
209 start = end + 1;
210 }
211 } while (end);
212
Jamie Gennisaca51c02011-11-03 17:42:43 -0700213 egl_cache_t::get()->initialize(this);
214
Mathias Agopian7db993a2012-03-25 00:49:46 -0700215 char value[PROPERTY_VALUE_MAX];
216 property_get("debug.egl.finish", value, "0");
217 if (atoi(value)) {
218 finishOnSwap = true;
219 }
220
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700221 property_get("debug.egl.traceGpuCompletion", value, "0");
222 if (atoi(value)) {
223 traceGpuCompletion = true;
224 }
225
Mathias Agopian7773c432012-02-13 20:06:08 -0800226 refs++;
227 if (major != NULL)
228 *major = VERSION_MAJOR;
229 if (minor != NULL)
230 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700231
232 mHibernation.setDisplayValid(true);
233
Mathias Agopian7773c432012-02-13 20:06:08 -0800234 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700235}
236
237EGLBoolean egl_display_t::terminate() {
238
239 Mutex::Autolock _l(lock);
240
241 if (refs == 0) {
Mathias Agopianfe981272012-06-13 15:21:21 -0700242 /*
243 * From the EGL spec (3.2):
244 * "Termination of a display that has already been terminated,
245 * (...), is allowed, but the only effect of such a call is
246 * to return EGL_TRUE (...)
247 */
248 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700249 }
250
251 // this is specific to Android, display termination is ref-counted.
252 if (refs > 1) {
253 refs--;
254 return EGL_TRUE;
255 }
256
257 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800258 egl_connection_t* const cnx = &gEGLImpl;
259 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
260 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
261 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
262 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700263 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800264 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800265 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800266 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700267 }
268
Jesse Halla0fef1c2012-04-17 12:02:26 -0700269 mHibernation.setDisplayValid(false);
270
Jamie Gennisa08cf6e2012-09-16 14:02:20 -0700271 // Reset the extension string since it will be regenerated if we get
272 // reinitialized.
273 mExtensionString.setTo("");
274
Mathias Agopian5b287a62011-05-16 18:58:55 -0700275 // Mark all objects remaining in the list as terminated, unless
276 // there are no reference to them, it which case, we're free to
277 // delete them.
278 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000279 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700280 for (size_t i=0 ; i<count ; i++) {
281 egl_object_t* o = objects.itemAt(i);
282 o->destroy();
283 }
284
285 // this marks all object handles are "terminated"
286 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700287
288 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700289 return res;
290}
291
Mathias Agopianfb87e542012-01-30 18:20:52 -0800292void egl_display_t::loseCurrent(egl_context_t * cur_c)
293{
294 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800295 egl_display_t* display = cur_c->getDisplay();
296 if (display) {
297 display->loseCurrentImpl(cur_c);
298 }
299 }
300}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800301
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800302void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
303{
304 // by construction, these are either 0 or valid (possibly terminated)
305 // it should be impossible for these to be invalid
306 ContextRef _cur_c(cur_c);
307 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
308 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800309
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800310 { // scope for the lock
311 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800312 cur_c->onLooseCurrent();
313
Mathias Agopianfb87e542012-01-30 18:20:52 -0800314 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800315
316 // This cannot be called with the lock held because it might end-up
317 // calling back into EGL (in particular when a surface is destroyed
318 // it calls ANativeWindow::disconnect
319 _cur_c.release();
320 _cur_r.release();
321 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800322}
323
324EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
325 EGLSurface draw, EGLSurface read, EGLContext ctx,
326 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
327{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800328 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800329
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);
335
336 { // scope for the lock
337 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800338 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800339 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800340 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800341 if (result == EGL_TRUE) {
342 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700343 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700344 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700345 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800346 }
347 } else {
348 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800349 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800350 if (result == EGL_TRUE) {
351 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700352 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800353 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800354 }
355 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800356
357 if (result == EGL_TRUE) {
358 // This cannot be called with the lock held because it might end-up
359 // calling back into EGL (in particular when a surface is destroyed
360 // it calls ANativeWindow::disconnect
361 _cur_c.release();
362 _cur_r.release();
363 _cur_d.release();
364 }
365
Mathias Agopianfb87e542012-01-30 18:20:52 -0800366 return result;
367}
Mathias Agopian518ec112011-05-13 16:21:08 -0700368
Jesse Halla0fef1c2012-04-17 12:02:26 -0700369// ----------------------------------------------------------------------------
370
371bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
372 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700373 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
374 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700375
Jesse Hallb29e5e82012-04-04 16:53:42 -0700376 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700377 if (strength == STRONG)
378 mAttemptHibernation = false;
379
Jesse Hall25838592012-04-05 15:53:28 -0700380 if (CC_UNLIKELY(mHibernating)) {
381 ALOGV("Awakening\n");
382 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700383
384 // These conditions should be guaranteed before entering hibernation;
385 // we don't want to get into a state where we can't wake up.
386 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
387 "Invalid hibernation state, unable to awaken\n");
388
Jesse Hall25838592012-04-05 15:53:28 -0700389 if (!cnx->egl.eglAwakenProcessIMG()) {
390 ALOGE("Failed to awaken EGL implementation\n");
391 return false;
392 }
393 mHibernating = false;
394 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700395 return true;
396}
397
Jesse Halla0fef1c2012-04-17 12:02:26 -0700398void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
399 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700400 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700401
402 mWakeCount--;
403 if (strength == STRONG)
404 mAttemptHibernation = true;
405
406 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700407 egl_connection_t* const cnx = &gEGLImpl;
408 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700409 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700410 cnx->egl.eglHibernateProcessIMG &&
411 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700412 ALOGV("Hibernating\n");
413 if (!cnx->egl.eglHibernateProcessIMG()) {
414 ALOGE("Failed to hibernate EGL implementation\n");
415 return;
416 }
417 mHibernating = true;
418 }
419 }
420}
421
Jesse Halla0fef1c2012-04-17 12:02:26 -0700422void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
423 Mutex::Autolock _l(mLock);
424 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700425}
426
Mathias Agopian518ec112011-05-13 16:21:08 -0700427// ----------------------------------------------------------------------------
428}; // namespace android
429// ----------------------------------------------------------------------------