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 | |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 19 | #include <ctype.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <hardware/gralloc.h> |
| 24 | #include <system/window.h> |
| 25 | |
| 26 | #include <EGL/egl.h> |
| 27 | #include <EGL/eglext.h> |
| 28 | #include <GLES/gl.h> |
| 29 | #include <GLES/glext.h> |
| 30 | |
| 31 | #include <cutils/log.h> |
| 32 | #include <cutils/atomic.h> |
Mathias Agopian | 7db993a | 2012-03-25 00:49:46 -0700 | [diff] [blame] | 33 | #include <cutils/compiler.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> |
| 35 | #include <cutils/memory.h> |
| 36 | |
| 37 | #include <utils/KeyedVector.h> |
| 38 | #include <utils/SortedVector.h> |
| 39 | #include <utils/String8.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 40 | #include <utils/Trace.h> |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 41 | |
| 42 | #include "egl_impl.h" |
| 43 | #include "egl_tls.h" |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 44 | #include "glestrace.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 45 | #include "hooks.h" |
| 46 | |
| 47 | #include "egl_display.h" |
| 48 | #include "egl_impl.h" |
| 49 | #include "egl_object.h" |
| 50 | #include "egl_tls.h" |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 51 | #include "egldefs.h" |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 52 | |
| 53 | using namespace android; |
| 54 | |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
Mathias Agopian | bc2d79e | 2011-11-29 17:55:46 -0800 | [diff] [blame] | 57 | #define EGL_VERSION_HW_ANDROID 0x3143 |
| 58 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 59 | struct extention_map_t { |
| 60 | const char* name; |
| 61 | __eglMustCastToProperFunctionPointerType address; |
| 62 | }; |
| 63 | |
| 64 | static const extention_map_t sExtentionMap[] = { |
| 65 | { "eglLockSurfaceKHR", |
| 66 | (__eglMustCastToProperFunctionPointerType)&eglLockSurfaceKHR }, |
| 67 | { "eglUnlockSurfaceKHR", |
| 68 | (__eglMustCastToProperFunctionPointerType)&eglUnlockSurfaceKHR }, |
| 69 | { "eglCreateImageKHR", |
| 70 | (__eglMustCastToProperFunctionPointerType)&eglCreateImageKHR }, |
| 71 | { "eglDestroyImageKHR", |
| 72 | (__eglMustCastToProperFunctionPointerType)&eglDestroyImageKHR }, |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 73 | { "eglGetSystemTimeFrequencyNV", |
| 74 | (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeFrequencyNV }, |
| 75 | { "eglGetSystemTimeNV", |
| 76 | (__eglMustCastToProperFunctionPointerType)&eglGetSystemTimeNV }, |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | // accesses protected by sExtensionMapMutex |
| 80 | static DefaultKeyedVector<String8, __eglMustCastToProperFunctionPointerType> sGLExtentionMap; |
| 81 | static int sGLExtentionSlot = 0; |
| 82 | static pthread_mutex_t sExtensionMapMutex = PTHREAD_MUTEX_INITIALIZER; |
| 83 | |
| 84 | static void(*findProcAddress(const char* name, |
| 85 | const extention_map_t* map, size_t n))() { |
| 86 | for (uint32_t i=0 ; i<n ; i++) { |
| 87 | if (!strcmp(name, map[i].name)) { |
| 88 | return map[i].address; |
| 89 | } |
| 90 | } |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | // ---------------------------------------------------------------------------- |
| 95 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 96 | namespace android { |
| 97 | extern void setGLHooksThreadSpecific(gl_hooks_t const *value); |
| 98 | extern EGLBoolean egl_init_drivers(); |
| 99 | extern const __eglMustCastToProperFunctionPointerType gExtensionForwarders[MAX_NUMBER_OF_GL_EXTENSIONS]; |
| 100 | extern int gEGLDebugLevel; |
| 101 | extern gl_hooks_t gHooksTrace; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 102 | } // namespace android; |
| 103 | |
| 104 | // ---------------------------------------------------------------------------- |
| 105 | |
| 106 | static inline void clearError() { egl_tls_t::clearError(); } |
| 107 | static inline EGLContext getContext() { return egl_tls_t::getContext(); } |
| 108 | |
| 109 | // ---------------------------------------------------------------------------- |
| 110 | |
| 111 | EGLDisplay eglGetDisplay(EGLNativeDisplayType display) |
| 112 | { |
| 113 | clearError(); |
| 114 | |
| 115 | uint32_t index = uint32_t(display); |
| 116 | if (index >= NUM_DISPLAYS) { |
| 117 | return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); |
| 118 | } |
| 119 | |
| 120 | if (egl_init_drivers() == EGL_FALSE) { |
| 121 | return setError(EGL_BAD_PARAMETER, EGL_NO_DISPLAY); |
| 122 | } |
| 123 | |
| 124 | EGLDisplay dpy = egl_display_t::getFromNativeDisplay(display); |
| 125 | return dpy; |
| 126 | } |
| 127 | |
| 128 | // ---------------------------------------------------------------------------- |
| 129 | // Initialization |
| 130 | // ---------------------------------------------------------------------------- |
| 131 | |
| 132 | EGLBoolean eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor) |
| 133 | { |
| 134 | clearError(); |
| 135 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 136 | egl_display_ptr dp = get_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 137 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 138 | |
| 139 | EGLBoolean res = dp->initialize(major, minor); |
| 140 | |
| 141 | return res; |
| 142 | } |
| 143 | |
| 144 | EGLBoolean eglTerminate(EGLDisplay dpy) |
| 145 | { |
| 146 | // NOTE: don't unload the drivers b/c some APIs can be called |
| 147 | // after eglTerminate() has been called. eglTerminate() only |
| 148 | // terminates an EGLDisplay, not a EGL itself. |
| 149 | |
| 150 | clearError(); |
| 151 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 152 | egl_display_ptr dp = get_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 153 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 154 | |
| 155 | EGLBoolean res = dp->terminate(); |
| 156 | |
| 157 | return res; |
| 158 | } |
| 159 | |
| 160 | // ---------------------------------------------------------------------------- |
| 161 | // configuration |
| 162 | // ---------------------------------------------------------------------------- |
| 163 | |
| 164 | EGLBoolean eglGetConfigs( EGLDisplay dpy, |
| 165 | EGLConfig *configs, |
| 166 | EGLint config_size, EGLint *num_config) |
| 167 | { |
| 168 | clearError(); |
| 169 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 170 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 171 | if (!dp) return EGL_FALSE; |
| 172 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 173 | if (num_config==0) { |
| 174 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 177 | EGLBoolean res = EGL_FALSE; |
| 178 | *num_config = 0; |
| 179 | |
| 180 | egl_connection_t* const cnx = &gEGLImpl; |
| 181 | if (cnx->dso) { |
| 182 | res = cnx->egl.eglGetConfigs( |
| 183 | dp->disp.dpy, configs, config_size, num_config); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 184 | } |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 185 | |
| 186 | return res; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | EGLBoolean eglChooseConfig( EGLDisplay dpy, const EGLint *attrib_list, |
| 190 | EGLConfig *configs, EGLint config_size, |
| 191 | EGLint *num_config) |
| 192 | { |
| 193 | clearError(); |
| 194 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 195 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 196 | if (!dp) return EGL_FALSE; |
| 197 | |
| 198 | if (num_config==0) { |
| 199 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 200 | } |
| 201 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 202 | EGLBoolean res = EGL_FALSE; |
| 203 | *num_config = 0; |
| 204 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 205 | egl_connection_t* const cnx = &gEGLImpl; |
| 206 | if (cnx->dso) { |
Romain Guy | 1cffc80 | 2012-10-15 18:13:05 -0700 | [diff] [blame] | 207 | if (attrib_list) { |
| 208 | char value[PROPERTY_VALUE_MAX]; |
| 209 | property_get("debug.egl.force_msaa", value, "false"); |
| 210 | |
| 211 | if (!strcmp(value, "true")) { |
| 212 | size_t attribCount = 0; |
| 213 | EGLint attrib = attrib_list[0]; |
| 214 | |
| 215 | // Only enable MSAA if the context is OpenGL ES 2.0 and |
Romain Guy | be3c3e4 | 2012-10-15 19:25:18 -0700 | [diff] [blame] | 216 | // if no caveat is requested |
Romain Guy | 1cffc80 | 2012-10-15 18:13:05 -0700 | [diff] [blame] | 217 | const EGLint *attribRendererable = NULL; |
| 218 | const EGLint *attribCaveat = NULL; |
| 219 | |
| 220 | // Count the number of attributes and look for |
Romain Guy | be3c3e4 | 2012-10-15 19:25:18 -0700 | [diff] [blame] | 221 | // EGL_RENDERABLE_TYPE and EGL_CONFIG_CAVEAT |
Romain Guy | 1cffc80 | 2012-10-15 18:13:05 -0700 | [diff] [blame] | 222 | while (attrib != EGL_NONE) { |
| 223 | attrib = attrib_list[attribCount]; |
| 224 | switch (attrib) { |
| 225 | case EGL_RENDERABLE_TYPE: |
| 226 | attribRendererable = &attrib_list[attribCount]; |
| 227 | break; |
| 228 | case EGL_CONFIG_CAVEAT: |
| 229 | attribCaveat = &attrib_list[attribCount]; |
| 230 | break; |
| 231 | } |
| 232 | attribCount++; |
| 233 | } |
| 234 | |
| 235 | if (attribRendererable && attribRendererable[1] == EGL_OPENGL_ES2_BIT && |
| 236 | (!attribCaveat || attribCaveat[1] != EGL_NONE)) { |
| 237 | |
| 238 | // Insert 2 extra attributes to force-enable MSAA 4x |
| 239 | EGLint aaAttribs[attribCount + 4]; |
| 240 | aaAttribs[0] = EGL_SAMPLE_BUFFERS; |
| 241 | aaAttribs[1] = 1; |
| 242 | aaAttribs[2] = EGL_SAMPLES; |
| 243 | aaAttribs[3] = 4; |
| 244 | |
| 245 | memcpy(&aaAttribs[4], attrib_list, attribCount * sizeof(EGLint)); |
| 246 | |
| 247 | EGLint numConfigAA; |
| 248 | EGLBoolean resAA = cnx->egl.eglChooseConfig( |
| 249 | dp->disp.dpy, aaAttribs, configs, config_size, &numConfigAA); |
| 250 | |
| 251 | if (resAA == EGL_TRUE && numConfigAA > 0) { |
| 252 | ALOGD("Enabling MSAA 4x"); |
| 253 | *num_config = numConfigAA; |
| 254 | return resAA; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 260 | res = cnx->egl.eglChooseConfig( |
| 261 | dp->disp.dpy, attrib_list, configs, config_size, num_config); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 262 | } |
| 263 | return res; |
| 264 | } |
| 265 | |
| 266 | EGLBoolean eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, |
| 267 | EGLint attribute, EGLint *value) |
| 268 | { |
| 269 | clearError(); |
| 270 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 271 | egl_connection_t* cnx = NULL; |
| 272 | const egl_display_ptr dp = validate_display_connection(dpy, cnx); |
| 273 | if (!dp) return EGL_FALSE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 274 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 275 | return cnx->egl.eglGetConfigAttrib( |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 276 | dp->disp.dpy, config, attribute, value); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // ---------------------------------------------------------------------------- |
| 280 | // surfaces |
| 281 | // ---------------------------------------------------------------------------- |
| 282 | |
| 283 | EGLSurface eglCreateWindowSurface( EGLDisplay dpy, EGLConfig config, |
| 284 | NativeWindowType window, |
| 285 | const EGLint *attrib_list) |
| 286 | { |
| 287 | clearError(); |
| 288 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 289 | egl_connection_t* cnx = NULL; |
| 290 | egl_display_ptr dp = validate_display_connection(dpy, cnx); |
| 291 | if (dp) { |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 292 | EGLDisplay iDpy = dp->disp.dpy; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 293 | EGLint format; |
| 294 | |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 295 | if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) != OK) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 296 | ALOGE("EGLNativeWindowType %p already connected to another API", |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 297 | window); |
| 298 | return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE); |
| 299 | } |
| 300 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 301 | // set the native window's buffers format to match this config |
| 302 | if (cnx->egl.eglGetConfigAttrib(iDpy, |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 303 | config, EGL_NATIVE_VISUAL_ID, &format)) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 304 | if (format != 0) { |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 305 | int err = native_window_set_buffers_format(window, format); |
| 306 | if (err != 0) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 307 | ALOGE("error setting native window pixel format: %s (%d)", |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 308 | strerror(-err), err); |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 309 | native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); |
Jamie Gennis | bee205f | 2011-07-01 13:12:07 -0700 | [diff] [blame] | 310 | return setError(EGL_BAD_NATIVE_WINDOW, EGL_NO_SURFACE); |
| 311 | } |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Jamie Gennis | 5976946 | 2011-11-19 18:04:43 -0800 | [diff] [blame] | 315 | // the EGL spec requires that a new EGLSurface default to swap interval |
| 316 | // 1, so explicitly set that on the window here. |
| 317 | ANativeWindow* anw = reinterpret_cast<ANativeWindow*>(window); |
| 318 | anw->setSwapInterval(anw, 1); |
| 319 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 320 | EGLSurface surface = cnx->egl.eglCreateWindowSurface( |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 321 | iDpy, config, window, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 322 | if (surface != EGL_NO_SURFACE) { |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 323 | egl_surface_t* s = new egl_surface_t(dp.get(), config, window, |
| 324 | surface, cnx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 325 | return s; |
| 326 | } |
Mathias Agopian | 81a6335 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 327 | |
| 328 | // EGLSurface creation failed |
| 329 | native_window_set_buffers_format(window, 0); |
| 330 | native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 331 | } |
| 332 | return EGL_NO_SURFACE; |
| 333 | } |
| 334 | |
| 335 | EGLSurface eglCreatePixmapSurface( EGLDisplay dpy, EGLConfig config, |
| 336 | NativePixmapType pixmap, |
| 337 | const EGLint *attrib_list) |
| 338 | { |
| 339 | clearError(); |
| 340 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 341 | egl_connection_t* cnx = NULL; |
| 342 | egl_display_ptr dp = validate_display_connection(dpy, cnx); |
| 343 | if (dp) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 344 | EGLSurface surface = cnx->egl.eglCreatePixmapSurface( |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 345 | dp->disp.dpy, config, pixmap, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 346 | if (surface != EGL_NO_SURFACE) { |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 347 | egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, |
| 348 | surface, cnx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 349 | return s; |
| 350 | } |
| 351 | } |
| 352 | return EGL_NO_SURFACE; |
| 353 | } |
| 354 | |
| 355 | EGLSurface eglCreatePbufferSurface( EGLDisplay dpy, EGLConfig config, |
| 356 | const EGLint *attrib_list) |
| 357 | { |
| 358 | clearError(); |
| 359 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 360 | egl_connection_t* cnx = NULL; |
| 361 | egl_display_ptr dp = validate_display_connection(dpy, cnx); |
| 362 | if (dp) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 363 | EGLSurface surface = cnx->egl.eglCreatePbufferSurface( |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 364 | dp->disp.dpy, config, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 365 | if (surface != EGL_NO_SURFACE) { |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 366 | egl_surface_t* s = new egl_surface_t(dp.get(), config, NULL, |
| 367 | surface, cnx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 368 | return s; |
| 369 | } |
| 370 | } |
| 371 | return EGL_NO_SURFACE; |
| 372 | } |
| 373 | |
| 374 | EGLBoolean eglDestroySurface(EGLDisplay dpy, EGLSurface surface) |
| 375 | { |
| 376 | clearError(); |
| 377 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 378 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 379 | if (!dp) return EGL_FALSE; |
| 380 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 381 | SurfaceRef _s(dp.get(), surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 382 | if (!_s.get()) |
| 383 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 384 | |
| 385 | egl_surface_t * const s = get_surface(surface); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 386 | EGLBoolean result = s->cnx->egl.eglDestroySurface(dp->disp.dpy, s->surface); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 387 | if (result == EGL_TRUE) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 388 | _s.terminate(); |
| 389 | } |
| 390 | return result; |
| 391 | } |
| 392 | |
| 393 | EGLBoolean eglQuerySurface( EGLDisplay dpy, EGLSurface surface, |
| 394 | EGLint attribute, EGLint *value) |
| 395 | { |
| 396 | clearError(); |
| 397 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 398 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 399 | if (!dp) return EGL_FALSE; |
| 400 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 401 | SurfaceRef _s(dp.get(), surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 402 | if (!_s.get()) |
| 403 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 404 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 405 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 406 | return s->cnx->egl.eglQuerySurface( |
| 407 | dp->disp.dpy, s->surface, attribute, value); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 410 | void EGLAPI eglBeginFrame(EGLDisplay dpy, EGLSurface surface) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 411 | ATRACE_CALL(); |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 412 | clearError(); |
| 413 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 414 | const egl_display_ptr dp = validate_display(dpy); |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 415 | if (!dp) { |
| 416 | return; |
| 417 | } |
| 418 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 419 | SurfaceRef _s(dp.get(), surface); |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 420 | if (!_s.get()) { |
| 421 | setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | int64_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC); |
| 426 | |
| 427 | egl_surface_t const * const s = get_surface(surface); |
| 428 | native_window_set_buffers_timestamp(s->win.get(), timestamp); |
| 429 | } |
| 430 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 431 | // ---------------------------------------------------------------------------- |
| 432 | // Contexts |
| 433 | // ---------------------------------------------------------------------------- |
| 434 | |
| 435 | EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, |
| 436 | EGLContext share_list, const EGLint *attrib_list) |
| 437 | { |
| 438 | clearError(); |
| 439 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 440 | egl_connection_t* cnx = NULL; |
| 441 | const egl_display_ptr dp = validate_display_connection(dpy, cnx); |
| 442 | if (dpy) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 443 | if (share_list != EGL_NO_CONTEXT) { |
| 444 | egl_context_t* const c = get_context(share_list); |
| 445 | share_list = c->context; |
| 446 | } |
| 447 | EGLContext context = cnx->egl.eglCreateContext( |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 448 | dp->disp.dpy, config, share_list, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 449 | if (context != EGL_NO_CONTEXT) { |
| 450 | // figure out if it's a GLESv1 or GLESv2 |
| 451 | int version = 0; |
| 452 | if (attrib_list) { |
| 453 | while (*attrib_list != EGL_NONE) { |
| 454 | GLint attr = *attrib_list++; |
| 455 | GLint value = *attrib_list++; |
| 456 | if (attr == EGL_CONTEXT_CLIENT_VERSION) { |
| 457 | if (value == 1) { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 458 | version = egl_connection_t::GLESv1_INDEX; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 459 | } else if (value == 2) { |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 460 | version = egl_connection_t::GLESv2_INDEX; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | }; |
| 464 | } |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 465 | egl_context_t* c = new egl_context_t(dpy, context, config, cnx, |
| 466 | version); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 467 | #if EGL_TRACE |
| 468 | if (gEGLDebugLevel > 0) |
| 469 | GLTrace_eglCreateContext(version, c); |
| 470 | #endif |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 471 | return c; |
Mathias Agopian | 500407a | 2012-09-24 17:57:48 -0700 | [diff] [blame] | 472 | } else { |
| 473 | EGLint error = eglGetError(); |
| 474 | ALOGE_IF(error == EGL_SUCCESS, |
| 475 | "eglCreateContext(%p, %p, %p, %p) returned EGL_NO_CONTEXT " |
| 476 | "but no EGL error!", |
| 477 | dpy, config, share_list, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | return EGL_NO_CONTEXT; |
| 481 | } |
| 482 | |
| 483 | EGLBoolean eglDestroyContext(EGLDisplay dpy, EGLContext ctx) |
| 484 | { |
| 485 | clearError(); |
| 486 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 487 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 488 | if (!dp) |
| 489 | return EGL_FALSE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 490 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 491 | ContextRef _c(dp.get(), ctx); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 492 | if (!_c.get()) |
| 493 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 494 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 495 | egl_context_t * const c = get_context(ctx); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 496 | EGLBoolean result = c->cnx->egl.eglDestroyContext(dp->disp.dpy, c->context); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 497 | if (result == EGL_TRUE) { |
| 498 | _c.terminate(); |
| 499 | } |
| 500 | return result; |
| 501 | } |
| 502 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 503 | EGLBoolean eglMakeCurrent( EGLDisplay dpy, EGLSurface draw, |
| 504 | EGLSurface read, EGLContext ctx) |
| 505 | { |
| 506 | clearError(); |
| 507 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 508 | egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 509 | if (!dp) return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 510 | |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 511 | // If ctx is not EGL_NO_CONTEXT, read is not EGL_NO_SURFACE, or draw is not |
| 512 | // EGL_NO_SURFACE, then an EGL_NOT_INITIALIZED error is generated if dpy is |
| 513 | // a valid but uninitialized display. |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 514 | if ( (ctx != EGL_NO_CONTEXT) || (read != EGL_NO_SURFACE) || |
| 515 | (draw != EGL_NO_SURFACE) ) { |
| 516 | if (!dp->isReady()) return setError(EGL_NOT_INITIALIZED, EGL_FALSE); |
| 517 | } |
| 518 | |
| 519 | // get a reference to the object passed in |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 520 | ContextRef _c(dp.get(), ctx); |
| 521 | SurfaceRef _d(dp.get(), draw); |
| 522 | SurfaceRef _r(dp.get(), read); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 523 | |
| 524 | // validate the context (if not EGL_NO_CONTEXT) |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 525 | if ((ctx != EGL_NO_CONTEXT) && !_c.get()) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 526 | // EGL_NO_CONTEXT is valid |
| 527 | return EGL_FALSE; |
| 528 | } |
| 529 | |
| 530 | // these are the underlying implementation's object |
| 531 | EGLContext impl_ctx = EGL_NO_CONTEXT; |
| 532 | EGLSurface impl_draw = EGL_NO_SURFACE; |
| 533 | EGLSurface impl_read = EGL_NO_SURFACE; |
| 534 | |
| 535 | // these are our objects structs passed in |
| 536 | egl_context_t * c = NULL; |
| 537 | egl_surface_t const * d = NULL; |
| 538 | egl_surface_t const * r = NULL; |
| 539 | |
| 540 | // these are the current objects structs |
| 541 | egl_context_t * cur_c = get_context(getContext()); |
| 542 | |
| 543 | if (ctx != EGL_NO_CONTEXT) { |
| 544 | c = get_context(ctx); |
| 545 | impl_ctx = c->context; |
| 546 | } else { |
| 547 | // no context given, use the implementation of the current context |
| 548 | if (cur_c == NULL) { |
| 549 | // no current context |
| 550 | if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE) { |
| 551 | // calling eglMakeCurrent( ..., !=0, !=0, EGL_NO_CONTEXT); |
| 552 | return setError(EGL_BAD_MATCH, EGL_FALSE); |
| 553 | } |
| 554 | // not an error, there is just no current context. |
| 555 | return EGL_TRUE; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // retrieve the underlying implementation's draw EGLSurface |
| 560 | if (draw != EGL_NO_SURFACE) { |
| 561 | d = get_surface(draw); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 562 | impl_draw = d->surface; |
| 563 | } |
| 564 | |
| 565 | // retrieve the underlying implementation's read EGLSurface |
| 566 | if (read != EGL_NO_SURFACE) { |
| 567 | r = get_surface(read); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 568 | impl_read = r->surface; |
| 569 | } |
| 570 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 571 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 572 | EGLBoolean result = dp->makeCurrent(c, cur_c, |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 573 | draw, read, ctx, |
| 574 | impl_draw, impl_read, impl_ctx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 575 | |
| 576 | if (result == EGL_TRUE) { |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 577 | if (c) { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 578 | setGLHooksThreadSpecific(c->cnx->hooks[c->version]); |
| 579 | egl_tls_t::setContext(ctx); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 580 | #if EGL_TRACE |
| 581 | if (gEGLDebugLevel > 0) |
Siva Velusamy | 93a826f | 2011-12-14 12:19:56 -0800 | [diff] [blame] | 582 | GLTrace_eglMakeCurrent(c->version, c->cnx->hooks[c->version], ctx); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 583 | #endif |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 584 | _c.acquire(); |
| 585 | _r.acquire(); |
| 586 | _d.acquire(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 587 | } else { |
| 588 | setGLHooksThreadSpecific(&gHooksNoContext); |
| 589 | egl_tls_t::setContext(EGL_NO_CONTEXT); |
| 590 | } |
Mathias Agopian | 5fecea7 | 2011-08-25 18:38:24 -0700 | [diff] [blame] | 591 | } else { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 592 | // this will ALOGE the error |
Mathias Agopian | 5fecea7 | 2011-08-25 18:38:24 -0700 | [diff] [blame] | 593 | result = setError(c->cnx->egl.eglGetError(), EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 594 | } |
| 595 | return result; |
| 596 | } |
| 597 | |
| 598 | |
| 599 | EGLBoolean eglQueryContext( EGLDisplay dpy, EGLContext ctx, |
| 600 | EGLint attribute, EGLint *value) |
| 601 | { |
| 602 | clearError(); |
| 603 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 604 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 605 | if (!dp) return EGL_FALSE; |
| 606 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 607 | ContextRef _c(dp.get(), ctx); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 608 | if (!_c.get()) return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 609 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 610 | egl_context_t * const c = get_context(ctx); |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 611 | return c->cnx->egl.eglQueryContext( |
| 612 | dp->disp.dpy, c->context, attribute, value); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 613 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | EGLContext eglGetCurrentContext(void) |
| 617 | { |
| 618 | // could be called before eglInitialize(), but we wouldn't have a context |
| 619 | // then, and this function would correctly return EGL_NO_CONTEXT. |
| 620 | |
| 621 | clearError(); |
| 622 | |
| 623 | EGLContext ctx = getContext(); |
| 624 | return ctx; |
| 625 | } |
| 626 | |
| 627 | EGLSurface eglGetCurrentSurface(EGLint readdraw) |
| 628 | { |
| 629 | // could be called before eglInitialize(), but we wouldn't have a context |
| 630 | // then, and this function would correctly return EGL_NO_SURFACE. |
| 631 | |
| 632 | clearError(); |
| 633 | |
| 634 | EGLContext ctx = getContext(); |
| 635 | if (ctx) { |
| 636 | egl_context_t const * const c = get_context(ctx); |
| 637 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); |
| 638 | switch (readdraw) { |
| 639 | case EGL_READ: return c->read; |
| 640 | case EGL_DRAW: return c->draw; |
| 641 | default: return setError(EGL_BAD_PARAMETER, EGL_NO_SURFACE); |
| 642 | } |
| 643 | } |
| 644 | return EGL_NO_SURFACE; |
| 645 | } |
| 646 | |
| 647 | EGLDisplay eglGetCurrentDisplay(void) |
| 648 | { |
| 649 | // could be called before eglInitialize(), but we wouldn't have a context |
| 650 | // then, and this function would correctly return EGL_NO_DISPLAY. |
| 651 | |
| 652 | clearError(); |
| 653 | |
| 654 | EGLContext ctx = getContext(); |
| 655 | if (ctx) { |
| 656 | egl_context_t const * const c = get_context(ctx); |
| 657 | if (!c) return setError(EGL_BAD_CONTEXT, EGL_NO_SURFACE); |
| 658 | return c->dpy; |
| 659 | } |
| 660 | return EGL_NO_DISPLAY; |
| 661 | } |
| 662 | |
| 663 | EGLBoolean eglWaitGL(void) |
| 664 | { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 665 | clearError(); |
| 666 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 667 | egl_connection_t* const cnx = &gEGLImpl; |
| 668 | if (!cnx->dso) |
| 669 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 670 | |
| 671 | return cnx->egl.eglWaitGL(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | EGLBoolean eglWaitNative(EGLint engine) |
| 675 | { |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 676 | clearError(); |
| 677 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 678 | egl_connection_t* const cnx = &gEGLImpl; |
| 679 | if (!cnx->dso) |
| 680 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 681 | |
| 682 | return cnx->egl.eglWaitNative(engine); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | EGLint eglGetError(void) |
| 686 | { |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 687 | EGLint err = EGL_SUCCESS; |
| 688 | egl_connection_t* const cnx = &gEGLImpl; |
| 689 | if (cnx->dso) { |
| 690 | err = cnx->egl.eglGetError(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 691 | } |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 692 | if (err == EGL_SUCCESS) { |
| 693 | err = egl_tls_t::getError(); |
| 694 | } |
| 695 | return err; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 698 | __eglMustCastToProperFunctionPointerType eglGetProcAddress(const char *procname) |
| 699 | { |
| 700 | // eglGetProcAddress() could be the very first function called |
| 701 | // in which case we must make sure we've initialized ourselves, this |
| 702 | // happens the first time egl_get_display() is called. |
| 703 | |
| 704 | clearError(); |
| 705 | |
| 706 | if (egl_init_drivers() == EGL_FALSE) { |
| 707 | setError(EGL_BAD_PARAMETER, NULL); |
| 708 | return NULL; |
| 709 | } |
| 710 | |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 711 | // These extensions should not be exposed to applications. They're used |
| 712 | // internally by the Android EGL layer. |
| 713 | if (!strcmp(procname, "eglSetBlobCacheFuncsANDROID") || |
Jamie Gennis | 331841b | 2012-09-06 14:52:00 -0700 | [diff] [blame] | 714 | !strcmp(procname, "eglDupNativeFenceFDANDROID") || |
Jamie Gennis | 010dd4f | 2012-09-09 17:46:17 -0700 | [diff] [blame] | 715 | !strcmp(procname, "eglWaitSyncANDROID") || |
Jesse Hall | 2583859 | 2012-04-05 15:53:28 -0700 | [diff] [blame] | 716 | !strcmp(procname, "eglHibernateProcessIMG") || |
| 717 | !strcmp(procname, "eglAwakenProcessIMG")) { |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 718 | return NULL; |
| 719 | } |
| 720 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 721 | __eglMustCastToProperFunctionPointerType addr; |
| 722 | addr = findProcAddress(procname, sExtentionMap, NELEM(sExtentionMap)); |
| 723 | if (addr) return addr; |
| 724 | |
Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 725 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 726 | // this protects accesses to sGLExtentionMap and sGLExtentionSlot |
| 727 | pthread_mutex_lock(&sExtensionMapMutex); |
| 728 | |
| 729 | /* |
| 730 | * Since eglGetProcAddress() is not associated to anything, it needs |
| 731 | * to return a function pointer that "works" regardless of what |
| 732 | * the current context is. |
| 733 | * |
| 734 | * For this reason, we return a "forwarder", a small stub that takes |
| 735 | * care of calling the function associated with the context |
| 736 | * currently bound. |
| 737 | * |
| 738 | * We first look for extensions we've already resolved, if we're seeing |
| 739 | * this extension for the first time, we go through all our |
| 740 | * implementations and call eglGetProcAddress() and record the |
| 741 | * result in the appropriate implementation hooks and return the |
| 742 | * address of the forwarder corresponding to that hook set. |
| 743 | * |
| 744 | */ |
| 745 | |
| 746 | const String8 name(procname); |
| 747 | addr = sGLExtentionMap.valueFor(name); |
| 748 | const int slot = sGLExtentionSlot; |
| 749 | |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 750 | ALOGE_IF(slot >= MAX_NUMBER_OF_GL_EXTENSIONS, |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 751 | "no more slots for eglGetProcAddress(\"%s\")", |
| 752 | procname); |
| 753 | |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 754 | #if EGL_TRACE |
| 755 | gl_hooks_t *debugHooks = GLTrace_getGLHooks(); |
| 756 | #endif |
| 757 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 758 | if (!addr && (slot < MAX_NUMBER_OF_GL_EXTENSIONS)) { |
| 759 | bool found = false; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 760 | |
| 761 | egl_connection_t* const cnx = &gEGLImpl; |
| 762 | if (cnx->dso && cnx->egl.eglGetProcAddress) { |
| 763 | found = true; |
| 764 | // Extensions are independent of the bound context |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 765 | cnx->hooks[egl_connection_t::GLESv1_INDEX]->ext.extensions[slot] = |
| 766 | cnx->hooks[egl_connection_t::GLESv2_INDEX]->ext.extensions[slot] = |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 767 | #if EGL_TRACE |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 768 | debugHooks->ext.extensions[slot] = |
| 769 | gHooksTrace.ext.extensions[slot] = |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 770 | #endif |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 771 | cnx->egl.eglGetProcAddress(procname); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 772 | } |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 773 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 774 | if (found) { |
| 775 | addr = gExtensionForwarders[slot]; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 776 | sGLExtentionMap.add(name, addr); |
| 777 | sGLExtentionSlot++; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | pthread_mutex_unlock(&sExtensionMapMutex); |
| 782 | return addr; |
| 783 | } |
| 784 | |
Jamie Gennis | 28ef8d7 | 2012-04-05 20:34:54 -0700 | [diff] [blame] | 785 | class FrameCompletionThread : public Thread { |
| 786 | public: |
| 787 | |
| 788 | static void queueSync(EGLSyncKHR sync) { |
| 789 | static sp<FrameCompletionThread> thread(new FrameCompletionThread); |
| 790 | static bool running = false; |
| 791 | if (!running) { |
| 792 | thread->run("GPUFrameCompletion"); |
| 793 | running = true; |
| 794 | } |
| 795 | { |
| 796 | Mutex::Autolock lock(thread->mMutex); |
| 797 | ScopedTrace st(ATRACE_TAG, String8::format("kicked off frame %d", |
| 798 | thread->mFramesQueued).string()); |
| 799 | thread->mQueue.push_back(sync); |
| 800 | thread->mCondition.signal(); |
| 801 | thread->mFramesQueued++; |
| 802 | ATRACE_INT("GPU Frames Outstanding", thread->mQueue.size()); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | private: |
| 807 | FrameCompletionThread() : mFramesQueued(0), mFramesCompleted(0) {} |
| 808 | |
| 809 | virtual bool threadLoop() { |
| 810 | EGLSyncKHR sync; |
| 811 | uint32_t frameNum; |
| 812 | { |
| 813 | Mutex::Autolock lock(mMutex); |
| 814 | while (mQueue.isEmpty()) { |
| 815 | mCondition.wait(mMutex); |
| 816 | } |
| 817 | sync = mQueue[0]; |
| 818 | frameNum = mFramesCompleted; |
| 819 | } |
| 820 | EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 821 | { |
| 822 | ScopedTrace st(ATRACE_TAG, String8::format("waiting for frame %d", |
| 823 | frameNum).string()); |
| 824 | EGLint result = eglClientWaitSyncKHR(dpy, sync, 0, EGL_FOREVER_KHR); |
| 825 | if (result == EGL_FALSE) { |
| 826 | ALOGE("FrameCompletion: error waiting for fence: %#x", eglGetError()); |
| 827 | } else if (result == EGL_TIMEOUT_EXPIRED_KHR) { |
| 828 | ALOGE("FrameCompletion: timeout waiting for fence"); |
| 829 | } |
| 830 | eglDestroySyncKHR(dpy, sync); |
| 831 | } |
| 832 | { |
| 833 | Mutex::Autolock lock(mMutex); |
| 834 | mQueue.removeAt(0); |
| 835 | mFramesCompleted++; |
| 836 | ATRACE_INT("GPU Frames Outstanding", mQueue.size()); |
| 837 | } |
| 838 | return true; |
| 839 | } |
| 840 | |
| 841 | uint32_t mFramesQueued; |
| 842 | uint32_t mFramesCompleted; |
| 843 | Vector<EGLSyncKHR> mQueue; |
| 844 | Condition mCondition; |
| 845 | Mutex mMutex; |
| 846 | }; |
| 847 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 848 | EGLBoolean eglSwapBuffers(EGLDisplay dpy, EGLSurface draw) |
| 849 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 850 | ATRACE_CALL(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 851 | clearError(); |
| 852 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 853 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 854 | if (!dp) return EGL_FALSE; |
| 855 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 856 | SurfaceRef _s(dp.get(), draw); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 857 | if (!_s.get()) |
| 858 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 859 | |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 860 | #if EGL_TRACE |
| 861 | if (gEGLDebugLevel > 0) |
| 862 | GLTrace_eglSwapBuffers(dpy, draw); |
| 863 | #endif |
| 864 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 865 | egl_surface_t const * const s = get_surface(draw); |
Mathias Agopian | 7db993a | 2012-03-25 00:49:46 -0700 | [diff] [blame] | 866 | |
| 867 | if (CC_UNLIKELY(dp->finishOnSwap)) { |
| 868 | uint32_t pixel; |
| 869 | egl_context_t * const c = get_context( egl_tls_t::getContext() ); |
| 870 | if (c) { |
| 871 | // glReadPixels() ensures that the frame is complete |
| 872 | s->cnx->hooks[c->version]->gl.glReadPixels(0,0,1,1, |
| 873 | GL_RGBA,GL_UNSIGNED_BYTE,&pixel); |
| 874 | } |
| 875 | } |
| 876 | |
Jamie Gennis | 28ef8d7 | 2012-04-05 20:34:54 -0700 | [diff] [blame] | 877 | EGLBoolean result = s->cnx->egl.eglSwapBuffers(dp->disp.dpy, s->surface); |
| 878 | |
| 879 | if (CC_UNLIKELY(dp->traceGpuCompletion)) { |
| 880 | EGLSyncKHR sync = EGL_NO_SYNC_KHR; |
| 881 | { |
| 882 | sync = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, NULL); |
| 883 | } |
| 884 | if (sync != EGL_NO_SYNC_KHR) { |
| 885 | FrameCompletionThread::queueSync(sync); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | return result; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | EGLBoolean eglCopyBuffers( EGLDisplay dpy, EGLSurface surface, |
| 893 | NativePixmapType target) |
| 894 | { |
| 895 | clearError(); |
| 896 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 897 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 898 | if (!dp) return EGL_FALSE; |
| 899 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 900 | SurfaceRef _s(dp.get(), surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 901 | if (!_s.get()) |
| 902 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 903 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 904 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 905 | return s->cnx->egl.eglCopyBuffers(dp->disp.dpy, s->surface, target); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | const char* eglQueryString(EGLDisplay dpy, EGLint name) |
| 909 | { |
| 910 | clearError(); |
| 911 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 912 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 913 | if (!dp) return (const char *) NULL; |
| 914 | |
| 915 | switch (name) { |
| 916 | case EGL_VENDOR: |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 917 | return dp->getVendorString(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 918 | case EGL_VERSION: |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 919 | return dp->getVersionString(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 920 | case EGL_EXTENSIONS: |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 921 | return dp->getExtensionString(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 922 | case EGL_CLIENT_APIS: |
Mathias Agopian | 4b9511c | 2011-11-13 23:52:47 -0800 | [diff] [blame] | 923 | return dp->getClientApiString(); |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 924 | case EGL_VERSION_HW_ANDROID: |
| 925 | return dp->disp.queryString.version; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 926 | } |
| 927 | return setError(EGL_BAD_PARAMETER, (const char *)0); |
| 928 | } |
| 929 | |
| 930 | |
| 931 | // ---------------------------------------------------------------------------- |
| 932 | // EGL 1.1 |
| 933 | // ---------------------------------------------------------------------------- |
| 934 | |
| 935 | EGLBoolean eglSurfaceAttrib( |
| 936 | EGLDisplay dpy, EGLSurface surface, EGLint attribute, EGLint value) |
| 937 | { |
| 938 | clearError(); |
| 939 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 940 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 941 | if (!dp) return EGL_FALSE; |
| 942 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 943 | SurfaceRef _s(dp.get(), surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 944 | if (!_s.get()) |
| 945 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 946 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 947 | egl_surface_t const * const s = get_surface(surface); |
| 948 | if (s->cnx->egl.eglSurfaceAttrib) { |
| 949 | return s->cnx->egl.eglSurfaceAttrib( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 950 | dp->disp.dpy, s->surface, attribute, value); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 951 | } |
| 952 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 953 | } |
| 954 | |
| 955 | EGLBoolean eglBindTexImage( |
| 956 | EGLDisplay dpy, EGLSurface surface, EGLint buffer) |
| 957 | { |
| 958 | clearError(); |
| 959 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 960 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 961 | if (!dp) return EGL_FALSE; |
| 962 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 963 | SurfaceRef _s(dp.get(), surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 964 | if (!_s.get()) |
| 965 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 966 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 967 | egl_surface_t const * const s = get_surface(surface); |
| 968 | if (s->cnx->egl.eglBindTexImage) { |
| 969 | return s->cnx->egl.eglBindTexImage( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 970 | dp->disp.dpy, s->surface, buffer); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 971 | } |
| 972 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 973 | } |
| 974 | |
| 975 | EGLBoolean eglReleaseTexImage( |
| 976 | EGLDisplay dpy, EGLSurface surface, EGLint buffer) |
| 977 | { |
| 978 | clearError(); |
| 979 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 980 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 981 | if (!dp) return EGL_FALSE; |
| 982 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 983 | SurfaceRef _s(dp.get(), surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 984 | if (!_s.get()) |
| 985 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 986 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 987 | egl_surface_t const * const s = get_surface(surface); |
| 988 | if (s->cnx->egl.eglReleaseTexImage) { |
| 989 | return s->cnx->egl.eglReleaseTexImage( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 990 | dp->disp.dpy, s->surface, buffer); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 991 | } |
| 992 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
| 993 | } |
| 994 | |
| 995 | EGLBoolean eglSwapInterval(EGLDisplay dpy, EGLint interval) |
| 996 | { |
| 997 | clearError(); |
| 998 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 999 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1000 | if (!dp) return EGL_FALSE; |
| 1001 | |
| 1002 | EGLBoolean res = EGL_TRUE; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1003 | egl_connection_t* const cnx = &gEGLImpl; |
| 1004 | if (cnx->dso && cnx->egl.eglSwapInterval) { |
| 1005 | res = cnx->egl.eglSwapInterval(dp->disp.dpy, interval); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1006 | } |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1007 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1008 | return res; |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | // ---------------------------------------------------------------------------- |
| 1013 | // EGL 1.2 |
| 1014 | // ---------------------------------------------------------------------------- |
| 1015 | |
| 1016 | EGLBoolean eglWaitClient(void) |
| 1017 | { |
| 1018 | clearError(); |
| 1019 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1020 | egl_connection_t* const cnx = &gEGLImpl; |
| 1021 | if (!cnx->dso) |
| 1022 | return setError(EGL_BAD_CONTEXT, EGL_FALSE); |
| 1023 | |
| 1024 | EGLBoolean res; |
| 1025 | if (cnx->egl.eglWaitClient) { |
| 1026 | res = cnx->egl.eglWaitClient(); |
| 1027 | } else { |
| 1028 | res = cnx->egl.eglWaitGL(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1029 | } |
| 1030 | return res; |
| 1031 | } |
| 1032 | |
| 1033 | EGLBoolean eglBindAPI(EGLenum api) |
| 1034 | { |
| 1035 | clearError(); |
| 1036 | |
| 1037 | if (egl_init_drivers() == EGL_FALSE) { |
| 1038 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1039 | } |
| 1040 | |
| 1041 | // bind this API on all EGLs |
| 1042 | EGLBoolean res = EGL_TRUE; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1043 | egl_connection_t* const cnx = &gEGLImpl; |
| 1044 | if (cnx->dso && cnx->egl.eglBindAPI) { |
| 1045 | res = cnx->egl.eglBindAPI(api); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1046 | } |
| 1047 | return res; |
| 1048 | } |
| 1049 | |
| 1050 | EGLenum eglQueryAPI(void) |
| 1051 | { |
| 1052 | clearError(); |
| 1053 | |
| 1054 | if (egl_init_drivers() == EGL_FALSE) { |
| 1055 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1056 | } |
| 1057 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1058 | egl_connection_t* const cnx = &gEGLImpl; |
| 1059 | if (cnx->dso && cnx->egl.eglQueryAPI) { |
| 1060 | return cnx->egl.eglQueryAPI(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1061 | } |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1062 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1063 | // or, it can only be OpenGL ES |
| 1064 | return EGL_OPENGL_ES_API; |
| 1065 | } |
| 1066 | |
| 1067 | EGLBoolean eglReleaseThread(void) |
| 1068 | { |
| 1069 | clearError(); |
| 1070 | |
| 1071 | // If there is context bound to the thread, release it |
Mathias Agopian | fb87e54 | 2012-01-30 18:20:52 -0800 | [diff] [blame] | 1072 | egl_display_t::loseCurrent(get_context(getContext())); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1073 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1074 | egl_connection_t* const cnx = &gEGLImpl; |
| 1075 | if (cnx->dso && cnx->egl.eglReleaseThread) { |
| 1076 | cnx->egl.eglReleaseThread(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1077 | } |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1078 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1079 | egl_tls_t::clearTLS(); |
Siva Velusamy | 0469dd6 | 2011-11-30 15:05:37 -0800 | [diff] [blame] | 1080 | #if EGL_TRACE |
| 1081 | if (gEGLDebugLevel > 0) |
| 1082 | GLTrace_eglReleaseThread(); |
| 1083 | #endif |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1084 | return EGL_TRUE; |
| 1085 | } |
| 1086 | |
| 1087 | EGLSurface eglCreatePbufferFromClientBuffer( |
| 1088 | EGLDisplay dpy, EGLenum buftype, EGLClientBuffer buffer, |
| 1089 | EGLConfig config, const EGLint *attrib_list) |
| 1090 | { |
| 1091 | clearError(); |
| 1092 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1093 | egl_connection_t* cnx = NULL; |
| 1094 | const egl_display_ptr dp = validate_display_connection(dpy, cnx); |
| 1095 | if (!dp) return EGL_FALSE; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1096 | if (cnx->egl.eglCreatePbufferFromClientBuffer) { |
| 1097 | return cnx->egl.eglCreatePbufferFromClientBuffer( |
Mathias Agopian | 7773c43 | 2012-02-13 20:06:08 -0800 | [diff] [blame] | 1098 | dp->disp.dpy, buftype, buffer, config, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1099 | } |
| 1100 | return setError(EGL_BAD_CONFIG, EGL_NO_SURFACE); |
| 1101 | } |
| 1102 | |
| 1103 | // ---------------------------------------------------------------------------- |
| 1104 | // EGL_EGLEXT_VERSION 3 |
| 1105 | // ---------------------------------------------------------------------------- |
| 1106 | |
| 1107 | EGLBoolean eglLockSurfaceKHR(EGLDisplay dpy, EGLSurface surface, |
| 1108 | const EGLint *attrib_list) |
| 1109 | { |
| 1110 | clearError(); |
| 1111 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1112 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1113 | if (!dp) return EGL_FALSE; |
| 1114 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1115 | SurfaceRef _s(dp.get(), surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1116 | if (!_s.get()) |
| 1117 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1118 | |
| 1119 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1120 | if (s->cnx->egl.eglLockSurfaceKHR) { |
| 1121 | return s->cnx->egl.eglLockSurfaceKHR( |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1122 | dp->disp.dpy, s->surface, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1123 | } |
| 1124 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1125 | } |
| 1126 | |
| 1127 | EGLBoolean eglUnlockSurfaceKHR(EGLDisplay dpy, EGLSurface surface) |
| 1128 | { |
| 1129 | clearError(); |
| 1130 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1131 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1132 | if (!dp) return EGL_FALSE; |
| 1133 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1134 | SurfaceRef _s(dp.get(), surface); |
Mathias Agopian | 5b287a6 | 2011-05-16 18:58:55 -0700 | [diff] [blame] | 1135 | if (!_s.get()) |
| 1136 | return setError(EGL_BAD_SURFACE, EGL_FALSE); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1137 | |
| 1138 | egl_surface_t const * const s = get_surface(surface); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1139 | if (s->cnx->egl.eglUnlockSurfaceKHR) { |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1140 | return s->cnx->egl.eglUnlockSurfaceKHR(dp->disp.dpy, s->surface); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1141 | } |
| 1142 | return setError(EGL_BAD_DISPLAY, EGL_FALSE); |
| 1143 | } |
| 1144 | |
| 1145 | EGLImageKHR eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target, |
| 1146 | EGLClientBuffer buffer, const EGLint *attrib_list) |
| 1147 | { |
| 1148 | clearError(); |
| 1149 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1150 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1151 | if (!dp) return EGL_NO_IMAGE_KHR; |
| 1152 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1153 | ContextRef _c(dp.get(), ctx); |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1154 | egl_context_t * const c = _c.get(); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1155 | |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1156 | EGLImageKHR result = EGL_NO_IMAGE_KHR; |
| 1157 | egl_connection_t* const cnx = &gEGLImpl; |
| 1158 | if (cnx->dso && cnx->egl.eglCreateImageKHR) { |
| 1159 | result = cnx->egl.eglCreateImageKHR( |
| 1160 | dp->disp.dpy, |
| 1161 | c ? c->context : EGL_NO_CONTEXT, |
| 1162 | target, buffer, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1163 | } |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1164 | return result; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | EGLBoolean eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR img) |
| 1168 | { |
| 1169 | clearError(); |
| 1170 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1171 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1172 | if (!dp) return EGL_FALSE; |
| 1173 | |
Steven Holte | 646a5c5 | 2012-06-04 20:02:11 -0700 | [diff] [blame] | 1174 | EGLBoolean result = EGL_FALSE; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1175 | egl_connection_t* const cnx = &gEGLImpl; |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1176 | if (cnx->dso && cnx->egl.eglDestroyImageKHR) { |
Steven Holte | 646a5c5 | 2012-06-04 20:02:11 -0700 | [diff] [blame] | 1177 | result = cnx->egl.eglDestroyImageKHR(dp->disp.dpy, img); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1178 | } |
Steven Holte | 646a5c5 | 2012-06-04 20:02:11 -0700 | [diff] [blame] | 1179 | return result; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | // ---------------------------------------------------------------------------- |
| 1183 | // EGL_EGLEXT_VERSION 5 |
| 1184 | // ---------------------------------------------------------------------------- |
| 1185 | |
| 1186 | |
| 1187 | EGLSyncKHR eglCreateSyncKHR(EGLDisplay dpy, EGLenum type, const EGLint *attrib_list) |
| 1188 | { |
| 1189 | clearError(); |
| 1190 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1191 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1192 | if (!dp) return EGL_NO_SYNC_KHR; |
| 1193 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1194 | EGLSyncKHR result = EGL_NO_SYNC_KHR; |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1195 | egl_connection_t* const cnx = &gEGLImpl; |
| 1196 | if (cnx->dso && cnx->egl.eglCreateSyncKHR) { |
| 1197 | result = cnx->egl.eglCreateSyncKHR(dp->disp.dpy, type, attrib_list); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1198 | } |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1199 | return result; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | EGLBoolean eglDestroySyncKHR(EGLDisplay dpy, EGLSyncKHR sync) |
| 1203 | { |
| 1204 | clearError(); |
| 1205 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1206 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1207 | if (!dp) return EGL_FALSE; |
| 1208 | |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1209 | EGLBoolean result = EGL_FALSE; |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1210 | egl_connection_t* const cnx = &gEGLImpl; |
| 1211 | if (cnx->dso && cnx->egl.eglDestroySyncKHR) { |
| 1212 | result = cnx->egl.eglDestroySyncKHR(dp->disp.dpy, sync); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1213 | } |
| 1214 | return result; |
| 1215 | } |
| 1216 | |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1217 | EGLint eglClientWaitSyncKHR(EGLDisplay dpy, EGLSyncKHR sync, |
| 1218 | EGLint flags, EGLTimeKHR timeout) |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1219 | { |
| 1220 | clearError(); |
| 1221 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1222 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1223 | if (!dp) return EGL_FALSE; |
| 1224 | |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1225 | EGLBoolean result = EGL_FALSE; |
| 1226 | egl_connection_t* const cnx = &gEGLImpl; |
| 1227 | if (cnx->dso && cnx->egl.eglClientWaitSyncKHR) { |
| 1228 | result = cnx->egl.eglClientWaitSyncKHR( |
| 1229 | dp->disp.dpy, sync, flags, timeout); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1230 | } |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1231 | return result; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1234 | EGLBoolean eglGetSyncAttribKHR(EGLDisplay dpy, EGLSyncKHR sync, |
| 1235 | EGLint attribute, EGLint *value) |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1236 | { |
| 1237 | clearError(); |
| 1238 | |
Jesse Hall | b29e5e8 | 2012-04-04 16:53:42 -0700 | [diff] [blame] | 1239 | const egl_display_ptr dp = validate_display(dpy); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1240 | if (!dp) return EGL_FALSE; |
| 1241 | |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1242 | EGLBoolean result = EGL_FALSE; |
| 1243 | egl_connection_t* const cnx = &gEGLImpl; |
| 1244 | if (cnx->dso && cnx->egl.eglGetSyncAttribKHR) { |
| 1245 | result = cnx->egl.eglGetSyncAttribKHR( |
| 1246 | dp->disp.dpy, sync, attribute, value); |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1247 | } |
Mathias Agopian | 7c0441a | 2012-02-14 17:14:36 -0800 | [diff] [blame] | 1248 | return result; |
Mathias Agopian | 518ec11 | 2011-05-13 16:21:08 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | // ---------------------------------------------------------------------------- |
| 1252 | // ANDROID extensions |
| 1253 | // ---------------------------------------------------------------------------- |
| 1254 | |
Jamie Gennis | 331841b | 2012-09-06 14:52:00 -0700 | [diff] [blame] | 1255 | EGLint eglDupNativeFenceFDANDROID(EGLDisplay dpy, EGLSyncKHR sync) |
| 1256 | { |
| 1257 | clearError(); |
| 1258 | |
| 1259 | const egl_display_ptr dp = validate_display(dpy); |
| 1260 | if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID; |
| 1261 | |
| 1262 | EGLint result = EGL_NO_NATIVE_FENCE_FD_ANDROID; |
| 1263 | egl_connection_t* const cnx = &gEGLImpl; |
| 1264 | if (cnx->dso && cnx->egl.eglDupNativeFenceFDANDROID) { |
| 1265 | result = cnx->egl.eglDupNativeFenceFDANDROID(dp->disp.dpy, sync); |
| 1266 | } |
| 1267 | return result; |
| 1268 | } |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1269 | |
Jamie Gennis | 010dd4f | 2012-09-09 17:46:17 -0700 | [diff] [blame] | 1270 | EGLint eglWaitSyncANDROID(EGLDisplay dpy, EGLSyncKHR sync, EGLint flags) |
| 1271 | { |
| 1272 | clearError(); |
| 1273 | |
| 1274 | const egl_display_ptr dp = validate_display(dpy); |
| 1275 | if (!dp) return EGL_NO_NATIVE_FENCE_FD_ANDROID; |
| 1276 | |
| 1277 | EGLint result = EGL_FALSE; |
| 1278 | egl_connection_t* const cnx = &gEGLImpl; |
| 1279 | if (cnx->dso && cnx->egl.eglWaitSyncANDROID) { |
| 1280 | result = cnx->egl.eglWaitSyncANDROID(dp->disp.dpy, sync, flags); |
| 1281 | } |
| 1282 | return result; |
| 1283 | } |
| 1284 | |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1285 | // ---------------------------------------------------------------------------- |
| 1286 | // NVIDIA extensions |
| 1287 | // ---------------------------------------------------------------------------- |
| 1288 | EGLuint64NV eglGetSystemTimeFrequencyNV() |
| 1289 | { |
| 1290 | clearError(); |
| 1291 | |
| 1292 | if (egl_init_drivers() == EGL_FALSE) { |
| 1293 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1294 | } |
| 1295 | |
| 1296 | EGLuint64NV ret = 0; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1297 | egl_connection_t* const cnx = &gEGLImpl; |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1298 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1299 | if (cnx->dso && cnx->egl.eglGetSystemTimeFrequencyNV) { |
| 1300 | return cnx->egl.eglGetSystemTimeFrequencyNV(); |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1301 | } |
| 1302 | |
Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 1303 | return setErrorQuiet(EGL_BAD_DISPLAY, 0); |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | EGLuint64NV eglGetSystemTimeNV() |
| 1307 | { |
| 1308 | clearError(); |
| 1309 | |
| 1310 | if (egl_init_drivers() == EGL_FALSE) { |
| 1311 | return setError(EGL_BAD_PARAMETER, EGL_FALSE); |
| 1312 | } |
| 1313 | |
| 1314 | EGLuint64NV ret = 0; |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1315 | egl_connection_t* const cnx = &gEGLImpl; |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1316 | |
Mathias Agopian | ada798b | 2012-02-13 17:09:30 -0800 | [diff] [blame] | 1317 | if (cnx->dso && cnx->egl.eglGetSystemTimeNV) { |
| 1318 | return cnx->egl.eglGetSystemTimeNV(); |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1319 | } |
| 1320 | |
Mathias Agopian | 0e8bbee | 2011-10-05 19:15:05 -0700 | [diff] [blame] | 1321 | return setErrorQuiet(EGL_BAD_DISPLAY, 0); |
Jonas Yang | 1c3d72a | 2011-08-26 20:04:39 +0800 | [diff] [blame] | 1322 | } |