Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 1 | /* |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [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 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 9 | ** |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [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 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 17 | #include <EGL/egl.h> |
| 18 | #include <android-base/properties.h> |
| 19 | #include <log/log.h> |
Mathias Agopian | d8fb7b5 | 2009-05-17 18:50:16 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 22 | #include "../egl_impl.h" |
Mathias Agopian | 5f1af04 | 2017-03-09 18:50:05 -0800 | [diff] [blame] | 23 | #include "CallStack.h" |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 24 | #include "Loader.h" |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 25 | #include "egl_display.h" |
| 26 | #include "egl_layers.h" |
| 27 | #include "egl_object.h" |
| 28 | #include "egl_tls.h" |
| 29 | #include "egldefs.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | namespace android { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 33 | egl_connection_t gEGLImpl; |
| 34 | gl_hooks_t gHooks[2]; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 35 | gl_hooks_t gHooksNoContext; |
| 36 | pthread_key_t gGLWrapperKey = -1; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 37 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 38 | void setGLHooksThreadSpecific(gl_hooks_t const* value) { |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 39 | setGlThreadSpecific(value); |
| 40 | } |
| 41 | |
Mathias Agopian | 6f08712 | 2010-09-23 16:38:38 -0700 | [diff] [blame] | 42 | static int gl_no_context() { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 43 | if (egl_tls_t::logNoContextCall()) { |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 44 | const char* const error = "call to OpenGL ES API with " |
| 45 | "no current context (logged once per thread)"; |
Mathias Agopian | 455e360 | 2012-09-26 17:19:48 -0700 | [diff] [blame] | 46 | if (LOG_NDEBUG) { |
| 47 | ALOGE(error); |
| 48 | } else { |
| 49 | LOG_ALWAYS_FATAL(error); |
| 50 | } |
Michael Hoisie | 4e0f56b | 2020-04-30 18:40:55 -0400 | [diff] [blame] | 51 | if (base::GetBoolProperty("debug.egl.callstack", false)) { |
Mathias Agopian | 5f1af04 | 2017-03-09 18:50:05 -0800 | [diff] [blame] | 52 | CallStack::log(LOG_TAG); |
Mathias Agopian | ecfe091 | 2011-09-06 17:24:05 -0700 | [diff] [blame] | 53 | } |
Mathias Agopian | d274eae | 2009-07-31 16:21:17 -0700 | [diff] [blame] | 54 | } |
Mathias Agopian | 6f08712 | 2010-09-23 16:38:38 -0700 | [diff] [blame] | 55 | return 0; |
Mathias Agopian | 05c5311 | 2010-09-23 11:32:52 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 58 | static void early_egl_init(void) { |
Dan Stoza | c3289c4 | 2014-01-17 11:38:34 -0800 | [diff] [blame] | 59 | int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer); |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 60 | EGLFuncPointer* iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext); |
Dan Stoza | c3289c4 | 2014-01-17 11:38:34 -0800 | [diff] [blame] | 61 | for (int hook = 0; hook < numHooks; ++hook) { |
| 62 | *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context); |
| 63 | } |
Mathias Agopian | 05c5311 | 2010-09-23 11:32:52 -0700 | [diff] [blame] | 64 | |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 65 | setGLHooksThreadSpecific(&gHooksNoContext); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 68 | const GLubyte* egl_get_string_for_current_context(GLenum name) { |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 69 | // NOTE: returning NULL here will fall-back to the default |
| 70 | // implementation. |
| 71 | |
| 72 | EGLContext context = egl_tls_t::getContext(); |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 73 | if (context == EGL_NO_CONTEXT) return nullptr; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 74 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 75 | const egl_context_t* const c = get_context(context); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 76 | if (c == nullptr) // this should never happen, by construction |
| 77 | return nullptr; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 78 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 79 | if (name != GL_EXTENSIONS) return nullptr; |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 80 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 81 | return (const GLubyte*)c->gl_extensions.c_str(); |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 84 | const GLubyte* egl_get_string_for_current_context(GLenum name, GLuint index) { |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 85 | // NOTE: returning NULL here will fall-back to the default |
| 86 | // implementation. |
| 87 | |
| 88 | EGLContext context = egl_tls_t::getContext(); |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 89 | if (context == EGL_NO_CONTEXT) return nullptr; |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 90 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 91 | const egl_context_t* const c = get_context(context); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 92 | if (c == nullptr) // this should never happen, by construction |
| 93 | return nullptr; |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 94 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 95 | if (name != GL_EXTENSIONS) return nullptr; |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 96 | |
| 97 | // if index is out of bounds, assume it will be in the default |
| 98 | // implementation too, so we don't have to generate a GL error here |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 99 | if (index >= c->tokenized_gl_extensions.size()) return nullptr; |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 100 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 101 | return (const GLubyte*)c->tokenized_gl_extensions[index].c_str(); |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | GLint egl_get_num_extensions_for_current_context() { |
| 105 | // NOTE: returning -1 here will fall-back to the default |
| 106 | // implementation. |
| 107 | |
| 108 | EGLContext context = egl_tls_t::getContext(); |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 109 | if (context == EGL_NO_CONTEXT) return -1; |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 110 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 111 | const egl_context_t* const c = get_context(context); |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 112 | if (c == nullptr) // this should never happen, by construction |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 113 | return -1; |
| 114 | |
| 115 | return (GLint)c->tokenized_gl_extensions.size(); |
| 116 | } |
| 117 | |
Cody Northrop | 68d1035 | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 118 | egl_connection_t* egl_get_connection() { |
| 119 | return &gEGLImpl; |
| 120 | } |
| 121 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 122 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 123 | static int sEarlyInitState = pthread_once(&once_control, &early_egl_init); |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 124 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 125 | static EGLBoolean egl_init_drivers_locked() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | if (sEarlyInitState) { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 127 | // initialized by static ctor. should be set here. |
| 128 | return EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 131 | // get our driver loader |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 132 | Loader& loader(Loader::getInstance()); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 133 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 134 | // dynamically load our EGL implementation |
| 135 | egl_connection_t* cnx = &gEGLImpl; |
Yiwei Zhang | 5e21eb3 | 2019-06-05 00:26:03 -0700 | [diff] [blame] | 136 | cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX]; |
| 137 | cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX]; |
| 138 | cnx->dso = loader.open(cnx); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 140 | // Check to see if any layers are enabled and route functions through them |
| 141 | if (cnx->dso) { |
| 142 | // Layers can be enabled long after the drivers have been loaded. |
| 143 | // They will only be initialized once. |
| 144 | LayerLoader& layer_loader(LayerLoader::getInstance()); |
| 145 | layer_loader.InitLayers(cnx); |
| 146 | } |
| 147 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 148 | return cnx->dso ? EGL_TRUE : EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Charlie Lao | c51d5f5 | 2020-01-14 11:05:53 -0800 | [diff] [blame] | 151 | // this mutex protects driver load logic as a critical section since it accesses to global variable |
| 152 | // like gEGLImpl |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 153 | static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER; |
| 154 | |
| 155 | EGLBoolean egl_init_drivers() { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 156 | EGLBoolean res; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 157 | pthread_mutex_lock(&sInitDriverMutex); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 158 | res = egl_init_drivers_locked(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 159 | pthread_mutex_unlock(&sInitDriverMutex); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 160 | return res; |
| 161 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 162 | |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 163 | static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 164 | static std::chrono::steady_clock::time_point sLogPrintTime; |
| 165 | static constexpr std::chrono::seconds DURATION(1); |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 166 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 167 | void gl_unimplemented() { |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 168 | bool printLog = false; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 169 | auto now = std::chrono::steady_clock::now(); |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 170 | pthread_mutex_lock(&sLogPrintMutex); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 171 | if ((now - sLogPrintTime) > DURATION) { |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 172 | sLogPrintTime = now; |
| 173 | printLog = true; |
| 174 | } |
| 175 | pthread_mutex_unlock(&sLogPrintMutex); |
| 176 | if (printLog) { |
| 177 | ALOGE("called unimplemented OpenGL ES API"); |
Michael Hoisie | 4e0f56b | 2020-04-30 18:40:55 -0400 | [diff] [blame] | 178 | if (base::GetBoolProperty("debug.egl.callstack", false)) { |
Mathias Agopian | 5f1af04 | 2017-03-09 18:50:05 -0800 | [diff] [blame] | 179 | CallStack::log(LOG_TAG); |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 180 | } |
Mathias Agopian | a6bb107 | 2013-08-07 20:10:20 -0700 | [diff] [blame] | 181 | } |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 184 | void gl_noop() {} |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 185 | |
Yiwei Zhang | 8af0306 | 2020-08-12 21:28:15 -0700 | [diff] [blame^] | 186 | void setGlThreadSpecific(gl_hooks_t const* value) { |
| 187 | gl_hooks_t const* volatile* tls_hooks = get_tls_hooks(); |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 188 | tls_hooks[TLS_SLOT_OPENGL_API] = value; |
| 189 | } |
| 190 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 191 | // ---------------------------------------------------------------------------- |
| 192 | // GL / EGL hooks |
| 193 | // ---------------------------------------------------------------------------- |
| 194 | |
| 195 | #undef GL_ENTRY |
| 196 | #undef EGL_ENTRY |
| 197 | #define GL_ENTRY(_r, _api, ...) #_api, |
| 198 | #define EGL_ENTRY(_r, _api, ...) #_api, |
| 199 | |
| 200 | char const * const gl_names[] = { |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 201 | #include "../entries.in" |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 202 | nullptr |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
Yiwei Zhang | 7cc5892 | 2018-10-10 11:37:43 -0700 | [diff] [blame] | 205 | char const * const gl_names_1[] = { |
| 206 | #include "../entries_gles1.in" |
| 207 | nullptr |
| 208 | }; |
| 209 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 210 | char const * const egl_names[] = { |
| 211 | #include "egl_entries.in" |
Yi Kong | 48a6cd2 | 2018-07-18 10:07:09 -0700 | [diff] [blame] | 212 | nullptr |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
Cody Northrop | 68d1035 | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 215 | char const * const platform_names[] = { |
| 216 | #include "platform_entries.in" |
| 217 | nullptr |
| 218 | }; |
| 219 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 220 | #undef GL_ENTRY |
| 221 | #undef EGL_ENTRY |
| 222 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 223 | }; // namespace android |