blob: 59dd2d959223ef5557ed5c6dfc9502e55674e243 [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
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070037extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080038
Mathias Agopian518ec112011-05-13 16:21:08 -070039extern void initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -080040extern void initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -070041extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
42
Mathias Agopian518ec112011-05-13 16:21:08 -070043// ----------------------------------------------------------------------------
44
45egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
46
47egl_display_t::egl_display_t() :
Jesse Halla0fef1c2012-04-17 12:02:26 -070048 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
Mathias Agopian518ec112011-05-13 16:21:08 -070049}
50
51egl_display_t::~egl_display_t() {
52 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080053 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070054}
55
56egl_display_t* egl_display_t::get(EGLDisplay dpy) {
57 uintptr_t index = uintptr_t(dpy)-1U;
58 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
59}
60
61void egl_display_t::addObject(egl_object_t* object) {
62 Mutex::Autolock _l(lock);
63 objects.add(object);
64}
65
Mathias Agopian5b287a62011-05-16 18:58:55 -070066void egl_display_t::removeObject(egl_object_t* object) {
67 Mutex::Autolock _l(lock);
68 objects.remove(object);
69}
70
Mathias Agopianf0480de2011-11-13 20:50:07 -080071bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070072 Mutex::Autolock _l(lock);
73 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080074 if (object->getDisplay() == this) {
75 object->incRef();
76 return true;
77 }
Mathias Agopian518ec112011-05-13 16:21:08 -070078 }
79 return false;
80}
81
Mathias Agopian518ec112011-05-13 16:21:08 -070082EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
83 if (uintptr_t(disp) >= NUM_DISPLAYS)
84 return NULL;
85
86 return sDisplay[uintptr_t(disp)].getDisplay(disp);
87}
88
89EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
90
91 Mutex::Autolock _l(lock);
92
93 // get our driver loader
94 Loader& loader(Loader::getInstance());
95
Mathias Agopianada798b2012-02-13 17:09:30 -080096 egl_connection_t* const cnx = &gEGLImpl;
97 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
98 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
99 disp.dpy = dpy;
100 if (dpy == EGL_NO_DISPLAY) {
101 loader.close(cnx->dso);
102 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700103 }
104 }
105
106 return EGLDisplay(uintptr_t(display) + 1U);
107}
108
109EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
110
111 Mutex::Autolock _l(lock);
112
113 if (refs > 0) {
114 if (major != NULL)
115 *major = VERSION_MAJOR;
116 if (minor != NULL)
117 *minor = VERSION_MINOR;
118 refs++;
119 return EGL_TRUE;
120 }
121
122#if EGL_TRACE
123
124 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
125 // the information from that call may be stale.)
126 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800127 initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -0700128
129#endif
130
131 setGLHooksThreadSpecific(&gHooksNoContext);
132
133 // initialize each EGL and
134 // build our own extension string first, based on the extension we know
135 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800136
137 egl_connection_t* const cnx = &gEGLImpl;
138 cnx->major = -1;
139 cnx->minor = -1;
140 if (cnx->dso) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700141
142#if defined(ADRENO130)
143#warning "Adreno-130 eglInitialize() workaround"
144 /*
145 * The ADRENO 130 driver returns a different EGLDisplay each time
146 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
147 * after eglTerminate() has been called, so that eglInitialize()
148 * cannot be called again. Therefore, we need to make sure to call
149 * eglGetDisplay() before calling eglInitialize();
150 */
151 if (i == IMPL_HARDWARE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800152 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian518ec112011-05-13 16:21:08 -0700153 }
154#endif
155
Mathias Agopianada798b2012-02-13 17:09:30 -0800156 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700157 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800158 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
159 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700160
161 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800162 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700163
164 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800165 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700166 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800167 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700168 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800169 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700170 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800171 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700172 EGL_CLIENT_APIS);
173
174 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800175 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700176 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
177 }
178 }
179
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800180 // the query strings are per-display
181 mVendorString.setTo(sVendorString);
182 mVersionString.setTo(sVersionString);
183 mClientApiString.setTo(sClientApiString);
184
Mathias Agopian7773c432012-02-13 20:06:08 -0800185 // we only add extensions that exist in the implementation
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700186 char const* start = gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800187 char const* end;
188 do {
189 // find the space separating this extension for the next one
190 end = strchr(start, ' ');
191 if (end) {
192 // length of the extension string
193 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800194 if (len) {
195 // NOTE: we could avoid the copy if we had strnstr.
196 const String8 ext(start, len);
Mathias Agopianada798b2012-02-13 17:09:30 -0800197 // now look for this extension
198 if (disp.queryString.extensions) {
199 // if we find it, add this extension string to our list
200 // (and don't forget the space)
201 const char* match = strstr(disp.queryString.extensions, ext.string());
202 if (match && (match[len] == ' ' || match[len] == 0)) {
203 mExtensionString.append(start, len+1);
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800204 }
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800205 }
206 }
207 // process the next extension string, and skip the space.
208 start = end + 1;
209 }
210 } while (end);
211
Jamie Gennisaca51c02011-11-03 17:42:43 -0700212 egl_cache_t::get()->initialize(this);
213
Mathias Agopian7db993a2012-03-25 00:49:46 -0700214 char value[PROPERTY_VALUE_MAX];
215 property_get("debug.egl.finish", value, "0");
216 if (atoi(value)) {
217 finishOnSwap = true;
218 }
219
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700220 property_get("debug.egl.traceGpuCompletion", value, "0");
221 if (atoi(value)) {
222 traceGpuCompletion = true;
223 }
224
Mathias Agopian7773c432012-02-13 20:06:08 -0800225 refs++;
226 if (major != NULL)
227 *major = VERSION_MAJOR;
228 if (minor != NULL)
229 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700230
231 mHibernation.setDisplayValid(true);
232
Mathias Agopian7773c432012-02-13 20:06:08 -0800233 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700234}
235
236EGLBoolean egl_display_t::terminate() {
237
238 Mutex::Autolock _l(lock);
239
240 if (refs == 0) {
Mathias Agopianfe981272012-06-13 15:21:21 -0700241 /*
242 * From the EGL spec (3.2):
243 * "Termination of a display that has already been terminated,
244 * (...), is allowed, but the only effect of such a call is
245 * to return EGL_TRUE (...)
246 */
247 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700248 }
249
250 // this is specific to Android, display termination is ref-counted.
251 if (refs > 1) {
252 refs--;
253 return EGL_TRUE;
254 }
255
256 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800257 egl_connection_t* const cnx = &gEGLImpl;
258 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
259 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
260 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
261 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700262 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800263 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800264 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800265 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700266 }
267
Jesse Halla0fef1c2012-04-17 12:02:26 -0700268 mHibernation.setDisplayValid(false);
269
Jamie Gennisa08cf6e2012-09-16 14:02:20 -0700270 // Reset the extension string since it will be regenerated if we get
271 // reinitialized.
272 mExtensionString.setTo("");
273
Mathias Agopian5b287a62011-05-16 18:58:55 -0700274 // Mark all objects remaining in the list as terminated, unless
275 // there are no reference to them, it which case, we're free to
276 // delete them.
277 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000278 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700279 for (size_t i=0 ; i<count ; i++) {
280 egl_object_t* o = objects.itemAt(i);
281 o->destroy();
282 }
283
284 // this marks all object handles are "terminated"
285 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700286
287 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700288 return res;
289}
290
Mathias Agopianfb87e542012-01-30 18:20:52 -0800291void egl_display_t::loseCurrent(egl_context_t * cur_c)
292{
293 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800294 egl_display_t* display = cur_c->getDisplay();
295 if (display) {
296 display->loseCurrentImpl(cur_c);
297 }
298 }
299}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800300
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800301void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
302{
303 // by construction, these are either 0 or valid (possibly terminated)
304 // it should be impossible for these to be invalid
305 ContextRef _cur_c(cur_c);
306 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
307 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800308
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800309 { // scope for the lock
310 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800311 cur_c->onLooseCurrent();
312
Mathias Agopianfb87e542012-01-30 18:20:52 -0800313 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800314
315 // This cannot be called with the lock held because it might end-up
316 // calling back into EGL (in particular when a surface is destroyed
317 // it calls ANativeWindow::disconnect
318 _cur_c.release();
319 _cur_r.release();
320 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800321}
322
323EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
324 EGLSurface draw, EGLSurface read, EGLContext ctx,
325 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
326{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800327 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800328
329 // by construction, these are either 0 or valid (possibly terminated)
330 // it should be impossible for these to be invalid
331 ContextRef _cur_c(cur_c);
332 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
333 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
334
335 { // scope for the lock
336 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800337 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800338 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800339 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800340 if (result == EGL_TRUE) {
341 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700342 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700343 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700344 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800345 }
346 } else {
347 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800348 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800349 if (result == EGL_TRUE) {
350 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700351 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800352 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800353 }
354 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355
356 if (result == EGL_TRUE) {
357 // This cannot be called with the lock held because it might end-up
358 // calling back into EGL (in particular when a surface is destroyed
359 // it calls ANativeWindow::disconnect
360 _cur_c.release();
361 _cur_r.release();
362 _cur_d.release();
363 }
364
Mathias Agopianfb87e542012-01-30 18:20:52 -0800365 return result;
366}
Mathias Agopian518ec112011-05-13 16:21:08 -0700367
Jesse Halla0fef1c2012-04-17 12:02:26 -0700368// ----------------------------------------------------------------------------
369
370bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
371 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700372 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
373 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700374
Jesse Hallb29e5e82012-04-04 16:53:42 -0700375 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700376 if (strength == STRONG)
377 mAttemptHibernation = false;
378
Jesse Hall25838592012-04-05 15:53:28 -0700379 if (CC_UNLIKELY(mHibernating)) {
380 ALOGV("Awakening\n");
381 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700382
383 // These conditions should be guaranteed before entering hibernation;
384 // we don't want to get into a state where we can't wake up.
385 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
386 "Invalid hibernation state, unable to awaken\n");
387
Jesse Hall25838592012-04-05 15:53:28 -0700388 if (!cnx->egl.eglAwakenProcessIMG()) {
389 ALOGE("Failed to awaken EGL implementation\n");
390 return false;
391 }
392 mHibernating = false;
393 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700394 return true;
395}
396
Jesse Halla0fef1c2012-04-17 12:02:26 -0700397void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
398 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700399 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700400
401 mWakeCount--;
402 if (strength == STRONG)
403 mAttemptHibernation = true;
404
405 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700406 egl_connection_t* const cnx = &gEGLImpl;
407 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700408 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700409 cnx->egl.eglHibernateProcessIMG &&
410 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700411 ALOGV("Hibernating\n");
412 if (!cnx->egl.eglHibernateProcessIMG()) {
413 ALOGE("Failed to hibernate EGL implementation\n");
414 return;
415 }
416 mHibernating = true;
417 }
418 }
419}
420
Jesse Halla0fef1c2012-04-17 12:02:26 -0700421void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
422 Mutex::Autolock _l(mLock);
423 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700424}
425
Mathias Agopian518ec112011-05-13 16:21:08 -0700426// ----------------------------------------------------------------------------
427}; // namespace android
428// ----------------------------------------------------------------------------