The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2007, The Android Open Source Project |
| 3 | ** |
| 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 |
| 7 | ** |
| 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | ** |
| 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 |
| 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> |
| 20 | #include <errno.h> |
| 21 | #include <dlfcn.h> |
| 22 | |
| 23 | #include <sys/ioctl.h> |
| 24 | |
| 25 | #if HAVE_ANDROID_OS |
| 26 | #include <linux/android_pmem.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <EGL/egl.h> |
| 30 | #include <EGL/eglext.h> |
| 31 | #include <GLES/gl.h> |
| 32 | #include <GLES/glext.h> |
| 33 | |
| 34 | #include <cutils/log.h> |
| 35 | #include <cutils/atomic.h> |
| 36 | #include <cutils/properties.h> |
| 37 | #include <cutils/memory.h> |
| 38 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 39 | #include <utils/SortedVector.h> |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 40 | #include <utils/KeyedVector.h> |
| 41 | #include <utils/String8.h> |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 42 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | #include "hooks.h" |
| 44 | #include "egl_impl.h" |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 45 | #include "Loader.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | #define setError(_e, _r) setErrorEtc(__FUNCTION__, __LINE__, _e, _r) |
| 48 | |
| 49 | // ---------------------------------------------------------------------------- |
| 50 | namespace android { |
| 51 | // ---------------------------------------------------------------------------- |
| 52 | |
| 53 | #define VERSION_MAJOR 1 |
| 54 | #define VERSION_MINOR 4 |
| 55 | static char const * const gVendorString = "Android"; |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 56 | static char const * const gVersionString = "1.4 Android META-EGL"; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | static char const * const gClientApiString = "OpenGL ES"; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 58 | static char const * const gExtensionString = |
| 59 | "EGL_KHR_image " |
Mathias Agopian | e6bf8b3 | 2009-05-06 23:47:08 -0700 | [diff] [blame] | 60 | "EGL_KHR_image_base " |
| 61 | "EGL_KHR_image_pixmap " |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 62 | "EGL_ANDROID_image_native_buffer " |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 63 | "EGL_ANDROID_swap_rectangle " |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 64 | ; |
| 65 | |
| 66 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 68 | class egl_object_t { |
| 69 | static SortedVector<egl_object_t*> sObjects; |
| 70 | static Mutex sLock; |
| 71 | |
| 72 | volatile int32_t terminated; |
| 73 | mutable volatile int32_t count; |
| 74 | |
| 75 | public: |
| 76 | egl_object_t() : terminated(0), count(1) { |
| 77 | Mutex::Autolock _l(sLock); |
| 78 | sObjects.add(this); |
| 79 | } |
| 80 | |
| 81 | inline bool isAlive() const { return !terminated; } |
| 82 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | private: |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 84 | bool get() { |
| 85 | Mutex::Autolock _l(sLock); |
| 86 | if (egl_object_t::sObjects.indexOf(this) >= 0) { |
| 87 | android_atomic_inc(&count); |
| 88 | return true; |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | bool put() { |
| 94 | Mutex::Autolock _l(sLock); |
| 95 | if (android_atomic_dec(&count) == 1) { |
| 96 | sObjects.remove(this); |
| 97 | return true; |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | public: |
| 103 | template <typename N, typename T> |
| 104 | struct LocalRef { |
| 105 | N* ref; |
| 106 | LocalRef(T o) : ref(0) { |
| 107 | N* native = reinterpret_cast<N*>(o); |
| 108 | if (o && native->get()) { |
| 109 | ref = native; |
| 110 | } |
| 111 | } |
| 112 | ~LocalRef() { |
| 113 | if (ref && ref->put()) { |
| 114 | delete ref; |
| 115 | } |
| 116 | } |
| 117 | inline N* get() { |
| 118 | return ref; |
| 119 | } |
| 120 | void acquire() const { |
| 121 | if (ref) { |
| 122 | android_atomic_inc(&ref->count); |
| 123 | } |
| 124 | } |
| 125 | void release() const { |
| 126 | if (ref) { |
| 127 | int32_t c = android_atomic_dec(&ref->count); |
| 128 | // ref->count cannot be 1 prior atomic_dec because we have |
| 129 | // a reference, and if we have one, it means there was |
| 130 | // already one before us. |
| 131 | LOGE_IF(c==1, "refcount is now 0 in release()"); |
| 132 | } |
| 133 | } |
| 134 | void terminate() { |
| 135 | if (ref) { |
| 136 | ref->terminated = 1; |
| 137 | release(); |
| 138 | } |
| 139 | } |
| 140 | }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 143 | SortedVector<egl_object_t*> egl_object_t::sObjects; |
| 144 | Mutex egl_object_t::sLock; |
| 145 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 146 | |
| 147 | struct egl_config_t { |
| 148 | egl_config_t() {} |
| 149 | egl_config_t(int impl, EGLConfig config) |
| 150 | : impl(impl), config(config), configId(0), implConfigId(0) { } |
| 151 | int impl; // the implementation this config is for |
| 152 | EGLConfig config; // the implementation's EGLConfig |
| 153 | EGLint configId; // our CONFIG_ID |
| 154 | EGLint implConfigId; // the implementation's CONFIG_ID |
| 155 | inline bool operator < (const egl_config_t& rhs) const { |
| 156 | if (impl < rhs.impl) return true; |
| 157 | if (impl > rhs.impl) return false; |
| 158 | return config < rhs.config; |
| 159 | } |
| 160 | }; |
| 161 | |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 162 | struct egl_display_t { |
| 163 | enum { NOT_INITIALIZED, INITIALIZED, TERMINATED }; |
| 164 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | struct strings_t { |
| 166 | char const * vendor; |
| 167 | char const * version; |
| 168 | char const * clientApi; |
| 169 | char const * extensions; |
| 170 | }; |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 171 | |
| 172 | struct DisplayImpl { |
| 173 | DisplayImpl() : dpy(EGL_NO_DISPLAY), config(0), |
| 174 | state(NOT_INITIALIZED), numConfigs(0) { } |
| 175 | EGLDisplay dpy; |
| 176 | EGLConfig* config; |
| 177 | EGLint state; |
| 178 | EGLint numConfigs; |
| 179 | strings_t queryString; |
| 180 | }; |
| 181 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 182 | uint32_t magic; |
| 183 | DisplayImpl disp[IMPL_NUM_IMPLEMENTATIONS]; |
| 184 | EGLint numTotalConfigs; |
| 185 | egl_config_t* configs; |
| 186 | uint32_t refs; |
| 187 | Mutex lock; |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 188 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 189 | egl_display_t() : magic('_dpy'), numTotalConfigs(0), configs(0) { } |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 190 | ~egl_display_t() { magic = 0; } |
| 191 | inline bool isValid() const { return magic == '_dpy'; } |
| 192 | inline bool isAlive() const { return isValid(); } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | }; |
| 194 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 195 | struct egl_surface_t : public egl_object_t |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 197 | typedef egl_object_t::LocalRef<egl_surface_t, EGLSurface> Ref; |
| 198 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 199 | egl_surface_t(EGLDisplay dpy, EGLSurface surface, EGLConfig config, |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 200 | int impl, egl_connection_t const* cnx) |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 201 | : dpy(dpy), surface(surface), config(config), impl(impl), cnx(cnx) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | } |
| 203 | ~egl_surface_t() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | } |
| 205 | EGLDisplay dpy; |
| 206 | EGLSurface surface; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 207 | EGLConfig config; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | int impl; |
| 209 | egl_connection_t const* cnx; |
| 210 | }; |
| 211 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 212 | struct egl_context_t : public egl_object_t |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 214 | typedef egl_object_t::LocalRef<egl_context_t, EGLContext> Ref; |
| 215 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 216 | egl_context_t(EGLDisplay dpy, EGLContext context, EGLConfig config, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 217 | int impl, egl_connection_t const* cnx, int version) |
| 218 | : dpy(dpy), context(context), read(0), draw(0), impl(impl), cnx(cnx), |
| 219 | version(version) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | { |
| 221 | } |
| 222 | EGLDisplay dpy; |
| 223 | EGLContext context; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 224 | EGLConfig config; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | EGLSurface read; |
| 226 | EGLSurface draw; |
| 227 | int impl; |
| 228 | egl_connection_t const* cnx; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 229 | int version; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | }; |
| 231 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 232 | struct egl_image_t : public egl_object_t |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 233 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 234 | typedef egl_object_t::LocalRef<egl_image_t, EGLImageKHR> Ref; |
| 235 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 236 | egl_image_t(EGLDisplay dpy, EGLContext context) |
| 237 | : dpy(dpy), context(context) |
| 238 | { |
| 239 | memset(images, 0, sizeof(images)); |
| 240 | } |
| 241 | EGLDisplay dpy; |
Mathias Agopian | 77fbf8d | 2010-09-09 11:12:54 -0700 | [diff] [blame] | 242 | EGLContext context; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 243 | EGLImageKHR images[IMPL_NUM_IMPLEMENTATIONS]; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 244 | }; |
| 245 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 246 | typedef egl_surface_t::Ref SurfaceRef; |
| 247 | typedef egl_context_t::Ref ContextRef; |
| 248 | typedef egl_image_t::Ref ImageRef; |
| 249 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | struct tls_t |
| 251 | { |
Mathias Agopian | d274eae | 2009-07-31 16:21:17 -0700 | [diff] [blame] | 252 | tls_t() : error(EGL_SUCCESS), ctx(0), logCallWithNoContext(EGL_TRUE) { } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | EGLint error; |
| 254 | EGLContext ctx; |
Mathias Agopian | d274eae | 2009-07-31 16:21:17 -0700 | [diff] [blame] | 255 | EGLBoolean logCallWithNoContext; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | }; |
| 257 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
| 259 | // ---------------------------------------------------------------------------- |
| 260 | |
Mathias Agopian | bf41b11 | 2010-04-09 13:37:34 -0700 | [diff] [blame] | 261 | static egl_connection_t gEGLImpl[IMPL_NUM_IMPLEMENTATIONS]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | static egl_display_t gDisplay[NUM_DISPLAYS]; |
| 263 | static pthread_mutex_t gThreadLocalStorageKeyMutex = PTHREAD_MUTEX_INITIALIZER; |
| 264 | static pthread_key_t gEGLThreadLocalStorageKey = -1; |
| 265 | |
| 266 | // ---------------------------------------------------------------------------- |
| 267 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 268 | EGLAPI gl_hooks_t gHooks[2][IMPL_NUM_IMPLEMENTATIONS]; |
| 269 | EGLAPI gl_hooks_t gHooksNoContext; |
Mathias Agopian | eccc8cf | 2009-05-13 00:19:22 -0700 | [diff] [blame] | 270 | EGLAPI pthread_key_t gGLWrapperKey = -1; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | |
| 272 | // ---------------------------------------------------------------------------- |
| 273 | |
| 274 | static __attribute__((noinline)) |
| 275 | const char *egl_strerror(EGLint err) |
| 276 | { |
| 277 | switch (err){ |
| 278 | case EGL_SUCCESS: return "EGL_SUCCESS"; |
| 279 | case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED"; |
| 280 | case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS"; |
| 281 | case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC"; |
| 282 | case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE"; |
| 283 | case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG"; |
| 284 | case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT"; |
| 285 | case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE"; |
| 286 | case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY"; |
| 287 | case EGL_BAD_MATCH: return "EGL_BAD_MATCH"; |
| 288 | case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP"; |
| 289 | case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW"; |
| 290 | case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER"; |
| 291 | case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE"; |
| 292 | case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST"; |
| 293 | default: return "UNKNOWN"; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static __attribute__((noinline)) |
| 298 | void clearTLS() { |
| 299 | if (gEGLThreadLocalStorageKey != -1) { |
| 300 | tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey); |
| 301 | if (tls) { |
| 302 | delete tls; |
| 303 | pthread_setspecific(gEGLThreadLocalStorageKey, 0); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static tls_t* getTLS() |
| 309 | { |
| 310 | tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey); |
| 311 | if (tls == 0) { |
| 312 | tls = new tls_t; |
| 313 | pthread_setspecific(gEGLThreadLocalStorageKey, tls); |
| 314 | } |
| 315 | return tls; |
| 316 | } |
| 317 | |
| 318 | template<typename T> |
| 319 | static __attribute__((noinline)) |
| 320 | T setErrorEtc(const char* caller, int line, EGLint error, T returnValue) { |
| 321 | if (gEGLThreadLocalStorageKey == -1) { |
| 322 | pthread_mutex_lock(&gThreadLocalStorageKeyMutex); |
| 323 | if (gEGLThreadLocalStorageKey == -1) |
| 324 | pthread_key_create(&gEGLThreadLocalStorageKey, NULL); |
| 325 | pthread_mutex_unlock(&gThreadLocalStorageKeyMutex); |
| 326 | } |
| 327 | tls_t* tls = getTLS(); |
| 328 | if (tls->error != error) { |
| 329 | LOGE("%s:%d error %x (%s)", caller, line, error, egl_strerror(error)); |
| 330 | tls->error = error; |
| 331 | } |
| 332 | return returnValue; |
| 333 | } |
| 334 | |
| 335 | static __attribute__((noinline)) |
| 336 | GLint getError() { |
| 337 | if (gEGLThreadLocalStorageKey == -1) |
| 338 | return EGL_SUCCESS; |
| 339 | tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey); |
| 340 | if (!tls) return EGL_SUCCESS; |
| 341 | GLint error = tls->error; |
| 342 | tls->error = EGL_SUCCESS; |
| 343 | return error; |
| 344 | } |
| 345 | |
| 346 | static __attribute__((noinline)) |
| 347 | void setContext(EGLContext ctx) { |
| 348 | if (gEGLThreadLocalStorageKey == -1) { |
| 349 | pthread_mutex_lock(&gThreadLocalStorageKeyMutex); |
| 350 | if (gEGLThreadLocalStorageKey == -1) |
| 351 | pthread_key_create(&gEGLThreadLocalStorageKey, NULL); |
| 352 | pthread_mutex_unlock(&gThreadLocalStorageKeyMutex); |
| 353 | } |
| 354 | tls_t* tls = getTLS(); |
| 355 | tls->ctx = ctx; |
| 356 | } |
| 357 | |
| 358 | static __attribute__((noinline)) |
| 359 | EGLContext getContext() { |
| 360 | if (gEGLThreadLocalStorageKey == -1) |
| 361 | return EGL_NO_CONTEXT; |
| 362 | tls_t* tls = (tls_t*)pthread_getspecific(gEGLThreadLocalStorageKey); |
| 363 | if (!tls) return EGL_NO_CONTEXT; |
| 364 | return tls->ctx; |
| 365 | } |
| 366 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | /*****************************************************************************/ |
| 368 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | template<typename T> |
| 370 | static __attribute__((noinline)) |
| 371 | int binarySearch( |
| 372 | T const sortedArray[], int first, int last, T key) |
| 373 | { |
| 374 | while (first <= last) { |
| 375 | int mid = (first + last) / 2; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 376 | if (sortedArray[mid] < key) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | first = mid + 1; |
| 378 | } else if (key < sortedArray[mid]) { |
| 379 | last = mid - 1; |
| 380 | } else { |
| 381 | return mid; |
| 382 | } |
| 383 | } |
| 384 | return -1; |
| 385 | } |
| 386 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | static int cmp_configs(const void* a, const void *b) |
| 388 | { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 389 | const egl_config_t& c0 = *(egl_config_t const *)a; |
| 390 | const egl_config_t& c1 = *(egl_config_t const *)b; |
| 391 | return c0<c1 ? -1 : (c1<c0 ? 1 : 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | struct extention_map_t { |
| 395 | const char* name; |
| 396 | __eglMustCastToProperFunctionPointerType address; |
| 397 | }; |
| 398 | |
| 399 | static const extention_map_t gExtentionMap[] = { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 400 | { "eglLockSurfaceKHR", |
| 401 | (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR }, |
| 402 | { "eglUnlockSurfaceKHR", |
| 403 | (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR }, |
| 404 | { "eglCreateImageKHR", |
| 405 | (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR }, |
| 406 | { "eglDestroyImageKHR", |
| 407 | (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR }, |
Mathias Agopian | 8d2e83b | 2009-06-24 22:37:39 -0700 | [diff] [blame] | 408 | { "eglSetSwapRectangleANDROID", |
| 409 | (__eglMustCastToProperFunctionPointerType)&eglSetSwapRectangleANDROID }, |
Mathias Agopian | 744026f | 2010-11-09 14:41:13 -0800 | [diff] [blame] | 410 | { "glEGLImageTargetTexture2DOES", |
| 411 | (__eglMustCastToProperFunctionPointerType)NULL }, |
| 412 | { "glEGLImageTargetRenderbufferStorageOES", |
| 413 | (__eglMustCastToProperFunctionPointerType)NULL }, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | }; |
| 415 | |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 416 | extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS]; |
| 417 | |
| 418 | // accesses protected by gInitDriverMutex |
| 419 | static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> gGLExtentionMap; |
| 420 | static int gGLExtentionSlot = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | |
| 422 | static void(*findProcAddress(const char* name, |
| 423 | const extention_map_t* map, size_t n))() |
| 424 | { |
| 425 | for (uint32_t i=0 ; i<n ; i++) { |
| 426 | if (!strcmp(name, map[i].name)) { |
| 427 | return map[i].address; |
| 428 | } |
| 429 | } |
| 430 | return NULL; |
| 431 | } |
| 432 | |
| 433 | // ---------------------------------------------------------------------------- |
| 434 | |
Mathias Agopian | 6f08712 | 2010-09-23 16:38:38 -0700 | [diff] [blame] | 435 | static int gl_no_context() { |
Mathias Agopian | d274eae | 2009-07-31 16:21:17 -0700 | [diff] [blame] | 436 | tls_t* tls = getTLS(); |
| 437 | if (tls->logCallWithNoContext == EGL_TRUE) { |
| 438 | tls->logCallWithNoContext = EGL_FALSE; |
| 439 | LOGE("call to OpenGL ES API with no current context " |
| 440 | "(logged once per thread)"); |
| 441 | } |
Mathias Agopian | 6f08712 | 2010-09-23 16:38:38 -0700 | [diff] [blame] | 442 | return 0; |
Mathias Agopian | 05c5311 | 2010-09-23 11:32:52 -0700 | [diff] [blame] | 443 | } |
| 444 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | static void early_egl_init(void) |
| 446 | { |
| 447 | #if !USE_FAST_TLS_KEY |
| 448 | pthread_key_create(&gGLWrapperKey, NULL); |
| 449 | #endif |
| 450 | uint32_t addr = (uint32_t)((void*)gl_no_context); |
| 451 | android_memset32( |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 452 | (uint32_t*)(void*)&gHooksNoContext, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | addr, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 454 | sizeof(gHooksNoContext)); |
Mathias Agopian | 05c5311 | 2010-09-23 11:32:52 -0700 | [diff] [blame] | 455 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 456 | setGlThreadSpecific(&gHooksNoContext); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static pthread_once_t once_control = PTHREAD_ONCE_INIT; |
| 460 | static int sEarlyInitState = pthread_once(&once_control, &early_egl_init); |
| 461 | |
| 462 | |
| 463 | static inline |
| 464 | egl_display_t* get_display(EGLDisplay dpy) |
| 465 | { |
| 466 | uintptr_t index = uintptr_t(dpy)-1U; |
| 467 | return (index >= NUM_DISPLAYS) ? NULL : &gDisplay[index]; |
| 468 | } |
| 469 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 470 | template<typename NATIVE, typename EGL> |
| 471 | static inline NATIVE* egl_to_native_cast(EGL arg) { |
| 472 | return reinterpret_cast<NATIVE*>(arg); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static inline |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 476 | egl_surface_t* get_surface(EGLSurface surface) { |
| 477 | return egl_to_native_cast<egl_surface_t>(surface); |
| 478 | } |
| 479 | |
| 480 | static inline |
| 481 | egl_context_t* get_context(EGLContext context) { |
| 482 | return egl_to_native_cast<egl_context_t>(context); |
| 483 | } |
| 484 | |
| 485 | static inline |
| 486 | egl_image_t* get_image(EGLImageKHR image) { |
| 487 | return egl_to_native_cast<egl_image_t>(image); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static egl_connection_t* validate_display_config( |
| 491 | EGLDisplay dpy, EGLConfig config, |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 492 | egl_display_t const*& dp) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | { |
| 494 | dp = get_display(dpy); |
| 495 | if (!dp) return setError(EGL_BAD_DISPLAY, (egl_connection_t*)NULL); |
| 496 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 497 | if (intptr_t(config) >= dp->numTotalConfigs) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL); |
| 499 | } |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 500 | egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(config)].impl]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | if (cnx->dso == 0) { |
| 502 | return setError(EGL_BAD_CONFIG, (egl_connection_t*)NULL); |
| 503 | } |
| 504 | return cnx; |
| 505 | } |
| 506 | |
| 507 | static EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx) |
| 508 | { |
| 509 | if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) |
| 510 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 511 | if (!get_display(dpy)->isAlive()) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 513 | if (!get_context(ctx)->isAlive()) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 515 | return EGL_TRUE; |
| 516 | } |
| 517 | |
| 518 | static EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface) |
| 519 | { |
| 520 | if ((uintptr_t(dpy)-1U) >= NUM_DISPLAYS) |
| 521 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 522 | if (!get_display(dpy)->isAlive()) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 524 | if (!get_surface(surface)->isAlive()) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 526 | return EGL_TRUE; |
| 527 | } |
| 528 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 529 | EGLImageKHR egl_get_image_for_current_context(EGLImageKHR image) |
| 530 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 531 | ImageRef _i(image); |
| 532 | if (!_i.get()) return EGL_NO_IMAGE_KHR; |
| 533 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 534 | EGLContext context = getContext(); |
| 535 | if (context == EGL_NO_CONTEXT || image == EGL_NO_IMAGE_KHR) |
| 536 | return EGL_NO_IMAGE_KHR; |
| 537 | |
| 538 | egl_context_t const * const c = get_context(context); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 539 | if (!c->isAlive()) |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 540 | return EGL_NO_IMAGE_KHR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 542 | egl_image_t const * const i = get_image(image); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 543 | return i->images[c->impl]; |
| 544 | } |
| 545 | |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 546 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 547 | |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 548 | // this mutex protects: |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 549 | // d->disp[] |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 550 | // egl_init_drivers_locked() |
| 551 | // |
| 552 | static pthread_mutex_t gInitDriverMutex = PTHREAD_MUTEX_INITIALIZER; |
| 553 | |
| 554 | EGLBoolean egl_init_drivers_locked() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | { |
| 556 | if (sEarlyInitState) { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 557 | // initialized by static ctor. should be set here. |
| 558 | return EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 561 | // get our driver loader |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 562 | Loader& loader(Loader::getInstance()); |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 563 | |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 564 | // dynamically load all our EGL implementations for all displays |
| 565 | // and retrieve the corresponding EGLDisplay |
| 566 | // if that fails, don't use this driver. |
| 567 | // TODO: currently we only deal with EGL_DEFAULT_DISPLAY |
| 568 | egl_connection_t* cnx; |
| 569 | egl_display_t* d = &gDisplay[0]; |
| 570 | |
| 571 | cnx = &gEGLImpl[IMPL_SOFTWARE]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | if (cnx->dso == 0) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 573 | cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_SOFTWARE]; |
| 574 | cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_SOFTWARE]; |
| 575 | cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 0, cnx); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 576 | if (cnx->dso) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 577 | EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 578 | LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for software EGL!"); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 579 | d->disp[IMPL_SOFTWARE].dpy = dpy; |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 580 | if (dpy == EGL_NO_DISPLAY) { |
| 581 | loader.close(cnx->dso); |
| 582 | cnx->dso = NULL; |
| 583 | } |
| 584 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | cnx = &gEGLImpl[IMPL_HARDWARE]; |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 588 | if (cnx->dso == 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | char value[PROPERTY_VALUE_MAX]; |
| 590 | property_get("debug.egl.hw", value, "1"); |
| 591 | if (atoi(value) != 0) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 592 | cnx->hooks[GLESv1_INDEX] = &gHooks[GLESv1_INDEX][IMPL_HARDWARE]; |
| 593 | cnx->hooks[GLESv2_INDEX] = &gHooks[GLESv2_INDEX][IMPL_HARDWARE]; |
| 594 | cnx->dso = loader.open(EGL_DEFAULT_DISPLAY, 1, cnx); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 595 | if (cnx->dso) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 596 | EGLDisplay dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 597 | LOGE_IF(dpy==EGL_NO_DISPLAY, "No EGLDisplay for hardware EGL!"); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 598 | d->disp[IMPL_HARDWARE].dpy = dpy; |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 599 | if (dpy == EGL_NO_DISPLAY) { |
| 600 | loader.close(cnx->dso); |
| 601 | cnx->dso = NULL; |
| 602 | } |
| 603 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | } else { |
| 605 | LOGD("3D hardware acceleration is disabled"); |
| 606 | } |
| 607 | } |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 608 | |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 609 | if (!gEGLImpl[IMPL_SOFTWARE].dso && !gEGLImpl[IMPL_HARDWARE].dso) { |
| 610 | return EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 613 | return EGL_TRUE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 616 | EGLBoolean egl_init_drivers() |
| 617 | { |
| 618 | EGLBoolean res; |
| 619 | pthread_mutex_lock(&gInitDriverMutex); |
| 620 | res = egl_init_drivers_locked(); |
| 621 | pthread_mutex_unlock(&gInitDriverMutex); |
| 622 | return res; |
| 623 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 624 | |
| 625 | // ---------------------------------------------------------------------------- |
| 626 | }; // namespace android |
| 627 | // ---------------------------------------------------------------------------- |
| 628 | |
| 629 | using namespace android; |
| 630 | |
| 631 | EGLDisplay eglGetDisplay(NativeDisplayType display) |
| 632 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 633 | uint32_t index = uint32_t(display); |
| 634 | if (index >= NUM_DISPLAYS) { |
| 635 | return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); |
| 636 | } |
| 637 | |
| 638 | if (egl_init_drivers() == EGL_FALSE) { |
| 639 | return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); |
| 640 | } |
| 641 | |
| 642 | EGLDisplay dpy = EGLDisplay(uintptr_t(display) + 1LU); |
| 643 | return dpy; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 644 | } |
| 645 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | // ---------------------------------------------------------------------------- |
| 647 | // Initialization |
| 648 | // ---------------------------------------------------------------------------- |
| 649 | |
| 650 | EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor) |
| 651 | { |
| 652 | egl_display_t * const dp = get_display(dpy); |
| 653 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 654 | |
Mathias Agopian | 75bc278 | 2010-02-05 16:17:01 -0800 | [diff] [blame] | 655 | Mutex::Autolock _l(dp->lock); |
| 656 | |
| 657 | if (dp->refs > 0) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | if (major != NULL) *major = VERSION_MAJOR; |
| 659 | if (minor != NULL) *minor = VERSION_MINOR; |
Jack Palevich | 81cd084 | 2010-03-15 20:45:21 -0700 | [diff] [blame] | 660 | dp->refs++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | return EGL_TRUE; |
| 662 | } |
| 663 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 664 | setGlThreadSpecific(&gHooksNoContext); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | |
| 666 | // initialize each EGL and |
| 667 | // build our own extension string first, based on the extension we know |
| 668 | // and the extension supported by our client implementation |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 669 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 671 | cnx->major = -1; |
| 672 | cnx->minor = -1; |
| 673 | if (!cnx->dso) |
| 674 | continue; |
| 675 | |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 676 | #if defined(ADRENO130) |
| 677 | #warning "Adreno-130 eglInitialize() workaround" |
| 678 | /* |
| 679 | * The ADRENO 130 driver returns a different EGLDisplay each time |
| 680 | * eglGetDisplay() is called, but also makes the EGLDisplay invalid |
| 681 | * after eglTerminate() has been called, so that eglInitialize() |
| 682 | * cannot be called again. Therefore, we need to make sure to call |
| 683 | * eglGetDisplay() before calling eglInitialize(); |
| 684 | */ |
| 685 | if (i == IMPL_HARDWARE) { |
| 686 | dp->disp[i].dpy = |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 687 | cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 688 | } |
| 689 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 691 | |
| 692 | EGLDisplay idpy = dp->disp[i].dpy; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 693 | if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | //LOGD("initialized %d dpy=%p, ver=%d.%d, cnx=%p", |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 695 | // i, idpy, cnx->major, cnx->minor, cnx); |
| 696 | |
| 697 | // display is now initialized |
| 698 | dp->disp[i].state = egl_display_t::INITIALIZED; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | |
| 700 | // get the query-strings for this display for each implementation |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 701 | dp->disp[i].queryString.vendor = |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 702 | cnx->egl.eglQueryString(idpy, EGL_VENDOR); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 703 | dp->disp[i].queryString.version = |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 704 | cnx->egl.eglQueryString(idpy, EGL_VERSION); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 705 | dp->disp[i].queryString.extensions = |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 706 | cnx->egl.eglQueryString(idpy, EGL_EXTENSIONS); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 707 | dp->disp[i].queryString.clientApi = |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 708 | cnx->egl.eglQueryString(idpy, EGL_CLIENT_APIS); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | |
| 710 | } else { |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 711 | LOGW("%d: eglInitialize(%p) failed (%s)", i, idpy, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 712 | egl_strerror(cnx->egl.eglGetError())); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
| 716 | EGLBoolean res = EGL_FALSE; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 717 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 719 | if (cnx->dso && cnx->major>=0 && cnx->minor>=0) { |
| 720 | EGLint n; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 721 | if (cnx->egl.eglGetConfigs(dp->disp[i].dpy, 0, 0, &n)) { |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 722 | dp->disp[i].config = (EGLConfig*)malloc(sizeof(EGLConfig)*n); |
| 723 | if (dp->disp[i].config) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 724 | if (cnx->egl.eglGetConfigs( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 725 | dp->disp[i].dpy, dp->disp[i].config, n, |
| 726 | &dp->disp[i].numConfigs)) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | dp->numTotalConfigs += n; |
| 729 | res = EGL_TRUE; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | if (res == EGL_TRUE) { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 737 | dp->configs = new egl_config_t[ dp->numTotalConfigs ]; |
| 738 | for (int i=0, k=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 739 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 740 | if (cnx->dso && cnx->major>=0 && cnx->minor>=0) { |
| 741 | for (int j=0 ; j<dp->disp[i].numConfigs ; j++) { |
| 742 | dp->configs[k].impl = i; |
| 743 | dp->configs[k].config = dp->disp[i].config[j]; |
| 744 | dp->configs[k].configId = k + 1; // CONFIG_ID start at 1 |
| 745 | // store the implementation's CONFIG_ID |
| 746 | cnx->egl.eglGetConfigAttrib( |
| 747 | dp->disp[i].dpy, |
| 748 | dp->disp[i].config[j], |
| 749 | EGL_CONFIG_ID, |
| 750 | &dp->configs[k].implConfigId); |
| 751 | k++; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // sort our configurations so we can do binary-searches |
| 757 | qsort( dp->configs, |
| 758 | dp->numTotalConfigs, |
| 759 | sizeof(egl_config_t), cmp_configs); |
| 760 | |
Mathias Agopian | 75bc278 | 2010-02-05 16:17:01 -0800 | [diff] [blame] | 761 | dp->refs++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | if (major != NULL) *major = VERSION_MAJOR; |
| 763 | if (minor != NULL) *minor = VERSION_MINOR; |
| 764 | return EGL_TRUE; |
| 765 | } |
| 766 | return setError(EGL_NOT_INITIALIZED, EGL_FALSE); |
| 767 | } |
| 768 | |
| 769 | EGLBoolean eglTerminate(EGLDisplay dpy) |
| 770 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 771 | // NOTE: don't unload the drivers b/c some APIs can be called |
| 772 | // after eglTerminate() has been called. eglTerminate() only |
| 773 | // terminates an EGLDisplay, not a EGL itself. |
| 774 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | egl_display_t* const dp = get_display(dpy); |
| 776 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
Mathias Agopian | 75bc278 | 2010-02-05 16:17:01 -0800 | [diff] [blame] | 777 | |
| 778 | Mutex::Autolock _l(dp->lock); |
| 779 | |
| 780 | if (dp->refs == 0) { |
| 781 | return setError(EGL_NOT_INITIALIZED, EGL_FALSE); |
| 782 | } |
| 783 | |
| 784 | // this is specific to Android, display termination is ref-counted. |
Jack Palevich | 81cd084 | 2010-03-15 20:45:21 -0700 | [diff] [blame] | 785 | if (dp->refs > 1) { |
| 786 | dp->refs--; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | return EGL_TRUE; |
Jack Palevich | 81cd084 | 2010-03-15 20:45:21 -0700 | [diff] [blame] | 788 | } |
Mathias Agopian | de58697 | 2009-05-28 17:39:03 -0700 | [diff] [blame] | 789 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 790 | EGLBoolean res = EGL_FALSE; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 791 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | egl_connection_t* const cnx = &gEGLImpl[i]; |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 793 | if (cnx->dso && dp->disp[i].state == egl_display_t::INITIALIZED) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 794 | if (cnx->egl.eglTerminate(dp->disp[i].dpy) == EGL_FALSE) { |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 795 | LOGW("%d: eglTerminate(%p) failed (%s)", i, dp->disp[i].dpy, |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 796 | egl_strerror(cnx->egl.eglGetError())); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 797 | } |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 798 | // REVISIT: it's unclear what to do if eglTerminate() fails |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 799 | free(dp->disp[i].config); |
| 800 | |
| 801 | dp->disp[i].numConfigs = 0; |
| 802 | dp->disp[i].config = 0; |
| 803 | dp->disp[i].state = egl_display_t::TERMINATED; |
| 804 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | res = EGL_TRUE; |
| 806 | } |
| 807 | } |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 808 | |
| 809 | // TODO: all egl_object_t should be marked for termination |
| 810 | |
Mathias Agopian | 75bc278 | 2010-02-05 16:17:01 -0800 | [diff] [blame] | 811 | dp->refs--; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 812 | dp->numTotalConfigs = 0; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 813 | delete [] dp->configs; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | clearTLS(); |
| 815 | return res; |
| 816 | } |
| 817 | |
| 818 | // ---------------------------------------------------------------------------- |
| 819 | // configuration |
| 820 | // ---------------------------------------------------------------------------- |
| 821 | |
| 822 | EGLBoolean eglGetConfigs( EGLDisplay dpy, |
| 823 | EGLConfig *configs, |
| 824 | EGLint config_size, EGLint *num_config) |
| 825 | { |
| 826 | egl_display_t const * const dp = get_display(dpy); |
| 827 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 828 | |
| 829 | GLint numConfigs = dp->numTotalConfigs; |
| 830 | if (!configs) { |
| 831 | *num_config = numConfigs; |
| 832 | return EGL_TRUE; |
| 833 | } |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 834 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | GLint n = 0; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 836 | for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) { |
| 837 | *configs++ = EGLConfig(i); |
| 838 | config_size--; |
| 839 | n++; |
| 840 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | |
| 842 | *num_config = n; |
| 843 | return EGL_TRUE; |
| 844 | } |
| 845 | |
| 846 | EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list, |
| 847 | EGLConfig *configs, EGLint config_size, |
| 848 | EGLint *num_config) |
| 849 | { |
| 850 | egl_display_t const * const dp = get_display(dpy); |
| 851 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 852 | |
Jack Palevich | 749c63d | 2009-03-25 15:12:17 -0700 | [diff] [blame] | 853 | if (num_config==0) { |
| 854 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | EGLint n; |
| 858 | EGLBoolean res = EGL_FALSE; |
| 859 | *num_config = 0; |
| 860 | |
| 861 | |
| 862 | // It is unfortunate, but we need to remap the EGL_CONFIG_IDs, |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 863 | // to do this, we have to go through the attrib_list array once |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | // to figure out both its size and if it contains an EGL_CONFIG_ID |
| 865 | // key. If so, the full array is copied and patched. |
| 866 | // NOTE: we assume that there can be only one occurrence |
| 867 | // of EGL_CONFIG_ID. |
| 868 | |
| 869 | EGLint patch_index = -1; |
| 870 | GLint attr; |
| 871 | size_t size = 0; |
Mathias Agopian | 04aed21 | 2010-05-17 14:45:43 -0700 | [diff] [blame] | 872 | if (attrib_list) { |
| 873 | while ((attr=attrib_list[size]) != EGL_NONE) { |
| 874 | if (attr == EGL_CONFIG_ID) |
| 875 | patch_index = size; |
| 876 | size += 2; |
| 877 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | } |
| 879 | if (patch_index >= 0) { |
| 880 | size += 2; // we need copy the sentinel as well |
| 881 | EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint)); |
| 882 | if (new_list == 0) |
| 883 | return setError(EGL_BAD_ALLOC, EGL_FALSE); |
| 884 | memcpy(new_list, attrib_list, size*sizeof(EGLint)); |
| 885 | |
| 886 | // patch the requested EGL_CONFIG_ID |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 887 | bool found = false; |
| 888 | EGLConfig ourConfig(0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | EGLint& configId(new_list[patch_index+1]); |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 890 | for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) { |
| 891 | if (dp->configs[i].configId == configId) { |
| 892 | ourConfig = EGLConfig(i); |
| 893 | configId = dp->configs[i].implConfigId; |
| 894 | found = true; |
| 895 | break; |
| 896 | } |
| 897 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 899 | egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl]; |
| 900 | if (found && cnx->dso) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | // and switch to the new list |
| 902 | attrib_list = const_cast<const EGLint *>(new_list); |
| 903 | |
| 904 | // At this point, the only configuration that can match is |
| 905 | // dp->configs[i][index], however, we don't know if it would be |
| 906 | // rejected because of the other attributes, so we do have to call |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 907 | // cnx->egl.eglChooseConfig() -- but we don't have to loop |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | // through all the EGLimpl[]. |
| 909 | // We also know we can only get a single config back, and we know |
| 910 | // which one. |
| 911 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 912 | res = cnx->egl.eglChooseConfig( |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 913 | dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy, |
| 914 | attrib_list, configs, config_size, &n); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | if (res && n>0) { |
| 916 | // n has to be 0 or 1, by construction, and we already know |
| 917 | // which config it will return (since there can be only one). |
Jack Palevich | 749c63d | 2009-03-25 15:12:17 -0700 | [diff] [blame] | 918 | if (configs) { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 919 | configs[0] = ourConfig; |
Jack Palevich | 749c63d | 2009-03-25 15:12:17 -0700 | [diff] [blame] | 920 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | *num_config = 1; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | free(const_cast<EGLint *>(attrib_list)); |
| 926 | return res; |
| 927 | } |
| 928 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 929 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 930 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 932 | if (cnx->dso) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 933 | if (cnx->egl.eglChooseConfig( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 934 | dp->disp[i].dpy, attrib_list, configs, config_size, &n)) { |
Jack Palevich | 749c63d | 2009-03-25 15:12:17 -0700 | [diff] [blame] | 935 | if (configs) { |
| 936 | // now we need to convert these client EGLConfig to our |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 937 | // internal EGLConfig format. |
| 938 | // This is done in O(n Log(n)) time. |
Jack Palevich | 749c63d | 2009-03-25 15:12:17 -0700 | [diff] [blame] | 939 | for (int j=0 ; j<n ; j++) { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 940 | egl_config_t key(i, configs[j]); |
| 941 | intptr_t index = binarySearch<egl_config_t>( |
| 942 | dp->configs, 0, dp->numTotalConfigs, key); |
Jack Palevich | 749c63d | 2009-03-25 15:12:17 -0700 | [diff] [blame] | 943 | if (index >= 0) { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 944 | configs[j] = EGLConfig(index); |
Jack Palevich | 749c63d | 2009-03-25 15:12:17 -0700 | [diff] [blame] | 945 | } else { |
| 946 | return setError(EGL_BAD_CONFIG, EGL_FALSE); |
| 947 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | } |
Jack Palevich | 749c63d | 2009-03-25 15:12:17 -0700 | [diff] [blame] | 949 | configs += n; |
| 950 | config_size -= n; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | *num_config += n; |
| 953 | res = EGL_TRUE; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | return res; |
| 958 | } |
| 959 | |
| 960 | EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, |
| 961 | EGLint attribute, EGLint *value) |
| 962 | { |
| 963 | egl_display_t const* dp = 0; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 964 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | if (!cnx) return EGL_FALSE; |
| 966 | |
| 967 | if (attribute == EGL_CONFIG_ID) { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 968 | *value = dp->configs[intptr_t(config)].configId; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | return EGL_TRUE; |
| 970 | } |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 971 | return cnx->egl.eglGetConfigAttrib( |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 972 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 973 | dp->configs[intptr_t(config)].config, attribute, value); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | // ---------------------------------------------------------------------------- |
| 977 | // surfaces |
| 978 | // ---------------------------------------------------------------------------- |
| 979 | |
| 980 | EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config, |
| 981 | NativeWindowType window, |
| 982 | const EGLint *attrib_list) |
| 983 | { |
| 984 | egl_display_t const* dp = 0; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 985 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | if (cnx) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 987 | EGLSurface surface = cnx->egl.eglCreateWindowSurface( |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 988 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 989 | dp->configs[intptr_t(config)].config, window, attrib_list); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | if (surface != EGL_NO_SURFACE) { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 991 | egl_surface_t* s = new egl_surface_t(dpy, surface, config, |
| 992 | dp->configs[intptr_t(config)].impl, cnx); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | return s; |
| 994 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | } |
| 996 | return EGL_NO_SURFACE; |
| 997 | } |
| 998 | |
| 999 | EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config, |
| 1000 | NativePixmapType pixmap, |
| 1001 | const EGLint *attrib_list) |
| 1002 | { |
| 1003 | egl_display_t const* dp = 0; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1004 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | if (cnx) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1006 | EGLSurface surface = cnx->egl.eglCreatePixmapSurface( |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1007 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 1008 | dp->configs[intptr_t(config)].config, pixmap, attrib_list); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | if (surface != EGL_NO_SURFACE) { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1010 | egl_surface_t* s = new egl_surface_t(dpy, surface, config, |
| 1011 | dp->configs[intptr_t(config)].impl, cnx); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | return s; |
| 1013 | } |
| 1014 | } |
| 1015 | return EGL_NO_SURFACE; |
| 1016 | } |
| 1017 | |
| 1018 | EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config, |
| 1019 | const EGLint *attrib_list) |
| 1020 | { |
| 1021 | egl_display_t const* dp = 0; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1022 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | if (cnx) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1024 | EGLSurface surface = cnx->egl.eglCreatePbufferSurface( |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1025 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 1026 | dp->configs[intptr_t(config)].config, attrib_list); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | if (surface != EGL_NO_SURFACE) { |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1028 | egl_surface_t* s = new egl_surface_t(dpy, surface, config, |
| 1029 | dp->configs[intptr_t(config)].impl, cnx); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | return s; |
| 1031 | } |
| 1032 | } |
| 1033 | return EGL_NO_SURFACE; |
| 1034 | } |
| 1035 | |
| 1036 | EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface) |
| 1037 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1038 | SurfaceRef _s(surface); |
| 1039 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1040 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | if (!validate_display_surface(dpy, surface)) |
| 1042 | return EGL_FALSE; |
| 1043 | egl_display_t const * const dp = get_display(dpy); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1045 | egl_surface_t * const s = get_surface(surface); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1046 | EGLBoolean result = s->cnx->egl.eglDestroySurface( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1047 | dp->disp[s->impl].dpy, s->surface); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1048 | if (result == EGL_TRUE) { |
| 1049 | _s.terminate(); |
| 1050 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | return result; |
| 1052 | } |
| 1053 | |
| 1054 | EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface, |
| 1055 | EGLint attribute, EGLint *value) |
| 1056 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1057 | SurfaceRef _s(surface); |
| 1058 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1059 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | if (!validate_display_surface(dpy, surface)) |
| 1061 | return EGL_FALSE; |
| 1062 | egl_display_t const * const dp = get_display(dpy); |
| 1063 | egl_surface_t const * const s = get_surface(surface); |
| 1064 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1065 | EGLBoolean result(EGL_TRUE); |
| 1066 | if (attribute == EGL_CONFIG_ID) { |
| 1067 | // We need to remap EGL_CONFIG_IDs |
| 1068 | *value = dp->configs[intptr_t(s->config)].configId; |
| 1069 | } else { |
| 1070 | result = s->cnx->egl.eglQuerySurface( |
| 1071 | dp->disp[s->impl].dpy, s->surface, attribute, value); |
| 1072 | } |
| 1073 | |
| 1074 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | // ---------------------------------------------------------------------------- |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1078 | // Contexts |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | // ---------------------------------------------------------------------------- |
| 1080 | |
| 1081 | EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, |
| 1082 | EGLContext share_list, const EGLint *attrib_list) |
| 1083 | { |
| 1084 | egl_display_t const* dp = 0; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1085 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | if (cnx) { |
Jamie Gennis | 4c39f8f | 2010-07-02 11:39:12 -0700 | [diff] [blame] | 1087 | if (share_list != EGL_NO_CONTEXT) { |
| 1088 | egl_context_t* const c = get_context(share_list); |
| 1089 | share_list = c->context; |
| 1090 | } |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1091 | EGLContext context = cnx->egl.eglCreateContext( |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1092 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 1093 | dp->configs[intptr_t(config)].config, |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1094 | share_list, attrib_list); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1095 | if (context != EGL_NO_CONTEXT) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1096 | // figure out if it's a GLESv1 or GLESv2 |
| 1097 | int version = 0; |
| 1098 | if (attrib_list) { |
| 1099 | while (*attrib_list != EGL_NONE) { |
| 1100 | GLint attr = *attrib_list++; |
| 1101 | GLint value = *attrib_list++; |
| 1102 | if (attr == EGL_CONTEXT_CLIENT_VERSION) { |
| 1103 | if (value == 1) { |
| 1104 | version = GLESv1_INDEX; |
| 1105 | } else if (value == 2) { |
| 1106 | version = GLESv2_INDEX; |
| 1107 | } |
| 1108 | } |
| 1109 | }; |
| 1110 | } |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1111 | egl_context_t* c = new egl_context_t(dpy, context, config, |
| 1112 | dp->configs[intptr_t(config)].impl, cnx, version); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | return c; |
| 1114 | } |
| 1115 | } |
| 1116 | return EGL_NO_CONTEXT; |
| 1117 | } |
| 1118 | |
| 1119 | EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx) |
| 1120 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1121 | ContextRef _c(ctx); |
| 1122 | if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1123 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1124 | if (!validate_display_context(dpy, ctx)) |
| 1125 | return EGL_FALSE; |
| 1126 | egl_display_t const * const dp = get_display(dpy); |
| 1127 | egl_context_t * const c = get_context(ctx); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1128 | EGLBoolean result = c->cnx->egl.eglDestroyContext( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1129 | dp->disp[c->impl].dpy, c->context); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1130 | if (result == EGL_TRUE) { |
| 1131 | _c.terminate(); |
| 1132 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | return result; |
| 1134 | } |
| 1135 | |
| 1136 | EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw, |
| 1137 | EGLSurface read, EGLContext ctx) |
| 1138 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1139 | // get a reference to the object passed in |
| 1140 | ContextRef _c(ctx); |
| 1141 | SurfaceRef _d(draw); |
| 1142 | SurfaceRef _r(read); |
| 1143 | |
| 1144 | // validate the display and the context (if not EGL_NO_CONTEXT) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | egl_display_t const * const dp = get_display(dpy); |
| 1146 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1147 | if ((ctx != EGL_NO_CONTEXT) && (!validate_display_context(dpy, ctx))) { |
| 1148 | // EGL_NO_CONTEXT is valid |
| 1149 | return EGL_FALSE; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | } |
| 1151 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1152 | // these are the underlying implementation's object |
| 1153 | EGLContext impl_ctx = EGL_NO_CONTEXT; |
Mathias Agopian | af74213 | 2009-06-25 00:01:11 -0700 | [diff] [blame] | 1154 | EGLSurface impl_draw = EGL_NO_SURFACE; |
| 1155 | EGLSurface impl_read = EGL_NO_SURFACE; |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1156 | |
| 1157 | // these are our objects structs passed in |
| 1158 | egl_context_t * c = NULL; |
| 1159 | egl_surface_t const * d = NULL; |
| 1160 | egl_surface_t const * r = NULL; |
| 1161 | |
| 1162 | // these are the current objects structs |
| 1163 | egl_context_t * cur_c = get_context(getContext()); |
| 1164 | egl_surface_t * cur_r = NULL; |
| 1165 | egl_surface_t * cur_d = NULL; |
| 1166 | |
| 1167 | if (ctx != EGL_NO_CONTEXT) { |
| 1168 | c = get_context(ctx); |
| 1169 | cur_r = get_surface(c->read); |
| 1170 | cur_d = get_surface(c->draw); |
| 1171 | impl_ctx = c->context; |
| 1172 | } else { |
| 1173 | // no context given, use the implementation of the current context |
| 1174 | if (cur_c == NULL) { |
| 1175 | // no current context |
| 1176 | if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) { |
Mathias Agopian | 8063c3a | 2010-01-25 11:30:11 -0800 | [diff] [blame] | 1177 | // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT); |
| 1178 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1179 | } |
Mathias Agopian | 8063c3a | 2010-01-25 11:30:11 -0800 | [diff] [blame] | 1180 | // not an error, there is just no current context. |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1181 | return EGL_TRUE; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | // retrieve the underlying implementation's draw EGLSurface |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | if (draw != EGL_NO_SURFACE) { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1187 | d = get_surface(draw); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1188 | // make sure the EGLContext and EGLSurface passed in are for |
| 1189 | // the same driver |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1190 | if (c && d->impl != c->impl) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
Mathias Agopian | af74213 | 2009-06-25 00:01:11 -0700 | [diff] [blame] | 1192 | impl_draw = d->surface; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | } |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1194 | |
| 1195 | // retrieve the underlying implementation's read EGLSurface |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | if (read != EGL_NO_SURFACE) { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1197 | r = get_surface(read); |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1198 | // make sure the EGLContext and EGLSurface passed in are for |
| 1199 | // the same driver |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1200 | if (c && r->impl != c->impl) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
Mathias Agopian | af74213 | 2009-06-25 00:01:11 -0700 | [diff] [blame] | 1202 | impl_read = r->surface; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | } |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1204 | |
| 1205 | EGLBoolean result; |
| 1206 | |
| 1207 | if (c) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1208 | result = c->cnx->egl.eglMakeCurrent( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1209 | dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1210 | } else { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1211 | result = cur_c->cnx->egl.eglMakeCurrent( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1212 | dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1213 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | |
| 1215 | if (result == EGL_TRUE) { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1216 | // by construction, these are either 0 or valid (possibly terminated) |
| 1217 | // it should be impossible for these to be invalid |
| 1218 | ContextRef _cur_c(cur_c); |
| 1219 | SurfaceRef _cur_r(cur_r); |
| 1220 | SurfaceRef _cur_d(cur_d); |
| 1221 | |
| 1222 | // cur_c has to be valid here (but could be terminated) |
| 1223 | if (ctx != EGL_NO_CONTEXT) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1224 | setGlThreadSpecific(c->cnx->hooks[c->version]); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1225 | setContext(ctx); |
| 1226 | _c.acquire(); |
| 1227 | } else { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1228 | setGlThreadSpecific(&gHooksNoContext); |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1229 | setContext(EGL_NO_CONTEXT); |
| 1230 | } |
| 1231 | _cur_c.release(); |
| 1232 | |
| 1233 | _r.acquire(); |
| 1234 | _cur_r.release(); |
| 1235 | if (c) c->read = read; |
| 1236 | |
| 1237 | _d.acquire(); |
| 1238 | _cur_d.release(); |
| 1239 | if (c) c->draw = draw; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 | } |
| 1241 | return result; |
| 1242 | } |
| 1243 | |
| 1244 | |
| 1245 | EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx, |
| 1246 | EGLint attribute, EGLint *value) |
| 1247 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1248 | ContextRef _c(ctx); |
| 1249 | if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1250 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | if (!validate_display_context(dpy, ctx)) |
| 1252 | return EGL_FALSE; |
| 1253 | |
| 1254 | egl_display_t const * const dp = get_display(dpy); |
| 1255 | egl_context_t * const c = get_context(ctx); |
| 1256 | |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1257 | EGLBoolean result(EGL_TRUE); |
| 1258 | if (attribute == EGL_CONFIG_ID) { |
| 1259 | *value = dp->configs[intptr_t(c->config)].configId; |
| 1260 | } else { |
| 1261 | // We need to remap EGL_CONFIG_IDs |
| 1262 | result = c->cnx->egl.eglQueryContext( |
| 1263 | dp->disp[c->impl].dpy, c->context, attribute, value); |
| 1264 | } |
| 1265 | |
| 1266 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | EGLContext eglGetCurrentContext(void) |
| 1270 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1271 | // could be called before eglInitialize(), but we wouldn't have a context |
| 1272 | // then, and this function would correctly return EGL_NO_CONTEXT. |
| 1273 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | EGLContext ctx = getContext(); |
| 1275 | return ctx; |
| 1276 | } |
| 1277 | |
| 1278 | EGLSurface eglGetCurrentSurface(EGLint readdraw) |
| 1279 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1280 | // could be called before eglInitialize(), but we wouldn't have a context |
| 1281 | // then, and this function would correctly return EGL_NO_SURFACE. |
| 1282 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | EGLContext ctx = getContext(); |
| 1284 | if (ctx) { |
| 1285 | egl_context_t const * const c = get_context(ctx); |
| 1286 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); |
| 1287 | switch (readdraw) { |
| 1288 | case EGL_READ: return c->read; |
| 1289 | case EGL_DRAW: return c->draw; |
| 1290 | default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
| 1291 | } |
| 1292 | } |
| 1293 | return EGL_NO_SURFACE; |
| 1294 | } |
| 1295 | |
| 1296 | EGLDisplay eglGetCurrentDisplay(void) |
| 1297 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1298 | // could be called before eglInitialize(), but we wouldn't have a context |
| 1299 | // then, and this function would correctly return EGL_NO_DISPLAY. |
| 1300 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | EGLContext ctx = getContext(); |
| 1302 | if (ctx) { |
| 1303 | egl_context_t const * const c = get_context(ctx); |
| 1304 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); |
| 1305 | return c->dpy; |
| 1306 | } |
| 1307 | return EGL_NO_DISPLAY; |
| 1308 | } |
| 1309 | |
| 1310 | EGLBoolean eglWaitGL(void) |
| 1311 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1312 | // could be called before eglInitialize(), but we wouldn't have a context |
| 1313 | // then, and this function would return GL_TRUE, which isn't wrong. |
| 1314 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | EGLBoolean res = EGL_TRUE; |
| 1316 | EGLContext ctx = getContext(); |
| 1317 | if (ctx) { |
| 1318 | egl_context_t const * const c = get_context(ctx); |
| 1319 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1320 | if (uint32_t(c->impl)>=2) |
| 1321 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1322 | egl_connection_t* const cnx = &gEGLImpl[c->impl]; |
| 1323 | if (!cnx->dso) |
| 1324 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1325 | res = cnx->egl.eglWaitGL(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | } |
| 1327 | return res; |
| 1328 | } |
| 1329 | |
| 1330 | EGLBoolean eglWaitNative(EGLint engine) |
| 1331 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1332 | // could be called before eglInitialize(), but we wouldn't have a context |
| 1333 | // then, and this function would return GL_TRUE, which isn't wrong. |
| 1334 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | EGLBoolean res = EGL_TRUE; |
| 1336 | EGLContext ctx = getContext(); |
| 1337 | if (ctx) { |
| 1338 | egl_context_t const * const c = get_context(ctx); |
| 1339 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1340 | if (uint32_t(c->impl)>=2) |
| 1341 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1342 | egl_connection_t* const cnx = &gEGLImpl[c->impl]; |
| 1343 | if (!cnx->dso) |
| 1344 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1345 | res = cnx->egl.eglWaitNative(engine); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | } |
| 1347 | return res; |
| 1348 | } |
| 1349 | |
| 1350 | EGLint eglGetError(void) |
| 1351 | { |
| 1352 | EGLint result = EGL_SUCCESS; |
Mathias Agopian | 02dafb5 | 2010-09-21 15:43:59 -0700 | [diff] [blame] | 1353 | EGLint err; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1354 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
Mathias Agopian | 02dafb5 | 2010-09-21 15:43:59 -0700 | [diff] [blame] | 1355 | err = EGL_SUCCESS; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1356 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1357 | if (cnx->dso) |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1358 | err = cnx->egl.eglGetError(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | if (err!=EGL_SUCCESS && result==EGL_SUCCESS) |
| 1360 | result = err; |
| 1361 | } |
Mathias Agopian | 02dafb5 | 2010-09-21 15:43:59 -0700 | [diff] [blame] | 1362 | err = getError(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | if (result == EGL_SUCCESS) |
Mathias Agopian | 02dafb5 | 2010-09-21 15:43:59 -0700 | [diff] [blame] | 1364 | result = err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | return result; |
| 1366 | } |
| 1367 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1368 | __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1370 | // eglGetProcAddress() could be the very first function called |
| 1371 | // in which case we must make sure we've initialized ourselves, this |
| 1372 | // happens the first time egl_get_display() is called. |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1373 | |
| 1374 | if (egl_init_drivers() == EGL_FALSE) { |
| 1375 | setError(EGL_BAD_PARAMETER, NULL); |
| 1376 | return NULL; |
| 1377 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1378 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1379 | __eglMustCastToProperFunctionPointerType addr; |
| 1380 | addr = findProcAddress(procname, gExtentionMap, NELEM(gExtentionMap)); |
| 1381 | if (addr) return addr; |
| 1382 | |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 1383 | // this protects accesses to gGLExtentionMap and gGLExtentionSlot |
| 1384 | pthread_mutex_lock(&gInitDriverMutex); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1385 | |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 1386 | /* |
| 1387 | * Since eglGetProcAddress() is not associated to anything, it needs |
| 1388 | * to return a function pointer that "works" regardless of what |
| 1389 | * the current context is. |
| 1390 | * |
| 1391 | * For this reason, we return a "forwarder", a small stub that takes |
| 1392 | * care of calling the function associated with the context |
| 1393 | * currently bound. |
| 1394 | * |
| 1395 | * We first look for extensions we've already resolved, if we're seeing |
| 1396 | * this extension for the first time, we go through all our |
| 1397 | * implementations and call eglGetProcAddress() and record the |
| 1398 | * result in the appropriate implementation hooks and return the |
| 1399 | * address of the forwarder corresponding to that hook set. |
| 1400 | * |
| 1401 | */ |
| 1402 | |
| 1403 | const String8 name(procname); |
| 1404 | addr = gGLExtentionMap.valueFor(name); |
| 1405 | const int slot = gGLExtentionSlot; |
| 1406 | |
| 1407 | LOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS, |
| 1408 | "no more slots for eglGetProcAddress(\"%s\")", |
| 1409 | procname); |
| 1410 | |
| 1411 | if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) { |
| 1412 | bool found = false; |
| 1413 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 1414 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1415 | if (cnx->dso && cnx->egl.eglGetProcAddress) { |
| 1416 | found = true; |
Mathias Agopian | 4a88b52 | 2010-08-13 12:19:04 -0700 | [diff] [blame] | 1417 | // Extensions are independent of the bound context |
| 1418 | cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] = |
| 1419 | cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] = |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 1420 | cnx->egl.eglGetProcAddress(procname); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | } |
| 1422 | } |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 1423 | if (found) { |
| 1424 | addr = gExtensionForwarders[slot]; |
| 1425 | gGLExtentionMap.add(name, addr); |
| 1426 | gGLExtentionSlot++; |
| 1427 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 1430 | pthread_mutex_unlock(&gInitDriverMutex); |
Mathias Agopian | 2403533 | 2010-08-02 17:34:32 -0700 | [diff] [blame] | 1431 | return addr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) |
| 1435 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1436 | SurfaceRef _s(draw); |
| 1437 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1438 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | if (!validate_display_surface(dpy, draw)) |
| 1440 | return EGL_FALSE; |
| 1441 | egl_display_t const * const dp = get_display(dpy); |
| 1442 | egl_surface_t const * const s = get_surface(draw); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1443 | return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface, |
| 1447 | NativePixmapType target) |
| 1448 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1449 | SurfaceRef _s(surface); |
| 1450 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1451 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | if (!validate_display_surface(dpy, surface)) |
| 1453 | return EGL_FALSE; |
| 1454 | egl_display_t const * const dp = get_display(dpy); |
| 1455 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1456 | return s->cnx->egl.eglCopyBuffers( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1457 | dp->disp[s->impl].dpy, s->surface, target); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | const char* eglQueryString(EGLDisplay dpy, EGLint name) |
| 1461 | { |
| 1462 | egl_display_t const * const dp = get_display(dpy); |
| 1463 | switch (name) { |
| 1464 | case EGL_VENDOR: |
| 1465 | return gVendorString; |
| 1466 | case EGL_VERSION: |
| 1467 | return gVersionString; |
| 1468 | case EGL_EXTENSIONS: |
| 1469 | return gExtensionString; |
| 1470 | case EGL_CLIENT_APIS: |
| 1471 | return gClientApiString; |
| 1472 | } |
| 1473 | return setError(EGL_BAD_PARAMETER, (const char *)0); |
| 1474 | } |
| 1475 | |
| 1476 | |
| 1477 | // ---------------------------------------------------------------------------- |
| 1478 | // EGL 1.1 |
| 1479 | // ---------------------------------------------------------------------------- |
| 1480 | |
| 1481 | EGLBoolean eglSurfaceAttrib( |
| 1482 | EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value) |
| 1483 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1484 | SurfaceRef _s(surface); |
| 1485 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1486 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | if (!validate_display_surface(dpy, surface)) |
| 1488 | return EGL_FALSE; |
| 1489 | egl_display_t const * const dp = get_display(dpy); |
| 1490 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1491 | if (s->cnx->egl.eglSurfaceAttrib) { |
| 1492 | return s->cnx->egl.eglSurfaceAttrib( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1493 | dp->disp[s->impl].dpy, s->surface, attribute, value); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | } |
| 1495 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1496 | } |
| 1497 | |
| 1498 | EGLBoolean eglBindTexImage( |
| 1499 | EGLDisplay dpy, EGLSurface surface, EGLint buffer) |
| 1500 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1501 | SurfaceRef _s(surface); |
| 1502 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1503 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | if (!validate_display_surface(dpy, surface)) |
| 1505 | return EGL_FALSE; |
| 1506 | egl_display_t const * const dp = get_display(dpy); |
| 1507 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1508 | if (s->cnx->egl.eglBindTexImage) { |
| 1509 | return s->cnx->egl.eglBindTexImage( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1510 | dp->disp[s->impl].dpy, s->surface, buffer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1511 | } |
| 1512 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1513 | } |
| 1514 | |
| 1515 | EGLBoolean eglReleaseTexImage( |
| 1516 | EGLDisplay dpy, EGLSurface surface, EGLint buffer) |
| 1517 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1518 | SurfaceRef _s(surface); |
| 1519 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1520 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | if (!validate_display_surface(dpy, surface)) |
| 1522 | return EGL_FALSE; |
| 1523 | egl_display_t const * const dp = get_display(dpy); |
| 1524 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1525 | if (s->cnx->egl.eglReleaseTexImage) { |
| 1526 | return s->cnx->egl.eglReleaseTexImage( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1527 | dp->disp[s->impl].dpy, s->surface, buffer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | } |
| 1529 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1530 | } |
| 1531 | |
| 1532 | EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval) |
| 1533 | { |
| 1534 | egl_display_t * const dp = get_display(dpy); |
| 1535 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1536 | |
| 1537 | EGLBoolean res = EGL_TRUE; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1538 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1539 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1540 | if (cnx->dso) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1541 | if (cnx->egl.eglSwapInterval) { |
| 1542 | if (cnx->egl.eglSwapInterval( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1543 | dp->disp[i].dpy, interval) == EGL_FALSE) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1544 | res = EGL_FALSE; |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | return res; |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | // ---------------------------------------------------------------------------- |
| 1554 | // EGL 1.2 |
| 1555 | // ---------------------------------------------------------------------------- |
| 1556 | |
| 1557 | EGLBoolean eglWaitClient(void) |
| 1558 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1559 | // could be called before eglInitialize(), but we wouldn't have a context |
| 1560 | // then, and this function would return GL_TRUE, which isn't wrong. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1561 | EGLBoolean res = EGL_TRUE; |
| 1562 | EGLContext ctx = getContext(); |
| 1563 | if (ctx) { |
| 1564 | egl_context_t const * const c = get_context(ctx); |
| 1565 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1566 | if (uint32_t(c->impl)>=2) |
| 1567 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1568 | egl_connection_t* const cnx = &gEGLImpl[c->impl]; |
| 1569 | if (!cnx->dso) |
| 1570 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1571 | if (cnx->egl.eglWaitClient) { |
| 1572 | res = cnx->egl.eglWaitClient(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1573 | } else { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1574 | res = cnx->egl.eglWaitGL(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | return res; |
| 1578 | } |
| 1579 | |
| 1580 | EGLBoolean eglBindAPI(EGLenum api) |
| 1581 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1582 | if (egl_init_drivers() == EGL_FALSE) { |
| 1583 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1584 | } |
| 1585 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | // bind this API on all EGLs |
| 1587 | EGLBoolean res = EGL_TRUE; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1588 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1590 | if (cnx->dso) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1591 | if (cnx->egl.eglBindAPI) { |
| 1592 | if (cnx->egl.eglBindAPI(api) == EGL_FALSE) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | res = EGL_FALSE; |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | return res; |
| 1599 | } |
| 1600 | |
| 1601 | EGLenum eglQueryAPI(void) |
| 1602 | { |
Mathias Agopian | 923c661 | 2009-08-17 18:07:06 -0700 | [diff] [blame] | 1603 | if (egl_init_drivers() == EGL_FALSE) { |
| 1604 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1605 | } |
| 1606 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1607 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1608 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1609 | if (cnx->dso) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1610 | if (cnx->egl.eglQueryAPI) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | // the first one we find is okay, because they all |
| 1612 | // should be the same |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1613 | return cnx->egl.eglQueryAPI(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | // or, it can only be OpenGL ES |
| 1618 | return EGL_OPENGL_ES_API; |
| 1619 | } |
| 1620 | |
| 1621 | EGLBoolean eglReleaseThread(void) |
| 1622 | { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1623 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1625 | if (cnx->dso) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1626 | if (cnx->egl.eglReleaseThread) { |
| 1627 | cnx->egl.eglReleaseThread(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | } |
| 1629 | } |
| 1630 | } |
| 1631 | clearTLS(); |
| 1632 | return EGL_TRUE; |
| 1633 | } |
| 1634 | |
| 1635 | EGLSurface eglCreatePbufferFromClientBuffer( |
| 1636 | EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, |
| 1637 | EGLConfig config, const EGLint *attrib_list) |
| 1638 | { |
| 1639 | egl_display_t const* dp = 0; |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1640 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1641 | if (!cnx) return EGL_FALSE; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1642 | if (cnx->egl.eglCreatePbufferFromClientBuffer) { |
| 1643 | return cnx->egl.eglCreatePbufferFromClientBuffer( |
Mathias Agopian | cee7939 | 2010-07-26 21:14:59 -0700 | [diff] [blame] | 1644 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 1645 | buftype, buffer, |
| 1646 | dp->configs[intptr_t(config)].config, attrib_list); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1647 | } |
| 1648 | return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE); |
| 1649 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1650 | |
| 1651 | // ---------------------------------------------------------------------------- |
| 1652 | // EGL_EGLEXT_VERSION 3 |
| 1653 | // ---------------------------------------------------------------------------- |
| 1654 | |
| 1655 | EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface, |
| 1656 | const EGLint *attrib_list) |
| 1657 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1658 | SurfaceRef _s(surface); |
| 1659 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1660 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1661 | if (!validate_display_surface(dpy, surface)) |
Mathias Agopian | 24e5f52 | 2009-08-12 21:18:15 -0700 | [diff] [blame] | 1662 | return EGL_FALSE; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1663 | |
| 1664 | egl_display_t const * const dp = get_display(dpy); |
| 1665 | egl_surface_t const * const s = get_surface(surface); |
| 1666 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1667 | if (s->cnx->egl.eglLockSurfaceKHR) { |
| 1668 | return s->cnx->egl.eglLockSurfaceKHR( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1669 | dp->disp[s->impl].dpy, s->surface, attrib_list); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1670 | } |
Mathias Agopian | 24e5f52 | 2009-08-12 21:18:15 -0700 | [diff] [blame] | 1671 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface) |
| 1675 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1676 | SurfaceRef _s(surface); |
| 1677 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1678 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1679 | if (!validate_display_surface(dpy, surface)) |
Mathias Agopian | 24e5f52 | 2009-08-12 21:18:15 -0700 | [diff] [blame] | 1680 | return EGL_FALSE; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1681 | |
| 1682 | egl_display_t const * const dp = get_display(dpy); |
| 1683 | egl_surface_t const * const s = get_surface(surface); |
| 1684 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1685 | if (s->cnx->egl.eglUnlockSurfaceKHR) { |
| 1686 | return s->cnx->egl.eglUnlockSurfaceKHR( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1687 | dp->disp[s->impl].dpy, s->surface); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1688 | } |
Mathias Agopian | 24e5f52 | 2009-08-12 21:18:15 -0700 | [diff] [blame] | 1689 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, |
| 1693 | EGLClientBuffer buffer, const EGLint *attrib_list) |
| 1694 | { |
| 1695 | if (ctx != EGL_NO_CONTEXT) { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1696 | ContextRef _c(ctx); |
| 1697 | if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1698 | if (!validate_display_context(dpy, ctx)) |
| 1699 | return EGL_NO_IMAGE_KHR; |
| 1700 | egl_display_t const * const dp = get_display(dpy); |
| 1701 | egl_context_t * const c = get_context(ctx); |
| 1702 | // since we have an EGLContext, we know which implementation to use |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1703 | EGLImageKHR image = c->cnx->egl.eglCreateImageKHR( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1704 | dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1705 | if (image == EGL_NO_IMAGE_KHR) |
| 1706 | return image; |
| 1707 | |
| 1708 | egl_image_t* result = new egl_image_t(dpy, ctx); |
| 1709 | result->images[c->impl] = image; |
| 1710 | return (EGLImageKHR)result; |
| 1711 | } else { |
| 1712 | // EGL_NO_CONTEXT is a valid parameter |
| 1713 | egl_display_t const * const dp = get_display(dpy); |
| 1714 | if (dp == 0) { |
| 1715 | return setError(EGL_BAD_DISPLAY, EGL_NO_IMAGE_KHR); |
| 1716 | } |
Mathias Agopian | df2d929 | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 1717 | |
| 1718 | /* Since we don't have a way to know which implementation to call, |
| 1719 | * we're calling all of them. If at least one of the implementation |
| 1720 | * succeeded, this is a success. |
| 1721 | */ |
| 1722 | |
| 1723 | EGLint currentError = eglGetError(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1724 | |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1725 | EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS]; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1726 | bool success = false; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1727 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1728 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1729 | implImages[i] = EGL_NO_IMAGE_KHR; |
| 1730 | if (cnx->dso) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1731 | if (cnx->egl.eglCreateImageKHR) { |
| 1732 | implImages[i] = cnx->egl.eglCreateImageKHR( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1733 | dp->disp[i].dpy, ctx, target, buffer, attrib_list); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1734 | if (implImages[i] != EGL_NO_IMAGE_KHR) { |
| 1735 | success = true; |
| 1736 | } |
| 1737 | } |
| 1738 | } |
| 1739 | } |
Mathias Agopian | df2d929 | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 1740 | |
| 1741 | if (!success) { |
| 1742 | // failure, if there was an error when we entered this function, |
| 1743 | // the error flag must not be updated. |
| 1744 | // Otherwise, the error is whatever happened in the implementation |
| 1745 | // that faulted. |
| 1746 | if (currentError != EGL_SUCCESS) { |
| 1747 | setError(currentError, EGL_NO_IMAGE_KHR); |
| 1748 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1749 | return EGL_NO_IMAGE_KHR; |
Mathias Agopian | df2d929 | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 1750 | } else { |
| 1751 | // In case of success, we need to clear all error flags |
| 1752 | // (especially those caused by the implementation that didn't |
Mathias Agopian | 863e5fd | 2009-10-29 18:29:30 -0700 | [diff] [blame] | 1753 | // succeed). TODO: we could avoid this if we knew this was |
Mathias Agopian | df2d929 | 2009-10-28 21:00:29 -0700 | [diff] [blame] | 1754 | // a "full" success (all implementation succeeded). |
| 1755 | eglGetError(); |
| 1756 | } |
| 1757 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1758 | egl_image_t* result = new egl_image_t(dpy, ctx); |
| 1759 | memcpy(result->images, implImages, sizeof(implImages)); |
| 1760 | return (EGLImageKHR)result; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img) |
| 1765 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1766 | egl_display_t const * const dp = get_display(dpy); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1767 | if (dp == 0) { |
| 1768 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1769 | } |
| 1770 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1771 | ImageRef _i(img); |
| 1772 | if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1773 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1774 | egl_image_t* image = get_image(img); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1775 | bool success = false; |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1776 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1777 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1778 | if (image->images[i] != EGL_NO_IMAGE_KHR) { |
| 1779 | if (cnx->dso) { |
Mathias Agopian | 77fbf8d | 2010-09-09 11:12:54 -0700 | [diff] [blame] | 1780 | if (cnx->egl.eglDestroyImageKHR) { |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1781 | if (cnx->egl.eglDestroyImageKHR( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1782 | dp->disp[i].dpy, image->images[i])) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1783 | success = true; |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | } |
| 1788 | } |
| 1789 | if (!success) |
| 1790 | return EGL_FALSE; |
| 1791 | |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1792 | _i.terminate(); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1793 | |
Mathias Agopian | 24e5f52 | 2009-08-12 21:18:15 -0700 | [diff] [blame] | 1794 | return EGL_TRUE; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1795 | } |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 1796 | |
| 1797 | |
| 1798 | // ---------------------------------------------------------------------------- |
| 1799 | // ANDROID extensions |
| 1800 | // ---------------------------------------------------------------------------- |
| 1801 | |
| 1802 | EGLBoolean eglSetSwapRectangleANDROID(EGLDisplay dpy, EGLSurface draw, |
| 1803 | EGLint left, EGLint top, EGLint width, EGLint height) |
| 1804 | { |
Mathias Agopian | 9429e9c | 2009-08-21 02:18:25 -0700 | [diff] [blame] | 1805 | SurfaceRef _s(draw); |
| 1806 | if (!_s.get()) return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1807 | |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 1808 | if (!validate_display_surface(dpy, draw)) |
| 1809 | return EGL_FALSE; |
| 1810 | egl_display_t const * const dp = get_display(dpy); |
| 1811 | egl_surface_t const * const s = get_surface(draw); |
Mathias Agopian | 618fa10 | 2009-10-14 02:06:37 -0700 | [diff] [blame] | 1812 | if (s->cnx->egl.eglSetSwapRectangleANDROID) { |
| 1813 | return s->cnx->egl.eglSetSwapRectangleANDROID( |
Mathias Agopian | a69e0ed | 2009-08-24 21:47:13 -0700 | [diff] [blame] | 1814 | dp->disp[s->impl].dpy, s->surface, left, top, width, height); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 1815 | } |
Mathias Agopian | 24e5f52 | 2009-08-12 21:18:15 -0700 | [diff] [blame] | 1816 | return setError(EGL_BAD_DISPLAY, NULL); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 1817 | } |