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