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