blob: 1e39aae40dfd3b987129cfa0ea92653b1926ad1f [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) {
Kalle Raita7804aa22016-04-18 16:03:37 -070047 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
48 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
49 return true;
50 }
Jesse Hallc2e41222013-08-08 13:40:22 -070051 }
52 }
53 return false;
54}
55
Mathias Agopian518ec112011-05-13 16:21:08 -070056egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
57
58egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080059 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070060}
61
62egl_display_t::~egl_display_t() {
63 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080064 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070065}
66
67egl_display_t* egl_display_t::get(EGLDisplay dpy) {
68 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -070069 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
70 return nullptr;
71 }
72 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -070073}
74
75void egl_display_t::addObject(egl_object_t* object) {
76 Mutex::Autolock _l(lock);
77 objects.add(object);
78}
79
Mathias Agopian5b287a62011-05-16 18:58:55 -070080void egl_display_t::removeObject(egl_object_t* object) {
81 Mutex::Autolock _l(lock);
82 objects.remove(object);
83}
84
Mathias Agopianf0480de2011-11-13 20:50:07 -080085bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070086 Mutex::Autolock _l(lock);
87 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080088 if (object->getDisplay() == this) {
89 object->incRef();
90 return true;
91 }
Mathias Agopian518ec112011-05-13 16:21:08 -070092 }
93 return false;
94}
95
Mathias Agopian518ec112011-05-13 16:21:08 -070096EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
97 if (uintptr_t(disp) >= NUM_DISPLAYS)
98 return NULL;
99
100 return sDisplay[uintptr_t(disp)].getDisplay(disp);
101}
102
103EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
104
105 Mutex::Autolock _l(lock);
106
107 // get our driver loader
108 Loader& loader(Loader::getInstance());
109
Mathias Agopianada798b2012-02-13 17:09:30 -0800110 egl_connection_t* const cnx = &gEGLImpl;
111 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
112 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
113 disp.dpy = dpy;
114 if (dpy == EGL_NO_DISPLAY) {
115 loader.close(cnx->dso);
116 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700117 }
118 }
119
120 return EGLDisplay(uintptr_t(display) + 1U);
121}
122
123EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
124
Michael Lentine54466bc2015-01-27 09:01:03 -0800125 {
126 Mutex::Autolock _rf(refLock);
Mathias Agopian518ec112011-05-13 16:21:08 -0700127
Michael Lentine54466bc2015-01-27 09:01:03 -0800128 refs++;
129 if (refs > 1) {
130 if (major != NULL)
131 *major = VERSION_MAJOR;
132 if (minor != NULL)
133 *minor = VERSION_MINOR;
134 while(!eglIsInitialized) refCond.wait(refLock);
135 return EGL_TRUE;
136 }
137
138 while(eglIsInitialized) refCond.wait(refLock);
139 }
140
141 {
142 Mutex::Autolock _l(lock);
143
Michael Lentine54466bc2015-01-27 09:01:03 -0800144 setGLHooksThreadSpecific(&gHooksNoContext);
145
146 // initialize each EGL and
147 // build our own extension string first, based on the extension we know
148 // and the extension supported by our client implementation
149
150 egl_connection_t* const cnx = &gEGLImpl;
151 cnx->major = -1;
152 cnx->minor = -1;
153 if (cnx->dso) {
154 EGLDisplay idpy = disp.dpy;
155 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
156 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
157 // idpy, cnx->major, cnx->minor, cnx);
158
159 // display is now initialized
160 disp.state = egl_display_t::INITIALIZED;
161
162 // get the query-strings for this display for each implementation
163 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
164 EGL_VENDOR);
165 disp.queryString.version = cnx->egl.eglQueryString(idpy,
166 EGL_VERSION);
167 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
168 EGL_EXTENSIONS);
169 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
170 EGL_CLIENT_APIS);
171
172 } else {
173 ALOGW("eglInitialize(%p) failed (%s)", idpy,
174 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
175 }
176 }
177
178 // the query strings are per-display
179 mVendorString.setTo(sVendorString);
180 mVersionString.setTo(sVersionString);
181 mClientApiString.setTo(sClientApiString);
182
183 mExtensionString.setTo(gBuiltinExtensionString);
184 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800185 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400186 // length of the extension name
187 size_t len = strcspn(start, " ");
188 if (len) {
189 // NOTE: we could avoid the copy if we had strnstr.
190 const String8 ext(start, len);
191 if (findExtension(disp.queryString.extensions, ext.string(),
192 len)) {
193 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800194 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400195 // advance to the next extension name, skipping the space.
196 start += len;
197 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800198 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400199 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800200
201 egl_cache_t::get()->initialize(this);
202
203 char value[PROPERTY_VALUE_MAX];
204 property_get("debug.egl.finish", value, "0");
205 if (atoi(value)) {
206 finishOnSwap = true;
207 }
208
209 property_get("debug.egl.traceGpuCompletion", value, "0");
210 if (atoi(value)) {
211 traceGpuCompletion = true;
212 }
213
Mathias Agopian518ec112011-05-13 16:21:08 -0700214 if (major != NULL)
215 *major = VERSION_MAJOR;
216 if (minor != NULL)
217 *minor = VERSION_MINOR;
Michael Lentine54466bc2015-01-27 09:01:03 -0800218
219 mHibernation.setDisplayValid(true);
Mathias Agopian518ec112011-05-13 16:21:08 -0700220 }
221
Michael Lentine54466bc2015-01-27 09:01:03 -0800222 {
223 Mutex::Autolock _rf(refLock);
224 eglIsInitialized = true;
225 refCond.broadcast();
Mathias Agopian518ec112011-05-13 16:21:08 -0700226 }
227
Mathias Agopian7773c432012-02-13 20:06:08 -0800228 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700229}
230
231EGLBoolean egl_display_t::terminate() {
232
Michael Lentine54466bc2015-01-27 09:01:03 -0800233 {
234 Mutex::Autolock _rl(refLock);
235 if (refs == 0) {
236 /*
237 * From the EGL spec (3.2):
238 * "Termination of a display that has already been terminated,
239 * (...), is allowed, but the only effect of such a call is
240 * to return EGL_TRUE (...)
241 */
242 return EGL_TRUE;
243 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700244
Michael Lentine54466bc2015-01-27 09:01:03 -0800245 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700246 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800247 if (refs > 0) {
248 return EGL_TRUE;
249 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700250 }
251
252 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800253
254 {
255 Mutex::Autolock _l(lock);
256
257 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()));
262 }
263 // REVISIT: it's unclear what to do if eglTerminate() fails
264 disp.state = egl_display_t::TERMINATED;
265 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700266 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800267
268 mHibernation.setDisplayValid(false);
269
270 // Reset the extension string since it will be regenerated if we get
271 // reinitialized.
272 mExtensionString.setTo("");
273
274 // 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();
Dan Alberteacd31f2016-02-02 15:08:34 -0800278 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Michael Lentine54466bc2015-01-27 09:01:03 -0800279 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
Michael Lentine54466bc2015-01-27 09:01:03 -0800288 {
289 Mutex::Autolock _rl(refLock);
290 eglIsInitialized = false;
291 refCond.broadcast();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700292 }
293
Mathias Agopian518ec112011-05-13 16:21:08 -0700294 return res;
295}
296
Mathias Agopianfb87e542012-01-30 18:20:52 -0800297void egl_display_t::loseCurrent(egl_context_t * cur_c)
298{
299 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800300 egl_display_t* display = cur_c->getDisplay();
301 if (display) {
302 display->loseCurrentImpl(cur_c);
303 }
304 }
305}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800306
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800307void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
308{
309 // by construction, these are either 0 or valid (possibly terminated)
310 // it should be impossible for these to be invalid
311 ContextRef _cur_c(cur_c);
312 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
313 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800314
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800315 { // scope for the lock
316 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800317 cur_c->onLooseCurrent();
318
Mathias Agopianfb87e542012-01-30 18:20:52 -0800319 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800320
321 // This cannot be called with the lock held because it might end-up
322 // calling back into EGL (in particular when a surface is destroyed
323 // it calls ANativeWindow::disconnect
324 _cur_c.release();
325 _cur_r.release();
326 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800327}
328
329EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700330 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800331 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
332{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800333 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800334
335 // by construction, these are either 0 or valid (possibly terminated)
336 // it should be impossible for these to be invalid
337 ContextRef _cur_c(cur_c);
338 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
339 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
340
341 { // scope for the lock
342 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800343 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800344 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800345 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800346 if (result == EGL_TRUE) {
347 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700348 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700349 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700350 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800351 }
352 } else {
353 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800354 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355 if (result == EGL_TRUE) {
356 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700357 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800358 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800359 }
360 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800361
362 if (result == EGL_TRUE) {
363 // This cannot be called with the lock held because it might end-up
364 // calling back into EGL (in particular when a surface is destroyed
365 // it calls ANativeWindow::disconnect
366 _cur_c.release();
367 _cur_r.release();
368 _cur_d.release();
369 }
370
Mathias Agopianfb87e542012-01-30 18:20:52 -0800371 return result;
372}
Mathias Agopian518ec112011-05-13 16:21:08 -0700373
Jesse Hallc2e41222013-08-08 13:40:22 -0700374bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
375 if (!nameLen) {
376 nameLen = strlen(name);
377 }
378 return findExtension(mExtensionString.string(), name, nameLen);
379}
380
Jesse Halla0fef1c2012-04-17 12:02:26 -0700381// ----------------------------------------------------------------------------
382
383bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
384 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700385 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
386 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700387
Jesse Hallb29e5e82012-04-04 16:53:42 -0700388 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700389 if (strength == STRONG)
390 mAttemptHibernation = false;
391
Jesse Hall25838592012-04-05 15:53:28 -0700392 if (CC_UNLIKELY(mHibernating)) {
393 ALOGV("Awakening\n");
394 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700395
396 // These conditions should be guaranteed before entering hibernation;
397 // we don't want to get into a state where we can't wake up.
398 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
399 "Invalid hibernation state, unable to awaken\n");
400
Jesse Hall25838592012-04-05 15:53:28 -0700401 if (!cnx->egl.eglAwakenProcessIMG()) {
402 ALOGE("Failed to awaken EGL implementation\n");
403 return false;
404 }
405 mHibernating = false;
406 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700407 return true;
408}
409
Jesse Halla0fef1c2012-04-17 12:02:26 -0700410void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
411 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700412 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700413
414 mWakeCount--;
415 if (strength == STRONG)
416 mAttemptHibernation = true;
417
418 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700419 egl_connection_t* const cnx = &gEGLImpl;
420 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700421 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700422 cnx->egl.eglHibernateProcessIMG &&
423 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700424 ALOGV("Hibernating\n");
425 if (!cnx->egl.eglHibernateProcessIMG()) {
426 ALOGE("Failed to hibernate EGL implementation\n");
427 return;
428 }
429 mHibernating = true;
430 }
431 }
432}
433
Jesse Halla0fef1c2012-04-17 12:02:26 -0700434void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
435 Mutex::Autolock _l(mLock);
436 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700437}
438
Mathias Agopian518ec112011-05-13 16:21:08 -0700439// ----------------------------------------------------------------------------
440}; // namespace android
441// ----------------------------------------------------------------------------