blob: e335a6cc313a2f49f1ae3cb5ddfc3b6f00c5b81a [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;
69 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
70}
71
72void egl_display_t::addObject(egl_object_t* object) {
73 Mutex::Autolock _l(lock);
74 objects.add(object);
75}
76
Mathias Agopian5b287a62011-05-16 18:58:55 -070077void egl_display_t::removeObject(egl_object_t* object) {
78 Mutex::Autolock _l(lock);
79 objects.remove(object);
80}
81
Mathias Agopianf0480de2011-11-13 20:50:07 -080082bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070083 Mutex::Autolock _l(lock);
84 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080085 if (object->getDisplay() == this) {
86 object->incRef();
87 return true;
88 }
Mathias Agopian518ec112011-05-13 16:21:08 -070089 }
90 return false;
91}
92
Mathias Agopian518ec112011-05-13 16:21:08 -070093EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
94 if (uintptr_t(disp) >= NUM_DISPLAYS)
95 return NULL;
96
97 return sDisplay[uintptr_t(disp)].getDisplay(disp);
98}
99
100EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
101
102 Mutex::Autolock _l(lock);
103
104 // get our driver loader
105 Loader& loader(Loader::getInstance());
106
Mathias Agopianada798b2012-02-13 17:09:30 -0800107 egl_connection_t* const cnx = &gEGLImpl;
108 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
109 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
110 disp.dpy = dpy;
111 if (dpy == EGL_NO_DISPLAY) {
112 loader.close(cnx->dso);
113 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700114 }
115 }
116
117 return EGLDisplay(uintptr_t(display) + 1U);
118}
119
120EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
121
Michael Lentine54466bc2015-01-27 09:01:03 -0800122 {
123 Mutex::Autolock _rf(refLock);
Mathias Agopian518ec112011-05-13 16:21:08 -0700124
Michael Lentine54466bc2015-01-27 09:01:03 -0800125 refs++;
126 if (refs > 1) {
127 if (major != NULL)
128 *major = VERSION_MAJOR;
129 if (minor != NULL)
130 *minor = VERSION_MINOR;
131 while(!eglIsInitialized) refCond.wait(refLock);
132 return EGL_TRUE;
133 }
134
135 while(eglIsInitialized) refCond.wait(refLock);
136 }
137
138 {
139 Mutex::Autolock _l(lock);
140
Michael Lentine54466bc2015-01-27 09:01:03 -0800141 setGLHooksThreadSpecific(&gHooksNoContext);
142
143 // initialize each EGL and
144 // build our own extension string first, based on the extension we know
145 // and the extension supported by our client implementation
146
147 egl_connection_t* const cnx = &gEGLImpl;
148 cnx->major = -1;
149 cnx->minor = -1;
150 if (cnx->dso) {
151 EGLDisplay idpy = disp.dpy;
152 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
153 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
154 // idpy, cnx->major, cnx->minor, cnx);
155
156 // display is now initialized
157 disp.state = egl_display_t::INITIALIZED;
158
159 // get the query-strings for this display for each implementation
160 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
161 EGL_VENDOR);
162 disp.queryString.version = cnx->egl.eglQueryString(idpy,
163 EGL_VERSION);
164 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
165 EGL_EXTENSIONS);
166 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
167 EGL_CLIENT_APIS);
168
169 } else {
170 ALOGW("eglInitialize(%p) failed (%s)", idpy,
171 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
172 }
173 }
174
175 // the query strings are per-display
176 mVendorString.setTo(sVendorString);
177 mVersionString.setTo(sVersionString);
178 mClientApiString.setTo(sClientApiString);
179
180 mExtensionString.setTo(gBuiltinExtensionString);
181 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800182 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400183 // length of the extension name
184 size_t len = strcspn(start, " ");
185 if (len) {
186 // NOTE: we could avoid the copy if we had strnstr.
187 const String8 ext(start, len);
188 if (findExtension(disp.queryString.extensions, ext.string(),
189 len)) {
190 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800191 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400192 // advance to the next extension name, skipping the space.
193 start += len;
194 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800195 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400196 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800197
198 egl_cache_t::get()->initialize(this);
199
200 char value[PROPERTY_VALUE_MAX];
201 property_get("debug.egl.finish", value, "0");
202 if (atoi(value)) {
203 finishOnSwap = true;
204 }
205
206 property_get("debug.egl.traceGpuCompletion", value, "0");
207 if (atoi(value)) {
208 traceGpuCompletion = true;
209 }
210
Mathias Agopian518ec112011-05-13 16:21:08 -0700211 if (major != NULL)
212 *major = VERSION_MAJOR;
213 if (minor != NULL)
214 *minor = VERSION_MINOR;
Michael Lentine54466bc2015-01-27 09:01:03 -0800215
216 mHibernation.setDisplayValid(true);
Mathias Agopian518ec112011-05-13 16:21:08 -0700217 }
218
Michael Lentine54466bc2015-01-27 09:01:03 -0800219 {
220 Mutex::Autolock _rf(refLock);
221 eglIsInitialized = true;
222 refCond.broadcast();
Mathias Agopian518ec112011-05-13 16:21:08 -0700223 }
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
Michael Lentine54466bc2015-01-27 09:01:03 -0800230 {
231 Mutex::Autolock _rl(refLock);
232 if (refs == 0) {
233 /*
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;
240 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700241
Michael Lentine54466bc2015-01-27 09:01:03 -0800242 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700243 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800244 if (refs > 0) {
245 return EGL_TRUE;
246 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700247 }
248
249 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800250
251 {
252 Mutex::Autolock _l(lock);
253
254 egl_connection_t* const cnx = &gEGLImpl;
255 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
256 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
257 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
258 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
259 }
260 // REVISIT: it's unclear what to do if eglTerminate() fails
261 disp.state = egl_display_t::TERMINATED;
262 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700263 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800264
265 mHibernation.setDisplayValid(false);
266
267 // Reset the extension string since it will be regenerated if we get
268 // reinitialized.
269 mExtensionString.setTo("");
270
271 // Mark all objects remaining in the list as terminated, unless
272 // there are no reference to them, it which case, we're free to
273 // delete them.
274 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800275 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Michael Lentine54466bc2015-01-27 09:01:03 -0800276 for (size_t i=0 ; i<count ; i++) {
277 egl_object_t* o = objects.itemAt(i);
278 o->destroy();
279 }
280
281 // this marks all object handles are "terminated"
282 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700283 }
284
Michael Lentine54466bc2015-01-27 09:01:03 -0800285 {
286 Mutex::Autolock _rl(refLock);
287 eglIsInitialized = false;
288 refCond.broadcast();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700289 }
290
Mathias Agopian518ec112011-05-13 16:21:08 -0700291 return res;
292}
293
Mathias Agopianfb87e542012-01-30 18:20:52 -0800294void egl_display_t::loseCurrent(egl_context_t * cur_c)
295{
296 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800297 egl_display_t* display = cur_c->getDisplay();
298 if (display) {
299 display->loseCurrentImpl(cur_c);
300 }
301 }
302}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800303
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800304void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
305{
306 // by construction, these are either 0 or valid (possibly terminated)
307 // it should be impossible for these to be invalid
308 ContextRef _cur_c(cur_c);
309 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
310 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800311
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800312 { // scope for the lock
313 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800314 cur_c->onLooseCurrent();
315
Mathias Agopianfb87e542012-01-30 18:20:52 -0800316 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800317
318 // This cannot be called with the lock held because it might end-up
319 // calling back into EGL (in particular when a surface is destroyed
320 // it calls ANativeWindow::disconnect
321 _cur_c.release();
322 _cur_r.release();
323 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800324}
325
326EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700327 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800328 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
329{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800330 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800331
332 // by construction, these are either 0 or valid (possibly terminated)
333 // it should be impossible for these to be invalid
334 ContextRef _cur_c(cur_c);
335 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
336 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
337
338 { // scope for the lock
339 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800340 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800341 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800342 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800343 if (result == EGL_TRUE) {
344 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700345 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700346 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700347 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800348 }
349 } else {
350 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800351 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800352 if (result == EGL_TRUE) {
353 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700354 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800356 }
357 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800358
359 if (result == EGL_TRUE) {
360 // This cannot be called with the lock held because it might end-up
361 // calling back into EGL (in particular when a surface is destroyed
362 // it calls ANativeWindow::disconnect
363 _cur_c.release();
364 _cur_r.release();
365 _cur_d.release();
366 }
367
Mathias Agopianfb87e542012-01-30 18:20:52 -0800368 return result;
369}
Mathias Agopian518ec112011-05-13 16:21:08 -0700370
Jesse Hallc2e41222013-08-08 13:40:22 -0700371bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
372 if (!nameLen) {
373 nameLen = strlen(name);
374 }
375 return findExtension(mExtensionString.string(), name, nameLen);
376}
377
Jesse Halla0fef1c2012-04-17 12:02:26 -0700378// ----------------------------------------------------------------------------
379
380bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
381 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700382 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
383 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700384
Jesse Hallb29e5e82012-04-04 16:53:42 -0700385 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700386 if (strength == STRONG)
387 mAttemptHibernation = false;
388
Jesse Hall25838592012-04-05 15:53:28 -0700389 if (CC_UNLIKELY(mHibernating)) {
390 ALOGV("Awakening\n");
391 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700392
393 // These conditions should be guaranteed before entering hibernation;
394 // we don't want to get into a state where we can't wake up.
395 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
396 "Invalid hibernation state, unable to awaken\n");
397
Jesse Hall25838592012-04-05 15:53:28 -0700398 if (!cnx->egl.eglAwakenProcessIMG()) {
399 ALOGE("Failed to awaken EGL implementation\n");
400 return false;
401 }
402 mHibernating = false;
403 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700404 return true;
405}
406
Jesse Halla0fef1c2012-04-17 12:02:26 -0700407void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
408 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700409 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700410
411 mWakeCount--;
412 if (strength == STRONG)
413 mAttemptHibernation = true;
414
415 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700416 egl_connection_t* const cnx = &gEGLImpl;
417 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700418 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700419 cnx->egl.eglHibernateProcessIMG &&
420 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700421 ALOGV("Hibernating\n");
422 if (!cnx->egl.eglHibernateProcessIMG()) {
423 ALOGE("Failed to hibernate EGL implementation\n");
424 return;
425 }
426 mHibernating = true;
427 }
428 }
429}
430
Jesse Halla0fef1c2012-04-17 12:02:26 -0700431void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
432 Mutex::Autolock _l(mLock);
433 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700434}
435
Mathias Agopian518ec112011-05-13 16:21:08 -0700436// ----------------------------------------------------------------------------
437}; // namespace android
438// ----------------------------------------------------------------------------