blob: 4e5833ab125ea22f40412bf36a4abada077755bb [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
Mathias Agopianb7f9a242017-03-08 22:29:31 -080024#include <private/EGL/display.h>
25
Jamie Gennisaca51c02011-11-03 17:42:43 -070026#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070027#include "egl_object.h"
28#include "egl_tls.h"
Mathias Agopian65421432017-03-08 11:49:05 -080029#include "egl_trace.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070030#include "Loader.h"
Mathias Agopian7db993a2012-03-25 00:49:46 -070031#include <cutils/properties.h>
Mathias Agopian311b4792017-02-28 15:00:49 -080032
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -060033#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
34#include <configstore/Utils.h>
35
36using namespace android::hardware::configstore;
37using namespace android::hardware::configstore::V1_0;
38
Mathias Agopian518ec112011-05-13 16:21:08 -070039// ----------------------------------------------------------------------------
40namespace android {
41// ----------------------------------------------------------------------------
42
Mathias Agopian4b9511c2011-11-13 23:52:47 -080043static char const * const sVendorString = "Android";
44static char const * const sVersionString = "1.4 Android META-EGL";
Mathias Agopiancc2b1562012-05-21 14:01:37 -070045static char const * const sClientApiString = "OpenGL_ES";
Mathias Agopian4b9511c2011-11-13 23:52:47 -080046
Jesse Hall21558da2013-08-06 15:31:22 -070047extern char const * const gBuiltinExtensionString;
Mathias Agopiane9b3dfb2013-03-27 14:30:19 -070048extern char const * const gExtensionString;
Mathias Agopian4b9511c2011-11-13 23:52:47 -080049
Mathias Agopian518ec112011-05-13 16:21:08 -070050extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
51
Mathias Agopian518ec112011-05-13 16:21:08 -070052// ----------------------------------------------------------------------------
53
Jesse Hallc2e41222013-08-08 13:40:22 -070054static bool findExtension(const char* exts, const char* name, size_t nameLen) {
55 if (exts) {
Kalle Raita7804aa22016-04-18 16:03:37 -070056 for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) {
57 if (match[nameLen] == '\0' || match[nameLen] == ' ') {
58 return true;
59 }
Jesse Hallc2e41222013-08-08 13:40:22 -070060 }
61 }
62 return false;
63}
64
Mathias Agopianb7f9a242017-03-08 22:29:31 -080065int egl_get_init_count(EGLDisplay dpy) {
66 egl_display_t* eglDisplay = egl_display_t::get(dpy);
67 return eglDisplay ? eglDisplay->getRefsCount() : 0;
68}
69
Mathias Agopian518ec112011-05-13 16:21:08 -070070egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
71
72egl_display_t::egl_display_t() :
Michael Lentine54466bc2015-01-27 09:01:03 -080073 magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) {
Mathias Agopian518ec112011-05-13 16:21:08 -070074}
75
76egl_display_t::~egl_display_t() {
77 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080078 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070079}
80
81egl_display_t* egl_display_t::get(EGLDisplay dpy) {
82 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -070083 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
84 return nullptr;
85 }
86 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -070087}
88
89void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080090 std::lock_guard<std::mutex> _l(lock);
91 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -070092}
93
Mathias Agopian5b287a62011-05-16 18:58:55 -070094void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080095 std::lock_guard<std::mutex> _l(lock);
96 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -070097}
98
Mathias Agopianf0480de2011-11-13 20:50:07 -080099bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800100 std::lock_guard<std::mutex> _l(lock);
101 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800102 if (object->getDisplay() == this) {
103 object->incRef();
104 return true;
105 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700106 }
107 return false;
108}
109
Mathias Agopian518ec112011-05-13 16:21:08 -0700110EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
111 if (uintptr_t(disp) >= NUM_DISPLAYS)
112 return NULL;
113
114 return sDisplay[uintptr_t(disp)].getDisplay(disp);
115}
116
117EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
118
Mathias Agopian65421432017-03-08 11:49:05 -0800119 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800120 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700121
122 // get our driver loader
123 Loader& loader(Loader::getInstance());
124
Mathias Agopianada798b2012-02-13 17:09:30 -0800125 egl_connection_t* const cnx = &gEGLImpl;
126 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
127 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
128 disp.dpy = dpy;
129 if (dpy == EGL_NO_DISPLAY) {
130 loader.close(cnx->dso);
131 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700132 }
133 }
134
135 return EGLDisplay(uintptr_t(display) + 1U);
136}
137
138EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
139
Mathias Agopian65421432017-03-08 11:49:05 -0800140 { // scope for refLock
141 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800142 refs++;
143 if (refs > 1) {
144 if (major != NULL)
145 *major = VERSION_MAJOR;
146 if (minor != NULL)
147 *minor = VERSION_MINOR;
Mathias Agopian65421432017-03-08 11:49:05 -0800148 while(!eglIsInitialized) {
149 refCond.wait(_l);
150 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800151 return EGL_TRUE;
152 }
Mathias Agopian65421432017-03-08 11:49:05 -0800153 while(eglIsInitialized) {
154 refCond.wait(_l);
155 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800156 }
157
Mathias Agopian65421432017-03-08 11:49:05 -0800158 { // scope for lock
159 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800160
Michael Lentine54466bc2015-01-27 09:01:03 -0800161 setGLHooksThreadSpecific(&gHooksNoContext);
162
163 // initialize each EGL and
164 // build our own extension string first, based on the extension we know
165 // and the extension supported by our client implementation
166
167 egl_connection_t* const cnx = &gEGLImpl;
168 cnx->major = -1;
169 cnx->minor = -1;
170 if (cnx->dso) {
171 EGLDisplay idpy = disp.dpy;
172 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
173 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
174 // idpy, cnx->major, cnx->minor, cnx);
175
176 // display is now initialized
177 disp.state = egl_display_t::INITIALIZED;
178
179 // get the query-strings for this display for each implementation
180 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
181 EGL_VENDOR);
182 disp.queryString.version = cnx->egl.eglQueryString(idpy,
183 EGL_VERSION);
184 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
185 EGL_EXTENSIONS);
186 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
187 EGL_CLIENT_APIS);
188
189 } else {
190 ALOGW("eglInitialize(%p) failed (%s)", idpy,
191 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
192 }
193 }
194
195 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800196 mVendorString = sVendorString;
197 mVersionString = sVersionString;
198 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800199
Mathias Agopian65421432017-03-08 11:49:05 -0800200 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchter1b717022017-07-07 14:55:40 -0600201
202 bool wideColorBoardConfig =
203 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
204 false);
205
206 // Add wide-color extensions if device can support wide-color
207 if (wideColorBoardConfig) {
208 mExtensionString.append(
209 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
210 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
211 }
212
Michael Lentine54466bc2015-01-27 09:01:03 -0800213 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800214 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400215 // length of the extension name
216 size_t len = strcspn(start, " ");
217 if (len) {
218 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800219 const std::string ext(start, len);
220 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400221 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800222 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400223 // advance to the next extension name, skipping the space.
224 start += len;
225 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800226 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400227 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800228
229 egl_cache_t::get()->initialize(this);
230
231 char value[PROPERTY_VALUE_MAX];
232 property_get("debug.egl.finish", value, "0");
233 if (atoi(value)) {
234 finishOnSwap = true;
235 }
236
237 property_get("debug.egl.traceGpuCompletion", value, "0");
238 if (atoi(value)) {
239 traceGpuCompletion = true;
240 }
241
Mathias Agopian518ec112011-05-13 16:21:08 -0700242 if (major != NULL)
243 *major = VERSION_MAJOR;
244 if (minor != NULL)
245 *minor = VERSION_MINOR;
Mathias Agopian518ec112011-05-13 16:21:08 -0700246 }
247
Mathias Agopian65421432017-03-08 11:49:05 -0800248 { // scope for refLock
249 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800250 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800251 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700252 }
253
Mathias Agopian7773c432012-02-13 20:06:08 -0800254 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700255}
256
257EGLBoolean egl_display_t::terminate() {
258
Mathias Agopian65421432017-03-08 11:49:05 -0800259 { // scope for refLock
260 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800261 if (refs == 0) {
262 /*
263 * From the EGL spec (3.2):
264 * "Termination of a display that has already been terminated,
265 * (...), is allowed, but the only effect of such a call is
266 * to return EGL_TRUE (...)
267 */
268 return EGL_TRUE;
269 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700270
Michael Lentine54466bc2015-01-27 09:01:03 -0800271 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700272 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800273 if (refs > 0) {
274 return EGL_TRUE;
275 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700276 }
277
278 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800279
Mathias Agopian65421432017-03-08 11:49:05 -0800280 { // scope for lock
281 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800282
283 egl_connection_t* const cnx = &gEGLImpl;
284 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
285 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
286 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
287 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
288 }
289 // REVISIT: it's unclear what to do if eglTerminate() fails
290 disp.state = egl_display_t::TERMINATED;
291 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700292 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800293
Michael Lentine54466bc2015-01-27 09:01:03 -0800294 // Reset the extension string since it will be regenerated if we get
295 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800296 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800297
298 // Mark all objects remaining in the list as terminated, unless
299 // there are no reference to them, it which case, we're free to
300 // delete them.
301 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800302 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800303 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800304 o->destroy();
305 }
306
307 // this marks all object handles are "terminated"
308 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700309 }
310
Mathias Agopian65421432017-03-08 11:49:05 -0800311 { // scope for refLock
312 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800313 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800314 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700315 }
316
Mathias Agopian518ec112011-05-13 16:21:08 -0700317 return res;
318}
319
Mathias Agopianfb87e542012-01-30 18:20:52 -0800320void egl_display_t::loseCurrent(egl_context_t * cur_c)
321{
322 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800323 egl_display_t* display = cur_c->getDisplay();
324 if (display) {
325 display->loseCurrentImpl(cur_c);
326 }
327 }
328}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800329
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800330void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
331{
332 // by construction, these are either 0 or valid (possibly terminated)
333 // it should be impossible for these to be invalid
334 ContextRef _cur_c(cur_c);
335 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
336 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800337
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800338 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800339 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800340 cur_c->onLooseCurrent();
341
Mathias Agopianfb87e542012-01-30 18:20:52 -0800342 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800343
344 // This cannot be called with the lock held because it might end-up
345 // calling back into EGL (in particular when a surface is destroyed
346 // it calls ANativeWindow::disconnect
347 _cur_c.release();
348 _cur_r.release();
349 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800350}
351
352EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700353 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800354 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
355{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800356 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800357
358 // by construction, these are either 0 or valid (possibly terminated)
359 // it should be impossible for these to be invalid
360 ContextRef _cur_c(cur_c);
361 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
362 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
363
364 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800365 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800366 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800367 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800368 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800369 if (result == EGL_TRUE) {
370 c->onMakeCurrent(draw, read);
371 }
372 } else {
373 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800374 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800375 if (result == EGL_TRUE) {
376 cur_c->onLooseCurrent();
377 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800378 }
379 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800380
381 if (result == EGL_TRUE) {
382 // This cannot be called with the lock held because it might end-up
383 // calling back into EGL (in particular when a surface is destroyed
384 // it calls ANativeWindow::disconnect
385 _cur_c.release();
386 _cur_r.release();
387 _cur_d.release();
388 }
389
Mathias Agopianfb87e542012-01-30 18:20:52 -0800390 return result;
391}
Mathias Agopian518ec112011-05-13 16:21:08 -0700392
Jesse Hallc2e41222013-08-08 13:40:22 -0700393bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
394 if (!nameLen) {
395 nameLen = strlen(name);
396 }
Mathias Agopian65421432017-03-08 11:49:05 -0800397 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700398}
399
Jesse Halla0fef1c2012-04-17 12:02:26 -0700400// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700401}; // namespace android
402// ----------------------------------------------------------------------------