blob: 0f36614ac13132116a18a53276a0bb690a672064 [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 Goeltzenleuchtere5d6f992017-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) {
Ivan Lozano7025b542017-12-06 14:19:44 -080082 if (uintptr_t(dpy) == 0) {
83 return nullptr;
84 }
85
Mathias Agopian518ec112011-05-13 16:21:08 -070086 uintptr_t index = uintptr_t(dpy)-1U;
Jesse Halld6e99462016-09-28 11:26:57 -070087 if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) {
88 return nullptr;
89 }
90 return &sDisplay[index];
Mathias Agopian518ec112011-05-13 16:21:08 -070091}
92
93void egl_display_t::addObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080094 std::lock_guard<std::mutex> _l(lock);
95 objects.insert(object);
Mathias Agopian518ec112011-05-13 16:21:08 -070096}
97
Mathias Agopian5b287a62011-05-16 18:58:55 -070098void egl_display_t::removeObject(egl_object_t* object) {
Mathias Agopian65421432017-03-08 11:49:05 -080099 std::lock_guard<std::mutex> _l(lock);
100 objects.erase(object);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700101}
102
Mathias Agopianf0480de2011-11-13 20:50:07 -0800103bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian65421432017-03-08 11:49:05 -0800104 std::lock_guard<std::mutex> _l(lock);
105 if (objects.find(object) != objects.end()) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800106 if (object->getDisplay() == this) {
107 object->incRef();
108 return true;
109 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700110 }
111 return false;
112}
113
Mathias Agopian518ec112011-05-13 16:21:08 -0700114EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
115 if (uintptr_t(disp) >= NUM_DISPLAYS)
116 return NULL;
117
118 return sDisplay[uintptr_t(disp)].getDisplay(disp);
119}
120
121EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
122
Mathias Agopian65421432017-03-08 11:49:05 -0800123 std::lock_guard<std::mutex> _l(lock);
Jesse Hall1508ae62017-01-19 17:43:26 -0800124 ATRACE_CALL();
Mathias Agopian518ec112011-05-13 16:21:08 -0700125
126 // get our driver loader
127 Loader& loader(Loader::getInstance());
128
Mathias Agopianada798b2012-02-13 17:09:30 -0800129 egl_connection_t* const cnx = &gEGLImpl;
130 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
131 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
132 disp.dpy = dpy;
133 if (dpy == EGL_NO_DISPLAY) {
134 loader.close(cnx->dso);
135 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700136 }
137 }
138
139 return EGLDisplay(uintptr_t(display) + 1U);
140}
141
142EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
143
Mathias Agopian65421432017-03-08 11:49:05 -0800144 { // scope for refLock
145 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800146 refs++;
147 if (refs > 1) {
148 if (major != NULL)
149 *major = VERSION_MAJOR;
150 if (minor != NULL)
151 *minor = VERSION_MINOR;
Mathias Agopian65421432017-03-08 11:49:05 -0800152 while(!eglIsInitialized) {
153 refCond.wait(_l);
154 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800155 return EGL_TRUE;
156 }
Mathias Agopian65421432017-03-08 11:49:05 -0800157 while(eglIsInitialized) {
158 refCond.wait(_l);
159 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800160 }
161
Mathias Agopian65421432017-03-08 11:49:05 -0800162 { // scope for lock
163 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800164
Michael Lentine54466bc2015-01-27 09:01:03 -0800165 setGLHooksThreadSpecific(&gHooksNoContext);
166
167 // initialize each EGL and
168 // build our own extension string first, based on the extension we know
169 // and the extension supported by our client implementation
170
171 egl_connection_t* const cnx = &gEGLImpl;
172 cnx->major = -1;
173 cnx->minor = -1;
174 if (cnx->dso) {
175 EGLDisplay idpy = disp.dpy;
176 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
177 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
178 // idpy, cnx->major, cnx->minor, cnx);
179
180 // display is now initialized
181 disp.state = egl_display_t::INITIALIZED;
182
183 // get the query-strings for this display for each implementation
184 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
185 EGL_VENDOR);
186 disp.queryString.version = cnx->egl.eglQueryString(idpy,
187 EGL_VERSION);
188 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
189 EGL_EXTENSIONS);
190 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
191 EGL_CLIENT_APIS);
192
193 } else {
194 ALOGW("eglInitialize(%p) failed (%s)", idpy,
195 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
196 }
197 }
198
199 // the query strings are per-display
Mathias Agopian65421432017-03-08 11:49:05 -0800200 mVendorString = sVendorString;
201 mVersionString = sVersionString;
202 mClientApiString = sClientApiString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800203
Mathias Agopian65421432017-03-08 11:49:05 -0800204 mExtensionString = gBuiltinExtensionString;
Courtney Goeltzenleuchtere5d6f992017-07-07 14:55:40 -0600205
206 bool wideColorBoardConfig =
207 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(
208 false);
209
210 // Add wide-color extensions if device can support wide-color
211 if (wideColorBoardConfig) {
212 mExtensionString.append(
213 "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear "
214 "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 ");
215 }
216
Courtney Goeltzenleuchter12ffe092017-11-16 14:27:48 -0700217 bool hasHdrBoardConfig =
218 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false);
219
220 if (hasHdrBoardConfig) {
221 // hasHDRBoardConfig indicates the system is capable of supporting HDR content.
222 // Typically that means there is an HDR capable display attached, but could be
223 // support for attaching an HDR display. In either case, advertise support for
224 // HDR color spaces.
225 mExtensionString.append(
226 "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq ");
227 }
228
229 // Always advertise HDR metadata extensions since it's okay for an application
230 // to specify such information even though it may not be used by the system.
231 mExtensionString.append(
232 "EGL_EXT_surface_SMPTE2086_metadata EGL_EXT_surface_CTA861_3_metadata ");
233
Michael Lentine54466bc2015-01-27 09:01:03 -0800234 char const* start = gExtensionString;
Michael Lentine54466bc2015-01-27 09:01:03 -0800235 do {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400236 // length of the extension name
237 size_t len = strcspn(start, " ");
238 if (len) {
239 // NOTE: we could avoid the copy if we had strnstr.
Mathias Agopian65421432017-03-08 11:49:05 -0800240 const std::string ext(start, len);
241 if (findExtension(disp.queryString.extensions, ext.c_str(), len)) {
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400242 mExtensionString.append(ext + " ");
Michael Lentine54466bc2015-01-27 09:01:03 -0800243 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400244 // advance to the next extension name, skipping the space.
245 start += len;
246 start += (*start == ' ') ? 1 : 0;
Michael Lentine54466bc2015-01-27 09:01:03 -0800247 }
Nicolas Capensecc0c9a2015-10-30 12:55:21 -0400248 } while (*start != '\0');
Michael Lentine54466bc2015-01-27 09:01:03 -0800249
250 egl_cache_t::get()->initialize(this);
251
252 char value[PROPERTY_VALUE_MAX];
253 property_get("debug.egl.finish", value, "0");
254 if (atoi(value)) {
255 finishOnSwap = true;
256 }
257
258 property_get("debug.egl.traceGpuCompletion", value, "0");
259 if (atoi(value)) {
260 traceGpuCompletion = true;
261 }
262
Mathias Agopian518ec112011-05-13 16:21:08 -0700263 if (major != NULL)
264 *major = VERSION_MAJOR;
265 if (minor != NULL)
266 *minor = VERSION_MINOR;
Mathias Agopian518ec112011-05-13 16:21:08 -0700267 }
268
Mathias Agopian65421432017-03-08 11:49:05 -0800269 { // scope for refLock
270 std::unique_lock<std::mutex> _l(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800271 eglIsInitialized = true;
Mathias Agopian65421432017-03-08 11:49:05 -0800272 refCond.notify_all();
Mathias Agopian518ec112011-05-13 16:21:08 -0700273 }
274
Mathias Agopian7773c432012-02-13 20:06:08 -0800275 return EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700276}
277
278EGLBoolean egl_display_t::terminate() {
279
Mathias Agopian65421432017-03-08 11:49:05 -0800280 { // scope for refLock
281 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800282 if (refs == 0) {
283 /*
284 * From the EGL spec (3.2):
285 * "Termination of a display that has already been terminated,
286 * (...), is allowed, but the only effect of such a call is
287 * to return EGL_TRUE (...)
288 */
289 return EGL_TRUE;
290 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700291
Michael Lentine54466bc2015-01-27 09:01:03 -0800292 // this is specific to Android, display termination is ref-counted.
Mathias Agopian518ec112011-05-13 16:21:08 -0700293 refs--;
Michael Lentine54466bc2015-01-27 09:01:03 -0800294 if (refs > 0) {
295 return EGL_TRUE;
296 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700297 }
298
299 EGLBoolean res = EGL_FALSE;
Michael Lentine54466bc2015-01-27 09:01:03 -0800300
Mathias Agopian65421432017-03-08 11:49:05 -0800301 { // scope for lock
302 std::lock_guard<std::mutex> _l(lock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800303
304 egl_connection_t* const cnx = &gEGLImpl;
305 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
306 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
307 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
308 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
309 }
310 // REVISIT: it's unclear what to do if eglTerminate() fails
311 disp.state = egl_display_t::TERMINATED;
312 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700313 }
Michael Lentine54466bc2015-01-27 09:01:03 -0800314
Michael Lentine54466bc2015-01-27 09:01:03 -0800315 // Reset the extension string since it will be regenerated if we get
316 // reinitialized.
Mathias Agopian65421432017-03-08 11:49:05 -0800317 mExtensionString.clear();
Michael Lentine54466bc2015-01-27 09:01:03 -0800318
319 // Mark all objects remaining in the list as terminated, unless
320 // there are no reference to them, it which case, we're free to
321 // delete them.
322 size_t count = objects.size();
Dan Alberteacd31f2016-02-02 15:08:34 -0800323 ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count);
Mathias Agopian65421432017-03-08 11:49:05 -0800324 for (auto o : objects) {
Michael Lentine54466bc2015-01-27 09:01:03 -0800325 o->destroy();
326 }
327
328 // this marks all object handles are "terminated"
329 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700330 }
331
Mathias Agopian65421432017-03-08 11:49:05 -0800332 { // scope for refLock
333 std::unique_lock<std::mutex> _rl(refLock);
Michael Lentine54466bc2015-01-27 09:01:03 -0800334 eglIsInitialized = false;
Mathias Agopian65421432017-03-08 11:49:05 -0800335 refCond.notify_all();
Mathias Agopian5b287a62011-05-16 18:58:55 -0700336 }
337
Mathias Agopian518ec112011-05-13 16:21:08 -0700338 return res;
339}
340
Mathias Agopianfb87e542012-01-30 18:20:52 -0800341void egl_display_t::loseCurrent(egl_context_t * cur_c)
342{
343 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800344 egl_display_t* display = cur_c->getDisplay();
345 if (display) {
346 display->loseCurrentImpl(cur_c);
347 }
348 }
349}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800350
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800351void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
352{
353 // by construction, these are either 0 or valid (possibly terminated)
354 // it should be impossible for these to be invalid
355 ContextRef _cur_c(cur_c);
356 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
357 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800358
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800359 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800360 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800361 cur_c->onLooseCurrent();
362
Mathias Agopianfb87e542012-01-30 18:20:52 -0800363 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800364
365 // This cannot be called with the lock held because it might end-up
366 // calling back into EGL (in particular when a surface is destroyed
367 // it calls ANativeWindow::disconnect
368 _cur_c.release();
369 _cur_r.release();
370 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800371}
372
373EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700374 EGLSurface draw, EGLSurface read, EGLContext /*ctx*/,
Mathias Agopianfb87e542012-01-30 18:20:52 -0800375 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
376{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800377 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800378
379 // by construction, these are either 0 or valid (possibly terminated)
380 // it should be impossible for these to be invalid
381 ContextRef _cur_c(cur_c);
382 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
383 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
384
385 { // scope for the lock
Mathias Agopian65421432017-03-08 11:49:05 -0800386 std::lock_guard<std::mutex> _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800387 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800388 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800389 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800390 if (result == EGL_TRUE) {
391 c->onMakeCurrent(draw, read);
392 }
393 } else {
394 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800395 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800396 if (result == EGL_TRUE) {
397 cur_c->onLooseCurrent();
398 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800399 }
400 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800401
402 if (result == EGL_TRUE) {
403 // This cannot be called with the lock held because it might end-up
404 // calling back into EGL (in particular when a surface is destroyed
405 // it calls ANativeWindow::disconnect
406 _cur_c.release();
407 _cur_r.release();
408 _cur_d.release();
409 }
410
Mathias Agopianfb87e542012-01-30 18:20:52 -0800411 return result;
412}
Mathias Agopian518ec112011-05-13 16:21:08 -0700413
Jesse Hallc2e41222013-08-08 13:40:22 -0700414bool egl_display_t::haveExtension(const char* name, size_t nameLen) const {
415 if (!nameLen) {
416 nameLen = strlen(name);
417 }
Mathias Agopian65421432017-03-08 11:49:05 -0800418 return findExtension(mExtensionString.c_str(), name, nameLen);
Jesse Hallc2e41222013-08-08 13:40:22 -0700419}
420
Jesse Halla0fef1c2012-04-17 12:02:26 -0700421// ----------------------------------------------------------------------------
Mathias Agopian518ec112011-05-13 16:21:08 -0700422}; // namespace android
423// ----------------------------------------------------------------------------