blob: a3502f29081f54042b0e0bde9516d90d5eedc54c [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 Agopian311b4792017-02-28 15:00:49 -080020#include "egl_display.h"
Mathias Agopian4b9511c2011-11-13 23:52:47 -080021
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_object.h"
26#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080027#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070028#include "Loader.h"
Mathias Agopian7db993a2012-03-25 00:49:46 -070029#include <cutils/properties.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080030
Mathias Agopian518ec112011-05-13 16:21:08 -070031// ----------------------------------------------------------------------------
32namespace android {
33// ----------------------------------------------------------------------------
34
Mathias Agopian4b9511c2011-11-13 23:52:47 -080035static char const * const sVendorString = "Android";
36static char const * const sVersionString = "1.4 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070037static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080038
Jesse Hall21558da2013-08-06 15:31:22 -070039extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070040extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080041
Mathias Agopian518ec112011-05-13 16:21:08 -070042extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
43
Mathias Agopian518ec112011-05-13 16:21:08 -070044// ----------------------------------------------------------------------------
45
Jesse Hallc2e41222013-08-08 13:40:22 -070046static bool findExtension(const char* exts, const char* name, size_t nameLen) {
47 if (exts) {
Kalle Raita7804aa22016-04-18 16:03:37 -070048 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
49 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
50 return true;
51 }
Jesse Hallc2e41222013-08-08 13:40:22 -070052 }
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() :
Michael Lentine54466bc2015-01-27 09:01:03 -080060 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
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;
Jesse Halld6e99462016-09-28 11:26:57 -070070 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
71 return nullptr;
72 }
73 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -070074}
75
76void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080077 std::lock_guard<std::mutex> _l(lock);
78 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -070079}
80
Mathias Agopian5b287a62011-05-16 18:58:55 -070081void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080082 std::lock_guard<std::mutex> _l(lock);
83 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -070084}
85
Mathias Agopianf0480de2011-11-13 20:50:07 -080086bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -080087 std::lock_guard<std::mutex> _l(lock);
88 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -080089 if (object->getDisplay() == this) {
90 object->incRef();
91 return true;
92 }
Mathias Agopian518ec112011-05-13 16:21:08 -070093 }
94 return false;
95}
96
Mathias Agopian518ec112011-05-13 16:21:08 -070097EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
98 if (uintptr_t(disp) >= NUM_DISPLAYS)
99 return NULL;
100
101 return sDisplay[uintptr_t(disp)].getDisplay(disp);
102}
103
104EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
105
Mathias Agopian65421432017-03-08 11:49:05 -0800106 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800107 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700108
109 // get our driver loader
110 Loader& loader(Loader::getInstance());
111
Mathias Agopianada798b2012-02-13 17:09:30 -0800112 egl_connection_t* const cnx = &gEGLImpl;
113 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
114 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
115 disp.dpy = dpy;
116 if (dpy == EGL_NO_DISPLAY) {
117 loader.close(cnx->dso);
118 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700119 }
120 }
121
122 return EGLDisplay(uintptr_t(display) + 1U);
123}
124
125EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
126
Mathias Agopian65421432017-03-08 11:49:05 -0800127 { // scope for refLock
128 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800129 refs++;
130 if (refs > 1) {
131 if (major != NULL)
132 *major = VERSION_MAJOR;
133 if (minor != NULL)
134 *minor = VERSION_MINOR;
Mathias Agopian65421432017-03-08 11:49:05 -0800135 while(!eglIsInitialized) {
136 refCond.wait(_l);
137 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800138 return EGL_TRUE;
139 }
Mathias Agopian65421432017-03-08 11:49:05 -0800140 while(eglIsInitialized) {
141 refCond.wait(_l);
142 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800143 }
144
Mathias Agopian65421432017-03-08 11:49:05 -0800145 { // scope for lock
146 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800147
Michael Lentine54466bc2015-01-27 09:01:03 -0800148 setGLHooksThreadSpecific(&gHooksNoContext);
149
150 // initialize each EGL and
151 // build our own extension string first, based on the extension we know
152 // and the extension supported by our client implementation
153
154 egl_connection_t* const cnx = &gEGLImpl;
155 cnx->major = -1;
156 cnx->minor = -1;
157 if (cnx->dso) {
158 EGLDisplay idpy = disp.dpy;
159 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
160 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
161 // idpy, cnx->major, cnx->minor, cnx);
162
163 // display is now initialized
164 disp.state = egl_display_t::INITIALIZED;
165
166 // get the query-strings for this display for each implementation
167 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
168 EGL_VENDOR);
169 disp.queryString.version = cnx->egl.eglQueryString(idpy,
170 EGL_VERSION);
171 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
172 EGL_EXTENSIONS);
173 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
174 EGL_CLIENT_APIS);
175
176 } else {
177 ALOGW("eglInitialize(%p) failed (%s)", idpy,
178 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
179 }
180 }
181
182 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800183 mVendorString = sVendorString;
184 mVersionString = sVersionString;
185 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800186
Mathias Agopian65421432017-03-08 11:49:05 -0800187 mExtensionString = gBuiltinExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800188 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800189 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400190 // length of the extension name
191 size_t len = strcspn(start, " ");
192 if (len) {
193 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800194 const std::string ext(start, len);
195 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400196 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
Mathias Agopian65421432017-03-08 11:49:05 -0800223 { // scope for refLock
224 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800225 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800226 refCond.notify_all();
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
Mathias Agopian65421432017-03-08 11:49:05 -0800234 { // scope for refLock
235 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800236 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
Mathias Agopian65421432017-03-08 11:49:05 -0800255 { // scope for lock
256 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800257
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.
Mathias Agopian65421432017-03-08 11:49:05 -0800271 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800272
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);
Mathias Agopian65421432017-03-08 11:49:05 -0800278 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800279 o->destroy();
280 }
281
282 // this marks all object handles are "terminated"
283 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700284 }
285
Mathias Agopian65421432017-03-08 11:49:05 -0800286 { // scope for refLock
287 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800288 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800289 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700290 }
291
Mathias Agopian518ec112011-05-13 16:21:08 -0700292 return res;
293}
294
Mathias Agopianfb87e542012-01-30 18:20:52 -0800295void egl_display_t::loseCurrent(egl_context_t * cur_c)
296{
297 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800298 egl_display_t* display = cur_c->getDisplay();
299 if (display) {
300 display->loseCurrentImpl(cur_c);
301 }
302 }
303}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800304
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800305void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
306{
307 // by construction, these are either 0 or valid (possibly terminated)
308 // it should be impossible for these to be invalid
309 ContextRef _cur_c(cur_c);
310 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
311 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800312
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800313 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800314 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800315 cur_c->onLooseCurrent();
316
Mathias Agopianfb87e542012-01-30 18:20:52 -0800317 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800318
319 // This cannot be called with the lock held because it might end-up
320 // calling back into EGL (in particular when a surface is destroyed
321 // it calls ANativeWindow::disconnect
322 _cur_c.release();
323 _cur_r.release();
324 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800325}
326
327EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700328 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800329 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
330{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800331 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800332
333 // by construction, these are either 0 or valid (possibly terminated)
334 // it should be impossible for these to be invalid
335 ContextRef _cur_c(cur_c);
336 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
337 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
338
339 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800340 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800341 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800342 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800343 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800344 if (result == EGL_TRUE) {
345 c->onMakeCurrent(draw, read);
346 }
347 } else {
348 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800349 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800350 if (result == EGL_TRUE) {
351 cur_c->onLooseCurrent();
352 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800353 }
354 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355
356 if (result == EGL_TRUE) {
357 // This cannot be called with the lock held because it might end-up
358 // calling back into EGL (in particular when a surface is destroyed
359 // it calls ANativeWindow::disconnect
360 _cur_c.release();
361 _cur_r.release();
362 _cur_d.release();
363 }
364
Mathias Agopianfb87e542012-01-30 18:20:52 -0800365 return result;
366}
Mathias Agopian518ec112011-05-13 16:21:08 -0700367
Jesse Hallc2e41222013-08-08 13:40:22 -0700368bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
369 if (!nameLen) {
370 nameLen = strlen(name);
371 }
Mathias Agopian65421432017-03-08 11:49:05 -0800372 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700373}
374
Jesse Halla0fef1c2012-04-17 12:02:26 -0700375// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700376}; // namespace android
377// ----------------------------------------------------------------------------