blob: d7df40c0868f948a3c2a6e26a217ec2107966ca7 [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
Jesse Hall1508ae62017-01-19 17:43:26 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Jesse Hallb29e5e82012-04-04 16:53:42 -070019
Mathias Agopian4b9511c2011-11-13 23:52:47 -080020#include <string.h>
21
Mathias Agopian39c24a22013-04-04 23:17:56 -070022#include "../egl_impl.h"
23
Jamie Gennisaca51c02011-11-03 17:42:43 -070024#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070025#include "egl_display.h"
26#include "egl_object.h"
27#include "egl_tls.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070028#include "Loader.h"
Mathias Agopian7db993a2012-03-25 00:49:46 -070029#include <cutils/properties.h>
Jesse Hall1508ae62017-01-19 17:43:26 -080030#include <utils/Trace.h>
Mathias Agopian518ec112011-05-13 16:21:08 -070031
32// ----------------------------------------------------------------------------
33namespace android {
34// ----------------------------------------------------------------------------
35
Mathias Agopian4b9511c2011-11-13 23:52:47 -080036static char const * const sVendorString = "Android";
37static char const * const sVersionString = "1.4 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070038static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080039
Jesse Hall21558da2013-08-06 15:31:22 -070040extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070041extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080042
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) {
Kalle Raita7804aa22016-04-18 16:03:37 -070049 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
50 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
51 return true;
52 }
Jesse Hallc2e41222013-08-08 13:40:22 -070053 }
54 }
55 return false;
56}
57
Mathias Agopian518ec112011-05-13 16:21:08 -070058egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
59
60egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080061 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070062}
63
64egl_display_t::~egl_display_t() {
65 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080066 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070067}
68
69egl_display_t* egl_display_t::get(EGLDisplay dpy) {
70 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -070071 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
72 return nullptr;
73 }
74 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -070075}
76
77void egl_display_t::addObject(egl_object_t* object) {
78 Mutex::Autolock _l(lock);
79 objects.add(object);
80}
81
Mathias Agopian5b287a62011-05-16 18:58:55 -070082void egl_display_t::removeObject(egl_object_t* object) {
83 Mutex::Autolock _l(lock);
84 objects.remove(object);
85}
86
Mathias Agopianf0480de2011-11-13 20:50:07 -080087bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070088 Mutex::Autolock _l(lock);
89 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080090 if (object->getDisplay() == this) {
91 object->incRef();
92 return true;
93 }
Mathias Agopian518ec112011-05-13 16:21:08 -070094 }
95 return false;
96}
97
Mathias Agopian518ec112011-05-13 16:21:08 -070098EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
99 if (uintptr_t(disp) >= NUM_DISPLAYS)
100 return NULL;
101
102 return sDisplay[uintptr_t(disp)].getDisplay(disp);
103}
104
105EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
106
107 Mutex::Autolock _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800108 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700109
110 // get our driver loader
111 Loader& loader(Loader::getInstance());
112
Mathias Agopianada798b2012-02-13 17:09:30 -0800113 egl_connection_t* const cnx = &gEGLImpl;
114 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
115 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
116 disp.dpy = dpy;
117 if (dpy == EGL_NO_DISPLAY) {
118 loader.close(cnx->dso);
119 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700120 }
121 }
122
123 return EGLDisplay(uintptr_t(display) + 1U);
124}
125
126EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
127
Michael Lentine54466bc2015-01-27 09:01:03 -0800128 {
129 Mutex::Autolock _rf(refLock);
Mathias Agopian518ec112011-05-13 16:21:08 -0700130
Michael Lentine54466bc2015-01-27 09:01:03 -0800131 refs++;
132 if (refs > 1) {
133 if (major != NULL)
134 *major = VERSION_MAJOR;
135 if (minor != NULL)
136 *minor = VERSION_MINOR;
137 while(!eglIsInitialized) refCond.wait(refLock);
138 return EGL_TRUE;
139 }
140
141 while(eglIsInitialized) refCond.wait(refLock);
142 }
143
144 {
145 Mutex::Autolock _l(lock);
146
Michael Lentine54466bc2015-01-27 09:01:03 -0800147 setGLHooksThreadSpecific(&gHooksNoContext);
148
149 // initialize each EGL and
150 // build our own extension string first, based on the extension we know
151 // and the extension supported by our client implementation
152
153 egl_connection_t* const cnx = &gEGLImpl;
154 cnx->major = -1;
155 cnx->minor = -1;
156 if (cnx->dso) {
157 EGLDisplay idpy = disp.dpy;
158 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
159 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
160 // idpy, cnx->major, cnx->minor, cnx);
161
162 // display is now initialized
163 disp.state = egl_display_t::INITIALIZED;
164
165 // get the query-strings for this display for each implementation
166 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
167 EGL_VENDOR);
168 disp.queryString.version = cnx->egl.eglQueryString(idpy,
169 EGL_VERSION);
170 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
171 EGL_EXTENSIONS);
172 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
173 EGL_CLIENT_APIS);
174
175 } else {
176 ALOGW("eglInitialize(%p) failed (%s)", idpy,
177 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
178 }
179 }
180
181 // the query strings are per-display
182 mVendorString.setTo(sVendorString);
183 mVersionString.setTo(sVersionString);
184 mClientApiString.setTo(sClientApiString);
185
186 mExtensionString.setTo(gBuiltinExtensionString);
187 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800188 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400189 // length of the extension name
190 size_t len = strcspn(start, " ");
191 if (len) {
192 // NOTE: we could avoid the copy if we had strnstr.
193 const String8 ext(start, len);
194 if (findExtension(disp.queryString.extensions, ext.string(),
195 len)) {
196 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800197 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400198 // advance to the next extension name, skipping the space.
199 start += len;
200 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800201 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400202 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800203
204 egl_cache_t::get()->initialize(this);
205
206 char value[PROPERTY_VALUE_MAX];
207 property_get("debug.egl.finish", value, "0");
208 if (atoi(value)) {
209 finishOnSwap = true;
210 }
211
212 property_get("debug.egl.traceGpuCompletion", value, "0");
213 if (atoi(value)) {
214 traceGpuCompletion = true;
215 }
216
Mathias Agopian518ec112011-05-13 16:21:08 -0700217 if (major != NULL)
218 *major = VERSION_MAJOR;
219 if (minor != NULL)
220 *minor = VERSION_MINOR;
Mathias Agopian518ec112011-05-13 16:21:08 -0700221 }
222
Michael Lentine54466bc2015-01-27 09:01:03 -0800223 {
224 Mutex::Autolock _rf(refLock);
225 eglIsInitialized = true;
226 refCond.broadcast();
Mathias Agopian518ec112011-05-13 16:21:08 -0700227 }
228
Mathias Agopian7773c432012-02-13 20:06:08 -0800229 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700230}
231
232EGLBoolean egl_display_t::terminate() {
233
Michael Lentine54466bc2015-01-27 09:01:03 -0800234 {
235 Mutex::Autolock _rl(refLock);
236 if (refs == 0) {
237 /*
238 * From the EGL spec (3.2):
239 * "Termination of a display that has already been terminated,
240 * (...), is allowed, but the only effect of such a call is
241 * to return EGL_TRUE (...)
242 */
243 return EGL_TRUE;
244 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700245
Michael Lentine54466bc2015-01-27 09:01:03 -0800246 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700247 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800248 if (refs > 0) {
249 return EGL_TRUE;
250 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700251 }
252
253 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800254
255 {
256 Mutex::Autolock _l(lock);
257
258 egl_connection_t* const cnx = &gEGLImpl;
259 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
260 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
261 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
262 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
263 }
264 // REVISIT: it's unclear what to do if eglTerminate() fails
265 disp.state = egl_display_t::TERMINATED;
266 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700267 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800268
Michael Lentine54466bc2015-01-27 09:01:03 -0800269 // Reset the extension string since it will be regenerated if we get
270 // reinitialized.
271 mExtensionString.setTo("");
272
273 // Mark all objects remaining in the list as terminated, unless
274 // there are no reference to them, it which case, we're free to
275 // delete them.
276 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800277 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Michael Lentine54466bc2015-01-27 09:01:03 -0800278 for (size_t i=0 ; i<count ; i++) {
279 egl_object_t* o = objects.itemAt(i);
280 o->destroy();
281 }
282
283 // this marks all object handles are "terminated"
284 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700285 }
286
Michael Lentine54466bc2015-01-27 09:01:03 -0800287 {
288 Mutex::Autolock _rl(refLock);
289 eglIsInitialized = false;
290 refCond.broadcast();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700291 }
292
Mathias Agopian518ec112011-05-13 16:21:08 -0700293 return res;
294}
295
Mathias Agopianfb87e542012-01-30 18:20:52 -0800296void egl_display_t::loseCurrent(egl_context_t * cur_c)
297{
298 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800299 egl_display_t* display = cur_c->getDisplay();
300 if (display) {
301 display->loseCurrentImpl(cur_c);
302 }
303 }
304}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800305
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800306void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
307{
308 // by construction, these are either 0 or valid (possibly terminated)
309 // it should be impossible for these to be invalid
310 ContextRef _cur_c(cur_c);
311 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
312 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800313
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800314 { // scope for the lock
315 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800316 cur_c->onLooseCurrent();
317
Mathias Agopianfb87e542012-01-30 18:20:52 -0800318 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800319
320 // This cannot be called with the lock held because it might end-up
321 // calling back into EGL (in particular when a surface is destroyed
322 // it calls ANativeWindow::disconnect
323 _cur_c.release();
324 _cur_r.release();
325 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800326}
327
328EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700329 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800330 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
331{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800332 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800333
334 // by construction, these are either 0 or valid (possibly terminated)
335 // it should be impossible for these to be invalid
336 ContextRef _cur_c(cur_c);
337 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
338 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
339
340 { // scope for the lock
341 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800342 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800343 result = 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 c->onMakeCurrent(draw, read);
347 }
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();
353 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800354 }
355 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800356
357 if (result == EGL_TRUE) {
358 // This cannot be called with the lock held because it might end-up
359 // calling back into EGL (in particular when a surface is destroyed
360 // it calls ANativeWindow::disconnect
361 _cur_c.release();
362 _cur_r.release();
363 _cur_d.release();
364 }
365
Mathias Agopianfb87e542012-01-30 18:20:52 -0800366 return result;
367}
Mathias Agopian518ec112011-05-13 16:21:08 -0700368
Jesse Hallc2e41222013-08-08 13:40:22 -0700369bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
370 if (!nameLen) {
371 nameLen = strlen(name);
372 }
373 return findExtension(mExtensionString.string(), name, nameLen);
374}
375
Jesse Halla0fef1c2012-04-17 12:02:26 -0700376// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700377}; // namespace android
378// ----------------------------------------------------------------------------