blob: e81621bf5edbc072c65731a6e3e19773c573bbc4 [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() :
Michael Lentine54466bc2015-01-27 09:01:03 -080060 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
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
Michael Lentine54466bc2015-01-27 09:01:03 -0800123 {
124 Mutex::Autolock _rf(refLock);
Mathias Agopian518ec112011-05-13 16:21:08 -0700125
Michael Lentine54466bc2015-01-27 09:01:03 -0800126 refs++;
127 if (refs > 1) {
128 if (major != NULL)
129 *major = VERSION_MAJOR;
130 if (minor != NULL)
131 *minor = VERSION_MINOR;
132 while(!eglIsInitialized) refCond.wait(refLock);
133 return EGL_TRUE;
134 }
135
136 while(eglIsInitialized) refCond.wait(refLock);
137 }
138
139 {
140 Mutex::Autolock _l(lock);
141
142#if EGL_TRACE
143
144 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
145 // the information from that call may be stale.)
146 initEglTraceLevel();
147 initEglDebugLevel();
148
149#endif
150
151 setGLHooksThreadSpecific(&gHooksNoContext);
152
153 // initialize each EGL and
154 // build our own extension string first, based on the extension we know
155 // and the extension supported by our client implementation
156
157 egl_connection_t* const cnx = &gEGLImpl;
158 cnx->major = -1;
159 cnx->minor = -1;
160 if (cnx->dso) {
161 EGLDisplay idpy = disp.dpy;
162 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
163 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
164 // idpy, cnx->major, cnx->minor, cnx);
165
166 // display is now initialized
167 disp.state = egl_display_t::INITIALIZED;
168
169 // get the query-strings for this display for each implementation
170 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
171 EGL_VENDOR);
172 disp.queryString.version = cnx->egl.eglQueryString(idpy,
173 EGL_VERSION);
174 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
175 EGL_EXTENSIONS);
176 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
177 EGL_CLIENT_APIS);
178
179 } else {
180 ALOGW("eglInitialize(%p) failed (%s)", idpy,
181 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
182 }
183 }
184
185 // the query strings are per-display
186 mVendorString.setTo(sVendorString);
187 mVersionString.setTo(sVersionString);
188 mClientApiString.setTo(sClientApiString);
189
190 mExtensionString.setTo(gBuiltinExtensionString);
191 char const* start = gExtensionString;
192 char const* end;
193 do {
194 // find the space separating this extension for the next one
195 end = strchr(start, ' ');
196 if (end) {
197 // length of the extension string
198 const size_t len = end - start;
199 if (len) {
200 // NOTE: we could avoid the copy if we had strnstr.
201 const String8 ext(start, len);
202 if (findExtension(disp.queryString.extensions, ext.string(),
203 len)) {
204 mExtensionString.append(start, len+1);
205 }
206 }
207 // process the next extension string, and skip the space.
208 start = end + 1;
209 }
210 } while (end);
211
212 egl_cache_t::get()->initialize(this);
213
214 char value[PROPERTY_VALUE_MAX];
215 property_get("debug.egl.finish", value, "0");
216 if (atoi(value)) {
217 finishOnSwap = true;
218 }
219
220 property_get("debug.egl.traceGpuCompletion", value, "0");
221 if (atoi(value)) {
222 traceGpuCompletion = true;
223 }
224
Mathias Agopian518ec112011-05-13 16:21:08 -0700225 if (major != NULL)
226 *major = VERSION_MAJOR;
227 if (minor != NULL)
228 *minor = VERSION_MINOR;
Michael Lentine54466bc2015-01-27 09:01:03 -0800229
230 mHibernation.setDisplayValid(true);
Mathias Agopian518ec112011-05-13 16:21:08 -0700231 }
232
Michael Lentine54466bc2015-01-27 09:01:03 -0800233 {
234 Mutex::Autolock _rf(refLock);
235 eglIsInitialized = true;
236 refCond.broadcast();
Mathias Agopian518ec112011-05-13 16:21:08 -0700237 }
238
Mathias Agopian7773c432012-02-13 20:06:08 -0800239 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700240}
241
242EGLBoolean egl_display_t::terminate() {
243
Michael Lentine54466bc2015-01-27 09:01:03 -0800244 {
245 Mutex::Autolock _rl(refLock);
246 if (refs == 0) {
247 /*
248 * From the EGL spec (3.2):
249 * "Termination of a display that has already been terminated,
250 * (...), is allowed, but the only effect of such a call is
251 * to return EGL_TRUE (...)
252 */
253 return EGL_TRUE;
254 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700255
Michael Lentine54466bc2015-01-27 09:01:03 -0800256 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700257 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800258 if (refs > 0) {
259 return EGL_TRUE;
260 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700261 }
262
263 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800264
265 {
266 Mutex::Autolock _l(lock);
267
268 egl_connection_t* const cnx = &gEGLImpl;
269 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
270 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
271 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
272 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
273 }
274 // REVISIT: it's unclear what to do if eglTerminate() fails
275 disp.state = egl_display_t::TERMINATED;
276 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700277 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800278
279 mHibernation.setDisplayValid(false);
280
281 // Reset the extension string since it will be regenerated if we get
282 // reinitialized.
283 mExtensionString.setTo("");
284
285 // Mark all objects remaining in the list as terminated, unless
286 // there are no reference to them, it which case, we're free to
287 // delete them.
288 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800289 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Michael Lentine54466bc2015-01-27 09:01:03 -0800290 for (size_t i=0 ; i<count ; i++) {
291 egl_object_t* o = objects.itemAt(i);
292 o->destroy();
293 }
294
295 // this marks all object handles are "terminated"
296 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700297 }
298
Michael Lentine54466bc2015-01-27 09:01:03 -0800299 {
300 Mutex::Autolock _rl(refLock);
301 eglIsInitialized = false;
302 refCond.broadcast();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700303 }
304
Mathias Agopian518ec112011-05-13 16:21:08 -0700305 return res;
306}
307
Mathias Agopianfb87e542012-01-30 18:20:52 -0800308void egl_display_t::loseCurrent(egl_context_t * cur_c)
309{
310 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800311 egl_display_t* display = cur_c->getDisplay();
312 if (display) {
313 display->loseCurrentImpl(cur_c);
314 }
315 }
316}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800317
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800318void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
319{
320 // by construction, these are either 0 or valid (possibly terminated)
321 // it should be impossible for these to be invalid
322 ContextRef _cur_c(cur_c);
323 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
324 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800325
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800326 { // scope for the lock
327 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800328 cur_c->onLooseCurrent();
329
Mathias Agopianfb87e542012-01-30 18:20:52 -0800330 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800331
332 // This cannot be called with the lock held because it might end-up
333 // calling back into EGL (in particular when a surface is destroyed
334 // it calls ANativeWindow::disconnect
335 _cur_c.release();
336 _cur_r.release();
337 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800338}
339
340EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700341 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800342 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
343{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800344 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800345
346 // by construction, these are either 0 or valid (possibly terminated)
347 // it should be impossible for these to be invalid
348 ContextRef _cur_c(cur_c);
349 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
350 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
351
352 { // scope for the lock
353 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800354 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800356 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800357 if (result == EGL_TRUE) {
358 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700359 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700360 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700361 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800362 }
363 } else {
364 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800365 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800366 if (result == EGL_TRUE) {
367 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700368 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800369 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800370 }
371 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800372
373 if (result == EGL_TRUE) {
374 // This cannot be called with the lock held because it might end-up
375 // calling back into EGL (in particular when a surface is destroyed
376 // it calls ANativeWindow::disconnect
377 _cur_c.release();
378 _cur_r.release();
379 _cur_d.release();
380 }
381
Mathias Agopianfb87e542012-01-30 18:20:52 -0800382 return result;
383}
Mathias Agopian518ec112011-05-13 16:21:08 -0700384
Jesse Hallc2e41222013-08-08 13:40:22 -0700385bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
386 if (!nameLen) {
387 nameLen = strlen(name);
388 }
389 return findExtension(mExtensionString.string(), name, nameLen);
390}
391
Jesse Halla0fef1c2012-04-17 12:02:26 -0700392// ----------------------------------------------------------------------------
393
394bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
395 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700396 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
397 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700398
Jesse Hallb29e5e82012-04-04 16:53:42 -0700399 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700400 if (strength == STRONG)
401 mAttemptHibernation = false;
402
Jesse Hall25838592012-04-05 15:53:28 -0700403 if (CC_UNLIKELY(mHibernating)) {
404 ALOGV("Awakening\n");
405 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700406
407 // These conditions should be guaranteed before entering hibernation;
408 // we don't want to get into a state where we can't wake up.
409 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
410 "Invalid hibernation state, unable to awaken\n");
411
Jesse Hall25838592012-04-05 15:53:28 -0700412 if (!cnx->egl.eglAwakenProcessIMG()) {
413 ALOGE("Failed to awaken EGL implementation\n");
414 return false;
415 }
416 mHibernating = false;
417 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700418 return true;
419}
420
Jesse Halla0fef1c2012-04-17 12:02:26 -0700421void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
422 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700423 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700424
425 mWakeCount--;
426 if (strength == STRONG)
427 mAttemptHibernation = true;
428
429 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700430 egl_connection_t* const cnx = &gEGLImpl;
431 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700432 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700433 cnx->egl.eglHibernateProcessIMG &&
434 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700435 ALOGV("Hibernating\n");
436 if (!cnx->egl.eglHibernateProcessIMG()) {
437 ALOGE("Failed to hibernate EGL implementation\n");
438 return;
439 }
440 mHibernating = true;
441 }
442 }
443}
444
Jesse Halla0fef1c2012-04-17 12:02:26 -0700445void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
446 Mutex::Autolock _l(mLock);
447 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700448}
449
Mathias Agopian518ec112011-05-13 16:21:08 -0700450// ----------------------------------------------------------------------------
451}; // namespace android
452// ----------------------------------------------------------------------------