blob: ab21c8f1160f24c442907aab9b627d25961f9a92 [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
Mathias Agopian518ec112011-05-13 16:21:08 -070092EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
93 if (uintptr_t(disp) >= NUM_DISPLAYS)
94 return NULL;
95
96 return sDisplay[uintptr_t(disp)].getDisplay(disp);
97}
98
99EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
100
101 Mutex::Autolock _l(lock);
102
103 // get our driver loader
104 Loader& loader(Loader::getInstance());
105
Mathias Agopianada798b2012-02-13 17:09:30 -0800106 egl_connection_t* const cnx = &gEGLImpl;
107 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
108 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
109 disp.dpy = dpy;
110 if (dpy == EGL_NO_DISPLAY) {
111 loader.close(cnx->dso);
112 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700113 }
114 }
115
116 return EGLDisplay(uintptr_t(display) + 1U);
117}
118
119EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
120
Michael Lentine54466bc2015-01-27 09:01:03 -0800121 {
122 Mutex::Autolock _rf(refLock);
Mathias Agopian518ec112011-05-13 16:21:08 -0700123
Michael Lentine54466bc2015-01-27 09:01:03 -0800124 refs++;
125 if (refs > 1) {
126 if (major != NULL)
127 *major = VERSION_MAJOR;
128 if (minor != NULL)
129 *minor = VERSION_MINOR;
130 while(!eglIsInitialized) refCond.wait(refLock);
131 return EGL_TRUE;
132 }
133
134 while(eglIsInitialized) refCond.wait(refLock);
135 }
136
137 {
138 Mutex::Autolock _l(lock);
139
Michael Lentine54466bc2015-01-27 09:01:03 -0800140 setGLHooksThreadSpecific(&gHooksNoContext);
141
142 // initialize each EGL and
143 // build our own extension string first, based on the extension we know
144 // and the extension supported by our client implementation
145
146 egl_connection_t* const cnx = &gEGLImpl;
147 cnx->major = -1;
148 cnx->minor = -1;
149 if (cnx->dso) {
150 EGLDisplay idpy = disp.dpy;
151 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
152 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
153 // idpy, cnx->major, cnx->minor, cnx);
154
155 // display is now initialized
156 disp.state = egl_display_t::INITIALIZED;
157
158 // get the query-strings for this display for each implementation
159 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
160 EGL_VENDOR);
161 disp.queryString.version = cnx->egl.eglQueryString(idpy,
162 EGL_VERSION);
163 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
164 EGL_EXTENSIONS);
165 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
166 EGL_CLIENT_APIS);
167
168 } else {
169 ALOGW("eglInitialize(%p) failed (%s)", idpy,
170 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
171 }
172 }
173
174 // the query strings are per-display
175 mVendorString.setTo(sVendorString);
176 mVersionString.setTo(sVersionString);
177 mClientApiString.setTo(sClientApiString);
178
179 mExtensionString.setTo(gBuiltinExtensionString);
180 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800181 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400182 // length of the extension name
183 size_t len = strcspn(start, " ");
184 if (len) {
185 // NOTE: we could avoid the copy if we had strnstr.
186 const String8 ext(start, len);
187 if (findExtension(disp.queryString.extensions, ext.string(),
188 len)) {
189 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800190 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400191 // advance to the next extension name, skipping the space.
192 start += len;
193 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800194 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400195 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800196
197 egl_cache_t::get()->initialize(this);
198
199 char value[PROPERTY_VALUE_MAX];
200 property_get("debug.egl.finish", value, "0");
201 if (atoi(value)) {
202 finishOnSwap = true;
203 }
204
205 property_get("debug.egl.traceGpuCompletion", value, "0");
206 if (atoi(value)) {
207 traceGpuCompletion = true;
208 }
209
Mathias Agopian518ec112011-05-13 16:21:08 -0700210 if (major != NULL)
211 *major = VERSION_MAJOR;
212 if (minor != NULL)
213 *minor = VERSION_MINOR;
Michael Lentine54466bc2015-01-27 09:01:03 -0800214
215 mHibernation.setDisplayValid(true);
Mathias Agopian518ec112011-05-13 16:21:08 -0700216 }
217
Michael Lentine54466bc2015-01-27 09:01:03 -0800218 {
219 Mutex::Autolock _rf(refLock);
220 eglIsInitialized = true;
221 refCond.broadcast();
Mathias Agopian518ec112011-05-13 16:21:08 -0700222 }
223
Mathias Agopian7773c432012-02-13 20:06:08 -0800224 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700225}
226
227EGLBoolean egl_display_t::terminate() {
228
Michael Lentine54466bc2015-01-27 09:01:03 -0800229 {
230 Mutex::Autolock _rl(refLock);
231 if (refs == 0) {
232 /*
233 * From the EGL spec (3.2):
234 * "Termination of a display that has already been terminated,
235 * (...), is allowed, but the only effect of such a call is
236 * to return EGL_TRUE (...)
237 */
238 return EGL_TRUE;
239 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700240
Michael Lentine54466bc2015-01-27 09:01:03 -0800241 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700242 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800243 if (refs > 0) {
244 return EGL_TRUE;
245 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700246 }
247
248 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800249
250 {
251 Mutex::Autolock _l(lock);
252
253 egl_connection_t* const cnx = &gEGLImpl;
254 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
255 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
256 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
257 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
258 }
259 // REVISIT: it's unclear what to do if eglTerminate() fails
260 disp.state = egl_display_t::TERMINATED;
261 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700262 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800263
264 mHibernation.setDisplayValid(false);
265
266 // Reset the extension string since it will be regenerated if we get
267 // reinitialized.
268 mExtensionString.setTo("");
269
270 // Mark all objects remaining in the list as terminated, unless
271 // there are no reference to them, it which case, we're free to
272 // delete them.
273 size_t count = objects.size();
274 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
275 for (size_t i=0 ; i<count ; i++) {
276 egl_object_t* o = objects.itemAt(i);
277 o->destroy();
278 }
279
280 // this marks all object handles are "terminated"
281 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700282 }
283
Michael Lentine54466bc2015-01-27 09:01:03 -0800284 {
285 Mutex::Autolock _rl(refLock);
286 eglIsInitialized = false;
287 refCond.broadcast();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700288 }
289
Mathias Agopian518ec112011-05-13 16:21:08 -0700290 return res;
291}
292
Mathias Agopianfb87e542012-01-30 18:20:52 -0800293void egl_display_t::loseCurrent(egl_context_t * cur_c)
294{
295 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800296 egl_display_t* display = cur_c->getDisplay();
297 if (display) {
298 display->loseCurrentImpl(cur_c);
299 }
300 }
301}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800302
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800303void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
304{
305 // by construction, these are either 0 or valid (possibly terminated)
306 // it should be impossible for these to be invalid
307 ContextRef _cur_c(cur_c);
308 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
309 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800310
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800311 { // scope for the lock
312 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800313 cur_c->onLooseCurrent();
314
Mathias Agopianfb87e542012-01-30 18:20:52 -0800315 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800316
317 // This cannot be called with the lock held because it might end-up
318 // calling back into EGL (in particular when a surface is destroyed
319 // it calls ANativeWindow::disconnect
320 _cur_c.release();
321 _cur_r.release();
322 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800323}
324
325EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700326 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800327 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
328{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800329 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800330
331 // by construction, these are either 0 or valid (possibly terminated)
332 // it should be impossible for these to be invalid
333 ContextRef _cur_c(cur_c);
334 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
335 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
336
337 { // scope for the lock
338 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800339 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800340 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800341 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800342 if (result == EGL_TRUE) {
343 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700344 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700345 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700346 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800347 }
348 } else {
349 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800350 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800351 if (result == EGL_TRUE) {
352 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700353 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800354 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800355 }
356 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800357
358 if (result == EGL_TRUE) {
359 // This cannot be called with the lock held because it might end-up
360 // calling back into EGL (in particular when a surface is destroyed
361 // it calls ANativeWindow::disconnect
362 _cur_c.release();
363 _cur_r.release();
364 _cur_d.release();
365 }
366
Mathias Agopianfb87e542012-01-30 18:20:52 -0800367 return result;
368}
Mathias Agopian518ec112011-05-13 16:21:08 -0700369
Jesse Hallc2e41222013-08-08 13:40:22 -0700370bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
371 if (!nameLen) {
372 nameLen = strlen(name);
373 }
374 return findExtension(mExtensionString.string(), name, nameLen);
375}
376
Jesse Halla0fef1c2012-04-17 12:02:26 -0700377// ----------------------------------------------------------------------------
378
379bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
380 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700381 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
382 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700383
Jesse Hallb29e5e82012-04-04 16:53:42 -0700384 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700385 if (strength == STRONG)
386 mAttemptHibernation = false;
387
Jesse Hall25838592012-04-05 15:53:28 -0700388 if (CC_UNLIKELY(mHibernating)) {
389 ALOGV("Awakening\n");
390 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700391
392 // These conditions should be guaranteed before entering hibernation;
393 // we don't want to get into a state where we can't wake up.
394 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
395 "Invalid hibernation state, unable to awaken\n");
396
Jesse Hall25838592012-04-05 15:53:28 -0700397 if (!cnx->egl.eglAwakenProcessIMG()) {
398 ALOGE("Failed to awaken EGL implementation\n");
399 return false;
400 }
401 mHibernating = false;
402 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700403 return true;
404}
405
Jesse Halla0fef1c2012-04-17 12:02:26 -0700406void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
407 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700408 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700409
410 mWakeCount--;
411 if (strength == STRONG)
412 mAttemptHibernation = true;
413
414 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700415 egl_connection_t* const cnx = &gEGLImpl;
416 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700417 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700418 cnx->egl.eglHibernateProcessIMG &&
419 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700420 ALOGV("Hibernating\n");
421 if (!cnx->egl.eglHibernateProcessIMG()) {
422 ALOGE("Failed to hibernate EGL implementation\n");
423 return;
424 }
425 mHibernating = true;
426 }
427 }
428}
429
Jesse Halla0fef1c2012-04-17 12:02:26 -0700430void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
431 Mutex::Autolock _l(mLock);
432 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700433}
434
Mathias Agopian518ec112011-05-13 16:21:08 -0700435// ----------------------------------------------------------------------------
436}; // namespace android
437// ----------------------------------------------------------------------------