blob: 038052198d179b349a0002eba23ccd6bf6e031f7 [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
Jesse Hallc2e41222013-08-08 13:40:22 -070047static bool findExtension(const char* exts, const char* name, size_t nameLen) {
48 if (exts) {
49 const char* match = strstr(exts, name);
50 if (match && (match[nameLen] == '\0' || match[nameLen] == ' ')) {
51 return true;
52 }
53 }
54 return false;
55}
56
Mathias Agopian518ec112011-05-13 16:21:08 -070057egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
58
59egl_display_t::egl_display_t() :
Jesse Halla0fef1c2012-04-17 12:02:26 -070060 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
Mathias Agopian518ec112011-05-13 16:21:08 -070061}
62
63egl_display_t::~egl_display_t() {
64 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080065 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070066}
67
68egl_display_t* egl_display_t::get(EGLDisplay dpy) {
69 uintptr_t index = uintptr_t(dpy)-1U;
70 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
71}
72
73void egl_display_t::addObject(egl_object_t* object) {
74 Mutex::Autolock _l(lock);
75 objects.add(object);
76}
77
Mathias Agopian5b287a62011-05-16 18:58:55 -070078void egl_display_t::removeObject(egl_object_t* object) {
79 Mutex::Autolock _l(lock);
80 objects.remove(object);
81}
82
Mathias Agopianf0480de2011-11-13 20:50:07 -080083bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070084 Mutex::Autolock _l(lock);
85 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080086 if (object->getDisplay() == this) {
87 object->incRef();
88 return true;
89 }
Mathias Agopian518ec112011-05-13 16:21:08 -070090 }
91 return false;
92}
93
Mathias Agopian518ec112011-05-13 16:21:08 -070094EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
95 if (uintptr_t(disp) >= NUM_DISPLAYS)
96 return NULL;
97
98 return sDisplay[uintptr_t(disp)].getDisplay(disp);
99}
100
101EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
102
103 Mutex::Autolock _l(lock);
104
105 // get our driver loader
106 Loader& loader(Loader::getInstance());
107
Mathias Agopianada798b2012-02-13 17:09:30 -0800108 egl_connection_t* const cnx = &gEGLImpl;
109 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
110 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
111 disp.dpy = dpy;
112 if (dpy == EGL_NO_DISPLAY) {
113 loader.close(cnx->dso);
114 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700115 }
116 }
117
118 return EGLDisplay(uintptr_t(display) + 1U);
119}
120
121EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
122
123 Mutex::Autolock _l(lock);
124
125 if (refs > 0) {
126 if (major != NULL)
127 *major = VERSION_MAJOR;
128 if (minor != NULL)
129 *minor = VERSION_MINOR;
130 refs++;
131 return EGL_TRUE;
132 }
133
134#if EGL_TRACE
135
136 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
137 // the information from that call may be stale.)
138 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800139 initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -0700140
141#endif
142
143 setGLHooksThreadSpecific(&gHooksNoContext);
144
145 // initialize each EGL and
146 // build our own extension string first, based on the extension we know
147 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800148
149 egl_connection_t* const cnx = &gEGLImpl;
150 cnx->major = -1;
151 cnx->minor = -1;
152 if (cnx->dso) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700153
154#if defined(ADRENO130)
155#warning "Adreno-130 eglInitialize() workaround"
156 /*
157 * The ADRENO 130 driver returns a different EGLDisplay each time
158 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
159 * after eglTerminate() has been called, so that eglInitialize()
160 * cannot be called again. Therefore, we need to make sure to call
161 * eglGetDisplay() before calling eglInitialize();
162 */
163 if (i == IMPL_HARDWARE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800164 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian518ec112011-05-13 16:21:08 -0700165 }
166#endif
167
Mathias Agopianada798b2012-02-13 17:09:30 -0800168 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700169 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800170 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
171 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700172
173 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800174 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700175
176 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800177 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700178 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800179 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700180 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800181 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700182 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800183 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700184 EGL_CLIENT_APIS);
185
186 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800187 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700188 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
189 }
190 }
191
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800192 // the query strings are per-display
193 mVendorString.setTo(sVendorString);
194 mVersionString.setTo(sVersionString);
195 mClientApiString.setTo(sClientApiString);
196
Jesse Hall21558da2013-08-06 15:31:22 -0700197 mExtensionString.setTo(gBuiltinExtensionString);
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700198 char const* start = gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800199 char const* end;
200 do {
201 // find the space separating this extension for the next one
202 end = strchr(start, ' ');
203 if (end) {
204 // length of the extension string
205 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800206 if (len) {
207 // NOTE: we could avoid the copy if we had strnstr.
208 const String8 ext(start, len);
Jesse Hallc2e41222013-08-08 13:40:22 -0700209 if (findExtension(disp.queryString.extensions, ext.string(),
210 len)) {
211 mExtensionString.append(start, len+1);
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800212 }
213 }
214 // process the next extension string, and skip the space.
215 start = end + 1;
216 }
217 } while (end);
218
Jamie Gennisaca51c02011-11-03 17:42:43 -0700219 egl_cache_t::get()->initialize(this);
220
Mathias Agopian7db993a2012-03-25 00:49:46 -0700221 char value[PROPERTY_VALUE_MAX];
222 property_get("debug.egl.finish", value, "0");
223 if (atoi(value)) {
224 finishOnSwap = true;
225 }
226
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700227 property_get("debug.egl.traceGpuCompletion", value, "0");
228 if (atoi(value)) {
229 traceGpuCompletion = true;
230 }
231
Mathias Agopian7773c432012-02-13 20:06:08 -0800232 refs++;
233 if (major != NULL)
234 *major = VERSION_MAJOR;
235 if (minor != NULL)
236 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700237
238 mHibernation.setDisplayValid(true);
239
Mathias Agopian7773c432012-02-13 20:06:08 -0800240 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700241}
242
243EGLBoolean egl_display_t::terminate() {
244
245 Mutex::Autolock _l(lock);
246
247 if (refs == 0) {
Mathias Agopianfe981272012-06-13 15:21:21 -0700248 /*
249 * From the EGL spec (3.2):
250 * "Termination of a display that has already been terminated,
251 * (...), is allowed, but the only effect of such a call is
252 * to return EGL_TRUE (...)
253 */
254 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700255 }
256
257 // this is specific to Android, display termination is ref-counted.
258 if (refs > 1) {
259 refs--;
260 return EGL_TRUE;
261 }
262
263 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800264 egl_connection_t* const cnx = &gEGLImpl;
265 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
266 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
267 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
268 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700269 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800270 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800271 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800272 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700273 }
274
Jesse Halla0fef1c2012-04-17 12:02:26 -0700275 mHibernation.setDisplayValid(false);
276
Jamie Gennisa08cf6e2012-09-16 14:02:20 -0700277 // Reset the extension string since it will be regenerated if we get
278 // reinitialized.
279 mExtensionString.setTo("");
280
Mathias Agopian5b287a62011-05-16 18:58:55 -0700281 // Mark all objects remaining in the list as terminated, unless
282 // there are no reference to them, it which case, we're free to
283 // delete them.
284 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000285 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700286 for (size_t i=0 ; i<count ; i++) {
287 egl_object_t* o = objects.itemAt(i);
288 o->destroy();
289 }
290
291 // this marks all object handles are "terminated"
292 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700293
294 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700295 return res;
296}
297
Mathias Agopianfb87e542012-01-30 18:20:52 -0800298void egl_display_t::loseCurrent(egl_context_t * cur_c)
299{
300 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800301 egl_display_t* display = cur_c->getDisplay();
302 if (display) {
303 display->loseCurrentImpl(cur_c);
304 }
305 }
306}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800307
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800308void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
309{
310 // by construction, these are either 0 or valid (possibly terminated)
311 // it should be impossible for these to be invalid
312 ContextRef _cur_c(cur_c);
313 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
314 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800315
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800316 { // scope for the lock
317 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800318 cur_c->onLooseCurrent();
319
Mathias Agopianfb87e542012-01-30 18:20:52 -0800320 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800321
322 // This cannot be called with the lock held because it might end-up
323 // calling back into EGL (in particular when a surface is destroyed
324 // it calls ANativeWindow::disconnect
325 _cur_c.release();
326 _cur_r.release();
327 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800328}
329
330EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
331 EGLSurface draw, EGLSurface read, EGLContext ctx,
332 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
333{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800334 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800335
336 // by construction, these are either 0 or valid (possibly terminated)
337 // it should be impossible for these to be invalid
338 ContextRef _cur_c(cur_c);
339 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
340 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
341
342 { // scope for the lock
343 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800344 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800345 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800346 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800347 if (result == EGL_TRUE) {
348 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700349 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700350 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700351 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800352 }
353 } else {
354 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800355 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800356 if (result == EGL_TRUE) {
357 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700358 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800359 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800360 }
361 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800362
363 if (result == EGL_TRUE) {
364 // This cannot be called with the lock held because it might end-up
365 // calling back into EGL (in particular when a surface is destroyed
366 // it calls ANativeWindow::disconnect
367 _cur_c.release();
368 _cur_r.release();
369 _cur_d.release();
370 }
371
Mathias Agopianfb87e542012-01-30 18:20:52 -0800372 return result;
373}
Mathias Agopian518ec112011-05-13 16:21:08 -0700374
Jesse Hallc2e41222013-08-08 13:40:22 -0700375bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
376 if (!nameLen) {
377 nameLen = strlen(name);
378 }
379 return findExtension(mExtensionString.string(), name, nameLen);
380}
381
Jesse Halla0fef1c2012-04-17 12:02:26 -0700382// ----------------------------------------------------------------------------
383
384bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
385 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700386 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
387 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700388
Jesse Hallb29e5e82012-04-04 16:53:42 -0700389 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700390 if (strength == STRONG)
391 mAttemptHibernation = false;
392
Jesse Hall25838592012-04-05 15:53:28 -0700393 if (CC_UNLIKELY(mHibernating)) {
394 ALOGV("Awakening\n");
395 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700396
397 // These conditions should be guaranteed before entering hibernation;
398 // we don't want to get into a state where we can't wake up.
399 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
400 "Invalid hibernation state, unable to awaken\n");
401
Jesse Hall25838592012-04-05 15:53:28 -0700402 if (!cnx->egl.eglAwakenProcessIMG()) {
403 ALOGE("Failed to awaken EGL implementation\n");
404 return false;
405 }
406 mHibernating = false;
407 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700408 return true;
409}
410
Jesse Halla0fef1c2012-04-17 12:02:26 -0700411void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
412 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700413 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700414
415 mWakeCount--;
416 if (strength == STRONG)
417 mAttemptHibernation = true;
418
419 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700420 egl_connection_t* const cnx = &gEGLImpl;
421 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700422 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700423 cnx->egl.eglHibernateProcessIMG &&
424 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700425 ALOGV("Hibernating\n");
426 if (!cnx->egl.eglHibernateProcessIMG()) {
427 ALOGE("Failed to hibernate EGL implementation\n");
428 return;
429 }
430 mHibernating = true;
431 }
432 }
433}
434
Jesse Halla0fef1c2012-04-17 12:02:26 -0700435void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
436 Mutex::Autolock _l(mLock);
437 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700438}
439
Mathias Agopian518ec112011-05-13 16:21:08 -0700440// ----------------------------------------------------------------------------
441}; // namespace android
442// ----------------------------------------------------------------------------