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