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