blob: 77a5f11b77c00afb7541f728d596bae532cf18e8 [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() :
Jesse Halla0fef1c2012-04-17 12:02:26 -070060 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0) {
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
123 Mutex::Autolock _l(lock);
124
125 if (refs > 0) {
126 if (major != NULL)
127 *major = VERSION_MAJOR;
128 if (minor != NULL)
129 *minor = VERSION_MINOR;
130 refs++;
131 return EGL_TRUE;
132 }
133
134#if EGL_TRACE
135
136 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
137 // the information from that call may be stale.)
138 initEglTraceLevel();
Siva Velusamyb13c78f2012-03-09 14:51:28 -0800139 initEglDebugLevel();
Mathias Agopian518ec112011-05-13 16:21:08 -0700140
141#endif
142
143 setGLHooksThreadSpecific(&gHooksNoContext);
144
145 // initialize each EGL and
146 // build our own extension string first, based on the extension we know
147 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800148
149 egl_connection_t* const cnx = &gEGLImpl;
150 cnx->major = -1;
151 cnx->minor = -1;
152 if (cnx->dso) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800153 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700154 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800155 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
156 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700157
158 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800159 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700160
161 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800162 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700163 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800164 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700165 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800166 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700167 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800168 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700169 EGL_CLIENT_APIS);
170
171 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800172 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700173 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Tom Hudsone11b1a72014-09-23 12:00:41 -0400174 return EGL_FALSE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700175 }
176 }
177
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800178 // the query strings are per-display
179 mVendorString.setTo(sVendorString);
180 mVersionString.setTo(sVersionString);
181 mClientApiString.setTo(sClientApiString);
182
Jesse Hall21558da2013-08-06 15:31:22 -0700183 mExtensionString.setTo(gBuiltinExtensionString);
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -0700184 char const* start = gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800185 char const* end;
186 do {
187 // find the space separating this extension for the next one
188 end = strchr(start, ' ');
189 if (end) {
190 // length of the extension string
191 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800192 if (len) {
193 // NOTE: we could avoid the copy if we had strnstr.
194 const String8 ext(start, len);
Jesse Hallc2e41222013-08-08 13:40:22 -0700195 if (findExtension(disp.queryString.extensions, ext.string(),
196 len)) {
197 mExtensionString.append(start, len+1);
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800198 }
199 }
200 // process the next extension string, and skip the space.
201 start = end + 1;
202 }
203 } while (end);
204
Jamie Gennisaca51c02011-11-03 17:42:43 -0700205 egl_cache_t::get()->initialize(this);
206
Mathias Agopian7db993a2012-03-25 00:49:46 -0700207 char value[PROPERTY_VALUE_MAX];
208 property_get("debug.egl.finish", value, "0");
209 if (atoi(value)) {
210 finishOnSwap = true;
211 }
212
Jamie Gennis28ef8d72012-04-05 20:34:54 -0700213 property_get("debug.egl.traceGpuCompletion", value, "0");
214 if (atoi(value)) {
215 traceGpuCompletion = true;
216 }
217
Mathias Agopian7773c432012-02-13 20:06:08 -0800218 refs++;
219 if (major != NULL)
220 *major = VERSION_MAJOR;
221 if (minor != NULL)
222 *minor = VERSION_MINOR;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700223
224 mHibernation.setDisplayValid(true);
225
Mathias Agopian7773c432012-02-13 20:06:08 -0800226 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700227}
228
229EGLBoolean egl_display_t::terminate() {
230
231 Mutex::Autolock _l(lock);
232
233 if (refs == 0) {
Mathias Agopianfe981272012-06-13 15:21:21 -0700234 /*
235 * From the EGL spec (3.2):
236 * "Termination of a display that has already been terminated,
237 * (...), is allowed, but the only effect of such a call is
238 * to return EGL_TRUE (...)
239 */
240 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700241 }
242
243 // this is specific to Android, display termination is ref-counted.
244 if (refs > 1) {
245 refs--;
246 return EGL_TRUE;
247 }
248
249 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800250 egl_connection_t* const cnx = &gEGLImpl;
251 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
252 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
253 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
254 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700255 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800256 // REVISIT: it's unclear what to do if eglTerminate() fails
Mathias Agopianada798b2012-02-13 17:09:30 -0800257 disp.state = egl_display_t::TERMINATED;
Mathias Agopianada798b2012-02-13 17:09:30 -0800258 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700259 }
260
Jesse Halla0fef1c2012-04-17 12:02:26 -0700261 mHibernation.setDisplayValid(false);
262
Jamie Gennisa08cf6e2012-09-16 14:02:20 -0700263 // Reset the extension string since it will be regenerated if we get
264 // reinitialized.
265 mExtensionString.setTo("");
266
Mathias Agopian5b287a62011-05-16 18:58:55 -0700267 // Mark all objects remaining in the list as terminated, unless
268 // there are no reference to them, it which case, we're free to
269 // delete them.
270 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000271 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700272 for (size_t i=0 ; i<count ; i++) {
273 egl_object_t* o = objects.itemAt(i);
274 o->destroy();
275 }
276
277 // this marks all object handles are "terminated"
278 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700279
280 refs--;
Mathias Agopian518ec112011-05-13 16:21:08 -0700281 return res;
282}
283
Mathias Agopianfb87e542012-01-30 18:20:52 -0800284void egl_display_t::loseCurrent(egl_context_t * cur_c)
285{
286 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800287 egl_display_t* display = cur_c->getDisplay();
288 if (display) {
289 display->loseCurrentImpl(cur_c);
290 }
291 }
292}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800293
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800294void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
295{
296 // by construction, these are either 0 or valid (possibly terminated)
297 // it should be impossible for these to be invalid
298 ContextRef _cur_c(cur_c);
299 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
300 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800301
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800302 { // scope for the lock
303 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800304 cur_c->onLooseCurrent();
305
Mathias Agopianfb87e542012-01-30 18:20:52 -0800306 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800307
308 // This cannot be called with the lock held because it might end-up
309 // calling back into EGL (in particular when a surface is destroyed
310 // it calls ANativeWindow::disconnect
311 _cur_c.release();
312 _cur_r.release();
313 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800314}
315
316EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700317 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800318 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
319{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800320 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800321
322 // by construction, these are either 0 or valid (possibly terminated)
323 // it should be impossible for these to be invalid
324 ContextRef _cur_c(cur_c);
325 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
326 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
327
328 { // scope for the lock
329 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800330 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800331 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800332 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800333 if (result == EGL_TRUE) {
334 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700335 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700336 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700337 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800338 }
339 } else {
340 result = cur_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 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700344 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800345 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800346 }
347 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800348
349 if (result == EGL_TRUE) {
350 // This cannot be called with the lock held because it might end-up
351 // calling back into EGL (in particular when a surface is destroyed
352 // it calls ANativeWindow::disconnect
353 _cur_c.release();
354 _cur_r.release();
355 _cur_d.release();
356 }
357
Mathias Agopianfb87e542012-01-30 18:20:52 -0800358 return result;
359}
Mathias Agopian518ec112011-05-13 16:21:08 -0700360
Jesse Hallc2e41222013-08-08 13:40:22 -0700361bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
362 if (!nameLen) {
363 nameLen = strlen(name);
364 }
365 return findExtension(mExtensionString.string(), name, nameLen);
366}
367
Jesse Halla0fef1c2012-04-17 12:02:26 -0700368// ----------------------------------------------------------------------------
369
370bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
371 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700372 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
373 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700374
Jesse Hallb29e5e82012-04-04 16:53:42 -0700375 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700376 if (strength == STRONG)
377 mAttemptHibernation = false;
378
Jesse Hall25838592012-04-05 15:53:28 -0700379 if (CC_UNLIKELY(mHibernating)) {
380 ALOGV("Awakening\n");
381 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700382
383 // These conditions should be guaranteed before entering hibernation;
384 // we don't want to get into a state where we can't wake up.
385 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
386 "Invalid hibernation state, unable to awaken\n");
387
Jesse Hall25838592012-04-05 15:53:28 -0700388 if (!cnx->egl.eglAwakenProcessIMG()) {
389 ALOGE("Failed to awaken EGL implementation\n");
390 return false;
391 }
392 mHibernating = false;
393 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700394 return true;
395}
396
Jesse Halla0fef1c2012-04-17 12:02:26 -0700397void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
398 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700399 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700400
401 mWakeCount--;
402 if (strength == STRONG)
403 mAttemptHibernation = true;
404
405 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700406 egl_connection_t* const cnx = &gEGLImpl;
407 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700408 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700409 cnx->egl.eglHibernateProcessIMG &&
410 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700411 ALOGV("Hibernating\n");
412 if (!cnx->egl.eglHibernateProcessIMG()) {
413 ALOGE("Failed to hibernate EGL implementation\n");
414 return;
415 }
416 mHibernating = true;
417 }
418 }
419}
420
Jesse Halla0fef1c2012-04-17 12:02:26 -0700421void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
422 Mutex::Autolock _l(mLock);
423 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700424}
425
Mathias Agopian518ec112011-05-13 16:21:08 -0700426// ----------------------------------------------------------------------------
427}; // namespace android
428// ----------------------------------------------------------------------------