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