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