blob: c368bad6bb2a4330212e12b675314872ca602ff6 [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
Pablo Ceballos541de492016-04-09 15:56:12 -070093void egl_display_t::addContext(egl_context_t* context) {
94 Mutex::Autolock _l(lock);
95 contexts.add(context);
96}
97
98void egl_display_t::removeContext(egl_context_t* context) {
99 Mutex::Autolock _l(lock);
100 contexts.remove(context);
101}
102
103void egl_display_t::removeSurface(EGLSurface surface) const {
104 Mutex::Autolock _l(lock);
105 for (size_t i = 0; i < contexts.size(); i++) {
106 egl_context_t* context = contexts[i];
107 if (context->read == surface) {
108 SurfaceRef _r(get_surface(context->read));
109 _r.release();
110 }
111 if (context->draw == surface) {
112 SurfaceRef _d(get_surface(context->draw));
113 _d.release();
114 }
115 }
116}
117
Mathias Agopian518ec112011-05-13 16:21:08 -0700118EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
119 if (uintptr_t(disp) >= NUM_DISPLAYS)
120 return NULL;
121
122 return sDisplay[uintptr_t(disp)].getDisplay(disp);
123}
124
125EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
126
127 Mutex::Autolock _l(lock);
128
129 // get our driver loader
130 Loader& loader(Loader::getInstance());
131
Mathias Agopianada798b2012-02-13 17:09:30 -0800132 egl_connection_t* const cnx = &gEGLImpl;
133 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
134 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
135 disp.dpy = dpy;
136 if (dpy == EGL_NO_DISPLAY) {
137 loader.close(cnx->dso);
138 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700139 }
140 }
141
142 return EGLDisplay(uintptr_t(display) + 1U);
143}
144
145EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
146
Michael Lentine54466bc2015-01-27 09:01:03 -0800147 {
148 Mutex::Autolock _rf(refLock);
Mathias Agopian518ec112011-05-13 16:21:08 -0700149
Michael Lentine54466bc2015-01-27 09:01:03 -0800150 refs++;
151 if (refs > 1) {
152 if (major != NULL)
153 *major = VERSION_MAJOR;
154 if (minor != NULL)
155 *minor = VERSION_MINOR;
156 while(!eglIsInitialized) refCond.wait(refLock);
157 return EGL_TRUE;
158 }
159
160 while(eglIsInitialized) refCond.wait(refLock);
161 }
162
163 {
164 Mutex::Autolock _l(lock);
165
Michael Lentine54466bc2015-01-27 09:01:03 -0800166 setGLHooksThreadSpecific(&gHooksNoContext);
167
168 // initialize each EGL and
169 // build our own extension string first, based on the extension we know
170 // and the extension supported by our client implementation
171
172 egl_connection_t* const cnx = &gEGLImpl;
173 cnx->major = -1;
174 cnx->minor = -1;
175 if (cnx->dso) {
176 EGLDisplay idpy = disp.dpy;
177 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
178 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
179 // idpy, cnx->major, cnx->minor, cnx);
180
181 // display is now initialized
182 disp.state = egl_display_t::INITIALIZED;
183
184 // get the query-strings for this display for each implementation
185 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
186 EGL_VENDOR);
187 disp.queryString.version = cnx->egl.eglQueryString(idpy,
188 EGL_VERSION);
189 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
190 EGL_EXTENSIONS);
191 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
192 EGL_CLIENT_APIS);
193
194 } else {
195 ALOGW("eglInitialize(%p) failed (%s)", idpy,
196 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
197 }
198 }
199
200 // the query strings are per-display
201 mVendorString.setTo(sVendorString);
202 mVersionString.setTo(sVersionString);
203 mClientApiString.setTo(sClientApiString);
204
205 mExtensionString.setTo(gBuiltinExtensionString);
206 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800207 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400208 // length of the extension name
209 size_t len = strcspn(start, " ");
210 if (len) {
211 // NOTE: we could avoid the copy if we had strnstr.
212 const String8 ext(start, len);
213 if (findExtension(disp.queryString.extensions, ext.string(),
214 len)) {
215 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800216 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400217 // advance to the next extension name, skipping the space.
218 start += len;
219 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800220 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400221 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800222
223 egl_cache_t::get()->initialize(this);
224
225 char value[PROPERTY_VALUE_MAX];
226 property_get("debug.egl.finish", value, "0");
227 if (atoi(value)) {
228 finishOnSwap = true;
229 }
230
231 property_get("debug.egl.traceGpuCompletion", value, "0");
232 if (atoi(value)) {
233 traceGpuCompletion = true;
234 }
235
Mathias Agopian518ec112011-05-13 16:21:08 -0700236 if (major != NULL)
237 *major = VERSION_MAJOR;
238 if (minor != NULL)
239 *minor = VERSION_MINOR;
Michael Lentine54466bc2015-01-27 09:01:03 -0800240
241 mHibernation.setDisplayValid(true);
Mathias Agopian518ec112011-05-13 16:21:08 -0700242 }
243
Michael Lentine54466bc2015-01-27 09:01:03 -0800244 {
245 Mutex::Autolock _rf(refLock);
246 eglIsInitialized = true;
247 refCond.broadcast();
Mathias Agopian518ec112011-05-13 16:21:08 -0700248 }
249
Mathias Agopian7773c432012-02-13 20:06:08 -0800250 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700251}
252
253EGLBoolean egl_display_t::terminate() {
254
Michael Lentine54466bc2015-01-27 09:01:03 -0800255 {
256 Mutex::Autolock _rl(refLock);
257 if (refs == 0) {
258 /*
259 * From the EGL spec (3.2):
260 * "Termination of a display that has already been terminated,
261 * (...), is allowed, but the only effect of such a call is
262 * to return EGL_TRUE (...)
263 */
264 return EGL_TRUE;
265 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700266
Michael Lentine54466bc2015-01-27 09:01:03 -0800267 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700268 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800269 if (refs > 0) {
270 return EGL_TRUE;
271 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700272 }
273
274 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800275
276 {
277 Mutex::Autolock _l(lock);
278
279 egl_connection_t* const cnx = &gEGLImpl;
280 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
281 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
282 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
283 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
284 }
285 // REVISIT: it's unclear what to do if eglTerminate() fails
286 disp.state = egl_display_t::TERMINATED;
287 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700288 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800289
290 mHibernation.setDisplayValid(false);
291
292 // Reset the extension string since it will be regenerated if we get
293 // reinitialized.
294 mExtensionString.setTo("");
295
296 // Mark all objects remaining in the list as terminated, unless
297 // there are no reference to them, it which case, we're free to
298 // delete them.
299 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800300 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Michael Lentine54466bc2015-01-27 09:01:03 -0800301 for (size_t i=0 ; i<count ; i++) {
302 egl_object_t* o = objects.itemAt(i);
303 o->destroy();
304 }
305
306 // this marks all object handles are "terminated"
307 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700308 }
309
Michael Lentine54466bc2015-01-27 09:01:03 -0800310 {
311 Mutex::Autolock _rl(refLock);
312 eglIsInitialized = false;
313 refCond.broadcast();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700314 }
315
Mathias Agopian518ec112011-05-13 16:21:08 -0700316 return res;
317}
318
Mathias Agopianfb87e542012-01-30 18:20:52 -0800319void egl_display_t::loseCurrent(egl_context_t * cur_c)
320{
321 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800322 egl_display_t* display = cur_c->getDisplay();
323 if (display) {
324 display->loseCurrentImpl(cur_c);
325 }
326 }
327}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800328
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800329void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
330{
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);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800336
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800337 { // scope for the lock
338 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800339 cur_c->onLooseCurrent();
340
Mathias Agopianfb87e542012-01-30 18:20:52 -0800341 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800342
343 // This cannot be called with the lock held because it might end-up
344 // calling back into EGL (in particular when a surface is destroyed
345 // it calls ANativeWindow::disconnect
346 _cur_c.release();
347 _cur_r.release();
348 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800349}
350
351EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700352 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800353 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
354{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800355 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800356
357 // by construction, these are either 0 or valid (possibly terminated)
358 // it should be impossible for these to be invalid
359 ContextRef _cur_c(cur_c);
360 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
361 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
362
363 { // scope for the lock
364 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800365 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800366 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800367 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800368 if (result == EGL_TRUE) {
369 c->onMakeCurrent(draw, read);
Jesse Hall25838592012-04-05 15:53:28 -0700370 if (!cur_c) {
Jesse Halla0fef1c2012-04-17 12:02:26 -0700371 mHibernation.incWakeCount(HibernationMachine::STRONG);
Jesse Hall25838592012-04-05 15:53:28 -0700372 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800373 }
374 } else {
375 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800376 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800377 if (result == EGL_TRUE) {
378 cur_c->onLooseCurrent();
Jesse Halla0fef1c2012-04-17 12:02:26 -0700379 mHibernation.decWakeCount(HibernationMachine::STRONG);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800380 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800381 }
382 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800383
384 if (result == EGL_TRUE) {
385 // This cannot be called with the lock held because it might end-up
386 // calling back into EGL (in particular when a surface is destroyed
387 // it calls ANativeWindow::disconnect
388 _cur_c.release();
389 _cur_r.release();
390 _cur_d.release();
391 }
392
Mathias Agopianfb87e542012-01-30 18:20:52 -0800393 return result;
394}
Mathias Agopian518ec112011-05-13 16:21:08 -0700395
Jesse Hallc2e41222013-08-08 13:40:22 -0700396bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
397 if (!nameLen) {
398 nameLen = strlen(name);
399 }
400 return findExtension(mExtensionString.string(), name, nameLen);
401}
402
Jesse Halla0fef1c2012-04-17 12:02:26 -0700403// ----------------------------------------------------------------------------
404
405bool egl_display_t::HibernationMachine::incWakeCount(WakeRefStrength strength) {
406 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700407 ALOGE_IF(mWakeCount < 0 || mWakeCount == INT32_MAX,
408 "Invalid WakeCount (%d) on enter\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700409
Jesse Hallb29e5e82012-04-04 16:53:42 -0700410 mWakeCount++;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700411 if (strength == STRONG)
412 mAttemptHibernation = false;
413
Jesse Hall25838592012-04-05 15:53:28 -0700414 if (CC_UNLIKELY(mHibernating)) {
415 ALOGV("Awakening\n");
416 egl_connection_t* const cnx = &gEGLImpl;
Jesse Halla0fef1c2012-04-17 12:02:26 -0700417
418 // These conditions should be guaranteed before entering hibernation;
419 // we don't want to get into a state where we can't wake up.
420 ALOGD_IF(!mDpyValid || !cnx->egl.eglAwakenProcessIMG,
421 "Invalid hibernation state, unable to awaken\n");
422
Jesse Hall25838592012-04-05 15:53:28 -0700423 if (!cnx->egl.eglAwakenProcessIMG()) {
424 ALOGE("Failed to awaken EGL implementation\n");
425 return false;
426 }
427 mHibernating = false;
428 }
Jesse Hallb29e5e82012-04-04 16:53:42 -0700429 return true;
430}
431
Jesse Halla0fef1c2012-04-17 12:02:26 -0700432void egl_display_t::HibernationMachine::decWakeCount(WakeRefStrength strength) {
433 Mutex::Autolock _l(mLock);
Jesse Hallb29e5e82012-04-04 16:53:42 -0700434 ALOGE_IF(mWakeCount <= 0, "Invalid WakeCount (%d) on leave\n", mWakeCount);
Jesse Halla0fef1c2012-04-17 12:02:26 -0700435
436 mWakeCount--;
437 if (strength == STRONG)
438 mAttemptHibernation = true;
439
440 if (mWakeCount == 0 && CC_UNLIKELY(mAttemptHibernation)) {
Jesse Hall25838592012-04-05 15:53:28 -0700441 egl_connection_t* const cnx = &gEGLImpl;
442 mAttemptHibernation = false;
Jesse Hall201f3b22012-05-04 14:52:40 -0700443 if (mAllowHibernation && mDpyValid &&
Jesse Halla0fef1c2012-04-17 12:02:26 -0700444 cnx->egl.eglHibernateProcessIMG &&
445 cnx->egl.eglAwakenProcessIMG) {
Jesse Hall25838592012-04-05 15:53:28 -0700446 ALOGV("Hibernating\n");
447 if (!cnx->egl.eglHibernateProcessIMG()) {
448 ALOGE("Failed to hibernate EGL implementation\n");
449 return;
450 }
451 mHibernating = true;
452 }
453 }
454}
455
Jesse Halla0fef1c2012-04-17 12:02:26 -0700456void egl_display_t::HibernationMachine::setDisplayValid(bool valid) {
457 Mutex::Autolock _l(mLock);
458 mDpyValid = valid;
Jesse Hallb29e5e82012-04-04 16:53:42 -0700459}
460
Mathias Agopian518ec112011-05-13 16:21:08 -0700461// ----------------------------------------------------------------------------
462}; // namespace android
463// ----------------------------------------------------------------------------