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 | |
Mathias Agopian | d8fb7b5 | 2009-05-17 18:50:16 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 19 | #include <hardware/gralloc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | #include <EGL/egl.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 24 | |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 25 | #include <log/log.h> |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 26 | |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 27 | #include "../egl_impl.h" |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 28 | |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 29 | #include "egldefs.h" |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 30 | #include "egl_tls.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 31 | #include "egl_display.h" |
| 32 | #include "egl_object.h" |
Mathias Agopian | 5f1af04 | 2017-03-09 18:50:05 -0800 | [diff] [blame] | 33 | #include "CallStack.h" |
Mathias Agopian | 311b479 | 2017-02-28 15:00:49 -0800 | [diff] [blame] | 34 | #include "Loader.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
Dan Stoza | c3289c4 | 2014-01-17 11:38:34 -0800 | [diff] [blame] | 36 | typedef __eglMustCastToProperFunctionPointerType EGLFuncPointer; |
| 37 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | // ---------------------------------------------------------------------------- |
| 39 | namespace android { |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 42 | egl_connection_t gEGLImpl; |
| 43 | gl_hooks_t gHooks[2]; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 44 | gl_hooks_t gHooksNoContext; |
| 45 | pthread_key_t gGLWrapperKey = -1; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 46 | |
| 47 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 49 | void setGLHooksThreadSpecific(gl_hooks_t const *value) { |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 50 | setGlThreadSpecific(value); |
| 51 | } |
| 52 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | /*****************************************************************************/ |
| 54 | |
Mathias Agopian | 6f08712 | 2010-09-23 16:38:38 -0700 | [diff] [blame] | 55 | static int gl_no_context() { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 56 | if (egl_tls_t::logNoContextCall()) { |
Mathias Agopian | 455e360 | 2012-09-26 17:19:48 -0700 | [diff] [blame] | 57 | char const* const error = "call to OpenGL ES API with " |
| 58 | "no current context (logged once per thread)"; |
| 59 | if (LOG_NDEBUG) { |
| 60 | ALOGE(error); |
| 61 | } else { |
| 62 | LOG_ALWAYS_FATAL(error); |
| 63 | } |
Mathias Agopian | ecfe091 | 2011-09-06 17:24:05 -0700 | [diff] [blame] | 64 | char value[PROPERTY_VALUE_MAX]; |
| 65 | property_get("debug.egl.callstack", value, "0"); |
John Reck | 1f246d7 | 2014-04-24 23:34:32 +0000 | [diff] [blame] | 66 | if (atoi(value)) { |
Mathias Agopian | 5f1af04 | 2017-03-09 18:50:05 -0800 | [diff] [blame] | 67 | CallStack::log(LOG_TAG); |
Mathias Agopian | ecfe091 | 2011-09-06 17:24:05 -0700 | [diff] [blame] | 68 | } |
Mathias Agopian | d274eae | 2009-07-31 16:21:17 -0700 | [diff] [blame] | 69 | } |
Mathias Agopian | 6f08712 | 2010-09-23 16:38:38 -0700 | [diff] [blame] | 70 | return 0; |
Mathias Agopian | 05c5311 | 2010-09-23 11:32:52 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jesse Hall | 4774338 | 2013-02-08 11:13:46 -0800 | [diff] [blame] | 73 | static void early_egl_init(void) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | { |
Dan Stoza | c3289c4 | 2014-01-17 11:38:34 -0800 | [diff] [blame] | 75 | int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer); |
| 76 | EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext); |
| 77 | for (int hook = 0; hook < numHooks; ++hook) { |
| 78 | *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context); |
| 79 | } |
Mathias Agopian | 05c5311 | 2010-09-23 11:32:52 -0700 | [diff] [blame] | 80 | |
Jack Palevich | a2dd6cf | 2010-10-26 15:21:24 -0700 | [diff] [blame] | 81 | setGLHooksThreadSpecific(&gHooksNoContext); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 85 | static int sEarlyInitState = pthread_once(&once_control, &early_egl_init); |
| 86 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 87 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 89 | egl_display_ptr validate_display(EGLDisplay dpy) { |
| 90 | egl_display_ptr dp = get_display(dpy); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 91 | if (!dp) |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 92 | return setError(EGL_BAD_DISPLAY, egl_display_ptr(NULL)); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 93 | if (!dp->isReady()) |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 94 | return setError(EGL_NOT_INITIALIZED, egl_display_ptr(NULL)); |
Eric Hassold | 3ede7c1 | 2011-03-23 15:59:00 -0700 | [diff] [blame] | 95 | |
| 96 | return dp; |
| 97 | } |
| 98 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 99 | egl_display_ptr validate_display_connection(EGLDisplay dpy, |
| 100 | egl_connection_t*& cnx) { |
| 101 | cnx = NULL; |
| 102 | egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 103 | if (!dp) |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 104 | return dp; |
| 105 | cnx = &gEGLImpl; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | if (cnx->dso == 0) { |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 107 | return setError(EGL_BAD_CONFIG, egl_display_ptr(NULL)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | } |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 109 | return dp; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 112 | // ---------------------------------------------------------------------------- |
| 113 | |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 114 | const GLubyte * egl_get_string_for_current_context(GLenum name) { |
| 115 | // NOTE: returning NULL here will fall-back to the default |
| 116 | // implementation. |
| 117 | |
| 118 | EGLContext context = egl_tls_t::getContext(); |
| 119 | if (context == EGL_NO_CONTEXT) |
| 120 | return NULL; |
| 121 | |
| 122 | egl_context_t const * const c = get_context(context); |
| 123 | if (c == NULL) // this should never happen, by construction |
| 124 | return NULL; |
| 125 | |
| 126 | if (name != GL_EXTENSIONS) |
| 127 | return NULL; |
| 128 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 129 | return (const GLubyte *)c->gl_extensions.c_str(); |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 132 | const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) { |
| 133 | // NOTE: returning NULL here will fall-back to the default |
| 134 | // implementation. |
| 135 | |
| 136 | EGLContext context = egl_tls_t::getContext(); |
| 137 | if (context == EGL_NO_CONTEXT) |
| 138 | return NULL; |
| 139 | |
| 140 | egl_context_t const * const c = get_context(context); |
| 141 | if (c == NULL) // this should never happen, by construction |
| 142 | return NULL; |
| 143 | |
| 144 | if (name != GL_EXTENSIONS) |
| 145 | return NULL; |
| 146 | |
| 147 | // if index is out of bounds, assume it will be in the default |
| 148 | // implementation too, so we don't have to generate a GL error here |
| 149 | if (index >= c->tokenized_gl_extensions.size()) |
| 150 | return NULL; |
| 151 | |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 152 | return (const GLubyte *)c->tokenized_gl_extensions[index].c_str(); |
Alistair Strachan | edfe72e | 2015-05-22 14:10:09 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | GLint egl_get_num_extensions_for_current_context() { |
| 156 | // NOTE: returning -1 here will fall-back to the default |
| 157 | // implementation. |
| 158 | |
| 159 | EGLContext context = egl_tls_t::getContext(); |
| 160 | if (context == EGL_NO_CONTEXT) |
| 161 | return -1; |
| 162 | |
| 163 | egl_context_t const * const c = get_context(context); |
| 164 | if (c == NULL) // this should never happen, by construction |
| 165 | return -1; |
| 166 | |
| 167 | return (GLint)c->tokenized_gl_extensions.size(); |
| 168 | } |
| 169 | |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 170 | // ---------------------------------------------------------------------------- |
| 171 | |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 172 | // this mutex protects: |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 173 | // d->disp[] |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 174 | // egl_init_drivers_locked() |
| 175 | // |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 176 | static EGLBoolean egl_init_drivers_locked() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | if (sEarlyInitState) { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 178 | // initialized by static ctor. should be set here. |
| 179 | return EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 182 | // get our driver loader |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 183 | Loader& loader(Loader::getInstance()); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 184 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 185 | // dynamically load our EGL implementation |
| 186 | egl_connection_t* cnx = &gEGLImpl; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | if (cnx->dso == 0) { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 188 | cnx->hooks[egl_connection_t::GLESv1_INDEX] = |
| 189 | &gHooks[egl_connection_t::GLESv1_INDEX]; |
| 190 | cnx->hooks[egl_connection_t::GLESv2_INDEX] = |
| 191 | &gHooks[egl_connection_t::GLESv2_INDEX]; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 192 | cnx->dso = loader.open(cnx); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 195 | return cnx->dso ? EGL_TRUE : EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 198 | static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER; |
| 199 | |
| 200 | EGLBoolean egl_init_drivers() { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 201 | EGLBoolean res; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 202 | pthread_mutex_lock(&sInitDriverMutex); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 203 | res = egl_init_drivers_locked(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 204 | pthread_mutex_unlock(&sInitDriverMutex); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 205 | return res; |
| 206 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 207 | |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 208 | static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 209 | static std::chrono::steady_clock::time_point sLogPrintTime; |
| 210 | static constexpr std::chrono::seconds DURATION(1); |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 211 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 212 | void gl_unimplemented() { |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 213 | bool printLog = false; |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 214 | auto now = std::chrono::steady_clock::now(); |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 215 | pthread_mutex_lock(&sLogPrintMutex); |
Mathias Agopian | 6542143 | 2017-03-08 11:49:05 -0800 | [diff] [blame] | 216 | if ((now - sLogPrintTime) > DURATION) { |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 217 | sLogPrintTime = now; |
| 218 | printLog = true; |
| 219 | } |
| 220 | pthread_mutex_unlock(&sLogPrintMutex); |
| 221 | if (printLog) { |
| 222 | ALOGE("called unimplemented OpenGL ES API"); |
| 223 | char value[PROPERTY_VALUE_MAX]; |
| 224 | property_get("debug.egl.callstack", value, "0"); |
| 225 | if (atoi(value)) { |
Mathias Agopian | 5f1af04 | 2017-03-09 18:50:05 -0800 | [diff] [blame] | 226 | CallStack::log(LOG_TAG); |
Michael Lentine | 12c4bda | 2014-09-11 12:24:13 -0700 | [diff] [blame] | 227 | } |
Mathias Agopian | a6bb107 | 2013-08-07 20:10:20 -0700 | [diff] [blame] | 228 | } |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Mathias Agopian | 48d438d | 2012-01-28 21:44:00 -0800 | [diff] [blame] | 231 | void gl_noop() { |
| 232 | } |
| 233 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 234 | // ---------------------------------------------------------------------------- |
| 235 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 236 | void setGlThreadSpecific(gl_hooks_t const *value) { |
| 237 | gl_hooks_t const * volatile * tls_hooks = get_tls_hooks(); |
| 238 | tls_hooks[TLS_SLOT_OPENGL_API] = value; |
| 239 | } |
| 240 | |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 241 | // ---------------------------------------------------------------------------- |
| 242 | // GL / EGL hooks |
| 243 | // ---------------------------------------------------------------------------- |
| 244 | |
| 245 | #undef GL_ENTRY |
| 246 | #undef EGL_ENTRY |
| 247 | #define GL_ENTRY(_r, _api, ...) #_api, |
| 248 | #define EGL_ENTRY(_r, _api, ...) #_api, |
| 249 | |
| 250 | char const * const gl_names[] = { |
Mathias Agopian | 39c24a2 | 2013-04-04 23:17:56 -0700 | [diff] [blame] | 251 | #include "../entries.in" |
Mathias Agopian | 1cadb25 | 2011-05-23 17:26:14 -0700 | [diff] [blame] | 252 | NULL |
| 253 | }; |
| 254 | |
| 255 | char const * const egl_names[] = { |
| 256 | #include "egl_entries.in" |
| 257 | NULL |
| 258 | }; |
| 259 | |
| 260 | #undef GL_ENTRY |
| 261 | #undef EGL_ENTRY |
| 262 | |
| 263 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 264 | // ---------------------------------------------------------------------------- |
| 265 | }; // namespace android |
| 266 | // ---------------------------------------------------------------------------- |
| 267 | |