blob: d849693d9a8380e37744d8f1189ce8d2c736bb17 [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 setGLHooksThreadSpecific(gl_hooks_t const *value);
42
Mathias Agopian518ec112011-05-13 16:21:08 -070043// ----------------------------------------------------------------------------
44
Jesse Hallc2e41222013-08-08 13:40:22 -070045static bool findExtension(const char* exts, const char* name, size_t nameLen) {
46 if (exts) {
47 const char* match = strstr(exts, name);
48 if (match && (match[nameLen] == '\0' || match[nameLen] == ' ')) {
49 return true;
50 }
51 }
52 return false;
53}
54
Mathias Agopian518ec112011-05-13 16:21:08 -070055egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
56
57egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080058 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070059}
60
61egl_display_t::~egl_display_t() {
62 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080063 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070064}
65
66egl_display_t* egl_display_t::get(EGLDisplay dpy) {
67 uintptr_t index = uintptr_t(dpy)-1U;
68 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
69}
70
71void egl_display_t::addObject(egl_object_t* object) {
72 Mutex::Autolock _l(lock);
73 objects.add(object);
74}
75
Mathias Agopian5b287a62011-05-16 18:58:55 -070076void egl_display_t::removeObject(egl_object_t* object) {
77 Mutex::Autolock _l(lock);
78 objects.remove(object);
79}
80
Mathias Agopianf0480de2011-11-13 20:50:07 -080081bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070082 Mutex::Autolock _l(lock);
83 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080084 if (object->getDisplay() == this) {
85 object->incRef();
86 return true;
87 }
Mathias Agopian518ec112011-05-13 16:21:08 -070088 }
89 return false;
90}
91
Pablo Ceballos541de492016-04-09 15:56:12 -070092void egl_display_t::addContext(egl_context_t* context) {
93 Mutex::Autolock _l(lock);
94 contexts.add(context);
95}
96
97void egl_display_t::removeContext(egl_context_t* context) {
98 Mutex::Autolock _l(lock);
99 contexts.remove(context);
100}
101
102void egl_display_t::removeSurface(EGLSurface surface) const {
103 Mutex::Autolock _l(lock);
104 for (size_t i = 0; i < contexts.size(); i++) {
105 egl_context_t* context = contexts[i];
106 if (context->read == surface) {
107 SurfaceRef _r(get_surface(context->read));
108 _r.release();
109 }
110 if (context->draw == surface) {
111 SurfaceRef _d(get_surface(context->draw));
112 _d.release();
113 }
114 }
115}
116
Mathias Agopian518ec112011-05-13 16:21:08 -0700117EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
118 if (uintptr_t(disp) >= NUM_DISPLAYS)
119 return NULL;
120
121 return sDisplay[uintptr_t(disp)].getDisplay(disp);
122}
123
124EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
125
126 Mutex::Autolock _l(lock);
127
128 // get our driver loader
129 Loader& loader(Loader::getInstance());
130
Mathias Agopianada798b2012-02-13 17:09:30 -0800131 egl_connection_t* const cnx = &gEGLImpl;
132 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
133 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
134 disp.dpy = dpy;
135 if (dpy == EGL_NO_DISPLAY) {
136 loader.close(cnx->dso);
137 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700138 }
139 }
140
141 return EGLDisplay(uintptr_t(display) + 1U);
142}
143
144EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
145
Michael Lentine54466bc2015-01-27 09:01:03 -0800146 {
147 Mutex::Autolock _rf(refLock);
Mathias Agopian518ec112011-05-13 16:21:08 -0700148
Michael Lentine54466bc2015-01-27 09:01:03 -0800149 refs++;
150 if (refs > 1) {
151 if (major != NULL)
152 *major = VERSION_MAJOR;
153 if (minor != NULL)
154 *minor = VERSION_MINOR;
155 while(!eglIsInitialized) refCond.wait(refLock);
156 return EGL_TRUE;
157 }
158
159 while(eglIsInitialized) refCond.wait(refLock);
160 }
161
162 {
163 Mutex::Autolock _l(lock);
164
Michael Lentine54466bc2015-01-27 09:01:03 -0800165 setGLHooksThreadSpecific(&gHooksNoContext);
166
167 // initialize each EGL and
168 // build our own extension string first, based on the extension we know
169 // and the extension supported by our client implementation
170
171 egl_connection_t* const cnx = &gEGLImpl;
172 cnx->major = -1;
173 cnx->minor = -1;
174 if (cnx->dso) {
175 EGLDisplay idpy = disp.dpy;
176 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
177 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
178 // idpy, cnx->major, cnx->minor, cnx);
179
180 // display is now initialized
181 disp.state = egl_display_t::INITIALIZED;
182
183 // get the query-strings for this display for each implementation
184 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
185 EGL_VENDOR);
186 disp.queryString.version = cnx->egl.eglQueryString(idpy,
187 EGL_VERSION);
188 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
189 EGL_EXTENSIONS);
190 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
191 EGL_CLIENT_APIS);
192
193 } else {
194 ALOGW("eglInitialize(%p) failed (%s)", idpy,
195 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
196 }
197 }
198
199 // the query strings are per-display
200 mVendorString.setTo(sVendorString);
201 mVersionString.setTo(sVersionString);
202 mClientApiString.setTo(sClientApiString);
203
204 mExtensionString.setTo(gBuiltinExtensionString);
205 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800206 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400207 // length of the extension name
208 size_t len = strcspn(start, " ");
209 if (len) {
210 // NOTE: we could avoid the copy if we had strnstr.
211 const String8 ext(start, len);
212 if (findExtension(disp.queryString.extensions, ext.string(),
213 len)) {
214 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800215 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400216 // advance to the next extension name, skipping the space.
217 start += len;
218 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800219 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400220 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800221
222 egl_cache_t::get()->initialize(this);
223
224 char value[PROPERTY_VALUE_MAX];
225 property_get("debug.egl.finish", value, "0");
226 if (atoi(value)) {
227 finishOnSwap = true;
228 }
229
230 property_get("debug.egl.traceGpuCompletion", value, "0");
231 if (atoi(value)) {
232 traceGpuCompletion = true;
233 }
234
Mathias Agopian518ec112011-05-13 16:21:08 -0700235 if (major != NULL)
236 *major = VERSION_MAJOR;
237 if (minor != NULL)
238 *minor = VERSION_MINOR;
Michael Lentine54466bc2015-01-27 09:01:03 -0800239
240 mHibernation.setDisplayValid(true);
Mathias Agopian518ec112011-05-13 16:21:08 -0700241 }
242
Michael Lentine54466bc2015-01-27 09:01:03 -0800243 {
244 Mutex::Autolock _rf(refLock);
245 eglIsInitialized = true;
246 refCond.broadcast();
Mathias Agopian518ec112011-05-13 16:21:08 -0700247 }
248
Mathias Agopian7773c432012-02-13 20:06:08 -0800249 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700250}
251
252EGLBoolean egl_display_t::terminate() {
253
Michael Lentine54466bc2015-01-27 09:01:03 -0800254 {
255 Mutex::Autolock _rl(refLock);
256 if (refs == 0) {
257 /*
258 * From the EGL spec (3.2):
259 * "Termination of a display that has already been terminated,
260 * (...), is allowed, but the only effect of such a call is
261 * to return EGL_TRUE (...)
262 */
263 return EGL_TRUE;
264 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700265
Michael Lentine54466bc2015-01-27 09:01:03 -0800266 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700267 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800268 if (refs > 0) {
269 return EGL_TRUE;
270 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700271 }
272
273 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800274
275 {
276 Mutex::Autolock _l(lock);
277
278 egl_connection_t* const cnx = &gEGLImpl;
279 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
280 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
281 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
282 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
283 }
284 // REVISIT: it's unclear what to do if eglTerminate() fails
285 disp.state = egl_display_t::TERMINATED;
286 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700287 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800288
289 mHibernation.setDisplayValid(false);
290
291 // Reset the extension string since it will be regenerated if we get
292 // reinitialized.
293 mExtensionString.setTo("");
294
295 // Mark all objects remaining in the list as terminated, unless
296 // there are no reference to them, it which case, we're free to
297 // delete them.
298 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800299 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Michael Lentine54466bc2015-01-27 09:01:03 -0800300 for (size_t i=0 ; i<count ; i++) {
301 egl_object_t* o = objects.itemAt(i);
302 o->destroy();
303 }
304
305 // this marks all object handles are "terminated"
306 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700307 }
308
Michael Lentine54466bc2015-01-27 09:01:03 -0800309 {
310 Mutex::Autolock _rl(refLock);
311 eglIsInitialized = false;
312 refCond.broadcast();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700313 }
314
Mathias Agopian518ec112011-05-13 16:21:08 -0700315 return res;
316}
317
Mathias Agopianfb87e542012-01-30 18:20:52 -0800318void egl_display_t::loseCurrent(egl_context_t * cur_c)
319{
320 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800321 egl_display_t* display = cur_c->getDisplay();
322 if (display) {
323 display->loseCurrentImpl(cur_c);
324 }
325 }
326}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800327
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800328void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
329{
330 // by construction, these are either 0 or valid (possibly terminated)
331 // it should be impossible for these to be invalid
332 ContextRef _cur_c(cur_c);
333 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
334 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800335
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800336 { // scope for the lock
337 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800338 cur_c->onLooseCurrent();
339
Mathias Agopianfb87e542012-01-30 18:20:52 -0800340 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800341
342 // This cannot be called with the lock held because it might end-up
343 // calling back into EGL (in particular when a surface is destroyed
344 // it calls ANativeWindow::disconnect
345 _cur_c.release();
346 _cur_r.release();
347 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800348}
349
350EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700351 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800352 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
353{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800354 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355
356 // by construction, these are either 0 or valid (possibly terminated)
357 // it should be impossible for these to be invalid
358 ContextRef _cur_c(cur_c);
359 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
360 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
361
362 { // scope for the lock
363 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800364 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800365 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800366 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800367 if (result == EGL_TRUE) {
368 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700369 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700370 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700371 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800372 }
373 } else {
374 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800375 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800376 if (result == EGL_TRUE) {
377 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700378 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800379 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800380 }
381 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800382
383 if (result == EGL_TRUE) {
384 // This cannot be called with the lock held because it might end-up
385 // calling back into EGL (in particular when a surface is destroyed
386 // it calls ANativeWindow::disconnect
387 _cur_c.release();
388 _cur_r.release();
389 _cur_d.release();
390 }
391
Mathias Agopianfb87e542012-01-30 18:20:52 -0800392 return result;
393}
Mathias Agopian518ec112011-05-13 16:21:08 -0700394
Jesse Hallc2e41222013-08-08 13:40:22 -0700395bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
396 if (!nameLen) {
397 nameLen = strlen(name);
398 }
399 return findExtension(mExtensionString.string(), name, nameLen);
400}
401
Jesse Halla0fef1c2012-04-17 12:02:26 -0700402// ----------------------------------------------------------------------------
403
404bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
405 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700406 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
407 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700408
Jesse Hallb29e5e82012-04-04 16:53:42 -0700409 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700410 if (strength == STRONG)
411 mAttemptHibernation = false;
412
Jesse Hall25838592012-04-05 15:53:28 -0700413 if (CC_UNLIKELY(mHibernating)) {
414 ALOGV("Awakening\n");
415 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700416
417 // These conditions should be guaranteed before entering hibernation;
418 // we don't want to get into a state where we can't wake up.
419 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
420 "Invalid hibernation state, unable to awaken\n");
421
Jesse Hall25838592012-04-05 15:53:28 -0700422 if (!cnx->egl.eglAwakenProcessIMG()) {
423 ALOGE("Failed to awaken EGL implementation\n");
424 return false;
425 }
426 mHibernating = false;
427 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700428 return true;
429}
430
Jesse Halla0fef1c2012-04-17 12:02:26 -0700431void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
432 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700433 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700434
435 mWakeCount--;
436 if (strength == STRONG)
437 mAttemptHibernation = true;
438
439 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700440 egl_connection_t* const cnx = &gEGLImpl;
441 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700442 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700443 cnx->egl.eglHibernateProcessIMG &&
444 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700445 ALOGV("Hibernating\n");
446 if (!cnx->egl.eglHibernateProcessIMG()) {
447 ALOGE("Failed to hibernate EGL implementation\n");
448 return;
449 }
450 mHibernating = true;
451 }
452 }
453}
454
Jesse Halla0fef1c2012-04-17 12:02:26 -0700455void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
456 Mutex::Autolock _l(mLock);
457 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700458}
459
Mathias Agopian518ec112011-05-13 16:21:08 -0700460// ----------------------------------------------------------------------------
461}; // namespace android
462// ----------------------------------------------------------------------------