Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 1 | /* |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 4 | ** 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 Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 7 | ** |
Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 9 | ** |
Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 10 | ** 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 Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 17 | #define __STDC_LIMIT_MACROS 1 |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 18 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 19 | |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 20 | #include "egl_display.h" |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 21 | |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 22 | #include "../egl_impl.h" |
| 23 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 24 | #include <EGL/eglext_angle.h> |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 25 | #include <private/EGL/display.h> |
| 26 | |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame] | 27 | #include <cutils/properties.h> |
| 28 | #include "Loader.h" |
| 29 | #include "egl_angle_platform.h" |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 30 | #include "egl_cache.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 31 | #include "egl_object.h" |
| 32 | #include "egl_tls.h" |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame] | 33 | |
| 34 | #include <android/dlext.h> |
| 35 | #include <dlfcn.h> |
| 36 | #include <graphicsenv/GraphicsEnv.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 37 | |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 38 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 39 | #include <configstore/Utils.h> |
| 40 | |
| 41 | using namespace android::hardware::configstore; |
| 42 | using namespace android::hardware::configstore::V1_0; |
| 43 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 44 | // ---------------------------------------------------------------------------- |
| 45 | namespace android { |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 48 | static char const * const sVendorString = "Android"; |
Courtney Goeltzenleuchter | 015ed67 | 2018-07-27 13:43:23 -0600 | [diff] [blame] | 49 | static char const* const sVersionString14 = "1.4 Android META-EGL"; |
| 50 | static char const* const sVersionString15 = "1.5 Android META-EGL"; |
Mathias Agopian | cc2b156 | 2012-05-21 14:01:37 -0700 | [diff] [blame] | 51 | static char const * const sClientApiString = "OpenGL_ES"; |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 52 | |
Jesse Hall | 21558da | 2013-08-06 15:31:22 -0700 | [diff] [blame] | 53 | extern char const * const gBuiltinExtensionString; |
Mathias Agopian | e9b3dfb | 2013-03-27 14:30:19 -0700 | [diff] [blame] | 54 | extern char const * const gExtensionString; |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 55 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 56 | extern void setGLHooksThreadSpecific(gl_hooks_t const *value); |
| 57 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 58 | // ---------------------------------------------------------------------------- |
| 59 | |
Courtney Goeltzenleuchter | a1e59f1 | 2018-03-05 08:19:25 -0700 | [diff] [blame] | 60 | bool findExtension(const char* exts, const char* name, size_t nameLen) { |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 61 | if (exts) { |
Courtney Goeltzenleuchter | a1e59f1 | 2018-03-05 08:19:25 -0700 | [diff] [blame] | 62 | if (!nameLen) { |
| 63 | nameLen = strlen(name); |
| 64 | } |
Kalle Raita | 7804aa2 | 2016-04-18 16:03:37 -0700 | [diff] [blame] | 65 | for (const char* match = strstr(exts, name); match; match = strstr(match + nameLen, name)) { |
| 66 | if (match[nameLen] == '\0' || match[nameLen] == ' ') { |
| 67 | return true; |
| 68 | } |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
Krzysztof KosiĆski | dbccd22 | 2019-05-16 14:10:25 -0700 | [diff] [blame] | 74 | bool needsAndroidPEglMitigation() { |
| 75 | static const int32_t vndk_version = property_get_int32("ro.vndk.version", -1); |
| 76 | return vndk_version <= 28; |
| 77 | } |
| 78 | |
Mathias Agopian | b7f9a24 | 2017-03-08 22:29:31 -0800 | [diff] [blame] | 79 | int egl_get_init_count(EGLDisplay dpy) { |
| 80 | egl_display_t* eglDisplay = egl_display_t::get(dpy); |
| 81 | return eglDisplay ? eglDisplay->getRefsCount() : 0; |
| 82 | } |
| 83 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 84 | egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS]; |
| 85 | |
| 86 | egl_display_t::egl_display_t() : |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 87 | magic('_dpy'), finishOnSwap(false), traceGpuCompletion(false), refs(0), eglIsInitialized(false) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | egl_display_t::~egl_display_t() { |
| 91 | magic = 0; |
Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 92 | egl_cache_t::get()->terminate(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | egl_display_t* egl_display_t::get(EGLDisplay dpy) { |
Ivan Lozano | 7025b54 | 2017-12-06 14:19:44 -0800 | [diff] [blame] | 96 | if (uintptr_t(dpy) == 0) { |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 100 | uintptr_t index = uintptr_t(dpy)-1U; |
Jesse Hall | d6e9946 | 2016-09-28 11:26:57 -0700 | [diff] [blame] | 101 | if (index >= NUM_DISPLAYS || !sDisplay[index].isValid()) { |
| 102 | return nullptr; |
| 103 | } |
| 104 | return &sDisplay[index]; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void egl_display_t::addObject(egl_object_t* object) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 108 | std::lock_guard<std::mutex> _l(lock); |
| 109 | objects.insert(object); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 112 | void egl_display_t::removeObject(egl_object_t* object) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 113 | std::lock_guard<std::mutex> _l(lock); |
| 114 | objects.erase(object); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 117 | bool egl_display_t::getObject(egl_object_t* object) const { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 118 | std::lock_guard<std::mutex> _l(lock); |
| 119 | if (objects.find(object) != objects.end()) { |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 120 | if (object->getDisplay() == this) { |
| 121 | object->incRef(); |
| 122 | return true; |
| 123 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 128 | EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp, |
| 129 | const EGLAttrib* attrib_list) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 130 | if (uintptr_t(disp) >= NUM_DISPLAYS) |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 131 | return nullptr; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 132 | |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 133 | return sDisplay[uintptr_t(disp)].getPlatformDisplay(disp, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 136 | static EGLDisplay getPlatformDisplayAngle(EGLNativeDisplayType display, egl_connection_t* const cnx, |
| 137 | const EGLAttrib* attrib_list, EGLint* error) { |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 138 | EGLDisplay dpy = EGL_NO_DISPLAY; |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 139 | *error = EGL_NONE; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 140 | |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 141 | if (cnx->egl.eglGetPlatformDisplay) { |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 142 | std::vector<EGLAttrib> attrs; |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 143 | if (attrib_list) { |
| 144 | for (const EGLAttrib* attr = attrib_list; *attr != EGL_NONE; attr += 2) { |
| 145 | attrs.push_back(attr[0]); |
| 146 | attrs.push_back(attr[1]); |
| 147 | } |
| 148 | } |
| 149 | |
Courtney Goeltzenleuchter | c1a1a86 | 2020-04-23 08:19:11 -0600 | [diff] [blame^] | 150 | attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_ANGLE); |
| 151 | attrs.push_back(EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE); |
| 152 | |
| 153 | char prop[PROPERTY_VALUE_MAX]; |
| 154 | property_get("debug.angle.validation", prop, "0"); |
| 155 | attrs.push_back(EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED_ANGLE); |
| 156 | attrs.push_back(atoi(prop)); |
| 157 | |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 158 | attrs.push_back(EGL_NONE); |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 159 | |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 160 | dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE, |
| 161 | reinterpret_cast<void*>(EGL_DEFAULT_DISPLAY), |
| 162 | attrs.data()); |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 163 | if (dpy == EGL_NO_DISPLAY) { |
| 164 | ALOGE("eglGetPlatformDisplay failed!"); |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame] | 165 | } else { |
Tobin Ehlis | 75ce477 | 2018-11-20 09:48:47 -0700 | [diff] [blame] | 166 | if (!angle::initializeAnglePlatform(dpy)) { |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame] | 167 | ALOGE("initializeAnglePlatform failed!"); |
| 168 | } |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 169 | } |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 170 | } else { |
| 171 | ALOGE("eglGetDisplay(%p) failed: Unable to look up eglGetPlatformDisplay from ANGLE", |
| 172 | display); |
| 173 | } |
| 174 | |
| 175 | return dpy; |
| 176 | } |
| 177 | |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 178 | EGLDisplay egl_display_t::getPlatformDisplay(EGLNativeDisplayType display, |
| 179 | const EGLAttrib* attrib_list) { |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 180 | std::lock_guard<std::mutex> _l(lock); |
Jesse Hall | 1508ae6 | 2017-01-19 17:43:26 -0800 | [diff] [blame] | 181 | ATRACE_CALL(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 182 | |
| 183 | // get our driver loader |
| 184 | Loader& loader(Loader::getInstance()); |
| 185 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 186 | egl_connection_t* const cnx = &gEGLImpl; |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 187 | if (cnx->dso) { |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 188 | EGLDisplay dpy = EGL_NO_DISPLAY; |
| 189 | |
Charlie Lao | 840bd53 | 2020-01-16 17:34:37 -0800 | [diff] [blame] | 190 | if (cnx->useAngle) { |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 191 | EGLint error; |
| 192 | dpy = getPlatformDisplayAngle(display, cnx, attrib_list, &error); |
| 193 | if (error != EGL_NONE) { |
| 194 | return setError(error, dpy); |
| 195 | } |
Courtney Goeltzenleuchter | 555f1df | 2018-09-25 14:34:29 -0600 | [diff] [blame] | 196 | } |
| 197 | if (dpy == EGL_NO_DISPLAY) { |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 198 | // NOTE: eglGetPlatformDisplay with a empty attribute list |
| 199 | // behaves the same as eglGetDisplay |
| 200 | if (cnx->egl.eglGetPlatformDisplay) { |
| 201 | dpy = cnx->egl.eglGetPlatformDisplay(EGL_PLATFORM_ANDROID_KHR, display, |
| 202 | attrib_list); |
Ben Lin | e17f69b | 2018-11-05 16:49:28 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // It is possible that eglGetPlatformDisplay does not have a |
| 206 | // working implementation for Android platform; in that case, |
| 207 | // one last fallback to eglGetDisplay |
| 208 | if(dpy == EGL_NO_DISPLAY) { |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 209 | if (attrib_list) { |
| 210 | ALOGW("getPlatformDisplay: unexpected attribute list, attributes ignored"); |
| 211 | } |
| 212 | dpy = cnx->egl.eglGetDisplay(display); |
| 213 | } |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 214 | } |
| 215 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 216 | disp.dpy = dpy; |
| 217 | if (dpy == EGL_NO_DISPLAY) { |
Courtney Goeltzenleuchter | ee768c2 | 2018-10-25 11:43:52 -0600 | [diff] [blame] | 218 | loader.close(cnx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | return EGLDisplay(uintptr_t(display) + 1U); |
| 223 | } |
| 224 | |
| 225 | EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) { |
| 226 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 227 | { // scope for refLock |
| 228 | std::unique_lock<std::mutex> _l(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 229 | refs++; |
| 230 | if (refs > 1) { |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 231 | // We don't know what to report until we know what the |
| 232 | // driver supports. Make sure we are initialized before |
| 233 | // returning the version info. |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 234 | while(!eglIsInitialized) { |
| 235 | refCond.wait(_l); |
| 236 | } |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 237 | egl_connection_t* const cnx = &gEGLImpl; |
| 238 | |
| 239 | // TODO: If device doesn't provide 1.4 or 1.5 then we'll be |
| 240 | // changing the behavior from the past where we always advertise |
| 241 | // version 1.4. May need to check that revision is valid |
| 242 | // before using cnx->major & cnx->minor |
| 243 | if (major != nullptr) *major = cnx->major; |
| 244 | if (minor != nullptr) *minor = cnx->minor; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 245 | return EGL_TRUE; |
| 246 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 247 | while(eglIsInitialized) { |
| 248 | refCond.wait(_l); |
| 249 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 252 | { // scope for lock |
| 253 | std::lock_guard<std::mutex> _l(lock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 254 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 255 | setGLHooksThreadSpecific(&gHooksNoContext); |
| 256 | |
| 257 | // initialize each EGL and |
| 258 | // build our own extension string first, based on the extension we know |
| 259 | // and the extension supported by our client implementation |
| 260 | |
| 261 | egl_connection_t* const cnx = &gEGLImpl; |
| 262 | cnx->major = -1; |
| 263 | cnx->minor = -1; |
| 264 | if (cnx->dso) { |
| 265 | EGLDisplay idpy = disp.dpy; |
| 266 | if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) { |
| 267 | //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p", |
| 268 | // idpy, cnx->major, cnx->minor, cnx); |
| 269 | |
| 270 | // display is now initialized |
| 271 | disp.state = egl_display_t::INITIALIZED; |
| 272 | |
| 273 | // get the query-strings for this display for each implementation |
| 274 | disp.queryString.vendor = cnx->egl.eglQueryString(idpy, |
| 275 | EGL_VENDOR); |
| 276 | disp.queryString.version = cnx->egl.eglQueryString(idpy, |
| 277 | EGL_VERSION); |
| 278 | disp.queryString.extensions = cnx->egl.eglQueryString(idpy, |
| 279 | EGL_EXTENSIONS); |
| 280 | disp.queryString.clientApi = cnx->egl.eglQueryString(idpy, |
| 281 | EGL_CLIENT_APIS); |
| 282 | |
| 283 | } else { |
| 284 | ALOGW("eglInitialize(%p) failed (%s)", idpy, |
| 285 | egl_tls_t::egl_strerror(cnx->egl.eglGetError())); |
| 286 | } |
| 287 | } |
| 288 | |
Sean Callanan | ad9061c | 2019-03-12 14:07:23 -0700 | [diff] [blame] | 289 | if (cnx->minor == 5) { |
| 290 | // full list in egl_entries.in |
| 291 | if (!cnx->egl.eglCreateImage || |
| 292 | !cnx->egl.eglDestroyImage || |
| 293 | !cnx->egl.eglGetPlatformDisplay || |
| 294 | !cnx->egl.eglCreatePlatformWindowSurface || |
| 295 | !cnx->egl.eglCreatePlatformPixmapSurface || |
| 296 | !cnx->egl.eglCreateSync || |
| 297 | !cnx->egl.eglDestroySync || |
| 298 | !cnx->egl.eglClientWaitSync || |
| 299 | !cnx->egl.eglGetSyncAttrib || |
| 300 | !cnx->egl.eglWaitSync) { |
| 301 | ALOGE("Driver indicates EGL 1.5 support, but does not have " |
| 302 | "a critical API"); |
| 303 | cnx->minor = 4; |
| 304 | } |
| 305 | } |
| 306 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 307 | // the query strings are per-display |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 308 | mVendorString = sVendorString; |
Courtney Goeltzenleuchter | 015ed67 | 2018-07-27 13:43:23 -0600 | [diff] [blame] | 309 | mVersionString.clear(); |
| 310 | cnx->driverVersion = EGL_MAKE_VERSION(1, 4, 0); |
Courtney Goeltzenleuchter | f5b5c41 | 2019-04-10 17:36:19 -0600 | [diff] [blame] | 311 | mVersionString = sVersionString14; |
Courtney Goeltzenleuchter | 015ed67 | 2018-07-27 13:43:23 -0600 | [diff] [blame] | 312 | if ((cnx->major == 1) && (cnx->minor == 5)) { |
| 313 | mVersionString = sVersionString15; |
| 314 | cnx->driverVersion = EGL_MAKE_VERSION(1, 5, 0); |
Courtney Goeltzenleuchter | 015ed67 | 2018-07-27 13:43:23 -0600 | [diff] [blame] | 315 | } |
| 316 | if (mVersionString.empty()) { |
| 317 | ALOGW("Unexpected driver version: %d.%d, want 1.4 or 1.5", cnx->major, cnx->minor); |
| 318 | mVersionString = sVersionString14; |
| 319 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 320 | mClientApiString = sClientApiString; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 321 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 322 | mExtensionString = gBuiltinExtensionString; |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 323 | |
Krzysztof KosiĆski | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 324 | hasColorSpaceSupport = findExtension(disp.queryString.extensions, "EGL_KHR_gl_colorspace"); |
| 325 | |
| 326 | // Note: CDD requires that devices supporting wide color and/or HDR color also support |
| 327 | // the EGL_KHR_gl_colorspace extension. |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 328 | bool wideColorBoardConfig = |
| 329 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>( |
| 330 | false); |
| 331 | |
| 332 | // Add wide-color extensions if device can support wide-color |
Krzysztof KosiĆski | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 333 | if (wideColorBoardConfig && hasColorSpaceSupport) { |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 334 | mExtensionString.append( |
| 335 | "EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear " |
Peiyong Lin | e0ff377 | 2018-12-08 22:23:20 -0800 | [diff] [blame] | 336 | "EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 " |
| 337 | "EGL_EXT_gl_colorspace_display_p3_passthrough "); |
Courtney Goeltzenleuchter | e5d6f99 | 2017-07-07 14:55:40 -0600 | [diff] [blame] | 338 | } |
| 339 | |
Courtney Goeltzenleuchter | 12ffe09 | 2017-11-16 14:27:48 -0700 | [diff] [blame] | 340 | bool hasHdrBoardConfig = |
| 341 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(false); |
| 342 | |
Krzysztof KosiĆski | f2fc4e9 | 2018-04-18 16:29:49 -0700 | [diff] [blame] | 343 | if (hasHdrBoardConfig && hasColorSpaceSupport) { |
Courtney Goeltzenleuchter | 12ffe09 | 2017-11-16 14:27:48 -0700 | [diff] [blame] | 344 | // hasHDRBoardConfig indicates the system is capable of supporting HDR content. |
| 345 | // Typically that means there is an HDR capable display attached, but could be |
| 346 | // support for attaching an HDR display. In either case, advertise support for |
| 347 | // HDR color spaces. |
| 348 | mExtensionString.append( |
| 349 | "EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq "); |
| 350 | } |
| 351 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 352 | char const* start = gExtensionString; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 353 | do { |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 354 | // length of the extension name |
| 355 | size_t len = strcspn(start, " "); |
| 356 | if (len) { |
| 357 | // NOTE: we could avoid the copy if we had strnstr. |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 358 | const std::string ext(start, len); |
Krzysztof KosiĆski | dbccd22 | 2019-05-16 14:10:25 -0700 | [diff] [blame] | 359 | // Mitigation for Android P vendor partitions: Adreno 530 driver shipped on |
| 360 | // some Android P vendor partitions this extension under the draft KHR name, |
| 361 | // but during Khronos review it was decided to demote it to EXT. |
| 362 | if (needsAndroidPEglMitigation() && ext == "EGL_EXT_image_gl_colorspace" && |
| 363 | findExtension(disp.queryString.extensions, "EGL_KHR_image_gl_colorspace")) { |
| 364 | mExtensionString.append("EGL_EXT_image_gl_colorspace "); |
| 365 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 366 | if (findExtension(disp.queryString.extensions, ext.c_str(), len)) { |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 367 | mExtensionString.append(ext + " "); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 368 | } |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 369 | // advance to the next extension name, skipping the space. |
| 370 | start += len; |
| 371 | start += (*start == ' ') ? 1 : 0; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 372 | } |
Nicolas Capens | ecc0c9a | 2015-10-30 12:55:21 -0400 | [diff] [blame] | 373 | } while (*start != '\0'); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 374 | |
| 375 | egl_cache_t::get()->initialize(this); |
| 376 | |
| 377 | char value[PROPERTY_VALUE_MAX]; |
| 378 | property_get("debug.egl.finish", value, "0"); |
| 379 | if (atoi(value)) { |
| 380 | finishOnSwap = true; |
| 381 | } |
| 382 | |
| 383 | property_get("debug.egl.traceGpuCompletion", value, "0"); |
| 384 | if (atoi(value)) { |
| 385 | traceGpuCompletion = true; |
| 386 | } |
| 387 | |
Courtney Goeltzenleuchter | 4adf75b | 2018-10-11 13:09:40 -0600 | [diff] [blame] | 388 | // TODO: If device doesn't provide 1.4 or 1.5 then we'll be |
| 389 | // changing the behavior from the past where we always advertise |
| 390 | // version 1.4. May need to check that revision is valid |
| 391 | // before using cnx->major & cnx->minor |
| 392 | if (major != nullptr) *major = cnx->major; |
| 393 | if (minor != nullptr) *minor = cnx->minor; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 396 | { // scope for refLock |
| 397 | std::unique_lock<std::mutex> _l(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 398 | eglIsInitialized = true; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 399 | refCond.notify_all(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 402 | return EGL_TRUE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | EGLBoolean egl_display_t::terminate() { |
| 406 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 407 | { // scope for refLock |
| 408 | std::unique_lock<std::mutex> _rl(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 409 | if (refs == 0) { |
| 410 | /* |
| 411 | * From the EGL spec (3.2): |
| 412 | * "Termination of a display that has already been terminated, |
| 413 | * (...), is allowed, but the only effect of such a call is |
| 414 | * to return EGL_TRUE (...) |
| 415 | */ |
| 416 | return EGL_TRUE; |
| 417 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 418 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 419 | // this is specific to Android, display termination is ref-counted. |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 420 | refs--; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 421 | if (refs > 0) { |
| 422 | return EGL_TRUE; |
| 423 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | EGLBoolean res = EGL_FALSE; |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 427 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 428 | { // scope for lock |
| 429 | std::lock_guard<std::mutex> _l(lock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 430 | |
| 431 | egl_connection_t* const cnx = &gEGLImpl; |
| 432 | if (cnx->dso && disp.state == egl_display_t::INITIALIZED) { |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame] | 433 | // If we're using ANGLE reset any custom DisplayPlatform |
Charlie Lao | 840bd53 | 2020-01-16 17:34:37 -0800 | [diff] [blame] | 434 | if (cnx->useAngle) { |
Tobin Ehlis | 75ce477 | 2018-11-20 09:48:47 -0700 | [diff] [blame] | 435 | angle::resetAnglePlatform(disp.dpy); |
Tobin Ehlis | 96a184d | 2018-07-18 16:14:07 -0600 | [diff] [blame] | 436 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 437 | if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) { |
| 438 | ALOGW("eglTerminate(%p) failed (%s)", disp.dpy, |
| 439 | egl_tls_t::egl_strerror(cnx->egl.eglGetError())); |
| 440 | } |
| 441 | // REVISIT: it's unclear what to do if eglTerminate() fails |
| 442 | disp.state = egl_display_t::TERMINATED; |
| 443 | res = EGL_TRUE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 444 | } |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 445 | |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 446 | // Reset the extension string since it will be regenerated if we get |
| 447 | // reinitialized. |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 448 | mExtensionString.clear(); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 449 | |
| 450 | // Mark all objects remaining in the list as terminated, unless |
| 451 | // there are no reference to them, it which case, we're free to |
| 452 | // delete them. |
| 453 | size_t count = objects.size(); |
Dan Albert | eacd31f | 2016-02-02 15:08:34 -0800 | [diff] [blame] | 454 | ALOGW_IF(count, "eglTerminate() called w/ %zu objects remaining", count); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 455 | for (auto o : objects) { |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 456 | o->destroy(); |
| 457 | } |
| 458 | |
| 459 | // this marks all object handles are "terminated" |
| 460 | objects.clear(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 463 | { // scope for refLock |
| 464 | std::unique_lock<std::mutex> _rl(refLock); |
Michael Lentine | 54466bc | 2015-01-27 09:01:03 -0800 | [diff] [blame] | 465 | eglIsInitialized = false; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 466 | refCond.notify_all(); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 469 | return res; |
| 470 | } |
| 471 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 472 | void egl_display_t::loseCurrent(egl_context_t * cur_c) |
| 473 | { |
| 474 | if (cur_c) { |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 475 | egl_display_t* display = cur_c->getDisplay(); |
| 476 | if (display) { |
| 477 | display->loseCurrentImpl(cur_c); |
| 478 | } |
| 479 | } |
| 480 | } |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 481 | |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 482 | void egl_display_t::loseCurrentImpl(egl_context_t * cur_c) |
| 483 | { |
| 484 | // by construction, these are either 0 or valid (possibly terminated) |
| 485 | // it should be impossible for these to be invalid |
| 486 | ContextRef _cur_c(cur_c); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 487 | SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr); |
| 488 | SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 489 | |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 490 | { // scope for the lock |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 491 | std::lock_guard<std::mutex> _l(lock); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 492 | cur_c->onLooseCurrent(); |
| 493 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 494 | } |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 495 | |
| 496 | // This cannot be called with the lock held because it might end-up |
| 497 | // calling back into EGL (in particular when a surface is destroyed |
| 498 | // it calls ANativeWindow::disconnect |
| 499 | _cur_c.release(); |
| 500 | _cur_r.release(); |
| 501 | _cur_d.release(); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c, |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 505 | EGLSurface draw, EGLSurface read, EGLContext /*ctx*/, |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 506 | EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx) |
| 507 | { |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 508 | EGLBoolean result; |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 509 | |
| 510 | // by construction, these are either 0 or valid (possibly terminated) |
| 511 | // it should be impossible for these to be invalid |
| 512 | ContextRef _cur_c(cur_c); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 513 | SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : nullptr); |
| 514 | SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : nullptr); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 515 | |
| 516 | { // scope for the lock |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 517 | std::lock_guard<std::mutex> _l(lock); |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 518 | if (c) { |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 519 | result = c->cnx->egl.eglMakeCurrent( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 520 | disp.dpy, impl_draw, impl_read, impl_ctx); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 521 | if (result == EGL_TRUE) { |
| 522 | c->onMakeCurrent(draw, read); |
| 523 | } |
| 524 | } else { |
| 525 | result = cur_c->cnx->egl.eglMakeCurrent( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 526 | disp.dpy, impl_draw, impl_read, impl_ctx); |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 527 | if (result == EGL_TRUE) { |
| 528 | cur_c->onLooseCurrent(); |
| 529 | } |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 530 | } |
| 531 | } |
Mathias Agopian | a4b2c04 | 2012-02-03 15:24:51 -0800 | [diff] [blame] | 532 | |
| 533 | if (result == EGL_TRUE) { |
| 534 | // This cannot be called with the lock held because it might end-up |
| 535 | // calling back into EGL (in particular when a surface is destroyed |
| 536 | // it calls ANativeWindow::disconnect |
| 537 | _cur_c.release(); |
| 538 | _cur_r.release(); |
| 539 | _cur_d.release(); |
| 540 | } |
| 541 | |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 542 | return result; |
| 543 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 544 | |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 545 | bool egl_display_t::haveExtension(const char* name, size_t nameLen) const { |
| 546 | if (!nameLen) { |
| 547 | nameLen = strlen(name); |
| 548 | } |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 549 | return findExtension(mExtensionString.c_str(), name, nameLen); |
Jesse Hall | c2e4122 | 2013-08-08 13:40:22 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Jesse Hall | a0fef1c | 2012-04-17 12:02:26 -0700 | [diff] [blame] | 552 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 553 | }; // namespace android |
| 554 | // ---------------------------------------------------------------------------- |