blob: 10523f50a79674f235e73f176aedaa1217b95044 [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;
Mathias Agopian518ec112011-05-13 16:21:08 -0700215 }
216
Michael Lentine54466bc2015-01-27 09:01:03 -0800217 {
218 Mutex::Autolock _rf(refLock);
219 eglIsInitialized = true;
220 refCond.broadcast();
Mathias Agopian518ec112011-05-13 16:21:08 -0700221 }
222
Mathias Agopian7773c432012-02-13 20:06:08 -0800223 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700224}
225
226EGLBoolean egl_display_t::terminate() {
227
Michael Lentine54466bc2015-01-27 09:01:03 -0800228 {
229 Mutex::Autolock _rl(refLock);
230 if (refs == 0) {
231 /*
232 * From the EGL spec (3.2):
233 * "Termination of a display that has already been terminated,
234 * (...), is allowed, but the only effect of such a call is
235 * to return EGL_TRUE (...)
236 */
237 return EGL_TRUE;
238 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700239
Michael Lentine54466bc2015-01-27 09:01:03 -0800240 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700241 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800242 if (refs > 0) {
243 return EGL_TRUE;
244 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700245 }
246
247 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800248
249 {
250 Mutex::Autolock _l(lock);
251
252 egl_connection_t* const cnx = &gEGLImpl;
253 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
254 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
255 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
256 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
257 }
258 // REVISIT: it's unclear what to do if eglTerminate() fails
259 disp.state = egl_display_t::TERMINATED;
260 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700261 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800262
Michael Lentine54466bc2015-01-27 09:01:03 -0800263 // Reset the extension string since it will be regenerated if we get
264 // reinitialized.
265 mExtensionString.setTo("");
266
267 // 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();
Dan Alberteacd31f2016-02-02 15:08:34 -0800271 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Michael Lentine54466bc2015-01-27 09:01:03 -0800272 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
Michael Lentine54466bc2015-01-27 09:01:03 -0800281 {
282 Mutex::Autolock _rl(refLock);
283 eglIsInitialized = false;
284 refCond.broadcast();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700285 }
286
Mathias Agopian518ec112011-05-13 16:21:08 -0700287 return res;
288}
289
Mathias Agopianfb87e542012-01-30 18:20:52 -0800290void egl_display_t::loseCurrent(egl_context_t * cur_c)
291{
292 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800293 egl_display_t* display = cur_c->getDisplay();
294 if (display) {
295 display->loseCurrentImpl(cur_c);
296 }
297 }
298}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800299
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800300void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
301{
302 // by construction, these are either 0 or valid (possibly terminated)
303 // it should be impossible for these to be invalid
304 ContextRef _cur_c(cur_c);
305 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
306 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800307
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800308 { // scope for the lock
309 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800310 cur_c->onLooseCurrent();
311
Mathias Agopianfb87e542012-01-30 18:20:52 -0800312 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800313
314 // This cannot be called with the lock held because it might end-up
315 // calling back into EGL (in particular when a surface is destroyed
316 // it calls ANativeWindow::disconnect
317 _cur_c.release();
318 _cur_r.release();
319 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800320}
321
322EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700323 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800324 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
325{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800326 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800327
328 // by construction, these are either 0 or valid (possibly terminated)
329 // it should be impossible for these to be invalid
330 ContextRef _cur_c(cur_c);
331 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
332 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
333
334 { // scope for the lock
335 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800336 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800337 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800338 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800339 if (result == EGL_TRUE) {
340 c->onMakeCurrent(draw, read);
341 }
342 } else {
343 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800344 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800345 if (result == EGL_TRUE) {
346 cur_c->onLooseCurrent();
347 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800348 }
349 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800350
351 if (result == EGL_TRUE) {
352 // This cannot be called with the lock held because it might end-up
353 // calling back into EGL (in particular when a surface is destroyed
354 // it calls ANativeWindow::disconnect
355 _cur_c.release();
356 _cur_r.release();
357 _cur_d.release();
358 }
359
Mathias Agopianfb87e542012-01-30 18:20:52 -0800360 return result;
361}
Mathias Agopian518ec112011-05-13 16:21:08 -0700362
Jesse Hallc2e41222013-08-08 13:40:22 -0700363bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
364 if (!nameLen) {
365 nameLen = strlen(name);
366 }
367 return findExtension(mExtensionString.string(), name, nameLen);
368}
369
Jesse Halla0fef1c2012-04-17 12:02:26 -0700370// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700371}; // namespace android
372// ----------------------------------------------------------------------------