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