Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [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 | |
| 17 | #include <ctype.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include <hardware/gralloc.h> |
| 22 | #include <system/window.h> |
| 23 | |
| 24 | #include <EGL/egl.h> |
| 25 | #include <EGL/eglext.h> |
| 26 | #include <GLES/gl.h> |
| 27 | #include <GLES/glext.h> |
| 28 | |
| 29 | #include <cutils/log.h> |
| 30 | #include <cutils/atomic.h> |
| 31 | #include <cutils/properties.h> |
| 32 | #include <cutils/memory.h> |
| 33 | |
| 34 | #include <utils/KeyedVector.h> |
| 35 | #include <utils/SortedVector.h> |
| 36 | #include <utils/String8.h> |
| 37 | |
| 38 | #include "egl_impl.h" |
| 39 | #include "egl_tls.h" |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 40 | #include "glestrace.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 41 | #include "hooks.h" |
| 42 | |
| 43 | #include "egl_display.h" |
| 44 | #include "egl_impl.h" |
| 45 | #include "egl_object.h" |
| 46 | #include "egl_tls.h" |
| 47 | |
| 48 | using namespace android; |
| 49 | |
| 50 | // ---------------------------------------------------------------------------- |
| 51 | |
Mathias Agopian | bc2d79e | 2011-11-29 17:55:46 -0800 | [diff] [blame] | 52 | #define EGL_VERSION_HW_ANDROID 0x3143 |
| 53 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 54 | struct extention_map_t { |
| 55 | const char* name; |
| 56 | __eglMustCastToProperFunctionPointerType address; |
| 57 | }; |
| 58 | |
| 59 | static const extention_map_t sExtentionMap[] = { |
| 60 | { "eglLockSurfaceKHR", |
| 61 | (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR }, |
| 62 | { "eglUnlockSurfaceKHR", |
| 63 | (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR }, |
| 64 | { "eglCreateImageKHR", |
| 65 | (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR }, |
| 66 | { "eglDestroyImageKHR", |
| 67 | (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR }, |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 68 | { "eglGetSystemTimeFrequencyNV", |
| 69 | (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV }, |
| 70 | { "eglGetSystemTimeNV", |
| 71 | (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV }, |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // accesses protected by sExtensionMapMutex |
| 75 | static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap; |
| 76 | static int sGLExtentionSlot = 0; |
| 77 | static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER; |
| 78 | |
| 79 | static void(*findProcAddress(const char* name, |
| 80 | const extention_map_t* map, size_t n))() { |
| 81 | for (uint32_t i=0 ; i<n ; i++) { |
| 82 | if (!strcmp(name, map[i].name)) { |
| 83 | return map[i].address; |
| 84 | } |
| 85 | } |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | // ---------------------------------------------------------------------------- |
| 90 | |
| 91 | template<typename T> |
| 92 | static __attribute__((noinline)) |
| 93 | int binarySearch(T const sortedArray[], int first, int last, T key) { |
| 94 | while (first <= last) { |
| 95 | int mid = (first + last) / 2; |
| 96 | if (sortedArray[mid] < key) { |
| 97 | first = mid + 1; |
| 98 | } else if (key < sortedArray[mid]) { |
| 99 | last = mid - 1; |
| 100 | } else { |
| 101 | return mid; |
| 102 | } |
| 103 | } |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | // ---------------------------------------------------------------------------- |
| 108 | |
| 109 | namespace android { |
| 110 | extern void setGLHooksThreadSpecific(gl_hooks_t const *value); |
| 111 | extern EGLBoolean egl_init_drivers(); |
| 112 | extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS]; |
| 113 | extern int gEGLDebugLevel; |
| 114 | extern gl_hooks_t gHooksTrace; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 115 | } // namespace android; |
| 116 | |
| 117 | // ---------------------------------------------------------------------------- |
| 118 | |
| 119 | static inline void clearError() { egl_tls_t::clearError(); } |
| 120 | static inline EGLContext getContext() { return egl_tls_t::getContext(); } |
| 121 | |
| 122 | // ---------------------------------------------------------------------------- |
| 123 | |
| 124 | EGLDisplay eglGetDisplay(EGLNativeDisplayType display) |
| 125 | { |
| 126 | clearError(); |
| 127 | |
| 128 | uint32_t index = uint32_t(display); |
| 129 | if (index >= NUM_DISPLAYS) { |
| 130 | return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); |
| 131 | } |
| 132 | |
| 133 | if (egl_init_drivers() == EGL_FALSE) { |
| 134 | return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); |
| 135 | } |
| 136 | |
| 137 | EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display); |
| 138 | return dpy; |
| 139 | } |
| 140 | |
| 141 | // ---------------------------------------------------------------------------- |
| 142 | // Initialization |
| 143 | // ---------------------------------------------------------------------------- |
| 144 | |
| 145 | EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor) |
| 146 | { |
| 147 | clearError(); |
| 148 | |
| 149 | egl_display_t * const dp = get_display(dpy); |
| 150 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 151 | |
| 152 | EGLBoolean res = dp->initialize(major, minor); |
| 153 | |
| 154 | return res; |
| 155 | } |
| 156 | |
| 157 | EGLBoolean eglTerminate(EGLDisplay dpy) |
| 158 | { |
| 159 | // NOTE: don't unload the drivers b/c some APIs can be called |
| 160 | // after eglTerminate() has been called. eglTerminate() only |
| 161 | // terminates an EGLDisplay, not a EGL itself. |
| 162 | |
| 163 | clearError(); |
| 164 | |
| 165 | egl_display_t* const dp = get_display(dpy); |
| 166 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 167 | |
| 168 | EGLBoolean res = dp->terminate(); |
| 169 | |
| 170 | return res; |
| 171 | } |
| 172 | |
| 173 | // ---------------------------------------------------------------------------- |
| 174 | // configuration |
| 175 | // ---------------------------------------------------------------------------- |
| 176 | |
| 177 | EGLBoolean eglGetConfigs( EGLDisplay dpy, |
| 178 | EGLConfig *configs, |
| 179 | EGLint config_size, EGLint *num_config) |
| 180 | { |
| 181 | clearError(); |
| 182 | |
| 183 | egl_display_t const * const dp = validate_display(dpy); |
| 184 | if (!dp) return EGL_FALSE; |
| 185 | |
| 186 | GLint numConfigs = dp->numTotalConfigs; |
| 187 | if (!configs) { |
| 188 | *num_config = numConfigs; |
| 189 | return EGL_TRUE; |
| 190 | } |
| 191 | |
| 192 | GLint n = 0; |
| 193 | for (intptr_t i=0 ; i<dp->numTotalConfigs && config_size ; i++) { |
| 194 | *configs++ = EGLConfig(i); |
| 195 | config_size--; |
| 196 | n++; |
| 197 | } |
| 198 | |
| 199 | *num_config = n; |
| 200 | return EGL_TRUE; |
| 201 | } |
| 202 | |
| 203 | EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list, |
| 204 | EGLConfig *configs, EGLint config_size, |
| 205 | EGLint *num_config) |
| 206 | { |
| 207 | clearError(); |
| 208 | |
| 209 | egl_display_t const * const dp = validate_display(dpy); |
| 210 | if (!dp) return EGL_FALSE; |
| 211 | |
| 212 | if (num_config==0) { |
| 213 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 214 | } |
| 215 | |
| 216 | EGLint n; |
| 217 | EGLBoolean res = EGL_FALSE; |
| 218 | *num_config = 0; |
| 219 | |
| 220 | |
| 221 | // It is unfortunate, but we need to remap the EGL_CONFIG_IDs, |
| 222 | // to do this, we have to go through the attrib_list array once |
| 223 | // to figure out both its size and if it contains an EGL_CONFIG_ID |
| 224 | // key. If so, the full array is copied and patched. |
| 225 | // NOTE: we assume that there can be only one occurrence |
| 226 | // of EGL_CONFIG_ID. |
| 227 | |
| 228 | EGLint patch_index = -1; |
| 229 | GLint attr; |
| 230 | size_t size = 0; |
| 231 | if (attrib_list) { |
| 232 | while ((attr=attrib_list[size]) != EGL_NONE) { |
| 233 | if (attr == EGL_CONFIG_ID) |
| 234 | patch_index = size; |
| 235 | size += 2; |
| 236 | } |
| 237 | } |
| 238 | if (patch_index >= 0) { |
| 239 | size += 2; // we need copy the sentinel as well |
| 240 | EGLint* new_list = (EGLint*)malloc(size*sizeof(EGLint)); |
| 241 | if (new_list == 0) |
| 242 | return setError(EGL_BAD_ALLOC, EGL_FALSE); |
| 243 | memcpy(new_list, attrib_list, size*sizeof(EGLint)); |
| 244 | |
| 245 | // patch the requested EGL_CONFIG_ID |
| 246 | bool found = false; |
| 247 | EGLConfig ourConfig(0); |
| 248 | EGLint& configId(new_list[patch_index+1]); |
| 249 | for (intptr_t i=0 ; i<dp->numTotalConfigs ; i++) { |
| 250 | if (dp->configs[i].configId == configId) { |
| 251 | ourConfig = EGLConfig(i); |
| 252 | configId = dp->configs[i].implConfigId; |
| 253 | found = true; |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | egl_connection_t* const cnx = &gEGLImpl[dp->configs[intptr_t(ourConfig)].impl]; |
| 259 | if (found && cnx->dso) { |
| 260 | // and switch to the new list |
| 261 | attrib_list = const_cast<const EGLint *>(new_list); |
| 262 | |
| 263 | // At this point, the only configuration that can match is |
| 264 | // dp->configs[i][index], however, we don't know if it would be |
| 265 | // rejected because of the other attributes, so we do have to call |
| 266 | // cnx->egl.eglChooseConfig() -- but we don't have to loop |
| 267 | // through all the EGLimpl[]. |
| 268 | // We also know we can only get a single config back, and we know |
| 269 | // which one. |
| 270 | |
| 271 | res = cnx->egl.eglChooseConfig( |
| 272 | dp->disp[ dp->configs[intptr_t(ourConfig)].impl ].dpy, |
| 273 | attrib_list, configs, config_size, &n); |
| 274 | if (res && n>0) { |
| 275 | // n has to be 0 or 1, by construction, and we already know |
| 276 | // which config it will return (since there can be only one). |
| 277 | if (configs) { |
| 278 | configs[0] = ourConfig; |
| 279 | } |
| 280 | *num_config = 1; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | free(const_cast<EGLint *>(attrib_list)); |
| 285 | return res; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 290 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 291 | if (cnx->dso) { |
| 292 | if (cnx->egl.eglChooseConfig( |
| 293 | dp->disp[i].dpy, attrib_list, configs, config_size, &n)) { |
| 294 | if (configs) { |
| 295 | // now we need to convert these client EGLConfig to our |
| 296 | // internal EGLConfig format. |
| 297 | // This is done in O(n Log(n)) time. |
| 298 | for (int j=0 ; j<n ; j++) { |
| 299 | egl_config_t key(i, configs[j]); |
| 300 | intptr_t index = binarySearch<egl_config_t>( |
| 301 | dp->configs, 0, dp->numTotalConfigs, key); |
| 302 | if (index >= 0) { |
| 303 | configs[j] = EGLConfig(index); |
| 304 | } else { |
| 305 | return setError(EGL_BAD_CONFIG, EGL_FALSE); |
| 306 | } |
| 307 | } |
| 308 | configs += n; |
| 309 | config_size -= n; |
| 310 | } |
| 311 | *num_config += n; |
| 312 | res = EGL_TRUE; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | return res; |
| 317 | } |
| 318 | |
| 319 | EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, |
| 320 | EGLint attribute, EGLint *value) |
| 321 | { |
| 322 | clearError(); |
| 323 | |
| 324 | egl_display_t const* dp = 0; |
| 325 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
| 326 | if (!cnx) return EGL_FALSE; |
| 327 | |
| 328 | if (attribute == EGL_CONFIG_ID) { |
| 329 | *value = dp->configs[intptr_t(config)].configId; |
| 330 | return EGL_TRUE; |
| 331 | } |
| 332 | return cnx->egl.eglGetConfigAttrib( |
| 333 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 334 | dp->configs[intptr_t(config)].config, attribute, value); |
| 335 | } |
| 336 | |
| 337 | // ---------------------------------------------------------------------------- |
| 338 | // surfaces |
| 339 | // ---------------------------------------------------------------------------- |
| 340 | |
| 341 | EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config, |
| 342 | NativeWindowType window, |
| 343 | const EGLint *attrib_list) |
| 344 | { |
| 345 | clearError(); |
| 346 | |
| 347 | egl_display_t const* dp = 0; |
| 348 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
| 349 | if (cnx) { |
| 350 | EGLDisplay iDpy = dp->disp[ dp->configs[intptr_t(config)].impl ].dpy; |
| 351 | EGLConfig iConfig = dp->configs[intptr_t(config)].config; |
| 352 | EGLint format; |
| 353 | |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 354 | if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 355 | ALOGE("EGLNativeWindowType %p already connected to another API", |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 356 | window); |
| 357 | return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE); |
| 358 | } |
| 359 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 360 | // set the native window's buffers format to match this config |
| 361 | if (cnx->egl.eglGetConfigAttrib(iDpy, |
| 362 | iConfig, EGL_NATIVE_VISUAL_ID, &format)) { |
| 363 | if (format != 0) { |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 364 | int err = native_window_set_buffers_format(window, format); |
| 365 | if (err != 0) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 366 | ALOGE("error setting native window pixel format: %s (%d)", |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 367 | strerror(-err), err); |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 368 | native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 369 | return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE); |
| 370 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 374 | // the EGL spec requires that a new EGLSurface default to swap interval |
| 375 | // 1, so explicitly set that on the window here. |
| 376 | ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window); |
| 377 | anw->setSwapInterval(anw, 1); |
| 378 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 379 | EGLSurface surface = cnx->egl.eglCreateWindowSurface( |
| 380 | iDpy, iConfig, window, attrib_list); |
| 381 | if (surface != EGL_NO_SURFACE) { |
| 382 | egl_surface_t* s = new egl_surface_t(dpy, config, window, surface, |
| 383 | dp->configs[intptr_t(config)].impl, cnx); |
| 384 | return s; |
| 385 | } |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 386 | |
| 387 | // EGLSurface creation failed |
| 388 | native_window_set_buffers_format(window, 0); |
| 389 | native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 390 | } |
| 391 | return EGL_NO_SURFACE; |
| 392 | } |
| 393 | |
| 394 | EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config, |
| 395 | NativePixmapType pixmap, |
| 396 | const EGLint *attrib_list) |
| 397 | { |
| 398 | clearError(); |
| 399 | |
| 400 | egl_display_t const* dp = 0; |
| 401 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
| 402 | if (cnx) { |
| 403 | EGLSurface surface = cnx->egl.eglCreatePixmapSurface( |
| 404 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 405 | dp->configs[intptr_t(config)].config, pixmap, attrib_list); |
| 406 | if (surface != EGL_NO_SURFACE) { |
| 407 | egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface, |
| 408 | dp->configs[intptr_t(config)].impl, cnx); |
| 409 | return s; |
| 410 | } |
| 411 | } |
| 412 | return EGL_NO_SURFACE; |
| 413 | } |
| 414 | |
| 415 | EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config, |
| 416 | const EGLint *attrib_list) |
| 417 | { |
| 418 | clearError(); |
| 419 | |
| 420 | egl_display_t const* dp = 0; |
| 421 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
| 422 | if (cnx) { |
| 423 | EGLSurface surface = cnx->egl.eglCreatePbufferSurface( |
| 424 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 425 | dp->configs[intptr_t(config)].config, attrib_list); |
| 426 | if (surface != EGL_NO_SURFACE) { |
| 427 | egl_surface_t* s = new egl_surface_t(dpy, config, NULL, surface, |
| 428 | dp->configs[intptr_t(config)].impl, cnx); |
| 429 | return s; |
| 430 | } |
| 431 | } |
| 432 | return EGL_NO_SURFACE; |
| 433 | } |
| 434 | |
| 435 | EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface) |
| 436 | { |
| 437 | clearError(); |
| 438 | |
| 439 | egl_display_t const * const dp = validate_display(dpy); |
| 440 | if (!dp) return EGL_FALSE; |
| 441 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 442 | SurfaceRef _s(dp, surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 443 | if (!_s.get()) |
| 444 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 445 | |
| 446 | egl_surface_t * const s = get_surface(surface); |
| 447 | EGLBoolean result = s->cnx->egl.eglDestroySurface( |
| 448 | dp->disp[s->impl].dpy, s->surface); |
| 449 | if (result == EGL_TRUE) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 450 | _s.terminate(); |
| 451 | } |
| 452 | return result; |
| 453 | } |
| 454 | |
| 455 | EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface, |
| 456 | EGLint attribute, EGLint *value) |
| 457 | { |
| 458 | clearError(); |
| 459 | |
| 460 | egl_display_t const * const dp = validate_display(dpy); |
| 461 | if (!dp) return EGL_FALSE; |
| 462 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 463 | SurfaceRef _s(dp, surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 464 | if (!_s.get()) |
| 465 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 466 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 467 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 468 | EGLBoolean result(EGL_TRUE); |
| 469 | if (attribute == EGL_CONFIG_ID) { |
| 470 | // We need to remap EGL_CONFIG_IDs |
| 471 | *value = dp->configs[intptr_t(s->config)].configId; |
| 472 | } else { |
| 473 | result = s->cnx->egl.eglQuerySurface( |
| 474 | dp->disp[s->impl].dpy, s->surface, attribute, value); |
| 475 | } |
| 476 | |
| 477 | return result; |
| 478 | } |
| 479 | |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 480 | void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) { |
| 481 | clearError(); |
| 482 | |
| 483 | egl_display_t const * const dp = validate_display(dpy); |
| 484 | if (!dp) { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | SurfaceRef _s(dp, surface); |
| 489 | if (!_s.get()) { |
| 490 | setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 495 | |
| 496 | egl_surface_t const * const s = get_surface(surface); |
| 497 | native_window_set_buffers_timestamp(s->win.get(), timestamp); |
| 498 | } |
| 499 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 500 | // ---------------------------------------------------------------------------- |
| 501 | // Contexts |
| 502 | // ---------------------------------------------------------------------------- |
| 503 | |
| 504 | EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, |
| 505 | EGLContext share_list, const EGLint *attrib_list) |
| 506 | { |
| 507 | clearError(); |
| 508 | |
| 509 | egl_display_t const* dp = 0; |
| 510 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
| 511 | if (cnx) { |
| 512 | if (share_list != EGL_NO_CONTEXT) { |
| 513 | egl_context_t* const c = get_context(share_list); |
| 514 | share_list = c->context; |
| 515 | } |
| 516 | EGLContext context = cnx->egl.eglCreateContext( |
| 517 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 518 | dp->configs[intptr_t(config)].config, |
| 519 | share_list, attrib_list); |
| 520 | if (context != EGL_NO_CONTEXT) { |
| 521 | // figure out if it's a GLESv1 or GLESv2 |
| 522 | int version = 0; |
| 523 | if (attrib_list) { |
| 524 | while (*attrib_list != EGL_NONE) { |
| 525 | GLint attr = *attrib_list++; |
| 526 | GLint value = *attrib_list++; |
| 527 | if (attr == EGL_CONTEXT_CLIENT_VERSION) { |
| 528 | if (value == 1) { |
| 529 | version = GLESv1_INDEX; |
| 530 | } else if (value == 2) { |
| 531 | version = GLESv2_INDEX; |
| 532 | } |
| 533 | } |
| 534 | }; |
| 535 | } |
| 536 | egl_context_t* c = new egl_context_t(dpy, context, config, |
| 537 | dp->configs[intptr_t(config)].impl, cnx, version); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 538 | #if EGL_TRACE |
| 539 | if (gEGLDebugLevel > 0) |
| 540 | GLTrace_eglCreateContext(version, c); |
| 541 | #endif |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 542 | return c; |
| 543 | } |
| 544 | } |
| 545 | return EGL_NO_CONTEXT; |
| 546 | } |
| 547 | |
| 548 | EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx) |
| 549 | { |
| 550 | clearError(); |
| 551 | |
| 552 | egl_display_t const * const dp = validate_display(dpy); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 553 | if (!dp) |
| 554 | return EGL_FALSE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 555 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 556 | ContextRef _c(dp, ctx); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 557 | if (!_c.get()) |
| 558 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 559 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 560 | egl_context_t * const c = get_context(ctx); |
| 561 | EGLBoolean result = c->cnx->egl.eglDestroyContext( |
| 562 | dp->disp[c->impl].dpy, c->context); |
| 563 | if (result == EGL_TRUE) { |
| 564 | _c.terminate(); |
| 565 | } |
| 566 | return result; |
| 567 | } |
| 568 | |
| 569 | static void loseCurrent(egl_context_t * cur_c) |
| 570 | { |
| 571 | if (cur_c) { |
| 572 | egl_surface_t * cur_r = get_surface(cur_c->read); |
| 573 | egl_surface_t * cur_d = get_surface(cur_c->draw); |
| 574 | |
| 575 | // by construction, these are either 0 or valid (possibly terminated) |
| 576 | // it should be impossible for these to be invalid |
| 577 | ContextRef _cur_c(cur_c); |
| 578 | SurfaceRef _cur_r(cur_r); |
| 579 | SurfaceRef _cur_d(cur_d); |
| 580 | |
| 581 | cur_c->read = NULL; |
| 582 | cur_c->draw = NULL; |
| 583 | |
| 584 | _cur_c.release(); |
| 585 | _cur_r.release(); |
| 586 | _cur_d.release(); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw, |
| 591 | EGLSurface read, EGLContext ctx) |
| 592 | { |
| 593 | clearError(); |
| 594 | |
| 595 | egl_display_t const * const dp = get_display(dpy); |
| 596 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 597 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 598 | // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not |
| 599 | // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is |
| 600 | // a valid but uninitialized display. |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 601 | if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) || |
| 602 | (draw != EGL_NO_SURFACE) ) { |
| 603 | if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE); |
| 604 | } |
| 605 | |
| 606 | // get a reference to the object passed in |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 607 | ContextRef _c(dp, ctx); |
| 608 | SurfaceRef _d(dp, draw); |
| 609 | SurfaceRef _r(dp, read); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 610 | |
| 611 | // validate the context (if not EGL_NO_CONTEXT) |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 612 | if ((ctx != EGL_NO_CONTEXT) && !_c.get()) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 613 | // EGL_NO_CONTEXT is valid |
| 614 | return EGL_FALSE; |
| 615 | } |
| 616 | |
| 617 | // these are the underlying implementation's object |
| 618 | EGLContext impl_ctx = EGL_NO_CONTEXT; |
| 619 | EGLSurface impl_draw = EGL_NO_SURFACE; |
| 620 | EGLSurface impl_read = EGL_NO_SURFACE; |
| 621 | |
| 622 | // these are our objects structs passed in |
| 623 | egl_context_t * c = NULL; |
| 624 | egl_surface_t const * d = NULL; |
| 625 | egl_surface_t const * r = NULL; |
| 626 | |
| 627 | // these are the current objects structs |
| 628 | egl_context_t * cur_c = get_context(getContext()); |
| 629 | |
| 630 | if (ctx != EGL_NO_CONTEXT) { |
| 631 | c = get_context(ctx); |
| 632 | impl_ctx = c->context; |
| 633 | } else { |
| 634 | // no context given, use the implementation of the current context |
| 635 | if (cur_c == NULL) { |
| 636 | // no current context |
| 637 | if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) { |
| 638 | // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT); |
| 639 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
| 640 | } |
| 641 | // not an error, there is just no current context. |
| 642 | return EGL_TRUE; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // retrieve the underlying implementation's draw EGLSurface |
| 647 | if (draw != EGL_NO_SURFACE) { |
| 648 | d = get_surface(draw); |
| 649 | // make sure the EGLContext and EGLSurface passed in are for |
| 650 | // the same driver |
| 651 | if (c && d->impl != c->impl) |
| 652 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
| 653 | impl_draw = d->surface; |
| 654 | } |
| 655 | |
| 656 | // retrieve the underlying implementation's read EGLSurface |
| 657 | if (read != EGL_NO_SURFACE) { |
| 658 | r = get_surface(read); |
| 659 | // make sure the EGLContext and EGLSurface passed in are for |
| 660 | // the same driver |
| 661 | if (c && r->impl != c->impl) |
| 662 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
| 663 | impl_read = r->surface; |
| 664 | } |
| 665 | |
| 666 | EGLBoolean result; |
| 667 | |
| 668 | if (c) { |
| 669 | result = c->cnx->egl.eglMakeCurrent( |
| 670 | dp->disp[c->impl].dpy, impl_draw, impl_read, impl_ctx); |
| 671 | } else { |
| 672 | result = cur_c->cnx->egl.eglMakeCurrent( |
| 673 | dp->disp[cur_c->impl].dpy, impl_draw, impl_read, impl_ctx); |
| 674 | } |
| 675 | |
| 676 | if (result == EGL_TRUE) { |
| 677 | |
| 678 | loseCurrent(cur_c); |
| 679 | |
| 680 | if (ctx != EGL_NO_CONTEXT) { |
| 681 | setGLHooksThreadSpecific(c->cnx->hooks[c->version]); |
| 682 | egl_tls_t::setContext(ctx); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 683 | #if EGL_TRACE |
| 684 | if (gEGLDebugLevel > 0) |
Siva Velusamy | 93a826f | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 685 | GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 686 | #endif |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 687 | _c.acquire(); |
| 688 | _r.acquire(); |
| 689 | _d.acquire(); |
| 690 | c->read = read; |
| 691 | c->draw = draw; |
| 692 | } else { |
| 693 | setGLHooksThreadSpecific(&gHooksNoContext); |
| 694 | egl_tls_t::setContext(EGL_NO_CONTEXT); |
| 695 | } |
Mathias Agopian | 5fecea7 | 2011-08-25 18:38:24 -0700 | [diff] [blame] | 696 | } else { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 697 | // this will ALOGE the error |
Mathias Agopian | 5fecea7 | 2011-08-25 18:38:24 -0700 | [diff] [blame] | 698 | result = setError(c->cnx->egl.eglGetError(), EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 699 | } |
| 700 | return result; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx, |
| 705 | EGLint attribute, EGLint *value) |
| 706 | { |
| 707 | clearError(); |
| 708 | |
| 709 | egl_display_t const * const dp = validate_display(dpy); |
| 710 | if (!dp) return EGL_FALSE; |
| 711 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 712 | ContextRef _c(dp, ctx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 713 | if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 714 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 715 | egl_context_t * const c = get_context(ctx); |
| 716 | |
| 717 | EGLBoolean result(EGL_TRUE); |
| 718 | if (attribute == EGL_CONFIG_ID) { |
| 719 | *value = dp->configs[intptr_t(c->config)].configId; |
| 720 | } else { |
| 721 | // We need to remap EGL_CONFIG_IDs |
| 722 | result = c->cnx->egl.eglQueryContext( |
| 723 | dp->disp[c->impl].dpy, c->context, attribute, value); |
| 724 | } |
| 725 | |
| 726 | return result; |
| 727 | } |
| 728 | |
| 729 | EGLContext eglGetCurrentContext(void) |
| 730 | { |
| 731 | // could be called before eglInitialize(), but we wouldn't have a context |
| 732 | // then, and this function would correctly return EGL_NO_CONTEXT. |
| 733 | |
| 734 | clearError(); |
| 735 | |
| 736 | EGLContext ctx = getContext(); |
| 737 | return ctx; |
| 738 | } |
| 739 | |
| 740 | EGLSurface eglGetCurrentSurface(EGLint readdraw) |
| 741 | { |
| 742 | // could be called before eglInitialize(), but we wouldn't have a context |
| 743 | // then, and this function would correctly return EGL_NO_SURFACE. |
| 744 | |
| 745 | clearError(); |
| 746 | |
| 747 | EGLContext ctx = getContext(); |
| 748 | if (ctx) { |
| 749 | egl_context_t const * const c = get_context(ctx); |
| 750 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); |
| 751 | switch (readdraw) { |
| 752 | case EGL_READ: return c->read; |
| 753 | case EGL_DRAW: return c->draw; |
| 754 | default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
| 755 | } |
| 756 | } |
| 757 | return EGL_NO_SURFACE; |
| 758 | } |
| 759 | |
| 760 | EGLDisplay eglGetCurrentDisplay(void) |
| 761 | { |
| 762 | // could be called before eglInitialize(), but we wouldn't have a context |
| 763 | // then, and this function would correctly return EGL_NO_DISPLAY. |
| 764 | |
| 765 | clearError(); |
| 766 | |
| 767 | EGLContext ctx = getContext(); |
| 768 | if (ctx) { |
| 769 | egl_context_t const * const c = get_context(ctx); |
| 770 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); |
| 771 | return c->dpy; |
| 772 | } |
| 773 | return EGL_NO_DISPLAY; |
| 774 | } |
| 775 | |
| 776 | EGLBoolean eglWaitGL(void) |
| 777 | { |
| 778 | // could be called before eglInitialize(), but we wouldn't have a context |
| 779 | // then, and this function would return GL_TRUE, which isn't wrong. |
| 780 | |
| 781 | clearError(); |
| 782 | |
| 783 | EGLBoolean res = EGL_TRUE; |
| 784 | EGLContext ctx = getContext(); |
| 785 | if (ctx) { |
| 786 | egl_context_t const * const c = get_context(ctx); |
| 787 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 788 | if (uint32_t(c->impl)>=2) |
| 789 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 790 | egl_connection_t* const cnx = &gEGLImpl[c->impl]; |
| 791 | if (!cnx->dso) |
| 792 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 793 | res = cnx->egl.eglWaitGL(); |
| 794 | } |
| 795 | return res; |
| 796 | } |
| 797 | |
| 798 | EGLBoolean eglWaitNative(EGLint engine) |
| 799 | { |
| 800 | // could be called before eglInitialize(), but we wouldn't have a context |
| 801 | // then, and this function would return GL_TRUE, which isn't wrong. |
| 802 | |
| 803 | clearError(); |
| 804 | |
| 805 | EGLBoolean res = EGL_TRUE; |
| 806 | EGLContext ctx = getContext(); |
| 807 | if (ctx) { |
| 808 | egl_context_t const * const c = get_context(ctx); |
| 809 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 810 | if (uint32_t(c->impl)>=2) |
| 811 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 812 | egl_connection_t* const cnx = &gEGLImpl[c->impl]; |
| 813 | if (!cnx->dso) |
| 814 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 815 | res = cnx->egl.eglWaitNative(engine); |
| 816 | } |
| 817 | return res; |
| 818 | } |
| 819 | |
| 820 | EGLint eglGetError(void) |
| 821 | { |
| 822 | EGLint result = EGL_SUCCESS; |
| 823 | EGLint err; |
| 824 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 825 | err = EGL_SUCCESS; |
| 826 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 827 | if (cnx->dso) |
| 828 | err = cnx->egl.eglGetError(); |
| 829 | if (err!=EGL_SUCCESS && result==EGL_SUCCESS) |
| 830 | result = err; |
| 831 | } |
| 832 | err = egl_tls_t::getError(); |
| 833 | if (result == EGL_SUCCESS) |
| 834 | result = err; |
| 835 | return result; |
| 836 | } |
| 837 | |
| 838 | // Note: Similar implementations of these functions also exist in |
| 839 | // gl2.cpp and gl.cpp, and are used by applications that call the |
| 840 | // exported entry points directly. |
| 841 | typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC) (GLenum target, GLeglImageOES image); |
| 842 | typedef void (GL_APIENTRYP PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC) (GLenum target, GLeglImageOES image); |
| 843 | |
| 844 | static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES_impl = NULL; |
| 845 | static PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES_impl = NULL; |
| 846 | |
| 847 | static void glEGLImageTargetTexture2DOES_wrapper(GLenum target, GLeglImageOES image) |
| 848 | { |
| 849 | GLeglImageOES implImage = |
| 850 | (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image); |
| 851 | glEGLImageTargetTexture2DOES_impl(target, implImage); |
| 852 | } |
| 853 | |
| 854 | static void glEGLImageTargetRenderbufferStorageOES_wrapper(GLenum target, GLeglImageOES image) |
| 855 | { |
| 856 | GLeglImageOES implImage = |
| 857 | (GLeglImageOES)egl_get_image_for_current_context((EGLImageKHR)image); |
| 858 | glEGLImageTargetRenderbufferStorageOES_impl(target, implImage); |
| 859 | } |
| 860 | |
| 861 | __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname) |
| 862 | { |
| 863 | // eglGetProcAddress() could be the very first function called |
| 864 | // in which case we must make sure we've initialized ourselves, this |
| 865 | // happens the first time egl_get_display() is called. |
| 866 | |
| 867 | clearError(); |
| 868 | |
| 869 | if (egl_init_drivers() == EGL_FALSE) { |
| 870 | setError(EGL_BAD_PARAMETER, NULL); |
| 871 | return NULL; |
| 872 | } |
| 873 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 874 | // The EGL_ANDROID_blob_cache extension should not be exposed to |
| 875 | // applications. It is used internally by the Android EGL layer. |
Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 876 | if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID")) { |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 877 | return NULL; |
| 878 | } |
| 879 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 880 | __eglMustCastToProperFunctionPointerType addr; |
| 881 | addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap)); |
| 882 | if (addr) return addr; |
| 883 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 884 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 885 | // this protects accesses to sGLExtentionMap and sGLExtentionSlot |
| 886 | pthread_mutex_lock(&sExtensionMapMutex); |
| 887 | |
| 888 | /* |
| 889 | * Since eglGetProcAddress() is not associated to anything, it needs |
| 890 | * to return a function pointer that "works" regardless of what |
| 891 | * the current context is. |
| 892 | * |
| 893 | * For this reason, we return a "forwarder", a small stub that takes |
| 894 | * care of calling the function associated with the context |
| 895 | * currently bound. |
| 896 | * |
| 897 | * We first look for extensions we've already resolved, if we're seeing |
| 898 | * this extension for the first time, we go through all our |
| 899 | * implementations and call eglGetProcAddress() and record the |
| 900 | * result in the appropriate implementation hooks and return the |
| 901 | * address of the forwarder corresponding to that hook set. |
| 902 | * |
| 903 | */ |
| 904 | |
| 905 | const String8 name(procname); |
| 906 | addr = sGLExtentionMap.valueFor(name); |
| 907 | const int slot = sGLExtentionSlot; |
| 908 | |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 909 | ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS, |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 910 | "no more slots for eglGetProcAddress(\"%s\")", |
| 911 | procname); |
| 912 | |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 913 | #if EGL_TRACE |
| 914 | gl_hooks_t *debugHooks = GLTrace_getGLHooks(); |
| 915 | #endif |
| 916 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 917 | if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) { |
| 918 | bool found = false; |
| 919 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 920 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 921 | if (cnx->dso && cnx->egl.eglGetProcAddress) { |
| 922 | found = true; |
| 923 | // Extensions are independent of the bound context |
| 924 | cnx->hooks[GLESv1_INDEX]->ext.extensions[slot] = |
| 925 | cnx->hooks[GLESv2_INDEX]->ext.extensions[slot] = |
| 926 | #if EGL_TRACE |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 927 | debugHooks->ext.extensions[slot] = gHooksTrace.ext.extensions[slot] = |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 928 | #endif |
| 929 | cnx->egl.eglGetProcAddress(procname); |
| 930 | } |
| 931 | } |
| 932 | if (found) { |
| 933 | addr = gExtensionForwarders[slot]; |
| 934 | |
| 935 | if (!strcmp(procname, "glEGLImageTargetTexture2DOES")) { |
| 936 | glEGLImageTargetTexture2DOES_impl = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)addr; |
| 937 | addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetTexture2DOES_wrapper; |
| 938 | } |
| 939 | if (!strcmp(procname, "glEGLImageTargetRenderbufferStorageOES")) { |
| 940 | glEGLImageTargetRenderbufferStorageOES_impl = (PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC)addr; |
| 941 | addr = (__eglMustCastToProperFunctionPointerType)glEGLImageTargetRenderbufferStorageOES_wrapper; |
| 942 | } |
| 943 | |
| 944 | sGLExtentionMap.add(name, addr); |
| 945 | sGLExtentionSlot++; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | pthread_mutex_unlock(&sExtensionMapMutex); |
| 950 | return addr; |
| 951 | } |
| 952 | |
| 953 | EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) |
| 954 | { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 955 | clearError(); |
| 956 | |
| 957 | egl_display_t const * const dp = validate_display(dpy); |
| 958 | if (!dp) return EGL_FALSE; |
| 959 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 960 | SurfaceRef _s(dp, draw); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 961 | if (!_s.get()) |
| 962 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 963 | |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 964 | #if EGL_TRACE |
| 965 | if (gEGLDebugLevel > 0) |
| 966 | GLTrace_eglSwapBuffers(dpy, draw); |
| 967 | #endif |
| 968 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 969 | egl_surface_t const * const s = get_surface(draw); |
| 970 | return s->cnx->egl.eglSwapBuffers(dp->disp[s->impl].dpy, s->surface); |
| 971 | } |
| 972 | |
| 973 | EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface, |
| 974 | NativePixmapType target) |
| 975 | { |
| 976 | clearError(); |
| 977 | |
| 978 | egl_display_t const * const dp = validate_display(dpy); |
| 979 | if (!dp) return EGL_FALSE; |
| 980 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 981 | SurfaceRef _s(dp, surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 982 | if (!_s.get()) |
| 983 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 984 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 985 | egl_surface_t const * const s = get_surface(surface); |
| 986 | return s->cnx->egl.eglCopyBuffers( |
| 987 | dp->disp[s->impl].dpy, s->surface, target); |
| 988 | } |
| 989 | |
| 990 | const char* eglQueryString(EGLDisplay dpy, EGLint name) |
| 991 | { |
| 992 | clearError(); |
| 993 | |
| 994 | egl_display_t const * const dp = validate_display(dpy); |
| 995 | if (!dp) return (const char *) NULL; |
| 996 | |
| 997 | switch (name) { |
| 998 | case EGL_VENDOR: |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 999 | return dp->getVendorString(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1000 | case EGL_VERSION: |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 1001 | return dp->getVersionString(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1002 | case EGL_EXTENSIONS: |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 1003 | return dp->getExtensionString(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1004 | case EGL_CLIENT_APIS: |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 1005 | return dp->getClientApiString(); |
Mathias Agopian | bc2d79e | 2011-11-29 17:55:46 -0800 | [diff] [blame] | 1006 | case EGL_VERSION_HW_ANDROID: { |
| 1007 | if (gEGLImpl[IMPL_HARDWARE].dso) { |
| 1008 | return dp->disp[IMPL_HARDWARE].queryString.version; |
| 1009 | } |
| 1010 | return dp->disp[IMPL_SOFTWARE].queryString.version; |
| 1011 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1012 | } |
| 1013 | return setError(EGL_BAD_PARAMETER, (const char *)0); |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | // ---------------------------------------------------------------------------- |
| 1018 | // EGL 1.1 |
| 1019 | // ---------------------------------------------------------------------------- |
| 1020 | |
| 1021 | EGLBoolean eglSurfaceAttrib( |
| 1022 | EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value) |
| 1023 | { |
| 1024 | clearError(); |
| 1025 | |
| 1026 | egl_display_t const * const dp = validate_display(dpy); |
| 1027 | if (!dp) return EGL_FALSE; |
| 1028 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1029 | SurfaceRef _s(dp, surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1030 | if (!_s.get()) |
| 1031 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1032 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1033 | egl_surface_t const * const s = get_surface(surface); |
| 1034 | if (s->cnx->egl.eglSurfaceAttrib) { |
| 1035 | return s->cnx->egl.eglSurfaceAttrib( |
| 1036 | dp->disp[s->impl].dpy, s->surface, attribute, value); |
| 1037 | } |
| 1038 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1039 | } |
| 1040 | |
| 1041 | EGLBoolean eglBindTexImage( |
| 1042 | EGLDisplay dpy, EGLSurface surface, EGLint buffer) |
| 1043 | { |
| 1044 | clearError(); |
| 1045 | |
| 1046 | egl_display_t const * const dp = validate_display(dpy); |
| 1047 | if (!dp) return EGL_FALSE; |
| 1048 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1049 | SurfaceRef _s(dp, surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1050 | if (!_s.get()) |
| 1051 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1052 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1053 | egl_surface_t const * const s = get_surface(surface); |
| 1054 | if (s->cnx->egl.eglBindTexImage) { |
| 1055 | return s->cnx->egl.eglBindTexImage( |
| 1056 | dp->disp[s->impl].dpy, s->surface, buffer); |
| 1057 | } |
| 1058 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1059 | } |
| 1060 | |
| 1061 | EGLBoolean eglReleaseTexImage( |
| 1062 | EGLDisplay dpy, EGLSurface surface, EGLint buffer) |
| 1063 | { |
| 1064 | clearError(); |
| 1065 | |
| 1066 | egl_display_t const * const dp = validate_display(dpy); |
| 1067 | if (!dp) return EGL_FALSE; |
| 1068 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1069 | SurfaceRef _s(dp, surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1070 | if (!_s.get()) |
| 1071 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1072 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1073 | egl_surface_t const * const s = get_surface(surface); |
| 1074 | if (s->cnx->egl.eglReleaseTexImage) { |
| 1075 | return s->cnx->egl.eglReleaseTexImage( |
| 1076 | dp->disp[s->impl].dpy, s->surface, buffer); |
| 1077 | } |
| 1078 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 1079 | } |
| 1080 | |
| 1081 | EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval) |
| 1082 | { |
| 1083 | clearError(); |
| 1084 | |
| 1085 | egl_display_t const * const dp = validate_display(dpy); |
| 1086 | if (!dp) return EGL_FALSE; |
| 1087 | |
| 1088 | EGLBoolean res = EGL_TRUE; |
| 1089 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 1090 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1091 | if (cnx->dso) { |
| 1092 | if (cnx->egl.eglSwapInterval) { |
| 1093 | if (cnx->egl.eglSwapInterval( |
| 1094 | dp->disp[i].dpy, interval) == EGL_FALSE) { |
| 1095 | res = EGL_FALSE; |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | return res; |
| 1101 | } |
| 1102 | |
| 1103 | |
| 1104 | // ---------------------------------------------------------------------------- |
| 1105 | // EGL 1.2 |
| 1106 | // ---------------------------------------------------------------------------- |
| 1107 | |
| 1108 | EGLBoolean eglWaitClient(void) |
| 1109 | { |
| 1110 | clearError(); |
| 1111 | |
| 1112 | // could be called before eglInitialize(), but we wouldn't have a context |
| 1113 | // then, and this function would return GL_TRUE, which isn't wrong. |
| 1114 | EGLBoolean res = EGL_TRUE; |
| 1115 | EGLContext ctx = getContext(); |
| 1116 | if (ctx) { |
| 1117 | egl_context_t const * const c = get_context(ctx); |
| 1118 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1119 | if (uint32_t(c->impl)>=2) |
| 1120 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1121 | egl_connection_t* const cnx = &gEGLImpl[c->impl]; |
| 1122 | if (!cnx->dso) |
| 1123 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1124 | if (cnx->egl.eglWaitClient) { |
| 1125 | res = cnx->egl.eglWaitClient(); |
| 1126 | } else { |
| 1127 | res = cnx->egl.eglWaitGL(); |
| 1128 | } |
| 1129 | } |
| 1130 | return res; |
| 1131 | } |
| 1132 | |
| 1133 | EGLBoolean eglBindAPI(EGLenum api) |
| 1134 | { |
| 1135 | clearError(); |
| 1136 | |
| 1137 | if (egl_init_drivers() == EGL_FALSE) { |
| 1138 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1139 | } |
| 1140 | |
| 1141 | // bind this API on all EGLs |
| 1142 | EGLBoolean res = EGL_TRUE; |
| 1143 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 1144 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1145 | if (cnx->dso) { |
| 1146 | if (cnx->egl.eglBindAPI) { |
| 1147 | if (cnx->egl.eglBindAPI(api) == EGL_FALSE) { |
| 1148 | res = EGL_FALSE; |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | return res; |
| 1154 | } |
| 1155 | |
| 1156 | EGLenum eglQueryAPI(void) |
| 1157 | { |
| 1158 | clearError(); |
| 1159 | |
| 1160 | if (egl_init_drivers() == EGL_FALSE) { |
| 1161 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1162 | } |
| 1163 | |
| 1164 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 1165 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1166 | if (cnx->dso) { |
| 1167 | if (cnx->egl.eglQueryAPI) { |
| 1168 | // the first one we find is okay, because they all |
| 1169 | // should be the same |
| 1170 | return cnx->egl.eglQueryAPI(); |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | // or, it can only be OpenGL ES |
| 1175 | return EGL_OPENGL_ES_API; |
| 1176 | } |
| 1177 | |
| 1178 | EGLBoolean eglReleaseThread(void) |
| 1179 | { |
| 1180 | clearError(); |
| 1181 | |
| 1182 | // If there is context bound to the thread, release it |
| 1183 | loseCurrent(get_context(getContext())); |
| 1184 | |
| 1185 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 1186 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1187 | if (cnx->dso) { |
| 1188 | if (cnx->egl.eglReleaseThread) { |
| 1189 | cnx->egl.eglReleaseThread(); |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | egl_tls_t::clearTLS(); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 1194 | #if EGL_TRACE |
| 1195 | if (gEGLDebugLevel > 0) |
| 1196 | GLTrace_eglReleaseThread(); |
| 1197 | #endif |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1198 | return EGL_TRUE; |
| 1199 | } |
| 1200 | |
| 1201 | EGLSurface eglCreatePbufferFromClientBuffer( |
| 1202 | EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, |
| 1203 | EGLConfig config, const EGLint *attrib_list) |
| 1204 | { |
| 1205 | clearError(); |
| 1206 | |
| 1207 | egl_display_t const* dp = 0; |
| 1208 | egl_connection_t* cnx = validate_display_config(dpy, config, dp); |
| 1209 | if (!cnx) return EGL_FALSE; |
| 1210 | if (cnx->egl.eglCreatePbufferFromClientBuffer) { |
| 1211 | return cnx->egl.eglCreatePbufferFromClientBuffer( |
| 1212 | dp->disp[ dp->configs[intptr_t(config)].impl ].dpy, |
| 1213 | buftype, buffer, |
| 1214 | dp->configs[intptr_t(config)].config, attrib_list); |
| 1215 | } |
| 1216 | return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE); |
| 1217 | } |
| 1218 | |
| 1219 | // ---------------------------------------------------------------------------- |
| 1220 | // EGL_EGLEXT_VERSION 3 |
| 1221 | // ---------------------------------------------------------------------------- |
| 1222 | |
| 1223 | EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface, |
| 1224 | const EGLint *attrib_list) |
| 1225 | { |
| 1226 | clearError(); |
| 1227 | |
| 1228 | egl_display_t const * const dp = validate_display(dpy); |
| 1229 | if (!dp) return EGL_FALSE; |
| 1230 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1231 | SurfaceRef _s(dp, surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1232 | if (!_s.get()) |
| 1233 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1234 | |
| 1235 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1236 | if (s->cnx->egl.eglLockSurfaceKHR) { |
| 1237 | return s->cnx->egl.eglLockSurfaceKHR( |
| 1238 | dp->disp[s->impl].dpy, s->surface, attrib_list); |
| 1239 | } |
| 1240 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1241 | } |
| 1242 | |
| 1243 | EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface) |
| 1244 | { |
| 1245 | clearError(); |
| 1246 | |
| 1247 | egl_display_t const * const dp = validate_display(dpy); |
| 1248 | if (!dp) return EGL_FALSE; |
| 1249 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1250 | SurfaceRef _s(dp, surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1251 | if (!_s.get()) |
| 1252 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1253 | |
| 1254 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1255 | if (s->cnx->egl.eglUnlockSurfaceKHR) { |
| 1256 | return s->cnx->egl.eglUnlockSurfaceKHR( |
| 1257 | dp->disp[s->impl].dpy, s->surface); |
| 1258 | } |
| 1259 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1260 | } |
| 1261 | |
| 1262 | EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, |
| 1263 | EGLClientBuffer buffer, const EGLint *attrib_list) |
| 1264 | { |
| 1265 | clearError(); |
| 1266 | |
| 1267 | egl_display_t const * const dp = validate_display(dpy); |
| 1268 | if (!dp) return EGL_NO_IMAGE_KHR; |
| 1269 | |
| 1270 | if (ctx != EGL_NO_CONTEXT) { |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1271 | ContextRef _c(dp, ctx); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1272 | if (!_c.get()) |
| 1273 | return setError(EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1274 | egl_context_t * const c = get_context(ctx); |
| 1275 | // since we have an EGLContext, we know which implementation to use |
| 1276 | EGLImageKHR image = c->cnx->egl.eglCreateImageKHR( |
| 1277 | dp->disp[c->impl].dpy, c->context, target, buffer, attrib_list); |
| 1278 | if (image == EGL_NO_IMAGE_KHR) |
| 1279 | return image; |
| 1280 | |
| 1281 | egl_image_t* result = new egl_image_t(dpy, ctx); |
| 1282 | result->images[c->impl] = image; |
| 1283 | return (EGLImageKHR)result; |
| 1284 | } else { |
| 1285 | // EGL_NO_CONTEXT is a valid parameter |
| 1286 | |
| 1287 | /* Since we don't have a way to know which implementation to call, |
| 1288 | * we're calling all of them. If at least one of the implementation |
| 1289 | * succeeded, this is a success. |
| 1290 | */ |
| 1291 | |
| 1292 | EGLint currentError = eglGetError(); |
| 1293 | |
| 1294 | EGLImageKHR implImages[IMPL_NUM_IMPLEMENTATIONS]; |
| 1295 | bool success = false; |
| 1296 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 1297 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1298 | implImages[i] = EGL_NO_IMAGE_KHR; |
| 1299 | if (cnx->dso) { |
| 1300 | if (cnx->egl.eglCreateImageKHR) { |
| 1301 | implImages[i] = cnx->egl.eglCreateImageKHR( |
| 1302 | dp->disp[i].dpy, ctx, target, buffer, attrib_list); |
| 1303 | if (implImages[i] != EGL_NO_IMAGE_KHR) { |
| 1304 | success = true; |
| 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | if (!success) { |
| 1311 | // failure, if there was an error when we entered this function, |
| 1312 | // the error flag must not be updated. |
| 1313 | // Otherwise, the error is whatever happened in the implementation |
| 1314 | // that faulted. |
| 1315 | if (currentError != EGL_SUCCESS) { |
| 1316 | setError(currentError, EGL_NO_IMAGE_KHR); |
| 1317 | } |
| 1318 | return EGL_NO_IMAGE_KHR; |
| 1319 | } else { |
| 1320 | // In case of success, we need to clear all error flags |
| 1321 | // (especially those caused by the implementation that didn't |
| 1322 | // succeed). TODO: we could avoid this if we knew this was |
| 1323 | // a "full" success (all implementation succeeded). |
| 1324 | eglGetError(); |
| 1325 | } |
| 1326 | |
| 1327 | egl_image_t* result = new egl_image_t(dpy, ctx); |
| 1328 | memcpy(result->images, implImages, sizeof(implImages)); |
| 1329 | return (EGLImageKHR)result; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img) |
| 1334 | { |
| 1335 | clearError(); |
| 1336 | |
| 1337 | egl_display_t const * const dp = validate_display(dpy); |
| 1338 | if (!dp) return EGL_FALSE; |
| 1339 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1340 | ImageRef _i(dp, img); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1341 | if (!_i.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1342 | |
| 1343 | egl_image_t* image = get_image(img); |
| 1344 | bool success = false; |
| 1345 | for (int i=0 ; i<IMPL_NUM_IMPLEMENTATIONS ; i++) { |
| 1346 | egl_connection_t* const cnx = &gEGLImpl[i]; |
| 1347 | if (image->images[i] != EGL_NO_IMAGE_KHR) { |
| 1348 | if (cnx->dso) { |
| 1349 | if (cnx->egl.eglDestroyImageKHR) { |
| 1350 | if (cnx->egl.eglDestroyImageKHR( |
| 1351 | dp->disp[i].dpy, image->images[i])) { |
| 1352 | success = true; |
| 1353 | } |
| 1354 | } |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | if (!success) |
| 1359 | return EGL_FALSE; |
| 1360 | |
| 1361 | _i.terminate(); |
| 1362 | |
| 1363 | return EGL_TRUE; |
| 1364 | } |
| 1365 | |
| 1366 | // ---------------------------------------------------------------------------- |
| 1367 | // EGL_EGLEXT_VERSION 5 |
| 1368 | // ---------------------------------------------------------------------------- |
| 1369 | |
| 1370 | |
| 1371 | EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list) |
| 1372 | { |
| 1373 | clearError(); |
| 1374 | |
| 1375 | egl_display_t const * const dp = validate_display(dpy); |
| 1376 | if (!dp) return EGL_NO_SYNC_KHR; |
| 1377 | |
| 1378 | EGLContext ctx = eglGetCurrentContext(); |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1379 | ContextRef _c(dp, ctx); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1380 | if (!_c.get()) |
| 1381 | return setError(EGL_BAD_CONTEXT, EGL_NO_SYNC_KHR); |
| 1382 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1383 | egl_context_t * const c = get_context(ctx); |
| 1384 | EGLSyncKHR result = EGL_NO_SYNC_KHR; |
| 1385 | if (c->cnx->egl.eglCreateSyncKHR) { |
| 1386 | EGLSyncKHR sync = c->cnx->egl.eglCreateSyncKHR( |
| 1387 | dp->disp[c->impl].dpy, type, attrib_list); |
| 1388 | if (sync == EGL_NO_SYNC_KHR) |
| 1389 | return sync; |
| 1390 | result = (egl_sync_t*)new egl_sync_t(dpy, ctx, sync); |
| 1391 | } |
| 1392 | return (EGLSyncKHR)result; |
| 1393 | } |
| 1394 | |
| 1395 | EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync) |
| 1396 | { |
| 1397 | clearError(); |
| 1398 | |
| 1399 | egl_display_t const * const dp = validate_display(dpy); |
| 1400 | if (!dp) return EGL_FALSE; |
| 1401 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1402 | SyncRef _s(dp, sync); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1403 | if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1404 | egl_sync_t* syncObject = get_sync(sync); |
| 1405 | |
| 1406 | EGLContext ctx = syncObject->context; |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1407 | ContextRef _c(dp, ctx); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1408 | if (!_c.get()) |
| 1409 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1410 | |
| 1411 | EGLBoolean result = EGL_FALSE; |
| 1412 | egl_context_t * const c = get_context(ctx); |
| 1413 | if (c->cnx->egl.eglDestroySyncKHR) { |
| 1414 | result = c->cnx->egl.eglDestroySyncKHR( |
| 1415 | dp->disp[c->impl].dpy, syncObject->sync); |
| 1416 | if (result) |
| 1417 | _s.terminate(); |
| 1418 | } |
| 1419 | return result; |
| 1420 | } |
| 1421 | |
| 1422 | EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags, EGLTimeKHR timeout) |
| 1423 | { |
| 1424 | clearError(); |
| 1425 | |
| 1426 | egl_display_t const * const dp = validate_display(dpy); |
| 1427 | if (!dp) return EGL_FALSE; |
| 1428 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1429 | SyncRef _s(dp, sync); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1430 | if (!_s.get()) return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1431 | egl_sync_t* syncObject = get_sync(sync); |
| 1432 | |
| 1433 | EGLContext ctx = syncObject->context; |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1434 | ContextRef _c(dp, ctx); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1435 | if (!_c.get()) |
| 1436 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1437 | |
| 1438 | egl_context_t * const c = get_context(ctx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1439 | if (c->cnx->egl.eglClientWaitSyncKHR) { |
| 1440 | return c->cnx->egl.eglClientWaitSyncKHR( |
| 1441 | dp->disp[c->impl].dpy, syncObject->sync, flags, timeout); |
| 1442 | } |
| 1443 | |
| 1444 | return EGL_FALSE; |
| 1445 | } |
| 1446 | |
| 1447 | EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, EGLint attribute, EGLint *value) |
| 1448 | { |
| 1449 | clearError(); |
| 1450 | |
| 1451 | egl_display_t const * const dp = validate_display(dpy); |
| 1452 | if (!dp) return EGL_FALSE; |
| 1453 | |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1454 | SyncRef _s(dp, sync); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1455 | if (!_s.get()) |
| 1456 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1457 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1458 | egl_sync_t* syncObject = get_sync(sync); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1459 | EGLContext ctx = syncObject->context; |
Mathias Agopian | f0480de | 2011-11-13 20:50:07 -0800 | [diff] [blame] | 1460 | ContextRef _c(dp, ctx); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1461 | if (!_c.get()) |
| 1462 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1463 | |
| 1464 | egl_context_t * const c = get_context(ctx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1465 | if (c->cnx->egl.eglGetSyncAttribKHR) { |
| 1466 | return c->cnx->egl.eglGetSyncAttribKHR( |
| 1467 | dp->disp[c->impl].dpy, syncObject->sync, attribute, value); |
| 1468 | } |
| 1469 | |
| 1470 | return EGL_FALSE; |
| 1471 | } |
| 1472 | |
| 1473 | // ---------------------------------------------------------------------------- |
| 1474 | // ANDROID extensions |
| 1475 | // ---------------------------------------------------------------------------- |
| 1476 | |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 1477 | /* ANDROID extensions entry-point go here */ |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1478 | |
| 1479 | // ---------------------------------------------------------------------------- |
| 1480 | // NVIDIA extensions |
| 1481 | // ---------------------------------------------------------------------------- |
| 1482 | EGLuint64NV eglGetSystemTimeFrequencyNV() |
| 1483 | { |
| 1484 | clearError(); |
| 1485 | |
| 1486 | if (egl_init_drivers() == EGL_FALSE) { |
| 1487 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1488 | } |
| 1489 | |
| 1490 | EGLuint64NV ret = 0; |
| 1491 | egl_connection_t* const cnx = &gEGLImpl[IMPL_HARDWARE]; |
| 1492 | |
| 1493 | if (cnx->dso) { |
| 1494 | if (cnx->egl.eglGetSystemTimeFrequencyNV) { |
| 1495 | return cnx->egl.eglGetSystemTimeFrequencyNV(); |
| 1496 | } |
| 1497 | } |
| 1498 | |
Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 1499 | return setErrorQuiet(EGL_BAD_DISPLAY, 0); |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | EGLuint64NV eglGetSystemTimeNV() |
| 1503 | { |
| 1504 | clearError(); |
| 1505 | |
| 1506 | if (egl_init_drivers() == EGL_FALSE) { |
| 1507 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1508 | } |
| 1509 | |
| 1510 | EGLuint64NV ret = 0; |
| 1511 | egl_connection_t* const cnx = &gEGLImpl[IMPL_HARDWARE]; |
| 1512 | |
| 1513 | if (cnx->dso) { |
| 1514 | if (cnx->egl.eglGetSystemTimeNV) { |
| 1515 | return cnx->egl.eglGetSystemTimeNV(); |
| 1516 | } |
| 1517 | } |
| 1518 | |
Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 1519 | return setErrorQuiet(EGL_BAD_DISPLAY, 0); |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1520 | } |