blob: 8d7890badd935a7743278b536ad747300c47fcb7 [file] [log] [blame]
Jesse Hall21558da2013-08-06 15:31:22 -07001/*
Mathias Agopian518ec112011-05-13 16:21:08 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall21558da2013-08-06 15:31:22 -07004 ** 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
Mathias Agopian518ec112011-05-13 16:21:08 -07007 **
Jesse Hall21558da2013-08-06 15:31:22 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian518ec112011-05-13 16:21:08 -07009 **
Jesse Hall21558da2013-08-06 15:31:22 -070010 ** 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
Mathias Agopian518ec112011-05-13 16:21:08 -070014 ** 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
Jesse Hall21558da2013-08-06 15:31:22 -070038extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070039extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080040
Mathias Agopian518ec112011-05-13 16:21:08 -070041extern void initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -080042extern void initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -070043extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
44
Mathias Agopian518ec112011-05-13 16:21:08 -070045// ----------------------------------------------------------------------------
46
47egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
48
49egl_display_t::egl_display_t() :
Jesse Halla0fef1c2012-04-17 12:02:26 -070050 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
Mathias Agopian518ec112011-05-13 16:21:08 -070051}
52
53egl_display_t::~egl_display_t() {
54 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080055 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070056}
57
58egl_display_t* egl_display_t::get(EGLDisplay dpy) {
59 uintptr_t index = uintptr_t(dpy)-1U;
60 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
61}
62
63void egl_display_t::addObject(egl_object_t* object) {
64 Mutex::Autolock _l(lock);
65 objects.add(object);
66}
67
Mathias Agopian5b287a62011-05-16 18:58:55 -070068void egl_display_t::removeObject(egl_object_t* object) {
69 Mutex::Autolock _l(lock);
70 objects.remove(object);
71}
72
Mathias Agopianf0480de2011-11-13 20:50:07 -080073bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070074 Mutex::Autolock _l(lock);
75 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080076 if (object->getDisplay() == this) {
77 object->incRef();
78 return true;
79 }
Mathias Agopian518ec112011-05-13 16:21:08 -070080 }
81 return false;
82}
83
Mathias Agopian518ec112011-05-13 16:21:08 -070084EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
85 if (uintptr_t(disp) >= NUM_DISPLAYS)
86 return NULL;
87
88 return sDisplay[uintptr_t(disp)].getDisplay(disp);
89}
90
91EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
92
93 Mutex::Autolock _l(lock);
94
95 // get our driver loader
96 Loader& loader(Loader::getInstance());
97
Mathias Agopianada798b2012-02-13 17:09:30 -080098 egl_connection_t* const cnx = &gEGLImpl;
99 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
100 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
101 disp.dpy = dpy;
102 if (dpy == EGL_NO_DISPLAY) {
103 loader.close(cnx->dso);
104 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700105 }
106 }
107
108 return EGLDisplay(uintptr_t(display) + 1U);
109}
110
111EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
112
113 Mutex::Autolock _l(lock);
114
115 if (refs > 0) {
116 if (major != NULL)
117 *major = VERSION_MAJOR;
118 if (minor != NULL)
119 *minor = VERSION_MINOR;
120 refs++;
121 return EGL_TRUE;
122 }
123
124#if EGL_TRACE
125
126 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
127 // the information from that call may be stale.)
128 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800129 initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -0700130
131#endif
132
133 setGLHooksThreadSpecific(&gHooksNoContext);
134
135 // initialize each EGL and
136 // build our own extension string first, based on the extension we know
137 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800138
139 egl_connection_t* const cnx = &gEGLImpl;
140 cnx->major = -1;
141 cnx->minor = -1;
142 if (cnx->dso) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700143
144#if defined(ADRENO130)
145#warning "Adreno-130 eglInitialize() workaround"
146 /*
147 * The ADRENO 130 driver returns a different EGLDisplay each time
148 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
149 * after eglTerminate() has been called, so that eglInitialize()
150 * cannot be called again. Therefore, we need to make sure to call
151 * eglGetDisplay() before calling eglInitialize();
152 */
153 if (i == IMPL_HARDWARE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800154 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian518ec112011-05-13 16:21:08 -0700155 }
156#endif
157
Mathias Agopianada798b2012-02-13 17:09:30 -0800158 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700159 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800160 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
161 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700162
163 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800164 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700165
166 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800167 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700168 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800169 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700170 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800171 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700172 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800173 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700174 EGL_CLIENT_APIS);
175
176 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800177 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700178 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
179 }
180 }
181
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800182 // the query strings are per-display
183 mVendorString.setTo(sVendorString);
184 mVersionString.setTo(sVersionString);
185 mClientApiString.setTo(sClientApiString);
186
Jesse Hall21558da2013-08-06 15:31:22 -0700187 mExtensionString.setTo(gBuiltinExtensionString);
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700188 char const* start = gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800189 char const* end;
190 do {
191 // find the space separating this extension for the next one
192 end = strchr(start, ' ');
193 if (end) {
194 // length of the extension string
195 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800196 if (len) {
197 // NOTE: we could avoid the copy if we had strnstr.
198 const String8 ext(start, len);
Mathias Agopianada798b2012-02-13 17:09:30 -0800199 // now look for this extension
200 if (disp.queryString.extensions) {
201 // if we find it, add this extension string to our list
202 // (and don't forget the space)
203 const char* match = strstr(disp.queryString.extensions, ext.string());
204 if (match && (match[len] == ' ' || match[len] == 0)) {
205 mExtensionString.append(start, len+1);
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800206 }
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800207 }
208 }
209 // process the next extension string, and skip the space.
210 start = end + 1;
211 }
212 } while (end);
213
Jamie Gennisaca51c02011-11-03 17:42:43 -0700214 egl_cache_t::get()->initialize(this);
215
Mathias Agopian7db993a2012-03-25 00:49:46 -0700216 char value[PROPERTY_VALUE_MAX];
217 property_get("debug.egl.finish", value, "0");
218 if (atoi(value)) {
219 finishOnSwap = true;
220 }
221
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700222 property_get("debug.egl.traceGpuCompletion", value, "0");
223 if (atoi(value)) {
224 traceGpuCompletion = true;
225 }
226
Mathias Agopian7773c432012-02-13 20:06:08 -0800227 refs++;
228 if (major != NULL)
229 *major = VERSION_MAJOR;
230 if (minor != NULL)
231 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700232
233 mHibernation.setDisplayValid(true);
234
Mathias Agopian7773c432012-02-13 20:06:08 -0800235 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700236}
237
238EGLBoolean egl_display_t::terminate() {
239
240 Mutex::Autolock _l(lock);
241
242 if (refs == 0) {
Mathias Agopianfe981272012-06-13 15:21:21 -0700243 /*
244 * From the EGL spec (3.2):
245 * "Termination of a display that has already been terminated,
246 * (...), is allowed, but the only effect of such a call is
247 * to return EGL_TRUE (...)
248 */
249 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700250 }
251
252 // this is specific to Android, display termination is ref-counted.
253 if (refs > 1) {
254 refs--;
255 return EGL_TRUE;
256 }
257
258 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800259 egl_connection_t* const cnx = &gEGLImpl;
260 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
261 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
262 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
263 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700264 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800265 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800266 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800267 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700268 }
269
Jesse Halla0fef1c2012-04-17 12:02:26 -0700270 mHibernation.setDisplayValid(false);
271
Jamie Gennisa08cf6e2012-09-16 14:02:20 -0700272 // Reset the extension string since it will be regenerated if we get
273 // reinitialized.
274 mExtensionString.setTo("");
275
Mathias Agopian5b287a62011-05-16 18:58:55 -0700276 // Mark all objects remaining in the list as terminated, unless
277 // there are no reference to them, it which case, we're free to
278 // delete them.
279 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000280 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700281 for (size_t i=0 ; i<count ; i++) {
282 egl_object_t* o = objects.itemAt(i);
283 o->destroy();
284 }
285
286 // this marks all object handles are "terminated"
287 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700288
289 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700290 return res;
291}
292
Mathias Agopianfb87e542012-01-30 18:20:52 -0800293void egl_display_t::loseCurrent(egl_context_t * cur_c)
294{
295 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800296 egl_display_t* display = cur_c->getDisplay();
297 if (display) {
298 display->loseCurrentImpl(cur_c);
299 }
300 }
301}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800302
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800303void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
304{
305 // by construction, these are either 0 or valid (possibly terminated)
306 // it should be impossible for these to be invalid
307 ContextRef _cur_c(cur_c);
308 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
309 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800310
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800311 { // scope for the lock
312 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800313 cur_c->onLooseCurrent();
314
Mathias Agopianfb87e542012-01-30 18:20:52 -0800315 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800316
317 // This cannot be called with the lock held because it might end-up
318 // calling back into EGL (in particular when a surface is destroyed
319 // it calls ANativeWindow::disconnect
320 _cur_c.release();
321 _cur_r.release();
322 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800323}
324
325EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
326 EGLSurface draw, EGLSurface read, EGLContext ctx,
327 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
328{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800329 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800330
331 // by construction, these are either 0 or valid (possibly terminated)
332 // it should be impossible for these to be invalid
333 ContextRef _cur_c(cur_c);
334 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
335 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
336
337 { // scope for the lock
338 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800339 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800340 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800341 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800342 if (result == EGL_TRUE) {
343 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700344 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700345 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700346 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800347 }
348 } else {
349 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800350 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800351 if (result == EGL_TRUE) {
352 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700353 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800354 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800355 }
356 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800357
358 if (result == EGL_TRUE) {
359 // This cannot be called with the lock held because it might end-up
360 // calling back into EGL (in particular when a surface is destroyed
361 // it calls ANativeWindow::disconnect
362 _cur_c.release();
363 _cur_r.release();
364 _cur_d.release();
365 }
366
Mathias Agopianfb87e542012-01-30 18:20:52 -0800367 return result;
368}
Mathias Agopian518ec112011-05-13 16:21:08 -0700369
Jesse Halla0fef1c2012-04-17 12:02:26 -0700370// ----------------------------------------------------------------------------
371
372bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
373 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700374 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
375 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700376
Jesse Hallb29e5e82012-04-04 16:53:42 -0700377 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700378 if (strength == STRONG)
379 mAttemptHibernation = false;
380
Jesse Hall25838592012-04-05 15:53:28 -0700381 if (CC_UNLIKELY(mHibernating)) {
382 ALOGV("Awakening\n");
383 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700384
385 // These conditions should be guaranteed before entering hibernation;
386 // we don't want to get into a state where we can't wake up.
387 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
388 "Invalid hibernation state, unable to awaken\n");
389
Jesse Hall25838592012-04-05 15:53:28 -0700390 if (!cnx->egl.eglAwakenProcessIMG()) {
391 ALOGE("Failed to awaken EGL implementation\n");
392 return false;
393 }
394 mHibernating = false;
395 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700396 return true;
397}
398
Jesse Halla0fef1c2012-04-17 12:02:26 -0700399void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
400 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700401 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700402
403 mWakeCount--;
404 if (strength == STRONG)
405 mAttemptHibernation = true;
406
407 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700408 egl_connection_t* const cnx = &gEGLImpl;
409 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700410 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700411 cnx->egl.eglHibernateProcessIMG &&
412 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700413 ALOGV("Hibernating\n");
414 if (!cnx->egl.eglHibernateProcessIMG()) {
415 ALOGE("Failed to hibernate EGL implementation\n");
416 return;
417 }
418 mHibernating = true;
419 }
420 }
421}
422
Jesse Halla0fef1c2012-04-17 12:02:26 -0700423void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
424 Mutex::Autolock _l(mLock);
425 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700426}
427
Mathias Agopian518ec112011-05-13 16:21:08 -0700428// ----------------------------------------------------------------------------
429}; // namespace android
430// ----------------------------------------------------------------------------