blob: 26240f1bcdc552f0f71bf5666bc21a50168440f6 [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 Agopianada798b2012-02-13 17:09:30 -0800153 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800155 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
156 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700157
158 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800159 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700160
161 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800162 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700163 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800164 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700165 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800166 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700167 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800168 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700169 EGL_CLIENT_APIS);
170
171 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800172 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700173 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
174 }
175 }
176
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800177 // the query strings are per-display
178 mVendorString.setTo(sVendorString);
179 mVersionString.setTo(sVersionString);
180 mClientApiString.setTo(sClientApiString);
181
Jesse Hall21558da2013-08-06 15:31:22 -0700182 mExtensionString.setTo(gBuiltinExtensionString);
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700183 char const* start = gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800184 char const* end;
185 do {
186 // find the space separating this extension for the next one
187 end = strchr(start, ' ');
188 if (end) {
189 // length of the extension string
190 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800191 if (len) {
192 // NOTE: we could avoid the copy if we had strnstr.
193 const String8 ext(start, len);
Jesse Hallc2e41222013-08-08 13:40:22 -0700194 if (findExtension(disp.queryString.extensions, ext.string(),
195 len)) {
196 mExtensionString.append(start, len+1);
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800197 }
198 }
199 // process the next extension string, and skip the space.
200 start = end + 1;
201 }
202 } while (end);
203
Jamie Gennisaca51c02011-11-03 17:42:43 -0700204 egl_cache_t::get()->initialize(this);
205
Mathias Agopian7db993a2012-03-25 00:49:46 -0700206 char value[PROPERTY_VALUE_MAX];
207 property_get("debug.egl.finish", value, "0");
208 if (atoi(value)) {
209 finishOnSwap = true;
210 }
211
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700212 property_get("debug.egl.traceGpuCompletion", value, "0");
213 if (atoi(value)) {
214 traceGpuCompletion = true;
215 }
216
Mathias Agopian7773c432012-02-13 20:06:08 -0800217 refs++;
218 if (major != NULL)
219 *major = VERSION_MAJOR;
220 if (minor != NULL)
221 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700222
223 mHibernation.setDisplayValid(true);
224
Mathias Agopian7773c432012-02-13 20:06:08 -0800225 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700226}
227
228EGLBoolean egl_display_t::terminate() {
229
230 Mutex::Autolock _l(lock);
231
232 if (refs == 0) {
Mathias Agopianfe981272012-06-13 15:21:21 -0700233 /*
234 * From the EGL spec (3.2):
235 * "Termination of a display that has already been terminated,
236 * (...), is allowed, but the only effect of such a call is
237 * to return EGL_TRUE (...)
238 */
239 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700240 }
241
242 // this is specific to Android, display termination is ref-counted.
243 if (refs > 1) {
244 refs--;
245 return EGL_TRUE;
246 }
247
248 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800249 egl_connection_t* const cnx = &gEGLImpl;
250 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
251 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
252 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
253 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700254 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800255 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800256 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800257 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700258 }
259
Jesse Halla0fef1c2012-04-17 12:02:26 -0700260 mHibernation.setDisplayValid(false);
261
Jamie Gennisa08cf6e2012-09-16 14:02:20 -0700262 // Reset the extension string since it will be regenerated if we get
263 // reinitialized.
264 mExtensionString.setTo("");
265
Mathias Agopian5b287a62011-05-16 18:58:55 -0700266 // Mark all objects remaining in the list as terminated, unless
267 // there are no reference to them, it which case, we're free to
268 // delete them.
269 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000270 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700271 for (size_t i=0 ; i<count ; i++) {
272 egl_object_t* o = objects.itemAt(i);
273 o->destroy();
274 }
275
276 // this marks all object handles are "terminated"
277 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700278
279 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700280 return res;
281}
282
Mathias Agopianfb87e542012-01-30 18:20:52 -0800283void egl_display_t::loseCurrent(egl_context_t * cur_c)
284{
285 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800286 egl_display_t* display = cur_c->getDisplay();
287 if (display) {
288 display->loseCurrentImpl(cur_c);
289 }
290 }
291}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800292
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800293void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
294{
295 // by construction, these are either 0 or valid (possibly terminated)
296 // it should be impossible for these to be invalid
297 ContextRef _cur_c(cur_c);
298 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
299 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800300
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800301 { // scope for the lock
302 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800303 cur_c->onLooseCurrent();
304
Mathias Agopianfb87e542012-01-30 18:20:52 -0800305 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800306
307 // This cannot be called with the lock held because it might end-up
308 // calling back into EGL (in particular when a surface is destroyed
309 // it calls ANativeWindow::disconnect
310 _cur_c.release();
311 _cur_r.release();
312 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800313}
314
315EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
316 EGLSurface draw, EGLSurface read, EGLContext ctx,
317 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
318{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800319 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800320
321 // by construction, these are either 0 or valid (possibly terminated)
322 // it should be impossible for these to be invalid
323 ContextRef _cur_c(cur_c);
324 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
325 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
326
327 { // scope for the lock
328 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800329 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800330 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800331 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800332 if (result == EGL_TRUE) {
333 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700334 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700335 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700336 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800337 }
338 } else {
339 result = cur_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 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700343 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800344 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800345 }
346 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800347
348 if (result == EGL_TRUE) {
349 // This cannot be called with the lock held because it might end-up
350 // calling back into EGL (in particular when a surface is destroyed
351 // it calls ANativeWindow::disconnect
352 _cur_c.release();
353 _cur_r.release();
354 _cur_d.release();
355 }
356
Mathias Agopianfb87e542012-01-30 18:20:52 -0800357 return result;
358}
Mathias Agopian518ec112011-05-13 16:21:08 -0700359
Jesse Hallc2e41222013-08-08 13:40:22 -0700360bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
361 if (!nameLen) {
362 nameLen = strlen(name);
363 }
364 return findExtension(mExtensionString.string(), name, nameLen);
365}
366
Jesse Halla0fef1c2012-04-17 12:02:26 -0700367// ----------------------------------------------------------------------------
368
369bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
370 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700371 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
372 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700373
Jesse Hallb29e5e82012-04-04 16:53:42 -0700374 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700375 if (strength == STRONG)
376 mAttemptHibernation = false;
377
Jesse Hall25838592012-04-05 15:53:28 -0700378 if (CC_UNLIKELY(mHibernating)) {
379 ALOGV("Awakening\n");
380 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700381
382 // These conditions should be guaranteed before entering hibernation;
383 // we don't want to get into a state where we can't wake up.
384 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
385 "Invalid hibernation state, unable to awaken\n");
386
Jesse Hall25838592012-04-05 15:53:28 -0700387 if (!cnx->egl.eglAwakenProcessIMG()) {
388 ALOGE("Failed to awaken EGL implementation\n");
389 return false;
390 }
391 mHibernating = false;
392 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700393 return true;
394}
395
Jesse Halla0fef1c2012-04-17 12:02:26 -0700396void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
397 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700398 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700399
400 mWakeCount--;
401 if (strength == STRONG)
402 mAttemptHibernation = true;
403
404 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700405 egl_connection_t* const cnx = &gEGLImpl;
406 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700407 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700408 cnx->egl.eglHibernateProcessIMG &&
409 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700410 ALOGV("Hibernating\n");
411 if (!cnx->egl.eglHibernateProcessIMG()) {
412 ALOGE("Failed to hibernate EGL implementation\n");
413 return;
414 }
415 mHibernating = true;
416 }
417 }
418}
419
Jesse Halla0fef1c2012-04-17 12:02:26 -0700420void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
421 Mutex::Autolock _l(mLock);
422 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700423}
424
Mathias Agopian518ec112011-05-13 16:21:08 -0700425// ----------------------------------------------------------------------------
426}; // namespace android
427// ----------------------------------------------------------------------------